Execute the selected tools.
(self, state: AgentState)
| 87 | } |
| 88 | |
| 89 | async def _call_tools(self, state: AgentState) -> AgentState: |
| 90 | """Execute the selected tools.""" |
| 91 | last_message = state["messages"][-1] |
| 92 | |
| 93 | if not last_message.tool_calls: |
| 94 | return state |
| 95 | |
| 96 | # Execute each tool call |
| 97 | for tool_call in last_message.tool_calls: |
| 98 | tool_name = tool_call["name"] |
| 99 | tool_args = tool_call["args"] |
| 100 | |
| 101 | # Find and execute the tool |
| 102 | tool = next((t for t in SCRAPING_TOOLS if t.name == tool_name), None) |
| 103 | if tool: |
| 104 | if asyncio.iscoroutinefunction(tool.func): |
| 105 | result = await tool.func(**tool_args) |
| 106 | else: |
| 107 | result = tool.func(**tool_args) |
| 108 | |
| 109 | # Update state based on tool |
| 110 | if tool_name == "add_url" and result["success"]: |
| 111 | state["urls"].append(tool_args["url"]) |
| 112 | elif tool_name == "remove_url" and result["success"]: |
| 113 | state["urls"].remove(tool_args["url"]) |
| 114 | elif tool_name == "define_schema" and result["success"]: |
| 115 | state["schema"] = result["schema"] |
| 116 | elif tool_name == "generate_code" and result["success"]: |
| 117 | state["generated_code"] = result["code"] |
| 118 | elif tool_name == "clear_pipeline" and result["success"]: |
| 119 | state["urls"] = [] |
| 120 | state["schema"] = {} |
| 121 | state["generated_code"] = "" |
| 122 | |
| 123 | # Add tool response as message |
| 124 | tool_message = AIMessage( |
| 125 | content=json.dumps(result), |
| 126 | name=tool_name, |
| 127 | additional_kwargs={"tool_call_id": tool_call["id"]} |
| 128 | ) |
| 129 | state["messages"].append(tool_message) |
| 130 | |
| 131 | return state |
| 132 | |
| 133 | async def _execute_scraping(self, state: AgentState) -> AgentState: |
| 134 | """Execute the actual scraping with ScrapeGraphAI.""" |
nothing calls this directly
no outgoing calls
no test coverage detected