|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +from pathlib import Path |
| 16 | +import textwrap |
15 | 17 | from typing import Optional |
16 | 18 |
|
17 | 19 | from google.adk.agents.base_agent import BaseAgent |
|
21 | 23 | from google.adk.apps.app import App |
22 | 24 | from google.adk.apps.app import ResumabilityConfig |
23 | 25 | from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService |
| 26 | +from google.adk.cli.utils.agent_loader import AgentLoader |
24 | 27 | from google.adk.events.event import Event |
25 | 28 | from google.adk.plugins.base_plugin import BasePlugin |
26 | 29 | from google.adk.runners import Runner |
@@ -158,6 +161,120 @@ def setup_method(self): |
158 | 161 | artifact_service=self.artifact_service, |
159 | 162 | ) |
160 | 163 |
|
| 164 | + |
| 165 | +@pytest.mark.asyncio |
| 166 | +async def test_session_not_found_message_includes_alignment_hint(): |
| 167 | + |
| 168 | + class RunnerWithMismatch(Runner): |
| 169 | + |
| 170 | + def _infer_agent_origin( |
| 171 | + self, agent: BaseAgent |
| 172 | + ) -> tuple[Optional[str], Optional[Path]]: |
| 173 | + del agent |
| 174 | + return "expected_app", Path("/workspace/agents/expected_app") |
| 175 | + |
| 176 | + session_service = InMemorySessionService() |
| 177 | + runner = RunnerWithMismatch( |
| 178 | + app_name="configured_app", |
| 179 | + agent=MockLlmAgent("root_agent"), |
| 180 | + session_service=session_service, |
| 181 | + artifact_service=InMemoryArtifactService(), |
| 182 | + ) |
| 183 | + |
| 184 | + agen = runner.run_async( |
| 185 | + user_id="user", |
| 186 | + session_id="missing", |
| 187 | + new_message=types.Content(role="user", parts=[]), |
| 188 | + ) |
| 189 | + |
| 190 | + with pytest.raises(ValueError) as excinfo: |
| 191 | + await agen.__anext__() |
| 192 | + |
| 193 | + await agen.aclose() |
| 194 | + |
| 195 | + message = str(excinfo.value) |
| 196 | + assert "Session not found" in message |
| 197 | + assert "configured_app" in message |
| 198 | + assert "expected_app" in message |
| 199 | + assert "Ensure the runner app_name matches" in message |
| 200 | + |
| 201 | + |
| 202 | +@pytest.mark.asyncio |
| 203 | +async def test_runner_allows_nested_agent_directories(tmp_path, monkeypatch): |
| 204 | + project_root = tmp_path / "workspace" |
| 205 | + agent_dir = project_root / "agents" / "examples" / "001_hello_world" |
| 206 | + agent_dir.mkdir(parents=True) |
| 207 | + # Make package structure importable. |
| 208 | + for pkg_dir in [ |
| 209 | + project_root / "agents", |
| 210 | + project_root / "agents" / "examples", |
| 211 | + agent_dir, |
| 212 | + ]: |
| 213 | + (pkg_dir / "__init__.py").write_text("", encoding="utf-8") |
| 214 | + # Extra directories that previously confused origin inference, e.g. virtualenv. |
| 215 | + (project_root / "agents" / ".venv").mkdir() |
| 216 | + |
| 217 | + agent_source = textwrap.dedent("""\ |
| 218 | + from google.adk.events.event import Event |
| 219 | + from google.adk.agents.base_agent import BaseAgent |
| 220 | + from google.genai import types |
| 221 | +
|
| 222 | +
|
| 223 | + class SimpleAgent(BaseAgent): |
| 224 | +
|
| 225 | + def __init__(self): |
| 226 | + super().__init__(name='simplest_agent', sub_agents=[]) |
| 227 | +
|
| 228 | + async def _run_async_impl(self, invocation_context): |
| 229 | + yield Event( |
| 230 | + invocation_id=invocation_context.invocation_id, |
| 231 | + author=self.name, |
| 232 | + content=types.Content( |
| 233 | + role='model', |
| 234 | + parts=[types.Part(text='hello from nested')], |
| 235 | + ), |
| 236 | + ) |
| 237 | +
|
| 238 | +
|
| 239 | + root_agent = SimpleAgent() |
| 240 | + """) |
| 241 | + (agent_dir / "agent.py").write_text(agent_source, encoding="utf-8") |
| 242 | + |
| 243 | + monkeypatch.chdir(project_root) |
| 244 | + loader = AgentLoader(agents_dir="agents/examples") |
| 245 | + loaded_agent = loader.load_agent("001_hello_world") |
| 246 | + |
| 247 | + assert isinstance(loaded_agent, BaseAgent) |
| 248 | + session_service = InMemorySessionService() |
| 249 | + artifact_service = InMemoryArtifactService() |
| 250 | + runner = Runner( |
| 251 | + app_name="001_hello_world", |
| 252 | + agent=loaded_agent, |
| 253 | + session_service=session_service, |
| 254 | + artifact_service=artifact_service, |
| 255 | + ) |
| 256 | + assert runner._app_name_alignment_hint is None |
| 257 | + |
| 258 | + session = await session_service.create_session( |
| 259 | + app_name="001_hello_world", |
| 260 | + user_id="user", |
| 261 | + ) |
| 262 | + agen = runner.run_async( |
| 263 | + user_id=session.user_id, |
| 264 | + session_id=session.id, |
| 265 | + new_message=types.Content( |
| 266 | + role="user", |
| 267 | + parts=[types.Part(text="hi")], |
| 268 | + ), |
| 269 | + ) |
| 270 | + event = await agen.__anext__() |
| 271 | + await agen.aclose() |
| 272 | + |
| 273 | + assert event.author == "simplest_agent" |
| 274 | + assert event.content |
| 275 | + assert event.content.parts |
| 276 | + assert event.content.parts[0].text == "hello from nested" |
| 277 | + |
161 | 278 | def test_find_agent_to_run_with_function_response_scenario(self): |
162 | 279 | """Test finding agent when last event is function response.""" |
163 | 280 | # Create a function call from sub_agent1 |
|
0 commit comments