use std::collections::HashMap;
use std::sync::{Mutex, PoisonError};
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use chrono::{DateTime, SecondsFormat, Utc};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::authentification::CredentialsOrApiKey;
use crate::drive::{FileDates, Progress, from_bytes, parse_stream};
use crate::error::{Error, Result};
use crate::http::{client, encode, ok, progress_body};
use crate::parrot::generative::{File, Part, TextPart};
pub const ATTACHMENT_TYPES: [(&str, &str); 15] = [
("image/jpeg", "image"),
("image/png", "image"),
("image/webp", "image"),
("image/heic", "image"),
("image/heif", "image"),
("audio/mpeg", "audio_file"),
("audio/wav", "audio_file"),
("video/mp4", "movie"),
("video/webm", "movie"),
("video/quicktime", "movie"),
("application/pdf", "picture_as_pdf"),
("text/plain", "description"),
("text/markdown", "description"),
("text/csv", "table_chart"),
("text/html", "language"),
];
pub fn mime_from_file_name(name: &str) -> Option<&'static str> {
let extension = if name.contains('.') {
name.rsplit('.').next().unwrap_or_default().to_lowercase()
} else {
String::new()
};
match extension.as_str() {
"jpg" | "jpeg" => Some("image/jpeg"),
"png" => Some("image/png"),
"webp" => Some("image/webp"),
"heic" => Some("image/heic"),
"heif" => Some("image/heif"),
"mp3" => Some("audio/mpeg"),
"wav" => Some("audio/wav"),
"mp4" => Some("video/mp4"),
"webm" => Some("video/webm"),
"mov" | "qt" => Some("video/quicktime"),
"pdf" => Some("application/pdf"),
"txt" => Some("text/plain"),
"md" | "markdown" => Some("text/markdown"),
"csv" => Some("text/csv"),
"html" | "htm" => Some("text/html"),
"json" => Some("application/json"),
"xml" => Some("application/xml"),
_ => None,
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Website {
pub url: String,
pub content_type: String,
pub title: String,
pub description: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProxyMode {
Residential,
Datacenter,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DocumentMode {
Extraction,
Render,
Virtualized,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContentTypeInfo {
pub content_type: String,
pub name: String,
pub extensions: Vec<String>,
}
static TYPES_CACHE: Mutex<Option<HashMap<String, Vec<ContentTypeInfo>>>> = Mutex::new(None);
pub async fn get_types() -> Result<HashMap<String, Vec<ContentTypeInfo>>> {
if let Some(types) = TYPES_CACHE
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone()
{
return Ok(types);
}
let response = client()
.get(format!("{}/media/types", super::get_url()))
.send()
.await?;
let types: HashMap<String, Vec<ContentTypeInfo>> = ok(response).await?.json().await?;
*TYPES_CACHE.lock().unwrap_or_else(PoisonError::into_inner) = Some(types.clone());
Ok(types)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BytesOrBytesList {
Bytes(Vec<u8>),
BytesList(Vec<Vec<u8>>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PullResponse {
pub data: BytesOrBytesList,
pub content_type: String,
}
#[derive(Debug, Clone, Default)]
pub struct QueryOptions {
pub cursor: u32,
pub max: u32,
pub start: Option<DateTime<Utc>>,
pub end: Option<DateTime<Utc>>,
}
pub async fn query(
authorization: &CredentialsOrApiKey,
query: &str,
options: QueryOptions,
) -> Result<Vec<Website>> {
let mut body = Map::new();
body.insert("query".to_string(), Value::from(query));
body.insert("cursor".to_string(), Value::from(options.cursor));
body.insert("max".to_string(), Value::from(options.max));
if let Some(start) = options.start {
body.insert(
"start".to_string(),
Value::from(start.to_rfc3339_opts(SecondsFormat::Millis, true)),
);
}
if let Some(end) = options.end {
body.insert(
"end".to_string(),
Value::from(end.to_rfc3339_opts(SecondsFormat::Millis, true)),
);
}
let response = client()
.post(format!("{}/media/query", super::get_url()))
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
)
.json(&body)
.send()
.await?;
Ok(response.json().await?)
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct PullOptions {
#[serde(skip_serializing_if = "Option::is_none")]
pub modals: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub segment: Option<[f64; 2]>,
#[serde(skip_serializing_if = "Option::is_none")]
pub split: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub proxy: Option<ProxyMode>,
}
pub async fn pull(
authorization: &CredentialsOrApiKey,
url: &str,
request: Option<&Value>,
options: PullOptions,
) -> Result<Option<PullResponse>> {
let effective_options = PullOptions {
proxy: Some(options.proxy.unwrap_or(ProxyMode::Datacenter)),
..options
};
let mut builder = client()
.post(format!("{}/media/pull", super::get_url()))
.header("Content-Type", "application/json")
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
)
.header("Url", url);
if let Some(request) = request {
builder = builder.header("Request", request.to_string());
}
let response = builder
.header("Options", serde_json::to_string(&effective_options)?)
.send()
.await?;
if !response.status().is_success() {
return Err(Error::Message(format!("Failed to fetch website at {url}.")));
}
let content_type = response
.headers()
.get("Content-Type")
.and_then(|value| value.to_str().ok())
.filter(|value| !value.is_empty())
.unwrap_or("application/octet-stream")
.to_string();
let buffer = parse_stream(response, None::<fn(u64)>).await?;
Ok(Some(PullResponse {
content_type,
data: BytesOrBytesList::Bytes(buffer.into_iter().next().unwrap_or_default()),
}))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Input {
Image,
Video,
Audio,
}
impl Input {
fn as_str(self) -> &'static str {
match self {
Input::Image => "image",
Input::Video => "video",
Input::Audio => "audio",
}
}
}
fn chat_parts(authorization: &CredentialsOrApiKey, raw: &[Value]) -> Vec<Part> {
raw.iter()
.map(|part| {
if part["type"] == "FILE" {
let text = |key: &str, fallback: &str| match &part[key] {
Value::Null => fallback.to_string(),
Value::String(value) => value.clone(),
value => value.to_string(),
};
Part::File(File {
file: from_bytes(
authorization.clone(),
&text("name", "file"),
&text("contentType", "application/octet-stream"),
STANDARD
.decode(part["data"].as_str().unwrap_or_default())
.unwrap_or_default(),
FileDates::default(),
),
})
} else {
Part::Text(TextPart {
text: match &part["text"] {
Value::Null => String::new(),
Value::String(text) => text.clone(),
text => text.to_string(),
},
})
}
})
.collect()
}
#[derive(Debug, Clone)]
pub enum PartsOrPartsList {
Parts(Vec<Part>),
PartsList(Vec<Vec<Part>>),
}
#[derive(Debug, Clone, Default)]
pub struct ChatOptions {
pub name: Option<String>,
pub takes: Option<Vec<Input>>,
pub segment: Option<[f64; 2]>,
pub split: Option<f64>,
}
pub async fn chat(
authorization: &CredentialsOrApiKey,
content_type: &str,
data: &[u8],
options: ChatOptions,
) -> Result<Option<PartsOrPartsList>> {
let takes = options
.takes
.unwrap_or_else(|| vec![Input::Image])
.iter()
.map(|input| input.as_str())
.collect::<Vec<_>>()
.join(",");
let mut request = client()
.post(format!("{}/media/chat", super::get_url()))
.header("Content-Type", content_type)
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
)
.header("Takes", takes);
if let Some(name) = options.name.as_deref().filter(|name| !name.is_empty()) {
request = request.header("Name", encode(name));
}
if let Some([start, end]) = options.segment {
request = request.header("Segment", format!("{start},{end}"));
}
if let Some(split) = options.split.filter(|split| *split != 0.0) {
request = request.header("Split", split.to_string());
}
let response = request.body(data.to_vec()).send().await?;
if !response.status().is_success() {
return Ok(None);
}
let json: Value = response.json().await?;
let Some(rows) = json.as_array() else {
return Ok(None);
};
if options.split.is_some_and(|split| split != 0.0) {
return Ok(Some(PartsOrPartsList::PartsList(
rows.iter()
.map(|segment| {
chat_parts(authorization, segment.as_array().map_or(&[], Vec::as_slice))
})
.collect(),
)));
}
Ok(Some(PartsOrPartsList::Parts(chat_parts(
authorization,
rows,
))))
}
pub async fn parse(
authorization: &CredentialsOrApiKey,
content_type: &str,
data: &[u8],
on_progress: Option<Progress>,
) -> Result<String> {
let response = client()
.post(format!("{}/media/parse", super::get_url()))
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
)
.header(
"Content-Type",
if content_type.is_empty() {
"application/octet-stream"
} else {
content_type
},
)
.header("Content-Length", data.len())
.body(progress_body(data.to_vec(), on_progress))
.send()
.await
.map_err(|_| Error::Message("Failed to parse media.".to_string()))?;
if !response.status().is_success() {
return Err(Error::Message(format!(
"Failed to parse media: {}",
response.status().as_u16()
)));
}
Ok(response.text().await?)
}
#[derive(Debug, Clone, Default)]
pub struct SliceOptions {
pub segment: Option<[f64; 2]>,
pub split: Option<f64>,
}
pub async fn slice(
authorization: &CredentialsOrApiKey,
content_type: &str,
data: &[u8],
options: SliceOptions,
) -> Result<Option<BytesOrBytesList>> {
let mut request = client()
.post(format!("{}/media/slice", super::get_url()))
.header(
"Content-Type",
if content_type.is_empty() {
"application/octet-stream"
} else {
content_type
},
)
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
);
if let Some([start, end]) = options.segment {
request = request.header("Segment", format!("{start},{end}"));
}
if let Some(split) = options.split.filter(|split| *split != 0.0) {
request = request.header("Split", split.to_string());
}
let response = request.body(data.to_vec()).send().await?;
if !response.status().is_success() {
return Ok(None);
}
let multipart = response
.headers()
.get("Encoding-Size")
.and_then(|value| value.to_str().ok())
.and_then(|value| value.trim().parse::<i64>().ok())
.is_some_and(|size| size > 1);
let mut parts = parse_stream(response, None::<fn(u64)>).await?;
Ok(Some(if multipart {
BytesOrBytesList::BytesList(parts)
} else {
BytesOrBytesList::Bytes(parts.pop().unwrap_or_default())
}))
}
pub async fn duration(
authorization: &CredentialsOrApiKey,
content_type: &str,
data: &[u8],
on_progress: Option<Progress>,
) -> Result<f64> {
let response = client()
.post(format!("{}/media/duration", super::get_url()))
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
)
.header(
"Content-Type",
if content_type.is_empty() {
"application/octet-stream"
} else {
content_type
},
)
.header("Content-Length", data.len())
.body(progress_body(data.to_vec(), on_progress))
.send()
.await
.map_err(|_| Error::Message("Failed to duration media.".to_string()))?;
if !response.status().is_success() {
return Err(Error::Message(format!(
"Failed to duration media: {}",
response.status().as_u16()
)));
}
let text = response.text().await?;
Ok(text.trim().parse::<f64>().unwrap_or(f64::NAN))
}