From 38882b25bd7b56914d0efb23402f6dac08c4a37f Mon Sep 17 00:00:00 2001 From: wucm667 Date: Mon, 27 Apr 2026 14:21:27 +0800 Subject: [PATCH] fix(web): filter non-agent directories from agent selection dropdown The Web UI agent selection dropdown inherits Google ADK's directory scanning mechanism, which lists ALL subdirectories including non-agent directories (like tools/, tests/, schemas/). Selecting these invalid directories causes loading errors. Add a monkey-patch to AgentLoader.list_agents that filters the directory list to only include valid agent directories. A directory is considered a valid agent if it contains any of: agent.py, __init__.py, root_agent.yaml, or {dir}.py as a module. Fixes volcengine#552 --- veadk/cli/cli_web.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/veadk/cli/cli_web.py b/veadk/cli/cli_web.py index 7719e624..14a04aa0 100644 --- a/veadk/cli/cli_web.py +++ b/veadk/cli/cli_web.py @@ -99,6 +99,54 @@ def wrapped_get_fast_api(self, *args, **kwargs): google.adk.cli.adk_web_server.AdkWebServer.get_fast_api_app = wrapped_get_fast_api +def patch_agent_loader_list_agents(): + """ + Monkey patch AgentLoader.list_agents to only return directories + that contain valid agent patterns. + + This function filters out non-agent directories (like tools/, tests/, schemas/) + from the agent selection dropdown. A directory is considered a valid agent if + it contains any of the following: + - {dir}/agent.py file + - {dir}/__init__.py file + - {dir}/root_agent.yaml file + - {dir}.py file (as a module) + """ + import os + from pathlib import Path + + from google.adk.cli.utils.agent_loader import AgentLoader + + original_list_agents = AgentLoader.list_agents + + def filtered_list_agents(self): + base_path = Path.cwd() / self.agents_dir + all_dirs = [ + x + for x in os.listdir(base_path) + if os.path.isdir(os.path.join(base_path, x)) + and not x.startswith(".") + and x != "__pycache__" + ] + + valid_agents = [] + for d in all_dirs: + dir_path = base_path / d + if (dir_path / "agent.py").exists(): + valid_agents.append(d) + elif (dir_path / "__init__.py").exists(): + valid_agents.append(d) + elif (dir_path / "root_agent.yaml").exists(): + valid_agents.append(d) + elif (base_path / f"{d}.py").exists(): + valid_agents.append(d) + + valid_agents.sort() + return valid_agents + + AgentLoader.list_agents = filtered_list_agents + + @click.command( context_settings=dict(ignore_unknown_options=True, allow_extra_args=True) ) @@ -199,6 +247,7 @@ async def wrapper(*args, **kwargs) -> ADKRunner: ) patch_adkwebserver_disable_openapi() + patch_agent_loader_list_agents() from google.adk.cli.cli_tools_click import cli_web