mistle/agent_runtime.py

37 lines
949 B
Python

from __future__ import annotations
import sys
from threading import Event
from typing import Callable
from agents import Agent
from tools import Tool
ToolBuilder = Callable[[str], Tool]
ToolRunner = Callable[[Tool], None]
def run_agent(
agent: Agent,
*,
build_tool: ToolBuilder,
run_tool: ToolRunner,
stop_event: Event,
) -> None:
"""Execute *agent* by wiring it to the tool runtime."""
def invoke_tool(spec: str) -> bool:
name = spec.strip()
if not name:
print("[Agent] Ignoring empty tool spec", file=sys.stderr)
return False
try:
tool = build_tool(name)
except RuntimeError as exc:
print(f"[Agent] Failed to load tool '{name}': {exc}", file=sys.stderr)
return False
print(f"[Agent] Running tool '{name}'")
run_tool(tool)
return True
agent.run(invoke_tool=invoke_tool, stop_event=stop_event)