Spaces:
Running
Running
| """ | |
| app/nodes/tool_executor.py — CHECKPOINT 1: Tool execution | |
| Fix: Format tool results as natural language instead of raw key:value dump. | |
| """ | |
| from app.state import AgentState | |
| from app.tools import TOOL_MAP | |
| def tool_executor_node(state: AgentState) -> AgentState: | |
| results = [] | |
| for tc in state.get("tool_calls", []): | |
| tool_name = tc["name"] | |
| tool_args = tc.get("args", {}) | |
| if tool_name in TOOL_MAP: | |
| result = TOOL_MAP[tool_name].invoke(tool_args) | |
| print(f"[TOOL] {tool_name}({tool_args}) → {result}") | |
| results.append({"tool": tool_name, "result": result}) | |
| else: | |
| results.append({"tool": tool_name, "result": "Tool not found."}) | |
| # Format as readable natural language instead of raw "tool: result" dump | |
| if results: | |
| if len(results) == 1: | |
| response = str(results[0]["result"]) | |
| else: | |
| lines = [f"- **{r['tool']}**: {r['result']}" for r in results] | |
| response = "\n".join(lines) | |
| log = state.get("node_log", []) + [f"tool_executor ({', '.join(r['tool'] for r in results)})"] | |
| return {**state, "tool_results": results, "response": response, "node_log": log} | |
| return state |