02f2725154
Co-authored-by: Aiden Cline <rekram1-node@users.noreply.github.com> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Nabs <nabil@instafork.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Victor Navarro <vn4varro@gmail.com> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com> Co-authored-by: AidenGeunGeun <eastlandwyvern@gmail.com> Co-authored-by: Mark <geraint0923@users.noreply.github.com> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: Adam <2363879+adamdotdevin@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: David Hill <1879069+iamdavidhill@users.noreply.github.com> Co-authored-by: Jay <air@live.ca> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: BB84 <110078428+BB-84C@users.noreply.github.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Sebastian <hasta84@gmail.com> Co-authored-by: Jérôme Benoit <jerome.benoit@sap.com> Co-authored-by: Test User <test@test.com> Co-authored-by: Simon Klee <hello@simonklee.dk> Co-authored-by: Rahul A Mistry <149420892+ProdigyRahul@users.noreply.github.com> Co-authored-by: Qiping Li <liqiping1991@gmail.com> Co-authored-by: liqiping <liqiping@msh.team> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Matthias Reso <13337103+mreso@users.noreply.github.com> Co-authored-by: tobwen <1864057+tobwen@users.noreply.github.com> Co-authored-by: Daniel Polito <danielbpolito@gmail.com> Co-authored-by: opencode <noreply@opencode.ai>
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { createOpencodeClient } from "@opencode-ai/sdk/v2/client"
|
|
import { OpenCode, type OpenCodeClient } from "@opencode-ai/client/promise"
|
|
import type { ServerConnection } from "@/context/server"
|
|
import { decode64 } from "@/utils/base64"
|
|
|
|
export function authTokenFromCredentials(input: { username?: string; password: string }) {
|
|
return btoa(`${input.username ?? "opencode"}:${input.password}`)
|
|
}
|
|
|
|
export function authFromToken(token: string | null) {
|
|
const decoded = decode64(token ?? undefined)
|
|
if (!decoded) return
|
|
const separator = decoded.indexOf(":")
|
|
if (separator === -1) return
|
|
return {
|
|
username: decoded.slice(0, separator) || "opencode",
|
|
password: decoded.slice(separator + 1),
|
|
}
|
|
}
|
|
|
|
export function createSdkForServer({
|
|
server,
|
|
...config
|
|
}: Omit<NonNullable<Parameters<typeof createOpencodeClient>[0]>, "baseUrl"> & {
|
|
server: ServerConnection.HttpBase
|
|
}) {
|
|
const auth = (() => {
|
|
if (!server.password) return
|
|
return {
|
|
Authorization: `Basic ${authTokenFromCredentials({ username: server.username, password: server.password })}`,
|
|
}
|
|
})()
|
|
|
|
return createOpencodeClient({
|
|
...config,
|
|
headers: {
|
|
...(config.headers instanceof Headers ? Object.fromEntries(config.headers.entries()) : config.headers),
|
|
...auth,
|
|
},
|
|
baseUrl: server.url,
|
|
})
|
|
}
|
|
|
|
export function createApiForServer(input: {
|
|
server: ServerConnection.HttpBase
|
|
fetch?: typeof globalThis.fetch
|
|
}): OpenCodeClient {
|
|
return OpenCode.make({
|
|
baseUrl: input.server.url,
|
|
fetch: input.fetch,
|
|
headers: input.server.password
|
|
? {
|
|
Authorization: `Basic ${authTokenFromCredentials({
|
|
username: input.server.username,
|
|
password: input.server.password,
|
|
})}`,
|
|
}
|
|
: undefined,
|
|
})
|
|
}
|
|
|
|
export type ServerApi = OpenCodeClient
|