@@ -58,55 +58,45 @@ async def run(self) -> None:
5858 self .session = session
5959 self .ui .set_is_connected (True )
6060 async for event in session :
61- await self .on_event (event )
61+ await self ._on_event (event )
62+ print ("done" )
6263
6364 # Wait for UI task to complete when session ends
6465 await ui_task
6566
6667 async def on_audio_recorded (self , audio_bytes : bytes ) -> None :
67- """Called when audio is recorded by the UI."""
68- try :
69- # Send the audio to the session
70- assert self .session is not None
71- await self .session .send_audio (audio_bytes )
72- except Exception as e :
73- self .ui .log_message (f"Error sending audio: { e } " )
74-
75- async def on_event (self , event : RealtimeSessionEvent ) -> None :
76- # Display event in the UI
77- try :
78- if event .type == "agent_start" :
79- self .ui .add_transcript (f"Agent started: { event .agent .name } " )
80- elif event .type == "agent_end" :
81- self .ui .add_transcript (f"Agent ended: { event .agent .name } " )
82- elif event .type == "handoff" :
83- self .ui .add_transcript (
84- f"Handoff from { event .from_agent .name } to { event .to_agent .name } "
85- )
86- elif event .type == "tool_start" :
87- self .ui .add_transcript (f"Tool started: { event .tool .name } " )
88- elif event .type == "tool_end" :
89- self .ui .add_transcript (f"Tool ended: { event .tool .name } ; output: { event .output } " )
90- elif event .type == "audio_end" :
91- self .ui .add_transcript ("Audio ended" )
92- elif event .type == "audio" :
93- np_audio = np .frombuffer (event .audio .data , dtype = np .int16 )
94- self .ui .play_audio (np_audio )
95- elif event .type == "audio_interrupted" :
96- self .ui .add_transcript ("Audio interrupted" )
97- elif event .type == "error" :
98- self .ui .add_transcript (f"Error: { event .error } " )
99- elif event .type == "history_updated" :
100- pass
101- elif event .type == "history_added" :
102- pass
103- elif event .type == "raw_model_event" :
104- self .ui .log_message (f"Raw model event: { event .data } " )
105- else :
106- self .ui .log_message (f"Unknown event type: { event .type } " )
107- except Exception as e :
108- # This can happen if the UI has already exited
109- self .ui .log_message (f"Event handling error: { str (e )} " )
68+ # Send the audio to the session
69+ assert self .session is not None
70+ await self .session .send_audio (audio_bytes )
71+
72+ async def _on_event (self , event : RealtimeSessionEvent ) -> None :
73+ if event .type == "agent_start" :
74+ self .ui .add_transcript (f"Agent started: { event .agent .name } " )
75+ elif event .type == "agent_end" :
76+ self .ui .add_transcript (f"Agent ended: { event .agent .name } " )
77+ elif event .type == "handoff" :
78+ self .ui .add_transcript (f"Handoff from { event .from_agent .name } to { event .to_agent .name } " )
79+ elif event .type == "tool_start" :
80+ self .ui .add_transcript (f"Tool started: { event .tool .name } " )
81+ elif event .type == "tool_end" :
82+ self .ui .add_transcript (f"Tool ended: { event .tool .name } ; output: { event .output } " )
83+ elif event .type == "audio_end" :
84+ self .ui .add_transcript ("Audio ended" )
85+ elif event .type == "audio" :
86+ np_audio = np .frombuffer (event .audio .data , dtype = np .int16 )
87+ self .ui .play_audio (np_audio )
88+ elif event .type == "audio_interrupted" :
89+ self .ui .add_transcript ("Audio interrupted" )
90+ elif event .type == "error" :
91+ self .ui .add_transcript (f"Error: { event .error } " )
92+ elif event .type == "history_updated" :
93+ pass
94+ elif event .type == "history_added" :
95+ pass
96+ elif event .type == "raw_model_event" :
97+ self .ui .log_message (f"Raw model event: { event .data } " )
98+ else :
99+ self .ui .log_message (f"Unknown event type: { event .type } " )
110100
111101
112102if __name__ == "__main__" :
0 commit comments