refactor(crates): kernel/generation module split + DeerFlow optimizations + middleware + dead code cleanup

- Split zclaw-kernel/kernel.rs (1486 lines) into 9 domain modules
- Split zclaw-kernel/generation.rs (1080 lines) into 3 modules
- Add DeerFlow-inspired middleware: DanglingTool, SubagentLimit, ToolError, ToolOutputGuard
- Add PromptBuilder for structured system prompt assembly
- Add FactStore (zclaw-memory) for persistent fact extraction
- Add task builtin tool for agent task management
- Driver improvements: Anthropic/OpenAI extended thinking, Gemini safety settings
- Replace let _ = with proper log::warn! across SaaS handlers
- Remove unused dependency (url) from zclaw-hands
This commit is contained in:
iven
2026-04-03 00:28:03 +08:00
parent 0a04b260a4
commit 52bdafa633
55 changed files with 4130 additions and 1959 deletions

View File

@@ -0,0 +1,113 @@
//! Agent CRUD operations
use zclaw_types::{AgentConfig, AgentId, AgentInfo, Event, Result};
#[cfg(feature = "multi-agent")]
use std::sync::Arc;
#[cfg(feature = "multi-agent")]
use tokio::sync::Mutex;
#[cfg(feature = "multi-agent")]
use super::adapters::AgentInbox;
use super::Kernel;
impl Kernel {
/// Spawn a new agent
pub async fn spawn_agent(&self, config: AgentConfig) -> Result<AgentId> {
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)
#[cfg(feature = "multi-agent")]
{
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
#[cfg(feature = "multi-agent")]
{
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<AgentInfo> {
self.registry.list()
}
/// Get agent info
pub fn get_agent(&self, id: &AgentId) -> Option<AgentInfo> {
self.registry.get_info(id)
}
/// Get agent config (for export)
pub fn get_agent_config(&self, id: &AgentId) -> Option<AgentConfig> {
self.registry.get(id)
}
}