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

Skip to content

Commit 3e0122c

Browse files
authored
[6/n] RealtimeSession + events (openai#1073)
RealtimeSession is like Runner, except for Realtime. It's also stateful and manages the conversation for you. --- [//]: # (BEGIN SAPLING FOOTER) * openai#1076 * openai#1074 * __->__ openai#1073 * openai#1072 * openai#1071
1 parent 8b7d862 commit 3e0122c

File tree

5 files changed

+621
-0
lines changed

5 files changed

+621
-0
lines changed

src/agents/realtime/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Realtime
2+
3+
Realtime agents are in beta: expect some breaking changes over the next few weeks as we find issues and fix them.

src/agents/realtime/__init__.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from .agent import RealtimeAgent, RealtimeAgentHooks, RealtimeRunHooks
2+
from .config import APIKeyOrKeyFunc
3+
from .events import (
4+
RealtimeAgentEndEvent,
5+
RealtimeAgentStartEvent,
6+
RealtimeAudio,
7+
RealtimeAudioEnd,
8+
RealtimeAudioInterrupted,
9+
RealtimeError,
10+
RealtimeHandoffEvent,
11+
RealtimeHistoryAdded,
12+
RealtimeHistoryUpdated,
13+
RealtimeRawTransportEvent,
14+
RealtimeSessionEvent,
15+
RealtimeToolEnd,
16+
RealtimeToolStart,
17+
)
18+
from .session import RealtimeSession
19+
from .transport import (
20+
RealtimeModelName,
21+
RealtimeSessionTransport,
22+
RealtimeTransportConnectionOptions,
23+
RealtimeTransportListener,
24+
)
25+
26+
__all__ = [
27+
"RealtimeAgent",
28+
"RealtimeAgentHooks",
29+
"RealtimeRunHooks",
30+
"RealtimeSession",
31+
"RealtimeSessionListener",
32+
"RealtimeSessionListenerFunc",
33+
"APIKeyOrKeyFunc",
34+
"RealtimeModelName",
35+
"RealtimeSessionTransport",
36+
"RealtimeTransportListener",
37+
"RealtimeTransportConnectionOptions",
38+
"RealtimeSessionEvent",
39+
"RealtimeAgentStartEvent",
40+
"RealtimeAgentEndEvent",
41+
"RealtimeHandoffEvent",
42+
"RealtimeToolStart",
43+
"RealtimeToolEnd",
44+
"RealtimeRawTransportEvent",
45+
"RealtimeAudioEnd",
46+
"RealtimeAudio",
47+
"RealtimeAudioInterrupted",
48+
"RealtimeError",
49+
"RealtimeHistoryUpdated",
50+
"RealtimeHistoryAdded",
51+
]

src/agents/realtime/events.py

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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

Comments
 (0)