snow-ai 0.4.16 → 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 -384
- 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 -1519
- 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
package/dist/api/anthropic.js
DELETED
|
@@ -1,598 +0,0 @@
|
|
|
1
|
-
import { createHash, randomUUID } from 'crypto';
|
|
2
|
-
import { getOpenAiConfig, getCustomSystemPrompt, getCustomHeaders, } from '../utils/apiConfig.js';
|
|
3
|
-
import { getSystemPrompt } from './systemPrompt.js';
|
|
4
|
-
import { withRetryGenerator, parseJsonWithFix } from '../utils/retryUtils.js';
|
|
5
|
-
import { logger } from '../utils/logger.js';
|
|
6
|
-
import { addProxyToFetchOptions } from '../utils/proxyUtils.js';
|
|
7
|
-
import { saveUsageToFile } from '../utils/usageLogger.js';
|
|
8
|
-
import { isDevMode, getDevUserId } from '../utils/devMode.js';
|
|
9
|
-
let anthropicConfig = null;
|
|
10
|
-
// Persistent userId that remains the same until application restart
|
|
11
|
-
let persistentUserId = null;
|
|
12
|
-
function getAnthropicConfig() {
|
|
13
|
-
if (!anthropicConfig) {
|
|
14
|
-
const config = getOpenAiConfig();
|
|
15
|
-
if (!config.apiKey) {
|
|
16
|
-
throw new Error('Anthropic API configuration is incomplete. Please configure API key first.');
|
|
17
|
-
}
|
|
18
|
-
const customHeaders = getCustomHeaders();
|
|
19
|
-
anthropicConfig = {
|
|
20
|
-
apiKey: config.apiKey,
|
|
21
|
-
baseUrl: config.baseUrl && config.baseUrl !== 'https://api.openai.com/v1'
|
|
22
|
-
? config.baseUrl
|
|
23
|
-
: 'https://api.anthropic.com/v1',
|
|
24
|
-
customHeaders,
|
|
25
|
-
anthropicBeta: config.anthropicBeta,
|
|
26
|
-
thinking: config.thinking,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
return anthropicConfig;
|
|
30
|
-
}
|
|
31
|
-
export function resetAnthropicClient() {
|
|
32
|
-
anthropicConfig = null;
|
|
33
|
-
persistentUserId = null; // Reset userId on client reset
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Generate a persistent user_id that remains the same until application restart
|
|
37
|
-
* Format: user_<hash>_account__session_<uuid>
|
|
38
|
-
* This matches Anthropic's expected format for tracking and caching
|
|
39
|
-
*
|
|
40
|
-
* In dev mode (--dev flag), uses a persistent userId from ~/.snow/dev-user-id
|
|
41
|
-
* instead of generating a new one each session
|
|
42
|
-
*/
|
|
43
|
-
function getPersistentUserId() {
|
|
44
|
-
// Check if dev mode is enabled
|
|
45
|
-
if (isDevMode()) {
|
|
46
|
-
return getDevUserId();
|
|
47
|
-
}
|
|
48
|
-
// Normal mode: generate userId per session
|
|
49
|
-
if (!persistentUserId) {
|
|
50
|
-
const sessionId = randomUUID();
|
|
51
|
-
const hash = createHash('sha256')
|
|
52
|
-
.update(`anthropic_user_${sessionId}`)
|
|
53
|
-
.digest('hex');
|
|
54
|
-
persistentUserId = `user_${hash}_account__session_${sessionId}`;
|
|
55
|
-
}
|
|
56
|
-
return persistentUserId;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Convert OpenAI-style tools to Anthropic tool format
|
|
60
|
-
* Adds cache_control to the last tool for prompt caching
|
|
61
|
-
*/
|
|
62
|
-
function convertToolsToAnthropic(tools) {
|
|
63
|
-
if (!tools || tools.length === 0) {
|
|
64
|
-
return undefined;
|
|
65
|
-
}
|
|
66
|
-
const convertedTools = tools
|
|
67
|
-
.filter(tool => tool.type === 'function' && 'function' in tool)
|
|
68
|
-
.map(tool => {
|
|
69
|
-
if (tool.type === 'function' && 'function' in tool) {
|
|
70
|
-
return {
|
|
71
|
-
name: tool.function.name,
|
|
72
|
-
description: tool.function.description || '',
|
|
73
|
-
input_schema: tool.function.parameters,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
throw new Error('Invalid tool format');
|
|
77
|
-
});
|
|
78
|
-
if (convertedTools.length > 0) {
|
|
79
|
-
const lastTool = convertedTools[convertedTools.length - 1];
|
|
80
|
-
lastTool.cache_control = { type: 'ephemeral' };
|
|
81
|
-
}
|
|
82
|
-
return convertedTools;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Convert our ChatMessage format to Anthropic's message format
|
|
86
|
-
* Adds cache_control to system prompt and last user message for prompt caching
|
|
87
|
-
* @param messages - The messages to convert
|
|
88
|
-
* @param includeBuiltinSystemPrompt - Whether to include builtin system prompt (default true)
|
|
89
|
-
*/
|
|
90
|
-
function convertToAnthropicMessages(messages, includeBuiltinSystemPrompt = true) {
|
|
91
|
-
const customSystemPrompt = getCustomSystemPrompt();
|
|
92
|
-
let systemContent;
|
|
93
|
-
const anthropicMessages = [];
|
|
94
|
-
for (const msg of messages) {
|
|
95
|
-
if (msg.role === 'system') {
|
|
96
|
-
systemContent = msg.content;
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
if (msg.role === 'tool' && msg.tool_call_id) {
|
|
100
|
-
// Build tool_result content - can be text or array with images
|
|
101
|
-
let toolResultContent;
|
|
102
|
-
if (msg.images && msg.images.length > 0) {
|
|
103
|
-
// Multimodal tool result with images
|
|
104
|
-
const contentArray = [];
|
|
105
|
-
// Add text content first
|
|
106
|
-
if (msg.content) {
|
|
107
|
-
contentArray.push({
|
|
108
|
-
type: 'text',
|
|
109
|
-
text: msg.content,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
// Add images
|
|
113
|
-
for (const image of msg.images) {
|
|
114
|
-
contentArray.push({
|
|
115
|
-
type: 'image',
|
|
116
|
-
source: {
|
|
117
|
-
type: 'base64',
|
|
118
|
-
media_type: image.mimeType,
|
|
119
|
-
data: image.data,
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
toolResultContent = contentArray;
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
// Text-only tool result
|
|
127
|
-
toolResultContent = msg.content;
|
|
128
|
-
}
|
|
129
|
-
anthropicMessages.push({
|
|
130
|
-
role: 'user',
|
|
131
|
-
content: [
|
|
132
|
-
{
|
|
133
|
-
type: 'tool_result',
|
|
134
|
-
tool_use_id: msg.tool_call_id,
|
|
135
|
-
content: toolResultContent,
|
|
136
|
-
},
|
|
137
|
-
],
|
|
138
|
-
});
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
if (msg.role === 'user' && msg.images && msg.images.length > 0) {
|
|
142
|
-
const content = [];
|
|
143
|
-
if (msg.content) {
|
|
144
|
-
content.push({
|
|
145
|
-
type: 'text',
|
|
146
|
-
text: msg.content,
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
for (const image of msg.images) {
|
|
150
|
-
const base64Match = image.data.match(/^data:([^;]+);base64,(.+)$/);
|
|
151
|
-
if (base64Match) {
|
|
152
|
-
content.push({
|
|
153
|
-
type: 'image',
|
|
154
|
-
source: {
|
|
155
|
-
type: 'base64',
|
|
156
|
-
media_type: base64Match[1] || image.mimeType,
|
|
157
|
-
data: base64Match[2] || '',
|
|
158
|
-
},
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
anthropicMessages.push({
|
|
163
|
-
role: 'user',
|
|
164
|
-
content,
|
|
165
|
-
});
|
|
166
|
-
continue;
|
|
167
|
-
}
|
|
168
|
-
if (msg.role === 'assistant' &&
|
|
169
|
-
msg.tool_calls &&
|
|
170
|
-
msg.tool_calls.length > 0) {
|
|
171
|
-
const content = [];
|
|
172
|
-
// When thinking is enabled, thinking block must come first
|
|
173
|
-
if (msg.thinking) {
|
|
174
|
-
// Use the complete thinking block object (includes signature)
|
|
175
|
-
content.push(msg.thinking);
|
|
176
|
-
}
|
|
177
|
-
if (msg.content) {
|
|
178
|
-
content.push({
|
|
179
|
-
type: 'text',
|
|
180
|
-
text: msg.content,
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
for (const toolCall of msg.tool_calls) {
|
|
184
|
-
content.push({
|
|
185
|
-
type: 'tool_use',
|
|
186
|
-
id: toolCall.id,
|
|
187
|
-
name: toolCall.function.name,
|
|
188
|
-
input: JSON.parse(toolCall.function.arguments),
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
anthropicMessages.push({
|
|
192
|
-
role: 'assistant',
|
|
193
|
-
content,
|
|
194
|
-
});
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
if (msg.role === 'user' || msg.role === 'assistant') {
|
|
198
|
-
// For assistant messages with thinking, convert to structured format
|
|
199
|
-
if (msg.role === 'assistant' && msg.thinking) {
|
|
200
|
-
const content = [];
|
|
201
|
-
// Thinking block must come first - use complete block object (includes signature)
|
|
202
|
-
content.push(msg.thinking);
|
|
203
|
-
// Then text content
|
|
204
|
-
if (msg.content) {
|
|
205
|
-
content.push({
|
|
206
|
-
type: 'text',
|
|
207
|
-
text: msg.content,
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
anthropicMessages.push({
|
|
211
|
-
role: 'assistant',
|
|
212
|
-
content,
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
anthropicMessages.push({
|
|
217
|
-
role: msg.role,
|
|
218
|
-
content: msg.content,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
// 如果配置了自定义系统提示词(最高优先级,始终添加)
|
|
224
|
-
if (customSystemPrompt) {
|
|
225
|
-
systemContent = customSystemPrompt;
|
|
226
|
-
if (includeBuiltinSystemPrompt) {
|
|
227
|
-
// 将默认系统提示词作为第一条用户消息
|
|
228
|
-
anthropicMessages.unshift({
|
|
229
|
-
role: 'user',
|
|
230
|
-
content: [
|
|
231
|
-
{
|
|
232
|
-
type: 'text',
|
|
233
|
-
text: getSystemPrompt(),
|
|
234
|
-
cache_control: { type: 'ephemeral' },
|
|
235
|
-
},
|
|
236
|
-
],
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
else if (!systemContent && includeBuiltinSystemPrompt) {
|
|
241
|
-
// 没有自定义系统提示词,但需要添加默认系统提示词
|
|
242
|
-
systemContent = getSystemPrompt();
|
|
243
|
-
}
|
|
244
|
-
let lastUserMessageIndex = -1;
|
|
245
|
-
for (let i = anthropicMessages.length - 1; i >= 0; i--) {
|
|
246
|
-
if (anthropicMessages[i]?.role === 'user') {
|
|
247
|
-
if (customSystemPrompt && i === 0) {
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
lastUserMessageIndex = i;
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
if (lastUserMessageIndex >= 0) {
|
|
255
|
-
const lastMessage = anthropicMessages[lastUserMessageIndex];
|
|
256
|
-
if (lastMessage && lastMessage.role === 'user') {
|
|
257
|
-
if (typeof lastMessage.content === 'string') {
|
|
258
|
-
lastMessage.content = [
|
|
259
|
-
{
|
|
260
|
-
type: 'text',
|
|
261
|
-
text: lastMessage.content,
|
|
262
|
-
cache_control: { type: 'ephemeral' },
|
|
263
|
-
},
|
|
264
|
-
];
|
|
265
|
-
}
|
|
266
|
-
else if (Array.isArray(lastMessage.content)) {
|
|
267
|
-
const lastContentIndex = lastMessage.content.length - 1;
|
|
268
|
-
if (lastContentIndex >= 0) {
|
|
269
|
-
const lastContent = lastMessage.content[lastContentIndex];
|
|
270
|
-
lastContent.cache_control = { type: 'ephemeral' };
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
const system = systemContent
|
|
276
|
-
? [
|
|
277
|
-
{
|
|
278
|
-
type: 'text',
|
|
279
|
-
text: systemContent,
|
|
280
|
-
cache_control: { type: 'ephemeral' },
|
|
281
|
-
},
|
|
282
|
-
]
|
|
283
|
-
: undefined;
|
|
284
|
-
return { system, messages: anthropicMessages };
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Parse Server-Sent Events (SSE) stream
|
|
288
|
-
*/
|
|
289
|
-
async function* parseSSEStream(reader) {
|
|
290
|
-
const decoder = new TextDecoder();
|
|
291
|
-
let buffer = '';
|
|
292
|
-
try {
|
|
293
|
-
while (true) {
|
|
294
|
-
const { done, value } = await reader.read();
|
|
295
|
-
if (done) {
|
|
296
|
-
// ✅ 关键修复:检查buffer是否有残留数据
|
|
297
|
-
if (buffer.trim()) {
|
|
298
|
-
// 连接异常中断,抛出明确错误
|
|
299
|
-
throw new Error(`Stream terminated unexpectedly with incomplete data: ${buffer.substring(0, 100)}...`);
|
|
300
|
-
}
|
|
301
|
-
break; // 正常结束
|
|
302
|
-
}
|
|
303
|
-
buffer += decoder.decode(value, { stream: true });
|
|
304
|
-
const lines = buffer.split('\n');
|
|
305
|
-
buffer = lines.pop() || '';
|
|
306
|
-
for (const line of lines) {
|
|
307
|
-
const trimmed = line.trim();
|
|
308
|
-
if (!trimmed || trimmed.startsWith(':'))
|
|
309
|
-
continue;
|
|
310
|
-
if (trimmed === 'data: [DONE]' || trimmed === 'data:[DONE]') {
|
|
311
|
-
return;
|
|
312
|
-
}
|
|
313
|
-
// Handle both "event: " and "event:" formats
|
|
314
|
-
if (trimmed.startsWith('event:')) {
|
|
315
|
-
// Event type, will be followed by data
|
|
316
|
-
continue;
|
|
317
|
-
}
|
|
318
|
-
// Handle both "data: " and "data:" formats
|
|
319
|
-
if (trimmed.startsWith('data:')) {
|
|
320
|
-
const data = trimmed.startsWith('data: ')
|
|
321
|
-
? trimmed.slice(6)
|
|
322
|
-
: trimmed.slice(5);
|
|
323
|
-
const parseResult = parseJsonWithFix(data, {
|
|
324
|
-
toolName: 'SSE stream',
|
|
325
|
-
logWarning: false,
|
|
326
|
-
logError: true,
|
|
327
|
-
});
|
|
328
|
-
if (parseResult.success) {
|
|
329
|
-
yield parseResult.data;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
catch (error) {
|
|
336
|
-
const { logger } = await import('../utils/logger.js');
|
|
337
|
-
logger.error('SSE stream parsing error:', {
|
|
338
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
339
|
-
remainingBuffer: buffer.substring(0, 200),
|
|
340
|
-
});
|
|
341
|
-
throw error;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
export async function* createStreamingAnthropicCompletion(options, abortSignal, onRetry) {
|
|
345
|
-
yield* withRetryGenerator(async function* () {
|
|
346
|
-
const config = getAnthropicConfig();
|
|
347
|
-
const { system, messages } = convertToAnthropicMessages(options.messages, options.includeBuiltinSystemPrompt !== false);
|
|
348
|
-
// Use persistent userId that remains the same until application restart
|
|
349
|
-
const userId = getPersistentUserId();
|
|
350
|
-
const requestBody = {
|
|
351
|
-
model: options.model,
|
|
352
|
-
max_tokens: options.max_tokens || 4096,
|
|
353
|
-
system,
|
|
354
|
-
messages,
|
|
355
|
-
tools: convertToolsToAnthropic(options.tools),
|
|
356
|
-
metadata: {
|
|
357
|
-
user_id: userId,
|
|
358
|
-
},
|
|
359
|
-
stream: true,
|
|
360
|
-
};
|
|
361
|
-
// Add thinking configuration if enabled and not explicitly disabled
|
|
362
|
-
// When thinking is enabled, temperature must be 1
|
|
363
|
-
// Note: agents and other internal tools should set disableThinking=true
|
|
364
|
-
if (config.thinking && !options.disableThinking) {
|
|
365
|
-
requestBody.thinking = config.thinking;
|
|
366
|
-
requestBody.temperature = 1;
|
|
367
|
-
}
|
|
368
|
-
// Prepare headers
|
|
369
|
-
const headers = {
|
|
370
|
-
'Content-Type': 'application/json',
|
|
371
|
-
'x-api-key': config.apiKey,
|
|
372
|
-
Authorization: `Bearer ${config.apiKey}`,
|
|
373
|
-
'anthropic-version': '2023-06-01',
|
|
374
|
-
'x-snow': 'true',
|
|
375
|
-
...config.customHeaders,
|
|
376
|
-
};
|
|
377
|
-
// Add beta parameter if configured
|
|
378
|
-
// if (config.anthropicBeta) {
|
|
379
|
-
// headers['anthropic-beta'] = 'prompt-caching-2024-07-31';
|
|
380
|
-
// }
|
|
381
|
-
const url = config.anthropicBeta
|
|
382
|
-
? `${config.baseUrl}/messages?beta=true`
|
|
383
|
-
: `${config.baseUrl}/messages`;
|
|
384
|
-
const fetchOptions = addProxyToFetchOptions(url, {
|
|
385
|
-
method: 'POST',
|
|
386
|
-
headers,
|
|
387
|
-
body: JSON.stringify(requestBody),
|
|
388
|
-
signal: abortSignal,
|
|
389
|
-
});
|
|
390
|
-
const response = await fetch(url, fetchOptions);
|
|
391
|
-
if (!response.ok) {
|
|
392
|
-
const errorText = await response.text();
|
|
393
|
-
throw new Error(`Anthropic API error: ${response.status} ${response.statusText} - ${errorText}`);
|
|
394
|
-
}
|
|
395
|
-
if (!response.body) {
|
|
396
|
-
throw new Error('No response body from Anthropic API');
|
|
397
|
-
}
|
|
398
|
-
let contentBuffer = '';
|
|
399
|
-
let thinkingTextBuffer = ''; // Accumulate thinking text content
|
|
400
|
-
let thinkingSignature = ''; // Accumulate thinking signature
|
|
401
|
-
let toolCallsBuffer = new Map();
|
|
402
|
-
let hasToolCalls = false;
|
|
403
|
-
let usageData;
|
|
404
|
-
let blockIndexToId = new Map();
|
|
405
|
-
let blockIndexToType = new Map(); // Track block types (text, thinking, tool_use)
|
|
406
|
-
let completedToolBlocks = new Set(); // Track which tool blocks have finished streaming
|
|
407
|
-
for await (const event of parseSSEStream(response.body.getReader())) {
|
|
408
|
-
if (abortSignal?.aborted) {
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
if (event.type === 'content_block_start') {
|
|
412
|
-
const block = event.content_block;
|
|
413
|
-
const blockIndex = event.index;
|
|
414
|
-
// Track block type for later reference
|
|
415
|
-
blockIndexToType.set(blockIndex, block.type);
|
|
416
|
-
if (block.type === 'tool_use') {
|
|
417
|
-
hasToolCalls = true;
|
|
418
|
-
blockIndexToId.set(blockIndex, block.id);
|
|
419
|
-
toolCallsBuffer.set(block.id, {
|
|
420
|
-
id: block.id,
|
|
421
|
-
type: 'function',
|
|
422
|
-
function: {
|
|
423
|
-
name: block.name,
|
|
424
|
-
arguments: '',
|
|
425
|
-
},
|
|
426
|
-
});
|
|
427
|
-
yield {
|
|
428
|
-
type: 'tool_call_delta',
|
|
429
|
-
delta: block.name,
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
// Handle thinking block start (Extended Thinking feature)
|
|
433
|
-
else if (block.type === 'thinking') {
|
|
434
|
-
// Thinking block started - emit reasoning_started event
|
|
435
|
-
yield {
|
|
436
|
-
type: 'reasoning_started',
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
else if (event.type === 'content_block_delta') {
|
|
441
|
-
const delta = event.delta;
|
|
442
|
-
if (delta.type === 'text_delta') {
|
|
443
|
-
const text = delta.text;
|
|
444
|
-
contentBuffer += text;
|
|
445
|
-
yield {
|
|
446
|
-
type: 'content',
|
|
447
|
-
content: text,
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
// Handle thinking_delta (Extended Thinking feature)
|
|
451
|
-
// Emit reasoning_delta event for thinking content
|
|
452
|
-
if (delta.type === 'thinking_delta') {
|
|
453
|
-
const thinkingText = delta.thinking;
|
|
454
|
-
thinkingTextBuffer += thinkingText; // Accumulate thinking text
|
|
455
|
-
yield {
|
|
456
|
-
type: 'reasoning_delta',
|
|
457
|
-
delta: thinkingText,
|
|
458
|
-
};
|
|
459
|
-
}
|
|
460
|
-
// Handle signature_delta (Extended Thinking feature)
|
|
461
|
-
// Signature is required for thinking blocks
|
|
462
|
-
if (delta.type === 'signature_delta') {
|
|
463
|
-
thinkingSignature += delta.signature; // Accumulate signature
|
|
464
|
-
}
|
|
465
|
-
if (delta.type === 'input_json_delta') {
|
|
466
|
-
const jsonDelta = delta.partial_json;
|
|
467
|
-
const blockIndex = event.index;
|
|
468
|
-
const toolId = blockIndexToId.get(blockIndex);
|
|
469
|
-
if (toolId) {
|
|
470
|
-
const toolCall = toolCallsBuffer.get(toolId);
|
|
471
|
-
if (toolCall) {
|
|
472
|
-
// Filter out any XML-like tags that might be mixed in the JSON delta
|
|
473
|
-
// This can happen when the model output contains XML that gets interpreted as JSON
|
|
474
|
-
const cleanedDelta = jsonDelta.replace(/<\/?parameter[^>]*>/g, '');
|
|
475
|
-
if (cleanedDelta) {
|
|
476
|
-
toolCall.function.arguments += cleanedDelta;
|
|
477
|
-
yield {
|
|
478
|
-
type: 'tool_call_delta',
|
|
479
|
-
delta: cleanedDelta,
|
|
480
|
-
};
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
else if (event.type === 'content_block_stop') {
|
|
487
|
-
// Mark this block as completed
|
|
488
|
-
const blockIndex = event.index;
|
|
489
|
-
const toolId = blockIndexToId.get(blockIndex);
|
|
490
|
-
if (toolId) {
|
|
491
|
-
completedToolBlocks.add(toolId);
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
else if (event.type === 'message_start') {
|
|
495
|
-
if (event.message.usage) {
|
|
496
|
-
usageData = {
|
|
497
|
-
prompt_tokens: event.message.usage.input_tokens || 0,
|
|
498
|
-
completion_tokens: event.message.usage.output_tokens || 0,
|
|
499
|
-
total_tokens: (event.message.usage.input_tokens || 0) +
|
|
500
|
-
(event.message.usage.output_tokens || 0),
|
|
501
|
-
cache_creation_input_tokens: event.message.usage
|
|
502
|
-
.cache_creation_input_tokens,
|
|
503
|
-
cache_read_input_tokens: event.message.usage
|
|
504
|
-
.cache_read_input_tokens,
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
else if (event.type === 'message_delta') {
|
|
509
|
-
if (event.usage) {
|
|
510
|
-
if (!usageData) {
|
|
511
|
-
usageData = {
|
|
512
|
-
prompt_tokens: 0,
|
|
513
|
-
completion_tokens: 0,
|
|
514
|
-
total_tokens: 0,
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
// Update prompt_tokens if present in message_delta
|
|
518
|
-
if (event.usage.input_tokens !== undefined) {
|
|
519
|
-
usageData.prompt_tokens = event.usage.input_tokens;
|
|
520
|
-
}
|
|
521
|
-
usageData.completion_tokens = event.usage.output_tokens || 0;
|
|
522
|
-
usageData.total_tokens =
|
|
523
|
-
usageData.prompt_tokens + usageData.completion_tokens;
|
|
524
|
-
if (event.usage.cache_creation_input_tokens !== undefined) {
|
|
525
|
-
usageData.cache_creation_input_tokens = event.usage.cache_creation_input_tokens;
|
|
526
|
-
}
|
|
527
|
-
if (event.usage.cache_read_input_tokens !== undefined) {
|
|
528
|
-
usageData.cache_read_input_tokens = event.usage.cache_read_input_tokens;
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
if (hasToolCalls && toolCallsBuffer.size > 0) {
|
|
534
|
-
const toolCalls = Array.from(toolCallsBuffer.values());
|
|
535
|
-
for (const toolCall of toolCalls) {
|
|
536
|
-
// Normalize the arguments
|
|
537
|
-
let args = toolCall.function.arguments.trim();
|
|
538
|
-
// If arguments is empty, use empty object
|
|
539
|
-
if (!args) {
|
|
540
|
-
args = '{}';
|
|
541
|
-
}
|
|
542
|
-
// Try to parse the JSON using the unified parseJsonWithFix utility
|
|
543
|
-
if (completedToolBlocks.has(toolCall.id)) {
|
|
544
|
-
// Tool block was completed, parse with fix and logging
|
|
545
|
-
const parseResult = parseJsonWithFix(args, {
|
|
546
|
-
toolName: toolCall.function.name,
|
|
547
|
-
fallbackValue: {},
|
|
548
|
-
logWarning: true,
|
|
549
|
-
logError: true,
|
|
550
|
-
});
|
|
551
|
-
// Use the parsed data or fallback value
|
|
552
|
-
toolCall.function.arguments = JSON.stringify(parseResult.data);
|
|
553
|
-
}
|
|
554
|
-
else {
|
|
555
|
-
// Tool block wasn't completed, likely interrupted stream
|
|
556
|
-
// Try to parse without logging errors (incomplete data is expected)
|
|
557
|
-
const parseResult = parseJsonWithFix(args, {
|
|
558
|
-
toolName: toolCall.function.name,
|
|
559
|
-
fallbackValue: {},
|
|
560
|
-
logWarning: false,
|
|
561
|
-
logError: false,
|
|
562
|
-
});
|
|
563
|
-
if (!parseResult.success) {
|
|
564
|
-
logger.warn(`Warning: Tool call ${toolCall.function.name} (${toolCall.id}) was incomplete. Using fallback data.`);
|
|
565
|
-
}
|
|
566
|
-
toolCall.function.arguments = JSON.stringify(parseResult.data);
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
yield {
|
|
570
|
-
type: 'tool_calls',
|
|
571
|
-
tool_calls: toolCalls,
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
if (usageData) {
|
|
575
|
-
// Save usage to file system at API layer
|
|
576
|
-
saveUsageToFile(options.model, usageData);
|
|
577
|
-
yield {
|
|
578
|
-
type: 'usage',
|
|
579
|
-
usage: usageData,
|
|
580
|
-
};
|
|
581
|
-
}
|
|
582
|
-
// Return complete thinking block with signature if thinking content exists
|
|
583
|
-
const thinkingBlock = thinkingTextBuffer
|
|
584
|
-
? {
|
|
585
|
-
type: 'thinking',
|
|
586
|
-
thinking: thinkingTextBuffer,
|
|
587
|
-
signature: thinkingSignature || undefined,
|
|
588
|
-
}
|
|
589
|
-
: undefined;
|
|
590
|
-
yield {
|
|
591
|
-
type: 'done',
|
|
592
|
-
thinking: thinkingBlock,
|
|
593
|
-
};
|
|
594
|
-
}, {
|
|
595
|
-
abortSignal,
|
|
596
|
-
onRetry,
|
|
597
|
-
});
|
|
598
|
-
}
|
package/dist/api/chat.d.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import type { ChatMessage, ChatCompletionTool, ToolCall, UsageInfo, ImageContent } from './types.js';
|
|
2
|
-
export type { ChatMessage, ChatCompletionTool, ToolCall, UsageInfo, ImageContent, };
|
|
3
|
-
export interface ChatCompletionOptions {
|
|
4
|
-
model: string;
|
|
5
|
-
messages: ChatMessage[];
|
|
6
|
-
stream?: boolean;
|
|
7
|
-
temperature?: number;
|
|
8
|
-
max_tokens?: number;
|
|
9
|
-
tools?: ChatCompletionTool[];
|
|
10
|
-
tool_choice?: 'auto' | 'none' | 'required' | {
|
|
11
|
-
type: 'function';
|
|
12
|
-
function: {
|
|
13
|
-
name: string;
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
includeBuiltinSystemPrompt?: boolean;
|
|
17
|
-
}
|
|
18
|
-
export interface ChatCompletionChunk {
|
|
19
|
-
id: string;
|
|
20
|
-
object: 'chat.completion.chunk';
|
|
21
|
-
created: number;
|
|
22
|
-
model: string;
|
|
23
|
-
choices: Array<{
|
|
24
|
-
index: number;
|
|
25
|
-
delta: {
|
|
26
|
-
role?: string;
|
|
27
|
-
content?: string;
|
|
28
|
-
tool_calls?: Array<{
|
|
29
|
-
index?: number;
|
|
30
|
-
id?: string;
|
|
31
|
-
type?: 'function';
|
|
32
|
-
function?: {
|
|
33
|
-
name?: string;
|
|
34
|
-
arguments?: string;
|
|
35
|
-
};
|
|
36
|
-
}>;
|
|
37
|
-
};
|
|
38
|
-
finish_reason?: string | null;
|
|
39
|
-
}>;
|
|
40
|
-
}
|
|
41
|
-
export interface ChatCompletionMessageParam {
|
|
42
|
-
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
43
|
-
content: string | Array<{
|
|
44
|
-
type: 'text' | 'image_url';
|
|
45
|
-
text?: string;
|
|
46
|
-
image_url?: {
|
|
47
|
-
url: string;
|
|
48
|
-
};
|
|
49
|
-
}>;
|
|
50
|
-
tool_call_id?: string;
|
|
51
|
-
tool_calls?: ToolCall[];
|
|
52
|
-
}
|
|
53
|
-
export declare function resetOpenAIClient(): void;
|
|
54
|
-
export interface StreamChunk {
|
|
55
|
-
type: 'content' | 'tool_calls' | 'tool_call_delta' | 'reasoning_delta' | 'reasoning_started' | 'done' | 'usage';
|
|
56
|
-
content?: string;
|
|
57
|
-
tool_calls?: Array<{
|
|
58
|
-
id: string;
|
|
59
|
-
type: 'function';
|
|
60
|
-
function: {
|
|
61
|
-
name: string;
|
|
62
|
-
arguments: string;
|
|
63
|
-
};
|
|
64
|
-
}>;
|
|
65
|
-
delta?: string;
|
|
66
|
-
usage?: UsageInfo;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Simple streaming chat completion - only handles OpenAI interaction
|
|
70
|
-
* Tool execution should be handled by the caller
|
|
71
|
-
*/
|
|
72
|
-
export declare function createStreamingChatCompletion(options: ChatCompletionOptions, abortSignal?: AbortSignal, onRetry?: (error: Error, attempt: number, nextDelay: number) => void): AsyncGenerator<StreamChunk, void, unknown>;
|
|
73
|
-
export declare function validateChatOptions(options: ChatCompletionOptions): string[];
|