use serde::{Deserialize, Serialize};
#[cfg(feature = "typescript")]
use tsify::Tsify;
use ahash::AHashMap;
use crate::conversation::ContextItem;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub struct MultiAgentContext {
pub session_id: String,
pub agents: Vec<AgentInfo>,
pub shared_context: Vec<ContextItem>,
pub agent_contexts: AHashMap<String, Vec<ContextItem>>,
pub agent_communications: Vec<AgentCommunication>,
pub created_at: chrono::DateTime<chrono::Utc>,
pub updated_at: chrono::DateTime<chrono::Utc>,
}
impl MultiAgentContext {
pub fn new() -> Self {
let now = chrono::Utc::now();
Self {
session_id: uuid::Uuid::new_v4().to_string(),
agents: Vec::new(),
shared_context: Vec::new(),
agent_contexts: AHashMap::new(),
agent_communications: Vec::new(),
created_at: now,
updated_at: now,
}
}
pub fn add_agent(&mut self, agent: AgentInfo) {
self.agents.push(agent.clone());
self.agent_contexts.insert(agent.id, Vec::new());
self.updated_at = chrono::Utc::now();
}
pub fn add_agent_context(&mut self, agent_id: &str, context: ContextItem) {
if let Some(contexts) = self.agent_contexts.get_mut(agent_id) {
contexts.push(context);
self.updated_at = chrono::Utc::now();
}
}
pub fn record_communication(
&mut self,
from_agent: &str,
to_agent: Option<&str>,
message: String,
) {
let communication = AgentCommunication {
from_agent: from_agent.to_string(),
to_agent: to_agent.map(|s| s.to_string()),
message,
timestamp: chrono::Utc::now(),
};
self.agent_communications.push(communication);
self.updated_at = chrono::Utc::now();
}
}
impl Default for MultiAgentContext {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub struct AgentInfo {
pub id: String,
pub name: String,
pub role: String,
pub capabilities: Vec<String>,
pub model: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(Tsify))]
#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
pub struct AgentCommunication {
pub from_agent: String,
pub to_agent: Option<String>,
pub message: String,
pub timestamp: chrono::DateTime<chrono::Utc>,
}