diff --git a/packages/opencode/src/cli/cmd/attach.ts b/packages/opencode/src/cli/cmd/attach.ts index 0d06bd7c41..fbdbaa2581 100644 --- a/packages/opencode/src/cli/cmd/attach.ts +++ b/packages/opencode/src/cli/cmd/attach.ts @@ -3,8 +3,6 @@ import { UI } from "@/cli/ui" import { errorMessage } from "@opencode-ai/tui/util/error" import { validateSession } from "../tui/validate-session" import { ServerAuth } from "@/server/auth" -import { OpenCode } from "@opencode-ai/client/promise" -import { createOpencodeClient } from "@opencode-ai/sdk/v2" export const AttachCommand = cmd({ command: "attach ", @@ -63,7 +61,9 @@ export const AttachCommand = cmd({ return } - const headers = ServerAuth.headers({ password: args.password, username: args.username }) + const credentials = { password: args.password, username: args.username } + const headers = ServerAuth.headers(credentials) + const endpoint = ServerAuth.endpoint(args.url, credentials) const config = await TuiConfig.get() try { @@ -84,9 +84,7 @@ export const AttachCommand = cmd({ const { createLegacyTuiPluginHost } = await import("@/plugin/tui/runtime") await Effect.runPromise( run({ - // @ts-expect-error V1 does not consume the V2-only server input. - client: createOpencodeClient({ baseUrl: args.url, headers, directory }), - api: OpenCode.make({ baseUrl: args.url, headers }), + server: { endpoint }, config, pluginHost: createLegacyTuiPluginHost(), args: { diff --git a/packages/opencode/src/cli/cmd/tui.ts b/packages/opencode/src/cli/cmd/tui.ts index 79e21ed6bf..06c448a521 100644 --- a/packages/opencode/src/cli/cmd/tui.ts +++ b/packages/opencode/src/cli/cmd/tui.ts @@ -8,8 +8,6 @@ import { errorMessage } from "@opencode-ai/tui/util/error" import { withTimeout } from "@/util/timeout" import { withNetworkOptions, resolveNetworkOptionsNoConfig, hasArg } from "@/cli/network" import { Filesystem } from "@/util/filesystem" -import { OpenCode } from "@opencode-ai/client/promise" -import { createOpencodeClient } from "@opencode-ai/sdk/v2" import { writeHeapSnapshot } from "v8" import { ServerAuth } from "@/server/auth" import { validateSession } from "../tui/validate-session" @@ -135,6 +133,7 @@ export const TuiThreadCommand = cmd({ const external = hasArg("--port") || hasArg("--hostname") || network.mdns === true const headers = external ? ServerAuth.headers() : undefined const url = (await client.call("server", network)).url + const endpoint = external ? ServerAuth.endpoint(url) : { url } try { await validateSession({ @@ -157,11 +156,9 @@ export const TuiThreadCommand = cmd({ const { Effect } = await import("effect") const { run } = await import("../tui/layer") const { createLegacyTuiPluginHost } = await import("@/plugin/tui/runtime") - await Effect.runPromise( + await Effect.runPromise( run({ - // @ts-expect-error V1 does not consume the V2-only server input. - client: createOpencodeClient({ baseUrl: url, headers, directory: cwd }), - api: OpenCode.make({ baseUrl: url, headers }), + server: { endpoint }, async onSnapshot() { const tui = writeHeapSnapshot("tui.heapsnapshot") const server = await client.call("snapshot", undefined) diff --git a/packages/opencode/src/server/auth.ts b/packages/opencode/src/server/auth.ts index 9630ddbe20..8022269de4 100644 --- a/packages/opencode/src/server/auth.ts +++ b/packages/opencode/src/server/auth.ts @@ -1,6 +1,7 @@ export * as ServerAuth from "./auth" import { ConfigService } from "@/effect/config-service" +import { Service } from "@opencode-ai/client/effect" import { Flag } from "@opencode-ai/core/flag/flag" import { Config as EffectConfig, Context, Option, Redacted } from "effect" @@ -46,3 +47,12 @@ export function headers(credentials?: Credentials) { if (!authorization) return undefined return { Authorization: authorization } } + +export function endpoint(url: string, credentials?: Credentials): Service.Endpoint { + const password = credentials?.password ?? Flag.OPENCODE_SERVER_PASSWORD + const username = credentials?.username ?? Flag.OPENCODE_SERVER_USERNAME ?? "opencode" + return { + url, + auth: password ? { type: "basic", username, password } : undefined, + } +} diff --git a/packages/opencode/test/server/auth.test.ts b/packages/opencode/test/server/auth.test.ts index 1278e8c72e..859ee0f167 100644 --- a/packages/opencode/test/server/auth.test.ts +++ b/packages/opencode/test/server/auth.test.ts @@ -20,6 +20,10 @@ describe("ServerAuth", () => { expect(ServerAuth.header()).toBeUndefined() expect(ServerAuth.headers()).toBeUndefined() + expect(ServerAuth.endpoint("http://localhost:4096")).toEqual({ + url: "http://localhost:4096", + auth: undefined, + }) }) test("defaults to the opencode username", () => { @@ -47,6 +51,10 @@ describe("ServerAuth", () => { expect(ServerAuth.headers({ password: "cli-secret", username: "bob" })).toEqual({ Authorization: `Basic ${Buffer.from("bob:cli-secret").toString("base64")}`, }) + expect(ServerAuth.endpoint("https://example.test", { password: "cli-secret", username: "bob" })).toEqual({ + url: "https://example.test", + auth: { type: "basic", username: "bob", password: "cli-secret" }, + }) }) test("validates decoded credentials against effect config", () => {