2222
2323from htmltools import (
2424 HTML ,
25- RenderedHTML ,
25+ HTMLDependency ,
2626 Tag ,
2727 TagAttrValue ,
2828 TagChild ,
@@ -595,6 +595,7 @@ async def append_message(
595595 * A dictionary with `content` and `role` keys. The `content` key can contain
596596 content as described above, and the `role` key can be "assistant" or
597597 "user".
598+ * More generally, any type registered with :func:`shinychat.message_content`.
598599
599600 **NOTE:** content may include specially formatted **input suggestion** links
600601 (see note below).
@@ -829,6 +830,7 @@ async def append_message_stream(
829830 * A dictionary with `content` and `role` keys. The `content` key can contain
830831 content as described above, and the `role` key can be "assistant" or
831832 "user".
833+ * More generally, any type registered with :func:`shinychat.message_content_chunk`.
832834
833835 **NOTE:** content may include specially formatted **input suggestion** links
834836 (see note below).
@@ -1588,7 +1590,9 @@ class ChatExpress(Chat):
15881590 def ui (
15891591 self ,
15901592 * ,
1591- messages : Optional [Sequence [TagChild | ChatMessageDict ]] = None ,
1593+ messages : Optional [
1594+ Iterable [str | TagChild | ChatMessageDict | ChatMessage | Any ]
1595+ ] = None ,
15921596 placeholder : str = "Enter a message..." ,
15931597 width : CssUnit = "min(680px, 100%)" ,
15941598 height : CssUnit = "auto" ,
@@ -1690,12 +1694,14 @@ def enable_bookmarking(
16901694def chat_ui (
16911695 id : str ,
16921696 * ,
1693- messages : Optional [Sequence [TagChild | ChatMessageDict ]] = None ,
1697+ messages : Optional [
1698+ Iterable [str | TagChild | ChatMessageDict | ChatMessage | Any ]
1699+ ] = None ,
16941700 placeholder : str = "Enter a message..." ,
16951701 width : CssUnit = "min(680px, 100%)" ,
16961702 height : CssUnit = "auto" ,
16971703 fill : bool = True ,
1698- icon_assistant : HTML | Tag | TagList | None = None ,
1704+ icon_assistant : Optional [ HTML | Tag | TagList ] = None ,
16991705 ** kwargs : TagAttrValue ,
17001706) -> Tag :
17011707 """
@@ -1722,6 +1728,7 @@ def chat_ui(
17221728 interpreted as markdown as long as they're not inside HTML.
17231729 * A dictionary with `content` and `role` keys. The `content` key can contain a
17241730 content as described above, and the `role` key can be "assistant" or "user".
1731+ * More generally, any type registered with :func:`shinychat.message_content`.
17251732
17261733 **NOTE:** content may include specially formatted **input suggestion** links
17271734 (see :method:`~shiny.ui.Chat.append_message` for more info).
@@ -1755,34 +1762,14 @@ def chat_ui(
17551762 if messages is None :
17561763 messages = []
17571764 for x in messages :
1758- role = "assistant"
1759- content : TagChild = None
1760- if not isinstance (x , dict ):
1761- content = x
1762- else :
1763- if "content" not in x :
1764- raise ValueError (
1765- "Each message dictionary must have a 'content' key."
1766- )
1767-
1768- content = x ["content" ]
1769- if "role" in x :
1770- role = x ["role" ]
1771-
1772- # `content` is most likely a string, so avoid overhead in that case
1773- # (it's also important that we *don't escape HTML* here).
1774- if isinstance (content , str ):
1775- ui : RenderedHTML = {"html" : content , "dependencies" : []}
1776- else :
1777- ui = TagList (content ).render ()
1778-
1765+ msg = message_content (x )
17791766 message_tags .append (
17801767 Tag (
17811768 "shiny-chat-message" ,
1782- ui [ "dependencies" ],
1783- content = ui [ "html" ] ,
1769+ * [ HTMLDependency ( ** d ) for d in msg . html_deps ],
1770+ content = msg . content ,
17841771 icon = icon_attr ,
1785- data_role = role ,
1772+ data_role = msg . role ,
17861773 )
17871774 )
17881775
0 commit comments