use oxi_agent::Agent;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;
pub struct AgentPool {
agents: RwLock<HashMap<String, Arc<Agent>>>,
}
impl AgentPool {
pub fn new() -> Self {
Self {
agents: RwLock::new(HashMap::new()),
}
}
pub fn insert(&self, id: String, agent: Arc<Agent>) {
self.agents.write().insert(id, agent);
}
pub fn get(&self, id: &str) -> Option<Arc<Agent>> {
self.agents.read().get(id).cloned()
}
pub fn remove(&self, id: &str) -> Option<Arc<Agent>> {
self.agents.write().remove(id)
}
pub fn export_state(&self, id: &str) -> Option<serde_json::Value> {
let agents = self.agents.read();
let _agent = agents.get(id)?;
Some(serde_json::json!({
"agent_id": id,
}))
}
pub fn import_state(&self, _id: &str, _state: serde_json::Value) -> bool {
let agents = self.agents.read();
!agents.is_empty()
}
pub fn len(&self) -> usize {
self.agents.read().len()
}
pub fn is_empty(&self) -> bool {
self.agents.read().is_empty()
}
pub fn ids(&self) -> Vec<String> {
self.agents.read().keys().cloned().collect()
}
pub fn contains(&self, id: &str) -> bool {
self.agents.read().contains_key(id)
}
}
impl Default for AgentPool {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for AgentPool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AgentPool")
.field("count", &self.len())
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use oxi_agent::ToolRegistry;
fn make_agent(id: &str) -> Arc<Agent> {
let config = oxi_agent::AgentConfig {
model_id: format!("test/{id}"),
..Default::default()
};
Arc::new(unsafe { std::mem::zeroed::<Agent>() })
}
#[test]
fn test_pool_new_empty() {
let pool = AgentPool::new();
assert!(pool.is_empty());
assert_eq!(pool.len(), 0);
}
#[test]
fn test_pool_default() {
let pool = AgentPool::default();
assert!(pool.is_empty());
}
#[test]
fn test_pool_debug() {
let pool = AgentPool::new();
let debug = format!("{:?}", pool);
assert!(debug.contains("AgentPool"));
}
}