137 lines
3.5 KiB
TypeScript
137 lines
3.5 KiB
TypeScript
import { existsSync, readFileSync } from "node:fs";
|
|
import path from "node:path";
|
|
|
|
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: 8080,
|
|
},
|
|
proxy: {
|
|
apiKey: "dev-proxy-key",
|
|
},
|
|
providers: {
|
|
openai: {},
|
|
anthropic: {},
|
|
},
|
|
logging: {
|
|
level: "info",
|
|
pretty: true,
|
|
},
|
|
};
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
function stringValue(value: unknown): string | undefined {
|
|
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
}
|
|
|
|
function booleanValue(value: unknown): boolean | undefined {
|
|
return typeof value === "boolean" ? value : undefined;
|
|
}
|
|
|
|
function numberValue(value: unknown): number | undefined {
|
|
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
}
|
|
|
|
function providerConfig(value: unknown): ProviderConfig {
|
|
if (!isRecord(value)) return {};
|
|
return {
|
|
...(stringValue(value["baseURL"]) ? { baseURL: stringValue(value["baseURL"]) } : {}),
|
|
...(stringValue(value["apiKey"]) ? { apiKey: stringValue(value["apiKey"]) } : {}),
|
|
};
|
|
}
|
|
|
|
function mergeConfig(raw: unknown): AppConfig {
|
|
if (!isRecord(raw)) return DEFAULT_CONFIG;
|
|
|
|
const server = isRecord(raw["server"]) ? raw["server"] : {};
|
|
const proxy = isRecord(raw["proxy"]) ? raw["proxy"] : {};
|
|
const providers = isRecord(raw["providers"]) ? raw["providers"] : {};
|
|
const logging = isRecord(raw["logging"]) ? raw["logging"] : {};
|
|
|
|
const port = numberValue(server["port"]) ?? DEFAULT_CONFIG.server.port;
|
|
if (!Number.isInteger(port) || port <= 0) {
|
|
throw new Error(`Invalid config value "server.port": ${port}`);
|
|
}
|
|
|
|
return {
|
|
server: {
|
|
host: stringValue(server["host"]) ?? DEFAULT_CONFIG.server.host,
|
|
port,
|
|
},
|
|
proxy: {
|
|
apiKey: stringValue(proxy["apiKey"]) ?? DEFAULT_CONFIG.proxy.apiKey,
|
|
},
|
|
providers: {
|
|
openai: providerConfig(providers["openai"]),
|
|
anthropic: providerConfig(providers["anthropic"]),
|
|
},
|
|
logging: {
|
|
level: stringValue(logging["level"]) ?? DEFAULT_CONFIG.logging.level,
|
|
pretty: booleanValue(logging["pretty"]) ?? DEFAULT_CONFIG.logging.pretty,
|
|
},
|
|
};
|
|
}
|
|
|
|
function configPathFromArgv(): string {
|
|
const configArgIndex = process.argv.findIndex((arg) => arg === "--config");
|
|
if (configArgIndex >= 0) {
|
|
const value = process.argv[configArgIndex + 1];
|
|
if (!value) {
|
|
throw new Error("Missing value after --config");
|
|
}
|
|
return path.resolve(value);
|
|
}
|
|
|
|
const inlineArg = process.argv.find((arg) => arg.startsWith("--config="));
|
|
if (inlineArg) {
|
|
return path.resolve(inlineArg.slice("--config=".length));
|
|
}
|
|
|
|
return path.resolve("config.json");
|
|
}
|
|
|
|
function loadConfig(): AppConfig {
|
|
const filePath = configPathFromArgv();
|
|
if (!existsSync(filePath)) {
|
|
return DEFAULT_CONFIG;
|
|
}
|
|
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(readFileSync(filePath, "utf8"));
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
throw new Error(`Could not read config file ${filePath}: ${message}`);
|
|
}
|
|
|
|
return mergeConfig(parsed);
|
|
}
|
|
|
|
export const config = loadConfig();
|