@@ -185,6 +185,17 @@ struct ChatArgs<'o, W> {
185185 terminal_width_provider : fn ( ) -> Option < usize > ,
186186}
187187
188+ /// Enum used to denote the origin of a tool use event
189+ enum ToolUseStatus {
190+ /// Variant denotes that the tool use event associated with chat context is a direct result of
191+ /// a user request
192+ Idle ,
193+ /// Variant denotes that the tool use event associated with the chat context is a result of a
194+ /// retry for one or more previously attempted tool use. The tuple is the utterance id
195+ /// associated with the original user request that necessitated the tool use
196+ RetryInProgress ( String ) ,
197+ }
198+
188199/// State required for a chat session.
189200struct ChatContext < ' o , W > {
190201 /// The [Write] destination for printing conversation text.
@@ -209,6 +220,9 @@ struct ChatContext<'o, W> {
209220 // tool_use_telemetry_events: Vec<ToolUseEventBuilder>,
210221 tool_use_telemetry_events : HashMap < String , ToolUseEventBuilder > ,
211222
223+ /// State used to keep track of tool use relation
224+ tool_use_status : ToolUseStatus ,
225+
212226 /// Whether or not an unexpected end of chat stream was encountered while consuming the model
213227 /// response's tool use data. Pair of (tool_use_id, name) for the tool that was being received.
214228 encountered_tool_use_eos : Option < ( String , String ) > ,
@@ -232,6 +246,7 @@ where
232246 conversation_state : ConversationState :: new ( args. tool_config ) ,
233247 tool_use_recursions : 0 ,
234248 tool_use_telemetry_events : HashMap :: new ( ) ,
249+ tool_use_status : ToolUseStatus :: Idle ,
235250 encountered_tool_use_eos : None ,
236251 }
237252 }
@@ -589,6 +604,13 @@ Hi, I'm <g>Amazon Q</g>. Ask me anything.
589604 }
590605 self . conversation_state . add_tool_results ( tool_results) ;
591606 self . send_tool_use_telemetry ( ) . await ;
607+ if let ToolUseStatus :: Idle = self . tool_use_status {
608+ self . tool_use_status = ToolUseStatus :: RetryInProgress (
609+ self . conversation_state
610+ . message_id ( )
611+ . map_or ( "No utterance id found" . to_string ( ) , |v| v. to_string ( ) ) ,
612+ ) ;
613+ }
592614 return Ok ( Some (
593615 self . client
594616 . send_message ( self . conversation_state . as_sendable_conversation_state ( ) )
@@ -757,6 +779,13 @@ Hi, I'm <g>Amazon Q</g>. Ask me anything.
757779 ) ) ] ,
758780 status : ToolResultStatus :: Error ,
759781 } ) ;
782+ if let ToolUseStatus :: Idle = self . tool_use_status {
783+ self . tool_use_status = ToolUseStatus :: RetryInProgress (
784+ self . conversation_state
785+ . message_id ( )
786+ . map_or ( "No utterance id found" . to_string ( ) , |v| v. to_string ( ) ) ,
787+ ) ;
788+ }
760789 } ,
761790 }
762791 }
@@ -771,6 +800,7 @@ Hi, I'm <g>Amazon Q</g>. Ask me anything.
771800 } ,
772801 // New user prompt.
773802 _ => {
803+ self . tool_use_status = ToolUseStatus :: Idle ;
774804 self . tool_use_recursions = 0 ;
775805
776806 if is_initial_input {
@@ -834,7 +864,12 @@ Hi, I'm <g>Amazon Q</g>. Ask me anything.
834864 }
835865
836866 async fn send_tool_use_telemetry ( & mut self ) {
837- for ( _, event) in self . tool_use_telemetry_events . drain ( ) {
867+ for ( _, mut event) in self . tool_use_telemetry_events . drain ( ) {
868+ event. user_input_id = match self . tool_use_status {
869+ ToolUseStatus :: Idle => self . conversation_state . message_id ( ) ,
870+ ToolUseStatus :: RetryInProgress ( ref id) => Some ( id. as_str ( ) ) ,
871+ }
872+ . map ( |v| v. to_string ( ) ) ;
838873 let event: fig_telemetry:: EventType = event. into ( ) ;
839874 let app_event = fig_telemetry:: AppTelemetryEvent :: new ( event) . await ;
840875 fig_telemetry:: dispatch_or_send_event ( app_event) . await ;
0 commit comments