2026-07-12 11:28:39 +07:00
|
|
|
//! In-memory conversation state: message history plus the system prompt and
|
|
|
|
|
//! model parameters used to drive the LLM.
|
|
|
|
|
|
2026-07-11 13:16:10 +07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// A single conversation's message history and generation settings.
|
2026-07-11 13:16:10 +07:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
pub struct Conversation {
|
|
|
|
|
pub messages: Vec<crate::dto::chat::message::ChatMessage>,
|
|
|
|
|
pub system_prompt: String,
|
|
|
|
|
pub session_id: String,
|
|
|
|
|
pub model: String,
|
|
|
|
|
pub max_tokens: u32,
|
|
|
|
|
pub temperature: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Conversation {
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Create an empty conversation with the given system prompt and
|
|
|
|
|
/// session id, using default model/token/temperature settings.
|
2026-07-11 13:16:10 +07:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Append a message to the conversation history.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn push(&mut self, msg: crate::dto::chat::message::ChatMessage) {
|
|
|
|
|
self.messages.push(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// 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.
|
2026-07-11 13:16:10 +07:00
|
|
|
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)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// 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.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn to_api_messages(&self) -> Vec<crate::dto::chat::message::ChatMessage> {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Number of messages in the conversation history (excluding the
|
|
|
|
|
/// synthesized system message).
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.messages.len()
|
|
|
|
|
}
|
|
|
|
|
}
|