6868chatlas_is_installed = importlib .util .find_spec ("chatlas" ) is not None
6969
7070
71+ def is_chatlas_chat_client (client : Any ) -> bool :
72+ if not chatlas_is_installed :
73+ return False
74+ import chatlas
75+
76+ return isinstance (client , chatlas .Chat )
77+
78+
7179@runtime_checkable
7280class ClientWithState (Protocol ):
7381 async def get_state (self ) -> Jsonifiable : ...
@@ -279,7 +287,7 @@ def __init__(
279287
280288 # Keep track of effects so we can destroy them when the chat is destroyed
281289 self ._effects : list [reactive .Effect_ ] = []
282- self ._cancel_bookmarking_callbacks : CancelCallback | None
290+ self ._cancel_bookmarking_callbacks : CancelCallback | None = None
283291
284292 # Initialize chat state and user input effect
285293 with session_context (self ._session ):
@@ -1463,7 +1471,7 @@ def enable_bookmarking(
14631471 get_state = wrap_async (client .get_state )
14641472 set_state = wrap_async (client .set_state )
14651473
1466- elif chatlas_is_installed and isinstance (client , chatlas . Chat ):
1474+ elif is_chatlas_chat_client (client ):
14671475
14681476 from chatlas import Turn as ChatlasTurn
14691477
@@ -1576,7 +1584,6 @@ async def _on_restore_client(state: RestoreState):
15761584
15771585 @root_session .bookmark .on_restore
15781586 async def _on_restore_ui (state : RestoreState ):
1579- print ("set chat ui" )
15801587 # Do not call `self.clear_messages()` as it will clear the
15811588 # `chat.ui(messages=)` in addition to the `self.messages()`
15821589 # (which is not what we want).
@@ -1585,16 +1592,19 @@ async def _on_restore_ui(state: RestoreState):
15851592 # and `self.messages()` are never initialized due to
15861593 # calling `self._init_chat.destroy()` above
15871594
1588- msgs : list [Any ] = state .values [resolved_bookmark_id_msgs_str ]
1589- if not msgs :
1595+ if resolved_bookmark_id_msgs_str not in state .values :
15901596 # If no messages to restore, display the `__init__(messages=)` messages
15911597 await self ._append_init_messages ()
15921598 return
15931599
1594- print ("restored msgs" , msgs )
1600+ msgs : list [Any ] = state .values [resolved_bookmark_id_msgs_str ]
1601+ if not isinstance (msgs , list ):
1602+ raise ValueError (
1603+ f"Bookmark value with id (`{ resolved_bookmark_id_msgs_str } `) must be a list of messages."
1604+ )
1605+
15951606 for message_dict in msgs :
15961607 await self .append_message (message_dict )
1597- print ("done-restore ui" )
15981608
15991609 def _cancel_bookmarking ():
16001610 _on_bookmark_client ()
0 commit comments