@@ -38,6 +38,7 @@ def __init__(self, name: str, system_prompt: str, description: str, model: LLMMo
3838 self .max_steps = max_steps # max ReAct steps, None means no limit
3939 self .conversation_history = []
4040 self .action_history = []
41+ self .captured_actions = [] # Initialize captured actions list
4142 self .ext_data = ""
4243 self .output_mode = output_mode
4344 if tools :
@@ -140,10 +141,6 @@ def step(self):
140141 final_answer = None
141142 current_step = 0
142143
143- # Initialize captured actions list (if it doesn't exist)
144- if not hasattr (self , 'captured_actions' ):
145- self .captured_actions = []
146-
147144 # Initialize final answer (if it doesn't exist)
148145 if not hasattr (self , 'final_answer' ):
149146 self .final_answer = ""
@@ -277,7 +274,25 @@ def step(self):
277274 tool : BaseTool = self ._find_tool (parsed ["action" ])
278275 observation = ""
279276 if tool :
277+ # Record start time for execution timing
278+ start_time = time .time ()
279+
280280 tool_result = tool .execute_tool (parsed .get ("action_input" , {}))
281+
282+ # Calculate execution time
283+ execution_time = time .time () - start_time
284+
285+ # Capture tool use for tracking
286+ self .capture_tool_use (
287+ tool_name = parsed ["action" ],
288+ input_params = parsed .get ("action_input" , {}),
289+ thought = parsed ['thought' ],
290+ output = tool_result .result ,
291+ status = tool_result .status ,
292+ error_message = tool_result .result if tool_result .status == "error" else None ,
293+ execution_time = execution_time ,
294+ )
295+
281296 # Update conversation history
282297 parsed ["Observation" ] = {
283298 "status" : tool_result .status ,
@@ -326,9 +341,25 @@ def _execute_post_process_tools(self):
326341 # Set tool context
327342 tool .context = self
328343
344+ # Record start time for execution timing
345+ start_time = time .time ()
346+
329347 # Execute tool (with empty parameters, tool will extract needed info from context)
330348 result = tool .execute ({})
331349
350+ # Calculate execution time
351+ execution_time = time .time () - start_time
352+
353+ # Capture tool use for tracking
354+ self .capture_tool_use (
355+ tool_name = tool .name ,
356+ input_params = {}, # Post-process tools typically don't take parameters
357+ output = result .result ,
358+ status = result .status ,
359+ error_message = str (result .result ) if result .status == "error" else None ,
360+ execution_time = execution_time
361+ )
362+
332363 # Log result
333364 if result .status == "success" :
334365 # Print tool execution result in the desired format
@@ -424,10 +455,12 @@ def _fetch_agents_outputs(self) -> str:
424455 f"member name: { agent_output .agent_name } \n output content: { agent_output .output } \n \n " )
425456 return "\n " .join (agent_outputs_list )
426457
427- def capture_tool_use (self , tool_name , input_params , output , status , error_message = None , execution_time = 0.0 ):
458+ def capture_tool_use (self , tool_name , input_params , output , status , thought = None , error_message = None ,
459+ execution_time = 0.0 ):
428460 """
429461 Capture a tool use action.
430462
463+ :param thought: thought content
431464 :param tool_name: Name of the tool used
432465 :param input_params: Parameters passed to the tool
433466 :param output: Output from the tool
@@ -448,52 +481,10 @@ def capture_tool_use(self, tool_name, input_params, output, status, error_messag
448481 agent_id = self .id if hasattr (self , 'id' ) else str (id (self )),
449482 agent_name = self .name ,
450483 action_type = AgentActionType .TOOL_USE ,
451- tool_result = tool_result
484+ tool_result = tool_result ,
485+ thought = thought
452486 )
453487
454- if not hasattr (self , 'captured_actions' ):
455- self .captured_actions = []
456-
457- self .captured_actions .append (action )
458-
459- return action
460-
461- def capture_thinking (self , thought_content ):
462- """
463- Capture a thinking action.
464-
465- :param thought_content: Content of the thought
466- """
467- action = AgentAction (
468- agent_id = self .id if hasattr (self , 'id' ) else str (id (self )),
469- agent_name = self .name ,
470- action_type = AgentActionType .THINKING ,
471- content = thought_content
472- )
473-
474- if not hasattr (self , 'captured_actions' ):
475- self .captured_actions = []
476-
477- self .captured_actions .append (action )
478-
479- return action
480-
481- def capture_final_answer (self , answer_content ):
482- """
483- Capture a final answer action.
484-
485- :param answer_content: Content of the final answer
486- """
487- action = AgentAction (
488- agent_id = self .id if hasattr (self , 'id' ) else str (id (self )),
489- agent_name = self .name ,
490- action_type = AgentActionType .FINAL_ANSWER ,
491- content = answer_content
492- )
493-
494- if not hasattr (self , 'captured_actions' ):
495- self .captured_actions = []
496-
497488 self .captured_actions .append (action )
498489
499490 return action
0 commit comments