|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any, Literal, TypeAlias, Union |
| 5 | + |
| 6 | +from .items import RealtimeItem |
| 7 | + |
| 8 | +RealtimeConnectionStatus: TypeAlias = Literal["connecting", "connected", "disconnected"] |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class RealtimeTransportErrorEvent: |
| 13 | + """Represents a transport‑layer error.""" |
| 14 | + |
| 15 | + error: Any |
| 16 | + |
| 17 | + type: Literal["error"] = "error" |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class RealtimeTransportToolCallEvent: |
| 22 | + """Model attempted a tool/function call.""" |
| 23 | + |
| 24 | + name: str |
| 25 | + call_id: str |
| 26 | + arguments: str |
| 27 | + |
| 28 | + id: str | None = None |
| 29 | + previous_item_id: str | None = None |
| 30 | + |
| 31 | + type: Literal["function_call"] = "function_call" |
| 32 | + |
| 33 | + |
| 34 | +@dataclass |
| 35 | +class RealtimeTransportAudioEvent: |
| 36 | + """Raw audio bytes emitted by the model.""" |
| 37 | + |
| 38 | + data: bytes |
| 39 | + response_id: str |
| 40 | + |
| 41 | + type: Literal["audio"] = "audio" |
| 42 | + |
| 43 | + |
| 44 | +@dataclass |
| 45 | +class RealtimeTransportAudioInterruptedEvent: |
| 46 | + """Audio interrupted.""" |
| 47 | + |
| 48 | + type: Literal["audio_interrupted"] = "audio_interrupted" |
| 49 | + |
| 50 | + |
| 51 | +@dataclass |
| 52 | +class RealtimeTransportAudioDoneEvent: |
| 53 | + """Audio done.""" |
| 54 | + |
| 55 | + type: Literal["audio_done"] = "audio_done" |
| 56 | + |
| 57 | + |
| 58 | +@dataclass |
| 59 | +class RealtimeTransportInputAudioTranscriptionCompletedEvent: |
| 60 | + """Input audio transcription completed.""" |
| 61 | + |
| 62 | + item_id: str |
| 63 | + transcript: str |
| 64 | + |
| 65 | + type: Literal["conversation.item.input_audio_transcription.completed"] = ( |
| 66 | + "conversation.item.input_audio_transcription.completed" |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@dataclass |
| 71 | +class RealtimeTransportTranscriptDelta: |
| 72 | + """Partial transcript update.""" |
| 73 | + |
| 74 | + item_id: str |
| 75 | + delta: str |
| 76 | + response_id: str |
| 77 | + |
| 78 | + type: Literal["transcript_delta"] = "transcript_delta" |
| 79 | + |
| 80 | + |
| 81 | +@dataclass |
| 82 | +class RealtimeTransportItemUpdatedEvent: |
| 83 | + """Item added to the history or updated.""" |
| 84 | + |
| 85 | + item: RealtimeItem |
| 86 | + |
| 87 | + type: Literal["item_updated"] = "item_updated" |
| 88 | + |
| 89 | + |
| 90 | +@dataclass |
| 91 | +class RealtimeTransportItemDeletedEvent: |
| 92 | + """Item deleted from the history.""" |
| 93 | + |
| 94 | + item_id: str |
| 95 | + |
| 96 | + type: Literal["item_deleted"] = "item_deleted" |
| 97 | + |
| 98 | + |
| 99 | +@dataclass |
| 100 | +class RealtimeTransportConnectionStatusEvent: |
| 101 | + """Connection status changed.""" |
| 102 | + |
| 103 | + status: RealtimeConnectionStatus |
| 104 | + |
| 105 | + type: Literal["connection_status"] = "connection_status" |
| 106 | + |
| 107 | + |
| 108 | +@dataclass |
| 109 | +class RealtimeTransportTurnStartedEvent: |
| 110 | + """Triggered when the model starts generating a response for a turn.""" |
| 111 | + |
| 112 | + type: Literal["turn_started"] = "turn_started" |
| 113 | + |
| 114 | + |
| 115 | +@dataclass |
| 116 | +class RealtimeTransportTurnEndedEvent: |
| 117 | + """Triggered when the model finishes generating a response for a turn.""" |
| 118 | + |
| 119 | + type: Literal["turn_ended"] = "turn_ended" |
| 120 | + |
| 121 | + |
| 122 | +@dataclass |
| 123 | +class RealtimeTransportOtherEvent: |
| 124 | + """Used as a catchall for vendor-specific events.""" |
| 125 | + |
| 126 | + data: Any |
| 127 | + |
| 128 | + type: Literal["other"] = "other" |
| 129 | + |
| 130 | + |
| 131 | +# TODO (rm) Add usage events |
| 132 | + |
| 133 | + |
| 134 | +RealtimeTransportEvent: TypeAlias = Union[ |
| 135 | + RealtimeTransportErrorEvent, |
| 136 | + RealtimeTransportToolCallEvent, |
| 137 | + RealtimeTransportAudioEvent, |
| 138 | + RealtimeTransportAudioInterruptedEvent, |
| 139 | + RealtimeTransportAudioDoneEvent, |
| 140 | + RealtimeTransportInputAudioTranscriptionCompletedEvent, |
| 141 | + RealtimeTransportTranscriptDelta, |
| 142 | + RealtimeTransportItemUpdatedEvent, |
| 143 | + RealtimeTransportItemDeletedEvent, |
| 144 | + RealtimeTransportConnectionStatusEvent, |
| 145 | + RealtimeTransportTurnStartedEvent, |
| 146 | + RealtimeTransportTurnEndedEvent, |
| 147 | + RealtimeTransportOtherEvent, |
| 148 | +] |
0 commit comments