Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a00edd

Browse files
authored
Tests for realtime session (openai#1080)
1 parent c0974d7 commit 7a00edd

File tree

2 files changed

+970
-13
lines changed

2 files changed

+970
-13
lines changed

src/agents/realtime/session.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ def __init__(
8181
self._model_config = model_config or {}
8282
self._event_queue: asyncio.Queue[RealtimeSessionEvent] = asyncio.Queue()
8383
self._closed = False
84-
self._background_task: asyncio.Task[None] | None = None
8584

8685
async def __aenter__(self) -> RealtimeSession:
8786
"""Start the session by connecting to the model. After this, you will be able to stream
@@ -129,14 +128,6 @@ async def close(self) -> None:
129128
self._model.remove_listener(self)
130129
await self._model.close()
131130

132-
# Cancel any background tasks
133-
if self._background_task and not self._background_task.done():
134-
self._background_task.cancel()
135-
try:
136-
await self._background_task
137-
except asyncio.CancelledError:
138-
pass
139-
140131
async def send_message(self, message: RealtimeUserInput) -> None:
141132
"""Send a message to the model."""
142133
await self._model.send_message(message)
@@ -155,16 +146,15 @@ async def on_event(self, event: RealtimeModelEvent) -> None:
155146
if event.type == "error":
156147
await self._put_event(RealtimeError(info=self._event_info, error=event.error))
157148
elif event.type == "function_call":
158-
# Handle tool calls in the background to avoid blocking event stream
159-
self._background_task = asyncio.create_task(self._handle_tool_call(event))
149+
await self._handle_tool_call(event)
160150
elif event.type == "audio":
161151
await self._put_event(RealtimeAudio(info=self._event_info, audio=event))
162152
elif event.type == "audio_interrupted":
163153
await self._put_event(RealtimeAudioInterrupted(info=self._event_info))
164154
elif event.type == "audio_done":
165155
await self._put_event(RealtimeAudioEnd(info=self._event_info))
166156
elif event.type == "conversation.item.input_audio_transcription.completed":
167-
self._history = self._get_new_history(self._history, event)
157+
self._history = RealtimeSession._get_new_history(self._history, event)
168158
await self._put_event(
169159
RealtimeHistoryUpdated(info=self._event_info, history=self._history)
170160
)
@@ -250,8 +240,9 @@ async def _handle_tool_call(self, event: RealtimeModelToolCallEvent) -> None:
250240
# TODO (rm) Add error handling
251241
pass
252242

243+
@classmethod
253244
def _get_new_history(
254-
self,
245+
cls,
255246
old_history: list[RealtimeItem],
256247
event: RealtimeModelInputAudioTranscriptionCompletedEvent | RealtimeItem,
257248
) -> list[RealtimeItem]:
@@ -296,5 +287,6 @@ def _get_new_history(
296287
new_history = old_history.copy()
297288
new_history.insert(previous_index + 1, event)
298289
return new_history
290+
299291
# Otherwise, add it to the end
300292
return old_history + [event]

0 commit comments

Comments
 (0)