chore: auto-format files.

PiperOrigin-RevId: 764980009
This commit is contained in:
Wei Sun (Jack) 2025-05-29 19:22:22 -07:00 committed by Copybara-Service
parent face2e8cf2
commit 4214c7eddd
4 changed files with 84 additions and 68 deletions

View File

@ -59,14 +59,14 @@ class HelpfulCommand(click.Command):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@staticmethod @staticmethod
def _format_missing_arg_error(click_exception): def _format_missing_arg_error(click_exception):
"""Format the missing argument error with uppercase parameter name. """Format the missing argument error with uppercase parameter name.
Args: Args:
click_exception: The MissingParameter exception from Click. click_exception: The MissingParameter exception from Click.
Returns: Returns:
str: Formatted error message with uppercase parameter name. str: Formatted error message with uppercase parameter name.
""" """

View File

@ -18,9 +18,9 @@ from .tool_context import ToolContext
def transfer_to_agent(agent_name: str, tool_context: ToolContext): def transfer_to_agent(agent_name: str, tool_context: ToolContext):
"""Transfer the question to another agent. """Transfer the question to another agent.
This tool hands off control to another agent when it's more suitable to This tool hands off control to another agent when it's more suitable to
answer the user's question according to the agent's description. answer the user's question according to the agent's description.
Args: Args:
agent_name: the agent name to transfer to. agent_name: the agent name to transfer to.
""" """

View File

@ -11,11 +11,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import pytest
from google.genai import types
from google.adk.code_executors.built_in_code_executor import BuiltInCodeExecutor from google.adk.code_executors.built_in_code_executor import BuiltInCodeExecutor
from google.adk.models.llm_request import LlmRequest from google.adk.models.llm_request import LlmRequest
from google.genai import types
import pytest
@pytest.fixture @pytest.fixture
@ -78,9 +78,10 @@ def test_process_llm_request_gemini_2_model_with_existing_tools(
built_in_executor.process_llm_request(llm_request) built_in_executor.process_llm_request(llm_request)
assert len(llm_request.config.tools) == 2 assert len(llm_request.config.tools) == 2
assert existing_tool in llm_request.config.tools assert existing_tool in llm_request.config.tools
assert types.Tool( assert (
code_execution=types.ToolCodeExecution() types.Tool(code_execution=types.ToolCodeExecution())
) in llm_request.config.tools in llm_request.config.tools
)
def test_process_llm_request_non_gemini_2_model( def test_process_llm_request_non_gemini_2_model(
@ -100,10 +101,9 @@ def test_process_llm_request_no_model_name(
built_in_executor: BuiltInCodeExecutor, built_in_executor: BuiltInCodeExecutor,
): ):
"""Tests that a ValueError is raised if model name is not set.""" """Tests that a ValueError is raised if model name is not set."""
llm_request = LlmRequest() # Model name defaults to None llm_request = LlmRequest() # Model name defaults to None
with pytest.raises(ValueError) as excinfo: with pytest.raises(ValueError) as excinfo:
built_in_executor.process_llm_request(llm_request) built_in_executor.process_llm_request(llm_request)
assert ( assert "Gemini code execution tool is not supported for model None" in str(
"Gemini code execution tool is not supported for model None" excinfo.value
in str(excinfo.value) )
)

View File

@ -12,76 +12,92 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import pytest
from unittest.mock import MagicMock from unittest.mock import MagicMock
from google.adk.code_executors.unsafe_local_code_executor import UnsafeLocalCodeExecutor
from google.adk.code_executors.code_execution_utils import CodeExecutionInput, CodeExecutionResult
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.base_agent import BaseAgent from google.adk.agents.base_agent import BaseAgent
from google.adk.sessions.session import Session from google.adk.agents.invocation_context import InvocationContext
from google.adk.code_executors.code_execution_utils import CodeExecutionInput
from google.adk.code_executors.code_execution_utils import CodeExecutionResult
from google.adk.code_executors.unsafe_local_code_executor import UnsafeLocalCodeExecutor
from google.adk.sessions.base_session_service import BaseSessionService from google.adk.sessions.base_session_service import BaseSessionService
from google.adk.sessions.session import Session
import pytest
@pytest.fixture @pytest.fixture
def mock_invocation_context() -> InvocationContext: def mock_invocation_context() -> InvocationContext:
"""Provides a mock InvocationContext.""" """Provides a mock InvocationContext."""
mock_agent = MagicMock(spec=BaseAgent) mock_agent = MagicMock(spec=BaseAgent)
mock_session = MagicMock(spec=Session) mock_session = MagicMock(spec=Session)
mock_session_service = MagicMock(spec=BaseSessionService) mock_session_service = MagicMock(spec=BaseSessionService)
return InvocationContext( return InvocationContext(
invocation_id="test_invocation", invocation_id="test_invocation",
agent=mock_agent, agent=mock_agent,
session=mock_session, session=mock_session,
session_service=mock_session_service session_service=mock_session_service,
) )
class TestUnsafeLocalCodeExecutor: class TestUnsafeLocalCodeExecutor:
def test_init_default(self): def test_init_default(self):
executor = UnsafeLocalCodeExecutor() executor = UnsafeLocalCodeExecutor()
assert not executor.stateful assert not executor.stateful
assert not executor.optimize_data_file assert not executor.optimize_data_file
def test_init_stateful_raises_error(self): def test_init_stateful_raises_error(self):
with pytest.raises(ValueError, match="Cannot set `stateful=True` in UnsafeLocalCodeExecutor."): with pytest.raises(
UnsafeLocalCodeExecutor(stateful=True) ValueError,
match="Cannot set `stateful=True` in UnsafeLocalCodeExecutor.",
):
UnsafeLocalCodeExecutor(stateful=True)
def test_init_optimize_data_file_raises_error(self): def test_init_optimize_data_file_raises_error(self):
with pytest.raises(ValueError, match="Cannot set `optimize_data_file=True` in UnsafeLocalCodeExecutor."): with pytest.raises(
UnsafeLocalCodeExecutor(optimize_data_file=True) ValueError,
match=(
"Cannot set `optimize_data_file=True` in UnsafeLocalCodeExecutor."
),
):
UnsafeLocalCodeExecutor(optimize_data_file=True)
def test_execute_code_simple_print(self, mock_invocation_context: InvocationContext): def test_execute_code_simple_print(
executor = UnsafeLocalCodeExecutor() self, mock_invocation_context: InvocationContext
code_input = CodeExecutionInput(code='print("hello world")') ):
result = executor.execute_code(mock_invocation_context, code_input) executor = UnsafeLocalCodeExecutor()
code_input = CodeExecutionInput(code='print("hello world")')
result = executor.execute_code(mock_invocation_context, code_input)
assert isinstance(result, CodeExecutionResult) assert isinstance(result, CodeExecutionResult)
assert result.stdout == "hello world\n" assert result.stdout == "hello world\n"
assert result.stderr == "" assert result.stderr == ""
assert result.output_files == [] assert result.output_files == []
def test_execute_code_with_error(self, mock_invocation_context: InvocationContext): def test_execute_code_with_error(
executor = UnsafeLocalCodeExecutor() self, mock_invocation_context: InvocationContext
code_input = CodeExecutionInput(code='raise ValueError("Test error")') ):
result = executor.execute_code(mock_invocation_context, code_input) executor = UnsafeLocalCodeExecutor()
code_input = CodeExecutionInput(code='raise ValueError("Test error")')
result = executor.execute_code(mock_invocation_context, code_input)
assert isinstance(result, CodeExecutionResult) assert isinstance(result, CodeExecutionResult)
assert result.stdout == "" assert result.stdout == ""
assert "Test error" in result.stderr assert "Test error" in result.stderr
assert result.output_files == [] assert result.output_files == []
def test_execute_code_variable_assignment(self, mock_invocation_context: InvocationContext): def test_execute_code_variable_assignment(
executor = UnsafeLocalCodeExecutor() self, mock_invocation_context: InvocationContext
code_input = CodeExecutionInput(code='x = 10\nprint(x * 2)') ):
result = executor.execute_code(mock_invocation_context, code_input) executor = UnsafeLocalCodeExecutor()
code_input = CodeExecutionInput(code="x = 10\nprint(x * 2)")
result = executor.execute_code(mock_invocation_context, code_input)
assert result.stdout == "20\n" assert result.stdout == "20\n"
assert result.stderr == "" assert result.stderr == ""
def test_execute_code_empty(self, mock_invocation_context: InvocationContext): def test_execute_code_empty(self, mock_invocation_context: InvocationContext):
executor = UnsafeLocalCodeExecutor() executor = UnsafeLocalCodeExecutor()
code_input = CodeExecutionInput(code='') code_input = CodeExecutionInput(code="")
result = executor.execute_code(mock_invocation_context, code_input) result = executor.execute_code(mock_invocation_context, code_input)
assert result.stdout == "" assert result.stdout == ""
assert result.stderr == "" assert result.stderr == ""