Skip to content

Commit 578fad7

Browse files
seanzhougooglecopybara-github
authored andcommitted
feat: Allow agent loader to load built-in agents from special directories in adk folder
PiperOrigin-RevId: 802716848
1 parent a3410fa commit 578fad7

File tree

2 files changed

+352
-19
lines changed

2 files changed

+352
-19
lines changed

src/google/adk/cli/utils/agent_loader.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232

3333
logger = logging.getLogger("google_adk." + __name__)
3434

35+
# Special agents directory for agents with names starting with double underscore
36+
SPECIAL_AGENTS_DIR = os.path.join(
37+
os.path.dirname(__file__), "..", "..", "built_in_agents"
38+
)
39+
3540

3641
class AgentLoader(BaseAgentLoader):
3742
"""Centralized agent loading with proper isolation, caching, and .env loading.
@@ -139,9 +144,11 @@ def _load_from_submodule(self, agent_name: str) -> Optional[BaseAgent]:
139144
return None
140145

141146
@experimental
142-
def _load_from_yaml_config(self, agent_name: str) -> Optional[BaseAgent]:
147+
def _load_from_yaml_config(
148+
self, agent_name: str, agents_dir: str
149+
) -> Optional[BaseAgent]:
143150
# Load from the config file at agents_dir/{agent_name}/root_agent.yaml
144-
config_path = os.path.join(self.agents_dir, agent_name, "root_agent.yaml")
151+
config_path = os.path.join(agents_dir, agent_name, "root_agent.yaml")
145152
try:
146153
agent = config_agent_utils.from_config(config_path)
147154
logger.info("Loaded root agent for %s from %s", agent_name, config_path)
@@ -163,32 +170,41 @@ def _load_from_yaml_config(self, agent_name: str) -> Optional[BaseAgent]:
163170

164171
def _perform_load(self, agent_name: str) -> BaseAgent:
165172
"""Internal logic to load an agent"""
166-
# Add self.agents_dir to sys.path
167-
if self.agents_dir not in sys.path:
168-
sys.path.insert(0, self.agents_dir)
169-
170-
logger.debug(
171-
"Loading .env for agent %s from %s", agent_name, self.agents_dir
172-
)
173-
envs.load_dotenv_for_agent(agent_name, str(self.agents_dir))
174-
175-
if root_agent := self._load_from_module_or_package(agent_name):
173+
# Determine the directory to use for loading
174+
if agent_name.startswith("__"):
175+
# Special agent: use special agents directory
176+
agents_dir = os.path.abspath(SPECIAL_AGENTS_DIR)
177+
# Remove the double underscore prefix for the actual agent name
178+
actual_agent_name = agent_name[2:]
179+
else:
180+
# Regular agent: use the configured agents directory
181+
agents_dir = self.agents_dir
182+
actual_agent_name = agent_name
183+
184+
# Add agents_dir to sys.path
185+
if agents_dir not in sys.path:
186+
sys.path.insert(0, agents_dir)
187+
188+
logger.debug("Loading .env for agent %s from %s", agent_name, agents_dir)
189+
envs.load_dotenv_for_agent(actual_agent_name, str(agents_dir))
190+
191+
if root_agent := self._load_from_module_or_package(actual_agent_name):
176192
return root_agent
177193

178-
if root_agent := self._load_from_submodule(agent_name):
194+
if root_agent := self._load_from_submodule(actual_agent_name):
179195
return root_agent
180196

181-
if root_agent := self._load_from_yaml_config(agent_name):
197+
if root_agent := self._load_from_yaml_config(actual_agent_name, agents_dir):
182198
return root_agent
183199

184200
# If no root_agent was found by any pattern
185201
raise ValueError(
186202
f"No root_agent found for '{agent_name}'. Searched in"
187-
f" '{agent_name}.agent.root_agent', '{agent_name}.root_agent' and"
188-
f" '{agent_name}/root_agent.yaml'."
189-
f" Ensure '{self.agents_dir}/{agent_name}' is structured correctly,"
190-
" an .env file can be loaded if present, and a root_agent is"
191-
" exposed."
203+
f" '{actual_agent_name}.agent.root_agent',"
204+
f" '{actual_agent_name}.root_agent' and"
205+
f" '{actual_agent_name}/root_agent.yaml'. Ensure"
206+
f" '{agents_dir}/{actual_agent_name}' is structured correctly, an .env"
207+
" file can be loaded if present, and a root_agent is exposed."
192208
)
193209

194210
@override

0 commit comments

Comments
 (0)