From ef5c74e972a99501adaa540813c1f9094945745c Mon Sep 17 00:00:00 2001 From: William Obino Date: Sat, 18 Apr 2026 17:49:05 +0300 Subject: [PATCH] fix(cli): inject server auth for attach clients --- src/cli/run/server-connection.ts | 5 +++++ src/shared/opencode-server-auth.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cli/run/server-connection.ts b/src/cli/run/server-connection.ts index bf658ff05..f4af3ddcd 100644 --- a/src/cli/run/server-connection.ts +++ b/src/cli/run/server-connection.ts @@ -1,6 +1,7 @@ import { createOpencode, createOpencodeClient } from "@opencode-ai/sdk" import pc from "picocolors" import type { ServerConnection } from "./types" +import { injectServerAuthIntoClient } from "../../shared/opencode-server-auth" import { getAvailableServerPort, isPortAvailable, DEFAULT_SERVER_PORT } from "../../shared/port-utils" import { withWorkingOpencodePath } from "./opencode-binary-resolver" @@ -40,6 +41,7 @@ export async function createServerConnection(options: { if (attach !== undefined) { console.log(pc.dim("Attaching to existing server at"), pc.cyan(attach)) const client = createOpencodeClient({ baseUrl: attach }) + injectServerAuthIntoClient(client) return { client, cleanup: () => {} } } @@ -66,12 +68,14 @@ export async function createServerConnection(options: { console.log(pc.dim("Port"), pc.cyan(port.toString()), pc.dim("became occupied, attaching to existing server")) const client = createOpencodeClient({ baseUrl: `http://127.0.0.1:${port}` }) + injectServerAuthIntoClient(client) return { client, cleanup: () => {} } } } console.log(pc.dim("Port"), pc.cyan(port.toString()), pc.dim("is occupied, attaching to existing server")) const client = createOpencodeClient({ baseUrl: `http://127.0.0.1:${port}` }) + injectServerAuthIntoClient(client) return { client, cleanup: () => {} } } @@ -93,6 +97,7 @@ export async function createServerConnection(options: { console.log(pc.dim("Port range exhausted, attaching to existing server on"), pc.cyan(DEFAULT_SERVER_PORT.toString())) const client = createOpencodeClient({ baseUrl: `http://127.0.0.1:${DEFAULT_SERVER_PORT}` }) + injectServerAuthIntoClient(client) return { client, cleanup: () => {} } } diff --git a/src/shared/opencode-server-auth.ts b/src/shared/opencode-server-auth.ts index 8d4957512..02ebfa928 100644 --- a/src/shared/opencode-server-auth.ts +++ b/src/shared/opencode-server-auth.ts @@ -78,7 +78,7 @@ function tryInjectViaInterceptors(internal: UnknownRecord, auth: string): boolea return false } - use((request: Request): Request => { + use.call(requestInterceptors, (request: Request): Request => { if (!request.headers.get("Authorization")) { request.headers.set("Authorization", auth) }