-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Expand file tree
/
Copy pathserver.ts
More file actions
35 lines (31 loc) · 996 Bytes
/
server.ts
File metadata and controls
35 lines (31 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { createOpencodeClient } from "@opencode-ai/sdk/v2/client"
import type { ServerConnection } from "@/context/server"
import { decode64 } from "./base64"
function absolute(dir: string) {
return dir.startsWith("/") || /^[A-Za-z]:[\\/]/.test(dir) || dir.startsWith("\\\\")
}
export function normalizeDirectory(dir?: string) {
if (!dir || absolute(dir)) return dir
const next = decode64(dir)
if (!next || !absolute(next)) return dir
return next
}
export function createSdkForServer({
server,
...config
}: Omit<NonNullable<Parameters<typeof createOpencodeClient>[0]>, "baseUrl"> & {
server: ServerConnection.HttpBase
}) {
const auth = (() => {
if (!server.password) return
return {
Authorization: `Basic ${btoa(`${server.username ?? "opencode"}:${server.password}`)}`,
}
})()
return createOpencodeClient({
...config,
directory: normalizeDirectory(config.directory),
headers: { ...config.headers, ...auth },
baseUrl: server.url,
})
}