fix(agent): clear stale chat error when switching provider or resetting#1001
Open
Aarjav Jain (A2rjav) wants to merge 1 commit into
Open
fix(agent): clear stale chat error when switching provider or resetting#1001Aarjav Jain (A2rjav) wants to merge 1 commit into
Aarjav Jain (A2rjav) wants to merge 1 commit into
Conversation
Closes browseros-ai#862 useChat from @ai-sdk/react keeps its `error` populated until a subsequent request succeeds and exposes no public clearError. After a failure (credits exhausted, daily limit, invalid auth, JSON parse) the banner persisted through provider/model switches and reset clicks, blocking recovery within the same session. - New chat-error-suppression module: - computeVisibleChatError hides chatError while its reference matches a recorded "stale" marker - isStaleErrorMarkerStillCurrent gates a one-shot effect that drops the marker as soon as useChat produces a different error reference or clears it on its own - useChatSession records the marker in two recovery paths: - handleSelectProvider on actual provider change (independent of message count, so first-send auth failures with zero assistant turns are also covered) - resetConversation on the explicit clear-chat action - Returned chatError is filtered through computeVisibleChatError so the Chat UI never re-renders the suppressed banner - 9 unit tests covering: empty state, no marker, matching reference, different reference (new error appears), structurally identical but distinct Error instances, and every marker-currency transition
Contributor
|
PR author is not in the allowed authors list. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #862
When a chat fails (credits exhausted, daily limit, invalid auth, malformed JSON),
useChatfrom@ai-sdk/reactkeeps itserrorpopulated until a subsequent request succeeds, and exposes no publicclearError. The banner therefore persists across provider/model switches and even after the user clicks "Reset conversation" — so users can't recover within the same session and end up restarting the browser.This PR adds a small, surgical suppression layer that hides the stale error after a recovery action while still letting genuinely new errors through.
Approach
chat-error-suppression.tsmodule with two pure helpers:computeVisibleChatError— hideschatErrorwhile its reference matches a recorded marker.isStaleErrorMarkerStillCurrent— gates the effect that drops the marker as soon asuseChatproduces a different error reference or clears it on its own.useChatSession.tscaptures the marker in two recovery paths:handleSelectProvideron actual provider change, independent of message count so first-send auth failures (which produce zero assistant turns) are also covered.resetConversationon the explicit clear-chat action.chatErroris filtered throughcomputeVisibleChatError, soChat.tsxandChatError.tsxneed no changes.Why reference equality rather than tracking which provider the error belonged to?
useChatallocates a freshErrorfor each failed request, so the next failure on the new provider has a different reference and falls through the suppression naturally. Cleaner than juggling provider IDs and immune to AI SDK internals.Changes
apps/agent/entrypoints/sidepanel/index/chat-error-suppression.ts— new pure-function module (32 lines incl. doc comment).apps/agent/entrypoints/sidepanel/index/chat-error-suppression.test.ts— 9 unit tests covering: no error, no marker, matching reference, different reference (fresh error appears), structurally identical but distinctErrorinstances, and every marker-currency transition.apps/agent/entrypoints/sidepanel/index/useChatSession.ts— wires the helpers in; the visible-error filter, the dependency-driven effect that auto-drops the marker, and the two recovery-path marker captures.Test plan
bun test apps/agent/entrypoints/sidepanel/index/chat-error-suppression.test.ts— 9/9 pass.bun test apps/agent/entrypoints/sidepanel/index/— 21/21 pass (no regressions in adjacent files).bunx @biomejs/biome checkon the three touched files — clean.Credits exhausted), switch to another provider/model from the picker → banner clears and the new send is allowed.Errorreference is not suppressed).