Skip to content

Chat.makeRequest finally throws on this.activeResponse!.state when activeResponse is undefined (should be ?.) #15668

Description

@alexanderjacobsen

Chat.makeRequest's finally builds the onFinish argument as:

message: this.activeResponse!.state.message,        // packages/ai/src/ui/chat.ts (main: 760)
...
finishReason: this.activeResponse?.state.finishReason,

this.activeResponse can be undefined when that finally runs (e.g. a trigger: 'resume-stream' request that overlaps another makeRequest such that the single shared this.activeResponse was already cleared), so the non-null assertion throws:

Cannot read properties of undefined (reading 'state')
  at Chat.makeRequest (packages/ai/src/ui/chat.ts:760)
  at async AbstractChat.resumeStream (packages/ai/src/ui/chat.ts:465)

The very next line already guards finishReason with ?., so the ! on message looks like an oversight.

It's caught (the finally wraps onFinish in a try/catch → console.error), so it's non-fatal — but it trips dev error overlays. It surfaces via @cloudflare/ai-chat, which drives client-tool continuations through resumeStream → makeRequest, so it fires on every client-tool continuation. Reproduces on 6.0.19x; not on 6.0.1.

Suggested fix:

- message: this.activeResponse!.state.message,
+ message: this.activeResponse?.state.message,

Happy to PR if useful.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions