snow-ai 0.4.15 → 0.4.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/cli.mjs +477445 -0
- package/bundle/sql-wasm.wasm +0 -0
- package/package.json +31 -26
- package/dist/agents/codebaseIndexAgent.d.ts +0 -102
- package/dist/agents/codebaseIndexAgent.js +0 -641
- package/dist/agents/codebaseReviewAgent.d.ts +0 -61
- package/dist/agents/codebaseReviewAgent.js +0 -301
- package/dist/agents/compactAgent.d.ts +0 -55
- package/dist/agents/compactAgent.js +0 -306
- package/dist/agents/promptOptimizeAgent.d.ts +0 -54
- package/dist/agents/promptOptimizeAgent.js +0 -268
- package/dist/agents/reviewAgent.d.ts +0 -50
- package/dist/agents/reviewAgent.js +0 -265
- package/dist/agents/summaryAgent.d.ts +0 -57
- package/dist/agents/summaryAgent.js +0 -260
- package/dist/api/anthropic.d.ts +0 -44
- package/dist/api/anthropic.js +0 -598
- package/dist/api/chat.d.ts +0 -73
- package/dist/api/chat.js +0 -386
- package/dist/api/embedding.d.ts +0 -34
- package/dist/api/embedding.js +0 -80
- package/dist/api/gemini.d.ts +0 -31
- package/dist/api/gemini.js +0 -445
- package/dist/api/models.d.ts +0 -15
- package/dist/api/models.js +0 -139
- package/dist/api/responses.d.ts +0 -38
- package/dist/api/responses.js +0 -515
- package/dist/api/systemPrompt.d.ts +0 -4
- package/dist/api/systemPrompt.js +0 -408
- package/dist/api/types.d.ts +0 -53
- package/dist/api/types.js +0 -4
- package/dist/app.d.ts +0 -8
- package/dist/app.js +0 -112
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -199
- package/dist/hooks/useAgentPicker.d.ts +0 -14
- package/dist/hooks/useAgentPicker.js +0 -119
- package/dist/hooks/useClipboard.d.ts +0 -4
- package/dist/hooks/useClipboard.js +0 -175
- package/dist/hooks/useCommandHandler.d.ts +0 -35
- package/dist/hooks/useCommandHandler.js +0 -346
- package/dist/hooks/useCommandPanel.d.ts +0 -17
- package/dist/hooks/useCommandPanel.js +0 -114
- package/dist/hooks/useConversation.d.ts +0 -49
- package/dist/hooks/useConversation.js +0 -1052
- package/dist/hooks/useFilePicker.d.ts +0 -18
- package/dist/hooks/useFilePicker.js +0 -224
- package/dist/hooks/useGlobalExit.d.ts +0 -5
- package/dist/hooks/useGlobalExit.js +0 -34
- package/dist/hooks/useGlobalNavigation.d.ts +0 -6
- package/dist/hooks/useGlobalNavigation.js +0 -17
- package/dist/hooks/useHistoryNavigation.d.ts +0 -35
- package/dist/hooks/useHistoryNavigation.js +0 -133
- package/dist/hooks/useInputBuffer.d.ts +0 -6
- package/dist/hooks/useInputBuffer.js +0 -45
- package/dist/hooks/useKeyboardInput.d.ts +0 -80
- package/dist/hooks/useKeyboardInput.js +0 -608
- package/dist/hooks/useSessionManagement.d.ts +0 -10
- package/dist/hooks/useSessionManagement.js +0 -43
- package/dist/hooks/useSessionSave.d.ts +0 -8
- package/dist/hooks/useSessionSave.js +0 -63
- package/dist/hooks/useSnapshotState.d.ts +0 -26
- package/dist/hooks/useSnapshotState.js +0 -28
- package/dist/hooks/useStreamingState.d.ts +0 -33
- package/dist/hooks/useStreamingState.js +0 -105
- package/dist/hooks/useTerminalFocus.d.ts +0 -28
- package/dist/hooks/useTerminalFocus.js +0 -87
- package/dist/hooks/useTerminalSize.d.ts +0 -4
- package/dist/hooks/useTerminalSize.js +0 -20
- package/dist/hooks/useTodoPicker.d.ts +0 -16
- package/dist/hooks/useTodoPicker.js +0 -94
- package/dist/hooks/useToolConfirmation.d.ts +0 -19
- package/dist/hooks/useToolConfirmation.js +0 -61
- package/dist/hooks/useVSCodeState.d.ts +0 -8
- package/dist/hooks/useVSCodeState.js +0 -81
- package/dist/i18n/I18nContext.d.ts +0 -14
- package/dist/i18n/I18nContext.js +0 -24
- package/dist/i18n/index.d.ts +0 -3
- package/dist/i18n/index.js +0 -2
- package/dist/i18n/lang/en.d.ts +0 -2
- package/dist/i18n/lang/en.js +0 -502
- package/dist/i18n/lang/es.d.ts +0 -2
- package/dist/i18n/lang/es.js +0 -502
- package/dist/i18n/lang/ja.d.ts +0 -2
- package/dist/i18n/lang/ja.js +0 -502
- package/dist/i18n/lang/ko.d.ts +0 -2
- package/dist/i18n/lang/ko.js +0 -502
- package/dist/i18n/lang/zh-TW.d.ts +0 -2
- package/dist/i18n/lang/zh-TW.js +0 -502
- package/dist/i18n/lang/zh.d.ts +0 -2
- package/dist/i18n/lang/zh.js +0 -502
- package/dist/i18n/translations.d.ts +0 -2
- package/dist/i18n/translations.js +0 -14
- package/dist/i18n/types.d.ts +0 -478
- package/dist/i18n/types.js +0 -1
- package/dist/mcp/aceCodeSearch.d.ts +0 -247
- package/dist/mcp/aceCodeSearch.js +0 -1058
- package/dist/mcp/bash.d.ts +0 -50
- package/dist/mcp/bash.js +0 -153
- package/dist/mcp/codebaseSearch.d.ts +0 -44
- package/dist/mcp/codebaseSearch.js +0 -275
- package/dist/mcp/filesystem.d.ts +0 -392
- package/dist/mcp/filesystem.js +0 -1445
- package/dist/mcp/ideDiagnostics.d.ts +0 -36
- package/dist/mcp/ideDiagnostics.js +0 -90
- package/dist/mcp/notebook.d.ts +0 -10
- package/dist/mcp/notebook.js +0 -367
- package/dist/mcp/subagent.d.ts +0 -37
- package/dist/mcp/subagent.js +0 -113
- package/dist/mcp/todo.d.ts +0 -46
- package/dist/mcp/todo.js +0 -511
- package/dist/mcp/types/aceCodeSearch.types.d.ts +0 -92
- package/dist/mcp/types/aceCodeSearch.types.js +0 -4
- package/dist/mcp/types/bash.types.d.ts +0 -13
- package/dist/mcp/types/bash.types.js +0 -4
- package/dist/mcp/types/filesystem.types.d.ts +0 -210
- package/dist/mcp/types/filesystem.types.js +0 -27
- package/dist/mcp/types/todo.types.d.ts +0 -27
- package/dist/mcp/types/todo.types.js +0 -4
- package/dist/mcp/types/websearch.types.d.ts +0 -30
- package/dist/mcp/types/websearch.types.js +0 -4
- package/dist/mcp/utils/aceCodeSearch/filesystem.utils.d.ts +0 -34
- package/dist/mcp/utils/aceCodeSearch/filesystem.utils.js +0 -146
- package/dist/mcp/utils/aceCodeSearch/language.utils.d.ts +0 -14
- package/dist/mcp/utils/aceCodeSearch/language.utils.js +0 -418
- package/dist/mcp/utils/aceCodeSearch/search.utils.d.ts +0 -31
- package/dist/mcp/utils/aceCodeSearch/search.utils.js +0 -136
- package/dist/mcp/utils/aceCodeSearch/symbol.utils.d.ts +0 -20
- package/dist/mcp/utils/aceCodeSearch/symbol.utils.js +0 -141
- package/dist/mcp/utils/bash/security.utils.d.ts +0 -20
- package/dist/mcp/utils/bash/security.utils.js +0 -34
- package/dist/mcp/utils/filesystem/batch-operations.utils.d.ts +0 -39
- package/dist/mcp/utils/filesystem/batch-operations.utils.js +0 -182
- package/dist/mcp/utils/filesystem/code-analysis.utils.d.ts +0 -18
- package/dist/mcp/utils/filesystem/code-analysis.utils.js +0 -165
- package/dist/mcp/utils/filesystem/match-finder.utils.d.ts +0 -16
- package/dist/mcp/utils/filesystem/match-finder.utils.js +0 -85
- package/dist/mcp/utils/filesystem/office-parser.utils.d.ts +0 -43
- package/dist/mcp/utils/filesystem/office-parser.utils.js +0 -163
- package/dist/mcp/utils/filesystem/path-fixer.utils.d.ts +0 -7
- package/dist/mcp/utils/filesystem/path-fixer.utils.js +0 -60
- package/dist/mcp/utils/filesystem/similarity.utils.d.ts +0 -22
- package/dist/mcp/utils/filesystem/similarity.utils.js +0 -75
- package/dist/mcp/utils/todo/date.utils.d.ts +0 -9
- package/dist/mcp/utils/todo/date.utils.js +0 -14
- package/dist/mcp/utils/websearch/browser.utils.d.ts +0 -8
- package/dist/mcp/utils/websearch/browser.utils.js +0 -58
- package/dist/mcp/utils/websearch/text.utils.d.ts +0 -16
- package/dist/mcp/utils/websearch/text.utils.js +0 -39
- package/dist/mcp/websearch.d.ts +0 -88
- package/dist/mcp/websearch.js +0 -375
- package/dist/test/logger-test.d.ts +0 -1
- package/dist/test/logger-test.js +0 -7
- package/dist/types/index.d.ts +0 -15
- package/dist/types/index.js +0 -1
- package/dist/ui/components/AgentPickerPanel.d.ts +0 -10
- package/dist/ui/components/AgentPickerPanel.js +0 -74
- package/dist/ui/components/ChatInput.d.ts +0 -46
- package/dist/ui/components/ChatInput.js +0 -379
- package/dist/ui/components/CommandPanel.d.ts +0 -15
- package/dist/ui/components/CommandPanel.js +0 -80
- package/dist/ui/components/DiffViewer.d.ts +0 -11
- package/dist/ui/components/DiffViewer.js +0 -178
- package/dist/ui/components/FileList.d.ts +0 -15
- package/dist/ui/components/FileList.js +0 -360
- package/dist/ui/components/FileRollbackConfirmation.d.ts +0 -8
- package/dist/ui/components/FileRollbackConfirmation.js +0 -108
- package/dist/ui/components/HelpPanel.d.ts +0 -2
- package/dist/ui/components/HelpPanel.js +0 -67
- package/dist/ui/components/MCPInfoPanel.d.ts +0 -2
- package/dist/ui/components/MCPInfoPanel.js +0 -108
- package/dist/ui/components/MCPInfoScreen.d.ts +0 -7
- package/dist/ui/components/MCPInfoScreen.js +0 -115
- package/dist/ui/components/MarkdownRenderer.d.ts +0 -6
- package/dist/ui/components/MarkdownRenderer.js +0 -70
- package/dist/ui/components/Menu.d.ts +0 -17
- package/dist/ui/components/Menu.js +0 -88
- package/dist/ui/components/MessageList.d.ts +0 -56
- package/dist/ui/components/MessageList.js +0 -97
- package/dist/ui/components/PendingMessages.d.ts +0 -13
- package/dist/ui/components/PendingMessages.js +0 -29
- package/dist/ui/components/PendingToolCalls.d.ts +0 -11
- package/dist/ui/components/PendingToolCalls.js +0 -35
- package/dist/ui/components/ScrollableSelectInput.d.ts +0 -29
- package/dist/ui/components/ScrollableSelectInput.js +0 -157
- package/dist/ui/components/SessionListPanel.d.ts +0 -7
- package/dist/ui/components/SessionListPanel.js +0 -175
- package/dist/ui/components/SessionListScreen.d.ts +0 -7
- package/dist/ui/components/SessionListScreen.js +0 -217
- package/dist/ui/components/SessionListScreenWrapper.d.ts +0 -7
- package/dist/ui/components/SessionListScreenWrapper.js +0 -14
- package/dist/ui/components/ShimmerText.d.ts +0 -9
- package/dist/ui/components/ShimmerText.js +0 -30
- package/dist/ui/components/TodoPickerPanel.d.ts +0 -14
- package/dist/ui/components/TodoPickerPanel.js +0 -119
- package/dist/ui/components/TodoTree.d.ts +0 -15
- package/dist/ui/components/TodoTree.js +0 -60
- package/dist/ui/components/ToolConfirmation.d.ts +0 -21
- package/dist/ui/components/ToolConfirmation.js +0 -204
- package/dist/ui/components/ToolResultPreview.d.ts +0 -13
- package/dist/ui/components/ToolResultPreview.js +0 -337
- package/dist/ui/components/UsagePanel.d.ts +0 -2
- package/dist/ui/components/UsagePanel.js +0 -394
- package/dist/ui/contexts/ThemeContext.d.ts +0 -13
- package/dist/ui/contexts/ThemeContext.js +0 -28
- package/dist/ui/pages/ChatScreen.d.ts +0 -6
- package/dist/ui/pages/ChatScreen.js +0 -1495
- package/dist/ui/pages/CodeBaseConfigScreen.d.ts +0 -8
- package/dist/ui/pages/CodeBaseConfigScreen.js +0 -350
- package/dist/ui/pages/ConfigScreen.d.ts +0 -8
- package/dist/ui/pages/ConfigScreen.js +0 -1101
- package/dist/ui/pages/CustomHeadersScreen.d.ts +0 -6
- package/dist/ui/pages/CustomHeadersScreen.js +0 -502
- package/dist/ui/pages/HeadlessModeScreen.d.ts +0 -7
- package/dist/ui/pages/HeadlessModeScreen.js +0 -381
- package/dist/ui/pages/LanguageSettingsScreen.d.ts +0 -7
- package/dist/ui/pages/LanguageSettingsScreen.js +0 -91
- package/dist/ui/pages/MCPConfigScreen.d.ts +0 -6
- package/dist/ui/pages/MCPConfigScreen.js +0 -55
- package/dist/ui/pages/ProxyConfigScreen.d.ts +0 -8
- package/dist/ui/pages/ProxyConfigScreen.js +0 -149
- package/dist/ui/pages/SensitiveCommandConfigScreen.d.ts +0 -7
- package/dist/ui/pages/SensitiveCommandConfigScreen.js +0 -271
- package/dist/ui/pages/SubAgentConfigScreen.d.ts +0 -9
- package/dist/ui/pages/SubAgentConfigScreen.js +0 -435
- package/dist/ui/pages/SubAgentListScreen.d.ts +0 -9
- package/dist/ui/pages/SubAgentListScreen.js +0 -131
- package/dist/ui/pages/SystemPromptConfigScreen.d.ts +0 -6
- package/dist/ui/pages/SystemPromptConfigScreen.js +0 -326
- package/dist/ui/pages/ThemeSettingsScreen.d.ts +0 -7
- package/dist/ui/pages/ThemeSettingsScreen.js +0 -106
- package/dist/ui/pages/WelcomeScreen.d.ts +0 -7
- package/dist/ui/pages/WelcomeScreen.js +0 -217
- package/dist/ui/themes/index.d.ts +0 -23
- package/dist/ui/themes/index.js +0 -140
- package/dist/utils/apiConfig.d.ts +0 -126
- package/dist/utils/apiConfig.js +0 -423
- package/dist/utils/autoCompress.d.ts +0 -15
- package/dist/utils/autoCompress.js +0 -24
- package/dist/utils/chatExporter.d.ts +0 -9
- package/dist/utils/chatExporter.js +0 -118
- package/dist/utils/checkpointManager.d.ts +0 -74
- package/dist/utils/checkpointManager.js +0 -181
- package/dist/utils/codebaseConfig.d.ts +0 -16
- package/dist/utils/codebaseConfig.js +0 -67
- package/dist/utils/codebaseDatabase.d.ts +0 -102
- package/dist/utils/codebaseDatabase.js +0 -333
- package/dist/utils/codebaseSearchEvents.d.ts +0 -16
- package/dist/utils/codebaseSearchEvents.js +0 -13
- package/dist/utils/commandExecutor.d.ts +0 -13
- package/dist/utils/commandExecutor.js +0 -26
- package/dist/utils/commands/agent.d.ts +0 -2
- package/dist/utils/commands/agent.js +0 -12
- package/dist/utils/commands/clear.d.ts +0 -2
- package/dist/utils/commands/clear.js +0 -12
- package/dist/utils/commands/compact.d.ts +0 -2
- package/dist/utils/commands/compact.js +0 -12
- package/dist/utils/commands/export.d.ts +0 -2
- package/dist/utils/commands/export.js +0 -12
- package/dist/utils/commands/help.d.ts +0 -2
- package/dist/utils/commands/help.js +0 -11
- package/dist/utils/commands/home.d.ts +0 -2
- package/dist/utils/commands/home.js +0 -34
- package/dist/utils/commands/ide.d.ts +0 -2
- package/dist/utils/commands/ide.js +0 -32
- package/dist/utils/commands/init.d.ts +0 -2
- package/dist/utils/commands/init.js +0 -93
- package/dist/utils/commands/mcp.d.ts +0 -2
- package/dist/utils/commands/mcp.js +0 -12
- package/dist/utils/commands/resume.d.ts +0 -2
- package/dist/utils/commands/resume.js +0 -12
- package/dist/utils/commands/review.d.ts +0 -2
- package/dist/utils/commands/review.js +0 -81
- package/dist/utils/commands/role.d.ts +0 -2
- package/dist/utils/commands/role.js +0 -37
- package/dist/utils/commands/todoPicker.d.ts +0 -2
- package/dist/utils/commands/todoPicker.js +0 -12
- package/dist/utils/commands/usage.d.ts +0 -2
- package/dist/utils/commands/usage.js +0 -12
- package/dist/utils/commands/yolo.d.ts +0 -2
- package/dist/utils/commands/yolo.js +0 -12
- package/dist/utils/configManager.d.ts +0 -45
- package/dist/utils/configManager.js +0 -303
- package/dist/utils/contextCompressor.d.ts +0 -16
- package/dist/utils/contextCompressor.js +0 -334
- package/dist/utils/devMode.d.ts +0 -13
- package/dist/utils/devMode.js +0 -54
- package/dist/utils/escapeHandler.d.ts +0 -79
- package/dist/utils/escapeHandler.js +0 -153
- package/dist/utils/fileDialog.d.ts +0 -9
- package/dist/utils/fileDialog.js +0 -74
- package/dist/utils/fileUtils.d.ts +0 -40
- package/dist/utils/fileUtils.js +0 -185
- package/dist/utils/historyManager.d.ts +0 -45
- package/dist/utils/historyManager.js +0 -159
- package/dist/utils/incrementalSnapshot.d.ts +0 -109
- package/dist/utils/incrementalSnapshot.js +0 -383
- package/dist/utils/index.d.ts +0 -11
- package/dist/utils/index.js +0 -18
- package/dist/utils/languageConfig.d.ts +0 -21
- package/dist/utils/languageConfig.js +0 -61
- package/dist/utils/logger.d.ts +0 -37
- package/dist/utils/logger.js +0 -122
- package/dist/utils/mcpToolsManager.d.ts +0 -52
- package/dist/utils/mcpToolsManager.js +0 -878
- package/dist/utils/messageFormatter.d.ts +0 -12
- package/dist/utils/messageFormatter.js +0 -115
- package/dist/utils/notebookManager.d.ts +0 -59
- package/dist/utils/notebookManager.js +0 -213
- package/dist/utils/patch-highlight.d.ts +0 -5
- package/dist/utils/patch-highlight.js +0 -23
- package/dist/utils/processManager.d.ts +0 -27
- package/dist/utils/processManager.js +0 -75
- package/dist/utils/proxyUtils.d.ts +0 -15
- package/dist/utils/proxyUtils.js +0 -50
- package/dist/utils/resourceMonitor.d.ts +0 -65
- package/dist/utils/resourceMonitor.js +0 -175
- package/dist/utils/retryUtils.d.ts +0 -49
- package/dist/utils/retryUtils.js +0 -303
- package/dist/utils/sensitiveCommandManager.d.ts +0 -53
- package/dist/utils/sensitiveCommandManager.js +0 -308
- package/dist/utils/sessionConverter.d.ts +0 -7
- package/dist/utils/sessionConverter.js +0 -306
- package/dist/utils/sessionManager.d.ts +0 -53
- package/dist/utils/sessionManager.js +0 -371
- package/dist/utils/subAgentConfig.d.ts +0 -50
- package/dist/utils/subAgentConfig.js +0 -221
- package/dist/utils/subAgentExecutor.d.ts +0 -40
- package/dist/utils/subAgentExecutor.js +0 -434
- package/dist/utils/terminal.d.ts +0 -5
- package/dist/utils/terminal.js +0 -13
- package/dist/utils/textBuffer.d.ts +0 -99
- package/dist/utils/textBuffer.js +0 -547
- package/dist/utils/textUtils.d.ts +0 -37
- package/dist/utils/textUtils.js +0 -102
- package/dist/utils/themeConfig.d.ts +0 -21
- package/dist/utils/themeConfig.js +0 -61
- package/dist/utils/todoPreprocessor.d.ts +0 -5
- package/dist/utils/todoPreprocessor.js +0 -18
- package/dist/utils/todoScanner.d.ts +0 -8
- package/dist/utils/todoScanner.js +0 -148
- package/dist/utils/toolDisplayConfig.d.ts +0 -16
- package/dist/utils/toolDisplayConfig.js +0 -47
- package/dist/utils/toolExecutor.d.ts +0 -37
- package/dist/utils/toolExecutor.js +0 -224
- package/dist/utils/usageLogger.d.ts +0 -11
- package/dist/utils/usageLogger.js +0 -114
- package/dist/utils/vscodeConnection.d.ts +0 -76
- package/dist/utils/vscodeConnection.js +0 -430
- package/dist/utils/workspaceSnapshot.d.ts +0 -63
- package/dist/utils/workspaceSnapshot.js +0 -300
|
@@ -1,878 +0,0 @@
|
|
|
1
|
-
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
2
|
-
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
3
|
-
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
4
|
-
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
5
|
-
import { getMCPConfig } from './apiConfig.js';
|
|
6
|
-
import { mcpTools as filesystemTools } from '../mcp/filesystem.js';
|
|
7
|
-
import { mcpTools as terminalTools } from '../mcp/bash.js';
|
|
8
|
-
import { mcpTools as aceCodeSearchTools } from '../mcp/aceCodeSearch.js';
|
|
9
|
-
import { mcpTools as websearchTools } from '../mcp/websearch.js';
|
|
10
|
-
import { mcpTools as ideDiagnosticsTools } from '../mcp/ideDiagnostics.js';
|
|
11
|
-
import { mcpTools as codebaseSearchTools } from '../mcp/codebaseSearch.js';
|
|
12
|
-
import { TodoService } from '../mcp/todo.js';
|
|
13
|
-
import { mcpTools as notebookTools, executeNotebookTool, } from '../mcp/notebook.js';
|
|
14
|
-
import { getMCPTools as getSubAgentTools, subAgentService, } from '../mcp/subagent.js';
|
|
15
|
-
import { sessionManager } from './sessionManager.js';
|
|
16
|
-
import { logger } from './logger.js';
|
|
17
|
-
import { resourceMonitor } from './resourceMonitor.js';
|
|
18
|
-
import os from 'os';
|
|
19
|
-
import path from 'path';
|
|
20
|
-
let toolsCache = null;
|
|
21
|
-
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
|
|
22
|
-
// Lazy initialization of TODO service to avoid circular dependencies
|
|
23
|
-
let todoService = null;
|
|
24
|
-
/**
|
|
25
|
-
* Get the TODO service instance (lazy initialization)
|
|
26
|
-
*/
|
|
27
|
-
export function getTodoService() {
|
|
28
|
-
if (!todoService) {
|
|
29
|
-
todoService = new TodoService(path.join(os.homedir(), '.snow'), () => {
|
|
30
|
-
const session = sessionManager.getCurrentSession();
|
|
31
|
-
return session ? session.id : null;
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
return todoService;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Generate a hash of the current MCP configuration and sub-agents
|
|
38
|
-
*/
|
|
39
|
-
async function generateConfigHash() {
|
|
40
|
-
try {
|
|
41
|
-
const mcpConfig = getMCPConfig();
|
|
42
|
-
const subAgents = getSubAgentTools(); // Include sub-agents in hash
|
|
43
|
-
// 🔥 CRITICAL: Include codebase enabled status in hash
|
|
44
|
-
const { loadCodebaseConfig } = await import('./codebaseConfig.js');
|
|
45
|
-
const codebaseConfig = loadCodebaseConfig();
|
|
46
|
-
return JSON.stringify({
|
|
47
|
-
mcpServers: mcpConfig.mcpServers,
|
|
48
|
-
subAgents: subAgents.map(t => t.name), // Only track agent names for hash
|
|
49
|
-
codebaseEnabled: codebaseConfig.enabled, // 🔥 Must include to invalidate cache on enable/disable
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
return '';
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Check if the cache is valid and not expired
|
|
58
|
-
*/
|
|
59
|
-
async function isCacheValid() {
|
|
60
|
-
if (!toolsCache)
|
|
61
|
-
return false;
|
|
62
|
-
const now = Date.now();
|
|
63
|
-
const isExpired = now - toolsCache.lastUpdate > CACHE_DURATION;
|
|
64
|
-
const configHash = await generateConfigHash();
|
|
65
|
-
const configChanged = toolsCache.configHash !== configHash;
|
|
66
|
-
return !isExpired && !configChanged;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Get cached tools or build cache if needed
|
|
70
|
-
*/
|
|
71
|
-
async function getCachedTools() {
|
|
72
|
-
if (await isCacheValid()) {
|
|
73
|
-
return toolsCache.tools;
|
|
74
|
-
}
|
|
75
|
-
await refreshToolsCache();
|
|
76
|
-
return toolsCache.tools;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Refresh the tools cache by collecting all available tools
|
|
80
|
-
*/
|
|
81
|
-
async function refreshToolsCache() {
|
|
82
|
-
const allTools = [];
|
|
83
|
-
const servicesInfo = [];
|
|
84
|
-
// Add built-in filesystem tools (always available)
|
|
85
|
-
const filesystemServiceTools = filesystemTools.map(tool => ({
|
|
86
|
-
name: tool.name.replace('filesystem-', ''),
|
|
87
|
-
description: tool.description,
|
|
88
|
-
inputSchema: tool.inputSchema,
|
|
89
|
-
}));
|
|
90
|
-
servicesInfo.push({
|
|
91
|
-
serviceName: 'filesystem',
|
|
92
|
-
tools: filesystemServiceTools,
|
|
93
|
-
isBuiltIn: true,
|
|
94
|
-
connected: true,
|
|
95
|
-
});
|
|
96
|
-
for (const tool of filesystemTools) {
|
|
97
|
-
allTools.push({
|
|
98
|
-
type: 'function',
|
|
99
|
-
function: {
|
|
100
|
-
name: tool.name,
|
|
101
|
-
description: tool.description,
|
|
102
|
-
parameters: tool.inputSchema,
|
|
103
|
-
},
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
// Add built-in terminal tools (always available)
|
|
107
|
-
const terminalServiceTools = terminalTools.map(tool => ({
|
|
108
|
-
name: tool.name.replace('terminal-', ''),
|
|
109
|
-
description: tool.description,
|
|
110
|
-
inputSchema: tool.inputSchema,
|
|
111
|
-
}));
|
|
112
|
-
servicesInfo.push({
|
|
113
|
-
serviceName: 'terminal',
|
|
114
|
-
tools: terminalServiceTools,
|
|
115
|
-
isBuiltIn: true,
|
|
116
|
-
connected: true,
|
|
117
|
-
});
|
|
118
|
-
for (const tool of terminalTools) {
|
|
119
|
-
allTools.push({
|
|
120
|
-
type: 'function',
|
|
121
|
-
function: {
|
|
122
|
-
name: tool.name,
|
|
123
|
-
description: tool.description,
|
|
124
|
-
parameters: tool.inputSchema,
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
// Add built-in TODO tools (always available)
|
|
129
|
-
const todoSvc = getTodoService(); // This will never return null after lazy init
|
|
130
|
-
await todoSvc.initialize();
|
|
131
|
-
const todoTools = todoSvc.getTools();
|
|
132
|
-
const todoServiceTools = todoTools.map(tool => ({
|
|
133
|
-
name: tool.name.replace('todo-', ''),
|
|
134
|
-
description: tool.description || '',
|
|
135
|
-
inputSchema: tool.inputSchema,
|
|
136
|
-
}));
|
|
137
|
-
servicesInfo.push({
|
|
138
|
-
serviceName: 'todo',
|
|
139
|
-
tools: todoServiceTools,
|
|
140
|
-
isBuiltIn: true,
|
|
141
|
-
connected: true,
|
|
142
|
-
});
|
|
143
|
-
for (const tool of todoTools) {
|
|
144
|
-
allTools.push({
|
|
145
|
-
type: 'function',
|
|
146
|
-
function: {
|
|
147
|
-
name: tool.name,
|
|
148
|
-
description: tool.description || '',
|
|
149
|
-
parameters: tool.inputSchema,
|
|
150
|
-
},
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
// Add built-in Notebook tools (always available)
|
|
154
|
-
const notebookServiceTools = notebookTools.map(tool => ({
|
|
155
|
-
name: tool.name.replace('notebook-', ''),
|
|
156
|
-
description: tool.description || '',
|
|
157
|
-
inputSchema: tool.inputSchema,
|
|
158
|
-
}));
|
|
159
|
-
servicesInfo.push({
|
|
160
|
-
serviceName: 'notebook',
|
|
161
|
-
tools: notebookServiceTools,
|
|
162
|
-
isBuiltIn: true,
|
|
163
|
-
connected: true,
|
|
164
|
-
});
|
|
165
|
-
for (const tool of notebookTools) {
|
|
166
|
-
allTools.push({
|
|
167
|
-
type: 'function',
|
|
168
|
-
function: {
|
|
169
|
-
name: tool.name,
|
|
170
|
-
description: tool.description || '',
|
|
171
|
-
parameters: tool.inputSchema,
|
|
172
|
-
},
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
// Add built-in ACE Code Search tools (always available)
|
|
176
|
-
const aceServiceTools = aceCodeSearchTools.map(tool => ({
|
|
177
|
-
name: tool.name.replace('ace-', ''),
|
|
178
|
-
description: tool.description,
|
|
179
|
-
inputSchema: tool.inputSchema,
|
|
180
|
-
}));
|
|
181
|
-
servicesInfo.push({
|
|
182
|
-
serviceName: 'ace',
|
|
183
|
-
tools: aceServiceTools,
|
|
184
|
-
isBuiltIn: true,
|
|
185
|
-
connected: true,
|
|
186
|
-
});
|
|
187
|
-
for (const tool of aceCodeSearchTools) {
|
|
188
|
-
allTools.push({
|
|
189
|
-
type: 'function',
|
|
190
|
-
function: {
|
|
191
|
-
name: tool.name,
|
|
192
|
-
description: tool.description,
|
|
193
|
-
parameters: tool.inputSchema,
|
|
194
|
-
},
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
// Add built-in Web Search tools (always available)
|
|
198
|
-
const websearchServiceTools = websearchTools.map(tool => ({
|
|
199
|
-
name: tool.name.replace('websearch-', ''),
|
|
200
|
-
description: tool.description,
|
|
201
|
-
inputSchema: tool.inputSchema,
|
|
202
|
-
}));
|
|
203
|
-
servicesInfo.push({
|
|
204
|
-
serviceName: 'websearch',
|
|
205
|
-
tools: websearchServiceTools,
|
|
206
|
-
isBuiltIn: true,
|
|
207
|
-
connected: true,
|
|
208
|
-
});
|
|
209
|
-
for (const tool of websearchTools) {
|
|
210
|
-
allTools.push({
|
|
211
|
-
type: 'function',
|
|
212
|
-
function: {
|
|
213
|
-
name: tool.name,
|
|
214
|
-
description: tool.description,
|
|
215
|
-
parameters: tool.inputSchema,
|
|
216
|
-
},
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
// Add built-in IDE Diagnostics tools (always available)
|
|
220
|
-
const ideDiagnosticsServiceTools = ideDiagnosticsTools.map(tool => ({
|
|
221
|
-
name: tool.name.replace('ide-', ''),
|
|
222
|
-
description: tool.description,
|
|
223
|
-
inputSchema: tool.inputSchema,
|
|
224
|
-
}));
|
|
225
|
-
servicesInfo.push({
|
|
226
|
-
serviceName: 'ide',
|
|
227
|
-
tools: ideDiagnosticsServiceTools,
|
|
228
|
-
isBuiltIn: true,
|
|
229
|
-
connected: true,
|
|
230
|
-
});
|
|
231
|
-
for (const tool of ideDiagnosticsTools) {
|
|
232
|
-
allTools.push({
|
|
233
|
-
type: 'function',
|
|
234
|
-
function: {
|
|
235
|
-
name: tool.name,
|
|
236
|
-
description: tool.description,
|
|
237
|
-
parameters: tool.inputSchema,
|
|
238
|
-
},
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
// Add sub-agent tools (dynamically generated from configuration)
|
|
242
|
-
const subAgentTools = getSubAgentTools();
|
|
243
|
-
if (subAgentTools.length > 0) {
|
|
244
|
-
servicesInfo.push({
|
|
245
|
-
serviceName: 'subagent',
|
|
246
|
-
tools: subAgentTools,
|
|
247
|
-
isBuiltIn: true,
|
|
248
|
-
connected: true,
|
|
249
|
-
});
|
|
250
|
-
for (const tool of subAgentTools) {
|
|
251
|
-
allTools.push({
|
|
252
|
-
type: 'function',
|
|
253
|
-
function: {
|
|
254
|
-
name: `subagent-${tool.name}`,
|
|
255
|
-
description: tool.description,
|
|
256
|
-
parameters: tool.inputSchema,
|
|
257
|
-
},
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
// Add built-in Codebase Search tools (conditionally loaded if enabled and index is available)
|
|
262
|
-
try {
|
|
263
|
-
// First check if codebase feature is enabled in config
|
|
264
|
-
const { loadCodebaseConfig } = await import('./codebaseConfig.js');
|
|
265
|
-
const codebaseConfig = loadCodebaseConfig();
|
|
266
|
-
// Only proceed if feature is enabled
|
|
267
|
-
if (codebaseConfig.enabled) {
|
|
268
|
-
const projectRoot = process.cwd();
|
|
269
|
-
const dbPath = path.join(projectRoot, '.snow', 'codebase', 'embeddings.db');
|
|
270
|
-
const fs = await import('node:fs');
|
|
271
|
-
// Only add if database file exists
|
|
272
|
-
if (fs.existsSync(dbPath)) {
|
|
273
|
-
// Check if database has data by importing CodebaseDatabase
|
|
274
|
-
const { CodebaseDatabase } = await import('./codebaseDatabase.js');
|
|
275
|
-
const db = new CodebaseDatabase(projectRoot);
|
|
276
|
-
db.initialize();
|
|
277
|
-
const totalChunks = db.getTotalChunks();
|
|
278
|
-
db.close();
|
|
279
|
-
if (totalChunks > 0) {
|
|
280
|
-
const codebaseSearchServiceTools = codebaseSearchTools.map(tool => ({
|
|
281
|
-
name: tool.name.replace('codebase-', ''),
|
|
282
|
-
description: tool.description,
|
|
283
|
-
inputSchema: tool.inputSchema,
|
|
284
|
-
}));
|
|
285
|
-
servicesInfo.push({
|
|
286
|
-
serviceName: 'codebase',
|
|
287
|
-
tools: codebaseSearchServiceTools,
|
|
288
|
-
isBuiltIn: true,
|
|
289
|
-
connected: true,
|
|
290
|
-
});
|
|
291
|
-
for (const tool of codebaseSearchTools) {
|
|
292
|
-
allTools.push({
|
|
293
|
-
type: 'function',
|
|
294
|
-
function: {
|
|
295
|
-
name: tool.name,
|
|
296
|
-
description: tool.description,
|
|
297
|
-
parameters: tool.inputSchema,
|
|
298
|
-
},
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
catch (error) {
|
|
306
|
-
// Silently ignore if codebase search tools are not available
|
|
307
|
-
logger.debug('Codebase search tools not available:', error);
|
|
308
|
-
}
|
|
309
|
-
// Add user-configured MCP server tools (probe for availability but don't maintain connections)
|
|
310
|
-
try {
|
|
311
|
-
const mcpConfig = getMCPConfig();
|
|
312
|
-
for (const [serviceName, server] of Object.entries(mcpConfig.mcpServers)) {
|
|
313
|
-
try {
|
|
314
|
-
const serviceTools = await probeServiceTools(serviceName, server);
|
|
315
|
-
servicesInfo.push({
|
|
316
|
-
serviceName,
|
|
317
|
-
tools: serviceTools,
|
|
318
|
-
isBuiltIn: false,
|
|
319
|
-
connected: true,
|
|
320
|
-
});
|
|
321
|
-
for (const tool of serviceTools) {
|
|
322
|
-
allTools.push({
|
|
323
|
-
type: 'function',
|
|
324
|
-
function: {
|
|
325
|
-
name: `${serviceName}-${tool.name}`,
|
|
326
|
-
description: tool.description,
|
|
327
|
-
parameters: tool.inputSchema,
|
|
328
|
-
},
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
catch (error) {
|
|
333
|
-
servicesInfo.push({
|
|
334
|
-
serviceName,
|
|
335
|
-
tools: [],
|
|
336
|
-
isBuiltIn: false,
|
|
337
|
-
connected: false,
|
|
338
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
catch (error) {
|
|
344
|
-
logger.warn('Failed to load MCP config:', error);
|
|
345
|
-
}
|
|
346
|
-
// Update cache
|
|
347
|
-
toolsCache = {
|
|
348
|
-
tools: allTools,
|
|
349
|
-
servicesInfo,
|
|
350
|
-
lastUpdate: Date.now(),
|
|
351
|
-
configHash: await generateConfigHash(),
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Manually refresh the tools cache (for configuration changes)
|
|
356
|
-
*/
|
|
357
|
-
export async function refreshMCPToolsCache() {
|
|
358
|
-
toolsCache = null;
|
|
359
|
-
await refreshToolsCache();
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Reconnect a specific MCP service and update cache
|
|
363
|
-
* @param serviceName - Name of the service to reconnect
|
|
364
|
-
*/
|
|
365
|
-
export async function reconnectMCPService(serviceName) {
|
|
366
|
-
if (!toolsCache) {
|
|
367
|
-
// If no cache, do full refresh
|
|
368
|
-
await refreshToolsCache();
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
// Handle built-in services (they don't need reconnection)
|
|
372
|
-
if (serviceName === 'filesystem' ||
|
|
373
|
-
serviceName === 'terminal' ||
|
|
374
|
-
serviceName === 'todo' ||
|
|
375
|
-
serviceName === 'ace' ||
|
|
376
|
-
serviceName === 'websearch' ||
|
|
377
|
-
serviceName === 'codebase' ||
|
|
378
|
-
serviceName === 'subagent') {
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
|
-
// Get the server config
|
|
382
|
-
const mcpConfig = getMCPConfig();
|
|
383
|
-
const server = mcpConfig.mcpServers[serviceName];
|
|
384
|
-
if (!server) {
|
|
385
|
-
throw new Error(`Service ${serviceName} not found in configuration`);
|
|
386
|
-
}
|
|
387
|
-
// Find and update the service in cache
|
|
388
|
-
const serviceIndex = toolsCache.servicesInfo.findIndex(s => s.serviceName === serviceName);
|
|
389
|
-
if (serviceIndex === -1) {
|
|
390
|
-
// Service not in cache, do full refresh
|
|
391
|
-
await refreshToolsCache();
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
try {
|
|
395
|
-
// Try to reconnect to the service
|
|
396
|
-
const serviceTools = await probeServiceTools(serviceName, server);
|
|
397
|
-
// Update service info in cache
|
|
398
|
-
toolsCache.servicesInfo[serviceIndex] = {
|
|
399
|
-
serviceName,
|
|
400
|
-
tools: serviceTools,
|
|
401
|
-
isBuiltIn: false,
|
|
402
|
-
connected: true,
|
|
403
|
-
};
|
|
404
|
-
// Remove old tools for this service from the tools list
|
|
405
|
-
toolsCache.tools = toolsCache.tools.filter(tool => !tool.function.name.startsWith(`${serviceName}-`));
|
|
406
|
-
// Add new tools for this service
|
|
407
|
-
for (const tool of serviceTools) {
|
|
408
|
-
toolsCache.tools.push({
|
|
409
|
-
type: 'function',
|
|
410
|
-
function: {
|
|
411
|
-
name: `${serviceName}-${tool.name}`,
|
|
412
|
-
description: tool.description,
|
|
413
|
-
parameters: tool.inputSchema,
|
|
414
|
-
},
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
catch (error) {
|
|
419
|
-
// Update service as failed
|
|
420
|
-
toolsCache.servicesInfo[serviceIndex] = {
|
|
421
|
-
serviceName,
|
|
422
|
-
tools: [],
|
|
423
|
-
isBuiltIn: false,
|
|
424
|
-
connected: false,
|
|
425
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
426
|
-
};
|
|
427
|
-
// Remove tools for this service from the tools list
|
|
428
|
-
toolsCache.tools = toolsCache.tools.filter(tool => !tool.function.name.startsWith(`${serviceName}-`));
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* Clear the tools cache (useful for testing or forcing refresh)
|
|
433
|
-
*/
|
|
434
|
-
export function clearMCPToolsCache() {
|
|
435
|
-
toolsCache = null;
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Collect all available MCP tools from built-in and user-configured services
|
|
439
|
-
* Uses caching to avoid reconnecting on every message
|
|
440
|
-
*/
|
|
441
|
-
export async function collectAllMCPTools() {
|
|
442
|
-
return await getCachedTools();
|
|
443
|
-
}
|
|
444
|
-
/**
|
|
445
|
-
* Get detailed information about all MCP services and their tools
|
|
446
|
-
* Uses cached data when available
|
|
447
|
-
*/
|
|
448
|
-
export async function getMCPServicesInfo() {
|
|
449
|
-
if (!isCacheValid()) {
|
|
450
|
-
await refreshToolsCache();
|
|
451
|
-
}
|
|
452
|
-
// Ensure toolsCache is not null before accessing
|
|
453
|
-
return toolsCache?.servicesInfo || [];
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Quick probe of MCP service tools without maintaining connections
|
|
457
|
-
* This is used for caching tool definitions
|
|
458
|
-
*/
|
|
459
|
-
async function probeServiceTools(serviceName, server) {
|
|
460
|
-
return await connectAndGetTools(serviceName, server, 3000); // Short timeout for probing
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Connect to MCP service and get tools (used for both caching and execution)
|
|
464
|
-
* @param serviceName - Name of the service
|
|
465
|
-
* @param server - Server configuration
|
|
466
|
-
* @param timeoutMs - Timeout in milliseconds (default 10000)
|
|
467
|
-
*/
|
|
468
|
-
async function connectAndGetTools(serviceName, server, timeoutMs = 10000) {
|
|
469
|
-
let client = null;
|
|
470
|
-
let transport;
|
|
471
|
-
let timeoutId = null;
|
|
472
|
-
let connectionAborted = false;
|
|
473
|
-
// Create abort mechanism for cleanup
|
|
474
|
-
const abortConnection = () => {
|
|
475
|
-
connectionAborted = true;
|
|
476
|
-
if (timeoutId) {
|
|
477
|
-
clearTimeout(timeoutId);
|
|
478
|
-
timeoutId = null;
|
|
479
|
-
}
|
|
480
|
-
};
|
|
481
|
-
try {
|
|
482
|
-
client = new Client({
|
|
483
|
-
name: `snow-cli-${serviceName}`,
|
|
484
|
-
version: '1.0.0',
|
|
485
|
-
}, {
|
|
486
|
-
capabilities: {},
|
|
487
|
-
});
|
|
488
|
-
resourceMonitor.trackMCPConnectionOpened(serviceName);
|
|
489
|
-
// Create transport based on server configuration
|
|
490
|
-
if (server.url) {
|
|
491
|
-
let urlString = server.url;
|
|
492
|
-
if (server.env) {
|
|
493
|
-
const allEnv = { ...process.env, ...server.env };
|
|
494
|
-
urlString = urlString.replace(/\$\{([^}]+)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g, (match, braced, simple) => {
|
|
495
|
-
const varName = braced || simple;
|
|
496
|
-
return allEnv[varName] || match;
|
|
497
|
-
});
|
|
498
|
-
}
|
|
499
|
-
else {
|
|
500
|
-
urlString = urlString.replace(/\$\{([^}]+)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g, (match, braced, simple) => {
|
|
501
|
-
const varName = braced || simple;
|
|
502
|
-
return process.env[varName] || match;
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
const url = new URL(urlString);
|
|
506
|
-
try {
|
|
507
|
-
// Try HTTP first
|
|
508
|
-
const headers = {
|
|
509
|
-
'Content-Type': 'application/json',
|
|
510
|
-
};
|
|
511
|
-
if (server.env) {
|
|
512
|
-
const allEnv = { ...process.env, ...server.env };
|
|
513
|
-
if (allEnv['MCP_API_KEY']) {
|
|
514
|
-
headers['Authorization'] = `Bearer ${allEnv['MCP_API_KEY']}`;
|
|
515
|
-
}
|
|
516
|
-
if (allEnv['MCP_AUTH_HEADER']) {
|
|
517
|
-
headers['Authorization'] = allEnv['MCP_AUTH_HEADER'];
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
transport = new StreamableHTTPClientTransport(url, {
|
|
521
|
-
requestInit: { headers },
|
|
522
|
-
});
|
|
523
|
-
// Use timeout with abort mechanism
|
|
524
|
-
await Promise.race([
|
|
525
|
-
client.connect(transport),
|
|
526
|
-
new Promise((_, reject) => {
|
|
527
|
-
timeoutId = setTimeout(() => {
|
|
528
|
-
abortConnection();
|
|
529
|
-
reject(new Error('HTTP connection timeout'));
|
|
530
|
-
}, timeoutMs);
|
|
531
|
-
}),
|
|
532
|
-
]);
|
|
533
|
-
if (timeoutId) {
|
|
534
|
-
clearTimeout(timeoutId);
|
|
535
|
-
timeoutId = null;
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
catch (httpError) {
|
|
539
|
-
// Fallback to SSE
|
|
540
|
-
try {
|
|
541
|
-
await client.close();
|
|
542
|
-
}
|
|
543
|
-
catch { }
|
|
544
|
-
if (connectionAborted) {
|
|
545
|
-
throw new Error('Connection aborted due to timeout');
|
|
546
|
-
}
|
|
547
|
-
client = new Client({
|
|
548
|
-
name: `snow-cli-${serviceName}`,
|
|
549
|
-
version: '1.0.0',
|
|
550
|
-
}, {
|
|
551
|
-
capabilities: {},
|
|
552
|
-
});
|
|
553
|
-
transport = new SSEClientTransport(url);
|
|
554
|
-
await Promise.race([
|
|
555
|
-
client.connect(transport),
|
|
556
|
-
new Promise((_, reject) => {
|
|
557
|
-
timeoutId = setTimeout(() => {
|
|
558
|
-
abortConnection();
|
|
559
|
-
reject(new Error('SSE connection timeout'));
|
|
560
|
-
}, timeoutMs);
|
|
561
|
-
}),
|
|
562
|
-
]);
|
|
563
|
-
if (timeoutId) {
|
|
564
|
-
clearTimeout(timeoutId);
|
|
565
|
-
timeoutId = null;
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
else if (server.command) {
|
|
570
|
-
const processEnv = {};
|
|
571
|
-
Object.entries(process.env).forEach(([key, value]) => {
|
|
572
|
-
if (value !== undefined) {
|
|
573
|
-
processEnv[key] = value;
|
|
574
|
-
}
|
|
575
|
-
});
|
|
576
|
-
if (server.env) {
|
|
577
|
-
Object.assign(processEnv, server.env);
|
|
578
|
-
}
|
|
579
|
-
transport = new StdioClientTransport({
|
|
580
|
-
command: server.command,
|
|
581
|
-
args: server.args || [],
|
|
582
|
-
env: processEnv,
|
|
583
|
-
});
|
|
584
|
-
await client.connect(transport);
|
|
585
|
-
}
|
|
586
|
-
else {
|
|
587
|
-
throw new Error('No URL or command specified');
|
|
588
|
-
}
|
|
589
|
-
// Get tools from the service
|
|
590
|
-
const toolsResult = await Promise.race([
|
|
591
|
-
client.listTools(),
|
|
592
|
-
new Promise((_, reject) => {
|
|
593
|
-
timeoutId = setTimeout(() => {
|
|
594
|
-
abortConnection();
|
|
595
|
-
reject(new Error('ListTools timeout'));
|
|
596
|
-
}, timeoutMs);
|
|
597
|
-
}),
|
|
598
|
-
]);
|
|
599
|
-
if (timeoutId) {
|
|
600
|
-
clearTimeout(timeoutId);
|
|
601
|
-
timeoutId = null;
|
|
602
|
-
}
|
|
603
|
-
return (toolsResult.tools?.map(tool => ({
|
|
604
|
-
name: tool.name,
|
|
605
|
-
description: tool.description || '',
|
|
606
|
-
inputSchema: tool.inputSchema,
|
|
607
|
-
})) || []);
|
|
608
|
-
}
|
|
609
|
-
finally {
|
|
610
|
-
// Clean up timeout
|
|
611
|
-
if (timeoutId) {
|
|
612
|
-
clearTimeout(timeoutId);
|
|
613
|
-
}
|
|
614
|
-
try {
|
|
615
|
-
if (client) {
|
|
616
|
-
await Promise.race([
|
|
617
|
-
client.close(),
|
|
618
|
-
new Promise(resolve => setTimeout(resolve, 1000)), // Max 1s for cleanup
|
|
619
|
-
]);
|
|
620
|
-
resourceMonitor.trackMCPConnectionClosed(serviceName);
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
catch (error) {
|
|
624
|
-
logger.warn(`Failed to close client for ${serviceName}:`, error);
|
|
625
|
-
resourceMonitor.trackMCPConnectionClosed(serviceName); // Track even on error
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
/**
|
|
630
|
-
* Execute an MCP tool by parsing the prefixed tool name
|
|
631
|
-
* Only connects to the service when actually needed
|
|
632
|
-
*/
|
|
633
|
-
export async function executeMCPTool(toolName, args, abortSignal, onTokenUpdate) {
|
|
634
|
-
// Find the service name by checking against known services
|
|
635
|
-
let serviceName = null;
|
|
636
|
-
let actualToolName = null;
|
|
637
|
-
// Check built-in services first
|
|
638
|
-
if (toolName.startsWith('todo-')) {
|
|
639
|
-
serviceName = 'todo';
|
|
640
|
-
actualToolName = toolName.substring('todo-'.length);
|
|
641
|
-
}
|
|
642
|
-
else if (toolName.startsWith('notebook-')) {
|
|
643
|
-
serviceName = 'notebook';
|
|
644
|
-
actualToolName = toolName.substring('notebook-'.length);
|
|
645
|
-
}
|
|
646
|
-
else if (toolName.startsWith('filesystem-')) {
|
|
647
|
-
serviceName = 'filesystem';
|
|
648
|
-
actualToolName = toolName.substring('filesystem-'.length);
|
|
649
|
-
}
|
|
650
|
-
else if (toolName.startsWith('terminal-')) {
|
|
651
|
-
serviceName = 'terminal';
|
|
652
|
-
actualToolName = toolName.substring('terminal-'.length);
|
|
653
|
-
}
|
|
654
|
-
else if (toolName.startsWith('ace-')) {
|
|
655
|
-
serviceName = 'ace';
|
|
656
|
-
actualToolName = toolName.substring('ace-'.length);
|
|
657
|
-
}
|
|
658
|
-
else if (toolName.startsWith('websearch-')) {
|
|
659
|
-
serviceName = 'websearch';
|
|
660
|
-
actualToolName = toolName.substring('websearch-'.length);
|
|
661
|
-
}
|
|
662
|
-
else if (toolName.startsWith('ide-')) {
|
|
663
|
-
serviceName = 'ide';
|
|
664
|
-
actualToolName = toolName.substring('ide-'.length);
|
|
665
|
-
}
|
|
666
|
-
else if (toolName.startsWith('codebase-')) {
|
|
667
|
-
serviceName = 'codebase';
|
|
668
|
-
actualToolName = toolName.substring('codebase-'.length);
|
|
669
|
-
}
|
|
670
|
-
else if (toolName.startsWith('subagent-')) {
|
|
671
|
-
serviceName = 'subagent';
|
|
672
|
-
actualToolName = toolName.substring('subagent-'.length);
|
|
673
|
-
}
|
|
674
|
-
else {
|
|
675
|
-
// Check configured MCP services
|
|
676
|
-
try {
|
|
677
|
-
const mcpConfig = getMCPConfig();
|
|
678
|
-
for (const configuredServiceName of Object.keys(mcpConfig.mcpServers)) {
|
|
679
|
-
const prefix = `${configuredServiceName}-`;
|
|
680
|
-
if (toolName.startsWith(prefix)) {
|
|
681
|
-
serviceName = configuredServiceName;
|
|
682
|
-
actualToolName = toolName.substring(prefix.length);
|
|
683
|
-
break;
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
catch {
|
|
688
|
-
// Ignore config errors, will handle below
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
if (!serviceName || !actualToolName) {
|
|
692
|
-
throw new Error(`Invalid tool name format: ${toolName}. Expected format: serviceName-toolName`);
|
|
693
|
-
}
|
|
694
|
-
if (serviceName === 'todo') {
|
|
695
|
-
// Handle built-in TODO tools (no connection needed)
|
|
696
|
-
return await getTodoService().executeTool(actualToolName, args);
|
|
697
|
-
}
|
|
698
|
-
else if (serviceName === 'notebook') {
|
|
699
|
-
// Handle built-in Notebook tools (no connection needed)
|
|
700
|
-
return await executeNotebookTool(toolName, args);
|
|
701
|
-
}
|
|
702
|
-
else if (serviceName === 'filesystem') {
|
|
703
|
-
// Handle built-in filesystem tools (no connection needed)
|
|
704
|
-
const { filesystemService } = await import('../mcp/filesystem.js');
|
|
705
|
-
switch (actualToolName) {
|
|
706
|
-
case 'read':
|
|
707
|
-
return await filesystemService.getFileContent(args.filePath, args.startLine, args.endLine);
|
|
708
|
-
case 'create':
|
|
709
|
-
return await filesystemService.createFile(args.filePath, args.content, args.createDirectories);
|
|
710
|
-
case 'exists':
|
|
711
|
-
return await filesystemService.exists(args.filePath);
|
|
712
|
-
case 'info':
|
|
713
|
-
return await filesystemService.getFileInfo(args.filePath);
|
|
714
|
-
case 'edit':
|
|
715
|
-
return await filesystemService.editFile(args.filePath, args.startLine, args.endLine, args.newContent, args.contextLines);
|
|
716
|
-
case 'edit_search':
|
|
717
|
-
return await filesystemService.editFileBySearch(args.filePath, args.searchContent, args.replaceContent, args.occurrence, args.contextLines);
|
|
718
|
-
default:
|
|
719
|
-
throw new Error(`Unknown filesystem tool: ${actualToolName}`);
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
else if (serviceName === 'terminal') {
|
|
723
|
-
// Handle built-in terminal tools (no connection needed)
|
|
724
|
-
const { terminalService } = await import('../mcp/bash.js');
|
|
725
|
-
switch (actualToolName) {
|
|
726
|
-
case 'execute':
|
|
727
|
-
return await terminalService.executeCommand(args.command, args.timeout);
|
|
728
|
-
default:
|
|
729
|
-
throw new Error(`Unknown terminal tool: ${actualToolName}`);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
else if (serviceName === 'ace') {
|
|
733
|
-
// Handle built-in ACE Code Search tools (no connection needed)
|
|
734
|
-
const { aceCodeSearchService } = await import('../mcp/aceCodeSearch.js');
|
|
735
|
-
switch (actualToolName) {
|
|
736
|
-
case 'search_symbols':
|
|
737
|
-
return await aceCodeSearchService.searchSymbols(args.query, args.symbolType, args.language, args.maxResults);
|
|
738
|
-
case 'find_definition':
|
|
739
|
-
return await aceCodeSearchService.findDefinition(args.symbolName, args.contextFile);
|
|
740
|
-
case 'find_references':
|
|
741
|
-
return await aceCodeSearchService.findReferences(args.symbolName, args.maxResults);
|
|
742
|
-
case 'semantic_search':
|
|
743
|
-
return await aceCodeSearchService.semanticSearch(args.query, args.searchType, args.language, args.maxResults);
|
|
744
|
-
case 'file_outline':
|
|
745
|
-
return await aceCodeSearchService.getFileOutline(args.filePath);
|
|
746
|
-
case 'text_search':
|
|
747
|
-
return await aceCodeSearchService.textSearch(args.pattern, args.fileGlob, args.isRegex, args.maxResults);
|
|
748
|
-
default:
|
|
749
|
-
throw new Error(`Unknown ACE tool: ${actualToolName}`);
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
else if (serviceName === 'websearch') {
|
|
753
|
-
// Handle built-in Web Search tools (no connection needed)
|
|
754
|
-
const { webSearchService } = await import('../mcp/websearch.js');
|
|
755
|
-
switch (actualToolName) {
|
|
756
|
-
case 'search':
|
|
757
|
-
const searchResponse = await webSearchService.search(args.query, args.maxResults);
|
|
758
|
-
// Return object directly, will be JSON.stringify in API layer
|
|
759
|
-
return searchResponse;
|
|
760
|
-
case 'fetch':
|
|
761
|
-
const pageContent = await webSearchService.fetchPage(args.url, args.maxLength, args.userQuery, // Pass optional userQuery parameter
|
|
762
|
-
abortSignal, // Pass abort signal
|
|
763
|
-
onTokenUpdate);
|
|
764
|
-
// Return object directly, will be JSON.stringify in API layer
|
|
765
|
-
return pageContent;
|
|
766
|
-
default:
|
|
767
|
-
throw new Error(`Unknown websearch tool: ${actualToolName}`);
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
else if (serviceName === 'ide') {
|
|
771
|
-
// Handle built-in IDE Diagnostics tools (no connection needed)
|
|
772
|
-
const { ideDiagnosticsService } = await import('../mcp/ideDiagnostics.js');
|
|
773
|
-
switch (actualToolName) {
|
|
774
|
-
case 'get_diagnostics':
|
|
775
|
-
const diagnostics = await ideDiagnosticsService.getDiagnostics(args.filePath);
|
|
776
|
-
// Format diagnostics for better readability
|
|
777
|
-
const formatted = ideDiagnosticsService.formatDiagnostics(diagnostics, args.filePath);
|
|
778
|
-
return {
|
|
779
|
-
diagnostics,
|
|
780
|
-
formatted,
|
|
781
|
-
summary: `Found ${diagnostics.length} diagnostic(s) in ${args.filePath}`,
|
|
782
|
-
};
|
|
783
|
-
default:
|
|
784
|
-
throw new Error(`Unknown IDE tool: ${actualToolName}`);
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
else if (serviceName === 'codebase') {
|
|
788
|
-
// Handle built-in Codebase Search tools (no connection needed)
|
|
789
|
-
const { codebaseSearchService } = await import('../mcp/codebaseSearch.js');
|
|
790
|
-
switch (actualToolName) {
|
|
791
|
-
case 'search':
|
|
792
|
-
return await codebaseSearchService.search(args.query, args.topN);
|
|
793
|
-
default:
|
|
794
|
-
throw new Error(`Unknown codebase tool: ${actualToolName}`);
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
else if (serviceName === 'subagent') {
|
|
798
|
-
// Handle sub-agent tools
|
|
799
|
-
// actualToolName is the agent ID
|
|
800
|
-
const result = await subAgentService.execute({
|
|
801
|
-
agentId: actualToolName,
|
|
802
|
-
prompt: args.prompt,
|
|
803
|
-
abortSignal,
|
|
804
|
-
});
|
|
805
|
-
return result;
|
|
806
|
-
}
|
|
807
|
-
else {
|
|
808
|
-
// Handle user-configured MCP service tools - connect only when needed
|
|
809
|
-
const mcpConfig = getMCPConfig();
|
|
810
|
-
const server = mcpConfig.mcpServers[serviceName];
|
|
811
|
-
if (!server) {
|
|
812
|
-
throw new Error(`MCP service not found: ${serviceName}`);
|
|
813
|
-
}
|
|
814
|
-
// Connect to service and execute tool
|
|
815
|
-
logger.info(`Executing tool ${actualToolName} on MCP service ${serviceName}... args: ${args ? JSON.stringify(args) : 'none'}`);
|
|
816
|
-
return await executeOnExternalMCPService(serviceName, server, actualToolName, args);
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
/**
|
|
820
|
-
* Execute a tool on an external MCP service - connects only when needed
|
|
821
|
-
*/
|
|
822
|
-
async function executeOnExternalMCPService(serviceName, server, toolName, args) {
|
|
823
|
-
let client = null;
|
|
824
|
-
logger.debug(`Connecting to MCP service ${serviceName} to execute tool ${toolName}...`);
|
|
825
|
-
try {
|
|
826
|
-
client = new Client({
|
|
827
|
-
name: `snow-cli-${serviceName}`,
|
|
828
|
-
version: '1.0.0',
|
|
829
|
-
}, {
|
|
830
|
-
capabilities: {},
|
|
831
|
-
});
|
|
832
|
-
resourceMonitor.trackMCPConnectionOpened(serviceName);
|
|
833
|
-
// Setup transport (similar to getServiceTools)
|
|
834
|
-
let transport;
|
|
835
|
-
if (server.url) {
|
|
836
|
-
let urlString = server.url;
|
|
837
|
-
if (server.env) {
|
|
838
|
-
const allEnv = { ...process.env, ...server.env };
|
|
839
|
-
urlString = urlString.replace(/\$\{([^}]+)\}|\$([A-Za-z_][A-Za-z0-9_]*)/g, (match, braced, simple) => {
|
|
840
|
-
const varName = braced || simple;
|
|
841
|
-
return allEnv[varName] || match;
|
|
842
|
-
});
|
|
843
|
-
}
|
|
844
|
-
const url = new URL(urlString);
|
|
845
|
-
transport = new StreamableHTTPClientTransport(url);
|
|
846
|
-
}
|
|
847
|
-
else if (server.command) {
|
|
848
|
-
transport = new StdioClientTransport({
|
|
849
|
-
command: server.command,
|
|
850
|
-
args: server.args || [],
|
|
851
|
-
env: server.env
|
|
852
|
-
? { ...process.env, ...server.env }
|
|
853
|
-
: process.env,
|
|
854
|
-
});
|
|
855
|
-
}
|
|
856
|
-
await client.connect(transport);
|
|
857
|
-
logger.debug(`ToolName ${toolName}, args:`, args ? JSON.stringify(args) : 'none');
|
|
858
|
-
// Execute the tool with the original tool name (not prefixed)
|
|
859
|
-
const result = await client.callTool({
|
|
860
|
-
name: toolName,
|
|
861
|
-
arguments: args,
|
|
862
|
-
});
|
|
863
|
-
logger.debug(`result from ${serviceName} tool ${toolName}:`, result);
|
|
864
|
-
return result.content;
|
|
865
|
-
}
|
|
866
|
-
finally {
|
|
867
|
-
try {
|
|
868
|
-
if (client) {
|
|
869
|
-
await client.close();
|
|
870
|
-
resourceMonitor.trackMCPConnectionClosed(serviceName);
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
catch (error) {
|
|
874
|
-
logger.warn(`Failed to close client for ${serviceName}:`, error);
|
|
875
|
-
resourceMonitor.trackMCPConnectionClosed(serviceName); // Track even on error
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
}
|