use crate::store::MemoryError;
use async_trait::async_trait;
#[async_trait]
pub trait LlmClient: Send + Sync {
async fn complete(&self, system: &str, user: &str) -> Result<String, MemoryError>;
async fn structured_output(
&self,
system: &str,
user: &str,
) -> Result<serde_json::Value, MemoryError>;
}
pub struct MockLlmClient {
responses: std::sync::Mutex<Vec<serde_json::Value>>,
}
impl MockLlmClient {
pub fn new(responses: Vec<serde_json::Value>) -> Self {
Self {
responses: std::sync::Mutex::new(responses),
}
}
}
#[async_trait]
impl LlmClient for MockLlmClient {
async fn complete(&self, _system: &str, _user: &str) -> Result<String, MemoryError> {
let mut queue = self
.responses
.lock()
.map_err(|e| MemoryError::Database(format!("mock lock error: {e}")))?;
let val = queue
.pop()
.ok_or_else(|| MemoryError::Database("mock LLM: no more responses".to_string()))?;
Ok(val.as_str().unwrap_or(&val.to_string()).to_string())
}
async fn structured_output(
&self,
_system: &str,
_user: &str,
) -> Result<serde_json::Value, MemoryError> {
let mut queue = self
.responses
.lock()
.map_err(|e| MemoryError::Database(format!("mock lock error: {e}")))?;
queue
.pop()
.ok_or_else(|| MemoryError::Database("mock LLM: no more responses".to_string()))
}
}