//! Agent CRUD operations use zclaw_types::{AgentConfig, AgentId, AgentInfo, Event, Result}; use std::sync::Arc; use tokio::sync::Mutex; use super::adapters::AgentInbox; use super::Kernel; impl Kernel { /// Spawn a new agent pub async fn spawn_agent(&self, config: AgentConfig) -> Result { let id = config.id; // Validate capabilities self.capabilities.validate(&config.capabilities)?; // Register in memory self.memory.save_agent(&config).await?; // Register with A2A router for multi-agent messaging (before config is moved) { let profile = Self::agent_config_to_a2a_profile(&config); let rx = self.a2a_router.register_agent(profile).await; self.a2a_inboxes.insert(id, Arc::new(Mutex::new(AgentInbox::new(rx)))); } // Register in registry (consumes config) let name = config.name.clone(); self.registry.register(config); // Emit event self.events.publish(Event::AgentSpawned { agent_id: id, name, }); Ok(id) } /// Kill an agent pub async fn kill_agent(&self, id: &AgentId) -> Result<()> { // Remove from registry self.registry.unregister(id); // Remove from memory self.memory.delete_agent(id).await?; // Unregister from A2A router { self.a2a_router.unregister_agent(id).await; self.a2a_inboxes.remove(id); } // Emit event self.events.publish(Event::AgentTerminated { agent_id: *id, reason: "killed".to_string(), }); Ok(()) } /// Update an existing agent's configuration pub async fn update_agent(&self, config: AgentConfig) -> Result<()> { let id = config.id; // Validate the agent exists if self.registry.get(&id).is_none() { return Err(zclaw_types::ZclawError::NotFound( format!("Agent not found: {}", id) )); } // Validate capabilities self.capabilities.validate(&config.capabilities)?; // Save updated config to memory self.memory.save_agent(&config).await?; // Update in registry (preserves state and message count) self.registry.update(config.clone()); // Emit event self.events.publish(Event::AgentConfigUpdated { agent_id: id, name: config.name.clone(), }); Ok(()) } /// List all agents pub fn list_agents(&self) -> Vec { self.registry.list() } /// Get agent info pub fn get_agent(&self, id: &AgentId) -> Option { self.registry.get_info(id) } /// Get agent config (for export) pub fn get_agent_config(&self, id: &AgentId) -> Option { self.registry.get(id) } }