44
55from htmltools import HTML , Tagifiable
66
7- from ._chat_types import ChatUIMessage
7+ from ._chat_types import ChatMessage
88
99if TYPE_CHECKING :
1010 from anthropic .types import Message as AnthropicMessage
@@ -25,11 +25,11 @@ class GenerateContentResponse:
2525
2626class BaseMessageNormalizer (ABC ):
2727 @abstractmethod
28- def normalize (self , message : Any ) -> ChatUIMessage :
28+ def normalize (self , message : Any ) -> ChatMessage :
2929 pass
3030
3131 @abstractmethod
32- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
32+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
3333 pass
3434
3535 @abstractmethod
@@ -42,13 +42,13 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
4242
4343
4444class StringNormalizer (BaseMessageNormalizer ):
45- def normalize (self , message : Any ) -> ChatUIMessage :
45+ def normalize (self , message : Any ) -> ChatMessage :
4646 x = cast (Optional [str ], message )
47- return ChatUIMessage (content = x or "" , role = "assistant" )
47+ return ChatMessage (content = x or "" , role = "assistant" )
4848
49- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
49+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
5050 x = cast (Optional [str ], chunk )
51- return ChatUIMessage (content = x or "" , role = "assistant" )
51+ return ChatMessage (content = x or "" , role = "assistant" )
5252
5353 def can_normalize (self , message : Any ) -> bool :
5454 return isinstance (message , (str , HTML )) or message is None
@@ -58,17 +58,17 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
5858
5959
6060class DictNormalizer (BaseMessageNormalizer ):
61- def normalize (self , message : Any ) -> ChatUIMessage :
61+ def normalize (self , message : Any ) -> ChatMessage :
6262 x = cast ("dict[str, Any]" , message )
6363 if "content" not in x :
6464 raise ValueError ("Message must have 'content' key" )
65- return ChatUIMessage (content = x ["content" ], role = x .get ("role" , "assistant" ))
65+ return ChatMessage (content = x ["content" ], role = x .get ("role" , "assistant" ))
6666
67- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
67+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
6868 x = cast ("dict[str, Any]" , chunk )
6969 if "content" not in x :
7070 raise ValueError ("Message must have 'content' key" )
71- return ChatUIMessage (content = x ["content" ], role = x .get ("role" , "assistant" ))
71+ return ChatMessage (content = x ["content" ], role = x .get ("role" , "assistant" ))
7272
7373 def can_normalize (self , message : Any ) -> bool :
7474 return isinstance (message , dict )
@@ -78,11 +78,11 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
7878
7979
8080class TagifiableNormalizer (DictNormalizer ):
81- def normalize (self , message : Any ) -> ChatUIMessage :
81+ def normalize (self , message : Any ) -> ChatMessage :
8282 x = cast ("Tagifiable" , message )
8383 return super ().normalize ({"content" : x })
8484
85- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
85+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
8686 x = cast ("Tagifiable" , chunk )
8787 return super ().normalize_chunk ({"content" : x })
8888
@@ -94,23 +94,23 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
9494
9595
9696class LangChainNormalizer (BaseMessageNormalizer ):
97- def normalize (self , message : Any ) -> ChatUIMessage :
97+ def normalize (self , message : Any ) -> ChatMessage :
9898 x = cast ("BaseMessage" , message )
9999 if isinstance (x .content , list ): # type: ignore
100100 raise ValueError (
101101 "The `message.content` provided seems to represent numerous messages. "
102102 "Consider iterating over `message.content` and calling .append_message() on each iteration."
103103 )
104- return ChatUIMessage (content = x .content , role = "assistant" )
104+ return ChatMessage (content = x .content , role = "assistant" )
105105
106- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
106+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
107107 x = cast ("BaseMessageChunk" , chunk )
108108 if isinstance (x .content , list ): # type: ignore
109109 raise ValueError (
110110 "The `message.content` provided seems to represent numerous messages. "
111111 "Consider iterating over `message.content` and calling .append_message() on each iteration."
112112 )
113- return ChatUIMessage (content = x .content , role = "assistant" )
113+ return ChatMessage (content = x .content , role = "assistant" )
114114
115115 def can_normalize (self , message : Any ) -> bool :
116116 try :
@@ -130,11 +130,11 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
130130
131131
132132class OpenAINormalizer (StringNormalizer ):
133- def normalize (self , message : Any ) -> ChatUIMessage :
133+ def normalize (self , message : Any ) -> ChatMessage :
134134 x = cast ("ChatCompletion" , message )
135135 return super ().normalize (x .choices [0 ].message .content )
136136
137- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
137+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
138138 x = cast ("ChatCompletionChunk" , chunk )
139139 return super ().normalize_chunk (x .choices [0 ].delta .content )
140140
@@ -156,17 +156,17 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
156156
157157
158158class AnthropicNormalizer (BaseMessageNormalizer ):
159- def normalize (self , message : Any ) -> ChatUIMessage :
159+ def normalize (self , message : Any ) -> ChatMessage :
160160 x = cast ("AnthropicMessage" , message )
161161 content = x .content [0 ]
162162 if content .type != "text" :
163163 raise ValueError (
164164 f"Anthropic message type { content .type } not supported. "
165165 "Only 'text' type is currently supported"
166166 )
167- return ChatUIMessage (content = content .text , role = "assistant" )
167+ return ChatMessage (content = content .text , role = "assistant" )
168168
169- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
169+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
170170 x = cast ("MessageStreamEvent" , chunk )
171171 content = ""
172172 if x .type == "content_block_delta" :
@@ -177,7 +177,7 @@ def normalize_chunk(self, chunk: Any) -> ChatUIMessage:
177177 )
178178 content = x .delta .text
179179
180- return ChatUIMessage (content = content , role = "assistant" )
180+ return ChatMessage (content = content , role = "assistant" )
181181
182182 def can_normalize (self , message : Any ) -> bool :
183183 try :
@@ -214,13 +214,13 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
214214
215215
216216class GoogleNormalizer (BaseMessageNormalizer ):
217- def normalize (self , message : Any ) -> ChatUIMessage :
217+ def normalize (self , message : Any ) -> ChatMessage :
218218 x = cast ("GenerateContentResponse" , message )
219- return ChatUIMessage (content = x .text , role = "assistant" )
219+ return ChatMessage (content = x .text , role = "assistant" )
220220
221- def normalize_chunk (self , chunk : Any ) -> ChatUIMessage :
221+ def normalize_chunk (self , chunk : Any ) -> ChatMessage :
222222 x = cast ("GenerateContentResponse" , chunk )
223- return ChatUIMessage (content = x .text , role = "assistant" )
223+ return ChatMessage (content = x .text , role = "assistant" )
224224
225225 def can_normalize (self , message : Any ) -> bool :
226226 try :
@@ -238,11 +238,11 @@ def can_normalize_chunk(self, chunk: Any) -> bool:
238238
239239
240240class OllamaNormalizer (DictNormalizer ):
241- def normalize (self , message : Any ) -> ChatUIMessage :
241+ def normalize (self , message : Any ) -> ChatMessage :
242242 x = cast ("dict[str, Any]" , message ["message" ])
243243 return super ().normalize (x )
244244
245- def normalize_chunk (self , chunk : "dict[str, Any]" ) -> ChatUIMessage :
245+ def normalize_chunk (self , chunk : "dict[str, Any]" ) -> ChatMessage :
246246 msg = cast ("dict[str, Any]" , chunk ["message" ])
247247 return super ().normalize_chunk (msg )
248248
@@ -295,7 +295,7 @@ def register(
295295message_normalizer_registry = NormalizerRegistry ()
296296
297297
298- def normalize_message (message : Any ) -> ChatUIMessage :
298+ def normalize_message (message : Any ) -> ChatMessage :
299299 strategies = message_normalizer_registry ._strategies
300300 for strategy in strategies .values ():
301301 if strategy .can_normalize (message ):
@@ -306,7 +306,7 @@ def normalize_message(message: Any) -> ChatUIMessage:
306306 )
307307
308308
309- def normalize_message_chunk (chunk : Any ) -> ChatUIMessage :
309+ def normalize_message_chunk (chunk : Any ) -> ChatMessage :
310310 strategies = message_normalizer_registry ._strategies
311311 for strategy in strategies .values ():
312312 if strategy .can_normalize_chunk (chunk ):
0 commit comments