class MultiToolAgent(ReasoningNode):
def __init__(self, tools_config):
super().__init__()
self.tools = tools_config
async def process_context(self, context):
user_input = context.get_latest_user_transcript_message()
# Analyze what tools are needed
if self.needs_weather_info(user_input):
yield ToolCall(
tool_name="get_weather", tool_args={"location": "NYC"}
)
if self.needs_calendar_info(user_input):
yield ToolCall(
tool_name="get_calendar", tool_args={"date": "today"}
)
if self.needs_calculation(user_input):
yield ToolCall(
tool_name="calculate", tool_args={"expression": "2+2"}
)
# Wait for all tool results, then respond
yield AgentResponse(
content="Let me gather that information..."
)
# Tool registry and dispatcher
class ToolDispatcher:
def __init__(self):
self.tools = {
"get_weather": self.get_weather,
"get_calendar": self.get_calendar,
"calculate": self.calculate,
}
async def execute_tool(self, tool_call):
tool_func = self.tools.get(tool_call.tool_name)
if not tool_func:
return ToolResult(
tool_name=tool_call.tool_name,
tool_args=tool_call.tool_args,
error=f"Unknown tool: {tool_call.tool_name}",
tool_call_id=tool_call.tool_call_id,
)
try:
result = await tool_func(**tool_call.tool_args)
return ToolResult(
tool_name=tool_call.tool_name,
tool_args=tool_call.tool_args,
result=result,
tool_call_id=tool_call.tool_call_id,
)
except Exception as e:
return ToolResult(
tool_name=tool_call.tool_name,
tool_args=tool_call.tool_args,
error=str(e),
tool_call_id=tool_call.tool_call_id,
)
dispatcher = ToolDispatcher()
bridge.on(ToolCall).map(dispatcher.execute_tool).broadcast()