Skip to content

Commit 7688287

Browse files
committed
Fix typing issues
1 parent d0d7719 commit 7688287

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

shiny/ui/_chat.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ async def _append_message(
540540
self._current_stream_message = ""
541541

542542
msg = await self._transform_message(
543-
msg, chunk=chunk, chunk_content=chunk_content
543+
msg, chunk=chunk, chunk_content=str(chunk_content)
544544
)
545545
if msg is None:
546546
return
@@ -776,11 +776,11 @@ async def _transform_message(
776776
key = res["transform_key"]
777777

778778
if message["role"] == "user" and self._transform_user is not None:
779-
content = await self._transform_user(message["content"])
779+
content = await self._transform_user(str(message["content"]))
780780

781781
elif message["role"] == "assistant" and self._transform_assistant is not None:
782782
content = await self._transform_assistant(
783-
message["content"],
783+
str(message["content"]),
784784
chunk_content or "",
785785
chunk == "end" or chunk is False,
786786
)
@@ -790,7 +790,7 @@ async def _transform_message(
790790
if content is None:
791791
return None
792792

793-
res[key] = content
793+
res[key] = content # type: ignore
794794

795795
return res
796796

@@ -950,7 +950,7 @@ def user_input(self, transform: bool = False) -> str | None:
950950
if msg is None:
951951
return None
952952
key = "content_server" if transform else "content_client"
953-
return msg[key]
953+
return str(msg[key])
954954

955955
def _user_input(self) -> str:
956956
id = self.user_input_id
@@ -1097,7 +1097,7 @@ def as_transformed_message(message: ChatMessage) -> TransformedMessage:
10971097

10981098
return TransformedMessage(
10991099
content_client=message["content"],
1100-
content_server=message["content"],
1100+
content_server=str(message["content"]),
11011101
role=message["role"],
11021102
transform_key=transform_key,
11031103
pre_transform_key=pre_transform_key,

shiny/ui/_chat_types.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
from typing import Literal, TypedDict
44

5+
from htmltools import HTML
6+
57
Role = Literal["assistant", "user", "system"]
68

79

810
# TODO: content should probably be [{"type": "text", "content": "..."}, {"type": "image", ...}]
911
# in order to support multiple content types...
1012
class ChatMessage(TypedDict):
11-
content: str
13+
content: str | HTML
1214
role: Role
1315

1416

1517
# A message once transformed have been applied
1618
class TransformedMessage(TypedDict):
17-
content_client: str
19+
content_client: str | HTML
1820
content_server: str
1921
role: Role
2022
transform_key: Literal["content_client", "content_server"]

tests/playwright/shiny/components/chat/transform_assistant/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
# TODO: test with append_message_stream() as well
1414
@chat.transform_assistant_response
15-
def transform(content: str) -> str:
15+
def transform(content: str) -> str | ui.HTML:
1616
if content == "return HTML":
1717
return ui.HTML(f"<b>Transformed response</b>: {content}")
1818
else:

0 commit comments

Comments
 (0)