From aa4f70d762bf78408d26288b9b69e66bb811f0ed Mon Sep 17 00:00:00 2001 From: Kris Date: Thu, 9 Apr 2026 14:02:49 +0800 Subject: [PATCH] fix(ui): add missing optional chaining for activeResponse in makeRequest finally block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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` --- packages/ai/src/ui/chat.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/ai/src/ui/chat.ts b/packages/ai/src/ui/chat.ts index 6dbd3c661c8f..990d2d8ab27b 100644 --- a/packages/ai/src/ui/chat.ts +++ b/packages/ai/src/ui/chat.ts @@ -172,7 +172,7 @@ export type ChatOnDataCallback = ( * @param finishReason The reason why the generation finished. */ export type ChatOnFinishCallback = (options: { - message: UI_MESSAGE; + message: UI_MESSAGE | undefined; messages: UI_MESSAGE[]; isAbort: boolean; isDisconnect: boolean; @@ -758,12 +758,12 @@ export abstract class AbstractChat { } finally { try { this.onFinish?.({ - message: this.activeResponse!.state.message, + message: this.activeResponse?.state?.message, messages: this.state.messages, isAbort, isDisconnect, isError, - finishReason: this.activeResponse?.state.finishReason, + finishReason: this.activeResponse?.state?.finishReason, }); } catch (err) { console.error(err);