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: refresh context for UnauthorizedRequest from Bing #658

Merged
merged 1 commit into from
Oct 17, 2024

Conversation

samanhappy
Copy link
Contributor

@samanhappy samanhappy commented Dec 8, 2023

Resolve #629

When token is expired, Bing will return such message
{"type":2,"invocationId":"1","item":{"firstNewMessageIndex":null,"defaultChatName":null,"result":{"value":"UnauthorizedRequest","message":"Token issued by https://sydney.bing.com/sydney is invalid (IDX10223: Lifetime validation failed. The token is expired. ValidTo (UTC): '12/8/2023 3:48:23 AM', Current time (UTC): '12/8/2023 3:58:42 AM'.)","error":"UnauthorizedRequest","renewCert":true,"serviceVersion":"20231207.96"}}}

There should be other scenarios for UnauthorizedRequest, I'm not sure whether it's appropriate to use it for direct comparison, what is your opinion ? @sunner

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error handling for the Bing Chat Bot to address "InvalidSession" and "UnauthorizedRequest" responses, allowing for automatic retries in these scenarios.

Copy link

vercel bot commented Dec 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
chatall ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 8, 2023 11:38am

@samanhappy samanhappy changed the title Fix: refresh context for UnauthorizedRequest Fix: refresh context for UnauthorizedRequest from Bing Dec 8, 2023
@sunner
Copy link
Member

sunner commented Dec 10, 2023

Perhaps, a relogin is required?

@samanhappy
Copy link
Contributor Author

For the UnauthorizedRequest scenario, either a relogin or the creation of a new conversation is necessary. Consider the following solution:

  1. Attempt to create a new conversation and retry the operation.
  2. If successful, proceed with the remaining steps.
  3. If the attempt fails, display an error message and advise the user to perform a relogin.

The crucial implementation step involves adding a retry flag to the context and utilizing it for both retry attempts and error display. This flag serves the additional purpose of preventing an infinite recursion loop in the chat.

If you agree with this approach, I will proceed with the implementation.

@sunner
Copy link
Member

sunner commented Dec 17, 2023

Thank you @samanhappy for the detailed deep thinking. But IMO, the solution is too smart. I prefer showing the error message and let the user determine what to do next. Just as Bing Chat's behavior.

@samanhappy
Copy link
Contributor Author

When using Bing Chat, user won't get an UnauthorizedRequest message or have to relogin for expired token, so I think Bing Chat has implemented a similar solution.

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,

Hope you're doing well! I wanted to check if the token issue is still something you're interested in tackling. It seems like it might still need some attention. Let me know what you think!

@samanhappy
Copy link
Contributor Author

Hi @PeterDaveHello , what is your opinion about this issue?

@PeterDaveHello
Copy link
Collaborator

@samanhappy I'm not sure if creating a new conversation is needed, but if it is, then we can try your solution.

Copy link
Contributor

coderabbitai bot commented Oct 15, 2024

Walkthrough

The changes in this pull request enhance the error handling within the _sendPrompt method of the BingChatBot class. Specifically, the error handling now accounts for both "InvalidSession" and "UnauthorizedRequest" responses from the Copilot service, allowing the bot to create a new conversation context and retry sending prompts when either error occurs. The existing handling for other response types remains unchanged, preserving the overall control flow of the method.

Changes

File Change Summary
src/bots/microsoft/BingChatBot.js Enhanced error handling in _sendPrompt to include "UnauthorizedRequest".

Assessment against linked issues

Objective Addressed Explanation
Handle expired Bing token (629)

🐰 In the meadow, where the chatbots play,
New prompts are sent without delay.
With errors caught, they hop and cheer,
For every session, they persevere!
A dance of code, so bright and spry,
Bing's conversations will surely fly! 🌼


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: 0

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

184-187: LGTM! Consider adding retry limits and logging.

The changes effectively address the issue of handling both "InvalidSession" and "UnauthorizedRequest" errors by creating a new conversation context and retrying the prompt. This aligns well with the PR objectives and the proposed solution discussed in the comments.

Consider the following improvements:

  1. Implement a retry limit to prevent potential infinite loops:
const MAX_RETRIES = 3;
let retryCount = 0;

// Inside the if block:
if (retryCount < MAX_RETRIES) {
  retryCount++;
  // Create a new conversation and retry
  // ...
} else {
  reject(new Error(i18n.global.t("bot.maxRetriesReached")));
}
  1. Add logging for the specific error type:
console.log(`Handling ${event.item.result.value} error. Retrying...`);
  1. Consider adding a small delay before retrying to avoid potential rate-limiting:
await new Promise(resolve => setTimeout(resolve, 1000)); // 1 second delay

These suggestions can enhance the robustness and debuggability of the error handling mechanism.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

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

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

@PeterDaveHello PeterDaveHello merged commit ef042e1 into ai-shifu:main Oct 17, 2024
1 check passed
eddieai pushed a commit to eddieai/ChatALL that referenced this pull request Nov 15, 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.

[BUG] Bing token is expired
3 participants