"""运行指标采集:监听 Agent 生命周期事件,结束时记录摘要日志。""" from loguru import logger from app.application.agent.extensions import ( AgentEvent, AgentRunContext, ExtensionRegistry, ) from app.application.agent.python_skills import PythonSkillDefinition def _setup(registry: ExtensionRegistry) -> None: turns = 0 tool_calls = 0 tool_errors = 0 tool_denied = 0 async def listen(event: AgentEvent, run: AgentRunContext) -> None: nonlocal turns, tool_calls, tool_errors, tool_denied # 工具事件经 Agent._on_tool_event 桥接进扩展事件流,故此处可直接观测 # tool.started / tool.completed(status: success / error / denied)。 if event.type == "agent.turn.started": turns += 1 elif event.type == "tool.started": tool_calls += 1 elif event.type == "tool.completed": status = event.payload.get("status") if status == "error": tool_errors += 1 elif status == "denied": tool_denied += 1 elif event.type == "agent.settled": logger.info( "agent_run_metrics", run_id=run.run_id, turns=turns, tool_calls=tool_calls, tool_errors=tool_errors, tool_denied=tool_denied, status=event.payload.get("status"), ) registry.add_event_listener(listen) SKILL = PythonSkillDefinition( name="run_metrics", description="采集 Agent 轮次指标并在运行结束时记录摘要日志。", triggers=("指标", "metrics", "观测"), instructions="", setup=_setup, )