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.
This commit is contained in:
chengyongru
2026-05-05 23:23:29 +08:00
committed by Xubin Ren
parent ca7877f272
commit d97e177981
2 changed files with 21 additions and 21 deletions
+19
View File
@@ -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)