-
Notifications
You must be signed in to change notification settings - Fork 60k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/isValidMessage #5579
Closed
Closed
Fix/isValidMessage #5579
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7fa8234
add more criteria for detecting invalid messages
code-october a039690
add response judgment conditions for summarizing messages request
code-october 4bb52b2
调整判断规则、优化代码规范
code-october 7d7ca64
Merge branch 'ChatGPTNextWeb:main' into fix/isValidMessage
code-october dd793e0
完善判断逻辑,添加jest测试样例
code-october fe9c31c
fix isValidMessage.test.ts
code-october b0236f7
fix jest error
code-october File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -681,6 +681,7 @@ export const useChatStore = createPersistStore( | |
}, | ||
onFinish(message) { | ||
console.log("[Memory] ", message); | ||
if (!isValidMessage(message)) return; | ||
get().updateCurrentSession((session) => { | ||
session.lastSummarizeIndex = lastSummarizeIndex; | ||
session.memoryPrompt = message; // Update the memory prompt for stored it in local storage | ||
|
@@ -693,7 +694,26 @@ export const useChatStore = createPersistStore( | |
} | ||
|
||
function isValidMessage(message: any): boolean { | ||
return typeof message === "string" && !message.startsWith("```json"); | ||
if (typeof message !== "string") { | ||
return false; | ||
} | ||
if (message.startsWith("```") && message.endsWith("```")) { | ||
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?.message) { | ||
return false; | ||
} | ||
return true; | ||
} catch (e) { | ||
console.log("Invalid JSON format."); | ||
// 非 json 格式,大概率是正常回复 | ||
return true; | ||
} | ||
} | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance robustness of The
Here's a suggested refactor: function isValidMessage(message: any): boolean {
if (typeof message !== "string") {
return false;
}
if (message.startsWith("```") && message.endsWith("```")) {
const codeBlockContent = message.slice(3, -3).trim();
- const jsonString = codeBlockContent.replace(/^json\s*/i, '').trim();
+ if (codeBlockContent.toLowerCase().startsWith('json')) {
+ const jsonString = codeBlockContent.replace(/^json\s*/i, '').trim();
+ try {
+ const jsonObject = JSON.parse(jsonString);
+ return !jsonObject?.error?.message;
+ } catch (e) {
+ console.log("Invalid JSON format:", e.message);
+ return false;
+ }
+ }
+ // Non-JSON code blocks are considered valid
+ return true;
}
- try {
- // 返回 json 格式消息,含 error.message 字段,判定为错误回复,否则为正常回复
- const jsonObject = JSON.parse(jsonString);
- if (jsonObject?.error?.message) {
- return false;
- }
- return true;
- } catch (e) {
- console.log("Invalid JSON format.");
- // 非 json 格式,大概率是正常回复
- return true;
- }
- }
return true;
} This refactor:
|
||
} | ||
}, | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Potential unvalidated message processing detected in
app/store/chat.ts
at line 696.Please ensure that
isValidMessage
is invoked consistently wherever messages are processed to maintain validation integrity.🔗 Analysis chain
Verify that message validation is consistently applied
In the
onFinish
callback, the checkif (!isValidMessage(message)) return;
ensures that invalid messages are not processed. To maintain consistency and robustness, ensure that all instances where messages are processed include validation usingisValidMessage
.Run the following script to check for other message handling without validation:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 2295