@@ -21,51 +21,46 @@ def extract_agent_tools_used_from_messages(agent_messages):
2121 for i , message in enumerate (agent_messages ):
2222 if message .get ("role" ) == "assistant" :
2323 message_info = message .get ("content" )
24- if len (message_info ) > 0 :
25- tools = []
26- for message in message_info :
27- if "toolUse" in message :
28- tools .append (message .get ("toolUse" ))
24+ if message_info :
25+ # Collect tool uses from this message
26+ tools = [cb .get ("toolUse" ) for cb in message_info if cb .get ("toolUse" )]
27+ if not tools :
28+ continue
29+
30+ # Build lookup dict of tool results from subsequent user messages
31+ tool_ids_needed = {tool .get ("toolUseId" ) for tool in tools }
32+ tool_results_by_id : dict [str , dict ] = {}
33+ for next_message in agent_messages [i + 1 :]:
34+ if next_message .get ("role" ) == "user" :
35+ for content_block in next_message .get ("content" ) or []:
36+ tool_result_dict = content_block .get ("toolResult" )
37+ if tool_result_dict :
38+ tool_id = tool_result_dict .get ("toolUseId" )
39+ if tool_id in tool_ids_needed and tool_id not in tool_results_by_id :
40+ tool_results_by_id [tool_id ] = tool_result_dict
41+ if len (tool_results_by_id ) == len (tool_ids_needed ):
42+ break
2943
3044 for tool in tools :
31- if tool :
32- tool_name = tool .get ("name" )
33- tool_input = tool .get ("input" )
34- tool_id = tool .get ("toolUseId" )
35- # get the tool result from the next message
36- tool_result = None
37- is_error = False
38- next_message_i = i + 1
39- while next_message_i < len (agent_messages ):
40- next_message = agent_messages [next_message_i ]
41- next_message_i += 1
42-
43- if next_message .get ("role" ) == "user" :
44- content = next_message .get ("content" )
45- if content :
46- # Find toolResult in content blocks - may not be at index 0
47- tool_result_dict = None
48- for content_block in content :
49- if "toolResult" in content_block :
50- tool_result_dict = content_block .get ("toolResult" )
51- break
52-
53- if tool_result_dict and tool_result_dict .get ("toolUseId" ) == tool_id :
54- tool_result_content = tool_result_dict .get ("content" , [])
55- # Find first text in tool result content - may not be at index 0
56- tool_result = None
57- if tool_result_content :
58- for result_item in tool_result_content :
59- if isinstance (result_item , dict ) and "text" in result_item :
60- tool_result = result_item .get ("text" )
61- break
62- is_error = tool_result_dict .get ("status" ) == "error"
63- break
64-
65- tools_used .append (
66- {"name" : tool_name , "input" : tool_input , "tool_result" : tool_result , "is_error" : is_error }
67- )
68- tool = message .get ("toolUse" )
45+ tool_name = tool .get ("name" )
46+ tool_input = tool .get ("input" )
47+ tool_id = tool .get ("toolUseId" )
48+ tool_result = None
49+ is_error = False
50+
51+ # Find the matching tool result block
52+ tool_result_dict = tool_results_by_id .get (tool_id )
53+ if tool_result_dict :
54+ tool_result_content = tool_result_dict .get ("content" , [])
55+ for result_item in tool_result_content :
56+ if isinstance (result_item , dict ) and "text" in result_item :
57+ tool_result = result_item .get ("text" )
58+ break
59+ is_error = tool_result_dict .get ("status" ) == "error"
60+
61+ tools_used .append (
62+ {"name" : tool_name , "input" : tool_input , "tool_result" : tool_result , "is_error" : is_error }
63+ )
6964 return tools_used
7065
7166
0 commit comments