Skip to content

Commit dd5197d

Browse files
committed
Follow up to #1846: replace .get_latest_stream_result() with a more general .get_message_stream()
1 parent c65ca17 commit dd5197d

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* The `ui.Chat()` component also gains the following:
2626
* The `.on_user_submit()` decorator method now passes the user input to the decorated function. This makes it a bit easier to access the user input. See the new templates (mentioned below) for examples. (#1801)
2727
* The assistant icon is now configurable via `ui.chat_ui()` (or the `ui.Chat.ui()` method in Shiny Express) or for individual messages in the `.append_message()` and `.append_message_stream()` methods of `ui.Chat()`. (#1853)
28-
* A new `get_latest_stream_result()` method was added for an easy way to access the final result of the stream when it completes. (#1846)
28+
* A new `get_message_stream()` method was added for an easy way to reactive read the stream's status, result, and also cancel an in progress stream. (#1846)
2929
* The `.append_message_stream()` method now returns the `reactive.extended_task` instance that it launches. (#1846)
3030
* The `ui.Chat()` component's `.update_user_input()` method gains `submit` and `focus` options that allow you to submit the input on behalf of the user and to choose whether the input receives focus after the update. (#1851)
3131

shiny/ui/_chat.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,13 @@ def __init__(
210210
reactive.Value(None)
211211
)
212212

213-
self._latest_stream: reactive.Value[
214-
reactive.ExtendedTask[[], str] | None
215-
] = reactive.Value(None)
213+
@reactive.extended_task
214+
async def _mock_task() -> str:
215+
return ""
216+
217+
self._latest_stream: reactive.Value[reactive.ExtendedTask[[], str]] = (
218+
reactive.Value(_mock_task)
219+
)
216220

217221
# TODO: deprecate messages once we start promoting managing LLM message
218222
# state through other means
@@ -669,32 +673,34 @@ async def _handle_error():
669673

670674
return _stream_task
671675

672-
def get_latest_stream_result(self) -> str | None:
676+
def get_message_stream(self) -> reactive.ExtendedTask[[], str]:
673677
"""
674-
Reactively read the latest message stream result.
678+
React to changes in the latest message stream.
679+
680+
Reactively reads for the latest :class:`~shiny.reactive.ExtendedTask` behind the
681+
latest message stream.
675682
676-
This method reads a reactive value containing the result of the latest
677-
`.append_message_stream()`. Therefore, this method must be called in a reactive
678-
context (e.g., a render function, a :func:`~shiny.reactive.calc`, or a
679-
:func:`~shiny.reactive.effect`).
683+
From the return value (i.e., the extended task), you can then:
684+
685+
1. Reactively read for the final `.result()`.
686+
2. `.cancel()` the stream.
687+
3. Check the `.status()` of the stream.
680688
681689
Returns
682690
-------
683691
:
684-
The result of the latest stream (a string).
692+
An extended task that represents the streaming task. The `.result()` method
693+
of the task can be called in a reactive context to get the final state of the
694+
stream.
685695
686-
Raises
687-
------
688-
:
689-
A silent exception if no stream has completed yet.
696+
Note
697+
----
698+
If no stream has yet been started when this method is called, then a "mock" task
699+
is returned. This mock task behaves much like a stream that hasn't yet completed,
700+
except it has a `.status()` of "initial" instead of "running", and `.cancel()`
701+
is a no-op.
690702
"""
691-
stream = self._latest_stream()
692-
if stream is None:
693-
from .. import req
694-
695-
req(False)
696-
else:
697-
return stream.result()
703+
return self._latest_stream()
698704

699705
async def _append_message_stream(
700706
self,

0 commit comments

Comments
 (0)