|
55 | 55 | from google.adk.sessions.session import Session |
56 | 56 | from google.adk.tools.agent_tool import AgentTool |
57 | 57 | from google.adk.tools.base_toolset import BaseToolset |
| 58 | +from google.adk.tools.long_running_tool import LongRunningFunctionTool |
58 | 59 | from google.adk.workflow import BaseNode |
59 | 60 | from google.adk.workflow import START |
60 | 61 | from google.adk.workflow._workflow import Workflow |
@@ -5691,5 +5692,140 @@ async def _counted_tool() -> dict[str, str]: |
5691 | 5692 | assert responses == [{"error": "Invocation was aborted by client."}] |
5692 | 5693 |
|
5693 | 5694 |
|
| 5695 | +async def test_run_async_routes_user_function_response_to_subagent_when_not_resumable(): |
| 5696 | + """User FunctionResponse routes to sub-agent without resumability.""" |
| 5697 | + |
| 5698 | + def _pending_lro() -> None: |
| 5699 | + """Starts a long-running operation.""" |
| 5700 | + return None |
| 5701 | + |
| 5702 | + sub_agent = LlmAgent( |
| 5703 | + name="sub_agent", |
| 5704 | + disallow_transfer_to_parent=True, |
| 5705 | + disallow_transfer_to_peers=True, |
| 5706 | + model=testing_utils.MockModel.create( |
| 5707 | + responses=[ |
| 5708 | + types.Part.from_function_call(name="_pending_lro", args={}), |
| 5709 | + "Subagent finished LRO", |
| 5710 | + ] |
| 5711 | + ), |
| 5712 | + tools=[LongRunningFunctionTool(func=_pending_lro)], |
| 5713 | + ) |
| 5714 | + root_agent = LlmAgent( |
| 5715 | + name="root_agent", |
| 5716 | + model=testing_utils.MockModel.create( |
| 5717 | + responses=[ |
| 5718 | + types.Part.from_function_call( |
| 5719 | + name="transfer_to_agent", args={"agent_name": "sub_agent"} |
| 5720 | + ), |
| 5721 | + "Root handled next turn", |
| 5722 | + ] |
| 5723 | + ), |
| 5724 | + sub_agents=[sub_agent], |
| 5725 | + ) |
| 5726 | + runner = testing_utils.InMemoryRunner(root_agent) |
| 5727 | + |
| 5728 | + # Turn 1: Root transfers to sub_agent, which emits LRO FunctionCall. |
| 5729 | + turn1_events = await runner.run_async("Start LRO") |
| 5730 | + lro_calls = [ |
| 5731 | + fc |
| 5732 | + for e in turn1_events |
| 5733 | + if e.author == "sub_agent" |
| 5734 | + for fc in e.get_function_calls() |
| 5735 | + if fc.name == "_pending_lro" |
| 5736 | + ] |
| 5737 | + assert len(lro_calls) == 1 |
| 5738 | + lro_fc = lro_calls[0] |
| 5739 | + |
| 5740 | + # Turn 2: User supplies FunctionResponse for the LRO without resumability. |
| 5741 | + fr_part = types.Part.from_function_response( |
| 5742 | + name="_pending_lro", response={"status": "completed"} |
| 5743 | + ) |
| 5744 | + fr_part.function_response.id = lro_fc.id |
| 5745 | + turn2_events = await runner.run_async(types.UserContent(parts=[fr_part])) |
| 5746 | + assert [e.author for e in turn2_events] == ["sub_agent"] |
| 5747 | + assert _texts([e.content for e in turn2_events]) == ["Subagent finished LRO"] |
| 5748 | + |
| 5749 | + # Turn 3: Plain text message returns to root_agent since sub_agent is non-transferable. |
| 5750 | + turn3_events = await runner.run_async("Next turn") |
| 5751 | + assert [e.author for e in turn3_events] == ["root_agent"] |
| 5752 | + assert _texts([e.content for e in turn3_events]) == ["Root handled next turn"] |
| 5753 | + |
| 5754 | + |
| 5755 | +async def test_run_async_does_not_hide_prior_agent_function_response_when_resumable(): |
| 5756 | + """Plain user message does not hide prior agent FunctionResponse.""" |
| 5757 | + sub_agent = LlmAgent( |
| 5758 | + name="sub_agent", |
| 5759 | + disallow_transfer_to_parent=True, |
| 5760 | + disallow_transfer_to_peers=True, |
| 5761 | + model=testing_utils.MockModel.create( |
| 5762 | + responses=["Subagent resumed after agent FR"] |
| 5763 | + ), |
| 5764 | + ) |
| 5765 | + root_agent = LlmAgent( |
| 5766 | + name="root_agent", |
| 5767 | + model=testing_utils.MockModel.create(responses=["Root response"]), |
| 5768 | + sub_agents=[sub_agent], |
| 5769 | + ) |
| 5770 | + app = App( |
| 5771 | + name=TEST_APP_ID, |
| 5772 | + root_agent=root_agent, |
| 5773 | + resumability_config=ResumabilityConfig(is_resumable=True), |
| 5774 | + ) |
| 5775 | + session_service = InMemorySessionService() |
| 5776 | + runner = Runner(app=app, session_service=session_service) |
| 5777 | + session = await session_service.create_session( |
| 5778 | + app_name=TEST_APP_ID, user_id=TEST_USER_ID, session_id=TEST_SESSION_ID |
| 5779 | + ) |
| 5780 | + await session_service.append_event( |
| 5781 | + session, |
| 5782 | + Event( |
| 5783 | + invocation_id="inv_1", |
| 5784 | + author="sub_agent", |
| 5785 | + content=types.Content( |
| 5786 | + role="model", |
| 5787 | + parts=[ |
| 5788 | + types.Part( |
| 5789 | + function_call=types.FunctionCall( |
| 5790 | + id="fc_1", name="tool_fn", args={} |
| 5791 | + ) |
| 5792 | + ) |
| 5793 | + ], |
| 5794 | + ), |
| 5795 | + ), |
| 5796 | + ) |
| 5797 | + await session_service.append_event( |
| 5798 | + session, |
| 5799 | + Event( |
| 5800 | + invocation_id="inv_1", |
| 5801 | + author="sub_agent", |
| 5802 | + content=types.Content( |
| 5803 | + role="user", |
| 5804 | + parts=[ |
| 5805 | + types.Part( |
| 5806 | + function_response=types.FunctionResponse( |
| 5807 | + id="fc_1", name="tool_fn", response={"ok": True} |
| 5808 | + ) |
| 5809 | + ) |
| 5810 | + ], |
| 5811 | + ), |
| 5812 | + ), |
| 5813 | + ) |
| 5814 | + |
| 5815 | + events = [ |
| 5816 | + e |
| 5817 | + async for e in runner.run_async( |
| 5818 | + user_id=TEST_USER_ID, |
| 5819 | + session_id=TEST_SESSION_ID, |
| 5820 | + new_message=types.UserContent(parts=[types.Part(text="Continue")]), |
| 5821 | + ) |
| 5822 | + ] |
| 5823 | + |
| 5824 | + assert {e.author for e in events} == {"sub_agent"} |
| 5825 | + assert _texts([e.content for e in events]) == [ |
| 5826 | + "Subagent resumed after agent FR" |
| 5827 | + ] |
| 5828 | + |
| 5829 | + |
5694 | 5830 | if __name__ == "__main__": |
5695 | 5831 | pytest.main([__file__]) |
0 commit comments