整理 opencode 接入方式,embedded 和 client 模式

This commit is contained in:
2026-05-19 10:27:12 +08:00
parent 69a90de9a1
commit 8b02cae2af
3 changed files with 181 additions and 108 deletions
+36 -13
View File
@@ -105,13 +105,16 @@ export class OpencodeRuntimeAdapter {
}
private async bootstrapClient(): Promise<OpencodeClient> {
if (config.OPENCODE_BASE_URL) {
if (config.OPENCODE_MODE === "client") {
logger.info(
{ baseUrl: config.OPENCODE_BASE_URL },
"connecting to external opencode server",
{
baseUrl: config.OPENCODE_CLIENT_BASE_URL,
mode: config.OPENCODE_MODE,
},
"connecting to opencode server in client mode",
);
return createOpencodeClient({
baseUrl: config.OPENCODE_BASE_URL,
baseUrl: config.OPENCODE_CLIENT_BASE_URL,
});
}
@@ -128,18 +131,29 @@ export class OpencodeRuntimeAdapter {
hostname: config.OPENCODE_HOSTNAME,
port: config.OPENCODE_PORT,
model: config.OPENCODE_MODEL,
mode: config.OPENCODE_MODE,
},
"starting embedded opencode server",
"starting opencode server in embedded mode",
);
const runtime = await createOpencode({
hostname: config.OPENCODE_HOSTNAME,
port: config.OPENCODE_PORT,
timeout: config.OPENCODE_TIMEOUT_MS,
config: {
model: config.OPENCODE_MODEL,
},
});
let runtime;
try {
runtime = await createOpencode({
hostname: config.OPENCODE_HOSTNAME,
port: config.OPENCODE_PORT,
timeout: config.OPENCODE_TIMEOUT_MS,
config: {
model: config.OPENCODE_MODEL,
},
});
} catch (error) {
if (isMissingOpencodeCli(error)) {
throw new Error(
"embedded mode requires the opencode CLI to be installed and available in PATH; otherwise set OPENCODE_MODE=client and provide OPENCODE_CLIENT_BASE_URL",
);
}
throw error;
}
this.closeServer = () => {
runtime.server.close();
@@ -151,6 +165,15 @@ export class OpencodeRuntimeAdapter {
export const opencodeRuntime = new OpencodeRuntimeAdapter();
function isMissingOpencodeCli(error: unknown): error is NodeJS.ErrnoException {
return (
typeof error === "object" &&
error !== null &&
"code" in error &&
(error as NodeJS.ErrnoException).code === "ENOENT"
);
}
function requireData<T>(data: T | undefined, operation: string): T {
if (data === undefined) {
throw new Error(`${operation} returned no data`);