|
20 | 20 | from unittest.mock import patch |
21 | 21 |
|
22 | 22 | from google.adk.agents.llm_agent import Agent |
| 23 | +from google.adk.code_executors.base_code_executor import BaseCodeExecutor |
23 | 24 | from google.adk.code_executors.built_in_code_executor import BuiltInCodeExecutor |
| 25 | +from google.adk.code_executors.code_execution_utils import CodeExecutionResult |
24 | 26 | from google.adk.flows.llm_flows._code_execution import response_processor |
25 | 27 | from google.adk.models.llm_response import LlmResponse |
26 | 28 | from google.genai import types |
@@ -105,3 +107,44 @@ async def test_builtin_code_executor_image_artifact_creation(mock_datetime): |
105 | 107 | == f'Saved as artifact: {expected_filename2}. ' |
106 | 108 | ) |
107 | 109 | assert not llm_response.content.parts[2].inline_data |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.asyncio |
| 113 | +@patch('google.adk.flows.llm_flows._code_execution.logger') |
| 114 | +async def test_logs_executed_code(mock_logger): |
| 115 | + """Test that the response processor logs the code it executes.""" |
| 116 | + mock_code_executor = MagicMock(spec=BaseCodeExecutor) |
| 117 | + mock_code_executor.code_block_delimiters = [('```python\n', '\n```')] |
| 118 | + mock_code_executor.error_retry_attempts = 2 |
| 119 | + mock_code_executor.stateful = False |
| 120 | + mock_code_executor.execute_code.return_value = CodeExecutionResult( |
| 121 | + stdout='hello' |
| 122 | + ) |
| 123 | + |
| 124 | + agent = Agent(name='test_agent', code_executor=mock_code_executor) |
| 125 | + invocation_context = await testing_utils.create_invocation_context( |
| 126 | + agent=agent, user_content='test message' |
| 127 | + ) |
| 128 | + invocation_context.artifact_service = MagicMock() |
| 129 | + invocation_context.artifact_service.save_artifact = AsyncMock() |
| 130 | + |
| 131 | + llm_response = LlmResponse( |
| 132 | + content=types.Content( |
| 133 | + parts=[ |
| 134 | + types.Part(text='Here is some code:'), |
| 135 | + types.Part(text='```python\nprint("hello")\n```'), |
| 136 | + ] |
| 137 | + ) |
| 138 | + ) |
| 139 | + |
| 140 | + _ = [ |
| 141 | + event |
| 142 | + async for event in response_processor.run_async( |
| 143 | + invocation_context, llm_response |
| 144 | + ) |
| 145 | + ] |
| 146 | + |
| 147 | + mock_code_executor.execute_code.assert_called_once() |
| 148 | + mock_logger.debug.assert_called_once_with( |
| 149 | + 'Executed code:\n```\n%s\n```', 'print("hello")' |
| 150 | + ) |
0 commit comments