feat: add internal ZCLAW kernel crates to git tracking

This commit is contained in:
iven
2026-03-22 09:26:36 +08:00
parent d72c0f7161
commit 58cd24f85b
36 changed files with 10298 additions and 0 deletions

View File

@@ -0,0 +1,180 @@
//! Kernel - central coordinator
use std::sync::Arc;
use tokio::sync::{broadcast, mpsc};
use zclaw_types::{AgentConfig, AgentId, AgentInfo, Event, Result};
use crate::registry::AgentRegistry;
use crate::capabilities::CapabilityManager;
use crate::events::EventBus;
use crate::config::KernelConfig;
use zclaw_memory::MemoryStore;
use zclaw_runtime::{AgentLoop, LlmDriver, ToolRegistry};
/// The ZCLAW Kernel
pub struct Kernel {
config: KernelConfig,
registry: AgentRegistry,
capabilities: CapabilityManager,
events: EventBus,
memory: Arc<MemoryStore>,
driver: Arc<dyn LlmDriver>,
}
impl Kernel {
/// Boot the kernel with the given configuration
pub async fn boot(config: KernelConfig) -> Result<Self> {
// Initialize memory store
let memory = Arc::new(MemoryStore::new(&config.database_url).await?);
// Initialize driver based on config
let driver = config.create_driver()?;
// Initialize subsystems
let registry = AgentRegistry::new();
let capabilities = CapabilityManager::new();
let events = EventBus::new();
// Restore persisted agents
let persisted = memory.list_agents().await?;
for agent in persisted {
registry.register(agent);
}
Ok(Self {
config,
registry,
capabilities,
events,
memory,
driver,
})
}
/// Create a tool registry with built-in tools
fn create_tool_registry(&self) -> ToolRegistry {
let mut tools = ToolRegistry::new();
zclaw_runtime::tool::builtin::register_builtin_tools(&mut tools);
tools
}
/// 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 in registry
self.registry.register(config);
// Emit event
self.events.publish(Event::AgentSpawned {
agent_id: id,
name: self.registry.get(&id).map(|a| a.name.clone()).unwrap_or_default(),
});
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?;
// Emit event
self.events.publish(Event::AgentTerminated {
agent_id: *id,
reason: "killed".to_string(),
});
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)
}
/// Send a message to an agent
pub async fn send_message(&self, agent_id: &AgentId, message: String) -> Result<MessageResponse> {
let _agent = self.registry.get(agent_id)
.ok_or_else(|| zclaw_types::ZclawError::NotFound(format!("Agent not found: {}", agent_id)))?;
// Create or get session
let session_id = self.memory.create_session(agent_id).await?;
// Create agent loop
let tools = self.create_tool_registry();
let loop_runner = AgentLoop::new(
*agent_id,
self.driver.clone(),
tools,
self.memory.clone(),
);
// Run the loop
let result = loop_runner.run(session_id, message).await?;
Ok(MessageResponse {
content: result.response,
input_tokens: result.input_tokens,
output_tokens: result.output_tokens,
})
}
/// Send a message with streaming
pub async fn send_message_stream(
&self,
agent_id: &AgentId,
message: String,
) -> Result<mpsc::Receiver<zclaw_runtime::LoopEvent>> {
let _agent = self.registry.get(agent_id)
.ok_or_else(|| zclaw_types::ZclawError::NotFound(format!("Agent not found: {}", agent_id)))?;
// Create session
let session_id = self.memory.create_session(agent_id).await?;
// Create agent loop
let tools = self.create_tool_registry();
let loop_runner = AgentLoop::new(
*agent_id,
self.driver.clone(),
tools,
self.memory.clone(),
);
// Run with streaming
loop_runner.run_streaming(session_id, message).await
}
/// Subscribe to events
pub fn subscribe(&self) -> broadcast::Receiver<Event> {
self.events.subscribe()
}
/// Shutdown the kernel
pub async fn shutdown(&self) -> Result<()> {
self.events.publish(Event::KernelShutdown);
Ok(())
}
}
/// Response from sending a message
#[derive(Debug, Clone)]
pub struct MessageResponse {
pub content: String,
pub input_tokens: u32,
pub output_tokens: u32,
}