refactor: unify agent runner lifecycle hooks

This commit is contained in:
Xubin Ren
2026-03-27 12:41:17 +08:00
committed by Xubin Ren
parent e7d371ec1e
commit 5bf0f6fe7d
5 changed files with 277 additions and 65 deletions
+8 -5
View File
@@ -8,6 +8,7 @@ from typing import Any
from loguru import logger
from nanobot.agent.hook import AgentHook, AgentHookContext
from nanobot.agent.runner import AgentRunSpec, AgentRunner
from nanobot.agent.skills import BUILTIN_SKILLS_DIR
from nanobot.agent.tools.filesystem import EditFileTool, ListDirTool, ReadFileTool, WriteFileTool
@@ -113,17 +114,19 @@ class SubagentManager:
{"role": "system", "content": system_prompt},
{"role": "user", "content": task},
]
async def _log_tool_calls(tool_calls) -> None:
for tool_call in tool_calls:
args_str = json.dumps(tool_call.arguments, ensure_ascii=False)
logger.debug("Subagent [{}] executing: {} with arguments: {}", task_id, tool_call.name, args_str)
class _SubagentHook(AgentHook):
async def before_execute_tools(self, context: AgentHookContext) -> None:
for tool_call in context.tool_calls:
args_str = json.dumps(tool_call.arguments, ensure_ascii=False)
logger.debug("Subagent [{}] executing: {} with arguments: {}", task_id, tool_call.name, args_str)
result = await self.runner.run(AgentRunSpec(
initial_messages=messages,
tools=tools,
model=self.model,
max_iterations=15,
before_execute_tools=_log_tool_calls,
hook=_SubagentHook(),
max_iterations_message="Task completed but no final response was generated.",
error_message=None,
fail_on_tool_error=True,