init
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env node
|
||||
import path from "node:path";
|
||||
import { loadConfig } from "./config.js";
|
||||
import { createServer } from "./server.js";
|
||||
|
||||
function configPathFromArgs(args: string[]): string {
|
||||
const index = args.indexOf("--config");
|
||||
if (index === -1) return "config.yaml";
|
||||
const value = args[index + 1];
|
||||
if (!value) throw new Error("--config requires a file path");
|
||||
return value;
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const configPath = path.resolve(configPathFromArgs(process.argv.slice(2)));
|
||||
const config = await loadConfig(configPath);
|
||||
const bridge = await createServer(config);
|
||||
|
||||
if (!bridge.installation.installed) {
|
||||
process.stderr.write(`警告:找不到 ${config.command_code_executable},API 将返回 503。\n`);
|
||||
} else if (!bridge.installation.authenticated) {
|
||||
process.stderr.write("警告:Command Code 未登录。运行 command-code login 后重启服务。\n");
|
||||
}
|
||||
|
||||
await bridge.app.listen({ host: config.host, port: config.port });
|
||||
process.stdout.write(`Command Code OpenAI Bridge 已启动\n`);
|
||||
process.stdout.write(`API: http://${config.host}:${config.port}/v1\n`);
|
||||
process.stdout.write(`工作目录: ${config.resolvedWorkingDirectory}\n`);
|
||||
process.stdout.write(`Command Code: ${bridge.installation.version ?? "unavailable"}\n`);
|
||||
process.stdout.write(`权限模式: ${config.dangerously_skip_permissions ? "yolo" : config.permission_mode}\n`);
|
||||
process.stdout.write("空闲时按 Ctrl+C 停止;请求运行中第一次 Ctrl+C 只取消当前请求。\n");
|
||||
|
||||
let shuttingDown = false;
|
||||
const shutdown = async (signal: NodeJS.Signals) => {
|
||||
if (bridge.abortActive()) {
|
||||
process.stderr.write(`\n收到 ${signal},正在取消当前 Command Code 请求。\n`);
|
||||
return;
|
||||
}
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
process.stdout.write(`\n收到 ${signal},正在停止服务。\n`);
|
||||
await bridge.app.close();
|
||||
process.exitCode = 0;
|
||||
};
|
||||
|
||||
process.on("SIGINT", () => { void shutdown("SIGINT"); });
|
||||
process.on("SIGTERM", () => { void shutdown("SIGTERM"); });
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user