Skip to content

Commit 567142f

Browse files
committed
refactor: Extract _get_root_agent helper method
Address code review feedback by refactoring duplicated App object handling logic into a reusable private helper method. - Added _get_root_agent() helper method - Refactored 3 occurrences to use the helper - Improves maintainability and reduces duplication
1 parent 01d76bb commit 567142f

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/google/adk/cli/adk_web_server.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ async def get_runner_async(self, app_name: str) -> Runner:
486486
self.runner_dict[app_name] = runner
487487
return runner
488488

489+
def _get_root_agent(self, agent_or_app: BaseAgent | App) -> BaseAgent:
490+
"""Extract root agent from either a BaseAgent or App object."""
491+
if isinstance(agent_or_app, App):
492+
return agent_or_app.root_agent
493+
return agent_or_app
494+
489495
def _create_runner(self, agentic_app: App) -> Runner:
490496
"""Create a runner with common services."""
491497
return Runner(
@@ -926,9 +932,8 @@ async def add_session_to_eval_set(
926932

927933
# Populate the session with initial session state.
928934
agent_or_app = self.agent_loader.load_agent(app_name)
929-
if isinstance(agent_or_app, App):
930-
agent_or_app = agent_or_app.root_agent
931-
initial_session_state = create_empty_state(agent_or_app)
935+
root_agent = self._get_root_agent(agent_or_app)
936+
initial_session_state = create_empty_state(root_agent)
932937

933938
new_eval_case = EvalCase(
934939
eval_id=req.eval_id,
@@ -1090,13 +1095,7 @@ async def run_eval(
10901095
)
10911096

10921097
agent_or_app = self.agent_loader.load_agent(app_name)
1093-
# The loader may return an App; unwrap to its root agent so the graph builder
1094-
# receives a BaseAgent instance.
1095-
root_agent = (
1096-
agent_or_app.root_agent
1097-
if isinstance(agent_or_app, App)
1098-
else agent_or_app
1099-
)
1098+
root_agent = self._get_root_agent(agent_or_app)
11001099

11011100
eval_case_results = []
11021101

@@ -1437,13 +1436,7 @@ async def get_event_graph(
14371436
function_calls = event.get_function_calls()
14381437
function_responses = event.get_function_responses()
14391438
agent_or_app = self.agent_loader.load_agent(app_name)
1440-
# The loader may return an App; unwrap to its root agent so the graph builder
1441-
# receives a BaseAgent instance.
1442-
root_agent = (
1443-
agent_or_app.root_agent
1444-
if isinstance(agent_or_app, App)
1445-
else agent_or_app
1446-
)
1439+
root_agent = self._get_root_agent(agent_or_app)
14471440
dot_graph = None
14481441
if function_calls:
14491442
function_call_highlights = []

0 commit comments

Comments
 (0)