|
| 1 | +"""Real google-genai responses for scripting the Google adapters' turns. |
| 2 | +
|
| 3 | +Gemini reads a ``GenerateContentResponse`` directly and Google ADK reads the |
| 4 | +same object through ``LlmResponse.create``, so both adapters' tests script |
| 5 | +turns from these builders. Every response carries ``SCRIPTED_USAGE``. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from typing import Any |
| 11 | +from unittest.mock import MagicMock |
| 12 | + |
| 13 | +from google.genai import types |
| 14 | + |
| 15 | +from band.runtime.tools import AgentTools |
| 16 | +from band.testing import FakeAgentTools |
| 17 | + |
| 18 | +SCRIPTED_USAGE = types.GenerateContentResponseUsageMetadata( |
| 19 | + prompt_token_count=11, candidates_token_count=3 |
| 20 | +) |
| 21 | + |
| 22 | +# Provider text that must reach only the agent log, never the room. |
| 23 | +PROVIDER_DETAIL = "blocked: the prompt asked for the secret" |
| 24 | + |
| 25 | + |
| 26 | +class PlatformSchemaFakeTools(FakeAgentTools): |
| 27 | + """A ``FakeAgentTools`` advertising the real platform tool schemas, so a |
| 28 | + scripted function call goes through the adapter's own tool bridge.""" |
| 29 | + |
| 30 | + def get_openai_tool_schemas(self, **kwargs: Any) -> list[dict[str, Any]]: |
| 31 | + # Schema building reads the tool registry, never the REST link. |
| 32 | + return AgentTools("room-123", MagicMock()).get_openai_tool_schemas(**kwargs) |
| 33 | + |
| 34 | + |
| 35 | +def tool_call(name: str, args: dict[str, Any]) -> types.GenerateContentResponse: |
| 36 | + return _candidate( |
| 37 | + types.Content( |
| 38 | + role="model", parts=[types.Part.from_function_call(name=name, args=args)] |
| 39 | + ), |
| 40 | + types.FinishReason.STOP, |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def text_reply( |
| 45 | + text: str, finish: types.FinishReason = types.FinishReason.STOP |
| 46 | +) -> types.GenerateContentResponse: |
| 47 | + return _candidate( |
| 48 | + types.Content(role="model", parts=[types.Part.from_text(text=text)]), finish |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def stopped(reason: types.FinishReason) -> types.GenerateContentResponse: |
| 53 | + """A candidate with no content, ended for ``reason``.""" |
| 54 | + return _candidate(None, reason, finish_message=PROVIDER_DETAIL) |
| 55 | + |
| 56 | + |
| 57 | +def prompt_blocked(reason: types.BlockedReason) -> types.GenerateContentResponse: |
| 58 | + """No candidates: the prompt itself was refused.""" |
| 59 | + return types.GenerateContentResponse( |
| 60 | + prompt_feedback=types.GenerateContentResponsePromptFeedback( |
| 61 | + block_reason=reason, block_reason_message=PROVIDER_DETAIL |
| 62 | + ), |
| 63 | + usage_metadata=SCRIPTED_USAGE, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def no_candidates() -> types.GenerateContentResponse: |
| 68 | + return types.GenerateContentResponse(usage_metadata=SCRIPTED_USAGE) |
| 69 | + |
| 70 | + |
| 71 | +def _candidate( |
| 72 | + content: types.Content | None, |
| 73 | + finish: types.FinishReason, |
| 74 | + *, |
| 75 | + finish_message: str | None = None, |
| 76 | +) -> types.GenerateContentResponse: |
| 77 | + return types.GenerateContentResponse( |
| 78 | + candidates=[ |
| 79 | + types.Candidate( |
| 80 | + content=content, finish_reason=finish, finish_message=finish_message |
| 81 | + ) |
| 82 | + ], |
| 83 | + usage_metadata=SCRIPTED_USAGE, |
| 84 | + ) |
0 commit comments