(
self, ctx: InvocationContext
)
| 176 | |
| 177 | @override |
| 178 | async def _run_async_impl( |
| 179 | self, ctx: InvocationContext |
| 180 | ) -> AsyncGenerator[Event, None]: |
| 181 | if not self.sub_agents: |
| 182 | return |
| 183 | |
| 184 | agent_state = self._load_agent_state(ctx, BaseAgentState) |
| 185 | if ctx.is_resumable and agent_state is None: |
| 186 | ctx.set_agent_state(self.name, agent_state=BaseAgentState()) |
| 187 | yield self._create_agent_state_event(ctx) |
| 188 | |
| 189 | agent_runs = [] |
| 190 | # Prepare and collect async generators for each sub-agent. |
| 191 | for sub_agent in self.sub_agents: |
| 192 | sub_agent_ctx = _create_branch_ctx_for_sub_agent(self, sub_agent, ctx) |
| 193 | |
| 194 | # Only include sub-agents that haven't finished in a previous run. |
| 195 | if not sub_agent_ctx.end_of_agents.get(sub_agent.name): |
| 196 | agent_runs.append(sub_agent.run_async(sub_agent_ctx)) |
| 197 | |
| 198 | pause_invocation = False |
| 199 | try: |
| 200 | merge_func = ( |
| 201 | _merge_agent_run |
| 202 | if sys.version_info >= (3, 11) |
| 203 | else _merge_agent_run_pre_3_11 |
| 204 | ) |
| 205 | async with Aclosing(merge_func(agent_runs)) as agen: |
| 206 | async for event in agen: |
| 207 | yield event |
| 208 | if ctx.should_pause_invocation(event): |
| 209 | pause_invocation = True |
| 210 | |
| 211 | if pause_invocation: |
| 212 | return |
| 213 | |
| 214 | # Once all sub-agents are done, mark the ParallelAgent as final. |
| 215 | if ctx.is_resumable and all( |
| 216 | ctx.end_of_agents.get(sub_agent.name) for sub_agent in self.sub_agents |
| 217 | ): |
| 218 | ctx.set_agent_state(self.name, end_of_agent=True) |
| 219 | yield self._create_agent_state_event(ctx) |
| 220 | |
| 221 | finally: |
| 222 | for sub_agent_run in agent_runs: |
| 223 | await sub_agent_run.aclose() |
| 224 | |
| 225 | @override |
| 226 | async def _run_live_impl( |
nothing calls this directly
no test coverage detected