Skip to content

Commit 6342826

Browse files
authored
Update runner.py (#2)
1 parent 621ac83 commit 6342826

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

src/agent/runner.py

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ async def _run_agent_loop(
173173
if not self._running:
174174
break
175175

176-
frame = await self._process_event(event)
177-
if frame:
176+
frames = await self._process_event(event)
177+
for frame in frames:
178178
await self.ctx.outbound_queue.put(frame)
179179

180180
except asyncio.CancelledError:
@@ -183,79 +183,71 @@ async def _run_agent_loop(
183183
logger.exception(f"Error in agent loop for {self.session_id}")
184184
raise
185185

186-
async def _process_event(self, event: Any) -> OutboundFrame | None:
187-
"""Process an ADK event and convert to OutboundFrame if applicable.
188-
189-
Args:
190-
event: The ADK Event to process
186+
async def _process_event(self, event: Any) -> list[OutboundFrame]:
187+
"""Process an ADK event and convert to OutboundFrame(s) if applicable."""
191188

192-
Returns:
193-
OutboundFrame if the event should be sent to client, None otherwise
194-
"""
189+
frames = []
195190
if event.interrupted:
196-
return OutboundFrame(
191+
frames.append(OutboundFrame(
197192
type="INTERRUPTED",
198193
payload={},
199194
session_id=self.session_id,
200-
)
195+
))
201196

202197
if event.turn_complete:
203-
return OutboundFrame(
198+
frames.append(OutboundFrame(
204199
type="TURN_COMPLETE",
205200
payload={},
206201
session_id=self.session_id,
207-
)
202+
))
208203

209204
if event.error_code:
210205
logger.error(f"Agent error: {event.error_code} - {event.error_message}")
211-
return OutboundFrame(
206+
frames.append(OutboundFrame(
212207
type="ERROR",
213208
payload={
214209
"code": event.error_code,
215210
"message": event.error_message,
216211
},
217212
session_id=self.session_id,
218-
)
213+
))
219214

220215
if event.content:
221-
return await self._process_content(event.content)
222-
223-
return None
216+
content_frames = await self._process_content(event.content)
217+
frames.extend(content_frames)
224218

225-
async def _process_content(self, content: types.Content) -> OutboundFrame | None:
226-
"""Process Content from an event, extracting audio/text.
227-
228-
Args:
229-
content: The Content to process
219+
return frames
230220

231-
Returns:
232-
OutboundFrame with audio or text data
233-
"""
221+
async def _process_content(self, content: types.Content) -> list[OutboundFrame]:
222+
"""Process Content from an event, extracting audio/text."""
223+
frames = []
234224
if not content.parts:
235-
return None
225+
return frames
236226

237227
for part in content.parts:
238228
if part.inline_data:
239-
return OutboundFrame(
229+
frames.append(OutboundFrame(
240230
type="AUDIO",
241231
payload={
242232
"data": part.inline_data.data,
243233
"mime_type": part.inline_data.mime_type,
244234
},
245235
session_id=self.session_id,
246-
)
236+
))
247237

248238
if part.text:
249-
return OutboundFrame(
239+
frames.append(OutboundFrame(
250240
type="TEXT",
251241
payload={"text": part.text},
252242
session_id=self.session_id,
253-
)
243+
))
254244

255245
if part.function_call:
256-
return await self._handle_function_call(part.function_call)
246+
frame = await self._handle_function_call(part.function_call)
247+
if frame:
248+
frames.append(frame)
257249

258-
return None
250+
return frames
259251

260252
async def _handle_function_call(
261253
self, function_call: types.FunctionCall

0 commit comments

Comments
 (0)