Remove config file deployment path

This commit is contained in:
hwhung
2026-04-27 12:37:02 +08:00
parent c5eca5bdb9
commit 934ab43ccc
4 changed files with 13 additions and 124 deletions
+1 -95
View File
@@ -1,6 +1,3 @@
import { existsSync, readFileSync } from "node:fs";
import path from "node:path";
export interface ProviderConfig {
baseURL?: string;
apiKey?: string;
@@ -42,80 +39,6 @@ const DEFAULT_CONFIG: AppConfig = {
},
};
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 argValue(name: string): string | undefined {
const longName = `--${name}`;
const index = process.argv.findIndex((arg) => arg === longName);
@@ -170,21 +93,4 @@ function applyArgOverrides(base: AppConfig): AppConfig {
};
}
function loadConfig(): AppConfig {
const filePath = configPathFromArgv();
if (!existsSync(filePath)) {
return applyArgOverrides(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 applyArgOverrides(mergeConfig(parsed));
}
export const config = loadConfig();
export const config = applyArgOverrides(DEFAULT_CONFIG);
+1 -1
View File
@@ -494,7 +494,7 @@ function errorMessage(err: unknown): string {
function providerNotConfigured(res: Response, provider: "openai" | "anthropic"): void {
res.status(503).json({
error: {
message: `${provider} provider is not configured. Add providers.${provider}.apiKey and providers.${provider}.baseURL, or pass the matching CLI flags. Use Replit AI Integrations endpoints, not public provider endpoints.`,
message: `${provider} provider is not configured. Pass the matching Replit AI Integrations CLI flags, not public provider endpoints.`,
type: "server_error",
},
});