pub mod llama_cpp;
pub mod ollama;
pub mod openai_compat;
pub mod vllm;
use std::fmt::Display;
use std::future::Future;
use std::pin::Pin;
use serde::{Deserialize, Serialize};
use crate::config::OllamaThinkType;
use crate::image::ImageAttachment;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolDefinition {
pub name: String,
pub description: String,
pub parameters: schemars::Schema,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
pub function: ToolCallFunction,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallFunction {
pub name: String,
pub arguments: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub thought_signature: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
pub content: String,
#[serde(default)]
pub tool_calls: Vec<ToolCall>,
#[serde(default)]
pub thinking: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ChatMessageResponse {
pub message: ChatMessage,
pub done: bool,
#[serde(default)]
pub is_error: bool,
#[serde(default)]
pub usage: Option<TokenUsage>,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct TokenUsage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum Role {
System,
User,
Assistant,
Tool,
}
impl Display for Role {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Role::System => f.write_str("System"),
Role::User => f.write_str("User"),
Role::Assistant => f.write_str("Assistant"),
Role::Tool => f.write_str("Tool"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
pub content: String,
pub tool_calls: Vec<ToolCall>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub images: Vec<ImageAttachment>,
}
impl Message {
pub fn simple(role: Role, content: impl Into<String>) -> Self {
Message {
role,
content: content.into(),
tool_calls: vec![],
images: vec![],
}
}
}
pub trait Provider: Send + Sync {
fn health_check(&self) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>>;
fn list_models(&self) -> Pin<Box<dyn Future<Output = Vec<String>> + Send>>;
fn select_model(&mut self, name: String);
fn current_model(&self) -> Option<String>;
fn chat(
&mut self,
messages: Vec<Message>,
tools: Vec<ToolDefinition>,
) -> Pin<
Box<
dyn Future<Output = Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String>>
+ Send,
>,
>;
fn set_timeout(&mut self, _timeout_secs: u64) {}
fn set_retries(&mut self, _max_retries: u32) {}
fn set_think_type(&mut self, _think_type: OllamaThinkType) {}
}