Skip to content

Commit 97efb12

Browse files
committed
Use pydantic json objects, not str for serialization
1 parent 63c2dfb commit 97efb12

File tree

1 file changed

+13
-34
lines changed

1 file changed

+13
-34
lines changed

shiny/ui/_chat_bookmark.py

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
import importlib.util
2-
from typing import (
3-
TYPE_CHECKING,
4-
Any,
5-
Awaitable,
6-
Callable,
7-
Protocol,
8-
cast,
9-
runtime_checkable,
10-
)
2+
from typing import Any, Awaitable, Callable, Protocol, runtime_checkable
113

124
from htmltools import TagChild
135

146
from .._utils import CancelCallback
157
from ..types import Jsonifiable
168

17-
if TYPE_CHECKING:
18-
19-
import chatlas
20-
21-
else:
22-
chatlas = object
23-
24-
259
chatlas_is_installed = importlib.util.find_spec("chatlas") is not None
2610

2711

@@ -78,39 +62,34 @@ def tagify(self) -> TagChild:
7862

7963
# Chatlas specific implementation
8064
def get_chatlas_state(
81-
client: chatlas.Chat[Any, Any],
65+
client: Any,
8266
) -> Callable[[], Awaitable[Jsonifiable]]:
8367

84-
from chatlas import Turn as ChatlasTurn
68+
from chatlas import Chat, Turn
69+
70+
assert isinstance(client, Chat)
8571

8672
async def get_state() -> Jsonifiable:
8773

88-
turns: list[ChatlasTurn[Any]] = client.get_turns()
89-
turns_json_str: list[str] = [turn.model_dump_json() for turn in turns]
90-
return cast(Jsonifiable, turns_json_str)
74+
turns: list[Turn[Any]] = client.get_turns()
75+
return [turn.model_dump(mode="json") for turn in turns]
9176

9277
return get_state
9378

9479

9580
def set_chatlas_state(
96-
client: chatlas.Chat[Any, Any],
81+
client: Any,
9782
) -> Callable[[Jsonifiable], Awaitable[None]]:
98-
from chatlas import Turn as ChatlasTurn
83+
from chatlas import Chat, Turn
84+
85+
assert isinstance(client, Chat)
9986

10087
async def set_state(value: Jsonifiable) -> None:
10188

10289
if not isinstance(value, list):
103-
raise ValueError("Chatlas bookmark value must be a list of JSON strings")
104-
for v in value:
105-
if not isinstance(v, str):
106-
raise ValueError("Chat bookmark value must be a list of strings")
107-
108-
turns_json_str = cast(list[str], value)
90+
raise ValueError("Chatlas bookmark value was not a list of objects")
10991

110-
turns: list[ChatlasTurn[Any]] = [
111-
ChatlasTurn.model_validate_json(turn_json_str)
112-
for turn_json_str in turns_json_str
113-
]
92+
turns: list[Turn[Any]] = [Turn.model_validate(turn_obj) for turn_obj in value]
11493
client.set_turns(turns) # pyright: ignore[reportUnknownMemberType]
11594

11695
return set_state

0 commit comments

Comments
 (0)