import dotenv from "dotenv"; dotenv.config(); type Config = { /** The port the proxy server will listen on. */ port: number; /** OpenAI API key, either a single key or a base64-encoded JSON array of key configs. */ openaiKey?: string; openaiProxy?: string; /** Proxy key. If set, requests must provide this key in the Authorization header to use the proxy. */ proxyKey?: string; promptInject?: string; /** Per-IP limit for requests per minute to OpenAI's completions endpoint. */ modelRateLimit: number; /** Max number of tokens to generate. Requests which specify a higher value will be rewritten to use this value. */ maxOutputTokens: number; /** Whether requests containing disallowed characters should be rejected. */ rejectDisallowed?: boolean; /** Rejection sample rate (0 - 1). Higher values are more strict but increase server load. */ rejectSampleRate?: number; /** Message to return when rejecting requests. */ rejectMessage?: string; /** Logging threshold. */ logLevel?: "debug" | "info" | "warn" | "error"; /** Whether prompts and responses should be logged. */ logPrompts?: boolean; // TODO keyPassword: string; proxyCaptcha?: boolean; }; export const config: Config = { port: getEnvWithDefault("PORT", 7860), openaiKey: getEnvWithDefault("OPENAI_KEY", ""), openaiProxy: getEnvWithDefault("OPENAI_PROXY", ""), proxyKey: getEnvWithDefault("PROXY_KEY", ""), promptInject: getEnvWithDefault("PROMPT_INJECT", ""), keyPassword: getEnvWithDefault("KEY_PASSWORD", ""), modelRateLimit: getEnvWithDefault("MODEL_RATE_LIMIT", 3), maxOutputTokens: getEnvWithDefault("MAX_OUTPUT_TOKENS", 300), proxyCaptcha: getEnvWithDefault("PROXY_CAPTCHA", false), rejectDisallowed: getEnvWithDefault("REJECT_DISALLOWED", true), rejectSampleRate: getEnvWithDefault("REJECT_SAMPLE_RATE", 0.2), rejectMessage: getEnvWithDefault("REJECT_MESSAGE", "Кыш отсюдова"), logLevel: getEnvWithDefault("LOG_LEVEL", "info"), logPrompts: getEnvWithDefault("LOG_PROMPTS", false), } as const; export const SENSITIVE_KEYS: (keyof Config)[] = ["proxyKey", "openaiKey", "keyPassword", "promptInject", "openaiProxy"]; const getKeys = Object.keys as (obj: T) => Array; export function listConfig(): Record { const result: Record = {}; for (const key of getKeys(config)) { const value = config[key]?.toString() || ""; if (!SENSITIVE_KEYS.includes(key)) { result[key] = value; } } return result; } function getEnvWithDefault(name: string, defaultValue: T): T { const value = process.env[name]; if (value === undefined) { return defaultValue; } try { if (name === "OPENAI_KEY") { return value as unknown as T; } return JSON.parse(value) as T; } catch (err) { return value as unknown as T; } }