|
| 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