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/responses.js
DELETED
|
@@ -1,515 +0,0 @@
|
|
|
1
|
-
import { getOpenAiConfig, getCustomSystemPrompt, getCustomHeaders, } from '../utils/apiConfig.js';
|
|
2
|
-
import { getSystemPrompt } from './systemPrompt.js';
|
|
3
|
-
import { withRetryGenerator, parseJsonWithFix } from '../utils/retryUtils.js';
|
|
4
|
-
import { addProxyToFetchOptions } from '../utils/proxyUtils.js';
|
|
5
|
-
import { saveUsageToFile } from '../utils/usageLogger.js';
|
|
6
|
-
/**
|
|
7
|
-
* 确保 schema 符合 Responses API 的要求:
|
|
8
|
-
* 1. additionalProperties: false
|
|
9
|
-
* 2. 保持原有的 required 数组(不修改)
|
|
10
|
-
*/
|
|
11
|
-
function ensureStrictSchema(schema) {
|
|
12
|
-
if (!schema) {
|
|
13
|
-
return undefined;
|
|
14
|
-
}
|
|
15
|
-
// 深拷贝 schema
|
|
16
|
-
const stringified = JSON.stringify(schema);
|
|
17
|
-
const parseResult = parseJsonWithFix(stringified, {
|
|
18
|
-
toolName: 'Schema deep copy',
|
|
19
|
-
fallbackValue: schema, // 如果失败,使用原始 schema
|
|
20
|
-
logWarning: true,
|
|
21
|
-
logError: true,
|
|
22
|
-
});
|
|
23
|
-
const strictSchema = parseResult.data;
|
|
24
|
-
if (strictSchema?.['type'] === 'object') {
|
|
25
|
-
// 添加 additionalProperties: false
|
|
26
|
-
strictSchema['additionalProperties'] = false;
|
|
27
|
-
// 递归处理嵌套的 object 属性
|
|
28
|
-
if (strictSchema['properties']) {
|
|
29
|
-
for (const key of Object.keys(strictSchema['properties'])) {
|
|
30
|
-
const prop = strictSchema['properties'][key];
|
|
31
|
-
// 递归处理嵌套的 object
|
|
32
|
-
if (prop['type'] === 'object' ||
|
|
33
|
-
(Array.isArray(prop['type']) && prop['type'].includes('object'))) {
|
|
34
|
-
if (!('additionalProperties' in prop)) {
|
|
35
|
-
prop['additionalProperties'] = false;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
// 如果 properties 为空且有 required 字段,删除它
|
|
41
|
-
if (strictSchema['properties'] &&
|
|
42
|
-
Object.keys(strictSchema['properties']).length === 0 &&
|
|
43
|
-
strictSchema['required']) {
|
|
44
|
-
delete strictSchema['required'];
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return strictSchema;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* 转换 Chat Completions 格式的工具为 Responses API 格式
|
|
51
|
-
* Chat Completions: {type: 'function', function: {name, description, parameters}}
|
|
52
|
-
* Responses API: {type: 'function', name, description, parameters, strict}
|
|
53
|
-
*/
|
|
54
|
-
function convertToolsForResponses(tools) {
|
|
55
|
-
if (!tools || tools.length === 0) {
|
|
56
|
-
return undefined;
|
|
57
|
-
}
|
|
58
|
-
return tools.map(tool => ({
|
|
59
|
-
type: 'function',
|
|
60
|
-
name: tool.function.name,
|
|
61
|
-
description: tool.function.description,
|
|
62
|
-
strict: false,
|
|
63
|
-
parameters: ensureStrictSchema(tool.function.parameters),
|
|
64
|
-
}));
|
|
65
|
-
}
|
|
66
|
-
let openaiConfig = null;
|
|
67
|
-
function getOpenAIConfig() {
|
|
68
|
-
if (!openaiConfig) {
|
|
69
|
-
const config = getOpenAiConfig();
|
|
70
|
-
if (!config.apiKey || !config.baseUrl) {
|
|
71
|
-
throw new Error('OpenAI API configuration is incomplete. Please configure API settings first.');
|
|
72
|
-
}
|
|
73
|
-
const customHeaders = getCustomHeaders();
|
|
74
|
-
openaiConfig = {
|
|
75
|
-
apiKey: config.apiKey,
|
|
76
|
-
baseUrl: config.baseUrl,
|
|
77
|
-
customHeaders,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
return openaiConfig;
|
|
81
|
-
}
|
|
82
|
-
function getResponsesReasoningConfig() {
|
|
83
|
-
const config = getOpenAiConfig();
|
|
84
|
-
const reasoningConfig = config.responsesReasoning;
|
|
85
|
-
// 如果 reasoning 未启用,返回 null
|
|
86
|
-
if (!reasoningConfig?.enabled) {
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
// 返回配置,summary 永远默认为 'auto'
|
|
90
|
-
return {
|
|
91
|
-
effort: reasoningConfig.effort || 'high',
|
|
92
|
-
summary: 'auto',
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
export function resetOpenAIClient() {
|
|
96
|
-
openaiConfig = null;
|
|
97
|
-
}
|
|
98
|
-
function convertToResponseInput(messages, includeBuiltinSystemPrompt = true) {
|
|
99
|
-
const customSystemPrompt = getCustomSystemPrompt();
|
|
100
|
-
const result = [];
|
|
101
|
-
for (const msg of messages) {
|
|
102
|
-
if (!msg)
|
|
103
|
-
continue;
|
|
104
|
-
// 跳过 system 消息(不放入 input,也不放入 instructions)
|
|
105
|
-
if (msg.role === 'system') {
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
// 用户消息:content 必须是数组格式,使用 type: "message" 包裹
|
|
109
|
-
if (msg.role === 'user') {
|
|
110
|
-
const contentParts = [];
|
|
111
|
-
// 添加文本内容
|
|
112
|
-
if (msg.content) {
|
|
113
|
-
contentParts.push({
|
|
114
|
-
type: 'input_text',
|
|
115
|
-
text: msg.content,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
// 添加图片内容
|
|
119
|
-
if (msg.images && msg.images.length > 0) {
|
|
120
|
-
for (const image of msg.images) {
|
|
121
|
-
contentParts.push({
|
|
122
|
-
type: 'input_image',
|
|
123
|
-
image_url: image.data,
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
result.push({
|
|
128
|
-
type: 'message',
|
|
129
|
-
role: 'user',
|
|
130
|
-
content: contentParts,
|
|
131
|
-
});
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
// Assistant 消息(带工具调用)
|
|
135
|
-
// 在 Responses API 中,需要将工具调用转换为 function_call 类型的独立项
|
|
136
|
-
if (msg.role === 'assistant' &&
|
|
137
|
-
msg.tool_calls &&
|
|
138
|
-
msg.tool_calls.length > 0) {
|
|
139
|
-
// 为每个工具调用添加 function_call 项
|
|
140
|
-
for (const toolCall of msg.tool_calls) {
|
|
141
|
-
result.push({
|
|
142
|
-
type: 'function_call',
|
|
143
|
-
name: toolCall.function.name,
|
|
144
|
-
arguments: toolCall.function.arguments,
|
|
145
|
-
call_id: toolCall.id,
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
// Assistant 消息(纯文本)
|
|
151
|
-
if (msg.role === 'assistant') {
|
|
152
|
-
result.push({
|
|
153
|
-
type: 'message',
|
|
154
|
-
role: 'assistant',
|
|
155
|
-
content: [
|
|
156
|
-
{
|
|
157
|
-
type: 'output_text',
|
|
158
|
-
text: msg.content || '',
|
|
159
|
-
},
|
|
160
|
-
],
|
|
161
|
-
});
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
// Tool 消息:转换为 function_call_output
|
|
165
|
-
if (msg.role === 'tool' && msg.tool_call_id) {
|
|
166
|
-
// Handle multimodal tool results with images
|
|
167
|
-
if (msg.images && msg.images.length > 0) {
|
|
168
|
-
// For Responses API, we need to include images in a structured way
|
|
169
|
-
// The output can be an array of content items
|
|
170
|
-
const outputContent = [];
|
|
171
|
-
// Add text content
|
|
172
|
-
if (msg.content) {
|
|
173
|
-
outputContent.push({
|
|
174
|
-
type: 'input_text',
|
|
175
|
-
text: msg.content,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
// Add images as base64 data URLs (Responses API format)
|
|
179
|
-
for (const image of msg.images) {
|
|
180
|
-
outputContent.push({
|
|
181
|
-
type: 'input_image',
|
|
182
|
-
image_url: `data:${image.mimeType};base64,${image.data}`,
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
result.push({
|
|
186
|
-
type: 'function_call_output',
|
|
187
|
-
call_id: msg.tool_call_id,
|
|
188
|
-
output: outputContent,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
result.push({
|
|
193
|
-
type: 'function_call_output',
|
|
194
|
-
call_id: msg.tool_call_id,
|
|
195
|
-
output: msg.content,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
// 确定系统提示词:参考 anthropic.ts 的逻辑
|
|
202
|
-
let systemInstructions;
|
|
203
|
-
// 如果配置了自定义系统提示词(最高优先级,始终添加)
|
|
204
|
-
if (customSystemPrompt) {
|
|
205
|
-
// 有自定义系统提示词:自定义作为 instructions
|
|
206
|
-
systemInstructions = customSystemPrompt;
|
|
207
|
-
if (includeBuiltinSystemPrompt) {
|
|
208
|
-
// 默认系统提示词作为第一条用户消息
|
|
209
|
-
result.unshift({
|
|
210
|
-
type: 'message',
|
|
211
|
-
role: 'user',
|
|
212
|
-
content: [
|
|
213
|
-
{
|
|
214
|
-
type: 'input_text',
|
|
215
|
-
text: getSystemPrompt(),
|
|
216
|
-
},
|
|
217
|
-
],
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
else if (includeBuiltinSystemPrompt) {
|
|
222
|
-
// 没有自定义系统提示词,但需要添加默认系统提示词
|
|
223
|
-
systemInstructions = getSystemPrompt();
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
// 既没有自定义系统提示词,也不需要添加默认系统提示词
|
|
227
|
-
systemInstructions = 'You are a helpful assistant.';
|
|
228
|
-
}
|
|
229
|
-
return { input: result, systemInstructions };
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Parse Server-Sent Events (SSE) stream
|
|
233
|
-
*/
|
|
234
|
-
async function* parseSSEStream(reader) {
|
|
235
|
-
const decoder = new TextDecoder();
|
|
236
|
-
let buffer = '';
|
|
237
|
-
try {
|
|
238
|
-
while (true) {
|
|
239
|
-
const { done, value } = await reader.read();
|
|
240
|
-
if (done) {
|
|
241
|
-
// ✅ 关键修复:检查buffer是否有残留数据
|
|
242
|
-
if (buffer.trim()) {
|
|
243
|
-
// 连接异常中断,抛出明确错误
|
|
244
|
-
throw new Error(`Stream terminated unexpectedly with incomplete data: ${buffer.substring(0, 100)}...`);
|
|
245
|
-
}
|
|
246
|
-
break; // 正常结束
|
|
247
|
-
}
|
|
248
|
-
buffer += decoder.decode(value, { stream: true });
|
|
249
|
-
const lines = buffer.split('\n');
|
|
250
|
-
buffer = lines.pop() || '';
|
|
251
|
-
for (const line of lines) {
|
|
252
|
-
const trimmed = line.trim();
|
|
253
|
-
if (!trimmed || trimmed.startsWith(':'))
|
|
254
|
-
continue;
|
|
255
|
-
if (trimmed === 'data: [DONE]' || trimmed === 'data:[DONE]') {
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
// Handle both "event: " and "event:" formats
|
|
259
|
-
if (trimmed.startsWith('event:')) {
|
|
260
|
-
// Event type, will be followed by data
|
|
261
|
-
continue;
|
|
262
|
-
}
|
|
263
|
-
// Handle both "data: " and "data:" formats
|
|
264
|
-
if (trimmed.startsWith('data:')) {
|
|
265
|
-
const data = trimmed.startsWith('data: ')
|
|
266
|
-
? trimmed.slice(6)
|
|
267
|
-
: trimmed.slice(5);
|
|
268
|
-
const parseResult = parseJsonWithFix(data, {
|
|
269
|
-
toolName: 'Responses API SSE stream',
|
|
270
|
-
logWarning: false,
|
|
271
|
-
logError: true,
|
|
272
|
-
});
|
|
273
|
-
if (parseResult.success) {
|
|
274
|
-
yield parseResult.data;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
catch (error) {
|
|
281
|
-
const { logger } = await import('../utils/logger.js');
|
|
282
|
-
logger.error('Responses API SSE stream parsing error:', {
|
|
283
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
284
|
-
remainingBuffer: buffer.substring(0, 200),
|
|
285
|
-
});
|
|
286
|
-
throw error;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* 使用 Responses API 创建流式响应(带自动工具调用)
|
|
291
|
-
*/
|
|
292
|
-
export async function* createStreamingResponse(options, abortSignal, onRetry) {
|
|
293
|
-
const config = getOpenAIConfig();
|
|
294
|
-
// 提取系统提示词和转换后的消息
|
|
295
|
-
const { input: requestInput, systemInstructions } = convertToResponseInput(options.messages, options.includeBuiltinSystemPrompt !== false);
|
|
296
|
-
// 获取配置的 reasoning 设置
|
|
297
|
-
const configuredReasoning = getResponsesReasoningConfig();
|
|
298
|
-
// 使用重试包装生成器
|
|
299
|
-
yield* withRetryGenerator(async function* () {
|
|
300
|
-
const requestPayload = {
|
|
301
|
-
model: options.model,
|
|
302
|
-
instructions: systemInstructions,
|
|
303
|
-
input: requestInput,
|
|
304
|
-
tools: convertToolsForResponses(options.tools),
|
|
305
|
-
tool_choice: options.tool_choice,
|
|
306
|
-
parallel_tool_calls: false,
|
|
307
|
-
// 只有当 reasoning 启用时才添加 reasoning 字段
|
|
308
|
-
...(configuredReasoning && {
|
|
309
|
-
reasoning: configuredReasoning,
|
|
310
|
-
}),
|
|
311
|
-
store: false,
|
|
312
|
-
stream: true,
|
|
313
|
-
prompt_cache_key: options.prompt_cache_key,
|
|
314
|
-
};
|
|
315
|
-
const url = `${config.baseUrl}/responses`;
|
|
316
|
-
const fetchOptions = addProxyToFetchOptions(url, {
|
|
317
|
-
method: 'POST',
|
|
318
|
-
headers: {
|
|
319
|
-
'Content-Type': 'application/json',
|
|
320
|
-
Authorization: `Bearer ${config.apiKey}`,
|
|
321
|
-
'x-snow': 'true',
|
|
322
|
-
...config.customHeaders,
|
|
323
|
-
},
|
|
324
|
-
body: JSON.stringify(requestPayload),
|
|
325
|
-
signal: abortSignal,
|
|
326
|
-
});
|
|
327
|
-
const response = await fetch(url, fetchOptions);
|
|
328
|
-
if (!response.ok) {
|
|
329
|
-
const errorText = await response.text();
|
|
330
|
-
throw new Error(`OpenAI Responses API error: ${response.status} ${response.statusText} - ${errorText}`);
|
|
331
|
-
}
|
|
332
|
-
if (!response.body) {
|
|
333
|
-
throw new Error('No response body from OpenAI Responses API');
|
|
334
|
-
}
|
|
335
|
-
let contentBuffer = '';
|
|
336
|
-
let toolCallsBuffer = {};
|
|
337
|
-
let hasToolCalls = false;
|
|
338
|
-
let currentFunctionCallId = null;
|
|
339
|
-
let usageData;
|
|
340
|
-
let reasoningData;
|
|
341
|
-
for await (const chunk of parseSSEStream(response.body.getReader())) {
|
|
342
|
-
if (abortSignal?.aborted) {
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
// Responses API 使用 SSE 事件格式
|
|
346
|
-
const eventType = chunk.type;
|
|
347
|
-
// 根据事件类型处理
|
|
348
|
-
if (eventType === 'response.created' ||
|
|
349
|
-
eventType === 'response.in_progress') {
|
|
350
|
-
// 响应创建/进行中 - 忽略
|
|
351
|
-
continue;
|
|
352
|
-
}
|
|
353
|
-
else if (eventType === 'response.output_item.added') {
|
|
354
|
-
// 新输出项添加
|
|
355
|
-
const item = chunk.item;
|
|
356
|
-
if (item?.type === 'reasoning') {
|
|
357
|
-
// 推理摘要开始 - 发送 reasoning_started 事件
|
|
358
|
-
yield {
|
|
359
|
-
type: 'reasoning_started',
|
|
360
|
-
};
|
|
361
|
-
continue;
|
|
362
|
-
}
|
|
363
|
-
else if (item?.type === 'message') {
|
|
364
|
-
// 消息开始 - 忽略
|
|
365
|
-
continue;
|
|
366
|
-
}
|
|
367
|
-
else if (item?.type === 'function_call') {
|
|
368
|
-
// 工具调用开始
|
|
369
|
-
hasToolCalls = true;
|
|
370
|
-
const callId = item.call_id || item.id;
|
|
371
|
-
currentFunctionCallId = callId;
|
|
372
|
-
toolCallsBuffer[callId] = {
|
|
373
|
-
id: callId,
|
|
374
|
-
type: 'function',
|
|
375
|
-
function: {
|
|
376
|
-
name: item.name || '',
|
|
377
|
-
arguments: '',
|
|
378
|
-
},
|
|
379
|
-
};
|
|
380
|
-
continue;
|
|
381
|
-
}
|
|
382
|
-
continue;
|
|
383
|
-
}
|
|
384
|
-
else if (eventType === 'response.function_call_arguments.delta') {
|
|
385
|
-
// 工具调用参数增量
|
|
386
|
-
const delta = chunk.delta;
|
|
387
|
-
if (delta && currentFunctionCallId) {
|
|
388
|
-
toolCallsBuffer[currentFunctionCallId].function.arguments += delta;
|
|
389
|
-
// 发送 delta 用于 token 计数
|
|
390
|
-
yield {
|
|
391
|
-
type: 'tool_call_delta',
|
|
392
|
-
delta: delta,
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
else if (eventType === 'response.function_call_arguments.done') {
|
|
397
|
-
// 工具调用参数完成
|
|
398
|
-
const itemId = chunk.item_id;
|
|
399
|
-
const args = chunk.arguments;
|
|
400
|
-
if (itemId && toolCallsBuffer[itemId]) {
|
|
401
|
-
toolCallsBuffer[itemId].function.arguments = args;
|
|
402
|
-
}
|
|
403
|
-
currentFunctionCallId = null;
|
|
404
|
-
continue;
|
|
405
|
-
}
|
|
406
|
-
else if (eventType === 'response.output_item.done') {
|
|
407
|
-
// 输出项完成
|
|
408
|
-
const item = chunk.item;
|
|
409
|
-
if (item?.type === 'function_call') {
|
|
410
|
-
// 确保工具调用信息完整
|
|
411
|
-
const callId = item.call_id || item.id;
|
|
412
|
-
if (toolCallsBuffer[callId]) {
|
|
413
|
-
toolCallsBuffer[callId].function.name = item.name;
|
|
414
|
-
toolCallsBuffer[callId].function.arguments = item.arguments;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
else if (item?.type === 'reasoning') {
|
|
418
|
-
// 捕获完整的 reasoning 对象(包括 encrypted_content)
|
|
419
|
-
reasoningData = {
|
|
420
|
-
summary: item.summary,
|
|
421
|
-
content: item.content,
|
|
422
|
-
encrypted_content: item.encrypted_content,
|
|
423
|
-
};
|
|
424
|
-
}
|
|
425
|
-
continue;
|
|
426
|
-
}
|
|
427
|
-
else if (eventType === 'response.content_part.added') {
|
|
428
|
-
// 内容部分添加 - 忽略
|
|
429
|
-
continue;
|
|
430
|
-
}
|
|
431
|
-
else if (eventType === 'response.reasoning_summary_text.delta') {
|
|
432
|
-
// 推理摘要增量更新(仅用于 token 计数,不包含在响应内容中)
|
|
433
|
-
const delta = chunk.delta;
|
|
434
|
-
if (delta) {
|
|
435
|
-
yield {
|
|
436
|
-
type: 'reasoning_delta',
|
|
437
|
-
delta: delta,
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
else if (eventType === 'response.output_text.delta') {
|
|
442
|
-
// 文本增量更新
|
|
443
|
-
const delta = chunk.delta;
|
|
444
|
-
if (delta) {
|
|
445
|
-
contentBuffer += delta;
|
|
446
|
-
yield {
|
|
447
|
-
type: 'content',
|
|
448
|
-
content: delta,
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
else if (eventType === 'response.output_text.done') {
|
|
453
|
-
// 文本输出完成 - 忽略
|
|
454
|
-
continue;
|
|
455
|
-
}
|
|
456
|
-
else if (eventType === 'response.content_part.done') {
|
|
457
|
-
// 内容部分完成 - 忽略
|
|
458
|
-
continue;
|
|
459
|
-
}
|
|
460
|
-
else if (eventType === 'response.completed') {
|
|
461
|
-
// 响应完全完成 - 从 response 对象中提取 usage
|
|
462
|
-
if (chunk.response && chunk.response.usage) {
|
|
463
|
-
usageData = {
|
|
464
|
-
prompt_tokens: chunk.response.usage.input_tokens || 0,
|
|
465
|
-
completion_tokens: chunk.response.usage.output_tokens || 0,
|
|
466
|
-
total_tokens: chunk.response.usage.total_tokens || 0,
|
|
467
|
-
// OpenAI Responses API: cached_tokens in input_tokens_details (note: tokenS)
|
|
468
|
-
cached_tokens: chunk.response.usage.input_tokens_details
|
|
469
|
-
?.cached_tokens,
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
break;
|
|
473
|
-
}
|
|
474
|
-
else if (eventType === 'response.failed' ||
|
|
475
|
-
eventType === 'response.cancelled') {
|
|
476
|
-
// 响应失败或取消
|
|
477
|
-
const error = chunk.error;
|
|
478
|
-
if (error) {
|
|
479
|
-
throw new Error(`Response failed: ${error.message || 'Unknown error'}`);
|
|
480
|
-
}
|
|
481
|
-
break;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
// 如果有工具调用,返回它们
|
|
485
|
-
if (hasToolCalls) {
|
|
486
|
-
yield {
|
|
487
|
-
type: 'tool_calls',
|
|
488
|
-
tool_calls: Object.values(toolCallsBuffer),
|
|
489
|
-
};
|
|
490
|
-
}
|
|
491
|
-
// Yield reasoning data if available
|
|
492
|
-
if (reasoningData) {
|
|
493
|
-
yield {
|
|
494
|
-
type: 'reasoning_data',
|
|
495
|
-
reasoning: reasoningData,
|
|
496
|
-
};
|
|
497
|
-
}
|
|
498
|
-
// Yield usage information if available
|
|
499
|
-
if (usageData) {
|
|
500
|
-
// Save usage to file system at API layer
|
|
501
|
-
saveUsageToFile(options.model, usageData);
|
|
502
|
-
yield {
|
|
503
|
-
type: 'usage',
|
|
504
|
-
usage: usageData,
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
// 发送完成信号 - For Responses API, thinking content is in reasoning object, not separate thinking field
|
|
508
|
-
yield {
|
|
509
|
-
type: 'done',
|
|
510
|
-
};
|
|
511
|
-
}, {
|
|
512
|
-
abortSignal,
|
|
513
|
-
onRetry,
|
|
514
|
-
});
|
|
515
|
-
}
|