Shago commited on
Commit
e98ca85
·
verified ·
1 Parent(s): 8d60062

Update agents/agents_nodes.py

Browse files
Files changed (1) hide show
  1. agents/agents_nodes.py +20 -57
agents/agents_nodes.py CHANGED
@@ -86,83 +86,46 @@ F_MAPPING = {
86
 
87
  def format_output(state: AgentState):
88
  try:
89
- # Validate messages list length
90
  if len(state["messages"]) < 2:
91
- return {"output": {"error": "Insufficient messages to process"}}
92
-
93
- # Safely access list indices
94
  last_msg = state["messages"][-1]
95
  second_last_msg = state["messages"][-2]
96
 
97
- # Verify message types
98
  if not isinstance(last_msg, ToolMessage):
99
- return {"output": {"error": "Last message must be ToolMessage"}}
100
  if not isinstance(second_last_msg, AIMessage):
101
- return {"output": {"error": "Second-last message must be AIMessage"}}
102
 
103
- # Parse tool result
104
  try:
105
  tool_result = json.loads(last_msg.content)
106
  except json.JSONDecodeError:
107
  return {"output": {"error": "ToolMessage content is invalid JSON"}}
108
 
109
- # Validate tool call structure
110
- if not second_last_msg.tool_calls:
111
- return {"output": {"error": "AIMessage has no tool calls"}}
112
 
113
- first_tool_call = second_last_msg.tool_calls
114
- args = first_tool_call.get("args", {})
 
115
 
116
- # Process factor type
117
- factor_type = args.get("F")
118
  if not factor_type or factor_type not in F_MAPPING:
119
- return {"output": {"error": f"Invalid factor type: {factor_type}"}}
120
 
121
  result_key = F_MAPPING[factor_type]
122
  if result_key not in tool_result:
123
- return {"output": {"error": f"Result key {result_key} not found"}}
124
 
 
125
  value = tool_result[result_key]
126
  return {"output": {result_key: round(float(value), 2)}}
127
 
128
  except Exception as e:
129
- return {"output": {"error": f"Formatting failed: {str(e)}"}}
130
-
131
-
132
-
133
- # def format_output(state: AgentState):
134
- # try:
135
- # # The last message should be the ToolMessage (from the tool node)
136
- # if not state["messages"] or not isinstance(state["messages"][-1], ToolMessage):
137
- # return {"output": {"error": "No tool result found in the last message"}}
138
-
139
- # tool_message = state["messages"][-1]
140
- # # Parse the content of the tool message as JSON
141
- # tool_result = json.loads(tool_message.content)
142
-
143
- # # The second last message should be the AIMessage with the tool call
144
- # if len(state["messages"]) < 2 or not isinstance(state["messages"][-2], AIMessage):
145
- # return {"output": {"error": "No AI message (with tool call) found before the tool message"}}
146
-
147
- # ai_message = state["messages"][-2]
148
- # if not ai_message.tool_calls:
149
- # return {"output": {"error": "The AI message does not contain tool calls"}}
150
-
151
- # # We take the first tool call (since we forced one tool)
152
- # tool_call = ai_message.tool_calls
153
- # args = tool_call["args"]
154
-
155
- # # Get the factor type from the args
156
- # factor_type = args["F"]
157
- # if factor_type not in F_MAPPING:
158
- # return {"output": {"error": f"Unrecognized factor type: {factor_type}"}}
159
-
160
- # result_key = F_MAPPING[factor_type]
161
- # if result_key not in tool_result:
162
- # return {"output": {"error": f"Expected key {result_key} not found in tool result"}}
163
-
164
- # value = tool_result[result_key]
165
- # return {"output": {result_key: round(float(value), 2)}}
166
-
167
- # except (KeyError, TypeError, json.JSONDecodeError, IndexError) as e:
168
- # return {"output": {"error": f"Result formatting failed: {str(e)}"}}
 
86
 
87
  def format_output(state: AgentState):
88
  try:
89
+ # 1. Validate minimum message count
90
  if len(state["messages"]) < 2:
91
+ return {"output": {"error": f"Only {len(state['messages'])} messages, need 2 for processing"}}
92
+
93
+ # 2. Safe element access
94
  last_msg = state["messages"][-1]
95
  second_last_msg = state["messages"][-2]
96
 
97
+ # 3. Verify message types
98
  if not isinstance(last_msg, ToolMessage):
99
+ return {"output": {"error": f"Last message is {type(last_msg).__name__}, expected ToolMessage"}}
100
  if not isinstance(second_last_msg, AIMessage):
101
+ return {"output": {"error": f"Second-last message is {type(second_last_msg).__name__}, expected AIMessage"}}
102
 
103
+ # 4. Parse tool result
104
  try:
105
  tool_result = json.loads(last_msg.content)
106
  except json.JSONDecodeError:
107
  return {"output": {"error": "ToolMessage content is invalid JSON"}}
108
 
109
+ # 5. Validate tool call structure
110
+ if not hasattr(second_last_msg, 'tool_calls') or not second_last_msg.tool_calls:
111
+ return {"output": {"error": "AIMessage contains no tool calls"}}
112
 
113
+ # 6. Handle single vs multiple tool calls
114
+ tool_call = second_last_msg.tool_calls if isinstance(second_last_msg.tool_calls, list) else second_last_msg.tool_calls
115
+ args = tool_call.get("args", {})
116
 
117
+ # 7. Process factor type
118
+ factor_type = args.get("F", "").strip()
119
  if not factor_type or factor_type not in F_MAPPING:
120
+ return {"output": {"error": f"Invalid/missing factor type: '{factor_type}'"}}
121
 
122
  result_key = F_MAPPING[factor_type]
123
  if result_key not in tool_result:
124
+ return {"output": {"error": f"Result key '{result_key}' not found in tool output"}}
125
 
126
+ # 8. Format final output
127
  value = tool_result[result_key]
128
  return {"output": {result_key: round(float(value), 2)}}
129
 
130
  except Exception as e:
131
+ return {"output": {"error": f"Unhandled exception: {str(e)}"}}