use std::sync::Arc; use anyhow::Result; use forge_app::ConversationService; use forge_app::domain::{Conversation, ConversationId}; use forge_domain::ConversationRepository; /// Service for managing conversations, including creation, retrieval, and /// updates #[derive(Clone)] pub struct ForgeConversationService { conversation_repository: Arc, } impl ForgeConversationService { /// Creates a new ForgeConversationService with the provided repository pub fn new(repo: Arc) -> Self { Self { conversation_repository: repo } } } #[async_trait::async_trait] impl ConversationService for ForgeConversationService { async fn modify_conversation(&self, id: &ConversationId, f: F) -> Result where F: FnOnce(&mut Conversation) -> T + Send, T: Send, { let mut conversation = self .conversation_repository .get_conversation(id) .await? .ok_or_else(|| forge_app::domain::Error::ConversationNotFound(*id))?; let out = f(&mut conversation); let _ = self .conversation_repository .upsert_conversation(conversation) .await?; Ok(out) } async fn find_conversation(&self, id: &ConversationId) -> Result> { self.conversation_repository.get_conversation(id).await } async fn upsert_conversation(&self, conversation: Conversation) -> Result<()> { let _ = self .conversation_repository .upsert_conversation(conversation) .await?; Ok(()) } async fn get_conversations(&self, limit: Option) -> Result>> { self.conversation_repository .get_all_conversations(limit) .await } async fn last_conversation(&self) -> Result> { self.conversation_repository.get_last_conversation().await } async fn delete_conversation(&self, conversation_id: &ConversationId) -> Result<()> { self.conversation_repository .delete_conversation(conversation_id) .await } }