UNPKG

@voyager-0x/agent-mcp

Version:

Voyager MCP Agent - A powerful Model Context Protocol agent

102 lines (101 loc) 2.92 kB
"use strict"; /** * AI大模型配置管理模块 * 提供统一的配置管理,供所有AI相关模块使用 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.onAIConfigChange = exports.isAIConfigured = exports.updateAIConfig = exports.getAIConfig = exports.setAIConfig = exports.aiConfigManager = void 0; // 默认配置 const DEFAULT_CONFIG = { debug: false, model: 'gpt-3.5-turbo', baseURL: 'https://api.openai.com/v1', }; class AIConfigManager { constructor() { this.config = null; this.listeners = []; } /** * 设置AI配置 */ setConfig(config) { this.config = { ...DEFAULT_CONFIG, ...config }; this.notifyListeners(); } /** * 获取AI配置 */ getConfig() { if (!this.config) { throw new Error('AI配置未初始化,请先调用 setConfig() 设置配置'); } return { ...this.config }; } /** * 更新部分配置 */ updateConfig(partialConfig) { if (!this.config) { throw new Error('AI配置未初始化,请先调用 setConfig() 设置配置'); } this.config = { ...this.config, ...partialConfig }; this.notifyListeners(); } /** * 检查配置是否已初始化 */ isConfigured() { return this.config !== null; } /** * 监听配置变化 */ onConfigChange(listener) { this.listeners.push(listener); // 返回取消监听的函数 return () => { const index = this.listeners.indexOf(listener); if (index > -1) { this.listeners.splice(index, 1); } }; } offConfigChange(listener) { const index = this.listeners.indexOf(listener); if (index > -1) { this.listeners.splice(index, 1); } } /** * 通知所有监听器 */ notifyListeners() { if (this.config) { this.listeners.forEach((listener) => listener(this.config)); } } isDebug() { return this.config?.debug; } /** * 重置配置 */ reset() { this.config = null; this.listeners = []; } } // 导出单例实例 exports.aiConfigManager = new AIConfigManager(); // 便捷方法 const setAIConfig = (config) => exports.aiConfigManager.setConfig(config); exports.setAIConfig = setAIConfig; const getAIConfig = () => exports.aiConfigManager.getConfig(); exports.getAIConfig = getAIConfig; const updateAIConfig = (config) => exports.aiConfigManager.updateConfig(config); exports.updateAIConfig = updateAIConfig; const isAIConfigured = () => exports.aiConfigManager.isConfigured(); exports.isAIConfigured = isAIConfigured; const onAIConfigChange = (listener) => exports.aiConfigManager.onConfigChange(listener); exports.onAIConfigChange = onAIConfigChange;