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. .. deprecated:: LoopAgent is deprecated and will be removed in future versions. Please use Workflow instead.
| 55 | ' Please use Workflow instead.' |
| 56 | ) |
| 57 | class LoopAgent(BaseAgent): |
| 58 | """A shell agent that run its sub-agents in a loop. |
| 59 | |
| 60 | When sub-agent generates an event with escalate or max_iterations are |
| 61 | reached, the loop agent will stop. |
| 62 | |
| 63 | .. deprecated:: |
| 64 | LoopAgent is deprecated and will be removed in future versions. |
| 65 | Please use Workflow instead. |
| 66 | """ |
| 67 | |
| 68 | config_type: ClassVar[type[BaseAgentConfig]] = LoopAgentConfig |
| 69 | """The config type for this agent. |
| 70 | |
| 71 | DEPRECATED: This attribute is deprecated and will be removed in a future |
| 72 | version, along with the AgentConfig YAML loader. |
| 73 | """ |
| 74 | |
| 75 | max_iterations: Optional[int] = None |
| 76 | """The maximum number of iterations to run the loop agent. |
| 77 | |
| 78 | If not set, the loop agent will run indefinitely until a sub-agent |
| 79 | escalates. |
| 80 | """ |
| 81 | |
| 82 | @override |
| 83 | async def _run_async_impl( |
| 84 | self, ctx: InvocationContext |
| 85 | ) -> AsyncGenerator[Event, None]: |
| 86 | if not self.sub_agents: |
| 87 | return |
| 88 | |
| 89 | agent_state = self._load_agent_state(ctx, LoopAgentState) |
| 90 | is_resuming_at_current_agent = agent_state is not None |
| 91 | times_looped, start_index = self._get_start_state(agent_state) |
| 92 | |
| 93 | should_exit = False |
| 94 | pause_invocation = False |
| 95 | while ( |
| 96 | not self.max_iterations or times_looped < self.max_iterations |
| 97 | ) and not (should_exit or pause_invocation): |
| 98 | for i in range(start_index, len(self.sub_agents)): |
| 99 | sub_agent = self.sub_agents[i] |
| 100 | |
| 101 | if ctx.is_resumable and not is_resuming_at_current_agent: |
| 102 | # If we are resuming from the current event, it means the same event |
| 103 | # has already been logged, so we should avoid yielding it again. |
| 104 | agent_state = LoopAgentState( |
| 105 | current_sub_agent=sub_agent.name, |
| 106 | times_looped=times_looped, |
| 107 | ) |
| 108 | ctx.set_agent_state(self.name, agent_state=agent_state) |
| 109 | yield self._create_agent_state_event(ctx) |
| 110 | |
| 111 | is_resuming_at_current_agent = False |
| 112 | |
| 113 | async with Aclosing(sub_agent.run_async(ctx)) as agen: |
| 114 | async for event in agen: |
no outgoing calls