Implementation for live SequentialAgent. Compared to the non-live case, live agents process a continuous stream of audio or video, so there is no way to tell if it's finished and should pass to the next agent or not. So we introduce a task_completed() function so the model can call
(
self, ctx: InvocationContext
)
| 132 | |
| 133 | @override |
| 134 | async def _run_live_impl( |
| 135 | self, ctx: InvocationContext |
| 136 | ) -> AsyncGenerator[Event, None]: |
| 137 | """Implementation for live SequentialAgent. |
| 138 | |
| 139 | Compared to the non-live case, live agents process a continuous stream of audio |
| 140 | or video, so there is no way to tell if it's finished and should pass |
| 141 | to the next agent or not. So we introduce a task_completed() function so the |
| 142 | model can call this function to signal that it's finished the task and we |
| 143 | can move on to the next agent. |
| 144 | |
| 145 | Args: |
| 146 | ctx: The invocation context of the agent. |
| 147 | """ |
| 148 | if not self.sub_agents: |
| 149 | return |
| 150 | |
| 151 | # There is no way to know if it's using live during init phase so we have to init it here |
| 152 | for sub_agent in self.sub_agents: |
| 153 | # add tool |
| 154 | def task_completed(): |
| 155 | """ |
| 156 | Signals that the agent has successfully completed the user's question |
| 157 | or task. |
| 158 | """ |
| 159 | return 'Task completion signaled.' |
| 160 | |
| 161 | if isinstance(sub_agent, LlmAgent): |
| 162 | # Use function name to dedupe. |
| 163 | if task_completed.__name__ not in sub_agent.tools: |
| 164 | sub_agent.tools.append(task_completed) |
| 165 | sub_agent.instruction += f"""If you finished the user's request |
| 166 | according to its description, call the {task_completed.__name__} function |
| 167 | to exit so the next agents can take over. When calling this function, |
| 168 | do not generate any text other than the function call.""" |
| 169 | |
| 170 | for sub_agent in self.sub_agents: |
| 171 | async with Aclosing(sub_agent.run_live(ctx)) as agen: |
| 172 | async for event in agen: |
| 173 | yield event |