(
self, ctx: InvocationContext
)
| 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: |
| 115 | yield event |
| 116 | if event.actions.escalate: |
| 117 | should_exit = True |
| 118 | if ctx.should_pause_invocation(event): |
| 119 | pause_invocation = True |
| 120 | |
| 121 | if should_exit or pause_invocation: |
| 122 | break # break inner for loop |
| 123 | |
| 124 | if not pause_invocation: |
| 125 | # Restart from the beginning of the loop. |
| 126 | start_index = 0 |
| 127 | times_looped += 1 |
| 128 | # Reset the state of all sub-agents in the loop. |
| 129 | ctx.reset_sub_agent_states(self.name) |
| 130 | |
| 131 | # If the invocation is paused, we should not yield the end of agent event. |
| 132 | if pause_invocation: |
| 133 | return |
| 134 | |
| 135 | if ctx.is_resumable: |
| 136 | ctx.set_agent_state(self.name, end_of_agent=True) |
| 137 | yield self._create_agent_state_event(ctx) |
| 138 | |
| 139 | def _get_start_state( |
| 140 | self, |
nothing calls this directly
no test coverage detected