openai-tools 3.0.0

Tools for OpenAI API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! OpenAI Videos API Request Module
//!
//! This module provides the functionality to interact with the OpenAI Videos API
//! (`/v1/videos`) for generating video clips with the Sora models.
//!
//! # Key Features
//!
//! - **Create**: Queue a generation job from a text prompt
//! - **Retrieve**: Poll a job for status and progress
//! - **List**: Page through recently generated videos
//! - **Delete**: Remove a video and its assets
//! - **Content**: Download the rendered video, thumbnail or spritesheet
//! - **Remix**: Re-generate an existing video with an updated prompt
//!
//! # Quick Start
//!
//! ```rust,no_run
//! use openai_tools::videos::request::{Videos, CreateVideoOptions};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let videos = Videos::new()?;
//!
//!     let job = videos.create("A red balloon over Tokyo", CreateVideoOptions::default()).await?;
//!     println!("Queued: {}", job.id);
//!
//!     Ok(())
//! }
//! ```

use crate::common::auth::{AuthProvider, OpenAIAuth};
use crate::common::client::create_http_client;
use crate::common::errors::{ErrorResponse, OpenAIToolError, Result};
use crate::videos::response::{DeleteVideoResponse, Video, VideoListResponse};
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Default API path for Videos
const VIDEOS_PATH: &str = "videos";

/// Video generation models.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[non_exhaustive]
pub enum VideoModel {
    /// Sora 2 - standard video generation model
    #[serde(rename = "sora-2")]
    #[default]
    Sora2,
    /// Sora 2 Pro - higher quality, more expensive
    #[serde(rename = "sora-2-pro")]
    Sora2Pro,
}

impl VideoModel {
    /// Returns the model identifier string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Sora2 => "sora-2",
            Self::Sora2Pro => "sora-2-pro",
        }
    }
}

impl std::fmt::Display for VideoModel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Output resolutions supported by the Videos API.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[non_exhaustive]
pub enum VideoSize {
    /// 720x1280 - portrait (default)
    #[serde(rename = "720x1280")]
    #[default]
    Size720x1280,
    /// 1280x720 - landscape
    #[serde(rename = "1280x720")]
    Size1280x720,
    /// 1024x1792 - tall portrait
    #[serde(rename = "1024x1792")]
    Size1024x1792,
    /// 1792x1024 - wide landscape
    #[serde(rename = "1792x1024")]
    Size1792x1024,
}

impl VideoSize {
    /// Returns the size string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Size720x1280 => "720x1280",
            Self::Size1280x720 => "1280x720",
            Self::Size1024x1792 => "1024x1792",
            Self::Size1792x1024 => "1792x1024",
        }
    }
}

impl std::fmt::Display for VideoSize {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Clip durations supported by the Videos API.
///
/// The API requires this value as a *string*; sending an integer is rejected
/// with `invalid_type`. The `serde` rename below produces the string form.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[non_exhaustive]
pub enum VideoSeconds {
    /// 4 second clip (default)
    #[serde(rename = "4")]
    #[default]
    Four,
    /// 8 second clip
    #[serde(rename = "8")]
    Eight,
    /// 12 second clip
    #[serde(rename = "12")]
    Twelve,
}

impl VideoSeconds {
    /// Returns the duration string as the API expects it.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Four => "4",
            Self::Eight => "8",
            Self::Twelve => "12",
        }
    }
}

impl std::fmt::Display for VideoSeconds {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Downloadable asset variants for a completed video.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum VideoVariant {
    /// The rendered video file (default)
    #[default]
    Video,
    /// A single still frame
    Thumbnail,
    /// A grid of frames
    Spritesheet,
}

impl VideoVariant {
    /// Returns the variant string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Video => "video",
            Self::Thumbnail => "thumbnail",
            Self::Spritesheet => "spritesheet",
        }
    }
}

impl std::fmt::Display for VideoVariant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// A reference image guiding generation.
///
/// Supply either an uploaded file or an image URL - never both.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct InputReference {
    /// ID of a file uploaded via the Files API
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_id: Option<String>,
    /// A fully qualified URL, or a base64-encoded data URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_url: Option<String>,
}

impl InputReference {
    /// Builds a reference from an uploaded file ID.
    pub fn file_id<S: Into<String>>(file_id: S) -> Self {
        Self { file_id: Some(file_id.into()), image_url: None }
    }

    /// Builds a reference from an image URL or data URL.
    pub fn image_url<S: Into<String>>(image_url: S) -> Self {
        Self { file_id: None, image_url: Some(image_url.into()) }
    }
}

/// Optional settings for [`Videos::create`].
///
/// Unset fields are omitted from the request so the API applies its own
/// defaults (`sora-2`, `720x1280`, 4 seconds).
#[derive(Debug, Clone, Default)]
pub struct CreateVideoOptions {
    /// The generation model
    pub model: Option<VideoModel>,
    /// Clip duration
    pub seconds: Option<VideoSeconds>,
    /// Output resolution
    pub size: Option<VideoSize>,
    /// Reference image guiding generation
    pub input_reference: Option<InputReference>,
}

impl CreateVideoOptions {
    /// Combines these options with a prompt into a serializable request body.
    pub fn into_request<S: Into<String>>(self, prompt: S) -> CreateVideoRequest {
        CreateVideoRequest { prompt: prompt.into(), model: self.model, seconds: self.seconds, size: self.size, input_reference: self.input_reference }
    }
}

/// Request body for `POST /v1/videos`.
#[derive(Debug, Clone, Serialize)]
pub struct CreateVideoRequest {
    /// Describes the video to generate
    pub prompt: String,
    /// The generation model
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<VideoModel>,
    /// Clip duration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seconds: Option<VideoSeconds>,
    /// Output resolution
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<VideoSize>,
    /// Reference image guiding generation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_reference: Option<InputReference>,
}

/// Request body for `POST /v1/videos/{video_id}/remix`.
#[derive(Debug, Clone, Serialize)]
struct RemixVideoRequest {
    prompt: String,
}

/// Sort order for [`Videos::list`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SortOrder {
    /// Oldest first
    Asc,
    /// Newest first (default)
    #[default]
    Desc,
}

impl SortOrder {
    /// Returns the order string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Asc => "asc",
            Self::Desc => "desc",
        }
    }
}

/// OpenAI Videos API client.
///
/// Video generation is a long-running job: [`create`](Videos::create) queues
/// the work and returns immediately, so poll [`retrieve`](Videos::retrieve)
/// until the job settles before downloading with [`content`](Videos::content).
///
/// # Example
///
/// ```rust,no_run
/// use openai_tools::videos::request::{Videos, CreateVideoOptions, VideoSize};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let videos = Videos::new()?;
///
///     let options = CreateVideoOptions { size: Some(VideoSize::Size1280x720), ..Default::default() };
///     let job = videos.create("A red balloon over Tokyo", options).await?;
///     println!("Queued: {}", job.id);
///
///     Ok(())
/// }
/// ```
pub struct Videos {
    /// Authentication provider (OpenAI or Azure)
    auth: AuthProvider,
    /// Optional request timeout duration
    timeout: Option<Duration>,
}

impl Videos {
    /// Creates a new Videos client from the `OPENAI_API_KEY` environment
    /// variable.
    pub fn new() -> Result<Self> {
        let auth = AuthProvider::openai_from_env()?;
        Ok(Self { auth, timeout: None })
    }

    /// Creates a new Videos client with a custom authentication provider.
    pub fn with_auth(auth: AuthProvider) -> Self {
        Self { auth, timeout: None }
    }

    /// Creates a new Videos client for Azure OpenAI API.
    pub fn azure() -> Result<Self> {
        let auth = AuthProvider::azure_from_env()?;
        Ok(Self { auth, timeout: None })
    }

    /// Creates a new Videos client by auto-detecting the provider.
    pub fn detect_provider() -> Result<Self> {
        let auth = AuthProvider::from_env()?;
        Ok(Self { auth, timeout: None })
    }

    /// Creates a new Videos client with URL-based provider detection.
    pub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self {
        let auth = AuthProvider::from_url_with_key(base_url, api_key);
        Self { auth, timeout: None }
    }

    /// Creates a new Videos client from a URL using environment credentials.
    pub fn from_url<S: Into<String>>(url: S) -> Result<Self> {
        let auth = AuthProvider::from_url(url)?;
        Ok(Self { auth, timeout: None })
    }

    /// Returns the authentication provider.
    pub fn auth(&self) -> &AuthProvider {
        &self.auth
    }

    /// Sets a custom API endpoint URL (OpenAI only).
    pub fn base_url<T: AsRef<str>>(&mut self, url: T) -> &mut Self {
        if let AuthProvider::OpenAI(ref openai_auth) = self.auth {
            let new_auth = OpenAIAuth::new(openai_auth.api_key()).with_base_url(url.as_ref());
            self.auth = AuthProvider::OpenAI(new_auth);
        } else {
            tracing::warn!("base_url() is only supported for OpenAI provider. Use azure() or with_auth() for Azure.");
        }
        self
    }

    /// Sets the request timeout duration.
    ///
    /// Generation jobs are queued asynchronously, so the default (no timeout)
    /// is usually fine; a longer timeout mainly matters for
    /// [`content`](Videos::content) downloads.
    pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
        self.timeout = Some(timeout);
        self
    }

    /// Creates the HTTP client with default headers.
    fn create_client(&self) -> Result<(request::Client, request::header::HeaderMap)> {
        let client = create_http_client(self.timeout)?;
        let mut headers = request::header::HeaderMap::new();
        self.auth.apply_headers(&mut headers)?;
        headers.insert("Content-Type", request::header::HeaderValue::from_static("application/json"));
        headers.insert("User-Agent", request::header::HeaderValue::from_static("openai-tools-rust"));
        Ok((client, headers))
    }

    /// Reads a response body, turning API errors into [`OpenAIToolError`].
    async fn read_json<T: serde::de::DeserializeOwned>(response: request::Response) -> Result<T> {
        let status = response.status();
        let content = response.text().await.map_err(OpenAIToolError::RequestError)?;

        if cfg!(test) {
            tracing::info!("Response content: {}", content);
        }

        if !status.is_success() {
            if let Ok(error_resp) = serde_json::from_str::<ErrorResponse>(&content) {
                return Err(OpenAIToolError::Error(error_resp.error.message.unwrap_or_default()));
            }
            return Err(OpenAIToolError::Error(format!("API error ({}): {}", status, content)));
        }

        serde_json::from_str::<T>(&content).map_err(OpenAIToolError::SerdeJsonError)
    }

    /// Queues a video generation job.
    ///
    /// Returns as soon as the job is accepted - the returned [`Video`] will be
    /// [`Queued`](crate::videos::response::VideoStatus::Queued), not finished.
    ///
    /// # Arguments
    ///
    /// * `prompt` - Describes the video to generate
    /// * `options` - Model, duration, size and reference image
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openai_tools::videos::request::{Videos, CreateVideoOptions, VideoSeconds};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let videos = Videos::new()?;
    ///
    ///     let options = CreateVideoOptions { seconds: Some(VideoSeconds::Eight), ..Default::default() };
    ///     let job = videos.create("A red balloon over Tokyo", options).await?;
    ///     println!("{} -> {:?}", job.id, job.status);
    ///     Ok(())
    /// }
    /// ```
    pub async fn create<S: Into<String>>(&self, prompt: S, options: CreateVideoOptions) -> Result<Video> {
        let (client, headers) = self.create_client()?;
        let url = self.auth.endpoint(VIDEOS_PATH);
        let body = serde_json::to_string(&options.into_request(prompt))?;

        let response = client.post(&url).headers(headers).body(body).send().await.map_err(OpenAIToolError::RequestError)?;

        Self::read_json(response).await
    }

    /// Retrieves the current state of a video generation job.
    ///
    /// # Arguments
    ///
    /// * `video_id` - The video identifier
    pub async fn retrieve(&self, video_id: &str) -> Result<Video> {
        let (client, headers) = self.create_client()?;
        let url = format!("{}/{}", self.auth.endpoint(VIDEOS_PATH), video_id);

        let response = client.get(&url).headers(headers).send().await.map_err(OpenAIToolError::RequestError)?;

        Self::read_json(response).await
    }

    /// Lists recently generated videos for the current project.
    ///
    /// # Arguments
    ///
    /// * `limit` - Maximum number of videos to return
    /// * `after` - Pagination cursor: return items after this video ID
    /// * `order` - Sort order by creation time
    pub async fn list(&self, limit: Option<u32>, after: Option<&str>, order: Option<SortOrder>) -> Result<VideoListResponse> {
        let (client, headers) = self.create_client()?;

        let mut query: Vec<String> = Vec::new();
        if let Some(limit) = limit {
            query.push(format!("limit={}", limit));
        }
        if let Some(after) = after {
            query.push(format!("after={}", after));
        }
        if let Some(order) = order {
            query.push(format!("order={}", order.as_str()));
        }

        let endpoint = self.auth.endpoint(VIDEOS_PATH);
        let url = if query.is_empty() { endpoint } else { format!("{}?{}", endpoint, query.join("&")) };

        let response = client.get(&url).headers(headers).send().await.map_err(OpenAIToolError::RequestError)?;

        Self::read_json(response).await
    }

    /// Deletes a completed or failed video along with its assets.
    ///
    /// # Arguments
    ///
    /// * `video_id` - The video identifier
    pub async fn delete(&self, video_id: &str) -> Result<DeleteVideoResponse> {
        let (client, headers) = self.create_client()?;
        let url = format!("{}/{}", self.auth.endpoint(VIDEOS_PATH), video_id);

        let response = client.delete(&url).headers(headers).send().await.map_err(OpenAIToolError::RequestError)?;

        Self::read_json(response).await
    }

    /// Downloads the bytes of a generated asset.
    ///
    /// # Arguments
    ///
    /// * `video_id` - The video identifier
    /// * `variant` - Which asset to download; defaults to
    ///   [`VideoVariant::Video`]
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openai_tools::videos::request::{Videos, VideoVariant};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let videos = Videos::new()?;
    ///
    ///     let mp4 = videos.content("video_abc123", None).await?;
    ///     std::fs::write("out.mp4", mp4)?;
    ///
    ///     let thumbnail = videos.content("video_abc123", Some(VideoVariant::Thumbnail)).await?;
    ///     std::fs::write("thumb.jpg", thumbnail)?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn content(&self, video_id: &str, variant: Option<VideoVariant>) -> Result<Vec<u8>> {
        let (client, headers) = self.create_client()?;
        let url = match variant {
            Some(variant) => format!("{}/{}/content?variant={}", self.auth.endpoint(VIDEOS_PATH), video_id, variant.as_str()),
            None => format!("{}/{}/content", self.auth.endpoint(VIDEOS_PATH), video_id),
        };

        let response = client.get(&url).headers(headers).send().await.map_err(OpenAIToolError::RequestError)?;

        let status = response.status();
        if !status.is_success() {
            let content = response.text().await.map_err(OpenAIToolError::RequestError)?;
            if let Ok(error_resp) = serde_json::from_str::<ErrorResponse>(&content) {
                return Err(OpenAIToolError::Error(error_resp.error.message.unwrap_or_default()));
            }
            return Err(OpenAIToolError::Error(format!("API error ({}): {}", status, content)));
        }

        let bytes = response.bytes().await.map_err(OpenAIToolError::RequestError)?;
        Ok(bytes.to_vec())
    }

    /// Creates a remix of an existing video using an updated prompt.
    ///
    /// # Arguments
    ///
    /// * `video_id` - The source video identifier
    /// * `prompt` - Updated prompt directing the remix
    pub async fn remix<S: Into<String>>(&self, video_id: &str, prompt: S) -> Result<Video> {
        let (client, headers) = self.create_client()?;
        let url = format!("{}/{}/remix", self.auth.endpoint(VIDEOS_PATH), video_id);
        let body = serde_json::to_string(&RemixVideoRequest { prompt: prompt.into() })?;

        let response = client.post(&url).headers(headers).body(body).send().await.map_err(OpenAIToolError::RequestError)?;

        Self::read_json(response).await
    }
}