extract parser.py and tool_runner.py from agent code
This commit is contained in:
@@ -11,6 +11,7 @@ from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from agents.shared.mcp_client import MCPMultiClient
|
||||
from agents.shared.tool_runner import build_tool_caller
|
||||
|
||||
ALL_HUBS = ["ORD", "EWR", "IAH", "SFO", "DEN"]
|
||||
|
||||
@@ -43,40 +44,7 @@ async def run_handover(
|
||||
|
||||
await emit("node_enter", node="gather_all")
|
||||
|
||||
async def _call(server, tool, args, is_live=False, timeout=15.0):
|
||||
t = time.time()
|
||||
ctx = lf.start_as_current_observation(
|
||||
name=tool, as_type="tool", input=args,
|
||||
metadata={"server": server, "is_live": is_live},
|
||||
) if lf else None
|
||||
if ctx:
|
||||
ctx.__enter__()
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
mcp.call_tool(server, tool, args), timeout=timeout,
|
||||
)
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if ctx:
|
||||
lf.update_current_span(output=result, metadata={"latency_ms": lat})
|
||||
ctx.__exit__(None, None, None)
|
||||
await emit("tool_call_end", tool=tool, latency_ms=lat, is_live=is_live)
|
||||
return result
|
||||
except asyncio.TimeoutError:
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if ctx:
|
||||
lf.update_current_span(output={"error": "timeout"}, level="ERROR")
|
||||
ctx.__exit__(None, None, None)
|
||||
await emit("tool_call_error", tool=tool, error="timeout", latency_ms=lat)
|
||||
errors.append(f"{tool}: timeout after {timeout}s")
|
||||
return None
|
||||
except Exception as e:
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if ctx:
|
||||
lf.update_current_span(output={"error": str(e)}, level="ERROR")
|
||||
ctx.__exit__(None, None, None)
|
||||
await emit("tool_call_error", tool=tool, error=str(e), latency_ms=lat)
|
||||
errors.append(f"{tool}: {e}")
|
||||
return None
|
||||
_call = build_tool_caller(mcp, emit=emit, errors=errors, lf=lf)
|
||||
|
||||
# Per-hub calls
|
||||
hub_tasks = {}
|
||||
|
||||
Reference in New Issue
Block a user