Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/opencode/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ const layer = Layer.effect(
registerAdapter(ctx.project.id, type, adapter as WorkspaceAdapter)
},
},
get serverUrl(): URL {
return Server.url ?? new URL("http://localhost:4096")
get serverUrl() {
return Server.url
},
// @ts-expect-error
$: typeof Bun === "undefined" ? undefined : Bun.$,
Expand Down
88 changes: 88 additions & 0 deletions packages/opencode/test/plugin/server-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import path from "path"
import { pathToFileURL } from "url"
import { Account } from "../../src/account/account"
import { Auth } from "../../src/auth"
import { RuntimeFlags } from "../../src/effect/runtime-flags"
import { Plugin } from "../../src/plugin/index"
import { Server } from "../../src/server/server"
import { TestInstance } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
import { AccountTest } from "../fake/account"
import { AuthTest } from "../fake/auth"
import { NpmTest } from "../fake/npm"
import { Npm } from "@opencode-ai/core/npm"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { ModelV2 } from "@opencode-ai/core/model"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"

const it = testEffect(
AppNodeBuilder.build(LayerNode.group([Plugin.node, CrossSpawnSpawner.node]), [
[Auth.node, AuthTest.empty],
[Account.node, AccountTest.empty],
[Npm.node, NpmTest.noop],
[RuntimeFlags.node, RuntimeFlags.layer({ disableDefaultPlugins: true })],
]),
)

// The plugin reports what it was given through a system transform, so the test reads the
// real PluginInput without reaching into globals.
const source = [
"export default async (input) => ({",
' "experimental.chat.system.transform": async (_input, output) => {',
" const project = await input.client.project.current()",
' output.system.push(String(input.serverUrl), project.data?.worktree === input.worktree ? "client ok" : "client failed")',
" },",
"})",
"",
].join("\n")

const report = Effect.fn("PluginServerUrlTest.report")(function* () {
const test = yield* TestInstance
const file = path.join(test.directory, "plugin.ts")
yield* Effect.promise(() =>
Promise.all([
Bun.write(file, source),
Bun.write(
path.join(test.directory, "opencode.json"),
JSON.stringify({ $schema: "https://opencode.ai/config.json", plugin: [pathToFileURL(file).href] }),
),
]),
)
const plugin = yield* Plugin.Service
const out = { system: [] as string[] }
yield* plugin.trigger(
"experimental.chat.system.transform",
{ model: { providerID: ProviderV2.ID.anthropic, modelID: ModelV2.ID.make("claude-sonnet-4-6") } },
out,
)
return out.system
})

describe("plugin input serverUrl", () => {
it.instance(
"is undefined without an HTTP listener and the client stays in-process",
() =>
Effect.gen(function* () {
expect(Server.url).toBeUndefined()
expect(yield* report()).toEqual(["undefined", "client ok"])
}),
30_000,
)

it.instance(
"is the listener URL when one is running",
() =>
Effect.gen(function* () {
const listener = yield* Effect.acquireRelease(
Effect.promise(() => Server.listen({ hostname: "127.0.0.1", port: 0 })),
(listener) => Effect.promise(() => listener.stop(true)),
)
expect(yield* report()).toEqual([listener.url.toString(), "client ok"])
}),
30_000,
)
})
6 changes: 5 additions & 1 deletion packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export type PluginInput = {
experimental_workspace: {
register(type: string, adapter: WorkspaceAdapter): void
}
serverUrl: URL
/**
* URL of this process's HTTP listener. Undefined when this process has no HTTP listener
* (for example a TUI started without `--port`); use `client`, which is in-process then.
*/
serverUrl?: URL
$: BunShell
}

Expand Down
Loading