api_openai/components/realtime_shared/
response.rs

1//! Response handling and streaming structures for the Realtime API.
2
3/// Define a private namespace for response-related items.
4mod private
5{
6  // Use full paths from crate root for components
7  use crate::components::common::{ Metadata, VoiceIdsShared, Error };
8  use crate::components::tools::{ Tool };
9  use crate::components::realtime_shared::conversation::
10  {
11    RealtimeConversationItem,
12    RealtimeConversationItemWithReference,
13  };
14
15  // Serde imports
16  use serde::{ Serialize, Deserialize };
17  use serde_json::Value; // Keep if needed for Value type
18
19  /// Represents details about the status of a Realtime response (e.g., why it failed or was incomplete).
20  ///
21  /// # Used By
22  /// - `RealtimeResponse`
23  #[ derive( Debug, Serialize, Deserialize, Clone, PartialEq ) ] // Added Serialize
24  pub struct RealtimeResponseStatusDetails
25  {
26    /// The type corresponding to the `status` field (`completed`, `cancelled`, `incomplete`, `failed`).
27    pub r#type : String,
28    /// The reason the Response did not complete (e.g., `turn_detected`, `max_output_tokens`).
29    #[ serde( skip_serializing_if = "Option::is_none" ) ]
30    pub reason : Option< String >,
31    /// Details of the error if the status is `failed`.
32    #[ serde( skip_serializing_if = "Option::is_none" ) ]
33    pub error : Option< Error >,
34  }
35
36  /// Detailed breakdown of input tokens used in a Realtime response.
37  ///
38  /// # Used By
39  /// - `RealtimeResponseUsage`
40  #[ derive( Debug, Serialize, Deserialize, Clone, PartialEq ) ] // Added Serialize
41  pub struct RealtimeResponseInputTokenDetails
42  {
43    /// The number of cached tokens used.
44    #[ serde( skip_serializing_if = "Option::is_none" ) ]
45    pub cached_tokens : Option< i32 >,
46    /// The number of text tokens used.
47    #[ serde( skip_serializing_if = "Option::is_none" ) ]
48    pub text_tokens : Option< i32 >,
49    /// The number of audio tokens used.
50    #[ serde( skip_serializing_if = "Option::is_none" ) ]
51    pub audio_tokens : Option< i32 >,
52  }
53
54  /// Detailed breakdown of output tokens used in a Realtime response.
55  ///
56  /// # Used By
57  /// - `RealtimeResponseUsage`
58  #[ derive( Debug, Serialize, Deserialize, Clone, PartialEq ) ] // Added Serialize
59  pub struct RealtimeResponseOutputTokenDetails
60  {
61    /// The number of text tokens used.
62    #[ serde( skip_serializing_if = "Option::is_none" ) ]
63    pub text_tokens : Option< i32 >,
64    /// The number of audio tokens used.
65    #[ serde( skip_serializing_if = "Option::is_none" ) ]
66    pub audio_tokens : Option< i32 >,
67  }
68
69  /// Usage statistics for a Realtime response, corresponding to billing.
70  ///
71  /// # Used By
72  /// - `RealtimeResponse`
73  #[ derive( Debug, Serialize, Deserialize, Clone, PartialEq ) ] // Added Serialize
74  pub struct RealtimeResponseUsage
75  {
76    /// The total number of tokens in the Response.
77    #[ serde( skip_serializing_if = "Option::is_none" ) ]
78    pub total_tokens : Option< i32 >,
79    /// The number of input tokens used.
80    #[ serde( skip_serializing_if = "Option::is_none" ) ]
81    pub input_tokens : Option< i32 >,
82    /// The number of output tokens sent.
83    #[ serde( skip_serializing_if = "Option::is_none" ) ]
84    pub output_tokens : Option< i32 >,
85    /// Details about the input tokens used.
86    #[ serde( skip_serializing_if = "Option::is_none" ) ]
87    pub input_token_details : Option< RealtimeResponseInputTokenDetails >,
88    /// Details about the output tokens used.
89    #[ serde( skip_serializing_if = "Option::is_none" ) ]
90    pub output_token_details : Option< RealtimeResponseOutputTokenDetails >,
91  }
92
93  /// Represents the response resource from the Realtime API.
94  ///
95  /// # Used By
96  /// - `RealtimeServerEventResponseCreated`
97  /// - `RealtimeServerEventResponseDone`
98  #[ derive( Debug, Serialize, Deserialize, Clone, PartialEq ) ] // Added Serialize
99  pub struct RealtimeResponse
100  {
101    /// The unique ID of the response.
102    pub id : String,
103    /// The object type, must be `realtime.response`.
104    pub object : String,
105    /// The final status of the response (`completed`, `cancelled`, `failed`, or `incomplete`).
106    pub status : String,
107    /// Additional details about the status.
108    #[ serde( skip_serializing_if = "Option::is_none" ) ]
109    pub status_details : Option< RealtimeResponseStatusDetails >,
110    /// The list of output items generated by the response.
111    pub output : Vec< RealtimeConversationItem >,
112    /// Metadata associated with the response.
113    #[ serde( skip_serializing_if = "Option::is_none" ) ]
114    pub metadata : Option< Metadata >,
115    /// Usage statistics for the Response.
116    #[ serde( skip_serializing_if = "Option::is_none" ) ]
117    pub usage : Option< RealtimeResponseUsage >,
118    /// Which conversation the response is added to (`auto`, `none`, or `conv_123`).
119    #[ serde( skip_serializing_if = "Option::is_none" ) ]
120    pub conversation_id : Option< String >,
121    /// The voice the model used to respond.
122    #[ serde( skip_serializing_if = "Option::is_none" ) ]
123    pub voice : Option< VoiceIdsShared >,
124    /// The set of modalities the model used to respond.
125    #[ serde( skip_serializing_if = "Option::is_none" ) ]
126    pub modalities : Option< Vec< String > >,
127    /// The format of output audio.
128    #[ serde( skip_serializing_if = "Option::is_none" ) ]
129    pub output_audio_format : Option< String >,
130    /// Sampling temperature used for this response.
131    #[ serde( skip_serializing_if = "Option::is_none" ) ]
132    pub temperature : Option< f64 >,
133    /// Maximum number of output tokens used in this response.
134    #[ serde( skip_serializing_if = "Option::is_none" ) ]
135    pub max_response_output_tokens : Option< Value >, // Can be integer or "inf"
136  }
137
138  /// Parameters for creating a new Realtime response via the client event.
139  ///
140  /// # Used By
141  /// - `RealtimeClientEventResponseCreate`
142  #[ derive( Debug, Serialize, Deserialize, Clone, PartialEq, former::Former ) ] // Added Serialize
143  pub struct RealtimeResponseCreateParams
144  {
145    /// The set of modalities the model can respond with.
146    #[ serde( skip_serializing_if = "Option::is_none" ) ]
147    pub modalities : Option< Vec< String > >,
148    /// System instructions override for this response.
149    #[ serde( skip_serializing_if = "Option::is_none" ) ]
150    pub instructions : Option< String >,
151    /// Voice override for this response.
152    #[ serde( skip_serializing_if = "Option::is_none" ) ]
153    pub voice : Option< VoiceIdsShared >,
154    /// Output audio format override for this response.
155    #[ serde( skip_serializing_if = "Option::is_none" ) ]
156    pub output_audio_format : Option< String >,
157    /// Tools override for this response.
158    #[ serde( skip_serializing_if = "Option::is_none" ) ]
159    pub tools : Option< Vec< Tool > >,
160    /// Tool choice override for this response.
161    #[ serde( skip_serializing_if = "Option::is_none" ) ]
162    pub tool_choice : Option< String >, // Should ideally be ToolChoice enum if possible
163    /// Temperature override for this response.
164    #[ serde( skip_serializing_if = "Option::is_none" ) ]
165    pub temperature : Option< f64 >,
166    /// Max output tokens override for this response.
167    #[ serde( skip_serializing_if = "Option::is_none" ) ]
168    pub max_response_output_tokens : Option< Value >, // Can be integer or "inf"
169    /// Conversation context override ("auto" or "none").
170    #[ serde( skip_serializing_if = "Option::is_none" ) ]
171    pub conversation : Option< String >,
172    /// Metadata override for this response.
173    #[ serde( skip_serializing_if = "Option::is_none" ) ]
174    pub metadata : Option< Metadata >,
175    /// Input items override for this response's context.
176    #[ serde( skip_serializing_if = "Option::is_none" ) ]
177    pub input : Option< Vec< RealtimeConversationItemWithReference > >,
178  }
179
180} // end mod private
181
182crate ::mod_interface!
183{
184  exposed use
185  {
186    RealtimeResponseStatusDetails,
187    RealtimeResponseInputTokenDetails,
188    RealtimeResponseOutputTokenDetails,
189    RealtimeResponseUsage,
190    RealtimeResponse,
191    RealtimeResponseCreateParams,
192  };
193}