gemini修复思考模型调用工具后报错
Browse files- src/utils/utils.js +35 -11
src/utils/utils.js
CHANGED
|
@@ -5,7 +5,7 @@ import os from 'os';
|
|
| 5 |
import { getReasoningSignature, getToolSignature } from './thoughtSignatureCache.js';
|
| 6 |
import { setToolNameMapping } from './toolNameCache.js';
|
| 7 |
|
| 8 |
-
// 思维链签名常量
|
| 9 |
// Claude 模型签名
|
| 10 |
const CLAUDE_THOUGHT_SIGNATURE = 'RXFRRENrZ0lDaEFDR0FJcVFKV1Bvcy9GV20wSmtMV2FmWkFEbGF1ZTZzQTdRcFlTc1NvbklmemtSNFo4c1dqeitIRHBOYW9hS2NYTE1TeTF3bjh2T1RHdE1KVjVuYUNQclZ5cm9DMFNETHk4M0hOSWsrTG1aRUhNZ3hvTTl0ZEpXUDl6UUMzOExxc2ZJakI0UkkxWE1mdWJ1VDQrZnY0Znp0VEoyTlhtMjZKL2daYi9HL1gwcmR4b2x0VE54empLemtLcEp0ZXRia2plb3NBcWlRSWlXUHloMGhVVTk1dHNha1dyNDVWNUo3MTJjZDNxdHQ5Z0dkbjdFaFk4dUllUC9CcThVY2VZZC9YbFpYbDc2bHpEbmdzL2lDZXlNY3NuZXdQMjZBTDRaQzJReXdibVQzbXlSZmpld3ZSaUxxOWR1TVNidHIxYXRtYTJ0U1JIRjI0Z0JwUnpadE1RTmoyMjR4bTZVNUdRNXlOSWVzUXNFNmJzRGNSV0RTMGFVOEZERExybmhVQWZQT2JYMG5lTGR1QnU1VGZOWW9NZGlRbTgyUHVqVE1xaTlmN0t2QmJEUUdCeXdyVXR2eUNnTEFHNHNqeWluZDRCOEg3N2ZJamt5blI3Q3ZpQzlIOTVxSENVTCt3K3JzMmsvV0sxNlVsbGlTK0pET3UxWXpPMWRPOUp3V3hEMHd5ZVU0a0Y5MjIxaUE5Z2lUd2djZXhSU2c4TWJVMm1NSjJlaGdlY3g0YjJ3QloxR0FFPQ==';
|
| 11 |
// Gemini 思维链签名
|
|
@@ -496,10 +496,10 @@ function generateGeminiRequestBody(geminiBody, modelName, token){
|
|
| 496 |
const request = JSON.parse(JSON.stringify(geminiBody));
|
| 497 |
//console.log(JSON.stringify(request,null,2));
|
| 498 |
|
| 499 |
-
// 处理 contents 中的 functionCall 和 functionResponse,确保
|
| 500 |
if (request.contents && Array.isArray(request.contents)) {
|
| 501 |
-
//
|
| 502 |
-
const functionCallIds =
|
| 503 |
request.contents.forEach(content => {
|
| 504 |
if (content.role === 'model' && content.parts && Array.isArray(content.parts)) {
|
| 505 |
content.parts.forEach(part => {
|
|
@@ -507,25 +507,47 @@ function generateGeminiRequestBody(geminiBody, modelName, token){
|
|
| 507 |
if (!part.functionCall.id) {
|
| 508 |
part.functionCall.id = `call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
| 509 |
}
|
| 510 |
-
|
| 511 |
-
functionCallIds.set(part.functionCall.name, part.functionCall.id);
|
| 512 |
}
|
| 513 |
});
|
| 514 |
}
|
| 515 |
});
|
| 516 |
|
| 517 |
-
//
|
|
|
|
| 518 |
request.contents.forEach(content => {
|
| 519 |
if (content.role === 'user' && content.parts && Array.isArray(content.parts)) {
|
| 520 |
content.parts.forEach(part => {
|
| 521 |
-
if (part.functionResponse
|
| 522 |
-
//
|
| 523 |
-
|
| 524 |
-
|
|
|
|
|
|
|
| 525 |
}
|
| 526 |
});
|
| 527 |
}
|
| 528 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
}
|
| 530 |
|
| 531 |
// 确保 generationConfig 存在
|
|
@@ -560,6 +582,7 @@ function generateGeminiRequestBody(geminiBody, modelName, token){
|
|
| 560 |
model: actualModelName,
|
| 561 |
userAgent: "antigravity"
|
| 562 |
};
|
|
|
|
| 563 |
|
| 564 |
return requestBody;
|
| 565 |
}
|
|
@@ -854,6 +877,7 @@ function generateClaudeRequestBody(claudeMessages, modelName, parameters, claude
|
|
| 854 |
parts: [{ text: mergedSystem }]
|
| 855 |
};
|
| 856 |
}
|
|
|
|
| 857 |
|
| 858 |
return requestBody;
|
| 859 |
}
|
|
|
|
| 5 |
import { getReasoningSignature, getToolSignature } from './thoughtSignatureCache.js';
|
| 6 |
import { setToolNameMapping } from './toolNameCache.js';
|
| 7 |
|
| 8 |
+
// 思维链签名常量
|
| 9 |
// Claude 模型签名
|
| 10 |
const CLAUDE_THOUGHT_SIGNATURE = 'RXFRRENrZ0lDaEFDR0FJcVFKV1Bvcy9GV20wSmtMV2FmWkFEbGF1ZTZzQTdRcFlTc1NvbklmemtSNFo4c1dqeitIRHBOYW9hS2NYTE1TeTF3bjh2T1RHdE1KVjVuYUNQclZ5cm9DMFNETHk4M0hOSWsrTG1aRUhNZ3hvTTl0ZEpXUDl6UUMzOExxc2ZJakI0UkkxWE1mdWJ1VDQrZnY0Znp0VEoyTlhtMjZKL2daYi9HL1gwcmR4b2x0VE54empLemtLcEp0ZXRia2plb3NBcWlRSWlXUHloMGhVVTk1dHNha1dyNDVWNUo3MTJjZDNxdHQ5Z0dkbjdFaFk4dUllUC9CcThVY2VZZC9YbFpYbDc2bHpEbmdzL2lDZXlNY3NuZXdQMjZBTDRaQzJReXdibVQzbXlSZmpld3ZSaUxxOWR1TVNidHIxYXRtYTJ0U1JIRjI0Z0JwUnpadE1RTmoyMjR4bTZVNUdRNXlOSWVzUXNFNmJzRGNSV0RTMGFVOEZERExybmhVQWZQT2JYMG5lTGR1QnU1VGZOWW9NZGlRbTgyUHVqVE1xaTlmN0t2QmJEUUdCeXdyVXR2eUNnTEFHNHNqeWluZDRCOEg3N2ZJamt5blI3Q3ZpQzlIOTVxSENVTCt3K3JzMmsvV0sxNlVsbGlTK0pET3UxWXpPMWRPOUp3V3hEMHd5ZVU0a0Y5MjIxaUE5Z2lUd2djZXhSU2c4TWJVMm1NSjJlaGdlY3g0YjJ3QloxR0FFPQ==';
|
| 11 |
// Gemini 思维链签名
|
|
|
|
| 496 |
const request = JSON.parse(JSON.stringify(geminiBody));
|
| 497 |
//console.log(JSON.stringify(request,null,2));
|
| 498 |
|
| 499 |
+
// 处理 contents 中的 functionCall 和 functionResponse,确保 id 匹配
|
| 500 |
if (request.contents && Array.isArray(request.contents)) {
|
| 501 |
+
// 收集所有 functionCall 的 id(按顺序)
|
| 502 |
+
const functionCallIds = [];
|
| 503 |
request.contents.forEach(content => {
|
| 504 |
if (content.role === 'model' && content.parts && Array.isArray(content.parts)) {
|
| 505 |
content.parts.forEach(part => {
|
|
|
|
| 507 |
if (!part.functionCall.id) {
|
| 508 |
part.functionCall.id = `call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
| 509 |
}
|
| 510 |
+
functionCallIds.push(part.functionCall.id);
|
|
|
|
| 511 |
}
|
| 512 |
});
|
| 513 |
}
|
| 514 |
});
|
| 515 |
|
| 516 |
+
// 为 functionResponse 匹配对应的 functionCall id
|
| 517 |
+
let responseIndex = 0;
|
| 518 |
request.contents.forEach(content => {
|
| 519 |
if (content.role === 'user' && content.parts && Array.isArray(content.parts)) {
|
| 520 |
content.parts.forEach(part => {
|
| 521 |
+
if (part.functionResponse) {
|
| 522 |
+
// 如果没有 id,按顺序匹配 functionCall 的 id
|
| 523 |
+
if (!part.functionResponse.id && responseIndex < functionCallIds.length) {
|
| 524 |
+
part.functionResponse.id = functionCallIds[responseIndex];
|
| 525 |
+
responseIndex++;
|
| 526 |
+
}
|
| 527 |
}
|
| 528 |
});
|
| 529 |
}
|
| 530 |
});
|
| 531 |
+
|
| 532 |
+
// 处理思考模型的思维链
|
| 533 |
+
if (enableThinking) {
|
| 534 |
+
const cachedSig = getReasoningSignature(token.sessionId, actualModelName);
|
| 535 |
+
const thoughtSignature = cachedSig || getThoughtSignatureForModel(actualModelName);
|
| 536 |
+
|
| 537 |
+
request.contents.forEach(content => {
|
| 538 |
+
if (content.role === 'model' && content.parts && Array.isArray(content.parts)) {
|
| 539 |
+
// 检查是否已有思维链标记
|
| 540 |
+
const hasThought = content.parts.some(p => p.thought === true);
|
| 541 |
+
if (!hasThought) {
|
| 542 |
+
// 在 parts 开头插入思维链占位符和签名
|
| 543 |
+
content.parts.unshift(
|
| 544 |
+
{ text: ' ', thought: true },
|
| 545 |
+
{ text: ' ', thoughtSignature },
|
| 546 |
+
);
|
| 547 |
+
}
|
| 548 |
+
}
|
| 549 |
+
});
|
| 550 |
+
}
|
| 551 |
}
|
| 552 |
|
| 553 |
// 确保 generationConfig 存在
|
|
|
|
| 582 |
model: actualModelName,
|
| 583 |
userAgent: "antigravity"
|
| 584 |
};
|
| 585 |
+
//console.log(JSON.stringify(requestBody, null, 2))
|
| 586 |
|
| 587 |
return requestBody;
|
| 588 |
}
|
|
|
|
| 877 |
parts: [{ text: mergedSystem }]
|
| 878 |
};
|
| 879 |
}
|
| 880 |
+
//console.log(JSON.stringify(requestBody, null, 2));
|
| 881 |
|
| 882 |
return requestBody;
|
| 883 |
}
|