import { randomUUID } from 'crypto'; import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs'; import { join } from 'path'; import { markSelfWrite } from './lib/config-watcher.js'; export type ProviderType = 'openai' | 'anthropic' | 'response-api'; export * from './lib/password.js'; export * from './lib/paths.js'; export * from './privacy/types.js'; /** * 模型使用限制配置 */ export interface ModelLimit { type: 'requests' | 'input_tokens' | 'cost'; period: 'day' | 'hours' | 'week' | 'month'; periodValue?: number; // 当 period='hours' 时,指定小时数 max: number; // 最大限制值 } export interface ApiKey { id: string; name: string; key: string; createdAt: number; updatedAt: number; } export interface ProviderConfig { customModel: string; realModel: string; apiKey: string; baseUrl: string; provider: ProviderType; desc?: string; inputPricePer1M?: number; // 输入 token 每百万价格(美元) outputPricePer1M?: number; // 输出 token 每百万价格(美元) cachedPricePer1M?: number; // 缓存 token 每百万价格(美元) limits?: ModelLimit[]; // 使用限制配置 hidden?: boolean; // 是否隐藏该模型(不对外展示) maxContextLength?: number; // 最大上下文窗口(token 数),未配置时默认 200000 defaultParams?: Record; // 默认参数配置 proxy?: string; // HTTP/HTTPS 代理地址,如 http://127.0.0.1:7890,空则不代理 } /** * 用户 API Key 配置 */ export interface UserApiKey { name: string; apikey: string; desc?: string; /** 允许使用的模型列表(customModel 名称)。空或 undefined 表示不限制 */ allowedModels?: string[]; } /** * 模型组配置 */ export interface ModelGroup { name: string; models: string[]; desc?: string; } /** * Privacy protection settings */ export interface PrivacySettings { enabled: boolean; stripUserField: boolean; sanitizeFilePaths: boolean; pathPlaceholder: string; whitelistFilter: boolean; } /** * UI 配置设置 */ export interface UiSettings { enabled?: boolean; // 是否启用公告,默认 false announcementMarkdown?: string; // Markdown 原始内容,可选 } export interface ProxyConfig { models: ProviderConfig[]; modelGroups?: ModelGroup[]; adminPassword?: string; // SHA256 哈希值 apiKeys?: ApiKey[]; userApiKeys?: UserApiKey[]; privacySettings?: PrivacySettings; uiSettings?: UiSettings; // 新增 UI 设置 detailLogEnabled?: boolean; // 后台开关:记录完整请求/响应内容(与 --debug 取 OR,--debug 优先级高) } const REQUIRED_FIELDS = ['customModel', 'realModel', 'apiKey', 'baseUrl', 'provider'] as const; /** * 验证配置项是否包含所有必需字段 */ function validateProviderConfig(config: any, index: number): void { for (const field of REQUIRED_FIELDS) { if (!config[field]) { throw new Error(`Missing required field: ${field} at index ${index}`); } } } /** * 验证 ModelLimit 配置 */ function validateModelLimit(limit: any, index: number, modelIndex: number): void { const validTypes = ['requests', 'input_tokens', 'cost']; const validPeriods = ['day', 'hours', 'week', 'month']; if (!validTypes.includes(limit.type)) { throw new Error(`Invalid limit type "${limit.type}" at model ${modelIndex}, limit ${index}. Valid types: ${validTypes.join(', ')}`); } if (!validPeriods.includes(limit.period)) { throw new Error(`Invalid limit period "${limit.period}" at model ${modelIndex}, limit ${index}. Valid periods: ${validPeriods.join(', ')}`); } if (limit.period === 'hours' && typeof limit.periodValue !== 'number') { throw new Error(`Missing periodValue for hours period at model ${modelIndex}, limit ${index}`); } if (typeof limit.max !== 'number') { throw new Error(`Missing or invalid max value at model ${modelIndex}, limit ${index}`); } } /** * 验证 models 数组 */ function validateModelsArray(models: any): ProviderConfig[] { if (!Array.isArray(models)) { throw new Error('models must be an array'); } models.forEach((item: any, index: number) => { validateProviderConfig(item, index); // 验证 limits if (item.limits) { if (!Array.isArray(item.limits)) { throw new Error(`limits must be an array at model ${index}`); } item.limits.forEach((limit: any, limitIndex: number) => { validateModelLimit(limit, limitIndex, index); }); } // 验证 defaultParams if (item.defaultParams !== undefined) { if (typeof item.defaultParams !== 'object' || Array.isArray(item.defaultParams) || item.defaultParams === null) { throw new Error(`defaultParams must be an object at model ${index}`); } } // 验证 proxy(可选):必须是字符串 if (item.proxy !== undefined && typeof item.proxy !== 'string') { throw new Error(`proxy must be a string at model ${index}`); } }); // 补全 maxContextLength 默认值 models.forEach((item: any) => { if (item.maxContextLength == null) { item.maxContextLength = 200000; } }); return models as ProviderConfig[]; } /** * 验证 ModelGroup 配置 */ function validateModelGroups( modelGroups: any[], models: ProviderConfig[] ): ModelGroup[] { const modelNames = new Set(models.map(m => m.customModel)); const groupNames = new Set(); modelGroups.forEach((group: any, index: number) => { if (!group.name || typeof group.name !== 'string') { throw new Error(`Model group at index ${index} must have a name`); } if (groupNames.has(group.name)) { throw new Error(`Duplicate model group name: "${group.name}"`); } groupNames.add(group.name); if (!Array.isArray(group.models) || group.models.length === 0) { throw new Error(`Model group "${group.name}" models array cannot be empty`); } group.models.forEach((modelName: string) => { if (!modelNames.has(modelName)) { throw new Error(`Model "${modelName}" in group "${group.name}" not found`); } }); }); return modelGroups as ModelGroup[]; } /** * 加载并验证配置文件(返回 models 数组) */ export function loadConfig(configPath: string): ProviderConfig[] { if (!existsSync(configPath)) { throw new Error(`Config file not found: ${configPath}`); } const content = readFileSync(configPath, 'utf-8'); let config: any; try { config = JSON.parse(content); } catch (error) { throw new Error(`Invalid JSON in config file: ${error}`); } // 向后兼容:如果配置是数组,自动转换为新格式 if (Array.isArray(config)) { config.forEach((item: any, index: number) => { validateProviderConfig(item, index); }); return config as ProviderConfig[]; } // 新格式:对象,包含 models 数组 if (typeof config === 'object' && config !== null) { if (!config.models) { throw new Error('Config must have a "models" array'); } return validateModelsArray(config.models); } throw new Error('Config must be an array or an object with "models" array'); } /** * 加载完整配置(包括 adminPassword) */ export function loadFullConfig(configPath: string): ProxyConfig { if (!existsSync(configPath)) { throw new Error(`Config file not found: ${configPath}`); } const content = readFileSync(configPath, 'utf-8'); let config: any; try { config = JSON.parse(content); } catch (error) { throw new Error(`Invalid JSON in config file: ${error}`); } // 向后兼容:如果配置是数组,自动转换为新格式 if (Array.isArray(config)) { config.forEach((item: any, index: number) => { validateProviderConfig(item, index); }); return { models: config, apiKeys: [] }; } // 新格式:对象,包含 models 数组 if (typeof config === 'object' && config !== null) { if (!config.models) { throw new Error('Config must have a "models" array'); } validateModelsArray(config.models); if (config.modelGroups) { validateModelGroups(config.modelGroups, config.models); } return { models: config.models, modelGroups: config.modelGroups, adminPassword: config.adminPassword, apiKeys: config.apiKeys || [], userApiKeys: config.userApiKeys, privacySettings: config.privacySettings, uiSettings: config.uiSettings, detailLogEnabled: config.detailLogEnabled }; } throw new Error('Config must be an array or an object with "models" array'); } /** * 根据 customModel 查找 provider */ export function findProvider(config: ProviderConfig[], model: string): ProviderConfig | null { return config.find(p => p.customModel === model) || null; } /** * 保存配置到文件 */ export function saveConfig(config: ProxyConfig, configPath: string): void { const dir = join(configPath, '..'); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true }); } markSelfWrite(); writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8'); } /** * 保存配置到文件(旧版本,保持向后兼容) * @deprecated 请使用 saveConfig(config, configPath) */ export function saveConfigLegacy(configPath: string, config: ProviderConfig[], adminPassword?: string, apiKeys?: ApiKey[]): void { const dir = join(configPath, '..'); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true }); } const proxyConfig: ProxyConfig = { models: config }; if (adminPassword) { proxyConfig.adminPassword = adminPassword; } if (apiKeys) { proxyConfig.apiKeys = apiKeys; } writeFileSync(configPath, JSON.stringify(proxyConfig, null, 2), 'utf-8'); } /** * 创建默认空配置 */ export function createDefaultConfig(configPath: string): void { const defaultConfig: ProviderConfig[] = []; const proxyConfig: ProxyConfig = { models: defaultConfig }; saveConfig(proxyConfig, configPath); } /** * 更新配置项 */ export function updateConfigEntry( config: ProviderConfig[], oldCustomModel: string, newEntry: ProviderConfig ): ProviderConfig[] { const index = config.findIndex(p => p.customModel === oldCustomModel); if (index === -1) { throw new Error(`未找到模型:${oldCustomModel}`); } const newConfig = [...config]; newConfig[index] = newEntry; return newConfig; } /** * 删除配置项 */ export function deleteConfigEntry( config: ProviderConfig[], customModel: string ): ProviderConfig[] { const index = config.findIndex(p => p.customModel === customModel); if (index === -1) { throw new Error(`未找到模型:${customModel}`); } return config.filter(p => p.customModel !== customModel); } /** * 生成 UUID */ function generateId(): string { return randomUUID(); } /** * 添加 API Key */ export function addApiKey( config: ApiKey[], name: string, key: string ): ApiKey { if (config.some(k => k.name === name)) { throw new Error(`API Key name "${name}" already exists`); } const now = Date.now(); const newKey: ApiKey = { id: generateId(), name, key, createdAt: now, updatedAt: now }; return newKey; } /** * 更新 API Key */ export function updateApiKey( config: ApiKey[], id: string, updates: Partial> ): ApiKey[] { const index = config.findIndex(k => k.id === id); if (index === -1) { throw new Error(`API Key not found: ${id}`); } if (updates.name && updates.name !== config[index].name) { if (config.some(k => k.name === updates.name && k.id !== id)) { throw new Error(`API Key name "${updates.name}" already exists`); } } const updated = { ...config[index], ...updates, updatedAt: Date.now() }; const newConfig = [...config]; newConfig[index] = updated; return newConfig; } /** * 删除 API Key */ export function deleteApiKey(config: ApiKey[], id: string): ApiKey[] { const index = config.findIndex(k => k.id === id); if (index === -1) { throw new Error(`API Key not found: ${id}`); } return config.filter(k => k.id !== id); } /** * 获取单个 API Key */ export function getApiKey(config: ApiKey[], id: string): ApiKey | null { return config.find(k => k.id === id) || null; } /** * API Key 列表/选项展示类型(不返回 key 明文,只返回脱敏提示) */ export interface ApiKeyOption extends Omit { keyHint: string; } /** * 脱敏显示 API Key:前面显示 *****,只露出最后 4 位 */ export function maskApiKey(key: string): string { if (!key) return '*****'; const last4 = key.slice(-4); return `*****${last4}`; } /** * 获取下拉选项/列表展示(不返回 key 本身,只返回 keyHint 脱敏提示) */ export function getApiKeyOptions(config: ApiKey[]): ApiKeyOption[] { return config.map(({ key, ...rest }) => ({ ...rest, keyHint: maskApiKey(key) })); } export function isApiKeyRef(apiKey: string): boolean { return /^\$\$(.+)\$\$$/.test(apiKey); } export function getApiKeyRefName(apiKey: string): string | null { const match = apiKey.match(/^\$\$(.+)\$\$$/); return match ? match[1] : null; } export function resolveApiKey(apiKey: string, apiKeys: ApiKey[]): string { const name = getApiKeyRefName(apiKey); if (name === null) return apiKey; const found = apiKeys.find(k => k.name === name); if (!found) { throw new Error(`API Key reference $$${name}$$ not found in saved API keys`); } return found.key; }