Skip to main content

agentrs_multi/
shared_memory.rs

1use std::sync::Arc;
2
3use tokio::sync::RwLock;
4
5use agentrs_core::{Message, Result};
6
7/// Shared conversation state used by multiple agents.
8#[derive(Clone, Default)]
9pub struct SharedConversation {
10    messages: Arc<RwLock<Vec<Message>>>,
11}
12
13impl SharedConversation {
14    /// Creates a new shared conversation.
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    /// Appends a message tagged with its source agent.
20    pub async fn add(&self, agent_name: &str, mut message: Message) -> Result<()> {
21        message.metadata.insert(
22            "agent".to_string(),
23            serde_json::Value::String(agent_name.to_string()),
24        );
25        self.messages.write().await.push(message);
26        Ok(())
27    }
28
29    /// Returns the full shared conversation.
30    pub async fn get_all(&self) -> Vec<Message> {
31        self.messages.read().await.clone()
32    }
33}