|
| 1 | +from google.adk.agents import Agent |
| 2 | +from google.adk.agents.invocation_context import InvocationContext |
| 3 | +from google.adk.agents.readonly_context import ReadonlyContext |
| 4 | +from google.adk.sessions import Session |
| 5 | +from google.adk.utils import instructions_utils |
| 6 | +import pytest |
| 7 | + |
| 8 | +from .. import utils |
| 9 | + |
| 10 | + |
| 11 | +class MockArtifactService: |
| 12 | + |
| 13 | + def __init__(self, artifacts: dict): |
| 14 | + self.artifacts = artifacts |
| 15 | + |
| 16 | + async def load_artifact(self, app_name, user_id, session_id, filename): |
| 17 | + if filename in self.artifacts: |
| 18 | + return self.artifacts[filename] |
| 19 | + else: |
| 20 | + raise KeyError(f"Artifact '{filename}' not found.") |
| 21 | + |
| 22 | + |
| 23 | +async def _create_test_readonly_context( |
| 24 | + state: dict = None, |
| 25 | + artifact_service: MockArtifactService = None, |
| 26 | + app_name: str = "test_app", |
| 27 | + user_id: str = "test_user", |
| 28 | + session_id: str = "test_session_id", |
| 29 | +) -> ReadonlyContext: |
| 30 | + agent = Agent( |
| 31 | + model="gemini-2.0-flash", |
| 32 | + name="agent", |
| 33 | + instruction="test", |
| 34 | + ) |
| 35 | + invocation_context = await utils.create_invocation_context(agent=agent) |
| 36 | + invocation_context.session = Session( |
| 37 | + state=state if state else {}, |
| 38 | + app_name=app_name, |
| 39 | + user_id=user_id, |
| 40 | + id=session_id, |
| 41 | + ) |
| 42 | + |
| 43 | + invocation_context.artifact_service = artifact_service |
| 44 | + return ReadonlyContext(invocation_context) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_inject_session_state(): |
| 49 | + instruction_template = "Hello {user_name}, you are in {app_state} state." |
| 50 | + invocation_context = await _create_test_readonly_context( |
| 51 | + state={"user_name": "Foo", "app_state": "active"} |
| 52 | + ) |
| 53 | + |
| 54 | + populated_instruction = await instructions_utils.inject_session_state( |
| 55 | + instruction_template, invocation_context |
| 56 | + ) |
| 57 | + assert populated_instruction == "Hello Foo, you are in active state." |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_inject_session_state_with_artifact(): |
| 62 | + instruction_template = "The artifact content is: {artifact.my_file}" |
| 63 | + mock_artifact_service = MockArtifactService( |
| 64 | + {"my_file": "This is my artifact content."} |
| 65 | + ) |
| 66 | + invocation_context = await _create_test_readonly_context( |
| 67 | + artifact_service=mock_artifact_service |
| 68 | + ) |
| 69 | + |
| 70 | + populated_instruction = await instructions_utils.inject_session_state( |
| 71 | + instruction_template, invocation_context |
| 72 | + ) |
| 73 | + assert ( |
| 74 | + populated_instruction |
| 75 | + == "The artifact content is: This is my artifact content." |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.asyncio |
| 80 | +async def test_inject_session_state_with_optional_state(): |
| 81 | + instruction_template = "Optional value: {optional_value?}" |
| 82 | + invocation_context = await _create_test_readonly_context() |
| 83 | + |
| 84 | + populated_instruction = await instructions_utils.inject_session_state( |
| 85 | + instruction_template, invocation_context |
| 86 | + ) |
| 87 | + assert populated_instruction == "Optional value: " |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.asyncio |
| 91 | +async def test_inject_session_state_with_missing_state_raises_key_error(): |
| 92 | + instruction_template = "Hello {missing_key}!" |
| 93 | + invocation_context = await _create_test_readonly_context( |
| 94 | + state={"user_name": "Foo"} |
| 95 | + ) |
| 96 | + |
| 97 | + with pytest.raises( |
| 98 | + KeyError, match="Context variable not found: `missing_key`." |
| 99 | + ): |
| 100 | + await instructions_utils.inject_session_state( |
| 101 | + instruction_template, invocation_context |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_inject_session_state_with_missing_artifact_raises_key_error(): |
| 107 | + instruction_template = "The artifact content is: {artifact.missing_file}" |
| 108 | + mock_artifact_service = MockArtifactService( |
| 109 | + {"my_file": "This is my artifact content."} |
| 110 | + ) |
| 111 | + invocation_context = await _create_test_readonly_context( |
| 112 | + artifact_service=mock_artifact_service |
| 113 | + ) |
| 114 | + |
| 115 | + with pytest.raises(KeyError, match="Artifact 'missing_file' not found."): |
| 116 | + await instructions_utils.inject_session_state( |
| 117 | + instruction_template, invocation_context |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.asyncio |
| 122 | +async def test_inject_session_state_with_invalid_state_name_returns_original(): |
| 123 | + instruction_template = "Hello {invalid-key}!" |
| 124 | + invocation_context = await _create_test_readonly_context( |
| 125 | + state={"user_name": "Foo"} |
| 126 | + ) |
| 127 | + |
| 128 | + populated_instruction = await instructions_utils.inject_session_state( |
| 129 | + instruction_template, invocation_context |
| 130 | + ) |
| 131 | + assert populated_instruction == "Hello {invalid-key}!" |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.asyncio |
| 135 | +async def test_inject_session_state_with_invalid_prefix_state_name_returns_original(): |
| 136 | + instruction_template = "Hello {invalid:key}!" |
| 137 | + invocation_context = await _create_test_readonly_context( |
| 138 | + state={"user_name": "Foo"} |
| 139 | + ) |
| 140 | + |
| 141 | + populated_instruction = await instructions_utils.inject_session_state( |
| 142 | + instruction_template, invocation_context |
| 143 | + ) |
| 144 | + assert populated_instruction == "Hello {invalid:key}!" |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.asyncio |
| 148 | +async def test_inject_session_state_with_valid_prefix_state(): |
| 149 | + instruction_template = "Hello {app:user_name}!" |
| 150 | + invocation_context = await _create_test_readonly_context( |
| 151 | + state={"app:user_name": "Foo"} |
| 152 | + ) |
| 153 | + |
| 154 | + populated_instruction = await instructions_utils.inject_session_state( |
| 155 | + instruction_template, invocation_context |
| 156 | + ) |
| 157 | + assert populated_instruction == "Hello Foo!" |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.asyncio |
| 161 | +async def test_inject_session_state_with_multiple_variables_and_artifacts(): |
| 162 | + instruction_template = """ |
| 163 | + Hello {user_name}, |
| 164 | + You are {user_age} years old. |
| 165 | + Your favorite color is {favorite_color?}. |
| 166 | + The artifact says: {artifact.my_file} |
| 167 | + And another optional artifact: {artifact.other_file} |
| 168 | + """ |
| 169 | + mock_artifact_service = MockArtifactService({ |
| 170 | + "my_file": "This is my artifact content.", |
| 171 | + "other_file": "This is another artifact content.", |
| 172 | + }) |
| 173 | + invocation_context = await _create_test_readonly_context( |
| 174 | + state={"user_name": "Foo", "user_age": 30, "favorite_color": "blue"}, |
| 175 | + artifact_service=mock_artifact_service, |
| 176 | + ) |
| 177 | + |
| 178 | + populated_instruction = await instructions_utils.inject_session_state( |
| 179 | + instruction_template, invocation_context |
| 180 | + ) |
| 181 | + expected_instruction = """ |
| 182 | + Hello Foo, |
| 183 | + You are 30 years old. |
| 184 | + Your favorite color is blue. |
| 185 | + The artifact says: This is my artifact content. |
| 186 | + And another optional artifact: This is another artifact content. |
| 187 | + """ |
| 188 | + assert populated_instruction == expected_instruction |
| 189 | + |
| 190 | + |
| 191 | +@pytest.mark.asyncio |
| 192 | +async def test_inject_session_state_with_empty_artifact_name_raises_key_error(): |
| 193 | + instruction_template = "The artifact content is: {artifact.}" |
| 194 | + mock_artifact_service = MockArtifactService( |
| 195 | + {"my_file": "This is my artifact content."} |
| 196 | + ) |
| 197 | + invocation_context = await _create_test_readonly_context( |
| 198 | + artifact_service=mock_artifact_service |
| 199 | + ) |
| 200 | + |
| 201 | + with pytest.raises(KeyError, match="Artifact '' not found."): |
| 202 | + await instructions_utils.inject_session_state( |
| 203 | + instruction_template, invocation_context |
| 204 | + ) |
| 205 | + |
| 206 | + |
| 207 | +@pytest.mark.asyncio |
| 208 | +async def test_inject_session_state_artifact_service_not_initialized_raises_value_error(): |
| 209 | + instruction_template = "The artifact content is: {artifact.my_file}" |
| 210 | + invocation_context = await _create_test_readonly_context() |
| 211 | + with pytest.raises(ValueError, match="Artifact service is not initialized."): |
| 212 | + await instructions_utils.inject_session_state( |
| 213 | + instruction_template, invocation_context |
| 214 | + ) |
0 commit comments