Compare commits

...
Author SHA1 Message Date
Brandon Thomas da16693252 artcraft_router: Vidu and Veo models 2026-07-06 23:40:24 -04:00
62 changed files with 6414 additions and 1597 deletions
@@ -98,6 +98,15 @@ pub enum RouterVideoModel {
#[serde(rename = "veo_3p1_fast")]
Veo3p1Fast,
#[serde(rename = "veo_3p1_lite")]
Veo3p1Lite,
#[serde(rename = "vidu_q3")]
ViduQ3,
#[serde(rename = "vidu_q3_turbo")]
ViduQ3Turbo,
#[serde(rename = "preview_model")]
PreviewModel,
@@ -123,4 +132,13 @@ mod tests {
assert_serde_round_trip(RouterVideoModel::Seedance2p0BytePlusMini, "seedance_2p0_bp_mini");
assert_serde_round_trip(RouterVideoModel::Seedance2p0BytePlusUltraMini, "seedance_2p0_bpu_mini");
}
#[test]
fn veo_3p1_lite_and_vidu_variants_serialize() {
// NB: These strings must match `CommonVideoModel` — the two enums convert
// via serde string round-trip in the omni handlers.
assert_serde_round_trip(RouterVideoModel::Veo3p1Lite, "veo_3p1_lite");
assert_serde_round_trip(RouterVideoModel::ViduQ3, "vidu_q3");
assert_serde_round_trip(RouterVideoModel::ViduQ3Turbo, "vidu_q3_turbo");
}
}
@@ -1,6 +1,5 @@
use fal_client::requests_old::webhook::image::angle::enqueue_flux_2_lora_edit_image_angle_webhook::{
EnqueueFlux2LoraAngleImageSize, EnqueueFlux2LoraAngleNumImages,
EnqueueFlux2LoraEditImageAngleRequest,
use fal_client::requests::api::image::angle::flux_2_lora_edit_image_angle::api::{
Flux2LoraAngleImageSize, Flux2LoraAngleNumImages, Flux2LoraEditImageAngleRequest,
};
use crate::api::router_aspect_ratio::RouterAspectRatio;
@@ -27,7 +26,7 @@ pub fn build_fal_flux_2_lora_angles(
let num_images = plan_num_images(builder.image_batch_count, strategy)?;
let image_size = plan_image_size(builder.aspect_ratio, strategy)?;
let request = EnqueueFlux2LoraEditImageAngleRequest {
let request = Flux2LoraEditImageAngleRequest {
image_urls,
horizontal_angle: builder.horizontal_angle,
vertical_angle: builder.vertical_angle.map(|v| v.clamp(0.0, 60.0)),
@@ -57,8 +56,8 @@ fn resolve_image_urls(image_inputs: Option<ImageListRef>) -> Result<Vec<String>,
fn plan_num_images(
image_batch_count: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<EnqueueFlux2LoraAngleNumImages, ArtcraftRouterError> {
use EnqueueFlux2LoraAngleNumImages as N;
) -> Result<Flux2LoraAngleNumImages, ArtcraftRouterError> {
use Flux2LoraAngleNumImages as N;
let count = image_batch_count.unwrap_or(1);
match count {
0 => Err(ArtcraftRouterError::Client(ClientError::UserRequestedZeroGenerations)),
@@ -81,8 +80,8 @@ fn plan_num_images(
fn plan_image_size(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<EnqueueFlux2LoraAngleImageSize>, ArtcraftRouterError> {
use EnqueueFlux2LoraAngleImageSize as S;
) -> Result<Option<Flux2LoraAngleImageSize>, ArtcraftRouterError> {
use Flux2LoraAngleImageSize as S;
match aspect_ratio {
None => Ok(None),
@@ -118,34 +117,6 @@ mod tests {
use crate::api::router_image_model::RouterImageModel;
use crate::api::router_provider::RouterProvider;
fn base() -> GenerateImageRequestBuilder {
GenerateImageRequestBuilder {
model: RouterImageModel::Flux2LoraAngles,
provider: RouterProvider::Fal,
prompt: None,
image_inputs: Some(ImageListRef::Urls(vec!["https://example.com/x.jpg".to_string()])),
resolution: None,
aspect_ratio: None,
quality: None,
image_batch_count: None,
horizontal_angle: Some(45.0),
vertical_angle: Some(-15.0),
zoom: Some(2.0),
request_mismatch_mitigation_strategy: RequestMismatchMitigationStrategy::ErrorOut,
generation_mode_mismatch_strategy: None,
idempotency_token: None,
}
}
fn unwrap_request(result: Result<ImageGenerationDraftOrRequest, ArtcraftRouterError>) -> EnqueueFlux2LoraEditImageAngleRequest {
let ImageGenerationDraftOrRequest::Request(
ImageGenerationRequest::FalFlux2LoraAngles(state)
) = result.expect("build should succeed") else {
panic!("expected FalFlux2LoraAngles variant")
};
state.request
}
#[test]
fn passes_through_camera_params() {
let builder = GenerateImageRequestBuilder {
@@ -207,4 +178,32 @@ mod tests {
Err(ArtcraftRouterError::Client(ClientError::UserRequestedZeroGenerations))
));
}
fn base() -> GenerateImageRequestBuilder {
GenerateImageRequestBuilder {
model: RouterImageModel::Flux2LoraAngles,
provider: RouterProvider::Fal,
prompt: None,
image_inputs: Some(ImageListRef::Urls(vec!["https://example.com/x.jpg".to_string()])),
resolution: None,
aspect_ratio: None,
quality: None,
image_batch_count: None,
horizontal_angle: Some(45.0),
vertical_angle: Some(-15.0),
zoom: Some(2.0),
request_mismatch_mitigation_strategy: RequestMismatchMitigationStrategy::ErrorOut,
generation_mode_mismatch_strategy: None,
idempotency_token: None,
}
}
fn unwrap_request(result: Result<ImageGenerationDraftOrRequest, ArtcraftRouterError>) -> Flux2LoraEditImageAngleRequest {
let ImageGenerationDraftOrRequest::Request(
ImageGenerationRequest::FalFlux2LoraAngles(state)
) = result.expect("build should succeed") else {
panic!("expected FalFlux2LoraAngles variant")
};
state.request
}
}
@@ -32,13 +32,33 @@ impl FalFlux2LoraAnglesCostState {
#[cfg(test)]
mod tests {
use super::*;
use fal_client::requests_old::webhook::image::angle::enqueue_flux_2_lora_edit_image_angle_webhook::{
EnqueueFlux2LoraAngleNumImages, EnqueueFlux2LoraEditImageAngleRequest,
use fal_client::requests::api::image::angle::flux_2_lora_edit_image_angle::api::{
Flux2LoraAngleNumImages, Flux2LoraEditImageAngleRequest,
};
fn cost_for(n: EnqueueFlux2LoraAngleNumImages) -> ImageGenerationCostEstimate {
#[test]
fn one_image_is_2_cents() {
assert_eq!(cost_for(Flux2LoraAngleNumImages::One).cost_in_usd_cents, Some(2));
}
#[test]
fn two_images_is_4_cents() {
assert_eq!(cost_for(Flux2LoraAngleNumImages::Two).cost_in_usd_cents, Some(4));
}
#[test]
fn three_images_is_6_cents() {
assert_eq!(cost_for(Flux2LoraAngleNumImages::Three).cost_in_usd_cents, Some(6));
}
#[test]
fn four_images_is_8_cents() {
assert_eq!(cost_for(Flux2LoraAngleNumImages::Four).cost_in_usd_cents, Some(8));
}
fn cost_for(n: Flux2LoraAngleNumImages) -> ImageGenerationCostEstimate {
let state = FalFlux2LoraAnglesCostState::from_request(&FalFlux2LoraAnglesRequestState {
request: EnqueueFlux2LoraEditImageAngleRequest {
request: Flux2LoraEditImageAngleRequest {
image_urls: vec!["https://example.com/x.jpg".to_string()],
horizontal_angle: None,
vertical_angle: None,
@@ -52,24 +72,4 @@ mod tests {
});
state.estimate_cost()
}
#[test]
fn one_image_is_2_cents() {
assert_eq!(cost_for(EnqueueFlux2LoraAngleNumImages::One).cost_in_usd_cents, Some(2));
}
#[test]
fn two_images_is_4_cents() {
assert_eq!(cost_for(EnqueueFlux2LoraAngleNumImages::Two).cost_in_usd_cents, Some(4));
}
#[test]
fn three_images_is_6_cents() {
assert_eq!(cost_for(EnqueueFlux2LoraAngleNumImages::Three).cost_in_usd_cents, Some(6));
}
#[test]
fn four_images_is_8_cents() {
assert_eq!(cost_for(EnqueueFlux2LoraAngleNumImages::Four).cost_in_usd_cents, Some(8));
}
}
@@ -1,55 +1,100 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests_old::webhook::image::angle::enqueue_flux_2_lora_edit_image_angle_webhook::{
enqueue_flux_2_lora_edit_image_angle_webhook, EnqueueFlux2LoraEditImageAngleArgs,
EnqueueFlux2LoraEditImageAngleRequest,
};
use fal_client::requests::api::image::angle::flux_2_lora_edit_image_angle::api::Flux2LoraEditImageAngleRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::errors::provider_error::ProviderError;
use crate::generate::generate_image::generate_image_response::{
FalImageResponsePayload, GenerateImageResponse,
};
#[derive(Clone, Debug)]
pub struct FalFlux2LoraAnglesRequestState {
pub request: EnqueueFlux2LoraEditImageAngleRequest,
pub request: Flux2LoraEditImageAngleRequest,
}
impl FalFlux2LoraAnglesRequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateImageResponse, ArtcraftRouterError> {
let webhook_url = client.webhook_url.as_deref()
.ok_or(ArtcraftRouterError::Client(ClientError::WebhookUrlRequired))?;
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(self.request.clone());
let args = EnqueueFlux2LoraEditImageAngleArgs {
request: self.request.clone(),
webhook_url,
api_key: &client.api_key,
};
let webhook_response = enqueue_flux_2_lora_edit_image_angle_webhook(args)
.await
.map_err(|e| ArtcraftRouterError::Provider(ProviderError::Fal(e)))?;
let payload = send_fal_request(&self.request, client).await?;
Ok(GenerateImageResponse::Fal(FalImageResponsePayload {
request_id: webhook_response.request_id,
gateway_request_id: webhook_response.gateway_request_id,
maybe_status_url: None,
maybe_response_url: None,
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
}
// ── Helpers ──
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request
.send_webhook_request(&client.api_key, webhook_url)
.await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request
.send_queue_request(&client.api_key)
.await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use fal_client::creds::fal_api_key::FalApiKey;
use fal_client::requests_old::webhook::image::angle::enqueue_flux_2_lora_edit_image_angle_webhook::{
EnqueueFlux2LoraAngleImageSize, EnqueueFlux2LoraAngleNumImages,
use fal_client::requests::api::image::angle::flux_2_lora_edit_image_angle::api::{
Flux2LoraAngleImageSize, Flux2LoraAngleNumImages,
};
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn send_via_webhook() {
let client = client_with_webhook();
let state = FalFlux2LoraAnglesRequestState {
request: Flux2LoraEditImageAngleRequest {
image_urls: vec![JUNO_AT_LAKE_IMAGE_URL.to_string()],
horizontal_angle: Some(45.0),
vertical_angle: Some(15.0),
zoom: Some(2.0),
num_images: Some(Flux2LoraAngleNumImages::One),
image_size: Some(Flux2LoraAngleImageSize::SquareHd),
lora_scale: None,
guidance_scale: None,
num_inference_steps: None,
},
};
let response = state.send(&client).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
fn read_fal_api_key() -> FalApiKey {
let secret = std::fs::read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")
@@ -63,26 +108,4 @@ mod tests {
"https://example.com/fal-webhook-test".to_string(),
)
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn send_via_webhook() {
let client = client_with_webhook();
let state = FalFlux2LoraAnglesRequestState {
request: EnqueueFlux2LoraEditImageAngleRequest {
image_urls: vec!["https://example.com/test.jpg".to_string()],
horizontal_angle: Some(45.0),
vertical_angle: Some(-15.0),
zoom: Some(2.0),
num_images: Some(EnqueueFlux2LoraAngleNumImages::One),
image_size: Some(EnqueueFlux2LoraAngleImageSize::SquareHd),
lora_scale: None,
guidance_scale: None,
num_inference_steps: None,
},
};
let response = state.send(&client).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
}
@@ -1,6 +1,5 @@
use fal_client::requests_old::webhook::image::angle::enqueue_qwen_edit_2511_edit_image_angle_webhook::{
EnqueueQwenEdit2511AngleImageSize, EnqueueQwenEdit2511AngleNumImages,
EnqueueQwenEdit2511EditImageAngleRequest,
use fal_client::requests::api::image::angle::qwen_edit_2511_edit_image_angle::api::{
QwenEdit2511AngleImageSize, QwenEdit2511AngleNumImages, QwenEdit2511EditImageAngleRequest,
};
use crate::api::router_aspect_ratio::RouterAspectRatio;
@@ -27,7 +26,7 @@ pub fn build_fal_qwen_edit_2511_angles(
let num_images = plan_num_images(builder.image_batch_count, strategy)?;
let image_size = plan_image_size(builder.aspect_ratio, strategy)?;
let request = EnqueueQwenEdit2511EditImageAngleRequest {
let request = QwenEdit2511EditImageAngleRequest {
image_urls,
horizontal_angle: builder.horizontal_angle,
vertical_angle: builder.vertical_angle,
@@ -58,8 +57,8 @@ fn resolve_image_urls(image_inputs: Option<ImageListRef>) -> Result<Vec<String>,
fn plan_num_images(
image_batch_count: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<EnqueueQwenEdit2511AngleNumImages, ArtcraftRouterError> {
use EnqueueQwenEdit2511AngleNumImages as N;
) -> Result<QwenEdit2511AngleNumImages, ArtcraftRouterError> {
use QwenEdit2511AngleNumImages as N;
let count = image_batch_count.unwrap_or(1);
match count {
0 => Err(ArtcraftRouterError::Client(ClientError::UserRequestedZeroGenerations)),
@@ -82,8 +81,8 @@ fn plan_num_images(
fn plan_image_size(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<EnqueueQwenEdit2511AngleImageSize>, ArtcraftRouterError> {
use EnqueueQwenEdit2511AngleImageSize as S;
) -> Result<Option<QwenEdit2511AngleImageSize>, ArtcraftRouterError> {
use QwenEdit2511AngleImageSize as S;
match aspect_ratio {
None => Ok(None),
@@ -119,34 +118,6 @@ mod tests {
use crate::api::router_image_model::RouterImageModel;
use crate::api::router_provider::RouterProvider;
fn base() -> GenerateImageRequestBuilder {
GenerateImageRequestBuilder {
model: RouterImageModel::QwenEdit2511Angles,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
image_inputs: Some(ImageListRef::Urls(vec!["https://example.com/x.jpg".to_string()])),
resolution: None,
aspect_ratio: None,
quality: None,
image_batch_count: None,
horizontal_angle: Some(45.0),
vertical_angle: Some(-15.0),
zoom: Some(2.0),
request_mismatch_mitigation_strategy: RequestMismatchMitigationStrategy::ErrorOut,
generation_mode_mismatch_strategy: None,
idempotency_token: None,
}
}
fn unwrap_request(result: Result<ImageGenerationDraftOrRequest, ArtcraftRouterError>) -> EnqueueQwenEdit2511EditImageAngleRequest {
let ImageGenerationDraftOrRequest::Request(
ImageGenerationRequest::FalQwenEdit2511Angles(state)
) = result.expect("build should succeed") else {
panic!("expected FalQwenEdit2511Angles variant")
};
state.request
}
#[test]
fn passes_through_camera_params() {
let req = unwrap_request(build_fal_qwen_edit_2511_angles(base()));
@@ -177,7 +148,7 @@ mod tests {
#[test]
fn default_num_images_is_one() {
let req = unwrap_request(build_fal_qwen_edit_2511_angles(base()));
assert!(matches!(req.num_images, Some(EnqueueQwenEdit2511AngleNumImages::One)));
assert!(matches!(req.num_images, Some(QwenEdit2511AngleNumImages::One)));
}
#[test]
@@ -197,6 +168,34 @@ mod tests {
..base()
};
let req = unwrap_request(build_fal_qwen_edit_2511_angles(builder));
assert!(matches!(req.num_images, Some(EnqueueQwenEdit2511AngleNumImages::Four)));
assert!(matches!(req.num_images, Some(QwenEdit2511AngleNumImages::Four)));
}
fn base() -> GenerateImageRequestBuilder {
GenerateImageRequestBuilder {
model: RouterImageModel::QwenEdit2511Angles,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
image_inputs: Some(ImageListRef::Urls(vec!["https://example.com/x.jpg".to_string()])),
resolution: None,
aspect_ratio: None,
quality: None,
image_batch_count: None,
horizontal_angle: Some(45.0),
vertical_angle: Some(-15.0),
zoom: Some(2.0),
request_mismatch_mitigation_strategy: RequestMismatchMitigationStrategy::ErrorOut,
generation_mode_mismatch_strategy: None,
idempotency_token: None,
}
}
fn unwrap_request(result: Result<ImageGenerationDraftOrRequest, ArtcraftRouterError>) -> QwenEdit2511EditImageAngleRequest {
let ImageGenerationDraftOrRequest::Request(
ImageGenerationRequest::FalQwenEdit2511Angles(state)
) = result.expect("build should succeed") else {
panic!("expected FalQwenEdit2511Angles variant")
};
state.request
}
}
@@ -32,13 +32,39 @@ impl FalQwenEdit2511AnglesCostState {
#[cfg(test)]
mod tests {
use super::*;
use fal_client::requests_old::webhook::image::angle::enqueue_qwen_edit_2511_edit_image_angle_webhook::{
EnqueueQwenEdit2511AngleNumImages, EnqueueQwenEdit2511EditImageAngleRequest,
use fal_client::requests::api::image::angle::qwen_edit_2511_edit_image_angle::api::{
QwenEdit2511AngleNumImages, QwenEdit2511EditImageAngleRequest,
};
fn cost_for(n: EnqueueQwenEdit2511AngleNumImages) -> ImageGenerationCostEstimate {
#[test]
fn one_image_is_4_cents() {
assert_eq!(cost_for(QwenEdit2511AngleNumImages::One).cost_in_usd_cents, Some(4));
}
#[test]
fn two_images_is_8_cents() {
assert_eq!(cost_for(QwenEdit2511AngleNumImages::Two).cost_in_usd_cents, Some(8));
}
#[test]
fn three_images_is_12_cents() {
assert_eq!(cost_for(QwenEdit2511AngleNumImages::Three).cost_in_usd_cents, Some(12));
}
#[test]
fn four_images_is_16_cents() {
assert_eq!(cost_for(QwenEdit2511AngleNumImages::Four).cost_in_usd_cents, Some(16));
}
#[test]
fn credits_equal_cents() {
let cost = cost_for(QwenEdit2511AngleNumImages::Two);
assert_eq!(cost.cost_in_credits, cost.cost_in_usd_cents);
}
fn cost_for(n: QwenEdit2511AngleNumImages) -> ImageGenerationCostEstimate {
let state = FalQwenEdit2511AnglesCostState::from_request(&FalQwenEdit2511AnglesRequestState {
request: EnqueueQwenEdit2511EditImageAngleRequest {
request: QwenEdit2511EditImageAngleRequest {
image_urls: vec!["https://example.com/x.jpg".to_string()],
horizontal_angle: None,
vertical_angle: None,
@@ -53,30 +79,4 @@ mod tests {
});
state.estimate_cost()
}
#[test]
fn one_image_is_4_cents() {
assert_eq!(cost_for(EnqueueQwenEdit2511AngleNumImages::One).cost_in_usd_cents, Some(4));
}
#[test]
fn two_images_is_8_cents() {
assert_eq!(cost_for(EnqueueQwenEdit2511AngleNumImages::Two).cost_in_usd_cents, Some(8));
}
#[test]
fn three_images_is_12_cents() {
assert_eq!(cost_for(EnqueueQwenEdit2511AngleNumImages::Three).cost_in_usd_cents, Some(12));
}
#[test]
fn four_images_is_16_cents() {
assert_eq!(cost_for(EnqueueQwenEdit2511AngleNumImages::Four).cost_in_usd_cents, Some(16));
}
#[test]
fn credits_equal_cents() {
let cost = cost_for(EnqueueQwenEdit2511AngleNumImages::Two);
assert_eq!(cost.cost_in_credits, cost.cost_in_usd_cents);
}
}
@@ -1,55 +1,101 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests_old::webhook::image::angle::enqueue_qwen_edit_2511_edit_image_angle_webhook::{
enqueue_qwen_edit_2511_edit_image_angle_webhook, EnqueueQwenEdit2511EditImageAngleArgs,
EnqueueQwenEdit2511EditImageAngleRequest,
};
use fal_client::requests::api::image::angle::qwen_edit_2511_edit_image_angle::api::QwenEdit2511EditImageAngleRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::errors::provider_error::ProviderError;
use crate::generate::generate_image::generate_image_response::{
FalImageResponsePayload, GenerateImageResponse,
};
#[derive(Clone, Debug)]
pub struct FalQwenEdit2511AnglesRequestState {
pub request: EnqueueQwenEdit2511EditImageAngleRequest,
pub request: QwenEdit2511EditImageAngleRequest,
}
impl FalQwenEdit2511AnglesRequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateImageResponse, ArtcraftRouterError> {
let webhook_url = client.webhook_url.as_deref()
.ok_or(ArtcraftRouterError::Client(ClientError::WebhookUrlRequired))?;
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(self.request.clone());
let args = EnqueueQwenEdit2511EditImageAngleArgs {
request: self.request.clone(),
webhook_url,
api_key: &client.api_key,
};
let webhook_response = enqueue_qwen_edit_2511_edit_image_angle_webhook(args)
.await
.map_err(|e| ArtcraftRouterError::Provider(ProviderError::Fal(e)))?;
let payload = send_fal_request(&self.request, client).await?;
Ok(GenerateImageResponse::Fal(FalImageResponsePayload {
request_id: webhook_response.request_id,
gateway_request_id: webhook_response.gateway_request_id,
maybe_status_url: None,
maybe_response_url: None,
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
}
// ── Helpers ──
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request
.send_webhook_request(&client.api_key, webhook_url)
.await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request
.send_queue_request(&client.api_key)
.await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use fal_client::creds::fal_api_key::FalApiKey;
use fal_client::requests_old::webhook::image::angle::enqueue_qwen_edit_2511_edit_image_angle_webhook::{
EnqueueQwenEdit2511AngleImageSize, EnqueueQwenEdit2511AngleNumImages,
use fal_client::requests::api::image::angle::qwen_edit_2511_edit_image_angle::api::{
QwenEdit2511AngleImageSize, QwenEdit2511AngleNumImages,
};
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn send_via_webhook() {
let client = client_with_webhook();
let state = FalQwenEdit2511AnglesRequestState {
request: QwenEdit2511EditImageAngleRequest {
image_urls: vec![JUNO_AT_LAKE_IMAGE_URL.to_string()],
horizontal_angle: Some(45.0),
vertical_angle: Some(-15.0),
zoom: Some(2.0),
additional_prompt: Some("cinematic".to_string()),
num_images: Some(QwenEdit2511AngleNumImages::One),
image_size: Some(QwenEdit2511AngleImageSize::SquareHd),
lora_scale: None,
guidance_scale: None,
num_inference_steps: None,
},
};
let response = state.send(&client).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
fn read_fal_api_key() -> FalApiKey {
let secret = std::fs::read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")
@@ -63,27 +109,4 @@ mod tests {
"https://example.com/fal-webhook-test".to_string(),
)
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn send_via_webhook() {
let client = client_with_webhook();
let state = FalQwenEdit2511AnglesRequestState {
request: EnqueueQwenEdit2511EditImageAngleRequest {
image_urls: vec!["https://example.com/test.jpg".to_string()],
horizontal_angle: Some(45.0),
vertical_angle: Some(-15.0),
zoom: Some(2.0),
additional_prompt: Some("cinematic".to_string()),
num_images: Some(EnqueueQwenEdit2511AngleNumImages::One),
image_size: Some(EnqueueQwenEdit2511AngleImageSize::SquareHd),
lora_scale: None,
guidance_scale: None,
num_inference_steps: None,
},
};
let response = state.send(&client).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
}
@@ -42,6 +42,9 @@ use crate::generate::generate_video::providers::artcraft::veo_3::build::build_ar
use crate::generate::generate_video::providers::artcraft::veo_3_fast::build::build_artcraft_veo_3_fast;
use crate::generate::generate_video::providers::artcraft::veo_3p1::build::build_artcraft_veo_3p1;
use crate::generate::generate_video::providers::artcraft::veo_3p1_fast::build::build_artcraft_veo_3p1_fast;
use crate::generate::generate_video::providers::artcraft::veo_3p1_lite::build::build_artcraft_veo_3p1_lite;
use crate::generate::generate_video::providers::artcraft::vidu_q3::build::build_artcraft_vidu_q3;
use crate::generate::generate_video::providers::artcraft::vidu_q3_turbo::build::build_artcraft_vidu_q3_turbo;
use crate::generate::generate_video::providers::kinovi::happy_horse_1p0::build::build_kinovi_happy_horse_1p0;
use crate::generate::generate_video::providers::kinovi::seedance_2p0::build::build_kinovi_seedance_2p0;
use crate::generate::generate_video::providers::gmicloud::seedance_2p0_g::build::build_gmicloud_seedance_2p0_u;
@@ -64,6 +67,9 @@ use crate::generate::generate_video::providers::fal::veo_3::build::build_fal_veo
use crate::generate::generate_video::providers::fal::veo_3_fast::build::build_fal_veo_3_fast;
use crate::generate::generate_video::providers::fal::veo_3p1::build::build_fal_veo_3p1;
use crate::generate::generate_video::providers::fal::veo_3p1_fast::build::build_fal_veo_3p1_fast;
use crate::generate::generate_video::providers::fal::veo_3p1_lite::build::build_fal_veo_3p1_lite;
use crate::generate::generate_video::providers::fal::vidu_q3::build::build_fal_vidu_q3;
use crate::generate::generate_video::providers::fal::vidu_q3_turbo::build::build_fal_vidu_q3_turbo;
use crate::generate::generate_video::providers::kinovi::seedance_2p0_fast::build::build_kinovi_seedance_2p0_fast;
use crate::generate::generate_video::providers::kinovi::seedance_2p0_mini::build::build_kinovi_seedance_2p0_mini;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
@@ -194,6 +200,9 @@ impl GenerateVideoRequestBuilder {
(RouterProvider::Artcraft, RouterVideoModel::Veo3Fast) => build_artcraft_veo_3_fast(self),
(RouterProvider::Artcraft, RouterVideoModel::Veo3p1) => build_artcraft_veo_3p1(self),
(RouterProvider::Artcraft, RouterVideoModel::Veo3p1Fast) => build_artcraft_veo_3p1_fast(self),
(RouterProvider::Artcraft, RouterVideoModel::Veo3p1Lite) => build_artcraft_veo_3p1_lite(self),
(RouterProvider::Artcraft, RouterVideoModel::ViduQ3) => build_artcraft_vidu_q3(self),
(RouterProvider::Artcraft, RouterVideoModel::ViduQ3Turbo) => build_artcraft_vidu_q3_turbo(self),
// Fal
(RouterProvider::Fal, RouterVideoModel::Kling16Pro) => build_fal_kling_1_6_pro(self),
(RouterProvider::Fal, RouterVideoModel::Kling21Master) => build_fal_kling_2_1_master(self),
@@ -211,6 +220,9 @@ impl GenerateVideoRequestBuilder {
(RouterProvider::Fal, RouterVideoModel::Veo3Fast) => build_fal_veo_3_fast(self),
(RouterProvider::Fal, RouterVideoModel::Veo3p1) => build_fal_veo_3p1(self),
(RouterProvider::Fal, RouterVideoModel::Veo3p1Fast) => build_fal_veo_3p1_fast(self),
(RouterProvider::Fal, RouterVideoModel::Veo3p1Lite) => build_fal_veo_3p1_lite(self),
(RouterProvider::Fal, RouterVideoModel::ViduQ3) => build_fal_vidu_q3(self),
(RouterProvider::Fal, RouterVideoModel::ViduQ3Turbo) => build_fal_vidu_q3_turbo(self),
// GmiCloud
(RouterProvider::GmiCloud, RouterVideoModel::Seedance2p0Ultra) => build_gmicloud_seedance_2p0_u(self),
(RouterProvider::GmiCloud, RouterVideoModel::Seedance2p0UltraFast) => build_gmicloud_seedance_2p0_u_fast(self),
@@ -36,4 +36,7 @@ pub mod veo_3;
pub mod veo_3_fast;
pub mod veo_3p1;
pub mod veo_3p1_fast;
pub mod veo_3p1_lite;
pub mod vidu_q3;
pub mod vidu_q3_turbo;
@@ -0,0 +1,29 @@
use enums::common::generation::common_video_model::CommonVideoModel as CommonVideoModelEnum;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::artcraft::build_common::{
build_artcraft_omni_video_request, SupportedResolutions, UltraWideSupport,
};
use crate::generate::generate_video::providers::artcraft::veo_3p1_lite::request::ArtcraftVeo3p1LiteRequestState;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
pub fn build_artcraft_veo_3p1_lite(builder: GenerateVideoRequestBuilder) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_artcraft_veo_3p1_lite_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::ArtcraftVeo3p1Lite(state)))
}
pub(crate) fn build_artcraft_veo_3p1_lite_state(builder: GenerateVideoRequestBuilder) -> Result<ArtcraftVeo3p1LiteRequestState, ArtcraftRouterError> {
// Veo 3.1 Lite supports `generate_audio` — preserve it so cost can read it back.
// build_artcraft_omni_video_request hardcodes generate_audio = None on its output.
let generate_audio = builder.generate_audio;
let mut request = build_artcraft_omni_video_request(
builder,
CommonVideoModelEnum::Veo3p1Lite,
SupportedResolutions::NoFourEightyP,
UltraWideSupport::Unsupported,
)?;
request.generate_audio = generate_audio;
Ok(ArtcraftVeo3p1LiteRequestState { request })
}
@@ -0,0 +1,119 @@
use enums::common::generation::common_resolution::CommonResolution as CommonResolutionEnum;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::artcraft::veo_3p1_lite::request::ArtcraftVeo3p1LiteRequestState;
/// Per-second rates in hundredths of a US cent.
const SEVEN_TWENTY_P_AUDIO_OFF_RATE: u64 = 315;
const SEVEN_TWENTY_P_AUDIO_ON_RATE: u64 = 525;
const TEN_EIGHTY_P_AUDIO_OFF_RATE: u64 = 525;
const TEN_EIGHTY_P_AUDIO_ON_RATE: u64 = 840;
#[derive(Clone, Debug)]
pub struct ArtcraftVeo3p1LiteCostState {
pub duration_seconds: u64,
pub is_1080p: bool,
pub generate_audio: bool,
}
impl ArtcraftVeo3p1LiteCostState {
pub fn from_request(request: &ArtcraftVeo3p1LiteRequestState) -> Self {
Self {
// Veo 3.1 Lite defaults None → 8s.
duration_seconds: request.request.duration_seconds.map(u64::from).unwrap_or(8),
is_1080p: is_1080p(request.request.resolution),
// Audio is on when unspecified.
generate_audio: request.request.generate_audio.unwrap_or(true),
}
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
let rate = match (self.is_1080p, self.generate_audio) {
(false, false) => SEVEN_TWENTY_P_AUDIO_OFF_RATE,
(false, true) => SEVEN_TWENTY_P_AUDIO_ON_RATE,
(true, false) => TEN_EIGHTY_P_AUDIO_OFF_RATE,
(true, true) => TEN_EIGHTY_P_AUDIO_ON_RATE,
};
// Round up to the next whole cent.
let cost_in_usd_cents = (rate * self.duration_seconds).div_ceil(100);
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
has_watermark: false,
failures_are_refunded: None,
}
}
}
/// Veo 3.1 Lite renders 720p (the default) or 1080p.
fn is_1080p(resolution: Option<CommonResolutionEnum>) -> bool {
matches!(
resolution,
Some(CommonResolutionEnum::TenEightyP)
| Some(CommonResolutionEnum::TwoK)
| Some(CommonResolutionEnum::ThreeK)
| Some(CommonResolutionEnum::FourK)
)
}
#[cfg(test)]
mod tests {
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::artcraft::veo_3p1_lite::build::build_artcraft_veo_3p1_lite_state;
use crate::generate::generate_video::providers::artcraft::veo_3p1_lite::cost::ArtcraftVeo3p1LiteCostState;
fn cost_cents(
duration_seconds: Option<u16>,
resolution: Option<RouterResolution>,
generate_audio: Option<bool>,
) -> u64 {
let b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Lite,
provider: RouterProvider::Artcraft,
prompt: Some("test".to_string()),
duration_seconds,
resolution,
generate_audio,
..Default::default()
};
let state = build_artcraft_veo_3p1_lite_state(b).unwrap();
ArtcraftVeo3p1LiteCostState::from_request(&state).estimate_cost().cost_in_usd_cents.unwrap()
}
#[test]
fn defaults_8s_720p_audio_on_is_42() { assert_eq!(cost_cents(None, None, None), 42); }
#[test]
fn audio_off_8s_720p_is_26() {
// 315 * 8 = 2520 hundredth-cents → 26 cents.
assert_eq!(cost_cents(Some(8), None, Some(false)), 26);
}
#[test]
fn audio_on_8s_1080p_is_68() {
// 840 * 8 = 6720 hundredth-cents → 68 cents.
assert_eq!(cost_cents(Some(8), Some(RouterResolution::TenEightyP), Some(true)), 68);
}
#[test]
fn audio_off_8s_1080p_is_42() {
assert_eq!(cost_cents(Some(8), Some(RouterResolution::TenEightyP), Some(false)), 42);
}
#[test]
fn audio_on_4s_720p_is_21() {
assert_eq!(cost_cents(Some(4), None, Some(true)), 21);
}
#[test]
fn audio_default_is_on() {
assert_eq!(cost_cents(Some(8), None, None), cost_cents(Some(8), None, Some(true)));
}
}
@@ -0,0 +1,3 @@
pub mod build;
pub mod cost;
pub mod request;
@@ -0,0 +1,104 @@
use artcraft_api_defs::omni_gen::cost_and_generate_requests::omni_gen_video_cost_and_generate_request::OmniGenVideoCostAndGenerateRequest;
use crate::client::router_artcraft_client::RouterArtcraftClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::providers::artcraft::request_common::send_artcraft_omni_video_request;
#[derive(Clone, Debug)]
pub struct ArtcraftVeo3p1LiteRequestState {
pub request: OmniGenVideoCostAndGenerateRequest,
}
impl ArtcraftVeo3p1LiteRequestState {
pub async fn send(&self, client: &RouterArtcraftClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
send_artcraft_omni_video_request(&self.request, client).await
}
}
#[cfg(test)]
mod tests {
use tokens::tokens::media_files::MediaFileToken;
use artcraft_client::credentials::storyteller_credential_set::StorytellerCredentialSet;
use artcraft_client::utils::api_host::ApiHost;
use test_data::web::image_media_tokens::JUNO_AT_LAKE_PRODUCTION_MEDIA_TOKEN;
use crate::api::image_ref::ImageRef;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use crate::client::router_artcraft_client::RouterArtcraftClient;
use crate::client::router_client::RouterClient;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
#[tokio::test]
#[ignore] // sends a real generation to the Artcraft backend, incurs cost
async fn text_to_video() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("A lighthouse beam sweeping across a foggy bay at night.".to_string()),
aspect_ratio: Some(RouterAspectRatio::WideSixteenByNine),
..artcraft_builder()
}).await;
assert!(matches!(response, GenerateVideoResponse::Artcraft(_)));
assert_eq!(1, 2, "Inspect output above");
}
#[tokio::test]
#[ignore] // sends a real generation to the Artcraft backend, incurs cost
async fn keyframe_start_frame() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("The dog turns its head toward the camera.".to_string()),
start_frame: Some(ImageRef::MediaFileToken(MediaFileToken::new(JUNO_AT_LAKE_PRODUCTION_MEDIA_TOKEN.to_string()))),
..artcraft_builder()
}).await;
assert!(matches!(response, GenerateVideoResponse::Artcraft(_)));
assert_eq!(1, 2, "Inspect output above");
}
// ── Helpers ──
fn artcraft_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Lite,
provider: RouterProvider::Artcraft,
duration_seconds: Some(8),
video_batch_count: Some(1),
..Default::default()
}
}
fn get_artcraft_client() -> RouterClient {
let cookies = std::fs::read_to_string("/Users/bt/Artcraft/credentials/artcraft_cookies.txt")
.expect("Failed to read artcraft cookies");
let cookies = cookies.trim().to_string();
let credentials = StorytellerCredentialSet::parse_multi_cookie_header(&cookies)
.expect("Failed to parse cookies")
.expect("No credentials found");
RouterClient::Artcraft(RouterArtcraftClient::new(ApiHost::Storyteller, credentials))
}
async fn run_pipeline(builder: GenerateVideoRequestBuilder) -> GenerateVideoResponse {
let client = get_artcraft_client();
let draft_or_request = builder.build2().expect("build2 should succeed");
let request = match draft_or_request {
VideoGenerationDraftOrRequest::Request(r) => r,
_ => panic!("expected Request variant (Artcraft skips draft)"),
};
let response = request.send_request(&client).await.expect("send_request should succeed");
match &response {
GenerateVideoResponse::Artcraft(p) => {
println!("inference_job_token={:?}", p.inference_job_token);
println!("all_inference_job_tokens={:?}", p.all_inference_job_tokens);
}
other => println!("response: {:?}", other),
}
response
}
}
@@ -0,0 +1,29 @@
use enums::common::generation::common_video_model::CommonVideoModel as CommonVideoModelEnum;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::artcraft::build_common::{
build_artcraft_omni_video_request, SupportedResolutions, UltraWideSupport,
};
use crate::generate::generate_video::providers::artcraft::vidu_q3::request::ArtcraftViduQ3RequestState;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
pub fn build_artcraft_vidu_q3(builder: GenerateVideoRequestBuilder) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_artcraft_vidu_q3_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::ArtcraftViduQ3(state)))
}
pub(crate) fn build_artcraft_vidu_q3_state(builder: GenerateVideoRequestBuilder) -> Result<ArtcraftViduQ3RequestState, ArtcraftRouterError> {
// Vidu Q3 supports `audio` — preserve it so cost can read it back.
// build_artcraft_omni_video_request hardcodes generate_audio = None on its output.
let generate_audio = builder.generate_audio;
let mut request = build_artcraft_omni_video_request(
builder,
CommonVideoModelEnum::ViduQ3,
SupportedResolutions::Full,
UltraWideSupport::Unsupported,
)?;
request.generate_audio = generate_audio;
Ok(ArtcraftViduQ3RequestState { request })
}
@@ -0,0 +1,118 @@
use enums::common::generation::common_resolution::CommonResolution as CommonResolutionEnum;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::artcraft::vidu_q3::request::ArtcraftViduQ3RequestState;
/// Per-second rates in hundredths of a US cent.
const LOW_RES_RATE_HUNDREDTH_CENTS_PER_SEC: u64 = 735;
const HIGH_RES_RATE_HUNDREDTH_CENTS_PER_SEC: u64 = 1_617;
#[derive(Clone, Debug)]
pub struct ArtcraftViduQ3CostState {
pub duration_seconds: u64,
pub is_high_res: bool,
}
impl ArtcraftViduQ3CostState {
pub fn from_request(request: &ArtcraftViduQ3RequestState) -> Self {
Self {
// Vidu Q3 defaults None → 5s.
duration_seconds: request.request.duration_seconds.map(u64::from).unwrap_or(5),
is_high_res: is_high_res(request.request.resolution),
}
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
let rate = if self.is_high_res {
HIGH_RES_RATE_HUNDREDTH_CENTS_PER_SEC
} else {
LOW_RES_RATE_HUNDREDTH_CENTS_PER_SEC
};
// Round up to the next whole cent.
let cost_in_usd_cents = (rate * self.duration_seconds).div_ceil(100);
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
has_watermark: false,
failures_are_refunded: None,
}
}
}
/// Vidu prices 720p/1080p at a higher tier than 360p/540p. The default (unset)
/// resolution is 720p.
fn is_high_res(resolution: Option<CommonResolutionEnum>) -> bool {
match resolution {
None => true, // defaults to 720p
Some(CommonResolutionEnum::SevenTwentyP)
| Some(CommonResolutionEnum::TenEightyP)
| Some(CommonResolutionEnum::OneK)
| Some(CommonResolutionEnum::TwoK)
| Some(CommonResolutionEnum::ThreeK)
| Some(CommonResolutionEnum::FourK) => true,
Some(CommonResolutionEnum::HalfK)
| Some(CommonResolutionEnum::FourEightyP) => false,
}
}
#[cfg(test)]
mod tests {
use enums::common::generation::common_resolution::CommonResolution as CommonResolutionEnum;
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::artcraft::vidu_q3::build::build_artcraft_vidu_q3_state;
use crate::generate::generate_video::providers::artcraft::vidu_q3::cost::ArtcraftViduQ3CostState;
fn cost_cents(duration_seconds: Option<u16>, resolution: Option<RouterResolution>) -> u64 {
let b = GenerateVideoRequestBuilder {
model: RouterVideoModel::ViduQ3,
provider: RouterProvider::Artcraft,
prompt: Some("test".to_string()),
duration_seconds,
resolution,
..Default::default()
};
let state = build_artcraft_vidu_q3_state(b).unwrap();
ArtcraftViduQ3CostState::from_request(&state).estimate_cost().cost_in_usd_cents.unwrap()
}
#[test]
fn default_resolution_5s_is_81() { assert_eq!(cost_cents(Some(5), None), 81); }
#[test]
fn default_duration_is_5s() { assert_eq!(cost_cents(None, None), 81); }
#[test]
fn high_res_10s_is_162() { assert_eq!(cost_cents(Some(10), Some(RouterResolution::TenEightyP)), 162); }
#[test]
fn low_res_5s_is_37() { assert_eq!(cost_cents(Some(5), Some(RouterResolution::FourEightyP)), 37); }
#[test]
fn low_res_10s_is_74() { assert_eq!(cost_cents(Some(10), Some(RouterResolution::FourEightyP)), 74); }
#[test]
fn seven_twenty_p_is_high_res_tier() {
assert_eq!(cost_cents(Some(8), Some(RouterResolution::SevenTwentyP)), 130);
}
#[test]
fn odd_duration_rounds_up_to_whole_cents() {
// 7s high res: 1617 * 7 = 11319 hundredth-cents → 114 cents.
assert_eq!(cost_cents(Some(7), None), 114);
}
#[test]
fn resolution_classifier_defaults_high() {
assert!(super::is_high_res(None));
assert!(super::is_high_res(Some(CommonResolutionEnum::TenEightyP)));
assert!(!super::is_high_res(Some(CommonResolutionEnum::FourEightyP)));
}
}
@@ -0,0 +1,3 @@
pub mod build;
pub mod cost;
pub mod request;
@@ -0,0 +1,117 @@
use artcraft_api_defs::omni_gen::cost_and_generate_requests::omni_gen_video_cost_and_generate_request::OmniGenVideoCostAndGenerateRequest;
use crate::client::router_artcraft_client::RouterArtcraftClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::providers::artcraft::request_common::send_artcraft_omni_video_request;
#[derive(Clone, Debug)]
pub struct ArtcraftViduQ3RequestState {
pub request: OmniGenVideoCostAndGenerateRequest,
}
impl ArtcraftViduQ3RequestState {
pub async fn send(&self, client: &RouterArtcraftClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
send_artcraft_omni_video_request(&self.request, client).await
}
}
#[cfg(test)]
mod tests {
use tokens::tokens::media_files::MediaFileToken;
use artcraft_client::credentials::storyteller_credential_set::StorytellerCredentialSet;
use artcraft_client::utils::api_host::ApiHost;
use test_data::web::image_media_tokens::JUNO_AT_LAKE_PRODUCTION_MEDIA_TOKEN;
use crate::api::image_ref::ImageRef;
use crate::api::image_list_ref::ImageListRef;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use crate::client::router_artcraft_client::RouterArtcraftClient;
use crate::client::router_client::RouterClient;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
#[tokio::test]
#[ignore] // sends a real generation to the Artcraft backend, incurs cost
async fn text_to_video() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("A red kite soaring over coastal cliffs, cinematic.".to_string()),
aspect_ratio: Some(RouterAspectRatio::WideSixteenByNine),
..artcraft_builder()
}).await;
assert!(matches!(response, GenerateVideoResponse::Artcraft(_)));
assert_eq!(1, 2, "Inspect output above");
}
#[tokio::test]
#[ignore] // sends a real generation to the Artcraft backend, incurs cost
async fn keyframe_start_frame() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("The dog stands up and walks toward the lake.".to_string()),
start_frame: Some(ImageRef::MediaFileToken(MediaFileToken::new(JUNO_AT_LAKE_PRODUCTION_MEDIA_TOKEN.to_string()))),
..artcraft_builder()
}).await;
assert!(matches!(response, GenerateVideoResponse::Artcraft(_)));
assert_eq!(1, 2, "Inspect output above");
}
#[tokio::test]
#[ignore] // sends a real generation to the Artcraft backend, incurs cost
async fn reference_images_to_video() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("The dog explores a mossy forest clearing.".to_string()),
reference_images: Some(ImageListRef::MediaFileTokens(vec![MediaFileToken::new(JUNO_AT_LAKE_PRODUCTION_MEDIA_TOKEN.to_string())])),
..artcraft_builder()
}).await;
assert!(matches!(response, GenerateVideoResponse::Artcraft(_)));
assert_eq!(1, 2, "Inspect output above");
}
// ── Helpers ──
fn artcraft_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::ViduQ3,
provider: RouterProvider::Artcraft,
duration_seconds: Some(5),
video_batch_count: Some(1),
..Default::default()
}
}
fn get_artcraft_client() -> RouterClient {
let cookies = std::fs::read_to_string("/Users/bt/Artcraft/credentials/artcraft_cookies.txt")
.expect("Failed to read artcraft cookies");
let cookies = cookies.trim().to_string();
let credentials = StorytellerCredentialSet::parse_multi_cookie_header(&cookies)
.expect("Failed to parse cookies")
.expect("No credentials found");
RouterClient::Artcraft(RouterArtcraftClient::new(ApiHost::Storyteller, credentials))
}
async fn run_pipeline(builder: GenerateVideoRequestBuilder) -> GenerateVideoResponse {
let client = get_artcraft_client();
let draft_or_request = builder.build2().expect("build2 should succeed");
let request = match draft_or_request {
VideoGenerationDraftOrRequest::Request(r) => r,
_ => panic!("expected Request variant (Artcraft skips draft)"),
};
let response = request.send_request(&client).await.expect("send_request should succeed");
match &response {
GenerateVideoResponse::Artcraft(p) => {
println!("inference_job_token={:?}", p.inference_job_token);
println!("all_inference_job_tokens={:?}", p.all_inference_job_tokens);
}
other => println!("response: {:?}", other),
}
response
}
}
@@ -0,0 +1,29 @@
use enums::common::generation::common_video_model::CommonVideoModel as CommonVideoModelEnum;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::artcraft::build_common::{
build_artcraft_omni_video_request, SupportedResolutions, UltraWideSupport,
};
use crate::generate::generate_video::providers::artcraft::vidu_q3_turbo::request::ArtcraftViduQ3TurboRequestState;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
pub fn build_artcraft_vidu_q3_turbo(builder: GenerateVideoRequestBuilder) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_artcraft_vidu_q3_turbo_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::ArtcraftViduQ3Turbo(state)))
}
pub(crate) fn build_artcraft_vidu_q3_turbo_state(builder: GenerateVideoRequestBuilder) -> Result<ArtcraftViduQ3TurboRequestState, ArtcraftRouterError> {
// Vidu Q3 Turbo supports `audio` — preserve it so cost can read it back.
// build_artcraft_omni_video_request hardcodes generate_audio = None on its output.
let generate_audio = builder.generate_audio;
let mut request = build_artcraft_omni_video_request(
builder,
CommonVideoModelEnum::ViduQ3Turbo,
SupportedResolutions::Full,
UltraWideSupport::Unsupported,
)?;
request.generate_audio = generate_audio;
Ok(ArtcraftViduQ3TurboRequestState { request })
}
@@ -0,0 +1,104 @@
use enums::common::generation::common_resolution::CommonResolution as CommonResolutionEnum;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::artcraft::vidu_q3_turbo::request::ArtcraftViduQ3TurboRequestState;
/// Per-second rates in hundredths of a US cent.
const LOW_RES_RATE_HUNDREDTH_CENTS_PER_SEC: u64 = 368;
const HIGH_RES_RATE_HUNDREDTH_CENTS_PER_SEC: u64 = 809;
#[derive(Clone, Debug)]
pub struct ArtcraftViduQ3TurboCostState {
pub duration_seconds: u64,
pub is_high_res: bool,
}
impl ArtcraftViduQ3TurboCostState {
pub fn from_request(request: &ArtcraftViduQ3TurboRequestState) -> Self {
Self {
// Vidu Q3 Turbo defaults None → 5s.
duration_seconds: request.request.duration_seconds.map(u64::from).unwrap_or(5),
is_high_res: is_high_res(request.request.resolution),
}
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
let rate = if self.is_high_res {
HIGH_RES_RATE_HUNDREDTH_CENTS_PER_SEC
} else {
LOW_RES_RATE_HUNDREDTH_CENTS_PER_SEC
};
// Round up to the next whole cent.
let cost_in_usd_cents = (rate * self.duration_seconds).div_ceil(100);
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
has_watermark: false,
failures_are_refunded: None,
}
}
}
/// Vidu prices 720p/1080p at a higher tier than 360p/540p. The default (unset)
/// resolution is 720p.
fn is_high_res(resolution: Option<CommonResolutionEnum>) -> bool {
match resolution {
None => true, // defaults to 720p
Some(CommonResolutionEnum::SevenTwentyP)
| Some(CommonResolutionEnum::TenEightyP)
| Some(CommonResolutionEnum::OneK)
| Some(CommonResolutionEnum::TwoK)
| Some(CommonResolutionEnum::ThreeK)
| Some(CommonResolutionEnum::FourK) => true,
Some(CommonResolutionEnum::HalfK)
| Some(CommonResolutionEnum::FourEightyP) => false,
}
}
#[cfg(test)]
mod tests {
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::artcraft::vidu_q3_turbo::build::build_artcraft_vidu_q3_turbo_state;
use crate::generate::generate_video::providers::artcraft::vidu_q3_turbo::cost::ArtcraftViduQ3TurboCostState;
fn cost_cents(duration_seconds: Option<u16>, resolution: Option<RouterResolution>) -> u64 {
let b = GenerateVideoRequestBuilder {
model: RouterVideoModel::ViduQ3Turbo,
provider: RouterProvider::Artcraft,
prompt: Some("test".to_string()),
duration_seconds,
resolution,
..Default::default()
};
let state = build_artcraft_vidu_q3_turbo_state(b).unwrap();
ArtcraftViduQ3TurboCostState::from_request(&state).estimate_cost().cost_in_usd_cents.unwrap()
}
#[test]
fn default_resolution_5s_is_41() { assert_eq!(cost_cents(Some(5), None), 41); }
#[test]
fn default_duration_is_5s() { assert_eq!(cost_cents(None, None), 41); }
#[test]
fn high_res_10s_is_81() { assert_eq!(cost_cents(Some(10), Some(RouterResolution::TenEightyP)), 81); }
#[test]
fn low_res_5s_is_19() { assert_eq!(cost_cents(Some(5), Some(RouterResolution::FourEightyP)), 19); }
#[test]
fn low_res_10s_is_37() { assert_eq!(cost_cents(Some(10), Some(RouterResolution::FourEightyP)), 37); }
#[test]
fn odd_duration_rounds_up_to_whole_cents() {
// 7s high res: 809 * 7 = 5663 hundredth-cents → 57 cents.
assert_eq!(cost_cents(Some(7), None), 57);
}
}
@@ -0,0 +1,3 @@
pub mod build;
pub mod cost;
pub mod request;
@@ -0,0 +1,104 @@
use artcraft_api_defs::omni_gen::cost_and_generate_requests::omni_gen_video_cost_and_generate_request::OmniGenVideoCostAndGenerateRequest;
use crate::client::router_artcraft_client::RouterArtcraftClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::providers::artcraft::request_common::send_artcraft_omni_video_request;
#[derive(Clone, Debug)]
pub struct ArtcraftViduQ3TurboRequestState {
pub request: OmniGenVideoCostAndGenerateRequest,
}
impl ArtcraftViduQ3TurboRequestState {
pub async fn send(&self, client: &RouterArtcraftClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
send_artcraft_omni_video_request(&self.request, client).await
}
}
#[cfg(test)]
mod tests {
use tokens::tokens::media_files::MediaFileToken;
use artcraft_client::credentials::storyteller_credential_set::StorytellerCredentialSet;
use artcraft_client::utils::api_host::ApiHost;
use test_data::web::image_media_tokens::JUNO_AT_LAKE_PRODUCTION_MEDIA_TOKEN;
use crate::api::image_ref::ImageRef;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use crate::client::router_artcraft_client::RouterArtcraftClient;
use crate::client::router_client::RouterClient;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
#[tokio::test]
#[ignore] // sends a real generation to the Artcraft backend, incurs cost
async fn text_to_video() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("A paper boat drifting down a rainy gutter stream.".to_string()),
aspect_ratio: Some(RouterAspectRatio::WideSixteenByNine),
..artcraft_builder()
}).await;
assert!(matches!(response, GenerateVideoResponse::Artcraft(_)));
assert_eq!(1, 2, "Inspect output above");
}
#[tokio::test]
#[ignore] // sends a real generation to the Artcraft backend, incurs cost
async fn keyframe_start_frame() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("The dog shakes off water in slow motion.".to_string()),
start_frame: Some(ImageRef::MediaFileToken(MediaFileToken::new(JUNO_AT_LAKE_PRODUCTION_MEDIA_TOKEN.to_string()))),
..artcraft_builder()
}).await;
assert!(matches!(response, GenerateVideoResponse::Artcraft(_)));
assert_eq!(1, 2, "Inspect output above");
}
// ── Helpers ──
fn artcraft_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::ViduQ3Turbo,
provider: RouterProvider::Artcraft,
duration_seconds: Some(5),
video_batch_count: Some(1),
..Default::default()
}
}
fn get_artcraft_client() -> RouterClient {
let cookies = std::fs::read_to_string("/Users/bt/Artcraft/credentials/artcraft_cookies.txt")
.expect("Failed to read artcraft cookies");
let cookies = cookies.trim().to_string();
let credentials = StorytellerCredentialSet::parse_multi_cookie_header(&cookies)
.expect("Failed to parse cookies")
.expect("No credentials found");
RouterClient::Artcraft(RouterArtcraftClient::new(ApiHost::Storyteller, credentials))
}
async fn run_pipeline(builder: GenerateVideoRequestBuilder) -> GenerateVideoResponse {
let client = get_artcraft_client();
let draft_or_request = builder.build2().expect("build2 should succeed");
let request = match draft_or_request {
VideoGenerationDraftOrRequest::Request(r) => r,
_ => panic!("expected Request variant (Artcraft skips draft)"),
};
let response = request.send_request(&client).await.expect("send_request should succeed");
match &response {
GenerateVideoResponse::Artcraft(p) => {
println!("inference_job_token={:?}", p.inference_job_token);
println!("all_inference_job_tokens={:?}", p.all_inference_job_tokens);
}
other => println!("response: {:?}", other),
}
response
}
}
@@ -1,10 +1,10 @@
use fal_client::requests_old::webhook::video::image::enqueue_kling_3p0_standard_image_to_video_webhook::{
EnqueueKling3p0StandardImageToVideoAspectRatio, EnqueueKling3p0StandardImageToVideoDuration,
EnqueueKling3p0StandardImageToVideoRequest,
use fal_client::requests::api::video::image::kling_3p0_standard_image_to_video::api::{
Kling3p0StandardImageToVideoAspectRatio, Kling3p0StandardImageToVideoDuration,
Kling3p0StandardImageToVideoRequest,
};
use fal_client::requests_old::webhook::video::text::enqueue_kling_3p0_standard_text_to_video_webhook::{
EnqueueKling3p0StandardTextToVideoAspectRatio, EnqueueKling3p0StandardTextToVideoDuration,
EnqueueKling3p0StandardTextToVideoRequest,
use fal_client::requests::api::video::text::kling_3p0_standard_text_to_video::api::{
Kling3p0StandardTextToVideoAspectRatio, Kling3p0StandardTextToVideoDuration,
Kling3p0StandardTextToVideoRequest,
};
use crate::errors::artcraft_router_error::ArtcraftRouterError;
@@ -23,6 +23,13 @@ use crate::generate::generate_video::video_generation_request::VideoGenerationRe
pub fn build_fal_kling_3p0_standard(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_fal_kling_3p0_standard_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalKling3p0Standard(state)))
}
pub(crate) fn build_fal_kling_3p0_standard_state(
builder: GenerateVideoRequestBuilder,
) -> Result<FalKling3p0StandardRequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
let aspect_ratio = plan_aspect_ratio(builder.aspect_ratio, strategy)?;
@@ -39,7 +46,7 @@ pub fn build_fal_kling_3p0_standard(
value: "Kling 3.0 Standard requires a start_frame when end_frame is provided".to_string(),
}));
}
FalKling3p0StandardMode::TextToVideo(EnqueueKling3p0StandardTextToVideoRequest {
FalKling3p0StandardMode::TextToVideo(Kling3p0StandardTextToVideoRequest {
prompt,
generate_audio,
negative_prompt,
@@ -48,7 +55,7 @@ pub fn build_fal_kling_3p0_standard(
shot_type: None,
})
}
Some(image_url) => FalKling3p0StandardMode::ImageToVideo(EnqueueKling3p0StandardImageToVideoRequest {
Some(image_url) => FalKling3p0StandardMode::ImageToVideo(Kling3p0StandardImageToVideoRequest {
prompt,
image_url,
end_image_url: optional_url(builder.end_frame.clone())?,
@@ -60,13 +67,11 @@ pub fn build_fal_kling_3p0_standard(
}),
};
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalKling3p0Standard(
FalKling3p0StandardRequestState { mode },
)))
Ok(FalKling3p0StandardRequestState { mode })
}
fn to_t2v_duration(d: PlanDuration) -> EnqueueKling3p0StandardTextToVideoDuration {
use EnqueueKling3p0StandardTextToVideoDuration as D;
fn to_t2v_duration(d: PlanDuration) -> Kling3p0StandardTextToVideoDuration {
use Kling3p0StandardTextToVideoDuration as D;
match d.0 {
3 => D::ThreeSeconds,
4 => D::FourSeconds,
@@ -84,8 +89,8 @@ fn to_t2v_duration(d: PlanDuration) -> EnqueueKling3p0StandardTextToVideoDuratio
}
}
fn to_i2v_duration(d: PlanDuration) -> EnqueueKling3p0StandardImageToVideoDuration {
use EnqueueKling3p0StandardImageToVideoDuration as D;
fn to_i2v_duration(d: PlanDuration) -> Kling3p0StandardImageToVideoDuration {
use Kling3p0StandardImageToVideoDuration as D;
match d.0 {
3 => D::ThreeSeconds,
4 => D::FourSeconds,
@@ -103,19 +108,19 @@ fn to_i2v_duration(d: PlanDuration) -> EnqueueKling3p0StandardImageToVideoDurati
}
}
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> EnqueueKling3p0StandardTextToVideoAspectRatio {
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> Kling3p0StandardTextToVideoAspectRatio {
match a {
PlanAspectRatio::Square => EnqueueKling3p0StandardTextToVideoAspectRatio::Square,
PlanAspectRatio::SixteenByNine => EnqueueKling3p0StandardTextToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => EnqueueKling3p0StandardTextToVideoAspectRatio::NineBySixteen,
PlanAspectRatio::Square => Kling3p0StandardTextToVideoAspectRatio::Square,
PlanAspectRatio::SixteenByNine => Kling3p0StandardTextToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Kling3p0StandardTextToVideoAspectRatio::NineBySixteen,
}
}
fn to_i2v_aspect_ratio(a: PlanAspectRatio) -> EnqueueKling3p0StandardImageToVideoAspectRatio {
fn to_i2v_aspect_ratio(a: PlanAspectRatio) -> Kling3p0StandardImageToVideoAspectRatio {
match a {
PlanAspectRatio::Square => EnqueueKling3p0StandardImageToVideoAspectRatio::Square,
PlanAspectRatio::SixteenByNine => EnqueueKling3p0StandardImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => EnqueueKling3p0StandardImageToVideoAspectRatio::NineBySixteen,
PlanAspectRatio::Square => Kling3p0StandardImageToVideoAspectRatio::Square,
PlanAspectRatio::SixteenByNine => Kling3p0StandardImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Kling3p0StandardImageToVideoAspectRatio::NineBySixteen,
}
}
@@ -127,6 +132,27 @@ mod tests {
use super::*;
#[test]
fn no_start_frame_picks_t2v() {
let state = build_fal_kling_3p0_standard_state(base_builder()).expect("build");
assert!(matches!(state.mode, FalKling3p0StandardMode::TextToVideo(_)));
}
#[test]
fn start_frame_picks_i2v() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
let state = build_fal_kling_3p0_standard_state(b).expect("build");
assert!(matches!(state.mode, FalKling3p0StandardMode::ImageToVideo(_)));
}
#[test]
fn end_frame_without_start_frame_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url("https://example.com/end.png".to_string()));
assert!(build_fal_kling_3p0_standard_state(b).is_err());
}
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Kling3p0Standard,
@@ -135,29 +161,4 @@ mod tests {
..Default::default()
}
}
#[test]
fn no_start_frame_picks_t2v() {
let result = build_fal_kling_3p0_standard(base_builder()).expect("build");
if let VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalKling3p0Standard(s)) = result {
assert!(matches!(s.mode, FalKling3p0StandardMode::TextToVideo(_)));
} else { panic!("expected FalKling3p0Standard"); }
}
#[test]
fn start_frame_picks_i2v() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
let result = build_fal_kling_3p0_standard(b).expect("build");
if let VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalKling3p0Standard(s)) = result {
assert!(matches!(s.mode, FalKling3p0StandardMode::ImageToVideo(_)));
} else { panic!("expected FalKling3p0Standard"); }
}
#[test]
fn end_frame_without_start_frame_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url("https://example.com/end.png".to_string()));
assert!(build_fal_kling_3p0_standard(b).is_err());
}
}
@@ -1,5 +1,4 @@
use fal_client::requests_old::webhook::video::image::enqueue_kling_3p0_standard_image_to_video_webhook::EnqueueKling3p0StandardImageToVideoDuration;
use fal_client::requests_old::webhook::video::text::enqueue_kling_3p0_standard_text_to_video_webhook::EnqueueKling3p0StandardTextToVideoDuration;
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::fal::kling_3p0_standard::request::{
@@ -8,35 +7,25 @@ use crate::generate::generate_video::providers::fal::kling_3p0_standard::request
#[derive(Clone, Debug)]
pub struct FalKling3p0StandardCostState {
pub duration_seconds: u64,
pub generate_audio: bool,
pub cost_in_usd_cents: u64,
}
impl FalKling3p0StandardCostState {
pub fn from_request(request: &FalKling3p0StandardRequestState) -> Self {
let (duration_seconds, generate_audio) = match &request.mode {
FalKling3p0StandardMode::TextToVideo(req) => (
t2v_duration_seconds(req.duration),
req.generate_audio.unwrap_or(true),
),
FalKling3p0StandardMode::ImageToVideo(req) => (
i2v_duration_seconds(req.duration),
req.generate_audio.unwrap_or(true),
),
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalKling3p0StandardMode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalKling3p0StandardMode::ImageToVideo(req) => req.calculate_cost_in_cents(),
};
Self { duration_seconds, generate_audio }
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
// Mirrors fal_client kling_3p0_standard:
// audio off: $0.168/sec (rate=168)
// audio on: $0.252/sec (rate=252)
let rate: u64 = if self.generate_audio { 252 } else { 168 };
let cost_in_usd_cents = (rate * self.duration_seconds + 9) / 10;
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
@@ -46,64 +35,17 @@ impl FalKling3p0StandardCostState {
}
}
/// Default duration is 5s when None per Fal client.
fn t2v_duration_seconds(d: Option<EnqueueKling3p0StandardTextToVideoDuration>) -> u64 {
use EnqueueKling3p0StandardTextToVideoDuration as D;
match d {
None => 5,
Some(D::ThreeSeconds) => 3,
Some(D::FourSeconds) => 4,
Some(D::FiveSeconds) => 5,
Some(D::SixSeconds) => 6,
Some(D::SevenSeconds) => 7,
Some(D::EightSeconds) => 8,
Some(D::NineSeconds) => 9,
Some(D::TenSeconds) => 10,
Some(D::ElevenSeconds) => 11,
Some(D::TwelveSeconds) => 12,
Some(D::ThirteenSeconds) => 13,
Some(D::FourteenSeconds) => 14,
Some(D::FifteenSeconds) => 15,
}
}
fn i2v_duration_seconds(d: Option<EnqueueKling3p0StandardImageToVideoDuration>) -> u64 {
use EnqueueKling3p0StandardImageToVideoDuration as D;
match d {
None => 5,
Some(D::ThreeSeconds) => 3,
Some(D::FourSeconds) => 4,
Some(D::FiveSeconds) => 5,
Some(D::SixSeconds) => 6,
Some(D::SevenSeconds) => 7,
Some(D::EightSeconds) => 8,
Some(D::NineSeconds) => 9,
Some(D::TenSeconds) => 10,
Some(D::ElevenSeconds) => 11,
Some(D::TwelveSeconds) => 12,
Some(D::ThirteenSeconds) => 13,
Some(D::FourteenSeconds) => 14,
Some(D::FifteenSeconds) => 15,
}
}
#[cfg(test)]
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::kling_3p0_standard::build::build_fal_kling_3p0_standard_state;
fn cost_cents(duration_seconds: Option<u16>, generate_audio: Option<bool>) -> u64 {
let b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Kling3p0Standard,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
generate_audio,
..Default::default()
};
b.build2().unwrap().estimate_cost().unwrap().cost_in_usd_cents.unwrap()
}
use super::*;
// Pricing (fal): audio off $0.168/sec (rate=168 tenths-of-cents), audio on
// $0.252/sec (rate=252), rounded up to whole cents.
#[test]
fn audio_on_5s_is_126() {
@@ -127,4 +69,20 @@ mod tests {
// (168*15 + 9) / 10 = 2529/10 = 252.
assert_eq!(cost_cents(Some(15), Some(false)), 252);
}
fn cost_cents(duration_seconds: Option<u16>, generate_audio: Option<bool>) -> u64 {
let b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Kling3p0Standard,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
generate_audio,
..Default::default()
};
let state = build_fal_kling_3p0_standard_state(b).expect("build_fal_kling_3p0_standard_state");
FalKling3p0StandardCostState::from_request(&state)
.estimate_cost()
.cost_in_usd_cents
.expect("cost_in_usd_cents")
}
}
@@ -1,27 +1,20 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests_old::webhook::video::image::enqueue_kling_3p0_standard_image_to_video_webhook::{
enqueue_kling_3p0_standard_image_to_video_webhook, EnqueueKling3p0StandardImageToVideoArgs,
EnqueueKling3p0StandardImageToVideoRequest,
};
use fal_client::requests_old::webhook::video::text::enqueue_kling_3p0_standard_text_to_video_webhook::{
enqueue_kling_3p0_standard_text_to_video_webhook, EnqueueKling3p0StandardTextToVideoArgs,
EnqueueKling3p0StandardTextToVideoRequest,
};
use fal_client::requests::api::video::image::kling_3p0_standard_image_to_video::api::Kling3p0StandardImageToVideoRequest;
use fal_client::requests::api::video::text::kling_3p0_standard_text_to_video::api::Kling3p0StandardTextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::errors::provider_error::ProviderError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
#[derive(Clone, Debug)]
pub enum FalKling3p0StandardMode {
TextToVideo(EnqueueKling3p0StandardTextToVideoRequest),
ImageToVideo(EnqueueKling3p0StandardImageToVideoRequest),
TextToVideo(Kling3p0StandardTextToVideoRequest),
ImageToVideo(Kling3p0StandardImageToVideoRequest),
}
#[derive(Clone, Debug)]
@@ -31,38 +24,56 @@ pub struct FalKling3p0StandardRequestState {
impl FalKling3p0StandardRequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
let webhook_url = client.webhook_url.as_deref()
.ok_or(ArtcraftRouterError::Client(ClientError::WebhookUrlRequired))?;
let (webhook_response, outbound_request): (_, Arc<dyn Debug + Send + Sync>) = match &self.mode {
FalKling3p0StandardMode::TextToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = EnqueueKling3p0StandardTextToVideoArgs {
request: request.clone(),
webhook_url,
api_key: &client.api_key,
};
(enqueue_kling_3p0_standard_text_to_video_webhook(args).await, outbound)
}
FalKling3p0StandardMode::ImageToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = EnqueueKling3p0StandardImageToVideoArgs {
request: request.clone(),
webhook_url,
api_key: &client.api_key,
};
(enqueue_kling_3p0_standard_image_to_video_webhook(args).await, outbound)
}
};
match &self.mode {
FalKling3p0StandardMode::TextToVideo(request) => send_request(request, client).await,
FalKling3p0StandardMode::ImageToVideo(request) => send_request(request, client).await,
}
}
}
let webhook_response = webhook_response
.map_err(|e| ArtcraftRouterError::Provider(ProviderError::Fal(e)))?;
// ── Helpers ──
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: webhook_response.request_id,
gateway_request_id: webhook_response.gateway_request_id,
maybe_status_url: None,
maybe_response_url: None,
maybe_outbound_request: Some(outbound_request),
}))
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
@@ -14,3 +14,6 @@ pub mod veo_3;
pub mod veo_3_fast;
pub mod veo_3p1;
pub mod veo_3p1_fast;
pub mod veo_3p1_lite;
pub mod vidu_q3;
pub mod vidu_q3_turbo;
@@ -1,7 +1,9 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_2_image_to_video_webhook::{
Veo2AspectRatio, Veo2Duration, Veo2Request,
use fal_client::requests::api::video::image::veo_2::api::{
Veo2ImageToVideoDuration, Veo2ImageToVideoRequest,
};
use fal_client::requests::api::video::text::veo_2::api::{
Veo2TextToVideoAspectRatio, Veo2TextToVideoDuration, Veo2TextToVideoRequest,
};
use fal_client::requests_old::webhook::video::text::enqueue_veo_2_text_to_video_webhook::Veo2TextToVideoRequest;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::image_ref::ImageRef;
@@ -13,9 +15,26 @@ use crate::generate::generate_video::providers::fal::veo_2::request::{FalVeo2Mod
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
/// Router-level duration plan shared by both modes — each mode maps it onto
/// its own fal duration enum.
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanDuration {
Five,
Six,
Seven,
Eight,
}
pub fn build_fal_veo_2(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_fal_veo_2_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo2(state)))
}
pub(crate) fn build_fal_veo_2_state(
builder: GenerateVideoRequestBuilder,
) -> Result<FalVeo2RequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
if builder.end_frame.is_some() {
@@ -27,10 +46,10 @@ pub fn build_fal_veo_2(
let duration = plan_duration(builder.duration_seconds, strategy)?;
let mode = match builder.start_frame.clone() {
Some(ImageRef::Url(url)) => FalVeo2Mode::ImageToVideo(Veo2Request {
image_url: url,
Some(ImageRef::Url(url)) => FalVeo2Mode::ImageToVideo(Veo2ImageToVideoRequest {
prompt,
duration,
image_url: url,
duration: duration.map(to_i2v_duration),
}),
Some(ImageRef::MediaFileToken(_)) => {
return Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls));
@@ -39,38 +58,41 @@ pub fn build_fal_veo_2(
let aspect_ratio = plan_aspect_ratio(builder.aspect_ratio, strategy)?;
FalVeo2Mode::TextToVideo(Veo2TextToVideoRequest {
prompt,
negative_prompt,
duration,
aspect_ratio,
duration: duration.map(to_t2v_duration),
negative_prompt,
enhance_prompt: None,
seed: None,
auto_fix: None,
})
}
};
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo2(
FalVeo2RequestState { mode },
)))
Ok(FalVeo2RequestState { mode })
}
/// `None` lets fal pick its default (16:9); the old endpoint's explicit "auto"
/// no longer exists in the new API, so Auto maps to unset.
fn plan_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo2AspectRatio, ArtcraftRouterError> {
use Veo2AspectRatio as Ar;
) -> Result<Option<Veo2TextToVideoAspectRatio>, ArtcraftRouterError> {
use Veo2TextToVideoAspectRatio as Ar;
match aspect_ratio {
None
| Some(RouterAspectRatio::Auto)
| Some(RouterAspectRatio::Auto2k)
| Some(RouterAspectRatio::Auto3k)
| Some(RouterAspectRatio::Auto4k) => Ok(Ar::Auto),
| Some(RouterAspectRatio::Auto4k) => Ok(None),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Ar::WideSixteenNine),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Ar::TallNineSixteen),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Some(Ar::SixteenByNine)),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Some(Ar::NineBySixteen)),
Some(unsupported_ar) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("aspect_ratio", &format!("{:?}", unsupported_ar)))
}
_ => Ok(Ar::Auto),
_ => Ok(None),
},
}
}
@@ -78,19 +100,19 @@ fn plan_aspect_ratio(
fn plan_duration(
duration_seconds: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo2Duration, ArtcraftRouterError> {
) -> Result<Option<PlanDuration>, ArtcraftRouterError> {
match duration_seconds {
None => Ok(Veo2Duration::Default),
Some(5) => Ok(Veo2Duration::FiveSeconds),
Some(6) => Ok(Veo2Duration::SixSeconds),
Some(7) => Ok(Veo2Duration::SevenSeconds),
Some(8) => Ok(Veo2Duration::EightSeconds),
None => Ok(None),
Some(5) => Ok(Some(PlanDuration::Five)),
Some(6) => Ok(Some(PlanDuration::Six)),
Some(7) => Ok(Some(PlanDuration::Seven)),
Some(8) => Ok(Some(PlanDuration::Eight)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("duration_seconds", &format!("{}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Veo2Duration::EightSeconds),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Veo2Duration::FiveSeconds),
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanDuration::Eight)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanDuration::Five)),
},
}
}
@@ -102,45 +124,33 @@ fn unsupported(field: &'static str, value: &str) -> ArtcraftRouterError {
})
}
fn to_t2v_duration(d: PlanDuration) -> Veo2TextToVideoDuration {
match d {
PlanDuration::Five => Veo2TextToVideoDuration::FiveSeconds,
PlanDuration::Six => Veo2TextToVideoDuration::SixSeconds,
PlanDuration::Seven => Veo2TextToVideoDuration::SevenSeconds,
PlanDuration::Eight => Veo2TextToVideoDuration::EightSeconds,
}
}
fn to_i2v_duration(d: PlanDuration) -> Veo2ImageToVideoDuration {
match d {
PlanDuration::Five => Veo2ImageToVideoDuration::FiveSeconds,
PlanDuration::Six => Veo2ImageToVideoDuration::SixSeconds,
PlanDuration::Seven => Veo2ImageToVideoDuration::SevenSeconds,
PlanDuration::Eight => Veo2ImageToVideoDuration::EightSeconds,
}
}
#[cfg(test)]
mod tests {
use tokens::tokens::media_files::MediaFileToken;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
use super::*;
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo2,
provider: RouterProvider::Fal,
prompt: Some("a corgi running".to_string()),
..Default::default()
}
}
fn t2v_request(b: GenerateVideoRequestBuilder) -> Veo2TextToVideoRequest {
match build_fal_veo_2(b).expect("build_fal_veo_2") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo2(state)) => match state.mode {
FalVeo2Mode::TextToVideo(r) => r,
FalVeo2Mode::ImageToVideo(_) => panic!("expected TextToVideo"),
},
_ => panic!("expected FalVeo2 request"),
}
}
fn i2v_request(b: GenerateVideoRequestBuilder) -> Veo2Request {
match build_fal_veo_2(b).expect("build_fal_veo_2") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo2(state)) => match state.mode {
FalVeo2Mode::ImageToVideo(r) => r,
FalVeo2Mode::TextToVideo(_) => panic!("expected ImageToVideo"),
},
_ => panic!("expected FalVeo2 request"),
}
}
mod mode_selection {
use super::*;
@@ -160,14 +170,14 @@ mod tests {
fn end_frame_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url("https://example.com/end.png".to_string()));
assert!(build_fal_veo_2(b).is_err());
assert!(build_fal_veo_2_state(b).is_err());
}
#[test]
fn media_file_token_for_start_frame_errors() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::MediaFileToken(MediaFileToken::new("mf_x".to_string())));
assert!(build_fal_veo_2(b).is_err());
assert!(build_fal_veo_2_state(b).is_err());
}
}
@@ -200,22 +210,22 @@ mod tests {
use super::*;
#[test]
fn duration_none_is_default() {
assert!(matches!(t2v_request(base_builder()).duration, Veo2Duration::Default));
fn duration_none_is_unset() {
// fal defaults to 5s when unset.
assert!(t2v_request(base_builder()).duration.is_none());
}
#[test]
fn duration_5_through_8() {
for (seconds, expected) in [
(5, Veo2Duration::FiveSeconds),
(6, Veo2Duration::SixSeconds),
(7, Veo2Duration::SevenSeconds),
(8, Veo2Duration::EightSeconds),
(5, Veo2TextToVideoDuration::FiveSeconds),
(6, Veo2TextToVideoDuration::SixSeconds),
(7, Veo2TextToVideoDuration::SevenSeconds),
(8, Veo2TextToVideoDuration::EightSeconds),
] {
let mut b = base_builder();
b.duration_seconds = Some(seconds);
let got = t2v_request(b).duration;
assert_eq!(format!("{:?}", got), format!("{:?}", expected));
assert_eq!(t2v_request(b).duration, Some(expected));
}
}
@@ -224,7 +234,7 @@ mod tests {
let mut b = base_builder();
b.duration_seconds = Some(10);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_veo_2(b).is_err());
assert!(build_fal_veo_2_state(b).is_err());
}
#[test]
@@ -232,7 +242,7 @@ mod tests {
let mut b = base_builder();
b.duration_seconds = Some(20);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayMoreUpgrade;
assert!(matches!(t2v_request(b).duration, Veo2Duration::EightSeconds));
assert_eq!(t2v_request(b).duration, Some(Veo2TextToVideoDuration::EightSeconds));
}
#[test]
@@ -240,7 +250,7 @@ mod tests {
let mut b = base_builder();
b.duration_seconds = Some(2);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayLessDowngrade;
assert!(matches!(t2v_request(b).duration, Veo2Duration::FiveSeconds));
assert_eq!(t2v_request(b).duration, Some(Veo2TextToVideoDuration::FiveSeconds));
}
}
@@ -249,17 +259,17 @@ mod tests {
use crate::api::router_aspect_ratio::RouterAspectRatio;
#[test]
fn t2v_auto() {
fn t2v_auto_is_unset() {
let mut b = base_builder();
b.aspect_ratio = Some(RouterAspectRatio::Auto);
assert!(matches!(t2v_request(b).aspect_ratio, Veo2AspectRatio::Auto));
assert!(t2v_request(b).aspect_ratio.is_none());
}
#[test]
fn t2v_sixteen_nine() {
let mut b = base_builder();
b.aspect_ratio = Some(RouterAspectRatio::WideSixteenByNine);
assert!(matches!(t2v_request(b).aspect_ratio, Veo2AspectRatio::WideSixteenNine));
assert_eq!(t2v_request(b).aspect_ratio, Some(Veo2TextToVideoAspectRatio::SixteenByNine));
}
#[test]
@@ -267,7 +277,7 @@ mod tests {
let mut b = base_builder();
b.aspect_ratio = Some(RouterAspectRatio::Square);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayMoreUpgrade;
assert!(matches!(t2v_request(b).aspect_ratio, Veo2AspectRatio::Auto));
assert!(t2v_request(b).aspect_ratio.is_none());
}
#[test]
@@ -275,7 +285,7 @@ mod tests {
let mut b = base_builder();
b.aspect_ratio = Some(RouterAspectRatio::Square);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_veo_2(b).is_err());
assert!(build_fal_veo_2_state(b).is_err());
}
}
@@ -295,11 +305,34 @@ mod tests {
if has_start_frame {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
assert!(build_fal_veo_2(b).is_ok());
assert!(build_fal_veo_2_state(b).is_ok());
combos += 1;
}
}
}
assert_eq!(combos, 4 * 5 * 2);
}
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo2,
provider: RouterProvider::Fal,
prompt: Some("a corgi running".to_string()),
..Default::default()
}
}
fn t2v_request(b: GenerateVideoRequestBuilder) -> Veo2TextToVideoRequest {
match build_fal_veo_2_state(b).expect("build_fal_veo_2_state").mode {
FalVeo2Mode::TextToVideo(r) => r,
FalVeo2Mode::ImageToVideo(_) => panic!("expected TextToVideo"),
}
}
fn i2v_request(b: GenerateVideoRequestBuilder) -> Veo2ImageToVideoRequest {
match build_fal_veo_2_state(b).expect("build_fal_veo_2_state").mode {
FalVeo2Mode::ImageToVideo(r) => r,
FalVeo2Mode::TextToVideo(_) => panic!("expected ImageToVideo"),
}
}
}
@@ -1,31 +1,29 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_2_image_to_video_webhook::Veo2Duration;
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::fal::veo_2::request::{FalVeo2Mode, FalVeo2RequestState};
#[derive(Clone, Debug)]
pub struct FalVeo2CostState {
pub duration: Veo2Duration,
pub cost_in_usd_cents: u64,
}
impl FalVeo2CostState {
pub fn from_request(request: &FalVeo2RequestState) -> Self {
let duration = match &request.mode {
FalVeo2Mode::TextToVideo(req) => req.duration,
FalVeo2Mode::ImageToVideo(req) => req.duration,
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalVeo2Mode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalVeo2Mode::ImageToVideo(req) => req.calculate_cost_in_cents(),
};
Self { duration }
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
// 5s = $2.50, +$0.50 per additional second.
let seconds = duration_seconds_for_cost(self.duration);
let extra = seconds.saturating_sub(5);
let cost_in_usd_cents = 250 + extra * 50;
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
@@ -35,40 +33,18 @@ impl FalVeo2CostState {
}
}
fn duration_seconds_for_cost(d: Veo2Duration) -> u64 {
match d {
Veo2Duration::Default | Veo2Duration::FiveSeconds => 5,
Veo2Duration::SixSeconds => 6,
Veo2Duration::SevenSeconds => 7,
Veo2Duration::EightSeconds => 8,
}
}
#[cfg(test)]
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_2::build::build_fal_veo_2_state;
fn cost_cents(duration_seconds: Option<u16>, has_start_frame: bool) -> u64 {
let mut b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo2,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
..Default::default()
};
if has_start_frame {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
b.build2()
.expect("build2")
.estimate_cost()
.expect("estimate_cost")
.cost_in_usd_cents
.expect("cost_in_usd_cents")
}
use super::*;
// Pricing (fal): 5s = $2.50, +$0.50 per additional second. Flat across
// modalities; no audio or resolution knobs.
mod numeric_literal_pricing {
use super::*;
@@ -112,4 +88,22 @@ mod tests {
assert!(cost_cents(Some(6), false) < cost_cents(Some(7), false));
assert!(cost_cents(Some(7), false) < cost_cents(Some(8), false));
}
fn cost_cents(duration_seconds: Option<u16>, has_start_frame: bool) -> u64 {
let mut b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo2,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
..Default::default()
};
if has_start_frame {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
let state = build_fal_veo_2_state(b).expect("build_fal_veo_2_state");
FalVeo2CostState::from_request(&state)
.estimate_cost()
.cost_in_usd_cents
.expect("cost_in_usd_cents")
}
}
@@ -1,17 +1,12 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests_old::webhook::video::image::enqueue_veo_2_image_to_video_webhook::{
enqueue_veo_2_image_to_video_webhook, Veo2Args, Veo2Request,
};
use fal_client::requests_old::webhook::video::text::enqueue_veo_2_text_to_video_webhook::{
enqueue_veo_2_text_to_video_webhook, Veo2TextToVideoArgs, Veo2TextToVideoRequest,
};
use fal_client::requests::api::video::image::veo_2::api::Veo2ImageToVideoRequest;
use fal_client::requests::api::video::text::veo_2::api::Veo2TextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::errors::provider_error::ProviderError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
@@ -19,7 +14,7 @@ use crate::generate::generate_video::generate_video_response::{
#[derive(Clone, Debug)]
pub enum FalVeo2Mode {
TextToVideo(Veo2TextToVideoRequest),
ImageToVideo(Veo2Request),
ImageToVideo(Veo2ImageToVideoRequest),
}
#[derive(Clone, Debug)]
@@ -29,96 +24,112 @@ pub struct FalVeo2RequestState {
impl FalVeo2RequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
let webhook_url = client.webhook_url.as_deref()
.ok_or(ArtcraftRouterError::Client(ClientError::WebhookUrlRequired))?;
let (webhook_response, outbound_request): (_, Arc<dyn Debug + Send + Sync>) = match &self.mode {
FalVeo2Mode::TextToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = Veo2TextToVideoArgs {
request: request.clone(),
api_key: &client.api_key,
webhook_url,
};
(enqueue_veo_2_text_to_video_webhook(args).await, outbound)
}
FalVeo2Mode::ImageToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = Veo2Args {
request: request.clone(),
api_key: &client.api_key,
webhook_url,
};
(enqueue_veo_2_image_to_video_webhook(args).await, outbound)
}
};
match &self.mode {
FalVeo2Mode::TextToVideo(request) => send_request(request, client).await,
FalVeo2Mode::ImageToVideo(request) => send_request(request, client).await,
}
}
}
let webhook_response = webhook_response
.map_err(|e| ArtcraftRouterError::Provider(ProviderError::Fal(e)))?;
// ── Helpers ──
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: webhook_response.request_id,
gateway_request_id: webhook_response.gateway_request_id,
maybe_status_url: None,
maybe_response_url: None,
maybe_outbound_request: Some(outbound_request),
}))
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use fal_client::creds::fal_api_key::FalApiKey;
use fal_client::requests::api::video::image::veo_2::api::Veo2ImageToVideoDuration;
use fal_client::requests::api::video::text::veo_2::api::{
Veo2TextToVideoAspectRatio, Veo2TextToVideoDuration,
};
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::test_helpers::get_fal_client;
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_text_to_video_5s() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("a peaceful lake at dawn".to_string()),
aspect_ratio: Some(RouterAspectRatio::WideSixteenByNine),
duration_seconds: Some(5),
..fal_veo_2_builder()
}).await;
println!("response: {:?}", response);
let state = FalVeo2RequestState {
mode: FalVeo2Mode::TextToVideo(Veo2TextToVideoRequest {
prompt: "a peaceful lake at dawn".to_string(),
aspect_ratio: Some(Veo2TextToVideoAspectRatio::SixteenByNine),
duration: Some(Veo2TextToVideoDuration::FiveSeconds),
negative_prompt: None,
enhance_prompt: None,
seed: None,
auto_fix: None,
}),
};
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
assert!(matches!(response, GenerateVideoResponse::Fal(_)));
}
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_image_to_video_7s() {
let response = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the dog leaps into the lake and splashes around.".to_string()),
start_frame: Some(ImageRef::Url(JUNO_AT_LAKE_IMAGE_URL.to_string())),
duration_seconds: Some(7),
..fal_veo_2_builder()
}).await;
println!("response: {:?}", response);
let state = FalVeo2RequestState {
mode: FalVeo2Mode::ImageToVideo(Veo2ImageToVideoRequest {
prompt: "the dog leaps into the lake and splashes around.".to_string(),
image_url: JUNO_AT_LAKE_IMAGE_URL.to_string(),
duration: Some(Veo2ImageToVideoDuration::SevenSeconds),
}),
};
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
assert!(matches!(response, GenerateVideoResponse::Fal(_)));
}
fn fal_veo_2_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo2,
provider: RouterProvider::Fal,
..Default::default()
}
fn read_fal_api_key() -> FalApiKey {
let secret = std::fs::read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")
.expect("Failed to read fal_api_key.txt");
FalApiKey::from_str(secret.trim())
}
async fn run_pipeline(builder: GenerateVideoRequestBuilder) -> GenerateVideoResponse {
let client = get_fal_client();
let draft_or_request = builder.build2().expect("build2");
let request = match draft_or_request {
VideoGenerationDraftOrRequest::Request(r) => r,
_ => panic!("expected Request variant"),
};
request.send_request(&client).await.expect("send_request")
fn client_with_webhook() -> RouterFalClient {
RouterFalClient::new_with_webhook(
read_fal_api_key(),
"https://example.com/fal-webhook-test".to_string(),
)
}
}
@@ -1,8 +1,10 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_3_image_to_video_webhook::{
Veo3I2vAspectRatio, Veo3I2vDuration, Veo3I2vResolution, Veo3Request,
use fal_client::requests::api::video::image::veo_3::api::{
Veo3ImageToVideoAspectRatio, Veo3ImageToVideoDuration, Veo3ImageToVideoRequest,
Veo3ImageToVideoResolution,
};
use fal_client::requests_old::webhook::video::text::enqueue_veo_3_text_to_video_webhook::{
Veo3T2vAspectRatio, Veo3T2vDuration, Veo3T2vResolution, Veo3TextToVideoRequest,
use fal_client::requests::api::video::text::veo_3::api::{
Veo3TextToVideoAspectRatio, Veo3TextToVideoDuration, Veo3TextToVideoRequest,
Veo3TextToVideoResolution,
};
use crate::api::router_aspect_ratio::RouterAspectRatio;
@@ -16,18 +18,16 @@ use crate::generate::generate_video::providers::fal::veo_3::request::{FalVeo3Mod
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
/// Router-level resolution shared between both modes — kept here so the cost
/// calculator can reconstruct it from either mode's request.
/// Router-level resolution shared between both modes — each mode maps it onto
/// its own fal resolution enum.
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanResolution {
Default,
SevenTwentyP,
TenEightyP,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanDuration {
Default,
FourSeconds,
SixSeconds,
EightSeconds,
@@ -36,6 +36,13 @@ pub(crate) enum PlanDuration {
pub fn build_fal_veo_3(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_fal_veo_3_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3(state)))
}
pub(crate) fn build_fal_veo_3_state(
builder: GenerateVideoRequestBuilder,
) -> Result<FalVeo3RequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
if builder.end_frame.is_some() {
@@ -46,18 +53,24 @@ pub fn build_fal_veo_3(
let negative_prompt = builder.negative_prompt.clone();
let resolution = plan_resolution(builder.resolution, strategy)?;
let duration = plan_duration(builder.duration_seconds, strategy)?;
let generate_audio = builder.generate_audio.unwrap_or(true);
// fal's server default is audio-on; keep the explicit `true` the old
// endpoint sent so pricing stays deterministic.
let generate_audio = Some(builder.generate_audio.unwrap_or(true));
let mode = match builder.start_frame.clone() {
Some(ImageRef::Url(url)) => {
let i2v_aspect_ratio = plan_i2v_aspect_ratio(builder.aspect_ratio, strategy)?;
FalVeo3Mode::ImageToVideo(Veo3Request {
image_url: url,
FalVeo3Mode::ImageToVideo(Veo3ImageToVideoRequest {
prompt,
duration: to_i2v_duration(duration),
image_url: url,
aspect_ratio: i2v_aspect_ratio,
resolution: to_i2v_resolution(resolution),
duration: duration.map(to_i2v_duration),
resolution: resolution.map(to_i2v_resolution),
generate_audio,
negative_prompt,
seed: None,
auto_fix: None,
safety_tolerance: None,
})
}
Some(ImageRef::MediaFileToken(_)) => {
@@ -67,35 +80,37 @@ pub fn build_fal_veo_3(
let t2v_aspect_ratio = plan_t2v_aspect_ratio(builder.aspect_ratio, strategy)?;
FalVeo3Mode::TextToVideo(Veo3TextToVideoRequest {
prompt,
negative_prompt,
duration: to_t2v_duration(duration),
aspect_ratio: t2v_aspect_ratio,
resolution: to_t2v_resolution(resolution),
duration: duration.map(to_t2v_duration),
resolution: resolution.map(to_t2v_resolution),
negative_prompt,
generate_audio,
seed: None,
auto_fix: None,
safety_tolerance: None,
})
}
};
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3(
FalVeo3RequestState { mode },
)))
Ok(FalVeo3RequestState { mode })
}
/// Text-to-video: only 16:9 and 9:16 (no Auto, no Square).
/// Text-to-video: only 16:9 and 9:16 (no Auto, no Square). `None` lets fal
/// pick its default (16:9).
fn plan_t2v_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo3T2vAspectRatio, ArtcraftRouterError> {
use Veo3T2vAspectRatio as Ar;
) -> Result<Option<Veo3TextToVideoAspectRatio>, ArtcraftRouterError> {
use Veo3TextToVideoAspectRatio as Ar;
match aspect_ratio {
None => Ok(Ar::Default),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Ar::WideSixteenNine),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Ar::TallNineSixteen),
None => Ok(None),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Some(Ar::SixteenByNine)),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Some(Ar::NineBySixteen)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("aspect_ratio", &format!("{:?}", other)))
}
_ => Ok(Ar::Default),
_ => Ok(None),
},
}
}
@@ -104,23 +119,23 @@ fn plan_t2v_aspect_ratio(
fn plan_i2v_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo3I2vAspectRatio, ArtcraftRouterError> {
use Veo3I2vAspectRatio as Ar;
) -> Result<Option<Veo3ImageToVideoAspectRatio>, ArtcraftRouterError> {
use Veo3ImageToVideoAspectRatio as Ar;
match aspect_ratio {
None
| Some(RouterAspectRatio::Auto)
| Some(RouterAspectRatio::Auto2k)
| Some(RouterAspectRatio::Auto3k)
| Some(RouterAspectRatio::Auto4k) => Ok(Ar::Auto),
| Some(RouterAspectRatio::Auto4k) => Ok(Some(Ar::Auto)),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Ar::WideSixteenNine),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Ar::TallNineSixteen),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Some(Ar::SixteenByNine)),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Some(Ar::NineBySixteen)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("aspect_ratio", &format!("{:?}", other)))
}
_ => Ok(Ar::Auto),
_ => Ok(Some(Ar::Auto)),
},
}
}
@@ -128,17 +143,17 @@ fn plan_i2v_aspect_ratio(
fn plan_resolution(
resolution: Option<RouterResolution>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<PlanResolution, ArtcraftRouterError> {
) -> Result<Option<PlanResolution>, ArtcraftRouterError> {
match resolution {
None => Ok(PlanResolution::Default),
Some(RouterResolution::SevenTwentyP) => Ok(PlanResolution::SevenTwentyP),
Some(RouterResolution::TenEightyP) => Ok(PlanResolution::TenEightyP),
None => Ok(None),
Some(RouterResolution::SevenTwentyP) => Ok(Some(PlanResolution::SevenTwentyP)),
Some(RouterResolution::TenEightyP) => Ok(Some(PlanResolution::TenEightyP)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("resolution", &format!("{:?}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(PlanResolution::TenEightyP),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(PlanResolution::SevenTwentyP),
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanResolution::TenEightyP)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanResolution::SevenTwentyP)),
},
}
}
@@ -146,18 +161,18 @@ fn plan_resolution(
fn plan_duration(
duration_seconds: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<PlanDuration, ArtcraftRouterError> {
) -> Result<Option<PlanDuration>, ArtcraftRouterError> {
match duration_seconds {
None => Ok(PlanDuration::Default),
Some(4) => Ok(PlanDuration::FourSeconds),
Some(6) => Ok(PlanDuration::SixSeconds),
Some(8) => Ok(PlanDuration::EightSeconds),
None => Ok(None),
Some(4) => Ok(Some(PlanDuration::FourSeconds)),
Some(6) => Ok(Some(PlanDuration::SixSeconds)),
Some(8) => Ok(Some(PlanDuration::EightSeconds)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("duration_seconds", &format!("{}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(PlanDuration::EightSeconds),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(PlanDuration::FourSeconds),
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanDuration::EightSeconds)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanDuration::FourSeconds)),
},
}
}
@@ -169,37 +184,33 @@ fn unsupported(field: &'static str, value: &str) -> ArtcraftRouterError {
})
}
fn to_t2v_duration(d: PlanDuration) -> Veo3T2vDuration {
fn to_t2v_duration(d: PlanDuration) -> Veo3TextToVideoDuration {
match d {
PlanDuration::Default => Veo3T2vDuration::Default,
PlanDuration::FourSeconds => Veo3T2vDuration::FourSeconds,
PlanDuration::SixSeconds => Veo3T2vDuration::SixSeconds,
PlanDuration::EightSeconds => Veo3T2vDuration::EightSeconds,
PlanDuration::FourSeconds => Veo3TextToVideoDuration::FourSeconds,
PlanDuration::SixSeconds => Veo3TextToVideoDuration::SixSeconds,
PlanDuration::EightSeconds => Veo3TextToVideoDuration::EightSeconds,
}
}
fn to_i2v_duration(d: PlanDuration) -> Veo3I2vDuration {
fn to_i2v_duration(d: PlanDuration) -> Veo3ImageToVideoDuration {
match d {
PlanDuration::Default => Veo3I2vDuration::Default,
PlanDuration::FourSeconds => Veo3I2vDuration::FourSeconds,
PlanDuration::SixSeconds => Veo3I2vDuration::SixSeconds,
PlanDuration::EightSeconds => Veo3I2vDuration::EightSeconds,
PlanDuration::FourSeconds => Veo3ImageToVideoDuration::FourSeconds,
PlanDuration::SixSeconds => Veo3ImageToVideoDuration::SixSeconds,
PlanDuration::EightSeconds => Veo3ImageToVideoDuration::EightSeconds,
}
}
fn to_t2v_resolution(r: PlanResolution) -> Veo3T2vResolution {
fn to_t2v_resolution(r: PlanResolution) -> Veo3TextToVideoResolution {
match r {
PlanResolution::Default => Veo3T2vResolution::Default,
PlanResolution::SevenTwentyP => Veo3T2vResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3T2vResolution::TenEightyP,
PlanResolution::SevenTwentyP => Veo3TextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3TextToVideoResolution::TenEightyP,
}
}
fn to_i2v_resolution(r: PlanResolution) -> Veo3I2vResolution {
fn to_i2v_resolution(r: PlanResolution) -> Veo3ImageToVideoResolution {
match r {
PlanResolution::Default => Veo3I2vResolution::Default,
PlanResolution::SevenTwentyP => Veo3I2vResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3I2vResolution::TenEightyP,
PlanResolution::SevenTwentyP => Veo3ImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3ImageToVideoResolution::TenEightyP,
}
}
@@ -207,48 +218,9 @@ fn to_i2v_resolution(r: PlanResolution) -> Veo3I2vResolution {
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
use super::*;
fn base_t2v_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
fn base_i2v_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
start_frame: Some(ImageRef::Url("https://example.com/a.png".to_string())),
..base_t2v_builder()
}
}
fn t2v_request(b: GenerateVideoRequestBuilder) -> Veo3TextToVideoRequest {
match build_fal_veo_3(b).expect("build") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3(state)) => match state.mode {
FalVeo3Mode::TextToVideo(r) => r,
FalVeo3Mode::ImageToVideo(_) => panic!("expected TextToVideo"),
},
_ => panic!("expected FalVeo3"),
}
}
fn i2v_request(b: GenerateVideoRequestBuilder) -> Veo3Request {
match build_fal_veo_3(b).expect("build") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3(state)) => match state.mode {
FalVeo3Mode::ImageToVideo(r) => r,
FalVeo3Mode::TextToVideo(_) => panic!("expected ImageToVideo"),
},
_ => panic!("expected FalVeo3"),
}
}
use crate::generate::generate_video::providers::fal::veo_3::request::{FalVeo3Mode, FalVeo3RequestState};
mod mode_selection {
use super::*;
@@ -262,7 +234,7 @@ mod tests {
fn end_frame_errors() {
let mut b = base_t2v_builder();
b.end_frame = Some(ImageRef::Url("https://example.com/end.png".to_string()));
assert!(build_fal_veo_3(b).is_err());
assert!(build_fal_veo_3_state(b).is_err());
}
}
@@ -273,14 +245,14 @@ mod tests {
fn t2v_720p() {
let mut b = base_t2v_builder();
b.resolution = Some(RouterResolution::SevenTwentyP);
assert!(matches!(t2v_request(b).resolution, Veo3T2vResolution::SevenTwentyP));
assert_eq!(t2v_request(b).resolution, Some(Veo3TextToVideoResolution::SevenTwentyP));
}
#[test]
fn t2v_1080p() {
let mut b = base_t2v_builder();
b.resolution = Some(RouterResolution::TenEightyP);
assert!(matches!(t2v_request(b).resolution, Veo3T2vResolution::TenEightyP));
assert_eq!(t2v_request(b).resolution, Some(Veo3TextToVideoResolution::TenEightyP));
}
#[test]
@@ -288,7 +260,7 @@ mod tests {
let mut b = base_t2v_builder();
b.resolution = Some(RouterResolution::FourK);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayMoreUpgrade;
assert!(matches!(t2v_request(b).resolution, Veo3T2vResolution::TenEightyP));
assert_eq!(t2v_request(b).resolution, Some(Veo3TextToVideoResolution::TenEightyP));
}
}
@@ -298,13 +270,13 @@ mod tests {
#[test]
fn t2v_4s_6s_8s() {
for (s, expected) in [
(4, Veo3T2vDuration::FourSeconds),
(6, Veo3T2vDuration::SixSeconds),
(8, Veo3T2vDuration::EightSeconds),
(4, Veo3TextToVideoDuration::FourSeconds),
(6, Veo3TextToVideoDuration::SixSeconds),
(8, Veo3TextToVideoDuration::EightSeconds),
] {
let mut b = base_t2v_builder();
b.duration_seconds = Some(s);
assert_eq!(format!("{:?}", t2v_request(b).duration), format!("{:?}", expected));
assert_eq!(t2v_request(b).duration, Some(expected));
}
}
}
@@ -316,12 +288,12 @@ mod tests {
fn t2v_sixteen_nine() {
let mut b = base_t2v_builder();
b.aspect_ratio = Some(RouterAspectRatio::WideSixteenByNine);
assert!(matches!(t2v_request(b).aspect_ratio, Veo3T2vAspectRatio::WideSixteenNine));
assert_eq!(t2v_request(b).aspect_ratio, Some(Veo3TextToVideoAspectRatio::SixteenByNine));
}
#[test]
fn i2v_auto_when_unspecified() {
assert!(matches!(i2v_request(base_i2v_builder()).aspect_ratio, Veo3I2vAspectRatio::Auto));
assert_eq!(i2v_request(base_i2v_builder()).aspect_ratio, Some(Veo3ImageToVideoAspectRatio::Auto));
}
}
@@ -330,14 +302,14 @@ mod tests {
#[test]
fn audio_defaults_to_true() {
assert!(t2v_request(base_t2v_builder()).generate_audio);
assert_eq!(t2v_request(base_t2v_builder()).generate_audio, Some(true));
}
#[test]
fn audio_false_is_passed_through() {
let mut b = base_t2v_builder();
b.generate_audio = Some(false);
assert!(!t2v_request(b).generate_audio);
assert_eq!(t2v_request(b).generate_audio, Some(false));
}
}
@@ -359,7 +331,7 @@ mod tests {
b.duration_seconds = duration;
b.aspect_ratio = aspect_ratio;
b.generate_audio = generate_audio;
assert!(build_fal_veo_3(b).is_ok());
assert!(build_fal_veo_3_state(b).is_ok());
combos += 1;
}
}
@@ -369,7 +341,33 @@ mod tests {
assert_eq!(combos, 3 * 4 * 4 * 3 * 2);
}
// Silence unused warnings for FalVeo3RequestState in this scope.
#[allow(dead_code)]
fn _silence(_: FalVeo3RequestState) {}
fn base_t2v_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
fn base_i2v_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
start_frame: Some(ImageRef::Url("https://example.com/a.png".to_string())),
..base_t2v_builder()
}
}
fn t2v_request(b: GenerateVideoRequestBuilder) -> Veo3TextToVideoRequest {
match build_fal_veo_3_state(b).expect("build_fal_veo_3_state").mode {
FalVeo3Mode::TextToVideo(r) => r,
FalVeo3Mode::ImageToVideo(_) => panic!("expected TextToVideo"),
}
}
fn i2v_request(b: GenerateVideoRequestBuilder) -> Veo3ImageToVideoRequest {
match build_fal_veo_3_state(b).expect("build_fal_veo_3_state").mode {
FalVeo3Mode::ImageToVideo(r) => r,
FalVeo3Mode::TextToVideo(_) => panic!("expected ImageToVideo"),
}
}
}
@@ -1,32 +1,29 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_3_image_to_video_webhook::Veo3I2vDuration;
use fal_client::requests_old::webhook::video::text::enqueue_veo_3_text_to_video_webhook::Veo3T2vDuration;
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::fal::veo_3::request::{FalVeo3Mode, FalVeo3RequestState};
#[derive(Clone, Debug)]
pub struct FalVeo3CostState {
pub duration_seconds: u64,
pub generate_audio: bool,
pub cost_in_usd_cents: u64,
}
impl FalVeo3CostState {
pub fn from_request(request: &FalVeo3RequestState) -> Self {
let (duration_seconds, generate_audio) = match &request.mode {
FalVeo3Mode::TextToVideo(req) => (t2v_duration_seconds(req.duration), req.generate_audio),
FalVeo3Mode::ImageToVideo(req) => (i2v_duration_seconds(req.duration), req.generate_audio),
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalVeo3Mode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3Mode::ImageToVideo(req) => req.calculate_cost_in_cents(),
};
Self { duration_seconds, generate_audio }
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
// $0.20/sec audio off, $0.40/sec audio on (720p/1080p).
let per_second_cents: u64 = if self.generate_audio { 40 } else { 20 };
let cost_in_usd_cents = per_second_cents * self.duration_seconds;
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
@@ -36,43 +33,18 @@ impl FalVeo3CostState {
}
}
fn t2v_duration_seconds(d: Veo3T2vDuration) -> u64 {
match d {
Veo3T2vDuration::Default | Veo3T2vDuration::EightSeconds => 8,
Veo3T2vDuration::SixSeconds => 6,
Veo3T2vDuration::FourSeconds => 4,
}
}
fn i2v_duration_seconds(d: Veo3I2vDuration) -> u64 {
match d {
Veo3I2vDuration::Default | Veo3I2vDuration::EightSeconds => 8,
Veo3I2vDuration::SixSeconds => 6,
Veo3I2vDuration::FourSeconds => 4,
}
}
#[cfg(test)]
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_3::build::build_fal_veo_3_state;
fn cost_cents(duration_seconds: Option<u16>, generate_audio: Option<bool>, has_start_frame: bool) -> u64 {
let mut b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
generate_audio,
..Default::default()
};
if has_start_frame {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
b.build2().expect("build2").estimate_cost().expect("estimate_cost").cost_in_usd_cents.expect("cost")
}
use super::*;
// Pricing (fal): $0.20/sec audio off, $0.40/sec audio on. Flat across
// modalities; resolution (720p/1080p) does not change the price.
mod numeric_literal_pricing {
use super::*;
@@ -100,7 +72,7 @@ mod tests {
#[test]
fn duration_default_is_8s() {
// No duration → Default → 8s.
// No duration → fal default 8s.
assert_eq!(cost_cents(None, Some(true), false), 320);
}
@@ -120,4 +92,23 @@ mod tests {
fn audio_costs_more_than_no_audio() {
assert!(cost_cents(Some(8), Some(false), false) < cost_cents(Some(8), Some(true), false));
}
fn cost_cents(duration_seconds: Option<u16>, generate_audio: Option<bool>, has_start_frame: bool) -> u64 {
let mut b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
generate_audio,
..Default::default()
};
if has_start_frame {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
let state = build_fal_veo_3_state(b).expect("build_fal_veo_3_state");
FalVeo3CostState::from_request(&state)
.estimate_cost()
.cost_in_usd_cents
.expect("cost_in_usd_cents")
}
}
@@ -1,17 +1,12 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests_old::webhook::video::image::enqueue_veo_3_image_to_video_webhook::{
enqueue_veo_3_image_to_video_webhook, Veo3Args, Veo3Request,
};
use fal_client::requests_old::webhook::video::text::enqueue_veo_3_text_to_video_webhook::{
enqueue_veo_3_text_to_video_webhook, Veo3TextToVideoArgs, Veo3TextToVideoRequest,
};
use fal_client::requests::api::video::image::veo_3::api::Veo3ImageToVideoRequest;
use fal_client::requests::api::video::text::veo_3::api::Veo3TextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::errors::provider_error::ProviderError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
@@ -19,7 +14,7 @@ use crate::generate::generate_video::generate_video_response::{
#[derive(Clone, Debug)]
pub enum FalVeo3Mode {
TextToVideo(Veo3TextToVideoRequest),
ImageToVideo(Veo3Request),
ImageToVideo(Veo3ImageToVideoRequest),
}
#[derive(Clone, Debug)]
@@ -29,97 +24,123 @@ pub struct FalVeo3RequestState {
impl FalVeo3RequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
let webhook_url = client.webhook_url.as_deref()
.ok_or(ArtcraftRouterError::Client(ClientError::WebhookUrlRequired))?;
let (webhook_response, outbound_request): (_, Arc<dyn Debug + Send + Sync>) = match &self.mode {
FalVeo3Mode::TextToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = Veo3TextToVideoArgs {
request: request.clone(),
api_key: &client.api_key,
webhook_url,
};
(enqueue_veo_3_text_to_video_webhook(args).await, outbound)
}
FalVeo3Mode::ImageToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = Veo3Args {
request: request.clone(),
api_key: &client.api_key,
webhook_url,
};
(enqueue_veo_3_image_to_video_webhook(args).await, outbound)
}
};
match &self.mode {
FalVeo3Mode::TextToVideo(request) => send_request(request, client).await,
FalVeo3Mode::ImageToVideo(request) => send_request(request, client).await,
}
}
}
let webhook_response = webhook_response
.map_err(|e| ArtcraftRouterError::Provider(ProviderError::Fal(e)))?;
// ── Helpers ──
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: webhook_response.request_id,
gateway_request_id: webhook_response.gateway_request_id,
maybe_status_url: None,
maybe_response_url: None,
maybe_outbound_request: Some(outbound_request),
}))
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use fal_client::creds::fal_api_key::FalApiKey;
use fal_client::requests::api::video::image::veo_3::api::{
Veo3ImageToVideoDuration, Veo3ImageToVideoResolution,
};
use fal_client::requests::api::video::text::veo_3::api::{
Veo3TextToVideoDuration, Veo3TextToVideoResolution,
};
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::test_helpers::get_fal_client;
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_text_to_video_720p_4s_audio_on() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("a serene mountain landscape".to_string()),
resolution: Some(RouterResolution::SevenTwentyP),
duration_seconds: Some(4),
generate_audio: Some(true),
..builder()
}).await;
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
let state = FalVeo3RequestState {
mode: FalVeo3Mode::TextToVideo(Veo3TextToVideoRequest {
prompt: "a serene mountain landscape".to_string(),
aspect_ratio: None,
duration: Some(Veo3TextToVideoDuration::FourSeconds),
resolution: Some(Veo3TextToVideoResolution::SevenTwentyP),
negative_prompt: None,
generate_audio: Some(true),
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
};
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
assert!(matches!(response, GenerateVideoResponse::Fal(_)));
}
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_image_to_video_1080p_8s_no_audio() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the dog leaps into the lake.".to_string()),
start_frame: Some(ImageRef::Url(JUNO_AT_LAKE_IMAGE_URL.to_string())),
resolution: Some(RouterResolution::TenEightyP),
duration_seconds: Some(8),
generate_audio: Some(false),
..builder()
}).await;
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
}
fn builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3,
provider: RouterProvider::Fal,
..Default::default()
}
}
async fn run_pipeline(b: GenerateVideoRequestBuilder) -> GenerateVideoResponse {
let client = get_fal_client();
let dor = b.build2().expect("build2");
let req = match dor {
VideoGenerationDraftOrRequest::Request(r) => r,
_ => panic!("expected Request"),
let state = FalVeo3RequestState {
mode: FalVeo3Mode::ImageToVideo(Veo3ImageToVideoRequest {
prompt: "the dog leaps into the lake.".to_string(),
image_url: JUNO_AT_LAKE_IMAGE_URL.to_string(),
aspect_ratio: None,
duration: Some(Veo3ImageToVideoDuration::EightSeconds),
resolution: Some(Veo3ImageToVideoResolution::TenEightyP),
generate_audio: Some(false),
negative_prompt: None,
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
};
req.send_request(&client).await.expect("send")
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
assert!(matches!(response, GenerateVideoResponse::Fal(_)));
}
fn read_fal_api_key() -> FalApiKey {
let secret = std::fs::read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")
.expect("Failed to read fal_api_key.txt");
FalApiKey::from_str(secret.trim())
}
fn client_with_webhook() -> RouterFalClient {
RouterFalClient::new_with_webhook(
read_fal_api_key(),
"https://example.com/fal-webhook-test".to_string(),
)
}
}
@@ -1,5 +1,10 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_3_fast_image_to_video_webhook::{
Veo3FastAspectRatio, Veo3FastDuration, Veo3FastRequest, Veo3FastResolution,
use fal_client::requests::api::video::image::veo_3_fast::api::{
Veo3FastImageToVideoAspectRatio, Veo3FastImageToVideoDuration, Veo3FastImageToVideoRequest,
Veo3FastImageToVideoResolution,
};
use fal_client::requests::api::video::text::veo_3_fast::api::{
Veo3FastTextToVideoAspectRatio, Veo3FastTextToVideoDuration, Veo3FastTextToVideoRequest,
Veo3FastTextToVideoResolution,
};
use crate::api::router_aspect_ratio::RouterAspectRatio;
@@ -9,69 +14,130 @@ use crate::client::request_mismatch_mitigation_strategy::RequestMismatchMitigati
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_3_fast::request::FalVeo3FastRequestState;
use crate::generate::generate_video::providers::fal::veo_3_fast::request::{
FalVeo3FastMode, FalVeo3FastRequestState,
};
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
/// Router-level resolution shared between both modes — each mode maps it onto
/// its own fal resolution enum.
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanResolution {
SevenTwentyP,
TenEightyP,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanDuration {
FourSeconds,
SixSeconds,
EightSeconds,
}
pub fn build_fal_veo_3_fast(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_fal_veo_3_fast_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3Fast(state)))
}
pub(crate) fn build_fal_veo_3_fast_state(
builder: GenerateVideoRequestBuilder,
) -> Result<FalVeo3FastRequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
let start_frame_url = require_url(builder.start_frame.clone())?;
if builder.end_frame.is_some() {
return Err(unsupported("end_frame", "Veo 3 Fast does not support an ending frame"));
}
let aspect_ratio = plan_aspect_ratio(builder.aspect_ratio, strategy)?;
let prompt = builder.prompt.clone().unwrap_or_default();
let negative_prompt = builder.negative_prompt.clone();
let resolution = plan_resolution(builder.resolution, strategy)?;
let duration = plan_duration(builder.duration_seconds, strategy)?;
let generate_audio = builder.generate_audio.unwrap_or(true);
// fal's server default is audio-on; keep the explicit `true` the old
// endpoint sent so pricing stays deterministic.
let generate_audio = Some(builder.generate_audio.unwrap_or(true));
let request = Veo3FastRequest {
prompt: builder.prompt.clone().unwrap_or_default(),
image_url: start_frame_url,
aspect_ratio,
duration,
resolution,
generate_audio,
let mode = match builder.start_frame.clone() {
Some(ImageRef::Url(url)) => {
let i2v_aspect_ratio = plan_i2v_aspect_ratio(builder.aspect_ratio, strategy)?;
FalVeo3FastMode::ImageToVideo(Veo3FastImageToVideoRequest {
prompt,
image_url: url,
aspect_ratio: i2v_aspect_ratio,
duration: duration.map(to_i2v_duration),
resolution: resolution.map(to_i2v_resolution),
generate_audio,
negative_prompt,
seed: None,
auto_fix: None,
safety_tolerance: None,
})
}
Some(ImageRef::MediaFileToken(_)) => {
return Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls));
}
None => {
let t2v_aspect_ratio = plan_t2v_aspect_ratio(builder.aspect_ratio, strategy)?;
FalVeo3FastMode::TextToVideo(Veo3FastTextToVideoRequest {
prompt,
aspect_ratio: t2v_aspect_ratio,
duration: duration.map(to_t2v_duration),
resolution: resolution.map(to_t2v_resolution),
negative_prompt,
generate_audio,
seed: None,
auto_fix: None,
safety_tolerance: None,
})
}
};
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3Fast(
FalVeo3FastRequestState { request },
)))
Ok(FalVeo3FastRequestState { mode })
}
fn require_url(image_ref: Option<ImageRef>) -> Result<String, ArtcraftRouterError> {
match image_ref {
Some(ImageRef::Url(url)) => Ok(url),
Some(ImageRef::MediaFileToken(_)) => {
Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls))
}
None => Err(unsupported("start_frame", "Veo 3 Fast requires a starting frame")),
/// Text-to-video: only 16:9 and 9:16 (no Auto, no Square). `None` lets fal
/// pick its default (16:9).
fn plan_t2v_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<Veo3FastTextToVideoAspectRatio>, ArtcraftRouterError> {
use Veo3FastTextToVideoAspectRatio as Ar;
match aspect_ratio {
None => Ok(None),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Some(Ar::SixteenByNine)),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Some(Ar::NineBySixteen)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("aspect_ratio", &format!("{:?}", other)))
}
_ => Ok(None),
},
}
}
fn plan_aspect_ratio(
/// Image-to-video: Auto, 16:9, 9:16 (no Square).
fn plan_i2v_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo3FastAspectRatio, ArtcraftRouterError> {
use Veo3FastAspectRatio as Ar;
) -> Result<Option<Veo3FastImageToVideoAspectRatio>, ArtcraftRouterError> {
use Veo3FastImageToVideoAspectRatio as Ar;
match aspect_ratio {
None
| Some(RouterAspectRatio::Auto)
| Some(RouterAspectRatio::Auto2k)
| Some(RouterAspectRatio::Auto3k)
| Some(RouterAspectRatio::Auto4k) => Ok(Ar::Auto),
| Some(RouterAspectRatio::Auto4k) => Ok(Some(Ar::Auto)),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Ar::WideSixteenNine),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Ar::TallNineSixteen),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Some(Ar::SixteenByNine)),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Some(Ar::NineBySixteen)),
Some(unsupported_ar) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("aspect_ratio", &format!("{:?}", unsupported_ar)))
}
_ => Ok(Ar::Auto),
_ => Ok(Some(Ar::Auto)),
},
}
}
@@ -79,18 +145,17 @@ fn plan_aspect_ratio(
fn plan_resolution(
resolution: Option<RouterResolution>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo3FastResolution, ArtcraftRouterError> {
use Veo3FastResolution as R;
) -> Result<Option<PlanResolution>, ArtcraftRouterError> {
match resolution {
None => Ok(R::Default),
Some(RouterResolution::SevenTwentyP) => Ok(R::SevenTwentyP),
Some(RouterResolution::TenEightyP) => Ok(R::TenEightyP),
None => Ok(None),
Some(RouterResolution::SevenTwentyP) => Ok(Some(PlanResolution::SevenTwentyP)),
Some(RouterResolution::TenEightyP) => Ok(Some(PlanResolution::TenEightyP)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("resolution", &format!("{:?}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(R::TenEightyP),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(R::SevenTwentyP),
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanResolution::TenEightyP)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanResolution::SevenTwentyP)),
},
}
}
@@ -98,18 +163,18 @@ fn plan_resolution(
fn plan_duration(
duration_seconds: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo3FastDuration, ArtcraftRouterError> {
) -> Result<Option<PlanDuration>, ArtcraftRouterError> {
match duration_seconds {
None => Ok(Veo3FastDuration::Default),
Some(4) => Ok(Veo3FastDuration::FourSeconds),
Some(6) => Ok(Veo3FastDuration::SixSeconds),
Some(8) => Ok(Veo3FastDuration::EightSeconds),
None => Ok(None),
Some(4) => Ok(Some(PlanDuration::FourSeconds)),
Some(6) => Ok(Some(PlanDuration::SixSeconds)),
Some(8) => Ok(Some(PlanDuration::EightSeconds)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("duration_seconds", &format!("{}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Veo3FastDuration::EightSeconds),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Veo3FastDuration::FourSeconds),
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanDuration::EightSeconds)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanDuration::FourSeconds)),
},
}
}
@@ -121,62 +186,100 @@ fn unsupported(field: &'static str, value: &str) -> ArtcraftRouterError {
})
}
fn to_t2v_duration(d: PlanDuration) -> Veo3FastTextToVideoDuration {
match d {
PlanDuration::FourSeconds => Veo3FastTextToVideoDuration::FourSeconds,
PlanDuration::SixSeconds => Veo3FastTextToVideoDuration::SixSeconds,
PlanDuration::EightSeconds => Veo3FastTextToVideoDuration::EightSeconds,
}
}
fn to_i2v_duration(d: PlanDuration) -> Veo3FastImageToVideoDuration {
match d {
PlanDuration::FourSeconds => Veo3FastImageToVideoDuration::FourSeconds,
PlanDuration::SixSeconds => Veo3FastImageToVideoDuration::SixSeconds,
PlanDuration::EightSeconds => Veo3FastImageToVideoDuration::EightSeconds,
}
}
fn to_t2v_resolution(r: PlanResolution) -> Veo3FastTextToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3FastTextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3FastTextToVideoResolution::TenEightyP,
}
}
fn to_i2v_resolution(r: PlanResolution) -> Veo3FastImageToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3FastImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3FastImageToVideoResolution::TenEightyP,
}
}
#[cfg(test)]
mod tests {
use tokens::tokens::media_files::MediaFileToken;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
use super::*;
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3Fast,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
start_frame: Some(ImageRef::Url("https://example.com/a.png".to_string())),
..Default::default()
mod mode_selection {
use super::*;
#[test]
fn no_start_frame_picks_t2v() {
let _ = t2v_request(base_t2v_builder());
}
#[test]
fn start_frame_picks_i2v() {
let _ = i2v_request(base_i2v_builder());
}
#[test]
fn end_frame_errors() {
let mut b = base_i2v_builder();
b.end_frame = Some(ImageRef::Url("https://example.com/end.png".to_string()));
assert!(build_fal_veo_3_fast_state(b).is_err());
}
#[test]
fn media_file_token_for_start_frame_errors() {
let mut b = base_t2v_builder();
b.start_frame = Some(ImageRef::MediaFileToken(MediaFileToken::new("mf_x".to_string())));
assert!(build_fal_veo_3_fast_state(b).is_err());
}
}
fn unwrap(b: GenerateVideoRequestBuilder) -> Veo3FastRequest {
match build_fal_veo_3_fast(b).expect("build") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3Fast(s)) => s.request,
_ => panic!("expected FalVeo3Fast"),
mod materialized_fields {
use super::*;
#[test]
fn i2v_prompt_passed_through() {
let mut b = base_i2v_builder();
b.prompt = Some("hello world".to_string());
assert_eq!(i2v_request(b).prompt, "hello world");
}
}
#[test]
fn missing_start_frame_errors() {
let mut b = base_builder();
b.start_frame = None;
assert!(build_fal_veo_3_fast(b).is_err());
}
#[test]
fn i2v_image_url_passed_through() {
assert_eq!(i2v_request(base_i2v_builder()).image_url, "https://example.com/a.png");
}
#[test]
fn end_frame_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url("https://example.com/end.png".to_string()));
assert!(build_fal_veo_3_fast(b).is_err());
}
#[test]
fn audio_defaults_to_true() {
assert_eq!(i2v_request(base_i2v_builder()).generate_audio, Some(true));
assert_eq!(t2v_request(base_t2v_builder()).generate_audio, Some(true));
}
#[test]
fn prompt_passed_through() {
let mut b = base_builder();
b.prompt = Some("hello world".to_string());
assert_eq!(unwrap(b).prompt, "hello world");
}
#[test]
fn audio_defaults_to_true() {
assert!(unwrap(base_builder()).generate_audio);
}
#[test]
fn audio_false_passed_through() {
let mut b = base_builder();
b.generate_audio = Some(false);
assert!(!unwrap(b).generate_audio);
#[test]
fn audio_false_passed_through() {
let mut b = base_i2v_builder();
b.generate_audio = Some(false);
assert_eq!(i2v_request(b).generate_audio, Some(false));
}
}
#[test]
@@ -191,17 +294,49 @@ mod tests {
for &duration in &durations {
for &aspect_ratio in &aspect_ratios {
for &generate_audio in &audios {
let mut b = base_builder();
b.resolution = resolution;
b.duration_seconds = duration;
b.aspect_ratio = aspect_ratio;
b.generate_audio = generate_audio;
assert!(build_fal_veo_3_fast(b).is_ok());
combos += 1;
for has_start_frame in [false, true] {
let mut b = if has_start_frame { base_i2v_builder() } else { base_t2v_builder() };
b.resolution = resolution;
b.duration_seconds = duration;
b.aspect_ratio = aspect_ratio;
b.generate_audio = generate_audio;
assert!(build_fal_veo_3_fast_state(b).is_ok());
combos += 1;
}
}
}
}
}
assert_eq!(combos, 3 * 4 * 4 * 3);
assert_eq!(combos, 3 * 4 * 4 * 3 * 2);
}
fn base_t2v_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3Fast,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
fn base_i2v_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
start_frame: Some(ImageRef::Url("https://example.com/a.png".to_string())),
..base_t2v_builder()
}
}
fn t2v_request(b: GenerateVideoRequestBuilder) -> Veo3FastTextToVideoRequest {
match build_fal_veo_3_fast_state(b).expect("build_fal_veo_3_fast_state").mode {
FalVeo3FastMode::TextToVideo(r) => r,
FalVeo3FastMode::ImageToVideo(_) => panic!("expected TextToVideo"),
}
}
fn i2v_request(b: GenerateVideoRequestBuilder) -> Veo3FastImageToVideoRequest {
match build_fal_veo_3_fast_state(b).expect("build_fal_veo_3_fast_state").mode {
FalVeo3FastMode::ImageToVideo(r) => r,
FalVeo3FastMode::TextToVideo(_) => panic!("expected ImageToVideo"),
}
}
}
@@ -1,30 +1,31 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_3_fast_image_to_video_webhook::Veo3FastDuration;
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::fal::veo_3_fast::request::FalVeo3FastRequestState;
use crate::generate::generate_video::providers::fal::veo_3_fast::request::{
FalVeo3FastMode, FalVeo3FastRequestState,
};
#[derive(Clone, Debug)]
pub struct FalVeo3FastCostState {
pub duration_seconds: u64,
pub generate_audio: bool,
pub cost_in_usd_cents: u64,
}
impl FalVeo3FastCostState {
pub fn from_request(request: &FalVeo3FastRequestState) -> Self {
Self {
duration_seconds: duration_seconds_for_cost(request.request.duration),
generate_audio: request.request.generate_audio,
}
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalVeo3FastMode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3FastMode::ImageToVideo(req) => req.calculate_cost_in_cents(),
};
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
// $0.10/sec audio off, $0.15/sec audio on (720p/1080p).
let per_second_cents: u64 = if self.generate_audio { 15 } else { 10 };
let cost_in_usd_cents = per_second_cents * self.duration_seconds;
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
@@ -34,33 +35,18 @@ impl FalVeo3FastCostState {
}
}
fn duration_seconds_for_cost(d: Veo3FastDuration) -> u64 {
match d {
Veo3FastDuration::Default | Veo3FastDuration::EightSeconds => 8,
Veo3FastDuration::SixSeconds => 6,
Veo3FastDuration::FourSeconds => 4,
}
}
#[cfg(test)]
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_3_fast::build::build_fal_veo_3_fast_state;
fn cost_cents(duration_seconds: Option<u16>, generate_audio: Option<bool>) -> u64 {
let b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3Fast,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
start_frame: Some(ImageRef::Url("https://example.com/a.png".to_string())),
duration_seconds,
generate_audio,
..Default::default()
};
b.build2().expect("build2").estimate_cost().expect("estimate_cost").cost_in_usd_cents.expect("cost")
}
use super::*;
// Pricing (fal): $0.10/sec audio off, $0.15/sec audio on. Flat across
// modalities; resolution (720p/1080p) does not change the price.
mod numeric_literal_pricing {
use super::*;
@@ -68,38 +54,62 @@ mod tests {
#[test]
fn audio_on_4s_is_60() {
// $0.15/sec × 4s = 60¢.
assert_eq!(cost_cents(Some(4), Some(true)), 60);
assert_eq!(cost_cents(Some(4), Some(true), true), 60);
}
#[test]
fn audio_on_6s_is_90() { assert_eq!(cost_cents(Some(6), Some(true)), 90); }
fn audio_on_6s_is_90() { assert_eq!(cost_cents(Some(6), Some(true), true), 90); }
#[test]
fn audio_on_8s_is_120() { assert_eq!(cost_cents(Some(8), Some(true)), 120); }
fn audio_on_8s_is_120() { assert_eq!(cost_cents(Some(8), Some(true), true), 120); }
#[test]
fn audio_off_4s_is_40() {
// $0.10/sec × 4s = 40¢.
assert_eq!(cost_cents(Some(4), Some(false)), 40);
assert_eq!(cost_cents(Some(4), Some(false), true), 40);
}
#[test]
fn audio_off_8s_is_80() { assert_eq!(cost_cents(Some(8), Some(false)), 80); }
fn audio_off_8s_is_80() { assert_eq!(cost_cents(Some(8), Some(false), true), 80); }
#[test]
fn default_duration_is_8s() {
assert_eq!(cost_cents(None, Some(true)), 120);
assert_eq!(cost_cents(None, Some(true), true), 120);
}
#[test]
fn t2v_matches_i2v() {
assert_eq!(cost_cents(Some(6), Some(true), false), cost_cents(Some(6), Some(true), true));
}
}
#[test]
fn audio_costs_more_than_no_audio() {
assert!(cost_cents(Some(8), Some(false)) < cost_cents(Some(8), Some(true)));
assert!(cost_cents(Some(8), Some(false), true) < cost_cents(Some(8), Some(true), true));
}
#[test]
fn longer_duration_costs_more() {
assert!(cost_cents(Some(4), Some(true)) < cost_cents(Some(6), Some(true)));
assert!(cost_cents(Some(6), Some(true)) < cost_cents(Some(8), Some(true)));
assert!(cost_cents(Some(4), Some(true), true) < cost_cents(Some(6), Some(true), true));
assert!(cost_cents(Some(6), Some(true), true) < cost_cents(Some(8), Some(true), true));
}
fn cost_cents(duration_seconds: Option<u16>, generate_audio: Option<bool>, has_start_frame: bool) -> u64 {
let mut b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3Fast,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
generate_audio,
..Default::default()
};
if has_start_frame {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
let state = build_fal_veo_3_fast_state(b).expect("build_fal_veo_3_fast_state");
FalVeo3FastCostState::from_request(&state)
.estimate_cost()
.cost_in_usd_cents
.expect("cost_in_usd_cents")
}
}
@@ -1,91 +1,123 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests_old::webhook::video::image::enqueue_veo_3_fast_image_to_video_webhook::{
enqueue_veo_3_fast_image_to_video_webhook, Veo3FastArgs, Veo3FastRequest,
};
use fal_client::requests::api::video::image::veo_3_fast::api::Veo3FastImageToVideoRequest;
use fal_client::requests::api::video::text::veo_3_fast::api::Veo3FastTextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::errors::provider_error::ProviderError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
#[derive(Clone, Debug)]
pub enum FalVeo3FastMode {
TextToVideo(Veo3FastTextToVideoRequest),
ImageToVideo(Veo3FastImageToVideoRequest),
}
#[derive(Clone, Debug)]
pub struct FalVeo3FastRequestState {
pub request: Veo3FastRequest,
pub mode: FalVeo3FastMode,
}
impl FalVeo3FastRequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
let webhook_url = client.webhook_url.as_deref()
.ok_or(ArtcraftRouterError::Client(ClientError::WebhookUrlRequired))?;
let outbound_request: Arc<dyn Debug + Send + Sync> = Arc::new(self.request.clone());
match &self.mode {
FalVeo3FastMode::TextToVideo(request) => send_request(request, client).await,
FalVeo3FastMode::ImageToVideo(request) => send_request(request, client).await,
}
}
}
let args = Veo3FastArgs {
request: self.request.clone(),
api_key: &client.api_key,
webhook_url,
};
// ── Helpers ──
let webhook_response = enqueue_veo_3_fast_image_to_video_webhook(args)
.await
.map_err(|e| ArtcraftRouterError::Provider(ProviderError::Fal(e)))?;
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: webhook_response.request_id,
gateway_request_id: webhook_response.gateway_request_id,
maybe_status_url: None,
maybe_response_url: None,
maybe_outbound_request: Some(outbound_request),
}))
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use fal_client::creds::fal_api_key::FalApiKey;
use fal_client::requests::api::video::image::veo_3_fast::api::{
Veo3FastImageToVideoDuration, Veo3FastImageToVideoResolution,
};
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::test_helpers::get_fal_client;
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_image_to_video_720p_4s_audio() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the dog leaps into the lake.".to_string()),
start_frame: Some(ImageRef::Url(JUNO_AT_LAKE_IMAGE_URL.to_string())),
resolution: Some(RouterResolution::SevenTwentyP),
duration_seconds: Some(4),
generate_audio: Some(true),
..builder()
}).await;
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
}
fn builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3Fast,
provider: RouterProvider::Fal,
..Default::default()
}
}
async fn run_pipeline(b: GenerateVideoRequestBuilder) -> GenerateVideoResponse {
let client = get_fal_client();
let dor = b.build2().expect("build2");
let req = match dor {
VideoGenerationDraftOrRequest::Request(r) => r,
_ => panic!("expected Request"),
let state = FalVeo3FastRequestState {
mode: FalVeo3FastMode::ImageToVideo(Veo3FastImageToVideoRequest {
prompt: "the dog leaps into the lake.".to_string(),
image_url: JUNO_AT_LAKE_IMAGE_URL.to_string(),
aspect_ratio: None,
duration: Some(Veo3FastImageToVideoDuration::FourSeconds),
resolution: Some(Veo3FastImageToVideoResolution::SevenTwentyP),
generate_audio: Some(true),
negative_prompt: None,
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
};
req.send_request(&client).await.expect("send")
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
assert!(matches!(response, GenerateVideoResponse::Fal(_)));
}
fn read_fal_api_key() -> FalApiKey {
let secret = std::fs::read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")
.expect("Failed to read fal_api_key.txt");
FalApiKey::from_str(secret.trim())
}
fn client_with_webhook() -> RouterFalClient {
RouterFalClient::new_with_webhook(
read_fal_api_key(),
"https://example.com/fal-webhook-test".to_string(),
)
}
}
@@ -1,19 +1,29 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_first_last_frame_image_to_video_webhook::{
EnqueueVeo3p1FirstLastFrameImageToVideoAspectRatio, EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds,
EnqueueVeo3p1FirstLastFrameImageToVideoRequest, EnqueueVeo3p1FirstLastFrameImageToVideoResolution,
use fal_client::requests::api::video::extend::veo_3p1::api::{
Veo3p1ExtendVideoAspectRatio, Veo3p1ExtendVideoDuration, Veo3p1ExtendVideoRequest,
Veo3p1ExtendVideoResolution,
};
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_image_to_video_webhook::{
EnqueueVeo3p1ImageToVideoAspectRatio, EnqueueVeo3p1ImageToVideoDurationSeconds,
EnqueueVeo3p1ImageToVideoRequest, EnqueueVeo3p1ImageToVideoResolution,
use fal_client::requests::api::video::image::veo_3p1::api::{
Veo3p1ImageToVideoAspectRatio, Veo3p1ImageToVideoDuration, Veo3p1ImageToVideoRequest,
Veo3p1ImageToVideoResolution,
};
use fal_client::requests_old::webhook::video::text::enqueue_veo_3p1_text_to_video_webhook::{
EnqueueVeo3p1TextToVideoAspectRatio, EnqueueVeo3p1TextToVideoDurationSeconds,
EnqueueVeo3p1TextToVideoRequest, EnqueueVeo3p1TextToVideoResolution,
use fal_client::requests::api::video::images::veo_3p1::api::{
Veo3p1FirstLastFrameToVideoAspectRatio, Veo3p1FirstLastFrameToVideoDuration,
Veo3p1FirstLastFrameToVideoRequest, Veo3p1FirstLastFrameToVideoResolution,
};
use fal_client::requests::api::video::reference::veo_3p1::api::{
Veo3p1ReferenceToVideoAspectRatio, Veo3p1ReferenceToVideoDuration, Veo3p1ReferenceToVideoRequest,
Veo3p1ReferenceToVideoResolution,
};
use fal_client::requests::api::video::text::veo_3p1::api::{
Veo3p1TextToVideoAspectRatio, Veo3p1TextToVideoDuration, Veo3p1TextToVideoRequest,
Veo3p1TextToVideoResolution,
};
use crate::api::image_list_ref::ImageListRef;
use crate::api::image_ref::ImageRef;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_resolution::RouterResolution;
use crate::api::image_ref::ImageRef;
use crate::api::video_list_ref::VideoListRef;
use crate::client::request_mismatch_mitigation_strategy::RequestMismatchMitigationStrategy;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
@@ -33,6 +43,7 @@ pub(crate) enum PlanAspectRatio {
pub(crate) enum PlanResolution {
SevenTwentyP,
TenEightyP,
FourK,
}
#[derive(Debug, Clone, Copy)]
@@ -45,66 +56,143 @@ pub(crate) enum PlanDuration {
pub fn build_fal_veo_3p1(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_veo_3p1_request_state(builder, "Veo 3.1")?;
let state = build_fal_veo_3p1_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3p1(state)))
}
/// Shared builder used by both Veo 3.1 and Veo 3.1 Fast. The two variants take
/// the exact same router-level inputs — only the downstream fal endpoint and
/// pricing differ — so they share planning code.
pub(crate) fn build_veo_3p1_request_state(
pub(crate) fn build_fal_veo_3p1_state(
builder: GenerateVideoRequestBuilder,
model_label: &'static str,
) -> Result<FalVeo3p1RequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
let start = optional_url(builder.start_frame.clone())?;
let end = optional_url(builder.end_frame.clone())?;
let reference_images = reference_image_urls(builder.reference_images.clone())?;
let reference_videos = reference_video_urls(builder.reference_videos.clone())?;
let aspect_ratio = plan_aspect_ratio(builder.aspect_ratio, strategy)?;
let resolution = plan_resolution(builder.resolution, strategy)?;
let duration = plan_duration(builder.duration_seconds, strategy)?;
let prompt = builder.prompt.clone().unwrap_or_default();
let negative_prompt = builder.negative_prompt.clone();
let generate_audio = builder.generate_audio;
let mode = match (start, end) {
(None, None) => FalVeo3p1Mode::TextToVideo(EnqueueVeo3p1TextToVideoRequest {
prompt,
duration: duration.map(to_t2v_duration),
aspect_ratio: aspect_ratio.map(to_t2v_aspect_ratio),
resolution: resolution.map(to_t2v_resolution),
generate_audio,
enhance_prompt: None,
negative_prompt,
seed: None,
auto_fix: None,
}),
(Some(image_url), None) => FalVeo3p1Mode::ImageToVideo(EnqueueVeo3p1ImageToVideoRequest {
prompt,
image_url,
duration: duration.map(to_i2v_duration),
aspect_ratio: aspect_ratio.map(to_i2v_aspect_ratio),
resolution: resolution.map(to_i2v_resolution),
generate_audio,
}),
(Some(first_frame_url), Some(last_frame_url)) => FalVeo3p1Mode::FirstLastFrame(
EnqueueVeo3p1FirstLastFrameImageToVideoRequest {
prompt,
first_frame_url,
last_frame_url,
duration: duration.map(to_flf_duration),
aspect_ratio: aspect_ratio.map(to_flf_aspect_ratio),
resolution: resolution.map(to_flf_resolution),
generate_audio,
},
),
(None, Some(_)) => {
// Modality dispatch, most specific inputs first:
// 1. reference videos → extend-video
// 2. reference images → reference-to-video
// 3. start + end frames → first-last-frame-to-video
// 4. start frame only → image-to-video
// 5. no media → text-to-video
let mode = if let Some(video_urls) = reference_videos {
if reference_images.is_some() {
return Err(unsupported(
"reference_images",
"Veo 3.1 extend-video cannot combine reference_images with a reference video",
));
}
if start.is_some() {
return Err(unsupported(
"start_frame",
"Veo 3.1 extend-video cannot combine start_frame with a reference video",
));
}
if end.is_some() {
return Err(unsupported(
"end_frame",
&format!("{} requires a start_frame when end_frame is provided", model_label),
"Veo 3.1 extend-video cannot combine end_frame with a reference video",
));
}
if video_urls.len() != 1 {
return Err(unsupported(
"reference_videos",
&format!("Veo 3.1 extend-video requires exactly 1 reference video, got {}", video_urls.len()),
));
}
let video_url = video_urls.into_iter().next().expect("checked len == 1");
FalVeo3p1Mode::ExtendVideo(Veo3p1ExtendVideoRequest {
prompt,
video_url,
aspect_ratio: aspect_ratio.map(to_extend_aspect_ratio),
duration: plan_extend_duration(builder.duration_seconds, strategy)?,
resolution: resolution.map(|r| to_extend_resolution(r, strategy)).transpose()?,
negative_prompt,
generate_audio,
seed: None,
auto_fix: None,
safety_tolerance: None,
})
} else if let Some(image_urls) = reference_images {
if start.is_some() {
return Err(unsupported(
"start_frame",
"Veo 3.1 reference-to-video cannot combine start_frame with reference_images",
));
}
if end.is_some() {
return Err(unsupported(
"end_frame",
"Veo 3.1 reference-to-video cannot combine end_frame with reference_images",
));
}
// NB: reference-to-video has no negative_prompt or seed fields on fal's
// schema — those builder inputs are dropped silently.
FalVeo3p1Mode::ReferenceToVideo(Veo3p1ReferenceToVideoRequest {
prompt,
image_urls,
aspect_ratio: aspect_ratio.and_then(to_reference_aspect_ratio),
duration: plan_duration(builder.duration_seconds, strategy)?.map(to_reference_duration),
resolution: resolution.map(to_reference_resolution),
generate_audio,
auto_fix: None,
safety_tolerance: None,
})
} else {
let duration = plan_duration(builder.duration_seconds, strategy)?;
match (start, end) {
(None, None) => FalVeo3p1Mode::TextToVideo(Veo3p1TextToVideoRequest {
prompt,
aspect_ratio: aspect_ratio.and_then(to_t2v_aspect_ratio),
duration: duration.map(to_t2v_duration),
resolution: resolution.map(to_t2v_resolution),
negative_prompt,
generate_audio,
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
(Some(image_url), None) => FalVeo3p1Mode::ImageToVideo(Veo3p1ImageToVideoRequest {
prompt,
image_url,
aspect_ratio: aspect_ratio.map(to_i2v_aspect_ratio),
duration: duration.map(to_i2v_duration),
resolution: resolution.map(to_i2v_resolution),
generate_audio,
negative_prompt,
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
(Some(first_frame_url), Some(last_frame_url)) => FalVeo3p1Mode::FirstLastFrameToVideo(
Veo3p1FirstLastFrameToVideoRequest {
prompt,
first_frame_url,
last_frame_url,
aspect_ratio: aspect_ratio.map(to_flf_aspect_ratio),
duration: duration.map(to_flf_duration),
resolution: resolution.map(to_flf_resolution),
generate_audio,
negative_prompt,
seed: None,
auto_fix: None,
safety_tolerance: None,
},
),
(None, Some(_)) => {
return Err(unsupported(
"end_frame",
"Veo 3.1 requires a start_frame when end_frame is provided",
));
}
}
};
Ok(FalVeo3p1RequestState { mode })
@@ -120,6 +208,30 @@ fn optional_url(image_ref: Option<ImageRef>) -> Result<Option<String>, ArtcraftR
}
}
fn reference_image_urls(refs: Option<ImageListRef>) -> Result<Option<Vec<String>>, ArtcraftRouterError> {
match refs {
None => Ok(None),
Some(ImageListRef::Urls(urls)) if urls.is_empty() => Ok(None),
Some(ImageListRef::Urls(urls)) => Ok(Some(urls)),
Some(ImageListRef::MediaFileTokens(tokens)) if tokens.is_empty() => Ok(None),
Some(ImageListRef::MediaFileTokens(_)) => {
Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls))
}
}
}
fn reference_video_urls(refs: Option<VideoListRef>) -> Result<Option<Vec<String>>, ArtcraftRouterError> {
match refs {
None => Ok(None),
Some(VideoListRef::Urls(urls)) if urls.is_empty() => Ok(None),
Some(VideoListRef::Urls(urls)) => Ok(Some(urls)),
Some(VideoListRef::MediaFileTokens(tokens)) if tokens.is_empty() => Ok(None),
Some(VideoListRef::MediaFileTokens(_)) => {
Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls))
}
}
}
fn plan_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
@@ -153,6 +265,7 @@ fn plan_resolution(
None => Ok(None),
Some(RouterResolution::SevenTwentyP) => Ok(Some(R::SevenTwentyP)),
Some(RouterResolution::TenEightyP) => Ok(Some(R::TenEightyP)),
Some(RouterResolution::FourK) => Ok(Some(R::FourK)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("resolution", &format!("{:?}", other)))
@@ -176,8 +289,43 @@ fn plan_duration(
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("duration_seconds", &format!("{}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanDuration::Eight)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanDuration::Four)),
// Nearest supported duration above/below, clamped to the 4s–8s range.
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(match other {
0..=4 => PlanDuration::Four,
5..=6 => PlanDuration::Six,
_ => PlanDuration::Eight,
})),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(match other {
0..=5 => PlanDuration::Four,
6..=7 => PlanDuration::Six,
_ => PlanDuration::Eight,
})),
},
}
}
/// Extend-video also supports fal's 7s default in addition to 4/6/8.
fn plan_extend_duration(
duration_seconds: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<Veo3p1ExtendVideoDuration>, ArtcraftRouterError> {
use Veo3p1ExtendVideoDuration as D;
match duration_seconds {
None => Ok(None),
Some(4) => Ok(Some(D::FourSeconds)),
Some(6) => Ok(Some(D::SixSeconds)),
Some(7) => Ok(Some(D::SevenSeconds)),
Some(8) => Ok(Some(D::EightSeconds)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("duration_seconds", &format!("{}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => {
Ok(Some(if other < 5 { D::FourSeconds } else if other == 5 { D::SixSeconds } else { D::EightSeconds }))
}
RequestMismatchMitigationStrategy::PayLessDowngrade => {
Ok(Some(if other <= 5 { D::FourSeconds } else { D::EightSeconds }))
}
},
}
}
@@ -189,98 +337,145 @@ fn unsupported(field: &'static str, value: &str) -> ArtcraftRouterError {
})
}
fn to_t2v_duration(d: PlanDuration) -> EnqueueVeo3p1TextToVideoDurationSeconds {
match d {
PlanDuration::Four => EnqueueVeo3p1TextToVideoDurationSeconds::Four,
PlanDuration::Six => EnqueueVeo3p1TextToVideoDurationSeconds::Six,
PlanDuration::Eight => EnqueueVeo3p1TextToVideoDurationSeconds::Eight,
}
}
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> EnqueueVeo3p1TextToVideoAspectRatio {
// The text-to-video endpoint has no `auto` aspect ratio; omit the field and
// let fal apply its own default (16:9).
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> Option<Veo3p1TextToVideoAspectRatio> {
match a {
PlanAspectRatio::Auto => EnqueueVeo3p1TextToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => EnqueueVeo3p1TextToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => EnqueueVeo3p1TextToVideoAspectRatio::NineBySixteen,
PlanAspectRatio::Auto => None,
PlanAspectRatio::SixteenByNine => Some(Veo3p1TextToVideoAspectRatio::SixteenByNine),
PlanAspectRatio::NineBySixteen => Some(Veo3p1TextToVideoAspectRatio::NineBySixteen),
}
}
fn to_t2v_resolution(r: PlanResolution) -> EnqueueVeo3p1TextToVideoResolution {
match r {
PlanResolution::SevenTwentyP => EnqueueVeo3p1TextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => EnqueueVeo3p1TextToVideoResolution::TenEightyP,
}
}
fn to_i2v_duration(d: PlanDuration) -> EnqueueVeo3p1ImageToVideoDurationSeconds {
fn to_t2v_duration(d: PlanDuration) -> Veo3p1TextToVideoDuration {
match d {
PlanDuration::Four => EnqueueVeo3p1ImageToVideoDurationSeconds::Four,
PlanDuration::Six => EnqueueVeo3p1ImageToVideoDurationSeconds::Six,
PlanDuration::Eight => EnqueueVeo3p1ImageToVideoDurationSeconds::Eight,
PlanDuration::Four => Veo3p1TextToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1TextToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1TextToVideoDuration::EightSeconds,
}
}
fn to_i2v_aspect_ratio(a: PlanAspectRatio) -> EnqueueVeo3p1ImageToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => EnqueueVeo3p1ImageToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => EnqueueVeo3p1ImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => EnqueueVeo3p1ImageToVideoAspectRatio::NineBySixteen,
}
}
fn to_i2v_resolution(r: PlanResolution) -> EnqueueVeo3p1ImageToVideoResolution {
fn to_t2v_resolution(r: PlanResolution) -> Veo3p1TextToVideoResolution {
match r {
PlanResolution::SevenTwentyP => EnqueueVeo3p1ImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => EnqueueVeo3p1ImageToVideoResolution::TenEightyP,
PlanResolution::SevenTwentyP => Veo3p1TextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1TextToVideoResolution::TenEightyP,
PlanResolution::FourK => Veo3p1TextToVideoResolution::FourK,
}
}
fn to_flf_duration(d: PlanDuration) -> EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds {
fn to_i2v_aspect_ratio(a: PlanAspectRatio) -> Veo3p1ImageToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => Veo3p1ImageToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => Veo3p1ImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Veo3p1ImageToVideoAspectRatio::NineBySixteen,
}
}
fn to_i2v_duration(d: PlanDuration) -> Veo3p1ImageToVideoDuration {
match d {
PlanDuration::Four => EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds::Four,
PlanDuration::Six => EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds::Six,
PlanDuration::Eight => EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds::Eight,
PlanDuration::Four => Veo3p1ImageToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1ImageToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1ImageToVideoDuration::EightSeconds,
}
}
fn to_flf_aspect_ratio(a: PlanAspectRatio) -> EnqueueVeo3p1FirstLastFrameImageToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => EnqueueVeo3p1FirstLastFrameImageToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => EnqueueVeo3p1FirstLastFrameImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => EnqueueVeo3p1FirstLastFrameImageToVideoAspectRatio::NineBySixteen,
}
}
fn to_flf_resolution(r: PlanResolution) -> EnqueueVeo3p1FirstLastFrameImageToVideoResolution {
fn to_i2v_resolution(r: PlanResolution) -> Veo3p1ImageToVideoResolution {
match r {
PlanResolution::SevenTwentyP => EnqueueVeo3p1FirstLastFrameImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => EnqueueVeo3p1FirstLastFrameImageToVideoResolution::TenEightyP,
PlanResolution::SevenTwentyP => Veo3p1ImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1ImageToVideoResolution::TenEightyP,
PlanResolution::FourK => Veo3p1ImageToVideoResolution::FourK,
}
}
fn to_flf_aspect_ratio(a: PlanAspectRatio) -> Veo3p1FirstLastFrameToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => Veo3p1FirstLastFrameToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => Veo3p1FirstLastFrameToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Veo3p1FirstLastFrameToVideoAspectRatio::NineBySixteen,
}
}
fn to_flf_duration(d: PlanDuration) -> Veo3p1FirstLastFrameToVideoDuration {
match d {
PlanDuration::Four => Veo3p1FirstLastFrameToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1FirstLastFrameToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1FirstLastFrameToVideoDuration::EightSeconds,
}
}
fn to_flf_resolution(r: PlanResolution) -> Veo3p1FirstLastFrameToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3p1FirstLastFrameToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1FirstLastFrameToVideoResolution::TenEightyP,
PlanResolution::FourK => Veo3p1FirstLastFrameToVideoResolution::FourK,
}
}
// The reference-to-video endpoint has no `auto` aspect ratio; omit the field
// and let fal apply its own default (16:9).
fn to_reference_aspect_ratio(a: PlanAspectRatio) -> Option<Veo3p1ReferenceToVideoAspectRatio> {
match a {
PlanAspectRatio::Auto => None,
PlanAspectRatio::SixteenByNine => Some(Veo3p1ReferenceToVideoAspectRatio::SixteenByNine),
PlanAspectRatio::NineBySixteen => Some(Veo3p1ReferenceToVideoAspectRatio::NineBySixteen),
}
}
fn to_reference_duration(d: PlanDuration) -> Veo3p1ReferenceToVideoDuration {
match d {
PlanDuration::Four => Veo3p1ReferenceToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1ReferenceToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1ReferenceToVideoDuration::EightSeconds,
}
}
fn to_reference_resolution(r: PlanResolution) -> Veo3p1ReferenceToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3p1ReferenceToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1ReferenceToVideoResolution::TenEightyP,
PlanResolution::FourK => Veo3p1ReferenceToVideoResolution::FourK,
}
}
fn to_extend_aspect_ratio(a: PlanAspectRatio) -> Veo3p1ExtendVideoAspectRatio {
match a {
PlanAspectRatio::Auto => Veo3p1ExtendVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => Veo3p1ExtendVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Veo3p1ExtendVideoAspectRatio::NineBySixteen,
}
}
// Extend-video has no 4k tier; the nearest supported resolution in either
// direction is 1080p.
fn to_extend_resolution(
r: PlanResolution,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo3p1ExtendVideoResolution, ArtcraftRouterError> {
match r {
PlanResolution::SevenTwentyP => Ok(Veo3p1ExtendVideoResolution::SevenTwentyP),
PlanResolution::TenEightyP => Ok(Veo3p1ExtendVideoResolution::TenEightyP),
PlanResolution::FourK => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("resolution", "Veo 3.1 extend-video does not support 4k"))
}
_ => Ok(Veo3p1ExtendVideoResolution::TenEightyP),
},
}
}
#[cfg(test)]
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
use super::*;
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
fn unwrap_mode(b: GenerateVideoRequestBuilder) -> FalVeo3p1Mode {
match build_fal_veo_3p1(b).expect("build") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3p1(s)) => s.mode,
_ => panic!("expected FalVeo3p1"),
}
}
const START_FRAME_URL: &str = "https://example.com/a.png";
const END_FRAME_URL: &str = "https://example.com/b.png";
const REFERENCE_IMAGE_URL_A: &str = "https://example.com/ref_a.png";
const REFERENCE_IMAGE_URL_B: &str = "https://example.com/ref_b.png";
const REFERENCE_VIDEO_URL: &str = "https://example.com/in.mp4";
mod mode_selection {
use super::*;
@@ -293,23 +488,211 @@ mod tests {
#[test]
fn start_only_picks_i2v() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1Mode::ImageToVideo(_)));
}
#[test]
fn start_and_end_picks_first_last_frame() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1Mode::FirstLastFrame(_)));
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1Mode::FirstLastFrameToVideo(_)));
}
#[test]
fn end_only_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
assert!(build_fal_veo_3p1(b).is_err());
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn reference_images_pick_reference_to_video() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL_A.to_string()]));
assert!(matches!(unwrap_mode(b), FalVeo3p1Mode::ReferenceToVideo(_)));
}
#[test]
fn reference_videos_pick_extend_video() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
assert!(matches!(unwrap_mode(b), FalVeo3p1Mode::ExtendVideo(_)));
}
#[test]
fn empty_reference_lists_fall_through_to_t2v() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![]));
b.reference_videos = Some(VideoListRef::Urls(vec![]));
assert!(matches!(unwrap_mode(b), FalVeo3p1Mode::TextToVideo(_)));
}
#[test]
fn public_build_fn_wraps_request_enum() {
match build_fal_veo_3p1(base_builder()).expect("build") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3p1(s)) => {
assert!(matches!(s.mode, FalVeo3p1Mode::TextToVideo(_)));
}
_ => panic!("expected Request(FalVeo3p1)"),
}
}
}
mod extend_video {
use super::*;
#[test]
fn extend_maps_video_url_and_7s_duration() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.duration_seconds = Some(7);
match unwrap_mode(b) {
FalVeo3p1Mode::ExtendVideo(r) => {
assert_eq!(r.video_url, REFERENCE_VIDEO_URL);
assert!(matches!(r.duration, Some(Veo3p1ExtendVideoDuration::SevenSeconds)));
}
_ => panic!("expected extend"),
}
}
#[test]
fn extend_requires_exactly_one_video() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![
REFERENCE_VIDEO_URL.to_string(),
"https://example.com/other.mp4".to_string(),
]));
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn extend_rejects_start_frame() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn extend_rejects_end_frame() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn extend_rejects_reference_images() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL_A.to_string()]));
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn extend_duration_5s_pay_more_upgrades_to_6() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.duration_seconds = Some(5);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayMoreUpgrade;
match unwrap_mode(b) {
FalVeo3p1Mode::ExtendVideo(r) => {
assert!(matches!(r.duration, Some(Veo3p1ExtendVideoDuration::SixSeconds)));
}
_ => panic!("expected extend"),
}
}
#[test]
fn extend_4k_errors_with_error_out() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.resolution = Some(RouterResolution::FourK);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn extend_4k_maps_to_1080p_with_mitigation() {
for strategy in [
RequestMismatchMitigationStrategy::PayMoreUpgrade,
RequestMismatchMitigationStrategy::PayLessDowngrade,
] {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.resolution = Some(RouterResolution::FourK);
b.request_mismatch_mitigation_strategy = strategy;
match unwrap_mode(b) {
FalVeo3p1Mode::ExtendVideo(r) => {
assert!(matches!(r.resolution, Some(Veo3p1ExtendVideoResolution::TenEightyP)));
}
_ => panic!("expected extend"),
}
}
}
}
mod reference_to_video {
use super::*;
#[test]
fn reference_maps_image_urls() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![
REFERENCE_IMAGE_URL_A.to_string(),
REFERENCE_IMAGE_URL_B.to_string(),
]));
match unwrap_mode(b) {
FalVeo3p1Mode::ReferenceToVideo(r) => {
assert_eq!(r.image_urls, vec![REFERENCE_IMAGE_URL_A, REFERENCE_IMAGE_URL_B]);
}
_ => panic!("expected reference"),
}
}
#[test]
fn reference_rejects_start_frame() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL_A.to_string()]));
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn reference_rejects_end_frame() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL_A.to_string()]));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn reference_auto_aspect_ratio_is_omitted() {
// reference-to-video has no `auto` wire value; Auto must map to None.
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL_A.to_string()]));
b.aspect_ratio = Some(RouterAspectRatio::Auto);
match unwrap_mode(b) {
FalVeo3p1Mode::ReferenceToVideo(r) => assert!(r.aspect_ratio.is_none()),
_ => panic!("expected reference"),
}
}
#[test]
fn reference_maps_duration_and_resolution() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL_A.to_string()]));
b.duration_seconds = Some(6);
b.resolution = Some(RouterResolution::FourK);
match unwrap_mode(b) {
FalVeo3p1Mode::ReferenceToVideo(r) => {
assert!(matches!(r.duration, Some(Veo3p1ReferenceToVideoDuration::SixSeconds)));
assert!(matches!(r.resolution, Some(Veo3p1ReferenceToVideoResolution::FourK)));
}
_ => panic!("expected reference"),
}
}
}
@@ -322,7 +705,7 @@ mod tests {
b.duration_seconds = Some(4);
match unwrap_mode(b) {
FalVeo3p1Mode::TextToVideo(r) => {
assert!(matches!(r.duration, Some(EnqueueVeo3p1TextToVideoDurationSeconds::Four)));
assert!(matches!(r.duration, Some(Veo3p1TextToVideoDuration::FourSeconds)));
}
_ => panic!("expected t2v"),
}
@@ -333,7 +716,33 @@ mod tests {
let mut b = base_builder();
b.duration_seconds = Some(5);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_veo_3p1(b).is_err());
assert!(build_fal_veo_3p1_state(b).is_err());
}
#[test]
fn duration_5s_pay_more_upgrades_to_6() {
let mut b = base_builder();
b.duration_seconds = Some(5);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayMoreUpgrade;
match unwrap_mode(b) {
FalVeo3p1Mode::TextToVideo(r) => {
assert!(matches!(r.duration, Some(Veo3p1TextToVideoDuration::SixSeconds)));
}
_ => panic!("expected t2v"),
}
}
#[test]
fn duration_5s_pay_less_downgrades_to_4() {
let mut b = base_builder();
b.duration_seconds = Some(5);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayLessDowngrade;
match unwrap_mode(b) {
FalVeo3p1Mode::TextToVideo(r) => {
assert!(matches!(r.duration, Some(Veo3p1TextToVideoDuration::FourSeconds)));
}
_ => panic!("expected t2v"),
}
}
}
@@ -346,16 +755,56 @@ mod tests {
b.resolution = Some(RouterResolution::SevenTwentyP);
match unwrap_mode(b) {
FalVeo3p1Mode::TextToVideo(r) => {
assert!(matches!(r.resolution, Some(EnqueueVeo3p1TextToVideoResolution::SevenTwentyP)));
assert!(matches!(r.resolution, Some(Veo3p1TextToVideoResolution::SevenTwentyP)));
}
_ => panic!("expected t2v"),
}
}
#[test]
fn t2v_4k_maps_to_four_k() {
let mut b = base_builder();
b.resolution = Some(RouterResolution::FourK);
match unwrap_mode(b) {
FalVeo3p1Mode::TextToVideo(r) => {
assert!(matches!(r.resolution, Some(Veo3p1TextToVideoResolution::FourK)));
}
_ => panic!("expected t2v"),
}
}
}
mod aspect_ratio_conversions {
use super::*;
#[test]
fn t2v_auto_aspect_ratio_is_omitted() {
// t2v has no `auto` wire value; Auto must map to None.
let mut b = base_builder();
b.aspect_ratio = Some(RouterAspectRatio::Auto);
match unwrap_mode(b) {
FalVeo3p1Mode::TextToVideo(r) => assert!(r.aspect_ratio.is_none()),
_ => panic!("expected t2v"),
}
}
#[test]
fn i2v_auto_aspect_ratio_maps_to_auto() {
let mut b = base_builder();
b.aspect_ratio = Some(RouterAspectRatio::Auto);
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
match unwrap_mode(b) {
FalVeo3p1Mode::ImageToVideo(r) => {
assert!(matches!(r.aspect_ratio, Some(Veo3p1ImageToVideoAspectRatio::Auto)));
}
_ => panic!("expected i2v"),
}
}
}
#[test]
fn full_combinatorial_pass() {
let resolutions = [None, Some(RouterResolution::SevenTwentyP), Some(RouterResolution::TenEightyP)];
let resolutions = [None, Some(RouterResolution::SevenTwentyP), Some(RouterResolution::TenEightyP), Some(RouterResolution::FourK)];
let durations = [None, Some(4u16), Some(6), Some(8)];
let aspect_ratios = [None, Some(RouterAspectRatio::Auto), Some(RouterAspectRatio::WideSixteenByNine), Some(RouterAspectRatio::TallNineBySixteen)];
let audios = [None, Some(true), Some(false)];
@@ -372,18 +821,31 @@ mod tests {
b.aspect_ratio = aspect_ratio;
b.generate_audio = generate_audio;
if frames >= 1 {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
}
if frames == 2 {
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
}
assert!(build_fal_veo_3p1(b).is_ok());
assert!(build_fal_veo_3p1_state(b).is_ok());
combos += 1;
}
}
}
}
}
assert_eq!(combos, 3 * 4 * 4 * 3 * 3);
assert_eq!(combos, 4 * 4 * 4 * 3 * 3);
}
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
fn unwrap_mode(b: GenerateVideoRequestBuilder) -> FalVeo3p1Mode {
build_fal_veo_3p1_state(b).expect("build").mode
}
}
@@ -1,30 +1,32 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_first_last_frame_image_to_video_webhook::EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds;
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_image_to_video_webhook::EnqueueVeo3p1ImageToVideoDurationSeconds;
use fal_client::requests_old::webhook::video::text::enqueue_veo_3p1_text_to_video_webhook::EnqueueVeo3p1TextToVideoDurationSeconds;
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::fal::veo_3p1::request::{FalVeo3p1Mode, FalVeo3p1RequestState};
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
#[derive(Clone, Debug)]
pub struct FalVeo3p1CostState {
pub duration_seconds: u64,
pub generate_audio: bool,
pub cost_in_usd_cents: u64,
}
impl FalVeo3p1CostState {
pub fn from_request(request: &FalVeo3p1RequestState) -> Self {
let (duration_seconds, generate_audio) = extract_duration_and_audio(&request.mode);
Self { duration_seconds, generate_audio }
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalVeo3p1Mode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1Mode::ImageToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1Mode::FirstLastFrameToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1Mode::ReferenceToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1Mode::ExtendVideo(req) => req.calculate_cost_in_cents(),
};
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
// $0.20/sec audio off, $0.40/sec audio on (720p/1080p).
let per_second_cents: u64 = if self.generate_audio { 40 } else { 20 };
let cost_in_usd_cents = per_second_cents * self.duration_seconds;
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
@@ -34,107 +36,204 @@ impl FalVeo3p1CostState {
}
}
pub(crate) fn extract_duration_and_audio(mode: &FalVeo3p1Mode) -> (u64, bool) {
match mode {
FalVeo3p1Mode::TextToVideo(req) => (t2v_duration_seconds(req.duration), req.generate_audio.unwrap_or(true)),
FalVeo3p1Mode::ImageToVideo(req) => (i2v_duration_seconds(req.duration), req.generate_audio.unwrap_or(true)),
FalVeo3p1Mode::FirstLastFrame(req) => (flf_duration_seconds(req.duration), req.generate_audio.unwrap_or(true)),
}
}
fn t2v_duration_seconds(d: Option<EnqueueVeo3p1TextToVideoDurationSeconds>) -> u64 {
match d {
Some(EnqueueVeo3p1TextToVideoDurationSeconds::Four) => 4,
Some(EnqueueVeo3p1TextToVideoDurationSeconds::Six) => 6,
Some(EnqueueVeo3p1TextToVideoDurationSeconds::Eight) | None => 8,
}
}
fn i2v_duration_seconds(d: Option<EnqueueVeo3p1ImageToVideoDurationSeconds>) -> u64 {
match d {
Some(EnqueueVeo3p1ImageToVideoDurationSeconds::Four) => 4,
Some(EnqueueVeo3p1ImageToVideoDurationSeconds::Six) => 6,
Some(EnqueueVeo3p1ImageToVideoDurationSeconds::Eight) | None => 8,
}
}
fn flf_duration_seconds(d: Option<EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds>) -> u64 {
match d {
Some(EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds::Four) => 4,
Some(EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds::Six) => 6,
Some(EnqueueVeo3p1FirstLastFrameImageToVideoDurationSeconds::Eight) | None => 8,
}
}
#[cfg(test)]
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_list_ref::ImageListRef;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::video_list_ref::VideoListRef;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_3p1::build::build_fal_veo_3p1_state;
use super::*;
// Veo 3.1 pricing:
// 720p / 1080p: $0.20/sec (audio off), $0.40/sec (audio on)
// 4k: $0.40/sec (audio off), $0.60/sec (audio on)
// Extend-video has no 4k tier and defaults to 7s.
#[derive(Clone, Copy)]
enum Modality {
TextToVideo,
ImageToVideo,
FirstLastFrame,
Reference,
Extend,
}
mod text_to_video {
use super::*;
#[test]
fn t2v_8s_audio_on_720p_is_320() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::SevenTwentyP), Some(true)), 320);
}
#[test]
fn t2v_8s_audio_off_720p_is_160() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::SevenTwentyP), Some(false)), 160);
}
#[test]
fn t2v_4s_audio_on_is_160() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(4), None, Some(true)), 160);
}
#[test]
fn t2v_6s_audio_on_is_240() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(6), None, Some(true)), 240);
}
#[test]
fn t2v_8s_audio_on_4k_is_480() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::FourK), Some(true)), 480);
}
#[test]
fn t2v_8s_audio_off_4k_is_320() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::FourK), Some(false)), 320);
}
#[test]
fn t2v_defaults_are_8s_720p_audio_on_320() {
// duration=None→8s, resolution=None→720p, audio=None→on.
assert_eq!(cost_cents(Modality::TextToVideo, None, None, None), 320);
}
#[test]
fn t2v_1080p_bills_same_as_720p() {
assert_eq!(
cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::TenEightyP), Some(true)),
cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::SevenTwentyP), Some(true)),
);
}
}
mod image_to_video {
use super::*;
#[test]
fn i2v_6s_audio_on_is_240() {
assert_eq!(cost_cents(Modality::ImageToVideo, Some(6), None, Some(true)), 240);
}
#[test]
fn i2v_8s_audio_off_1080p_is_160() {
assert_eq!(cost_cents(Modality::ImageToVideo, Some(8), Some(RouterResolution::TenEightyP), Some(false)), 160);
}
}
mod first_last_frame_to_video {
use super::*;
#[test]
fn flf_4s_audio_on_is_160() {
assert_eq!(cost_cents(Modality::FirstLastFrame, Some(4), None, Some(true)), 160);
}
#[test]
fn flf_8s_audio_on_4k_is_480() {
assert_eq!(cost_cents(Modality::FirstLastFrame, Some(8), Some(RouterResolution::FourK), Some(true)), 480);
}
}
mod reference_to_video {
use super::*;
#[test]
fn reference_8s_audio_on_1080p_is_320() {
assert_eq!(cost_cents(Modality::Reference, Some(8), Some(RouterResolution::TenEightyP), Some(true)), 320);
}
#[test]
fn reference_4s_audio_off_720p_is_80() {
assert_eq!(cost_cents(Modality::Reference, Some(4), Some(RouterResolution::SevenTwentyP), Some(false)), 80);
}
#[test]
fn reference_8s_audio_on_4k_is_480() {
assert_eq!(cost_cents(Modality::Reference, Some(8), Some(RouterResolution::FourK), Some(true)), 480);
}
}
mod extend_video {
use super::*;
#[test]
fn extend_7s_audio_on_is_280() {
assert_eq!(cost_cents(Modality::Extend, Some(7), None, Some(true)), 280);
}
#[test]
fn extend_7s_audio_off_is_140() {
assert_eq!(cost_cents(Modality::Extend, Some(7), None, Some(false)), 140);
}
#[test]
fn extend_8s_audio_on_is_320() {
assert_eq!(cost_cents(Modality::Extend, Some(8), None, Some(true)), 320);
}
#[test]
fn extend_defaults_are_7s_audio_on_280() {
// duration=None→7s (fal's extend default), audio=None→on.
assert_eq!(cost_cents(Modality::Extend, None, None, None), 280);
}
}
#[test]
fn t2v_i2v_flf_and_reference_price_identically() {
let expected = cost_cents(Modality::TextToVideo, Some(6), Some(RouterResolution::TenEightyP), Some(true));
for modality in [Modality::ImageToVideo, Modality::FirstLastFrame, Modality::Reference] {
assert_eq!(cost_cents(modality, Some(6), Some(RouterResolution::TenEightyP), Some(true)), expected);
}
}
#[test]
fn audio_costs_more_than_no_audio() {
assert!(
cost_cents(Modality::TextToVideo, Some(8), None, Some(false))
< cost_cents(Modality::TextToVideo, Some(8), None, Some(true))
);
}
fn cost_cents(
modality: Modality,
duration_seconds: Option<u16>,
resolution: Option<RouterResolution>,
generate_audio: Option<bool>,
frames: u8,
) -> u64 {
let mut b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
resolution,
generate_audio,
..Default::default()
};
if frames >= 1 {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
match modality {
Modality::TextToVideo => {}
Modality::ImageToVideo => {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
Modality::FirstLastFrame => {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
}
Modality::Reference => {
b.reference_images = Some(ImageListRef::Urls(vec!["https://example.com/ref.png".to_string()]));
}
Modality::Extend => {
b.reference_videos = Some(VideoListRef::Urls(vec!["https://example.com/in.mp4".to_string()]));
}
}
if frames == 2 {
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
}
b.build2().expect("build2").estimate_cost().expect("estimate_cost").cost_in_usd_cents.expect("cost")
}
mod numeric_literal_pricing {
use super::*;
#[test]
fn t2v_audio_on_4s_is_160() { assert_eq!(cost_cents(Some(4), Some(true), 0), 160); }
#[test]
fn t2v_audio_on_6s_is_240() { assert_eq!(cost_cents(Some(6), Some(true), 0), 240); }
#[test]
fn t2v_audio_on_8s_is_320() { assert_eq!(cost_cents(Some(8), Some(true), 0), 320); }
#[test]
fn t2v_audio_off_4s_is_80() { assert_eq!(cost_cents(Some(4), Some(false), 0), 80); }
#[test]
fn t2v_audio_off_8s_is_160() { assert_eq!(cost_cents(Some(8), Some(false), 0), 160); }
#[test]
fn t2v_no_duration_defaults_to_8s() {
assert_eq!(cost_cents(None, Some(true), 0), 320);
}
#[test]
fn t2v_no_audio_defaults_to_audio_on() {
assert_eq!(cost_cents(Some(6), None, 0), 240);
}
}
#[test]
fn all_three_modes_price_identically() {
let t2v = cost_cents(Some(6), Some(true), 0);
let i2v = cost_cents(Some(6), Some(true), 1);
let flf = cost_cents(Some(6), Some(true), 2);
assert_eq!(t2v, i2v);
assert_eq!(i2v, flf);
}
#[test]
fn audio_costs_more_than_no_audio() {
assert!(cost_cents(Some(8), Some(false), 0) < cost_cents(Some(8), Some(true), 0));
let state = build_fal_veo_3p1_state(b).expect("build state");
FalVeo3p1CostState::from_request(&state)
.estimate_cost()
.cost_in_usd_cents
.expect("cost")
}
}
@@ -1,30 +1,26 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_first_last_frame_image_to_video_webhook::{
enqueue_veo_3p1_first_last_frame_image_to_video_webhook, EnqueueVeo3p1FirstLastFrameImageToVideoArgs,
EnqueueVeo3p1FirstLastFrameImageToVideoRequest,
};
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_image_to_video_webhook::{
enqueue_veo_3p1_image_to_video_webhook, EnqueueVeo3p1ImageToVideoArgs, EnqueueVeo3p1ImageToVideoRequest,
};
use fal_client::requests_old::webhook::video::text::enqueue_veo_3p1_text_to_video_webhook::{
enqueue_veo_3p1_text_to_video_webhook, EnqueueVeo3p1TextToVideoArgs, EnqueueVeo3p1TextToVideoRequest,
};
use fal_client::requests::api::video::extend::veo_3p1::api::Veo3p1ExtendVideoRequest;
use fal_client::requests::api::video::image::veo_3p1::api::Veo3p1ImageToVideoRequest;
use fal_client::requests::api::video::images::veo_3p1::api::Veo3p1FirstLastFrameToVideoRequest;
use fal_client::requests::api::video::reference::veo_3p1::api::Veo3p1ReferenceToVideoRequest;
use fal_client::requests::api::video::text::veo_3p1::api::Veo3p1TextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::errors::provider_error::ProviderError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
#[derive(Clone, Debug)]
pub enum FalVeo3p1Mode {
TextToVideo(EnqueueVeo3p1TextToVideoRequest),
ImageToVideo(EnqueueVeo3p1ImageToVideoRequest),
FirstLastFrame(EnqueueVeo3p1FirstLastFrameImageToVideoRequest),
TextToVideo(Veo3p1TextToVideoRequest),
ImageToVideo(Veo3p1ImageToVideoRequest),
FirstLastFrameToVideo(Veo3p1FirstLastFrameToVideoRequest),
ReferenceToVideo(Veo3p1ReferenceToVideoRequest),
ExtendVideo(Veo3p1ExtendVideoRequest),
}
#[derive(Clone, Debug)]
@@ -34,66 +30,81 @@ pub struct FalVeo3p1RequestState {
impl FalVeo3p1RequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
let webhook_url = client.webhook_url.as_deref()
.ok_or(ArtcraftRouterError::Client(ClientError::WebhookUrlRequired))?;
let (webhook_response, outbound_request): (_, Arc<dyn Debug + Send + Sync>) = match &self.mode {
FalVeo3p1Mode::TextToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = EnqueueVeo3p1TextToVideoArgs {
request: request.clone(),
webhook_url,
api_key: &client.api_key,
};
(enqueue_veo_3p1_text_to_video_webhook(args).await, outbound)
}
FalVeo3p1Mode::ImageToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = EnqueueVeo3p1ImageToVideoArgs {
request: request.clone(),
webhook_url,
api_key: &client.api_key,
};
(enqueue_veo_3p1_image_to_video_webhook(args).await, outbound)
}
FalVeo3p1Mode::FirstLastFrame(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = EnqueueVeo3p1FirstLastFrameImageToVideoArgs {
request: request.clone(),
webhook_url,
api_key: &client.api_key,
};
(enqueue_veo_3p1_first_last_frame_image_to_video_webhook(args).await, outbound)
}
};
match &self.mode {
FalVeo3p1Mode::TextToVideo(request) => send_request(request, client).await,
FalVeo3p1Mode::ImageToVideo(request) => send_request(request, client).await,
FalVeo3p1Mode::FirstLastFrameToVideo(request) => send_request(request, client).await,
FalVeo3p1Mode::ReferenceToVideo(request) => send_request(request, client).await,
FalVeo3p1Mode::ExtendVideo(request) => send_request(request, client).await,
}
}
}
let webhook_response = webhook_response
.map_err(|e| ArtcraftRouterError::Provider(ProviderError::Fal(e)))?;
// ── Helpers ──
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: webhook_response.request_id,
gateway_request_id: webhook_response.gateway_request_id,
maybe_status_url: None,
maybe_response_url: None,
maybe_outbound_request: Some(outbound_request),
}))
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
use test_data::web::video_urls::ANGRY_SHIBA_VIDEO_URL;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_list_ref::ImageListRef;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::video_list_ref::VideoListRef;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::test_helpers::get_fal_client;
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_text_to_video_720p_4s() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("a quiet meadow with wildflowers".to_string()),
@@ -105,7 +116,7 @@ mod tests {
}
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_image_to_video_8s() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the dog leaps into the lake.".to_string()),
@@ -117,7 +128,7 @@ mod tests {
}
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_first_last_frame() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("a smooth transition between scenes".to_string()),
@@ -129,6 +140,32 @@ mod tests {
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn live_reference_to_video_4s() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the dog runs across a sunlit meadow".to_string()),
reference_images: Some(ImageListRef::Urls(vec![JUNO_AT_LAKE_IMAGE_URL.to_string()])),
duration_seconds: Some(4),
generate_audio: Some(false),
..builder()
}).await;
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn live_extend_video_7s() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the scene continues naturally, keeping the same motion and style".to_string()),
reference_videos: Some(VideoListRef::Urls(vec![ANGRY_SHIBA_VIDEO_URL.to_string()])),
duration_seconds: Some(7),
generate_audio: Some(false),
..builder()
}).await;
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
}
fn builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1,
@@ -1,19 +1,29 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_fast_first_last_frame_image_to_video_webhook::{
EnqueueVeo3p1FastFirstLastFrameImageToVideoAspectRatio, EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds,
EnqueueVeo3p1FastFirstLastFrameImageToVideoRequest, EnqueueVeo3p1FastFirstLastFrameImageToVideoResolution,
use fal_client::requests::api::video::extend::veo_3p1_fast::api::{
Veo3p1FastExtendVideoAspectRatio, Veo3p1FastExtendVideoDuration, Veo3p1FastExtendVideoRequest,
Veo3p1FastExtendVideoResolution,
};
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_fast_image_to_video_webhook::{
EnqueueVeo3p1FastImageToVideoAspectRatio, EnqueueVeo3p1FastImageToVideoDurationSeconds,
EnqueueVeo3p1FastImageToVideoRequest, EnqueueVeo3p1FastImageToVideoResolution,
use fal_client::requests::api::video::image::veo_3p1_fast::api::{
Veo3p1FastImageToVideoAspectRatio, Veo3p1FastImageToVideoDuration, Veo3p1FastImageToVideoRequest,
Veo3p1FastImageToVideoResolution,
};
use fal_client::requests_old::webhook::video::text::enqueue_veo_3p1_fast_text_to_video_webhook::{
EnqueueVeo3p1FastTextToVideoAspectRatio, EnqueueVeo3p1FastTextToVideoDurationSeconds,
EnqueueVeo3p1FastTextToVideoRequest, EnqueueVeo3p1FastTextToVideoResolution,
use fal_client::requests::api::video::images::veo_3p1_fast::api::{
Veo3p1FastFirstLastFrameToVideoAspectRatio, Veo3p1FastFirstLastFrameToVideoDuration,
Veo3p1FastFirstLastFrameToVideoRequest, Veo3p1FastFirstLastFrameToVideoResolution,
};
use fal_client::requests::api::video::reference::veo_3p1_fast::api::{
Veo3p1FastReferenceToVideoAspectRatio, Veo3p1FastReferenceToVideoDuration,
Veo3p1FastReferenceToVideoRequest, Veo3p1FastReferenceToVideoResolution,
};
use fal_client::requests::api::video::text::veo_3p1_fast::api::{
Veo3p1FastTextToVideoAspectRatio, Veo3p1FastTextToVideoDuration, Veo3p1FastTextToVideoRequest,
Veo3p1FastTextToVideoResolution,
};
use crate::api::image_list_ref::ImageListRef;
use crate::api::image_ref::ImageRef;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_resolution::RouterResolution;
use crate::api::image_ref::ImageRef;
use crate::api::video_list_ref::VideoListRef;
use crate::client::request_mismatch_mitigation_strategy::RequestMismatchMitigationStrategy;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
@@ -35,6 +45,7 @@ pub(crate) enum PlanAspectRatio {
pub(crate) enum PlanResolution {
SevenTwentyP,
TenEightyP,
FourK,
}
#[derive(Debug, Clone, Copy)]
@@ -47,60 +58,146 @@ pub(crate) enum PlanDuration {
pub fn build_fal_veo_3p1_fast(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_fal_veo_3p1_fast_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3p1Fast(state)))
}
pub(crate) fn build_fal_veo_3p1_fast_state(
builder: GenerateVideoRequestBuilder,
) -> Result<FalVeo3p1FastRequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
let start = optional_url(builder.start_frame.clone())?;
let end = optional_url(builder.end_frame.clone())?;
let reference_images = reference_image_urls(builder.reference_images.clone())?;
let reference_videos = reference_video_urls(builder.reference_videos.clone())?;
let aspect_ratio = plan_aspect_ratio(builder.aspect_ratio, strategy)?;
let resolution = plan_resolution(builder.resolution, strategy)?;
let duration = plan_duration(builder.duration_seconds, strategy)?;
let prompt = builder.prompt.clone().unwrap_or_default();
let negative_prompt = builder.negative_prompt.clone();
let generate_audio = builder.generate_audio;
let mode = match (start, end) {
(None, None) => FalVeo3p1FastMode::TextToVideo(EnqueueVeo3p1FastTextToVideoRequest {
prompt,
duration: duration.map(to_t2v_duration),
aspect_ratio: aspect_ratio.map(to_t2v_aspect_ratio),
resolution: resolution.map(to_t2v_resolution),
generate_audio,
enhance_prompt: None,
negative_prompt,
seed: None,
auto_fix: None,
}),
(Some(image_url), None) => FalVeo3p1FastMode::ImageToVideo(EnqueueVeo3p1FastImageToVideoRequest {
prompt,
image_url,
duration: duration.map(to_i2v_duration),
aspect_ratio: aspect_ratio.map(to_i2v_aspect_ratio),
resolution: resolution.map(to_i2v_resolution),
generate_audio,
}),
(Some(first_frame_url), Some(last_frame_url)) => FalVeo3p1FastMode::FirstLastFrame(
EnqueueVeo3p1FastFirstLastFrameImageToVideoRequest {
prompt,
first_frame_url,
last_frame_url,
duration: duration.map(to_flf_duration),
aspect_ratio: aspect_ratio.map(to_flf_aspect_ratio),
resolution: resolution.map(to_flf_resolution),
generate_audio,
},
),
(None, Some(_)) => {
// Modality dispatch, most specific inputs first:
// 1. reference videos → extend-video
// 2. reference images → reference-to-video
// 3. start + end frames → first-last-frame-to-video
// 4. start frame only → image-to-video
// 5. no media → text-to-video
let mode = if let Some(video_urls) = reference_videos {
if reference_images.is_some() {
return Err(unsupported(
"reference_images",
"Veo 3.1 Fast extend-video cannot combine reference_images with a reference video",
));
}
if start.is_some() {
return Err(unsupported(
"start_frame",
"Veo 3.1 Fast extend-video cannot combine start_frame with a reference video",
));
}
if end.is_some() {
return Err(unsupported(
"end_frame",
"Veo 3.1 Fast requires a start_frame when end_frame is provided",
"Veo 3.1 Fast extend-video cannot combine end_frame with a reference video",
));
}
if video_urls.len() != 1 {
return Err(unsupported(
"reference_videos",
&format!("Veo 3.1 Fast extend-video requires exactly 1 reference video, got {}", video_urls.len()),
));
}
let video_url = video_urls.into_iter().next().expect("checked len == 1");
FalVeo3p1FastMode::ExtendVideo(Veo3p1FastExtendVideoRequest {
prompt,
video_url,
aspect_ratio: aspect_ratio.map(to_extend_aspect_ratio),
duration: plan_extend_duration(builder.duration_seconds, strategy)?,
resolution: resolution.map(|r| to_extend_resolution(r, strategy)).transpose()?,
negative_prompt,
generate_audio,
seed: None,
auto_fix: None,
safety_tolerance: None,
})
} else if let Some(image_urls) = reference_images {
if start.is_some() {
return Err(unsupported(
"start_frame",
"Veo 3.1 Fast reference-to-video cannot combine start_frame with reference_images",
));
}
if end.is_some() {
return Err(unsupported(
"end_frame",
"Veo 3.1 Fast reference-to-video cannot combine end_frame with reference_images",
));
}
// NB: reference-to-video has no negative_prompt or seed fields on fal's
// schema — those builder inputs are dropped silently.
FalVeo3p1FastMode::ReferenceToVideo(Veo3p1FastReferenceToVideoRequest {
prompt,
image_urls,
aspect_ratio: aspect_ratio.and_then(to_reference_aspect_ratio),
duration: plan_duration(builder.duration_seconds, strategy)?.map(to_reference_duration),
resolution: resolution.map(to_reference_resolution),
generate_audio,
auto_fix: None,
safety_tolerance: None,
})
} else {
let duration = plan_duration(builder.duration_seconds, strategy)?;
match (start, end) {
(None, None) => FalVeo3p1FastMode::TextToVideo(Veo3p1FastTextToVideoRequest {
prompt,
aspect_ratio: aspect_ratio.and_then(to_t2v_aspect_ratio),
duration: duration.map(to_t2v_duration),
resolution: resolution.map(to_t2v_resolution),
negative_prompt,
generate_audio,
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
(Some(image_url), None) => FalVeo3p1FastMode::ImageToVideo(Veo3p1FastImageToVideoRequest {
prompt,
image_url,
aspect_ratio: aspect_ratio.map(to_i2v_aspect_ratio),
duration: duration.map(to_i2v_duration),
resolution: resolution.map(to_i2v_resolution),
generate_audio,
negative_prompt,
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
(Some(first_frame_url), Some(last_frame_url)) => FalVeo3p1FastMode::FirstLastFrameToVideo(
Veo3p1FastFirstLastFrameToVideoRequest {
prompt,
first_frame_url,
last_frame_url,
aspect_ratio: aspect_ratio.map(to_flf_aspect_ratio),
duration: duration.map(to_flf_duration),
resolution: resolution.map(to_flf_resolution),
generate_audio,
negative_prompt,
seed: None,
auto_fix: None,
safety_tolerance: None,
},
),
(None, Some(_)) => {
return Err(unsupported(
"end_frame",
"Veo 3.1 Fast requires a start_frame when end_frame is provided",
));
}
}
};
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3p1Fast(
FalVeo3p1FastRequestState { mode },
)))
Ok(FalVeo3p1FastRequestState { mode })
}
fn optional_url(image_ref: Option<ImageRef>) -> Result<Option<String>, ArtcraftRouterError> {
@@ -113,6 +210,30 @@ fn optional_url(image_ref: Option<ImageRef>) -> Result<Option<String>, ArtcraftR
}
}
fn reference_image_urls(refs: Option<ImageListRef>) -> Result<Option<Vec<String>>, ArtcraftRouterError> {
match refs {
None => Ok(None),
Some(ImageListRef::Urls(urls)) if urls.is_empty() => Ok(None),
Some(ImageListRef::Urls(urls)) => Ok(Some(urls)),
Some(ImageListRef::MediaFileTokens(tokens)) if tokens.is_empty() => Ok(None),
Some(ImageListRef::MediaFileTokens(_)) => {
Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls))
}
}
}
fn reference_video_urls(refs: Option<VideoListRef>) -> Result<Option<Vec<String>>, ArtcraftRouterError> {
match refs {
None => Ok(None),
Some(VideoListRef::Urls(urls)) if urls.is_empty() => Ok(None),
Some(VideoListRef::Urls(urls)) => Ok(Some(urls)),
Some(VideoListRef::MediaFileTokens(tokens)) if tokens.is_empty() => Ok(None),
Some(VideoListRef::MediaFileTokens(_)) => {
Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls))
}
}
}
fn plan_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
@@ -146,6 +267,7 @@ fn plan_resolution(
None => Ok(None),
Some(RouterResolution::SevenTwentyP) => Ok(Some(R::SevenTwentyP)),
Some(RouterResolution::TenEightyP) => Ok(Some(R::TenEightyP)),
Some(RouterResolution::FourK) => Ok(Some(R::FourK)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("resolution", &format!("{:?}", other)))
@@ -169,8 +291,43 @@ fn plan_duration(
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("duration_seconds", &format!("{}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanDuration::Eight)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanDuration::Four)),
// Nearest supported duration above/below, clamped to the 4s–8s range.
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(match other {
0..=4 => PlanDuration::Four,
5..=6 => PlanDuration::Six,
_ => PlanDuration::Eight,
})),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(match other {
0..=5 => PlanDuration::Four,
6..=7 => PlanDuration::Six,
_ => PlanDuration::Eight,
})),
},
}
}
/// Extend-video also supports fal's 7s default in addition to 4/6/8.
fn plan_extend_duration(
duration_seconds: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<Veo3p1FastExtendVideoDuration>, ArtcraftRouterError> {
use Veo3p1FastExtendVideoDuration as D;
match duration_seconds {
None => Ok(None),
Some(4) => Ok(Some(D::FourSeconds)),
Some(6) => Ok(Some(D::SixSeconds)),
Some(7) => Ok(Some(D::SevenSeconds)),
Some(8) => Ok(Some(D::EightSeconds)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("duration_seconds", &format!("{}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => {
Ok(Some(if other < 5 { D::FourSeconds } else if other == 5 { D::SixSeconds } else { D::EightSeconds }))
}
RequestMismatchMitigationStrategy::PayLessDowngrade => {
Ok(Some(if other <= 5 { D::FourSeconds } else { D::EightSeconds }))
}
},
}
}
@@ -182,129 +339,367 @@ fn unsupported(field: &'static str, value: &str) -> ArtcraftRouterError {
})
}
fn to_t2v_duration(d: PlanDuration) -> EnqueueVeo3p1FastTextToVideoDurationSeconds {
match d {
PlanDuration::Four => EnqueueVeo3p1FastTextToVideoDurationSeconds::Four,
PlanDuration::Six => EnqueueVeo3p1FastTextToVideoDurationSeconds::Six,
PlanDuration::Eight => EnqueueVeo3p1FastTextToVideoDurationSeconds::Eight,
}
}
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> EnqueueVeo3p1FastTextToVideoAspectRatio {
// The text-to-video endpoint has no `auto` aspect ratio; omit the field and
// let fal apply its own default (16:9).
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> Option<Veo3p1FastTextToVideoAspectRatio> {
match a {
PlanAspectRatio::Auto => EnqueueVeo3p1FastTextToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => EnqueueVeo3p1FastTextToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => EnqueueVeo3p1FastTextToVideoAspectRatio::NineBySixteen,
PlanAspectRatio::Auto => None,
PlanAspectRatio::SixteenByNine => Some(Veo3p1FastTextToVideoAspectRatio::SixteenByNine),
PlanAspectRatio::NineBySixteen => Some(Veo3p1FastTextToVideoAspectRatio::NineBySixteen),
}
}
fn to_t2v_resolution(r: PlanResolution) -> EnqueueVeo3p1FastTextToVideoResolution {
match r {
PlanResolution::SevenTwentyP => EnqueueVeo3p1FastTextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => EnqueueVeo3p1FastTextToVideoResolution::TenEightyP,
}
}
fn to_i2v_duration(d: PlanDuration) -> EnqueueVeo3p1FastImageToVideoDurationSeconds {
fn to_t2v_duration(d: PlanDuration) -> Veo3p1FastTextToVideoDuration {
match d {
PlanDuration::Four => EnqueueVeo3p1FastImageToVideoDurationSeconds::Four,
PlanDuration::Six => EnqueueVeo3p1FastImageToVideoDurationSeconds::Six,
PlanDuration::Eight => EnqueueVeo3p1FastImageToVideoDurationSeconds::Eight,
PlanDuration::Four => Veo3p1FastTextToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1FastTextToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1FastTextToVideoDuration::EightSeconds,
}
}
fn to_i2v_aspect_ratio(a: PlanAspectRatio) -> EnqueueVeo3p1FastImageToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => EnqueueVeo3p1FastImageToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => EnqueueVeo3p1FastImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => EnqueueVeo3p1FastImageToVideoAspectRatio::NineBySixteen,
}
}
fn to_i2v_resolution(r: PlanResolution) -> EnqueueVeo3p1FastImageToVideoResolution {
fn to_t2v_resolution(r: PlanResolution) -> Veo3p1FastTextToVideoResolution {
match r {
PlanResolution::SevenTwentyP => EnqueueVeo3p1FastImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => EnqueueVeo3p1FastImageToVideoResolution::TenEightyP,
PlanResolution::SevenTwentyP => Veo3p1FastTextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1FastTextToVideoResolution::TenEightyP,
PlanResolution::FourK => Veo3p1FastTextToVideoResolution::FourK,
}
}
fn to_flf_duration(d: PlanDuration) -> EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds {
fn to_i2v_aspect_ratio(a: PlanAspectRatio) -> Veo3p1FastImageToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => Veo3p1FastImageToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => Veo3p1FastImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Veo3p1FastImageToVideoAspectRatio::NineBySixteen,
}
}
fn to_i2v_duration(d: PlanDuration) -> Veo3p1FastImageToVideoDuration {
match d {
PlanDuration::Four => EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds::Four,
PlanDuration::Six => EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds::Six,
PlanDuration::Eight => EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds::Eight,
PlanDuration::Four => Veo3p1FastImageToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1FastImageToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1FastImageToVideoDuration::EightSeconds,
}
}
fn to_flf_aspect_ratio(a: PlanAspectRatio) -> EnqueueVeo3p1FastFirstLastFrameImageToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => EnqueueVeo3p1FastFirstLastFrameImageToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => EnqueueVeo3p1FastFirstLastFrameImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => EnqueueVeo3p1FastFirstLastFrameImageToVideoAspectRatio::NineBySixteen,
}
}
fn to_flf_resolution(r: PlanResolution) -> EnqueueVeo3p1FastFirstLastFrameImageToVideoResolution {
fn to_i2v_resolution(r: PlanResolution) -> Veo3p1FastImageToVideoResolution {
match r {
PlanResolution::SevenTwentyP => EnqueueVeo3p1FastFirstLastFrameImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => EnqueueVeo3p1FastFirstLastFrameImageToVideoResolution::TenEightyP,
PlanResolution::SevenTwentyP => Veo3p1FastImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1FastImageToVideoResolution::TenEightyP,
PlanResolution::FourK => Veo3p1FastImageToVideoResolution::FourK,
}
}
fn to_flf_aspect_ratio(a: PlanAspectRatio) -> Veo3p1FastFirstLastFrameToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => Veo3p1FastFirstLastFrameToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => Veo3p1FastFirstLastFrameToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Veo3p1FastFirstLastFrameToVideoAspectRatio::NineBySixteen,
}
}
fn to_flf_duration(d: PlanDuration) -> Veo3p1FastFirstLastFrameToVideoDuration {
match d {
PlanDuration::Four => Veo3p1FastFirstLastFrameToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1FastFirstLastFrameToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1FastFirstLastFrameToVideoDuration::EightSeconds,
}
}
fn to_flf_resolution(r: PlanResolution) -> Veo3p1FastFirstLastFrameToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3p1FastFirstLastFrameToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1FastFirstLastFrameToVideoResolution::TenEightyP,
PlanResolution::FourK => Veo3p1FastFirstLastFrameToVideoResolution::FourK,
}
}
// The reference-to-video endpoint has no `auto` aspect ratio; omit the field
// and let fal apply its own default (16:9).
fn to_reference_aspect_ratio(a: PlanAspectRatio) -> Option<Veo3p1FastReferenceToVideoAspectRatio> {
match a {
PlanAspectRatio::Auto => None,
PlanAspectRatio::SixteenByNine => Some(Veo3p1FastReferenceToVideoAspectRatio::SixteenByNine),
PlanAspectRatio::NineBySixteen => Some(Veo3p1FastReferenceToVideoAspectRatio::NineBySixteen),
}
}
fn to_reference_duration(d: PlanDuration) -> Veo3p1FastReferenceToVideoDuration {
match d {
PlanDuration::Four => Veo3p1FastReferenceToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1FastReferenceToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1FastReferenceToVideoDuration::EightSeconds,
}
}
fn to_reference_resolution(r: PlanResolution) -> Veo3p1FastReferenceToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3p1FastReferenceToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1FastReferenceToVideoResolution::TenEightyP,
PlanResolution::FourK => Veo3p1FastReferenceToVideoResolution::FourK,
}
}
fn to_extend_aspect_ratio(a: PlanAspectRatio) -> Veo3p1FastExtendVideoAspectRatio {
match a {
PlanAspectRatio::Auto => Veo3p1FastExtendVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => Veo3p1FastExtendVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Veo3p1FastExtendVideoAspectRatio::NineBySixteen,
}
}
// Extend-video has no 4k tier; the nearest supported resolution in either
// direction is 1080p.
fn to_extend_resolution(
r: PlanResolution,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Veo3p1FastExtendVideoResolution, ArtcraftRouterError> {
match r {
PlanResolution::SevenTwentyP => Ok(Veo3p1FastExtendVideoResolution::SevenTwentyP),
PlanResolution::TenEightyP => Ok(Veo3p1FastExtendVideoResolution::TenEightyP),
PlanResolution::FourK => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("resolution", "Veo 3.1 Fast extend-video does not support 4k"))
}
_ => Ok(Veo3p1FastExtendVideoResolution::TenEightyP),
},
}
}
#[cfg(test)]
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
use super::*;
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Fast,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
const START_FRAME_URL: &str = "https://example.com/a.png";
const END_FRAME_URL: &str = "https://example.com/b.png";
const REFERENCE_IMAGE_URL: &str = "https://example.com/ref.png";
const REFERENCE_VIDEO_URL: &str = "https://example.com/in.mp4";
mod mode_selection {
use super::*;
#[test]
fn no_frames_picks_t2v() {
assert!(matches!(unwrap_mode(base_builder()), FalVeo3p1FastMode::TextToVideo(_)));
}
#[test]
fn start_only_picks_i2v() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1FastMode::ImageToVideo(_)));
}
#[test]
fn start_and_end_picks_flf() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1FastMode::FirstLastFrameToVideo(_)));
}
#[test]
fn end_only_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
#[test]
fn reference_images_pick_reference_to_video() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL.to_string()]));
assert!(matches!(unwrap_mode(b), FalVeo3p1FastMode::ReferenceToVideo(_)));
}
#[test]
fn reference_videos_pick_extend_video() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
assert!(matches!(unwrap_mode(b), FalVeo3p1FastMode::ExtendVideo(_)));
}
#[test]
fn public_build_fn_wraps_request_enum() {
match build_fal_veo_3p1_fast(base_builder()).expect("build") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3p1Fast(s)) => {
assert!(matches!(s.mode, FalVeo3p1FastMode::TextToVideo(_)));
}
_ => panic!("expected Request(FalVeo3p1Fast)"),
}
}
}
fn unwrap_mode(b: GenerateVideoRequestBuilder) -> FalVeo3p1FastMode {
match build_fal_veo_3p1_fast(b).expect("build") {
VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3p1Fast(s)) => s.mode,
_ => panic!("expected FalVeo3p1Fast"),
mod extend_video {
use super::*;
#[test]
fn extend_maps_video_url_and_7s_duration() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.duration_seconds = Some(7);
match unwrap_mode(b) {
FalVeo3p1FastMode::ExtendVideo(r) => {
assert_eq!(r.video_url, REFERENCE_VIDEO_URL);
assert!(matches!(r.duration, Some(Veo3p1FastExtendVideoDuration::SevenSeconds)));
}
_ => panic!("expected extend"),
}
}
#[test]
fn extend_requires_exactly_one_video() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![
REFERENCE_VIDEO_URL.to_string(),
"https://example.com/other.mp4".to_string(),
]));
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
#[test]
fn extend_rejects_start_frame() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
#[test]
fn extend_rejects_end_frame() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
#[test]
fn extend_rejects_reference_images() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL.to_string()]));
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
#[test]
fn extend_4k_errors_with_error_out() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.resolution = Some(RouterResolution::FourK);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
#[test]
fn extend_4k_maps_to_1080p_with_mitigation() {
for strategy in [
RequestMismatchMitigationStrategy::PayMoreUpgrade,
RequestMismatchMitigationStrategy::PayLessDowngrade,
] {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
b.resolution = Some(RouterResolution::FourK);
b.request_mismatch_mitigation_strategy = strategy;
match unwrap_mode(b) {
FalVeo3p1FastMode::ExtendVideo(r) => {
assert!(matches!(r.resolution, Some(Veo3p1FastExtendVideoResolution::TenEightyP)));
}
_ => panic!("expected extend"),
}
}
}
}
#[test]
fn no_frames_picks_t2v() {
assert!(matches!(unwrap_mode(base_builder()), FalVeo3p1FastMode::TextToVideo(_)));
mod reference_to_video {
use super::*;
#[test]
fn reference_maps_image_urls() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL.to_string()]));
match unwrap_mode(b) {
FalVeo3p1FastMode::ReferenceToVideo(r) => {
assert_eq!(r.image_urls, vec![REFERENCE_IMAGE_URL]);
}
_ => panic!("expected reference"),
}
}
#[test]
fn reference_rejects_start_frame() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL.to_string()]));
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
#[test]
fn reference_rejects_end_frame() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL.to_string()]));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
}
#[test]
fn start_only_picks_i2v() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1FastMode::ImageToVideo(_)));
mod duration_conversions {
use super::*;
#[test]
fn duration_4s() {
let mut b = base_builder();
b.duration_seconds = Some(4);
match unwrap_mode(b) {
FalVeo3p1FastMode::TextToVideo(r) => {
assert!(matches!(r.duration, Some(Veo3p1FastTextToVideoDuration::FourSeconds)));
}
_ => panic!("expected t2v"),
}
}
#[test]
fn unsupported_duration_errors_with_error_out() {
let mut b = base_builder();
b.duration_seconds = Some(5);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_veo_3p1_fast_state(b).is_err());
}
}
#[test]
fn start_and_end_picks_flf() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1FastMode::FirstLastFrame(_)));
}
mod resolution_conversions {
use super::*;
#[test]
fn end_only_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
assert!(build_fal_veo_3p1_fast(b).is_err());
#[test]
fn t2v_720p() {
let mut b = base_builder();
b.resolution = Some(RouterResolution::SevenTwentyP);
match unwrap_mode(b) {
FalVeo3p1FastMode::TextToVideo(r) => {
assert!(matches!(r.resolution, Some(Veo3p1FastTextToVideoResolution::SevenTwentyP)));
}
_ => panic!("expected t2v"),
}
}
#[test]
fn t2v_4k_maps_to_four_k() {
let mut b = base_builder();
b.resolution = Some(RouterResolution::FourK);
match unwrap_mode(b) {
FalVeo3p1FastMode::TextToVideo(r) => {
assert!(matches!(r.resolution, Some(Veo3p1FastTextToVideoResolution::FourK)));
}
_ => panic!("expected t2v"),
}
}
}
#[test]
fn full_combinatorial_pass() {
let resolutions = [None, Some(RouterResolution::SevenTwentyP), Some(RouterResolution::TenEightyP)];
let resolutions = [None, Some(RouterResolution::SevenTwentyP), Some(RouterResolution::TenEightyP), Some(RouterResolution::FourK)];
let durations = [None, Some(4u16), Some(6), Some(8)];
let aspect_ratios = [None, Some(RouterAspectRatio::Auto), Some(RouterAspectRatio::WideSixteenByNine), Some(RouterAspectRatio::TallNineBySixteen)];
let audios = [None, Some(true), Some(false)];
@@ -321,18 +716,31 @@ mod tests {
b.aspect_ratio = aspect_ratio;
b.generate_audio = generate_audio;
if frames >= 1 {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
}
if frames == 2 {
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
}
assert!(build_fal_veo_3p1_fast(b).is_ok());
assert!(build_fal_veo_3p1_fast_state(b).is_ok());
combos += 1;
}
}
}
}
}
assert_eq!(combos, 3 * 4 * 4 * 3 * 3);
assert_eq!(combos, 4 * 4 * 4 * 3 * 3);
}
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Fast,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
fn unwrap_mode(b: GenerateVideoRequestBuilder) -> FalVeo3p1FastMode {
build_fal_veo_3p1_fast_state(b).expect("build").mode
}
}
@@ -1,36 +1,34 @@
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_fast_first_last_frame_image_to_video_webhook::EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds;
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_fast_image_to_video_webhook::EnqueueVeo3p1FastImageToVideoDurationSeconds;
use fal_client::requests_old::webhook::video::text::enqueue_veo_3p1_fast_text_to_video_webhook::EnqueueVeo3p1FastTextToVideoDurationSeconds;
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
use crate::generate::generate_video::providers::fal::veo_3p1_fast::request::{
FalVeo3p1FastMode, FalVeo3p1FastRequestState,
};
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
#[derive(Clone, Debug)]
pub struct FalVeo3p1FastCostState {
pub duration_seconds: u64,
pub generate_audio: bool,
pub cost_in_usd_cents: u64,
}
impl FalVeo3p1FastCostState {
pub fn from_request(request: &FalVeo3p1FastRequestState) -> Self {
let (duration_seconds, generate_audio) = match &request.mode {
FalVeo3p1FastMode::TextToVideo(req) => (t2v_duration_seconds(req.duration), req.generate_audio.unwrap_or(true)),
FalVeo3p1FastMode::ImageToVideo(req) => (i2v_duration_seconds(req.duration), req.generate_audio.unwrap_or(true)),
FalVeo3p1FastMode::FirstLastFrame(req) => (flf_duration_seconds(req.duration), req.generate_audio.unwrap_or(true)),
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalVeo3p1FastMode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1FastMode::ImageToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1FastMode::FirstLastFrameToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1FastMode::ReferenceToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1FastMode::ExtendVideo(req) => req.calculate_cost_in_cents(),
};
Self { duration_seconds, generate_audio }
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
// $0.10/sec audio off, $0.15/sec audio on (720p/1080p).
let per_second_cents: u64 = if self.generate_audio { 15 } else { 10 };
let cost_in_usd_cents = per_second_cents * self.duration_seconds;
VideoGenerationCostEstimate {
cost_in_credits: Some(cost_in_usd_cents),
cost_in_usd_cents: Some(cost_in_usd_cents),
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
@@ -40,90 +38,204 @@ impl FalVeo3p1FastCostState {
}
}
fn t2v_duration_seconds(d: Option<EnqueueVeo3p1FastTextToVideoDurationSeconds>) -> u64 {
match d {
Some(EnqueueVeo3p1FastTextToVideoDurationSeconds::Four) => 4,
Some(EnqueueVeo3p1FastTextToVideoDurationSeconds::Six) => 6,
Some(EnqueueVeo3p1FastTextToVideoDurationSeconds::Eight) | None => 8,
}
}
fn i2v_duration_seconds(d: Option<EnqueueVeo3p1FastImageToVideoDurationSeconds>) -> u64 {
match d {
Some(EnqueueVeo3p1FastImageToVideoDurationSeconds::Four) => 4,
Some(EnqueueVeo3p1FastImageToVideoDurationSeconds::Six) => 6,
Some(EnqueueVeo3p1FastImageToVideoDurationSeconds::Eight) | None => 8,
}
}
fn flf_duration_seconds(d: Option<EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds>) -> u64 {
match d {
Some(EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds::Four) => 4,
Some(EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds::Six) => 6,
Some(EnqueueVeo3p1FastFirstLastFrameImageToVideoDurationSeconds::Eight) | None => 8,
}
}
#[cfg(test)]
mod tests {
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_list_ref::ImageListRef;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::video_list_ref::VideoListRef;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_3p1_fast::build::build_fal_veo_3p1_fast_state;
fn cost_cents(duration_seconds: Option<u16>, generate_audio: Option<bool>, frames: u8) -> u64 {
use super::*;
// Veo 3.1 Fast pricing:
// 720p / 1080p: $0.10/sec (audio off), $0.15/sec (audio on)
// 4k: $0.30/sec (audio off), $0.35/sec (audio on)
// Extend-video has no 4k tier and defaults to 7s.
#[derive(Clone, Copy)]
enum Modality {
TextToVideo,
ImageToVideo,
FirstLastFrame,
Reference,
Extend,
}
mod text_to_video {
use super::*;
#[test]
fn t2v_4s_audio_on_is_60() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(4), None, Some(true)), 60);
}
#[test]
fn t2v_6s_audio_on_is_90() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(6), None, Some(true)), 90);
}
#[test]
fn t2v_8s_audio_on_is_120() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(8), None, Some(true)), 120);
}
#[test]
fn t2v_4s_audio_off_is_40() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(4), None, Some(false)), 40);
}
#[test]
fn t2v_8s_audio_off_is_80() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(8), None, Some(false)), 80);
}
#[test]
fn t2v_8s_audio_on_4k_is_280() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::FourK), Some(true)), 280);
}
#[test]
fn t2v_8s_audio_off_4k_is_240() {
assert_eq!(cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::FourK), Some(false)), 240);
}
#[test]
fn t2v_defaults_are_8s_720p_audio_on_120() {
// duration=None→8s, resolution=None→720p, audio=None→on.
assert_eq!(cost_cents(Modality::TextToVideo, None, None, None), 120);
}
#[test]
fn t2v_1080p_bills_same_as_720p() {
assert_eq!(
cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::TenEightyP), Some(true)),
cost_cents(Modality::TextToVideo, Some(8), Some(RouterResolution::SevenTwentyP), Some(true)),
);
}
}
mod image_to_video {
use super::*;
#[test]
fn i2v_6s_audio_on_is_90() {
assert_eq!(cost_cents(Modality::ImageToVideo, Some(6), None, Some(true)), 90);
}
#[test]
fn i2v_8s_audio_off_1080p_is_80() {
assert_eq!(cost_cents(Modality::ImageToVideo, Some(8), Some(RouterResolution::TenEightyP), Some(false)), 80);
}
}
mod first_last_frame_to_video {
use super::*;
#[test]
fn flf_6s_audio_on_is_90() {
assert_eq!(cost_cents(Modality::FirstLastFrame, Some(6), None, Some(true)), 90);
}
#[test]
fn flf_8s_audio_on_4k_is_280() {
assert_eq!(cost_cents(Modality::FirstLastFrame, Some(8), Some(RouterResolution::FourK), Some(true)), 280);
}
}
mod reference_to_video {
use super::*;
#[test]
fn reference_8s_audio_on_1080p_is_120() {
assert_eq!(cost_cents(Modality::Reference, Some(8), Some(RouterResolution::TenEightyP), Some(true)), 120);
}
#[test]
fn reference_4s_audio_off_720p_is_40() {
assert_eq!(cost_cents(Modality::Reference, Some(4), Some(RouterResolution::SevenTwentyP), Some(false)), 40);
}
}
mod extend_video {
use super::*;
#[test]
fn extend_7s_audio_on_is_105() {
assert_eq!(cost_cents(Modality::Extend, Some(7), None, Some(true)), 105);
}
#[test]
fn extend_7s_audio_off_is_70() {
assert_eq!(cost_cents(Modality::Extend, Some(7), None, Some(false)), 70);
}
#[test]
fn extend_8s_audio_on_is_120() {
assert_eq!(cost_cents(Modality::Extend, Some(8), None, Some(true)), 120);
}
#[test]
fn extend_defaults_are_7s_audio_on_105() {
// duration=None→7s (fal's extend default), audio=None→on.
assert_eq!(cost_cents(Modality::Extend, None, None, None), 105);
}
}
#[test]
fn t2v_i2v_flf_and_reference_price_identically() {
let expected = cost_cents(Modality::TextToVideo, Some(6), Some(RouterResolution::TenEightyP), Some(true));
for modality in [Modality::ImageToVideo, Modality::FirstLastFrame, Modality::Reference] {
assert_eq!(cost_cents(modality, Some(6), Some(RouterResolution::TenEightyP), Some(true)), expected);
}
}
#[test]
fn audio_costs_more_than_no_audio() {
assert!(
cost_cents(Modality::TextToVideo, Some(8), None, Some(false))
< cost_cents(Modality::TextToVideo, Some(8), None, Some(true))
);
}
fn cost_cents(
modality: Modality,
duration_seconds: Option<u16>,
resolution: Option<RouterResolution>,
generate_audio: Option<bool>,
) -> u64 {
let mut b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Fast,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
resolution,
generate_audio,
..Default::default()
};
if frames >= 1 {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
match modality {
Modality::TextToVideo => {}
Modality::ImageToVideo => {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
Modality::FirstLastFrame => {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
}
Modality::Reference => {
b.reference_images = Some(ImageListRef::Urls(vec!["https://example.com/ref.png".to_string()]));
}
Modality::Extend => {
b.reference_videos = Some(VideoListRef::Urls(vec!["https://example.com/in.mp4".to_string()]));
}
}
if frames == 2 {
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
}
b.build2().expect("build2").estimate_cost().expect("estimate_cost").cost_in_usd_cents.expect("cost")
}
mod numeric_literal_pricing {
use super::*;
#[test]
fn audio_on_4s_is_60() { assert_eq!(cost_cents(Some(4), Some(true), 0), 60); }
#[test]
fn audio_on_6s_is_90() { assert_eq!(cost_cents(Some(6), Some(true), 0), 90); }
#[test]
fn audio_on_8s_is_120() { assert_eq!(cost_cents(Some(8), Some(true), 0), 120); }
#[test]
fn audio_off_4s_is_40() { assert_eq!(cost_cents(Some(4), Some(false), 0), 40); }
#[test]
fn audio_off_8s_is_80() { assert_eq!(cost_cents(Some(8), Some(false), 0), 80); }
#[test]
fn no_duration_defaults_to_8s() {
assert_eq!(cost_cents(None, Some(true), 0), 120);
}
}
#[test]
fn all_three_modes_price_identically() {
let t2v = cost_cents(Some(6), Some(true), 0);
let i2v = cost_cents(Some(6), Some(true), 1);
let flf = cost_cents(Some(6), Some(true), 2);
assert_eq!(t2v, i2v);
assert_eq!(i2v, flf);
}
#[test]
fn audio_costs_more_than_no_audio() {
assert!(cost_cents(Some(8), Some(false), 0) < cost_cents(Some(8), Some(true), 0));
let state = build_fal_veo_3p1_fast_state(b).expect("build state");
FalVeo3p1FastCostState::from_request(&state)
.estimate_cost()
.cost_in_usd_cents
.expect("cost")
}
}
@@ -1,33 +1,26 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_fast_first_last_frame_image_to_video_webhook::{
enqueue_veo_3p1_fast_first_last_frame_image_to_video_webhook,
EnqueueVeo3p1FastFirstLastFrameImageToVideoArgs,
EnqueueVeo3p1FastFirstLastFrameImageToVideoRequest,
};
use fal_client::requests_old::webhook::video::image::enqueue_veo_3p1_fast_image_to_video_webhook::{
enqueue_veo_3p1_fast_image_to_video_webhook, EnqueueVeo3p1FastImageToVideoArgs,
EnqueueVeo3p1FastImageToVideoRequest,
};
use fal_client::requests_old::webhook::video::text::enqueue_veo_3p1_fast_text_to_video_webhook::{
enqueue_veo_3p1_fast_text_to_video_webhook, EnqueueVeo3p1FastTextToVideoArgs,
EnqueueVeo3p1FastTextToVideoRequest,
};
use fal_client::requests::api::video::extend::veo_3p1_fast::api::Veo3p1FastExtendVideoRequest;
use fal_client::requests::api::video::image::veo_3p1_fast::api::Veo3p1FastImageToVideoRequest;
use fal_client::requests::api::video::images::veo_3p1_fast::api::Veo3p1FastFirstLastFrameToVideoRequest;
use fal_client::requests::api::video::reference::veo_3p1_fast::api::Veo3p1FastReferenceToVideoRequest;
use fal_client::requests::api::video::text::veo_3p1_fast::api::Veo3p1FastTextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::errors::provider_error::ProviderError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
#[derive(Clone, Debug)]
pub enum FalVeo3p1FastMode {
TextToVideo(EnqueueVeo3p1FastTextToVideoRequest),
ImageToVideo(EnqueueVeo3p1FastImageToVideoRequest),
FirstLastFrame(EnqueueVeo3p1FastFirstLastFrameImageToVideoRequest),
TextToVideo(Veo3p1FastTextToVideoRequest),
ImageToVideo(Veo3p1FastImageToVideoRequest),
FirstLastFrameToVideo(Veo3p1FastFirstLastFrameToVideoRequest),
ReferenceToVideo(Veo3p1FastReferenceToVideoRequest),
ExtendVideo(Veo3p1FastExtendVideoRequest),
}
#[derive(Clone, Debug)]
@@ -37,65 +30,80 @@ pub struct FalVeo3p1FastRequestState {
impl FalVeo3p1FastRequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
let webhook_url = client.webhook_url.as_deref()
.ok_or(ArtcraftRouterError::Client(ClientError::WebhookUrlRequired))?;
let (webhook_response, outbound_request): (_, Arc<dyn Debug + Send + Sync>) = match &self.mode {
FalVeo3p1FastMode::TextToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = EnqueueVeo3p1FastTextToVideoArgs {
request: request.clone(),
webhook_url,
api_key: &client.api_key,
};
(enqueue_veo_3p1_fast_text_to_video_webhook(args).await, outbound)
}
FalVeo3p1FastMode::ImageToVideo(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = EnqueueVeo3p1FastImageToVideoArgs {
request: request.clone(),
webhook_url,
api_key: &client.api_key,
};
(enqueue_veo_3p1_fast_image_to_video_webhook(args).await, outbound)
}
FalVeo3p1FastMode::FirstLastFrame(request) => {
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let args = EnqueueVeo3p1FastFirstLastFrameImageToVideoArgs {
request: request.clone(),
webhook_url,
api_key: &client.api_key,
};
(enqueue_veo_3p1_fast_first_last_frame_image_to_video_webhook(args).await, outbound)
}
};
match &self.mode {
FalVeo3p1FastMode::TextToVideo(request) => send_request(request, client).await,
FalVeo3p1FastMode::ImageToVideo(request) => send_request(request, client).await,
FalVeo3p1FastMode::FirstLastFrameToVideo(request) => send_request(request, client).await,
FalVeo3p1FastMode::ReferenceToVideo(request) => send_request(request, client).await,
FalVeo3p1FastMode::ExtendVideo(request) => send_request(request, client).await,
}
}
}
let webhook_response = webhook_response
.map_err(|e| ArtcraftRouterError::Provider(ProviderError::Fal(e)))?;
// ── Helpers ──
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: webhook_response.request_id,
gateway_request_id: webhook_response.gateway_request_id,
maybe_status_url: None,
maybe_response_url: None,
maybe_outbound_request: Some(outbound_request),
}))
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
use test_data::web::video_urls::ANGRY_SHIBA_VIDEO_URL;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::image_list_ref::ImageListRef;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use crate::api::video_list_ref::VideoListRef;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::generate_video_response::GenerateVideoResponse;
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::test_helpers::get_fal_client;
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_text_to_video_4s() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("a candle flickering in the dark".to_string()),
@@ -106,7 +114,7 @@ mod tests {
}
#[tokio::test]
#[ignore]
#[ignore] // requires real API key, incurs cost
async fn live_image_to_video_6s() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the dog leaps into the lake.".to_string()),
@@ -117,6 +125,32 @@ mod tests {
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn live_reference_to_video_4s() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the dog runs across a sunlit meadow".to_string()),
reference_images: Some(ImageListRef::Urls(vec![JUNO_AT_LAKE_IMAGE_URL.to_string()])),
duration_seconds: Some(4),
generate_audio: Some(false),
..builder()
}).await;
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn live_extend_video_7s() {
let r = run_pipeline(GenerateVideoRequestBuilder {
prompt: Some("the scene continues naturally, keeping the same motion and style".to_string()),
reference_videos: Some(VideoListRef::Urls(vec![ANGRY_SHIBA_VIDEO_URL.to_string()])),
duration_seconds: Some(7),
generate_audio: Some(false),
..builder()
}).await;
assert!(matches!(r, GenerateVideoResponse::Fal(_)));
}
fn builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Fast,
@@ -0,0 +1,548 @@
use fal_client::requests::api::video::image::veo_3p1_lite::api::{
Veo3p1LiteImageToVideoAspectRatio, Veo3p1LiteImageToVideoDuration, Veo3p1LiteImageToVideoRequest,
Veo3p1LiteImageToVideoResolution,
};
use fal_client::requests::api::video::images::veo_3p1_lite::api::{
Veo3p1LiteFirstLastFrameToVideoAspectRatio, Veo3p1LiteFirstLastFrameToVideoDuration,
Veo3p1LiteFirstLastFrameToVideoRequest, Veo3p1LiteFirstLastFrameToVideoResolution,
};
use fal_client::requests::api::video::text::veo_3p1_lite::api::{
Veo3p1LiteTextToVideoAspectRatio, Veo3p1LiteTextToVideoDuration, Veo3p1LiteTextToVideoRequest,
Veo3p1LiteTextToVideoResolution,
};
use crate::api::image_list_ref::ImageListRef;
use crate::api::image_ref::ImageRef;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_resolution::RouterResolution;
use crate::api::video_list_ref::VideoListRef;
use crate::client::request_mismatch_mitigation_strategy::RequestMismatchMitigationStrategy;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_3p1_lite::request::{
FalVeo3p1LiteMode, FalVeo3p1LiteRequestState,
};
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanAspectRatio {
Auto,
SixteenByNine,
NineBySixteen,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanResolution {
SevenTwentyP,
TenEightyP,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanDuration {
Four,
Six,
Eight,
}
pub fn build_fal_veo_3p1_lite(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_fal_veo_3p1_lite_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalVeo3p1Lite(state)))
}
pub(crate) fn build_fal_veo_3p1_lite_state(
builder: GenerateVideoRequestBuilder,
) -> Result<FalVeo3p1LiteRequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
// Lite has no reference-to-video or extend-video endpoints on fal.
if has_reference_videos(&builder.reference_videos) {
return Err(unsupported(
"reference_videos",
"Veo 3.1 Lite does not support extend-video (reference videos)",
));
}
if has_reference_images(&builder.reference_images) {
return Err(unsupported(
"reference_images",
"Veo 3.1 Lite does not support reference-to-video (reference images)",
));
}
let start = optional_url(builder.start_frame.clone())?;
let end = optional_url(builder.end_frame.clone())?;
let aspect_ratio = plan_aspect_ratio(builder.aspect_ratio, strategy)?;
let resolution = plan_resolution(builder.resolution, strategy)?;
let duration = plan_duration(builder.duration_seconds, strategy)?;
let prompt = builder.prompt.clone().unwrap_or_default();
let negative_prompt = builder.negative_prompt.clone();
let generate_audio = builder.generate_audio;
let mode = match (start, end) {
(None, None) => FalVeo3p1LiteMode::TextToVideo(Veo3p1LiteTextToVideoRequest {
prompt,
aspect_ratio: aspect_ratio.and_then(to_t2v_aspect_ratio),
duration: duration.map(to_t2v_duration),
resolution: resolution.map(to_t2v_resolution),
negative_prompt,
generate_audio,
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
(Some(image_url), None) => FalVeo3p1LiteMode::ImageToVideo(Veo3p1LiteImageToVideoRequest {
prompt,
image_url,
aspect_ratio: aspect_ratio.map(to_i2v_aspect_ratio),
duration: duration.map(to_i2v_duration),
resolution: resolution.map(to_i2v_resolution),
generate_audio,
negative_prompt,
seed: None,
auto_fix: None,
safety_tolerance: None,
}),
(Some(first_frame_url), Some(last_frame_url)) => FalVeo3p1LiteMode::FirstLastFrameToVideo(
Veo3p1LiteFirstLastFrameToVideoRequest {
prompt,
first_frame_url,
last_frame_url,
aspect_ratio: aspect_ratio.map(to_flf_aspect_ratio),
duration: duration.map(to_flf_duration),
resolution: resolution.map(to_flf_resolution),
generate_audio,
negative_prompt,
seed: None,
auto_fix: None,
safety_tolerance: None,
},
),
(None, Some(_)) => {
return Err(unsupported(
"end_frame",
"Veo 3.1 Lite requires a start_frame when end_frame is provided",
));
}
};
Ok(FalVeo3p1LiteRequestState { mode })
}
fn optional_url(image_ref: Option<ImageRef>) -> Result<Option<String>, ArtcraftRouterError> {
match image_ref {
None => Ok(None),
Some(ImageRef::Url(url)) => Ok(Some(url)),
Some(ImageRef::MediaFileToken(_)) => {
Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls))
}
}
}
fn has_reference_images(refs: &Option<ImageListRef>) -> bool {
match refs {
None => false,
Some(ImageListRef::Urls(urls)) => !urls.is_empty(),
Some(ImageListRef::MediaFileTokens(tokens)) => !tokens.is_empty(),
}
}
fn has_reference_videos(refs: &Option<VideoListRef>) -> bool {
match refs {
None => false,
Some(VideoListRef::Urls(urls)) => !urls.is_empty(),
Some(VideoListRef::MediaFileTokens(tokens)) => !tokens.is_empty(),
}
}
fn plan_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<PlanAspectRatio>, ArtcraftRouterError> {
use PlanAspectRatio as Ar;
match aspect_ratio {
None => Ok(None),
Some(RouterAspectRatio::Auto)
| Some(RouterAspectRatio::Auto2k)
| Some(RouterAspectRatio::Auto4k) => Ok(Some(Ar::Auto)),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Some(Ar::SixteenByNine)),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Some(Ar::NineBySixteen)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("aspect_ratio", &format!("{:?}", other)))
}
_ => Ok(Some(Ar::Auto)),
},
}
}
fn plan_resolution(
resolution: Option<RouterResolution>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<PlanResolution>, ArtcraftRouterError> {
use PlanResolution as R;
match resolution {
None => Ok(None),
Some(RouterResolution::SevenTwentyP) => Ok(Some(R::SevenTwentyP)),
Some(RouterResolution::TenEightyP) => Ok(Some(R::TenEightyP)),
// Lite has no 4k tier; the nearest supported resolution in either
// direction is 1080p.
Some(RouterResolution::FourK) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("resolution", "Veo 3.1 Lite does not support 4k"))
}
_ => Ok(Some(R::TenEightyP)),
},
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("resolution", &format!("{:?}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(R::TenEightyP)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(R::SevenTwentyP)),
},
}
}
fn plan_duration(
duration_seconds: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<PlanDuration>, ArtcraftRouterError> {
match duration_seconds {
None => Ok(None),
Some(4) => Ok(Some(PlanDuration::Four)),
Some(6) => Ok(Some(PlanDuration::Six)),
Some(8) => Ok(Some(PlanDuration::Eight)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(unsupported("duration_seconds", &format!("{}", other)))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(match other {
0..=4 => PlanDuration::Four,
5..=6 => PlanDuration::Six,
_ => PlanDuration::Eight,
})),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(match other {
0..=5 => PlanDuration::Four,
6..=7 => PlanDuration::Six,
_ => PlanDuration::Eight,
})),
},
}
}
fn unsupported(field: &'static str, value: &str) -> ArtcraftRouterError {
ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field,
value: value.to_string(),
})
}
// The Lite text-to-video endpoint has no `auto` aspect ratio; omit the field
// and let fal apply its own default (16:9).
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> Option<Veo3p1LiteTextToVideoAspectRatio> {
match a {
PlanAspectRatio::Auto => None,
PlanAspectRatio::SixteenByNine => Some(Veo3p1LiteTextToVideoAspectRatio::SixteenByNine),
PlanAspectRatio::NineBySixteen => Some(Veo3p1LiteTextToVideoAspectRatio::NineBySixteen),
}
}
fn to_t2v_duration(d: PlanDuration) -> Veo3p1LiteTextToVideoDuration {
match d {
PlanDuration::Four => Veo3p1LiteTextToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1LiteTextToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1LiteTextToVideoDuration::EightSeconds,
}
}
fn to_t2v_resolution(r: PlanResolution) -> Veo3p1LiteTextToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3p1LiteTextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1LiteTextToVideoResolution::TenEightyP,
}
}
fn to_i2v_aspect_ratio(a: PlanAspectRatio) -> Veo3p1LiteImageToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => Veo3p1LiteImageToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => Veo3p1LiteImageToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Veo3p1LiteImageToVideoAspectRatio::NineBySixteen,
}
}
fn to_i2v_duration(d: PlanDuration) -> Veo3p1LiteImageToVideoDuration {
match d {
PlanDuration::Four => Veo3p1LiteImageToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1LiteImageToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1LiteImageToVideoDuration::EightSeconds,
}
}
fn to_i2v_resolution(r: PlanResolution) -> Veo3p1LiteImageToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3p1LiteImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1LiteImageToVideoResolution::TenEightyP,
}
}
fn to_flf_aspect_ratio(a: PlanAspectRatio) -> Veo3p1LiteFirstLastFrameToVideoAspectRatio {
match a {
PlanAspectRatio::Auto => Veo3p1LiteFirstLastFrameToVideoAspectRatio::Auto,
PlanAspectRatio::SixteenByNine => Veo3p1LiteFirstLastFrameToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => Veo3p1LiteFirstLastFrameToVideoAspectRatio::NineBySixteen,
}
}
fn to_flf_duration(d: PlanDuration) -> Veo3p1LiteFirstLastFrameToVideoDuration {
match d {
PlanDuration::Four => Veo3p1LiteFirstLastFrameToVideoDuration::FourSeconds,
PlanDuration::Six => Veo3p1LiteFirstLastFrameToVideoDuration::SixSeconds,
PlanDuration::Eight => Veo3p1LiteFirstLastFrameToVideoDuration::EightSeconds,
}
}
fn to_flf_resolution(r: PlanResolution) -> Veo3p1LiteFirstLastFrameToVideoResolution {
match r {
PlanResolution::SevenTwentyP => Veo3p1LiteFirstLastFrameToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => Veo3p1LiteFirstLastFrameToVideoResolution::TenEightyP,
}
}
#[cfg(test)]
mod tests {
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use super::*;
const START_FRAME_URL: &str = "https://example.com/a.png";
const END_FRAME_URL: &str = "https://example.com/b.png";
const REFERENCE_IMAGE_URL: &str = "https://example.com/ref.png";
const REFERENCE_VIDEO_URL: &str = "https://example.com/in.mp4";
mod mode_selection {
use super::*;
#[test]
fn no_frames_picks_t2v() {
assert!(matches!(unwrap_mode(base_builder()), FalVeo3p1LiteMode::TextToVideo(_)));
}
#[test]
fn start_only_picks_i2v() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1LiteMode::ImageToVideo(_)));
}
#[test]
fn start_and_end_picks_first_last_frame() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(matches!(unwrap_mode(b), FalVeo3p1LiteMode::FirstLastFrameToVideo(_)));
}
#[test]
fn end_only_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
assert!(build_fal_veo_3p1_lite_state(b).is_err());
}
}
mod unsupported_modalities {
use super::*;
#[test]
fn reference_images_are_rejected() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![REFERENCE_IMAGE_URL.to_string()]));
assert!(build_fal_veo_3p1_lite_state(b).is_err());
}
#[test]
fn reference_videos_are_rejected() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec![REFERENCE_VIDEO_URL.to_string()]));
assert!(build_fal_veo_3p1_lite_state(b).is_err());
}
#[test]
fn empty_reference_lists_are_ignored() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec![]));
b.reference_videos = Some(VideoListRef::Urls(vec![]));
assert!(matches!(unwrap_mode(b), FalVeo3p1LiteMode::TextToVideo(_)));
}
}
mod duration_conversions {
use super::*;
#[test]
fn duration_4s() {
let mut b = base_builder();
b.duration_seconds = Some(4);
match unwrap_mode(b) {
FalVeo3p1LiteMode::TextToVideo(r) => {
assert!(matches!(r.duration, Some(Veo3p1LiteTextToVideoDuration::FourSeconds)));
}
_ => panic!("expected t2v"),
}
}
#[test]
fn unsupported_duration_errors_with_error_out() {
let mut b = base_builder();
b.duration_seconds = Some(5);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_veo_3p1_lite_state(b).is_err());
}
#[test]
fn duration_5s_pay_more_upgrades_to_6() {
let mut b = base_builder();
b.duration_seconds = Some(5);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayMoreUpgrade;
match unwrap_mode(b) {
FalVeo3p1LiteMode::TextToVideo(r) => {
assert!(matches!(r.duration, Some(Veo3p1LiteTextToVideoDuration::SixSeconds)));
}
_ => panic!("expected t2v"),
}
}
#[test]
fn duration_5s_pay_less_downgrades_to_4() {
let mut b = base_builder();
b.duration_seconds = Some(5);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayLessDowngrade;
match unwrap_mode(b) {
FalVeo3p1LiteMode::TextToVideo(r) => {
assert!(matches!(r.duration, Some(Veo3p1LiteTextToVideoDuration::FourSeconds)));
}
_ => panic!("expected t2v"),
}
}
}
mod resolution_conversions {
use super::*;
#[test]
fn t2v_720p() {
let mut b = base_builder();
b.resolution = Some(RouterResolution::SevenTwentyP);
match unwrap_mode(b) {
FalVeo3p1LiteMode::TextToVideo(r) => {
assert!(matches!(r.resolution, Some(Veo3p1LiteTextToVideoResolution::SevenTwentyP)));
}
_ => panic!("expected t2v"),
}
}
#[test]
fn four_k_errors_with_error_out() {
let mut b = base_builder();
b.resolution = Some(RouterResolution::FourK);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_veo_3p1_lite_state(b).is_err());
}
#[test]
fn four_k_maps_to_1080p_with_mitigation() {
for strategy in [
RequestMismatchMitigationStrategy::PayMoreUpgrade,
RequestMismatchMitigationStrategy::PayLessDowngrade,
] {
let mut b = base_builder();
b.resolution = Some(RouterResolution::FourK);
b.request_mismatch_mitigation_strategy = strategy;
match unwrap_mode(b) {
FalVeo3p1LiteMode::TextToVideo(r) => {
assert!(matches!(r.resolution, Some(Veo3p1LiteTextToVideoResolution::TenEightyP)));
}
_ => panic!("expected t2v"),
}
}
}
}
#[test]
fn t2v_auto_aspect_ratio_is_omitted() {
// Lite t2v has no `auto` wire value; Auto must map to None.
let mut b = base_builder();
b.aspect_ratio = Some(RouterAspectRatio::Auto);
match unwrap_mode(b) {
FalVeo3p1LiteMode::TextToVideo(r) => assert!(r.aspect_ratio.is_none()),
_ => panic!("expected t2v"),
}
}
#[test]
fn i2v_auto_aspect_ratio_maps_to_auto() {
let mut b = base_builder();
b.aspect_ratio = Some(RouterAspectRatio::Auto);
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
match unwrap_mode(b) {
FalVeo3p1LiteMode::ImageToVideo(r) => {
assert!(matches!(r.aspect_ratio, Some(Veo3p1LiteImageToVideoAspectRatio::Auto)));
}
_ => panic!("expected i2v"),
}
}
#[test]
fn full_combinatorial_pass() {
let resolutions = [None, Some(RouterResolution::SevenTwentyP), Some(RouterResolution::TenEightyP)];
let durations = [None, Some(4u16), Some(6), Some(8)];
let aspect_ratios = [None, Some(RouterAspectRatio::Auto), Some(RouterAspectRatio::WideSixteenByNine), Some(RouterAspectRatio::TallNineBySixteen)];
let audios = [None, Some(true), Some(false)];
let mut combos = 0;
for &resolution in &resolutions {
for &duration in &durations {
for &aspect_ratio in &aspect_ratios {
for &generate_audio in &audios {
for frames in [0, 1, 2] {
let mut b = base_builder();
b.resolution = resolution;
b.duration_seconds = duration;
b.aspect_ratio = aspect_ratio;
b.generate_audio = generate_audio;
if frames >= 1 {
b.start_frame = Some(ImageRef::Url(START_FRAME_URL.to_string()));
}
if frames == 2 {
b.end_frame = Some(ImageRef::Url(END_FRAME_URL.to_string()));
}
assert!(build_fal_veo_3p1_lite_state(b).is_ok());
combos += 1;
}
}
}
}
}
assert_eq!(combos, 3 * 4 * 4 * 3 * 3);
}
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Lite,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
fn unwrap_mode(b: GenerateVideoRequestBuilder) -> FalVeo3p1LiteMode {
build_fal_veo_3p1_lite_state(b).expect("build").mode
}
}
@@ -0,0 +1,174 @@
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::providers::fal::veo_3p1_lite::request::{
FalVeo3p1LiteMode, FalVeo3p1LiteRequestState,
};
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
#[derive(Clone, Debug)]
pub struct FalVeo3p1LiteCostState {
pub cost_in_usd_cents: u64,
}
impl FalVeo3p1LiteCostState {
pub fn from_request(request: &FalVeo3p1LiteRequestState) -> Self {
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalVeo3p1LiteMode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1LiteMode::ImageToVideo(req) => req.calculate_cost_in_cents(),
FalVeo3p1LiteMode::FirstLastFrameToVideo(req) => req.calculate_cost_in_cents(),
};
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
VideoGenerationCostEstimate {
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
has_watermark: false,
failures_are_refunded: None,
}
}
}
#[cfg(test)]
mod tests {
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_3p1_lite::build::build_fal_veo_3p1_lite_state;
use super::*;
// Veo 3.1 Lite pricing (both resolution and audio move the rate; no 4k):
// 720p: $0.03/sec (audio off), $0.05/sec (audio on)
// 1080p: $0.05/sec (audio off), $0.08/sec (audio on)
mod text_to_video {
use super::*;
#[test]
fn t2v_8s_audio_on_720p_is_40() {
assert_eq!(cost_cents(Some(8), Some(RouterResolution::SevenTwentyP), Some(true), 0), 40);
}
#[test]
fn t2v_8s_audio_off_720p_is_24() {
assert_eq!(cost_cents(Some(8), Some(RouterResolution::SevenTwentyP), Some(false), 0), 24);
}
#[test]
fn t2v_8s_audio_on_1080p_is_64() {
assert_eq!(cost_cents(Some(8), Some(RouterResolution::TenEightyP), Some(true), 0), 64);
}
#[test]
fn t2v_8s_audio_off_1080p_is_40() {
assert_eq!(cost_cents(Some(8), Some(RouterResolution::TenEightyP), Some(false), 0), 40);
}
#[test]
fn t2v_4s_audio_on_720p_is_20() {
assert_eq!(cost_cents(Some(4), Some(RouterResolution::SevenTwentyP), Some(true), 0), 20);
}
#[test]
fn t2v_6s_audio_on_720p_is_30() {
assert_eq!(cost_cents(Some(6), Some(RouterResolution::SevenTwentyP), Some(true), 0), 30);
}
#[test]
fn t2v_defaults_are_8s_720p_audio_on_40() {
// duration=None→8s, resolution=None→720p, audio=None→on.
assert_eq!(cost_cents(None, None, None, 0), 40);
}
}
mod image_to_video {
use super::*;
#[test]
fn i2v_8s_audio_off_720p_is_24() {
assert_eq!(cost_cents(Some(8), Some(RouterResolution::SevenTwentyP), Some(false), 1), 24);
}
#[test]
fn i2v_6s_audio_on_720p_is_30() {
assert_eq!(cost_cents(Some(6), Some(RouterResolution::SevenTwentyP), Some(true), 1), 30);
}
}
mod first_last_frame_to_video {
use super::*;
#[test]
fn flf_8s_audio_on_1080p_is_64() {
assert_eq!(cost_cents(Some(8), Some(RouterResolution::TenEightyP), Some(true), 2), 64);
}
#[test]
fn flf_4s_audio_off_720p_is_12() {
assert_eq!(cost_cents(Some(4), Some(RouterResolution::SevenTwentyP), Some(false), 2), 12);
}
}
#[test]
fn all_three_modes_price_identically() {
let t2v = cost_cents(Some(6), Some(RouterResolution::TenEightyP), Some(true), 0);
let i2v = cost_cents(Some(6), Some(RouterResolution::TenEightyP), Some(true), 1);
let flf = cost_cents(Some(6), Some(RouterResolution::TenEightyP), Some(true), 2);
assert_eq!(t2v, i2v);
assert_eq!(i2v, flf);
}
#[test]
fn audio_costs_more_than_no_audio() {
assert!(
cost_cents(Some(8), Some(RouterResolution::SevenTwentyP), Some(false), 0)
< cost_cents(Some(8), Some(RouterResolution::SevenTwentyP), Some(true), 0)
);
}
#[test]
fn ten_eighty_costs_more_than_seven_twenty() {
assert!(
cost_cents(Some(8), Some(RouterResolution::SevenTwentyP), Some(true), 0)
< cost_cents(Some(8), Some(RouterResolution::TenEightyP), Some(true), 0)
);
}
fn cost_cents(
duration_seconds: Option<u16>,
resolution: Option<RouterResolution>,
generate_audio: Option<bool>,
frames: u8,
) -> u64 {
let mut b = GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Lite,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
resolution,
generate_audio,
..Default::default()
};
if frames >= 1 {
b.start_frame = Some(ImageRef::Url("https://example.com/a.png".to_string()));
}
if frames == 2 {
b.end_frame = Some(ImageRef::Url("https://example.com/b.png".to_string()));
}
let state = build_fal_veo_3p1_lite_state(b).expect("build state");
FalVeo3p1LiteCostState::from_request(&state)
.estimate_cost()
.cost_in_usd_cents
.expect("cost")
}
}
@@ -0,0 +1,3 @@
pub mod build;
pub mod cost;
pub mod request;
@@ -0,0 +1,166 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests::api::video::image::veo_3p1_lite::api::Veo3p1LiteImageToVideoRequest;
use fal_client::requests::api::video::images::veo_3p1_lite::api::Veo3p1LiteFirstLastFrameToVideoRequest;
use fal_client::requests::api::video::text::veo_3p1_lite::api::Veo3p1LiteTextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
#[derive(Clone, Debug)]
pub enum FalVeo3p1LiteMode {
TextToVideo(Veo3p1LiteTextToVideoRequest),
ImageToVideo(Veo3p1LiteImageToVideoRequest),
FirstLastFrameToVideo(Veo3p1LiteFirstLastFrameToVideoRequest),
}
#[derive(Clone, Debug)]
pub struct FalVeo3p1LiteRequestState {
pub mode: FalVeo3p1LiteMode,
}
impl FalVeo3p1LiteRequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
match &self.mode {
FalVeo3p1LiteMode::TextToVideo(request) => send_request(request, client).await,
FalVeo3p1LiteMode::ImageToVideo(request) => send_request(request, client).await,
FalVeo3p1LiteMode::FirstLastFrameToVideo(request) => send_request(request, client).await,
}
}
}
// ── Helpers ──
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use fal_client::creds::fal_api_key::FalApiKey;
use test_data::web::image_urls::{JUNO_AT_LAKE_IMAGE_URL, TALL_MOCHI_WITH_GLASSES_IMAGE_URL};
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::veo_3p1_lite::build::build_fal_veo_3p1_lite_state;
use super::*;
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn live_text_to_video_4s() {
let state = build_state(GenerateVideoRequestBuilder {
prompt: Some("a candle flickering in the dark".to_string()),
duration_seconds: Some(4),
generate_audio: Some(false),
..builder()
});
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn live_image_to_video_4s() {
let state = build_state(GenerateVideoRequestBuilder {
prompt: Some("the dog leaps into the lake.".to_string()),
start_frame: Some(ImageRef::Url(JUNO_AT_LAKE_IMAGE_URL.to_string())),
duration_seconds: Some(4),
generate_audio: Some(false),
..builder()
});
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn live_first_last_frame_4s() {
let state = build_state(GenerateVideoRequestBuilder {
prompt: Some("a smooth transition between scenes".to_string()),
start_frame: Some(ImageRef::Url(TALL_MOCHI_WITH_GLASSES_IMAGE_URL.to_string())),
end_frame: Some(ImageRef::Url(JUNO_AT_LAKE_IMAGE_URL.to_string())),
duration_seconds: Some(4),
generate_audio: Some(false),
..builder()
});
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
fn builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::Veo3p1Lite,
provider: RouterProvider::Fal,
..Default::default()
}
}
fn build_state(b: GenerateVideoRequestBuilder) -> FalVeo3p1LiteRequestState {
build_fal_veo_3p1_lite_state(b).expect("build state")
}
fn read_fal_api_key() -> FalApiKey {
let secret = std::fs::read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")
.expect("Failed to read fal_api_key.txt");
FalApiKey::from_str(secret.trim())
}
fn client_with_webhook() -> RouterFalClient {
RouterFalClient::new_with_webhook(
read_fal_api_key(),
"https://example.com/fal-webhook-test".to_string(),
)
}
}
@@ -0,0 +1,514 @@
use fal_client::requests::api::video::image::vidu_q3::api::{
ViduQ3ImageToVideoRequest, ViduQ3ImageToVideoResolution,
};
use fal_client::requests::api::video::reference::vidu_q3_reference_to_video::api::{
ViduQ3ReferenceToVideoAspectRatio, ViduQ3ReferenceToVideoRequest, ViduQ3ReferenceToVideoResolution,
};
use fal_client::requests::api::video::reference::vidu_q3_reference_to_video_mix::api::{
ViduQ3ReferenceToVideoMixAspectRatio, ViduQ3ReferenceToVideoMixRequest,
ViduQ3ReferenceToVideoMixResolution,
};
use fal_client::requests::api::video::text::vidu_q3::api::{
ViduQ3TextToVideoAspectRatio, ViduQ3TextToVideoRequest, ViduQ3TextToVideoResolution,
};
use crate::api::image_list_ref::ImageListRef;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_resolution::RouterResolution;
use crate::api::video_list_ref::VideoListRef;
use crate::client::request_mismatch_mitigation_strategy::RequestMismatchMitigationStrategy;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::kling_1_6_pro::build::optional_url;
use crate::generate::generate_video::providers::fal::vidu_q3::request::{
FalViduQ3Mode, FalViduQ3RequestState,
};
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
const MAX_REFERENCE_IMAGES: usize = 4;
const MIN_DURATION_SECONDS: u16 = 1;
const MAX_DURATION_SECONDS: u16 = 16;
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanAspectRatio {
SixteenByNine,
NineBySixteen,
FourByThree,
ThreeByFour,
Square,
}
/// Vidu also offers 360p, but no `RouterResolution` maps to it (fal bills 360p
/// and 540p identically, so 540p is always the better pick).
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanResolution {
FiveFortyP,
SevenTwentyP,
TenEightyP,
}
pub fn build_fal_vidu_q3(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_fal_vidu_q3_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalViduQ3(state)))
}
pub(crate) fn build_fal_vidu_q3_state(
builder: GenerateVideoRequestBuilder,
) -> Result<FalViduQ3RequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
reject_reference_videos(&builder.reference_videos)?;
let aspect_ratio = plan_aspect_ratio(builder.aspect_ratio, strategy)?;
let resolution = plan_resolution(builder.resolution, strategy)?;
let duration = plan_duration(builder.duration_seconds, strategy)?;
let prompt = builder.prompt.clone().unwrap_or_default();
let audio = builder.generate_audio;
let reference_image_urls = resolve_reference_image_urls(builder.reference_images.clone())?;
let start_image_url = optional_url(builder.start_frame.clone())?;
let end_image_url = optional_url(builder.end_frame.clone())?;
// Dispatch: reference_images → reference-to-video; start_frame → image; else → text.
let mode = if !reference_image_urls.is_empty() {
if start_image_url.is_some() || end_image_url.is_some() {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "reference_images",
value: "Vidu Q3 reference-to-video cannot also accept a start_frame or end_frame".to_string(),
}));
}
if reference_image_urls.len() > MAX_REFERENCE_IMAGES {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "reference_images",
value: format!(
"Vidu Q3 supports at most {} reference images, got {}",
MAX_REFERENCE_IMAGES,
reference_image_urls.len(),
),
}));
}
// The plain reference endpoint keeps a single subject consistent; the /mix
// variant blends multiple subjects, so 2+ references route there.
if reference_image_urls.len() == 1 {
FalViduQ3Mode::ReferenceToVideo(ViduQ3ReferenceToVideoRequest {
prompt,
reference_image_urls,
duration,
seed: None,
aspect_ratio: aspect_ratio.map(to_reference_aspect_ratio),
resolution: resolution.map(to_reference_resolution),
audio,
})
} else {
FalViduQ3Mode::ReferenceToVideoMix(ViduQ3ReferenceToVideoMixRequest {
prompt,
reference_image_urls,
duration,
seed: None,
aspect_ratio: aspect_ratio.map(to_mix_aspect_ratio),
resolution: resolution.map(to_mix_resolution),
audio,
})
}
} else if let Some(image_url) = start_image_url {
let i2v_resolution = resolution.map(to_i2v_resolution);
check_i2v_end_frame_resolution(i2v_resolution, end_image_url.is_some())?;
// Image-to-video has no aspect_ratio input — the output follows the start
// frame — so any requested aspect ratio is silently dropped here.
FalViduQ3Mode::ImageToVideo(ViduQ3ImageToVideoRequest {
prompt,
image_url,
end_image_url,
duration,
seed: None,
resolution: i2v_resolution,
audio,
})
} else {
if end_image_url.is_some() {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "end_frame",
value: "Vidu Q3 requires a start_frame when end_frame is provided".to_string(),
}));
}
FalViduQ3Mode::TextToVideo(ViduQ3TextToVideoRequest {
prompt,
duration,
seed: None,
aspect_ratio: aspect_ratio.map(to_t2v_aspect_ratio),
resolution: resolution.map(to_t2v_resolution),
audio,
})
};
Ok(FalViduQ3RequestState { mode })
}
// ── Input helpers ──
fn reject_reference_videos(
reference_videos: &Option<VideoListRef>,
) -> Result<(), ArtcraftRouterError> {
let has_reference_videos = match reference_videos {
None => false,
Some(VideoListRef::Urls(urls)) => !urls.is_empty(),
Some(VideoListRef::MediaFileTokens(tokens)) => !tokens.is_empty(),
};
if has_reference_videos {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "reference_videos",
value: "Vidu Q3 does not support video references".to_string(),
}));
}
Ok(())
}
fn resolve_reference_image_urls(
reference_images: Option<ImageListRef>,
) -> Result<Vec<String>, ArtcraftRouterError> {
match reference_images {
None => Ok(vec![]),
Some(ImageListRef::Urls(urls)) => Ok(urls),
Some(ImageListRef::MediaFileTokens(_)) => {
Err(ArtcraftRouterError::Client(ClientError::FalOnlySupportsUrls))
}
}
}
/// fal rejects 360p image-to-video requests that include an end frame.
/// `plan_resolution` can't currently produce 360p, but guard anyway so a
/// future mapping change can't silently ship the rejected combination.
fn check_i2v_end_frame_resolution(
resolution: Option<ViduQ3ImageToVideoResolution>,
has_end_frame: bool,
) -> Result<(), ArtcraftRouterError> {
if has_end_frame && matches!(resolution, Some(ViduQ3ImageToVideoResolution::ThreeSixtyP)) {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "resolution",
value: "Vidu Q3 image-to-video does not support 360p when an end_frame is provided".to_string(),
}));
}
Ok(())
}
// ── Plan helpers ──
fn plan_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<PlanAspectRatio>, ArtcraftRouterError> {
use PlanAspectRatio as Ar;
match aspect_ratio {
None => Ok(None),
Some(RouterAspectRatio::Square) | Some(RouterAspectRatio::SquareHd) => Ok(Some(Ar::Square)),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Some(Ar::SixteenByNine)),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Some(Ar::NineBySixteen)),
Some(RouterAspectRatio::WideFourByThree) => Ok(Some(Ar::FourByThree)),
Some(RouterAspectRatio::TallThreeByFour) => Ok(Some(Ar::ThreeByFour)),
Some(RouterAspectRatio::Auto)
| Some(RouterAspectRatio::Auto2k)
| Some(RouterAspectRatio::Auto3k)
| Some(RouterAspectRatio::Auto4k) => Ok(Some(Ar::SixteenByNine)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "aspect_ratio",
value: format!("{:?}", other),
}))
}
_ => Ok(Some(Ar::SixteenByNine)),
},
}
}
fn plan_resolution(
resolution: Option<RouterResolution>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<PlanResolution>, ArtcraftRouterError> {
match resolution {
None => Ok(None),
// Vidu doesn't offer 480p; 540p is the nearest supported size (and bills
// at the same per-second rate as 360p, so nothing cheaper is lost).
Some(RouterResolution::FourEightyP) => Ok(Some(PlanResolution::FiveFortyP)),
Some(RouterResolution::SevenTwentyP) => Ok(Some(PlanResolution::SevenTwentyP)),
Some(RouterResolution::TenEightyP) => Ok(Some(PlanResolution::TenEightyP)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "resolution",
value: format!("{:?}", other),
}))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanResolution::TenEightyP)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanResolution::FiveFortyP)),
},
}
}
fn plan_duration(
duration_seconds: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<u8>, ArtcraftRouterError> {
match duration_seconds {
None => Ok(None),
Some(seconds) if (MIN_DURATION_SECONDS..=MAX_DURATION_SECONDS).contains(&seconds) => {
Ok(Some(seconds as u8))
}
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "duration_seconds",
value: format!("{}", other),
}))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade
| RequestMismatchMitigationStrategy::PayLessDowngrade => {
Ok(Some(other.clamp(MIN_DURATION_SECONDS, MAX_DURATION_SECONDS) as u8))
}
},
}
}
// ── Leaf converters ──
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> ViduQ3TextToVideoAspectRatio {
match a {
PlanAspectRatio::SixteenByNine => ViduQ3TextToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => ViduQ3TextToVideoAspectRatio::NineBySixteen,
PlanAspectRatio::FourByThree => ViduQ3TextToVideoAspectRatio::FourByThree,
PlanAspectRatio::ThreeByFour => ViduQ3TextToVideoAspectRatio::ThreeByFour,
PlanAspectRatio::Square => ViduQ3TextToVideoAspectRatio::Square,
}
}
fn to_reference_aspect_ratio(a: PlanAspectRatio) -> ViduQ3ReferenceToVideoAspectRatio {
match a {
PlanAspectRatio::SixteenByNine => ViduQ3ReferenceToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => ViduQ3ReferenceToVideoAspectRatio::NineBySixteen,
PlanAspectRatio::FourByThree => ViduQ3ReferenceToVideoAspectRatio::FourByThree,
PlanAspectRatio::ThreeByFour => ViduQ3ReferenceToVideoAspectRatio::ThreeByFour,
PlanAspectRatio::Square => ViduQ3ReferenceToVideoAspectRatio::Square,
}
}
fn to_mix_aspect_ratio(a: PlanAspectRatio) -> ViduQ3ReferenceToVideoMixAspectRatio {
match a {
PlanAspectRatio::SixteenByNine => ViduQ3ReferenceToVideoMixAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => ViduQ3ReferenceToVideoMixAspectRatio::NineBySixteen,
PlanAspectRatio::FourByThree => ViduQ3ReferenceToVideoMixAspectRatio::FourByThree,
PlanAspectRatio::ThreeByFour => ViduQ3ReferenceToVideoMixAspectRatio::ThreeByFour,
PlanAspectRatio::Square => ViduQ3ReferenceToVideoMixAspectRatio::Square,
}
}
fn to_t2v_resolution(r: PlanResolution) -> ViduQ3TextToVideoResolution {
match r {
PlanResolution::FiveFortyP => ViduQ3TextToVideoResolution::FiveFortyP,
PlanResolution::SevenTwentyP => ViduQ3TextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => ViduQ3TextToVideoResolution::TenEightyP,
}
}
fn to_i2v_resolution(r: PlanResolution) -> ViduQ3ImageToVideoResolution {
match r {
PlanResolution::FiveFortyP => ViduQ3ImageToVideoResolution::FiveFortyP,
PlanResolution::SevenTwentyP => ViduQ3ImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => ViduQ3ImageToVideoResolution::TenEightyP,
}
}
fn to_reference_resolution(r: PlanResolution) -> ViduQ3ReferenceToVideoResolution {
match r {
PlanResolution::FiveFortyP => ViduQ3ReferenceToVideoResolution::FiveFortyP,
PlanResolution::SevenTwentyP => ViduQ3ReferenceToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => ViduQ3ReferenceToVideoResolution::TenEightyP,
}
}
fn to_mix_resolution(r: PlanResolution) -> ViduQ3ReferenceToVideoMixResolution {
match r {
PlanResolution::FiveFortyP => ViduQ3ReferenceToVideoMixResolution::FiveFortyP,
PlanResolution::SevenTwentyP => ViduQ3ReferenceToVideoMixResolution::SevenTwentyP,
PlanResolution::TenEightyP => ViduQ3ReferenceToVideoMixResolution::TenEightyP,
}
}
#[cfg(test)]
mod tests {
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use super::*;
const START_URL: &str = "https://example.com/start.png";
const END_URL: &str = "https://example.com/end.png";
mod dispatch_tests {
use super::*;
#[test]
fn no_inputs_picks_t2v() {
let state = build_fal_vidu_q3_state(base_builder()).expect("build");
assert!(matches!(state.mode, FalViduQ3Mode::TextToVideo(_)));
}
#[test]
fn start_frame_picks_i2v() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_URL.to_string()));
let state = build_fal_vidu_q3_state(b).expect("build");
assert!(matches!(state.mode, FalViduQ3Mode::ImageToVideo(_)));
}
#[test]
fn start_and_end_frame_picks_i2v_with_end_image_url() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_URL.to_string()));
b.end_frame = Some(ImageRef::Url(END_URL.to_string()));
let state = build_fal_vidu_q3_state(b).expect("build");
let FalViduQ3Mode::ImageToVideo(req) = state.mode else {
panic!("expected ImageToVideo");
};
assert_eq!(req.image_url, START_URL);
assert_eq!(req.end_image_url.as_deref(), Some(END_URL));
}
#[test]
fn end_frame_without_start_frame_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url(END_URL.to_string()));
assert!(build_fal_vidu_q3_state(b).is_err());
}
#[test]
fn one_reference_image_picks_reference_to_video() {
let state = build_fal_vidu_q3_state(builder_with_references(1)).expect("build");
assert!(matches!(state.mode, FalViduQ3Mode::ReferenceToVideo(_)));
}
#[test]
fn three_reference_images_pick_reference_to_video_mix() {
let state = build_fal_vidu_q3_state(builder_with_references(3)).expect("build");
let FalViduQ3Mode::ReferenceToVideoMix(req) = state.mode else {
panic!("expected ReferenceToVideoMix");
};
assert_eq!(req.reference_image_urls.len(), 3);
}
#[test]
fn five_reference_images_error() {
assert!(build_fal_vidu_q3_state(builder_with_references(5)).is_err());
}
#[test]
fn reference_images_with_start_frame_error() {
let mut b = builder_with_references(2);
b.start_frame = Some(ImageRef::Url(START_URL.to_string()));
assert!(build_fal_vidu_q3_state(b).is_err());
}
#[test]
fn reference_videos_error() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec!["https://example.com/a.mp4".to_string()]));
assert!(build_fal_vidu_q3_state(b).is_err());
}
}
mod plan_tests {
use super::*;
#[test]
fn duration_20_errors_under_error_out() {
let mut b = base_builder();
b.duration_seconds = Some(20);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_vidu_q3_state(b).is_err());
}
#[test]
fn duration_20_clamps_to_16_under_pay_less() {
let mut b = base_builder();
b.duration_seconds = Some(20);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayLessDowngrade;
let state = build_fal_vidu_q3_state(b).expect("build");
let FalViduQ3Mode::TextToVideo(req) = state.mode else {
panic!("expected TextToVideo");
};
assert_eq!(req.duration, Some(16));
}
#[test]
fn resolution_480p_maps_to_540p() {
let mut b = base_builder();
b.resolution = Some(RouterResolution::FourEightyP);
let state = build_fal_vidu_q3_state(b).expect("build");
let FalViduQ3Mode::TextToVideo(req) = state.mode else {
panic!("expected TextToVideo");
};
assert_eq!(req.resolution, Some(ViduQ3TextToVideoResolution::FiveFortyP));
}
#[test]
fn i2v_aspect_ratio_is_silently_dropped() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_URL.to_string()));
b.aspect_ratio = Some(RouterAspectRatio::TallNineBySixteen);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
// Image-to-video has no aspect_ratio field; the request still builds.
let state = build_fal_vidu_q3_state(b).expect("build");
assert!(matches!(state.mode, FalViduQ3Mode::ImageToVideo(_)));
}
#[test]
fn i2v_360p_with_end_frame_is_rejected_by_guard() {
// 360p is unreachable from RouterResolution today, so exercise the
// guard directly against the fal-level resolution enum.
let result = check_i2v_end_frame_resolution(
Some(ViduQ3ImageToVideoResolution::ThreeSixtyP),
true,
);
assert!(result.is_err());
assert!(check_i2v_end_frame_resolution(Some(ViduQ3ImageToVideoResolution::ThreeSixtyP), false).is_ok());
assert!(check_i2v_end_frame_resolution(Some(ViduQ3ImageToVideoResolution::SevenTwentyP), true).is_ok());
}
#[test]
fn generate_audio_propagates() {
let mut b = base_builder();
b.generate_audio = Some(false);
let state = build_fal_vidu_q3_state(b).expect("build");
let FalViduQ3Mode::TextToVideo(req) = state.mode else {
panic!("expected TextToVideo");
};
assert_eq!(req.audio, Some(false));
}
}
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::ViduQ3,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
fn builder_with_references(count: usize) -> GenerateVideoRequestBuilder {
let urls = (0..count)
.map(|i| format!("https://example.com/ref-{}.png", i))
.collect();
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(urls));
b
}
}
@@ -0,0 +1,141 @@
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::providers::fal::vidu_q3::request::{
FalViduQ3Mode, FalViduQ3RequestState,
};
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
#[derive(Clone, Debug)]
pub struct FalViduQ3CostState {
pub cost_in_usd_cents: u64,
}
impl FalViduQ3CostState {
pub fn from_request(request: &FalViduQ3RequestState) -> Self {
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalViduQ3Mode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalViduQ3Mode::ImageToVideo(req) => req.calculate_cost_in_cents(),
FalViduQ3Mode::ReferenceToVideo(req) => req.calculate_cost_in_cents(),
FalViduQ3Mode::ReferenceToVideoMix(req) => req.calculate_cost_in_cents(),
};
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
VideoGenerationCostEstimate {
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
has_watermark: false,
failures_are_refunded: None,
}
}
}
#[cfg(test)]
mod tests {
use crate::api::image_list_ref::ImageListRef;
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::vidu_q3::build::build_fal_vidu_q3_state;
use super::*;
// Pricing (from fal_client's vidu_q3 cost modules):
// 360p/540p: $0.07/sec → 5s = 35¢
// 720p/1080p: $0.154/sec → 5s = 77¢, 10s = 154¢ (ceil to whole cents)
// fal defaults when unset: duration = 5s, resolution = 720p.
// All four modes (text/image/reference/mix) bill identically.
//
// NB: `build2()` isn't wired up for Vidu Q3 yet, so these tests go through
// `build_fal_vidu_q3_state()` directly.
#[test]
fn t2v_default_settings_is_77() {
assert_eq!(cost_cents(base_builder(None, None)), 77);
}
#[test]
fn t2v_5s_720p_is_77() {
assert_eq!(cost_cents(base_builder(Some(5), Some(RouterResolution::SevenTwentyP))), 77);
}
#[test]
fn t2v_5s_540p_is_35() {
// RouterResolution::FourEightyP maps to Vidu's 540p.
assert_eq!(cost_cents(base_builder(Some(5), Some(RouterResolution::FourEightyP))), 35);
}
#[test]
fn t2v_10s_1080p_is_154() {
assert_eq!(cost_cents(base_builder(Some(10), Some(RouterResolution::TenEightyP))), 154);
}
#[test]
fn t2v_3s_720p_rounds_up_to_47() {
// 3 × 15.4¢ = 46.2¢ → 47¢.
assert_eq!(cost_cents(base_builder(Some(3), Some(RouterResolution::SevenTwentyP))), 47);
}
#[test]
fn i2v_5s_720p_is_77() {
let mut b = base_builder(Some(5), Some(RouterResolution::SevenTwentyP));
b.start_frame = Some(ImageRef::Url("https://example.com/start.png".to_string()));
assert_eq!(cost_cents(b), 77);
}
#[test]
fn reference_5s_540p_is_35() {
let mut b = base_builder(Some(5), Some(RouterResolution::FourEightyP));
b.reference_images = Some(ImageListRef::Urls(vec![
"https://example.com/ref-0.png".to_string(),
]));
assert_eq!(cost_cents(b), 35);
}
#[test]
fn reference_mix_10s_1080p_is_154() {
let mut b = base_builder(Some(10), Some(RouterResolution::TenEightyP));
b.reference_images = Some(ImageListRef::Urls(vec![
"https://example.com/ref-0.png".to_string(),
"https://example.com/ref-1.png".to_string(),
"https://example.com/ref-2.png".to_string(),
]));
assert_eq!(cost_cents(b), 154);
}
#[test]
fn estimate_cost_forwards_cents() {
let estimate = FalViduQ3CostState { cost_in_usd_cents: 77 }.estimate_cost();
assert_eq!(estimate.cost_in_usd_cents, Some(77));
assert_eq!(estimate.cost_in_credits, Some(77));
assert!(!estimate.is_free);
}
fn base_builder(
duration_seconds: Option<u16>,
resolution: Option<RouterResolution>,
) -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::ViduQ3,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
resolution,
..Default::default()
}
}
fn cost_cents(builder: GenerateVideoRequestBuilder) -> u64 {
let state = build_fal_vidu_q3_state(builder).expect("build");
FalViduQ3CostState::from_request(&state).cost_in_usd_cents
}
}
@@ -0,0 +1,3 @@
pub mod build;
pub mod cost;
pub mod request;
@@ -0,0 +1,146 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests::api::video::image::vidu_q3::api::ViduQ3ImageToVideoRequest;
use fal_client::requests::api::video::reference::vidu_q3_reference_to_video::api::ViduQ3ReferenceToVideoRequest;
use fal_client::requests::api::video::reference::vidu_q3_reference_to_video_mix::api::ViduQ3ReferenceToVideoMixRequest;
use fal_client::requests::api::video::text::vidu_q3::api::ViduQ3TextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
#[derive(Clone, Debug)]
pub enum FalViduQ3Mode {
TextToVideo(ViduQ3TextToVideoRequest),
ImageToVideo(ViduQ3ImageToVideoRequest),
ReferenceToVideo(ViduQ3ReferenceToVideoRequest),
ReferenceToVideoMix(ViduQ3ReferenceToVideoMixRequest),
}
#[derive(Clone, Debug)]
pub struct FalViduQ3RequestState {
pub mode: FalViduQ3Mode,
}
impl FalViduQ3RequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
match &self.mode {
FalViduQ3Mode::TextToVideo(request) => send_request(request, client).await,
FalViduQ3Mode::ImageToVideo(request) => send_request(request, client).await,
FalViduQ3Mode::ReferenceToVideo(request) => send_request(request, client).await,
FalViduQ3Mode::ReferenceToVideoMix(request) => send_request(request, client).await,
}
}
}
// ── Helpers ──
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use fal_client::creds::fal_api_key::FalApiKey;
use fal_client::requests::api::video::image::vidu_q3::api::ViduQ3ImageToVideoResolution;
use fal_client::requests::api::video::text::vidu_q3::api::{
ViduQ3TextToVideoAspectRatio, ViduQ3TextToVideoResolution,
};
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
fn read_fal_api_key() -> FalApiKey {
let secret = std::fs::read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")
.expect("Failed to read fal_api_key.txt");
FalApiKey::from_str(secret.trim())
}
fn client_with_webhook() -> RouterFalClient {
RouterFalClient::new_with_webhook(
read_fal_api_key(),
"https://example.com/fal-webhook-test".to_string(),
)
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn send_text_to_video_webhook() {
let state = FalViduQ3RequestState {
mode: FalViduQ3Mode::TextToVideo(ViduQ3TextToVideoRequest {
prompt: "a calm lake at sunrise".to_string(),
duration: Some(5),
seed: None,
aspect_ratio: Some(ViduQ3TextToVideoAspectRatio::SixteenByNine),
resolution: Some(ViduQ3TextToVideoResolution::SevenTwentyP),
audio: Some(false),
}),
};
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn send_image_to_video_webhook() {
let state = FalViduQ3RequestState {
mode: FalViduQ3Mode::ImageToVideo(ViduQ3ImageToVideoRequest {
prompt: "the lake comes alive".to_string(),
image_url: JUNO_AT_LAKE_IMAGE_URL.to_string(),
end_image_url: None,
duration: Some(5),
seed: None,
resolution: Some(ViduQ3ImageToVideoResolution::SevenTwentyP),
audio: Some(false),
}),
};
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
}
@@ -0,0 +1,407 @@
use fal_client::requests::api::video::image::vidu_q3_turbo::api::{
ViduQ3TurboImageToVideoRequest, ViduQ3TurboImageToVideoResolution,
};
use fal_client::requests::api::video::text::vidu_q3_turbo::api::{
ViduQ3TurboTextToVideoAspectRatio, ViduQ3TurboTextToVideoRequest, ViduQ3TurboTextToVideoResolution,
};
use crate::api::image_list_ref::ImageListRef;
use crate::api::router_aspect_ratio::RouterAspectRatio;
use crate::api::router_resolution::RouterResolution;
use crate::api::video_list_ref::VideoListRef;
use crate::client::request_mismatch_mitigation_strategy::RequestMismatchMitigationStrategy;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::errors::client_error::ClientError;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::kling_1_6_pro::build::optional_url;
use crate::generate::generate_video::providers::fal::vidu_q3_turbo::request::{
FalViduQ3TurboMode, FalViduQ3TurboRequestState,
};
use crate::generate::generate_video::video_generation_draft_or_request::VideoGenerationDraftOrRequest;
use crate::generate::generate_video::video_generation_request::VideoGenerationRequest;
const MIN_DURATION_SECONDS: u16 = 1;
const MAX_DURATION_SECONDS: u16 = 16;
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanAspectRatio {
SixteenByNine,
NineBySixteen,
FourByThree,
ThreeByFour,
Square,
}
/// Vidu also offers 360p, but no `RouterResolution` maps to it (fal bills 360p
/// and 540p identically, so 540p is always the better pick).
#[derive(Debug, Clone, Copy)]
pub(crate) enum PlanResolution {
FiveFortyP,
SevenTwentyP,
TenEightyP,
}
pub fn build_fal_vidu_q3_turbo(
builder: GenerateVideoRequestBuilder,
) -> Result<VideoGenerationDraftOrRequest, ArtcraftRouterError> {
let state = build_fal_vidu_q3_turbo_state(builder)?;
Ok(VideoGenerationDraftOrRequest::Request(VideoGenerationRequest::FalViduQ3Turbo(state)))
}
pub(crate) fn build_fal_vidu_q3_turbo_state(
builder: GenerateVideoRequestBuilder,
) -> Result<FalViduQ3TurboRequestState, ArtcraftRouterError> {
let strategy = builder.request_mismatch_mitigation_strategy;
reject_reference_videos(&builder.reference_videos)?;
reject_reference_images(&builder.reference_images)?;
let aspect_ratio = plan_aspect_ratio(builder.aspect_ratio, strategy)?;
let resolution = plan_resolution(builder.resolution, strategy)?;
let duration = plan_duration(builder.duration_seconds, strategy)?;
let prompt = builder.prompt.clone().unwrap_or_default();
let audio = builder.generate_audio;
let start_image_url = optional_url(builder.start_frame.clone())?;
let end_image_url = optional_url(builder.end_frame.clone())?;
// Dispatch: start_frame → image; else → text. Turbo has no reference-to-video
// endpoint (that's the non-turbo Vidu Q3 model).
let mode = if let Some(image_url) = start_image_url {
let i2v_resolution = resolution.map(to_i2v_resolution);
check_i2v_end_frame_resolution(i2v_resolution, end_image_url.is_some())?;
// Image-to-video has no aspect_ratio input — the output follows the start
// frame — so any requested aspect ratio is silently dropped here.
FalViduQ3TurboMode::ImageToVideo(ViduQ3TurboImageToVideoRequest {
prompt,
image_url,
end_image_url,
duration,
seed: None,
resolution: i2v_resolution,
audio,
})
} else {
if end_image_url.is_some() {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "end_frame",
value: "Vidu Q3 Turbo requires a start_frame when end_frame is provided".to_string(),
}));
}
FalViduQ3TurboMode::TextToVideo(ViduQ3TurboTextToVideoRequest {
prompt,
duration,
seed: None,
aspect_ratio: aspect_ratio.map(to_t2v_aspect_ratio),
resolution: resolution.map(to_t2v_resolution),
audio,
})
};
Ok(FalViduQ3TurboRequestState { mode })
}
// ── Input helpers ──
fn reject_reference_videos(
reference_videos: &Option<VideoListRef>,
) -> Result<(), ArtcraftRouterError> {
let has_reference_videos = match reference_videos {
None => false,
Some(VideoListRef::Urls(urls)) => !urls.is_empty(),
Some(VideoListRef::MediaFileTokens(tokens)) => !tokens.is_empty(),
};
if has_reference_videos {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "reference_videos",
value: "Vidu Q3 Turbo does not support video references".to_string(),
}));
}
Ok(())
}
fn reject_reference_images(
reference_images: &Option<ImageListRef>,
) -> Result<(), ArtcraftRouterError> {
let has_reference_images = match reference_images {
None => false,
Some(ImageListRef::Urls(urls)) => !urls.is_empty(),
Some(ImageListRef::MediaFileTokens(tokens)) => !tokens.is_empty(),
};
if has_reference_images {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "reference_images",
value: "Vidu Q3 Turbo does not support reference images (use Vidu Q3 for reference-to-video)".to_string(),
}));
}
Ok(())
}
/// fal rejects 360p image-to-video requests that include an end frame.
/// `plan_resolution` can't currently produce 360p, but guard anyway so a
/// future mapping change can't silently ship the rejected combination.
fn check_i2v_end_frame_resolution(
resolution: Option<ViduQ3TurboImageToVideoResolution>,
has_end_frame: bool,
) -> Result<(), ArtcraftRouterError> {
if has_end_frame && matches!(resolution, Some(ViduQ3TurboImageToVideoResolution::ThreeSixtyP)) {
return Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "resolution",
value: "Vidu Q3 Turbo image-to-video does not support 360p when an end_frame is provided".to_string(),
}));
}
Ok(())
}
// ── Plan helpers ──
fn plan_aspect_ratio(
aspect_ratio: Option<RouterAspectRatio>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<PlanAspectRatio>, ArtcraftRouterError> {
use PlanAspectRatio as Ar;
match aspect_ratio {
None => Ok(None),
Some(RouterAspectRatio::Square) | Some(RouterAspectRatio::SquareHd) => Ok(Some(Ar::Square)),
Some(RouterAspectRatio::WideSixteenByNine) | Some(RouterAspectRatio::Wide) => Ok(Some(Ar::SixteenByNine)),
Some(RouterAspectRatio::TallNineBySixteen) | Some(RouterAspectRatio::Tall) => Ok(Some(Ar::NineBySixteen)),
Some(RouterAspectRatio::WideFourByThree) => Ok(Some(Ar::FourByThree)),
Some(RouterAspectRatio::TallThreeByFour) => Ok(Some(Ar::ThreeByFour)),
Some(RouterAspectRatio::Auto)
| Some(RouterAspectRatio::Auto2k)
| Some(RouterAspectRatio::Auto3k)
| Some(RouterAspectRatio::Auto4k) => Ok(Some(Ar::SixteenByNine)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "aspect_ratio",
value: format!("{:?}", other),
}))
}
_ => Ok(Some(Ar::SixteenByNine)),
},
}
}
fn plan_resolution(
resolution: Option<RouterResolution>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<PlanResolution>, ArtcraftRouterError> {
match resolution {
None => Ok(None),
// Vidu doesn't offer 480p; 540p is the nearest supported size (and bills
// at the same per-second rate as 360p, so nothing cheaper is lost).
Some(RouterResolution::FourEightyP) => Ok(Some(PlanResolution::FiveFortyP)),
Some(RouterResolution::SevenTwentyP) => Ok(Some(PlanResolution::SevenTwentyP)),
Some(RouterResolution::TenEightyP) => Ok(Some(PlanResolution::TenEightyP)),
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "resolution",
value: format!("{:?}", other),
}))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade => Ok(Some(PlanResolution::TenEightyP)),
RequestMismatchMitigationStrategy::PayLessDowngrade => Ok(Some(PlanResolution::FiveFortyP)),
},
}
}
fn plan_duration(
duration_seconds: Option<u16>,
strategy: RequestMismatchMitigationStrategy,
) -> Result<Option<u8>, ArtcraftRouterError> {
match duration_seconds {
None => Ok(None),
Some(seconds) if (MIN_DURATION_SECONDS..=MAX_DURATION_SECONDS).contains(&seconds) => {
Ok(Some(seconds as u8))
}
Some(other) => match strategy {
RequestMismatchMitigationStrategy::ErrorOut => {
Err(ArtcraftRouterError::Client(ClientError::ModelDoesNotSupportOption {
field: "duration_seconds",
value: format!("{}", other),
}))
}
RequestMismatchMitigationStrategy::PayMoreUpgrade
| RequestMismatchMitigationStrategy::PayLessDowngrade => {
Ok(Some(other.clamp(MIN_DURATION_SECONDS, MAX_DURATION_SECONDS) as u8))
}
},
}
}
// ── Leaf converters ──
fn to_t2v_aspect_ratio(a: PlanAspectRatio) -> ViduQ3TurboTextToVideoAspectRatio {
match a {
PlanAspectRatio::SixteenByNine => ViduQ3TurboTextToVideoAspectRatio::SixteenByNine,
PlanAspectRatio::NineBySixteen => ViduQ3TurboTextToVideoAspectRatio::NineBySixteen,
PlanAspectRatio::FourByThree => ViduQ3TurboTextToVideoAspectRatio::FourByThree,
PlanAspectRatio::ThreeByFour => ViduQ3TurboTextToVideoAspectRatio::ThreeByFour,
PlanAspectRatio::Square => ViduQ3TurboTextToVideoAspectRatio::Square,
}
}
fn to_t2v_resolution(r: PlanResolution) -> ViduQ3TurboTextToVideoResolution {
match r {
PlanResolution::FiveFortyP => ViduQ3TurboTextToVideoResolution::FiveFortyP,
PlanResolution::SevenTwentyP => ViduQ3TurboTextToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => ViduQ3TurboTextToVideoResolution::TenEightyP,
}
}
fn to_i2v_resolution(r: PlanResolution) -> ViduQ3TurboImageToVideoResolution {
match r {
PlanResolution::FiveFortyP => ViduQ3TurboImageToVideoResolution::FiveFortyP,
PlanResolution::SevenTwentyP => ViduQ3TurboImageToVideoResolution::SevenTwentyP,
PlanResolution::TenEightyP => ViduQ3TurboImageToVideoResolution::TenEightyP,
}
}
#[cfg(test)]
mod tests {
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_video_model::RouterVideoModel;
use super::*;
const START_URL: &str = "https://example.com/start.png";
const END_URL: &str = "https://example.com/end.png";
mod dispatch_tests {
use super::*;
#[test]
fn no_inputs_picks_t2v() {
let state = build_fal_vidu_q3_turbo_state(base_builder()).expect("build");
assert!(matches!(state.mode, FalViduQ3TurboMode::TextToVideo(_)));
}
#[test]
fn start_frame_picks_i2v() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_URL.to_string()));
let state = build_fal_vidu_q3_turbo_state(b).expect("build");
assert!(matches!(state.mode, FalViduQ3TurboMode::ImageToVideo(_)));
}
#[test]
fn start_and_end_frame_picks_i2v_with_end_image_url() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_URL.to_string()));
b.end_frame = Some(ImageRef::Url(END_URL.to_string()));
let state = build_fal_vidu_q3_turbo_state(b).expect("build");
let FalViduQ3TurboMode::ImageToVideo(req) = state.mode else {
panic!("expected ImageToVideo");
};
assert_eq!(req.image_url, START_URL);
assert_eq!(req.end_image_url.as_deref(), Some(END_URL));
}
#[test]
fn end_frame_without_start_frame_errors() {
let mut b = base_builder();
b.end_frame = Some(ImageRef::Url(END_URL.to_string()));
assert!(build_fal_vidu_q3_turbo_state(b).is_err());
}
#[test]
fn reference_images_error() {
let mut b = base_builder();
b.reference_images = Some(ImageListRef::Urls(vec!["https://example.com/ref.png".to_string()]));
assert!(build_fal_vidu_q3_turbo_state(b).is_err());
}
#[test]
fn reference_videos_error() {
let mut b = base_builder();
b.reference_videos = Some(VideoListRef::Urls(vec!["https://example.com/a.mp4".to_string()]));
assert!(build_fal_vidu_q3_turbo_state(b).is_err());
}
}
mod plan_tests {
use super::*;
#[test]
fn duration_20_errors_under_error_out() {
let mut b = base_builder();
b.duration_seconds = Some(20);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
assert!(build_fal_vidu_q3_turbo_state(b).is_err());
}
#[test]
fn duration_20_clamps_to_16_under_pay_less() {
let mut b = base_builder();
b.duration_seconds = Some(20);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::PayLessDowngrade;
let state = build_fal_vidu_q3_turbo_state(b).expect("build");
let FalViduQ3TurboMode::TextToVideo(req) = state.mode else {
panic!("expected TextToVideo");
};
assert_eq!(req.duration, Some(16));
}
#[test]
fn resolution_480p_maps_to_540p() {
let mut b = base_builder();
b.resolution = Some(RouterResolution::FourEightyP);
let state = build_fal_vidu_q3_turbo_state(b).expect("build");
let FalViduQ3TurboMode::TextToVideo(req) = state.mode else {
panic!("expected TextToVideo");
};
assert_eq!(req.resolution, Some(ViduQ3TurboTextToVideoResolution::FiveFortyP));
}
#[test]
fn i2v_aspect_ratio_is_silently_dropped() {
let mut b = base_builder();
b.start_frame = Some(ImageRef::Url(START_URL.to_string()));
b.aspect_ratio = Some(RouterAspectRatio::TallNineBySixteen);
b.request_mismatch_mitigation_strategy = RequestMismatchMitigationStrategy::ErrorOut;
// Image-to-video has no aspect_ratio field; the request still builds.
let state = build_fal_vidu_q3_turbo_state(b).expect("build");
assert!(matches!(state.mode, FalViduQ3TurboMode::ImageToVideo(_)));
}
#[test]
fn i2v_360p_with_end_frame_is_rejected_by_guard() {
// 360p is unreachable from RouterResolution today, so exercise the
// guard directly against the fal-level resolution enum.
let result = check_i2v_end_frame_resolution(
Some(ViduQ3TurboImageToVideoResolution::ThreeSixtyP),
true,
);
assert!(result.is_err());
assert!(check_i2v_end_frame_resolution(Some(ViduQ3TurboImageToVideoResolution::ThreeSixtyP), false).is_ok());
assert!(check_i2v_end_frame_resolution(Some(ViduQ3TurboImageToVideoResolution::SevenTwentyP), true).is_ok());
}
#[test]
fn generate_audio_propagates() {
let mut b = base_builder();
b.generate_audio = Some(false);
let state = build_fal_vidu_q3_turbo_state(b).expect("build");
let FalViduQ3TurboMode::TextToVideo(req) = state.mode else {
panic!("expected TextToVideo");
};
assert_eq!(req.audio, Some(false));
}
}
fn base_builder() -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::ViduQ3Turbo,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
..Default::default()
}
}
}
@@ -0,0 +1,126 @@
use fal_client::requests::traits::fal_request_cost_calculator_trait::FalRequestCostCalculator;
use crate::generate::generate_video::providers::fal::vidu_q3_turbo::request::{
FalViduQ3TurboMode, FalViduQ3TurboRequestState,
};
use crate::generate::generate_video::video_generation_cost_estimate::VideoGenerationCostEstimate;
#[derive(Clone, Debug)]
pub struct FalViduQ3TurboCostState {
pub cost_in_usd_cents: u64,
}
impl FalViduQ3TurboCostState {
pub fn from_request(request: &FalViduQ3TurboRequestState) -> Self {
// Cost math is owned by fal_client's per-endpoint
// `FalRequestCostCalculator` implementations. The router state just
// forwards the result so router cost ≡ fal_client cost by construction.
let cost_in_usd_cents = match &request.mode {
FalViduQ3TurboMode::TextToVideo(req) => req.calculate_cost_in_cents(),
FalViduQ3TurboMode::ImageToVideo(req) => req.calculate_cost_in_cents(),
};
Self { cost_in_usd_cents }
}
pub fn estimate_cost(&self) -> VideoGenerationCostEstimate {
VideoGenerationCostEstimate {
cost_in_credits: Some(self.cost_in_usd_cents),
cost_in_usd_cents: Some(self.cost_in_usd_cents),
is_free: false,
is_unlimited: false,
is_rate_limited: false,
has_watermark: false,
failures_are_refunded: None,
}
}
}
#[cfg(test)]
mod tests {
use crate::api::image_ref::ImageRef;
use crate::api::router_provider::RouterProvider;
use crate::api::router_resolution::RouterResolution;
use crate::api::router_video_model::RouterVideoModel;
use crate::generate::generate_video::generate_video_request_builder::GenerateVideoRequestBuilder;
use crate::generate::generate_video::providers::fal::vidu_q3_turbo::build::build_fal_vidu_q3_turbo_state;
use super::*;
// Pricing (from fal_client's vidu_q3_turbo cost modules; half the Vidu Q3 rate):
// 360p/540p: $0.035/sec → 5s = 17.5¢ → 18¢
// 720p/1080p: $0.077/sec → 5s = 38.5¢ → 39¢, 10s = 77¢ (ceil to whole cents)
// fal defaults when unset: duration = 5s, resolution = 720p.
// Both modes (text/image) bill identically.
//
// NB: `build2()` isn't wired up for Vidu Q3 Turbo yet, so these tests go
// through `build_fal_vidu_q3_turbo_state()` directly.
#[test]
fn t2v_default_settings_is_39() {
assert_eq!(cost_cents(base_builder(None, None)), 39);
}
#[test]
fn t2v_5s_720p_rounds_up_to_39() {
// 5 × 7.7¢ = 38.5¢ → 39¢.
assert_eq!(cost_cents(base_builder(Some(5), Some(RouterResolution::SevenTwentyP))), 39);
}
#[test]
fn t2v_5s_540p_rounds_up_to_18() {
// RouterResolution::FourEightyP maps to Vidu's 540p; 5 × 3.5¢ = 17.5¢ → 18¢.
assert_eq!(cost_cents(base_builder(Some(5), Some(RouterResolution::FourEightyP))), 18);
}
#[test]
fn t2v_10s_1080p_is_77() {
assert_eq!(cost_cents(base_builder(Some(10), Some(RouterResolution::TenEightyP))), 77);
}
#[test]
fn t2v_3s_720p_rounds_up_to_24() {
// 3 × 7.7¢ = 23.1¢ → 24¢.
assert_eq!(cost_cents(base_builder(Some(3), Some(RouterResolution::SevenTwentyP))), 24);
}
#[test]
fn i2v_5s_720p_is_39() {
let mut b = base_builder(Some(5), Some(RouterResolution::SevenTwentyP));
b.start_frame = Some(ImageRef::Url("https://example.com/start.png".to_string()));
assert_eq!(cost_cents(b), 39);
}
#[test]
fn i2v_10s_540p_is_35() {
let mut b = base_builder(Some(10), Some(RouterResolution::FourEightyP));
b.start_frame = Some(ImageRef::Url("https://example.com/start.png".to_string()));
assert_eq!(cost_cents(b), 35);
}
#[test]
fn estimate_cost_forwards_cents() {
let estimate = FalViduQ3TurboCostState { cost_in_usd_cents: 39 }.estimate_cost();
assert_eq!(estimate.cost_in_usd_cents, Some(39));
assert_eq!(estimate.cost_in_credits, Some(39));
assert!(!estimate.is_free);
}
fn base_builder(
duration_seconds: Option<u16>,
resolution: Option<RouterResolution>,
) -> GenerateVideoRequestBuilder {
GenerateVideoRequestBuilder {
model: RouterVideoModel::ViduQ3Turbo,
provider: RouterProvider::Fal,
prompt: Some("test".to_string()),
duration_seconds,
resolution,
..Default::default()
}
}
fn cost_cents(builder: GenerateVideoRequestBuilder) -> u64 {
let state = build_fal_vidu_q3_turbo_state(builder).expect("build");
FalViduQ3TurboCostState::from_request(&state).cost_in_usd_cents
}
}
@@ -0,0 +1,3 @@
pub mod build;
pub mod cost;
pub mod request;
@@ -0,0 +1,140 @@
use std::fmt::Debug;
use std::sync::Arc;
use fal_client::requests::api::video::image::vidu_q3_turbo::api::ViduQ3TurboImageToVideoRequest;
use fal_client::requests::api::video::text::vidu_q3_turbo::api::ViduQ3TurboTextToVideoRequest;
use fal_client::requests::traits::fal_endpoint_trait::FalEndpoint;
use crate::client::router_fal_client::RouterFalClient;
use crate::errors::artcraft_router_error::ArtcraftRouterError;
use crate::generate::generate_video::generate_video_response::{
FalVideoResponsePayload, GenerateVideoResponse,
};
#[derive(Clone, Debug)]
pub enum FalViduQ3TurboMode {
TextToVideo(ViduQ3TurboTextToVideoRequest),
ImageToVideo(ViduQ3TurboImageToVideoRequest),
}
#[derive(Clone, Debug)]
pub struct FalViduQ3TurboRequestState {
pub mode: FalViduQ3TurboMode,
}
impl FalViduQ3TurboRequestState {
pub async fn send(&self, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError> {
match &self.mode {
FalViduQ3TurboMode::TextToVideo(request) => send_request(request, client).await,
FalViduQ3TurboMode::ImageToVideo(request) => send_request(request, client).await,
}
}
}
// ── Helpers ──
async fn send_request<T>(request: &T, client: &RouterFalClient) -> Result<GenerateVideoResponse, ArtcraftRouterError>
where
T: FalEndpoint + Clone + Debug + Send + Sync + 'static,
{
let outbound: Arc<dyn Debug + Send + Sync> = Arc::new(request.clone());
let payload = send_fal_request(request, client).await?;
Ok(GenerateVideoResponse::Fal(FalVideoResponsePayload {
request_id: payload.request_id,
gateway_request_id: payload.gateway_request_id,
maybe_status_url: payload.status_url,
maybe_response_url: payload.response_url,
maybe_outbound_request: Some(outbound),
}))
}
struct FalResponseIds {
request_id: Option<String>,
gateway_request_id: Option<String>,
status_url: Option<String>,
response_url: Option<String>,
}
async fn send_fal_request<T: FalEndpoint>(
request: &T,
client: &RouterFalClient,
) -> Result<FalResponseIds, ArtcraftRouterError> {
if let Some(webhook_url) = &client.webhook_url {
let response = request.send_webhook_request(&client.api_key, webhook_url).await?;
Ok(FalResponseIds {
request_id: response.request_id,
gateway_request_id: response.gateway_request_id,
status_url: None,
response_url: None,
})
} else {
let response = request.send_queue_request(&client.api_key).await?;
Ok(FalResponseIds {
request_id: Some(response.request_id),
gateway_request_id: None,
status_url: Some(response.status_url),
response_url: Some(response.response_url),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use fal_client::creds::fal_api_key::FalApiKey;
use fal_client::requests::api::video::image::vidu_q3_turbo::api::ViduQ3TurboImageToVideoResolution;
use fal_client::requests::api::video::text::vidu_q3_turbo::api::{
ViduQ3TurboTextToVideoAspectRatio, ViduQ3TurboTextToVideoResolution,
};
use test_data::web::image_urls::JUNO_AT_LAKE_IMAGE_URL;
fn read_fal_api_key() -> FalApiKey {
let secret = std::fs::read_to_string("/Users/bt/Artcraft/credentials/fal_api_key.txt")
.expect("Failed to read fal_api_key.txt");
FalApiKey::from_str(secret.trim())
}
fn client_with_webhook() -> RouterFalClient {
RouterFalClient::new_with_webhook(
read_fal_api_key(),
"https://example.com/fal-webhook-test".to_string(),
)
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn send_text_to_video_webhook() {
let state = FalViduQ3TurboRequestState {
mode: FalViduQ3TurboMode::TextToVideo(ViduQ3TurboTextToVideoRequest {
prompt: "a calm lake at sunrise".to_string(),
duration: Some(5),
seed: None,
aspect_ratio: Some(ViduQ3TurboTextToVideoAspectRatio::SixteenByNine),
resolution: Some(ViduQ3TurboTextToVideoResolution::SevenTwentyP),
audio: Some(false),
}),
};
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
#[tokio::test]
#[ignore] // requires real API key, incurs cost
async fn send_image_to_video_webhook() {
let state = FalViduQ3TurboRequestState {
mode: FalViduQ3TurboMode::ImageToVideo(ViduQ3TurboImageToVideoRequest {
prompt: "the lake comes alive".to_string(),
image_url: JUNO_AT_LAKE_IMAGE_URL.to_string(),
end_image_url: None,
duration: Some(5),
seed: None,
resolution: Some(ViduQ3TurboImageToVideoResolution::SevenTwentyP),
audio: Some(false),
}),
};
let response = state.send(&client_with_webhook()).await.expect("send should succeed");
let payload = response.get_fal_payload().expect("expected Fal payload");
assert!(payload.request_id.is_some() || payload.gateway_request_id.is_some());
}
}
@@ -67,6 +67,12 @@ use crate::generate::generate_video::providers::artcraft::veo_3p1::cost::Artcraf
use crate::generate::generate_video::providers::artcraft::veo_3p1::request::ArtcraftVeo3p1RequestState;
use crate::generate::generate_video::providers::artcraft::veo_3p1_fast::cost::ArtcraftVeo3p1FastCostState;
use crate::generate::generate_video::providers::artcraft::veo_3p1_fast::request::ArtcraftVeo3p1FastRequestState;
use crate::generate::generate_video::providers::artcraft::veo_3p1_lite::cost::ArtcraftVeo3p1LiteCostState;
use crate::generate::generate_video::providers::artcraft::veo_3p1_lite::request::ArtcraftVeo3p1LiteRequestState;
use crate::generate::generate_video::providers::artcraft::vidu_q3::cost::ArtcraftViduQ3CostState;
use crate::generate::generate_video::providers::artcraft::vidu_q3::request::ArtcraftViduQ3RequestState;
use crate::generate::generate_video::providers::artcraft::vidu_q3_turbo::cost::ArtcraftViduQ3TurboCostState;
use crate::generate::generate_video::providers::artcraft::vidu_q3_turbo::request::ArtcraftViduQ3TurboRequestState;
use crate::generate::generate_video::providers::fal::kling_1_6_pro::cost::FalKling16ProCostState;
use crate::generate::generate_video::providers::fal::kling_1_6_pro::request::FalKling16ProRequestState;
use crate::generate::generate_video::providers::fal::kling_2_1_master::cost::FalKling21MasterCostState;
@@ -99,6 +105,12 @@ use crate::generate::generate_video::providers::fal::veo_3p1::cost::FalVeo3p1Cos
use crate::generate::generate_video::providers::fal::veo_3p1::request::FalVeo3p1RequestState;
use crate::generate::generate_video::providers::fal::veo_3p1_fast::cost::FalVeo3p1FastCostState;
use crate::generate::generate_video::providers::fal::veo_3p1_fast::request::FalVeo3p1FastRequestState;
use crate::generate::generate_video::providers::fal::veo_3p1_lite::cost::FalVeo3p1LiteCostState;
use crate::generate::generate_video::providers::fal::veo_3p1_lite::request::FalVeo3p1LiteRequestState;
use crate::generate::generate_video::providers::fal::vidu_q3::cost::FalViduQ3CostState;
use crate::generate::generate_video::providers::fal::vidu_q3::request::FalViduQ3RequestState;
use crate::generate::generate_video::providers::fal::vidu_q3_turbo::cost::FalViduQ3TurboCostState;
use crate::generate::generate_video::providers::fal::vidu_q3_turbo::request::FalViduQ3TurboRequestState;
use crate::generate::generate_video::providers::gmicloud::seedance_2p0_g::cost::GmiCloudSeedance2p0UltraCostState;
use crate::generate::generate_video::providers::gmicloud::seedance_2p0_g::request::GmiCloudSeedance2p0UltraRequestState;
use crate::generate::generate_video::providers::gmicloud::seedance_2p0_fast_g::cost::GmiCloudSeedance2p0UltraFastCostState;
@@ -150,6 +162,9 @@ pub enum VideoGenerationRequest {
ArtcraftVeo3Fast(ArtcraftVeo3FastRequestState),
ArtcraftVeo3p1(ArtcraftVeo3p1RequestState),
ArtcraftVeo3p1Fast(ArtcraftVeo3p1FastRequestState),
ArtcraftVeo3p1Lite(ArtcraftVeo3p1LiteRequestState),
ArtcraftViduQ3(ArtcraftViduQ3RequestState),
ArtcraftViduQ3Turbo(ArtcraftViduQ3TurboRequestState),
FalKling16Pro(FalKling16ProRequestState),
FalKling21Master(FalKling21MasterRequestState),
FalKling21Pro(FalKling21ProRequestState),
@@ -166,6 +181,9 @@ pub enum VideoGenerationRequest {
FalVeo3Fast(FalVeo3FastRequestState),
FalVeo3p1(FalVeo3p1RequestState),
FalVeo3p1Fast(FalVeo3p1FastRequestState),
FalVeo3p1Lite(FalVeo3p1LiteRequestState),
FalViduQ3(FalViduQ3RequestState),
FalViduQ3Turbo(FalViduQ3TurboRequestState),
GmiCloudSeedance2p0Ultra(GmiCloudSeedance2p0UltraRequestState),
GmiCloudSeedance2p0UltraFast(GmiCloudSeedance2p0UltraFastRequestState),
GrokApiGrokImagineVideo(GrokApiGrokImagineVideoRequestState),
@@ -212,6 +230,9 @@ impl VideoGenerationRequest {
Self::ArtcraftVeo3Fast(_) => RouterProvider::Artcraft,
Self::ArtcraftVeo3p1(_) => RouterProvider::Artcraft,
Self::ArtcraftVeo3p1Fast(_) => RouterProvider::Artcraft,
Self::ArtcraftVeo3p1Lite(_) => RouterProvider::Artcraft,
Self::ArtcraftViduQ3(_) => RouterProvider::Artcraft,
Self::ArtcraftViduQ3Turbo(_) => RouterProvider::Artcraft,
Self::FalKling16Pro(_) => RouterProvider::Fal,
Self::FalKling21Master(_) => RouterProvider::Fal,
Self::FalKling21Pro(_) => RouterProvider::Fal,
@@ -228,6 +249,9 @@ impl VideoGenerationRequest {
Self::FalVeo3Fast(_) => RouterProvider::Fal,
Self::FalVeo3p1(_) => RouterProvider::Fal,
Self::FalVeo3p1Fast(_) => RouterProvider::Fal,
Self::FalVeo3p1Lite(_) => RouterProvider::Fal,
Self::FalViduQ3(_) => RouterProvider::Fal,
Self::FalViduQ3Turbo(_) => RouterProvider::Fal,
Self::GmiCloudSeedance2p0Ultra(_) => RouterProvider::GmiCloud,
Self::GmiCloudSeedance2p0UltraFast(_) => RouterProvider::GmiCloud,
Self::GrokApiGrokImagineVideo(_) => RouterProvider::GrokApi,
@@ -274,6 +298,9 @@ impl VideoGenerationRequest {
VideoGenerationRequest::ArtcraftVeo3Fast(request) => Ok(ArtcraftVeo3FastCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::ArtcraftVeo3p1(request) => Ok(ArtcraftVeo3p1CostState::from_request(request).estimate_cost()),
VideoGenerationRequest::ArtcraftVeo3p1Fast(request) => Ok(ArtcraftVeo3p1FastCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::ArtcraftVeo3p1Lite(request) => Ok(ArtcraftVeo3p1LiteCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::ArtcraftViduQ3(request) => Ok(ArtcraftViduQ3CostState::from_request(request).estimate_cost()),
VideoGenerationRequest::ArtcraftViduQ3Turbo(request) => Ok(ArtcraftViduQ3TurboCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::FalKling16Pro(request) => Ok(FalKling16ProCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::FalKling21Master(request) => Ok(FalKling21MasterCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::FalKling21Pro(request) => Ok(FalKling21ProCostState::from_request(request).estimate_cost()),
@@ -290,6 +317,9 @@ impl VideoGenerationRequest {
VideoGenerationRequest::FalVeo3Fast(request) => Ok(FalVeo3FastCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::FalVeo3p1(request) => Ok(FalVeo3p1CostState::from_request(request).estimate_cost()),
VideoGenerationRequest::FalVeo3p1Fast(request) => Ok(FalVeo3p1FastCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::FalVeo3p1Lite(request) => Ok(FalVeo3p1LiteCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::FalViduQ3(request) => Ok(FalViduQ3CostState::from_request(request).estimate_cost()),
VideoGenerationRequest::FalViduQ3Turbo(request) => Ok(FalViduQ3TurboCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::GmiCloudSeedance2p0Ultra(request) => Ok(GmiCloudSeedance2p0UltraCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::GmiCloudSeedance2p0UltraFast(request) => Ok(GmiCloudSeedance2p0UltraFastCostState::from_request(request).estimate_cost()),
VideoGenerationRequest::GrokApiGrokImagineVideo(request) => Ok(GrokApiGrokImagineVideoCostState::from_request(request).estimate_cost()),
@@ -433,6 +463,18 @@ impl VideoGenerationRequest {
let client_ref = client.get_artcraft_client_ref()?;
request.send(client_ref).await
},
VideoGenerationRequest::ArtcraftVeo3p1Lite(request) => {
let client_ref = client.get_artcraft_client_ref()?;
request.send(client_ref).await
},
VideoGenerationRequest::ArtcraftViduQ3(request) => {
let client_ref = client.get_artcraft_client_ref()?;
request.send(client_ref).await
},
VideoGenerationRequest::ArtcraftViduQ3Turbo(request) => {
let client_ref = client.get_artcraft_client_ref()?;
request.send(client_ref).await
},
VideoGenerationRequest::FalKling16Pro(request) => {
let client_ref = client.get_fal_client_ref()?;
request.send(client_ref).await
@@ -497,6 +539,18 @@ impl VideoGenerationRequest {
let client_ref = client.get_fal_client_ref()?;
request.send(client_ref).await
},
VideoGenerationRequest::FalVeo3p1Lite(request) => {
let client_ref = client.get_fal_client_ref()?;
request.send(client_ref).await
},
VideoGenerationRequest::FalViduQ3(request) => {
let client_ref = client.get_fal_client_ref()?;
request.send(client_ref).await
},
VideoGenerationRequest::FalViduQ3Turbo(request) => {
let client_ref = client.get_fal_client_ref()?;
request.send(client_ref).await
},
VideoGenerationRequest::GmiCloudSeedance2p0Ultra(request) => {
let client_ref = client.get_gmicloud_client_ref()?;
request.send(client_ref).await
@@ -68,6 +68,9 @@ fn video_model_to_common_model_type(model: RouterVideoModel) -> Option<CommonMod
RouterVideoModel::Veo3Fast => Some(CommonModelType::Veo3Fast),
RouterVideoModel::Veo3p1 => Some(CommonModelType::Veo3p1),
RouterVideoModel::Veo3p1Fast => Some(CommonModelType::Veo3p1Fast),
RouterVideoModel::Veo3p1Lite => Some(CommonModelType::Veo3p1Lite),
RouterVideoModel::ViduQ3 => Some(CommonModelType::ViduQ3),
RouterVideoModel::ViduQ3Turbo => Some(CommonModelType::ViduQ3Turbo),
RouterVideoModel::Seedance2p0BytePlus => Some(CommonModelType::Seedance2p0BytePlus),
RouterVideoModel::Seedance2p0BytePlusFast => Some(CommonModelType::Seedance2p0BytePlusFast),
RouterVideoModel::Seedance2p0Ultra => Some(CommonModelType::Seedance2p0Ultra),
@@ -218,6 +218,12 @@ pub enum InferenceModelType {
Veo3p1,
#[serde(rename = "veo_3p1_fast")]
Veo3p1Fast,
#[serde(rename = "veo_3p1_lite")]
Veo3p1Lite,
#[serde(rename = "vidu_q3")]
ViduQ3,
#[serde(rename = "vidu_q3_turbo")]
ViduQ3Turbo,
#[serde(rename = "preview_model")]
PreviewModel,
#[serde(rename = "preview_model_fast")]
@@ -336,6 +342,9 @@ impl InferenceModelType {
Self::Veo3Fast => "veo_3_fast",
Self::Veo3p1 => "veo_3p1",
Self::Veo3p1Fast => "veo_3p1_fast",
Self::Veo3p1Lite => "veo_3p1_lite",
Self::ViduQ3 => "vidu_q3",
Self::ViduQ3Turbo => "vidu_q3_turbo",
Self::PreviewModel => "preview_model",
Self::PreviewModelFast => "preview_model_fast",
Self::SwitchX => "switch_x",
@@ -441,6 +450,9 @@ impl InferenceModelType {
"veo_3_fast" => Ok(Self::Veo3Fast),
"veo_3p1" => Ok(Self::Veo3p1),
"veo_3p1_fast" => Ok(Self::Veo3p1Fast),
"veo_3p1_lite" => Ok(Self::Veo3p1Lite),
"vidu_q3" => Ok(Self::ViduQ3),
"vidu_q3_turbo" => Ok(Self::ViduQ3Turbo),
"preview_model" => Ok(Self::PreviewModel),
"preview_model_fast" => Ok(Self::PreviewModelFast),
"switch_x" => Ok(Self::SwitchX),
@@ -550,6 +562,9 @@ impl InferenceModelType {
Self::Veo3Fast,
Self::Veo3p1,
Self::Veo3p1Fast,
Self::Veo3p1Lite,
Self::ViduQ3,
Self::ViduQ3Turbo,
Self::PreviewModel,
Self::PreviewModelFast,
Self::SwitchX,
@@ -642,6 +657,9 @@ impl InferenceModelType {
CommonModelType::Veo3Fast => Self::Veo3Fast,
CommonModelType::Veo3p1 => Self::Veo3p1,
CommonModelType::Veo3p1Fast => Self::Veo3p1Fast,
CommonModelType::Veo3p1Lite => Self::Veo3p1Lite,
CommonModelType::ViduQ3 => Self::ViduQ3,
CommonModelType::ViduQ3Turbo => Self::ViduQ3Turbo,
CommonModelType::PreviewModel => Self::PreviewModel,
CommonModelType::PreviewModelFast => Self::PreviewModelFast,
CommonModelType::SwitchX => Self::SwitchX,
@@ -757,6 +775,9 @@ mod tests {
assert_serialization(InferenceModelType::Veo3Fast, "veo_3_fast");
assert_serialization(InferenceModelType::Veo3p1, "veo_3p1");
assert_serialization(InferenceModelType::Veo3p1Fast, "veo_3p1_fast");
assert_serialization(InferenceModelType::Veo3p1Lite, "veo_3p1_lite");
assert_serialization(InferenceModelType::ViduQ3, "vidu_q3");
assert_serialization(InferenceModelType::ViduQ3Turbo, "vidu_q3_turbo");
assert_serialization(InferenceModelType::PreviewModel, "preview_model");
assert_serialization(InferenceModelType::PreviewModelFast, "preview_model_fast");
assert_serialization(InferenceModelType::SwitchX, "switch_x");
@@ -861,6 +882,9 @@ mod tests {
assert_eq!(InferenceModelType::Veo3Fast.to_str(), "veo_3_fast");
assert_eq!(InferenceModelType::Veo3p1.to_str(), "veo_3p1");
assert_eq!(InferenceModelType::Veo3p1Fast.to_str(), "veo_3p1_fast");
assert_eq!(InferenceModelType::Veo3p1Lite.to_str(), "veo_3p1_lite");
assert_eq!(InferenceModelType::ViduQ3.to_str(), "vidu_q3");
assert_eq!(InferenceModelType::ViduQ3Turbo.to_str(), "vidu_q3_turbo");
assert_eq!(InferenceModelType::PreviewModel.to_str(), "preview_model");
assert_eq!(InferenceModelType::PreviewModelFast.to_str(), "preview_model_fast");
assert_eq!(InferenceModelType::SwitchX.to_str(), "switch_x");
@@ -965,6 +989,9 @@ mod tests {
assert_eq!(InferenceModelType::from_str("veo_3_fast").unwrap(), InferenceModelType::Veo3Fast);
assert_eq!(InferenceModelType::from_str("veo_3p1").unwrap(), InferenceModelType::Veo3p1);
assert_eq!(InferenceModelType::from_str("veo_3p1_fast").unwrap(), InferenceModelType::Veo3p1Fast);
assert_eq!(InferenceModelType::from_str("veo_3p1_lite").unwrap(), InferenceModelType::Veo3p1Lite);
assert_eq!(InferenceModelType::from_str("vidu_q3").unwrap(), InferenceModelType::ViduQ3);
assert_eq!(InferenceModelType::from_str("vidu_q3_turbo").unwrap(), InferenceModelType::ViduQ3Turbo);
assert_eq!(InferenceModelType::from_str("preview_model").unwrap(), InferenceModelType::PreviewModel);
assert_eq!(InferenceModelType::from_str("preview_model_fast").unwrap(), InferenceModelType::PreviewModelFast);
assert_eq!(InferenceModelType::from_str("switch_x").unwrap(), InferenceModelType::SwitchX);
@@ -187,6 +187,12 @@ pub enum CommonModelType {
Veo3p1,
#[serde(rename = "veo_3p1_fast")]
Veo3p1Fast,
#[serde(rename = "veo_3p1_lite")]
Veo3p1Lite,
#[serde(rename = "vidu_q3")]
ViduQ3,
#[serde(rename = "vidu_q3_turbo")]
ViduQ3Turbo,
#[serde(rename = "preview_model")]
PreviewModel,
#[serde(rename = "preview_model_fast")]
@@ -289,6 +295,9 @@ impl CommonModelType {
Self::Veo3Fast => "veo_3_fast",
Self::Veo3p1 => "veo_3p1",
Self::Veo3p1Fast => "veo_3p1_fast",
Self::Veo3p1Lite => "veo_3p1_lite",
Self::ViduQ3 => "vidu_q3",
Self::ViduQ3Turbo => "vidu_q3_turbo",
Self::PreviewModel => "preview_model",
Self::PreviewModelFast => "preview_model_fast",
Self::SwitchX => "switch_x",
@@ -377,6 +386,9 @@ impl CommonModelType {
"veo_3_fast" => Ok(Self::Veo3Fast),
"veo_3p1" => Ok(Self::Veo3p1),
"veo_3p1_fast" => Ok(Self::Veo3p1Fast),
"veo_3p1_lite" => Ok(Self::Veo3p1Lite),
"vidu_q3" => Ok(Self::ViduQ3),
"vidu_q3_turbo" => Ok(Self::ViduQ3Turbo),
"preview_model" => Ok(Self::PreviewModel),
"preview_model_fast" => Ok(Self::PreviewModelFast),
"switch_x" => Ok(Self::SwitchX),
@@ -469,6 +481,9 @@ impl CommonModelType {
Self::Veo3Fast,
Self::Veo3p1,
Self::Veo3p1Fast,
Self::Veo3p1Lite,
Self::ViduQ3,
Self::ViduQ3Turbo,
Self::PreviewModel,
Self::PreviewModelFast,
Self::SwitchX,
@@ -558,6 +573,9 @@ impl CommonModelType {
Self::Veo3Fast => CommonModelClass::Video,
Self::Veo3p1 => CommonModelClass::Video,
Self::Veo3p1Fast => CommonModelClass::Video,
Self::Veo3p1Lite => CommonModelClass::Video,
Self::ViduQ3 => CommonModelClass::Video,
Self::ViduQ3Turbo => CommonModelClass::Video,
Self::PreviewModel => CommonModelClass::Video,
Self::PreviewModelFast => CommonModelClass::Video,
Self::SwitchX => CommonModelClass::Video,
@@ -656,6 +674,9 @@ mod tests {
assert_serialization(CommonModelType::Veo3Fast, "veo_3_fast");
assert_serialization(CommonModelType::Veo3p1, "veo_3p1");
assert_serialization(CommonModelType::Veo3p1Fast, "veo_3p1_fast");
assert_serialization(CommonModelType::Veo3p1Lite, "veo_3p1_lite");
assert_serialization(CommonModelType::ViduQ3, "vidu_q3");
assert_serialization(CommonModelType::ViduQ3Turbo, "vidu_q3_turbo");
assert_serialization(CommonModelType::PreviewModel, "preview_model");
assert_serialization(CommonModelType::PreviewModelFast, "preview_model_fast");
assert_serialization(CommonModelType::SwitchX, "switch_x");
@@ -741,6 +762,9 @@ mod tests {
assert_eq!(CommonModelType::Veo3Fast.to_str(), "veo_3_fast");
assert_eq!(CommonModelType::Veo3p1.to_str(), "veo_3p1");
assert_eq!(CommonModelType::Veo3p1Fast.to_str(), "veo_3p1_fast");
assert_eq!(CommonModelType::Veo3p1Lite.to_str(), "veo_3p1_lite");
assert_eq!(CommonModelType::ViduQ3.to_str(), "vidu_q3");
assert_eq!(CommonModelType::ViduQ3Turbo.to_str(), "vidu_q3_turbo");
assert_eq!(CommonModelType::PreviewModel.to_str(), "preview_model");
assert_eq!(CommonModelType::PreviewModelFast.to_str(), "preview_model_fast");
assert_eq!(CommonModelType::SwitchX.to_str(), "switch_x");
@@ -826,6 +850,9 @@ mod tests {
assert_eq!(CommonModelType::from_str("veo_3_fast").unwrap(), CommonModelType::Veo3Fast);
assert_eq!(CommonModelType::from_str("veo_3p1").unwrap(), CommonModelType::Veo3p1);
assert_eq!(CommonModelType::from_str("veo_3p1_fast").unwrap(), CommonModelType::Veo3p1Fast);
assert_eq!(CommonModelType::from_str("veo_3p1_lite").unwrap(), CommonModelType::Veo3p1Lite);
assert_eq!(CommonModelType::from_str("vidu_q3").unwrap(), CommonModelType::ViduQ3);
assert_eq!(CommonModelType::from_str("vidu_q3_turbo").unwrap(), CommonModelType::ViduQ3Turbo);
assert_eq!(CommonModelType::from_str("preview_model").unwrap(), CommonModelType::PreviewModel);
assert_eq!(CommonModelType::from_str("preview_model_fast").unwrap(), CommonModelType::PreviewModelFast);
assert_eq!(CommonModelType::from_str("switch_x").unwrap(), CommonModelType::SwitchX);
@@ -842,7 +869,7 @@ mod tests {
#[test]
fn all_variants() {
let mut variants = CommonModelType::all_variants();
assert_eq!(variants.len(), 76);
assert_eq!(variants.len(), 79);
// Image models
assert_eq!(variants.pop_first(), Some(CommonModelType::Flux1Dev));
assert_eq!(variants.pop_first(), Some(CommonModelType::Flux1Schnell));
@@ -913,6 +940,9 @@ mod tests {
assert_eq!(variants.pop_first(), Some(CommonModelType::Veo3Fast));
assert_eq!(variants.pop_first(), Some(CommonModelType::Veo3p1));
assert_eq!(variants.pop_first(), Some(CommonModelType::Veo3p1Fast));
assert_eq!(variants.pop_first(), Some(CommonModelType::Veo3p1Lite));
assert_eq!(variants.pop_first(), Some(CommonModelType::ViduQ3));
assert_eq!(variants.pop_first(), Some(CommonModelType::ViduQ3Turbo));
assert_eq!(variants.pop_first(), Some(CommonModelType::PreviewModel));
assert_eq!(variants.pop_first(), Some(CommonModelType::PreviewModelFast));
assert_eq!(variants.pop_first(), Some(CommonModelType::SwitchX));
@@ -101,6 +101,15 @@ pub enum CommonVideoModel {
#[serde(rename = "veo_3p1_fast")]
Veo3p1Fast,
#[serde(rename = "veo_3p1_lite")]
Veo3p1Lite,
#[serde(rename = "vidu_q3")]
ViduQ3,
#[serde(rename = "vidu_q3_turbo")]
ViduQ3Turbo,
// NB: Temporary model rollout. Previously used for "Seedance 2.0 BytePlus".
// This can be reused in the future.
#[serde(rename = "preview_model")]
@@ -147,6 +156,9 @@ impl CommonVideoModel {
Self::Veo3Fast => CommonModelType::Veo3Fast,
Self::Veo3p1 => CommonModelType::Veo3p1,
Self::Veo3p1Fast => CommonModelType::Veo3p1Fast,
Self::Veo3p1Lite => CommonModelType::Veo3p1Lite,
Self::ViduQ3 => CommonModelType::ViduQ3,
Self::ViduQ3Turbo => CommonModelType::ViduQ3Turbo,
Self::PreviewModel => CommonModelType::PreviewModel,
Self::PreviewModelFast => CommonModelType::PreviewModelFast,
}
@@ -192,6 +204,9 @@ mod tests {
assert_serialization(CommonVideoModel::Veo3Fast, "veo_3_fast");
assert_serialization(CommonVideoModel::Veo3p1, "veo_3p1");
assert_serialization(CommonVideoModel::Veo3p1Fast, "veo_3p1_fast");
assert_serialization(CommonVideoModel::Veo3p1Lite, "veo_3p1_lite");
assert_serialization(CommonVideoModel::ViduQ3, "vidu_q3");
assert_serialization(CommonVideoModel::ViduQ3Turbo, "vidu_q3_turbo");
assert_serialization(CommonVideoModel::PreviewModel, "preview_model");
assert_serialization(CommonVideoModel::PreviewModelFast, "preview_model_fast");
}
@@ -230,6 +245,9 @@ mod tests {
("veo_3_fast", CommonVideoModel::Veo3Fast),
("veo_3p1", CommonVideoModel::Veo3p1),
("veo_3p1_fast", CommonVideoModel::Veo3p1Fast),
("veo_3p1_lite", CommonVideoModel::Veo3p1Lite),
("vidu_q3", CommonVideoModel::ViduQ3),
("vidu_q3_turbo", CommonVideoModel::ViduQ3Turbo),
("preview_model", CommonVideoModel::PreviewModel),
("preview_model_fast", CommonVideoModel::PreviewModelFast),
];
@@ -275,6 +293,9 @@ mod tests {
CommonVideoModel::Veo3Fast,
CommonVideoModel::Veo3p1,
CommonVideoModel::Veo3p1Fast,
CommonVideoModel::Veo3p1Lite,
CommonVideoModel::ViduQ3,
CommonVideoModel::ViduQ3Turbo,
CommonVideoModel::PreviewModel,
CommonVideoModel::PreviewModelFast,
];
@@ -319,6 +340,9 @@ mod tests {
(CommonVideoModel::Veo3Fast, CommonModelType::Veo3Fast),
(CommonVideoModel::Veo3p1, CommonModelType::Veo3p1),
(CommonVideoModel::Veo3p1Fast, CommonModelType::Veo3p1Fast),
(CommonVideoModel::Veo3p1Lite, CommonModelType::Veo3p1Lite),
(CommonVideoModel::ViduQ3, CommonModelType::ViduQ3),
(CommonVideoModel::ViduQ3Turbo, CommonModelType::ViduQ3Turbo),
(CommonVideoModel::PreviewModel, CommonModelType::PreviewModel),
(CommonVideoModel::PreviewModelFast, CommonModelType::PreviewModelFast),
];
@@ -5,3 +5,4 @@ pub mod seedance_1x_video_models;
pub mod seedance_2p0_video_models;
pub mod sora_video_models;
pub mod veo_video_models;
pub mod vidu_video_models;
@@ -74,13 +74,16 @@ pub fn veo_video_models() -> Vec<OmniGenVideoModelDetails> {
});
models.push(OmniGenVideoModelDetails {
is_disabled: Some(true), // TODO: Temporarily disable
model: CommonVideoModel::Veo3p1,
model_creator: Some(ModelCreator::Google),
full_name: Some("Veo 3.1".to_string()),
text_prompt_supported: Some(true),
starting_keyframe_supported: Some(true),
ending_keyframe_supported: Some(true),
image_references_supported: Some(true),
image_references_max: Some(3),
video_references_supported: Some(true),
video_references_max: Some(1),
show_generate_with_sound_toggle: Some(true),
aspect_ratio_options: Some(vec![
CommonAspectRatio::Auto,
@@ -100,13 +103,16 @@ pub fn veo_video_models() -> Vec<OmniGenVideoModelDetails> {
});
models.push(OmniGenVideoModelDetails {
is_disabled: Some(true), // TODO: Temporarily disable
model: CommonVideoModel::Veo3p1Fast,
model_creator: Some(ModelCreator::Google),
full_name: Some("Veo 3.1 Fast".to_string()),
text_prompt_supported: Some(true),
starting_keyframe_supported: Some(true),
ending_keyframe_supported: Some(true),
image_references_supported: Some(true),
image_references_max: Some(3),
video_references_supported: Some(true),
video_references_max: Some(1),
show_generate_with_sound_toggle: Some(true),
aspect_ratio_options: Some(vec![
CommonAspectRatio::Auto,
@@ -125,5 +131,30 @@ pub fn veo_video_models() -> Vec<OmniGenVideoModelDetails> {
..Default::default()
});
models.push(OmniGenVideoModelDetails {
model: CommonVideoModel::Veo3p1Lite,
model_creator: Some(ModelCreator::Google),
full_name: Some("Veo 3.1 Lite".to_string()),
text_prompt_supported: Some(true),
starting_keyframe_supported: Some(true),
ending_keyframe_supported: Some(true),
show_generate_with_sound_toggle: Some(true),
aspect_ratio_options: Some(vec![
CommonAspectRatio::Auto,
CommonAspectRatio::WideSixteenByNine,
CommonAspectRatio::TallNineBySixteen,
]),
aspect_ratio_default: Some(CommonAspectRatio::WideSixteenByNine),
resolution_options: Some(vec![
CommonResolution::SevenTwentyP,
CommonResolution::TenEightyP,
]),
resolution_default: Some(CommonResolution::SevenTwentyP),
duration_seconds_min: Some(4),
duration_seconds_max: Some(8),
duration_seconds_default: Some(8),
..Default::default()
});
models
}
@@ -0,0 +1,70 @@
use artcraft_api_defs::omni_gen::models::omni_gen_video_models::OmniGenVideoModelDetails;
use enums::common::generation::common_aspect_ratio::CommonAspectRatio;
use enums::common::generation::common_resolution::CommonResolution;
use enums::common::generation::common_video_model::CommonVideoModel;
use enums::common::generation::model_creator::ModelCreator;
/// Vidu (Shengshu) video models.
pub fn vidu_video_models() -> Vec<OmniGenVideoModelDetails> {
let mut models = Vec::new();
models.push(OmniGenVideoModelDetails {
model: CommonVideoModel::ViduQ3,
model_creator: Some(ModelCreator::Vidu),
full_name: Some("Vidu Q3".to_string()),
text_prompt_supported: Some(true),
starting_keyframe_supported: Some(true),
ending_keyframe_supported: Some(true),
image_references_supported: Some(true),
image_references_max: Some(4),
show_generate_with_sound_toggle: Some(true),
aspect_ratio_options: Some(vec![
CommonAspectRatio::WideSixteenByNine,
CommonAspectRatio::TallNineBySixteen,
CommonAspectRatio::WideFourByThree,
CommonAspectRatio::TallThreeByFour,
CommonAspectRatio::Square,
]),
aspect_ratio_default: Some(CommonAspectRatio::WideSixteenByNine),
resolution_options: Some(vec![
CommonResolution::FourEightyP,
CommonResolution::SevenTwentyP,
CommonResolution::TenEightyP,
]),
resolution_default: Some(CommonResolution::SevenTwentyP),
duration_seconds_min: Some(1),
duration_seconds_max: Some(16),
duration_seconds_default: Some(5),
..Default::default()
});
models.push(OmniGenVideoModelDetails {
model: CommonVideoModel::ViduQ3Turbo,
model_creator: Some(ModelCreator::Vidu),
full_name: Some("Vidu Q3 Turbo".to_string()),
text_prompt_supported: Some(true),
starting_keyframe_supported: Some(true),
ending_keyframe_supported: Some(true),
show_generate_with_sound_toggle: Some(true),
aspect_ratio_options: Some(vec![
CommonAspectRatio::WideSixteenByNine,
CommonAspectRatio::TallNineBySixteen,
CommonAspectRatio::WideFourByThree,
CommonAspectRatio::TallThreeByFour,
CommonAspectRatio::Square,
]),
aspect_ratio_default: Some(CommonAspectRatio::WideSixteenByNine),
resolution_options: Some(vec![
CommonResolution::FourEightyP,
CommonResolution::SevenTwentyP,
CommonResolution::TenEightyP,
]),
resolution_default: Some(CommonResolution::SevenTwentyP),
duration_seconds_min: Some(1),
duration_seconds_max: Some(16),
duration_seconds_default: Some(5),
..Default::default()
});
models
}
@@ -10,6 +10,7 @@ use super::by_type::seedance_1x_video_models::{seedance_1p0_video_models, seedan
use super::by_type::seedance_2p0_video_models::seedance_2p0_video_models;
use super::by_type::sora_video_models::sora_video_models;
use super::by_type::veo_video_models::veo_video_models;
use super::by_type::vidu_video_models::vidu_video_models;
pub const OMNI_GEN_VIDEO_MODELS_AND_PROVIDERS: Lazy<OmniGenVideoModelsResponse> = Lazy::new(|| {
let models = build_omni_gen_video_models();
@@ -33,6 +34,7 @@ fn build_omni_gen_video_models() -> Vec<OmniGenVideoModelDetails> {
models.extend(seedance_2p0_video_models());
models.extend(sora_video_models());
models.extend(veo_video_models());
models.extend(vidu_video_models());
models
}
@@ -83,6 +85,18 @@ fn build_omni_gen_video_model_providers() -> Vec<OmniGenVideoModelProviderDetail
model: CommonVideoModel::Veo3p1Fast,
overrides: None,
},
OmniGenVideoProviderModelDetails {
model: CommonVideoModel::Veo3p1Lite,
overrides: None,
},
OmniGenVideoProviderModelDetails {
model: CommonVideoModel::ViduQ3,
overrides: None,
},
OmniGenVideoProviderModelDetails {
model: CommonVideoModel::ViduQ3Turbo,
overrides: None,
},
OmniGenVideoProviderModelDetails {
model: CommonVideoModel::Kling16Pro,
overrides: None,
@@ -161,6 +161,9 @@ fn map_video_model(model: CommonVideoModel) -> Result<RouterVideoModel, HandlerE
CommonVideoModel::Veo3Fast => RouterVideoModel::Veo3Fast,
CommonVideoModel::Veo3p1 => RouterVideoModel::Veo3p1,
CommonVideoModel::Veo3p1Fast => RouterVideoModel::Veo3p1Fast,
CommonVideoModel::Veo3p1Lite => RouterVideoModel::Veo3p1Lite,
CommonVideoModel::ViduQ3 => RouterVideoModel::ViduQ3,
CommonVideoModel::ViduQ3Turbo => RouterVideoModel::ViduQ3Turbo,
CommonVideoModel::PreviewModel => RouterVideoModel::PreviewModel,
CommonVideoModel::PreviewModelFast => RouterVideoModel::PreviewModelFast,
};