export interface ProviderConfig { baseURL?: string; apiKey?: string; } export interface AppConfig { server: { host: string; port: number; }; proxy: { apiKey: string; }; providers: { openai: ProviderConfig; anthropic: ProviderConfig; }; logging: { level: string; pretty: boolean; }; } const DEFAULT_CONFIG: AppConfig = { server: { host: "0.0.0.0", port: 3000, }, proxy: { apiKey: "dev-proxy-key", }, providers: { openai: {}, anthropic: {}, }, logging: { level: "info", pretty: true, }, }; function argValue(name: string): string | undefined { const longName = `--${name}`; const index = process.argv.findIndex((arg) => arg === longName); if (index >= 0) { const value = process.argv[index + 1]; return value && !value.startsWith("--") ? value : undefined; } const inlineArg = process.argv.find((arg) => arg.startsWith(`${longName}=`)); return inlineArg ? inlineArg.slice(longName.length + 1) : undefined; } function argFlag(name: string): boolean { return process.argv.includes(`--${name}`); } function applyArgOverrides(base: AppConfig): AppConfig { const rawPort = argValue("port"); const port = rawPort === undefined ? base.server.port : Number(rawPort); if (!Number.isInteger(port) || port <= 0) { throw new Error(`Invalid CLI value "--port": ${rawPort}`); } return { server: { host: argValue("host") ?? base.server.host, port, }, proxy: { apiKey: argValue("proxy-api-key") ?? base.proxy.apiKey, }, providers: { openai: { ...base.providers.openai, ...(argValue("openai-base-url") ? { baseURL: argValue("openai-base-url") } : {}), ...(argValue("openai-api-key") ? { apiKey: argValue("openai-api-key") } : {}), }, anthropic: { ...base.providers.anthropic, ...(argValue("anthropic-base-url") ? { baseURL: argValue("anthropic-base-url") } : {}), ...(argValue("anthropic-api-key") ? { apiKey: argValue("anthropic-api-key") } : {}), }, }, logging: { level: argValue("log-level") ?? base.logging.level, pretty: argFlag("pretty-logs") ? true : argFlag("no-pretty-logs") ? false : base.logging.pretty, }, }; } export const config = applyArgOverrides(DEFAULT_CONFIG);