|
15 | 15 | RealtimeAudioInterrupted, |
16 | 16 | RealtimeError, |
17 | 17 | RealtimeGuardrailTripped, |
| 18 | + RealtimeHandoffEvent, |
18 | 19 | RealtimeHistoryAdded, |
19 | 20 | RealtimeHistoryUpdated, |
20 | 21 | RealtimeRawModelEvent, |
|
45 | 46 | RealtimeModelTurnEndedEvent, |
46 | 47 | RealtimeModelTurnStartedEvent, |
47 | 48 | ) |
| 49 | +from agents.realtime.model_inputs import RealtimeModelSendSessionUpdate |
48 | 50 | from agents.realtime.session import RealtimeSession |
49 | 51 | from agents.tool import FunctionTool |
50 | 52 | from agents.tool_context import ToolContext |
@@ -1488,3 +1490,30 @@ async def mock_get_handoffs(cls, agent, context_wrapper): |
1488 | 1490 | assert model_settings["voice"] == "model_config_only_voice" |
1489 | 1491 | assert model_settings["tool_choice"] == "required" |
1490 | 1492 | assert model_settings["output_audio_format"] == "g711_ulaw" |
| 1493 | + |
| 1494 | + |
| 1495 | +class TestUpdateAgentFunctionality: |
| 1496 | + """Tests for update agent functionality in RealtimeSession""" |
| 1497 | + |
| 1498 | + @pytest.mark.asyncio |
| 1499 | + async def test_update_agent_creates_handoff_and_session_update_event(self, mock_model): |
| 1500 | + first_agent = RealtimeAgent(name="first", instructions="first", tools=[], handoffs=[]) |
| 1501 | + second_agent = RealtimeAgent(name="second", instructions="second", tools=[], handoffs=[]) |
| 1502 | + |
| 1503 | + session = RealtimeSession(mock_model, first_agent, None) |
| 1504 | + |
| 1505 | + await session.update_agent(second_agent) |
| 1506 | + |
| 1507 | + # Should have sent handoff event |
| 1508 | + handoff_event = await session._event_queue.get() |
| 1509 | + assert isinstance(handoff_event, RealtimeHandoffEvent) |
| 1510 | + assert handoff_event.from_agent == first_agent |
| 1511 | + assert handoff_event.to_agent == second_agent |
| 1512 | + |
| 1513 | + # Should have sent session update |
| 1514 | + session_update_event = mock_model.sent_events[0] |
| 1515 | + assert isinstance(session_update_event, RealtimeModelSendSessionUpdate) |
| 1516 | + assert session_update_event.session_settings["instructions"] == "second" |
| 1517 | + |
| 1518 | + # Check that the current agent and session settings are updated |
| 1519 | + assert session._current_agent == second_agent |
0 commit comments