|
32 | 32 | from google.adk.agents.invocation_context import InvocationContext |
33 | 33 | from google.adk.agents.llm_agent import LlmAgent |
34 | 34 | from google.adk.apps.app import ResumabilityConfig |
| 35 | +from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService |
35 | 36 | from google.adk.events.event import Event |
36 | 37 | from google.adk.plugins.base_plugin import BasePlugin |
37 | 38 | from google.adk.plugins.plugin_manager import PluginManager |
@@ -745,6 +746,51 @@ async def test_run_async_with_async_after_agent_callback_append_reply( |
745 | 746 | ) |
746 | 747 |
|
747 | 748 |
|
| 749 | +@pytest.mark.asyncio |
| 750 | +@pytest.mark.parametrize( |
| 751 | + ('callback_field', 'filename', 'artifact_event_idx', 'reply_event_idx'), |
| 752 | + [ |
| 753 | + ('before_agent_callback', 'before_agent.txt', 0, 1), |
| 754 | + ('after_agent_callback', 'after_agent.txt', 1, 0), |
| 755 | + ], |
| 756 | +) |
| 757 | +async def test_run_async_agent_callback_records_artifact_delta( |
| 758 | + request: pytest.FixtureRequest, |
| 759 | + callback_field: str, |
| 760 | + filename: str, |
| 761 | + artifact_event_idx: int, |
| 762 | + reply_event_idx: int, |
| 763 | +) -> None: |
| 764 | + async def _save_artifact(callback_context: CallbackContext) -> None: |
| 765 | + await callback_context.save_artifact( |
| 766 | + filename, types.Part.from_text(text=f'saved in {callback_field}') |
| 767 | + ) |
| 768 | + |
| 769 | + agent = _TestingAgent( |
| 770 | + name=f'{request.function.__name__}_test_agent', |
| 771 | + before_agent_callback=( |
| 772 | + _save_artifact if callback_field == 'before_agent_callback' else None |
| 773 | + ), |
| 774 | + after_agent_callback=( |
| 775 | + _save_artifact if callback_field == 'after_agent_callback' else None |
| 776 | + ), |
| 777 | + ) |
| 778 | + parent_ctx = await _create_parent_invocation_context( |
| 779 | + request.function.__name__, agent |
| 780 | + ) |
| 781 | + parent_ctx.artifact_service = InMemoryArtifactService() |
| 782 | + |
| 783 | + events = [e async for e in agent.run_async(parent_ctx)] |
| 784 | + |
| 785 | + assert len(events) == 2 |
| 786 | + assert events[artifact_event_idx].author == agent.name |
| 787 | + assert events[artifact_event_idx].content is None |
| 788 | + assert events[artifact_event_idx].actions.artifact_delta == {filename: 0} |
| 789 | + assert events[reply_event_idx].content == types.Content( |
| 790 | + parts=[types.Part(text='Hello, world!')] |
| 791 | + ) |
| 792 | + |
| 793 | + |
748 | 794 | @pytest.mark.asyncio |
749 | 795 | @pytest.mark.parametrize('method_name', ['run_async', 'run_live']) |
750 | 796 | @pytest.mark.parametrize('exit_type', ['cancelled', 'generator_exit']) |
|
0 commit comments