Skip to content

Commit c066da0

Browse files
committed
Actually handle TagChild values properly in chat_ui()
1 parent 287a030 commit c066da0

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

shiny/ui/_chat.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
)
1818
from weakref import WeakValueDictionary
1919

20-
from htmltools import HTML, Tag, TagAttrValue, TagList, css
20+
from htmltools import HTML, Tag, TagAttrValue, TagChild, TagList, css
2121

2222
from .. import _utils, reactive
2323
from .._deprecated import warn_deprecated
@@ -1244,7 +1244,7 @@ def ui(
12441244
def chat_ui(
12451245
id: str,
12461246
*,
1247-
messages: Optional[Sequence[str | ChatMessage]] = None,
1247+
messages: Optional[Sequence[TagChild | ChatMessage]] = None,
12481248
placeholder: str = "Enter a message...",
12491249
width: CssUnit = "min(680px, 100%)",
12501250
height: CssUnit = "auto",
@@ -1300,23 +1300,33 @@ def chat_ui(
13001300
message_tags: list[Tag] = []
13011301
if messages is None:
13021302
messages = []
1303-
for msg in messages:
1304-
if isinstance(msg, str):
1305-
msg = {"content": msg, "role": "assistant"}
1306-
elif isinstance(msg, dict):
1307-
if "content" not in msg:
1308-
raise ValueError("Each message must have a 'content' key.")
1309-
if "role" not in msg:
1310-
raise ValueError("Each message must have a 'role' key.")
1303+
for x in messages:
1304+
role = "assistant"
1305+
content: TagChild = None
1306+
if not isinstance(x, dict):
1307+
content = x
13111308
else:
1312-
raise ValueError("Each message must be a string or a dictionary.")
1309+
if "content" not in x:
1310+
raise ValueError("Each message dictionary must have a 'content' key.")
13131311

1314-
if msg["role"] == "user":
1312+
content = x["content"]
1313+
if "role" in x:
1314+
role = x["role"]
1315+
1316+
ui = TagList(content).render()
1317+
1318+
if role == "user":
13151319
tag_name = "shiny-user-message"
13161320
else:
13171321
tag_name = "shiny-chat-message"
13181322

1319-
message_tags.append(Tag(tag_name, content=msg["content"]))
1323+
message_tags.append(
1324+
Tag(
1325+
tag_name,
1326+
ui["dependencies"],
1327+
content=ui["html"],
1328+
)
1329+
)
13201330

13211331
html_deps = None
13221332
if isinstance(icon_assistant, (Tag, TagList)):

0 commit comments

Comments
 (0)