Skip to content

Commit 7cfabaa

Browse files
authored
feat(bookmarking): Use version info when un/serializing ui.Chat() messages (#1954)
1 parent 165f566 commit 7cfabaa

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
* To enable bookmarking in Express mode, set `shiny.express.app_opts(bookmark_store=)` during the app's initial construction.
1414
* To enable bookmarking in Core mode, set `shiny.App(bookmark_store=)` when constructing the `app` object.
1515

16-
* Added a new `.enable_bookmarking(client)` method to `ui.Chat()`. This method will attach bookmark hooks to save and restore the chat's messages and client state. (#1951)
16+
* Added a new `.enable_bookmarking(client)` method to `ui.Chat()`. This method will attach bookmark hooks to save and restore the chat's messages and client state. (#1951, #1954)
1717

1818
* Both `ui.Chat()` and `ui.MarkdownStream()` now support the inclusion of Shiny UI elements inside of messages. This allows for gathering input from the user (e.g., `ui.input_select()`), displaying of rich output (e.g., `render.DataGrid()`), and more. (#1868)
1919

shiny/ui/_chat_bookmark.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ def get_chatlas_state(
7272
async def get_state() -> Jsonifiable:
7373

7474
turns: list[Turn[Any]] = client.get_turns()
75-
return [turn.model_dump(mode="json") for turn in turns]
75+
return {
76+
"version": 1,
77+
"turns": [turn.model_dump(mode="json") for turn in turns],
78+
}
7679

7780
return get_state
7881

@@ -84,12 +87,26 @@ def set_chatlas_state(
8487

8588
assert isinstance(client, Chat)
8689

90+
# TODO-future: Use pydantic model for validation
91+
# instead of manual validation
8792
async def set_state(value: Jsonifiable) -> None:
8893

89-
if not isinstance(value, list):
90-
raise ValueError("Chatlas bookmark value was not a list of objects")
94+
if not isinstance(value, dict):
95+
raise ValueError("Chatlas bookmark value was not a dictionary")
9196

92-
turns: list[Turn[Any]] = [Turn.model_validate(turn_obj) for turn_obj in value]
97+
version = value.get("version")
98+
if version != 1:
99+
raise ValueError(f"Unsupported Chatlas bookmark version: {version}")
100+
turns_arr = value.get("turns")
101+
102+
if not isinstance(turns_arr, list):
103+
raise ValueError(
104+
"Chatlas bookmark value was not a list of chat message information"
105+
)
106+
107+
turns: list[Turn[Any]] = [
108+
Turn.model_validate(turn_obj) for turn_obj in turns_arr
109+
]
93110
client.set_turns(turns) # pyright: ignore[reportUnknownMemberType]
94111

95112
return set_state

0 commit comments

Comments
 (0)