From 4bb52b2a19f1b8248a525736f52e5a309a912d11 Mon Sep 17 00:00:00 2001 From: code-october <148516338+code-october@users.noreply.github.com> Date: Fri, 11 Oct 2024 14:38:03 +0000 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=88=A4=E6=96=AD=E8=A7=84?= =?UTF-8?q?=E5=88=99=E3=80=81=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E8=A7=84?= =?UTF-8?q?=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/store/chat.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index 60558932cf5..8ee86d38909 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -694,18 +694,26 @@ export const useChatStore = createPersistStore( } function isValidMessage(message: any): boolean { + if (typeof message !== "string") { + return false; + } if (message.startsWith("```") && message.endsWith("```")) { - const jsonString = message.slice(3, -3).trim(); + const codeBlockContent = message.slice(3, -3).trim(); + const jsonString = codeBlockContent.replace(/^json\s*/i, '').trim(); try { + // 返回 json 格式消息,含 error.message 字段,判定为错误回复,否则为正常回复 const jsonObject = JSON.parse(jsonString); - if (jsonObject.error) { + if (jsonObject?.error?.message) { return false; } + return true; } catch (e) { console.log("Invalid JSON format."); + // 非 json 格式,大概率是正常回复 + return true; } } - return typeof message === "string" && !message.startsWith("```json"); + return true; } },