|
26 | 26 |
|
27 | 27 | from fastapi.testclient import TestClient |
28 | 28 | from google.adk.agents.base_agent import BaseAgent |
| 29 | +from google.adk.agents.llm_agent import LlmAgent |
29 | 30 | from google.adk.agents.run_config import RunConfig |
30 | 31 | from google.adk.apps.app import App |
31 | 32 | from google.adk.artifacts.base_artifact_service import ArtifactVersion |
@@ -801,6 +802,198 @@ def test_list_apps_detailed(test_app): |
801 | 802 | logger.info(f"Listed apps: {data}") |
802 | 803 |
|
803 | 804 |
|
| 805 | +def test_get_adk_app_info_llm_agent(test_app, mock_agent_loader): |
| 806 | + """Test retrieving app info when root agent is an LlmAgent.""" |
| 807 | + agent = LlmAgent( |
| 808 | + name="test_llm_agent", description="test description", model="test_model" |
| 809 | + ) |
| 810 | + with patch.object(mock_agent_loader, "load_agent", return_value=agent): |
| 811 | + response = test_app.get("/apps/test_app/app-info") |
| 812 | + assert response.status_code == 200 |
| 813 | + data = response.json() |
| 814 | + assert data["name"] == "test_app" |
| 815 | + assert data["rootAgentName"] == "test_llm_agent" |
| 816 | + assert data["description"] == "test description" |
| 817 | + assert data["language"] == "python" |
| 818 | + assert "agents" in data |
| 819 | + assert "test_llm_agent" in data["agents"] |
| 820 | + |
| 821 | + |
| 822 | +def test_get_adk_app_info_llm_agent_with_subagents(test_app, mock_agent_loader): |
| 823 | + """Test retrieving app info when root agent is an LlmAgent with sub_agents and tools.""" |
| 824 | + |
| 825 | + def sub_tool1(a: int) -> str: |
| 826 | + """Sub tool 1.""" |
| 827 | + return str(a) |
| 828 | + |
| 829 | + def sub_tool2(b: str) -> str: |
| 830 | + """Sub tool 2.""" |
| 831 | + return b |
| 832 | + |
| 833 | + sub_agent1 = LlmAgent( |
| 834 | + name="sub_agent1", |
| 835 | + description="sub description 1", |
| 836 | + model="test_model", |
| 837 | + tools=[sub_tool1], |
| 838 | + ) |
| 839 | + sub_agent2 = LlmAgent( |
| 840 | + name="sub_agent2", |
| 841 | + description="sub description 2", |
| 842 | + model="test_model", |
| 843 | + tools=[sub_tool2], |
| 844 | + ) |
| 845 | + agent = LlmAgent( |
| 846 | + name="test_llm_agent", |
| 847 | + description="test description", |
| 848 | + model="test_model", |
| 849 | + sub_agents=[sub_agent1, sub_agent2], |
| 850 | + ) |
| 851 | + with patch.object(mock_agent_loader, "load_agent", return_value=agent): |
| 852 | + response = test_app.get("/apps/test_app/app-info") |
| 853 | + assert response.status_code == 200 |
| 854 | + data = response.json() |
| 855 | + assert data["rootAgentName"] == "test_llm_agent" |
| 856 | + assert "test_llm_agent" in data["agents"] |
| 857 | + assert "sub_agent1" in data["agents"] |
| 858 | + assert "sub_agent2" in data["agents"] |
| 859 | + |
| 860 | + # Verify tools for sub_agent1 |
| 861 | + agent1_info = data["agents"]["sub_agent1"] |
| 862 | + assert "tools" in agent1_info |
| 863 | + assert len(agent1_info["tools"]) == 1 |
| 864 | + tool1 = agent1_info["tools"][0] |
| 865 | + field_name1 = ( |
| 866 | + "functionDeclarations" |
| 867 | + if "functionDeclarations" in tool1 |
| 868 | + else "function_declarations" |
| 869 | + ) |
| 870 | + assert field_name1 in tool1 |
| 871 | + assert tool1[field_name1][0]["name"] == "sub_tool1" |
| 872 | + |
| 873 | + # Verify tools for sub_agent2 |
| 874 | + agent2_info = data["agents"]["sub_agent2"] |
| 875 | + assert "tools" in agent2_info |
| 876 | + assert len(agent2_info["tools"]) == 1 |
| 877 | + tool2 = agent2_info["tools"][0] |
| 878 | + field_name2 = ( |
| 879 | + "functionDeclarations" |
| 880 | + if "functionDeclarations" in tool2 |
| 881 | + else "function_declarations" |
| 882 | + ) |
| 883 | + assert field_name2 in tool2 |
| 884 | + assert tool2[field_name2][0]["name"] == "sub_tool2" |
| 885 | + |
| 886 | + |
| 887 | +def test_get_adk_app_info_triple_nested_agents_with_tools( |
| 888 | + test_app, mock_agent_loader |
| 889 | +): |
| 890 | + """Test retrieving app info when there are triple nested agents with tools.""" |
| 891 | + |
| 892 | + def tool1(a: int) -> str: |
| 893 | + """Tool 1.""" |
| 894 | + return str(a) |
| 895 | + |
| 896 | + def tool2(b: str) -> str: |
| 897 | + """Tool 2.""" |
| 898 | + return b |
| 899 | + |
| 900 | + def tool3(c: float) -> str: |
| 901 | + """Tool 3.""" |
| 902 | + return str(c) |
| 903 | + |
| 904 | + # Level 3 (deepest) |
| 905 | + agent3 = LlmAgent( |
| 906 | + name="agent3", |
| 907 | + description="Level 3 agent", |
| 908 | + model="test_model", |
| 909 | + tools=[tool3], |
| 910 | + ) |
| 911 | + |
| 912 | + # Level 2 |
| 913 | + agent2 = LlmAgent( |
| 914 | + name="agent2", |
| 915 | + description="Level 2 agent", |
| 916 | + model="test_model", |
| 917 | + tools=[tool2], |
| 918 | + sub_agents=[agent3], |
| 919 | + ) |
| 920 | + |
| 921 | + # Level 1 (root) |
| 922 | + root_agent = LlmAgent( |
| 923 | + name="root_agent", |
| 924 | + description="Level 1 agent", |
| 925 | + model="test_model", |
| 926 | + tools=[tool1], |
| 927 | + sub_agents=[agent2], |
| 928 | + ) |
| 929 | + |
| 930 | + with patch.object(mock_agent_loader, "load_agent", return_value=root_agent): |
| 931 | + response = test_app.get("/apps/test_app/app-info") |
| 932 | + assert response.status_code == 200 |
| 933 | + data = response.json() |
| 934 | + assert data["rootAgentName"] == "root_agent" |
| 935 | + assert "root_agent" in data["agents"] |
| 936 | + assert "agent2" in data["agents"] |
| 937 | + assert "agent3" in data["agents"] |
| 938 | + |
| 939 | + # Verify each has its tools |
| 940 | + for agent_name, exp_tool_name in [ |
| 941 | + ("root_agent", "tool1"), |
| 942 | + ("agent2", "tool2"), |
| 943 | + ("agent3", "tool3"), |
| 944 | + ]: |
| 945 | + ai = data["agents"][agent_name] |
| 946 | + assert len(ai["tools"]) == 1 |
| 947 | + tool = ai["tools"][0] |
| 948 | + field_name = ( |
| 949 | + "functionDeclarations" |
| 950 | + if "functionDeclarations" in tool |
| 951 | + else "function_declarations" |
| 952 | + ) |
| 953 | + assert tool[field_name][0]["name"] == exp_tool_name |
| 954 | + |
| 955 | + |
| 956 | +def test_get_adk_app_info_llm_agent_with_function_tool( |
| 957 | + test_app, mock_agent_loader |
| 958 | +): |
| 959 | + """Test retrieving app info when root agent has tools.""" |
| 960 | + |
| 961 | + def my_tool(a: int, b: str) -> str: |
| 962 | + """A dummy tool function.""" |
| 963 | + return f"{a} {b}" |
| 964 | + |
| 965 | + agent = LlmAgent( |
| 966 | + name="test_llm_agent", |
| 967 | + description="test description", |
| 968 | + model="test_model", |
| 969 | + tools=[my_tool], |
| 970 | + ) |
| 971 | + with patch.object(mock_agent_loader, "load_agent", return_value=agent): |
| 972 | + response = test_app.get("/apps/test_app/app-info") |
| 973 | + assert response.status_code == 200 |
| 974 | + data = response.json() |
| 975 | + assert data["rootAgentName"] == "test_llm_agent" |
| 976 | + assert "test_llm_agent" in data["agents"] |
| 977 | + agent_info = data["agents"]["test_llm_agent"] |
| 978 | + assert "tools" in agent_info |
| 979 | + assert len(agent_info["tools"]) == 1 |
| 980 | + |
| 981 | + # Verify tool serialization |
| 982 | + tool = agent_info["tools"][0] |
| 983 | + func_decls = tool["functionDeclarations"] |
| 984 | + assert len(func_decls) == 1 |
| 985 | + assert func_decls[0]["name"] == "my_tool" |
| 986 | + |
| 987 | + |
| 988 | +def test_get_adk_app_info_non_llm_agent(test_app, mock_agent_loader): |
| 989 | + """Test retrieving app info when root agent is not an LlmAgent raises 400.""" |
| 990 | + agent = DummyAgent("dummy_agent") |
| 991 | + with patch.object(mock_agent_loader, "load_agent", return_value=agent): |
| 992 | + response = test_app.get("/apps/test_app/app-info") |
| 993 | + assert response.status_code == 400 |
| 994 | + assert "Root agent is not an LlmAgent" in response.json()["detail"] |
| 995 | + |
| 996 | + |
804 | 997 | def test_create_session_with_id(test_app, test_session_info): |
805 | 998 | """Test creating a session with a specific ID.""" |
806 | 999 | new_session_id = "new_session_id" |
|
0 commit comments