//! In-memory conversation state: message history plus the system prompt and //! model parameters used to drive the LLM. use serde::{Deserialize, Serialize}; /// A single conversation's message history and generation settings. #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Conversation { pub messages: Vec, pub system_prompt: String, pub session_id: String, pub model: String, pub max_tokens: u32, pub temperature: f32, } impl Conversation { /// Create an empty conversation with the given system prompt and /// session id, using default model/token/temperature settings. pub fn new(system_prompt: String, session_id: String) -> Self { Conversation { messages: Vec::new(), system_prompt, session_id, model: "anthropic/claude-opus-4-8".to_string(), max_tokens: 8192, temperature: 0.7, } } /// Append a message to the conversation history. pub fn push(&mut self, msg: crate::dto::chat::message::ChatMessage) { self.messages.push(msg); } /// Replace the system prompt and strip any prior `System`-role /// messages from history. /// /// Why: the system prompt is re-injected fresh at request time via /// `to_api_messages`, so stale `System` messages in `self.messages` /// would be redundant/conflicting if left in place. pub fn rebuild_system(&mut self, new_prompt: String) { self.system_prompt = new_prompt; self.messages.retain(|m| { !matches!(m.role, crate::dto::chat::message::Role::System) }); } /// Build the message list to send to the LLM API, with the system /// prompt prepended. /// /// Return: a new `Vec` (clone of history) with a synthesized system /// message at index 0. pub fn to_api_messages(&self) -> Vec { let mut msgs = Vec::with_capacity(self.messages.len() + 1); msgs.push(crate::dto::chat::message::ChatMessage::system(&self.system_prompt)); msgs.extend(self.messages.iter().cloned()); msgs } /// Number of messages in the conversation history (excluding the /// synthesized system message). pub fn len(&self) -> usize { self.messages.len() } }