fix: MALFORMED_FUNCTION_CALL

This commit is contained in:
ikechan8370 2025-02-17 23:54:08 +08:00
parent b9ed12adb7
commit bde81c0a17

View file

@ -111,9 +111,10 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
* search: boolean, * search: boolean,
* codeExecution: boolean, * codeExecution: boolean,
* }} opt * }} opt
* @param {number} retryTime 重试次数
* @returns {Promise<{conversationId: string?, parentMessageId: string, text: string, id: string}>} * @returns {Promise<{conversationId: string?, parentMessageId: string, text: string, id: string}>}
*/ */
async sendMessage (text, opt = {}) { async sendMessage (text, opt = {}, retryTime = 3) {
let history = await this.getHistory(opt.parentMessageId) let history = await this.getHistory(opt.parentMessageId)
let systemMessage = opt.system let systemMessage = opt.system
// if (systemMessage) { // if (systemMessage) {
@ -269,7 +270,7 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
*/ */
let responseContent let responseContent
/** /**
* @type {{candidates: Array<{content: Content, groundingMetadata: GroundingMetadata}>}} * @type {{candidates: Array<{content: Content, groundingMetadata: GroundingMetadata, finishReason: string}>}}
*/ */
let response = await result.json() let response = await result.json()
if (this.debug) { if (this.debug) {
@ -277,6 +278,11 @@ export class CustomGoogleGeminiClient extends GoogleGeminiClient {
} }
responseContent = response.candidates[0].content responseContent = response.candidates[0].content
let groundingMetadata = response.candidates[0].groundingMetadata let groundingMetadata = response.candidates[0].groundingMetadata
if (response.candidates[0].finishReason === 'MALFORMED_FUNCTION_CALL') {
logger.warn('遇到MALFORMED_FUNCTION_CALL进行重试。')
return this.sendMessage(text, opt, retryTime--)
}
// todo 空回复也可以重试
if (responseContent.parts.filter(i => i.functionCall).length > 0) { if (responseContent.parts.filter(i => i.functionCall).length > 0) {
// functionCall // functionCall
const functionCall = responseContent.parts.filter(i => i.functionCall).map(i => i.functionCall) const functionCall = responseContent.parts.filter(i => i.functionCall).map(i => i.functionCall)