mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-01 22:10:23 +00:00
fix(app): more startup perf (#19288)
This commit is contained in:
parent
2e6ac8ff49
commit
c7760b433b
28 changed files with 1012 additions and 568 deletions
|
|
@ -5,6 +5,30 @@ import { type Config } from "./gen/client/types.gen.js"
|
|||
import { OpencodeClient } from "./gen/sdk.gen.js"
|
||||
export { type Config as OpencodeClientConfig, OpencodeClient }
|
||||
|
||||
function pick(value: string | null, fallback?: string) {
|
||||
if (!value) return
|
||||
if (!fallback) return value
|
||||
if (value === fallback) return fallback
|
||||
if (value === encodeURIComponent(fallback)) return fallback
|
||||
return value
|
||||
}
|
||||
|
||||
function rewrite(request: Request, directory?: string) {
|
||||
if (request.method !== "GET" && request.method !== "HEAD") return request
|
||||
|
||||
const value = pick(request.headers.get("x-opencode-directory"), directory)
|
||||
if (!value) return request
|
||||
|
||||
const url = new URL(request.url)
|
||||
if (!url.searchParams.has("directory")) {
|
||||
url.searchParams.set("directory", value)
|
||||
}
|
||||
|
||||
const next = new Request(url, request)
|
||||
next.headers.delete("x-opencode-directory")
|
||||
return next
|
||||
}
|
||||
|
||||
export function createOpencodeClient(config?: Config & { directory?: string }) {
|
||||
if (!config?.fetch) {
|
||||
const customFetch: any = (req: any) => {
|
||||
|
|
@ -26,5 +50,6 @@ export function createOpencodeClient(config?: Config & { directory?: string }) {
|
|||
}
|
||||
|
||||
const client = createClient(config)
|
||||
client.interceptors.request.use((request) => rewrite(request, config?.directory))
|
||||
return new OpencodeClient({ client })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,44 @@ import { type Config } from "./gen/client/types.gen.js"
|
|||
import { OpencodeClient } from "./gen/sdk.gen.js"
|
||||
export { type Config as OpencodeClientConfig, OpencodeClient }
|
||||
|
||||
function pick(value: string | null, fallback?: string, encode?: (value: string) => string) {
|
||||
if (!value) return
|
||||
if (!fallback) return value
|
||||
if (value === fallback) return fallback
|
||||
if (encode && value === encode(fallback)) return fallback
|
||||
return value
|
||||
}
|
||||
|
||||
function rewrite(request: Request, values: { directory?: string; workspace?: string }) {
|
||||
if (request.method !== "GET" && request.method !== "HEAD") return request
|
||||
|
||||
const url = new URL(request.url)
|
||||
let changed = false
|
||||
|
||||
for (const [name, key] of [
|
||||
["x-opencode-directory", "directory"],
|
||||
["x-opencode-workspace", "workspace"],
|
||||
] as const) {
|
||||
const value = pick(
|
||||
request.headers.get(name),
|
||||
key === "directory" ? values.directory : values.workspace,
|
||||
key === "directory" ? encodeURIComponent : undefined,
|
||||
)
|
||||
if (!value) continue
|
||||
if (!url.searchParams.has(key)) {
|
||||
url.searchParams.set(key, value)
|
||||
}
|
||||
changed = true
|
||||
}
|
||||
|
||||
if (!changed) return request
|
||||
|
||||
const next = new Request(url, request)
|
||||
next.headers.delete("x-opencode-directory")
|
||||
next.headers.delete("x-opencode-workspace")
|
||||
return next
|
||||
}
|
||||
|
||||
export function createOpencodeClient(config?: Config & { directory?: string; experimental_workspaceID?: string }) {
|
||||
if (!config?.fetch) {
|
||||
const customFetch: any = (req: any) => {
|
||||
|
|
@ -19,11 +57,9 @@ export function createOpencodeClient(config?: Config & { directory?: string; exp
|
|||
}
|
||||
|
||||
if (config?.directory) {
|
||||
const isNonASCII = /[^\x00-\x7F]/.test(config.directory)
|
||||
const encodedDirectory = isNonASCII ? encodeURIComponent(config.directory) : config.directory
|
||||
config.headers = {
|
||||
...config.headers,
|
||||
"x-opencode-directory": encodedDirectory,
|
||||
"x-opencode-directory": encodeURIComponent(config.directory),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,5 +71,11 @@ export function createOpencodeClient(config?: Config & { directory?: string; exp
|
|||
}
|
||||
|
||||
const client = createClient(config)
|
||||
client.interceptors.request.use((request) =>
|
||||
rewrite(request, {
|
||||
directory: config?.directory,
|
||||
workspace: config?.experimental_workspaceID,
|
||||
}),
|
||||
)
|
||||
return new OpencodeClient({ client })
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue