Skip to content
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: clear context to avoid [belongerr] when Spark return [error] #638

Closed
wants to merge 1 commit into from

Conversation

samanhappy
Copy link
Contributor

@samanhappy samanhappy commented Nov 25, 2023

This PR aims to address the [belongerr] issue of SparkBot in #633

The reason to clear context is Spark will make the current conversation unusable for sensitive message
WX20231125-160349@2x

so I choose to clear context whenever encoutering this situation

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling in the SparkBot's message processing, ensuring better response management during issues.
    • Enhanced chat context management by resetting it in case of errors, leading to a more reliable user experience.

@sunner
Copy link
Member

sunner commented Nov 25, 2023

Thank you.

I have a concern. Clearing the context, or reporting an error and requiring creating a new chat. Which solution is better? I have no idea now.

@samanhappy
Copy link
Contributor Author

I agree with you that finding a perfect solution is tricky, here is what I think:

  • clearing the context directly is user-transparent, but Spark may block us for creating too many conversations too quickly

  • requiring creating a new chat gives user control, but it might be inconvenient if they are using multiple bots, because we can only start a new chat that includes all selected bots if I understand correctly. If we choose this solution, can we or should we just creating a new chat for bots with error like Spark?

@sunner
Copy link
Member

sunner commented Nov 26, 2023

Inspired by your reply. I have the answer now.

Any error should be exposed to users as is. Only the user can determine what to do next. There has been code that does not follow this rule and I will modify them.

Copy link
Collaborator

@PeterDaveHello PeterDaveHello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @samanhappy, I hope you're doing great! Are you still interested in continuing with this PR? It looks like there are a few areas that might need some attention. Let me know what you think!

@samanhappy
Copy link
Contributor Author

Hi @PeterDaveHello, it's been a long time since the issue was created. I'm still interested but unsure about the next steps. Any guidance?

@PeterDaveHello
Copy link
Collaborator

Hey @samanhappy,

Thanks for getting back to us! Since the consensus was to expose the error to the users and let them decide how to proceed, we could adjust your PR to reflect that.

Instead of automatically clearing the context when Spark returns an error, could we modify it so that the error message is displayed to the user? That way, they can choose whether to clear the context or start a new chat themselves. Let me know what you think.

@samanhappy
Copy link
Contributor Author

That's fine. I'll adjust the relevant code to implement it and let you know when it's done.

Copy link
Contributor

coderabbitai bot commented Oct 10, 2024

Walkthrough

The SparkBot class in src/bots/SparkBot.js has been modified to enhance error handling and response processing within its _sendPrompt method. A new variable error tracks error occurrences during message handling. The method now includes logic to set error when the event data indicates an error and to reset the chat context if an error is detected. Additionally, it resolves promises and closes the source when the event data signals the end of a message.

Changes

File Change Summary
src/bots/SparkBot.js Modified _sendPrompt method to improve error handling and response processing; added error tracking and chat context reset logic.

Possibly related PRs

  • Fix QianWen Bot #892: The changes in the QianWenBot class involve modifications to event handling logic and message processing, which may relate to the improvements in error handling and response processing in the SparkBot class, particularly in how both bots manage incoming messages and errors.

Suggested reviewers

  • PeterDaveHello

Poem

In the garden where bots play,
SparkBot hops in a brighter way.
With errors caught and context clear,
It chats with joy, spreading cheer!
A little tweak, a gentle nudge,
Now it dances, won't budge! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (1)
src/bots/SparkBot.js (1)

Line range hint 95-118: Ensure promises are properly resolved or rejected upon error

When an error occurs (event.data === "[error]"), the current implementation sets an error flag but does not resolve or reject the promise immediately. If the <end> event is not received after an error, the promise may remain pending indefinitely. To prevent this, consider resolving or rejecting the promise as soon as the error is detected.

For example, you can reject the promise when the error is encountered:

 } else if (event.data === "[error]") {
-  error = true;
+  source.close();
+  reject(new Error("An error occurred during message processing."));
 } else if (event.data.slice(-5) === "<sid>") {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e0bf419 and f84cd58.

📒 Files selected for processing (1)
  • src/bots/SparkBot.js (2 hunks)
🧰 Additional context used

Comment on lines +95 to +96
} else if (event.data === "[error]") {
error = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Display error message to the user instead of automatically clearing the context

The current implementation automatically clears the chat context when an error occurs by setting error = true and later invoking this.setChatContext(null). According to the PR discussion, the preferred approach is to display the error message to the user, allowing them to decide whether to clear the context or start a new chat. Please modify the code to present the error to the user instead of clearing the context automatically.

Would you like assistance in updating the implementation to display the error message to the user?

Comment on lines +118 to +121
// clear context if error
if (error) {
this.setChatContext(null);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Avoid clearing context inside the event listener for each message

Placing this.setChatContext(null); inside the message event listener means the context could be cleared multiple times during message processing, especially if multiple errors occur. This might not be the intended behavior and could lead to inconsistent state management. Consider handling context clearance outside the event listener or ensuring it occurs only once after all events are processed.

You might move the context clearing logic to where the source is closed or when the promise is resolved or rejected due to an error.

Comment on lines +89 to +96
let error = false;
source.addEventListener("message", (event) => {
if (event.data === "<end>") {
onUpdateResponse(callbackParam, { done: true });
source.close();
resolve();
} else if (event.data === "[error]") {
error = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify error handling by removing the unnecessary error flag

Introducing an error flag adds extra state management that may not be necessary. Since you can detect the error condition directly when event.data === "[error]", you can handle the error immediately without setting a separate flag. This simplifies the code and reduces the potential for bugs related to state handling.

Consider modifying the code as follows:

 let text = "";
-let error = false;
 source.addEventListener("message", (event) => {
   if (event.data === "<end>") {
     onUpdateResponse(callbackParam, { done: true });
     source.close();
     resolve();
-  } else if (event.data === "[error]") {
-    error = true;
+  } else if (event.data === "[error]") {
+    // Handle the error immediately
+    this.setChatContext(null); // Or display error to user
+    source.close();
+    reject(new Error("An error occurred during message processing."));
   } else if (event.data.slice(-5) === "<sid>") {
     // ignore <sid> message
     return;

Committable suggestion was skipped due to low confidence.

@samanhappy
Copy link
Contributor Author

@PeterDaveHello
I just tested an input message like "第三次世界大战" but couldn't reproduce the [belongerr] error. It seems that Spark might have improved handling for this situation, so I'm unsure if this PR is still necessary. What do you think?

@PeterDaveHello
Copy link
Collaborator

If the problem just gone away, then yes, maybe we can spend the time and effort on some other issues ;)

@samanhappy
Copy link
Contributor Author

Unable to reproduce

@samanhappy samanhappy closed this Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants