fix(ui): add missing optional chaining for activeResponse in makeRequest finally block#14260
Open
krislavten wants to merge 1 commit intovercel:mainfrom
Open
fix(ui): add missing optional chaining for activeResponse in makeRequest finally block#14260krislavten wants to merge 1 commit intovercel:mainfrom
krislavten wants to merge 1 commit intovercel:mainfrom
Conversation
…est finally block When two `makeRequest` calls run concurrently (e.g. `sendMessage` + `resumeStream`), the first to complete sets `this.activeResponse = undefined` in its finally block. The second call's finally block then crashes with: `TypeError: Cannot read properties of undefined (reading 'state')` This happened because `this.activeResponse!.state.message` used a non-null assertion instead of optional chaining, while `this.activeResponse?.state.finishReason` on the very next property already used `?.` — an inconsistency. Changes: - Replace `this.activeResponse!.state.message` with `this.activeResponse?.state?.message` - Add `?.` to `this.activeResponse?.state?.finishReason` for consistency - Update `ChatOnFinishCallback` type to reflect that `message` can be `undefined`
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.
Bug
When two
makeRequestcalls run concurrently (e.g.sendMessage+resumeStream), the first to complete setsthis.activeResponse = undefinedin itsfinallyblock. The second call'sfinallyblock then crashes:This is caused by an inconsistency in
packages/ai/src/ui/chat.ts— theonFinishcallback in thefinallyblock uses a non-null assertion (!) formessagebut optional chaining (?.) forfinishReason:Fix
this.activeResponse!.state.messagewiththis.activeResponse?.state?.message?.to.state?.finishReasonfor full safetyChatOnFinishCallbacktype to allowmessage: UI_MESSAGE | undefinedAffected Versions
This bug exists from at least 6.0.x through the current 7.0.0-beta — any version with the
AbstractChatclass and itsmakeRequestmethod.Reproduction
onFinishcallbackmakeRequestcalls (e.g. callsendMessage()andresumeStream()without awaiting the first)this.activeResponse = undefinedfinallyblock →TypeErrorTest Plan
chat.test.tspass