Skip to content

Commit b620166

Browse files
authored
Realtime: update model to have a single send_event method (#1111)
--- [//]: # (BEGIN SAPLING FOOTER) * #1112 * __->__ #1111
1 parent af4cef3 commit b620166

File tree

10 files changed

+295
-173
lines changed

10 files changed

+295
-173
lines changed

src/agents/realtime/config.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,17 @@
2626
"""The name of a realtime model."""
2727

2828

29+
RealtimeAudioFormat: TypeAlias = Union[Literal["pcm16", "g711_ulaw", "g711_alaw"], str]
30+
31+
2932
class RealtimeClientMessage(TypedDict):
33+
"""A raw message to be sent to the model."""
34+
3035
type: str # explicitly required
3136
other_data: NotRequired[dict[str, Any]]
3237
"""Merged into the message body."""
3338

3439

35-
class RealtimeUserInputText(TypedDict):
36-
type: Literal["input_text"]
37-
text: str
38-
39-
40-
class RealtimeUserInputMessage(TypedDict):
41-
type: Literal["message"]
42-
role: Literal["user"]
43-
content: list[RealtimeUserInputText]
44-
45-
46-
RealtimeUserInput: TypeAlias = Union[str, RealtimeUserInputMessage]
47-
48-
49-
RealtimeAudioFormat: TypeAlias = Union[Literal["pcm16", "g711_ulaw", "g711_alaw"], str]
50-
51-
5240
class RealtimeInputAudioTranscriptionConfig(TypedDict):
5341
language: NotRequired[str]
5442
model: NotRequired[Literal["gpt-4o-transcribe", "gpt-4o-mini-transcribe", "whisper-1"] | str]
@@ -124,3 +112,17 @@ class RealtimeRunConfig(TypedDict):
124112
"""Whether tracing is disabled for this run."""
125113

126114
# TODO (rm) Add history audio storage config
115+
116+
117+
class RealtimeUserInputText(TypedDict):
118+
type: Literal["input_text"]
119+
text: str
120+
121+
122+
class RealtimeUserInputMessage(TypedDict):
123+
type: Literal["message"]
124+
role: Literal["user"]
125+
content: list[RealtimeUserInputText]
126+
127+
128+
RealtimeUserInput: TypeAlias = Union[str, RealtimeUserInputMessage]

src/agents/realtime/model.py

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
from __future__ import annotations
22

33
import abc
4-
from typing import Any, Callable
4+
from typing import Callable
55

66
from typing_extensions import NotRequired, TypedDict
77

88
from ..util._types import MaybeAwaitable
99
from .config import (
10-
RealtimeClientMessage,
1110
RealtimeSessionModelSettings,
12-
RealtimeUserInput,
1311
)
14-
from .model_events import RealtimeModelEvent, RealtimeModelToolCallEvent
12+
from .model_events import RealtimeModelEvent
13+
from .model_inputs import RealtimeModelSendEvent
1514

1615

1716
class RealtimeModelListener(abc.ABC):
@@ -60,40 +59,10 @@ def remove_listener(self, listener: RealtimeModelListener) -> None:
6059
pass
6160

6261
@abc.abstractmethod
63-
async def send_event(self, event: RealtimeClientMessage) -> None:
62+
async def send_event(self, event: RealtimeModelSendEvent) -> None:
6463
"""Send an event to the model."""
6564
pass
6665

67-
@abc.abstractmethod
68-
async def send_message(
69-
self, message: RealtimeUserInput, other_event_data: dict[str, Any] | None = None
70-
) -> None:
71-
"""Send a message to the model."""
72-
pass
73-
74-
@abc.abstractmethod
75-
async def send_audio(self, audio: bytes, *, commit: bool = False) -> None:
76-
"""Send a raw audio chunk to the model.
77-
78-
Args:
79-
audio: The audio data to send.
80-
commit: Whether to commit the audio buffer to the model. If the model does not do turn
81-
detection, this can be used to indicate the turn is completed.
82-
"""
83-
pass
84-
85-
@abc.abstractmethod
86-
async def send_tool_output(
87-
self, tool_call: RealtimeModelToolCallEvent, output: str, start_response: bool
88-
) -> None:
89-
"""Send tool output to the model."""
90-
pass
91-
92-
@abc.abstractmethod
93-
async def interrupt(self) -> None:
94-
"""Interrupt the model. For example, could be triggered by a guardrail."""
95-
pass
96-
9766
@abc.abstractmethod
9867
async def close(self) -> None:
9968
"""Close the session."""

src/agents/realtime/model_events.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ class RealtimeModelInputAudioTranscriptionCompletedEvent:
6464
item_id: str
6565
transcript: str
6666

67-
type: Literal["conversation.item.input_audio_transcription.completed"] = (
68-
"conversation.item.input_audio_transcription.completed"
69-
)
67+
type: Literal["input_audio_transcription_completed"] = "input_audio_transcription_completed"
7068

7169

7270
@dataclass

src/agents/realtime/model_inputs.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from __future__ import annotations
2+
3+
from dataclasses import dataclass
4+
from typing import Any, Literal, Union
5+
6+
from typing_extensions import NotRequired, TypeAlias, TypedDict
7+
8+
from .model_events import RealtimeModelToolCallEvent
9+
10+
11+
class RealtimeModelRawClientMessage(TypedDict):
12+
"""A raw message to be sent to the model."""
13+
14+
type: str # explicitly required
15+
other_data: NotRequired[dict[str, Any]]
16+
"""Merged into the message body."""
17+
18+
19+
class RealtimeModelInputTextContent(TypedDict):
20+
"""A piece of text to be sent to the model."""
21+
22+
type: Literal["input_text"]
23+
text: str
24+
25+
26+
class RealtimeModelUserInputMessage(TypedDict):
27+
"""A message to be sent to the model."""
28+
29+
type: Literal["message"]
30+
role: Literal["user"]
31+
content: list[RealtimeModelInputTextContent]
32+
33+
34+
RealtimeModelUserInput: TypeAlias = Union[str, RealtimeModelUserInputMessage]
35+
"""A user input to be sent to the model."""
36+
37+
38+
# Model messages
39+
40+
41+
@dataclass
42+
class RealtimeModelSendRawMessage:
43+
"""Send a raw message to the model."""
44+
45+
message: RealtimeModelRawClientMessage
46+
"""The message to send."""
47+
48+
49+
@dataclass
50+
class RealtimeModelSendUserInput:
51+
"""Send a user input to the model."""
52+
53+
user_input: RealtimeModelUserInput
54+
"""The user input to send."""
55+
56+
57+
@dataclass
58+
class RealtimeModelSendAudio:
59+
"""Send audio to the model."""
60+
61+
audio: bytes
62+
commit: bool = False
63+
64+
65+
@dataclass
66+
class RealtimeModelSendToolOutput:
67+
"""Send tool output to the model."""
68+
69+
tool_call: RealtimeModelToolCallEvent
70+
"""The tool call to send."""
71+
72+
output: str
73+
"""The output to send."""
74+
75+
start_response: bool
76+
"""Whether to start a response."""
77+
78+
79+
@dataclass
80+
class RealtimeModelSendInterrupt:
81+
"""Send an interrupt to the model."""
82+
83+
84+
RealtimeModelSendEvent: TypeAlias = Union[
85+
RealtimeModelSendRawMessage,
86+
RealtimeModelSendUserInput,
87+
RealtimeModelSendAudio,
88+
RealtimeModelSendToolOutput,
89+
RealtimeModelSendInterrupt,
90+
]

0 commit comments

Comments
 (0)