Skip to content

Commit

Permalink
调整判断规则、优化代码规范
Browse files Browse the repository at this point in the history
  • Loading branch information
code-october committed Oct 11, 2024
1 parent a039690 commit 4bb52b2
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},

Expand Down

0 comments on commit 4bb52b2

Please sign in to comment.