完善判断逻辑,添加jest测试样例

This commit is contained in:
code-october
2024-10-12 06:19:11 +00:00
parent 7d7ca641a3
commit dd793e0833
2 changed files with 99 additions and 3 deletions

View File

@@ -743,15 +743,15 @@ export const useChatStore = createPersistStore(
const codeBlockContent = message.slice(3, -3).trim();
const jsonString = codeBlockContent.replace(/^json\s*/i, '').trim();
try {
// 返回 json 格式消息,含 error.message 字段,判定为错误回复,否则为正常回复
// 返回 json 格式消息,error 字段为 true 或者包含 error.message 字段,判定为错误回复,否则为正常回复
const jsonObject = JSON.parse(jsonString);
if (jsonObject?.error?.message) {
if (jsonObject?.error == true || jsonObject?.error?.message) {
return false;
}
return true;
} catch (e) {
console.log("Invalid JSON format.");
// 非 json 格式,大概率是正常回复
// 非 json 格式,通常可认为是正常回复
return true;
}
}