Skip to content

Commit 38882b2

Browse files
committed
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 #552
1 parent bf3c45d commit 38882b2

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

veadk/cli/cli_web.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,54 @@ def wrapped_get_fast_api(self, *args, **kwargs):
9999
google.adk.cli.adk_web_server.AdkWebServer.get_fast_api_app = wrapped_get_fast_api
100100

101101

102+
def patch_agent_loader_list_agents():
103+
"""
104+
Monkey patch AgentLoader.list_agents to only return directories
105+
that contain valid agent patterns.
106+
107+
This function filters out non-agent directories (like tools/, tests/, schemas/)
108+
from the agent selection dropdown. A directory is considered a valid agent if
109+
it contains any of the following:
110+
- {dir}/agent.py file
111+
- {dir}/__init__.py file
112+
- {dir}/root_agent.yaml file
113+
- {dir}.py file (as a module)
114+
"""
115+
import os
116+
from pathlib import Path
117+
118+
from google.adk.cli.utils.agent_loader import AgentLoader
119+
120+
original_list_agents = AgentLoader.list_agents
121+
122+
def filtered_list_agents(self):
123+
base_path = Path.cwd() / self.agents_dir
124+
all_dirs = [
125+
x
126+
for x in os.listdir(base_path)
127+
if os.path.isdir(os.path.join(base_path, x))
128+
and not x.startswith(".")
129+
and x != "__pycache__"
130+
]
131+
132+
valid_agents = []
133+
for d in all_dirs:
134+
dir_path = base_path / d
135+
if (dir_path / "agent.py").exists():
136+
valid_agents.append(d)
137+
elif (dir_path / "__init__.py").exists():
138+
valid_agents.append(d)
139+
elif (dir_path / "root_agent.yaml").exists():
140+
valid_agents.append(d)
141+
elif (base_path / f"{d}.py").exists():
142+
valid_agents.append(d)
143+
144+
valid_agents.sort()
145+
return valid_agents
146+
147+
AgentLoader.list_agents = filtered_list_agents
148+
149+
102150
@click.command(
103151
context_settings=dict(ignore_unknown_options=True, allow_extra_args=True)
104152
)
@@ -199,6 +247,7 @@ async def wrapper(*args, **kwargs) -> ADKRunner:
199247
)
200248

201249
patch_adkwebserver_disable_openapi()
250+
patch_agent_loader_list_agents()
202251

203252
from google.adk.cli.cli_tools_click import cli_web
204253

0 commit comments

Comments
 (0)