diff --git a/config.example.json b/config.example.json deleted file mode 100644 index d8860b1..0000000 --- a/config.example.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "server": { - "host": "0.0.0.0", - "port": 24731 - }, - "proxy": { - "apiKey": "replace-this-token" - }, - "providers": { - "openai": { - "apiKey": "ai-integrations-openai-api-key", - "baseURL": "https://ai-integrations-openai-base-url/v1" - }, - "anthropic": { - "apiKey": "ai-integrations-anthropic-api-key", - "baseURL": "https://ai-integrations-anthropic-base-url" - } - }, - "logging": { - "level": "info", - "pretty": true - } -} diff --git a/replit.md b/replit.md index 3dd4142..9724657 100644 --- a/replit.md +++ b/replit.md @@ -2,7 +2,7 @@ ## Overview -Standalone TypeScript API server with file-based configuration. +Standalone TypeScript API server with CLI-only deployment configuration. ## Stack @@ -11,16 +11,22 @@ Standalone TypeScript API server with file-based configuration. - **TypeScript version**: 5.9 - **API framework**: Express 5 - **Build**: esbuild -- **Config**: `config.json` by default, or `--config ./path.json` ## Provider Endpoints -Do not configure this proxy with public official provider endpoints. Agents should populate `providers.openai.baseURL` and `providers.anthropic.baseURL` with the internal endpoints supplied by their runtime/integration, along with the matching internal tokens. +Use Replit AI Integrations endpoints through CLI flags: + +- `--openai-base-url` +- `--openai-api-key` +- `--anthropic-base-url` +- `--anthropic-api-key` + +Never use public official provider endpoints. ## Key Commands -- `pnpm dev` — run API server locally -- `pnpm dev -- --config ./config.local.json` — run with a custom config file +- `pnpm install --ignore-workspace` — install dependencies without joining a parent workspace +- `pnpm dev` — run API server locally on port `24731` - `pnpm run typecheck` — typecheck the server - `pnpm run build` — typecheck and build `dist/index.mjs` - `pnpm start` — run the built server diff --git a/src/lib/config.ts b/src/lib/config.ts index 1a52001..a39f6e3 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -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 { - 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); diff --git a/src/routes/proxy.ts b/src/routes/proxy.ts index dbca49b..5240cc9 100644 --- a/src/routes/proxy.ts +++ b/src/routes/proxy.ts @@ -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", }, });