|
| 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 | +"""Unit tests for LlmAgent include_contents field behavior.""" |
| 16 | + |
| 17 | +from google.adk.agents.llm_agent import LlmAgent |
| 18 | +from google.adk.agents.sequential_agent import SequentialAgent |
| 19 | +from google.genai import types |
| 20 | +import pytest |
| 21 | + |
| 22 | +from .. import testing_utils |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.asyncio |
| 26 | +async def test_include_contents_default_behavior(): |
| 27 | + """Test that include_contents='default' preserves conversation history including tool interactions.""" |
| 28 | + |
| 29 | + def simple_tool(message: str) -> dict: |
| 30 | + return {"result": f"Tool processed: {message}"} |
| 31 | + |
| 32 | + mock_model = testing_utils.MockModel.create( |
| 33 | + responses=[ |
| 34 | + types.Part.from_function_call( |
| 35 | + name="simple_tool", args={"message": "first"} |
| 36 | + ), |
| 37 | + "First response", |
| 38 | + types.Part.from_function_call( |
| 39 | + name="simple_tool", args={"message": "second"} |
| 40 | + ), |
| 41 | + "Second response", |
| 42 | + ] |
| 43 | + ) |
| 44 | + |
| 45 | + agent = LlmAgent( |
| 46 | + name="test_agent", |
| 47 | + model=mock_model, |
| 48 | + include_contents="default", |
| 49 | + instruction="You are a helpful assistant", |
| 50 | + tools=[simple_tool], |
| 51 | + ) |
| 52 | + |
| 53 | + runner = testing_utils.InMemoryRunner(agent) |
| 54 | + runner.run("First message") |
| 55 | + runner.run("Second message") |
| 56 | + |
| 57 | + # First turn requests |
| 58 | + assert testing_utils.simplify_contents(mock_model.requests[0].contents) == [ |
| 59 | + ("user", "First message") |
| 60 | + ] |
| 61 | + |
| 62 | + assert testing_utils.simplify_contents(mock_model.requests[1].contents) == [ |
| 63 | + ("user", "First message"), |
| 64 | + ( |
| 65 | + "model", |
| 66 | + types.Part.from_function_call( |
| 67 | + name="simple_tool", args={"message": "first"} |
| 68 | + ), |
| 69 | + ), |
| 70 | + ( |
| 71 | + "user", |
| 72 | + types.Part.from_function_response( |
| 73 | + name="simple_tool", response={"result": "Tool processed: first"} |
| 74 | + ), |
| 75 | + ), |
| 76 | + ] |
| 77 | + |
| 78 | + # Second turn should include full conversation history |
| 79 | + assert testing_utils.simplify_contents(mock_model.requests[2].contents) == [ |
| 80 | + ("user", "First message"), |
| 81 | + ( |
| 82 | + "model", |
| 83 | + types.Part.from_function_call( |
| 84 | + name="simple_tool", args={"message": "first"} |
| 85 | + ), |
| 86 | + ), |
| 87 | + ( |
| 88 | + "user", |
| 89 | + types.Part.from_function_response( |
| 90 | + name="simple_tool", response={"result": "Tool processed: first"} |
| 91 | + ), |
| 92 | + ), |
| 93 | + ("model", "First response"), |
| 94 | + ("user", "Second message"), |
| 95 | + ] |
| 96 | + |
| 97 | + # Second turn with tool should include full history + current tool interaction |
| 98 | + assert testing_utils.simplify_contents(mock_model.requests[3].contents) == [ |
| 99 | + ("user", "First message"), |
| 100 | + ( |
| 101 | + "model", |
| 102 | + types.Part.from_function_call( |
| 103 | + name="simple_tool", args={"message": "first"} |
| 104 | + ), |
| 105 | + ), |
| 106 | + ( |
| 107 | + "user", |
| 108 | + types.Part.from_function_response( |
| 109 | + name="simple_tool", response={"result": "Tool processed: first"} |
| 110 | + ), |
| 111 | + ), |
| 112 | + ("model", "First response"), |
| 113 | + ("user", "Second message"), |
| 114 | + ( |
| 115 | + "model", |
| 116 | + types.Part.from_function_call( |
| 117 | + name="simple_tool", args={"message": "second"} |
| 118 | + ), |
| 119 | + ), |
| 120 | + ( |
| 121 | + "user", |
| 122 | + types.Part.from_function_response( |
| 123 | + name="simple_tool", response={"result": "Tool processed: second"} |
| 124 | + ), |
| 125 | + ), |
| 126 | + ] |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.asyncio |
| 130 | +async def test_include_contents_none_behavior(): |
| 131 | + """Test that include_contents='none' excludes conversation history but includes current input.""" |
| 132 | + |
| 133 | + def simple_tool(message: str) -> dict: |
| 134 | + return {"result": f"Tool processed: {message}"} |
| 135 | + |
| 136 | + mock_model = testing_utils.MockModel.create( |
| 137 | + responses=[ |
| 138 | + types.Part.from_function_call( |
| 139 | + name="simple_tool", args={"message": "first"} |
| 140 | + ), |
| 141 | + "First response", |
| 142 | + "Second response", |
| 143 | + ] |
| 144 | + ) |
| 145 | + |
| 146 | + agent = LlmAgent( |
| 147 | + name="test_agent", |
| 148 | + model=mock_model, |
| 149 | + include_contents="none", |
| 150 | + instruction="You are a helpful assistant", |
| 151 | + tools=[simple_tool], |
| 152 | + ) |
| 153 | + |
| 154 | + runner = testing_utils.InMemoryRunner(agent) |
| 155 | + runner.run("First message") |
| 156 | + runner.run("Second message") |
| 157 | + |
| 158 | + # First turn behavior |
| 159 | + assert testing_utils.simplify_contents(mock_model.requests[0].contents) == [ |
| 160 | + ("user", "First message") |
| 161 | + ] |
| 162 | + |
| 163 | + assert testing_utils.simplify_contents(mock_model.requests[1].contents) == [ |
| 164 | + ("user", "First message"), |
| 165 | + ( |
| 166 | + "model", |
| 167 | + types.Part.from_function_call( |
| 168 | + name="simple_tool", args={"message": "first"} |
| 169 | + ), |
| 170 | + ), |
| 171 | + ( |
| 172 | + "user", |
| 173 | + types.Part.from_function_response( |
| 174 | + name="simple_tool", response={"result": "Tool processed: first"} |
| 175 | + ), |
| 176 | + ), |
| 177 | + ] |
| 178 | + |
| 179 | + # Second turn should only have current input, no history |
| 180 | + assert testing_utils.simplify_contents(mock_model.requests[2].contents) == [ |
| 181 | + ("user", "Second message") |
| 182 | + ] |
| 183 | + |
| 184 | + # System instruction and tools should be preserved |
| 185 | + assert ( |
| 186 | + "You are a helpful assistant" |
| 187 | + in mock_model.requests[0].config.system_instruction |
| 188 | + ) |
| 189 | + assert len(mock_model.requests[0].config.tools) > 0 |
| 190 | + |
| 191 | + |
| 192 | +@pytest.mark.asyncio |
| 193 | +async def test_include_contents_none_sequential_agents(): |
| 194 | + """Test include_contents='none' with sequential agents.""" |
| 195 | + |
| 196 | + agent1_model = testing_utils.MockModel.create( |
| 197 | + responses=["Agent1 response: XYZ"] |
| 198 | + ) |
| 199 | + agent1 = LlmAgent( |
| 200 | + name="agent1", |
| 201 | + model=agent1_model, |
| 202 | + instruction="You are Agent1", |
| 203 | + ) |
| 204 | + |
| 205 | + agent2_model = testing_utils.MockModel.create( |
| 206 | + responses=["Agent2 final response"] |
| 207 | + ) |
| 208 | + agent2 = LlmAgent( |
| 209 | + name="agent2", |
| 210 | + model=agent2_model, |
| 211 | + include_contents="none", |
| 212 | + instruction="You are Agent2", |
| 213 | + ) |
| 214 | + |
| 215 | + sequential_agent = SequentialAgent( |
| 216 | + name="sequential_test_agent", sub_agents=[agent1, agent2] |
| 217 | + ) |
| 218 | + |
| 219 | + runner = testing_utils.InMemoryRunner(sequential_agent) |
| 220 | + events = runner.run("Original user request") |
| 221 | + |
| 222 | + assert len(events) == 2 |
| 223 | + assert events[0].author == "agent1" |
| 224 | + assert events[1].author == "agent2" |
| 225 | + |
| 226 | + # Agent1 sees original user request |
| 227 | + agent1_contents = testing_utils.simplify_contents( |
| 228 | + agent1_model.requests[0].contents |
| 229 | + ) |
| 230 | + assert ("user", "Original user request") in agent1_contents |
| 231 | + |
| 232 | + # Agent2 with include_contents='none' should not see original request |
| 233 | + agent2_contents = testing_utils.simplify_contents( |
| 234 | + agent2_model.requests[0].contents |
| 235 | + ) |
| 236 | + |
| 237 | + assert not any( |
| 238 | + "Original user request" in str(content) for _, content in agent2_contents |
| 239 | + ) |
| 240 | + assert any( |
| 241 | + "Agent1 response" in str(content) for _, content in agent2_contents |
| 242 | + ) |
0 commit comments