11"""Chat runtime implementation."""
22
33import logging
4+ from datetime import datetime , timezone
45from typing import Any , AsyncGenerator , cast
56
6- from uipath .core .triggers import UiPathResumeTriggerType
7+ from uipath .core .chat import (
8+ UiPathConversationMessageEvent ,
9+ UiPathConversationToolCallEvent ,
10+ UiPathConversationToolCallStartEvent ,
11+ )
12+ from uipath .core .triggers import UiPathResumeTrigger , UiPathResumeTriggerType
713
814from uipath .runtime .base import (
915 UiPathExecuteOptions ,
@@ -41,6 +47,7 @@ def __init__(
4147 super ().__init__ ()
4248 self .delegate = delegate
4349 self .chat_bridge = chat_bridge
50+ self ._current_message_id : str | None = None
4451
4552 async def execute (
4653 self ,
@@ -80,6 +87,7 @@ async def stream(
8087 ):
8188 if isinstance (event , UiPathRuntimeMessageEvent ):
8289 if event .payload :
90+ self ._current_message_id = event .payload .message_id
8391 await self .chat_bridge .emit_message_event (event .payload )
8492
8593 if isinstance (event , UiPathRuntimeResult ):
@@ -103,6 +111,12 @@ async def stream(
103111
104112 resume_data = await self .chat_bridge .wait_for_resume ()
105113
114+ await (
115+ self ._emit_start_tool_call_on_confirmation_approval (
116+ trigger , resume_data
117+ )
118+ )
119+
106120 assert trigger .interrupt_id is not None , (
107121 "Trigger interrupt_id cannot be None"
108122 )
@@ -122,6 +136,36 @@ async def stream(
122136 else :
123137 yield event
124138
139+ async def _emit_start_tool_call_on_confirmation_approval (
140+ self , trigger : UiPathResumeTrigger , resume_data : dict [str , Any ]
141+ ) -> None :
142+ """Emit a startToolCall event when a HITL tool call confirmation is approved."""
143+ if self ._current_message_id is None or trigger .api_resume is None :
144+ return
145+
146+ request = trigger .api_resume .request or {}
147+ value = resume_data .get ("value" ) or {}
148+ tool_input = value .get ("input" )
149+
150+ tool_call_start_event = UiPathConversationMessageEvent (
151+ message_id = self ._current_message_id ,
152+ tool_call = UiPathConversationToolCallEvent (
153+ tool_call_id = request .get ("toolCallId" , "" ),
154+ start = UiPathConversationToolCallStartEvent (
155+ tool_name = request .get ("toolName" , "" ),
156+ timestamp = datetime .now (timezone .utc )
157+ .isoformat (timespec = "milliseconds" )
158+ .replace ("+00:00" , "Z" ),
159+ input = tool_input ,
160+ ),
161+ ),
162+ )
163+
164+ try :
165+ await self .chat_bridge .emit_message_event (tool_call_start_event )
166+ except Exception as e :
167+ logger .warning (f"Error emitting startToolCall on approval: { e } " )
168+
125169 async def get_schema (self ) -> UiPathRuntimeSchema :
126170 """Get schema from the delegate runtime."""
127171 return await self .delegate .get_schema ()
0 commit comments