|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import Any, Literal, Union |
| 3 | + |
| 4 | +from typing_extensions import TypeAlias |
| 5 | + |
| 6 | +from ..run_context import RunContextWrapper |
| 7 | +from ..tool import Tool |
| 8 | +from .agent import RealtimeAgent |
| 9 | +from .items import RealtimeItem |
| 10 | +from .transport_events import RealtimeTransportAudioEvent, RealtimeTransportEvent |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class RealtimeEventInfo: |
| 15 | + context: RunContextWrapper |
| 16 | + """The context for the event.""" |
| 17 | + |
| 18 | + |
| 19 | +@dataclass |
| 20 | +class RealtimeAgentStartEvent: |
| 21 | + """A new agent has started.""" |
| 22 | + |
| 23 | + agent: RealtimeAgent |
| 24 | + """The new agent.""" |
| 25 | + |
| 26 | + info: RealtimeEventInfo |
| 27 | + """Common info for all events, such as the context.""" |
| 28 | + |
| 29 | + type: Literal["agent_start"] = "agent_start" |
| 30 | + |
| 31 | + |
| 32 | +@dataclass |
| 33 | +class RealtimeAgentEndEvent: |
| 34 | + """An agent has ended.""" |
| 35 | + |
| 36 | + agent: RealtimeAgent |
| 37 | + """The agent that ended.""" |
| 38 | + |
| 39 | + info: RealtimeEventInfo |
| 40 | + """Common info for all events, such as the context.""" |
| 41 | + |
| 42 | + type: Literal["agent_end"] = "agent_end" |
| 43 | + |
| 44 | + |
| 45 | +@dataclass |
| 46 | +class RealtimeHandoffEvent: |
| 47 | + """An agent has handed off to another agent.""" |
| 48 | + |
| 49 | + from_agent: RealtimeAgent |
| 50 | + """The agent that handed off.""" |
| 51 | + |
| 52 | + to_agent: RealtimeAgent |
| 53 | + """The agent that was handed off to.""" |
| 54 | + |
| 55 | + info: RealtimeEventInfo |
| 56 | + """Common info for all events, such as the context.""" |
| 57 | + |
| 58 | + type: Literal["handoff"] = "handoff" |
| 59 | + |
| 60 | + |
| 61 | +@dataclass |
| 62 | +class RealtimeToolStart: |
| 63 | + """An agent is starting a tool call.""" |
| 64 | + |
| 65 | + agent: RealtimeAgent |
| 66 | + """The agent that updated.""" |
| 67 | + |
| 68 | + tool: Tool |
| 69 | + |
| 70 | + info: RealtimeEventInfo |
| 71 | + """Common info for all events, such as the context.""" |
| 72 | + |
| 73 | + type: Literal["tool_start"] = "tool_start" |
| 74 | + |
| 75 | + |
| 76 | +@dataclass |
| 77 | +class RealtimeToolEnd: |
| 78 | + """An agent has ended a tool call.""" |
| 79 | + |
| 80 | + agent: RealtimeAgent |
| 81 | + """The agent that ended the tool call.""" |
| 82 | + |
| 83 | + tool: Tool |
| 84 | + """The tool that was called.""" |
| 85 | + |
| 86 | + output: Any |
| 87 | + """The output of the tool call.""" |
| 88 | + |
| 89 | + info: RealtimeEventInfo |
| 90 | + """Common info for all events, such as the context.""" |
| 91 | + |
| 92 | + type: Literal["tool_end"] = "tool_end" |
| 93 | + |
| 94 | + |
| 95 | +@dataclass |
| 96 | +class RealtimeRawTransportEvent: |
| 97 | + """Forwards raw events from the transport layer.""" |
| 98 | + |
| 99 | + data: RealtimeTransportEvent |
| 100 | + """The raw data from the transport layer.""" |
| 101 | + |
| 102 | + info: RealtimeEventInfo |
| 103 | + """Common info for all events, such as the context.""" |
| 104 | + |
| 105 | + type: Literal["raw_transport_event"] = "raw_transport_event" |
| 106 | + |
| 107 | + |
| 108 | +@dataclass |
| 109 | +class RealtimeAudioEnd: |
| 110 | + """Triggered when the agent stops generating audio.""" |
| 111 | + |
| 112 | + info: RealtimeEventInfo |
| 113 | + """Common info for all events, such as the context.""" |
| 114 | + |
| 115 | + type: Literal["audio_end"] = "audio_end" |
| 116 | + |
| 117 | + |
| 118 | +@dataclass |
| 119 | +class RealtimeAudio: |
| 120 | + """Triggered when the agent generates new audio to be played.""" |
| 121 | + |
| 122 | + audio: RealtimeTransportAudioEvent |
| 123 | + """The audio event from the transport layer.""" |
| 124 | + |
| 125 | + info: RealtimeEventInfo |
| 126 | + """Common info for all events, such as the context.""" |
| 127 | + |
| 128 | + type: Literal["audio"] = "audio" |
| 129 | + |
| 130 | + |
| 131 | +@dataclass |
| 132 | +class RealtimeAudioInterrupted: |
| 133 | + """Triggered when the agent is interrupted. Can be listened to by the user to stop audio |
| 134 | + playback or give visual indicators to the user. |
| 135 | + """ |
| 136 | + |
| 137 | + info: RealtimeEventInfo |
| 138 | + """Common info for all events, such as the context.""" |
| 139 | + |
| 140 | + type: Literal["audio_interrupted"] = "audio_interrupted" |
| 141 | + |
| 142 | + |
| 143 | +@dataclass |
| 144 | +class RealtimeError: |
| 145 | + """An error has occurred.""" |
| 146 | + |
| 147 | + error: Any |
| 148 | + """The error that occurred.""" |
| 149 | + |
| 150 | + info: RealtimeEventInfo |
| 151 | + """Common info for all events, such as the context.""" |
| 152 | + |
| 153 | + type: Literal["error"] = "error" |
| 154 | + |
| 155 | + |
| 156 | +@dataclass |
| 157 | +class RealtimeHistoryUpdated: |
| 158 | + """The history has been updated. Contains the full history of the session.""" |
| 159 | + |
| 160 | + history: list[RealtimeItem] |
| 161 | + """The full history of the session.""" |
| 162 | + |
| 163 | + info: RealtimeEventInfo |
| 164 | + """Common info for all events, such as the context.""" |
| 165 | + |
| 166 | + type: Literal["history_updated"] = "history_updated" |
| 167 | + |
| 168 | + |
| 169 | +@dataclass |
| 170 | +class RealtimeHistoryAdded: |
| 171 | + """A new item has been added to the history.""" |
| 172 | + |
| 173 | + item: RealtimeItem |
| 174 | + """The new item that was added to the history.""" |
| 175 | + |
| 176 | + info: RealtimeEventInfo |
| 177 | + """Common info for all events, such as the context.""" |
| 178 | + |
| 179 | + type: Literal["history_added"] = "history_added" |
| 180 | + |
| 181 | + |
| 182 | +# TODO (rm) Add guardrails |
| 183 | + |
| 184 | +RealtimeSessionEvent: TypeAlias = Union[ |
| 185 | + RealtimeAgentStartEvent, |
| 186 | + RealtimeAgentEndEvent, |
| 187 | + RealtimeHandoffEvent, |
| 188 | + RealtimeToolStart, |
| 189 | + RealtimeToolEnd, |
| 190 | + RealtimeRawTransportEvent, |
| 191 | + RealtimeAudioEnd, |
| 192 | + RealtimeAudio, |
| 193 | + RealtimeAudioInterrupted, |
| 194 | + RealtimeError, |
| 195 | + RealtimeHistoryUpdated, |
| 196 | + RealtimeHistoryAdded, |
| 197 | +] |
| 198 | +"""An event emitted by the realtime session.""" |
0 commit comments