Skip to main content

openai_tools/realtime/
conversation.rs

1//! Conversation item types for the Realtime API.
2
3use serde::{Deserialize, Serialize};
4
5/// A conversation item in the Realtime API.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(tag = "type", rename_all = "snake_case")]
8pub enum ConversationItem {
9    /// A message item (user, assistant, or system).
10    Message(MessageItem),
11    /// A function call made by the assistant.
12    FunctionCall(FunctionCallItem),
13    /// The output of a function call.
14    FunctionCallOutput(FunctionCallOutputItem),
15}
16
17/// A message in the conversation.
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct MessageItem {
20    /// Unique identifier for this item.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub id: Option<String>,
23
24    /// Role of the message sender.
25    pub role: MessageRole,
26
27    /// Content parts of the message.
28    pub content: Vec<ContentPart>,
29}
30
31impl MessageItem {
32    /// Create a new user text message.
33    pub fn user_text(text: impl Into<String>) -> Self {
34        Self { id: None, role: MessageRole::User, content: vec![ContentPart::InputText { text: text.into() }] }
35    }
36
37    /// Create a new assistant text message.
38    pub fn assistant_text(text: impl Into<String>) -> Self {
39        Self { id: None, role: MessageRole::Assistant, content: vec![ContentPart::Text { text: text.into() }] }
40    }
41
42    /// Create a new system message.
43    pub fn system(text: impl Into<String>) -> Self {
44        Self { id: None, role: MessageRole::System, content: vec![ContentPart::InputText { text: text.into() }] }
45    }
46
47    /// Create a new user audio message.
48    pub fn user_audio(audio_base64: impl Into<String>) -> Self {
49        Self { id: None, role: MessageRole::User, content: vec![ContentPart::InputAudio { audio: audio_base64.into(), transcript: None }] }
50    }
51}
52
53/// Role of a message sender.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
55#[serde(rename_all = "lowercase")]
56pub enum MessageRole {
57    /// System message (instructions).
58    System,
59    /// User message.
60    User,
61    /// Assistant message.
62    Assistant,
63}
64
65/// A content part within a message.
66#[derive(Debug, Clone, Serialize, Deserialize)]
67#[serde(tag = "type", rename_all = "snake_case")]
68pub enum ContentPart {
69    /// Text input from user.
70    InputText { text: String },
71    /// Audio input from user (base64 encoded).
72    InputAudio {
73        audio: String,
74        #[serde(skip_serializing_if = "Option::is_none")]
75        transcript: Option<String>,
76    },
77    /// Text output from assistant.
78    Text { text: String },
79    /// Audio output from assistant.
80    Audio {
81        #[serde(skip_serializing_if = "Option::is_none")]
82        audio: Option<String>,
83        #[serde(skip_serializing_if = "Option::is_none")]
84        transcript: Option<String>,
85    },
86    /// Reference to another item.
87    ItemReference { id: String },
88}
89
90impl ContentPart {
91    /// Create a text input content part.
92    pub fn input_text(text: impl Into<String>) -> Self {
93        Self::InputText { text: text.into() }
94    }
95
96    /// Create an audio input content part.
97    pub fn input_audio(audio_base64: impl Into<String>) -> Self {
98        Self::InputAudio { audio: audio_base64.into(), transcript: None }
99    }
100
101    /// Create a text output content part.
102    pub fn text(text: impl Into<String>) -> Self {
103        Self::Text { text: text.into() }
104    }
105}
106
107/// A function call made by the assistant.
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct FunctionCallItem {
110    /// Unique identifier for this item.
111    #[serde(skip_serializing_if = "Option::is_none")]
112    pub id: Option<String>,
113
114    /// The function call ID (used to match with output).
115    pub call_id: String,
116
117    /// Name of the function being called.
118    pub name: String,
119
120    /// JSON string of function arguments.
121    pub arguments: String,
122}
123
124/// The output of a function call.
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct FunctionCallOutputItem {
127    /// Unique identifier for this item.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub id: Option<String>,
130
131    /// The function call ID this is responding to.
132    pub call_id: String,
133
134    /// The output value (usually JSON string).
135    pub output: String,
136}
137
138impl FunctionCallOutputItem {
139    /// Create a new function call output.
140    pub fn new(call_id: impl Into<String>, output: impl Into<String>) -> Self {
141        Self { id: None, call_id: call_id.into(), output: output.into() }
142    }
143}
144
145/// Item status in the conversation.
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
147#[serde(rename_all = "snake_case")]
148pub enum ItemStatus {
149    /// Item is being processed.
150    InProgress,
151    /// Item processing completed.
152    Completed,
153    /// Item processing was incomplete.
154    Incomplete,
155}