|
| 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 __future__ import annotations |
| 16 | + |
| 17 | +from typing import Any |
| 18 | +from unittest.mock import Mock |
| 19 | + |
| 20 | +from google.adk.agents.base_agent import BaseAgent |
| 21 | +from google.adk.agents.callback_context import CallbackContext |
| 22 | +from google.adk.models.llm_request import LlmRequest |
| 23 | +from google.adk.plugins.multimodal_tool_results_plugin import MultimodalToolResultsPlugin |
| 24 | +from google.adk.plugins.multimodal_tool_results_plugin import PARTS_RETURNED_BY_TOOLS_ID |
| 25 | +from google.adk.tools.base_tool import BaseTool |
| 26 | +from google.adk.tools.tool_context import ToolContext |
| 27 | +from google.genai import types |
| 28 | +import pytest |
| 29 | + |
| 30 | +from .. import testing_utils |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def plugin() -> MultimodalToolResultsPlugin: |
| 35 | + """Create a default plugin instance for testing.""" |
| 36 | + return MultimodalToolResultsPlugin() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def mock_tool() -> MockTool: |
| 41 | + """Create a mock tool for testing.""" |
| 42 | + return Mock(spec=BaseTool) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +async def tool_context() -> ToolContext: |
| 47 | + """Create a mock tool context.""" |
| 48 | + return ToolContext( |
| 49 | + invocation_context=await testing_utils.create_invocation_context( |
| 50 | + agent=Mock(spec=BaseAgent) |
| 51 | + ) |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_tool_returning_parts_are_added_to_llm_request( |
| 57 | + plugin: MultimodalToolResultsPlugin, |
| 58 | + mock_tool: MockTool, |
| 59 | + tool_context: ToolContext, |
| 60 | +): |
| 61 | + """Test that parts returned by a tool are present in the llm_request later.""" |
| 62 | + parts = [types.Part(text="part1"), types.Part(text="part2")] |
| 63 | + |
| 64 | + result = await plugin.after_tool_callback( |
| 65 | + tool=mock_tool, |
| 66 | + tool_args={}, |
| 67 | + tool_context=tool_context, |
| 68 | + result=parts, |
| 69 | + ) |
| 70 | + |
| 71 | + assert result == None |
| 72 | + assert PARTS_RETURNED_BY_TOOLS_ID in tool_context.state |
| 73 | + assert tool_context.state[PARTS_RETURNED_BY_TOOLS_ID] == parts |
| 74 | + |
| 75 | + callback_context = Mock(spec=CallbackContext) |
| 76 | + callback_context.state = tool_context.state |
| 77 | + llm_request = LlmRequest(contents=[types.Content(parts=[])]) |
| 78 | + |
| 79 | + await plugin.before_model_callback( |
| 80 | + callback_context=callback_context, llm_request=llm_request |
| 81 | + ) |
| 82 | + |
| 83 | + assert llm_request.contents[-1].parts == parts |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.asyncio |
| 87 | +async def test_tool_returning_non_list_of_parts_is_unchanged( |
| 88 | + plugin: MultimodalToolResultsPlugin, |
| 89 | + mock_tool: MockTool, |
| 90 | + tool_context: ToolContext, |
| 91 | +): |
| 92 | + """Test where tool returning non list of parts, has this result unchanged.""" |
| 93 | + original_result = {"some": "data"} |
| 94 | + |
| 95 | + result = await plugin.after_tool_callback( |
| 96 | + tool=mock_tool, |
| 97 | + tool_args={}, |
| 98 | + tool_context=tool_context, |
| 99 | + result=original_result, |
| 100 | + ) |
| 101 | + |
| 102 | + assert result == original_result |
| 103 | + assert PARTS_RETURNED_BY_TOOLS_ID not in tool_context.state |
| 104 | + |
| 105 | + callback_context = Mock(spec=CallbackContext) |
| 106 | + callback_context.state = tool_context.state |
| 107 | + llm_request = LlmRequest( |
| 108 | + contents=[types.Content(parts=[types.Part(text="original")])] |
| 109 | + ) |
| 110 | + original_parts = list(llm_request.contents[-1].parts) |
| 111 | + |
| 112 | + await plugin.before_model_callback( |
| 113 | + callback_context=callback_context, llm_request=llm_request |
| 114 | + ) |
| 115 | + |
| 116 | + assert llm_request.contents[-1].parts == original_parts |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.asyncio |
| 120 | +async def test_multiple_tools_returning_parts_are_accumulated( |
| 121 | + plugin: ToolReturningGenAiPartsPlugin, |
| 122 | + mock_tool: MockTool, |
| 123 | + tool_context: ToolContext, |
| 124 | +): |
| 125 | + """Test that parts from multiple tool calls are accumulated.""" |
| 126 | + parts1 = [types.Part(text="part1")] |
| 127 | + parts2 = [types.Part(text="part2")] |
| 128 | + |
| 129 | + await plugin.after_tool_callback( |
| 130 | + tool=mock_tool, |
| 131 | + tool_args={}, |
| 132 | + tool_context=tool_context, |
| 133 | + result=parts1, |
| 134 | + ) |
| 135 | + |
| 136 | + await plugin.after_tool_callback( |
| 137 | + tool=mock_tool, |
| 138 | + tool_args={}, |
| 139 | + tool_context=tool_context, |
| 140 | + result=parts2, |
| 141 | + ) |
| 142 | + |
| 143 | + assert PARTS_RETURNED_BY_TOOLS_ID in tool_context.state |
| 144 | + assert tool_context.state[PARTS_RETURNED_BY_TOOLS_ID] == parts1 + parts2 |
| 145 | + |
| 146 | + callback_context = Mock(spec=CallbackContext) |
| 147 | + callback_context.state = tool_context.state |
| 148 | + llm_request = LlmRequest(contents=[types.Content(parts=[])]) |
| 149 | + |
| 150 | + await plugin.before_model_callback( |
| 151 | + callback_context=callback_context, llm_request=llm_request |
| 152 | + ) |
| 153 | + |
| 154 | + assert llm_request.contents[-1].parts == parts1 + parts2 |
0 commit comments