use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use chrono::{DateTime, Utc};
use futures_util::future::BoxFuture;
use futures_util::stream::BoxStream;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Map, Value, json};
use super::socket;
use crate::authentification::{CredentialsOrApiKey, PartialApiKey, Profile};
use crate::drive::DriveAuthorization;
use crate::error::Error;
use crate::groups::Group;
use crate::http::{client, ok};
use crate::shared::schema::Schema;
pub use crate::ant::persona::Demographic;
use crate::ant::persona::PartialDemographic;
macro_rules! models {
($(#[$meta:meta])* pub enum $name:ident { $($variant:ident = $value:literal,)* }) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum $name {
$(#[serde(rename = $value)] $variant,)*
}
impl $name {
pub(crate) fn as_str(self) -> &'static str {
match self {
$($name::$variant => $value,)*
}
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
};
}
models! {
pub enum Text {
Auto = "auto",
Gpt5_4 = "gpt-5.4",
Gpt5_4Mini = "gpt-5.4-mini",
Gpt5_4Nano = "gpt-5.4-nano",
Gpt5_4Pro = "gpt-5.4-pro",
Gpt5_5 = "gpt-5.5",
Gpt5_5Pro = "gpt-5.5-pro",
Gpt5_6Sol = "gpt-5.6-sol",
Gpt5_6Terra = "gpt-5.6-terra",
Gpt5_6Luna = "gpt-5.6-luna",
ClaudeSonnet4_6 = "claude-sonnet-4-6",
ClaudeSonnet5 = "claude-sonnet-5",
ClaudeOpus4_6 = "claude-opus-4-6",
ClaudeOpus4_7 = "claude-opus-4-7",
ClaudeOpus4_8 = "claude-opus-4-8",
ClaudeOpus5 = "claude-opus-5",
ClaudeFable5 = "claude-fable-5",
Gemini3_1Pro = "gemini-3.1-pro-preview",
Gemini3Flash = "gemini-3-flash-preview",
Gemini3_1FlashLite = "gemini-3.1-flash-lite",
Gemini3_5Flash = "gemini-3.5-flash",
Grok4_20Reasoning = "grok-4.20-reasoning-latest",
Grok4_20NonReasoning = "grok-4.20-non-reasoning-latest",
Grok4_3 = "grok-4.3-latest",
Grok4_5 = "grok-4.5",
Grok4_6 = "grok-4.6",
GrokCode4_1Fast = "grok-code-fast-1",
}
}
models! {
pub enum Image {
GrokImagineImage = "grok-imagine-image",
GrokImagineImage2 = "grok-imagine-image-2.0",
GrokImagineImageQuality = "grok-imagine-image-quality",
GptImage1_5 = "gpt-image-1.5",
GptImage1Mini = "gpt-image-1-mini",
}
}
models! {
pub enum Video {
GrokImagineVideo = "grok-imagine-video",
GrokImagineVideo1_5 = "grok-imagine-video-1.5",
Sora2 = "sora-2",
Sora2Pro = "sora-2-pro",
}
}
models! {
pub enum Speech {
Gpt4oMiniTts = "gpt-4o-mini-tts",
GrokTts = "grok-tts",
ElevenV3 = "eleven_v3",
ElevenFlashV2_5 = "eleven_flash_v2_5",
}
}
models! {
pub enum Transcription {
GptTranscribe = "gpt-transcribe",
GrokVoiceTranscribe2_0 = "grok-voice-transcribe-2.0",
ScribeV2 = "scribe_v2",
}
}
models! {
pub enum Embedding {
TextEmbedding3Small = "text-embedding-3-small",
TextEmbedding3Large = "text-embedding-3-large",
GeminiEmbedding001 = "gemini-embedding-001",
GeminiEmbedding2Preview = "gemini-embedding-2-preview",
Voyage3 = "voyage-3",
Voyage3Large = "voyage-3-large",
Voyage3Lite = "voyage-3-lite",
VoyageCode3 = "voyage-code-3",
VoyageFinance2 = "voyage-finance-2",
VoyageLaw2 = "voyage-law-2",
}
}
models! {
pub enum Voice {
GptRealtime = "gpt-realtime-2",
GrokVoice = "grok-voice-think-fast-2.0",
ElevenV3 = "eleven_v3_conversational",
}
}
models! {
pub enum Decision {
Jev1_13 = "jev-1.13.0",
Jev1_14 = "jev-1.14.0",
Gpt6Luna = "gpt-6-luna",
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Role {
User,
Model,
System,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct TextPart {
#[serde(default)]
pub text: String,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct HtmlPart {
#[serde(default)]
pub html: String,
}
#[derive(Debug, Clone)]
pub struct File {
pub file: crate::drive::File,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InvocationStatus {
Running,
Success,
Error,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Invocation {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub args: Option<Map<String, Value>>,
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<InvocationStatus>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub result: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Reaction {
pub emoji: String,
pub position: u32,
pub on: bool,
}
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum Part {
Text(TextPart),
Html(HtmlPart),
File(File),
Invocation(Invocation),
Reaction(Reaction),
}
pub(crate) fn file_ref(file: &crate::drive::File) -> Value {
let mut wire = Map::new();
wire.insert("type".to_string(), Value::from("FILE"));
wire.insert("name".to_string(), Value::from(file.name()));
wire.insert(
"contentType".to_string(),
Value::from(file.content_type.as_str()),
);
match &file.authorization {
DriveAuthorization::AccessCode { access_code, .. } => {
wire.insert("accessCode".to_string(), Value::from(access_code.as_str()));
}
DriveAuthorization::Path { path, group, .. }
| DriveAuthorization::Repository { path, group, .. } => {
wire.insert("path".to_string(), Value::from(path.as_str()));
if let Some(group) = group {
wire.insert("group".to_string(), Value::from(group.as_str()));
}
}
}
Value::Object(wire)
}
fn tagged(kind: &str, value: impl Serialize) -> Value {
let mut wire = Map::new();
wire.insert("type".to_string(), Value::from(kind));
if let Ok(Value::Object(fields)) = serde_json::to_value(value) {
wire.extend(fields);
}
Value::Object(wire)
}
impl Part {
pub(crate) fn wire(&self) -> Value {
match self {
Part::Text(part) => tagged("TEXT", part),
Part::Html(part) => tagged("HTML", part),
Part::File(part) => file_ref(&part.file),
Part::Invocation(part) => tagged("INVOCATION", part),
Part::Reaction(part) => tagged("REACTION", part),
}
}
pub(crate) fn from_json(json: &Value) -> Option<Part> {
match json["type"].as_str()? {
"TEXT" => serde_json::from_value(json.clone()).ok().map(Part::Text),
"HTML" => serde_json::from_value(json.clone()).ok().map(Part::Html),
"INVOCATION" => serde_json::from_value(json.clone())
.ok()
.map(Part::Invocation),
"REACTION" => serde_json::from_value(json.clone())
.ok()
.map(Part::Reaction),
_ => None,
}
}
}
impl Serialize for Part {
fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
self.wire().serialize(serializer)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum FunctionBehavior {
Blocking,
NonBlocking,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Function {
pub name: String,
pub parameters: Schema,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub default: Option<Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub example: Option<Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub behavior: Option<FunctionBehavior>,
}
pub type ToolEvaluate =
Arc<dyn Fn(Value) -> BoxFuture<'static, crate::Result<String>> + Send + Sync>;
pub type ToolInstruction = Arc<dyn Fn(&Value) -> String + Send + Sync>;
#[derive(Clone)]
pub struct Tool {
pub function: Function,
pub evaluate: ToolEvaluate,
pub instruction: Option<ToolInstruction>,
}
impl fmt::Debug for Tool {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Tool")
.field("function", &self.function)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum ToolsOrTool {
Tools(Vec<Tool>),
Tool(Tool),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Author {
Profile(Profile),
ApiKey(PartialApiKey),
Model,
Address(String),
}
impl Author {
pub fn from_json(json: &Value) -> Option<Author> {
match json {
Value::String(text) if text == "MODEL" => Some(Author::Model),
Value::String(text) if !text.is_empty() => Some(Author::Address(text.clone())),
Value::Object(_) => Profile::from_json(json).map(Author::Profile).or_else(|| {
json["name"].as_str().map(|name| {
Author::ApiKey(PartialApiKey {
name: Some(name.to_string()),
..Default::default()
})
})
}),
_ => None,
}
}
}
impl Serialize for Author {
fn serialize<S: serde::Serializer>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
match self {
Author::Profile(profile) => profile.serialize(serializer),
Author::ApiKey(key) => json!({ "name": key.name }).serialize(serializer),
Author::Model => serializer.serialize_str("MODEL"),
Author::Address(address) => serializer.serialize_str(address),
}
}
}
impl<'de> Deserialize<'de> for Author {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
let json = Value::deserialize(deserializer)?;
Author::from_json(&json).ok_or_else(|| serde::de::Error::custom("not an author"))
}
}
#[derive(Debug, Clone)]
pub struct Content {
pub parts: Vec<Part>,
pub role: Role,
pub created_at: DateTime<Utc>,
pub author: Option<Author>,
pub model: Option<Text>,
}
impl Content {
pub fn from_json(json: &Value, file: Option<&dyn Fn(&Value) -> Part>) -> Content {
Content {
parts: json["parts"]
.as_array()
.into_iter()
.flatten()
.filter_map(|part| match (part["type"].as_str(), file) {
(Some("FILE"), Some(file)) => Some(file(part)),
(Some("FILE"), None) => None,
_ => Part::from_json(part),
})
.collect(),
role: serde_json::from_value(json["role"].clone()).unwrap_or(Role::User),
created_at: crate::tickets::parse_date(&json["createdAt"]).unwrap_or_default(),
author: Author::from_json(&json["author"]),
model: serde_json::from_value(json["model"].clone()).ok(),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Usage {
#[serde(default)]
pub prompt_tokens: u64,
#[serde(default)]
pub completion_tokens: u64,
}
#[derive(Debug, Clone)]
pub struct TextResponse {
pub content: Content,
pub usage: Usage,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EmbeddingUsage {
#[serde(default)]
pub prompt_tokens: u64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EmbeddingResponse {
pub embedding: Vec<f64>,
pub usage: EmbeddingUsage,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "event", rename_all = "snake_case")]
pub enum StreamEvent {
Text {
#[serde(default)]
delta: String,
},
ToolCall {
id: String,
name: String,
#[serde(default)]
args: Map<String, Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
server: Option<bool>,
},
Done {
#[serde(default)]
usage: Usage,
},
Error {
message: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
code: Option<String>,
},
}
fn pcm() -> String {
"audio/pcm".to_string()
}
fn audio_out<S: Serializer>(delta: &[u8], serializer: S) -> std::result::Result<S::Ok, S::Error> {
serializer.serialize_str(&STANDARD.encode(delta))
}
fn audio_in<'de, D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Vec<u8>, D::Error> {
Ok(socket::audio_delta(&Value::deserialize(deserializer)?))
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "event", rename_all = "snake_case")]
pub enum VoiceEvent {
Audio {
#[serde(serialize_with = "audio_out", deserialize_with = "audio_in")]
delta: Vec<u8>,
#[serde(rename = "contentType", default = "pcm")]
content_type: String,
},
InputTranscript {
#[serde(default)]
delta: String,
},
OutputTranscript {
#[serde(default)]
delta: String,
},
ToolCall {
id: String,
name: String,
#[serde(default)]
args: Map<String, Value>,
},
SpeechStarted,
SpeechStopped,
Done {
#[serde(default)]
usage: Usage,
},
Error {
message: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
code: Option<String>,
},
}
#[derive(Debug, Clone, Default)]
pub struct TextOptions {
pub content: Option<Vec<Content>>,
pub tools: Option<ToolsOrTool>,
pub max_tools: Option<u32>,
pub reasoning: Option<String>,
pub group: Option<Group>,
}
pub async fn generate_text(
authorization: &CredentialsOrApiKey,
model: Text,
options: TextOptions,
) -> crate::Result<BoxStream<'static, StreamEvent>> {
socket::stream_text(
authorization,
"/generative/text",
model.as_str(),
socket::TextOptions {
content: options.content,
tools: options.tools,
max_tools: options.max_tools,
memories: None,
reasoning: options.reasoning,
group: options.group,
},
)
.await
}
pub async fn generate_text_sync(
authorization: &CredentialsOrApiKey,
model: Text,
options: TextOptions,
) -> crate::Result<TextResponse> {
socket::collect_text(generate_text(authorization, model, options).await?).await
}
pub async fn generate_embedding(
authorization: &CredentialsOrApiKey,
model: Embedding,
content: &str,
group: Option<&Group>,
) -> crate::Result<EmbeddingResponse> {
let mut request = client()
.post(format!("{}/generative/embed", super::get_url()))
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
);
if let Some(group) = group {
request = request.header("Group", &group.id);
}
let response = request
.json(&json!({ "model": model, "content": content }))
.send()
.await?;
if !response.status().is_success() {
return Err(Error::Message(format!(
"Failed to generate embedding: {}",
response.status().canonical_reason().unwrap_or_default()
)));
}
Ok(response.json().await?)
}
pub async fn generate_decision(
authorization: &CredentialsOrApiKey,
model: Decision,
content: &[Content],
questions: &HashMap<String, Vec<String>>,
group: Option<&Group>,
) -> crate::Result<HashMap<String, HashMap<String, f64>>> {
let messages = socket::prepare_messages(authorization, content).await?;
let mut request = client()
.post(format!("{}/generative/decision", super::get_url()))
.header(
"Authorization",
match authorization {
CredentialsOrApiKey::Credentials(credentials) => {
format!("Token {}", credentials.token)
}
CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
},
);
if let Some(group) = group {
request = request.header("Group", &group.id);
}
let response = request
.json(&json!({ "model": model, "messages": messages, "questions": questions }))
.send()
.await?;
Ok(ok(response).await?.json().await?)
}
fn media_extra(fields: Vec<(&str, Option<Value>)>) -> Map<String, Value> {
fields
.into_iter()
.filter_map(|(key, value)| value.map(|value| (key.to_string(), value)))
.collect()
}
#[derive(Debug, Clone, Default)]
pub struct ImageOptions {
pub content: Option<Vec<Content>>,
pub size: Option<[u32; 2]>,
pub group: Option<Group>,
}
pub async fn generate_image(
authorization: &CredentialsOrApiKey,
model: Image,
options: ImageOptions,
) -> crate::Result<TextResponse> {
socket::generate_media(
authorization,
"/generative/image",
model.as_str(),
options.content,
options.group,
media_extra(vec![("size", options.size.map(|size| json!(size)))]),
"image/png",
)
.await
}
#[derive(Debug, Clone, Default)]
pub struct VideoOptions {
pub content: Option<Vec<Content>>,
pub duration: Option<f64>,
pub size: Option<[u32; 2]>,
pub group: Option<Group>,
}
pub async fn generate_video(
authorization: &CredentialsOrApiKey,
model: Video,
options: VideoOptions,
) -> crate::Result<TextResponse> {
socket::generate_media(
authorization,
"/generative/video",
model.as_str(),
options.content,
options.group,
media_extra(vec![
("duration", options.duration.map(Value::from)),
("size", options.size.map(|size| json!(size))),
]),
"video/mp4",
)
.await
}
#[derive(Debug, Clone, Default)]
pub struct SpeechOptions {
pub content: Option<Vec<Content>>,
pub voice: Option<Demographic>,
pub group: Option<Group>,
}
pub async fn generate_speech(
authorization: &CredentialsOrApiKey,
model: Speech,
options: SpeechOptions,
) -> crate::Result<TextResponse> {
let voice = options.voice.map(serde_json::to_value).transpose()?;
socket::generate_media(
authorization,
"/generative/speech",
model.as_str(),
options.content,
options.group,
media_extra(vec![("voice", voice)]),
"audio/mpeg",
)
.await
}
#[derive(Debug, Clone, Default)]
pub struct TranscriptionOptions {
pub content: Option<Vec<Content>>,
pub group: Option<Group>,
}
pub async fn generate_transcription(
authorization: &CredentialsOrApiKey,
model: Transcription,
options: TranscriptionOptions,
) -> crate::Result<TextResponse> {
socket::generate_media(
authorization,
"/generative/transcription",
model.as_str(),
options.content,
options.group,
Map::new(),
"application/octet-stream",
)
.await
}
#[derive(Default)]
pub struct VoiceOptions {
pub content: Option<Vec<Content>>,
pub audio: Option<BoxStream<'static, Vec<u8>>>,
pub notes: Option<BoxStream<'static, String>>,
pub tools: Option<ToolsOrTool>,
pub max_tools: Option<u32>,
pub voice: Option<PartialDemographic>,
pub group: Option<Group>,
}
pub async fn generate_voice(
authorization: &CredentialsOrApiKey,
model: Voice,
options: VoiceOptions,
) -> crate::Result<BoxStream<'static, VoiceEvent>> {
socket::stream_voice(
authorization,
"/generative/voice",
model.as_str(),
socket::VoiceOptions {
content: options.content,
audio: options.audio,
notes: options.notes,
tools: options.tools,
max_tools: options.max_tools,
voice: options.voice,
memories: None,
group: options.group,
},
)
.await
}
pub async fn generate_voice_sync(
authorization: &CredentialsOrApiKey,
model: Voice,
options: VoiceOptions,
) -> crate::Result<TextResponse> {
socket::collect_voice(
authorization,
generate_voice(authorization, model, options).await?,
)
.await
}