Skip to content

Commit 3d9bc44

Browse files
cpsievertCopilot
andauthored
feat(pkg-py): Add new message_content() and message_content_chunk() (#96)
* feat(contents_shinychat): Add new contents_shinychat() and contents_shinychat_chunk() These new functions are the singledispatch equivalent of the previous normalize_message() and normalize_message_chunk(). Although not quite as flexible as the previous strategies pattern, it's much simpler (implementation wise and also for those registering new methods) and aligns much better with the vision for the R package * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * Fix format check * Rename * Add ChatMessage to types module. Add types and playwright module to top-level namespace * Fix for Anthropic; cleanup * Just require python 3.11 with Anthropic * Update pkg-py/src/shinychat/_chat_normalize.py Co-authored-by: Copilot <[email protected]> * Just require python 3.11 with Anthropic * Just require python 3.11 with Anthropic * Add a test for registering custom objects * Ignore missing anthropic imports when type checking * fml * rename (again) * rename (again) --------- Co-authored-by: Copilot <[email protected]>
1 parent c2fa3a9 commit 3d9bc44

File tree

8 files changed

+320
-341
lines changed

8 files changed

+320
-341
lines changed

pkg-py/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [UNRELEASED]
99

10-
* The chat input no longer submits incomplete text when the user has activated IME completions (e.g. while typing in Japanese or Chinese). (#85)
10+
### New features
11+
12+
* Added new `message_content()` and `message_content_chunk()` generic (`singledispatch`) functions. These functions aren't intended to be called directly by users, but instead, provide an opportunity to teach `Chat.append_message()`/`Chat.append_message_stream()` to extract message contents from different types of objects. (#96)
1113

14+
### Bug fixes
15+
16+
* The chat input no longer submits incomplete text when the user has activated IME completions (e.g. while typing in Japanese or Chinese). (#85)
1217

1318
## [0.1.0] - 2025-08-07
1419

1520
This first release of the `shinychat` package simply copies the `Chat` and `MarkdownStream` components exactly as they are in version 1.4.0 of `shiny`. Future versions of `shiny` will import these components from `shinychat`. By maintaining these components via a separate library, we can ship features more quickly and independently of `shiny`.
16-

pkg-py/src/shinychat/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
from . import playwright, types
12
from ._chat import Chat, chat_ui
3+
from ._chat_normalize import message_content, message_content_chunk
24
from ._markdown_stream import MarkdownStream, output_markdown_stream
35

4-
__all__ = ["Chat", "chat_ui", "MarkdownStream", "output_markdown_stream"]
6+
__all__ = [
7+
"Chat",
8+
"chat_ui",
9+
"MarkdownStream",
10+
"output_markdown_stream",
11+
"message_content",
12+
"message_content_chunk",
13+
"types",
14+
"playwright",
15+
]

pkg-py/src/shinychat/_chat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
is_chatlas_chat_client,
5353
set_chatlas_state,
5454
)
55-
from ._chat_normalize import normalize_message, normalize_message_chunk
55+
from ._chat_normalize import message_content, message_content_chunk
5656
from ._chat_provider_types import (
57-
AnthropicMessage,
57+
AnthropicMessage, # pyright: ignore[reportAttributeAccessIssue]
5858
GoogleMessage,
5959
LangChainMessage,
6060
OllamaMessage,
@@ -636,7 +636,7 @@ async def append_message(
636636
self._pending_messages.append((message, False, "append", None))
637637
return
638638

639-
msg = normalize_message(message)
639+
msg = message_content(message)
640640
msg = await self._transform_message(msg)
641641
if msg is None:
642642
return
@@ -753,7 +753,7 @@ async def _append_message_chunk(
753753
self._current_stream_id = stream_id
754754

755755
# Normalize various message types into a ChatMessage()
756-
msg = normalize_message_chunk(message)
756+
msg = message_content_chunk(message)
757757

758758
if operation == "replace":
759759
self._current_stream_message = (

0 commit comments

Comments
 (0)