|
17 | 17 | )
|
18 | 18 | from weakref import WeakValueDictionary
|
19 | 19 |
|
20 |
| -from htmltools import HTML, Tag, TagAttrValue, TagList, css |
| 20 | +from htmltools import HTML, Tag, TagAttrValue, TagChild, TagList, css |
21 | 21 |
|
22 | 22 | from .. import _utils, reactive
|
23 | 23 | from .._deprecated import warn_deprecated
|
@@ -1244,7 +1244,7 @@ def ui(
|
1244 | 1244 | def chat_ui(
|
1245 | 1245 | id: str,
|
1246 | 1246 | *,
|
1247 |
| - messages: Optional[Sequence[str | ChatMessage]] = None, |
| 1247 | + messages: Optional[Sequence[TagChild | ChatMessage]] = None, |
1248 | 1248 | placeholder: str = "Enter a message...",
|
1249 | 1249 | width: CssUnit = "min(680px, 100%)",
|
1250 | 1250 | height: CssUnit = "auto",
|
@@ -1300,23 +1300,33 @@ def chat_ui(
|
1300 | 1300 | message_tags: list[Tag] = []
|
1301 | 1301 | if messages is None:
|
1302 | 1302 | 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 |
1311 | 1308 | 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.") |
1313 | 1311 |
|
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": |
1315 | 1319 | tag_name = "shiny-user-message"
|
1316 | 1320 | else:
|
1317 | 1321 | tag_name = "shiny-chat-message"
|
1318 | 1322 |
|
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 | + ) |
1320 | 1330 |
|
1321 | 1331 | html_deps = None
|
1322 | 1332 | if isinstance(icon_assistant, (Tag, TagList)):
|
|
0 commit comments