mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-15 17:13:12 +00:00
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { createOpencodeClient } from "@opencode-ai/sdk/v2/client"
|
|
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,
|
|
})
|
|
}
|