A shell agent that runs its sub-agents in sequence. .. deprecated:: SequentialAgent is deprecated and will be removed in future versions. Please use Workflow instead.
| 51 | ' Please use Workflow instead.' |
| 52 | ) |
| 53 | class SequentialAgent(BaseAgent): |
| 54 | """A shell agent that runs its sub-agents in sequence. |
| 55 | |
| 56 | .. deprecated:: |
| 57 | SequentialAgent is deprecated and will be removed in future versions. |
| 58 | Please use Workflow instead. |
| 59 | """ |
| 60 | |
| 61 | config_type: ClassVar[Type[BaseAgentConfig]] = SequentialAgentConfig |
| 62 | """The config type for this agent. |
| 63 | |
| 64 | DEPRECATED: This attribute is deprecated and will be removed in a future |
| 65 | version, along with the AgentConfig YAML loader. |
| 66 | """ |
| 67 | |
| 68 | @override |
| 69 | async def _run_async_impl( |
| 70 | self, ctx: InvocationContext |
| 71 | ) -> AsyncGenerator[Event, None]: |
| 72 | if not self.sub_agents: |
| 73 | return |
| 74 | |
| 75 | # Initialize or resume the execution state from the agent state. |
| 76 | agent_state = self._load_agent_state(ctx, SequentialAgentState) |
| 77 | start_index = self._get_start_index(agent_state) |
| 78 | |
| 79 | pause_invocation = False |
| 80 | resuming_sub_agent = agent_state is not None |
| 81 | for i in range(start_index, len(self.sub_agents)): |
| 82 | sub_agent = self.sub_agents[i] |
| 83 | if not resuming_sub_agent: |
| 84 | # If we are resuming from the current event, it means the same event has |
| 85 | # already been logged, so we should avoid yielding it again. |
| 86 | if ctx.is_resumable: |
| 87 | agent_state = SequentialAgentState(current_sub_agent=sub_agent.name) |
| 88 | ctx.set_agent_state(self.name, agent_state=agent_state) |
| 89 | yield self._create_agent_state_event(ctx) |
| 90 | |
| 91 | async with Aclosing(sub_agent.run_async(ctx)) as agen: |
| 92 | async for event in agen: |
| 93 | yield event |
| 94 | if ctx.should_pause_invocation(event): |
| 95 | pause_invocation = True |
| 96 | |
| 97 | # Skip the rest of the sub-agents if the invocation is paused. |
| 98 | if pause_invocation: |
| 99 | return |
| 100 | |
| 101 | # Reset the flag for the next sub-agent. |
| 102 | resuming_sub_agent = False |
| 103 | |
| 104 | if ctx.is_resumable: |
| 105 | ctx.set_agent_state(self.name, end_of_agent=True) |
| 106 | yield self._create_agent_state_event(ctx) |
| 107 | |
| 108 | def _get_start_index( |
| 109 | self, |
| 110 | agent_state: SequentialAgentState, |
no outgoing calls