Add CLI instance proxy and route UI traffic through it

This commit is contained in:
Shantur Rathore
2025-11-19 02:03:15 +00:00
parent defa637dbc
commit 146eae5220
15 changed files with 592 additions and 84 deletions

View File

@@ -1,27 +1,27 @@
import { createOpencodeClient, type OpencodeClient } from "@opencode-ai/sdk/client"
import { CODENOMAD_API_BASE } from "./api-client"
class SDKManager {
private clients = new Map<number, OpencodeClient>()
private clients = new Map<string, OpencodeClient>()
createClient(port: number): OpencodeClient {
if (this.clients.has(port)) {
return this.clients.get(port)!
createClient(instanceId: string, proxyPath: string): OpencodeClient {
if (this.clients.has(instanceId)) {
return this.clients.get(instanceId)!
}
const client = createOpencodeClient({
baseUrl: `http://localhost:${port}`,
})
const baseUrl = buildInstanceBaseUrl(proxyPath)
const client = createOpencodeClient({ baseUrl })
this.clients.set(port, client)
this.clients.set(instanceId, client)
return client
}
getClient(port: number): OpencodeClient | null {
return this.clients.get(port) || null
getClient(instanceId: string): OpencodeClient | null {
return this.clients.get(instanceId) ?? null
}
destroyClient(port: number): void {
this.clients.delete(port)
destroyClient(instanceId: string): void {
this.clients.delete(instanceId)
}
destroyAll(): void {
@@ -29,4 +29,19 @@ class SDKManager {
}
}
function buildInstanceBaseUrl(proxyPath: string): string {
const normalized = normalizeProxyPath(proxyPath)
const base = stripTrailingSlashes(CODENOMAD_API_BASE)
return `${base}${normalized}/`
}
function normalizeProxyPath(proxyPath: string): string {
const withLeading = proxyPath.startsWith("/") ? proxyPath : `/${proxyPath}`
return withLeading.replace(/\/+/g, "/").replace(/\/+$/, "")
}
function stripTrailingSlashes(input: string): string {
return input.replace(/\/+$/, "")
}
export const sdkManager = new SDKManager()