import { DEFAULT_BROWSER_CONFIG, DEFAULT_COMPUTER_USE_CONFIG, } from './configDefaults.js'; import type { ModelConfig } from './configFile.js'; import type { ConfigSnapshot } from './configService.js'; export type ManagementStatus = 'unconfigured' | 'configured' | 'incomplete'; export interface ManagedToolView { state: ManagementStatus; configured: boolean; message?: string; } export interface ManagedModelView { id: string; apiFormat: ModelConfig['apiFormat']; displayName?: string; baseURL: string; apiKeyConfigured: boolean; model: string; contextWindow: number; maxTokens?: number; supportsVision?: boolean; reasoningEffort?: ModelConfig['reasoningEffort']; promptCache: NonNullable; enabled: boolean; updatedAt: number; } /** * CLI 和 Desktop 共用的脱敏配置视图。这里不返回任何凭据,也不读取 Runtime * 状态;它只是把 V4 权威配置稳定地投影成可展示、可比较的管理模型。 */ export function buildConfigManagementView(snapshot: ConfigSnapshot) { const config = snapshot.config; if (!config) return null; const drafts = config.drafts?.tools ?? {}; const stateFor = (key: keyof typeof config.tools): ManagedToolView => { const configured = !!config.tools[key]; if (drafts[key] && Object.keys(drafts[key]!).length > 0) { return { state: 'incomplete', configured, message: '配置尚未完整,已安全保存为草稿', }; } return { state: configured ? 'configured' : 'unconfigured', configured }; }; const baseModel = config.models.find( (model) => model.id === config.baseModelId, ); const advisorModel = config.advisor?.modelId ? config.models.find((model) => model.id === config.advisor!.modelId) : undefined; const models: ManagedModelView[] = config.models.map((model) => ({ id: model.id, apiFormat: model.apiFormat, ...(model.displayName ? { displayName: model.displayName } : {}), baseURL: model.baseURL, apiKeyConfigured: model.apiKey.length > 0, model: model.model, contextWindow: model.contextWindow, ...(model.maxTokens ? { maxTokens: model.maxTokens } : {}), ...(model.supportsVision ? { supportsVision: true } : {}), ...(model.reasoningEffort ? { reasoningEffort: model.reasoningEffort } : {}), promptCache: model.promptCache ?? { mode: 'auto' }, enabled: model.enabled, updatedAt: model.updatedAt, })); return { revision: config.savedAt, source: snapshot.source, readOnly: snapshot.readOnly, models, baseModelId: config.baseModelId, ...(config.compactRatio !== undefined ? { compactRatio: config.compactRatio } : {}), ...(baseModel ? { model: baseModel.model, contextWindow: baseModel.contextWindow } : {}), draftFields: Object.keys(drafts).map((key) => `tools.${key}`), advisor: config.advisor ? { configured: !!advisorModel?.apiKey, ...config.advisor, } : undefined, subagents: { ...config.subagents }, browser: { ...(config.browser ?? DEFAULT_BROWSER_CONFIG) }, computerUse: { ...(config.computerUse ?? DEFAULT_COMPUTER_USE_CONFIG) }, tools: { webSearch: stateFor('webSearch'), imageGeneration: { ...stateFor('imageGeneration'), provider: config.tools.imageGeneration?.provider, baseURL: config.tools.imageGeneration?.baseURL, model: config.tools.imageGeneration?.model, protocol: config.tools.imageGeneration?.protocol ?? 'auto', }, openRouter: stateFor('openRouter'), textToSpeech: { ...stateFor('textToSpeech'), groupIdConfigured: !!config.tools.textToSpeech?.groupId, }, }, toolInput: { ...(typeof drafts.imageGeneration?.provider === 'string' ? { imageProvider: drafts.imageGeneration.provider } : {}), ...(typeof drafts.imageGeneration?.baseURL === 'string' ? { imageBaseURL: drafts.imageGeneration.baseURL } : {}), ...(typeof drafts.imageGeneration?.model === 'string' ? { imageModel: drafts.imageGeneration.model } : {}), ...(drafts.imageGeneration?.protocol === 'auto' || drafts.imageGeneration?.protocol === 'chat' || drafts.imageGeneration?.protocol === 'images' ? { imageProtocol: drafts.imageGeneration.protocol as | 'auto' | 'chat' | 'images', } : {}), ...(typeof drafts.textToSpeech?.groupId === 'string' ? { ttsGroupId: drafts.textToSpeech.groupId } : {}), }, }; } /** 完整 Origin 的唯一规范化入口,供 Desktop 和 CLI 表单共用。 */ export function normalizeAllowedOrigins(input: string | string[]): string[] { const pieces = Array.isArray(input) ? input : input.split(/[\n,]/); const out: string[] = []; for (const piece of pieces) { const raw = piece.trim(); if (!raw) continue; try { const url = new URL(raw); if ( (url.protocol !== 'http:' && url.protocol !== 'https:') || url.pathname !== '/' || url.search || url.hash || url.username || url.password ) continue; const origin = url.origin; if (!out.includes(origin)) out.push(origin); } catch { // Invalid entries are intentionally omitted; callers compare counts to show feedback. } } return out.slice(0, 100); }