@blackbox_ai/blackbox-cli-core 0.0.9 → 0.8.1
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/README.md +11 -183
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/src/blackbox/blackboxOAuth2.js +17 -1
- package/dist/src/blackbox/blackboxOAuth2.js.map +1 -1
- package/dist/src/code_assist/oauth2.js +15 -3
- package/dist/src/code_assist/oauth2.js.map +1 -1
- package/dist/src/config/blackboxModels.d.ts +3 -2
- package/dist/src/config/blackboxModels.js +262 -33
- package/dist/src/config/blackboxModels.js.map +1 -1
- package/dist/src/config/config.d.ts +65 -0
- package/dist/src/config/config.js +282 -17
- package/dist/src/config/config.js.map +1 -1
- package/dist/src/config/models.d.ts +1 -1
- package/dist/src/config/models.js +1 -1
- package/dist/src/config/models.js.map +1 -1
- package/dist/src/config/multiAgentModels.d.ts +63 -0
- package/dist/src/config/multiAgentModels.js +194 -0
- package/dist/src/config/multiAgentModels.js.map +1 -0
- package/dist/src/core/client.js +8 -2
- package/dist/src/core/client.js.map +1 -1
- package/dist/src/core/contentGenerator.d.ts +1 -0
- package/dist/src/core/contentGenerator.js +57 -7
- package/dist/src/core/contentGenerator.js.map +1 -1
- package/dist/src/core/encryptedClientFactory.d.ts +17 -0
- package/dist/src/core/encryptedClientFactory.js +92 -0
- package/dist/src/core/encryptedClientFactory.js.map +1 -0
- package/dist/src/core/encryptedContentGenerator.d.ts +47 -0
- package/dist/src/core/encryptedContentGenerator.js +445 -0
- package/dist/src/core/encryptedContentGenerator.js.map +1 -0
- package/dist/src/core/encryptedGeminiClient.d.ts +59 -0
- package/dist/src/core/encryptedGeminiClient.js +177 -0
- package/dist/src/core/encryptedGeminiClient.js.map +1 -0
- package/dist/src/core/encryptedGeminiClientBridge.d.ts +107 -0
- package/dist/src/core/encryptedGeminiClientBridge.js +808 -0
- package/dist/src/core/encryptedGeminiClientBridge.js.map +1 -0
- package/dist/src/core/encryptedGeminiClientWrapper.d.ts +129 -0
- package/dist/src/core/encryptedGeminiClientWrapper.js +305 -0
- package/dist/src/core/encryptedGeminiClientWrapper.js.map +1 -0
- package/dist/src/core/encryptedTurn.d.ts +40 -0
- package/dist/src/core/encryptedTurn.js +114 -0
- package/dist/src/core/encryptedTurn.js.map +1 -0
- package/dist/src/core/logger.d.ts +21 -0
- package/dist/src/core/logger.js +110 -0
- package/dist/src/core/logger.js.map +1 -1
- package/dist/src/core/openaiContentGenerator/constants.d.ts +2 -0
- package/dist/src/core/openaiContentGenerator/constants.js +2 -0
- package/dist/src/core/openaiContentGenerator/constants.js.map +1 -1
- package/dist/src/core/openaiContentGenerator/converter.d.ts +16 -1
- package/dist/src/core/openaiContentGenerator/converter.js +135 -4
- package/dist/src/core/openaiContentGenerator/converter.js.map +1 -1
- package/dist/src/core/openaiContentGenerator/pipeline.js +6 -2
- package/dist/src/core/openaiContentGenerator/pipeline.js.map +1 -1
- package/dist/src/core/openaiContentGenerator/provider/default.js +10 -1
- package/dist/src/core/openaiContentGenerator/provider/default.js.map +1 -1
- package/dist/src/core/prompts.d.ts +17 -0
- package/dist/src/core/prompts.js +347 -19
- package/dist/src/core/prompts.js.map +1 -1
- package/dist/src/core/tokenLimits.d.ts +1 -0
- package/dist/src/core/tokenLimits.js +37 -2
- package/dist/src/core/tokenLimits.js.map +1 -1
- package/dist/src/core/tokenLimits.test.js +36 -1
- package/dist/src/core/tokenLimits.test.js.map +1 -1
- package/dist/src/encrypt/attestation.d.ts +5 -0
- package/dist/src/encrypt/attestation.js +100 -0
- package/dist/src/encrypt/attestation.js.map +1 -0
- package/dist/src/encrypt/client.d.ts +14 -0
- package/dist/src/encrypt/client.js +132 -0
- package/dist/src/encrypt/client.js.map +1 -0
- package/dist/src/encrypt/config.d.ts +22 -0
- package/dist/src/encrypt/config.js +43 -0
- package/dist/src/encrypt/config.js.map +1 -0
- package/dist/src/encrypt/crypto-utils.d.ts +57 -0
- package/dist/src/encrypt/crypto-utils.js +257 -0
- package/dist/src/encrypt/crypto-utils.js.map +1 -0
- package/dist/src/encrypt/history-manager.d.ts +43 -0
- package/dist/src/encrypt/history-manager.js +164 -0
- package/dist/src/encrypt/history-manager.js.map +1 -0
- package/dist/src/encrypt/minimax-template.d.ts +73 -0
- package/dist/src/encrypt/minimax-template.js +276 -0
- package/dist/src/encrypt/minimax-template.js.map +1 -0
- package/dist/src/encrypt/sessions.d.ts +17 -0
- package/dist/src/encrypt/sessions.js +221 -0
- package/dist/src/encrypt/sessions.js.map +1 -0
- package/dist/src/encrypt/streaming-client.d.ts +29 -0
- package/dist/src/encrypt/streaming-client.js +232 -0
- package/dist/src/encrypt/streaming-client.js.map +1 -0
- package/dist/src/encrypt/tool-formatter.d.ts +36 -0
- package/dist/src/encrypt/tool-formatter.js +353 -0
- package/dist/src/encrypt/tool-formatter.js.map +1 -0
- package/dist/src/encrypt/tool-parser.d.ts +93 -0
- package/dist/src/encrypt/tool-parser.js +567 -0
- package/dist/src/encrypt/tool-parser.js.map +1 -0
- package/dist/src/encrypt/types.d.ts +81 -0
- package/dist/src/encrypt/types.js +2 -0
- package/dist/src/encrypt/types.js.map +1 -0
- package/dist/src/generated/git-commit.d.ts +3 -3
- package/dist/src/generated/git-commit.js +3 -3
- package/dist/src/ide/ide-client.js +9 -19
- package/dist/src/ide/ide-client.js.map +1 -1
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.js +15 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/oauth-provider.js +2 -6
- package/dist/src/mcp/oauth-provider.js.map +1 -1
- package/dist/src/mcp/oauth-token-storage.d.ts +7 -0
- package/dist/src/mcp/oauth-token-storage.js +24 -0
- package/dist/src/mcp/oauth-token-storage.js.map +1 -1
- package/dist/src/services/EncryptedChatService.d.ts +80 -0
- package/dist/src/services/EncryptedChatService.js +202 -0
- package/dist/src/services/EncryptedChatService.js.map +1 -0
- package/dist/src/services/StatsHistoryService.d.ts +131 -0
- package/dist/src/services/StatsHistoryService.js +427 -0
- package/dist/src/services/StatsHistoryService.js.map +1 -0
- package/dist/src/services/checkpointApiService.d.ts +101 -0
- package/dist/src/services/checkpointApiService.js +215 -0
- package/dist/src/services/checkpointApiService.js.map +1 -0
- package/dist/src/services/environmentSanitization.d.ts +24 -0
- package/dist/src/services/environmentSanitization.js +152 -0
- package/dist/src/services/environmentSanitization.js.map +1 -0
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.d.ts +2 -6
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.js +29 -135
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.js.map +1 -1
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.test.js +1 -1
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.test.js.map +1 -1
- package/dist/src/telemetry/uiTelemetry.d.ts +8 -0
- package/dist/src/telemetry/uiTelemetry.js +17 -0
- package/dist/src/telemetry/uiTelemetry.js.map +1 -1
- package/dist/src/tools/browser-interactive.d.ts +63 -0
- package/dist/src/tools/browser-interactive.js +394 -0
- package/dist/src/tools/browser-interactive.js.map +1 -0
- package/dist/src/tools/browser_use.d.ts +22 -1
- package/dist/src/tools/browser_use.js +403 -23
- package/dist/src/tools/browser_use.js.map +1 -1
- package/dist/src/tools/data-file-constants.d.ts +17 -0
- package/dist/src/tools/data-file-constants.js +30 -0
- package/dist/src/tools/data-file-constants.js.map +1 -0
- package/dist/src/tools/edit.js +44 -7
- package/dist/src/tools/edit.js.map +1 -1
- package/dist/src/tools/ls.js +40 -6
- package/dist/src/tools/ls.js.map +1 -1
- package/dist/src/tools/ls.test.js +4 -4
- package/dist/src/tools/ls.test.js.map +1 -1
- package/dist/src/tools/mcp-client-manager.d.ts +28 -2
- package/dist/src/tools/mcp-client-manager.js +62 -4
- package/dist/src/tools/mcp-client-manager.js.map +1 -1
- package/dist/src/tools/mcp-client.d.ts +5 -3
- package/dist/src/tools/mcp-client.js +39 -11
- package/dist/src/tools/mcp-client.js.map +1 -1
- package/dist/src/tools/mcp-tool.d.ts +3 -1
- package/dist/src/tools/mcp-tool.js +37 -9
- package/dist/src/tools/mcp-tool.js.map +1 -1
- package/dist/src/tools/memoryTool.d.ts +14 -4
- package/dist/src/tools/memoryTool.js +98 -39
- package/dist/src/tools/memoryTool.js.map +1 -1
- package/dist/src/tools/read-data-file.d.ts +31 -0
- package/dist/src/tools/read-data-file.js +469 -0
- package/dist/src/tools/read-data-file.js.map +1 -0
- package/dist/src/tools/read-file.js +64 -5
- package/dist/src/tools/read-file.js.map +1 -1
- package/dist/src/tools/read-file.test.js +40 -6
- package/dist/src/tools/read-file.test.js.map +1 -1
- package/dist/src/tools/shell.d.ts +3 -1
- package/dist/src/tools/shell.js +25 -4
- package/dist/src/tools/shell.js.map +1 -1
- package/dist/src/tools/skill.d.ts +34 -0
- package/dist/src/tools/skill.js +143 -0
- package/dist/src/tools/skill.js.map +1 -0
- package/dist/src/tools/sql_db.d.ts +101 -0
- package/dist/src/tools/sql_db.js +1033 -0
- package/dist/src/tools/sql_db.js.map +1 -0
- package/dist/src/tools/sql_db_configure.d.ts +18 -0
- package/dist/src/tools/sql_db_configure.js +96 -0
- package/dist/src/tools/sql_db_configure.js.map +1 -0
- package/dist/src/tools/taskCompletion.d.ts +29 -0
- package/dist/src/tools/taskCompletion.js +231 -0
- package/dist/src/tools/taskCompletion.js.map +1 -0
- package/dist/src/tools/tool-error.d.ts +3 -1
- package/dist/src/tools/tool-error.js +3 -0
- package/dist/src/tools/tool-error.js.map +1 -1
- package/dist/src/tools/tool-names.d.ts +8 -0
- package/dist/src/tools/tool-names.js +8 -0
- package/dist/src/tools/tool-names.js.map +1 -1
- package/dist/src/tools/tool-registry.d.ts +22 -0
- package/dist/src/tools/tool-registry.js +41 -1
- package/dist/src/tools/tool-registry.js.map +1 -1
- package/dist/src/tools/tools.d.ts +18 -2
- package/dist/src/tools/tools.js +3 -0
- package/dist/src/tools/tools.js.map +1 -1
- package/dist/src/tools/web-fetch.js +24 -4
- package/dist/src/tools/web-fetch.js.map +1 -1
- package/dist/src/tools/web-search.js +160 -2
- package/dist/src/tools/web-search.js.map +1 -1
- package/dist/src/tools/workspace-error-helper.d.ts +9 -0
- package/dist/src/tools/workspace-error-helper.js +43 -0
- package/dist/src/tools/workspace-error-helper.js.map +1 -0
- package/dist/src/tools/workspace-error-helper.test.js +85 -0
- package/dist/src/tools/workspace-error-helper.test.js.map +1 -0
- package/dist/src/tools/write-file.js +42 -7
- package/dist/src/tools/write-file.js.map +1 -1
- package/dist/src/utils/environmentContext.js +3 -1
- package/dist/src/utils/environmentContext.js.map +1 -1
- package/dist/src/utils/environmentContext.test.js +3 -2
- package/dist/src/utils/environmentContext.test.js.map +1 -1
- package/dist/src/utils/fetch.d.ts +3 -1
- package/dist/src/utils/fetch.js +35 -2
- package/dist/src/utils/fetch.js.map +1 -1
- package/dist/src/utils/fileUtils.js +30 -3
- package/dist/src/utils/fileUtils.js.map +1 -1
- package/dist/src/utils/filesearch/fileSearch.d.ts +2 -0
- package/dist/src/utils/filesearch/fileSearch.js +38 -7
- package/dist/src/utils/filesearch/fileSearch.js.map +1 -1
- package/dist/src/utils/git-worktree-utils.d.ts +56 -0
- package/dist/src/utils/git-worktree-utils.js +176 -0
- package/dist/src/utils/git-worktree-utils.js.map +1 -0
- package/dist/src/utils/imageCompression.d.ts +34 -0
- package/dist/src/utils/imageCompression.js +170 -0
- package/dist/src/utils/imageCompression.js.map +1 -0
- package/dist/src/utils/messageTruncator.d.ts +51 -0
- package/dist/src/utils/messageTruncator.js +346 -0
- package/dist/src/utils/messageTruncator.js.map +1 -0
- package/dist/src/utils/pathReader.js +26 -6
- package/dist/src/utils/pathReader.js.map +1 -1
- package/dist/src/utils/skill.d.ts +65 -0
- package/dist/src/utils/skill.js +241 -0
- package/dist/src/utils/skill.js.map +1 -0
- package/dist/src/utils/textCleaning.d.ts +51 -0
- package/dist/src/utils/textCleaning.js +327 -0
- package/dist/src/utils/textCleaning.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -2
- package/dist/src/tools/mcp-client-manager.test.js +0 -39
- package/dist/src/tools/mcp-client-manager.test.js.map +0 -1
- package/dist/src/tools/mcp-client.test.d.ts +0 -6
- package/dist/src/tools/mcp-client.test.js +0 -454
- package/dist/src/tools/mcp-client.test.js.map +0 -1
- package/dist/src/tools/mcp-tool.test.d.ts +0 -6
- package/dist/src/tools/mcp-tool.test.js +0 -576
- package/dist/src/tools/mcp-tool.test.js.map +0 -1
- package/dist/src/tools/memoryTool.test.d.ts +0 -6
- package/dist/src/tools/memoryTool.test.js +0 -420
- package/dist/src/tools/memoryTool.test.js.map +0 -1
- package/dist/src/tools/tool-registry.test.d.ts +0 -6
- package/dist/src/tools/tool-registry.test.js +0 -332
- package/dist/src/tools/tool-registry.test.js.map +0 -1
- /package/dist/src/tools/{mcp-client-manager.test.d.ts → workspace-error-helper.test.d.ts} +0 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { serializePublicKeyPem, encryptAndSign, decryptAndVerify } from './crypto-utils.js';
|
|
2
|
+
import {} from './types.js';
|
|
3
|
+
import { ENCRYPTION_CONFIG } from './config.js';
|
|
4
|
+
import { truncateHistory, getHistorySummary, HISTORY_CONFIG } from './history-manager.js';
|
|
5
|
+
/**
|
|
6
|
+
* StreamingClient handles the complete encrypted streaming protocol
|
|
7
|
+
*/
|
|
8
|
+
export class StreamingClient {
|
|
9
|
+
baseUrl;
|
|
10
|
+
initialized = false;
|
|
11
|
+
nonce = 1000;
|
|
12
|
+
history = [];
|
|
13
|
+
attestationData;
|
|
14
|
+
constructor(baseUrl, _attestationUrl, systemPrompt) {
|
|
15
|
+
this.baseUrl = baseUrl;
|
|
16
|
+
if (systemPrompt) {
|
|
17
|
+
this.history.push({ role: 'system', content: systemPrompt });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Initialize from existing session data (avoids re-attestation)
|
|
22
|
+
* Now uses the session management layer
|
|
23
|
+
*/
|
|
24
|
+
initializeFromSession(sessionData) {
|
|
25
|
+
// sessionId is stored in attestationData, no need to store separately
|
|
26
|
+
this.nonce = sessionData.nonce;
|
|
27
|
+
this.history = sessionData.history || [];
|
|
28
|
+
this.attestationData = sessionData.attestationData;
|
|
29
|
+
this.initialized = true;
|
|
30
|
+
}
|
|
31
|
+
async sendMessageStream(userText, fileData, retryAttempt = 0) {
|
|
32
|
+
if (!this.initialized || !this.attestationData) {
|
|
33
|
+
throw new Error('StreamingClient not initialized with attestation data. Call initializeFromSession() first.');
|
|
34
|
+
}
|
|
35
|
+
// Validate user text is not empty
|
|
36
|
+
if (!userText || userText.trim().length === 0) {
|
|
37
|
+
throw new Error('User message cannot be empty');
|
|
38
|
+
}
|
|
39
|
+
// Process file data and build context
|
|
40
|
+
let processedMessage = userText;
|
|
41
|
+
const contextParts = [];
|
|
42
|
+
// Process file data if provided
|
|
43
|
+
if (fileData) {
|
|
44
|
+
// Handle PDF/text content
|
|
45
|
+
if (fileData.fileText) {
|
|
46
|
+
contextParts.push(fileData.fileText);
|
|
47
|
+
}
|
|
48
|
+
// Build message with context
|
|
49
|
+
if (contextParts.length > 0) {
|
|
50
|
+
const contextText = contextParts.join('\n\n');
|
|
51
|
+
processedMessage = `Context:\n\n${contextText}\n\nQuery: ${userText}`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Create message object
|
|
55
|
+
const messageObj = {
|
|
56
|
+
role: 'user',
|
|
57
|
+
content: processedMessage
|
|
58
|
+
};
|
|
59
|
+
// Process image if provided
|
|
60
|
+
if (fileData?.imagesData && fileData.imagesData.length > 0) {
|
|
61
|
+
// Use the first image for now (following the sample pattern)
|
|
62
|
+
const firstImage = fileData.imagesData[0];
|
|
63
|
+
if (firstImage?.contents) {
|
|
64
|
+
// Extract base64 data (remove data:image/...;base64, prefix if present)
|
|
65
|
+
const base64Data = firstImage.contents.includes(',')
|
|
66
|
+
? firstImage.contents.split(',')[1]
|
|
67
|
+
: firstImage.contents;
|
|
68
|
+
messageObj.image_url = base64Data;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Ensure we have a valid history array - add the current message to history for encryption
|
|
72
|
+
const currentHistory = [...this.history, messageObj];
|
|
73
|
+
// Validate history is not empty
|
|
74
|
+
if (currentHistory.length === 0) {
|
|
75
|
+
throw new Error('Message history cannot be empty');
|
|
76
|
+
}
|
|
77
|
+
// Truncate history to prevent payload size issues (413 errors)
|
|
78
|
+
let truncatedHistory = truncateHistory(currentHistory);
|
|
79
|
+
// If this is a retry, apply even more aggressive truncation
|
|
80
|
+
if (retryAttempt > 0) {
|
|
81
|
+
const { truncateToSize } = await import('./history-manager.js');
|
|
82
|
+
// Reduce limit by half for each retry: 100KB → 50KB → 25KB
|
|
83
|
+
const retryLimit = HISTORY_CONFIG.maxHistoryBytes / (2 ** retryAttempt);
|
|
84
|
+
console.log(`🔄 Retry attempt ${retryAttempt}: Using ${(retryLimit / 1024).toFixed(1)}KB limit`);
|
|
85
|
+
truncatedHistory = truncateToSize(truncatedHistory, retryLimit);
|
|
86
|
+
}
|
|
87
|
+
// Log truncation if it occurred
|
|
88
|
+
if (truncatedHistory.length < currentHistory.length) {
|
|
89
|
+
const summary = getHistorySummary(currentHistory, truncatedHistory);
|
|
90
|
+
console.log(`📦 ${summary}`);
|
|
91
|
+
}
|
|
92
|
+
const historyJson = JSON.stringify(truncatedHistory);
|
|
93
|
+
const payloadSize = new TextEncoder().encode(historyJson).length;
|
|
94
|
+
console.log(`📊 Payload size before encryption: ${(payloadSize / 1024).toFixed(1)}KB (${truncatedHistory.length} messages)`);
|
|
95
|
+
// Warn if payload is large
|
|
96
|
+
if (payloadSize > 600_000) {
|
|
97
|
+
console.warn(`⚠️ Large payload: ${(payloadSize / 1024).toFixed(1)}KB - may cause 413 error`);
|
|
98
|
+
}
|
|
99
|
+
const localKeys = this.attestationData.localKeys;
|
|
100
|
+
// Encrypt with all shared keys for attestation
|
|
101
|
+
const encryptedPayloads = {};
|
|
102
|
+
for (const sharedKeyData of this.attestationData.sharedKeys) {
|
|
103
|
+
// Use the imported encryptAndSign function for each shared key
|
|
104
|
+
const payload = await encryptAndSign(historyJson, sharedKeyData.sharedKey, localKeys.ecdsaPrivate, this.nonce);
|
|
105
|
+
const peerPubPem = await serializePublicKeyPem(localKeys.ecdsaPublic);
|
|
106
|
+
encryptedPayloads[sharedKeyData.id] = {
|
|
107
|
+
peer_public_key: peerPubPem,
|
|
108
|
+
payload
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
// Build request with all encrypted payloads
|
|
112
|
+
const request = {
|
|
113
|
+
payloads: encryptedPayloads
|
|
114
|
+
};
|
|
115
|
+
// Send to /message_stream endpoint
|
|
116
|
+
const url = `${this.baseUrl.replace(/\/$/, '')}/message_stream`;
|
|
117
|
+
const response = await fetch(url, {
|
|
118
|
+
method: 'POST',
|
|
119
|
+
headers: { 'Content-Type': 'application/json' },
|
|
120
|
+
body: JSON.stringify(request),
|
|
121
|
+
signal: AbortSignal.timeout(ENCRYPTION_CONFIG.streamingTimeout)
|
|
122
|
+
});
|
|
123
|
+
if (!response.ok) {
|
|
124
|
+
// Provide helpful error messages for common failure cases
|
|
125
|
+
if (response.status === 401 || response.status === 403) {
|
|
126
|
+
throw new Error(`Authentication failed (${response.status}). ` +
|
|
127
|
+
`Please ensure BLACKBOX_API_KEY is set correctly. ` +
|
|
128
|
+
`Run 'blackbox configure' to set up your API key.`);
|
|
129
|
+
}
|
|
130
|
+
// Handle 413 Payload Too Large with retry logic
|
|
131
|
+
if (response.status === 413 && retryAttempt < 3) {
|
|
132
|
+
console.warn(`⚠️ 413 Payload Too Large error (attempt ${retryAttempt + 1}/3)`);
|
|
133
|
+
console.log(`🔄 Retrying with more aggressive history truncation...`);
|
|
134
|
+
// Retry with more aggressive truncation
|
|
135
|
+
return this.sendMessageStream(userText, fileData, retryAttempt + 1);
|
|
136
|
+
}
|
|
137
|
+
// If all retries failed or other error
|
|
138
|
+
if (response.status === 413) {
|
|
139
|
+
throw new Error(`Payload too large even after ${retryAttempt + 1} retry attempts. ` +
|
|
140
|
+
`The conversation history is too large for the encrypted service. ` +
|
|
141
|
+
`Please start a new conversation.`);
|
|
142
|
+
}
|
|
143
|
+
throw new Error(`Streaming request failed: ${response.status}`);
|
|
144
|
+
}
|
|
145
|
+
// Process streaming response using ReadableStream (NDJSON format)
|
|
146
|
+
let buffer = '';
|
|
147
|
+
let fullResponse = '';
|
|
148
|
+
const attestationData = this.attestationData;
|
|
149
|
+
// FIX: Use arrow functions to preserve 'this' context
|
|
150
|
+
const transformStream = new TransformStream({
|
|
151
|
+
transform: async (chunk, controller) => {
|
|
152
|
+
try {
|
|
153
|
+
buffer += new TextDecoder().decode(chunk);
|
|
154
|
+
// Process complete lines
|
|
155
|
+
const lines = buffer.split('\n');
|
|
156
|
+
buffer = lines.pop() || ''; // Keep incomplete line in buffer
|
|
157
|
+
for (const line of lines) {
|
|
158
|
+
const trimmed = line.trim();
|
|
159
|
+
if (!trimmed)
|
|
160
|
+
continue;
|
|
161
|
+
try {
|
|
162
|
+
const jsonData = JSON.parse(trimmed);
|
|
163
|
+
// Check for end-of-stream marker
|
|
164
|
+
if (jsonData.eos === true) {
|
|
165
|
+
// Add the complete response to history when stream ends
|
|
166
|
+
if (fullResponse.trim().length > 0) {
|
|
167
|
+
// 'this' correctly refers to the class instance here
|
|
168
|
+
this.history.push({ role: 'assistant', content: fullResponse });
|
|
169
|
+
this.nonce = this.nonce + 1;
|
|
170
|
+
}
|
|
171
|
+
controller.terminate();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
// The server should indicate which key was used for encryption
|
|
175
|
+
// Try to decrypt with the appropriate key based on server response
|
|
176
|
+
let decryptedChunk = null;
|
|
177
|
+
// If server indicates which key was used
|
|
178
|
+
if (jsonData.key_id) {
|
|
179
|
+
const keyData = attestationData.sharedKeys.find((k) => k.id === jsonData.key_id);
|
|
180
|
+
const serverKey = attestationData.serverPublicKeys.find((k) => k.id === jsonData.key_id);
|
|
181
|
+
if (keyData && serverKey) {
|
|
182
|
+
try {
|
|
183
|
+
decryptedChunk = await decryptAndVerify(jsonData, keyData.sharedKey, serverKey.publicKeySpki);
|
|
184
|
+
}
|
|
185
|
+
catch (_error) {
|
|
186
|
+
// Failed to decrypt with specified key, will try all keys
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// If no key_id specified or decryption failed, try all keys
|
|
191
|
+
if (!decryptedChunk) {
|
|
192
|
+
for (let i = 0; i < attestationData.sharedKeys.length; i++) {
|
|
193
|
+
const keyData = attestationData.sharedKeys[i];
|
|
194
|
+
const serverKey = attestationData.serverPublicKeys[i];
|
|
195
|
+
try {
|
|
196
|
+
decryptedChunk = await decryptAndVerify(jsonData, keyData.sharedKey, serverKey.publicKeySpki);
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
catch (_error) {
|
|
200
|
+
// Try next key
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (!decryptedChunk) {
|
|
206
|
+
throw new Error('Failed to decrypt chunk with any available key');
|
|
207
|
+
}
|
|
208
|
+
fullResponse += decryptedChunk;
|
|
209
|
+
// Enqueue the decrypted chunk
|
|
210
|
+
controller.enqueue(new TextEncoder().encode(decryptedChunk));
|
|
211
|
+
}
|
|
212
|
+
catch (_e) {
|
|
213
|
+
// Error processing chunk, skip this line
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
controller.error(error);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
flush: () => {
|
|
222
|
+
// Handle case where stream ends without explicit EOS marker
|
|
223
|
+
if (fullResponse.trim().length > 0) {
|
|
224
|
+
this.history.push({ role: 'assistant', content: fullResponse });
|
|
225
|
+
this.nonce = this.nonce + 1;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
return response.body.pipeThrough(transformStream);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
//# sourceMappingURL=streaming-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming-client.js","sourceRoot":"","sources":["../../../src/encrypt/streaming-client.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,cAAc,EACd,gBAAgB,EAEjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAgD,MAAM,YAAY,CAAC;AAC1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAkB1F;;GAEG;AACH,MAAM,OAAO,eAAe;IAClB,OAAO,CAAS;IAChB,WAAW,GAAY,KAAK,CAAC;IAC7B,KAAK,GAAW,IAAI,CAAC;IACrB,OAAO,GAAkB,EAAE,CAAC;IAC5B,eAAe,CAA4B;IAEnD,YAAY,OAAe,EAAE,eAAuB,EAAE,YAAqB;QACzE,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,IAAI,YAAY,EAAE,CAAC;YACjB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC;IAED;;;OAGG;IACH,qBAAqB,CAAC,WAKrB;QACC,sEAAsE;QACtE,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC;QAC/B,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,IAAI,EAAE,CAAC;QACzC,IAAI,CAAC,eAAe,GAAG,WAAW,CAAC,eAAe,CAAC;QACnD,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC1B,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,QAAgB,EAAE,QAAmB,EAAE,eAAuB,CAAC;QAC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,WAAW,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC;wBAE/D,CAAC;wBAAC,OAAO,EAAW,EAAE,CAAC;4BACrB,yCAAyC;wBAC3C,CAAC;oBACH,CAAC;gBACH,CAAC;gBAAC,OAAO,KAAc,EAAE,CAAC;oBACxB,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBAC1B,CAAC;YACH,CAAC;YAED,KAAK,EAAE,GAAG,EAAE;gBACV,4DAA4D;gBAC5D,IAAI,YAAY,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBACnC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC,CAAC;oBAChE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;gBAC9B,CAAC;YACH,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC,IAAK,CAAC,WAAW,CAAC,eAAe,CAAC,CAAC;IACrD,CAAC;CACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import type { FunctionDeclaration } from '@google/genai';
|
|
7
|
+
export interface ToolCall {
|
|
8
|
+
name: string;
|
|
9
|
+
arguments: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
export interface ChatMessage {
|
|
12
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
13
|
+
content: string;
|
|
14
|
+
tool_calls?: ToolCall[];
|
|
15
|
+
image_url?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Format tools for system prompt based on model type
|
|
19
|
+
* Matches the Rust implementation's format_tools_for_system_prompt function
|
|
20
|
+
*/
|
|
21
|
+
export declare function formatToolsForSystemPrompt(tools: FunctionDeclaration[], modelType?: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Format messages with tool calls based on model type
|
|
24
|
+
* Matches the Rust implementation's format_messages function
|
|
25
|
+
*/
|
|
26
|
+
export declare function formatMessages(messages: ChatMessage[], modelType?: string, history?: ChatMessage[]): ChatMessage[];
|
|
27
|
+
/**
|
|
28
|
+
* Parse tool calls from text response
|
|
29
|
+
* Supports MiniMax, Qwen and DeepSeek formats
|
|
30
|
+
* Enhanced version with better error handling
|
|
31
|
+
*/
|
|
32
|
+
export declare function parseToolCallsFromText(text: string, modelType?: string): ToolCall[];
|
|
33
|
+
/**
|
|
34
|
+
* Detect model type from response text
|
|
35
|
+
*/
|
|
36
|
+
export declare function detectModelType(text: string): string;
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Format tools for system prompt based on model type
|
|
8
|
+
* Matches the Rust implementation's format_tools_for_system_prompt function
|
|
9
|
+
*/
|
|
10
|
+
export function formatToolsForSystemPrompt(tools, modelType = 'minimax') {
|
|
11
|
+
if (!tools || tools.length === 0) {
|
|
12
|
+
return '';
|
|
13
|
+
}
|
|
14
|
+
let result = '';
|
|
15
|
+
switch (modelType.toLowerCase()) {
|
|
16
|
+
case 'minimax': {
|
|
17
|
+
result += '\n\n# Tools\n';
|
|
18
|
+
result += 'You may call one or more tools to assist with the user query.\n';
|
|
19
|
+
result += 'Here are the tools available in JSONSchema format:\n\n';
|
|
20
|
+
result += '<tools>\n';
|
|
21
|
+
for (const tool of tools) {
|
|
22
|
+
const toolFunction = {
|
|
23
|
+
name: tool.name,
|
|
24
|
+
description: tool.description || '',
|
|
25
|
+
parameters: tool.parametersJsonSchema || {}
|
|
26
|
+
};
|
|
27
|
+
result += `<tool>${JSON.stringify(toolFunction, null, 0)}</tool>\n`;
|
|
28
|
+
}
|
|
29
|
+
result += '</tools>\n\n';
|
|
30
|
+
result += 'When making tool calls, use XML format to invoke tools and pass parameters:\n\n';
|
|
31
|
+
result += '<minimax:tool_call>\n';
|
|
32
|
+
result += '<invoke name="tool-name-1">\n';
|
|
33
|
+
result += '<parameter name="param-key-1">param-value-1</parameter>\n';
|
|
34
|
+
result += '<parameter name="param-key-2">param-value-2</parameter>\n';
|
|
35
|
+
result += '...\n';
|
|
36
|
+
result += '</invoke>\n';
|
|
37
|
+
result += '</minimax:tool_call>\n\n';
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case 'deepseek': {
|
|
41
|
+
result += '\n\n## Tools\n\nYou have access to the following tools. Use them when needed to help the user:\n\n';
|
|
42
|
+
for (const tool of tools) {
|
|
43
|
+
const name = tool.name || '';
|
|
44
|
+
const description = tool.description || '';
|
|
45
|
+
const parameters = tool.parametersJsonSchema || {};
|
|
46
|
+
result += `**${name}**: ${description}\n`;
|
|
47
|
+
if (parameters && typeof parameters === 'object' && Object.keys(parameters).length > 0) {
|
|
48
|
+
result += `Parameters: ${JSON.stringify(parameters, null, 2)}\n`;
|
|
49
|
+
}
|
|
50
|
+
result += '\n';
|
|
51
|
+
}
|
|
52
|
+
result += '**CRITICAL TOOL USAGE RULES:**\n';
|
|
53
|
+
result += '1. NEVER output tool descriptions, parameters, or schemas in your response\n';
|
|
54
|
+
result += '2. NEVER show tool invocation payloads or debug information\n';
|
|
55
|
+
result += '3. Use tools silently - the user should only see the results\n';
|
|
56
|
+
result += '4. Use this EXACT format for tool calls:\n';
|
|
57
|
+
result += ' <|tool▁calls▁begin|><|tool▁call▁begin|>tool_name<|tool▁sep|>{"param":"value"}<|tool▁call▁end|><|tool▁calls▁end|>\n\n';
|
|
58
|
+
result += '5. Replace tool_name with the exact tool name and provide valid JSON arguments\n';
|
|
59
|
+
result += '6. Do NOT explain what tools you are using - just use them and show results\n\n';
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 'qwen': { // Qwen format
|
|
63
|
+
result += '\n\n## Tools\n\nYou have access to the following tools. Use them when needed to help the user:\n\n';
|
|
64
|
+
for (const tool of tools) {
|
|
65
|
+
const name = tool.name || '';
|
|
66
|
+
const description = tool.description || '';
|
|
67
|
+
const parameters = tool.parametersJsonSchema || {};
|
|
68
|
+
result += `**${name}**: ${description}\n`;
|
|
69
|
+
if (parameters && typeof parameters === 'object' && Object.keys(parameters).length > 0) {
|
|
70
|
+
result += `Parameters: ${JSON.stringify(parameters, null, 2)}\n`;
|
|
71
|
+
}
|
|
72
|
+
result += '\n';
|
|
73
|
+
}
|
|
74
|
+
result += '**CRITICAL TOOL USAGE RULES:**\n';
|
|
75
|
+
result += '1. NEVER output tool descriptions, parameters, or schemas in your response\n';
|
|
76
|
+
result += '2. NEVER show tool invocation payloads or debug information\n';
|
|
77
|
+
result += '3. Use tools silently - the user should only see the results\n';
|
|
78
|
+
result += '4. Use this EXACT format for tool calls:\n';
|
|
79
|
+
result += ' <tool_call>\n {"name": "tool_name", "arguments": {"param": "value"}}\n </tool_call>\n\n';
|
|
80
|
+
result += '5. Replace tool_name with the exact tool name and provide valid JSON arguments\n';
|
|
81
|
+
result += '6. Do NOT explain what tools you are using - just use them and show results\n\n';
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
default: {
|
|
85
|
+
result += '\n\n## Tools\n\nYou have access to the following tools:\n\n';
|
|
86
|
+
for (const tool of tools) {
|
|
87
|
+
const name = tool.name || '';
|
|
88
|
+
const description = tool.description || '';
|
|
89
|
+
const parameters = tool.parametersJsonSchema || {};
|
|
90
|
+
result += `### ${name}\n`;
|
|
91
|
+
result += `Description: ${description}\n\n`;
|
|
92
|
+
result += `Parameters: ${JSON.stringify(parameters)}\n\n`;
|
|
93
|
+
}
|
|
94
|
+
result += 'IMPORTANT: ALWAYS adhere to this exact format for tool use:\n';
|
|
95
|
+
result += '<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{additional_tool_calls}<|tool▁calls▁end|>\n\n';
|
|
96
|
+
result += 'Where:\n';
|
|
97
|
+
result += '- `tool_call_name` must be an exact match to one of the available tools\n';
|
|
98
|
+
result += '- `tool_call_arguments` must be valid JSON that strictly follows the tool\'s Parameters Schema\n';
|
|
99
|
+
result += '- For multiple tool calls, chain them directly without separators or spaces\n';
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Format messages with tool calls based on model type
|
|
107
|
+
* Matches the Rust implementation's format_messages function
|
|
108
|
+
*/
|
|
109
|
+
export function formatMessages(messages, modelType = 'deepseek', history = []) {
|
|
110
|
+
// Helper function to create a message key for deduplication
|
|
111
|
+
const messageKey = (msg) => `${msg.role}:${msg.content}`;
|
|
112
|
+
const formattedMessages = [];
|
|
113
|
+
const seenMessages = new Set();
|
|
114
|
+
// Extract messages from input (skip system messages)
|
|
115
|
+
const requestMessages = messages.filter(msg => msg.role !== 'system');
|
|
116
|
+
// Process messages and format tool calls
|
|
117
|
+
for (const msg of requestMessages) {
|
|
118
|
+
const formattedMsg = {
|
|
119
|
+
role: msg.role,
|
|
120
|
+
content: msg.content,
|
|
121
|
+
image_url: msg.image_url
|
|
122
|
+
};
|
|
123
|
+
// Format tool calls if present
|
|
124
|
+
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
125
|
+
let toolCallsStr = '';
|
|
126
|
+
switch (modelType.toLowerCase()) {
|
|
127
|
+
case 'minimax': {
|
|
128
|
+
toolCallsStr = '<minimax:tool_call>\n';
|
|
129
|
+
for (const tc of msg.tool_calls) {
|
|
130
|
+
toolCallsStr += `<invoke name="${tc.name}">\n`;
|
|
131
|
+
for (const [key, value] of Object.entries(tc.arguments)) {
|
|
132
|
+
const valueStr = typeof value === 'string' ? value : JSON.stringify(value);
|
|
133
|
+
toolCallsStr += `<parameter name="${key}">${valueStr}</parameter>\n`;
|
|
134
|
+
}
|
|
135
|
+
toolCallsStr += '</invoke>\n';
|
|
136
|
+
}
|
|
137
|
+
toolCallsStr += '</minimax:tool_call>';
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case 'deepseek': {
|
|
141
|
+
const formattedCalls = msg.tool_calls.map(tc => {
|
|
142
|
+
const argsStr = JSON.stringify(tc.arguments);
|
|
143
|
+
return `<|tool▁call▁begin|>${tc.name}<|tool▁sep|>${argsStr}<|tool▁call▁end|>`;
|
|
144
|
+
});
|
|
145
|
+
if (formattedCalls.length > 0) {
|
|
146
|
+
toolCallsStr = `<|tool▁calls▁begin|>${formattedCalls.join('')}<|tool▁calls▁end|>`;
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case 'qwen': { // Qwen format
|
|
151
|
+
toolCallsStr = msg.tool_calls.map(tc => {
|
|
152
|
+
const argsStr = JSON.stringify(tc.arguments);
|
|
153
|
+
return `<tool_call>\n{"name": "${tc.name}", "arguments": ${argsStr}}\n</tool_call>`;
|
|
154
|
+
}).join('\n');
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
default: {
|
|
158
|
+
const formattedCalls = msg.tool_calls.map(tc => {
|
|
159
|
+
const argsStr = JSON.stringify(tc.arguments);
|
|
160
|
+
return `<|tool▁call▁begin|>${tc.name}<|tool▁sep|>${argsStr}<|tool▁call▁end|>`;
|
|
161
|
+
});
|
|
162
|
+
if (formattedCalls.length > 0) {
|
|
163
|
+
toolCallsStr = `<|tool▁calls▁begin|>${formattedCalls.join('')}<|tool▁calls▁end|>`;
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Combine content and tool calls
|
|
169
|
+
if (formattedMsg.content && toolCallsStr) {
|
|
170
|
+
formattedMsg.content = `${formattedMsg.content}\n${toolCallsStr}`;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
formattedMsg.content = formattedMsg.content + toolCallsStr;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// Add to formatted messages if not seen before
|
|
177
|
+
const key = messageKey(formattedMsg);
|
|
178
|
+
if (!seenMessages.has(key)) {
|
|
179
|
+
seenMessages.add(key);
|
|
180
|
+
formattedMessages.push(formattedMsg);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// Merge with history (simplified version - can be enhanced based on needs)
|
|
184
|
+
const result = [];
|
|
185
|
+
// Add system messages from history first
|
|
186
|
+
for (const msg of history) {
|
|
187
|
+
if (msg.role === 'system') {
|
|
188
|
+
const key = messageKey(msg);
|
|
189
|
+
if (!seenMessages.has(key)) {
|
|
190
|
+
seenMessages.add(key);
|
|
191
|
+
result.push(msg);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// Add formatted messages
|
|
196
|
+
result.push(...formattedMessages);
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Parse tool calls from text response
|
|
201
|
+
* Supports MiniMax, Qwen and DeepSeek formats
|
|
202
|
+
* Enhanced version with better error handling
|
|
203
|
+
*/
|
|
204
|
+
export function parseToolCallsFromText(text, modelType = 'minimax') {
|
|
205
|
+
const toolCalls = [];
|
|
206
|
+
switch (modelType.toLowerCase()) {
|
|
207
|
+
case 'minimax': {
|
|
208
|
+
// Parse MiniMax format with XML-style tool calls
|
|
209
|
+
const minimaxRegex = /<minimax:tool_call>([\s\S]*?)<\/minimax:tool_call>/g;
|
|
210
|
+
let minimaxMatch;
|
|
211
|
+
while ((minimaxMatch = minimaxRegex.exec(text)) !== null) {
|
|
212
|
+
const toolCallContent = minimaxMatch[1];
|
|
213
|
+
// Parse individual invoke blocks
|
|
214
|
+
const invokeRegex = /<invoke name="([^"]+)">([\s\S]*?)<\/invoke>/g;
|
|
215
|
+
let invokeMatch;
|
|
216
|
+
while ((invokeMatch = invokeRegex.exec(toolCallContent)) !== null) {
|
|
217
|
+
const toolName = invokeMatch[1];
|
|
218
|
+
const parametersContent = invokeMatch[2];
|
|
219
|
+
// Parse parameters
|
|
220
|
+
const parameterRegex = /<parameter name="([^"]+)">([\s\S]*?)<\/parameter>/g;
|
|
221
|
+
const toolArguments = {};
|
|
222
|
+
let paramMatch;
|
|
223
|
+
while ((paramMatch = parameterRegex.exec(parametersContent)) !== null) {
|
|
224
|
+
const paramName = paramMatch[1];
|
|
225
|
+
const paramValue = paramMatch[2];
|
|
226
|
+
// Try to parse as JSON, fallback to string
|
|
227
|
+
try {
|
|
228
|
+
toolArguments[paramName] = JSON.parse(paramValue);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
toolArguments[paramName] = paramValue;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
toolCalls.push({
|
|
235
|
+
name: toolName,
|
|
236
|
+
arguments: toolArguments
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case 'deepseek': {
|
|
243
|
+
// Parse DeepSeek format with more robust regex that handles complex JSON
|
|
244
|
+
const deepseekRegex = /<|tool▁call▁begin|>(.*?)<|tool▁sep|>(.*?)<|tool▁call▁end|>/gs;
|
|
245
|
+
let deepseekMatch;
|
|
246
|
+
while ((deepseekMatch = deepseekRegex.exec(text)) !== null) {
|
|
247
|
+
try {
|
|
248
|
+
const name = deepseekMatch[1].trim();
|
|
249
|
+
const argsJson = deepseekMatch[2].trim();
|
|
250
|
+
const args = parseJsonWithFallback(argsJson);
|
|
251
|
+
toolCalls.push({ name, arguments: args });
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
console.warn('Failed to parse DeepSeek tool call:', error);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
default: { // Qwen format
|
|
260
|
+
// Parse Qwen format with more robust regex that handles multiline JSON
|
|
261
|
+
const qwenRegex = /<tool_call>\s*([\s\S]*?)\s*<\/tool_call>/g;
|
|
262
|
+
let qwenMatch;
|
|
263
|
+
while ((qwenMatch = qwenRegex.exec(text)) !== null) {
|
|
264
|
+
try {
|
|
265
|
+
const jsonStr = qwenMatch[1].trim();
|
|
266
|
+
const toolCallData = parseJsonWithFallback(jsonStr);
|
|
267
|
+
if (toolCallData['name'] && toolCallData['arguments']) {
|
|
268
|
+
toolCalls.push({
|
|
269
|
+
name: toolCallData['name'],
|
|
270
|
+
arguments: toolCallData['arguments']
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
console.warn('Failed to parse Qwen tool call:', error);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return toolCalls;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Parse JSON with fallback error handling
|
|
285
|
+
*/
|
|
286
|
+
function parseJsonWithFallback(jsonStr) {
|
|
287
|
+
try {
|
|
288
|
+
return JSON.parse(jsonStr);
|
|
289
|
+
}
|
|
290
|
+
catch (_error) {
|
|
291
|
+
// Try to fix common JSON issues
|
|
292
|
+
let fixedJson = jsonStr;
|
|
293
|
+
// Fix trailing commas
|
|
294
|
+
fixedJson = fixedJson.replace(/,(\s*[}\]])/g, '$1');
|
|
295
|
+
// Try parsing again
|
|
296
|
+
try {
|
|
297
|
+
return JSON.parse(fixedJson);
|
|
298
|
+
}
|
|
299
|
+
catch (_secondError) {
|
|
300
|
+
// If still fails, try to extract key-value pairs manually
|
|
301
|
+
return extractKeyValuePairs(jsonStr);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Extract key-value pairs from malformed JSON as fallback
|
|
307
|
+
*/
|
|
308
|
+
function extractKeyValuePairs(jsonStr) {
|
|
309
|
+
const result = {};
|
|
310
|
+
// Simple regex to extract "key": "value" or "key": value patterns
|
|
311
|
+
const kvRegex = /"([^"]+)"\s*:\s*("([^"]*)"|([^,}\]]+))/g;
|
|
312
|
+
let match;
|
|
313
|
+
while ((match = kvRegex.exec(jsonStr)) !== null) {
|
|
314
|
+
const key = match[1];
|
|
315
|
+
const value = match[3] !== undefined ? match[3] : match[4]?.trim();
|
|
316
|
+
if (value !== undefined) {
|
|
317
|
+
// Try to parse value as number or boolean
|
|
318
|
+
if (value === 'true') {
|
|
319
|
+
result[key] = true;
|
|
320
|
+
}
|
|
321
|
+
else if (value === 'false') {
|
|
322
|
+
result[key] = false;
|
|
323
|
+
}
|
|
324
|
+
else if (!isNaN(Number(value))) {
|
|
325
|
+
result[key] = Number(value);
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
result[key] = value;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return result;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Detect model type from response text
|
|
336
|
+
*/
|
|
337
|
+
export function detectModelType(text) {
|
|
338
|
+
// Check for MiniMax format tokens
|
|
339
|
+
if (text.includes('<minimax:tool_call>') || text.includes('<invoke name=')) {
|
|
340
|
+
return 'minimax';
|
|
341
|
+
}
|
|
342
|
+
// Check for DeepSeek format tokens
|
|
343
|
+
if (text.includes('<|tool▁calls▁begin|>') || text.includes('<|tool▁call▁begin|>')) {
|
|
344
|
+
return 'deepseek';
|
|
345
|
+
}
|
|
346
|
+
// Check for Qwen format tokens
|
|
347
|
+
if (text.includes('<tool_call>')) {
|
|
348
|
+
return 'qwen';
|
|
349
|
+
}
|
|
350
|
+
// Default to MiniMax format
|
|
351
|
+
return 'minimax';
|
|
352
|
+
}
|
|
353
|
+
//# sourceMappingURL=tool-formatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-formatter.js","sourceRoot":"","sources":["../../../src/encrypt/tool-formatter.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAgBH;;;GAGG;AACH,MAAM,U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