111 lines
2.9 KiB
TypeScript
111 lines
2.9 KiB
TypeScript
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: 3001,
|
|
},
|
|
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 envValue(name: string): string | undefined {
|
|
const value = process.env[name];
|
|
return value && value.length > 0 ? value : undefined;
|
|
}
|
|
|
|
function applyArgOverrides(base: AppConfig): AppConfig {
|
|
const rawPort = argValue("port") ?? envValue("PORT");
|
|
const port = rawPort === undefined ? base.server.port : Number(rawPort);
|
|
if (!Number.isInteger(port) || port <= 0) {
|
|
throw new Error(`Invalid port value: ${rawPort}`);
|
|
}
|
|
|
|
const host = argValue("host") ?? envValue("HOST") ?? base.server.host;
|
|
const proxyApiKey = argValue("proxy-api-key") ?? envValue("PROXY_API_KEY") ?? base.proxy.apiKey;
|
|
const openaiBaseURL = argValue("openai-base-url") ?? envValue("AI_INTEGRATIONS_OPENAI_BASE_URL");
|
|
const openaiApiKey = argValue("openai-api-key") ?? envValue("AI_INTEGRATIONS_OPENAI_API_KEY");
|
|
const anthropicBaseURL =
|
|
argValue("anthropic-base-url") ?? envValue("AI_INTEGRATIONS_ANTHROPIC_BASE_URL");
|
|
const anthropicApiKey =
|
|
argValue("anthropic-api-key") ?? envValue("AI_INTEGRATIONS_ANTHROPIC_API_KEY");
|
|
|
|
return {
|
|
server: {
|
|
host,
|
|
port,
|
|
},
|
|
proxy: {
|
|
apiKey: proxyApiKey,
|
|
},
|
|
providers: {
|
|
openai: {
|
|
...base.providers.openai,
|
|
...(openaiBaseURL ? { baseURL: openaiBaseURL } : {}),
|
|
...(openaiApiKey ? { apiKey: openaiApiKey } : {}),
|
|
},
|
|
anthropic: {
|
|
...base.providers.anthropic,
|
|
...(anthropicBaseURL ? { baseURL: anthropicBaseURL } : {}),
|
|
...(anthropicApiKey ? { apiKey: anthropicApiKey } : {}),
|
|
},
|
|
},
|
|
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);
|