-
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
Fix/isValidMessage #5579
Changes from 2 commits
7fa8234
a039690
4bb52b2
7d7ca64
dd793e0
fe9c31c
b0236f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,6 +694,17 @@ export const useChatStore = createPersistStore( | |||||||||||
} | ||||||||||||
|
||||||||||||
function isValidMessage(message: any): boolean { | ||||||||||||
if (message.startsWith("```") && message.endsWith("```")) { | ||||||||||||
const jsonString = message.slice(3, -3).trim(); | ||||||||||||
try { | ||||||||||||
const jsonObject = JSON.parse(jsonString); | ||||||||||||
if (jsonObject.error) { | ||||||||||||
return false; | ||||||||||||
} | ||||||||||||
} catch (e) { | ||||||||||||
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 Simplify JSON extraction and improve accuracy The current implementation checks for messages enclosed in triple backticks and attempts to parse the content as JSON. However, it might be more precise to specifically check for messages starting with ```json to accurately identify JSON-formatted messages. Consider adjusting the function to enhance accuracy: - if (message.startsWith("```") && message.endsWith("```")) {
- const jsonString = message.slice(3, -3).trim();
+ if (typeof message === "string" && message.startsWith("```json") && message.endsWith("```")) {
+ const jsonString = message.slice(7, -3).trim();
try {
const jsonObject = JSON.parse(jsonString);
if (jsonObject.error) {
return false;
}
} catch (e) {
console.log("Invalid JSON format.");
return false;
}
}
- return typeof message === "string" && !message.startsWith("```json");
+ return true; This refactor:
|
||||||||||||
console.log("Invalid JSON format."); | ||||||||||||
} | ||||||||||||
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. Return In the catch block of Consider returning Apply this diff to return } catch (e) {
console.log("Invalid JSON format.");
+ return false;
} 📝 Committable suggestion
Suggested change
|
||||||||||||
} | ||||||||||||
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. Ensure In the To prevent potential errors, check if Apply this diff to add a type check: + if (typeof message !== "string") {
+ return false;
+ }
if (message.startsWith("```") && message.endsWith("```")) {
const jsonString = message.slice(3, -3).trim();
try {
const jsonObject = JSON.parse(jsonString);
if (jsonObject.error) {
return false;
}
} catch (e) {
console.log("Invalid JSON format.");
+ return false;
}
}
- return typeof message === "string" && !message.startsWith("```json");
+ return !message.startsWith("```json"); |
||||||||||||
return typeof message === "string" && !message.startsWith("```json"); | ||||||||||||
} | ||||||||||||
}, | ||||||||||||
|
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