use async_trait::async_trait; use serde::{Deserialize, Serialize}; use serde_json::Value; /// Role in a conversation #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "lowercase")] pub enum Role { User, Assistant, System, Tool, } /// A message in the conversation #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Message { pub role: Role, pub content: String, #[serde(skip_serializing_if = "Option::is_none")] pub tool_call_id: Option, } impl Message { pub fn user(content: impl Into) -> Self { Self { role: Role::User, content: content.into(), tool_call_id: None, } } pub fn assistant(content: impl Into) -> Self { Self { role: Role::Assistant, content: content.into(), tool_call_id: None, } } pub fn system(content: impl Into) -> Self { Self { role: Role::System, content: content.into(), tool_call_id: None, } } pub fn tool_result(tool_call_id: impl Into, content: impl Into) -> Self { Self { role: Role::Tool, content: content.into(), tool_call_id: Some(tool_call_id.into()), } } } /// Definition of a tool that can be called by the LLM #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ToolDefinition { pub name: String, pub description: String, pub parameters: Value, } /// A tool call requested by the LLM #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ToolCall { pub id: String, pub name: String, pub parameters: Value, } /// Result of executing a tool #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ToolResult { pub success: bool, pub output: String, } /// Content of a response from the LLM #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(untagged)] pub enum ResponseContent { Text(String), ToolCalls(Vec), } /// Response from the LLM #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Response { pub content: ResponseContent, pub stop_reason: Option, } /// Trait for LLM client implementations #[async_trait] pub trait LlmClient: Send + Sync { async fn chat( &self, messages: Vec, tools: &[ToolDefinition], ) -> anyhow::Result; } pub mod anthropic; pub mod error; pub mod factory; pub mod mock; pub mod ollama; pub mod openai; pub mod retry;