// 设置相关功能 let currentConfig = null; let allTools = []; // 全局工具状态映射,用于保存用户在所有页面的修改 // key: 唯一工具标识符(toolKey),value: { enabled: boolean, is_external: boolean, external_mcp: string } let toolStateMap = new Map(); // 生成工具的唯一标识符,用于区分同名但来源不同的工具 function getToolKey(tool) { // 如果是外部工具,使用 external_mcp::tool.name 作为唯一标识 // 如果是内部工具,使用 tool.name 作为标识 if (tool.is_external && tool.external_mcp) { return `${tool.external_mcp}::${tool.name}`; } return tool.name; } // 从localStorage读取每页显示数量,默认为20 const getToolsPageSize = () => { const saved = localStorage.getItem('toolsPageSize'); return saved ? parseInt(saved, 10) : 20; }; let toolsPagination = { page: 1, pageSize: getToolsPageSize(), total: 0, totalPages: 0 }; // 切换设置分类 function switchSettingsSection(section) { // 更新导航项状态 document.querySelectorAll('.settings-nav-item').forEach(item => { item.classList.remove('active'); }); const activeNavItem = document.querySelector(`.settings-nav-item[data-section="${section}"]`); if (activeNavItem) { activeNavItem.classList.add('active'); } // 更新内容区域显示 document.querySelectorAll('.settings-section-content').forEach(content => { content.classList.remove('active'); }); const activeContent = document.getElementById(`settings-section-${section}`); if (activeContent) { activeContent.classList.add('active'); } if (section === 'terminal' && typeof initTerminal === 'function') { setTimeout(initTerminal, 0); } } // 打开设置 async function openSettings() { // 切换到设置页面 if (typeof switchPage === 'function') { switchPage('settings'); } // 每次打开时清空全局状态映射,重新加载最新配置 toolStateMap.clear(); // 每次打开时重新加载最新配置(系统设置页面不需要加载工具列表) await loadConfig(false); // 清除之前的验证错误状态 document.querySelectorAll('.form-group input').forEach(input => { input.classList.remove('error'); }); // 默认显示基本设置 switchSettingsSection('basic'); } // 关闭设置(保留函数以兼容旧代码,但现在不需要关闭功能) function closeSettings() { // 不再需要关闭功能,因为现在是页面而不是模态框 // 如果需要,可以切换回对话页面 if (typeof switchPage === 'function') { switchPage('chat'); } } // 点击模态框外部关闭(只保留MCP详情模态框) window.onclick = function(event) { const mcpModal = document.getElementById('mcp-detail-modal'); if (event.target === mcpModal) { closeMCPDetail(); } } // 加载配置 async function loadConfig(loadTools = true) { try { const response = await apiFetch('/api/config'); if (!response.ok) { throw new Error('获取配置失败'); } currentConfig = await response.json(); // 填充OpenAI配置 document.getElementById('openai-api-key').value = currentConfig.openai.api_key || ''; document.getElementById('openai-base-url').value = currentConfig.openai.base_url || ''; document.getElementById('openai-model').value = currentConfig.openai.model || ''; // 填充FOFA配置 const fofa = currentConfig.fofa || {}; const fofaEmailEl = document.getElementById('fofa-email'); const fofaKeyEl = document.getElementById('fofa-api-key'); const fofaBaseUrlEl = document.getElementById('fofa-base-url'); if (fofaEmailEl) fofaEmailEl.value = fofa.email || ''; if (fofaKeyEl) fofaKeyEl.value = fofa.api_key || ''; if (fofaBaseUrlEl) fofaBaseUrlEl.value = fofa.base_url || ''; // 填充Agent配置 document.getElementById('agent-max-iterations').value = currentConfig.agent.max_iterations || 30; // 填充知识库配置 const knowledgeEnabledCheckbox = document.getElementById('knowledge-enabled'); if (knowledgeEnabledCheckbox) { knowledgeEnabledCheckbox.checked = currentConfig.knowledge?.enabled !== false; } // 填充知识库详细配置 if (currentConfig.knowledge) { const knowledge = currentConfig.knowledge; // 基本配置 const basePathInput = document.getElementById('knowledge-base-path'); if (basePathInput) { basePathInput.value = knowledge.base_path || 'knowledge_base'; } // 嵌入模型配置 const embeddingProviderSelect = document.getElementById('knowledge-embedding-provider'); if (embeddingProviderSelect) { embeddingProviderSelect.value = knowledge.embedding?.provider || 'openai'; } const embeddingModelInput = document.getElementById('knowledge-embedding-model'); if (embeddingModelInput) { embeddingModelInput.value = knowledge.embedding?.model || ''; } const embeddingBaseUrlInput = document.getElementById('knowledge-embedding-base-url'); if (embeddingBaseUrlInput) { embeddingBaseUrlInput.value = knowledge.embedding?.base_url || ''; } const embeddingApiKeyInput = document.getElementById('knowledge-embedding-api-key'); if (embeddingApiKeyInput) { embeddingApiKeyInput.value = knowledge.embedding?.api_key || ''; } // 检索配置 const retrievalTopKInput = document.getElementById('knowledge-retrieval-top-k'); if (retrievalTopKInput) { retrievalTopKInput.value = knowledge.retrieval?.top_k || 5; } const retrievalThresholdInput = document.getElementById('knowledge-retrieval-similarity-threshold'); if (retrievalThresholdInput) { retrievalThresholdInput.value = knowledge.retrieval?.similarity_threshold || 0.7; } const retrievalWeightInput = document.getElementById('knowledge-retrieval-hybrid-weight'); if (retrievalWeightInput) { const hybridWeight = knowledge.retrieval?.hybrid_weight; // 允许0.0值,只有undefined/null时才使用默认值 retrievalWeightInput.value = (hybridWeight !== undefined && hybridWeight !== null) ? hybridWeight : 0.7; } // 索引配置 const indexing = knowledge.indexing || {}; const chunkSizeInput = document.getElementById('knowledge-indexing-chunk-size'); if (chunkSizeInput) { chunkSizeInput.value = indexing.chunk_size || 512; } const chunkOverlapInput = document.getElementById('knowledge-indexing-chunk-overlap'); if (chunkOverlapInput) { chunkOverlapInput.value = indexing.chunk_overlap ?? 50; } const maxChunksPerItemInput = document.getElementById('knowledge-indexing-max-chunks-per-item'); if (maxChunksPerItemInput) { maxChunksPerItemInput.value = indexing.max_chunks_per_item ?? 0; } const maxRpmInput = document.getElementById('knowledge-indexing-max-rpm'); if (maxRpmInput) { maxRpmInput.value = indexing.max_rpm ?? 0; } const rateLimitDelayInput = document.getElementById('knowledge-indexing-rate-limit-delay-ms'); if (rateLimitDelayInput) { rateLimitDelayInput.value = indexing.rate_limit_delay_ms ?? 300; } const maxRetriesInput = document.getElementById('knowledge-indexing-max-retries'); if (maxRetriesInput) { maxRetriesInput.value = indexing.max_retries ?? 3; } const retryDelayInput = document.getElementById('knowledge-indexing-retry-delay-ms'); if (retryDelayInput) { retryDelayInput.value = indexing.retry_delay_ms ?? 1000; } } // 填充机器人配置 const robots = currentConfig.robots || {}; const wecom = robots.wecom || {}; const dingtalk = robots.dingtalk || {}; const lark = robots.lark || {}; const wecomEnabled = document.getElementById('robot-wecom-enabled'); if (wecomEnabled) wecomEnabled.checked = wecom.enabled === true; const wecomToken = document.getElementById('robot-wecom-token'); if (wecomToken) wecomToken.value = wecom.token || ''; const wecomAes = document.getElementById('robot-wecom-encoding-aes-key'); if (wecomAes) wecomAes.value = wecom.encoding_aes_key || ''; const wecomCorp = document.getElementById('robot-wecom-corp-id'); if (wecomCorp) wecomCorp.value = wecom.corp_id || ''; const wecomSecret = document.getElementById('robot-wecom-secret'); if (wecomSecret) wecomSecret.value = wecom.secret || ''; const wecomAgentId = document.getElementById('robot-wecom-agent-id'); if (wecomAgentId) wecomAgentId.value = wecom.agent_id || '0'; const dingtalkEnabled = document.getElementById('robot-dingtalk-enabled'); if (dingtalkEnabled) dingtalkEnabled.checked = dingtalk.enabled === true; const dingtalkClientId = document.getElementById('robot-dingtalk-client-id'); if (dingtalkClientId) dingtalkClientId.value = dingtalk.client_id || ''; const dingtalkClientSecret = document.getElementById('robot-dingtalk-client-secret'); if (dingtalkClientSecret) dingtalkClientSecret.value = dingtalk.client_secret || ''; const larkEnabled = document.getElementById('robot-lark-enabled'); if (larkEnabled) larkEnabled.checked = lark.enabled === true; const larkAppId = document.getElementById('robot-lark-app-id'); if (larkAppId) larkAppId.value = lark.app_id || ''; const larkAppSecret = document.getElementById('robot-lark-app-secret'); if (larkAppSecret) larkAppSecret.value = lark.app_secret || ''; const larkVerify = document.getElementById('robot-lark-verify-token'); if (larkVerify) larkVerify.value = lark.verify_token || ''; // 只有在需要时才加载工具列表(MCP管理页面需要,系统设置页面不需要) if (loadTools) { // 设置每页显示数量(会在分页控件渲染时设置) const savedPageSize = getToolsPageSize(); toolsPagination.pageSize = savedPageSize; // 加载工具列表(使用分页) toolsSearchKeyword = ''; await loadToolsList(1, ''); } } catch (error) { console.error('加载配置失败:', error); const baseMsg = (typeof window !== 'undefined' && typeof window.t === 'function') ? window.t('settings.apply.loadFailed') : '加载配置失败'; alert(baseMsg + ': ' + error.message); } } // 工具搜索关键词 let toolsSearchKeyword = ''; // 加载工具列表(分页) async function loadToolsList(page = 1, searchKeyword = '') { const toolsList = document.getElementById('tools-list'); // 显示加载状态 if (toolsList) { // 清空整个容器,包括可能存在的分页控件 toolsList.innerHTML = '