feat: add stream cancellation support for R and Python#241
Merged
Conversation
Users can now cancel in-progress LLM responses via a stop button that appears during streaming. This leverages shinychat's enable_cancel UI and cooperative cancellation via ellmer::stream_controller() (R) and chatlas.StreamController (Python). Dependency bumps: - chatlas >= 0.18.0 (StreamController) - shinychat >= 0.4.0 (enable_cancel) - ellmer >= 0.4.1 (stream_controller)
- Add Remotes field to DESCRIPTION for dev shinychat from GitHub - Add chat_cancel to mock input objects in unit tests
The dev shinychat on GitHub reports 0.3.0.9000, which doesn't satisfy >= 0.4.0. Use >= 0.3.0.9000 until shinychat 0.4.0 is released.
cpsievert
commented
May 26, 2026
Use input[f"{CHAT_ID}_cancel"] instead of hardcoded input.chat_cancel,
and make the test mock input subscriptable to support bracket access.
Python: use kwargs.setdefault() so callers can override. R: accept enable_cancel as a named argument with default TRUE.
The cancel_app was nearly identical to 01-hello-app, and cancellation is always enabled, so a separate app isn't needed.
These tests were deeply coupled to internal wiring (closure vars, patched Shiny primitives, fake sessions). The Playwright E2E tests cover the same user-visible behavior through the real stack.
The viz tests depend on LLM reliably producing tool calls, which is inherently non-deterministic. This flakiness also occurs on main (see runs from May 14). Bump reruns from 2 to 4 and timeout from 30 to 45 minutes.
The viz example app had no static greeting, so the LLM generated one via stream_async on startup. Tests didn't wait for this greeting stream to finish before sending user input, causing two concurrent streams on the same Chat object — corrupting conversation history and producing flaky failures (especially under parallel workers and with shinychat 0.4.0's new cancel button). - Add greeting-viz.md with a static greeting for the titanic viz app - Update 10-viz-app.py and viz_bookmark_app.py to use the static greeting - Wait for greeting text to render before sending test input - Remove bookmark test's dependency on greeting triggering a bookmark (static messages may not trigger bookmark_on="response" due to ignore_init timing)
The increased reruns (2→4) and timeout (30→45 min) were a workaround for flaky viz tests caused by the LLM-generated greeting race condition, which is now fixed by the static greeting approach.
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.
Summary
Adds the ability to cancel in-progress LLM response streams in both R and Python. When a user sends a message, a stop button now appears during streaming — clicking it (or pressing Escape) cooperatively cancels the response.
Dependency bumps
StreamControllerclassenable_cancelparam inchat_ui()enable_cancelparam inchat_ui()stream_controller()Note: shinychat 0.4.0 hasn't been released yet in either language. Python is temporarily installed from GitHub via
[tool.uv.sources]; R requires a dev install (pak::pak("posit-dev/shinychat/pkg-r")).Test plan