Skip to content

Commit 260f902

Browse files
committed
.append_message() should also queue when a stream is active
1 parent 3786b79 commit 260f902

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

shiny/ui/_chat.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@
8080
UserSubmitFunction1,
8181
]
8282

83-
PendingMessage = Tuple[Any, Literal["start", "end", True], Union[str, None]]
83+
ChunkOption = Literal["start", "end", True, False]
84+
85+
PendingMessage = Tuple[Any, ChunkOption, Union[str, None]]
8486

8587

8688
@add_example(ex_dir="../templates/chat/starters/hello")
@@ -570,6 +572,11 @@ async def append_message(
570572
similar) is specified in model's completion method.
571573
:::
572574
"""
575+
# If we're in a stream, queue the message
576+
if self._current_stream_id:
577+
self._pending_messages.append((message, False, None))
578+
return
579+
573580
msg = normalize_message(message)
574581
msg = await self._transform_message(msg)
575582
if msg is None:
@@ -668,20 +675,20 @@ async def message_stream(self):
668675
await self._append_message_chunk(
669676
"",
670677
chunk="end",
671-
stream_id=self._current_stream_id,
678+
stream_id=cast(str, self._current_stream_id),
672679
)
673680

674681
async def _append_message_chunk(
675682
self,
676683
message: Any,
677684
*,
678685
chunk: Literal[True, "start", "end"] = True,
686+
stream_id: str,
679687
operation: Literal["append", "replace"] = "append",
680-
stream_id: str | None = None,
681688
icon: HTML | Tag | TagList | None = None,
682689
) -> None:
683-
# If currently we're in a stream, handle other messages (outside the stream) later
684-
if not self._can_append_message(stream_id):
690+
# If currently we're in a *different* stream, queue the message chunk
691+
if self._current_stream_id and self._current_stream_id != stream_id:
685692
self._pending_messages.append((message, chunk, stream_id))
686693
return
687694

@@ -876,24 +883,21 @@ async def _append_message_stream(
876883
await self._flush_pending_messages()
877884

878885
async def _flush_pending_messages(self):
879-
still_pending: list[PendingMessage] = []
880-
for msg, chunk, stream_id in self._pending_messages:
881-
if self._can_append_message(stream_id):
882-
await self._append_message_chunk(msg, chunk=chunk, stream_id=stream_id)
886+
pending = self._pending_messages
887+
self._pending_messages = []
888+
for msg, chunk, stream_id in pending:
889+
if chunk is False:
890+
await self.append_message(msg)
883891
else:
884-
still_pending.append((msg, chunk, stream_id))
885-
self._pending_messages = still_pending
886-
887-
def _can_append_message(self, stream_id: str | None) -> bool:
888-
if self._current_stream_id is None:
889-
return True
890-
return self._current_stream_id == stream_id
892+
await self._append_message_chunk(
893+
msg, chunk=chunk, stream_id=cast(str, stream_id)
894+
)
891895

892896
# Send a message to the UI
893897
async def _send_append_message(
894898
self,
895899
message: TransformedMessage | ChatMessage,
896-
chunk: Literal["start", "end", True, False] = False,
900+
chunk: ChunkOption = False,
897901
operation: Literal["append", "replace"] = "append",
898902
icon: HTML | Tag | TagList | None = None,
899903
):

0 commit comments

Comments
 (0)