237 lines
6.7 KiB
Python
237 lines
6.7 KiB
Python
"""Public Pydantic and JSON Schema contract for OpenAI Realtime subset v1."""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Annotated, Any, Literal, get_args
|
||
|
|
|
||
|
|
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter
|
||
|
|
|
||
|
|
|
||
|
|
class _ExtensibleModel(BaseModel):
|
||
|
|
model_config = ConfigDict(extra="allow")
|
||
|
|
|
||
|
|
|
||
|
|
class RealtimeSessionRequest(_ExtensibleModel):
|
||
|
|
type: Literal["realtime"] | None = None
|
||
|
|
model: str
|
||
|
|
output_modalities: list[Literal["audio", "text"]] | None = None
|
||
|
|
audio: dict[str, Any] | None = None
|
||
|
|
turn_detection: dict[str, Any] | None = None
|
||
|
|
instructions: str | None = Field(
|
||
|
|
default=None,
|
||
|
|
description="Platform-owned in subset v1; requests containing it are rejected.",
|
||
|
|
)
|
||
|
|
voice: str | None = Field(
|
||
|
|
default=None,
|
||
|
|
description="Platform-owned in subset v1; requests containing it are rejected.",
|
||
|
|
)
|
||
|
|
tools: list[dict[str, Any]] | None = Field(
|
||
|
|
default=None,
|
||
|
|
description="Platform-owned in subset v1; requests containing it are rejected.",
|
||
|
|
)
|
||
|
|
tool_choice: Any | None = Field(
|
||
|
|
default=None,
|
||
|
|
description="Platform-owned in subset v1; requests containing it are rejected.",
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class RealtimeClientSecretRequest(_ExtensibleModel):
|
||
|
|
session: RealtimeSessionRequest
|
||
|
|
|
||
|
|
|
||
|
|
RealtimeClientSecretBody = RealtimeClientSecretRequest | RealtimeSessionRequest
|
||
|
|
|
||
|
|
|
||
|
|
class RealtimeClientSecretResponse(BaseModel):
|
||
|
|
model_config = ConfigDict(
|
||
|
|
extra="forbid",
|
||
|
|
json_schema_extra={
|
||
|
|
"examples": [
|
||
|
|
{
|
||
|
|
"value": "ek-rt-...",
|
||
|
|
"expires_at": 1788739260,
|
||
|
|
"session": {
|
||
|
|
"type": "realtime",
|
||
|
|
"model": "assistant:asst_xxx",
|
||
|
|
"output_modalities": ["audio"],
|
||
|
|
},
|
||
|
|
}
|
||
|
|
]
|
||
|
|
},
|
||
|
|
)
|
||
|
|
|
||
|
|
value: str
|
||
|
|
expires_at: int
|
||
|
|
session: dict[str, Any]
|
||
|
|
|
||
|
|
|
||
|
|
class _ClientEvent(_ExtensibleModel):
|
||
|
|
event_id: str | None = None
|
||
|
|
|
||
|
|
|
||
|
|
class SessionUpdateEvent(_ClientEvent):
|
||
|
|
type: Literal["session.update"]
|
||
|
|
session: dict[str, Any]
|
||
|
|
|
||
|
|
|
||
|
|
class ConversationItemCreateEvent(_ClientEvent):
|
||
|
|
type: Literal["conversation.item.create"]
|
||
|
|
item: dict[str, Any]
|
||
|
|
previous_item_id: str | None = None
|
||
|
|
|
||
|
|
|
||
|
|
class ConversationItemTruncateEvent(_ClientEvent):
|
||
|
|
type: Literal["conversation.item.truncate"]
|
||
|
|
item_id: str
|
||
|
|
content_index: int = Field(default=0, ge=0)
|
||
|
|
audio_end_ms: int = Field(ge=0)
|
||
|
|
|
||
|
|
|
||
|
|
class InputAudioBufferAppendEvent(_ClientEvent):
|
||
|
|
type: Literal["input_audio_buffer.append"]
|
||
|
|
audio: str
|
||
|
|
|
||
|
|
|
||
|
|
class InputAudioBufferCommitEvent(_ClientEvent):
|
||
|
|
type: Literal["input_audio_buffer.commit"]
|
||
|
|
|
||
|
|
|
||
|
|
class InputAudioBufferClearEvent(_ClientEvent):
|
||
|
|
type: Literal["input_audio_buffer.clear"]
|
||
|
|
|
||
|
|
|
||
|
|
class OutputAudioBufferClearEvent(_ClientEvent):
|
||
|
|
type: Literal["output_audio_buffer.clear"]
|
||
|
|
|
||
|
|
|
||
|
|
class ResponseCreateEvent(_ClientEvent):
|
||
|
|
type: Literal["response.create"]
|
||
|
|
response: dict[str, Any] | None = None
|
||
|
|
|
||
|
|
|
||
|
|
class ResponseCancelEvent(_ClientEvent):
|
||
|
|
type: Literal["response.cancel"]
|
||
|
|
response_id: str | None = None
|
||
|
|
|
||
|
|
|
||
|
|
class CapabilitiesUpdateEvent(_ClientEvent):
|
||
|
|
type: Literal["x.interactive_media.capabilities.update"]
|
||
|
|
version: Literal["1"]
|
||
|
|
capabilities: list[str]
|
||
|
|
|
||
|
|
|
||
|
|
class SessionVariablesUpdateEvent(_ClientEvent):
|
||
|
|
type: Literal["x.interactive_media.session.variables.update"]
|
||
|
|
version: Literal["1"]
|
||
|
|
variables: dict[str, Any]
|
||
|
|
|
||
|
|
|
||
|
|
RealtimeClientEvent = Annotated[
|
||
|
|
SessionUpdateEvent
|
||
|
|
| ConversationItemCreateEvent
|
||
|
|
| ConversationItemTruncateEvent
|
||
|
|
| InputAudioBufferAppendEvent
|
||
|
|
| InputAudioBufferCommitEvent
|
||
|
|
| InputAudioBufferClearEvent
|
||
|
|
| OutputAudioBufferClearEvent
|
||
|
|
| ResponseCreateEvent
|
||
|
|
| ResponseCancelEvent
|
||
|
|
| CapabilitiesUpdateEvent
|
||
|
|
| SessionVariablesUpdateEvent,
|
||
|
|
Field(discriminator="type"),
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
REALTIME_CLIENT_EVENT_ADAPTER = TypeAdapter(RealtimeClientEvent)
|
||
|
|
|
||
|
|
|
||
|
|
RealtimeServerEventType = Literal[
|
||
|
|
"session.created",
|
||
|
|
"session.updated",
|
||
|
|
"conversation.item.added",
|
||
|
|
"conversation.item.done",
|
||
|
|
"conversation.item.truncated",
|
||
|
|
"conversation.item.input_audio_transcription.completed",
|
||
|
|
"input_audio_buffer.committed",
|
||
|
|
"input_audio_buffer.cleared",
|
||
|
|
"input_audio_buffer.speech_started",
|
||
|
|
"input_audio_buffer.speech_stopped",
|
||
|
|
"output_audio_buffer.cleared",
|
||
|
|
"response.created",
|
||
|
|
"response.output_item.added",
|
||
|
|
"response.output_item.done",
|
||
|
|
"response.content_part.added",
|
||
|
|
"response.content_part.done",
|
||
|
|
"response.output_text.delta",
|
||
|
|
"response.output_text.done",
|
||
|
|
"response.output_audio.delta",
|
||
|
|
"response.output_audio.done",
|
||
|
|
"response.output_audio_transcript.delta",
|
||
|
|
"response.output_audio_transcript.done",
|
||
|
|
"response.function_call_arguments.delta",
|
||
|
|
"response.function_call_arguments.done",
|
||
|
|
"response.done",
|
||
|
|
"error",
|
||
|
|
"x.interactive_media.capabilities.updated",
|
||
|
|
"x.interactive_media.session.variables.updated",
|
||
|
|
"x.interactive_media.workflow.node_active",
|
||
|
|
"x.interactive_media.workflow.event",
|
||
|
|
"x.interactive_media.workflow.variables.updated",
|
||
|
|
"x.interactive_media.workflow.error",
|
||
|
|
"x.interactive_media.call.handoff_requested",
|
||
|
|
"x.interactive_media.call.ended",
|
||
|
|
]
|
||
|
|
SERVER_EVENT_TYPES = get_args(RealtimeServerEventType)
|
||
|
|
EXTENSION_SERVER_EVENT_TYPES = tuple(
|
||
|
|
event_type
|
||
|
|
for event_type in SERVER_EVENT_TYPES
|
||
|
|
if event_type.startswith("x.interactive_media.")
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class RealtimeServerEvent(_ExtensibleModel):
|
||
|
|
type: RealtimeServerEventType
|
||
|
|
event_id: str
|
||
|
|
|
||
|
|
|
||
|
|
def realtime_client_event_json_schema() -> dict[str, Any]:
|
||
|
|
schema = REALTIME_CLIENT_EVENT_ADAPTER.json_schema(
|
||
|
|
ref_template="#/$defs/{model}",
|
||
|
|
)
|
||
|
|
schema.update(
|
||
|
|
{
|
||
|
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||
|
|
"$id": "https://interactive-media.local/schemas/realtime-client-events-v1.json",
|
||
|
|
"title": "Interactive Media OpenAI Realtime client events v1",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
return schema
|
||
|
|
|
||
|
|
|
||
|
|
def realtime_server_event_json_schema() -> dict[str, Any]:
|
||
|
|
schema = RealtimeServerEvent.model_json_schema()
|
||
|
|
schema.update(
|
||
|
|
{
|
||
|
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||
|
|
"$id": "https://interactive-media.local/schemas/realtime-server-events-v1.json",
|
||
|
|
"title": "Interactive Media OpenAI Realtime server events v1",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
schema["allOf"] = [
|
||
|
|
{
|
||
|
|
"if": {
|
||
|
|
"properties": {
|
||
|
|
"type": {"enum": list(EXTENSION_SERVER_EVENT_TYPES)},
|
||
|
|
},
|
||
|
|
"required": ["type"],
|
||
|
|
},
|
||
|
|
"then": {
|
||
|
|
"properties": {
|
||
|
|
"version": {"const": "1", "type": "string"},
|
||
|
|
},
|
||
|
|
"required": ["version"],
|
||
|
|
},
|
||
|
|
}
|
||
|
|
]
|
||
|
|
return schema
|