Skip to content

feat(py): Add client= parameter to Chat for batteries-included chatlas apps#233

Open
cpsievert wants to merge 4 commits into
mainfrom
feat/chat-auto-module
Open

feat(py): Add client= parameter to Chat for batteries-included chatlas apps#233
cpsievert wants to merge 4 commits into
mainfrom
feat/chat-auto-module

Conversation

@cpsievert
Copy link
Copy Markdown
Collaborator

@cpsievert cpsievert commented May 26, 2026

Summary

Building a chatbot with shinychat + chatlas currently requires manually wiring up streaming, cancellation, and bookmarking — roughly 20-30 lines of boilerplate that look the same in every app. This PR adds a client= parameter to Chat that handles all of it automatically.

Express:

from chatlas import ChatAnthropic
from shinychat.express import Chat

chat = Chat(id="chat", client=ChatAnthropic())
chat.ui()

Core:

from chatlas import ChatAnthropic
from shiny import App, ui
from shinychat import Chat, chat_ui

app_ui = ui.page_fillable(
    chat_ui("chat", enable_cancel=True),
)

def server(input, output, session):
    chat = Chat("chat", client=ChatAnthropic())

app = App(app_ui, server)

No new API surface to learn — just one parameter on the class you already know. Client-specific methods live on a ChatClient wrapper at chat.client:

  • .value — the raw chatlas client
  • .set(new_client, sync=True) — swap models mid-session (deferred if streaming)
  • .clear(client_history="clear") — reset UI and client turn history

In Express mode, enable_cancel defaults to True when a client is set.

R package changes

  • chat_mod_server() now returns set_client(new_client, sync=TRUE) for swapping clients at runtime, and a status reactive ("idle" / "streaming").
  • chat_restore() returns a cancel function for clean re-registration during client swaps.

Other changes

  • enable_bookmarking() no longer raises when no bookmark store is configured — it silently no-ops instead.
  • Docs lead with Chat(client=...) as the recommended path.

Test plan

  • uv run pytest — unit tests verify ChatClient behavior (swap, clear, exports, messages_to_turns)
  • uv run pytest pkg-py/tests/playwright — Playwright tests cover Express, Core, cancellation, and bookmark-no-store flows
  • uv run pyright — type checking passes
  • uv run ruff check pkg-py --config pyproject.toml — linting passes
  • R package unaffected: cd pkg-r && Rscript -e "devtools::check(document=FALSE)" still passes

@cpsievert cpsievert marked this pull request as draft May 26, 2026 22:34
@cpsievert cpsievert changed the title feat(py): Add batteries-included chat_auto API for chatlas-powered apps feat(py): Add batteries-included API for chatlas-powered apps May 26, 2026
@cpsievert cpsievert force-pushed the feat/chat-auto-module branch 7 times, most recently from 787b682 to a882451 Compare May 26, 2026 22:48
@cpsievert cpsievert requested a review from gadenbuie May 26, 2026 22:49
@cpsievert

This comment was marked as outdated.

@cpsievert cpsievert changed the title feat(py): Add batteries-included API for chatlas-powered apps feat(py): Add client= parameter to Chat for batteries-included chatlas apps May 27, 2026
cpsievert and others added 2 commits May 27, 2026 10:40
Add chat_auto_ui(), chat_auto_server(), and ChatExpress.ui_auto() — a
high-level API that wires streaming, cancellation, and bookmarking to a
chatlas client automatically.

Also fix enable_bookmarking() to silently no-op when no bookmark store
is configured (both Python and R), so it can be called unconditionally.
Instead of a separate ChatAutoServer/chat_auto_server/chat_auto_ui API
surface, users now pass client= directly to Chat(). When provided,
streaming, cancellation, bookmarking, and greeting handling are wired
up automatically.

The client-specific surface lives on a ChatClient wrapper at
chat.client, with .value (raw chatlas object), .set() for swapping
models mid-session, and .clear() for resetting conversation history.

Express mode auto-defaults enable_cancel=True when a client is set.

- Add _chat_client.py with ChatClient class and helpers
- Add client=, greeting=, bookmark_on= params to Chat.__init__
- Add _setup_client() for reactive wiring
- Remove _chat_auto.py, ChatAutoServer, chat_auto_server, chat_auto_ui
- Remove ChatExpress.ui_auto() method
- Remove bookmark-store raise from enable_bookmarking()
- Update exports, tests, docs, and changelog
@cpsievert cpsievert force-pushed the feat/chat-auto-module branch 2 times, most recently from b45d438 to a9d70b9 Compare May 27, 2026 15:44
cpsievert added 2 commits May 27, 2026 10:46
… public API

greeting= now works regardless of whether client= is provided. bookmark_on
is hardcoded to "response" internally. Removed the thin _setup_greeting
wrapper; added docstrings for client and greeting parameters.
@cpsievert
Copy link
Copy Markdown
Collaborator Author

@gadenbuie there's a bit more review and polish I'd like to do here, but before I do, would be great to have a gut check from you in terms of the overall shape of the proposal here.

@cpsievert cpsievert marked this pull request as ready for review May 27, 2026 16:11
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