quantum-sdk 0.6.0

Rust client SDK for the Quantum AI 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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::client::Client;
use crate::error::Result;

/// Request body for video generation.
#[derive(Debug, Clone, Serialize, Default)]
pub struct VideoRequest {
    /// Video generation model (e.g. "heygen", "grok-imagine-video", "sora-2", "veo-2").
    pub model: String,

    /// Describes the video to generate.
    pub prompt: String,

    /// Target video duration in seconds (default 8).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration_seconds: Option<i32>,

    /// Video aspect ratio (e.g. "16:9", "9:16").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aspect_ratio: Option<String>,
}

/// Response from video generation.
#[derive(Debug, Clone, Deserialize)]
pub struct VideoResponse {
    /// Generated videos.
    pub videos: Vec<GeneratedVideo>,

    /// Model that generated the videos.
    pub model: String,

    /// Total cost in ticks.
    #[serde(default)]
    pub cost_ticks: i64,

    /// Post-deduction credit balance in ticks (Receipt Pattern).
    #[serde(default)]
    pub balance_after: i64,

    /// Unique request identifier.
    #[serde(default)]
    pub request_id: String,
}

/// A single generated video.
#[derive(Debug, Clone, Deserialize)]
pub struct GeneratedVideo {
    /// Base64-encoded video data (or a URL).
    pub base64: String,

    /// Video format (e.g. "mp4").
    pub format: String,

    /// Video file size.
    pub size_bytes: i64,

    /// Video index within the batch.
    pub index: i32,
}

// ---------------------------------------------------------------------------
// Job response (shared by HeyGen endpoints)
// ---------------------------------------------------------------------------

/// Response from async video job submission.
#[derive(Debug, Clone, Deserialize)]
pub struct JobResponse {
    /// Job identifier for polling status.
    pub job_id: String,

    /// Current status.
    #[serde(default)]
    pub status: String,

    /// Total cost in ticks (may be 0 until job completes).
    #[serde(default)]
    pub cost_ticks: i64,

    /// Additional response fields.
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

// ---------------------------------------------------------------------------
// HeyGen Studio
// ---------------------------------------------------------------------------

/// A clip in a studio video.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct StudioClip {
    /// Avatar ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_id: Option<String>,

    /// Voice ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice_id: Option<String>,

    /// Script text for this clip.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub script: Option<String>,

    /// Background settings.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<serde_json::Value>,
}

/// Request body for HeyGen studio video creation.
#[derive(Debug, Clone, Serialize, Default)]
pub struct VideoStudioRequest {
    /// Video title.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// Video clips.
    pub clips: Vec<StudioClip>,

    /// Video dimensions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dimension: Option<String>,

    /// Aspect ratio.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aspect_ratio: Option<String>,
}

// ---------------------------------------------------------------------------
// HeyGen Translate
// ---------------------------------------------------------------------------

/// Backwards-compatible alias.
pub type StudioVideoRequest = VideoStudioRequest;

/// Request body for video translation.
#[derive(Debug, Clone, Serialize, Default)]
pub struct VideoTranslateRequest {
    /// URL of the video to translate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_url: Option<String>,

    /// Base64-encoded video (alternative to URL).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_base64: Option<String>,

    /// Target language code.
    pub target_language: String,

    /// Source language code (auto-detected if omitted).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_language: Option<String>,
}

/// Backwards-compatible alias.
pub type TranslateRequest = VideoTranslateRequest;

// ---------------------------------------------------------------------------
// HeyGen Photo Avatar
// ---------------------------------------------------------------------------

/// Request body for creating a photo avatar video.
#[derive(Debug, Clone, Serialize, Default)]
pub struct PhotoAvatarRequest {
    /// Base64-encoded photo.
    pub photo_base64: String,

    /// Script text for the avatar to speak.
    pub script: String,

    /// Voice ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice_id: Option<String>,

    /// Aspect ratio.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aspect_ratio: Option<String>,
}

// ---------------------------------------------------------------------------
// HeyGen Digital Twin
// ---------------------------------------------------------------------------

/// Request body for digital twin video generation.
#[derive(Debug, Clone, Serialize, Default)]
pub struct DigitalTwinRequest {
    /// Digital twin / avatar ID.
    pub avatar_id: String,

    /// Script text.
    pub script: String,

    /// Voice ID (uses twin's default voice if omitted).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice_id: Option<String>,

    /// Aspect ratio.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aspect_ratio: Option<String>,
}

// ---------------------------------------------------------------------------
// HeyGen Avatars
// ---------------------------------------------------------------------------

/// A HeyGen avatar.
#[derive(Debug, Clone, Deserialize)]
pub struct Avatar {
    /// Avatar identifier.
    pub avatar_id: String,

    /// Avatar name.
    #[serde(default)]
    pub name: Option<String>,

    /// Avatar gender.
    #[serde(default)]
    pub gender: Option<String>,

    /// Preview image URL.
    #[serde(default)]
    pub preview_url: Option<String>,

    /// Additional fields.
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Response from listing HeyGen avatars.
#[derive(Debug, Clone, Deserialize)]
pub struct AvatarsResponse {
    pub avatars: Vec<Avatar>,
}

// ---------------------------------------------------------------------------
// HeyGen Templates
// ---------------------------------------------------------------------------

/// A HeyGen video template.
#[derive(Debug, Clone, Deserialize)]
pub struct VideoTemplate {
    /// Template identifier.
    pub template_id: String,

    /// Template name.
    #[serde(default)]
    pub name: Option<String>,

    /// Preview image URL.
    #[serde(default)]
    pub preview_url: Option<String>,

    /// Additional fields.
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Response from listing HeyGen video templates.
#[derive(Debug, Clone, Deserialize)]
pub struct VideoTemplatesResponse {
    pub templates: Vec<VideoTemplate>,
}

// ---------------------------------------------------------------------------
// HeyGen typed responses (with request_id)
// ---------------------------------------------------------------------------

/// Response from listing HeyGen avatars (includes request_id).
#[derive(Debug, Clone, Deserialize)]
pub struct HeyGenAvatarsResponse {
    /// Available avatars (raw JSON items).
    #[serde(default)]
    pub avatars: Vec<serde_json::Value>,

    /// Unique request identifier.
    #[serde(default)]
    pub request_id: String,
}

/// Response from listing HeyGen templates (includes request_id).
#[derive(Debug, Clone, Deserialize)]
pub struct HeyGenTemplatesResponse {
    /// Available templates (raw JSON items).
    #[serde(default)]
    pub templates: Vec<serde_json::Value>,

    /// Unique request identifier.
    #[serde(default)]
    pub request_id: String,
}

// ---------------------------------------------------------------------------
// HeyGen Voices
// ---------------------------------------------------------------------------

/// A HeyGen voice.
#[derive(Debug, Clone, Deserialize)]
pub struct HeyGenVoice {
    /// Voice identifier.
    pub voice_id: String,

    /// Voice name.
    #[serde(default)]
    pub name: Option<String>,

    /// Language.
    #[serde(default)]
    pub language: Option<String>,

    /// Gender.
    #[serde(default)]
    pub gender: Option<String>,

    /// Additional fields.
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Response from listing HeyGen voices.
#[derive(Debug, Clone, Deserialize)]
pub struct HeyGenVoicesResponse {
    pub voices: Vec<HeyGenVoice>,
}

// ---------------------------------------------------------------------------
// Client impl
// ---------------------------------------------------------------------------

impl Client {
    /// Generates a video from a text prompt.
    ///
    /// Video generation is slow (30s-5min). For production use, consider submitting
    /// via the Jobs API instead.
    pub async fn generate_video(&self, req: &VideoRequest) -> Result<VideoResponse> {
        let (mut resp, meta) = self
            .post_json::<VideoRequest, VideoResponse>("/qai/v1/video/generate", req)
            .await?;
        if resp.cost_ticks == 0 {
            resp.cost_ticks = meta.cost_ticks;
        }
        if resp.request_id.is_empty() {
            resp.request_id = meta.request_id;
        }
        Ok(resp)
    }

    /// Creates a HeyGen studio video from clips.
    pub async fn video_studio(&self, req: &VideoStudioRequest) -> Result<JobResponse> {
        let (resp, _meta) = self
            .post_json::<VideoStudioRequest, JobResponse>("/qai/v1/video/studio", req)
            .await?;
        Ok(resp)
    }

    /// Translates a video into another language (HeyGen).
    pub async fn video_translate(&self, req: &VideoTranslateRequest) -> Result<JobResponse> {
        let (resp, _meta) = self
            .post_json::<VideoTranslateRequest, JobResponse>("/qai/v1/video/translate", req)
            .await?;
        Ok(resp)
    }

    /// Creates a video from a photo avatar (HeyGen).
    pub async fn video_photo_avatar(&self, req: &PhotoAvatarRequest) -> Result<JobResponse> {
        let (resp, _meta) = self
            .post_json::<PhotoAvatarRequest, JobResponse>("/qai/v1/video/photo-avatar", req)
            .await?;
        Ok(resp)
    }

    /// Creates a video from a digital twin avatar (HeyGen).
    pub async fn video_digital_twin(&self, req: &DigitalTwinRequest) -> Result<JobResponse> {
        let (resp, _meta) = self
            .post_json::<DigitalTwinRequest, JobResponse>("/qai/v1/video/digital-twin", req)
            .await?;
        Ok(resp)
    }

    /// Lists available HeyGen avatars.
    pub async fn video_avatars(&self) -> Result<AvatarsResponse> {
        let (resp, _meta) = self
            .get_json::<AvatarsResponse>("/qai/v1/video/avatars")
            .await?;
        Ok(resp)
    }

    /// Lists available HeyGen video templates.
    pub async fn video_templates(&self) -> Result<VideoTemplatesResponse> {
        let (resp, _meta) = self
            .get_json::<VideoTemplatesResponse>("/qai/v1/video/templates")
            .await?;
        Ok(resp)
    }

    /// Lists available HeyGen voices.
    pub async fn video_heygen_voices(&self) -> Result<HeyGenVoicesResponse> {
        let (resp, _meta) = self
            .get_json::<HeyGenVoicesResponse>("/qai/v1/video/heygen-voices")
            .await?;
        Ok(resp)
    }
}