Files
adk-python/src/google/adk/agents/loop_agent.py
Hangfei Lin 4188673b0f feat(live): Support live mode of sequential agent
Add a `task_completed` function to the agent so when a model finished the task, it can send a signal and the program knows it can go to next agent.

This cl include:
* Implements the `_run_live_impl` in `sequential_agent` so it can handle live case.
* Add an example for sequential agent.
* Improve error message for unimplemented _run_live_impl in other agents.

Note:
1. Compared to non-live case, live agents process a continuous streams of audio
or video, so it doesn't have a native way to tell if it's finished and should pass
to next agent or not. So we introduce a task_compelted() function so the
model can call this function to signal that it's finished the task and we
can move on to next agent.

2. live agents doesn't seems to be very useful or natural in parallel or loop agents so we don't implement it for now. If there is user demand, we can implement it easily using similar approach.

PiperOrigin-RevId: 758315430
2025-05-13 11:56:16 -07:00

63 lines
1.9 KiB
Python

# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Loop agent implementation."""
from __future__ import annotations
from typing import AsyncGenerator
from typing import Optional
from typing_extensions import override
from ..agents.invocation_context import InvocationContext
from ..events.event import Event
from .base_agent import BaseAgent
class LoopAgent(BaseAgent):
"""A shell agent that run its sub-agents in a loop.
When sub-agent generates an event with escalate or max_iterations are
reached, the loop agent will stop.
"""
max_iterations: Optional[int] = None
"""The maximum number of iterations to run the loop agent.
If not set, the loop agent will run indefinitely until a sub-agent
escalates.
"""
@override
async def _run_async_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
times_looped = 0
while not self.max_iterations or times_looped < self.max_iterations:
for sub_agent in self.sub_agents:
async for event in sub_agent.run_async(ctx):
yield event
if event.actions.escalate:
return
times_looped += 1
return
@override
async def _run_live_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
raise NotImplementedError('This is not supported yet for LoopAgent.')
yield # AsyncGenerator requires having at least one yield statement