Skip to content

Commit 2a6d934

Browse files
committed
Include the operation when queueing message chunks
1 parent bd5e610 commit 2a6d934

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

shiny/ui/_chat.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@
8282

8383
ChunkOption = Literal["start", "end", True, False]
8484

85-
PendingMessage = Tuple[Any, ChunkOption, Union[str, None]]
85+
PendingMessage = Tuple[
86+
Any,
87+
ChunkOption,
88+
Literal["append", "replace"],
89+
Union[str, None],
90+
]
8691

8792

8893
@add_example(ex_dir="../templates/chat/starters/hello")
@@ -576,7 +581,7 @@ async def append_message(
576581
"""
577582
# If we're in a stream, queue the message
578583
if self._current_stream_id:
579-
self._pending_messages.append((message, False, None))
584+
self._pending_messages.append((message, False, "append", None))
580585
return
581586

582587
msg = normalize_message(message)
@@ -685,7 +690,7 @@ async def _append_message_chunk(
685690
) -> None:
686691
# If currently we're in a *different* stream, queue the message chunk
687692
if self._current_stream_id and self._current_stream_id != stream_id:
688-
self._pending_messages.append((message, chunk, stream_id))
693+
self._pending_messages.append((message, chunk, operation, stream_id))
689694
return
690695

691696
self._current_stream_id = stream_id
@@ -876,12 +881,15 @@ async def _append_message_stream(
876881
async def _flush_pending_messages(self):
877882
pending = self._pending_messages
878883
self._pending_messages = []
879-
for msg, chunk, stream_id in pending:
884+
for msg, chunk, operation, stream_id in pending:
880885
if chunk is False:
881886
await self.append_message(msg)
882887
else:
883888
await self._append_message_chunk(
884-
msg, chunk=chunk, stream_id=cast(str, stream_id)
889+
msg,
890+
chunk=chunk,
891+
operation=operation,
892+
stream_id=cast(str, stream_id),
885893
)
886894

887895
# Send a message to the UI

0 commit comments

Comments
 (0)