diff --git a/CHANGELOG.md b/CHANGELOG.md index 0af22517a..f26466bd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * 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) +* The assistant icons 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) + ### Bug fixes * `ui.Chat()` now correctly handles new `ollama.chat()` return value introduced in `ollama` v0.4. (#1787) diff --git a/js/chat/chat.scss b/js/chat/chat.scss index b863aaf0c..1cc6eb784 100644 --- a/js/chat/chat.scss +++ b/js/chat/chat.scss @@ -73,12 +73,30 @@ shiny-chat-message { .message-icon { border-radius: 50%; border: var(--shiny-chat-border); + height: 2rem; + width: 2rem; + display: grid; + place-items: center; + overflow: clip; + > * { - margin: 0.5rem; - height: 20px; - width: 20px; + // images and avatars are full-bleed + height: 100%; + width: 100%; + margin: 0 !important; + object-fit: contain; + } + + > svg, + > .icon, + > .fa, + > .bi { + // icons and svgs need some padding within the circle + max-height: 66%; + max-width: 66%; } } + /* Vertically center the 2nd column (message content) */ shiny-markdown-stream { align-self: center; diff --git a/js/chat/chat.ts b/js/chat/chat.ts index e32b70594..8c6281af5 100644 --- a/js/chat/chat.ts +++ b/js/chat/chat.ts @@ -15,6 +15,7 @@ type Message = { role: "user" | "assistant"; chunk_type: "message_start" | "message_end" | null; content_type: ContentType; + icon?: string; operation: "append" | null; }; type ShinyChatMessage = { @@ -60,10 +61,12 @@ class ChatMessage extends LightElement { @property() content = "..."; @property() content_type: ContentType = "markdown"; @property({ type: Boolean, reflect: true }) streaming = false; + @property() icon = ""; render() { - const noContent = this.content.trim().length === 0; - const icon = noContent ? ICONS.dots_fade : ICONS.robot; + // Show dots until we have content + const isEmpty = this.content.trim().length === 0; + const icon = isEmpty ? ICONS.dots_fade : this.icon || ICONS.robot; return html`
@@ -262,6 +265,7 @@ class ChatInput extends LightElement { } class ChatContainer extends LightElement { + @property({ attribute: "icon-assistant" }) iconAssistant = ""; inputSentinelObserver?: IntersectionObserver; private get input(): ChatInput { @@ -381,6 +385,11 @@ class ChatContainer extends LightElement { const TAG_NAME = message.role === "user" ? CHAT_USER_MESSAGE_TAG : CHAT_MESSAGE_TAG; + + if (this.iconAssistant) { + message.icon = message.icon || this.iconAssistant; + } + const msg = createElement(TAG_NAME, message); this.messages.appendChild(msg); diff --git a/shiny/ui/_chat.py b/shiny/ui/_chat.py index f9cd874c9..7f8adbeb5 100644 --- a/shiny/ui/_chat.py +++ b/shiny/ui/_chat.py @@ -17,7 +17,7 @@ ) from weakref import WeakValueDictionary -from htmltools import HTML, Tag, TagAttrValue, css +from htmltools import HTML, Tag, TagAttrValue, TagList, css from .. import _utils, reactive from .._deprecated import warn_deprecated @@ -493,7 +493,12 @@ def messages( return tuple(res) - async def append_message(self, message: Any) -> None: + async def append_message( + self, + message: Any, + *, + icon: HTML | Tag | TagList | None = None, + ): """ Append a message to the chat. @@ -506,10 +511,14 @@ async def append_message(self, message: Any) -> None: Content strings are interpreted as markdown and rendered to HTML on the client. Content may also include specially formatted **input suggestion** links (see note below). + icon + An optional icon to display next to the message, currently only used for + assistant messages. The icon can be any HTML element (e.g., an + :func:`~shiny.ui.img` tag) or a string of HTML. Note ---- - ``{.callout-note title="Input suggestions"} + :::{.callout-note title="Input suggestions"} Input suggestions are special links that send text to the user input box when clicked (or accessed via keyboard). They can be created in the following ways: @@ -528,17 +537,22 @@ async def append_message(self, message: Any) -> None: Note that a user may also opt-out of submitting a suggestion by holding the `Alt/Option` key while clicking the suggestion link. - ``` + ::: - ```{.callout-note title="Streamed messages"} + :::{.callout-note title="Streamed messages"} Use `.append_message_stream()` instead of this method when `stream=True` (or similar) is specified in model's completion method. - ``` + ::: """ - await self._append_message(message) + await self._append_message(message, icon=icon) async def _append_message( - self, message: Any, *, chunk: ChunkOption = False, stream_id: str | None = None + self, + message: Any, + *, + chunk: ChunkOption = False, + stream_id: str | None = None, + icon: HTML | Tag | TagList | None = None, ) -> None: # If currently we're in a stream, handle other messages (outside the stream) later if not self._can_append_message(stream_id): @@ -568,9 +582,18 @@ async def _append_message( if msg is None: return self._store_message(msg, chunk=chunk) - await self._send_append_message(msg, chunk=chunk) + await self._send_append_message( + msg, + chunk=chunk, + icon=icon, + ) - async def append_message_stream(self, message: Iterable[Any] | AsyncIterable[Any]): + async def append_message_stream( + self, + message: Iterable[Any] | AsyncIterable[Any], + *, + icon: HTML | Tag | None = None, + ): """ Append a message as a stream of message chunks. @@ -583,6 +606,10 @@ async def append_message_stream(self, message: Iterable[Any] | AsyncIterable[Any OpenAI, Anthropic, Ollama, and others. Content strings are interpreted as markdown and rendered to HTML on the client. Content may also include specially formatted **input suggestion** links (see note below). + icon + An optional icon to display next to the message, currently only used for + assistant messages. The icon can be any HTML element (e.g., an + :func:`~shiny.ui.img` tag) or a string of HTML. Note ---- @@ -625,7 +652,7 @@ async def append_message_stream(self, message: Iterable[Any] | AsyncIterable[Any # Run the stream in the background to get non-blocking behavior @reactive.extended_task async def _stream_task(): - return await self._append_message_stream(message) + return await self._append_message_stream(message, icon=icon) _stream_task() @@ -669,11 +696,15 @@ def get_latest_stream_result(self) -> str | None: else: return stream.result() - async def _append_message_stream(self, message: AsyncIterable[Any]): + async def _append_message_stream( + self, + message: AsyncIterable[Any], + icon: HTML | Tag | None = None, + ): id = _utils.private_random_id() empty = ChatMessage(content="", role="assistant") - await self._append_message(empty, chunk="start", stream_id=id) + await self._append_message(empty, chunk="start", stream_id=id, icon=icon) try: async for msg in message: @@ -702,6 +733,7 @@ async def _send_append_message( self, message: TransformedMessage, chunk: ChunkOption = False, + icon: HTML | Tag | TagList | None = None, ): if message["role"] == "system": # System messages are not displayed in the UI @@ -721,6 +753,7 @@ async def _send_append_message( content = message["content_client"] content_type = "html" if isinstance(content, HTML) else "markdown" + # TODO: pass along dependencies for both content and icon (if any) msg = ClientMessage( content=str(content), role=message["role"], @@ -728,6 +761,9 @@ async def _send_append_message( chunk_type=chunk_type, ) + if icon is not None: + msg["icon"] = str(icon) + # print(msg) await self._send_custom_message(msg_type, msg) @@ -1118,7 +1154,6 @@ async def _send_custom_message(self, handler: str, obj: ClientMessage | None): @add_example(ex_dir="../templates/chat/starters/hello") class ChatExpress(Chat): - def ui( self, *, @@ -1127,6 +1162,7 @@ def ui( width: CssUnit = "min(680px, 100%)", height: CssUnit = "auto", fill: bool = True, + icon_assistant: HTML | Tag | TagList | None = None, **kwargs: TagAttrValue, ) -> Tag: """ @@ -1148,6 +1184,10 @@ def ui( fill Whether the chat should vertically take available space inside a fillable container. + icon_assistant + The icon to use for the assistant chat messages. Can be a HTML or a tag in + the form of :class:`~htmltools.HTML` or :class:`~htmltools.Tag`. If `None`, + a default robot icon is used. kwargs Additional attributes for the chat container element. """ @@ -1158,6 +1198,7 @@ def ui( width=width, height=height, fill=fill, + icon_assistant=icon_assistant, **kwargs, ) @@ -1171,6 +1212,7 @@ def chat_ui( width: CssUnit = "min(680px, 100%)", height: CssUnit = "auto", fill: bool = True, + icon_assistant: HTML | Tag | TagList | None = None, **kwargs: TagAttrValue, ) -> Tag: """ @@ -1199,6 +1241,10 @@ def chat_ui( The height of the chat container. fill Whether the chat should vertically take available space inside a fillable container. + icon_assistant + The icon to use for the assistant chat messages. Can be a HTML or a tag in + the form of :class:`~htmltools.HTML` or :class:`~htmltools.Tag`. If `None`, + a default robot icon is used. kwargs Additional attributes for the chat container element. """ @@ -1226,6 +1272,10 @@ def chat_ui( message_tags.append(Tag(tag_name, content=msg["content"])) + html_deps = None + if isinstance(icon_assistant, (Tag, TagList)): + html_deps = icon_assistant.get_dependencies() + res = Tag( "shiny-chat-container", Tag("shiny-chat-messages", *message_tags), @@ -1235,6 +1285,7 @@ def chat_ui( placeholder=placeholder, ), chat_deps(), + html_deps, { "style": css( width=as_css_unit(width), @@ -1244,6 +1295,7 @@ def chat_ui( id=id, placeholder=placeholder, fill=fill, + icon_assistant=str(icon_assistant) if icon_assistant is not None else None, **kwargs, ) diff --git a/shiny/ui/_chat_types.py b/shiny/ui/_chat_types.py index 34924904e..b10bfdf29 100644 --- a/shiny/ui/_chat_types.py +++ b/shiny/ui/_chat_types.py @@ -4,6 +4,8 @@ from htmltools import HTML +from .._typing_extensions import NotRequired + Role = Literal["assistant", "user", "system"] @@ -27,3 +29,4 @@ class TransformedMessage(TypedDict): class ClientMessage(ChatMessage): content_type: Literal["markdown", "html"] chunk_type: Literal["message_start", "message_end"] | None + icon: NotRequired[str] diff --git a/shiny/www/py-shiny/chat/chat.css b/shiny/www/py-shiny/chat/chat.css index 1a0014656..b5f53156f 100644 --- a/shiny/www/py-shiny/chat/chat.css +++ b/shiny/www/py-shiny/chat/chat.css @@ -1,2 +1,2 @@ -@charset "UTF-8";shiny-chat-container{--shiny-chat-border: var(--bs-border-width, 1px) solid var(--bs-border-color, #e9ecef);--shiny-chat-user-message-bg: RGBA(var(--bs-primary-rgb, 0, 123, 194), .06);--_chat-container-padding: .25rem;display:grid;grid-template-columns:1fr;grid-template-rows:1fr auto;margin:0 auto;gap:0;padding:var(--_chat-container-padding);padding-bottom:0}shiny-chat-container p:last-child{margin-bottom:0}shiny-chat-container .suggestion,shiny-chat-container [data-suggestion]{cursor:pointer;color:var(--bs-link-color, #007bc2);text-decoration-color:var(--bs-link-color, #007bc2);text-decoration-line:underline;text-decoration-style:dotted;text-underline-offset:2px;text-underline-offset:4px;text-decoration-thickness:2px;padding:2px}shiny-chat-container .suggestion:hover,shiny-chat-container [data-suggestion]:hover{text-decoration-style:solid}shiny-chat-container .suggestion:after,shiny-chat-container [data-suggestion]:after{content:"\2726";display:inline-block;margin-inline-start:.15em}shiny-chat-container .suggestion.submit:after,shiny-chat-container .suggestion[data-suggestion-submit=""]:after,shiny-chat-container .suggestion[data-suggestion-submit=true]:after,shiny-chat-container [data-suggestion].submit:after,shiny-chat-container [data-suggestion][data-suggestion-submit=""]:after,shiny-chat-container [data-suggestion][data-suggestion-submit=true]:after{content:"\21b5"}shiny-chat-messages{display:flex;flex-direction:column;gap:2rem;overflow:auto;margin-bottom:1rem;--_scroll-margin: 1rem;padding-right:var(--_scroll-margin);margin-right:calc(-1 * var(--_scroll-margin))}shiny-chat-message{display:grid;grid-template-columns:auto minmax(0,1fr);gap:1rem}shiny-chat-message>*{height:fit-content}shiny-chat-message .message-icon{border-radius:50%;border:var(--shiny-chat-border)}shiny-chat-message .message-icon>*{margin:.5rem;height:20px;width:20px}shiny-chat-message shiny-markdown-stream{align-self:center}shiny-user-message{align-self:flex-end;padding:.75rem 1rem;border-radius:10px;background-color:var(--shiny-chat-user-message-bg);max-width:100%}shiny-user-message[content_type=text],shiny-chat-message[content_type=text]{white-space:pre;overflow-x:auto}shiny-chat-input{--_input-padding-top: 0;--_input-padding-bottom: var(--_chat-container-padding, .25rem);margin-top:calc(-1 * var(--_input-padding-top));position:sticky;bottom:calc(-1 * var(--_input-padding-bottom));padding-block:var(--_input-padding-top) var(--_input-padding-bottom)}shiny-chat-input textarea{--bs-border-radius: 26px;resize:none;padding-right:36px!important;max-height:175px}shiny-chat-input textarea::placeholder{color:var(--bs-gray-600, #707782)!important}shiny-chat-input button{position:absolute;bottom:calc(6px + var(--_input-padding-bottom));right:8px;background-color:transparent;color:var(--bs-primary, #007bc2);transition:color .25s ease-in-out;border:none;padding:0;cursor:pointer;line-height:16px;border-radius:50%}shiny-chat-input button:disabled{cursor:not-allowed;color:var(--bs-gray-500, #8d959e)}.shiny-busy:has(shiny-chat-input[disabled]):after{display:none} +@charset "UTF-8";shiny-chat-container{--shiny-chat-border: var(--bs-border-width, 1px) solid var(--bs-border-color, #e9ecef);--shiny-chat-user-message-bg: RGBA(var(--bs-primary-rgb, 0, 123, 194), .06);--_chat-container-padding: .25rem;display:grid;grid-template-columns:1fr;grid-template-rows:1fr auto;margin:0 auto;gap:0;padding:var(--_chat-container-padding);padding-bottom:0}shiny-chat-container p:last-child{margin-bottom:0}shiny-chat-container .suggestion,shiny-chat-container [data-suggestion]{cursor:pointer;color:var(--bs-link-color, #007bc2);text-decoration-color:var(--bs-link-color, #007bc2);text-decoration-line:underline;text-decoration-style:dotted;text-underline-offset:2px;text-underline-offset:4px;text-decoration-thickness:2px;padding:2px}shiny-chat-container .suggestion:hover,shiny-chat-container [data-suggestion]:hover{text-decoration-style:solid}shiny-chat-container .suggestion:after,shiny-chat-container [data-suggestion]:after{content:"\2726";display:inline-block;margin-inline-start:.15em}shiny-chat-container .suggestion.submit:after,shiny-chat-container .suggestion[data-suggestion-submit=""]:after,shiny-chat-container .suggestion[data-suggestion-submit=true]:after,shiny-chat-container [data-suggestion].submit:after,shiny-chat-container [data-suggestion][data-suggestion-submit=""]:after,shiny-chat-container [data-suggestion][data-suggestion-submit=true]:after{content:"\21b5"}shiny-chat-messages{display:flex;flex-direction:column;gap:2rem;overflow:auto;margin-bottom:1rem;--_scroll-margin: 1rem;padding-right:var(--_scroll-margin);margin-right:calc(-1 * var(--_scroll-margin))}shiny-chat-message{display:grid;grid-template-columns:auto minmax(0,1fr);gap:1rem}shiny-chat-message>*{height:fit-content}shiny-chat-message .message-icon{border-radius:50%;border:var(--shiny-chat-border);height:2rem;width:2rem;display:grid;place-items:center;overflow:clip}shiny-chat-message .message-icon>*{height:100%;width:100%;margin:0!important;object-fit:contain}shiny-chat-message .message-icon>svg,shiny-chat-message .message-icon>.icon,shiny-chat-message .message-icon>.fa,shiny-chat-message .message-icon>.bi{max-height:66%;max-width:66%}shiny-chat-message shiny-markdown-stream{align-self:center}shiny-user-message{align-self:flex-end;padding:.75rem 1rem;border-radius:10px;background-color:var(--shiny-chat-user-message-bg);max-width:100%}shiny-user-message[content_type=text],shiny-chat-message[content_type=text]{white-space:pre;overflow-x:auto}shiny-chat-input{--_input-padding-top: 0;--_input-padding-bottom: var(--_chat-container-padding, .25rem);margin-top:calc(-1 * var(--_input-padding-top));position:sticky;bottom:calc(-1 * var(--_input-padding-bottom));padding-block:var(--_input-padding-top) var(--_input-padding-bottom)}shiny-chat-input textarea{--bs-border-radius: 26px;resize:none;padding-right:36px!important;max-height:175px}shiny-chat-input textarea::placeholder{color:var(--bs-gray-600, #707782)!important}shiny-chat-input button{position:absolute;bottom:calc(6px + var(--_input-padding-bottom));right:8px;background-color:transparent;color:var(--bs-primary, #007bc2);transition:color .25s ease-in-out;border:none;padding:0;cursor:pointer;line-height:16px;border-radius:50%}shiny-chat-input button:disabled{cursor:not-allowed;color:var(--bs-gray-500, #8d959e)}.shiny-busy:has(shiny-chat-input[disabled]):after{display:none} /*# sourceMappingURL=chat.css.map */ diff --git a/shiny/www/py-shiny/chat/chat.css.map b/shiny/www/py-shiny/chat/chat.css.map index e246bd41c..02f8d0095 100644 --- a/shiny/www/py-shiny/chat/chat.css.map +++ b/shiny/www/py-shiny/chat/chat.css.map @@ -1,7 +1,7 @@ { "version": 3, "sources": ["../../../../js/chat/chat.scss"], - "sourcesContent": ["@charset \"UTF-8\";\nshiny-chat-container {\n --shiny-chat-border: var(--bs-border-width, 1px) solid var(--bs-border-color, #e9ecef);\n --shiny-chat-user-message-bg: RGBA(var(--bs-primary-rgb, 0, 123, 194), 0.06);\n --_chat-container-padding: 0.25rem;\n display: grid;\n grid-template-columns: 1fr;\n grid-template-rows: 1fr auto;\n margin: 0 auto;\n gap: 0;\n padding: var(--_chat-container-padding);\n padding-bottom: 0;\n}\nshiny-chat-container p:last-child {\n margin-bottom: 0;\n}\nshiny-chat-container .suggestion,\nshiny-chat-container [data-suggestion] {\n cursor: pointer;\n color: var(--bs-link-color, #007bc2);\n text-decoration-color: var(--bs-link-color, #007bc2);\n text-decoration-line: underline;\n text-decoration-style: dotted;\n text-decoration-thickness: 2px;\n text-underline-offset: 2px;\n text-underline-offset: 4px;\n text-decoration-thickness: 2px;\n padding: 2px;\n}\nshiny-chat-container .suggestion:hover,\nshiny-chat-container [data-suggestion]:hover {\n text-decoration-style: solid;\n}\nshiny-chat-container .suggestion::after,\nshiny-chat-container [data-suggestion]::after {\n content: \"\u2726\";\n display: inline-block;\n margin-inline-start: 0.15em;\n}\nshiny-chat-container .suggestion.submit::after, shiny-chat-container .suggestion[data-suggestion-submit=\"\"]::after, shiny-chat-container .suggestion[data-suggestion-submit=true]::after,\nshiny-chat-container [data-suggestion].submit::after,\nshiny-chat-container [data-suggestion][data-suggestion-submit=\"\"]::after,\nshiny-chat-container [data-suggestion][data-suggestion-submit=true]::after {\n content: \"\u21B5\";\n}\n\nshiny-chat-messages {\n display: flex;\n flex-direction: column;\n gap: 2rem;\n overflow: auto;\n margin-bottom: 1rem;\n --_scroll-margin: 1rem;\n padding-right: var(--_scroll-margin);\n margin-right: calc(-1 * var(--_scroll-margin));\n}\n\nshiny-chat-message {\n display: grid;\n grid-template-columns: auto minmax(0, 1fr);\n gap: 1rem;\n /* Vertically center the 2nd column (message content) */\n}\nshiny-chat-message > * {\n height: fit-content;\n}\nshiny-chat-message .message-icon {\n border-radius: 50%;\n border: var(--shiny-chat-border);\n}\nshiny-chat-message .message-icon > * {\n margin: 0.5rem;\n height: 20px;\n width: 20px;\n}\nshiny-chat-message shiny-markdown-stream {\n align-self: center;\n}\n\n/* Align the user message to the right */\nshiny-user-message {\n align-self: flex-end;\n padding: 0.75rem 1rem;\n border-radius: 10px;\n background-color: var(--shiny-chat-user-message-bg);\n max-width: 100%;\n}\n\nshiny-user-message[content_type=text],\nshiny-chat-message[content_type=text] {\n white-space: pre;\n overflow-x: auto;\n}\n\nshiny-chat-input {\n --_input-padding-top: 0;\n --_input-padding-bottom: var(--_chat-container-padding, 0.25rem);\n margin-top: calc(-1 * var(--_input-padding-top));\n position: sticky;\n bottom: calc(-1 * var(--_input-padding-bottom));\n padding-block: var(--_input-padding-top) var(--_input-padding-bottom);\n}\nshiny-chat-input textarea {\n --bs-border-radius: 26px;\n resize: none;\n padding-right: 36px !important;\n max-height: 175px;\n}\nshiny-chat-input textarea::placeholder {\n color: var(--bs-gray-600, #707782) !important;\n}\nshiny-chat-input button {\n position: absolute;\n bottom: calc(6px + var(--_input-padding-bottom));\n right: 8px;\n background-color: transparent;\n color: var(--bs-primary, #007bc2);\n transition: color 0.25s ease-in-out;\n border: none;\n padding: 0;\n cursor: pointer;\n line-height: 16px;\n border-radius: 50%;\n}\nshiny-chat-input button:disabled {\n cursor: not-allowed;\n color: var(--bs-gray-500, #8d959e);\n}\n\n/*\n Disable the page-level pulse when the chat input is disabled\n (i.e., when a response is being generated and brought into the chat)\n*/\n.shiny-busy:has(shiny-chat-input[disabled])::after {\n display: none;\n}"], - "mappings": "iBACA,qBACE,uFACA,4EACA,kCACA,aACA,0BACA,4BAPF,cASE,MACA,uCACA,iBAEF,kCACE,gBAEF,wEAEE,eACA,oCACA,oDACA,+BACA,6BAEA,0BACA,0BACA,8BA1BF,YA6BA,oFAEE,4BAEF,oFAEE,gBACA,qBACA,0BAEF,0XAIE,gBAGF,oBACE,aACA,sBACA,SACA,cACA,mBACA,uBACA,oCACA,8CAGF,mBACE,aACA,yCACA,SAGF,qBACE,mBAEF,iCAlEA,kBAoEE,gCAEF,mCAtEA,aAwEE,YACA,WAEF,yCACE,kBAIF,mBACE,oBAjFF,uCAoFE,mDACA,eAGF,4EAEE,gBACA,gBAGF,iBACE,wBACA,gEACA,gDACA,gBACA,+CACA,qEAEF,0BACE,yBACA,YACA,6BACA,iBAEF,uCACE,4CAEF,wBACE,kBACA,gDACA,UACA,6BACA,iCACA,kCACA,YAtHF,UAwHE,eACA,iBAzHF,kBA4HA,iCACE,mBACA,kCAOF,kDACE", + "sourcesContent": ["@charset \"UTF-8\";\nshiny-chat-container {\n --shiny-chat-border: var(--bs-border-width, 1px) solid var(--bs-border-color, #e9ecef);\n --shiny-chat-user-message-bg: RGBA(var(--bs-primary-rgb, 0, 123, 194), 0.06);\n --_chat-container-padding: 0.25rem;\n display: grid;\n grid-template-columns: 1fr;\n grid-template-rows: 1fr auto;\n margin: 0 auto;\n gap: 0;\n padding: var(--_chat-container-padding);\n padding-bottom: 0;\n}\nshiny-chat-container p:last-child {\n margin-bottom: 0;\n}\nshiny-chat-container .suggestion,\nshiny-chat-container [data-suggestion] {\n cursor: pointer;\n color: var(--bs-link-color, #007bc2);\n text-decoration-color: var(--bs-link-color, #007bc2);\n text-decoration-line: underline;\n text-decoration-style: dotted;\n text-decoration-thickness: 2px;\n text-underline-offset: 2px;\n text-underline-offset: 4px;\n text-decoration-thickness: 2px;\n padding: 2px;\n}\nshiny-chat-container .suggestion:hover,\nshiny-chat-container [data-suggestion]:hover {\n text-decoration-style: solid;\n}\nshiny-chat-container .suggestion::after,\nshiny-chat-container [data-suggestion]::after {\n content: \"\u2726\";\n display: inline-block;\n margin-inline-start: 0.15em;\n}\nshiny-chat-container .suggestion.submit::after, shiny-chat-container .suggestion[data-suggestion-submit=\"\"]::after, shiny-chat-container .suggestion[data-suggestion-submit=true]::after,\nshiny-chat-container [data-suggestion].submit::after,\nshiny-chat-container [data-suggestion][data-suggestion-submit=\"\"]::after,\nshiny-chat-container [data-suggestion][data-suggestion-submit=true]::after {\n content: \"\u21B5\";\n}\n\nshiny-chat-messages {\n display: flex;\n flex-direction: column;\n gap: 2rem;\n overflow: auto;\n margin-bottom: 1rem;\n --_scroll-margin: 1rem;\n padding-right: var(--_scroll-margin);\n margin-right: calc(-1 * var(--_scroll-margin));\n}\n\nshiny-chat-message {\n display: grid;\n grid-template-columns: auto minmax(0, 1fr);\n gap: 1rem;\n /* Vertically center the 2nd column (message content) */\n}\nshiny-chat-message > * {\n height: fit-content;\n}\nshiny-chat-message .message-icon {\n border-radius: 50%;\n border: var(--shiny-chat-border);\n height: 2rem;\n width: 2rem;\n display: grid;\n place-items: center;\n overflow: clip;\n}\nshiny-chat-message .message-icon > * {\n height: 100%;\n width: 100%;\n margin: 0 !important;\n object-fit: contain;\n}\nshiny-chat-message .message-icon > svg,\nshiny-chat-message .message-icon > .icon,\nshiny-chat-message .message-icon > .fa,\nshiny-chat-message .message-icon > .bi {\n max-height: 66%;\n max-width: 66%;\n}\nshiny-chat-message shiny-markdown-stream {\n align-self: center;\n}\n\n/* Align the user message to the right */\nshiny-user-message {\n align-self: flex-end;\n padding: 0.75rem 1rem;\n border-radius: 10px;\n background-color: var(--shiny-chat-user-message-bg);\n max-width: 100%;\n}\n\nshiny-user-message[content_type=text],\nshiny-chat-message[content_type=text] {\n white-space: pre;\n overflow-x: auto;\n}\n\nshiny-chat-input {\n --_input-padding-top: 0;\n --_input-padding-bottom: var(--_chat-container-padding, 0.25rem);\n margin-top: calc(-1 * var(--_input-padding-top));\n position: sticky;\n bottom: calc(-1 * var(--_input-padding-bottom));\n padding-block: var(--_input-padding-top) var(--_input-padding-bottom);\n}\nshiny-chat-input textarea {\n --bs-border-radius: 26px;\n resize: none;\n padding-right: 36px !important;\n max-height: 175px;\n}\nshiny-chat-input textarea::placeholder {\n color: var(--bs-gray-600, #707782) !important;\n}\nshiny-chat-input button {\n position: absolute;\n bottom: calc(6px + var(--_input-padding-bottom));\n right: 8px;\n background-color: transparent;\n color: var(--bs-primary, #007bc2);\n transition: color 0.25s ease-in-out;\n border: none;\n padding: 0;\n cursor: pointer;\n line-height: 16px;\n border-radius: 50%;\n}\nshiny-chat-input button:disabled {\n cursor: not-allowed;\n color: var(--bs-gray-500, #8d959e);\n}\n\n/*\n Disable the page-level pulse when the chat input is disabled\n (i.e., when a response is being generated and brought into the chat)\n*/\n.shiny-busy:has(shiny-chat-input[disabled])::after {\n display: none;\n}"], + "mappings": "iBACA,qBACE,uFACA,4EACA,kCACA,aACA,0BACA,4BAPF,cASE,MACA,uCACA,iBAEF,kCACE,gBAEF,wEAEE,eACA,oCACA,oDACA,+BACA,6BAEA,0BACA,0BACA,8BA1BF,YA6BA,oFAEE,4BAEF,oFAEE,gBACA,qBACA,0BAEF,0XAIE,gBAGF,oBACE,aACA,sBACA,SACA,cACA,mBACA,uBACA,oCACA,8CAGF,mBACE,aACA,yCACA,SAGF,qBACE,mBAEF,iCAlEA,kBAoEE,gCACA,YACA,WACA,aACA,mBACA,cAEF,mCACE,YACA,WA7EF,mBA+EE,mBAEF,sJAIE,eACA,cAEF,yCACE,kBAIF,mBACE,oBA9FF,uCAiGE,mDACA,eAGF,4EAEE,gBACA,gBAGF,iBACE,wBACA,gEACA,gDACA,gBACA,+CACA,qEAEF,0BACE,yBACA,YACA,6BACA,iBAEF,uCACE,4CAEF,wBACE,kBACA,gDACA,UACA,6BACA,iCACA,kCACA,YAnIF,UAqIE,eACA,iBAtIF,kBAyIA,iCACE,mBACA,kCAOF,kDACE", "names": [] } diff --git a/shiny/www/py-shiny/chat/chat.js b/shiny/www/py-shiny/chat/chat.js index ef28e3c8c..cb85101c6 100644 --- a/shiny/www/py-shiny/chat/chat.js +++ b/shiny/www/py-shiny/chat/chat.js @@ -1,7 +1,7 @@ -var Ot=Object.defineProperty;var kt=Object.getOwnPropertyDescriptor;var E=(n,t,e,s)=>{for(var i=s>1?void 0:s?kt(t,e):t,r=n.length-1,o;r>=0;r--)(o=n[r])&&(i=(s?o(t,e,i):o(i))||i);return s&&i&&Ot(t,e,i),i};var q=globalThis,j=q.ShadowRoot&&(q.ShadyCSS===void 0||q.ShadyCSS.nativeShadow)&&"adoptedStyleSheets"in Document.prototype&&"replace"in CSSStyleSheet.prototype,ct=Symbol(),lt=new WeakMap,B=class{constructor(t,e,s){if(this._$cssResult$=!0,s!==ct)throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead.");this.cssText=t,this.t=e}get styleSheet(){let t=this.o,e=this.t;if(j&&t===void 0){let s=e!==void 0&&e.length===1;s&&(t=lt.get(e)),t===void 0&&((this.o=t=new CSSStyleSheet).replaceSync(this.cssText),s&<.set(e,t))}return t}toString(){return this.cssText}},dt=n=>new B(typeof n=="string"?n:n+"",void 0,ct);var Y=(n,t)=>{if(j)n.adoptedStyleSheets=t.map(e=>e instanceof CSSStyleSheet?e:e.styleSheet);else for(let e of t){let s=document.createElement("style"),i=q.litNonce;i!==void 0&&s.setAttribute("nonce",i),s.textContent=e.cssText,n.appendChild(s)}},V=j?n=>n:n=>n instanceof CSSStyleSheet?(t=>{let e="";for(let s of t.cssRules)e+=s.cssText;return dt(e)})(n):n;var{is:Nt,defineProperty:Rt,getOwnPropertyDescriptor:It,getOwnPropertyNames:Dt,getOwnPropertySymbols:qt,getPrototypeOf:Bt}=Object,z=globalThis,ut=z.trustedTypes,jt=ut?ut.emptyScript:"",Vt=z.reactiveElementPolyfillSupport,P=(n,t)=>n,U={toAttribute(n,t){switch(t){case Boolean:n=n?jt:null;break;case Object:case Array:n=n==null?n:JSON.stringify(n)}return n},fromAttribute(n,t){let e=n;switch(t){case Boolean:e=n!==null;break;case Number:e=n===null?null:Number(n);break;case Object:case Array:try{e=JSON.parse(n)}catch{e=null}}return e}},K=(n,t)=>!Nt(n,t),pt={attribute:!0,type:String,converter:U,reflect:!1,hasChanged:K};Symbol.metadata??=Symbol("metadata"),z.litPropertyMetadata??=new WeakMap;var m=class extends HTMLElement{static addInitializer(t){this._$Ei(),(this.l??=[]).push(t)}static get observedAttributes(){return this.finalize(),this._$Eh&&[...this._$Eh.keys()]}static createProperty(t,e=pt){if(e.state&&(e.attribute=!1),this._$Ei(),this.elementProperties.set(t,e),!e.noAccessor){let s=Symbol(),i=this.getPropertyDescriptor(t,s,e);i!==void 0&&Rt(this.prototype,t,i)}}static getPropertyDescriptor(t,e,s){let{get:i,set:r}=It(this.prototype,t)??{get(){return this[e]},set(o){this[e]=o}};return{get(){return i?.call(this)},set(o){let c=i?.call(this);r.call(this,o),this.requestUpdate(t,c,s)},configurable:!0,enumerable:!0}}static getPropertyOptions(t){return this.elementProperties.get(t)??pt}static _$Ei(){if(this.hasOwnProperty(P("elementProperties")))return;let t=Bt(this);t.finalize(),t.l!==void 0&&(this.l=[...t.l]),this.elementProperties=new Map(t.elementProperties)}static finalize(){if(this.hasOwnProperty(P("finalized")))return;if(this.finalized=!0,this._$Ei(),this.hasOwnProperty(P("properties"))){let e=this.properties,s=[...Dt(e),...qt(e)];for(let i of s)this.createProperty(i,e[i])}let t=this[Symbol.metadata];if(t!==null){let e=litPropertyMetadata.get(t);if(e!==void 0)for(let[s,i]of e)this.elementProperties.set(s,i)}this._$Eh=new Map;for(let[e,s]of this.elementProperties){let i=this._$Eu(e,s);i!==void 0&&this._$Eh.set(i,e)}this.elementStyles=this.finalizeStyles(this.styles)}static finalizeStyles(t){let e=[];if(Array.isArray(t)){let s=new Set(t.flat(1/0).reverse());for(let i of s)e.unshift(V(i))}else t!==void 0&&e.push(V(t));return e}static _$Eu(t,e){let s=e.attribute;return s===!1?void 0:typeof s=="string"?s:typeof t=="string"?t.toLowerCase():void 0}constructor(){super(),this._$Ep=void 0,this.isUpdatePending=!1,this.hasUpdated=!1,this._$Em=null,this._$Ev()}_$Ev(){this._$ES=new Promise(t=>this.enableUpdating=t),this._$AL=new Map,this._$E_(),this.requestUpdate(),this.constructor.l?.forEach(t=>t(this))}addController(t){(this._$EO??=new Set).add(t),this.renderRoot!==void 0&&this.isConnected&&t.hostConnected?.()}removeController(t){this._$EO?.delete(t)}_$E_(){let t=new Map,e=this.constructor.elementProperties;for(let s of e.keys())this.hasOwnProperty(s)&&(t.set(s,this[s]),delete this[s]);t.size>0&&(this._$Ep=t)}createRenderRoot(){let t=this.shadowRoot??this.attachShadow(this.constructor.shadowRootOptions);return Y(t,this.constructor.elementStyles),t}connectedCallback(){this.renderRoot??=this.createRenderRoot(),this.enableUpdating(!0),this._$EO?.forEach(t=>t.hostConnected?.())}enableUpdating(t){}disconnectedCallback(){this._$EO?.forEach(t=>t.hostDisconnected?.())}attributeChangedCallback(t,e,s){this._$AK(t,s)}_$EC(t,e){let s=this.constructor.elementProperties.get(t),i=this.constructor._$Eu(t,s);if(i!==void 0&&s.reflect===!0){let r=(s.converter?.toAttribute!==void 0?s.converter:U).toAttribute(e,s.type);this._$Em=t,r==null?this.removeAttribute(i):this.setAttribute(i,r),this._$Em=null}}_$AK(t,e){let s=this.constructor,i=s._$Eh.get(t);if(i!==void 0&&this._$Em!==i){let r=s.getPropertyOptions(i),o=typeof r.converter=="function"?{fromAttribute:r.converter}:r.converter?.fromAttribute!==void 0?r.converter:U;this._$Em=i,this[i]=o.fromAttribute(e,r.type),this._$Em=null}}requestUpdate(t,e,s){if(t!==void 0){if(s??=this.constructor.getPropertyOptions(t),!(s.hasChanged??K)(this[t],e))return;this.P(t,e,s)}this.isUpdatePending===!1&&(this._$ES=this._$ET())}P(t,e,s){this._$AL.has(t)||this._$AL.set(t,e),s.reflect===!0&&this._$Em!==t&&(this._$Ej??=new Set).add(t)}async _$ET(){this.isUpdatePending=!0;try{await this._$ES}catch(e){Promise.reject(e)}let t=this.scheduleUpdate();return t!=null&&await t,!this.isUpdatePending}scheduleUpdate(){return this.performUpdate()}performUpdate(){if(!this.isUpdatePending)return;if(!this.hasUpdated){if(this.renderRoot??=this.createRenderRoot(),this._$Ep){for(let[i,r]of this._$Ep)this[i]=r;this._$Ep=void 0}let s=this.constructor.elementProperties;if(s.size>0)for(let[i,r]of s)r.wrapped!==!0||this._$AL.has(i)||this[i]===void 0||this.P(i,this[i],r)}let t=!1,e=this._$AL;try{t=this.shouldUpdate(e),t?(this.willUpdate(e),this._$EO?.forEach(s=>s.hostUpdate?.()),this.update(e)):this._$EU()}catch(s){throw t=!1,this._$EU(),s}t&&this._$AE(e)}willUpdate(t){}_$AE(t){this._$EO?.forEach(e=>e.hostUpdated?.()),this.hasUpdated||(this.hasUpdated=!0,this.firstUpdated(t)),this.updated(t)}_$EU(){this._$AL=new Map,this.isUpdatePending=!1}get updateComplete(){return this.getUpdateComplete()}getUpdateComplete(){return this._$ES}shouldUpdate(t){return!0}update(t){this._$Ej&&=this._$Ej.forEach(e=>this._$EC(e,this[e])),this._$EU()}updated(t){}firstUpdated(t){}};m.elementStyles=[],m.shadowRootOptions={mode:"open"},m[P("elementProperties")]=new Map,m[P("finalized")]=new Map,Vt?.({ReactiveElement:m}),(z.reactiveElementVersions??=[]).push("2.0.4");var nt=globalThis,G=nt.trustedTypes,mt=G?G.createPolicy("lit-html",{createHTML:n=>n}):void 0,$t="$lit$",v=`lit$${Math.random().toFixed(9).slice(2)}$`,At="?"+v,zt=`<${At}>`,C=document,H=()=>C.createComment(""),O=n=>n===null||typeof n!="object"&&typeof n!="function",Et=Array.isArray,Kt=n=>Et(n)||typeof n?.[Symbol.iterator]=="function",Q=`[ -\f\r]`,L=/<(?:(!--|\/[^a-zA-Z])|(\/?[a-zA-Z][^>\s]*)|(\/?$))/g,gt=/-->/g,ft=/>/g,b=RegExp(`>|${Q}(?:([^\\s"'>=/]+)(${Q}*=${Q}*(?:[^ -\f\r"'\`<>=]|("|')|))|$)`,"g"),vt=/'/g,yt=/"/g,bt=/^(?:script|style|textarea|title)$/i,St=n=>(t,...e)=>({_$litType$:n,strings:t,values:e}),M=St(1),ne=St(2),g=Symbol.for("lit-noChange"),l=Symbol.for("lit-nothing"),_t=new WeakMap,S=C.createTreeWalker(C,129);function Ct(n,t){if(!Array.isArray(n)||!n.hasOwnProperty("raw"))throw Error("invalid template strings array");return mt!==void 0?mt.createHTML(t):t}var Gt=(n,t)=>{let e=n.length-1,s=[],i,r=t===2?"":"")),s]},k=class n{constructor({strings:t,_$litType$:e},s){let i;this.parts=[];let r=0,o=0,c=t.length-1,a=this.parts,[d,u]=Gt(t,e);if(this.el=n.createElement(d,s),S.currentNode=this.el.content,e===2){let h=this.el.content.firstChild;h.replaceWith(...h.childNodes)}for(;(i=S.nextNode())!==null&&a.length2||s[0]!==""||s[1]!==""?(this._$AH=Array(s.length-1).fill(new String),this.strings=s):this._$AH=l}_$AI(t,e=this,s,i){let r=this.strings,o=!1;if(r===void 0)t=x(this,t,e,0),o=!O(t)||t!==this._$AH&&t!==g,o&&(this._$AH=t);else{let c=t,a,d;for(t=r[0],a=0;a