From 221cf4082d682a33d4e5036a24302398413eb56a Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Sun, 29 Dec 2024 20:11:32 +0800 Subject: [PATCH] fix: gemini function call --- client/CustomGoogleGeminiClient.js | 34 +++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/client/CustomGoogleGeminiClient.js b/client/CustomGoogleGeminiClient.js index 08b9090..1ed5542 100644 --- a/client/CustomGoogleGeminiClient.js +++ b/client/CustomGoogleGeminiClient.js @@ -228,6 +228,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient { delete content.parentMessageId delete content.conversationId }) + // logger.info(JSON.stringify(body)) let result = await newFetch(url, { method: 'POST', body: JSON.stringify(body), @@ -366,20 +367,37 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient { */ function handleSearchResponse (responseContent) { let final = '' - for (let part of responseContent.parts) { + + // 遍历每个 part 并处理 + responseContent.parts = responseContent.parts.map((part) => { + let newText = '' + if (part.text) { - final += part.text + newText += part.text + final += part.text // 累积到 final } if (part.executableCode) { - final += '\n执行代码:\n' + '```' + part.executableCode.language + '\n' + part.executableCode.code.trim() + '\n```\n\n' + const codeBlock = '\n执行代码:\n' + '```' + part.executableCode.language + '\n' + part.executableCode.code.trim() + '\n```\n\n' + newText += codeBlock + final += codeBlock // 累积到 final } if (part.codeExecutionResult) { - final += `\n执行结果(${part.codeExecutionResult.outcome}):\n` + '```\n' + part.codeExecutionResult.output + '\n```\n\n' + const resultBlock = `\n执行结果(${part.codeExecutionResult.outcome}):\n` + '```\n' + part.codeExecutionResult.output + '\n```\n\n' + newText += resultBlock + final += resultBlock // 累积到 final } - } - responseContent.parts = [{ - text: final - }] + + // 返回更新后的 part,但不设置空的 text + const updatedPart = { ...part } + if (newText) { + updatedPart.text = newText // 仅在 newText 非空时设置 text + } else { + delete updatedPart.text // 如果 newText 是空的,则删除 text 字段 + } + + return updatedPart + }) + return { final, responseContent