Skip to content

fix(ai-chat): abort/cancel support for streaming responses#983

Merged
threepointone merged 1 commit intomainfrom
fix/ai-chat-abort-and-test-config
Feb 24, 2026
Merged

fix(ai-chat): abort/cancel support for streaming responses#983
threepointone merged 1 commit intomainfrom
fix/ai-chat-abort-and-test-config

Conversation

@threepointone
Copy link
Contributor

Summary

Fixes the stop button in AI Chat so it properly aborts streaming responses, and adds a framework-level safety net for cancellation.

What changed

Framework (@cloudflare/ai-chat)

  • Abort signal propagation in _reply, _streamSSEReply, _sendPlaintextReply: When the abort signal fires, the reader loop is now cancelled and a done signal is sent to the client. Previously, cancelling a request would send the cancel message but the server-side stream would keep running until it finished naturally.
  • Warning log: If cancellation arrives but the stream has not closed, a warning is logged suggesting the developer forgot to pass abortSignal to their LLM call (e.g. streamText). This helps catch a common misconfiguration.

Example (examples/ai-chat)

  • Pass options.abortSignal through to streamText in server.ts so the upstream LLM call is actually cancelled when the user hits stop.

Test infrastructure

  • Added cancel-request.test.ts with 5 tests covering: plaintext cancel, SSE cancel, abort controller cleanup, cancel with abortSignal wired, and full stream completion without cancel.
  • Added SlowStreamAgent test fixture that streams chunks with configurable delays.
  • Fixed vitest project configs (src/tests/vitest.config.ts, src/react-tests/vitest.config.ts) to add explicit include patterns. Without these, vitest's default glob was picking up e2e/*.spec.ts files and trying to run them in the Workers pool (causing No such module "node:child_process" errors) and react-test files in the wrong runner.

Reviewer notes

  • The core abort logic lives in packages/ai-chat/src/index.ts around the _reply, _streamSSEReply, and _sendPlaintextReply methods. Look for the abortSignal event listener pattern — when abort fires, we cancel the reader, send the done signal, and log a warning if the stream was still open.
  • The vitest config fix is minimal (include globs) but important — without it, npm run test from the ai-chat package root would fail on CI because Playwright e2e tests and browser-mode react tests got picked up by the Workers pool. The e2e tests are intentionally separate and run via npm run test:e2e / the nightly CI workflow.
  • The SlowStreamAgent in worker.ts is a test-only Durable Object. It simulates a slow LLM by yielding chunks with setTimeout delays, which gives the cancel tests enough time to send a cancel message mid-stream.
  • All 223 existing tests continue to pass, plus the 5 new cancel tests.

Testing

# Unit/integration tests (workers pool)
cd packages/ai-chat && npx vitest run

# 25 files, 223 tests — all pass

- Pass abortSignal from options to streamText in ai-chat example
- Framework safety net: cancel reader loop on abort signal, send done signal
- Warning log when cancel arrives but stream still active (missing abortSignal)
- Add cancel-request tests (plaintext, SSE, abort cleanup, full completion)
- Fix vitest configs to scope test file discovery (prevents e2e/react cross-pickup)
@changeset-bot
Copy link

changeset-bot bot commented Feb 24, 2026

🦋 Changeset detected

Latest commit: 0ac1fa2

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

This PR includes changesets to release 1 package
Name Type
@cloudflare/ai-chat 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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 24, 2026

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@983
npm i https://pkg.pr.new/cloudflare/agents/@cloudflare/ai-chat@983
npm i https://pkg.pr.new/cloudflare/agents/@cloudflare/codemode@983
npm i https://pkg.pr.new/cloudflare/agents/hono-agents@983

commit: 0ac1fa2

@threepointone threepointone merged commit 2785f10 into main Feb 24, 2026
4 checks passed
@threepointone threepointone deleted the fix/ai-chat-abort-and-test-config branch February 24, 2026 23:45
@github-actions github-actions bot mentioned this pull request Feb 24, 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.

1 participant