From d97e1779819750e14b7f99af1468233c7f25e0c5 Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Mon, 4 May 2026 23:26:20 +0800 Subject: [PATCH] refactor(sdk): move SDKCaptureHook to agent/hook.py Colocate the capture hook with the rest of the hook infrastructure instead of inlining it in the top-level facade module. --- nanobot/agent/hook.py | 19 +++++++++++++++++++ nanobot/nanobot.py | 23 ++--------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/nanobot/agent/hook.py b/nanobot/agent/hook.py index 52daf6042..d0106cfb6 100644 --- a/nanobot/agent/hook.py +++ b/nanobot/agent/hook.py @@ -102,3 +102,22 @@ class CompositeHook(AgentHook): for h in self._hooks: content = h.finalize_content(context, content) return content + + +class SDKCaptureHook(AgentHook): + """Record tool names and the final message list for ``RunResult``. + + The runner mutates ``context.messages`` in place across iterations, so the + snapshot is refreshed on every ``after_iteration`` call; the last call + reflects the end-of-turn state the SDK caller cares about. + """ + + def __init__(self) -> None: + super().__init__() + self.tools_used: list[str] = [] + self.messages: list[dict[str, Any]] = [] + + async def after_iteration(self, context: AgentHookContext) -> None: + for call in context.tool_calls: + self.tools_used.append(call.name) + self.messages = list(context.messages) diff --git a/nanobot/nanobot.py b/nanobot/nanobot.py index f8ffd8fa7..5e5857595 100644 --- a/nanobot/nanobot.py +++ b/nanobot/nanobot.py @@ -6,7 +6,7 @@ from dataclasses import dataclass from pathlib import Path from typing import Any -from nanobot.agent.hook import AgentHook, AgentHookContext +from nanobot.agent.hook import AgentHook, SDKCaptureHook from nanobot.agent.loop import AgentLoop from nanobot.bus.queue import MessageBus @@ -104,7 +104,7 @@ class Nanobot: Different keys get independent history. hooks: Optional lifecycle hooks for this run. """ - capture = _SDKCaptureHook() + capture = SDKCaptureHook() prev = self._loop._extra_hooks base_hooks = list(hooks) if hooks is not None else list(prev or []) self._loop._extra_hooks = [capture, *base_hooks] @@ -123,25 +123,6 @@ class Nanobot: ) -class _SDKCaptureHook(AgentHook): - """Record tool names and the final message list for ``RunResult``. - - The runner mutates ``context.messages`` in place across iterations, so the - snapshot is refreshed on every ``after_iteration`` call; the last call - reflects the end-of-turn state the SDK caller cares about. - """ - - def __init__(self) -> None: - super().__init__() - self.tools_used: list[str] = [] - self.messages: list[dict[str, Any]] = [] - - async def after_iteration(self, context: AgentHookContext) -> None: - for call in context.tool_calls: - self.tools_used.append(call.name) - self.messages = list(context.messages) - - def _make_provider(config: Any) -> Any: """Create the LLM provider from config (extracted from CLI).""" from nanobot.providers.factory import make_provider