11import json
22import logging
3- import uuid
43from datetime import datetime , timezone
54from typing import Any , cast
65
1716 UiPathConversationContentPartEndEvent ,
1817 UiPathConversationContentPartEvent ,
1918 UiPathConversationContentPartStartEvent ,
20- UiPathConversationEvent ,
21- UiPathConversationExchangeEvent ,
2219 UiPathConversationMessage ,
2320 UiPathConversationMessageEndEvent ,
2421 UiPathConversationMessageEvent ,
3330
3431
3532class UiPathChatMessagesMapper :
36- """Stateful mapper that converts LangChain messages to UiPath conversation events.
33+ """Stateful mapper that converts LangChain messages to UiPath message events.
3734
3835 Maintains state across multiple message conversions to properly track:
3936 - The AI message ID associated with each tool call for proper correlation with ToolMessage
@@ -44,21 +41,6 @@ def __init__(self):
4441 self .tool_call_to_ai_message : dict [str , str ] = {}
4542 self .seen_message_ids : set [str ] = set ()
4643
47- def _wrap_in_conversation_event (
48- self ,
49- msg_event : UiPathConversationMessageEvent ,
50- exchange_id : str | None = None ,
51- conversation_id : str | None = None ,
52- ) -> UiPathConversationEvent :
53- """Helper to wrap a message event into a conversation-level event."""
54- return UiPathConversationEvent (
55- conversation_id = conversation_id or str (uuid .uuid4 ()),
56- exchange = UiPathConversationExchangeEvent (
57- exchange_id = exchange_id or str (uuid .uuid4 ()),
58- message = msg_event ,
59- ),
60- )
61-
6244 def _extract_text (self , content : Any ) -> str :
6345 """Normalize LangGraph message.content to plain text."""
6446 if isinstance (content , str ):
@@ -146,18 +128,14 @@ def _map_messages_internal(
146128 def map_event (
147129 self ,
148130 message : BaseMessage ,
149- exchange_id : str | None = None ,
150- conversation_id : str | None = None ,
151- ) -> UiPathConversationEvent | None :
152- """Convert LangGraph BaseMessage (chunk or full) into a UiPathConversationEvent.
131+ ) -> UiPathConversationMessageEvent | None :
132+ """Convert LangGraph BaseMessage (chunk or full) into a UiPathConversationMessageEvent.
153133
154134 Args:
155135 message: The LangChain message to convert
156- exchange_id: Optional exchange ID for the conversation
157- conversation_id: Optional conversation ID
158136
159137 Returns:
160- A UiPathConversationEvent if the message should be emitted, None otherwise
138+ A UiPathConversationMessageEvent if the message should be emitted, None otherwise.
161139 """
162140 # Format timestamp as ISO 8601 UTC with milliseconds: 2025-01-04T10:30:00.123Z
163141 timestamp = (
@@ -182,9 +160,7 @@ def map_event(
182160 content_part_id = f"chunk-{ message .id } -0" ,
183161 end = UiPathConversationContentPartEndEvent (),
184162 )
185- return self ._wrap_in_conversation_event (
186- msg_event , exchange_id , conversation_id
187- )
163+ return msg_event
188164
189165 # For every new message_id, start a new message
190166 if message .id not in self .seen_message_ids :
@@ -252,9 +228,7 @@ def map_event(
252228 or msg_event .tool_call
253229 or msg_event .end
254230 ):
255- return self ._wrap_in_conversation_event (
256- msg_event , exchange_id , conversation_id
257- )
231+ return msg_event
258232
259233 return None
260234
@@ -286,42 +260,34 @@ def map_event(
286260 # Keep as string if not valid JSON
287261 pass
288262
289- return self ._wrap_in_conversation_event (
290- UiPathConversationMessageEvent (
291- message_id = result_message_id ,
292- tool_call = UiPathConversationToolCallEvent (
293- tool_call_id = message .tool_call_id ,
294- start = UiPathConversationToolCallStartEvent (
295- tool_name = message .name ,
296- arguments = None ,
297- timestamp = timestamp ,
298- ),
299- end = UiPathConversationToolCallEndEvent (
300- timestamp = timestamp ,
301- result = UiPathInlineValue (inline = content_value ),
302- ),
263+ return UiPathConversationMessageEvent (
264+ message_id = result_message_id ,
265+ tool_call = UiPathConversationToolCallEvent (
266+ tool_call_id = message .tool_call_id ,
267+ start = UiPathConversationToolCallStartEvent (
268+ tool_name = message .name ,
269+ arguments = None ,
270+ timestamp = timestamp ,
271+ ),
272+ end = UiPathConversationToolCallEndEvent (
273+ timestamp = timestamp ,
274+ result = UiPathInlineValue (inline = content_value ),
303275 ),
304276 ),
305- exchange_id ,
306- conversation_id ,
307277 )
308278
309279 # --- Fallback for other BaseMessage types ---
310280 text_content = self ._extract_text (message .content )
311- return self ._wrap_in_conversation_event (
312- UiPathConversationMessageEvent (
313- message_id = message .id ,
314- start = UiPathConversationMessageStartEvent (
315- role = "assistant" , timestamp = timestamp
316- ),
317- content_part = UiPathConversationContentPartEvent (
318- content_part_id = f"cp-{ message .id } " ,
319- chunk = UiPathConversationContentPartChunkEvent (data = text_content ),
320- ),
321- end = UiPathConversationMessageEndEvent (),
281+ return UiPathConversationMessageEvent (
282+ message_id = message .id ,
283+ start = UiPathConversationMessageStartEvent (
284+ role = "assistant" , timestamp = timestamp
285+ ),
286+ content_part = UiPathConversationContentPartEvent (
287+ content_part_id = f"cp-{ message .id } " ,
288+ chunk = UiPathConversationContentPartChunkEvent (data = text_content ),
322289 ),
323- exchange_id ,
324- conversation_id ,
290+ end = UiPathConversationMessageEndEvent (),
325291 )
326292
327293
0 commit comments