|
| 1 | +from collections import defaultdict |
| 2 | +from typing import Any, Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from agents.agent import Agent |
| 7 | +from agents.items import ItemHelpers, ModelResponse, TResponseInputItem |
| 8 | +from agents.lifecycle import AgentHooks |
| 9 | +from agents.run import Runner |
| 10 | +from agents.run_context import RunContextWrapper, TContext |
| 11 | +from agents.tool import Tool |
| 12 | + |
| 13 | +from .fake_model import FakeModel |
| 14 | +from .test_responses import ( |
| 15 | + get_function_tool, |
| 16 | + get_text_message, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class AgentHooksForTests(AgentHooks): |
| 21 | + def __init__(self): |
| 22 | + self.events: dict[str, int] = defaultdict(int) |
| 23 | + |
| 24 | + def reset(self): |
| 25 | + self.events.clear() |
| 26 | + |
| 27 | + async def on_start(self, context: RunContextWrapper[TContext], agent: Agent[TContext]) -> None: |
| 28 | + self.events["on_start"] += 1 |
| 29 | + |
| 30 | + async def on_end( |
| 31 | + self, context: RunContextWrapper[TContext], agent: Agent[TContext], output: Any |
| 32 | + ) -> None: |
| 33 | + self.events["on_end"] += 1 |
| 34 | + |
| 35 | + async def on_handoff( |
| 36 | + self, context: RunContextWrapper[TContext], agent: Agent[TContext], source: Agent[TContext] |
| 37 | + ) -> None: |
| 38 | + self.events["on_handoff"] += 1 |
| 39 | + |
| 40 | + async def on_tool_start( |
| 41 | + self, context: RunContextWrapper[TContext], agent: Agent[TContext], tool: Tool |
| 42 | + ) -> None: |
| 43 | + self.events["on_tool_start"] += 1 |
| 44 | + |
| 45 | + async def on_tool_end( |
| 46 | + self, |
| 47 | + context: RunContextWrapper[TContext], |
| 48 | + agent: Agent[TContext], |
| 49 | + tool: Tool, |
| 50 | + result: str, |
| 51 | + ) -> None: |
| 52 | + self.events["on_tool_end"] += 1 |
| 53 | + |
| 54 | + # NEW: LLM hooks |
| 55 | + async def on_llm_start( |
| 56 | + self, |
| 57 | + context: RunContextWrapper[TContext], |
| 58 | + agent: Agent[TContext], |
| 59 | + system_prompt: Optional[str], |
| 60 | + input_items: list[TResponseInputItem], |
| 61 | + ) -> None: |
| 62 | + self.events["on_llm_start"] += 1 |
| 63 | + |
| 64 | + async def on_llm_end( |
| 65 | + self, |
| 66 | + context: RunContextWrapper[TContext], |
| 67 | + agent: Agent[TContext], |
| 68 | + response: ModelResponse, |
| 69 | + ) -> None: |
| 70 | + self.events["on_llm_end"] += 1 |
| 71 | + |
| 72 | + |
| 73 | +# Example test using the above hooks: |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_async_agent_hooks_with_llm(): |
| 76 | + hooks = AgentHooksForTests() |
| 77 | + model = FakeModel() |
| 78 | + agent = Agent( |
| 79 | + name="A", model=model, tools=[get_function_tool("f", "res")], handoffs=[], hooks=hooks |
| 80 | + ) |
| 81 | + # Simulate a single LLM call producing an output: |
| 82 | + model.set_next_output([get_text_message("hello")]) |
| 83 | + await Runner.run(agent, input="hello") |
| 84 | + # Expect one on_start, one on_llm_start, one on_llm_end, and one on_end |
| 85 | + assert hooks.events == {"on_start": 1, "on_llm_start": 1, "on_llm_end": 1, "on_end": 1} |
| 86 | + |
| 87 | + |
| 88 | +# test_sync_agent_hook_with_llm() |
| 89 | +def test_sync_agent_hook_with_llm(): |
| 90 | + hooks = AgentHooksForTests() |
| 91 | + model = FakeModel() |
| 92 | + agent = Agent( |
| 93 | + name="A", model=model, tools=[get_function_tool("f", "res")], handoffs=[], hooks=hooks |
| 94 | + ) |
| 95 | + # Simulate a single LLM call producing an output: |
| 96 | + model.set_next_output([get_text_message("hello")]) |
| 97 | + Runner.run_sync(agent, input="hello") |
| 98 | + # Expect one on_start, one on_llm_start, one on_llm_end, and one on_end |
| 99 | + assert hooks.events == {"on_start": 1, "on_llm_start": 1, "on_llm_end": 1, "on_end": 1} |
| 100 | + |
| 101 | + |
| 102 | +# test_streamed_agent_hooks_with_llm(): |
| 103 | +@pytest.mark.asyncio |
| 104 | +async def test_streamed_agent_hooks_with_llm(): |
| 105 | + hooks = AgentHooksForTests() |
| 106 | + model = FakeModel() |
| 107 | + agent = Agent( |
| 108 | + name="A", model=model, tools=[get_function_tool("f", "res")], handoffs=[], hooks=hooks |
| 109 | + ) |
| 110 | + # Simulate a single LLM call producing an output: |
| 111 | + model.set_next_output([get_text_message("hello")]) |
| 112 | + stream = Runner.run_streamed(agent, input="hello") |
| 113 | + |
| 114 | + async for event in stream.stream_events(): |
| 115 | + if event.type == "raw_response_event": |
| 116 | + continue |
| 117 | + if event.type == "agent_updated_stream_event": |
| 118 | + print(f"[EVENT] agent_updated → {event.new_agent.name}") |
| 119 | + elif event.type == "run_item_stream_event": |
| 120 | + item = event.item |
| 121 | + if item.type == "tool_call_item": |
| 122 | + print("[EVENT] tool_call_item") |
| 123 | + elif item.type == "tool_call_output_item": |
| 124 | + print(f"[EVENT] tool_call_output_item → {item.output}") |
| 125 | + elif item.type == "message_output_item": |
| 126 | + text = ItemHelpers.text_message_output(item) |
| 127 | + print(f"[EVENT] message_output_item → {text}") |
| 128 | + |
| 129 | + # Expect one on_start, one on_llm_start, one on_llm_end, and one on_end |
| 130 | + assert hooks.events == {"on_start": 1, "on_llm_start": 1, "on_llm_end": 1, "on_end": 1} |
0 commit comments