From e0b0b5bacf863e696d1a8e8ccdc27e3077413b44 Mon Sep 17 00:00:00 2001 From: Jerryplusy Date: Mon, 5 Jan 2026 18:05:02 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20fix(openaiChat):=20impr?= =?UTF-8?q?ove=20AI=20response=20parsing=20and=20error=20handling=20for=20?= =?UTF-8?q?better=20logging=20and=20response=20extraction=20(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/openai/openaiChat.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/openai/openaiChat.js b/modules/openai/openaiChat.js index e59f8f6..bc0a920 100644 --- a/modules/openai/openaiChat.js +++ b/modules/openai/openaiChat.js @@ -62,9 +62,32 @@ class OpenaiChat { presence_penalty: 0.2, stream:false }); - - const aiResponse = completion.choices[0].message.content; - //logger.info(aiResponse); + let parsedCompletion = completion; + if (typeof completion === 'string') { + try { + parsedCompletion = JSON.parse(completion); + } catch (parseError) { + logger.error('[crystelf-ai] 响应JSON解析失败:', parseError); + return { success: false }; + } + } + + //logger.info("[DEBUG] 解析后的响应:", JSON.stringify(parsedCompletion)); + let aiResponse = null; + + if (parsedCompletion && parsedCompletion.choices && Array.isArray(parsedCompletion.choices) && parsedCompletion.choices.length > 0) { + const choice = parsedCompletion.choices[0]; + if (choice && choice.message && choice.message.content) { + aiResponse = choice.message.content; + } + } + + if (!aiResponse) { + logger.error('[crystelf-ai] 无法从响应中提取AI回复内容:', parsedCompletion); + return { success: false }; + } + + logger.info("[DEBUG] AI响应内容:", aiResponse); return { success: true, aiResponse: aiResponse,