Skip to content

fix: allow returning a non-streaming reponse from onChatMessage()#761

Merged
threepointone merged 5 commits into
cloudflare:mainfrom
iTrooz:respond_with_raw_string
Jan 8, 2026
Merged

fix: allow returning a non-streaming reponse from onChatMessage()#761
threepointone merged 5 commits into
cloudflare:mainfrom
iTrooz:respond_with_raw_string

Conversation

@iTrooz

@iTrooz iTrooz commented Dec 29, 2025

Copy link
Copy Markdown
Contributor

Currently, doing

  async onChatMessage(
    onFinish: StreamTextOnFinishCallback<ToolSet>,
    _options?: { abortSignal?: AbortSignal }
  ) {
    return new Response("Hello !");
  }

from an AIChatAgent creates an error on the frontend, because doing so is only generating a text-delta event, and not the text-start and text-end events that should go in-between, according to https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#text-start-part (from my understanding ?)

This PR fixes it by adding these 2 events.

@changeset-bot

changeset-bot Bot commented Dec 29, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6ffbee0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@cloudflare/ai-chat Patch
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@iTrooz
iTrooz force-pushed the respond_with_raw_string branch from 9fe6f2a to 6e83bfe Compare December 30, 2025 00:04
@iTrooz
iTrooz force-pushed the respond_with_raw_string branch from 6e83bfe to 1e8d4f7 Compare December 30, 2025 00:14
@iTrooz iTrooz changed the title fix: allow returning a normal reponse from onChatMessage() fix: allow returning a non-streaming reponse from onChatMessage() Dec 30, 2025
@pkg-pr-new

pkg-pr-new Bot commented Dec 30, 2025

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@761

commit: 6ffbee0

- Add _broadcastTextEvent helper with discriminated union for type safety
- Inject text-start/text-end events for plain text responses per AI SDK v5 protocol
- Add id field to text-delta events (was missing)
- Move isSSE check outside the loop
- Add tests verifying event sequence and id consistency
pull common code into a helper file

@threepointone threepointone left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moves some common stuff into modules

@threepointone threepointone left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually hold up I think I want to refactor a bit more, I'll land it when happy

@iTrooz

iTrooz commented Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

I was planning to submit a second PR refactoring the file after this, but if you prefer to do it yourself, sure

@threepointone

Copy link
Copy Markdown
Contributor

let's land this and then work on the next one

@threepointone
threepointone merged commit 0e8fc1e into cloudflare:main Jan 8, 2026
3 of 5 checks passed
@iTrooz
iTrooz deleted the respond_with_raw_string branch January 8, 2026 13:10
@github-actions github-actions Bot mentioned this pull request Jan 8, 2026
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.

3 participants