Skip to main content

agentai_video/
lib.rs

1//! # AgentAI Video
2//!
3//! Video generation for [AgentAI](https://crates.io/crates/agentai). Video APIs are
4//! asynchronous: [`VideoProvider::submit`] sends a request and returns a [`VideoJob`], which you
5//! follow with [`VideoProvider::status`] and, when it is [`JobStatus::Completed`], fetch with
6//! [`VideoProvider::download`]. Polling is up to the caller.
7//!
8//! ```rust,no_run
9//! use std::time::Duration;
10//! use agentai_video::{JobStatus, VideoRequest, from_model};
11//!
12//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
13//! // The provider comes from the prefix of the model name, its settings from environment
14//! // variables (`COMFY_SERVE_BASE_URL` here)
15//! let provider = from_model("comfy-serve::wan-2.2")?;
16//! let job = provider
17//!     .submit(VideoRequest::new("A lighthouse at dawn").with_duration(5))
18//!     .await?;
19//! loop {
20//!     match provider.status(&job).await? {
21//!         JobStatus::Completed => break,
22//!         JobStatus::Failed(message) => return Err(message.into()),
23//!         JobStatus::Gone => return Err("the job is gone".into()),
24//!         _ => tokio::time::sleep(Duration::from_secs(5)).await,
25//!     }
26//! }
27//! let video = provider.download(&job).await?; // a `Media`, base64 or a URL
28//! # Ok(())
29//! # }
30//! ```
31//!
32//! A [`VideoJob`] can be stored (serde) and given back, possibly to a new provider, after a
33//! restart: `status`, `download` and `cancel` need no model. A job does not record which provider
34//! made it, so store the provider and model name next to it.
35//!
36//! ## Inputs
37//!
38//! [`VideoRequest::with_inputs`] takes [`VideoInputs`], one of three modes:
39//!
40//! - `None` (the default): text-to-video.
41//! - `Frames { first, last }`: image-to-video, from a first frame, a last frame or both.
42//! - `References(`[`VideoReferences`]`)`: reference-to-video, from reference images, videos and
43//!   audio.
44//!
45//! Frames and references exclude each other. A provider that cannot send some input returns
46//! [`VideoError::Unsupported`]. Options you do not set (duration, resolution, aspect ratio, seed)
47//! are not sent, and values are not checked here: the API reports the ones it does not accept.
48//!
49//! ## Models
50//!
51//! [`list_models`] (or [`VideoProvider::list_models`]) returns [`VideoModel`]s with the durations,
52//! resolutions, aspect ratios and input kinds a model accepts, where the provider says so. `None`
53//! in one of these lists means the provider does not say, not that the option is unsupported:
54//! offer it and let the server answer. `Some(vec![])` means there is nothing to choose.
55//!
56//! ## Providers
57//!
58//! | Prefix | Settings | Cancel | Inputs |
59//! |--------|----------|--------|--------|
60//! | `comfy-serve` | `COMFY_SERVE_BASE_URL` (required), `COMFY_SERVE_API_KEY` (optional) | yes | first and last frames; image, video and audio references; files are uploaded as multipart |
61//! | `openrouter` | `OPENROUTER_API_KEY` (required), `OPENROUTER_BASE_URL` (optional) | no | first and last frames and image references, sent as URLs or data URLs |
62//!
63//! To set the key and base URL in code, or to change timeouts or the proxy, build the provider
64//! yourself and give it your own client:
65//!
66//! ```rust,ignore
67//! // `reqwest::Client` needs `reqwest` in your dependencies; `ReqwestClient` and the other HTTP
68//! // types are re-exported from this crate
69//! use agentai_video::ReqwestClient;
70//! use agentai_video::provider::comfy_serve::ComfyServeVideoProvider;
71//!
72//! let client = reqwest::Client::builder().build().unwrap();
73//! let provider = ComfyServeVideoProvider::new("wan-2.2", "", "http://localhost:8000")
74//!     .with_http_client(ReqwestClient::from_client(client));
75//! ```
76//!
77//! The HTTP types ([`HttpClient`], [`HttpRequest`], [`HttpResponse`], [`HttpError`],
78//! [`ReqwestClient`]), [`Media`] and [`mime`] are re-exported, so this crate is enough to use a
79//! provider with your own client or to implement [`VideoProvider`] for another API.
80//!
81//! ## Files
82//!
83//! [`mime::from_path`] guesses the type of an input file from its name, and
84//! [`Media::extension`] gives the file extension of a downloaded video's type:
85//!
86//! ```rust,no_run
87//! # use agentai_video::{Media, ReqwestClient, VideoInputs, VideoRequest, mime};
88//! # async fn run(video: Media) -> Result<(), Box<dyn std::error::Error>> {
89//! let path = "first.jpg";
90//! let first = Media::from_bytes(mime::from_path(path).ok_or("unknown type")?, std::fs::read(path)?);
91//! let request = VideoRequest::new("The scene comes alive").with_inputs(VideoInputs::Frames {
92//!     first: Some(first),
93//!     last: None,
94//! });
95//! // ... submit, poll, download ...
96//! let bytes = video.bytes(&ReqwestClient::new()).await?;
97//! std::fs::write(format!("video.{}", video.extension()), bytes)?;
98//! # Ok(())
99//! # }
100//! ```
101//!
102//! ## Limits
103//!
104//! - Files travel in memory as [`Media`] (base64 or a URL), so large videos are held whole.
105//! - There is no waiting helper (the loop above is yours to write, with your timing and your
106//!   runtime), no retries and no timeouts of its own.
107//! - There are no agent tools: this crate does not depend on `agentai`.
108//!
109//! ## Features
110#![doc = document_features::document_features!()]
111
112#[cfg(all(
113    feature = "native-tls",
114    any(feature = "rustls-ring", feature = "rustls-aws-lc")
115))]
116compile_error!(
117    "agentai-video: `native-tls` cannot be combined with `rustls-ring` or `rustls-aws-lc`. \
118     To use native-tls, set `default-features = false` and enable `native-tls`."
119);
120
121pub mod provider;
122
123pub use agentai_core::message::{Media, MediaSource};
124#[doc(inline)]
125pub use agentai_core::mime;
126pub use agentai_core::{HttpClient, HttpError, HttpRequest, HttpResponse, ReqwestClient};
127pub use provider::{
128    JobStatus, VideoError, VideoInputKind, VideoInputs, VideoJob, VideoModel, VideoProvider,
129    VideoReferences, VideoRequest, from_model, list_models,
130};