fix(cli): inject server auth for attach clients
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { createOpencode, createOpencodeClient } from "@opencode-ai/sdk"
|
import { createOpencode, createOpencodeClient } from "@opencode-ai/sdk"
|
||||||
import pc from "picocolors"
|
import pc from "picocolors"
|
||||||
import type { ServerConnection } from "./types"
|
import type { ServerConnection } from "./types"
|
||||||
|
import { injectServerAuthIntoClient } from "../../shared/opencode-server-auth"
|
||||||
import { getAvailableServerPort, isPortAvailable, DEFAULT_SERVER_PORT } from "../../shared/port-utils"
|
import { getAvailableServerPort, isPortAvailable, DEFAULT_SERVER_PORT } from "../../shared/port-utils"
|
||||||
import { withWorkingOpencodePath } from "./opencode-binary-resolver"
|
import { withWorkingOpencodePath } from "./opencode-binary-resolver"
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@ export async function createServerConnection(options: {
|
|||||||
if (attach !== undefined) {
|
if (attach !== undefined) {
|
||||||
console.log(pc.dim("Attaching to existing server at"), pc.cyan(attach))
|
console.log(pc.dim("Attaching to existing server at"), pc.cyan(attach))
|
||||||
const client = createOpencodeClient({ baseUrl: attach })
|
const client = createOpencodeClient({ baseUrl: attach })
|
||||||
|
injectServerAuthIntoClient(client)
|
||||||
return { client, cleanup: () => {} }
|
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"))
|
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}` })
|
const client = createOpencodeClient({ baseUrl: `http://127.0.0.1:${port}` })
|
||||||
|
injectServerAuthIntoClient(client)
|
||||||
return { client, cleanup: () => {} }
|
return { client, cleanup: () => {} }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(pc.dim("Port"), pc.cyan(port.toString()), pc.dim("is occupied, attaching to existing server"))
|
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}` })
|
const client = createOpencodeClient({ baseUrl: `http://127.0.0.1:${port}` })
|
||||||
|
injectServerAuthIntoClient(client)
|
||||||
return { client, cleanup: () => {} }
|
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()))
|
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}` })
|
const client = createOpencodeClient({ baseUrl: `http://127.0.0.1:${DEFAULT_SERVER_PORT}` })
|
||||||
|
injectServerAuthIntoClient(client)
|
||||||
return { client, cleanup: () => {} }
|
return { client, cleanup: () => {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ function tryInjectViaInterceptors(internal: UnknownRecord, auth: string): boolea
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
use((request: Request): Request => {
|
use.call(requestInterceptors, (request: Request): Request => {
|
||||||
if (!request.headers.get("Authorization")) {
|
if (!request.headers.get("Authorization")) {
|
||||||
request.headers.set("Authorization", auth)
|
request.headers.set("Authorization", auth)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user