|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any |
| 16 | +from typing import Optional |
| 17 | + |
| 18 | +from google.adk.agents.callback_context import CallbackContext |
| 19 | +from google.adk.agents.llm_agent import Agent |
| 20 | +from google.adk.models import LlmRequest |
| 21 | +from google.adk.models import LlmResponse |
| 22 | +from google.genai import types |
| 23 | +from google.genai import types |
| 24 | +from pydantic import BaseModel |
| 25 | +import pytest |
| 26 | + |
| 27 | +from .. import utils |
| 28 | + |
| 29 | + |
| 30 | +class MockAgentCallback(BaseModel): |
| 31 | + mock_response: str |
| 32 | + |
| 33 | + def __call__( |
| 34 | + self, |
| 35 | + callback_context: CallbackContext, |
| 36 | + ) -> types.Content: |
| 37 | + return types.Content(parts=[types.Part(text=self.mock_response)]) |
| 38 | + |
| 39 | + |
| 40 | +class MockAsyncAgentCallback(BaseModel): |
| 41 | + mock_response: str |
| 42 | + |
| 43 | + async def __call__( |
| 44 | + self, |
| 45 | + callback_context: CallbackContext, |
| 46 | + ) -> types.Content: |
| 47 | + return types.Content(parts=[types.Part(text=self.mock_response)]) |
| 48 | + |
| 49 | + |
| 50 | +def noop_callback(**kwargs) -> Optional[LlmResponse]: |
| 51 | + pass |
| 52 | + |
| 53 | + |
| 54 | +async def async_noop_callback(**kwargs) -> Optional[LlmResponse]: |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_before_agent_callback(): |
| 60 | + responses = ['agent_response'] |
| 61 | + mock_model = utils.MockModel.create(responses=responses) |
| 62 | + agent = Agent( |
| 63 | + name='root_agent', |
| 64 | + model=mock_model, |
| 65 | + before_agent_callback=MockAgentCallback( |
| 66 | + mock_response='before_agent_callback' |
| 67 | + ), |
| 68 | + ) |
| 69 | + |
| 70 | + runner = utils.TestInMemoryRunner(agent) |
| 71 | + assert utils.simplify_events( |
| 72 | + await runner.run_async_with_new_session('test') |
| 73 | + ) == [ |
| 74 | + ('root_agent', 'before_agent_callback'), |
| 75 | + ] |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_after_agent_callback(): |
| 80 | + responses = ['agent_response'] |
| 81 | + mock_model = utils.MockModel.create(responses=responses) |
| 82 | + agent = Agent( |
| 83 | + name='root_agent', |
| 84 | + model=mock_model, |
| 85 | + after_agent_callback=MockAgentCallback( |
| 86 | + mock_response='after_agent_callback' |
| 87 | + ), |
| 88 | + ) |
| 89 | + |
| 90 | + runner = utils.TestInMemoryRunner(agent) |
| 91 | + assert utils.simplify_events( |
| 92 | + await runner.run_async_with_new_session('test') |
| 93 | + ) == [ |
| 94 | + ('root_agent', 'agent_response'), |
| 95 | + ('root_agent', 'after_agent_callback'), |
| 96 | + ] |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.asyncio |
| 100 | +async def test_before_agent_callback_noop(): |
| 101 | + responses = ['agent_response'] |
| 102 | + mock_model = utils.MockModel.create(responses=responses) |
| 103 | + agent = Agent( |
| 104 | + name='root_agent', |
| 105 | + model=mock_model, |
| 106 | + before_agent_callback=noop_callback, |
| 107 | + ) |
| 108 | + |
| 109 | + runner = utils.TestInMemoryRunner(agent) |
| 110 | + assert utils.simplify_events( |
| 111 | + await runner.run_async_with_new_session('test') |
| 112 | + ) == [ |
| 113 | + ('root_agent', 'agent_response'), |
| 114 | + ] |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.asyncio |
| 118 | +async def test_after_agent_callback_noop(): |
| 119 | + responses = ['agent_response'] |
| 120 | + mock_model = utils.MockModel.create(responses=responses) |
| 121 | + agent = Agent( |
| 122 | + name='root_agent', |
| 123 | + model=mock_model, |
| 124 | + before_agent_callback=noop_callback, |
| 125 | + ) |
| 126 | + |
| 127 | + runner = utils.TestInMemoryRunner(agent) |
| 128 | + assert utils.simplify_events( |
| 129 | + await runner.run_async_with_new_session('test') |
| 130 | + ) == [ |
| 131 | + ('root_agent', 'agent_response'), |
| 132 | + ] |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.asyncio |
| 136 | +async def test_async_before_agent_callback(): |
| 137 | + responses = ['agent_response'] |
| 138 | + mock_model = utils.MockModel.create(responses=responses) |
| 139 | + agent = Agent( |
| 140 | + name='root_agent', |
| 141 | + model=mock_model, |
| 142 | + before_agent_callback=MockAsyncAgentCallback( |
| 143 | + mock_response='async_before_agent_callback' |
| 144 | + ), |
| 145 | + ) |
| 146 | + |
| 147 | + runner = utils.TestInMemoryRunner(agent) |
| 148 | + assert utils.simplify_events( |
| 149 | + await runner.run_async_with_new_session('test') |
| 150 | + ) == [ |
| 151 | + ('root_agent', 'async_before_agent_callback'), |
| 152 | + ] |
| 153 | + |
| 154 | + |
| 155 | +@pytest.mark.asyncio |
| 156 | +async def test_async_after_agent_callback(): |
| 157 | + responses = ['agent_response'] |
| 158 | + mock_model = utils.MockModel.create(responses=responses) |
| 159 | + agent = Agent( |
| 160 | + name='root_agent', |
| 161 | + model=mock_model, |
| 162 | + after_agent_callback=MockAsyncAgentCallback( |
| 163 | + mock_response='async_after_agent_callback' |
| 164 | + ), |
| 165 | + ) |
| 166 | + |
| 167 | + runner = utils.TestInMemoryRunner(agent) |
| 168 | + assert utils.simplify_events( |
| 169 | + await runner.run_async_with_new_session('test') |
| 170 | + ) == [ |
| 171 | + ('root_agent', 'agent_response'), |
| 172 | + ('root_agent', 'async_after_agent_callback'), |
| 173 | + ] |
| 174 | + |
| 175 | + |
| 176 | +@pytest.mark.asyncio |
| 177 | +async def test_async_before_agent_callback_noop(): |
| 178 | + responses = ['agent_response'] |
| 179 | + mock_model = utils.MockModel.create(responses=responses) |
| 180 | + agent = Agent( |
| 181 | + name='root_agent', |
| 182 | + model=mock_model, |
| 183 | + before_agent_callback=async_noop_callback, |
| 184 | + ) |
| 185 | + |
| 186 | + runner = utils.TestInMemoryRunner(agent) |
| 187 | + assert utils.simplify_events( |
| 188 | + await runner.run_async_with_new_session('test') |
| 189 | + ) == [ |
| 190 | + ('root_agent', 'agent_response'), |
| 191 | + ] |
| 192 | + |
| 193 | + |
| 194 | +@pytest.mark.asyncio |
|
862F
195 | +async def test_async_after_agent_callback_noop(): |
| 196 | + responses = ['agent_response'] |
| 197 | + mock_model = utils.MockModel.create(responses=responses) |
| 198 | + agent = Agent( |
| 199 | + name='root_agent', |
| 200 | + model=mock_model, |
| 201 | + before_agent_callback=async_noop_callback, |
| 202 | + ) |
| 203 | + |
| 204 | + runner = utils.TestInMemoryRunner(agent) |
| 205 | + assert utils.simplify_events( |
| 206 | + await runner.run_async_with_new_session('test') |
| 207 | + ) == [ |
| 208 | + ('root_agent', 'agent_response'), |
| 209 | + ] |
0 commit comments