Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### New features

* `ui.Chat()` gains a new `.update_user_input()` method, which adds the ability to update the input placeholder message. As a result, `.set_user_message()` is now deprecated (since the new method can also be used to update the message). (#1594)

### Other changes

### Bug fixes
Expand Down
24 changes: 19 additions & 5 deletions js/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ type requestScrollEvent = {
cancelIfScrolledUp: boolean;
};

type UpdateUserInput = {
value?: string;
placeholder?: string;
};

// https://github.com/microsoft/TypeScript/issues/28357#issuecomment-748550734
declare global {
interface GlobalEventHandlersEventMap {
"shiny-chat-input-sent": CustomEvent<Message>;
"shiny-chat-append-message": CustomEvent<Message>;
"shiny-chat-append-message-chunk": CustomEvent<Message>;
"shiny-chat-clear-messages": CustomEvent;
"shiny-chat-set-user-input": CustomEvent<string>;
"shiny-chat-update-user-input": CustomEvent<UpdateUserInput>;
"shiny-chat-remove-loading-message": CustomEvent;
"shiny-chat-request-scroll": CustomEvent<requestScrollEvent>;
}
Expand Down Expand Up @@ -270,7 +275,7 @@ class ChatContainer extends LightElement {
this.#onAppendChunk
);
this.addEventListener("shiny-chat-clear-messages", this.#onClear);
this.addEventListener("shiny-chat-set-user-input", this.#onSetUserInput);
this.addEventListener("shiny-chat-update-user-input", this.#onUpdateUserInput);
this.addEventListener(
"shiny-chat-remove-loading-message",
this.#onRemoveLoadingMessage
Expand All @@ -291,7 +296,10 @@ class ChatContainer extends LightElement {
this.#onAppendChunk
);
this.removeEventListener("shiny-chat-clear-messages", this.#onClear);
this.removeEventListener("shiny-chat-set-user-input", this.#onSetUserInput);
this.removeEventListener(
"shiny-chat-update-user-input",
this.#onUpdateUserInput
);
this.removeEventListener(
"shiny-chat-remove-loading-message",
this.#onRemoveLoadingMessage
Expand Down Expand Up @@ -373,8 +381,14 @@ class ChatContainer extends LightElement {
this.messages.innerHTML = "";
}

#onSetUserInput(event: CustomEvent<string>): void {
this.input.setInputValue(event.detail);
#onUpdateUserInput(event: CustomEvent<UpdateUserInput>): void {
const { value, placeholder } = event.detail;
if (value !== undefined) {
this.input.setInputValue(value);
}
if (placeholder !== undefined) {
this.input.placeholder = placeholder;
}
}

#onRemoveLoadingMessage(): void {
Expand Down
26 changes: 22 additions & 4 deletions shiny/ui/_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from htmltools import HTML, Tag, TagAttrValue, css

from .. import _utils, reactive
from .._deprecated import warn_deprecated
from .._docstring import add_example
from .._namespaces import ResolvedId, resolve_id
from ..session import require_active_session, session_context
Expand Down Expand Up @@ -933,27 +934,44 @@ def _user_input(self) -> str:
id = self.user_input_id
return cast(str, self._session.input[id]())

def set_user_message(self, value: str):
def update_user_input(
self, *, value: str | None = None, placeholder: str | None = None
):
"""
Set the user's message.
Update the user input.

Parameters
----------
value
The value to set the user input to.
placeholder
The placeholder text for the user input.
"""

obj = _utils.drop_none({"value": value, "placeholder": placeholder})

_utils.run_coro_sync(
self._session.send_custom_message(
"shinyChatMessage",
{
"id": self.id,
"handler": "shiny-chat-set-user-input",
"obj": value,
"handler": "shiny-chat-update-user-input",
"obj": obj,
},
)
)

def set_user_message(self, value: str):
"""
Deprecated. Use `update_user_input(value=value)` instead.
"""

warn_deprecated(
"set_user_message() is deprecated. Use update_user_input(value=value) instead."
)

self.update_user_input(value=value)

async def clear_messages(self):
"""
Clear all chat messages.
Expand Down
2 changes: 1 addition & 1 deletion shiny/www/py-shiny/chat/chat.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions shiny/www/py-shiny/chat/chat.js.map

Large diffs are not rendered by default.