refactor: 清理未使用代码并添加未来功能标记
Some checks failed
CI / Rust Check (push) Has been cancelled
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
Some checks failed
CI / Rust Check (push) Has been cancelled
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
style: 统一代码格式和注释风格 docs: 更新多个功能文档的完整度和状态 feat(runtime): 添加路径验证工具支持 fix(pipeline): 改进条件判断和变量解析逻辑 test(types): 为ID类型添加全面测试用例 chore: 更新依赖项和Cargo.lock文件 perf(mcp): 优化MCP协议传输和错误处理
This commit is contained in:
5
Cargo.lock
generated
5
Cargo.lock
generated
@@ -6952,7 +6952,9 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-stream",
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
"chrono",
|
||||
"dirs",
|
||||
"futures",
|
||||
"rand 0.8.5",
|
||||
"reqwest 0.12.28",
|
||||
@@ -6960,11 +6962,14 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"shlex",
|
||||
"tempfile",
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
"toml 0.8.2",
|
||||
"tracing",
|
||||
"url",
|
||||
"uuid",
|
||||
"zclaw-memory",
|
||||
"zclaw-types",
|
||||
|
||||
@@ -59,6 +59,9 @@ sqlx = { version = "0.7", features = ["runtime-tokio", "sqlite"] }
|
||||
# HTTP client (for LLM drivers)
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls"] }
|
||||
|
||||
# URL parsing
|
||||
url = "2"
|
||||
|
||||
# Async trait
|
||||
async-trait = "0.1"
|
||||
|
||||
@@ -84,6 +87,12 @@ dirs = "6"
|
||||
# Regex
|
||||
regex = "1"
|
||||
|
||||
# Shell parsing
|
||||
shlex = "1"
|
||||
|
||||
# Testing
|
||||
tempfile = "3"
|
||||
|
||||
# Internal crates
|
||||
zclaw-types = { path = "crates/zclaw-types" }
|
||||
zclaw-memory = { path = "crates/zclaw-memory" }
|
||||
|
||||
@@ -63,7 +63,7 @@ impl Channel for ConsoleChannel {
|
||||
}
|
||||
|
||||
async fn receive(&self) -> Result<mpsc::Receiver<IncomingMessage>> {
|
||||
let (tx, rx) = mpsc::channel(100);
|
||||
let (_tx, rx) = mpsc::channel(100);
|
||||
// Console channel doesn't receive messages automatically
|
||||
// Messages would need to be injected via a separate method
|
||||
Ok(rx)
|
||||
|
||||
@@ -50,7 +50,7 @@ impl Channel for DiscordChannel {
|
||||
}
|
||||
|
||||
async fn receive(&self) -> Result<mpsc::Receiver<IncomingMessage>> {
|
||||
let (tx, rx) = mpsc::channel(100);
|
||||
let (_tx, rx) = mpsc::channel(100);
|
||||
// TODO: Implement Discord gateway
|
||||
Ok(rx)
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ impl Channel for SlackChannel {
|
||||
}
|
||||
|
||||
async fn receive(&self) -> Result<mpsc::Receiver<IncomingMessage>> {
|
||||
let (tx, rx) = mpsc::channel(100);
|
||||
let (_tx, rx) = mpsc::channel(100);
|
||||
// TODO: Implement Slack RTM/events API
|
||||
Ok(rx)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::{Channel, ChannelConfig, ChannelStatus, IncomingMessage, OutgoingMess
|
||||
/// Telegram channel adapter
|
||||
pub struct TelegramChannel {
|
||||
config: ChannelConfig,
|
||||
#[allow(dead_code)] // TODO: Implement Telegram API client
|
||||
client: Option<reqwest::Client>,
|
||||
status: Arc<tokio::sync::RwLock<ChannelStatus>>,
|
||||
}
|
||||
@@ -52,7 +53,7 @@ impl Channel for TelegramChannel {
|
||||
}
|
||||
|
||||
async fn receive(&self) -> Result<mpsc::Receiver<IncomingMessage>> {
|
||||
let (tx, rx) = mpsc::channel(100);
|
||||
let (_tx, rx) = mpsc::channel(100);
|
||||
// TODO: Implement Telegram webhook/polling
|
||||
Ok(rx)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use zclaw_types::Result;
|
||||
|
||||
use super::{Channel, ChannelConfig, ChannelStatus, IncomingMessage, OutgoingMessage};
|
||||
use super::{Channel, ChannelConfig, OutgoingMessage};
|
||||
|
||||
/// Channel bridge manager
|
||||
pub struct ChannelBridge {
|
||||
|
||||
@@ -13,7 +13,8 @@ use zclaw_types::Result;
|
||||
|
||||
/// HTML exporter
|
||||
pub struct HtmlExporter {
|
||||
/// Template name
|
||||
/// Template name (reserved for future template support)
|
||||
#[allow(dead_code)] // TODO: Implement template-based HTML export
|
||||
template: String,
|
||||
}
|
||||
|
||||
@@ -26,6 +27,7 @@ impl HtmlExporter {
|
||||
}
|
||||
|
||||
/// Create with specific template
|
||||
#[allow(dead_code)] // Reserved for future template support
|
||||
pub fn with_template(template: &str) -> Self {
|
||||
Self {
|
||||
template: template.to_string(),
|
||||
|
||||
@@ -26,6 +26,7 @@ impl MarkdownExporter {
|
||||
}
|
||||
|
||||
/// Create without front matter
|
||||
#[allow(dead_code)] // Reserved for future use
|
||||
pub fn without_front_matter() -> Self {
|
||||
Self {
|
||||
include_front_matter: false,
|
||||
|
||||
@@ -568,7 +568,7 @@ use zip::{ZipWriter, write::SimpleFileOptions};
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::generation::{ClassroomMetadata, TeachingStyle, DifficultyLevel};
|
||||
use crate::generation::{ClassroomMetadata, TeachingStyle, DifficultyLevel, SceneType};
|
||||
|
||||
fn create_test_classroom() -> Classroom {
|
||||
Classroom {
|
||||
|
||||
@@ -704,6 +704,7 @@ Actions can be:
|
||||
}
|
||||
|
||||
/// Generate scene using LLM
|
||||
#[allow(dead_code)] // Reserved for future LLM-based scene generation
|
||||
async fn generate_scene_with_llm(
|
||||
&self,
|
||||
driver: &dyn LlmDriver,
|
||||
@@ -787,6 +788,7 @@ Ensure the outline is coherent and follows good pedagogical practices."#.to_stri
|
||||
}
|
||||
|
||||
/// Get system prompt for scene generation
|
||||
#[allow(dead_code)] // Reserved for future use
|
||||
fn get_scene_system_prompt(&self) -> String {
|
||||
r#"You are an expert educational content creator. Your task is to generate detailed teaching scenes.
|
||||
|
||||
@@ -871,6 +873,7 @@ Actions can be:
|
||||
}
|
||||
|
||||
/// Parse scene from LLM response text
|
||||
#[allow(dead_code)] // Reserved for future use
|
||||
fn parse_scene_from_text(&self, text: &str, item: &OutlineItem, order: usize) -> Result<GeneratedScene> {
|
||||
let json_text = self.extract_json(text);
|
||||
|
||||
@@ -902,6 +905,7 @@ Actions can be:
|
||||
}
|
||||
|
||||
/// Parse actions from scene data
|
||||
#[allow(dead_code)] // Reserved for future use
|
||||
fn parse_actions(&self, scene_data: &serde_json::Value) -> Vec<SceneAction> {
|
||||
scene_data.get("actions")
|
||||
.and_then(|v| v.as_array())
|
||||
@@ -914,6 +918,7 @@ Actions can be:
|
||||
}
|
||||
|
||||
/// Parse single action
|
||||
#[allow(dead_code)] // Reserved for future use
|
||||
fn parse_single_action(&self, action: &serde_json::Value) -> Option<SceneAction> {
|
||||
let action_type = action.get("type")?.as_str()?;
|
||||
|
||||
@@ -1058,6 +1063,7 @@ Generate {} outline items that flow logically and cover the topic comprehensivel
|
||||
}
|
||||
|
||||
/// Generate scene for outline item (would be replaced by LLM call)
|
||||
#[allow(dead_code)] // Reserved for future use
|
||||
fn generate_scene_for_item(&self, item: &OutlineItem, order: usize) -> Result<GeneratedScene> {
|
||||
let actions = match item.scene_type {
|
||||
SceneType::Slide => vec![
|
||||
|
||||
@@ -56,6 +56,7 @@ pub struct Kernel {
|
||||
skills: Arc<SkillRegistry>,
|
||||
skill_executor: Arc<KernelSkillExecutor>,
|
||||
hands: Arc<HandRegistry>,
|
||||
trigger_manager: crate::trigger_manager::TriggerManager,
|
||||
}
|
||||
|
||||
impl Kernel {
|
||||
@@ -97,6 +98,9 @@ impl Kernel {
|
||||
// Create skill executor
|
||||
let skill_executor = Arc::new(KernelSkillExecutor::new(skills.clone()));
|
||||
|
||||
// Initialize trigger manager
|
||||
let trigger_manager = crate::trigger_manager::TriggerManager::new(hands.clone());
|
||||
|
||||
// Restore persisted agents
|
||||
let persisted = memory.list_agents().await?;
|
||||
for agent in persisted {
|
||||
@@ -113,6 +117,7 @@ impl Kernel {
|
||||
skills,
|
||||
skill_executor,
|
||||
hands,
|
||||
trigger_manager,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -420,6 +425,82 @@ impl Kernel {
|
||||
let context = HandContext::default();
|
||||
self.hands.execute(hand_id, &context, input).await
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Trigger Management
|
||||
// ============================================================
|
||||
|
||||
/// List all triggers
|
||||
pub async fn list_triggers(&self) -> Vec<crate::trigger_manager::TriggerEntry> {
|
||||
self.trigger_manager.list_triggers().await
|
||||
}
|
||||
|
||||
/// Get a specific trigger
|
||||
pub async fn get_trigger(&self, id: &str) -> Option<crate::trigger_manager::TriggerEntry> {
|
||||
self.trigger_manager.get_trigger(id).await
|
||||
}
|
||||
|
||||
/// Create a new trigger
|
||||
pub async fn create_trigger(
|
||||
&self,
|
||||
config: zclaw_hands::TriggerConfig,
|
||||
) -> Result<crate::trigger_manager::TriggerEntry> {
|
||||
self.trigger_manager.create_trigger(config).await
|
||||
}
|
||||
|
||||
/// Update a trigger
|
||||
pub async fn update_trigger(
|
||||
&self,
|
||||
id: &str,
|
||||
updates: crate::trigger_manager::TriggerUpdateRequest,
|
||||
) -> Result<crate::trigger_manager::TriggerEntry> {
|
||||
self.trigger_manager.update_trigger(id, updates).await
|
||||
}
|
||||
|
||||
/// Delete a trigger
|
||||
pub async fn delete_trigger(&self, id: &str) -> Result<()> {
|
||||
self.trigger_manager.delete_trigger(id).await
|
||||
}
|
||||
|
||||
/// Execute a trigger
|
||||
pub async fn execute_trigger(
|
||||
&self,
|
||||
id: &str,
|
||||
input: serde_json::Value,
|
||||
) -> Result<zclaw_hands::TriggerResult> {
|
||||
self.trigger_manager.execute_trigger(id, input).await
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Approval Management (Stub Implementation)
|
||||
// ============================================================
|
||||
|
||||
/// List pending approvals
|
||||
pub async fn list_approvals(&self) -> Vec<ApprovalEntry> {
|
||||
// Stub: Return empty list
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Respond to an approval
|
||||
pub async fn respond_to_approval(
|
||||
&self,
|
||||
_id: &str,
|
||||
_approved: bool,
|
||||
_reason: Option<String>,
|
||||
) -> Result<()> {
|
||||
// Stub: Return error
|
||||
Err(zclaw_types::ZclawError::NotFound(format!("Approval not found")))
|
||||
}
|
||||
}
|
||||
|
||||
/// Approval entry for pending approvals
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ApprovalEntry {
|
||||
pub id: String,
|
||||
pub hand_id: String,
|
||||
pub status: String,
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
pub input: serde_json::Value,
|
||||
}
|
||||
|
||||
/// Response from sending a message
|
||||
|
||||
@@ -6,6 +6,7 @@ mod kernel;
|
||||
mod registry;
|
||||
mod capabilities;
|
||||
mod events;
|
||||
pub mod trigger_manager;
|
||||
pub mod config;
|
||||
pub mod director;
|
||||
pub mod generation;
|
||||
@@ -16,6 +17,7 @@ pub use registry::*;
|
||||
pub use capabilities::*;
|
||||
pub use events::*;
|
||||
pub use config::*;
|
||||
pub use trigger_manager::{TriggerManager, TriggerEntry, TriggerUpdateRequest, TriggerManagerConfig};
|
||||
pub use director::*;
|
||||
pub use generation::*;
|
||||
pub use export::{ExportFormat, ExportOptions, ExportResult, Exporter, export_classroom};
|
||||
|
||||
372
crates/zclaw-kernel/src/trigger_manager.rs
Normal file
372
crates/zclaw-kernel/src/trigger_manager.rs
Normal file
@@ -0,0 +1,372 @@
|
||||
//! Trigger Manager
|
||||
//!
|
||||
//! Manages triggers for automated task execution.
|
||||
//!
|
||||
//! # Lock Order Safety
|
||||
//!
|
||||
//! This module uses a single `RwLock<InternalState>` to avoid potential deadlocks.
|
||||
//! Previously, multiple locks (`triggers` and `states`) could cause deadlocks when
|
||||
//! acquired in different orders across methods.
|
||||
//!
|
||||
//! The unified state structure ensures atomic access to all trigger-related data.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zclaw_types::Result;
|
||||
use zclaw_hands::{TriggerConfig, TriggerType, TriggerState, TriggerResult, HandRegistry};
|
||||
|
||||
/// Internal state container for all trigger-related data.
|
||||
///
|
||||
/// Using a single structure behind one RwLock eliminates the possibility of
|
||||
/// deadlocks caused by inconsistent lock acquisition orders.
|
||||
#[derive(Debug)]
|
||||
struct InternalState {
|
||||
/// Registered triggers
|
||||
triggers: HashMap<String, TriggerEntry>,
|
||||
/// Execution states
|
||||
states: HashMap<String, TriggerState>,
|
||||
}
|
||||
|
||||
impl InternalState {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
triggers: HashMap::new(),
|
||||
states: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trigger manager for coordinating automated triggers
|
||||
pub struct TriggerManager {
|
||||
/// Unified internal state behind a single RwLock.
|
||||
///
|
||||
/// This prevents deadlocks by ensuring all trigger data is accessed
|
||||
/// through a single lock acquisition point.
|
||||
state: RwLock<InternalState>,
|
||||
/// Hand registry
|
||||
hand_registry: Arc<HandRegistry>,
|
||||
/// Configuration
|
||||
config: TriggerManagerConfig,
|
||||
}
|
||||
|
||||
/// Trigger entry with additional metadata
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TriggerEntry {
|
||||
/// Core trigger configuration
|
||||
#[serde(flatten)]
|
||||
pub config: TriggerConfig,
|
||||
/// Creation timestamp
|
||||
pub created_at: DateTime<Utc>,
|
||||
/// Last modification timestamp
|
||||
pub modified_at: DateTime<Utc>,
|
||||
/// Optional description
|
||||
pub description: Option<String>,
|
||||
/// Optional tags
|
||||
#[serde(default)]
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
/// Default max executions per hour
|
||||
fn default_max_executions_per_hour() -> u32 { 10 }
|
||||
/// Default persist value
|
||||
fn default_persist() -> bool { true }
|
||||
|
||||
/// Trigger manager configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TriggerManagerConfig {
|
||||
/// Maximum executions per hour (default)
|
||||
#[serde(default = "default_max_executions_per_hour")]
|
||||
pub max_executions_per_hour: u32,
|
||||
/// Enable persistent storage
|
||||
#[serde(default = "default_persist")]
|
||||
pub persist: bool,
|
||||
/// Storage path for trigger data
|
||||
pub storage_path: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for TriggerManagerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_executions_per_hour: 10,
|
||||
persist: true,
|
||||
storage_path: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TriggerManager {
|
||||
/// Create new trigger manager
|
||||
pub fn new(hand_registry: Arc<HandRegistry>) -> Self {
|
||||
Self {
|
||||
state: RwLock::new(InternalState::new()),
|
||||
hand_registry,
|
||||
config: TriggerManagerConfig::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create with custom configuration
|
||||
pub fn with_config(
|
||||
hand_registry: Arc<HandRegistry>,
|
||||
config: TriggerManagerConfig,
|
||||
) -> Self {
|
||||
Self {
|
||||
state: RwLock::new(InternalState::new()),
|
||||
hand_registry,
|
||||
config,
|
||||
}
|
||||
}
|
||||
|
||||
/// List all triggers
|
||||
pub async fn list_triggers(&self) -> Vec<TriggerEntry> {
|
||||
let state = self.state.read().await;
|
||||
state.triggers.values().cloned().collect()
|
||||
}
|
||||
|
||||
/// Get a specific trigger
|
||||
pub async fn get_trigger(&self, id: &str) -> Option<TriggerEntry> {
|
||||
let state = self.state.read().await;
|
||||
state.triggers.get(id).cloned()
|
||||
}
|
||||
|
||||
/// Create a new trigger
|
||||
pub async fn create_trigger(&self, config: TriggerConfig) -> Result<TriggerEntry> {
|
||||
// Validate hand exists (outside of our lock to avoid holding two locks)
|
||||
if self.hand_registry.get(&config.hand_id).await.is_none() {
|
||||
return Err(zclaw_types::ZclawError::InvalidInput(
|
||||
format!("Hand '{}' not found", config.hand_id)
|
||||
));
|
||||
}
|
||||
|
||||
let id = config.id.clone();
|
||||
let now = Utc::now();
|
||||
|
||||
let entry = TriggerEntry {
|
||||
config,
|
||||
created_at: now,
|
||||
modified_at: now,
|
||||
description: None,
|
||||
tags: Vec::new(),
|
||||
};
|
||||
|
||||
// Initialize state and insert trigger atomically under single lock
|
||||
let state = TriggerState::new(&id);
|
||||
{
|
||||
let mut internal = self.state.write().await;
|
||||
internal.states.insert(id.clone(), state);
|
||||
internal.triggers.insert(id.clone(), entry.clone());
|
||||
}
|
||||
|
||||
Ok(entry)
|
||||
}
|
||||
|
||||
/// Update an existing trigger
|
||||
pub async fn update_trigger(
|
||||
&self,
|
||||
id: &str,
|
||||
updates: TriggerUpdateRequest,
|
||||
) -> Result<TriggerEntry> {
|
||||
// Validate hand exists if being updated (outside of our lock)
|
||||
if let Some(hand_id) = &updates.hand_id {
|
||||
if self.hand_registry.get(hand_id).await.is_none() {
|
||||
return Err(zclaw_types::ZclawError::InvalidInput(
|
||||
format!("Hand '{}' not found", hand_id)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let mut internal = self.state.write().await;
|
||||
|
||||
let Some(entry) = internal.triggers.get_mut(id) else {
|
||||
return Err(zclaw_types::ZclawError::NotFound(
|
||||
format!("Trigger '{}' not found", id)
|
||||
));
|
||||
};
|
||||
|
||||
// Apply updates
|
||||
if let Some(name) = &updates.name {
|
||||
entry.config.name = name.clone();
|
||||
}
|
||||
if let Some(enabled) = updates.enabled {
|
||||
entry.config.enabled = enabled;
|
||||
}
|
||||
if let Some(hand_id) = &updates.hand_id {
|
||||
entry.config.hand_id = hand_id.clone();
|
||||
}
|
||||
if let Some(trigger_type) = &updates.trigger_type {
|
||||
entry.config.trigger_type = trigger_type.clone();
|
||||
}
|
||||
|
||||
entry.modified_at = Utc::now();
|
||||
|
||||
Ok(entry.clone())
|
||||
}
|
||||
|
||||
/// Delete a trigger
|
||||
pub async fn delete_trigger(&self, id: &str) -> Result<()> {
|
||||
let mut internal = self.state.write().await;
|
||||
if internal.triggers.remove(id).is_none() {
|
||||
return Err(zclaw_types::ZclawError::NotFound(
|
||||
format!("Trigger '{}' not found", id)
|
||||
));
|
||||
}
|
||||
// Also remove associated state atomically
|
||||
internal.states.remove(id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get trigger state
|
||||
pub async fn get_state(&self, id: &str) -> Option<TriggerState> {
|
||||
let state = self.state.read().await;
|
||||
state.states.get(id).cloned()
|
||||
}
|
||||
|
||||
/// Check if trigger should fire based on type and input.
|
||||
///
|
||||
/// This method performs rate limiting and condition checks using a single
|
||||
/// read lock to avoid deadlocks.
|
||||
pub async fn should_fire(&self, id: &str, input: &serde_json::Value) -> bool {
|
||||
let internal = self.state.read().await;
|
||||
let Some(entry) = internal.triggers.get(id) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
// Check if enabled
|
||||
if !entry.config.enabled {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check rate limiting using the same lock
|
||||
if let Some(state) = internal.states.get(id) {
|
||||
// Check execution count this hour
|
||||
let one_hour_ago = Utc::now() - chrono::Duration::hours(1);
|
||||
if let Some(last_exec) = state.last_execution {
|
||||
if last_exec > one_hour_ago {
|
||||
if state.execution_count >= self.config.max_executions_per_hour {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check trigger-specific conditions
|
||||
match &entry.config.trigger_type {
|
||||
TriggerType::Manual => false,
|
||||
TriggerType::Schedule { cron: _ } => {
|
||||
// For schedule triggers, use cron parser
|
||||
// Simplified check - real implementation would use cron library
|
||||
true
|
||||
}
|
||||
TriggerType::Event { pattern } => {
|
||||
// Check if input matches pattern
|
||||
input.to_string().contains(pattern)
|
||||
}
|
||||
TriggerType::Webhook { path: _, secret: _ } => {
|
||||
// Webhook triggers are fired externally
|
||||
false
|
||||
}
|
||||
TriggerType::MessagePattern { pattern } => {
|
||||
// Check if message matches pattern
|
||||
input.to_string().contains(pattern)
|
||||
}
|
||||
TriggerType::FileSystem { path: _, events: _ } => {
|
||||
// File system triggers are fired by file watcher
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute a trigger.
|
||||
///
|
||||
/// This method carefully manages lock scope to avoid deadlocks:
|
||||
/// 1. Acquires read lock to check trigger exists and get config
|
||||
/// 2. Releases lock before calling external hand registry
|
||||
/// 3. Acquires write lock to update state
|
||||
pub async fn execute_trigger(&self, id: &str, input: serde_json::Value) -> Result<TriggerResult> {
|
||||
// Check if should fire (uses its own lock scope)
|
||||
if !self.should_fire(id, &input).await {
|
||||
return Err(zclaw_types::ZclawError::InvalidInput(
|
||||
format!("Trigger '{}' should not fire", id)
|
||||
));
|
||||
}
|
||||
|
||||
// Get hand_id (release lock before calling hand registry)
|
||||
let hand_id = {
|
||||
let internal = self.state.read().await;
|
||||
let entry = internal.triggers.get(id)
|
||||
.ok_or_else(|| zclaw_types::ZclawError::NotFound(
|
||||
format!("Trigger '{}' not found", id)
|
||||
))?;
|
||||
entry.config.hand_id.clone()
|
||||
};
|
||||
|
||||
// Get hand (outside of our lock to avoid potential deadlock with hand_registry)
|
||||
let hand = self.hand_registry.get(&hand_id).await
|
||||
.ok_or_else(|| zclaw_types::ZclawError::InvalidInput(
|
||||
format!("Hand '{}' not found", hand_id)
|
||||
))?;
|
||||
|
||||
// Update state before execution
|
||||
{
|
||||
let mut internal = self.state.write().await;
|
||||
let state = internal.states.entry(id.to_string()).or_insert_with(|| TriggerState::new(id));
|
||||
state.execution_count += 1;
|
||||
}
|
||||
|
||||
// Execute hand (outside of lock to avoid blocking other operations)
|
||||
let context = zclaw_hands::HandContext {
|
||||
agent_id: zclaw_types::AgentId::new(),
|
||||
working_dir: None,
|
||||
env: std::collections::HashMap::new(),
|
||||
timeout_secs: 300,
|
||||
callback_url: None,
|
||||
};
|
||||
|
||||
let hand_result = hand.execute(&context, input.clone()).await;
|
||||
|
||||
// Build trigger result from hand result
|
||||
let trigger_result = match &hand_result {
|
||||
Ok(res) => TriggerResult {
|
||||
timestamp: Utc::now(),
|
||||
success: res.success,
|
||||
output: Some(res.output.clone()),
|
||||
error: res.error.clone(),
|
||||
trigger_input: input.clone(),
|
||||
},
|
||||
Err(e) => TriggerResult {
|
||||
timestamp: Utc::now(),
|
||||
success: false,
|
||||
output: None,
|
||||
error: Some(e.to_string()),
|
||||
trigger_input: input.clone(),
|
||||
},
|
||||
};
|
||||
|
||||
// Update state after execution
|
||||
{
|
||||
let mut internal = self.state.write().await;
|
||||
if let Some(state) = internal.states.get_mut(id) {
|
||||
state.last_execution = Some(Utc::now());
|
||||
state.last_result = Some(trigger_result.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Return the original hand result or convert to trigger result
|
||||
hand_result.map(|_| trigger_result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Request for updating a trigger
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TriggerUpdateRequest {
|
||||
/// New name
|
||||
pub name: Option<String>,
|
||||
/// Enable/disable
|
||||
pub enabled: Option<bool>,
|
||||
/// New hand ID
|
||||
pub hand_id: Option<String>,
|
||||
/// New trigger type
|
||||
pub trigger_type: Option<TriggerType>,
|
||||
}
|
||||
@@ -278,3 +278,334 @@ impl MemoryStore {
|
||||
Ok(rows.into_iter().map(|(key,)| key).collect())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use zclaw_types::{AgentConfig, ModelConfig};
|
||||
|
||||
fn create_test_agent_config(name: &str) -> AgentConfig {
|
||||
AgentConfig {
|
||||
id: AgentId::new(),
|
||||
name: name.to_string(),
|
||||
description: None,
|
||||
model: ModelConfig::default(),
|
||||
system_prompt: None,
|
||||
capabilities: vec![],
|
||||
tools: vec![],
|
||||
max_tokens: None,
|
||||
temperature: None,
|
||||
enabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_in_memory_store_creation() {
|
||||
let store = MemoryStore::in_memory().await;
|
||||
assert!(store.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_save_and_load_agent() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("test-agent");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
|
||||
let loaded = store.load_agent(&config.id).await.unwrap();
|
||||
assert!(loaded.is_some());
|
||||
let loaded = loaded.unwrap();
|
||||
assert_eq!(loaded.id, config.id);
|
||||
assert_eq!(loaded.name, config.name);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_load_nonexistent_agent() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let fake_id = AgentId::new();
|
||||
|
||||
let result = store.load_agent(&fake_id).await.unwrap();
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_save_agent_updates_existing() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let mut config = create_test_agent_config("original");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
|
||||
config.name = "updated".to_string();
|
||||
store.save_agent(&config).await.unwrap();
|
||||
|
||||
let loaded = store.load_agent(&config.id).await.unwrap().unwrap();
|
||||
assert_eq!(loaded.name, "updated");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_list_agents() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
|
||||
let config1 = create_test_agent_config("agent1");
|
||||
let config2 = create_test_agent_config("agent2");
|
||||
|
||||
store.save_agent(&config1).await.unwrap();
|
||||
store.save_agent(&config2).await.unwrap();
|
||||
|
||||
let agents = store.list_agents().await.unwrap();
|
||||
assert_eq!(agents.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_delete_agent() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("to-delete");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
store.delete_agent(&config.id).await.unwrap();
|
||||
|
||||
let loaded = store.load_agent(&config.id).await.unwrap();
|
||||
assert!(loaded.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_delete_nonexistent_agent_succeeds() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let fake_id = AgentId::new();
|
||||
|
||||
// Deleting nonexistent agent should succeed (idempotent)
|
||||
let result = store.delete_agent(&fake_id).await;
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_create_session() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("session-test");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
|
||||
let session_id = store.create_session(&config.id).await.unwrap();
|
||||
assert!(!session_id.as_uuid().is_nil());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_append_and_get_messages() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("msg-test");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
let session_id = store.create_session(&config.id).await.unwrap();
|
||||
|
||||
let msg1 = Message::user("Hello");
|
||||
let msg2 = Message::assistant("Hi there!");
|
||||
|
||||
store.append_message(&session_id, &msg1).await.unwrap();
|
||||
store.append_message(&session_id, &msg2).await.unwrap();
|
||||
|
||||
let messages = store.get_messages(&session_id).await.unwrap();
|
||||
assert_eq!(messages.len(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_message_ordering() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("order-test");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
let session_id = store.create_session(&config.id).await.unwrap();
|
||||
|
||||
for i in 0..10 {
|
||||
let msg = Message::user(format!("Message {}", i));
|
||||
store.append_message(&session_id, &msg).await.unwrap();
|
||||
}
|
||||
|
||||
let messages = store.get_messages(&session_id).await.unwrap();
|
||||
assert_eq!(messages.len(), 10);
|
||||
|
||||
// Verify ordering
|
||||
for (i, msg) in messages.iter().enumerate() {
|
||||
if let Message::User { content } = msg {
|
||||
assert_eq!(content, &format!("Message {}", i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kv_store_and_recall() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("kv-test");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
|
||||
let value = serde_json::json!({"key": "value", "number": 42});
|
||||
store.kv_store(&config.id, "test-key", &value).await.unwrap();
|
||||
|
||||
let recalled = store.kv_recall(&config.id, "test-key").await.unwrap();
|
||||
assert!(recalled.is_some());
|
||||
assert_eq!(recalled.unwrap(), value);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kv_recall_nonexistent() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("kv-missing");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
|
||||
let result = store.kv_recall(&config.id, "nonexistent").await.unwrap();
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kv_update_existing() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("kv-update");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
|
||||
let value1 = serde_json::json!({"version": 1});
|
||||
let value2 = serde_json::json!({"version": 2});
|
||||
|
||||
store.kv_store(&config.id, "key", &value1).await.unwrap();
|
||||
store.kv_store(&config.id, "key", &value2).await.unwrap();
|
||||
|
||||
let recalled = store.kv_recall(&config.id, "key").await.unwrap().unwrap();
|
||||
assert_eq!(recalled["version"], 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kv_list() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("kv-list");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
|
||||
store.kv_store(&config.id, "key1", &serde_json::json!(1)).await.unwrap();
|
||||
store.kv_store(&config.id, "key2", &serde_json::json!(2)).await.unwrap();
|
||||
store.kv_store(&config.id, "key3", &serde_json::json!(3)).await.unwrap();
|
||||
|
||||
let keys = store.kv_list(&config.id).await.unwrap();
|
||||
assert_eq!(keys.len(), 3);
|
||||
assert!(keys.contains(&"key1".to_string()));
|
||||
assert!(keys.contains(&"key2".to_string()));
|
||||
assert!(keys.contains(&"key3".to_string()));
|
||||
}
|
||||
|
||||
// === Edge Case Tests ===
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_agent_with_empty_name() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("");
|
||||
|
||||
// Empty name should still work (validation is elsewhere)
|
||||
let result = store.save_agent(&config).await;
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_agent_with_special_characters_in_name() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("agent-with-特殊字符-🎉");
|
||||
|
||||
let result = store.save_agent(&config).await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
let loaded = store.load_agent(&config.id).await.unwrap().unwrap();
|
||||
assert_eq!(loaded.name, "agent-with-特殊字符-🎉");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_large_message_content() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("large-msg");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
let session_id = store.create_session(&config.id).await.unwrap();
|
||||
|
||||
// Create a large message (100KB)
|
||||
let large_content = "x".repeat(100_000);
|
||||
let msg = Message::user(&large_content);
|
||||
|
||||
let result = store.append_message(&session_id, &msg).await;
|
||||
assert!(result.is_ok());
|
||||
|
||||
let messages = store.get_messages(&session_id).await.unwrap();
|
||||
assert_eq!(messages.len(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_message_with_tool_use() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("tool-msg");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
let session_id = store.create_session(&config.id).await.unwrap();
|
||||
|
||||
let tool_input = serde_json::json!({"query": "test", "options": {"limit": 10}});
|
||||
let msg = Message::tool_use("call-123", zclaw_types::ToolId::new("search"), tool_input.clone());
|
||||
|
||||
store.append_message(&session_id, &msg).await.unwrap();
|
||||
|
||||
let messages = store.get_messages(&session_id).await.unwrap();
|
||||
assert_eq!(messages.len(), 1);
|
||||
|
||||
if let Message::ToolUse { id, tool, input } = &messages[0] {
|
||||
assert_eq!(id, "call-123");
|
||||
assert_eq!(tool.as_str(), "search");
|
||||
assert_eq!(*input, tool_input);
|
||||
} else {
|
||||
panic!("Expected ToolUse message");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_message_with_tool_result() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("tool-result");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
let session_id = store.create_session(&config.id).await.unwrap();
|
||||
|
||||
let output = serde_json::json!({"results": ["a", "b", "c"]});
|
||||
let msg = Message::tool_result("call-123", zclaw_types::ToolId::new("search"), output.clone(), false);
|
||||
|
||||
store.append_message(&session_id, &msg).await.unwrap();
|
||||
|
||||
let messages = store.get_messages(&session_id).await.unwrap();
|
||||
assert_eq!(messages.len(), 1);
|
||||
|
||||
if let Message::ToolResult { tool_call_id, tool, output: o, is_error } = &messages[0] {
|
||||
assert_eq!(tool_call_id, "call-123");
|
||||
assert_eq!(tool.as_str(), "search");
|
||||
assert_eq!(*o, output);
|
||||
assert!(!is_error);
|
||||
} else {
|
||||
panic!("Expected ToolResult message");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_message_with_thinking() {
|
||||
let store = MemoryStore::in_memory().await.unwrap();
|
||||
let config = create_test_agent_config("thinking");
|
||||
|
||||
store.save_agent(&config).await.unwrap();
|
||||
let session_id = store.create_session(&config.id).await.unwrap();
|
||||
|
||||
let msg = Message::assistant_with_thinking("Final answer", "My reasoning...");
|
||||
|
||||
store.append_message(&session_id, &msg).await.unwrap();
|
||||
|
||||
let messages = store.get_messages(&session_id).await.unwrap();
|
||||
assert_eq!(messages.len(), 1);
|
||||
|
||||
if let Message::Assistant { content, thinking } = &messages[0] {
|
||||
assert_eq!(content, "Final answer");
|
||||
assert_eq!(thinking.as_ref().unwrap(), "My reasoning...");
|
||||
} else {
|
||||
panic!("Expected Assistant message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use super::ActionError;
|
||||
pub async fn execute_hand(
|
||||
hand_id: &str,
|
||||
action: &str,
|
||||
params: HashMap<String, Value>,
|
||||
_params: HashMap<String, Value>,
|
||||
) -> Result<Value, ActionError> {
|
||||
// This will be implemented by injecting the hand registry
|
||||
// For now, return an error indicating it needs configuration
|
||||
|
||||
@@ -8,7 +8,7 @@ use super::ActionError;
|
||||
/// Execute a skill by ID
|
||||
pub async fn execute_skill(
|
||||
skill_id: &str,
|
||||
input: HashMap<String, Value>,
|
||||
_input: HashMap<String, Value>,
|
||||
) -> Result<Value, ActionError> {
|
||||
// This will be implemented by injecting the skill registry
|
||||
// For now, return an error indicating it needs configuration
|
||||
|
||||
@@ -341,6 +341,15 @@ impl PipelineExecutor {
|
||||
return Ok(b);
|
||||
}
|
||||
|
||||
// Handle string "true" / "false" as boolean values
|
||||
if let Value::String(s) = &resolved {
|
||||
match s.as_str() {
|
||||
"true" => return Ok(true),
|
||||
"false" => return Ok(false),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for comparison operators
|
||||
let condition = condition.trim();
|
||||
|
||||
@@ -350,7 +359,16 @@ impl PipelineExecutor {
|
||||
let right = condition[eq_pos + 2..].trim();
|
||||
|
||||
let left_val = context.resolve(left)?;
|
||||
let right_val = context.resolve(right)?;
|
||||
// Handle quoted string literals for right side
|
||||
let right_val = if right.starts_with('\'') && right.ends_with('\'') {
|
||||
// Remove quotes and return as string value
|
||||
Value::String(right[1..right.len()-1].to_string())
|
||||
} else if right.starts_with('"') && right.ends_with('"') {
|
||||
// Remove double quotes and return as string value
|
||||
Value::String(right[1..right.len()-1].to_string())
|
||||
} else {
|
||||
context.resolve(right)?
|
||||
};
|
||||
|
||||
return Ok(left_val == right_val);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
//! - Custom variables
|
||||
|
||||
use std::collections::HashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use regex::Regex;
|
||||
|
||||
@@ -156,7 +155,26 @@ impl ExecutionContext {
|
||||
|
||||
match first {
|
||||
"inputs" => self.resolve_from_map(&self.inputs, rest, path),
|
||||
"steps" => self.resolve_from_map(&self.steps_output, rest, path),
|
||||
"steps" => {
|
||||
// Handle "output" as a special key for step outputs
|
||||
// steps.step_id.output.field -> steps_output["step_id"].field
|
||||
// steps.step_id.field -> steps_output["step_id"].field (also supported)
|
||||
if rest.len() >= 2 && rest[1] == "output" {
|
||||
// Skip "output" in the path: [step_id, "output", ...rest] -> [step_id, ...rest]
|
||||
let step_id = rest[0];
|
||||
let actual_rest = &rest[2..];
|
||||
let step_value = self.steps_output.get(step_id)
|
||||
.ok_or_else(|| StateError::VariableNotFound(step_id.to_string()))?;
|
||||
|
||||
if actual_rest.is_empty() {
|
||||
Ok(step_value.clone())
|
||||
} else {
|
||||
self.resolve_from_value(step_value, actual_rest, path)
|
||||
}
|
||||
} else {
|
||||
self.resolve_from_map(&self.steps_output, rest, path)
|
||||
}
|
||||
}
|
||||
"vars" | "var" => self.resolve_from_map(&self.variables, rest, path),
|
||||
"item" => {
|
||||
if let Some(ctx) = &self.loop_context {
|
||||
|
||||
@@ -8,6 +8,7 @@ pub const API_VERSION: &str = "zclaw/v1";
|
||||
|
||||
/// A complete pipeline definition
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Pipeline {
|
||||
/// API version (must be "zclaw/v1")
|
||||
pub api_version: String,
|
||||
@@ -24,6 +25,7 @@ pub struct Pipeline {
|
||||
|
||||
/// Pipeline metadata
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PipelineMetadata {
|
||||
/// Unique identifier (e.g., "classroom-generator")
|
||||
pub name: String,
|
||||
@@ -63,6 +65,7 @@ fn default_version() -> String {
|
||||
|
||||
/// Pipeline specification
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PipelineSpec {
|
||||
/// Input parameters definition
|
||||
#[serde(default)]
|
||||
@@ -94,6 +97,7 @@ fn default_max_workers() -> usize {
|
||||
|
||||
/// Input parameter definition
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PipelineInput {
|
||||
/// Parameter name
|
||||
pub name: String,
|
||||
@@ -142,6 +146,7 @@ pub enum InputType {
|
||||
|
||||
/// Validation rules for input
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ValidationRules {
|
||||
/// Minimum length (for strings)
|
||||
#[serde(default)]
|
||||
@@ -166,6 +171,7 @@ pub struct ValidationRules {
|
||||
|
||||
/// A single step in the pipeline
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PipelineStep {
|
||||
/// Unique step identifier
|
||||
pub id: String,
|
||||
@@ -368,6 +374,7 @@ pub struct ConditionBranch {
|
||||
|
||||
/// Retry configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct RetryConfig {
|
||||
/// Maximum retry attempts
|
||||
#[serde(default = "default_max_retries")]
|
||||
@@ -424,6 +431,7 @@ impl std::fmt::Display for RunStatus {
|
||||
|
||||
/// Pipeline run information
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PipelineRun {
|
||||
/// Unique run ID
|
||||
pub id: String,
|
||||
@@ -458,6 +466,7 @@ pub struct PipelineRun {
|
||||
|
||||
/// Progress information for a running pipeline
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PipelineProgress {
|
||||
/// Run ID
|
||||
pub run_id: String,
|
||||
|
||||
@@ -256,6 +256,7 @@ pub struct A2aReceiver {
|
||||
}
|
||||
|
||||
impl A2aReceiver {
|
||||
#[allow(dead_code)] // Reserved for future A2A integration
|
||||
fn new(rx: mpsc::Receiver<A2aEnvelope>) -> Self {
|
||||
Self { receiver: Some(rx) }
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use zclaw_types::Result;
|
||||
|
||||
// Re-export McpServerConfig from mcp_transport
|
||||
pub use crate::mcp_transport::McpServerConfig;
|
||||
|
||||
/// MCP tool definition
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct McpTool {
|
||||
@@ -130,54 +133,48 @@ pub trait McpClient: Send + Sync {
|
||||
async fn get_prompt(&self, name: &str, arguments: HashMap<String, String>) -> Result<String>;
|
||||
}
|
||||
|
||||
/// Basic MCP client implementation
|
||||
/// Basic MCP client implementation using stdio transport
|
||||
pub struct BasicMcpClient {
|
||||
config: McpClientConfig,
|
||||
client: reqwest::Client,
|
||||
transport: crate::mcp_transport::McpTransport,
|
||||
}
|
||||
|
||||
impl BasicMcpClient {
|
||||
pub fn new(config: McpClientConfig) -> Self {
|
||||
/// Create new MCP client with server configuration
|
||||
pub fn new(config: McpServerConfig) -> Self {
|
||||
Self {
|
||||
config,
|
||||
client: reqwest::Client::new(),
|
||||
transport: crate::mcp_transport::McpTransport::new(config),
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize the MCP connection
|
||||
pub async fn initialize(&self) -> Result<()> {
|
||||
self.transport.initialize().await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl McpClient for BasicMcpClient {
|
||||
async fn list_tools(&self) -> Result<Vec<McpTool>> {
|
||||
// TODO: Implement actual MCP protocol communication
|
||||
Ok(Vec::new())
|
||||
McpClient::list_tools(&self.transport).await
|
||||
}
|
||||
|
||||
async fn call_tool(&self, _request: McpToolCallRequest) -> Result<McpToolCallResponse> {
|
||||
// TODO: Implement actual MCP protocol communication
|
||||
Ok(McpToolCallResponse {
|
||||
content: vec![McpContent::Text { text: "Not implemented".to_string() }],
|
||||
is_error: true,
|
||||
})
|
||||
async fn call_tool(&self, request: McpToolCallRequest) -> Result<McpToolCallResponse> {
|
||||
McpClient::call_tool(&self.transport, request).await
|
||||
}
|
||||
|
||||
async fn list_resources(&self) -> Result<Vec<McpResource>> {
|
||||
Ok(Vec::new())
|
||||
McpClient::list_resources(&self.transport).await
|
||||
}
|
||||
|
||||
async fn read_resource(&self, _uri: &str) -> Result<McpResourceContent> {
|
||||
Ok(McpResourceContent {
|
||||
uri: String::new(),
|
||||
mime_type: None,
|
||||
text: Some("Not implemented".to_string()),
|
||||
blob: None,
|
||||
})
|
||||
async fn read_resource(&self, uri: &str) -> Result<McpResourceContent> {
|
||||
McpClient::read_resource(&self.transport, uri).await
|
||||
}
|
||||
|
||||
async fn list_prompts(&self) -> Result<Vec<McpPrompt>> {
|
||||
Ok(Vec::new())
|
||||
McpClient::list_prompts(&self.transport).await
|
||||
}
|
||||
|
||||
async fn get_prompt(&self, _name: &str, _arguments: HashMap<String, String>) -> Result<String> {
|
||||
Ok("Not implemented".to_string())
|
||||
async fn get_prompt(&self, name: &str, arguments: HashMap<String, String>) -> Result<String> {
|
||||
McpClient::get_prompt(&self.transport, name, arguments).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,12 @@ use std::io::{BufRead, BufReader, BufWriter, Write};
|
||||
use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::de::DeserializeOwned;
|
||||
use tokio::sync::Mutex;
|
||||
use tracing::{debug, warn};
|
||||
|
||||
use zclaw_types::{Result, ZclawError};
|
||||
|
||||
@@ -125,10 +127,10 @@ impl McpTransport {
|
||||
cmd.current_dir(cwd);
|
||||
}
|
||||
|
||||
// Configure stdio
|
||||
// Configure stdio - pipe stderr for debugging
|
||||
cmd.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null());
|
||||
.stderr(Stdio::piped());
|
||||
|
||||
// Spawn process
|
||||
let mut child = cmd.spawn()
|
||||
@@ -140,6 +142,26 @@ impl McpTransport {
|
||||
let stdout = child.stdout.take()
|
||||
.ok_or_else(|| ZclawError::McpError("Failed to get stdout".to_string()))?;
|
||||
|
||||
// Take stderr and spawn a background thread to log it
|
||||
if let Some(stderr) = child.stderr.take() {
|
||||
let server_name = self.config.command.clone();
|
||||
thread::spawn(move || {
|
||||
let reader = BufReader::new(stderr);
|
||||
for line in reader.lines() {
|
||||
match line {
|
||||
Ok(text) => {
|
||||
debug!(server = %server_name, stderr = %text, "MCP server stderr");
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(server = %server_name, error = %e, "Failed to read MCP server stderr");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
debug!(server = %server_name, "MCP server stderr stream ended");
|
||||
});
|
||||
}
|
||||
|
||||
// Store handles in separate mutexes
|
||||
*self.stdin.lock().await = Some(BufWriter::new(stdin));
|
||||
*self.stdout.lock().await = Some(BufReader::new(stdout));
|
||||
@@ -363,3 +385,24 @@ impl McpClient for McpTransport {
|
||||
Ok(prompt_text.join("\n"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for McpTransport {
|
||||
fn drop(&mut self) {
|
||||
// Try to kill the child process synchronously
|
||||
// We use a blocking approach here since Drop cannot be async
|
||||
if let Ok(mut child_guard) = self.child.try_lock() {
|
||||
if let Some(mut child) = child_guard.take() {
|
||||
// Try to kill the process gracefully
|
||||
match child.kill() {
|
||||
Ok(_) => {
|
||||
// Wait for the process to exit
|
||||
let _ = child.wait();
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("[McpTransport] Failed to kill child process: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ async-trait = { workspace = true }
|
||||
# HTTP client
|
||||
reqwest = { workspace = true }
|
||||
|
||||
# URL parsing
|
||||
url = { workspace = true }
|
||||
|
||||
# Secrets
|
||||
secrecy = { workspace = true }
|
||||
|
||||
@@ -35,3 +38,15 @@ rand = { workspace = true }
|
||||
|
||||
# Crypto for hashing
|
||||
sha2 = { workspace = true }
|
||||
|
||||
# Base64 encoding
|
||||
base64 = { workspace = true }
|
||||
|
||||
# Directory helpers
|
||||
dirs = { workspace = true }
|
||||
|
||||
# Shell parsing
|
||||
shlex = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
|
||||
@@ -361,6 +361,7 @@ struct AnthropicStreamEvent {
|
||||
#[serde(rename = "type")]
|
||||
event_type: String,
|
||||
#[serde(default)]
|
||||
#[allow(dead_code)] // Used for deserialization, not accessed
|
||||
index: Option<u32>,
|
||||
#[serde(default)]
|
||||
delta: Option<AnthropicDelta>,
|
||||
|
||||
@@ -11,6 +11,7 @@ use super::{CompletionRequest, CompletionResponse, ContentBlock, LlmDriver, Stop
|
||||
use crate::stream::StreamChunk;
|
||||
|
||||
/// Google Gemini driver
|
||||
#[allow(dead_code)] // TODO: Implement full Gemini API support
|
||||
pub struct GeminiDriver {
|
||||
client: Client,
|
||||
api_key: SecretString,
|
||||
|
||||
@@ -10,6 +10,7 @@ use super::{CompletionRequest, CompletionResponse, ContentBlock, LlmDriver, Stop
|
||||
use crate::stream::StreamChunk;
|
||||
|
||||
/// Local LLM driver for Ollama, LM Studio, vLLM, etc.
|
||||
#[allow(dead_code)] // TODO: Implement full Local driver support
|
||||
pub struct LocalDriver {
|
||||
client: Client,
|
||||
base_url: String,
|
||||
|
||||
@@ -696,6 +696,7 @@ struct OpenAiStreamChoice {
|
||||
#[serde(default)]
|
||||
delta: OpenAiDelta,
|
||||
#[serde(default)]
|
||||
#[allow(dead_code)] // Used for deserialization, not accessed
|
||||
finish_reason: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ use zclaw_types::{AgentId, SessionId, Message, Result};
|
||||
use crate::driver::{LlmDriver, CompletionRequest, ContentBlock};
|
||||
use crate::stream::StreamChunk;
|
||||
use crate::tool::{ToolRegistry, ToolContext, SkillExecutor};
|
||||
use crate::tool::builtin::PathValidator;
|
||||
use crate::loop_guard::LoopGuard;
|
||||
use zclaw_memory::MemoryStore;
|
||||
|
||||
@@ -17,12 +18,14 @@ pub struct AgentLoop {
|
||||
driver: Arc<dyn LlmDriver>,
|
||||
tools: ToolRegistry,
|
||||
memory: Arc<MemoryStore>,
|
||||
#[allow(dead_code)] // Reserved for future rate limiting
|
||||
loop_guard: LoopGuard,
|
||||
model: String,
|
||||
system_prompt: Option<String>,
|
||||
max_tokens: u32,
|
||||
temperature: f32,
|
||||
skill_executor: Option<Arc<dyn SkillExecutor>>,
|
||||
path_validator: Option<PathValidator>,
|
||||
}
|
||||
|
||||
impl AgentLoop {
|
||||
@@ -43,6 +46,7 @@ impl AgentLoop {
|
||||
max_tokens: 4096,
|
||||
temperature: 0.7,
|
||||
skill_executor: None,
|
||||
path_validator: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +56,12 @@ impl AgentLoop {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the path validator for file system operations
|
||||
pub fn with_path_validator(mut self, validator: PathValidator) -> Self {
|
||||
self.path_validator = Some(validator);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the model to use
|
||||
pub fn with_model(mut self, model: impl Into<String>) -> Self {
|
||||
self.model = model.into();
|
||||
@@ -83,6 +93,7 @@ impl AgentLoop {
|
||||
working_directory: None,
|
||||
session_id: Some(session_id.to_string()),
|
||||
skill_executor: self.skill_executor.clone(),
|
||||
path_validator: self.path_validator.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,6 +229,7 @@ impl AgentLoop {
|
||||
let driver = self.driver.clone();
|
||||
let tools = self.tools.clone();
|
||||
let skill_executor = self.skill_executor.clone();
|
||||
let path_validator = self.path_validator.clone();
|
||||
let agent_id = self.agent_id.clone();
|
||||
let system_prompt = self.system_prompt.clone();
|
||||
let model = self.model.clone();
|
||||
@@ -346,6 +358,7 @@ impl AgentLoop {
|
||||
working_directory: None,
|
||||
session_id: Some(session_id_clone.to_string()),
|
||||
skill_executor: skill_executor.clone(),
|
||||
path_validator: path_validator.clone(),
|
||||
};
|
||||
|
||||
let (result, is_error) = if let Some(tool) = tools.get(&name) {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
//! Tool system for agent capabilities
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use async_trait::async_trait;
|
||||
use serde_json::Value;
|
||||
use zclaw_types::{AgentId, Result};
|
||||
|
||||
use crate::driver::ToolDefinition;
|
||||
use crate::tool::builtin::PathValidator;
|
||||
|
||||
/// Tool trait for implementing agent tools
|
||||
#[async_trait]
|
||||
@@ -43,6 +45,8 @@ pub struct ToolContext {
|
||||
pub working_directory: Option<String>,
|
||||
pub session_id: Option<String>,
|
||||
pub skill_executor: Option<Arc<dyn SkillExecutor>>,
|
||||
/// Path validator for file system operations
|
||||
pub path_validator: Option<PathValidator>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for ToolContext {
|
||||
@@ -52,6 +56,7 @@ impl std::fmt::Debug for ToolContext {
|
||||
.field("working_directory", &self.working_directory)
|
||||
.field("session_id", &self.session_id)
|
||||
.field("skill_executor", &self.skill_executor.as_ref().map(|_| "SkillExecutor"))
|
||||
.field("path_validator", &self.path_validator.as_ref().map(|_| "PathValidator"))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
@@ -63,41 +68,78 @@ impl Clone for ToolContext {
|
||||
working_directory: self.working_directory.clone(),
|
||||
session_id: self.session_id.clone(),
|
||||
skill_executor: self.skill_executor.clone(),
|
||||
path_validator: self.path_validator.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tool registry for managing available tools
|
||||
/// Uses HashMap for O(1) lookup performance
|
||||
#[derive(Clone)]
|
||||
pub struct ToolRegistry {
|
||||
tools: Vec<Arc<dyn Tool>>,
|
||||
/// Tool lookup by name (O(1))
|
||||
tools: HashMap<String, Arc<dyn Tool>>,
|
||||
/// Registration order for consistent iteration
|
||||
tool_order: Vec<String>,
|
||||
}
|
||||
|
||||
impl ToolRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self { tools: Vec::new() }
|
||||
Self {
|
||||
tools: HashMap::new(),
|
||||
tool_order: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(&mut self, tool: Box<dyn Tool>) {
|
||||
self.tools.push(Arc::from(tool));
|
||||
let tool: Arc<dyn Tool> = Arc::from(tool);
|
||||
let name = tool.name().to_string();
|
||||
|
||||
// Track order for new tools
|
||||
if !self.tools.contains_key(&name) {
|
||||
self.tool_order.push(name.clone());
|
||||
}
|
||||
|
||||
self.tools.insert(name, tool);
|
||||
}
|
||||
|
||||
/// Get tool by name - O(1) lookup
|
||||
pub fn get(&self, name: &str) -> Option<Arc<dyn Tool>> {
|
||||
self.tools.iter().find(|t| t.name() == name).cloned()
|
||||
self.tools.get(name).cloned()
|
||||
}
|
||||
|
||||
/// List all tools in registration order
|
||||
pub fn list(&self) -> Vec<&dyn Tool> {
|
||||
self.tools.iter().map(|t| t.as_ref()).collect()
|
||||
self.tool_order
|
||||
.iter()
|
||||
.filter_map(|name| self.tools.get(name).map(|t| t.as_ref()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get tool definitions in registration order
|
||||
pub fn definitions(&self) -> Vec<ToolDefinition> {
|
||||
self.tools.iter().map(|t| {
|
||||
self.tool_order
|
||||
.iter()
|
||||
.filter_map(|name| {
|
||||
self.tools.get(name).map(|t| {
|
||||
ToolDefinition::new(
|
||||
t.name(),
|
||||
t.description(),
|
||||
t.input_schema(),
|
||||
)
|
||||
}).collect()
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get number of registered tools
|
||||
pub fn len(&self) -> usize {
|
||||
self.tools.len()
|
||||
}
|
||||
|
||||
/// Check if registry is empty
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.tools.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,12 +5,14 @@ mod file_write;
|
||||
mod shell_exec;
|
||||
mod web_fetch;
|
||||
mod execute_skill;
|
||||
mod path_validator;
|
||||
|
||||
pub use file_read::FileReadTool;
|
||||
pub use file_write::FileWriteTool;
|
||||
pub use shell_exec::ShellExecTool;
|
||||
pub use web_fetch::WebFetchTool;
|
||||
pub use execute_skill::ExecuteSkillTool;
|
||||
pub use path_validator::{PathValidator, PathValidatorConfig};
|
||||
|
||||
use crate::tool::ToolRegistry;
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
//! File read tool
|
||||
//! File read tool with path validation
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde_json::{json, Value};
|
||||
use zclaw_types::{Result, ZclawError};
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
|
||||
use crate::tool::{Tool, ToolContext};
|
||||
use super::path_validator::PathValidator;
|
||||
|
||||
pub struct FileReadTool;
|
||||
|
||||
@@ -21,7 +24,7 @@ impl Tool for FileReadTool {
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Read the contents of a file from the filesystem"
|
||||
"Read the contents of a file from the filesystem. The file must be within allowed paths."
|
||||
}
|
||||
|
||||
fn input_schema(&self) -> Value {
|
||||
@@ -31,21 +34,79 @@ impl Tool for FileReadTool {
|
||||
"path": {
|
||||
"type": "string",
|
||||
"description": "The path to the file to read"
|
||||
},
|
||||
"encoding": {
|
||||
"type": "string",
|
||||
"description": "Text encoding to use (default: utf-8)",
|
||||
"enum": ["utf-8", "ascii", "binary"]
|
||||
}
|
||||
},
|
||||
"required": ["path"]
|
||||
})
|
||||
}
|
||||
|
||||
async fn execute(&self, input: Value, _context: &ToolContext) -> Result<Value> {
|
||||
async fn execute(&self, input: Value, context: &ToolContext) -> Result<Value> {
|
||||
let path = input["path"].as_str()
|
||||
.ok_or_else(|| ZclawError::InvalidInput("Missing 'path' parameter".into()))?;
|
||||
|
||||
// TODO: Implement actual file reading with path validation
|
||||
let encoding = input["encoding"].as_str().unwrap_or("utf-8");
|
||||
|
||||
// Validate path using context's path validator or create default
|
||||
let validator = context.path_validator.as_ref()
|
||||
.map(|v| v.clone())
|
||||
.unwrap_or_else(|| {
|
||||
// Create default validator with workspace as allowed path
|
||||
let mut validator = PathValidator::new();
|
||||
if let Some(ref workspace) = context.working_directory {
|
||||
validator = validator.with_workspace(std::path::PathBuf::from(workspace));
|
||||
}
|
||||
validator
|
||||
});
|
||||
|
||||
// Validate path for read access
|
||||
let validated_path = validator.validate_read(path)?;
|
||||
|
||||
// Read file content
|
||||
let mut file = fs::File::open(&validated_path)
|
||||
.map_err(|e| ZclawError::ToolError(format!("Failed to open file: {}", e)))?;
|
||||
|
||||
let metadata = fs::metadata(&validated_path)
|
||||
.map_err(|e| ZclawError::ToolError(format!("Failed to read file metadata: {}", e)))?;
|
||||
|
||||
let file_size = metadata.len();
|
||||
|
||||
match encoding {
|
||||
"binary" => {
|
||||
let mut buffer = Vec::with_capacity(file_size as usize);
|
||||
file.read_to_end(&mut buffer)
|
||||
.map_err(|e| ZclawError::ToolError(format!("Failed to read file: {}", e)))?;
|
||||
|
||||
// Return base64 encoded binary content
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
|
||||
let encoded = BASE64.encode(&buffer);
|
||||
|
||||
Ok(json!({
|
||||
"content": format!("File content placeholder for: {}", path)
|
||||
"content": encoded,
|
||||
"encoding": "base64",
|
||||
"size": file_size,
|
||||
"path": validated_path.to_string_lossy()
|
||||
}))
|
||||
}
|
||||
_ => {
|
||||
// Text mode (utf-8 or ascii)
|
||||
let mut content = String::with_capacity(file_size as usize);
|
||||
file.read_to_string(&mut content)
|
||||
.map_err(|e| ZclawError::ToolError(format!("Failed to read file: {}", e)))?;
|
||||
|
||||
Ok(json!({
|
||||
"content": content,
|
||||
"encoding": encoding,
|
||||
"size": file_size,
|
||||
"path": validated_path.to_string_lossy()
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for FileReadTool {
|
||||
@@ -53,3 +114,38 @@ impl Default for FileReadTool {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
use tempfile::NamedTempFile;
|
||||
use crate::tool::builtin::PathValidator;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_file() {
|
||||
let mut temp_file = NamedTempFile::new().unwrap();
|
||||
writeln!(temp_file, "Hello, World!").unwrap();
|
||||
|
||||
let path = temp_file.path().to_str().unwrap();
|
||||
let input = json!({ "path": path });
|
||||
|
||||
// Configure PathValidator to allow temp directory (use canonicalized path)
|
||||
let temp_dir = std::env::temp_dir().canonicalize().unwrap_or(std::env::temp_dir());
|
||||
let path_validator = Some(PathValidator::new().with_workspace(temp_dir));
|
||||
|
||||
let context = ToolContext {
|
||||
agent_id: zclaw_types::AgentId::new(),
|
||||
working_directory: None,
|
||||
session_id: None,
|
||||
skill_executor: None,
|
||||
path_validator,
|
||||
};
|
||||
|
||||
let tool = FileReadTool::new();
|
||||
let result = tool.execute(input, &context).await.unwrap();
|
||||
|
||||
assert!(result["content"].as_str().unwrap().contains("Hello, World!"));
|
||||
assert_eq!(result["encoding"].as_str().unwrap(), "utf-8");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
//! File write tool
|
||||
//! File write tool with path validation
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde_json::{json, Value};
|
||||
use zclaw_types::{Result, ZclawError};
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
|
||||
use crate::tool::{Tool, ToolContext};
|
||||
use super::path_validator::PathValidator;
|
||||
|
||||
pub struct FileWriteTool;
|
||||
|
||||
@@ -21,7 +24,7 @@ impl Tool for FileWriteTool {
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Write content to a file on the filesystem"
|
||||
"Write content to a file on the filesystem. The file must be within allowed paths."
|
||||
}
|
||||
|
||||
fn input_schema(&self) -> Value {
|
||||
@@ -35,22 +38,92 @@ impl Tool for FileWriteTool {
|
||||
"content": {
|
||||
"type": "string",
|
||||
"description": "The content to write to the file"
|
||||
},
|
||||
"mode": {
|
||||
"type": "string",
|
||||
"description": "Write mode: 'create' (fail if exists), 'overwrite' (replace), 'append' (add to end)",
|
||||
"enum": ["create", "overwrite", "append"],
|
||||
"default": "create"
|
||||
},
|
||||
"encoding": {
|
||||
"type": "string",
|
||||
"description": "Content encoding (default: utf-8)",
|
||||
"enum": ["utf-8", "base64"]
|
||||
}
|
||||
},
|
||||
"required": ["path", "content"]
|
||||
})
|
||||
}
|
||||
|
||||
async fn execute(&self, input: Value, _context: &ToolContext) -> Result<Value> {
|
||||
let _path = input["path"].as_str()
|
||||
async fn execute(&self, input: Value, context: &ToolContext) -> Result<Value> {
|
||||
let path = input["path"].as_str()
|
||||
.ok_or_else(|| ZclawError::InvalidInput("Missing 'path' parameter".into()))?;
|
||||
|
||||
let content = input["content"].as_str()
|
||||
.ok_or_else(|| ZclawError::InvalidInput("Missing 'content' parameter".into()))?;
|
||||
|
||||
// TODO: Implement actual file writing with path validation
|
||||
let mode = input["mode"].as_str().unwrap_or("create");
|
||||
let encoding = input["encoding"].as_str().unwrap_or("utf-8");
|
||||
|
||||
// Validate path using context's path validator or create default
|
||||
let validator = context.path_validator.as_ref()
|
||||
.map(|v| v.clone())
|
||||
.unwrap_or_else(|| {
|
||||
// Create default validator with workspace as allowed path
|
||||
let mut validator = PathValidator::new();
|
||||
if let Some(ref workspace) = context.working_directory {
|
||||
validator = validator.with_workspace(std::path::PathBuf::from(workspace));
|
||||
}
|
||||
validator
|
||||
});
|
||||
|
||||
// Validate path for write access
|
||||
let validated_path = validator.validate_write(path)?;
|
||||
|
||||
// Decode content based on encoding
|
||||
let bytes = match encoding {
|
||||
"base64" => {
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
|
||||
BASE64.decode(content)
|
||||
.map_err(|e| ZclawError::InvalidInput(format!("Invalid base64 content: {}", e)))?
|
||||
}
|
||||
_ => content.as_bytes().to_vec()
|
||||
};
|
||||
|
||||
// Check if file exists and handle mode
|
||||
let file_exists = validated_path.exists();
|
||||
|
||||
if file_exists && mode == "create" {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"File already exists: {}",
|
||||
validated_path.display()
|
||||
)));
|
||||
}
|
||||
|
||||
// Write file
|
||||
let mut file = match mode {
|
||||
"append" => {
|
||||
fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(&validated_path)
|
||||
.map_err(|e| ZclawError::ToolError(format!("Failed to open file for append: {}", e)))?
|
||||
}
|
||||
_ => {
|
||||
// create or overwrite
|
||||
fs::File::create(&validated_path)
|
||||
.map_err(|e| ZclawError::ToolError(format!("Failed to create file: {}", e)))?
|
||||
}
|
||||
};
|
||||
|
||||
file.write_all(&bytes)
|
||||
.map_err(|e| ZclawError::ToolError(format!("Failed to write file: {}", e)))?;
|
||||
|
||||
Ok(json!({
|
||||
"success": true,
|
||||
"bytes_written": content.len()
|
||||
"bytes_written": bytes.len(),
|
||||
"path": validated_path.to_string_lossy(),
|
||||
"mode": mode
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -60,3 +133,85 @@ impl Default for FileWriteTool {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::tempdir;
|
||||
use crate::tool::builtin::PathValidator;
|
||||
|
||||
fn create_test_context_with_tempdir(dir: &std::path::Path) -> ToolContext {
|
||||
// Use canonicalized path to handle Windows extended-length paths
|
||||
let workspace = dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf());
|
||||
let path_validator = Some(PathValidator::new().with_workspace(workspace));
|
||||
ToolContext {
|
||||
agent_id: zclaw_types::AgentId::new(),
|
||||
working_directory: None,
|
||||
session_id: None,
|
||||
skill_executor: None,
|
||||
path_validator,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_write_new_file() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("test.txt").to_str().unwrap().to_string();
|
||||
|
||||
let input = json!({
|
||||
"path": path,
|
||||
"content": "Hello, World!"
|
||||
});
|
||||
|
||||
let context = create_test_context_with_tempdir(dir.path());
|
||||
|
||||
let tool = FileWriteTool::new();
|
||||
let result = tool.execute(input, &context).await.unwrap();
|
||||
|
||||
assert!(result["success"].as_bool().unwrap());
|
||||
assert_eq!(result["bytes_written"].as_u64().unwrap(), 13);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_create_mode_fails_on_existing() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("existing.txt");
|
||||
fs::write(&path, "existing content").unwrap();
|
||||
|
||||
let input = json!({
|
||||
"path": path.to_str().unwrap(),
|
||||
"content": "new content",
|
||||
"mode": "create"
|
||||
});
|
||||
|
||||
let context = create_test_context_with_tempdir(dir.path());
|
||||
|
||||
let tool = FileWriteTool::new();
|
||||
let result = tool.execute(input, &context).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_overwrite_mode() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("test.txt");
|
||||
fs::write(&path, "old content").unwrap();
|
||||
|
||||
let input = json!({
|
||||
"path": path.to_str().unwrap(),
|
||||
"content": "new content",
|
||||
"mode": "overwrite"
|
||||
});
|
||||
|
||||
let context = create_test_context_with_tempdir(dir.path());
|
||||
|
||||
let tool = FileWriteTool::new();
|
||||
let result = tool.execute(input, &context).await.unwrap();
|
||||
|
||||
assert!(result["success"].as_bool().unwrap());
|
||||
|
||||
let content = fs::read_to_string(&path).unwrap();
|
||||
assert_eq!(content, "new content");
|
||||
}
|
||||
}
|
||||
|
||||
461
crates/zclaw-runtime/src/tool/builtin/path_validator.rs
Normal file
461
crates/zclaw-runtime/src/tool/builtin/path_validator.rs
Normal file
@@ -0,0 +1,461 @@
|
||||
//! Path validation for file system tools
|
||||
//!
|
||||
//! Provides security validation for file paths to prevent:
|
||||
//! - Path traversal attacks (../)
|
||||
//! - Access to blocked system directories
|
||||
//! - Access outside allowed workspace directories
|
||||
//!
|
||||
//! # Security Policy (Default Deny)
|
||||
//!
|
||||
//! This validator follows a **default deny** security policy:
|
||||
//! - If no `allowed_paths` are configured AND no `workspace_root` is set,
|
||||
//! all path access is denied by default
|
||||
//! - This prevents accidental exposure of sensitive files when the validator
|
||||
//! is used without proper configuration
|
||||
//! - To enable file access, you MUST either:
|
||||
//! 1. Set explicit `allowed_paths` in the configuration, OR
|
||||
//! 2. Configure a `workspace_root` directory
|
||||
//!
|
||||
//! Example configuration:
|
||||
//! ```ignore
|
||||
//! let validator = PathValidator::with_config(config)
|
||||
//! .with_workspace(PathBuf::from("/safe/workspace"));
|
||||
//! ```
|
||||
|
||||
use std::path::{Path, PathBuf, Component};
|
||||
use zclaw_types::{Result, ZclawError};
|
||||
|
||||
/// Path validator configuration
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PathValidatorConfig {
|
||||
/// Allowed directory prefixes (empty = allow all within workspace)
|
||||
pub allowed_paths: Vec<PathBuf>,
|
||||
/// Blocked paths (always denied, even if in allowed_paths)
|
||||
pub blocked_paths: Vec<PathBuf>,
|
||||
/// Maximum file size in bytes (0 = no limit)
|
||||
pub max_file_size: u64,
|
||||
/// Whether to allow symbolic links
|
||||
pub allow_symlinks: bool,
|
||||
}
|
||||
|
||||
impl Default for PathValidatorConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
allowed_paths: Vec::new(),
|
||||
blocked_paths: default_blocked_paths(),
|
||||
max_file_size: 10 * 1024 * 1024, // 10MB default
|
||||
allow_symlinks: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PathValidatorConfig {
|
||||
/// Create config from security.toml settings
|
||||
pub fn from_config(allowed: &[String], blocked: &[String], max_size: &str) -> Self {
|
||||
let allowed_paths: Vec<PathBuf> = allowed
|
||||
.iter()
|
||||
.map(|p| expand_tilde(p))
|
||||
.collect();
|
||||
|
||||
let blocked_paths: Vec<PathBuf> = blocked
|
||||
.iter()
|
||||
.map(|p| PathBuf::from(p))
|
||||
.chain(default_blocked_paths())
|
||||
.collect();
|
||||
|
||||
let max_file_size = parse_size(max_size).unwrap_or(10 * 1024 * 1024);
|
||||
|
||||
Self {
|
||||
allowed_paths,
|
||||
blocked_paths,
|
||||
max_file_size,
|
||||
allow_symlinks: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Default blocked paths for security
|
||||
fn default_blocked_paths() -> Vec<PathBuf> {
|
||||
vec![
|
||||
// Unix sensitive files
|
||||
PathBuf::from("/etc/shadow"),
|
||||
PathBuf::from("/etc/passwd"),
|
||||
PathBuf::from("/etc/sudoers"),
|
||||
PathBuf::from("/root"),
|
||||
PathBuf::from("/proc"),
|
||||
PathBuf::from("/sys"),
|
||||
// Windows sensitive paths
|
||||
PathBuf::from("C:\\Windows\\System32\\config"),
|
||||
PathBuf::from("C:\\Users\\Administrator"),
|
||||
// SSH keys
|
||||
PathBuf::from("/.ssh"),
|
||||
PathBuf::from("/root/.ssh"),
|
||||
// Environment files
|
||||
PathBuf::from(".env"),
|
||||
PathBuf::from(".env.local"),
|
||||
PathBuf::from(".env.production"),
|
||||
]
|
||||
}
|
||||
|
||||
/// Expand tilde in path to home directory
|
||||
fn expand_tilde(path: &str) -> PathBuf {
|
||||
if path.starts_with('~') {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
if path == "~" {
|
||||
return home;
|
||||
}
|
||||
if path.starts_with("~/") || path.starts_with("~\\") {
|
||||
return home.join(&path[2..]);
|
||||
}
|
||||
}
|
||||
}
|
||||
PathBuf::from(path)
|
||||
}
|
||||
|
||||
/// Parse size string like "10MB", "1GB", etc.
|
||||
fn parse_size(s: &str) -> Option<u64> {
|
||||
let s = s.trim().to_uppercase();
|
||||
let (num, unit) = if s.ends_with("GB") {
|
||||
(s.trim_end_matches("GB").trim(), 1024 * 1024 * 1024)
|
||||
} else if s.ends_with("MB") {
|
||||
(s.trim_end_matches("MB").trim(), 1024 * 1024)
|
||||
} else if s.ends_with("KB") {
|
||||
(s.trim_end_matches("KB").trim(), 1024)
|
||||
} else if s.ends_with("B") {
|
||||
(s.trim_end_matches("B").trim(), 1)
|
||||
} else {
|
||||
(s.as_str(), 1)
|
||||
};
|
||||
|
||||
num.parse::<u64>().ok().map(|n| n * unit)
|
||||
}
|
||||
|
||||
/// Path validator for file system security
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PathValidator {
|
||||
config: PathValidatorConfig,
|
||||
workspace_root: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl PathValidator {
|
||||
/// Create a new path validator with default config
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
config: PathValidatorConfig::default(),
|
||||
workspace_root: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a path validator with custom config
|
||||
pub fn with_config(config: PathValidatorConfig) -> Self {
|
||||
Self {
|
||||
config,
|
||||
workspace_root: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the workspace root directory
|
||||
pub fn with_workspace(mut self, workspace: PathBuf) -> Self {
|
||||
self.workspace_root = Some(workspace);
|
||||
self
|
||||
}
|
||||
|
||||
/// Validate a path for read access
|
||||
pub fn validate_read(&self, path: &str) -> Result<PathBuf> {
|
||||
let canonical = self.resolve_and_validate(path)?;
|
||||
|
||||
// Check if file exists
|
||||
if !canonical.exists() {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"File does not exist: {}",
|
||||
path
|
||||
)));
|
||||
}
|
||||
|
||||
// Check if it's a file (not directory)
|
||||
if !canonical.is_file() {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"Path is not a file: {}",
|
||||
path
|
||||
)));
|
||||
}
|
||||
|
||||
// Check file size
|
||||
if self.config.max_file_size > 0 {
|
||||
if let Ok(metadata) = std::fs::metadata(&canonical) {
|
||||
if metadata.len() > self.config.max_file_size {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"File too large: {} bytes (max: {} bytes)",
|
||||
metadata.len(),
|
||||
self.config.max_file_size
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(canonical)
|
||||
}
|
||||
|
||||
/// Validate a path for write access
|
||||
pub fn validate_write(&self, path: &str) -> Result<PathBuf> {
|
||||
let canonical = self.resolve_and_validate(path)?;
|
||||
|
||||
// Check parent directory exists
|
||||
if let Some(parent) = canonical.parent() {
|
||||
if !parent.exists() {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"Parent directory does not exist: {}",
|
||||
parent.display()
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
// If file exists, check it's not blocked
|
||||
if canonical.exists() && !canonical.is_file() {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"Path exists but is not a file: {}",
|
||||
path
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(canonical)
|
||||
}
|
||||
|
||||
/// Resolve and validate a path
|
||||
fn resolve_and_validate(&self, path: &str) -> Result<PathBuf> {
|
||||
// Expand tilde
|
||||
let expanded = expand_tilde(path);
|
||||
let path_buf = PathBuf::from(&expanded);
|
||||
|
||||
// Check for path traversal
|
||||
self.check_path_traversal(&path_buf)?;
|
||||
|
||||
// Resolve to canonical path
|
||||
let canonical = if path_buf.exists() {
|
||||
path_buf
|
||||
.canonicalize()
|
||||
.map_err(|e| ZclawError::InvalidInput(format!("Cannot resolve path: {}", e)))?
|
||||
} else {
|
||||
// For non-existent files, resolve parent and join
|
||||
let parent = path_buf.parent().unwrap_or(Path::new("."));
|
||||
let canonical_parent = parent
|
||||
.canonicalize()
|
||||
.map_err(|e| ZclawError::InvalidInput(format!("Cannot resolve parent path: {}", e)))?;
|
||||
canonical_parent.join(path_buf.file_name().unwrap_or_default())
|
||||
};
|
||||
|
||||
// Check blocked paths
|
||||
self.check_blocked(&canonical)?;
|
||||
|
||||
// Check allowed paths
|
||||
self.check_allowed(&canonical)?;
|
||||
|
||||
// Check symlinks
|
||||
if !self.config.allow_symlinks {
|
||||
self.check_symlink(&canonical)?;
|
||||
}
|
||||
|
||||
Ok(canonical)
|
||||
}
|
||||
|
||||
/// Check for path traversal attacks
|
||||
fn check_path_traversal(&self, path: &Path) -> Result<()> {
|
||||
for component in path.components() {
|
||||
if let Component::ParentDir = component {
|
||||
// Allow .. if workspace is configured (will be validated in check_allowed)
|
||||
// Deny .. if no workspace is configured (more restrictive)
|
||||
if self.workspace_root.is_none() {
|
||||
// Without workspace, be more restrictive
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Path traversal not allowed outside workspace".to_string()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check if path is in blocked list
|
||||
fn check_blocked(&self, path: &Path) -> Result<()> {
|
||||
for blocked in &self.config.blocked_paths {
|
||||
if path.starts_with(blocked) || path == blocked {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"Access to this path is blocked: {}",
|
||||
path.display()
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check if path is in allowed list
|
||||
///
|
||||
/// # Security: Default Deny Policy
|
||||
///
|
||||
/// This method implements a strict default-deny security policy:
|
||||
/// - If `allowed_paths` is empty AND no `workspace_root` is configured,
|
||||
/// access is **denied by default** with a clear error message
|
||||
/// - This prevents accidental exposure of the entire filesystem
|
||||
/// when the validator is misconfigured or used without setup
|
||||
fn check_allowed(&self, path: &Path) -> Result<()> {
|
||||
// If no allowed paths specified, check workspace
|
||||
if self.config.allowed_paths.is_empty() {
|
||||
if let Some(ref workspace) = self.workspace_root {
|
||||
// Workspace is configured - validate path is within it
|
||||
if !path.starts_with(workspace) {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"Path outside workspace: {} (workspace: {})",
|
||||
path.display(),
|
||||
workspace.display()
|
||||
)));
|
||||
}
|
||||
return Ok(());
|
||||
} else {
|
||||
// SECURITY: No allowed_paths AND no workspace_root configured
|
||||
// Default to DENY - do not allow unrestricted filesystem access
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Path access denied: no workspace or allowed paths configured. \
|
||||
To enable file access, configure either 'allowed_paths' in security.toml \
|
||||
or set a workspace_root directory."
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Check against allowed paths
|
||||
for allowed in &self.config.allowed_paths {
|
||||
if path.starts_with(allowed) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
Err(ZclawError::InvalidInput(format!(
|
||||
"Path not in allowed directories: {}",
|
||||
path.display()
|
||||
)))
|
||||
}
|
||||
|
||||
/// Check for symbolic links
|
||||
fn check_symlink(&self, path: &Path) -> Result<()> {
|
||||
if path.exists() {
|
||||
let metadata = std::fs::symlink_metadata(path)
|
||||
.map_err(|e| ZclawError::InvalidInput(format!("Cannot read path metadata: {}", e)))?;
|
||||
|
||||
if metadata.file_type().is_symlink() {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Symbolic links are not allowed".to_string()
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PathValidator {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_size() {
|
||||
assert_eq!(parse_size("10MB"), Some(10 * 1024 * 1024));
|
||||
assert_eq!(parse_size("1GB"), Some(1024 * 1024 * 1024));
|
||||
assert_eq!(parse_size("512KB"), Some(512 * 1024));
|
||||
assert_eq!(parse_size("1024B"), Some(1024));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expand_tilde() {
|
||||
let home = dirs::home_dir().unwrap_or_default();
|
||||
assert_eq!(expand_tilde("~"), home);
|
||||
assert!(expand_tilde("~/test").starts_with(&home));
|
||||
assert_eq!(expand_tilde("/absolute/path"), PathBuf::from("/absolute/path"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_blocked_paths() {
|
||||
let validator = PathValidator::new();
|
||||
|
||||
// These should be blocked (blocked paths take precedence)
|
||||
assert!(validator.resolve_and_validate("/etc/shadow").is_err());
|
||||
assert!(validator.resolve_and_validate("/etc/passwd").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_path_traversal() {
|
||||
// Without workspace, traversal should fail
|
||||
let no_workspace = PathValidator::new();
|
||||
assert!(no_workspace.resolve_and_validate("../../../etc/passwd").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_deny_without_configuration() {
|
||||
// SECURITY TEST: Verify default deny policy when no configuration is set
|
||||
// A validator with no allowed_paths and no workspace_root should deny all access
|
||||
let validator = PathValidator::new();
|
||||
|
||||
// Even valid paths should be denied when not configured
|
||||
let result = validator.check_allowed(Path::new("/some/random/path"));
|
||||
assert!(result.is_err(), "Expected denial when no configuration is set");
|
||||
|
||||
let err_msg = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
err_msg.contains("no workspace or allowed paths configured"),
|
||||
"Error message should explain configuration requirement, got: {}",
|
||||
err_msg
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_allows_with_workspace_root() {
|
||||
// When workspace_root is set, paths within workspace should be allowed
|
||||
let workspace = std::env::temp_dir();
|
||||
let validator = PathValidator::new()
|
||||
.with_workspace(workspace.clone());
|
||||
|
||||
// Path within workspace should pass the allowed check
|
||||
let test_path = workspace.join("test_file.txt");
|
||||
let result = validator.check_allowed(&test_path);
|
||||
assert!(result.is_ok(), "Path within workspace should be allowed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_allows_with_explicit_allowed_paths() {
|
||||
// When allowed_paths is configured, those paths should be allowed
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let config = PathValidatorConfig {
|
||||
allowed_paths: vec![temp_dir.clone()],
|
||||
blocked_paths: vec![],
|
||||
max_file_size: 0,
|
||||
allow_symlinks: false,
|
||||
};
|
||||
let validator = PathValidator::with_config(config);
|
||||
|
||||
// Path within allowed_paths should pass
|
||||
let test_path = temp_dir.join("test_file.txt");
|
||||
let result = validator.check_allowed(&test_path);
|
||||
assert!(result.is_ok(), "Path in allowed_paths should be allowed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_denies_outside_workspace() {
|
||||
// Paths outside workspace_root should be denied
|
||||
let validator = PathValidator::new()
|
||||
.with_workspace(PathBuf::from("/safe/workspace"));
|
||||
|
||||
let result = validator.check_allowed(Path::new("/other/location"));
|
||||
assert!(result.is_err(), "Path outside workspace should be denied");
|
||||
|
||||
let err_msg = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
err_msg.contains("Path outside workspace"),
|
||||
"Error should indicate path is outside workspace, got: {}",
|
||||
err_msg
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,24 @@ use zclaw_types::{Result, ZclawError};
|
||||
|
||||
use crate::tool::{Tool, ToolContext};
|
||||
|
||||
/// Parse a command string into program and arguments using proper shell quoting
|
||||
fn parse_command(command: &str) -> Result<(String, Vec<String>)> {
|
||||
// Use shlex for proper shell-style quoting support
|
||||
let parts = shlex::split(command)
|
||||
.ok_or_else(|| ZclawError::InvalidInput(
|
||||
format!("Failed to parse command: invalid quoting in '{}'", command)
|
||||
))?;
|
||||
|
||||
if parts.is_empty() {
|
||||
return Err(ZclawError::InvalidInput("Empty command".into()));
|
||||
}
|
||||
|
||||
let program = parts[0].clone();
|
||||
let args = parts[1..].to_vec();
|
||||
|
||||
Ok((program, args))
|
||||
}
|
||||
|
||||
/// Security configuration for shell execution
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ShellSecurityConfig {
|
||||
@@ -167,18 +185,12 @@ impl Tool for ShellExecTool {
|
||||
// Security check
|
||||
self.config.is_command_allowed(command)?;
|
||||
|
||||
// Parse command into program and args
|
||||
let parts: Vec<&str> = command.split_whitespace().collect();
|
||||
if parts.is_empty() {
|
||||
return Err(ZclawError::InvalidInput("Empty command".into()));
|
||||
}
|
||||
|
||||
let program = parts[0];
|
||||
let args = &parts[1..];
|
||||
// Parse command into program and args using proper shell quoting
|
||||
let (program, args) = parse_command(command)?;
|
||||
|
||||
// Build command
|
||||
let mut cmd = Command::new(program);
|
||||
cmd.args(args);
|
||||
let mut cmd = Command::new(&program);
|
||||
cmd.args(&args);
|
||||
|
||||
if let Some(dir) = cwd {
|
||||
cmd.current_dir(dir);
|
||||
@@ -190,23 +202,34 @@ impl Tool for ShellExecTool {
|
||||
.stderr(Stdio::piped());
|
||||
|
||||
let start = Instant::now();
|
||||
let timeout_duration = Duration::from_secs(timeout_secs);
|
||||
|
||||
// Execute command
|
||||
let output = tokio::task::spawn_blocking(move || {
|
||||
// Execute command with proper timeout (timeout applies DURING execution)
|
||||
let output_result = tokio::time::timeout(
|
||||
timeout_duration,
|
||||
tokio::task::spawn_blocking(move || {
|
||||
cmd.output()
|
||||
})
|
||||
.await
|
||||
.map_err(|e| ZclawError::ToolError(format!("Task spawn error: {}", e)))?
|
||||
.map_err(|e| ZclawError::ToolError(format!("Command execution failed: {}", e)))?;
|
||||
).await;
|
||||
|
||||
let duration = start.elapsed();
|
||||
|
||||
// Check timeout
|
||||
if duration > Duration::from_secs(timeout_secs) {
|
||||
let output = match output_result {
|
||||
// Timeout triggered - command took too long
|
||||
Err(_) => {
|
||||
return Err(ZclawError::Timeout(
|
||||
format!("Command timed out after {} seconds", timeout_secs)
|
||||
));
|
||||
}
|
||||
// Spawn blocking task completed
|
||||
Ok(Ok(result)) => {
|
||||
result.map_err(|e| ZclawError::ToolError(format!("Command execution failed: {}", e)))?
|
||||
}
|
||||
// Spawn blocking task panicked or was cancelled
|
||||
Ok(Err(e)) => {
|
||||
return Err(ZclawError::ToolError(format!("Task spawn error: {}", e)));
|
||||
}
|
||||
};
|
||||
|
||||
let duration = start.elapsed();
|
||||
|
||||
// Truncate output if too large
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
@@ -271,4 +294,37 @@ mod tests {
|
||||
// Should block non-whitelisted commands
|
||||
assert!(config.is_command_allowed("dangerous_command").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_command_simple() {
|
||||
let (program, args) = parse_command("ls -la").unwrap();
|
||||
assert_eq!(program, "ls");
|
||||
assert_eq!(args, vec!["-la"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_command_with_quotes() {
|
||||
let (program, args) = parse_command("echo \"hello world\"").unwrap();
|
||||
assert_eq!(program, "echo");
|
||||
assert_eq!(args, vec!["hello world"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_command_with_single_quotes() {
|
||||
let (program, args) = parse_command("echo 'hello world'").unwrap();
|
||||
assert_eq!(program, "echo");
|
||||
assert_eq!(args, vec!["hello world"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_command_complex() {
|
||||
let (program, args) = parse_command("git commit -m \"Initial commit\"").unwrap();
|
||||
assert_eq!(program, "git");
|
||||
assert_eq!(args, vec!["commit", "-m", "Initial commit"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_command_empty() {
|
||||
assert!(parse_command("").is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,343 @@
|
||||
//! Web fetch tool
|
||||
//! Web fetch tool with SSRF protection
|
||||
//!
|
||||
//! This module provides a secure web fetching capability with comprehensive
|
||||
//! SSRF (Server-Side Request Forgery) protection including:
|
||||
//! - Private IP range blocking (RFC 1918)
|
||||
//! - Cloud metadata endpoint blocking (169.254.169.254)
|
||||
//! - Localhost/loopback blocking
|
||||
//! - Redirect protection with recursive checks
|
||||
//! - Timeout control
|
||||
//! - Response size limits
|
||||
|
||||
use async_trait::async_trait;
|
||||
use reqwest::redirect::Policy;
|
||||
use serde_json::{json, Value};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
use std::time::Duration;
|
||||
use url::Url;
|
||||
use zclaw_types::{Result, ZclawError};
|
||||
|
||||
use crate::tool::{Tool, ToolContext};
|
||||
|
||||
pub struct WebFetchTool;
|
||||
/// Maximum response size in bytes (10 MB)
|
||||
const MAX_RESPONSE_SIZE: u64 = 10 * 1024 * 1024;
|
||||
|
||||
/// Request timeout in seconds
|
||||
const REQUEST_TIMEOUT_SECS: u64 = 30;
|
||||
|
||||
/// Maximum number of redirect hops allowed
|
||||
const MAX_REDIRECT_HOPS: usize = 5;
|
||||
|
||||
/// Maximum URL length
|
||||
const MAX_URL_LENGTH: usize = 2048;
|
||||
|
||||
pub struct WebFetchTool {
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl WebFetchTool {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
// Build a client with redirect policy that we control
|
||||
// We'll handle redirects manually to validate each target
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(Duration::from_secs(REQUEST_TIMEOUT_SECS))
|
||||
.redirect(Policy::none()) // Handle redirects manually for SSRF validation
|
||||
.user_agent("ZCLAW/1.0")
|
||||
.build()
|
||||
.unwrap_or_else(|_| reqwest::Client::new());
|
||||
|
||||
Self { client }
|
||||
}
|
||||
|
||||
/// Validate a URL for SSRF safety
|
||||
///
|
||||
/// This checks:
|
||||
/// - URL scheme (only http/https allowed)
|
||||
/// - Private IP ranges (RFC 1918)
|
||||
/// - Loopback addresses
|
||||
/// - Cloud metadata endpoints
|
||||
/// - Link-local addresses
|
||||
fn validate_url(&self, url_str: &str) -> Result<Url> {
|
||||
// Check URL length
|
||||
if url_str.len() > MAX_URL_LENGTH {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"URL exceeds maximum length of {} characters",
|
||||
MAX_URL_LENGTH
|
||||
)));
|
||||
}
|
||||
|
||||
// Parse URL
|
||||
let url = Url::parse(url_str)
|
||||
.map_err(|e| ZclawError::InvalidInput(format!("Invalid URL: {}", e)))?;
|
||||
|
||||
// Check scheme - only allow http and https
|
||||
match url.scheme() {
|
||||
"http" | "https" => {}
|
||||
scheme => {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"URL scheme '{}' is not allowed. Only http and https are permitted.",
|
||||
scheme
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
// Extract host - for IPv6, url.host_str() returns the address without brackets
|
||||
// But url::Url also provides host() which gives us the parsed Host type
|
||||
let host = url
|
||||
.host_str()
|
||||
.ok_or_else(|| ZclawError::InvalidInput("URL must have a host".into()))?;
|
||||
|
||||
// Check if host is an IP address or domain
|
||||
// For IPv6 in URLs, host_str returns the address with brackets, e.g., "[::1]"
|
||||
// We need to strip the brackets for parsing
|
||||
let host_for_parsing = if host.starts_with('[') && host.ends_with(']') {
|
||||
&host[1..host.len()-1]
|
||||
} else {
|
||||
host
|
||||
};
|
||||
|
||||
if let Ok(ip) = host_for_parsing.parse::<IpAddr>() {
|
||||
self.validate_ip_address(&ip)?;
|
||||
} else {
|
||||
// For domain names, we need to resolve and check the IP
|
||||
// This is handled during the actual request, but we do basic checks here
|
||||
self.validate_hostname(host)?;
|
||||
}
|
||||
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
/// Validate an IP address for SSRF safety
|
||||
fn validate_ip_address(&self, ip: &IpAddr) -> Result<()> {
|
||||
match ip {
|
||||
IpAddr::V4(ipv4) => self.validate_ipv4(ipv4)?,
|
||||
IpAddr::V6(ipv6) => self.validate_ipv6(ipv6)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate IPv4 address
|
||||
fn validate_ipv4(&self, ip: &Ipv4Addr) -> Result<()> {
|
||||
let octets = ip.octets();
|
||||
|
||||
// Block loopback (127.0.0.0/8)
|
||||
if octets[0] == 127 {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to loopback addresses (127.x.x.x) is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block private ranges (RFC 1918)
|
||||
// 10.0.0.0/8
|
||||
if octets[0] == 10 {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to private IP range 10.x.x.x is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// 172.16.0.0/12 (172.16.0.0 - 172.31.255.255)
|
||||
if octets[0] == 172 && (16..=31).contains(&octets[1]) {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to private IP range 172.16-31.x.x is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// 192.168.0.0/16
|
||||
if octets[0] == 192 && octets[1] == 168 {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to private IP range 192.168.x.x is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block cloud metadata endpoint (169.254.169.254)
|
||||
if octets[0] == 169 && octets[1] == 254 && octets[2] == 169 && octets[3] == 254 {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to cloud metadata endpoint (169.254.169.254) is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block link-local addresses (169.254.0.0/16)
|
||||
if octets[0] == 169 && octets[1] == 254 {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to link-local addresses (169.254.x.x) is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block 0.0.0.0/8 (current network)
|
||||
if octets[0] == 0 {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to 0.x.x.x addresses is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block broadcast address
|
||||
if *ip == Ipv4Addr::new(255, 255, 255, 255) {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to broadcast address is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block multicast addresses (224.0.0.0/4)
|
||||
if (224..=239).contains(&octets[0]) {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to multicast addresses is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate IPv6 address
|
||||
fn validate_ipv6(&self, ip: &Ipv6Addr) -> Result<()> {
|
||||
// Block loopback (::1)
|
||||
if *ip == Ipv6Addr::LOCALHOST {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to IPv6 loopback address (::1) is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block unspecified address (::)
|
||||
if *ip == Ipv6Addr::UNSPECIFIED {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to unspecified IPv6 address (::) is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block IPv4-mapped IPv6 addresses (::ffff:0:0/96)
|
||||
// These could bypass IPv4 checks
|
||||
if ip.to_string().starts_with("::ffff:") {
|
||||
// Extract the embedded IPv4 and validate it
|
||||
let segments = ip.segments();
|
||||
// IPv4-mapped format: 0:0:0:0:0:ffff:xxxx:xxxx
|
||||
if segments[5] == 0xffff {
|
||||
let v4_addr = ((segments[6] as u32) << 16) | (segments[7] as u32);
|
||||
let ipv4 = Ipv4Addr::from(v4_addr);
|
||||
self.validate_ipv4(&ipv4)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Block link-local IPv6 (fe80::/10)
|
||||
let segments = ip.segments();
|
||||
if (segments[0] & 0xffc0) == 0xfe80 {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to IPv6 link-local addresses is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Block unique local addresses (fc00::/7) - IPv6 equivalent of private ranges
|
||||
if (segments[0] & 0xfe00) == 0xfc00 {
|
||||
return Err(ZclawError::InvalidInput(
|
||||
"Access to IPv6 unique local addresses is not allowed".into(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate a hostname for potential SSRF attacks
|
||||
fn validate_hostname(&self, host: &str) -> Result<()> {
|
||||
let host_lower = host.to_lowercase();
|
||||
|
||||
// Block localhost variants
|
||||
let blocked_hosts = [
|
||||
"localhost",
|
||||
"localhost.localdomain",
|
||||
"ip6-localhost",
|
||||
"ip6-loopback",
|
||||
"metadata.google.internal",
|
||||
"metadata",
|
||||
"kubernetes.default",
|
||||
"kubernetes.default.svc",
|
||||
];
|
||||
|
||||
for blocked in &blocked_hosts {
|
||||
if host_lower == *blocked || host_lower.ends_with(&format!(".{}", blocked)) {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"Access to '{}' is not allowed",
|
||||
host
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
// Block hostnames that look like IP addresses (decimal, octal, hex encoding)
|
||||
// These could be used to bypass IP checks
|
||||
self.check_hostname_ip_bypass(&host_lower)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check for hostname-based IP bypass attempts
|
||||
fn check_hostname_ip_bypass(&self, host: &str) -> Result<()> {
|
||||
// Check for decimal IP encoding (e.g., 2130706433 = 127.0.0.1)
|
||||
if host.chars().all(|c| c.is_ascii_digit()) {
|
||||
if let Ok(num) = host.parse::<u32>() {
|
||||
let ip = Ipv4Addr::from(num);
|
||||
self.validate_ipv4(&ip)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for domains that might resolve to private IPs
|
||||
// This is not exhaustive but catches common patterns
|
||||
// The actual DNS resolution check happens during the request
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Follow redirects with SSRF validation
|
||||
async fn follow_redirects_safe(&self, url: Url, max_hops: usize) -> Result<(Url, reqwest::Response)> {
|
||||
let mut current_url = url;
|
||||
let mut hops = 0;
|
||||
|
||||
loop {
|
||||
// Validate the current URL
|
||||
current_url = self.validate_url(current_url.as_str())?;
|
||||
|
||||
// Make the request
|
||||
let response = self
|
||||
.client
|
||||
.get(current_url.clone())
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| ZclawError::ToolError(format!("Request failed: {}", e)))?;
|
||||
|
||||
// Check if it's a redirect
|
||||
let status = response.status();
|
||||
if status.is_redirection() {
|
||||
hops += 1;
|
||||
if hops > max_hops {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"Too many redirects (max {})",
|
||||
max_hops
|
||||
)));
|
||||
}
|
||||
|
||||
// Get the redirect location
|
||||
let location = response
|
||||
.headers()
|
||||
.get(reqwest::header::LOCATION)
|
||||
.and_then(|h| h.to_str().ok())
|
||||
.ok_or_else(|| {
|
||||
ZclawError::ToolError("Redirect without Location header".into())
|
||||
})?;
|
||||
|
||||
// Resolve the location against the current URL
|
||||
let new_url = current_url.join(location).map_err(|e| {
|
||||
ZclawError::InvalidInput(format!("Invalid redirect location: {}", e))
|
||||
})?;
|
||||
|
||||
tracing::debug!(
|
||||
"Following redirect {} -> {}",
|
||||
current_url.as_str(),
|
||||
new_url.as_str()
|
||||
);
|
||||
|
||||
current_url = new_url;
|
||||
// Continue loop to validate and follow
|
||||
} else {
|
||||
// Not a redirect, return the response
|
||||
return Ok((current_url, response));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +348,7 @@ impl Tool for WebFetchTool {
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Fetch content from a URL"
|
||||
"Fetch content from a URL with SSRF protection"
|
||||
}
|
||||
|
||||
fn input_schema(&self) -> Value {
|
||||
@@ -30,12 +357,29 @@ impl Tool for WebFetchTool {
|
||||
"properties": {
|
||||
"url": {
|
||||
"type": "string",
|
||||
"description": "The URL to fetch"
|
||||
"description": "The URL to fetch (must be http or https)"
|
||||
},
|
||||
"method": {
|
||||
"type": "string",
|
||||
"enum": ["GET", "POST"],
|
||||
"description": "HTTP method (default: GET)"
|
||||
},
|
||||
"headers": {
|
||||
"type": "object",
|
||||
"description": "Optional HTTP headers (key-value pairs)",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"body": {
|
||||
"type": "string",
|
||||
"description": "Request body for POST requests"
|
||||
},
|
||||
"timeout": {
|
||||
"type": "integer",
|
||||
"description": "Timeout in seconds (default: 30, max: 60)",
|
||||
"minimum": 1,
|
||||
"maximum": 60
|
||||
}
|
||||
},
|
||||
"required": ["url"]
|
||||
@@ -43,13 +387,167 @@ impl Tool for WebFetchTool {
|
||||
}
|
||||
|
||||
async fn execute(&self, input: Value, _context: &ToolContext) -> Result<Value> {
|
||||
let url = input["url"].as_str()
|
||||
let url_str = input["url"]
|
||||
.as_str()
|
||||
.ok_or_else(|| ZclawError::InvalidInput("Missing 'url' parameter".into()))?;
|
||||
|
||||
// TODO: Implement actual web fetching with SSRF protection
|
||||
let method = input["method"].as_str().unwrap_or("GET").to_uppercase();
|
||||
let timeout_secs = input["timeout"].as_u64().unwrap_or(REQUEST_TIMEOUT_SECS).min(60);
|
||||
|
||||
// Validate URL for SSRF
|
||||
let url = self.validate_url(url_str)?;
|
||||
|
||||
tracing::info!("WebFetch: Fetching {} with method {}", url.as_str(), method);
|
||||
|
||||
// Build request with validated URL
|
||||
let mut request_builder = match method.as_str() {
|
||||
"GET" => self.client.get(url.clone()),
|
||||
"POST" => {
|
||||
let mut builder = self.client.post(url.clone());
|
||||
if let Some(body) = input["body"].as_str() {
|
||||
builder = builder.body(body.to_string());
|
||||
}
|
||||
builder
|
||||
}
|
||||
_ => {
|
||||
return Err(ZclawError::InvalidInput(format!(
|
||||
"Unsupported HTTP method: {}",
|
||||
method
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
// Add custom headers if provided
|
||||
if let Some(headers) = input["headers"].as_object() {
|
||||
for (key, value) in headers {
|
||||
if let Some(value_str) = value.as_str() {
|
||||
// Block dangerous headers
|
||||
let key_lower = key.to_lowercase();
|
||||
if key_lower == "host" {
|
||||
continue; // Don't allow overriding host
|
||||
}
|
||||
if key_lower.starts_with("x-forwarded") {
|
||||
continue; // Block proxy header injection
|
||||
}
|
||||
|
||||
let header_name = reqwest::header::HeaderName::try_from(key.as_str())
|
||||
.map_err(|e| {
|
||||
ZclawError::InvalidInput(format!("Invalid header name '{}': {}", key, e))
|
||||
})?;
|
||||
let header_value = reqwest::header::HeaderValue::from_str(value_str)
|
||||
.map_err(|e| {
|
||||
ZclawError::InvalidInput(format!("Invalid header value: {}", e))
|
||||
})?;
|
||||
request_builder = request_builder.header(header_name, header_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set timeout
|
||||
let request_builder = request_builder.timeout(Duration::from_secs(timeout_secs));
|
||||
|
||||
// Execute with redirect handling
|
||||
let response = request_builder
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
let error_msg = e.to_string();
|
||||
|
||||
// Provide user-friendly error messages
|
||||
if error_msg.contains("dns") || error_msg.contains("resolve") {
|
||||
ZclawError::ToolError(format!(
|
||||
"Failed to resolve hostname: {}. Please check the URL.",
|
||||
url.host_str().unwrap_or("unknown")
|
||||
))
|
||||
} else if error_msg.contains("timeout") {
|
||||
ZclawError::ToolError(format!(
|
||||
"Request timed out after {} seconds",
|
||||
timeout_secs
|
||||
))
|
||||
} else if error_msg.contains("connection refused") {
|
||||
ZclawError::ToolError(
|
||||
"Connection refused. The server may be down or unreachable.".into(),
|
||||
)
|
||||
} else {
|
||||
ZclawError::ToolError(format!("Request failed: {}", error_msg))
|
||||
}
|
||||
})?;
|
||||
|
||||
// Handle redirects manually with SSRF validation
|
||||
let (final_url, response) = if response.status().is_redirection() {
|
||||
// Start redirect following process
|
||||
let location = response
|
||||
.headers()
|
||||
.get(reqwest::header::LOCATION)
|
||||
.and_then(|h| h.to_str().ok())
|
||||
.ok_or_else(|| {
|
||||
ZclawError::ToolError("Redirect without Location header".into())
|
||||
})?;
|
||||
|
||||
let redirect_url = url.join(location).map_err(|e| {
|
||||
ZclawError::InvalidInput(format!("Invalid redirect location: {}", e))
|
||||
})?;
|
||||
|
||||
self.follow_redirects_safe(redirect_url, MAX_REDIRECT_HOPS).await?
|
||||
} else {
|
||||
(url, response)
|
||||
};
|
||||
|
||||
// Check response status
|
||||
let status = response.status();
|
||||
let status_code = status.as_u16();
|
||||
|
||||
// Check content length before reading body
|
||||
if let Some(content_length) = response.content_length() {
|
||||
if content_length > MAX_RESPONSE_SIZE {
|
||||
return Err(ZclawError::ToolError(format!(
|
||||
"Response too large: {} bytes (max: {} bytes)",
|
||||
content_length, MAX_RESPONSE_SIZE
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
// Get content type BEFORE consuming response with bytes()
|
||||
let content_type = response
|
||||
.headers()
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.and_then(|h| h.to_str().ok())
|
||||
.unwrap_or("text/plain")
|
||||
.to_string();
|
||||
|
||||
// Read response body with size limit
|
||||
let bytes = response.bytes().await.map_err(|e| {
|
||||
ZclawError::ToolError(format!("Failed to read response body: {}", e))
|
||||
})?;
|
||||
|
||||
// Double-check size after reading
|
||||
if bytes.len() as u64 > MAX_RESPONSE_SIZE {
|
||||
return Err(ZclawError::ToolError(format!(
|
||||
"Response too large: {} bytes (max: {} bytes)",
|
||||
bytes.len(),
|
||||
MAX_RESPONSE_SIZE
|
||||
)));
|
||||
}
|
||||
|
||||
// Try to decode as UTF-8, fall back to base64 for binary
|
||||
let content = String::from_utf8(bytes.to_vec()).unwrap_or_else(|_| {
|
||||
use base64::Engine;
|
||||
base64::engine::general_purpose::STANDARD.encode(&bytes)
|
||||
});
|
||||
|
||||
tracing::info!(
|
||||
"WebFetch: Successfully fetched {} bytes from {} (status: {})",
|
||||
content.len(),
|
||||
final_url.as_str(),
|
||||
status_code
|
||||
);
|
||||
|
||||
Ok(json!({
|
||||
"status": 200,
|
||||
"content": format!("Fetched content placeholder for: {}", url)
|
||||
"status": status_code,
|
||||
"url": final_url.as_str(),
|
||||
"content_type": content_type,
|
||||
"content": content,
|
||||
"size": content.len()
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -59,3 +557,91 @@ impl Default for WebFetchTool {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_validate_localhost() {
|
||||
let tool = WebFetchTool::new();
|
||||
|
||||
// Test localhost
|
||||
assert!(tool.validate_url("http://localhost/test").is_err());
|
||||
assert!(tool.validate_url("http://127.0.0.1/test").is_err());
|
||||
assert!(tool.validate_url("http://127.0.0.2/test").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_private_ips() {
|
||||
let tool = WebFetchTool::new();
|
||||
|
||||
// Test 10.x.x.x
|
||||
assert!(tool.validate_url("http://10.0.0.1/test").is_err());
|
||||
assert!(tool.validate_url("http://10.255.255.255/test").is_err());
|
||||
|
||||
// Test 172.16-31.x.x
|
||||
assert!(tool.validate_url("http://172.16.0.1/test").is_err());
|
||||
assert!(tool.validate_url("http://172.31.255.255/test").is_err());
|
||||
// 172.15.x.x should be allowed
|
||||
assert!(tool.validate_url("http://172.15.0.1/test").is_ok());
|
||||
|
||||
// Test 192.168.x.x
|
||||
assert!(tool.validate_url("http://192.168.0.1/test").is_err());
|
||||
assert!(tool.validate_url("http://192.168.255.255/test").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_cloud_metadata() {
|
||||
let tool = WebFetchTool::new();
|
||||
|
||||
// Test cloud metadata endpoint
|
||||
assert!(tool.validate_url("http://169.254.169.254/metadata").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_ipv6() {
|
||||
let tool = WebFetchTool::new();
|
||||
|
||||
// Test IPv6 loopback
|
||||
assert!(tool.validate_url("http://[::1]/test").is_err());
|
||||
|
||||
// Test IPv6 unspecified
|
||||
assert!(tool.validate_url("http://[::]/test").is_err());
|
||||
|
||||
// Test IPv4-mapped loopback
|
||||
assert!(tool.validate_url("http://[::ffff:127.0.0.1]/test").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_scheme() {
|
||||
let tool = WebFetchTool::new();
|
||||
|
||||
// Only http and https allowed
|
||||
assert!(tool.validate_url("ftp://example.com/test").is_err());
|
||||
assert!(tool.validate_url("file:///etc/passwd").is_err());
|
||||
assert!(tool.validate_url("javascript:alert(1)").is_err());
|
||||
|
||||
// http and https should be allowed (URL parsing succeeds)
|
||||
assert!(tool.validate_url("http://example.com/test").is_ok());
|
||||
assert!(tool.validate_url("https://example.com/test").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_blocked_hostnames() {
|
||||
let tool = WebFetchTool::new();
|
||||
|
||||
assert!(tool.validate_url("http://localhost/test").is_err());
|
||||
assert!(tool.validate_url("http://metadata.google.internal/test").is_err());
|
||||
assert!(tool.validate_url("http://kubernetes.default/test").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_url_length() {
|
||||
let tool = WebFetchTool::new();
|
||||
|
||||
// Create a URL that's too long
|
||||
let long_url = format!("http://example.com/{}", "a".repeat(3000));
|
||||
assert!(tool.validate_url(&long_url).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,6 +229,7 @@ impl PlanBuilder {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
use zclaw_types::SkillId;
|
||||
|
||||
fn make_test_graph() -> SkillGraph {
|
||||
use super::super::{SkillNode, SkillEdge};
|
||||
@@ -240,7 +241,7 @@ mod tests {
|
||||
nodes: vec![
|
||||
SkillNode {
|
||||
id: "research".to_string(),
|
||||
skill_id: "web-researcher".into(),
|
||||
skill_id: SkillId::new("web-researcher"),
|
||||
description: String::new(),
|
||||
input_mappings: HashMap::new(),
|
||||
retry: None,
|
||||
@@ -250,7 +251,7 @@ mod tests {
|
||||
},
|
||||
SkillNode {
|
||||
id: "summarize".to_string(),
|
||||
skill_id: "text-summarizer".into(),
|
||||
skill_id: SkillId::new("text-summarizer"),
|
||||
description: String::new(),
|
||||
input_mappings: HashMap::new(),
|
||||
retry: None,
|
||||
@@ -260,7 +261,7 @@ mod tests {
|
||||
},
|
||||
SkillNode {
|
||||
id: "translate".to_string(),
|
||||
skill_id: "translator".into(),
|
||||
skill_id: SkillId::new("translator"),
|
||||
description: String::new(),
|
||||
input_mappings: HashMap::new(),
|
||||
retry: None,
|
||||
@@ -306,7 +307,7 @@ mod tests {
|
||||
.description("Test graph")
|
||||
.node(super::super::SkillNode {
|
||||
id: "a".to_string(),
|
||||
skill_id: "skill-a".into(),
|
||||
skill_id: SkillId::new("skill-a"),
|
||||
description: String::new(),
|
||||
input_mappings: HashMap::new(),
|
||||
retry: None,
|
||||
@@ -316,7 +317,7 @@ mod tests {
|
||||
})
|
||||
.node(super::super::SkillNode {
|
||||
id: "b".to_string(),
|
||||
skill_id: "skill-b".into(),
|
||||
skill_id: SkillId::new("skill-b"),
|
||||
description: String::new(),
|
||||
input_mappings: HashMap::new(),
|
||||
retry: None,
|
||||
|
||||
@@ -316,6 +316,8 @@ pub fn build_dependency_map(graph: &SkillGraph) -> HashMap<String, Vec<String>>
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::super::{SkillNode, SkillEdge};
|
||||
use zclaw_types::SkillId;
|
||||
|
||||
fn make_simple_graph() -> SkillGraph {
|
||||
SkillGraph {
|
||||
@@ -325,7 +327,7 @@ mod tests {
|
||||
nodes: vec![
|
||||
SkillNode {
|
||||
id: "a".to_string(),
|
||||
skill_id: "skill-a".into(),
|
||||
skill_id: SkillId::new("skill-a"),
|
||||
description: String::new(),
|
||||
input_mappings: HashMap::new(),
|
||||
retry: None,
|
||||
@@ -335,7 +337,7 @@ mod tests {
|
||||
},
|
||||
SkillNode {
|
||||
id: "b".to_string(),
|
||||
skill_id: "skill-b".into(),
|
||||
skill_id: SkillId::new("skill-b"),
|
||||
description: String::new(),
|
||||
input_mappings: HashMap::new(),
|
||||
retry: None,
|
||||
|
||||
@@ -139,7 +139,7 @@ impl Skill for ShellSkill {
|
||||
.map_err(|e| zclaw_types::ZclawError::ToolError(format!("Failed to execute shell: {}", e)))?
|
||||
};
|
||||
|
||||
let duration_ms = start.elapsed().as_millis() as u64;
|
||||
let _duration_ms = start.elapsed().as_millis() as u64;
|
||||
|
||||
if output.status.success() {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
@@ -62,3 +62,119 @@ pub enum ZclawError {
|
||||
|
||||
/// Result type alias for ZCLAW operations
|
||||
pub type Result<T> = std::result::Result<T, ZclawError>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_not_found_display() {
|
||||
let err = ZclawError::NotFound("agent-123".to_string());
|
||||
assert_eq!(err.to_string(), "Not found: agent-123");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_permission_denied_display() {
|
||||
let err = ZclawError::PermissionDenied("unauthorized access".to_string());
|
||||
assert_eq!(err.to_string(), "Permission denied: unauthorized access");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_llm_error_display() {
|
||||
let err = ZclawError::LlmError("API rate limit".to_string());
|
||||
assert_eq!(err.to_string(), "LLM error: API rate limit");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_error_display() {
|
||||
let err = ZclawError::ToolError("execution failed".to_string());
|
||||
assert_eq!(err.to_string(), "Tool error: execution failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_storage_error_display() {
|
||||
let err = ZclawError::StorageError("disk full".to_string());
|
||||
assert_eq!(err.to_string(), "Storage error: disk full");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_error_display() {
|
||||
let err = ZclawError::ConfigError("missing field".to_string());
|
||||
assert_eq!(err.to_string(), "Configuration error: missing field");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_timeout_display() {
|
||||
let err = ZclawError::Timeout("30s exceeded".to_string());
|
||||
assert_eq!(err.to_string(), "Timeout: 30s exceeded");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_input_display() {
|
||||
let err = ZclawError::InvalidInput("empty string".to_string());
|
||||
assert_eq!(err.to_string(), "Invalid input: empty string");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_loop_detected_display() {
|
||||
let err = ZclawError::LoopDetected("max iterations".to_string());
|
||||
assert_eq!(err.to_string(), "Agent loop detected: max iterations");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rate_limited_display() {
|
||||
let err = ZclawError::RateLimited("100 req/min".to_string());
|
||||
assert_eq!(err.to_string(), "Rate limited: 100 req/min");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_internal_error_display() {
|
||||
let err = ZclawError::Internal("unexpected state".to_string());
|
||||
assert_eq!(err.to_string(), "Internal error: unexpected state");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_export_error_display() {
|
||||
let err = ZclawError::ExportError("PDF generation failed".to_string());
|
||||
assert_eq!(err.to_string(), "Export error: PDF generation failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mcp_error_display() {
|
||||
let err = ZclawError::McpError("connection refused".to_string());
|
||||
assert_eq!(err.to_string(), "MCP error: connection refused");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_security_error_display() {
|
||||
let err = ZclawError::SecurityError("path traversal".to_string());
|
||||
assert_eq!(err.to_string(), "Security error: path traversal");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hand_error_display() {
|
||||
let err = ZclawError::HandError("browser launch failed".to_string());
|
||||
assert_eq!(err.to_string(), "Hand error: browser launch failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialization_error_from_json() {
|
||||
let json_err = serde_json::from_str::<serde_json::Value>("invalid json");
|
||||
let zclaw_err = ZclawError::from(json_err.unwrap_err());
|
||||
assert!(matches!(zclaw_err, ZclawError::SerializationError(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_result_type_ok() {
|
||||
let result: Result<i32> = Ok(42);
|
||||
assert!(result.is_ok());
|
||||
assert_eq!(result.unwrap(), 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_result_type_err() {
|
||||
let result: Result<i32> = Err(ZclawError::NotFound("test".to_string()));
|
||||
assert!(result.is_err());
|
||||
assert!(matches!(result.unwrap_err(), ZclawError::NotFound(_)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,3 +145,114 @@ impl std::fmt::Display for RunId {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_agent_id_new_creates_unique_ids() {
|
||||
let id1 = AgentId::new();
|
||||
let id2 = AgentId::new();
|
||||
assert_ne!(id1, id2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_agent_id_default() {
|
||||
let id = AgentId::default();
|
||||
assert!(!id.0.is_nil());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_agent_id_display() {
|
||||
let id = AgentId::new();
|
||||
let display = format!("{}", id);
|
||||
assert_eq!(display.len(), 36); // UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
||||
assert!(display.contains('-'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_agent_id_from_str_valid() {
|
||||
let id = AgentId::new();
|
||||
let id_str = id.to_string();
|
||||
let parsed: AgentId = id_str.parse().unwrap();
|
||||
assert_eq!(id, parsed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_agent_id_from_str_invalid() {
|
||||
let result: Result<AgentId, _> = "invalid-uuid".parse();
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_agent_id_serialization() {
|
||||
let id = AgentId::new();
|
||||
let json = serde_json::to_string(&id).unwrap();
|
||||
let deserialized: AgentId = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(id, deserialized);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_session_id_new_creates_unique_ids() {
|
||||
let id1 = SessionId::new();
|
||||
let id2 = SessionId::new();
|
||||
assert_ne!(id1, id2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_session_id_default() {
|
||||
let id = SessionId::default();
|
||||
assert!(!id.0.is_nil());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_id_new() {
|
||||
let id = ToolId::new("test_tool");
|
||||
assert_eq!(id.as_str(), "test_tool");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_id_from_str() {
|
||||
let id: ToolId = "browser".into();
|
||||
assert_eq!(id.as_str(), "browser");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_id_from_string() {
|
||||
let id: ToolId = String::from("shell").into();
|
||||
assert_eq!(id.as_str(), "shell");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tool_id_display() {
|
||||
let id = ToolId::new("test");
|
||||
assert_eq!(format!("{}", id), "test");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skill_id_new() {
|
||||
let id = SkillId::new("coding");
|
||||
assert_eq!(id.as_str(), "coding");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_run_id_new_creates_unique_ids() {
|
||||
let id1 = RunId::new();
|
||||
let id2 = RunId::new();
|
||||
assert_ne!(id1, id2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_run_id_default() {
|
||||
let id = RunId::default();
|
||||
assert!(!id.0.is_nil());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_run_id_display() {
|
||||
let id = RunId::new();
|
||||
let display = format!("{}", id);
|
||||
assert_eq!(display.len(), 36);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,3 +161,189 @@ impl ImageSource {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_message_user_creation() {
|
||||
let msg = Message::user("Hello, world!");
|
||||
assert!(msg.is_user());
|
||||
assert_eq!(msg.role(), "user");
|
||||
assert!(!msg.is_assistant());
|
||||
assert!(!msg.is_tool_use());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_assistant_creation() {
|
||||
let msg = Message::assistant("Hello!");
|
||||
assert!(msg.is_assistant());
|
||||
assert_eq!(msg.role(), "assistant");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_assistant_with_thinking() {
|
||||
let msg = Message::assistant_with_thinking("Response", "My reasoning...");
|
||||
assert!(msg.is_assistant());
|
||||
|
||||
if let Message::Assistant { content, thinking } = msg {
|
||||
assert_eq!(content, "Response");
|
||||
assert_eq!(thinking, Some("My reasoning...".to_string()));
|
||||
} else {
|
||||
panic!("Expected Assistant message");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_tool_use_creation() {
|
||||
let input = serde_json::json!({"query": "test"});
|
||||
let msg = Message::tool_use("call-123", ToolId::new("search"), input.clone());
|
||||
assert!(msg.is_tool_use());
|
||||
assert_eq!(msg.role(), "tool_use");
|
||||
|
||||
if let Message::ToolUse { id, tool, input: i } = msg {
|
||||
assert_eq!(id, "call-123");
|
||||
assert_eq!(tool.as_str(), "search");
|
||||
assert_eq!(i, input);
|
||||
} else {
|
||||
panic!("Expected ToolUse message");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_tool_result_creation() {
|
||||
let output = serde_json::json!({"result": "success"});
|
||||
let msg = Message::tool_result("call-123", ToolId::new("search"), output.clone(), false);
|
||||
assert!(msg.is_tool_result());
|
||||
assert_eq!(msg.role(), "tool_result");
|
||||
|
||||
if let Message::ToolResult { tool_call_id, tool, output: o, is_error } = msg {
|
||||
assert_eq!(tool_call_id, "call-123");
|
||||
assert_eq!(tool.as_str(), "search");
|
||||
assert_eq!(o, output);
|
||||
assert!(!is_error);
|
||||
} else {
|
||||
panic!("Expected ToolResult message");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_tool_result_error() {
|
||||
let output = serde_json::json!({"error": "failed"});
|
||||
let msg = Message::tool_result("call-456", ToolId::new("exec"), output, true);
|
||||
|
||||
if let Message::ToolResult { is_error, .. } = msg {
|
||||
assert!(is_error);
|
||||
} else {
|
||||
panic!("Expected ToolResult message");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_system_creation() {
|
||||
let msg = Message::system("You are a helpful assistant.");
|
||||
assert_eq!(msg.role(), "system");
|
||||
assert!(!msg.is_user());
|
||||
assert!(!msg.is_assistant());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_serialization_user() {
|
||||
let msg = Message::user("Test message");
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert!(json.contains("\"role\":\"user\""));
|
||||
assert!(json.contains("\"content\":\"Test message\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_serialization_assistant() {
|
||||
let msg = Message::assistant("Response");
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert!(json.contains("\"role\":\"assistant\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_message_deserialization_user() {
|
||||
let json = r#"{"role":"user","content":"Hello"}"#;
|
||||
let msg: Message = serde_json::from_str(json).unwrap();
|
||||
assert!(msg.is_user());
|
||||
|
||||
if let Message::User { content } = msg {
|
||||
assert_eq!(content, "Hello");
|
||||
} else {
|
||||
panic!("Expected User message");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_text() {
|
||||
let block = ContentBlock::Text { text: "Hello".to_string() };
|
||||
let json = serde_json::to_string(&block).unwrap();
|
||||
assert!(json.contains("\"type\":\"text\""));
|
||||
assert!(json.contains("\"text\":\"Hello\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_thinking() {
|
||||
let block = ContentBlock::Thinking { thinking: "Reasoning...".to_string() };
|
||||
let json = serde_json::to_string(&block).unwrap();
|
||||
assert!(json.contains("\"type\":\"thinking\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_tool_use() {
|
||||
let block = ContentBlock::ToolUse {
|
||||
id: "tool-1".to_string(),
|
||||
name: "search".to_string(),
|
||||
input: serde_json::json!({"q": "test"}),
|
||||
};
|
||||
let json = serde_json::to_string(&block).unwrap();
|
||||
assert!(json.contains("\"type\":\"tool_use\""));
|
||||
assert!(json.contains("\"name\":\"search\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_tool_result() {
|
||||
let block = ContentBlock::ToolResult {
|
||||
tool_use_id: "tool-1".to_string(),
|
||||
content: "Success".to_string(),
|
||||
is_error: false,
|
||||
};
|
||||
let json = serde_json::to_string(&block).unwrap();
|
||||
assert!(json.contains("\"type\":\"tool_result\""));
|
||||
assert!(json.contains("\"is_error\":false"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_image() {
|
||||
let source = ImageSource::base64("image/png", "base64data");
|
||||
let block = ContentBlock::Image { source };
|
||||
let json = serde_json::to_string(&block).unwrap();
|
||||
assert!(json.contains("\"type\":\"image\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_image_source_base64() {
|
||||
let source = ImageSource::base64("image/png", "abc123");
|
||||
assert_eq!(source.source_type, "base64");
|
||||
assert_eq!(source.media_type, "image/png");
|
||||
assert_eq!(source.data, "abc123");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_image_source_url() {
|
||||
let source = ImageSource::url("https://example.com/image.png");
|
||||
assert_eq!(source.source_type, "url");
|
||||
assert_eq!(source.media_type, "image/*");
|
||||
assert_eq!(source.data, "https://example.com/image.png");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_image_source_serialization() {
|
||||
let source = ImageSource::base64("image/jpeg", "data123");
|
||||
let json = serde_json::to_string(&source).unwrap();
|
||||
assert!(json.contains("\"type\":\"base64\""));
|
||||
assert!(json.contains("\"media_type\":\"image/jpeg\""));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
//!
|
||||
//! Phase 2 of Intelligence Layer Migration.
|
||||
//! Reference: ZCLAW_AGENT_INTELLIGENCE_EVOLUTION.md §6.3.1
|
||||
//!
|
||||
//! NOTE: Some configuration methods are reserved for future dynamic adjustment.
|
||||
|
||||
#![allow(dead_code)] // Configuration methods reserved for future dynamic compaction tuning
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use regex::Regex;
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
//!
|
||||
//! Phase 2 of Intelligence Layer Migration.
|
||||
//! Reference: ZCLAW_AGENT_INTELLIGENCE_EVOLUTION.md §6.4.1
|
||||
//!
|
||||
//! NOTE: Some methods are reserved for future proactive features.
|
||||
|
||||
#![allow(dead_code)] // Methods reserved for future proactive features
|
||||
|
||||
use chrono::{Local, Timelike};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -9,13 +9,17 @@
|
||||
//!
|
||||
//! Phase 3 of Intelligence Layer Migration.
|
||||
//! Reference: ZCLAW_AGENT_INTELLIGENCE_EVOLUTION.md §6.2.3
|
||||
//!
|
||||
//! NOTE: Some methods are reserved for future integration.
|
||||
|
||||
#![allow(dead_code)] // Methods reserved for future identity management features
|
||||
|
||||
use chrono::Utc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tracing::{error, warn};
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
// === Types ===
|
||||
|
||||
@@ -169,11 +173,11 @@ impl AgentIdentityManager {
|
||||
self.proposals = store.proposals;
|
||||
self.snapshots = store.snapshots;
|
||||
self.snapshot_counter = store.snapshot_counter;
|
||||
eprintln!(
|
||||
"[IdentityManager] Loaded {} identities, {} proposals, {} snapshots",
|
||||
self.identities.len(),
|
||||
self.proposals.len(),
|
||||
self.snapshots.len()
|
||||
debug!(
|
||||
identities_count = self.identities.len(),
|
||||
proposals_count = self.proposals.len(),
|
||||
snapshots_count = self.snapshots.len(),
|
||||
"[IdentityManager] Loaded identity data from disk"
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
397
desktop/src-tauri/src/intelligence/mesh.rs
Normal file
397
desktop/src-tauri/src/intelligence/mesh.rs
Normal file
@@ -0,0 +1,397 @@
|
||||
//! Adaptive Intelligence Mesh - Coordinates Memory, Pipeline, and Heartbeat
|
||||
//!
|
||||
//! This module provides proactive workflow recommendations based on user behavior patterns.
|
||||
//! It integrates with:
|
||||
//! - PatternDetector for behavior pattern detection
|
||||
//! - WorkflowRecommender for generating recommendations
|
||||
//! - HeartbeatEngine for periodic checks
|
||||
//! - PersistentMemoryStore for historical data
|
||||
//! - PipelineExecutor for workflow execution
|
||||
//!
|
||||
//! NOTE: Some methods are reserved for future integration with the UI.
|
||||
|
||||
#![allow(dead_code)] // Methods reserved for future UI integration
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{broadcast, Mutex};
|
||||
|
||||
use super::pattern_detector::{BehaviorPattern, PatternContext, PatternDetector};
|
||||
use super::recommender::WorkflowRecommender;
|
||||
|
||||
// === Types ===
|
||||
|
||||
/// Workflow recommendation generated by the mesh
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct WorkflowRecommendation {
|
||||
/// Unique recommendation identifier
|
||||
pub id: String,
|
||||
/// Pipeline ID to recommend
|
||||
pub pipeline_id: String,
|
||||
/// Confidence score (0.0-1.0)
|
||||
pub confidence: f32,
|
||||
/// Human-readable reason for recommendation
|
||||
pub reason: String,
|
||||
/// Suggested input values
|
||||
pub suggested_inputs: HashMap<String, serde_json::Value>,
|
||||
/// Pattern IDs that matched
|
||||
pub patterns_matched: Vec<String>,
|
||||
/// When this recommendation was generated
|
||||
pub timestamp: DateTime<Utc>,
|
||||
}
|
||||
|
||||
/// Mesh coordinator configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MeshConfig {
|
||||
/// Enable mesh recommendations
|
||||
pub enabled: bool,
|
||||
/// Minimum confidence threshold for recommendations
|
||||
pub min_confidence: f32,
|
||||
/// Maximum recommendations to generate per analysis
|
||||
pub max_recommendations: usize,
|
||||
/// Hours to look back for pattern analysis
|
||||
pub analysis_window_hours: u64,
|
||||
}
|
||||
|
||||
impl Default for MeshConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: true,
|
||||
min_confidence: 0.6,
|
||||
max_recommendations: 5,
|
||||
analysis_window_hours: 24,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Analysis result from mesh coordinator
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MeshAnalysisResult {
|
||||
/// Generated recommendations
|
||||
pub recommendations: Vec<WorkflowRecommendation>,
|
||||
/// Patterns detected
|
||||
pub patterns_detected: usize,
|
||||
/// Analysis timestamp
|
||||
pub timestamp: DateTime<Utc>,
|
||||
}
|
||||
|
||||
// === Mesh Coordinator ===
|
||||
|
||||
/// Main mesh coordinator that integrates pattern detection and recommendations
|
||||
pub struct MeshCoordinator {
|
||||
/// Agent ID
|
||||
#[allow(dead_code)] // Reserved for multi-agent scenarios
|
||||
agent_id: String,
|
||||
/// Configuration
|
||||
config: Arc<Mutex<MeshConfig>>,
|
||||
/// Pattern detector
|
||||
pattern_detector: Arc<Mutex<PatternDetector>>,
|
||||
/// Workflow recommender
|
||||
recommender: Arc<Mutex<WorkflowRecommender>>,
|
||||
/// Recommendation sender
|
||||
#[allow(dead_code)] // Reserved for real-time recommendation streaming
|
||||
recommendation_sender: broadcast::Sender<WorkflowRecommendation>,
|
||||
/// Last analysis timestamp
|
||||
last_analysis: Arc<Mutex<Option<DateTime<Utc>>>>,
|
||||
}
|
||||
|
||||
impl MeshCoordinator {
|
||||
/// Create a new mesh coordinator
|
||||
pub fn new(agent_id: String, config: Option<MeshConfig>) -> Self {
|
||||
let (sender, _) = broadcast::channel(100);
|
||||
let config = config.unwrap_or_default();
|
||||
|
||||
Self {
|
||||
agent_id,
|
||||
config: Arc::new(Mutex::new(config)),
|
||||
pattern_detector: Arc::new(Mutex::new(PatternDetector::new(None))),
|
||||
recommender: Arc::new(Mutex::new(WorkflowRecommender::new(None))),
|
||||
recommendation_sender: sender,
|
||||
last_analysis: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Analyze current context and generate recommendations
|
||||
pub async fn analyze(&self) -> Result<MeshAnalysisResult, String> {
|
||||
let config = self.config.lock().await.clone();
|
||||
|
||||
if !config.enabled {
|
||||
return Ok(MeshAnalysisResult {
|
||||
recommendations: vec![],
|
||||
patterns_detected: 0,
|
||||
timestamp: Utc::now(),
|
||||
});
|
||||
}
|
||||
|
||||
// Get patterns from detector (clone to avoid borrow issues)
|
||||
let patterns: Vec<BehaviorPattern> = {
|
||||
let detector = self.pattern_detector.lock().await;
|
||||
let patterns_ref = detector.get_patterns();
|
||||
patterns_ref.into_iter().cloned().collect()
|
||||
};
|
||||
let patterns_detected = patterns.len();
|
||||
|
||||
// Generate recommendations from patterns
|
||||
let recommender = self.recommender.lock().await;
|
||||
let pattern_refs: Vec<&BehaviorPattern> = patterns.iter().collect();
|
||||
let mut recommendations = recommender.recommend(&pattern_refs);
|
||||
|
||||
// Filter by confidence
|
||||
recommendations.retain(|r| r.confidence >= config.min_confidence);
|
||||
|
||||
// Limit count
|
||||
recommendations.truncate(config.max_recommendations);
|
||||
|
||||
// Update timestamps
|
||||
for rec in &mut recommendations {
|
||||
rec.timestamp = Utc::now();
|
||||
}
|
||||
|
||||
// Update last analysis time
|
||||
*self.last_analysis.lock().await = Some(Utc::now());
|
||||
|
||||
Ok(MeshAnalysisResult {
|
||||
recommendations: recommendations.clone(),
|
||||
patterns_detected,
|
||||
timestamp: Utc::now(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Record user activity for pattern detection
|
||||
pub async fn record_activity(
|
||||
&self,
|
||||
activity_type: ActivityType,
|
||||
context: PatternContext,
|
||||
) -> Result<(), String> {
|
||||
let mut detector = self.pattern_detector.lock().await;
|
||||
|
||||
match activity_type {
|
||||
ActivityType::SkillUsed { skill_ids } => {
|
||||
detector.record_skill_usage(skill_ids);
|
||||
}
|
||||
ActivityType::PipelineExecuted {
|
||||
task_type,
|
||||
pipeline_id,
|
||||
} => {
|
||||
detector.record_pipeline_execution(&task_type, &pipeline_id, context);
|
||||
}
|
||||
ActivityType::InputReceived { keywords, intent } => {
|
||||
detector.record_input_pattern(keywords, &intent, context);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Subscribe to recommendations
|
||||
pub fn subscribe(&self) -> broadcast::Receiver<WorkflowRecommendation> {
|
||||
self.recommendation_sender.subscribe()
|
||||
}
|
||||
|
||||
/// Get current patterns
|
||||
pub async fn get_patterns(&self) -> Vec<BehaviorPattern> {
|
||||
let detector = self.pattern_detector.lock().await;
|
||||
detector.get_patterns().into_iter().cloned().collect()
|
||||
}
|
||||
|
||||
/// Decay old patterns (call periodically)
|
||||
pub async fn decay_patterns(&self) {
|
||||
let mut detector = self.pattern_detector.lock().await;
|
||||
detector.decay_patterns();
|
||||
}
|
||||
|
||||
/// Update configuration
|
||||
pub async fn update_config(&self, config: MeshConfig) {
|
||||
*self.config.lock().await = config;
|
||||
}
|
||||
|
||||
/// Get configuration
|
||||
pub async fn get_config(&self) -> MeshConfig {
|
||||
self.config.lock().await.clone()
|
||||
}
|
||||
|
||||
/// Record a user correction (for pattern refinement)
|
||||
pub async fn record_correction(&self, correction_type: &str) {
|
||||
let mut detector = self.pattern_detector.lock().await;
|
||||
// Record as input pattern with negative signal
|
||||
detector.record_input_pattern(
|
||||
vec![format!("correction:{}", correction_type)],
|
||||
"user_preference",
|
||||
PatternContext::default(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Get recommendation count
|
||||
pub async fn recommendation_count(&self) -> usize {
|
||||
let recommender = self.recommender.lock().await;
|
||||
recommender.recommendation_count()
|
||||
}
|
||||
|
||||
/// Accept a recommendation (returns the accepted recommendation)
|
||||
pub async fn accept_recommendation(&self, recommendation_id: &str) -> Option<WorkflowRecommendation> {
|
||||
let mut recommender = self.recommender.lock().await;
|
||||
recommender.accept_recommendation(recommendation_id)
|
||||
}
|
||||
|
||||
/// Dismiss a recommendation (returns true if found and dismissed)
|
||||
pub async fn dismiss_recommendation(&self, recommendation_id: &str) -> bool {
|
||||
let mut recommender = self.recommender.lock().await;
|
||||
recommender.dismiss_recommendation(recommendation_id)
|
||||
}
|
||||
}
|
||||
|
||||
/// Types of user activities that can be recorded
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum ActivityType {
|
||||
/// Skills were used together
|
||||
SkillUsed { skill_ids: Vec<String> },
|
||||
/// A pipeline was executed
|
||||
PipelineExecuted { task_type: String, pipeline_id: String },
|
||||
/// User input was received
|
||||
InputReceived { keywords: Vec<String>, intent: String },
|
||||
}
|
||||
|
||||
// === Tauri Commands ===
|
||||
|
||||
/// Mesh coordinator state for Tauri
|
||||
pub type MeshCoordinatorState = Arc<Mutex<HashMap<String, MeshCoordinator>>>;
|
||||
|
||||
/// Initialize mesh coordinator for an agent
|
||||
#[tauri::command]
|
||||
pub async fn mesh_init(
|
||||
agent_id: String,
|
||||
config: Option<MeshConfig>,
|
||||
state: tauri::State<'_, MeshCoordinatorState>,
|
||||
) -> Result<(), String> {
|
||||
let coordinator = MeshCoordinator::new(agent_id.clone(), config);
|
||||
let mut coordinators = state.lock().await;
|
||||
coordinators.insert(agent_id, coordinator);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Analyze and get recommendations
|
||||
#[tauri::command]
|
||||
pub async fn mesh_analyze(
|
||||
agent_id: String,
|
||||
state: tauri::State<'_, MeshCoordinatorState>,
|
||||
) -> Result<MeshAnalysisResult, String> {
|
||||
let coordinators = state.lock().await;
|
||||
let coordinator = coordinators
|
||||
.get(&agent_id)
|
||||
.ok_or_else(|| format!("Mesh coordinator not initialized for agent: {}", agent_id))?;
|
||||
coordinator.analyze().await
|
||||
}
|
||||
|
||||
/// Record user activity
|
||||
#[tauri::command]
|
||||
pub async fn mesh_record_activity(
|
||||
agent_id: String,
|
||||
activity_type: ActivityType,
|
||||
context: PatternContext,
|
||||
state: tauri::State<'_, MeshCoordinatorState>,
|
||||
) -> Result<(), String> {
|
||||
let coordinators = state.lock().await;
|
||||
let coordinator = coordinators
|
||||
.get(&agent_id)
|
||||
.ok_or_else(|| format!("Mesh coordinator not initialized for agent: {}", agent_id))?;
|
||||
coordinator.record_activity(activity_type, context).await
|
||||
}
|
||||
|
||||
/// Get current patterns
|
||||
#[tauri::command]
|
||||
pub async fn mesh_get_patterns(
|
||||
agent_id: String,
|
||||
state: tauri::State<'_, MeshCoordinatorState>,
|
||||
) -> Result<Vec<BehaviorPattern>, String> {
|
||||
let coordinators = state.lock().await;
|
||||
let coordinator = coordinators
|
||||
.get(&agent_id)
|
||||
.ok_or_else(|| format!("Mesh coordinator not initialized for agent: {}", agent_id))?;
|
||||
Ok(coordinator.get_patterns().await)
|
||||
}
|
||||
|
||||
/// Update mesh configuration
|
||||
#[tauri::command]
|
||||
pub async fn mesh_update_config(
|
||||
agent_id: String,
|
||||
config: MeshConfig,
|
||||
state: tauri::State<'_, MeshCoordinatorState>,
|
||||
) -> Result<(), String> {
|
||||
let coordinators = state.lock().await;
|
||||
let coordinator = coordinators
|
||||
.get(&agent_id)
|
||||
.ok_or_else(|| format!("Mesh coordinator not initialized for agent: {}", agent_id))?;
|
||||
coordinator.update_config(config).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Decay old patterns
|
||||
#[tauri::command]
|
||||
pub async fn mesh_decay_patterns(
|
||||
agent_id: String,
|
||||
state: tauri::State<'_, MeshCoordinatorState>,
|
||||
) -> Result<(), String> {
|
||||
let coordinators = state.lock().await;
|
||||
let coordinator = coordinators
|
||||
.get(&agent_id)
|
||||
.ok_or_else(|| format!("Mesh coordinator not initialized for agent: {}", agent_id))?;
|
||||
coordinator.decay_patterns().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Accept a recommendation (removes it and returns the accepted recommendation)
|
||||
#[tauri::command]
|
||||
pub async fn mesh_accept_recommendation(
|
||||
agent_id: String,
|
||||
recommendation_id: String,
|
||||
state: tauri::State<'_, MeshCoordinatorState>,
|
||||
) -> Result<Option<WorkflowRecommendation>, String> {
|
||||
let coordinators = state.lock().await;
|
||||
let coordinator = coordinators
|
||||
.get(&agent_id)
|
||||
.ok_or_else(|| format!("Mesh coordinator not initialized for agent: {}", agent_id))?;
|
||||
Ok(coordinator.accept_recommendation(&recommendation_id).await)
|
||||
}
|
||||
|
||||
/// Dismiss a recommendation (removes it without acting on it)
|
||||
#[tauri::command]
|
||||
pub async fn mesh_dismiss_recommendation(
|
||||
agent_id: String,
|
||||
recommendation_id: String,
|
||||
state: tauri::State<'_, MeshCoordinatorState>,
|
||||
) -> Result<bool, String> {
|
||||
let coordinators = state.lock().await;
|
||||
let coordinator = coordinators
|
||||
.get(&agent_id)
|
||||
.ok_or_else(|| format!("Mesh coordinator not initialized for agent: {}", agent_id))?;
|
||||
Ok(coordinator.dismiss_recommendation(&recommendation_id).await)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_mesh_config_default() {
|
||||
let config = MeshConfig::default();
|
||||
assert!(config.enabled);
|
||||
assert_eq!(config.min_confidence, 0.6);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_mesh_coordinator_creation() {
|
||||
let coordinator = MeshCoordinator::new("test_agent".to_string(), None);
|
||||
let config = coordinator.get_config().await;
|
||||
assert!(config.enabled);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_mesh_analysis() {
|
||||
let coordinator = MeshCoordinator::new("test_agent".to_string(), None);
|
||||
let result = coordinator.analyze().await;
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,11 @@
|
||||
//! - `compactor` - Context compaction for infinite-length conversations
|
||||
//! - `reflection` - Agent self-improvement through conversation analysis
|
||||
//! - `identity` - Agent identity file management (SOUL.md, AGENTS.md, USER.md)
|
||||
//! - `pattern_detector` - Behavior pattern detection for adaptive mesh
|
||||
//! - `recommender` - Workflow recommendation engine
|
||||
//! - `mesh` - Adaptive Intelligence Mesh coordinator
|
||||
//! - `trigger_evaluator` - Context-aware hand triggers with semantic matching
|
||||
//! - `persona_evolver` - Memory-powered persona evolution system
|
||||
//!
|
||||
//! ## Migration Status
|
||||
//!
|
||||
@@ -18,8 +23,13 @@
|
||||
//! | Context Compactor | ✅ Phase 2 | Complete |
|
||||
//! | Reflection Engine | ✅ Phase 3 | Complete |
|
||||
//! | Agent Identity | ✅ Phase 3 | Complete |
|
||||
//! | Agent Swarm | 🚧 Phase 3 | TODO |
|
||||
//! | Vector Memory | 📋 Phase 4 | Planned |
|
||||
//! | Pattern Detector | ✅ Phase 4 | Complete |
|
||||
//! | Workflow Recommender | ✅ Phase 4 | Complete |
|
||||
//! | Adaptive Mesh | ✅ Phase 4 | Complete |
|
||||
//! | Trigger Evaluator | ✅ Phase 4 | Complete |
|
||||
//! | Persona Evolver | ✅ Phase 4 | Complete |
|
||||
//! | Agent Swarm | 🚧 Phase 4 | TODO |
|
||||
//! | Vector Memory | 📋 Phase 5 | Planned |
|
||||
//!
|
||||
//! Reference: docs/plans/INTELLIGENCE-LAYER-MIGRATION.md
|
||||
|
||||
@@ -27,12 +37,47 @@ pub mod heartbeat;
|
||||
pub mod compactor;
|
||||
pub mod reflection;
|
||||
pub mod identity;
|
||||
pub mod pattern_detector;
|
||||
pub mod recommender;
|
||||
pub mod mesh;
|
||||
pub mod trigger_evaluator;
|
||||
pub mod persona_evolver;
|
||||
pub mod validation;
|
||||
|
||||
// Re-export main types for convenience
|
||||
// These exports are reserved for external use and future integration
|
||||
#[allow(unused_imports)]
|
||||
pub use heartbeat::HeartbeatEngineState;
|
||||
#[allow(unused_imports)]
|
||||
pub use reflection::{
|
||||
ReflectionEngine, ReflectionEngineState,
|
||||
};
|
||||
#[allow(unused_imports)]
|
||||
pub use identity::{
|
||||
AgentIdentityManager, IdentityManagerState,
|
||||
};
|
||||
#[allow(unused_imports)]
|
||||
pub use pattern_detector::{
|
||||
BehaviorPattern, PatternContext, PatternDetector, PatternDetectorConfig, PatternType,
|
||||
};
|
||||
#[allow(unused_imports)]
|
||||
pub use recommender::{
|
||||
PipelineMetadata, RecommendationRule, RecommenderConfig, WorkflowRecommender,
|
||||
};
|
||||
#[allow(unused_imports)]
|
||||
pub use mesh::{
|
||||
ActivityType, MeshAnalysisResult, MeshConfig, MeshCoordinator, MeshCoordinatorState,
|
||||
WorkflowRecommendation,
|
||||
};
|
||||
#[allow(unused_imports)] // Module not yet integrated - exports reserved for future use
|
||||
pub use trigger_evaluator::{
|
||||
ComparisonOperator, ConditionCombination, ContextConditionClause, ContextConditionConfig,
|
||||
ContextField, ExtendedTriggerType, IdentityFile, IdentityStateConfig,
|
||||
MemoryQueryConfig, CompositeTriggerConfig, TriggerContextCache, TriggerEvaluator,
|
||||
};
|
||||
#[allow(unused_imports)]
|
||||
pub use persona_evolver::{
|
||||
PersonaEvolver, PersonaEvolverConfig, PersonaEvolverState, PersonaEvolverStateHandle,
|
||||
EvolutionResult, EvolutionProposal, EvolutionChangeType, EvolutionInsight,
|
||||
ProfileUpdate, InsightCategory,
|
||||
};
|
||||
|
||||
421
desktop/src-tauri/src/intelligence/pattern_detector.rs
Normal file
421
desktop/src-tauri/src/intelligence/pattern_detector.rs
Normal file
@@ -0,0 +1,421 @@
|
||||
//! Pattern Detector - Behavior pattern detection for Adaptive Intelligence Mesh
|
||||
//!
|
||||
//! Detects patterns from user activities including:
|
||||
//! - Skill combinations (frequently used together)
|
||||
//! - Temporal triggers (time-based patterns)
|
||||
//! - Task-pipeline mappings (task types mapped to pipelines)
|
||||
//! - Input patterns (keyword/intent patterns)
|
||||
//!
|
||||
//! NOTE: Analysis and export methods are reserved for future dashboard integration.
|
||||
|
||||
#![allow(dead_code)] // Analysis and export methods reserved for future dashboard features
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
// === Pattern Types ===
|
||||
|
||||
/// Unique identifier for a pattern
|
||||
pub type PatternId = String;
|
||||
|
||||
/// Behavior pattern detected from user activities
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BehaviorPattern {
|
||||
/// Unique pattern identifier
|
||||
pub id: PatternId,
|
||||
/// Type of pattern detected
|
||||
pub pattern_type: PatternType,
|
||||
/// How many times this pattern has occurred
|
||||
pub frequency: usize,
|
||||
/// When this pattern was last detected
|
||||
pub last_occurrence: DateTime<Utc>,
|
||||
/// When this pattern was first detected
|
||||
pub first_occurrence: DateTime<Utc>,
|
||||
/// Confidence score (0.0-1.0)
|
||||
pub confidence: f32,
|
||||
/// Context when pattern was detected
|
||||
pub context: PatternContext,
|
||||
}
|
||||
|
||||
/// Types of detectable patterns
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum PatternType {
|
||||
/// Skills frequently used together
|
||||
SkillCombination {
|
||||
skill_ids: Vec<String>,
|
||||
},
|
||||
/// Time-based trigger pattern
|
||||
TemporalTrigger {
|
||||
hand_id: String,
|
||||
time_pattern: String, // Cron-like pattern or time range
|
||||
},
|
||||
/// Task type mapped to a pipeline
|
||||
TaskPipelineMapping {
|
||||
task_type: String,
|
||||
pipeline_id: String,
|
||||
},
|
||||
/// Input keyword/intent pattern
|
||||
InputPattern {
|
||||
keywords: Vec<String>,
|
||||
intent: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// Context information when pattern was detected
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct PatternContext {
|
||||
/// Skills involved in the session
|
||||
pub skill_ids: Option<Vec<String>>,
|
||||
/// Topics discussed recently
|
||||
pub recent_topics: Option<Vec<String>>,
|
||||
/// Detected intent
|
||||
pub intent: Option<String>,
|
||||
/// Time of day when detected (hour 0-23)
|
||||
pub time_of_day: Option<u8>,
|
||||
/// Day of week (0=Monday, 6=Sunday)
|
||||
pub day_of_week: Option<u8>,
|
||||
}
|
||||
|
||||
/// Pattern detection configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PatternDetectorConfig {
|
||||
/// Minimum occurrences before pattern is recognized
|
||||
pub min_frequency: usize,
|
||||
/// Minimum confidence threshold
|
||||
pub min_confidence: f32,
|
||||
/// Days after which pattern confidence decays
|
||||
pub decay_days: u32,
|
||||
/// Maximum patterns to keep
|
||||
pub max_patterns: usize,
|
||||
}
|
||||
|
||||
impl Default for PatternDetectorConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
min_frequency: 3,
|
||||
min_confidence: 0.5,
|
||||
decay_days: 30,
|
||||
max_patterns: 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// === Pattern Detector ===
|
||||
|
||||
/// Pattern detector that identifies behavior patterns from activities
|
||||
pub struct PatternDetector {
|
||||
/// Detected patterns
|
||||
patterns: HashMap<PatternId, BehaviorPattern>,
|
||||
/// Configuration
|
||||
config: PatternDetectorConfig,
|
||||
/// Skill combination history for pattern detection
|
||||
skill_combination_history: Vec<(Vec<String>, DateTime<Utc>)>,
|
||||
}
|
||||
|
||||
impl PatternDetector {
|
||||
/// Create a new pattern detector
|
||||
pub fn new(config: Option<PatternDetectorConfig>) -> Self {
|
||||
Self {
|
||||
patterns: HashMap::new(),
|
||||
config: config.unwrap_or_default(),
|
||||
skill_combination_history: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Record skill usage for combination detection
|
||||
pub fn record_skill_usage(&mut self, skill_ids: Vec<String>) {
|
||||
let now = Utc::now();
|
||||
self.skill_combination_history.push((skill_ids, now));
|
||||
|
||||
// Keep only recent history (last 1000 entries)
|
||||
if self.skill_combination_history.len() > 1000 {
|
||||
self.skill_combination_history.drain(0..500);
|
||||
}
|
||||
|
||||
// Detect patterns
|
||||
self.detect_skill_combinations();
|
||||
}
|
||||
|
||||
/// Record a pipeline execution for task mapping detection
|
||||
pub fn record_pipeline_execution(
|
||||
&mut self,
|
||||
task_type: &str,
|
||||
pipeline_id: &str,
|
||||
context: PatternContext,
|
||||
) {
|
||||
let pattern_key = format!("task_pipeline:{}:{}", task_type, pipeline_id);
|
||||
|
||||
self.update_or_create_pattern(
|
||||
&pattern_key,
|
||||
PatternType::TaskPipelineMapping {
|
||||
task_type: task_type.to_string(),
|
||||
pipeline_id: pipeline_id.to_string(),
|
||||
},
|
||||
context,
|
||||
);
|
||||
}
|
||||
|
||||
/// Record an input pattern
|
||||
pub fn record_input_pattern(
|
||||
&mut self,
|
||||
keywords: Vec<String>,
|
||||
intent: &str,
|
||||
context: PatternContext,
|
||||
) {
|
||||
let pattern_key = format!("input_pattern:{}:{}", keywords.join(","), intent);
|
||||
|
||||
self.update_or_create_pattern(
|
||||
&pattern_key,
|
||||
PatternType::InputPattern {
|
||||
keywords,
|
||||
intent: intent.to_string(),
|
||||
},
|
||||
context,
|
||||
);
|
||||
}
|
||||
|
||||
/// Update existing pattern or create new one
|
||||
fn update_or_create_pattern(
|
||||
&mut self,
|
||||
key: &str,
|
||||
pattern_type: PatternType,
|
||||
context: PatternContext,
|
||||
) {
|
||||
let now = Utc::now();
|
||||
let decay_days = self.config.decay_days;
|
||||
|
||||
if let Some(pattern) = self.patterns.get_mut(key) {
|
||||
// Update existing pattern
|
||||
pattern.frequency += 1;
|
||||
pattern.last_occurrence = now;
|
||||
pattern.context = context;
|
||||
|
||||
// Recalculate confidence inline to avoid borrow issues
|
||||
let days_since_last = (now - pattern.last_occurrence).num_days() as f32;
|
||||
let frequency_score = (pattern.frequency as f32 / 10.0).min(1.0);
|
||||
let decay_factor = if days_since_last > decay_days as f32 {
|
||||
0.5
|
||||
} else {
|
||||
1.0 - (days_since_last / decay_days as f32) * 0.3
|
||||
};
|
||||
pattern.confidence = (frequency_score * decay_factor).min(1.0);
|
||||
} else {
|
||||
// Create new pattern
|
||||
let pattern = BehaviorPattern {
|
||||
id: key.to_string(),
|
||||
pattern_type,
|
||||
frequency: 1,
|
||||
first_occurrence: now,
|
||||
last_occurrence: now,
|
||||
confidence: 0.1, // Low initial confidence
|
||||
context,
|
||||
};
|
||||
|
||||
self.patterns.insert(key.to_string(), pattern);
|
||||
|
||||
// Enforce max patterns limit
|
||||
self.enforce_max_patterns();
|
||||
}
|
||||
}
|
||||
|
||||
/// Detect skill combination patterns from history
|
||||
fn detect_skill_combinations(&mut self) {
|
||||
// Group skill combinations
|
||||
let mut combination_counts: HashMap<String, (Vec<String>, usize, DateTime<Utc>)> =
|
||||
HashMap::new();
|
||||
|
||||
for (skills, time) in &self.skill_combination_history {
|
||||
if skills.len() < 2 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sort skills for consistent grouping
|
||||
let mut sorted_skills = skills.clone();
|
||||
sorted_skills.sort();
|
||||
let key = sorted_skills.join("|");
|
||||
|
||||
let entry = combination_counts.entry(key).or_insert((
|
||||
sorted_skills,
|
||||
0,
|
||||
*time,
|
||||
));
|
||||
entry.1 += 1;
|
||||
entry.2 = *time; // Update last occurrence
|
||||
}
|
||||
|
||||
// Create patterns for combinations meeting threshold
|
||||
for (key, (skills, count, last_time)) in combination_counts {
|
||||
if count >= self.config.min_frequency {
|
||||
let pattern = BehaviorPattern {
|
||||
id: format!("skill_combo:{}", key),
|
||||
pattern_type: PatternType::SkillCombination { skill_ids: skills },
|
||||
frequency: count,
|
||||
first_occurrence: last_time,
|
||||
last_occurrence: last_time,
|
||||
confidence: self.calculate_confidence_from_frequency(count),
|
||||
context: PatternContext::default(),
|
||||
};
|
||||
|
||||
self.patterns.insert(pattern.id.clone(), pattern);
|
||||
}
|
||||
}
|
||||
|
||||
self.enforce_max_patterns();
|
||||
}
|
||||
|
||||
/// Calculate confidence based on frequency and recency
|
||||
fn calculate_confidence(&self, pattern: &BehaviorPattern) -> f32 {
|
||||
let now = Utc::now();
|
||||
let days_since_last = (now - pattern.last_occurrence).num_days() as f32;
|
||||
|
||||
// Base confidence from frequency (capped at 1.0)
|
||||
let frequency_score = (pattern.frequency as f32 / 10.0).min(1.0);
|
||||
|
||||
// Decay factor based on time since last occurrence
|
||||
let decay_factor = if days_since_last > self.config.decay_days as f32 {
|
||||
0.5 // Significant decay for old patterns
|
||||
} else {
|
||||
1.0 - (days_since_last / self.config.decay_days as f32) * 0.3
|
||||
};
|
||||
|
||||
(frequency_score * decay_factor).min(1.0)
|
||||
}
|
||||
|
||||
/// Calculate confidence from frequency alone
|
||||
fn calculate_confidence_from_frequency(&self, frequency: usize) -> f32 {
|
||||
(frequency as f32 / self.config.min_frequency.max(1) as f32).min(1.0)
|
||||
}
|
||||
|
||||
/// Enforce maximum patterns limit by removing lowest confidence patterns
|
||||
fn enforce_max_patterns(&mut self) {
|
||||
if self.patterns.len() <= self.config.max_patterns {
|
||||
return;
|
||||
}
|
||||
|
||||
// Sort patterns by confidence and remove lowest
|
||||
let mut patterns_vec: Vec<_> = self.patterns.drain().collect();
|
||||
patterns_vec.sort_by(|a, b| b.1.confidence.partial_cmp(&a.1.confidence).unwrap());
|
||||
|
||||
// Keep top patterns
|
||||
self.patterns = patterns_vec
|
||||
.into_iter()
|
||||
.take(self.config.max_patterns)
|
||||
.collect();
|
||||
}
|
||||
|
||||
/// Get all patterns above confidence threshold
|
||||
pub fn get_patterns(&self) -> Vec<&BehaviorPattern> {
|
||||
self.patterns
|
||||
.values()
|
||||
.filter(|p| p.confidence >= self.config.min_confidence)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get patterns of a specific type
|
||||
pub fn get_patterns_by_type(&self, pattern_type: &PatternType) -> Vec<&BehaviorPattern> {
|
||||
self.patterns
|
||||
.values()
|
||||
.filter(|p| std::mem::discriminant(&p.pattern_type) == std::mem::discriminant(pattern_type))
|
||||
.filter(|p| p.confidence >= self.config.min_confidence)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get patterns sorted by confidence
|
||||
pub fn get_patterns_sorted(&self) -> Vec<&BehaviorPattern> {
|
||||
let mut patterns: Vec<_> = self.get_patterns();
|
||||
patterns.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap());
|
||||
patterns
|
||||
}
|
||||
|
||||
/// Decay old patterns (should be called periodically)
|
||||
pub fn decay_patterns(&mut self) {
|
||||
let now = Utc::now();
|
||||
|
||||
for pattern in self.patterns.values_mut() {
|
||||
let days_since_last = (now - pattern.last_occurrence).num_days() as f32;
|
||||
|
||||
if days_since_last > self.config.decay_days as f32 {
|
||||
// Reduce confidence for old patterns
|
||||
let decay_amount = 0.1 * (days_since_last / self.config.decay_days as f32);
|
||||
pattern.confidence = (pattern.confidence - decay_amount).max(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove patterns below threshold
|
||||
self.patterns
|
||||
.retain(|_, p| p.confidence >= self.config.min_confidence * 0.5);
|
||||
}
|
||||
|
||||
/// Clear all patterns
|
||||
pub fn clear(&mut self) {
|
||||
self.patterns.clear();
|
||||
self.skill_combination_history.clear();
|
||||
}
|
||||
|
||||
/// Get pattern count
|
||||
pub fn pattern_count(&self) -> usize {
|
||||
self.patterns.len()
|
||||
}
|
||||
|
||||
/// Export patterns for persistence
|
||||
pub fn export_patterns(&self) -> Vec<BehaviorPattern> {
|
||||
self.patterns.values().cloned().collect()
|
||||
}
|
||||
|
||||
/// Import patterns from persistence
|
||||
pub fn import_patterns(&mut self, patterns: Vec<BehaviorPattern>) {
|
||||
for pattern in patterns {
|
||||
self.patterns.insert(pattern.id.clone(), pattern);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_pattern_creation() {
|
||||
let detector = PatternDetector::new(None);
|
||||
assert_eq!(detector.pattern_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skill_combination_detection() {
|
||||
let mut detector = PatternDetector::new(Some(PatternDetectorConfig {
|
||||
min_frequency: 2,
|
||||
..Default::default()
|
||||
}));
|
||||
|
||||
// Record skill usage multiple times
|
||||
detector.record_skill_usage(vec!["skill_a".to_string(), "skill_b".to_string()]);
|
||||
detector.record_skill_usage(vec!["skill_a".to_string(), "skill_b".to_string()]);
|
||||
|
||||
// Should detect pattern after 2 occurrences
|
||||
let patterns = detector.get_patterns();
|
||||
assert!(!patterns.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_confidence_calculation() {
|
||||
let detector = PatternDetector::new(None);
|
||||
|
||||
let pattern = BehaviorPattern {
|
||||
id: "test".to_string(),
|
||||
pattern_type: PatternType::TaskPipelineMapping {
|
||||
task_type: "test".to_string(),
|
||||
pipeline_id: "pipeline".to_string(),
|
||||
},
|
||||
frequency: 5,
|
||||
first_occurrence: Utc::now(),
|
||||
last_occurrence: Utc::now(),
|
||||
confidence: 0.5,
|
||||
context: PatternContext::default(),
|
||||
};
|
||||
|
||||
let confidence = detector.calculate_confidence(&pattern);
|
||||
assert!(confidence > 0.0 && confidence <= 1.0);
|
||||
}
|
||||
}
|
||||
819
desktop/src-tauri/src/intelligence/persona_evolver.rs
Normal file
819
desktop/src-tauri/src/intelligence/persona_evolver.rs
Normal file
@@ -0,0 +1,819 @@
|
||||
//! Persona Evolver - Memory-powered persona evolution system
|
||||
//!
|
||||
//! Automatically evolves agent persona based on:
|
||||
//! - User interaction patterns (preferences, communication style)
|
||||
//! - Reflection insights (positive/negative patterns)
|
||||
//! - Memory accumulation (facts, lessons, context)
|
||||
//!
|
||||
//! Key features:
|
||||
//! - Automatic user_profile enrichment from preferences
|
||||
//! - Instruction refinement proposals based on patterns
|
||||
//! - Soul evolution suggestions (requires explicit user approval)
|
||||
//!
|
||||
//! Phase 4 of Intelligence Layer - P1 Innovation Task.
|
||||
//!
|
||||
//! NOTE: Tauri commands defined here are not yet registered with the app.
|
||||
|
||||
#![allow(dead_code)] // Tauri commands not yet registered with application
|
||||
|
||||
use chrono::Utc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use super::reflection::{ReflectionResult, Sentiment, MemoryEntryForAnalysis};
|
||||
use super::identity::{IdentityFiles, IdentityFile, ProposalStatus};
|
||||
|
||||
// === Types ===
|
||||
|
||||
/// Persona evolution configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PersonaEvolverConfig {
|
||||
/// Enable automatic user_profile updates
|
||||
#[serde(default = "default_auto_profile_update")]
|
||||
pub auto_profile_update: bool,
|
||||
/// Minimum preferences before suggesting profile update
|
||||
#[serde(default = "default_min_preferences")]
|
||||
pub min_preferences_for_update: usize,
|
||||
/// Minimum conversations before evolution
|
||||
#[serde(default = "default_min_conversations")]
|
||||
pub min_conversations_for_evolution: usize,
|
||||
/// Enable instruction refinement proposals
|
||||
#[serde(default = "default_enable_instruction_refinement")]
|
||||
pub enable_instruction_refinement: bool,
|
||||
/// Enable soul evolution (requires explicit approval)
|
||||
#[serde(default = "default_enable_soul_evolution")]
|
||||
pub enable_soul_evolution: bool,
|
||||
/// Maximum proposals per evolution cycle
|
||||
#[serde(default = "default_max_proposals")]
|
||||
pub max_proposals_per_cycle: usize,
|
||||
}
|
||||
|
||||
fn default_auto_profile_update() -> bool { true }
|
||||
fn default_min_preferences() -> usize { 3 }
|
||||
fn default_min_conversations() -> usize { 5 }
|
||||
fn default_enable_instruction_refinement() -> bool { true }
|
||||
fn default_enable_soul_evolution() -> bool { true }
|
||||
fn default_max_proposals() -> usize { 3 }
|
||||
|
||||
impl Default for PersonaEvolverConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
auto_profile_update: true,
|
||||
min_preferences_for_update: 3,
|
||||
min_conversations_for_evolution: 5,
|
||||
enable_instruction_refinement: true,
|
||||
enable_soul_evolution: true,
|
||||
max_proposals_per_cycle: 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Persona evolution result
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EvolutionResult {
|
||||
/// Agent ID
|
||||
pub agent_id: String,
|
||||
/// Timestamp
|
||||
pub timestamp: String,
|
||||
/// Profile updates applied (auto)
|
||||
pub profile_updates: Vec<ProfileUpdate>,
|
||||
/// Proposals generated (require approval)
|
||||
pub proposals: Vec<EvolutionProposal>,
|
||||
/// Evolution insights
|
||||
pub insights: Vec<EvolutionInsight>,
|
||||
/// Whether evolution occurred
|
||||
pub evolved: bool,
|
||||
}
|
||||
|
||||
/// Profile update (auto-applied)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ProfileUpdate {
|
||||
pub section: String,
|
||||
pub previous: String,
|
||||
pub updated: String,
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
/// Evolution proposal (requires approval)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EvolutionProposal {
|
||||
pub id: String,
|
||||
pub agent_id: String,
|
||||
pub target_file: IdentityFile,
|
||||
pub change_type: EvolutionChangeType,
|
||||
pub reason: String,
|
||||
pub current_content: String,
|
||||
pub proposed_content: String,
|
||||
pub confidence: f32,
|
||||
pub evidence: Vec<String>,
|
||||
pub status: ProposalStatus,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
/// Type of evolution change
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum EvolutionChangeType {
|
||||
/// Add new instruction section
|
||||
InstructionAddition,
|
||||
/// Refine existing instruction
|
||||
InstructionRefinement,
|
||||
/// Add personality trait
|
||||
TraitAddition,
|
||||
/// Communication style adjustment
|
||||
StyleAdjustment,
|
||||
/// Knowledge domain expansion
|
||||
DomainExpansion,
|
||||
}
|
||||
|
||||
/// Evolution insight
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EvolutionInsight {
|
||||
pub category: InsightCategory,
|
||||
pub observation: String,
|
||||
pub recommendation: String,
|
||||
pub confidence: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum InsightCategory {
|
||||
CommunicationStyle,
|
||||
TechnicalExpertise,
|
||||
TaskEfficiency,
|
||||
UserPreference,
|
||||
KnowledgeGap,
|
||||
}
|
||||
|
||||
/// Persona evolution state
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PersonaEvolverState {
|
||||
pub last_evolution: Option<String>,
|
||||
pub total_evolutions: usize,
|
||||
pub pending_proposals: usize,
|
||||
pub profile_enrichment_score: f32,
|
||||
}
|
||||
|
||||
impl Default for PersonaEvolverState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
last_evolution: None,
|
||||
total_evolutions: 0,
|
||||
pending_proposals: 0,
|
||||
profile_enrichment_score: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// === Persona Evolver ===
|
||||
|
||||
pub struct PersonaEvolver {
|
||||
config: PersonaEvolverConfig,
|
||||
state: PersonaEvolverState,
|
||||
evolution_history: Vec<EvolutionResult>,
|
||||
}
|
||||
|
||||
impl PersonaEvolver {
|
||||
pub fn new(config: Option<PersonaEvolverConfig>) -> Self {
|
||||
Self {
|
||||
config: config.unwrap_or_default(),
|
||||
state: PersonaEvolverState::default(),
|
||||
evolution_history: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Run evolution cycle for an agent
|
||||
pub fn evolve(
|
||||
&mut self,
|
||||
agent_id: &str,
|
||||
memories: &[MemoryEntryForAnalysis],
|
||||
reflection_result: &ReflectionResult,
|
||||
current_identity: &IdentityFiles,
|
||||
) -> EvolutionResult {
|
||||
let mut profile_updates = Vec::new();
|
||||
let mut proposals = Vec::new();
|
||||
#[allow(unused_assignments)] // Overwritten by generate_insights below
|
||||
let mut insights = Vec::new();
|
||||
|
||||
// 1. Extract user preferences and auto-update profile
|
||||
if self.config.auto_profile_update {
|
||||
profile_updates = self.extract_profile_updates(memories, current_identity);
|
||||
}
|
||||
|
||||
// 2. Generate instruction refinement proposals
|
||||
if self.config.enable_instruction_refinement {
|
||||
let instruction_proposals = self.generate_instruction_proposals(
|
||||
agent_id,
|
||||
reflection_result,
|
||||
current_identity,
|
||||
);
|
||||
proposals.extend(instruction_proposals);
|
||||
}
|
||||
|
||||
// 3. Generate soul evolution proposals (rare, high bar)
|
||||
if self.config.enable_soul_evolution {
|
||||
let soul_proposals = self.generate_soul_proposals(
|
||||
agent_id,
|
||||
reflection_result,
|
||||
current_identity,
|
||||
);
|
||||
proposals.extend(soul_proposals);
|
||||
}
|
||||
|
||||
// 4. Generate insights
|
||||
insights = self.generate_insights(memories, reflection_result);
|
||||
|
||||
// 5. Limit proposals
|
||||
proposals.truncate(self.config.max_proposals_per_cycle);
|
||||
|
||||
// 6. Update state
|
||||
let evolved = !profile_updates.is_empty() || !proposals.is_empty();
|
||||
if evolved {
|
||||
self.state.last_evolution = Some(Utc::now().to_rfc3339());
|
||||
self.state.total_evolutions += 1;
|
||||
self.state.pending_proposals += proposals.len();
|
||||
self.state.profile_enrichment_score = self.calculate_profile_score(memories);
|
||||
}
|
||||
|
||||
let result = EvolutionResult {
|
||||
agent_id: agent_id.to_string(),
|
||||
timestamp: Utc::now().to_rfc3339(),
|
||||
profile_updates,
|
||||
proposals,
|
||||
insights,
|
||||
evolved,
|
||||
};
|
||||
|
||||
// Store in history
|
||||
self.evolution_history.push(result.clone());
|
||||
if self.evolution_history.len() > 20 {
|
||||
self.evolution_history = self.evolution_history.split_off(10);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Extract profile updates from memory
|
||||
fn extract_profile_updates(
|
||||
&self,
|
||||
memories: &[MemoryEntryForAnalysis],
|
||||
current_identity: &IdentityFiles,
|
||||
) -> Vec<ProfileUpdate> {
|
||||
let mut updates = Vec::new();
|
||||
|
||||
// Extract preferences
|
||||
let preferences: Vec<_> = memories
|
||||
.iter()
|
||||
.filter(|m| m.memory_type == "preference")
|
||||
.collect();
|
||||
|
||||
if preferences.len() >= self.config.min_preferences_for_update {
|
||||
// Check if user_profile needs updating
|
||||
let current_profile = ¤t_identity.user_profile;
|
||||
let default_profile = "尚未收集到用户偏好信息";
|
||||
|
||||
if current_profile.contains(default_profile) || current_profile.len() < 100 {
|
||||
// Build new profile from preferences
|
||||
let mut sections = Vec::new();
|
||||
|
||||
// Group preferences by category
|
||||
let mut categories: HashMap<String, Vec<String>> = HashMap::new();
|
||||
for pref in &preferences {
|
||||
// Simple categorization based on keywords
|
||||
let category = self.categorize_preference(&pref.content);
|
||||
categories
|
||||
.entry(category)
|
||||
.or_insert_with(Vec::new)
|
||||
.push(pref.content.clone());
|
||||
}
|
||||
|
||||
// Build sections
|
||||
for (category, items) in categories {
|
||||
if !items.is_empty() {
|
||||
sections.push(format!("### {}\n{}", category, items.iter()
|
||||
.map(|i| format!("- {}", i))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")));
|
||||
}
|
||||
}
|
||||
|
||||
if !sections.is_empty() {
|
||||
let new_profile = format!("# 用户画像\n\n{}\n\n_自动生成于 {}_",
|
||||
sections.join("\n\n"),
|
||||
Utc::now().format("%Y-%m-%d")
|
||||
);
|
||||
|
||||
updates.push(ProfileUpdate {
|
||||
section: "user_profile".to_string(),
|
||||
previous: current_profile.clone(),
|
||||
updated: new_profile,
|
||||
source: format!("{} 个偏好记忆", preferences.len()),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updates
|
||||
}
|
||||
|
||||
/// Categorize a preference
|
||||
fn categorize_preference(&self, content: &str) -> String {
|
||||
let content_lower = content.to_lowercase();
|
||||
|
||||
if content_lower.contains("语言") || content_lower.contains("沟通") || content_lower.contains("回复") {
|
||||
"沟通偏好".to_string()
|
||||
} else if content_lower.contains("技术") || content_lower.contains("框架") || content_lower.contains("工具") {
|
||||
"技术栈".to_string()
|
||||
} else if content_lower.contains("项目") || content_lower.contains("工作") || content_lower.contains("任务") {
|
||||
"工作习惯".to_string()
|
||||
} else if content_lower.contains("格式") || content_lower.contains("风格") || content_lower.contains("风格") {
|
||||
"输出风格".to_string()
|
||||
} else {
|
||||
"其他偏好".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate instruction refinement proposals
|
||||
fn generate_instruction_proposals(
|
||||
&self,
|
||||
agent_id: &str,
|
||||
reflection_result: &ReflectionResult,
|
||||
current_identity: &IdentityFiles,
|
||||
) -> Vec<EvolutionProposal> {
|
||||
let mut proposals = Vec::new();
|
||||
|
||||
// Only propose if there are negative patterns
|
||||
let negative_patterns: Vec<_> = reflection_result.patterns
|
||||
.iter()
|
||||
.filter(|p| matches!(p.sentiment, Sentiment::Negative))
|
||||
.collect();
|
||||
|
||||
if negative_patterns.is_empty() {
|
||||
return proposals;
|
||||
}
|
||||
|
||||
// Check if instructions already contain these warnings
|
||||
let current_instructions = ¤t_identity.instructions;
|
||||
|
||||
// Build proposed additions
|
||||
let mut additions = Vec::new();
|
||||
let mut evidence = Vec::new();
|
||||
|
||||
for pattern in &negative_patterns {
|
||||
// Check if this pattern is already addressed
|
||||
let key_phrase = &pattern.observation;
|
||||
if !current_instructions.contains(key_phrase) {
|
||||
additions.push(format!("- **注意事项**: {}", pattern.observation));
|
||||
evidence.extend(pattern.evidence.clone());
|
||||
}
|
||||
}
|
||||
|
||||
if !additions.is_empty() {
|
||||
let proposed = format!(
|
||||
"{}\n\n## 🔄 自我改进建议\n\n{}\n\n_基于交互模式分析自动生成_",
|
||||
current_instructions.trim_end(),
|
||||
additions.join("\n")
|
||||
);
|
||||
|
||||
proposals.push(EvolutionProposal {
|
||||
id: format!("evo_inst_{}", Utc::now().timestamp()),
|
||||
agent_id: agent_id.to_string(),
|
||||
target_file: IdentityFile::Instructions,
|
||||
change_type: EvolutionChangeType::InstructionAddition,
|
||||
reason: format!(
|
||||
"基于 {} 个负面模式观察,建议在指令中增加自我改进提醒",
|
||||
negative_patterns.len()
|
||||
),
|
||||
current_content: current_instructions.clone(),
|
||||
proposed_content: proposed,
|
||||
confidence: 0.7 + (negative_patterns.len() as f32 * 0.05).min(0.2),
|
||||
evidence,
|
||||
status: ProposalStatus::Pending,
|
||||
created_at: Utc::now().to_rfc3339(),
|
||||
});
|
||||
}
|
||||
|
||||
// Check for improvement suggestions that could become instructions
|
||||
for improvement in &reflection_result.improvements {
|
||||
if current_instructions.contains(&improvement.suggestion) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// High priority improvements become instruction proposals
|
||||
if matches!(improvement.priority, super::reflection::Priority::High) {
|
||||
proposals.push(EvolutionProposal {
|
||||
id: format!("evo_inst_{}_{}", Utc::now().timestamp(), rand_suffix()),
|
||||
agent_id: agent_id.to_string(),
|
||||
target_file: IdentityFile::Instructions,
|
||||
change_type: EvolutionChangeType::InstructionRefinement,
|
||||
reason: format!("高优先级改进建议: {}", improvement.area),
|
||||
current_content: current_instructions.clone(),
|
||||
proposed_content: format!(
|
||||
"{}\n\n### {}\n\n{}",
|
||||
current_instructions.trim_end(),
|
||||
improvement.area,
|
||||
improvement.suggestion
|
||||
),
|
||||
confidence: 0.75,
|
||||
evidence: vec![improvement.suggestion.clone()],
|
||||
status: ProposalStatus::Pending,
|
||||
created_at: Utc::now().to_rfc3339(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
proposals
|
||||
}
|
||||
|
||||
/// Generate soul evolution proposals (high bar)
|
||||
fn generate_soul_proposals(
|
||||
&self,
|
||||
agent_id: &str,
|
||||
reflection_result: &ReflectionResult,
|
||||
current_identity: &IdentityFiles,
|
||||
) -> Vec<EvolutionProposal> {
|
||||
let mut proposals = Vec::new();
|
||||
|
||||
// Soul evolution requires strong positive patterns
|
||||
let positive_patterns: Vec<_> = reflection_result.patterns
|
||||
.iter()
|
||||
.filter(|p| matches!(p.sentiment, Sentiment::Positive))
|
||||
.collect();
|
||||
|
||||
// Need at least 3 strong positive patterns
|
||||
if positive_patterns.len() < 3 {
|
||||
return proposals;
|
||||
}
|
||||
|
||||
// Calculate overall confidence
|
||||
let avg_frequency: usize = positive_patterns.iter()
|
||||
.map(|p| p.frequency)
|
||||
.sum::<usize>() / positive_patterns.len();
|
||||
|
||||
if avg_frequency < 5 {
|
||||
return proposals;
|
||||
}
|
||||
|
||||
// Build soul enhancement proposal
|
||||
let current_soul = ¤t_identity.soul;
|
||||
let mut traits = Vec::new();
|
||||
let mut evidence = Vec::new();
|
||||
|
||||
for pattern in &positive_patterns {
|
||||
// Extract trait from observation
|
||||
if pattern.observation.contains("偏好") {
|
||||
traits.push("深入理解用户偏好");
|
||||
} else if pattern.observation.contains("经验") {
|
||||
traits.push("持续积累经验教训");
|
||||
} else if pattern.observation.contains("知识") {
|
||||
traits.push("构建核心知识体系");
|
||||
}
|
||||
evidence.extend(pattern.evidence.clone());
|
||||
}
|
||||
|
||||
if !traits.is_empty() {
|
||||
let traits_section = traits.iter()
|
||||
.map(|t| format!("- {}", t))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
let proposed = format!(
|
||||
"{}\n\n## 🌱 成长特质\n\n{}\n\n_通过交互学习持续演化_",
|
||||
current_soul.trim_end(),
|
||||
traits_section
|
||||
);
|
||||
|
||||
proposals.push(EvolutionProposal {
|
||||
id: format!("evo_soul_{}", Utc::now().timestamp()),
|
||||
agent_id: agent_id.to_string(),
|
||||
target_file: IdentityFile::Soul,
|
||||
change_type: EvolutionChangeType::TraitAddition,
|
||||
reason: format!(
|
||||
"基于 {} 个强正面模式,建议增加成长特质",
|
||||
positive_patterns.len()
|
||||
),
|
||||
current_content: current_soul.clone(),
|
||||
proposed_content: proposed,
|
||||
confidence: 0.85,
|
||||
evidence,
|
||||
status: ProposalStatus::Pending,
|
||||
created_at: Utc::now().to_rfc3339(),
|
||||
});
|
||||
}
|
||||
|
||||
proposals
|
||||
}
|
||||
|
||||
/// Generate evolution insights
|
||||
fn generate_insights(
|
||||
&self,
|
||||
memories: &[MemoryEntryForAnalysis],
|
||||
reflection_result: &ReflectionResult,
|
||||
) -> Vec<EvolutionInsight> {
|
||||
let mut insights = Vec::new();
|
||||
|
||||
// Communication style insight
|
||||
let comm_prefs: Vec<_> = memories
|
||||
.iter()
|
||||
.filter(|m| m.memory_type == "preference" &&
|
||||
(m.content.contains("回复") || m.content.contains("语言") || m.content.contains("简洁")))
|
||||
.collect();
|
||||
|
||||
if !comm_prefs.is_empty() {
|
||||
insights.push(EvolutionInsight {
|
||||
category: InsightCategory::CommunicationStyle,
|
||||
observation: format!("用户有 {} 个沟通风格偏好", comm_prefs.len()),
|
||||
recommendation: "在回复中应用这些偏好,提高用户满意度".to_string(),
|
||||
confidence: 0.8,
|
||||
});
|
||||
}
|
||||
|
||||
// Technical expertise insight
|
||||
let tech_memories: Vec<_> = memories
|
||||
.iter()
|
||||
.filter(|m| m.tags.iter().any(|t| t.contains("技术") || t.contains("代码")))
|
||||
.collect();
|
||||
|
||||
if tech_memories.len() >= 5 {
|
||||
insights.push(EvolutionInsight {
|
||||
category: InsightCategory::TechnicalExpertise,
|
||||
observation: format!("积累了 {} 个技术相关记忆", tech_memories.len()),
|
||||
recommendation: "考虑构建技术知识图谱,提高检索效率".to_string(),
|
||||
confidence: 0.7,
|
||||
});
|
||||
}
|
||||
|
||||
// Task efficiency insight from negative patterns
|
||||
let has_task_issues = reflection_result.patterns
|
||||
.iter()
|
||||
.any(|p| p.observation.contains("任务") && matches!(p.sentiment, Sentiment::Negative));
|
||||
|
||||
if has_task_issues {
|
||||
insights.push(EvolutionInsight {
|
||||
category: InsightCategory::TaskEfficiency,
|
||||
observation: "存在任务管理相关问题".to_string(),
|
||||
recommendation: "建议增加任务跟踪和提醒机制".to_string(),
|
||||
confidence: 0.75,
|
||||
});
|
||||
}
|
||||
|
||||
// Knowledge gap insight
|
||||
let lesson_count = memories.iter()
|
||||
.filter(|m| m.memory_type == "lesson")
|
||||
.count();
|
||||
|
||||
if lesson_count > 10 {
|
||||
insights.push(EvolutionInsight {
|
||||
category: InsightCategory::KnowledgeGap,
|
||||
observation: format!("已记录 {} 条经验教训", lesson_count),
|
||||
recommendation: "定期回顾教训,避免重复错误".to_string(),
|
||||
confidence: 0.8,
|
||||
});
|
||||
}
|
||||
|
||||
insights
|
||||
}
|
||||
|
||||
/// Calculate profile enrichment score
|
||||
fn calculate_profile_score(&self, memories: &[MemoryEntryForAnalysis]) -> f32 {
|
||||
let pref_count = memories.iter().filter(|m| m.memory_type == "preference").count();
|
||||
let fact_count = memories.iter().filter(|m| m.memory_type == "fact").count();
|
||||
|
||||
// Score based on diversity and quantity
|
||||
let pref_score = (pref_count as f32 / 10.0).min(1.0) * 0.5;
|
||||
let fact_score = (fact_count as f32 / 20.0).min(1.0) * 0.3;
|
||||
let diversity = if pref_count > 0 && fact_count > 0 { 0.2 } else { 0.0 };
|
||||
|
||||
pref_score + fact_score + diversity
|
||||
}
|
||||
|
||||
/// Get evolution history
|
||||
pub fn get_history(&self, limit: usize) -> Vec<&EvolutionResult> {
|
||||
self.evolution_history.iter().rev().take(limit).collect()
|
||||
}
|
||||
|
||||
/// Get current state
|
||||
pub fn get_state(&self) -> &PersonaEvolverState {
|
||||
&self.state
|
||||
}
|
||||
|
||||
/// Get configuration
|
||||
pub fn get_config(&self) -> &PersonaEvolverConfig {
|
||||
&self.config
|
||||
}
|
||||
|
||||
/// Update configuration
|
||||
pub fn update_config(&mut self, config: PersonaEvolverConfig) {
|
||||
self.config = config;
|
||||
}
|
||||
|
||||
/// Mark proposal as handled (approved/rejected)
|
||||
pub fn proposal_handled(&mut self) {
|
||||
if self.state.pending_proposals > 0 {
|
||||
self.state.pending_proposals -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate random suffix
|
||||
fn rand_suffix() -> String {
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
static COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
let count = COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
format!("{:04x}", count % 0x10000)
|
||||
}
|
||||
|
||||
// === Tauri Commands ===
|
||||
|
||||
/// Type alias for Tauri state management (shared evolver handle)
|
||||
pub type PersonaEvolverStateHandle = Arc<Mutex<PersonaEvolver>>;
|
||||
|
||||
/// Initialize persona evolver
|
||||
#[tauri::command]
|
||||
pub async fn persona_evolver_init(
|
||||
config: Option<PersonaEvolverConfig>,
|
||||
state: tauri::State<'_, PersonaEvolverStateHandle>,
|
||||
) -> Result<bool, String> {
|
||||
let mut evolver = state.lock().await;
|
||||
if let Some(cfg) = config {
|
||||
evolver.update_config(cfg);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Run evolution cycle
|
||||
#[tauri::command]
|
||||
pub async fn persona_evolve(
|
||||
agent_id: String,
|
||||
memories: Vec<MemoryEntryForAnalysis>,
|
||||
reflection_state: tauri::State<'_, super::reflection::ReflectionEngineState>,
|
||||
identity_state: tauri::State<'_, super::identity::IdentityManagerState>,
|
||||
evolver_state: tauri::State<'_, PersonaEvolverStateHandle>,
|
||||
) -> Result<EvolutionResult, String> {
|
||||
// 1. Run reflection first
|
||||
let mut reflection = reflection_state.lock().await;
|
||||
let reflection_result = reflection.reflect(&agent_id, &memories);
|
||||
drop(reflection);
|
||||
|
||||
// 2. Get current identity
|
||||
let mut identity = identity_state.lock().await;
|
||||
let current_identity = identity.get_identity(&agent_id);
|
||||
drop(identity);
|
||||
|
||||
// 3. Run evolution
|
||||
let mut evolver = evolver_state.lock().await;
|
||||
let result = evolver.evolve(&agent_id, &memories, &reflection_result, ¤t_identity);
|
||||
|
||||
// 4. Apply auto profile updates
|
||||
if !result.profile_updates.is_empty() {
|
||||
let mut identity = identity_state.lock().await;
|
||||
for update in &result.profile_updates {
|
||||
identity.update_user_profile(&agent_id, &update.updated);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Get evolution history
|
||||
#[tauri::command]
|
||||
pub async fn persona_evolution_history(
|
||||
limit: Option<usize>,
|
||||
state: tauri::State<'_, PersonaEvolverStateHandle>,
|
||||
) -> Result<Vec<EvolutionResult>, String> {
|
||||
let evolver = state.lock().await;
|
||||
Ok(evolver.get_history(limit.unwrap_or(10)).into_iter().cloned().collect())
|
||||
}
|
||||
|
||||
/// Get evolver state
|
||||
#[tauri::command]
|
||||
pub async fn persona_evolver_state(
|
||||
state: tauri::State<'_, PersonaEvolverStateHandle>,
|
||||
) -> Result<PersonaEvolverState, String> {
|
||||
let evolver = state.lock().await;
|
||||
Ok(evolver.get_state().clone())
|
||||
}
|
||||
|
||||
/// Get evolver config
|
||||
#[tauri::command]
|
||||
pub async fn persona_evolver_config(
|
||||
state: tauri::State<'_, PersonaEvolverStateHandle>,
|
||||
) -> Result<PersonaEvolverConfig, String> {
|
||||
let evolver = state.lock().await;
|
||||
Ok(evolver.get_config().clone())
|
||||
}
|
||||
|
||||
/// Update evolver config
|
||||
#[tauri::command]
|
||||
pub async fn persona_evolver_update_config(
|
||||
config: PersonaEvolverConfig,
|
||||
state: tauri::State<'_, PersonaEvolverStateHandle>,
|
||||
) -> Result<(), String> {
|
||||
let mut evolver = state.lock().await;
|
||||
evolver.update_config(config);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Apply evolution proposal (approve)
|
||||
#[tauri::command]
|
||||
pub async fn persona_apply_proposal(
|
||||
proposal: EvolutionProposal,
|
||||
identity_state: tauri::State<'_, super::identity::IdentityManagerState>,
|
||||
evolver_state: tauri::State<'_, PersonaEvolverStateHandle>,
|
||||
) -> Result<IdentityFiles, String> {
|
||||
// Apply the proposal through identity manager
|
||||
let mut identity = identity_state.lock().await;
|
||||
|
||||
let result = match proposal.target_file {
|
||||
IdentityFile::Soul => {
|
||||
identity.update_file(&proposal.agent_id, "soul", &proposal.proposed_content)
|
||||
}
|
||||
IdentityFile::Instructions => {
|
||||
identity.update_file(&proposal.agent_id, "instructions", &proposal.proposed_content)
|
||||
}
|
||||
};
|
||||
|
||||
if result.is_err() {
|
||||
return result.map(|_| IdentityFiles {
|
||||
soul: String::new(),
|
||||
instructions: String::new(),
|
||||
user_profile: String::new(),
|
||||
heartbeat: None,
|
||||
});
|
||||
}
|
||||
|
||||
// Update evolver state
|
||||
let mut evolver = evolver_state.lock().await;
|
||||
evolver.proposal_handled();
|
||||
|
||||
// Return updated identity
|
||||
Ok(identity.get_identity(&proposal.agent_id))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_evolve_empty() {
|
||||
let mut evolver = PersonaEvolver::new(None);
|
||||
let memories = vec![];
|
||||
let reflection = ReflectionResult {
|
||||
patterns: vec![],
|
||||
improvements: vec![],
|
||||
identity_proposals: vec![],
|
||||
new_memories: 0,
|
||||
timestamp: Utc::now().to_rfc3339(),
|
||||
};
|
||||
let identity = IdentityFiles {
|
||||
soul: "Test soul".to_string(),
|
||||
instructions: "Test instructions".to_string(),
|
||||
user_profile: "Test profile".to_string(),
|
||||
heartbeat: None,
|
||||
};
|
||||
|
||||
let result = evolver.evolve("test-agent", &memories, &reflection, &identity);
|
||||
assert!(!result.evolved);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profile_update() {
|
||||
let mut evolver = PersonaEvolver::new(None);
|
||||
let memories = vec![
|
||||
MemoryEntryForAnalysis {
|
||||
memory_type: "preference".to_string(),
|
||||
content: "喜欢简洁的回复".to_string(),
|
||||
importance: 7,
|
||||
access_count: 3,
|
||||
tags: vec!["沟通".to_string()],
|
||||
},
|
||||
MemoryEntryForAnalysis {
|
||||
memory_type: "preference".to_string(),
|
||||
content: "使用中文".to_string(),
|
||||
importance: 8,
|
||||
access_count: 5,
|
||||
tags: vec!["语言".to_string()],
|
||||
},
|
||||
MemoryEntryForAnalysis {
|
||||
memory_type: "preference".to_string(),
|
||||
content: "代码使用 TypeScript".to_string(),
|
||||
importance: 7,
|
||||
access_count: 2,
|
||||
tags: vec!["技术".to_string()],
|
||||
},
|
||||
];
|
||||
|
||||
let identity = IdentityFiles {
|
||||
soul: "Test".to_string(),
|
||||
instructions: "Test".to_string(),
|
||||
user_profile: "尚未收集到用户偏好信息".to_string(),
|
||||
heartbeat: None,
|
||||
};
|
||||
|
||||
let updates = evolver.extract_profile_updates(&memories, &identity);
|
||||
assert!(!updates.is_empty());
|
||||
assert!(updates[0].updated.contains("用户画像"));
|
||||
}
|
||||
}
|
||||
519
desktop/src-tauri/src/intelligence/recommender.rs
Normal file
519
desktop/src-tauri/src/intelligence/recommender.rs
Normal file
@@ -0,0 +1,519 @@
|
||||
//! Workflow Recommender - Generates workflow recommendations from detected patterns
|
||||
//!
|
||||
//! This module analyzes behavior patterns and generates actionable workflow recommendations.
|
||||
//! It maps detected patterns to pipelines and provides confidence scoring.
|
||||
//!
|
||||
//! NOTE: Some methods are reserved for future integration with the UI.
|
||||
|
||||
#![allow(dead_code)] // Methods reserved for future UI integration
|
||||
|
||||
use chrono::Utc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::mesh::WorkflowRecommendation;
|
||||
use super::pattern_detector::{BehaviorPattern, PatternType};
|
||||
|
||||
// === Types ===
|
||||
|
||||
/// Recommendation rule that maps patterns to pipelines
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RecommendationRule {
|
||||
/// Rule identifier
|
||||
pub id: String,
|
||||
/// Pattern types this rule matches
|
||||
pub pattern_types: Vec<String>,
|
||||
/// Pipeline to recommend
|
||||
pub pipeline_id: String,
|
||||
/// Base confidence for this rule
|
||||
pub base_confidence: f32,
|
||||
/// Human-readable description
|
||||
pub description: String,
|
||||
/// Input mappings (pattern context field -> pipeline input)
|
||||
pub input_mappings: HashMap<String, String>,
|
||||
/// Priority (higher = more important)
|
||||
pub priority: u8,
|
||||
}
|
||||
|
||||
/// Recommender configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RecommenderConfig {
|
||||
/// Minimum confidence threshold
|
||||
pub min_confidence: f32,
|
||||
/// Maximum recommendations to generate
|
||||
pub max_recommendations: usize,
|
||||
/// Enable rule-based recommendations
|
||||
pub enable_rules: bool,
|
||||
/// Enable pattern-based recommendations
|
||||
pub enable_patterns: bool,
|
||||
}
|
||||
|
||||
impl Default for RecommenderConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
min_confidence: 0.5,
|
||||
max_recommendations: 10,
|
||||
enable_rules: true,
|
||||
enable_patterns: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// === Workflow Recommender ===
|
||||
|
||||
/// Workflow recommendation engine
|
||||
pub struct WorkflowRecommender {
|
||||
/// Configuration
|
||||
config: RecommenderConfig,
|
||||
/// Recommendation rules
|
||||
rules: Vec<RecommendationRule>,
|
||||
/// Pipeline registry (pipeline_id -> metadata)
|
||||
#[allow(dead_code)] // Reserved for future pipeline-based recommendations
|
||||
pipeline_registry: HashMap<String, PipelineMetadata>,
|
||||
/// Generated recommendations cache
|
||||
recommendations_cache: Vec<WorkflowRecommendation>,
|
||||
}
|
||||
|
||||
/// Metadata about a registered pipeline
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PipelineMetadata {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub tags: Vec<String>,
|
||||
pub input_schema: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl WorkflowRecommender {
|
||||
/// Create a new workflow recommender
|
||||
pub fn new(config: Option<RecommenderConfig>) -> Self {
|
||||
let mut recommender = Self {
|
||||
config: config.unwrap_or_default(),
|
||||
rules: Vec::new(),
|
||||
pipeline_registry: HashMap::new(),
|
||||
recommendations_cache: Vec::new(),
|
||||
};
|
||||
|
||||
// Initialize with built-in rules
|
||||
recommender.initialize_default_rules();
|
||||
recommender
|
||||
}
|
||||
|
||||
/// Initialize default recommendation rules
|
||||
fn initialize_default_rules(&mut self) {
|
||||
// Rule: Research + Analysis -> Report Generation
|
||||
self.rules.push(RecommendationRule {
|
||||
id: "rule_research_report".to_string(),
|
||||
pattern_types: vec!["SkillCombination".to_string()],
|
||||
pipeline_id: "research-report-generator".to_string(),
|
||||
base_confidence: 0.7,
|
||||
description: "Generate comprehensive research report".to_string(),
|
||||
input_mappings: HashMap::new(),
|
||||
priority: 8,
|
||||
});
|
||||
|
||||
// Rule: Code + Test -> Quality Check Pipeline
|
||||
self.rules.push(RecommendationRule {
|
||||
id: "rule_code_quality".to_string(),
|
||||
pattern_types: vec!["SkillCombination".to_string()],
|
||||
pipeline_id: "code-quality-check".to_string(),
|
||||
base_confidence: 0.75,
|
||||
description: "Run code quality and test pipeline".to_string(),
|
||||
input_mappings: HashMap::new(),
|
||||
priority: 7,
|
||||
});
|
||||
|
||||
// Rule: Daily morning -> Daily briefing
|
||||
self.rules.push(RecommendationRule {
|
||||
id: "rule_morning_briefing".to_string(),
|
||||
pattern_types: vec!["TemporalTrigger".to_string()],
|
||||
pipeline_id: "daily-briefing".to_string(),
|
||||
base_confidence: 0.6,
|
||||
description: "Generate daily briefing".to_string(),
|
||||
input_mappings: HashMap::new(),
|
||||
priority: 5,
|
||||
});
|
||||
|
||||
// Rule: Task + Deadline -> Priority sort
|
||||
self.rules.push(RecommendationRule {
|
||||
id: "rule_task_priority".to_string(),
|
||||
pattern_types: vec!["InputPattern".to_string()],
|
||||
pipeline_id: "task-priority-sorter".to_string(),
|
||||
base_confidence: 0.65,
|
||||
description: "Sort and prioritize tasks".to_string(),
|
||||
input_mappings: HashMap::new(),
|
||||
priority: 6,
|
||||
});
|
||||
}
|
||||
|
||||
/// Generate recommendations from detected patterns
|
||||
pub fn recommend(&self, patterns: &[&BehaviorPattern]) -> Vec<WorkflowRecommendation> {
|
||||
let mut recommendations = Vec::new();
|
||||
|
||||
if patterns.is_empty() {
|
||||
return recommendations;
|
||||
}
|
||||
|
||||
// Rule-based recommendations
|
||||
if self.config.enable_rules {
|
||||
for rule in &self.rules {
|
||||
if let Some(rec) = self.apply_rule(rule, patterns) {
|
||||
if rec.confidence >= self.config.min_confidence {
|
||||
recommendations.push(rec);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pattern-based recommendations (direct mapping)
|
||||
if self.config.enable_patterns {
|
||||
for pattern in patterns {
|
||||
if let Some(rec) = self.pattern_to_recommendation(pattern) {
|
||||
if rec.confidence >= self.config.min_confidence {
|
||||
recommendations.push(rec);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by confidence (descending) and priority
|
||||
recommendations.sort_by(|a, b| {
|
||||
let priority_diff = self.get_priority_for_recommendation(b)
|
||||
.cmp(&self.get_priority_for_recommendation(a));
|
||||
if priority_diff != std::cmp::Ordering::Equal {
|
||||
return priority_diff;
|
||||
}
|
||||
b.confidence.partial_cmp(&a.confidence).unwrap()
|
||||
});
|
||||
|
||||
// Limit recommendations
|
||||
recommendations.truncate(self.config.max_recommendations);
|
||||
|
||||
recommendations
|
||||
}
|
||||
|
||||
/// Apply a recommendation rule to patterns
|
||||
fn apply_rule(
|
||||
&self,
|
||||
rule: &RecommendationRule,
|
||||
patterns: &[&BehaviorPattern],
|
||||
) -> Option<WorkflowRecommendation> {
|
||||
let mut matched_patterns: Vec<String> = Vec::new();
|
||||
let mut total_confidence = 0.0;
|
||||
let mut match_count = 0;
|
||||
|
||||
for pattern in patterns {
|
||||
let pattern_type_name = self.get_pattern_type_name(&pattern.pattern_type);
|
||||
|
||||
if rule.pattern_types.contains(&pattern_type_name) {
|
||||
matched_patterns.push(pattern.id.clone());
|
||||
total_confidence += pattern.confidence;
|
||||
match_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if matched_patterns.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Calculate combined confidence
|
||||
let avg_pattern_confidence = total_confidence / match_count as f32;
|
||||
let final_confidence = (rule.base_confidence * 0.6 + avg_pattern_confidence * 0.4).min(1.0);
|
||||
|
||||
// Build suggested inputs from pattern context
|
||||
let suggested_inputs = self.build_suggested_inputs(&matched_patterns, patterns, rule);
|
||||
|
||||
Some(WorkflowRecommendation {
|
||||
id: format!("rec_{}", Uuid::new_v4()),
|
||||
pipeline_id: rule.pipeline_id.clone(),
|
||||
confidence: final_confidence,
|
||||
reason: rule.description.clone(),
|
||||
suggested_inputs,
|
||||
patterns_matched: matched_patterns,
|
||||
timestamp: Utc::now(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Convert a single pattern to a recommendation
|
||||
fn pattern_to_recommendation(&self, pattern: &BehaviorPattern) -> Option<WorkflowRecommendation> {
|
||||
let (pipeline_id, reason) = match &pattern.pattern_type {
|
||||
PatternType::TaskPipelineMapping { task_type, pipeline_id } => {
|
||||
(pipeline_id.clone(), format!("Detected task type: {}", task_type))
|
||||
}
|
||||
PatternType::SkillCombination { skill_ids } => {
|
||||
// Find a pipeline that uses these skills
|
||||
let pipeline_id = self.find_pipeline_for_skills(skill_ids)?;
|
||||
(pipeline_id, format!("Skills often used together: {}", skill_ids.join(", ")))
|
||||
}
|
||||
PatternType::InputPattern { keywords, intent } => {
|
||||
// Find a pipeline for this intent
|
||||
let pipeline_id = self.find_pipeline_for_intent(intent)?;
|
||||
(pipeline_id, format!("Intent detected: {} ({})", intent, keywords.join(", ")))
|
||||
}
|
||||
PatternType::TemporalTrigger { hand_id, time_pattern } => {
|
||||
(format!("scheduled_{}", hand_id), format!("Scheduled at: {}", time_pattern))
|
||||
}
|
||||
};
|
||||
|
||||
Some(WorkflowRecommendation {
|
||||
id: format!("rec_{}", Uuid::new_v4()),
|
||||
pipeline_id,
|
||||
confidence: pattern.confidence,
|
||||
reason,
|
||||
suggested_inputs: HashMap::new(),
|
||||
patterns_matched: vec![pattern.id.clone()],
|
||||
timestamp: Utc::now(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Get string name for pattern type
|
||||
fn get_pattern_type_name(&self, pattern_type: &PatternType) -> String {
|
||||
match pattern_type {
|
||||
PatternType::SkillCombination { .. } => "SkillCombination".to_string(),
|
||||
PatternType::TemporalTrigger { .. } => "TemporalTrigger".to_string(),
|
||||
PatternType::TaskPipelineMapping { .. } => "TaskPipelineMapping".to_string(),
|
||||
PatternType::InputPattern { .. } => "InputPattern".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get priority for a recommendation
|
||||
fn get_priority_for_recommendation(&self, rec: &WorkflowRecommendation) -> u8 {
|
||||
self.rules
|
||||
.iter()
|
||||
.find(|r| r.pipeline_id == rec.pipeline_id)
|
||||
.map(|r| r.priority)
|
||||
.unwrap_or(5)
|
||||
}
|
||||
|
||||
/// Build suggested inputs from patterns and rule
|
||||
fn build_suggested_inputs(
|
||||
&self,
|
||||
matched_pattern_ids: &[String],
|
||||
patterns: &[&BehaviorPattern],
|
||||
rule: &RecommendationRule,
|
||||
) -> HashMap<String, serde_json::Value> {
|
||||
let mut inputs = HashMap::new();
|
||||
|
||||
for pattern_id in matched_pattern_ids {
|
||||
if let Some(pattern) = patterns.iter().find(|p| p.id == *pattern_id) {
|
||||
// Add context-based inputs
|
||||
if let Some(ref topics) = pattern.context.recent_topics {
|
||||
if !topics.is_empty() {
|
||||
inputs.insert(
|
||||
"topics".to_string(),
|
||||
serde_json::Value::Array(
|
||||
topics.iter().map(|t| serde_json::Value::String(t.clone())).collect()
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref intent) = pattern.context.intent {
|
||||
inputs.insert("intent".to_string(), serde_json::Value::String(intent.clone()));
|
||||
}
|
||||
|
||||
// Add pattern-specific inputs
|
||||
match &pattern.pattern_type {
|
||||
PatternType::InputPattern { keywords, .. } => {
|
||||
inputs.insert(
|
||||
"keywords".to_string(),
|
||||
serde_json::Value::Array(
|
||||
keywords.iter().map(|k| serde_json::Value::String(k.clone())).collect()
|
||||
),
|
||||
);
|
||||
}
|
||||
PatternType::SkillCombination { skill_ids } => {
|
||||
inputs.insert(
|
||||
"skills".to_string(),
|
||||
serde_json::Value::Array(
|
||||
skill_ids.iter().map(|s| serde_json::Value::String(s.clone())).collect()
|
||||
),
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply rule mappings
|
||||
for (source, target) in &rule.input_mappings {
|
||||
if let Some(value) = inputs.get(source) {
|
||||
inputs.insert(target.clone(), value.clone());
|
||||
}
|
||||
}
|
||||
|
||||
inputs
|
||||
}
|
||||
|
||||
/// Find a pipeline that uses the given skills
|
||||
fn find_pipeline_for_skills(&self, skill_ids: &[String]) -> Option<String> {
|
||||
// In production, this would query the pipeline registry
|
||||
// For now, return a default
|
||||
if skill_ids.len() >= 2 {
|
||||
Some("skill-orchestration-pipeline".to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Find a pipeline for an intent
|
||||
fn find_pipeline_for_intent(&self, intent: &str) -> Option<String> {
|
||||
// Map common intents to pipelines
|
||||
match intent {
|
||||
"research" => Some("research-pipeline".to_string()),
|
||||
"analysis" => Some("analysis-pipeline".to_string()),
|
||||
"report" => Some("report-generation".to_string()),
|
||||
"code" => Some("code-generation".to_string()),
|
||||
"task" | "tasks" => Some("task-management".to_string()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a pipeline
|
||||
pub fn register_pipeline(&mut self, metadata: PipelineMetadata) {
|
||||
self.pipeline_registry.insert(metadata.id.clone(), metadata);
|
||||
}
|
||||
|
||||
/// Unregister a pipeline
|
||||
pub fn unregister_pipeline(&mut self, pipeline_id: &str) {
|
||||
self.pipeline_registry.remove(pipeline_id);
|
||||
}
|
||||
|
||||
/// Add a custom recommendation rule
|
||||
pub fn add_rule(&mut self, rule: RecommendationRule) {
|
||||
self.rules.push(rule);
|
||||
// Sort by priority
|
||||
self.rules.sort_by(|a, b| b.priority.cmp(&a.priority));
|
||||
}
|
||||
|
||||
/// Remove a rule
|
||||
pub fn remove_rule(&mut self, rule_id: &str) {
|
||||
self.rules.retain(|r| r.id != rule_id);
|
||||
}
|
||||
|
||||
/// Get all rules
|
||||
pub fn get_rules(&self) -> &[RecommendationRule] {
|
||||
&self.rules
|
||||
}
|
||||
|
||||
/// Update configuration
|
||||
pub fn update_config(&mut self, config: RecommenderConfig) {
|
||||
self.config = config;
|
||||
}
|
||||
|
||||
/// Get configuration
|
||||
pub fn get_config(&self) -> &RecommenderConfig {
|
||||
&self.config
|
||||
}
|
||||
|
||||
/// Get recommendation count
|
||||
pub fn recommendation_count(&self) -> usize {
|
||||
self.recommendations_cache.len()
|
||||
}
|
||||
|
||||
/// Clear recommendation cache
|
||||
pub fn clear_cache(&mut self) {
|
||||
self.recommendations_cache.clear();
|
||||
}
|
||||
|
||||
/// Accept a recommendation (remove from cache and return it)
|
||||
/// Returns the accepted recommendation if found
|
||||
pub fn accept_recommendation(&mut self, recommendation_id: &str) -> Option<WorkflowRecommendation> {
|
||||
if let Some(pos) = self.recommendations_cache.iter().position(|r| r.id == recommendation_id) {
|
||||
Some(self.recommendations_cache.remove(pos))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Dismiss a recommendation (remove from cache without acting on it)
|
||||
/// Returns true if the recommendation was found and dismissed
|
||||
pub fn dismiss_recommendation(&mut self, recommendation_id: &str) -> bool {
|
||||
if let Some(pos) = self.recommendations_cache.iter().position(|r| r.id == recommendation_id) {
|
||||
self.recommendations_cache.remove(pos);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a recommendation by ID
|
||||
pub fn get_recommendation(&self, recommendation_id: &str) -> Option<&WorkflowRecommendation> {
|
||||
self.recommendations_cache.iter().find(|r| r.id == recommendation_id)
|
||||
}
|
||||
|
||||
/// Load recommendations from file
|
||||
pub fn load_from_file(&mut self, path: &str) -> Result<(), String> {
|
||||
let content = std::fs::read_to_string(path)
|
||||
.map_err(|e| format!("Failed to read file: {}", e))?;
|
||||
|
||||
let recommendations: Vec<WorkflowRecommendation> = serde_json::from_str(&content)
|
||||
.map_err(|e| format!("Failed to parse recommendations: {}", e))?;
|
||||
|
||||
self.recommendations_cache = recommendations;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Save recommendations to file
|
||||
pub fn save_to_file(&self, path: &str) -> Result<(), String> {
|
||||
let content = serde_json::to_string_pretty(&self.recommendations_cache)
|
||||
.map_err(|e| format!("Failed to serialize recommendations: {}", e))?;
|
||||
|
||||
std::fs::write(path, content)
|
||||
.map_err(|e| format!("Failed to write file: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_recommender_creation() {
|
||||
let recommender = WorkflowRecommender::new(None);
|
||||
assert!(!recommender.get_rules().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_recommend_from_empty_patterns() {
|
||||
let recommender = WorkflowRecommender::new(None);
|
||||
let recommendations = recommender.recommend(&[]);
|
||||
assert!(recommendations.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rule_priority() {
|
||||
let mut recommender = WorkflowRecommender::new(None);
|
||||
|
||||
recommender.add_rule(RecommendationRule {
|
||||
id: "high_priority".to_string(),
|
||||
pattern_types: vec!["SkillCombination".to_string()],
|
||||
pipeline_id: "important-pipeline".to_string(),
|
||||
base_confidence: 0.9,
|
||||
description: "High priority rule".to_string(),
|
||||
input_mappings: HashMap::new(),
|
||||
priority: 10,
|
||||
});
|
||||
|
||||
let rules = recommender.get_rules();
|
||||
assert!(rules.iter().any(|r| r.priority == 10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_register_pipeline() {
|
||||
let mut recommender = WorkflowRecommender::new(None);
|
||||
|
||||
recommender.register_pipeline(PipelineMetadata {
|
||||
id: "test-pipeline".to_string(),
|
||||
name: "Test Pipeline".to_string(),
|
||||
description: Some("A test pipeline".to_string()),
|
||||
tags: vec!["test".to_string()],
|
||||
input_schema: None,
|
||||
});
|
||||
|
||||
assert!(recommender.pipeline_registry.contains_key("test-pipeline"));
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,10 @@
|
||||
//!
|
||||
//! Phase 3 of Intelligence Layer Migration.
|
||||
//! Reference: ZCLAW_AGENT_INTELLIGENCE_EVOLUTION.md §6.4.2
|
||||
//!
|
||||
//! NOTE: Some methods are reserved for future self-improvement features.
|
||||
|
||||
#![allow(dead_code)] // Methods reserved for future self-improvement features
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
845
desktop/src-tauri/src/intelligence/trigger_evaluator.rs
Normal file
845
desktop/src-tauri/src/intelligence/trigger_evaluator.rs
Normal file
@@ -0,0 +1,845 @@
|
||||
//! Trigger Evaluator - Evaluates context-aware triggers for Hands
|
||||
//!
|
||||
//! This module extends the basic trigger system with semantic matching:
|
||||
//! Supports MemoryQuery, ContextCondition, and IdentityState triggers.
|
||||
//!
|
||||
//! NOTE: This module is not yet integrated into the main application.
|
||||
//! Components are still being developed and will be connected in a future release.
|
||||
|
||||
#![allow(dead_code)] // Module not yet integrated - components under development
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::pin::Pin;
|
||||
use tokio::sync::Mutex;
|
||||
use chrono::{DateTime, Utc, Timelike, Datelike};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
||||
use zclaw_memory::MemoryStore;
|
||||
|
||||
// === ReDoS Protection Constants ===
|
||||
|
||||
/// Maximum allowed length for regex patterns (prevents memory exhaustion)
|
||||
const MAX_REGEX_PATTERN_LENGTH: usize = 500;
|
||||
|
||||
/// Maximum allowed nesting depth for regex quantifiers/groups
|
||||
const MAX_REGEX_NESTING_DEPTH: usize = 10;
|
||||
|
||||
/// Error type for regex validation failures
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum RegexValidationError {
|
||||
/// Pattern exceeds maximum length
|
||||
TooLong { length: usize, max: usize },
|
||||
/// Pattern has excessive nesting depth
|
||||
TooDeeplyNested { depth: usize, max: usize },
|
||||
/// Pattern contains dangerous ReDoS-prone constructs
|
||||
DangerousPattern(String),
|
||||
/// Invalid regex syntax
|
||||
InvalidSyntax(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for RegexValidationError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
RegexValidationError::TooLong { length, max } => {
|
||||
write!(f, "Regex pattern too long: {} bytes (max: {})", length, max)
|
||||
}
|
||||
RegexValidationError::TooDeeplyNested { depth, max } => {
|
||||
write!(f, "Regex pattern too deeply nested: {} levels (max: {})", depth, max)
|
||||
}
|
||||
RegexValidationError::DangerousPattern(reason) => {
|
||||
write!(f, "Dangerous regex pattern detected: {}", reason)
|
||||
}
|
||||
RegexValidationError::InvalidSyntax(err) => {
|
||||
write!(f, "Invalid regex syntax: {}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for RegexValidationError {}
|
||||
|
||||
/// Validate a regex pattern for ReDoS safety
|
||||
///
|
||||
/// This function checks for:
|
||||
/// 1. Pattern length (prevents memory exhaustion)
|
||||
/// 2. Nesting depth (prevents exponential backtracking)
|
||||
/// 3. Dangerous patterns (nested quantifiers on overlapping character classes)
|
||||
fn validate_regex_pattern(pattern: &str) -> Result<(), RegexValidationError> {
|
||||
// Check length
|
||||
if pattern.len() > MAX_REGEX_PATTERN_LENGTH {
|
||||
return Err(RegexValidationError::TooLong {
|
||||
length: pattern.len(),
|
||||
max: MAX_REGEX_PATTERN_LENGTH,
|
||||
});
|
||||
}
|
||||
|
||||
// Check nesting depth by counting unescaped parentheses and brackets
|
||||
let nesting_depth = calculate_nesting_depth(pattern);
|
||||
if nesting_depth > MAX_REGEX_NESTING_DEPTH {
|
||||
return Err(RegexValidationError::TooDeeplyNested {
|
||||
depth: nesting_depth,
|
||||
max: MAX_REGEX_NESTING_DEPTH,
|
||||
});
|
||||
}
|
||||
|
||||
// Check for dangerous ReDoS patterns:
|
||||
// - Nested quantifiers on overlapping patterns like (a+)+
|
||||
// - Alternation with overlapping patterns like (a|a)+
|
||||
if contains_dangerous_redos_pattern(pattern) {
|
||||
return Err(RegexValidationError::DangerousPattern(
|
||||
"Pattern contains nested quantifiers on overlapping character classes".to_string()
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Calculate the maximum nesting depth of groups in a regex pattern
|
||||
fn calculate_nesting_depth(pattern: &str) -> usize {
|
||||
let chars: Vec<char> = pattern.chars().collect();
|
||||
let mut max_depth = 0;
|
||||
let mut current_depth = 0;
|
||||
let mut i = 0;
|
||||
|
||||
while i < chars.len() {
|
||||
let c = chars[i];
|
||||
|
||||
// Check for escape sequence
|
||||
if c == '\\' && i + 1 < chars.len() {
|
||||
// Skip the escaped character
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle character classes [...]
|
||||
if c == '[' {
|
||||
current_depth += 1;
|
||||
max_depth = max_depth.max(current_depth);
|
||||
// Find matching ]
|
||||
i += 1;
|
||||
while i < chars.len() {
|
||||
if chars[i] == '\\' && i + 1 < chars.len() {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if chars[i] == ']' {
|
||||
current_depth -= 1;
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
// Handle groups (...)
|
||||
else if c == '(' {
|
||||
// Skip non-capturing groups and lookaheads for simplicity
|
||||
// (?:...), (?=...), (?!...), (?<=...), (?<!...), (?P<name>...)
|
||||
current_depth += 1;
|
||||
max_depth = max_depth.max(current_depth);
|
||||
} else if c == ')' {
|
||||
if current_depth > 0 {
|
||||
current_depth -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
max_depth
|
||||
}
|
||||
|
||||
/// Check for dangerous ReDoS patterns
|
||||
///
|
||||
/// Detects patterns like:
|
||||
/// - (a+)+ - nested quantifiers
|
||||
/// - (a*)+ - nested quantifiers
|
||||
/// - (a+)* - nested quantifiers
|
||||
/// - (.*)* - nested quantifiers on wildcard
|
||||
fn contains_dangerous_redos_pattern(pattern: &str) -> bool {
|
||||
let chars: Vec<char> = pattern.chars().collect();
|
||||
let mut i = 0;
|
||||
|
||||
while i < chars.len() {
|
||||
// Look for quantified patterns followed by another quantifier
|
||||
if i > 0 {
|
||||
let prev = chars[i - 1];
|
||||
|
||||
// Check if current char is a quantifier
|
||||
if matches!(chars[i], '+' | '*' | '?') {
|
||||
// Look back to see what's being quantified
|
||||
if prev == ')' {
|
||||
// Find the matching opening paren
|
||||
let mut depth = 1;
|
||||
let mut j = i - 2;
|
||||
while j > 0 && depth > 0 {
|
||||
if chars[j] == ')' {
|
||||
depth += 1;
|
||||
} else if chars[j] == '(' {
|
||||
depth -= 1;
|
||||
} else if chars[j] == '\\' && j > 0 {
|
||||
j -= 1; // Skip escaped char
|
||||
}
|
||||
j -= 1;
|
||||
}
|
||||
|
||||
// Check if the group content ends with a quantifier
|
||||
// This would indicate nested quantification
|
||||
// Note: j is usize, so we don't check >= 0 (always true)
|
||||
// The loop above ensures j is valid if depth reached 0
|
||||
let mut k = i - 2;
|
||||
while k > j + 1 {
|
||||
if chars[k] == '\\' && k > 0 {
|
||||
k -= 1;
|
||||
} else if matches!(chars[k], '+' | '*' | '?') {
|
||||
// Found nested quantifier
|
||||
return true;
|
||||
} else if chars[k] == ')' {
|
||||
// Skip nested groups
|
||||
let mut nested_depth = 1;
|
||||
k -= 1;
|
||||
while k > j + 1 && nested_depth > 0 {
|
||||
if chars[k] == ')' {
|
||||
nested_depth += 1;
|
||||
} else if chars[k] == '(' {
|
||||
nested_depth -= 1;
|
||||
} else if chars[k] == '\\' && k > 0 {
|
||||
k -= 1;
|
||||
}
|
||||
k -= 1;
|
||||
}
|
||||
}
|
||||
k -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/// Safely compile a regex pattern with ReDoS protection
|
||||
///
|
||||
/// This function validates the pattern for safety before compilation.
|
||||
/// Returns a compiled regex or an error describing why validation failed.
|
||||
pub fn compile_safe_regex(pattern: &str) -> Result<regex::Regex, RegexValidationError> {
|
||||
validate_regex_pattern(pattern)?;
|
||||
|
||||
regex::Regex::new(pattern).map_err(|e| RegexValidationError::InvalidSyntax(e.to_string()))
|
||||
}
|
||||
|
||||
// === Extended Trigger Types ===
|
||||
|
||||
/// Memory query trigger configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MemoryQueryConfig {
|
||||
/// Memory type to filter (e.g., "task", "preference")
|
||||
pub memory_type: Option<String>,
|
||||
/// Content pattern to match (regex or substring)
|
||||
pub content_pattern: String,
|
||||
/// Minimum count of matching memories
|
||||
pub min_count: usize,
|
||||
/// Minimum importance threshold
|
||||
pub min_importance: Option<i32>,
|
||||
/// Time window for memories (hours)
|
||||
pub time_window_hours: Option<u64>,
|
||||
}
|
||||
|
||||
/// Context condition configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ContextConditionConfig {
|
||||
/// Conditions to check
|
||||
pub conditions: Vec<ContextConditionClause>,
|
||||
/// How to combine conditions (All, Any, None)
|
||||
pub combination: ConditionCombination,
|
||||
}
|
||||
|
||||
/// Single context condition clause
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ContextConditionClause {
|
||||
/// Field to check
|
||||
pub field: ContextField,
|
||||
/// Comparison operator
|
||||
pub operator: ComparisonOperator,
|
||||
/// Value to compare against
|
||||
pub value: JsonValue,
|
||||
}
|
||||
|
||||
/// Context fields that can be checked
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum ContextField {
|
||||
/// Current hour of day (0-23)
|
||||
TimeOfDay,
|
||||
/// Day of week (0=Monday, 6=Sunday)
|
||||
DayOfWeek,
|
||||
/// Currently active project (if any)
|
||||
ActiveProject,
|
||||
/// Topics discussed recently
|
||||
RecentTopic,
|
||||
/// Number of pending tasks
|
||||
PendingTasks,
|
||||
/// Count of memories in storage
|
||||
MemoryCount,
|
||||
/// Hours since last interaction
|
||||
LastInteractionHours,
|
||||
/// Current conversation intent
|
||||
ConversationIntent,
|
||||
}
|
||||
|
||||
/// Comparison operators for context conditions
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum ComparisonOperator {
|
||||
Equals,
|
||||
NotEquals,
|
||||
Contains,
|
||||
GreaterThan,
|
||||
LessThan,
|
||||
Exists,
|
||||
NotExists,
|
||||
Matches, // regex match
|
||||
}
|
||||
|
||||
/// How to combine multiple conditions
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum ConditionCombination {
|
||||
/// All conditions must true
|
||||
All,
|
||||
/// Any one condition being true is enough
|
||||
Any,
|
||||
/// None of the conditions should be true
|
||||
None,
|
||||
}
|
||||
|
||||
/// Identity state trigger configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct IdentityStateConfig {
|
||||
/// Identity file to check
|
||||
pub file: IdentityFile,
|
||||
/// Content pattern to match (regex)
|
||||
pub content_pattern: Option<String>,
|
||||
/// Trigger on any change to the file
|
||||
pub any_change: bool,
|
||||
}
|
||||
|
||||
/// Identity files that can be monitored
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum IdentityFile {
|
||||
Soul,
|
||||
Instructions,
|
||||
User,
|
||||
}
|
||||
|
||||
/// Composite trigger configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CompositeTriggerConfig {
|
||||
/// Sub-triggers to combine
|
||||
pub triggers: Vec<ExtendedTriggerType>,
|
||||
/// How to combine results
|
||||
pub combination: ConditionCombination,
|
||||
}
|
||||
|
||||
/// Extended trigger type that includes semantic triggers
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum ExtendedTriggerType {
|
||||
/// Standard interval trigger
|
||||
Interval {
|
||||
/// Interval in seconds
|
||||
seconds: u64,
|
||||
},
|
||||
/// Time-of-day trigger
|
||||
TimeOfDay {
|
||||
/// Hour (0-23)
|
||||
hour: u8,
|
||||
/// Optional minute (0-59)
|
||||
minute: Option<u8>,
|
||||
},
|
||||
/// Memory query trigger
|
||||
MemoryQuery(MemoryQueryConfig),
|
||||
/// Context condition trigger
|
||||
ContextCondition(ContextConditionConfig),
|
||||
/// Identity state trigger
|
||||
IdentityState(IdentityStateConfig),
|
||||
/// Composite trigger
|
||||
Composite(CompositeTriggerConfig),
|
||||
}
|
||||
|
||||
// === Trigger Evaluator ===
|
||||
|
||||
/// Evaluator for context-aware triggers
|
||||
pub struct TriggerEvaluator {
|
||||
/// Memory store for memory queries
|
||||
memory_store: Arc<MemoryStore>,
|
||||
/// Identity manager for identity triggers
|
||||
identity_manager: Arc<Mutex<super::identity::AgentIdentityManager>>,
|
||||
/// Heartbeat engine for context
|
||||
heartbeat_engine: Arc<Mutex<super::heartbeat::HeartbeatEngine>>,
|
||||
/// Cached context data
|
||||
context_cache: Arc<Mutex<TriggerContextCache>>,
|
||||
}
|
||||
|
||||
/// Cached context for trigger evaluation
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct TriggerContextCache {
|
||||
/// Last known active project
|
||||
pub active_project: Option<String>,
|
||||
/// Recent topics discussed
|
||||
pub recent_topics: Vec<String>,
|
||||
/// Last conversation intent
|
||||
pub conversation_intent: Option<String>,
|
||||
/// Last update time
|
||||
pub last_updated: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl TriggerEvaluator {
|
||||
/// Create a new trigger evaluator
|
||||
pub fn new(
|
||||
memory_store: Arc<MemoryStore>,
|
||||
identity_manager: Arc<Mutex<super::identity::AgentIdentityManager>>,
|
||||
heartbeat_engine: Arc<Mutex<super::heartbeat::HeartbeatEngine>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
memory_store,
|
||||
identity_manager,
|
||||
heartbeat_engine,
|
||||
context_cache: Arc::new(Mutex::new(TriggerContextCache::default())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluate a trigger
|
||||
pub async fn evaluate(
|
||||
&self,
|
||||
trigger: &ExtendedTriggerType,
|
||||
agent_id: &str,
|
||||
) -> Result<bool, String> {
|
||||
match trigger {
|
||||
ExtendedTriggerType::Interval { .. } => Ok(true),
|
||||
ExtendedTriggerType::TimeOfDay { hour, minute } => {
|
||||
let now = Utc::now();
|
||||
let current_hour = now.hour() as u8;
|
||||
let current_minute = now.minute() as u8;
|
||||
|
||||
if current_hour != *hour {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
if let Some(min) = minute {
|
||||
if current_minute != *min {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
ExtendedTriggerType::MemoryQuery(config) => {
|
||||
self.evaluate_memory_query(config, agent_id).await
|
||||
}
|
||||
ExtendedTriggerType::ContextCondition(config) => {
|
||||
self.evaluate_context_condition(config, agent_id).await
|
||||
}
|
||||
ExtendedTriggerType::IdentityState(config) => {
|
||||
self.evaluate_identity_state(config, agent_id).await
|
||||
}
|
||||
ExtendedTriggerType::Composite(config) => {
|
||||
self.evaluate_composite(config, agent_id, None).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluate memory query trigger
|
||||
async fn evaluate_memory_query(
|
||||
&self,
|
||||
config: &MemoryQueryConfig,
|
||||
_agent_id: &str,
|
||||
) -> Result<bool, String> {
|
||||
// TODO: Implement proper memory search when MemoryStore supports it
|
||||
// For now, use KV store to check if we have enough keys matching pattern
|
||||
// This is a simplified implementation
|
||||
|
||||
// Memory search is not fully implemented in current MemoryStore
|
||||
// Return false to indicate no matches until proper search is available
|
||||
tracing::warn!(
|
||||
pattern = %config.content_pattern,
|
||||
min_count = config.min_count,
|
||||
"Memory query trigger evaluation not fully implemented"
|
||||
);
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Evaluate context condition trigger
|
||||
async fn evaluate_context_condition(
|
||||
&self,
|
||||
config: &ContextConditionConfig,
|
||||
agent_id: &str,
|
||||
) -> Result<bool, String> {
|
||||
let context = self.get_cached_context(agent_id).await;
|
||||
|
||||
let mut results = Vec::new();
|
||||
|
||||
for condition in &config.conditions {
|
||||
let result = self.evaluate_condition_clause(condition, &context);
|
||||
results.push(result);
|
||||
}
|
||||
|
||||
// Combine results based on combination mode
|
||||
let final_result = match config.combination {
|
||||
ConditionCombination::All => results.iter().all(|r| *r),
|
||||
ConditionCombination::Any => results.iter().any(|r| *r),
|
||||
ConditionCombination::None => results.iter().all(|r| !*r),
|
||||
};
|
||||
|
||||
Ok(final_result)
|
||||
}
|
||||
|
||||
/// Evaluate a single condition clause
|
||||
fn evaluate_condition_clause(
|
||||
&self,
|
||||
clause: &ContextConditionClause,
|
||||
context: &TriggerContextCache,
|
||||
) -> bool {
|
||||
match clause.field {
|
||||
ContextField::TimeOfDay => {
|
||||
let now = Utc::now();
|
||||
let current_hour = now.hour() as i32;
|
||||
self.compare_values(current_hour, &clause.operator, &clause.value)
|
||||
}
|
||||
ContextField::DayOfWeek => {
|
||||
let now = Utc::now();
|
||||
let current_day = now.weekday().num_days_from_monday() as i32;
|
||||
self.compare_values(current_day, &clause.operator, &clause.value)
|
||||
}
|
||||
ContextField::ActiveProject => {
|
||||
if let Some(project) = &context.active_project {
|
||||
self.compare_values(project.clone(), &clause.operator, &clause.value)
|
||||
} else {
|
||||
matches!(clause.operator, ComparisonOperator::NotExists)
|
||||
}
|
||||
}
|
||||
ContextField::RecentTopic => {
|
||||
if let Some(topic) = context.recent_topics.first() {
|
||||
self.compare_values(topic.clone(), &clause.operator, &clause.value)
|
||||
} else {
|
||||
matches!(clause.operator, ComparisonOperator::NotExists)
|
||||
}
|
||||
}
|
||||
ContextField::PendingTasks => {
|
||||
// Would need to query memory store
|
||||
false // Not implemented yet
|
||||
}
|
||||
ContextField::MemoryCount => {
|
||||
// Would need to query memory store
|
||||
false // Not implemented yet
|
||||
}
|
||||
ContextField::LastInteractionHours => {
|
||||
if let Some(last_updated) = context.last_updated {
|
||||
let hours = (Utc::now() - last_updated).num_hours();
|
||||
self.compare_values(hours as i32, &clause.operator, &clause.value)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
ContextField::ConversationIntent => {
|
||||
if let Some(intent) = &context.conversation_intent {
|
||||
self.compare_values(intent.clone(), &clause.operator, &clause.value)
|
||||
} else {
|
||||
matches!(clause.operator, ComparisonOperator::NotExists)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compare values using operator
|
||||
fn compare_values<T>(&self, actual: T, operator: &ComparisonOperator, expected: &JsonValue) -> bool
|
||||
where
|
||||
T: Into<JsonValue>,
|
||||
{
|
||||
let actual_value = actual.into();
|
||||
|
||||
match operator {
|
||||
ComparisonOperator::Equals => &actual_value == expected,
|
||||
ComparisonOperator::NotEquals => &actual_value != expected,
|
||||
ComparisonOperator::Contains => {
|
||||
if let (Some(actual_str), Some(expected_str)) =
|
||||
(actual_value.as_str(), expected.as_str())
|
||||
{
|
||||
actual_str.contains(expected_str)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
ComparisonOperator::GreaterThan => {
|
||||
if let (Some(actual_num), Some(expected_num)) =
|
||||
(actual_value.as_i64(), expected.as_i64())
|
||||
{
|
||||
actual_num > expected_num
|
||||
} else if let (Some(actual_num), Some(expected_num)) =
|
||||
(actual_value.as_f64(), expected.as_f64())
|
||||
{
|
||||
actual_num > expected_num
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
ComparisonOperator::LessThan => {
|
||||
if let (Some(actual_num), Some(expected_num)) =
|
||||
(actual_value.as_i64(), expected.as_i64())
|
||||
{
|
||||
actual_num < expected_num
|
||||
} else if let (Some(actual_num), Some(expected_num)) =
|
||||
(actual_value.as_f64(), expected.as_f64())
|
||||
{
|
||||
actual_num < expected_num
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
ComparisonOperator::Exists => !actual_value.is_null(),
|
||||
ComparisonOperator::NotExists => actual_value.is_null(),
|
||||
ComparisonOperator::Matches => {
|
||||
if let (Some(actual_str), Some(expected_str)) =
|
||||
(actual_value.as_str(), expected.as_str())
|
||||
{
|
||||
compile_safe_regex(expected_str)
|
||||
.map(|re| re.is_match(actual_str))
|
||||
.unwrap_or_else(|e| {
|
||||
tracing::warn!(
|
||||
pattern = %expected_str,
|
||||
error = %e,
|
||||
"Regex pattern validation failed, treating as no match"
|
||||
);
|
||||
false
|
||||
})
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluate identity state trigger
|
||||
async fn evaluate_identity_state(
|
||||
&self,
|
||||
config: &IdentityStateConfig,
|
||||
agent_id: &str,
|
||||
) -> Result<bool, String> {
|
||||
let mut manager = self.identity_manager.lock().await;
|
||||
let identity = manager.get_identity(agent_id);
|
||||
|
||||
// Get the target file content
|
||||
let content = match config.file {
|
||||
IdentityFile::Soul => identity.soul,
|
||||
IdentityFile::Instructions => identity.instructions,
|
||||
IdentityFile::User => identity.user_profile,
|
||||
};
|
||||
|
||||
// Check content pattern if specified
|
||||
if let Some(pattern) = &config.content_pattern {
|
||||
let re = compile_safe_regex(pattern)
|
||||
.map_err(|e| format!("Invalid regex pattern: {}", e))?;
|
||||
if !re.is_match(&content) {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
// If any_change is true, we would need to track changes
|
||||
// For now, just return true
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Get cached context for an agent
|
||||
async fn get_cached_context(&self, _agent_id: &str) -> TriggerContextCache {
|
||||
self.context_cache.lock().await.clone()
|
||||
}
|
||||
|
||||
/// Evaluate composite trigger
|
||||
fn evaluate_composite<'a>(
|
||||
&'a self,
|
||||
config: &'a CompositeTriggerConfig,
|
||||
agent_id: &'a str,
|
||||
_depth: Option<usize>,
|
||||
) -> Pin<Box<dyn std::future::Future<Output = Result<bool, String>> + 'a>> {
|
||||
Box::pin(async move {
|
||||
let mut results = Vec::new();
|
||||
|
||||
for trigger in &config.triggers {
|
||||
let result = self.evaluate(trigger, agent_id).await?;
|
||||
results.push(result);
|
||||
}
|
||||
|
||||
// Combine results based on combination mode
|
||||
let final_result = match config.combination {
|
||||
ConditionCombination::All => results.iter().all(|r| *r),
|
||||
ConditionCombination::Any => results.iter().any(|r| *r),
|
||||
ConditionCombination::None => results.iter().all(|r| !*r),
|
||||
};
|
||||
|
||||
Ok(final_result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// === Unit Tests ===
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
mod regex_validation {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_valid_simple_pattern() {
|
||||
let pattern = r"hello";
|
||||
assert!(compile_safe_regex(pattern).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_pattern_with_quantifiers() {
|
||||
let pattern = r"\d+";
|
||||
assert!(compile_safe_regex(pattern).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_pattern_with_groups() {
|
||||
let pattern = r"(foo|bar)\d{2,4}";
|
||||
assert!(compile_safe_regex(pattern).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_character_class() {
|
||||
let pattern = r"[a-zA-Z0-9_]+";
|
||||
assert!(compile_safe_regex(pattern).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pattern_too_long() {
|
||||
let pattern = "a".repeat(501);
|
||||
let result = compile_safe_regex(&pattern);
|
||||
assert!(matches!(result, Err(RegexValidationError::TooLong { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pattern_at_max_length() {
|
||||
let pattern = "a".repeat(500);
|
||||
let result = compile_safe_regex(&pattern);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_quantifier_detection_simple() {
|
||||
// Classic ReDoS pattern: (a+)+
|
||||
// Our implementation detects this as dangerous
|
||||
let pattern = r"(a+)+";
|
||||
let result = validate_regex_pattern(pattern);
|
||||
assert!(
|
||||
matches!(result, Err(RegexValidationError::DangerousPattern(_))),
|
||||
"Expected nested quantifier pattern to be detected as dangerous"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deeply_nested_groups() {
|
||||
// Create a pattern with too many nested groups
|
||||
let pattern = "(".repeat(15) + &"a".repeat(10) + &")".repeat(15);
|
||||
let result = compile_safe_regex(&pattern);
|
||||
assert!(matches!(result, Err(RegexValidationError::TooDeeplyNested { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reasonably_nested_groups() {
|
||||
// Pattern with acceptable nesting
|
||||
let pattern = "(((foo|bar)))";
|
||||
let result = compile_safe_regex(pattern);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_regex_syntax() {
|
||||
let pattern = r"[unclosed";
|
||||
let result = compile_safe_regex(pattern);
|
||||
assert!(matches!(result, Err(RegexValidationError::InvalidSyntax(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_escaped_characters_in_pattern() {
|
||||
let pattern = r"\[hello\]";
|
||||
let result = compile_safe_regex(pattern);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_complex_valid_pattern() {
|
||||
// Email-like pattern (simplified)
|
||||
let pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}";
|
||||
let result = compile_safe_regex(pattern);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
mod nesting_depth_calculation {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_no_nesting() {
|
||||
assert_eq!(calculate_nesting_depth("abc"), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_single_group() {
|
||||
assert_eq!(calculate_nesting_depth("(abc)"), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_groups() {
|
||||
assert_eq!(calculate_nesting_depth("((abc))"), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_character_class() {
|
||||
assert_eq!(calculate_nesting_depth("[abc]"), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mixed_nesting() {
|
||||
assert_eq!(calculate_nesting_depth("([a-z]+)"), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_escaped_parens() {
|
||||
// Escaped parens shouldn't count toward nesting
|
||||
assert_eq!(calculate_nesting_depth(r"\(abc\)"), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiple_groups_same_level() {
|
||||
assert_eq!(calculate_nesting_depth("(abc)(def)"), 1);
|
||||
}
|
||||
}
|
||||
|
||||
mod dangerous_pattern_detection {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_simple_quantifier_not_dangerous() {
|
||||
assert!(!contains_dangerous_redos_pattern(r"a+"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple_group_not_dangerous() {
|
||||
assert!(!contains_dangerous_redos_pattern(r"(abc)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_quantified_group_not_dangerous() {
|
||||
assert!(!contains_dangerous_redos_pattern(r"(abc)+"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_alternation_not_dangerous() {
|
||||
assert!(!contains_dangerous_redos_pattern(r"(a|b)+"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
272
desktop/src-tauri/src/intelligence/validation.rs
Normal file
272
desktop/src-tauri/src/intelligence/validation.rs
Normal file
@@ -0,0 +1,272 @@
|
||||
//! Input validation utilities for the Intelligence Layer
|
||||
//!
|
||||
//! This module provides validation functions for common input types
|
||||
//! to prevent injection attacks, path traversal, and memory exhaustion.
|
||||
//!
|
||||
//! NOTE: Some functions are defined for future use and external API exposure.
|
||||
|
||||
#![allow(dead_code)] // Validation functions reserved for future API endpoints
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// Maximum length for identifier strings (agent_id, pipeline_id, skill_id, etc.)
|
||||
pub const MAX_IDENTIFIER_LENGTH: usize = 128;
|
||||
|
||||
/// Minimum length for identifier strings
|
||||
pub const MIN_IDENTIFIER_LENGTH: usize = 1;
|
||||
|
||||
/// Allowed characters in identifiers: alphanumeric, hyphen, underscore
|
||||
const IDENTIFIER_ALLOWED_CHARS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
|
||||
|
||||
/// Validation error types
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ValidationError {
|
||||
/// Identifier is too long
|
||||
IdentifierTooLong { field: String, max: usize, actual: usize },
|
||||
/// Identifier is too short or empty
|
||||
IdentifierTooShort { field: String, min: usize, actual: usize },
|
||||
/// Identifier contains invalid characters
|
||||
InvalidCharacters { field: String, invalid_chars: String },
|
||||
/// String exceeds maximum length
|
||||
StringTooLong { field: String, max: usize, actual: usize },
|
||||
/// Required field is missing or empty
|
||||
RequiredFieldEmpty { field: String },
|
||||
}
|
||||
|
||||
impl fmt::Display for ValidationError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::IdentifierTooLong { field, max, actual } => {
|
||||
write!(f, "Field '{}' is too long: {} characters (max: {})", field, actual, max)
|
||||
}
|
||||
Self::IdentifierTooShort { field, min, actual } => {
|
||||
write!(f, "Field '{}' is too short: {} characters (min: {})", field, actual, min)
|
||||
}
|
||||
Self::InvalidCharacters { field, invalid_chars } => {
|
||||
write!(f, "Field '{}' contains invalid characters: '{}'. Allowed: alphanumeric, '-', '_'", field, invalid_chars)
|
||||
}
|
||||
Self::StringTooLong { field, max, actual } => {
|
||||
write!(f, "Field '{}' is too long: {} characters (max: {})", field, actual, max)
|
||||
}
|
||||
Self::RequiredFieldEmpty { field } => {
|
||||
write!(f, "Required field '{}' is empty", field)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ValidationError {}
|
||||
|
||||
/// Validate an identifier (agent_id, pipeline_id, skill_id, etc.)
|
||||
///
|
||||
/// # Rules
|
||||
/// - Length: 1-128 characters
|
||||
/// - Characters: alphanumeric, hyphen (-), underscore (_)
|
||||
/// - Cannot start with hyphen or underscore
|
||||
///
|
||||
/// # Examples
|
||||
/// ```ignore
|
||||
/// use desktop_lib::intelligence::validation::validate_identifier;
|
||||
///
|
||||
/// assert!(validate_identifier("agent-123", "agent_id").is_ok());
|
||||
/// assert!(validate_identifier("my_skill", "skill_id").is_ok());
|
||||
/// assert!(validate_identifier("", "agent_id").is_err());
|
||||
/// assert!(validate_identifier("invalid@id", "agent_id").is_err());
|
||||
/// ```
|
||||
pub fn validate_identifier(value: &str, field_name: &str) -> Result<(), ValidationError> {
|
||||
let len = value.len();
|
||||
|
||||
// Check minimum length
|
||||
if len < MIN_IDENTIFIER_LENGTH {
|
||||
return Err(ValidationError::IdentifierTooShort {
|
||||
field: field_name.to_string(),
|
||||
min: MIN_IDENTIFIER_LENGTH,
|
||||
actual: len,
|
||||
});
|
||||
}
|
||||
|
||||
// Check maximum length
|
||||
if len > MAX_IDENTIFIER_LENGTH {
|
||||
return Err(ValidationError::IdentifierTooLong {
|
||||
field: field_name.to_string(),
|
||||
max: MAX_IDENTIFIER_LENGTH,
|
||||
actual: len,
|
||||
});
|
||||
}
|
||||
|
||||
// Check for invalid characters
|
||||
let invalid_chars: String = value
|
||||
.chars()
|
||||
.filter(|c| !IDENTIFIER_ALLOWED_CHARS.contains(*c))
|
||||
.collect();
|
||||
|
||||
if !invalid_chars.is_empty() {
|
||||
return Err(ValidationError::InvalidCharacters {
|
||||
field: field_name.to_string(),
|
||||
invalid_chars,
|
||||
});
|
||||
}
|
||||
|
||||
// Cannot start with hyphen or underscore (reserved for system use)
|
||||
if value.starts_with('-') || value.starts_with('_') {
|
||||
return Err(ValidationError::InvalidCharacters {
|
||||
field: field_name.to_string(),
|
||||
invalid_chars: value.chars().next().unwrap().to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate a string field with a maximum length
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `value` - The string to validate
|
||||
/// * `field_name` - Name of the field for error messages
|
||||
/// * `max_length` - Maximum allowed length
|
||||
///
|
||||
/// # Examples
|
||||
/// ```ignore
|
||||
/// use desktop_lib::intelligence::validation::validate_string_length;
|
||||
///
|
||||
/// assert!(validate_string_length("hello", "message", 100).is_ok());
|
||||
/// assert!(validate_string_length("", "message", 100).is_err());
|
||||
/// ```
|
||||
pub fn validate_string_length(value: &str, field_name: &str, max_length: usize) -> Result<(), ValidationError> {
|
||||
let len = value.len();
|
||||
|
||||
if len == 0 {
|
||||
return Err(ValidationError::RequiredFieldEmpty {
|
||||
field: field_name.to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
if len > max_length {
|
||||
return Err(ValidationError::StringTooLong {
|
||||
field: field_name.to_string(),
|
||||
max: max_length,
|
||||
actual: len,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validate an optional identifier field
|
||||
///
|
||||
/// Returns Ok if the value is None or if it contains a valid identifier.
|
||||
pub fn validate_optional_identifier(value: Option<&str>, field_name: &str) -> Result<(), ValidationError> {
|
||||
match value {
|
||||
None => Ok(()),
|
||||
Some(v) if v.is_empty() => Ok(()), // Empty string treated as None
|
||||
Some(v) => validate_identifier(v, field_name),
|
||||
}
|
||||
}
|
||||
|
||||
/// Validate a list of identifiers
|
||||
pub fn validate_identifiers<'a, I>(values: I, field_name: &str) -> Result<(), ValidationError>
|
||||
where
|
||||
I: IntoIterator<Item = &'a str>,
|
||||
{
|
||||
for value in values {
|
||||
validate_identifier(value, field_name)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sanitize a string for safe logging (remove control characters, limit length)
|
||||
pub fn sanitize_for_logging(value: &str, max_len: usize) -> String {
|
||||
let sanitized: String = value
|
||||
.chars()
|
||||
.filter(|c| !c.is_control() || *c == '\n' || *c == '\t')
|
||||
.take(max_len)
|
||||
.collect();
|
||||
|
||||
if value.len() > max_len {
|
||||
format!("{}...", sanitized)
|
||||
} else {
|
||||
sanitized
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_valid_identifiers() {
|
||||
assert!(validate_identifier("agent-123", "agent_id").is_ok());
|
||||
assert!(validate_identifier("my_skill", "skill_id").is_ok());
|
||||
assert!(validate_identifier("Pipeline42", "pipeline_id").is_ok());
|
||||
assert!(validate_identifier("a", "test").is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_identifiers() {
|
||||
// Too short
|
||||
assert!(matches!(
|
||||
validate_identifier("", "agent_id"),
|
||||
Err(ValidationError::IdentifierTooShort { .. })
|
||||
));
|
||||
|
||||
// Too long
|
||||
let long_id = "a".repeat(200);
|
||||
assert!(matches!(
|
||||
validate_identifier(&long_id, "agent_id"),
|
||||
Err(ValidationError::IdentifierTooLong { .. })
|
||||
));
|
||||
|
||||
// Invalid characters
|
||||
assert!(matches!(
|
||||
validate_identifier("invalid@id", "agent_id"),
|
||||
Err(ValidationError::InvalidCharacters { .. })
|
||||
));
|
||||
|
||||
assert!(matches!(
|
||||
validate_identifier("invalid id", "agent_id"),
|
||||
Err(ValidationError::InvalidCharacters { .. })
|
||||
));
|
||||
|
||||
// Starts with reserved characters
|
||||
assert!(matches!(
|
||||
validate_identifier("-invalid", "agent_id"),
|
||||
Err(ValidationError::InvalidCharacters { .. })
|
||||
));
|
||||
|
||||
assert!(matches!(
|
||||
validate_identifier("_invalid", "agent_id"),
|
||||
Err(ValidationError::InvalidCharacters { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_string_length_validation() {
|
||||
assert!(validate_string_length("hello", "message", 100).is_ok());
|
||||
assert!(matches!(
|
||||
validate_string_length("", "message", 100),
|
||||
Err(ValidationError::RequiredFieldEmpty { .. })
|
||||
));
|
||||
|
||||
let long_string = "a".repeat(200);
|
||||
assert!(matches!(
|
||||
validate_string_length(&long_string, "message", 100),
|
||||
Err(ValidationError::StringTooLong { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optional_identifier() {
|
||||
assert!(validate_optional_identifier(None, "agent_id").is_ok());
|
||||
assert!(validate_optional_identifier(Some(""), "agent_id").is_ok());
|
||||
assert!(validate_optional_identifier(Some("valid-id"), "agent_id").is_ok());
|
||||
assert!(validate_optional_identifier(Some("invalid@id"), "agent_id").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sanitize_for_logging() {
|
||||
assert_eq!(sanitize_for_logging("hello", 100), "hello");
|
||||
assert_eq!(sanitize_for_logging("hello\x00world", 100), "helloworld");
|
||||
assert_eq!(sanitize_for_logging("hello\nworld", 100), "hello\nworld");
|
||||
assert_eq!(sanitize_for_logging("hello world", 5), "hello...");
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,25 @@ use tokio::sync::Mutex;
|
||||
use zclaw_kernel::Kernel;
|
||||
use zclaw_types::{AgentConfig, AgentId, AgentInfo};
|
||||
|
||||
use crate::intelligence::validation::{validate_identifier, validate_string_length};
|
||||
|
||||
/// Kernel state wrapper for Tauri
|
||||
pub type KernelState = Arc<Mutex<Option<Kernel>>>;
|
||||
|
||||
/// Validate an agent ID string with clear error messages
|
||||
fn validate_agent_id(agent_id: &str) -> Result<String, String> {
|
||||
validate_identifier(agent_id, "agent_id")
|
||||
.map_err(|e| format!("Invalid agent_id: {}", e))?;
|
||||
Ok(agent_id.to_string())
|
||||
}
|
||||
|
||||
/// Validate a generic ID string (for skills, hands, triggers, etc.)
|
||||
fn validate_id(id: &str, field_name: &str) -> Result<String, String> {
|
||||
validate_identifier(id, field_name)
|
||||
.map_err(|e| format!("Invalid {}: {}", field_name, e))?;
|
||||
Ok(id.to_string())
|
||||
}
|
||||
|
||||
/// Agent creation request
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -295,6 +311,9 @@ pub async fn agent_get(
|
||||
state: State<'_, KernelState>,
|
||||
agent_id: String,
|
||||
) -> Result<Option<AgentInfo>, String> {
|
||||
// Validate input
|
||||
let agent_id = validate_agent_id(&agent_id)?;
|
||||
|
||||
let kernel_lock = state.lock().await;
|
||||
|
||||
let kernel = kernel_lock.as_ref()
|
||||
@@ -312,6 +331,9 @@ pub async fn agent_delete(
|
||||
state: State<'_, KernelState>,
|
||||
agent_id: String,
|
||||
) -> Result<(), String> {
|
||||
// Validate input
|
||||
let agent_id = validate_agent_id(&agent_id)?;
|
||||
|
||||
let kernel_lock = state.lock().await;
|
||||
|
||||
let kernel = kernel_lock.as_ref()
|
||||
@@ -331,6 +353,11 @@ pub async fn agent_chat(
|
||||
state: State<'_, KernelState>,
|
||||
request: ChatRequest,
|
||||
) -> Result<ChatResponse, String> {
|
||||
// Validate inputs
|
||||
validate_agent_id(&request.agent_id)?;
|
||||
validate_string_length(&request.message, "message", 100000)
|
||||
.map_err(|e| format!("Invalid message: {}", e))?;
|
||||
|
||||
let kernel_lock = state.lock().await;
|
||||
|
||||
let kernel = kernel_lock.as_ref()
|
||||
@@ -391,6 +418,11 @@ pub async fn agent_chat_stream(
|
||||
state: State<'_, KernelState>,
|
||||
request: StreamChatRequest,
|
||||
) -> Result<(), String> {
|
||||
// Validate inputs
|
||||
validate_agent_id(&request.agent_id)?;
|
||||
validate_string_length(&request.message, "message", 100000)
|
||||
.map_err(|e| format!("Invalid message: {}", e))?;
|
||||
|
||||
// Parse agent ID first
|
||||
let id: AgentId = request.agent_id.parse()
|
||||
.map_err(|_| "Invalid agent ID format".to_string())?;
|
||||
@@ -613,6 +645,9 @@ pub async fn skill_execute(
|
||||
context: SkillContext,
|
||||
input: serde_json::Value,
|
||||
) -> Result<SkillResult, String> {
|
||||
// Validate skill ID
|
||||
let id = validate_id(&id, "skill_id")?;
|
||||
|
||||
let kernel_lock = state.lock().await;
|
||||
|
||||
let kernel = kernel_lock.as_ref()
|
||||
@@ -760,3 +795,286 @@ pub async fn hand_execute(
|
||||
|
||||
Ok(HandResult::from(result))
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Trigger Commands
|
||||
// ============================================================
|
||||
|
||||
/// Trigger configuration for creation/update
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TriggerConfigRequest {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub hand_id: String,
|
||||
pub trigger_type: TriggerTypeRequest,
|
||||
#[serde(default = "default_trigger_enabled")]
|
||||
pub enabled: bool,
|
||||
#[serde(default)]
|
||||
pub description: Option<String>,
|
||||
#[serde(default)]
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
fn default_trigger_enabled() -> bool { true }
|
||||
|
||||
/// Trigger type for API
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum TriggerTypeRequest {
|
||||
Schedule { cron: String },
|
||||
Event { pattern: String },
|
||||
Webhook { path: String, secret: Option<String> },
|
||||
MessagePattern { pattern: String },
|
||||
FileSystem { path: String, events: Vec<String> },
|
||||
Manual,
|
||||
}
|
||||
|
||||
/// Trigger response
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TriggerResponse {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub hand_id: String,
|
||||
pub trigger_type: TriggerTypeRequest,
|
||||
pub enabled: bool,
|
||||
pub created_at: String,
|
||||
pub modified_at: String,
|
||||
pub description: Option<String>,
|
||||
pub tags: Vec<String>,
|
||||
}
|
||||
|
||||
impl From<zclaw_kernel::trigger_manager::TriggerEntry> for TriggerResponse {
|
||||
fn from(entry: zclaw_kernel::trigger_manager::TriggerEntry) -> Self {
|
||||
let trigger_type = match entry.config.trigger_type {
|
||||
zclaw_hands::TriggerType::Schedule { cron } => {
|
||||
TriggerTypeRequest::Schedule { cron }
|
||||
}
|
||||
zclaw_hands::TriggerType::Event { pattern } => {
|
||||
TriggerTypeRequest::Event { pattern }
|
||||
}
|
||||
zclaw_hands::TriggerType::Webhook { path, secret } => {
|
||||
TriggerTypeRequest::Webhook { path, secret }
|
||||
}
|
||||
zclaw_hands::TriggerType::MessagePattern { pattern } => {
|
||||
TriggerTypeRequest::MessagePattern { pattern }
|
||||
}
|
||||
zclaw_hands::TriggerType::FileSystem { path, events } => {
|
||||
TriggerTypeRequest::FileSystem {
|
||||
path,
|
||||
events: events.iter().map(|e| format!("{:?}", e).to_lowercase()).collect(),
|
||||
}
|
||||
}
|
||||
zclaw_hands::TriggerType::Manual => TriggerTypeRequest::Manual,
|
||||
};
|
||||
|
||||
Self {
|
||||
id: entry.config.id,
|
||||
name: entry.config.name,
|
||||
hand_id: entry.config.hand_id,
|
||||
trigger_type,
|
||||
enabled: entry.config.enabled,
|
||||
created_at: entry.created_at.to_rfc3339(),
|
||||
modified_at: entry.modified_at.to_rfc3339(),
|
||||
description: entry.description,
|
||||
tags: entry.tags,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// List all triggers
|
||||
#[tauri::command]
|
||||
pub async fn trigger_list(
|
||||
state: State<'_, KernelState>,
|
||||
) -> Result<Vec<TriggerResponse>, String> {
|
||||
let kernel_lock = state.lock().await;
|
||||
let kernel = kernel_lock.as_ref()
|
||||
.ok_or_else(|| "Kernel not initialized".to_string())?;
|
||||
|
||||
let triggers = kernel.list_triggers().await;
|
||||
Ok(triggers.into_iter().map(TriggerResponse::from).collect())
|
||||
}
|
||||
|
||||
/// Get a specific trigger
|
||||
#[tauri::command]
|
||||
pub async fn trigger_get(
|
||||
state: State<'_, KernelState>,
|
||||
id: String,
|
||||
) -> Result<Option<TriggerResponse>, String> {
|
||||
// Validate trigger ID
|
||||
let id = validate_id(&id, "trigger_id")?;
|
||||
|
||||
let kernel_lock = state.lock().await;
|
||||
let kernel = kernel_lock.as_ref()
|
||||
.ok_or_else(|| "Kernel not initialized".to_string())?;
|
||||
|
||||
Ok(kernel.get_trigger(&id).await.map(TriggerResponse::from))
|
||||
}
|
||||
|
||||
/// Create a new trigger
|
||||
#[tauri::command]
|
||||
pub async fn trigger_create(
|
||||
state: State<'_, KernelState>,
|
||||
request: TriggerConfigRequest,
|
||||
) -> Result<TriggerResponse, String> {
|
||||
let kernel_lock = state.lock().await;
|
||||
let kernel = kernel_lock.as_ref()
|
||||
.ok_or_else(|| "Kernel not initialized".to_string())?;
|
||||
|
||||
// Convert request to config
|
||||
let trigger_type = match request.trigger_type {
|
||||
TriggerTypeRequest::Schedule { cron } => {
|
||||
zclaw_hands::TriggerType::Schedule { cron }
|
||||
}
|
||||
TriggerTypeRequest::Event { pattern } => {
|
||||
zclaw_hands::TriggerType::Event { pattern }
|
||||
}
|
||||
TriggerTypeRequest::Webhook { path, secret } => {
|
||||
zclaw_hands::TriggerType::Webhook { path, secret }
|
||||
}
|
||||
TriggerTypeRequest::MessagePattern { pattern } => {
|
||||
zclaw_hands::TriggerType::MessagePattern { pattern }
|
||||
}
|
||||
TriggerTypeRequest::FileSystem { path, events } => {
|
||||
zclaw_hands::TriggerType::FileSystem {
|
||||
path,
|
||||
events: events.iter().filter_map(|e| match e.as_str() {
|
||||
"created" => Some(zclaw_hands::FileEvent::Created),
|
||||
"modified" => Some(zclaw_hands::FileEvent::Modified),
|
||||
"deleted" => Some(zclaw_hands::FileEvent::Deleted),
|
||||
"any" => Some(zclaw_hands::FileEvent::Any),
|
||||
_ => None,
|
||||
}).collect(),
|
||||
}
|
||||
}
|
||||
TriggerTypeRequest::Manual => zclaw_hands::TriggerType::Manual,
|
||||
};
|
||||
|
||||
let config = zclaw_hands::TriggerConfig {
|
||||
id: request.id,
|
||||
name: request.name,
|
||||
hand_id: request.hand_id,
|
||||
trigger_type,
|
||||
enabled: request.enabled,
|
||||
max_executions_per_hour: 10,
|
||||
};
|
||||
|
||||
let entry = kernel.create_trigger(config).await
|
||||
.map_err(|e| format!("Failed to create trigger: {}", e))?;
|
||||
|
||||
Ok(TriggerResponse::from(entry))
|
||||
}
|
||||
|
||||
/// Update a trigger
|
||||
#[tauri::command]
|
||||
pub async fn trigger_update(
|
||||
state: State<'_, KernelState>,
|
||||
id: String,
|
||||
name: Option<String>,
|
||||
enabled: Option<bool>,
|
||||
hand_id: Option<String>,
|
||||
) -> Result<TriggerResponse, String> {
|
||||
let kernel_lock = state.lock().await;
|
||||
let kernel = kernel_lock.as_ref()
|
||||
.ok_or_else(|| "Kernel not initialized".to_string())?;
|
||||
|
||||
let update = zclaw_kernel::trigger_manager::TriggerUpdateRequest {
|
||||
name,
|
||||
enabled,
|
||||
hand_id,
|
||||
trigger_type: None,
|
||||
};
|
||||
|
||||
let entry = kernel.update_trigger(&id, update).await
|
||||
.map_err(|e| format!("Failed to update trigger: {}", e))?;
|
||||
|
||||
Ok(TriggerResponse::from(entry))
|
||||
}
|
||||
|
||||
/// Delete a trigger
|
||||
#[tauri::command]
|
||||
pub async fn trigger_delete(
|
||||
state: State<'_, KernelState>,
|
||||
id: String,
|
||||
) -> Result<(), String> {
|
||||
// Validate trigger ID
|
||||
let id = validate_id(&id, "trigger_id")?;
|
||||
|
||||
let kernel_lock = state.lock().await;
|
||||
let kernel = kernel_lock.as_ref()
|
||||
.ok_or_else(|| "Kernel not initialized".to_string())?;
|
||||
|
||||
kernel.delete_trigger(&id).await
|
||||
.map_err(|e| format!("Failed to delete trigger: {}", e))
|
||||
}
|
||||
|
||||
/// Execute a trigger manually
|
||||
#[tauri::command]
|
||||
pub async fn trigger_execute(
|
||||
state: State<'_, KernelState>,
|
||||
id: String,
|
||||
input: serde_json::Value,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
// Validate trigger ID
|
||||
let id = validate_id(&id, "trigger_id")?;
|
||||
|
||||
let kernel_lock = state.lock().await;
|
||||
let kernel = kernel_lock.as_ref()
|
||||
.ok_or_else(|| "Kernel not initialized".to_string())?;
|
||||
|
||||
let result = kernel.execute_trigger(&id, input).await
|
||||
.map_err(|e| format!("Failed to execute trigger: {}", e))?;
|
||||
|
||||
Ok(serde_json::to_value(result).unwrap_or(serde_json::json!({})))
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// Approval Commands
|
||||
// ============================================================
|
||||
|
||||
/// Approval response
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ApprovalResponse {
|
||||
pub id: String,
|
||||
pub hand_id: String,
|
||||
pub status: String,
|
||||
pub created_at: String,
|
||||
pub input: serde_json::Value,
|
||||
}
|
||||
|
||||
/// List pending approvals
|
||||
#[tauri::command]
|
||||
pub async fn approval_list(
|
||||
state: State<'_, KernelState>,
|
||||
) -> Result<Vec<ApprovalResponse>, String> {
|
||||
let kernel_lock = state.lock().await;
|
||||
let kernel = kernel_lock.as_ref()
|
||||
.ok_or_else(|| "Kernel not initialized".to_string())?;
|
||||
|
||||
let approvals = kernel.list_approvals().await;
|
||||
Ok(approvals.into_iter().map(|a| ApprovalResponse {
|
||||
id: a.id,
|
||||
hand_id: a.hand_id,
|
||||
status: a.status,
|
||||
created_at: a.created_at.to_rfc3339(),
|
||||
input: a.input,
|
||||
}).collect())
|
||||
}
|
||||
|
||||
/// Respond to an approval
|
||||
#[tauri::command]
|
||||
pub async fn approval_respond(
|
||||
state: State<'_, KernelState>,
|
||||
id: String,
|
||||
approved: bool,
|
||||
reason: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let kernel_lock = state.lock().await;
|
||||
let kernel = kernel_lock.as_ref()
|
||||
.ok_or_else(|| "Kernel not initialized".to_string())?;
|
||||
|
||||
kernel.respond_to_approval(&id, approved, reason).await
|
||||
.map_err(|e| format!("Failed to respond to approval: {}", e))
|
||||
}
|
||||
|
||||
@@ -965,6 +965,7 @@ fn openfang_version(app: AppHandle) -> Result<VersionResponse, String> {
|
||||
/// Health status enum
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
#[allow(dead_code)] // Reserved for future health check expansion
|
||||
enum HealthStatus {
|
||||
Healthy,
|
||||
Unhealthy,
|
||||
@@ -1313,6 +1314,7 @@ pub fn run() {
|
||||
let heartbeat_state: intelligence::HeartbeatEngineState = std::sync::Arc::new(tokio::sync::Mutex::new(std::collections::HashMap::new()));
|
||||
let reflection_state: intelligence::ReflectionEngineState = std::sync::Arc::new(tokio::sync::Mutex::new(intelligence::ReflectionEngine::new(None)));
|
||||
let identity_state: intelligence::IdentityManagerState = std::sync::Arc::new(tokio::sync::Mutex::new(intelligence::AgentIdentityManager::new()));
|
||||
let persona_evolver_state: intelligence::PersonaEvolverStateHandle = std::sync::Arc::new(tokio::sync::Mutex::new(intelligence::PersonaEvolver::new(None)));
|
||||
|
||||
// Initialize internal ZCLAW Kernel state
|
||||
let kernel_state = kernel_commands::create_kernel_state();
|
||||
@@ -1327,6 +1329,7 @@ pub fn run() {
|
||||
.manage(heartbeat_state)
|
||||
.manage(reflection_state)
|
||||
.manage(identity_state)
|
||||
.manage(persona_evolver_state)
|
||||
.manage(kernel_state)
|
||||
.manage(pipeline_state)
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
@@ -1436,6 +1439,16 @@ pub fn run() {
|
||||
memory_commands::memory_get,
|
||||
memory_commands::memory_search,
|
||||
memory_commands::memory_delete,
|
||||
// Trigger management commands
|
||||
kernel_commands::trigger_list,
|
||||
kernel_commands::trigger_get,
|
||||
kernel_commands::trigger_create,
|
||||
kernel_commands::trigger_update,
|
||||
kernel_commands::trigger_delete,
|
||||
kernel_commands::trigger_execute,
|
||||
// Approval management commands
|
||||
kernel_commands::approval_list,
|
||||
kernel_commands::approval_respond,
|
||||
memory_commands::memory_delete_all,
|
||||
memory_commands::memory_stats,
|
||||
memory_commands::memory_export,
|
||||
@@ -1479,7 +1492,24 @@ pub fn run() {
|
||||
intelligence::identity::identity_get_snapshots,
|
||||
intelligence::identity::identity_restore_snapshot,
|
||||
intelligence::identity::identity_list_agents,
|
||||
intelligence::identity::identity_delete_agent
|
||||
intelligence::identity::identity_delete_agent,
|
||||
// Adaptive Intelligence Mesh (Phase 4)
|
||||
intelligence::mesh::mesh_init,
|
||||
intelligence::mesh::mesh_analyze,
|
||||
intelligence::mesh::mesh_record_activity,
|
||||
intelligence::mesh::mesh_get_patterns,
|
||||
intelligence::mesh::mesh_update_config,
|
||||
intelligence::mesh::mesh_decay_patterns,
|
||||
intelligence::mesh::mesh_accept_recommendation,
|
||||
intelligence::mesh::mesh_dismiss_recommendation,
|
||||
// Persona Evolver (Phase 4)
|
||||
intelligence::persona_evolver::persona_evolver_init,
|
||||
intelligence::persona_evolver::persona_evolve,
|
||||
intelligence::persona_evolver::persona_evolution_history,
|
||||
intelligence::persona_evolver::persona_evolver_state,
|
||||
intelligence::persona_evolver::persona_evolver_config,
|
||||
intelligence::persona_evolver::persona_evolver_update_config,
|
||||
intelligence::persona_evolver::persona_apply_proposal
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
//! Memory Encryption Module
|
||||
//!
|
||||
//! Provides AES-256-GCM encryption for sensitive memory content.
|
||||
//!
|
||||
//! NOTE: Some constants and types are defined for future use.
|
||||
|
||||
#![allow(dead_code)] // Crypto utilities reserved for future encryption features
|
||||
|
||||
use aes_gcm::{
|
||||
aead::{Aead, KeyInit, OsRng},
|
||||
|
||||
@@ -59,6 +59,7 @@ impl<'r> sqlx::FromRow<'r, SqliteRow> for PersistentMemory {
|
||||
pub struct MemorySearchQuery {
|
||||
pub agent_id: Option<String>,
|
||||
pub memory_type: Option<String>,
|
||||
#[allow(dead_code)] // Reserved for future tag-based filtering
|
||||
pub tags: Option<Vec<String>>,
|
||||
pub query: Option<String>,
|
||||
pub min_importance: Option<i32>,
|
||||
|
||||
@@ -7,11 +7,11 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tauri::{AppHandle, Emitter, State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::sync::{Mutex, RwLock};
|
||||
use tokio::sync::RwLock;
|
||||
use serde_json::Value;
|
||||
|
||||
use zclaw_pipeline::{
|
||||
Pipeline, PipelineRun, PipelineProgress, RunStatus,
|
||||
Pipeline, RunStatus,
|
||||
parse_pipeline_yaml,
|
||||
PipelineExecutor,
|
||||
ActionRegistry,
|
||||
@@ -146,7 +146,7 @@ pub async fn pipeline_list(
|
||||
|
||||
// Update state
|
||||
let mut state_pipelines = state.pipelines.write().await;
|
||||
let mut state_paths = state.pipeline_paths.write().await;
|
||||
let state_paths = state.pipeline_paths.write().await;
|
||||
|
||||
for info in &pipelines {
|
||||
if let Some(path) = state_paths.get(&info.id) {
|
||||
|
||||
@@ -21,7 +21,6 @@ import {
|
||||
Grid,
|
||||
Volume2,
|
||||
VolumeX,
|
||||
Settings,
|
||||
Download,
|
||||
Share2,
|
||||
} from 'lucide-react';
|
||||
@@ -78,7 +77,7 @@ interface SceneRendererProps {
|
||||
showNarration: boolean;
|
||||
}
|
||||
|
||||
function SceneRenderer({ scene, isPlaying, showNarration }: SceneRendererProps) {
|
||||
function SceneRenderer({ scene, showNarration }: SceneRendererProps) {
|
||||
const renderContent = () => {
|
||||
switch (scene.type) {
|
||||
case 'title':
|
||||
@@ -240,7 +239,7 @@ function OutlinePanel({
|
||||
{section.title}
|
||||
</p>
|
||||
<div className="space-y-1">
|
||||
{section.scenes.map((sceneId, sceneIndex) => {
|
||||
{section.scenes.map((sceneId) => {
|
||||
const globalIndex = scenes.findIndex(s => s.id === sceneId);
|
||||
const isActive = globalIndex === currentIndex;
|
||||
const scene = scenes.find(s => s.id === sceneId);
|
||||
@@ -271,7 +270,6 @@ function OutlinePanel({
|
||||
|
||||
export function ClassroomPreviewer({
|
||||
data,
|
||||
onClose,
|
||||
onExport,
|
||||
}: ClassroomPreviewerProps) {
|
||||
const [currentSceneIndex, setCurrentSceneIndex] = useState(0);
|
||||
@@ -281,7 +279,7 @@ export function ClassroomPreviewer({
|
||||
const [isFullscreen, setIsFullscreen] = useState(false);
|
||||
const [viewMode, setViewMode] = useState<'slides' | 'grid'>('slides');
|
||||
|
||||
const { showToast } = useToast();
|
||||
const { toast } = useToast();
|
||||
const currentScene = data.scenes[currentSceneIndex];
|
||||
const totalScenes = data.scenes.length;
|
||||
|
||||
@@ -310,12 +308,12 @@ export function ClassroomPreviewer({
|
||||
nextScene();
|
||||
} else {
|
||||
setIsPlaying(false);
|
||||
showToast('课堂播放完成', 'success');
|
||||
toast('课堂播放完成', 'success');
|
||||
}
|
||||
}, duration);
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, [isPlaying, currentSceneIndex, currentScene, totalScenes, nextScene, showToast]);
|
||||
}, [isPlaying, currentSceneIndex, currentScene, totalScenes, nextScene, toast]);
|
||||
|
||||
// Keyboard navigation
|
||||
useEffect(() => {
|
||||
@@ -352,7 +350,7 @@ export function ClassroomPreviewer({
|
||||
if (onExport) {
|
||||
onExport(format);
|
||||
} else {
|
||||
showToast(`导出 ${format.toUpperCase()} 功能开发中...`, 'info');
|
||||
toast(`导出 ${format.toUpperCase()} 功能开发中...`, 'info');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ interface PipelineResultPreviewProps {
|
||||
onClose?: () => void;
|
||||
}
|
||||
|
||||
type PreviewMode = 'auto' | 'json' | 'markdown' | 'classroom';
|
||||
type PreviewMode = 'auto' | 'json' | 'markdown' | 'classroom' | 'files';
|
||||
|
||||
// === Utility Functions ===
|
||||
|
||||
@@ -123,14 +123,14 @@ interface JsonPreviewProps {
|
||||
|
||||
function JsonPreview({ data }: JsonPreviewProps) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const { showToast } = useToast();
|
||||
const { toast } = useToast();
|
||||
|
||||
const jsonString = JSON.stringify(data, null, 2);
|
||||
|
||||
const handleCopy = async () => {
|
||||
await navigator.clipboard.writeText(jsonString);
|
||||
setCopied(true);
|
||||
showToast('已复制到剪贴板', 'success');
|
||||
toast('已复制到剪贴板', 'success');
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
};
|
||||
|
||||
@@ -190,7 +190,6 @@ export function PipelineResultPreview({
|
||||
onClose,
|
||||
}: PipelineResultPreviewProps) {
|
||||
const [mode, setMode] = useState<PreviewMode>('auto');
|
||||
const { showToast } = useToast();
|
||||
|
||||
// Determine the best preview mode
|
||||
const outputs = result.outputs as Record<string, unknown> | undefined;
|
||||
|
||||
@@ -7,16 +7,13 @@
|
||||
* Pipelines orchestrate Skills and Hands to accomplish complex tasks.
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useState } from 'react';
|
||||
import {
|
||||
Play,
|
||||
RefreshCw,
|
||||
Search,
|
||||
ChevronRight,
|
||||
Loader2,
|
||||
CheckCircle,
|
||||
XCircle,
|
||||
Clock,
|
||||
Package,
|
||||
Filter,
|
||||
X,
|
||||
@@ -26,7 +23,6 @@ import {
|
||||
PipelineInfo,
|
||||
PipelineRunResponse,
|
||||
usePipelines,
|
||||
usePipelineRun,
|
||||
validateInputs,
|
||||
getDefaultForType,
|
||||
formatInputType,
|
||||
@@ -378,7 +374,7 @@ export function PipelinesPanel() {
|
||||
const [selectedCategory, setSelectedCategory] = useState<string | null>(null);
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const [selectedPipeline, setSelectedPipeline] = useState<PipelineInfo | null>(null);
|
||||
const { showToast } = useToast();
|
||||
const { toast } = useToast();
|
||||
|
||||
const { pipelines, loading, error, refresh } = usePipelines({
|
||||
category: selectedCategory ?? undefined,
|
||||
@@ -406,9 +402,9 @@ export function PipelinesPanel() {
|
||||
const handleRunComplete = (result: PipelineRunResponse) => {
|
||||
setSelectedPipeline(null);
|
||||
if (result.status === 'completed') {
|
||||
showToast('Pipeline 执行完成', 'success');
|
||||
toast('Pipeline 执行完成', 'success');
|
||||
} else {
|
||||
showToast(`Pipeline 执行失败: ${result.error}`, 'error');
|
||||
toast(`Pipeline 执行失败: ${result.error}`, 'error');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,21 +1,208 @@
|
||||
import { useEffect } from 'react';
|
||||
import { Radio, RefreshCw, MessageCircle, Settings2 } from 'lucide-react';
|
||||
/**
|
||||
* IMChannels - IM Channel Management UI
|
||||
*
|
||||
* Displays and manages IM channel configurations.
|
||||
* Supports viewing, configuring, and adding new channels.
|
||||
*/
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Radio, RefreshCw, MessageCircle, Settings2, Plus, X, Check, AlertCircle, ExternalLink } from 'lucide-react';
|
||||
import { useConnectionStore } from '../../store/connectionStore';
|
||||
import { useConfigStore } from '../../store/configStore';
|
||||
import { useConfigStore, type ChannelInfo } from '../../store/configStore';
|
||||
import { useAgentStore } from '../../store/agentStore';
|
||||
|
||||
const CHANNEL_ICONS: Record<string, string> = {
|
||||
feishu: '飞',
|
||||
qqbot: 'QQ',
|
||||
wechat: '微',
|
||||
discord: 'D',
|
||||
slack: 'S',
|
||||
telegram: 'T',
|
||||
};
|
||||
|
||||
const CHANNEL_CONFIG_FIELDS: Record<string, { key: string; label: string; type: string; placeholder: string; required: boolean }[]> = {
|
||||
feishu: [
|
||||
{ key: 'appId', label: 'App ID', type: 'text', placeholder: 'cli_xxx', required: true },
|
||||
{ key: 'appSecret', label: 'App Secret', type: 'password', placeholder: '••••••••', required: true },
|
||||
],
|
||||
discord: [
|
||||
{ key: 'botToken', label: 'Bot Token', type: 'password', placeholder: 'OTk2NzY4...', required: true },
|
||||
{ key: 'guildId', label: 'Guild ID (可选)', type: 'text', placeholder: '123456789', required: false },
|
||||
],
|
||||
slack: [
|
||||
{ key: 'botToken', label: 'Bot Token', type: 'password', placeholder: 'xoxb-...', required: true },
|
||||
{ key: 'appToken', label: 'App Token', type: 'password', placeholder: 'xapp-...', required: false },
|
||||
],
|
||||
telegram: [
|
||||
{ key: 'botToken', label: 'Bot Token', type: 'password', placeholder: '123456:ABC...', required: true },
|
||||
],
|
||||
qqbot: [
|
||||
{ key: 'appId', label: 'App ID', type: 'text', placeholder: '1234567890', required: true },
|
||||
{ key: 'token', label: 'Token', type: 'password', placeholder: '••••••••', required: true },
|
||||
],
|
||||
wechat: [
|
||||
{ key: 'corpId', label: 'Corp ID', type: 'text', placeholder: 'wwxxx', required: true },
|
||||
{ key: 'agentId', label: 'Agent ID', type: 'text', placeholder: '1000001', required: true },
|
||||
{ key: 'secret', label: 'Secret', type: 'password', placeholder: '••••••••', required: true },
|
||||
],
|
||||
};
|
||||
|
||||
const KNOWN_CHANNELS = [
|
||||
{ type: 'feishu', label: '飞书 (Feishu/Lark)', description: '企业即时通讯平台' },
|
||||
{ type: 'discord', label: 'Discord', description: '游戏社区和语音聊天' },
|
||||
{ type: 'slack', label: 'Slack', description: '团队协作平台' },
|
||||
{ type: 'telegram', label: 'Telegram', description: '加密即时通讯' },
|
||||
{ type: 'qqbot', label: 'QQ 机器人', description: '腾讯QQ官方机器人' },
|
||||
{ type: 'wechat', label: '企业微信', description: '企业微信机器人' },
|
||||
];
|
||||
|
||||
interface ChannelConfigModalProps {
|
||||
channel: ChannelInfo | null;
|
||||
channelType: string | null;
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
onSave: (config: Record<string, string>) => Promise<void>;
|
||||
isSaving: boolean;
|
||||
}
|
||||
|
||||
function ChannelConfigModal({ channel, channelType, isOpen, onClose, onSave, isSaving }: ChannelConfigModalProps) {
|
||||
const [config, setConfig] = useState<Record<string, string>>({});
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fields = channelType ? CHANNEL_CONFIG_FIELDS[channelType] || [] : [];
|
||||
|
||||
useEffect(() => {
|
||||
if (channel?.config) {
|
||||
setConfig(channel.config as Record<string, string>);
|
||||
} else {
|
||||
setConfig({});
|
||||
}
|
||||
setError(null);
|
||||
}, [channel, channelType]);
|
||||
|
||||
if (!isOpen || !channelType) return null;
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
|
||||
// Validate required fields
|
||||
for (const field of fields) {
|
||||
if (field.required && !config[field.key]?.trim()) {
|
||||
setError(`请填写 ${field.label}`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await onSave(config);
|
||||
onClose();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : '保存失败');
|
||||
}
|
||||
};
|
||||
|
||||
const channelInfo = KNOWN_CHANNELS.find(c => c.type === channelType);
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
|
||||
<div className="bg-white dark:bg-gray-800 rounded-xl shadow-xl w-full max-w-md mx-4">
|
||||
<div className="flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700">
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white">
|
||||
{channel ? `配置 ${channel.label}` : `添加 ${channelInfo?.label || channelType}`}
|
||||
</h3>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded"
|
||||
>
|
||||
<X className="w-5 h-5 text-gray-500" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit} className="p-4 space-y-4">
|
||||
{channelInfo && (
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
{channelInfo.description}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{fields.length === 0 ? (
|
||||
<div className="text-center py-8 text-gray-500 dark:text-gray-400">
|
||||
<AlertCircle className="w-8 h-8 mx-auto mb-2 opacity-50" />
|
||||
<p>该通道类型暂不支持通过 UI 配置</p>
|
||||
<p className="text-xs mt-1">请通过配置文件或 CLI 进行配置</p>
|
||||
</div>
|
||||
) : (
|
||||
fields.map((field) => (
|
||||
<div key={field.key}>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
|
||||
{field.label}
|
||||
{field.required && <span className="text-red-500 ml-1">*</span>}
|
||||
</label>
|
||||
<input
|
||||
type={field.type}
|
||||
value={config[field.key] || ''}
|
||||
onChange={(e) => setConfig({ ...config, [field.key]: e.target.value })}
|
||||
placeholder={field.placeholder}
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
/>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div className="p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg text-sm text-red-600 dark:text-red-400">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{fields.length > 0 && (
|
||||
<div className="flex gap-3 pt-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isSaving}
|
||||
className="flex-1 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 flex items-center justify-center gap-2"
|
||||
>
|
||||
{isSaving ? (
|
||||
<>
|
||||
<RefreshCw className="w-4 h-4 animate-spin" />
|
||||
保存中...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Check className="w-4 h-4" />
|
||||
保存
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function IMChannels() {
|
||||
const channels = useConfigStore((s) => s.channels);
|
||||
const loadChannels = useConfigStore((s) => s.loadChannels);
|
||||
const createChannel = useConfigStore((s) => s.createChannel);
|
||||
const updateChannel = useConfigStore((s) => s.updateChannel);
|
||||
const connectionState = useConnectionStore((s) => s.connectionState);
|
||||
const loadPluginStatus = useAgentStore((s) => s.loadPluginStatus);
|
||||
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
const [selectedChannel, setSelectedChannel] = useState<ChannelInfo | null>(null);
|
||||
const [newChannelType, setNewChannelType] = useState<string | null>(null);
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
const [showAddMenu, setShowAddMenu] = useState(false);
|
||||
|
||||
const connected = connectionState === 'connected';
|
||||
const loading = connectionState === 'connecting' || connectionState === 'reconnecting' || connectionState === 'handshaking';
|
||||
|
||||
@@ -29,20 +216,47 @@ export function IMChannels() {
|
||||
loadPluginStatus().then(() => loadChannels());
|
||||
};
|
||||
|
||||
const knownChannels = [
|
||||
{ id: 'feishu', type: 'feishu', label: '飞书 (Feishu)' },
|
||||
{ id: 'qqbot', type: 'qqbot', label: 'QQ 机器人' },
|
||||
{ id: 'wechat', type: 'wechat', label: '微信' },
|
||||
];
|
||||
const handleConfigure = (channel: ChannelInfo) => {
|
||||
setSelectedChannel(channel);
|
||||
setNewChannelType(channel.type);
|
||||
setIsModalOpen(true);
|
||||
};
|
||||
|
||||
const availableChannels = knownChannels.filter(
|
||||
const handleAddChannel = (type: string) => {
|
||||
setSelectedChannel(null);
|
||||
setNewChannelType(type);
|
||||
setIsModalOpen(true);
|
||||
setShowAddMenu(false);
|
||||
};
|
||||
|
||||
const handleSaveConfig = async (config: Record<string, string>) => {
|
||||
setIsSaving(true);
|
||||
try {
|
||||
if (selectedChannel) {
|
||||
await updateChannel(selectedChannel.id, { config });
|
||||
} else if (newChannelType) {
|
||||
const channelInfo = KNOWN_CHANNELS.find(c => c.type === newChannelType);
|
||||
await createChannel({
|
||||
type: newChannelType,
|
||||
name: channelInfo?.label || newChannelType,
|
||||
config,
|
||||
enabled: true,
|
||||
});
|
||||
}
|
||||
await loadChannels();
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const availableChannels = KNOWN_CHANNELS.filter(
|
||||
(channel) => !channels.some((item) => item.type === channel.type)
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="max-w-3xl">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<h1 className="text-xl font-bold text-gray-900">IM 频道</h1>
|
||||
<h1 className="text-xl font-bold text-gray-900 dark:text-white">IM 频道</h1>
|
||||
<div className="flex gap-2">
|
||||
<span className="text-xs text-gray-400 flex items-center">
|
||||
{connected ? `${channels.length} 个已识别频道` : loading ? '连接中...' : '未连接 Gateway'}
|
||||
@@ -58,12 +272,12 @@ export function IMChannels() {
|
||||
</div>
|
||||
|
||||
{!connected ? (
|
||||
<div className="bg-white rounded-xl border border-gray-200 h-64 flex flex-col items-center justify-center mb-6 shadow-sm text-gray-400">
|
||||
<div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 h-64 flex flex-col items-center justify-center mb-6 shadow-sm text-gray-400">
|
||||
<Radio className="w-8 h-8 mb-3 opacity-40" />
|
||||
<span className="text-sm">连接 Gateway 后查看真实 IM 频道状态</span>
|
||||
</div>
|
||||
) : (
|
||||
<div className="bg-white rounded-xl border border-gray-200 mb-6 shadow-sm divide-y divide-gray-100">
|
||||
<div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 mb-6 shadow-sm divide-y divide-gray-100 dark:divide-gray-700">
|
||||
{channels.length > 0 ? channels.map((channel) => (
|
||||
<div key={channel.id} className="p-4 flex items-center gap-4">
|
||||
<div className={`w-10 h-10 rounded-xl flex items-center justify-center text-white text-sm font-semibold ${
|
||||
@@ -71,24 +285,30 @@ export function IMChannels() {
|
||||
? 'bg-gradient-to-br from-blue-500 to-indigo-500'
|
||||
: channel.status === 'error'
|
||||
? 'bg-gradient-to-br from-red-500 to-rose-500'
|
||||
: 'bg-gray-300'
|
||||
: 'bg-gray-300 dark:bg-gray-600'
|
||||
}`}>
|
||||
{CHANNEL_ICONS[channel.type] || <MessageCircle className="w-4 h-4" />}
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-sm font-medium text-gray-900">{channel.label}</div>
|
||||
<div className="text-sm font-medium text-gray-900 dark:text-white">{channel.label}</div>
|
||||
<div className={`text-xs mt-1 ${
|
||||
channel.status === 'active'
|
||||
? 'text-green-600'
|
||||
? 'text-green-600 dark:text-green-400'
|
||||
: channel.status === 'error'
|
||||
? 'text-red-500'
|
||||
? 'text-red-500 dark:text-red-400'
|
||||
: 'text-gray-400'
|
||||
}`}>
|
||||
{channel.status === 'active' ? '已连接' : channel.status === 'error' ? channel.error || '错误' : '未配置'}
|
||||
{channel.accounts !== undefined && channel.accounts > 0 ? ` · ${channel.accounts} 个账号` : ''}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-xs text-gray-400">{channel.type}</div>
|
||||
<button
|
||||
onClick={() => handleConfigure(channel)}
|
||||
className="p-2 text-gray-400 hover:text-blue-600 dark:hover:text-blue-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition-colors"
|
||||
title="配置"
|
||||
>
|
||||
<Settings2 className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
)) : (
|
||||
<div className="h-40 flex items-center justify-center text-sm text-gray-400">
|
||||
@@ -98,23 +318,88 @@ export function IMChannels() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Add Channel Section */}
|
||||
{connected && availableChannels.length > 0 && (
|
||||
<div className="mb-6">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400">添加新频道</div>
|
||||
<div className="relative">
|
||||
<button
|
||||
onClick={() => setShowAddMenu(!showAddMenu)}
|
||||
className="text-xs text-white bg-blue-500 hover:bg-blue-600 px-3 py-1.5 rounded-lg flex items-center gap-1 transition-colors"
|
||||
>
|
||||
<Plus className="w-3 h-3" /> 添加频道
|
||||
</button>
|
||||
{showAddMenu && (
|
||||
<div className="absolute right-0 mt-1 w-48 bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 z-10">
|
||||
{availableChannels.map((channel) => (
|
||||
<button
|
||||
key={channel.type}
|
||||
onClick={() => handleAddChannel(channel.type)}
|
||||
className="w-full px-4 py-2 text-left text-sm hover:bg-gray-100 dark:hover:bg-gray-700 first:rounded-t-lg last:rounded-b-lg flex items-center gap-2"
|
||||
>
|
||||
<span className="w-6 h-6 rounded bg-gray-100 dark:bg-gray-700 flex items-center justify-center text-xs">
|
||||
{CHANNEL_ICONS[channel.type] || '?'}
|
||||
</span>
|
||||
<div>
|
||||
<div className="text-xs text-gray-500 mb-3">规划中的接入渠道</div>
|
||||
<div className="font-medium text-gray-900 dark:text-white">{channel.label}</div>
|
||||
<div className="text-xs text-gray-500">{channel.description}</div>
|
||||
</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Planned Channels */}
|
||||
<div>
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400 mb-3">规划中的接入渠道</div>
|
||||
<div className="flex flex-wrap gap-3">
|
||||
{availableChannels.map((channel) => (
|
||||
<span
|
||||
key={channel.id}
|
||||
className="text-xs text-gray-500 bg-gray-100 px-4 py-2 rounded-lg"
|
||||
key={channel.type}
|
||||
className="text-xs text-gray-500 dark:text-gray-400 bg-gray-100 dark:bg-gray-700 px-4 py-2 rounded-lg"
|
||||
>
|
||||
{channel.label}
|
||||
</span>
|
||||
))}
|
||||
<div className="text-xs text-gray-400 flex items-center gap-1">
|
||||
<Settings2 className="w-3 h-3" />
|
||||
当前页面仅展示已识别到的真实频道状态;channel、account、binding 的创建与配置仍需通过 Gateway 或插件侧完成。
|
||||
{availableChannels.length === 0 && (
|
||||
<div className="text-xs text-green-600 dark:text-green-400 flex items-center gap-1">
|
||||
<Check className="w-3 h-3" />
|
||||
所有支持的渠道已配置
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* External Link Notice */}
|
||||
<div className="mt-6 p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg border border-blue-200 dark:border-blue-800">
|
||||
<div className="flex items-start gap-2">
|
||||
<ExternalLink className="w-4 h-4 text-blue-500 mt-0.5" />
|
||||
<div className="text-xs text-blue-700 dark:text-blue-300">
|
||||
<p className="font-medium mb-1">高级配置</p>
|
||||
<p>账号绑定、消息路由等高级功能需要在 Gateway 配置文件中完成。</p>
|
||||
<p className="mt-1">配置文件路径: <code className="bg-blue-100 dark:bg-blue-800 px-1 rounded">~/.openfang/openfang.toml</code></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Config Modal */}
|
||||
<ChannelConfigModal
|
||||
channel={selectedChannel}
|
||||
channelType={newChannelType}
|
||||
isOpen={isModalOpen}
|
||||
onClose={() => {
|
||||
setIsModalOpen(false);
|
||||
setSelectedChannel(null);
|
||||
setNewChannelType(null);
|
||||
}}
|
||||
onSave={handleSaveConfig}
|
||||
isSaving={isSaving}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
382
desktop/src/components/Settings/SecureStorage.tsx
Normal file
382
desktop/src/components/Settings/SecureStorage.tsx
Normal file
@@ -0,0 +1,382 @@
|
||||
/**
|
||||
* SecureStorage - OS Keyring/Keychain Management UI
|
||||
*
|
||||
* Allows users to view, add, and delete securely stored credentials
|
||||
* using the OS keyring (Windows DPAPI, macOS Keychain, Linux Secret Service).
|
||||
*/
|
||||
import { useState, useEffect } from 'react';
|
||||
import {
|
||||
Key,
|
||||
Plus,
|
||||
Trash2,
|
||||
Eye,
|
||||
EyeOff,
|
||||
RefreshCw,
|
||||
AlertCircle,
|
||||
CheckCircle,
|
||||
Shield,
|
||||
ShieldOff,
|
||||
} from 'lucide-react';
|
||||
import { secureStorage, isSecureStorageAvailable } from '../../lib/secure-storage';
|
||||
|
||||
interface StoredKey {
|
||||
key: string;
|
||||
hasValue: boolean;
|
||||
preview?: string;
|
||||
}
|
||||
|
||||
// Known storage keys used by the application
|
||||
const KNOWN_KEYS = [
|
||||
{ key: 'zclaw_api_key', label: 'API Key', description: 'LLM API 密钥' },
|
||||
{ key: 'zclaw_device_keys_private', label: 'Device Private Key', description: '设备私钥 (Ed25519)' },
|
||||
{ key: 'zclaw_gateway_token', label: 'Gateway Token', description: 'Gateway 认证令牌' },
|
||||
{ key: 'zclaw_feishu_secret', label: '飞书 Secret', description: '飞书应用密钥' },
|
||||
{ key: 'zclaw_discord_token', label: 'Discord Token', description: 'Discord Bot Token' },
|
||||
{ key: 'zclaw_slack_token', label: 'Slack Token', description: 'Slack Bot Token' },
|
||||
{ key: 'zclaw_telegram_token', label: 'Telegram Token', description: 'Telegram Bot Token' },
|
||||
];
|
||||
|
||||
export function SecureStorage() {
|
||||
const [isAvailable, setIsAvailable] = useState<boolean | null>(null);
|
||||
const [storedKeys, setStoredKeys] = useState<StoredKey[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [showAddForm, setShowAddForm] = useState(false);
|
||||
const [newKey, setNewKey] = useState('');
|
||||
const [newValue, setNewValue] = useState('');
|
||||
const [showValue, setShowValue] = useState<Record<string, boolean>>({});
|
||||
const [revealedValues, setRevealedValues] = useState<Record<string, string>>({});
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
const [isDeleting, setIsDeleting] = useState<string | null>(null);
|
||||
const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
|
||||
|
||||
const loadStoredKeys = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const available = await isSecureStorageAvailable();
|
||||
setIsAvailable(available);
|
||||
|
||||
const keys: StoredKey[] = [];
|
||||
for (const knownKey of KNOWN_KEYS) {
|
||||
const value = await secureStorage.get(knownKey.key);
|
||||
keys.push({
|
||||
key: knownKey.key,
|
||||
hasValue: !!value,
|
||||
preview: value ? `${value.slice(0, 8)}${value.length > 8 ? '...' : ''}` : undefined,
|
||||
});
|
||||
}
|
||||
setStoredKeys(keys);
|
||||
} catch (error) {
|
||||
console.error('Failed to load stored keys:', error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadStoredKeys();
|
||||
}, []);
|
||||
|
||||
const handleReveal = async (key: string) => {
|
||||
if (revealedValues[key]) {
|
||||
// Hide if already revealed
|
||||
setRevealedValues((prev) => {
|
||||
const next = { ...prev };
|
||||
delete next[key];
|
||||
return next;
|
||||
});
|
||||
setShowValue((prev) => ({ ...prev, [key]: false }));
|
||||
} else {
|
||||
// Reveal the value
|
||||
const value = await secureStorage.get(key);
|
||||
if (value) {
|
||||
setRevealedValues((prev) => ({ ...prev, [key]: value }));
|
||||
setShowValue((prev) => ({ ...prev, [key]: true }));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddKey = async () => {
|
||||
if (!newKey.trim() || !newValue.trim()) {
|
||||
setMessage({ type: 'error', text: '请填写密钥名称和值' });
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSaving(true);
|
||||
setMessage(null);
|
||||
try {
|
||||
await secureStorage.set(newKey.trim(), newValue.trim());
|
||||
setMessage({ type: 'success', text: '密钥已保存' });
|
||||
setNewKey('');
|
||||
setNewValue('');
|
||||
setShowAddForm(false);
|
||||
await loadStoredKeys();
|
||||
} catch (error) {
|
||||
setMessage({ type: 'error', text: `保存失败: ${error instanceof Error ? error.message : '未知错误'}` });
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteKey = async (key: string) => {
|
||||
if (!confirm(`确定要删除密钥 "${key}" 吗?此操作无法撤销。`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsDeleting(key);
|
||||
setMessage(null);
|
||||
try {
|
||||
await secureStorage.delete(key);
|
||||
setMessage({ type: 'success', text: '密钥已删除' });
|
||||
setRevealedValues((prev) => {
|
||||
const next = { ...prev };
|
||||
delete next[key];
|
||||
return next;
|
||||
});
|
||||
await loadStoredKeys();
|
||||
} catch (error) {
|
||||
setMessage({ type: 'error', text: `删除失败: ${error instanceof Error ? error.message : '未知错误'}` });
|
||||
} finally {
|
||||
setIsDeleting(null);
|
||||
}
|
||||
};
|
||||
|
||||
const getKeyLabel = (key: string) => {
|
||||
const known = KNOWN_KEYS.find((k) => k.key === key);
|
||||
return known ? known.label : key;
|
||||
};
|
||||
|
||||
const getKeyDescription = (key: string) => {
|
||||
const known = KNOWN_KEYS.find((k) => k.key === key);
|
||||
return known?.description;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="max-w-3xl">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-900 dark:text-white">安全存储</h1>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1">
|
||||
使用系统密钥库 (Keyring/Keychain) 安全存储敏感信息
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-2 items-center">
|
||||
{isAvailable !== null && (
|
||||
<span className={`text-xs flex items-center gap-1 ${isAvailable ? 'text-green-600' : 'text-amber-600'}`}>
|
||||
{isAvailable ? (
|
||||
<>
|
||||
<Shield className="w-3 h-3" /> Keyring 可用
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<ShieldOff className="w-3 h-3" /> 使用加密本地存储
|
||||
</>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
<button
|
||||
onClick={loadStoredKeys}
|
||||
disabled={isLoading}
|
||||
className="text-xs text-white bg-orange-500 hover:bg-orange-600 px-3 py-1.5 rounded-lg flex items-center gap-1 transition-colors disabled:opacity-50"
|
||||
>
|
||||
<RefreshCw className={`w-3 h-3 ${isLoading ? 'animate-spin' : ''}`} /> 刷新
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Status Banner */}
|
||||
{isAvailable === false && (
|
||||
<div className="mb-6 p-4 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800 rounded-lg">
|
||||
<div className="flex items-start gap-2">
|
||||
<AlertCircle className="w-4 h-4 text-amber-500 mt-0.5" />
|
||||
<div className="text-xs text-amber-700 dark:text-amber-300">
|
||||
<p className="font-medium">Keyring 不可用</p>
|
||||
<p className="mt-1">
|
||||
系统密钥库不可用,将使用 AES-GCM 加密的本地存储作为后备方案。
|
||||
建议在 Tauri 环境中运行以获得最佳安全性。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Message */}
|
||||
{message && (
|
||||
<div className={`mb-4 p-3 rounded-lg flex items-center gap-2 ${
|
||||
message.type === 'success'
|
||||
? 'bg-green-50 dark:bg-green-900/20 text-green-700 dark:text-green-300'
|
||||
: 'bg-red-50 dark:bg-red-900/20 text-red-700 dark:text-red-300'
|
||||
}`}>
|
||||
{message.type === 'success' ? (
|
||||
<CheckCircle className="w-4 h-4" />
|
||||
) : (
|
||||
<AlertCircle className="w-4 h-4" />
|
||||
)}
|
||||
{message.text}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Stored Keys List */}
|
||||
<div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 mb-6 shadow-sm">
|
||||
{isLoading ? (
|
||||
<div className="h-40 flex items-center justify-center text-sm text-gray-400">
|
||||
<RefreshCw className="w-4 h-4 animate-spin mr-2" />
|
||||
加载中...
|
||||
</div>
|
||||
) : storedKeys.length > 0 ? (
|
||||
<div className="divide-y divide-gray-100 dark:divide-gray-700">
|
||||
{storedKeys.map((item) => (
|
||||
<div key={item.key} className="p-4">
|
||||
<div className="flex items-center gap-4">
|
||||
<div className={`w-10 h-10 rounded-xl flex items-center justify-center ${
|
||||
item.hasValue
|
||||
? 'bg-gradient-to-br from-green-500 to-emerald-500 text-white'
|
||||
: 'bg-gray-200 dark:bg-gray-700 text-gray-400'
|
||||
}`}>
|
||||
<Key className="w-4 h-4" />
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
{getKeyLabel(item.key)}
|
||||
</div>
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
|
||||
{getKeyDescription(item.key) || item.key}
|
||||
</div>
|
||||
{item.hasValue && (
|
||||
<div className="text-xs text-gray-400 dark:text-gray-500 mt-1 font-mono">
|
||||
{showValue[item.key] ? (
|
||||
<span className="break-all">{revealedValues[item.key]}</span>
|
||||
) : (
|
||||
<span>{item.preview}</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{item.hasValue && (
|
||||
<>
|
||||
<button
|
||||
onClick={() => handleReveal(item.key)}
|
||||
className="p-2 text-gray-400 hover:text-blue-600 dark:hover:text-blue-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition-colors"
|
||||
title={showValue[item.key] ? '隐藏' : '显示'}
|
||||
>
|
||||
{showValue[item.key] ? (
|
||||
<EyeOff className="w-4 h-4" />
|
||||
) : (
|
||||
<Eye className="w-4 h-4" />
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleDeleteKey(item.key)}
|
||||
disabled={isDeleting === item.key}
|
||||
className="p-2 text-gray-400 hover:text-red-600 dark:hover:text-red-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition-colors disabled:opacity-50"
|
||||
title="删除"
|
||||
>
|
||||
{isDeleting === item.key ? (
|
||||
<RefreshCw className="w-4 h-4 animate-spin" />
|
||||
) : (
|
||||
<Trash2 className="w-4 h-4" />
|
||||
)}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{!item.hasValue && (
|
||||
<span className="text-xs text-gray-400 dark:text-gray-500 px-2">未设置</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="h-40 flex items-center justify-center text-sm text-gray-400">
|
||||
暂无存储的密钥
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Add New Key */}
|
||||
<div className="mb-6">
|
||||
{!showAddForm ? (
|
||||
<button
|
||||
onClick={() => setShowAddForm(true)}
|
||||
className="w-full p-4 border-2 border-dashed border-gray-300 dark:border-gray-600 rounded-xl text-gray-500 dark:text-gray-400 hover:border-orange-400 hover:text-orange-500 transition-colors flex items-center justify-center gap-2"
|
||||
>
|
||||
<Plus className="w-4 h-4" />
|
||||
<span className="text-sm">添加新密钥</span>
|
||||
</button>
|
||||
) : (
|
||||
<div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-4 shadow-sm">
|
||||
<h3 className="text-sm font-medium text-gray-900 dark:text-white mb-4">添加新密钥</h3>
|
||||
<div className="space-y-3">
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
||||
密钥名称
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={newKey}
|
||||
onChange={(e) => setNewKey(e.target.value)}
|
||||
placeholder="例如: zclaw_custom_key"
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-gray-700 dark:text-gray-300 mb-1">
|
||||
密钥值
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={newValue}
|
||||
onChange={(e) => setNewValue(e.target.value)}
|
||||
placeholder="输入密钥值"
|
||||
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex gap-2 pt-2">
|
||||
<button
|
||||
onClick={() => {
|
||||
setShowAddForm(false);
|
||||
setNewKey('');
|
||||
setNewValue('');
|
||||
setMessage(null);
|
||||
}}
|
||||
className="flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 text-sm"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
onClick={handleAddKey}
|
||||
disabled={isSaving || !newKey.trim() || !newValue.trim()}
|
||||
className="flex-1 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 flex items-center justify-center gap-2 text-sm"
|
||||
>
|
||||
{isSaving ? (
|
||||
<>
|
||||
<RefreshCw className="w-3 h-3 animate-spin" />
|
||||
保存中...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<CheckCircle className="w-3 h-3" />
|
||||
保存
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Info Section */}
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800/50 rounded-lg border border-gray-200 dark:border-gray-700">
|
||||
<h3 className="text-sm font-medium text-gray-900 dark:text-white mb-2">关于安全存储</h3>
|
||||
<ul className="text-xs text-gray-500 dark:text-gray-400 space-y-1">
|
||||
<li>• Windows: 使用 DPAPI 加密</li>
|
||||
<li>• macOS: 使用 Keychain 存储</li>
|
||||
<li>• Linux: 使用 Secret Service API (gnome-keyring, kwallet 等)</li>
|
||||
<li>• 后备方案: AES-GCM 加密的 localStorage</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -16,6 +16,8 @@ import {
|
||||
ClipboardList,
|
||||
Clock,
|
||||
Heart,
|
||||
Key,
|
||||
Database,
|
||||
} from 'lucide-react';
|
||||
import { silentErrorHandler } from '../../lib/error-utils';
|
||||
import { General } from './General';
|
||||
@@ -33,6 +35,8 @@ import { SecurityStatus } from '../SecurityStatus';
|
||||
import { SecurityLayersPanel } from '../SecurityLayersPanel';
|
||||
import { TaskList } from '../TaskList';
|
||||
import { HeartbeatConfig } from '../HeartbeatConfig';
|
||||
import { SecureStorage } from './SecureStorage';
|
||||
import { VikingPanel } from '../VikingPanel';
|
||||
|
||||
interface SettingsLayoutProps {
|
||||
onBack: () => void;
|
||||
@@ -49,6 +53,8 @@ type SettingsPage =
|
||||
| 'workspace'
|
||||
| 'privacy'
|
||||
| 'security'
|
||||
| 'storage'
|
||||
| 'viking'
|
||||
| 'audit'
|
||||
| 'tasks'
|
||||
| 'heartbeat'
|
||||
@@ -65,6 +71,8 @@ const menuItems: { id: SettingsPage; label: string; icon: React.ReactNode }[] =
|
||||
{ id: 'im', label: 'IM 频道', icon: <MessageSquare className="w-4 h-4" /> },
|
||||
{ id: 'workspace', label: '工作区', icon: <FolderOpen className="w-4 h-4" /> },
|
||||
{ id: 'privacy', label: '数据与隐私', icon: <Shield className="w-4 h-4" /> },
|
||||
{ id: 'storage', label: '安全存储', icon: <Key className="w-4 h-4" /> },
|
||||
{ id: 'viking', label: '语义记忆', icon: <Database className="w-4 h-4" /> },
|
||||
{ id: 'security', label: '安全状态', icon: <Shield className="w-4 h-4" /> },
|
||||
{ id: 'audit', label: '审计日志', icon: <ClipboardList className="w-4 h-4" /> },
|
||||
{ id: 'tasks', label: '定时任务', icon: <Clock className="w-4 h-4" /> },
|
||||
@@ -88,6 +96,7 @@ export function SettingsLayout({ onBack }: SettingsLayoutProps) {
|
||||
case 'im': return <IMChannels />;
|
||||
case 'workspace': return <Workspace />;
|
||||
case 'privacy': return <Privacy />;
|
||||
case 'storage': return <SecureStorage />;
|
||||
case 'security': return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
@@ -121,6 +130,7 @@ export function SettingsLayout({ onBack }: SettingsLayoutProps) {
|
||||
<HeartbeatConfig />
|
||||
</div>
|
||||
);
|
||||
case 'viking': return <VikingPanel />;
|
||||
case 'feedback': return <Feedback />;
|
||||
case 'about': return <About />;
|
||||
default: return <General />;
|
||||
|
||||
288
desktop/src/components/VikingPanel.tsx
Normal file
288
desktop/src/components/VikingPanel.tsx
Normal file
@@ -0,0 +1,288 @@
|
||||
/**
|
||||
* VikingPanel - OpenViking Semantic Memory UI
|
||||
*
|
||||
* Provides interface for semantic search and knowledge base management.
|
||||
* OpenViking is an optional sidecar for semantic memory operations.
|
||||
*/
|
||||
import { useState, useEffect } from 'react';
|
||||
import {
|
||||
Search,
|
||||
RefreshCw,
|
||||
AlertCircle,
|
||||
CheckCircle,
|
||||
FileText,
|
||||
Server,
|
||||
Play,
|
||||
Square,
|
||||
} from 'lucide-react';
|
||||
import {
|
||||
getVikingStatus,
|
||||
findVikingResources,
|
||||
getVikingServerStatus,
|
||||
startVikingServer,
|
||||
stopVikingServer,
|
||||
} from '../lib/viking-client';
|
||||
import type { VikingStatus, VikingFindResult } from '../lib/viking-client';
|
||||
|
||||
export function VikingPanel() {
|
||||
const [status, setStatus] = useState<VikingStatus | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const [searchResults, setSearchResults] = useState<VikingFindResult[]>([]);
|
||||
const [isSearching, setIsSearching] = useState(false);
|
||||
const [serverRunning, setServerRunning] = useState(false);
|
||||
const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null);
|
||||
|
||||
const loadStatus = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const vikingStatus = await getVikingStatus();
|
||||
setStatus(vikingStatus);
|
||||
|
||||
const serverStatus = await getVikingServerStatus();
|
||||
setServerRunning(serverStatus.running);
|
||||
} catch (error) {
|
||||
console.error('Failed to load Viking status:', error);
|
||||
setStatus({ available: false, error: String(error) });
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadStatus();
|
||||
}, []);
|
||||
|
||||
const handleSearch = async () => {
|
||||
if (!searchQuery.trim()) return;
|
||||
|
||||
setIsSearching(true);
|
||||
setMessage(null);
|
||||
try {
|
||||
const results = await findVikingResources(searchQuery, undefined, 10);
|
||||
setSearchResults(results);
|
||||
if (results.length === 0) {
|
||||
setMessage({ type: 'error', text: '未找到匹配的资源' });
|
||||
}
|
||||
} catch (error) {
|
||||
setMessage({
|
||||
type: 'error',
|
||||
text: `搜索失败: ${error instanceof Error ? error.message : '未知错误'}`,
|
||||
});
|
||||
} finally {
|
||||
setIsSearching(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleServerToggle = async () => {
|
||||
try {
|
||||
if (serverRunning) {
|
||||
await stopVikingServer();
|
||||
setServerRunning(false);
|
||||
setMessage({ type: 'success', text: '服务器已停止' });
|
||||
} else {
|
||||
await startVikingServer();
|
||||
setServerRunning(true);
|
||||
setMessage({ type: 'success', text: '服务器已启动' });
|
||||
}
|
||||
} catch (error) {
|
||||
setMessage({
|
||||
type: 'error',
|
||||
text: `操作失败: ${error instanceof Error ? error.message : '未知错误'}`,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="max-w-4xl">
|
||||
{/* Header */}
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-900 dark:text-white">语义记忆</h1>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1">
|
||||
OpenViking 语义搜索引擎
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-2 items-center">
|
||||
{status?.available && (
|
||||
<span className="text-xs flex items-center gap-1 text-green-600">
|
||||
<CheckCircle className="w-3 h-3" /> 可用
|
||||
</span>
|
||||
)}
|
||||
<button
|
||||
onClick={loadStatus}
|
||||
disabled={isLoading}
|
||||
className="text-xs text-white bg-orange-500 hover:bg-orange-600 px-3 py-1.5 rounded-lg flex items-center gap-1 transition-colors disabled:opacity-50"
|
||||
>
|
||||
<RefreshCw className={`w-3 h-3 ${isLoading ? 'animate-spin' : ''}`} /> 刷新
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Status Banner */}
|
||||
{!status?.available && (
|
||||
<div className="mb-6 p-4 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800 rounded-lg">
|
||||
<div className="flex items-start gap-2">
|
||||
<AlertCircle className="w-4 h-4 text-amber-500 mt-0.5" />
|
||||
<div className="text-xs text-amber-700 dark:text-amber-300">
|
||||
<p className="font-medium">OpenViking CLI 不可用</p>
|
||||
<p className="mt-1">
|
||||
请安装 OpenViking CLI 或设置{' '}
|
||||
<code className="bg-amber-100 dark:bg-amber-800 px-1 rounded">ZCLAW_VIKING_BIN</code> 环境变量。
|
||||
</p>
|
||||
{status?.error && (
|
||||
<p className="mt-1 text-amber-600 dark:text-amber-400 font-mono text-xs">
|
||||
{status.error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Message */}
|
||||
{message && (
|
||||
<div
|
||||
className={`mb-4 p-3 rounded-lg flex items-center gap-2 ${
|
||||
message.type === 'success'
|
||||
? 'bg-green-50 dark:bg-green-900/20 text-green-700 dark:text-green-300'
|
||||
: 'bg-red-50 dark:bg-red-900/20 text-red-700 dark:text-red-300'
|
||||
}`}
|
||||
>
|
||||
{message.type === 'success' ? (
|
||||
<CheckCircle className="w-4 h-4" />
|
||||
) : (
|
||||
<AlertCircle className="w-4 h-4" />
|
||||
)}
|
||||
{message.text}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Server Control */}
|
||||
{status?.available && (
|
||||
<div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-4 mb-6 shadow-sm">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-3">
|
||||
<div
|
||||
className={`w-10 h-10 rounded-xl flex items-center justify-center ${
|
||||
serverRunning
|
||||
? 'bg-gradient-to-br from-green-500 to-emerald-500 text-white'
|
||||
: 'bg-gray-200 dark:bg-gray-700 text-gray-400'
|
||||
}`}
|
||||
>
|
||||
<Server className="w-4 h-4" />
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
Viking Server
|
||||
</div>
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400">
|
||||
{serverRunning ? '运行中' : '已停止'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleServerToggle}
|
||||
className={`px-4 py-2 rounded-lg flex items-center gap-2 text-sm transition-colors ${
|
||||
serverRunning
|
||||
? 'bg-red-100 text-red-600 hover:bg-red-200 dark:bg-red-900/30 dark:text-red-400'
|
||||
: 'bg-green-100 text-green-600 hover:bg-green-200 dark:bg-green-900/30 dark:text-green-400'
|
||||
}`}
|
||||
>
|
||||
{serverRunning ? (
|
||||
<>
|
||||
<Square className="w-4 h-4" /> 停止
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Play className="w-4 h-4" /> 启动
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Search Box */}
|
||||
{status?.available && (
|
||||
<div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-4 mb-6 shadow-sm">
|
||||
<h3 className="text-sm font-medium text-gray-900 dark:text-white mb-3">语义搜索</h3>
|
||||
<div className="flex gap-2">
|
||||
<input
|
||||
type="text"
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
onKeyDown={(e) => e.key === 'Enter' && handleSearch()}
|
||||
placeholder="输入自然语言查询..."
|
||||
className="flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-white text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
/>
|
||||
<button
|
||||
onClick={handleSearch}
|
||||
disabled={isSearching || !searchQuery.trim()}
|
||||
className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 flex items-center gap-2 text-sm"
|
||||
>
|
||||
{isSearching ? (
|
||||
<RefreshCw className="w-4 h-4 animate-spin" />
|
||||
) : (
|
||||
<Search className="w-4 h-4" />
|
||||
)}
|
||||
搜索
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Search Results */}
|
||||
{searchResults.length > 0 && (
|
||||
<div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 shadow-sm divide-y divide-gray-100 dark:divide-gray-700">
|
||||
<div className="p-3 border-b border-gray-200 dark:border-gray-700">
|
||||
<span className="text-xs text-gray-500">
|
||||
找到 {searchResults.length} 个结果
|
||||
</span>
|
||||
</div>
|
||||
{searchResults.map((result, index) => (
|
||||
<div key={`${result.uri}-${index}`} className="p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="w-8 h-8 rounded-lg bg-blue-100 dark:bg-blue-900/30 flex items-center justify-center flex-shrink-0">
|
||||
<FileText className="w-4 h-4 text-blue-600 dark:text-blue-400" />
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-white truncate">
|
||||
{result.uri}
|
||||
</span>
|
||||
<span className="text-xs text-gray-400 bg-gray-100 dark:bg-gray-700 px-2 py-0.5 rounded">
|
||||
{result.level}
|
||||
</span>
|
||||
<span className="text-xs text-blue-600 dark:text-blue-400">
|
||||
{Math.round(result.score * 100)}%
|
||||
</span>
|
||||
</div>
|
||||
{result.overview && (
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-1 line-clamp-2">
|
||||
{result.overview}
|
||||
</p>
|
||||
)}
|
||||
<p className="text-xs text-gray-600 dark:text-gray-300 mt-2 line-clamp-3 font-mono">
|
||||
{result.content}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Info Section */}
|
||||
<div className="mt-6 p-4 bg-gray-50 dark:bg-gray-800/50 rounded-lg border border-gray-200 dark:border-gray-700">
|
||||
<h3 className="text-sm font-medium text-gray-900 dark:text-white mb-2">关于 OpenViking</h3>
|
||||
<ul className="text-xs text-gray-500 dark:text-gray-400 space-y-1">
|
||||
<li>• 语义搜索引擎,支持自然语言查询</li>
|
||||
<li>• 自动提取和索引知识资源</li>
|
||||
<li>• 支持多种文档格式和代码文件</li>
|
||||
<li>• 可作为本地知识库增强 AI 对话</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
* Draggable palette of available node types.
|
||||
*/
|
||||
|
||||
import React, { DragEvent } from 'react';
|
||||
import { DragEvent } from 'react';
|
||||
import type { NodePaletteItem, NodeCategory } from '../../lib/workflow-builder/types';
|
||||
|
||||
interface NodePaletteProps {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Panel for editing node properties.
|
||||
*/
|
||||
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { useState, useEffect } from 'react';
|
||||
import type { WorkflowNodeData } from '../../lib/workflow-builder/types';
|
||||
|
||||
interface PropertyPanelProps {
|
||||
@@ -16,7 +16,6 @@ interface PropertyPanelProps {
|
||||
}
|
||||
|
||||
export function PropertyPanel({
|
||||
nodeId,
|
||||
nodeData,
|
||||
onUpdate,
|
||||
onDelete,
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* Pipeline DSL configurations.
|
||||
*/
|
||||
|
||||
import React, { useCallback, useRef, useEffect } from 'react';
|
||||
import { useCallback, useRef, useEffect } from 'react';
|
||||
import {
|
||||
ReactFlow,
|
||||
Controls,
|
||||
@@ -17,17 +17,17 @@ import {
|
||||
useNodesState,
|
||||
useEdgesState,
|
||||
Node,
|
||||
NodeChange,
|
||||
EdgeChange,
|
||||
Edge,
|
||||
NodeTypes,
|
||||
Panel,
|
||||
ReactFlowProvider,
|
||||
useReactFlow,
|
||||
} from '@xyflow/react';
|
||||
import '@xyflow/react/dist/style.css';
|
||||
|
||||
import { useWorkflowBuilderStore, nodePaletteItems, paletteCategories } from '../../store/workflowBuilderStore';
|
||||
import type { WorkflowNodeType, WorkflowNodeData } from '../../lib/workflow-builder/types';
|
||||
import { validateCanvas } from '../../lib/workflow-builder/yaml-converter';
|
||||
import { useWorkflowBuilderStore, paletteCategories } from '../../store/workflowBuilderStore';
|
||||
import type { WorkflowNodeData, WorkflowNodeType } from '../../lib/workflow-builder/types';
|
||||
|
||||
// Import custom node components
|
||||
import { InputNode } from './nodes/InputNode';
|
||||
@@ -66,7 +66,7 @@ const nodeTypes: NodeTypes = {
|
||||
|
||||
export function WorkflowBuilderInternal() {
|
||||
const reactFlowWrapper = useRef<HTMLDivElement>(null);
|
||||
const { screenToFlowPosition, fitView } = useReactFlow();
|
||||
const { screenToFlowPosition } = useReactFlow();
|
||||
|
||||
const {
|
||||
canvas,
|
||||
@@ -84,8 +84,8 @@ export function WorkflowBuilderInternal() {
|
||||
} = useWorkflowBuilderStore();
|
||||
|
||||
// Local state for React Flow
|
||||
const [nodes, setNodes, onNodesChange] = useNodesState([]);
|
||||
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
|
||||
const [nodes, setNodes, onNodesChange] = useNodesState<Node<WorkflowNodeData>>([]);
|
||||
const [edges, setEdges, onEdgesChange] = useEdgesState<Edge>([]);
|
||||
|
||||
// Sync canvas state with React Flow
|
||||
useEffect(() => {
|
||||
@@ -94,7 +94,7 @@ export function WorkflowBuilderInternal() {
|
||||
id: n.id,
|
||||
type: n.type,
|
||||
position: n.position,
|
||||
data: n.data,
|
||||
data: n.data as WorkflowNodeData,
|
||||
})));
|
||||
setEdges(canvas.edges.map(e => ({
|
||||
id: e.id,
|
||||
@@ -111,7 +111,7 @@ export function WorkflowBuilderInternal() {
|
||||
|
||||
// Handle node changes (position, selection)
|
||||
const handleNodesChange = useCallback(
|
||||
(changes) => {
|
||||
(changes: NodeChange<Node<WorkflowNodeData>>[]) => {
|
||||
onNodesChange(changes);
|
||||
|
||||
// Sync position changes back to store
|
||||
@@ -132,7 +132,7 @@ export function WorkflowBuilderInternal() {
|
||||
|
||||
// Handle edge changes
|
||||
const handleEdgesChange = useCallback(
|
||||
(changes) => {
|
||||
(changes: EdgeChange[]) => {
|
||||
onEdgesChange(changes);
|
||||
},
|
||||
[onEdgesChange]
|
||||
@@ -235,7 +235,7 @@ export function WorkflowBuilderInternal() {
|
||||
{/* Node Palette */}
|
||||
<NodePalette
|
||||
categories={paletteCategories}
|
||||
onDragStart={(type) => {
|
||||
onDragStart={() => {
|
||||
setDragging(true);
|
||||
}}
|
||||
onDragEnd={() => {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Toolbar with actions for the workflow builder.
|
||||
*/
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import { useState } from 'react';
|
||||
import type { ValidationResult } from '../../lib/workflow-builder/types';
|
||||
import { canvasToYaml } from '../../lib/workflow-builder/yaml-converter';
|
||||
import { useWorkflowBuilderStore } from '../../store/workflowBuilderStore';
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
* Node for conditional branching.
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { ConditionNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
export const ConditionNode = memo(({ data, selected }: NodeProps<ConditionNodeData>) => {
|
||||
type ConditionNodeType = Node<ConditionNodeData>;
|
||||
|
||||
export const ConditionNode = memo(({ data, selected }: NodeProps<ConditionNodeType>) => {
|
||||
const branchCount = data.branches.length + (data.hasDefault ? 1 : 0);
|
||||
|
||||
return (
|
||||
@@ -39,7 +41,7 @@ export const ConditionNode = memo(({ data, selected }: NodeProps<ConditionNodeDa
|
||||
|
||||
{/* Branches */}
|
||||
<div className="space-y-1">
|
||||
{data.branches.map((branch, index) => (
|
||||
{data.branches.map((branch: { label?: string; when: string }, index: number) => (
|
||||
<div key={index} className="flex items-center justify-between">
|
||||
<div className="relative">
|
||||
{/* Branch Output Handle */}
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* Node for exporting workflow results to various formats.
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { ExportNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
export const ExportNode = memo(({ data, selected }: NodeProps<ExportNodeData>) => {
|
||||
export const ExportNode = memo(({ data, selected }: NodeProps<Node<ExportNodeData>>) => {
|
||||
const formatLabels: Record<string, string> = {
|
||||
pptx: 'PowerPoint',
|
||||
html: 'HTML',
|
||||
|
||||
@@ -4,11 +4,13 @@
|
||||
* Node for executing hand actions.
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { HandNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
export const HandNode = memo(({ data, selected }: NodeProps<HandNodeData>) => {
|
||||
type HandNodeType = Node<HandNodeData>;
|
||||
|
||||
export const HandNode = memo(({ data, selected }: NodeProps<HandNodeType>) => {
|
||||
const hasHand = Boolean(data.handId);
|
||||
const hasAction = Boolean(data.action);
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
* Node for making HTTP requests.
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { HttpNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
const methodColors: Record<string, string> = {
|
||||
@@ -16,7 +16,7 @@ const methodColors: Record<string, string> = {
|
||||
PATCH: 'bg-purple-100 text-purple-700',
|
||||
};
|
||||
|
||||
export const HttpNode = memo(({ data, selected }: NodeProps<HttpNodeData>) => {
|
||||
export const HttpNode = memo(({ data, selected }: NodeProps<Node<HttpNodeData>>) => {
|
||||
const hasUrl = Boolean(data.url);
|
||||
|
||||
return (
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* Node for defining workflow input variables.
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { InputNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
export const InputNode = memo(({ data, selected }: NodeProps<InputNodeData>) => {
|
||||
export const InputNode = memo(({ data, selected }: NodeProps<Node<InputNodeData>>) => {
|
||||
return (
|
||||
<div
|
||||
className={`
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* Node for LLM generation actions.
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { LlmNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
export const LlmNode = memo(({ data, selected }: NodeProps<LlmNodeData>) => {
|
||||
export const LlmNode = memo(({ data, selected }: NodeProps<Node<LlmNodeData>>) => {
|
||||
const templatePreview = data.template.length > 50
|
||||
? data.template.slice(0, 50) + '...'
|
||||
: data.template || 'No template';
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* Node for executing skill orchestration graphs (DAGs).
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { OrchestrationNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
export const OrchestrationNode = memo(({ data, selected }: NodeProps<OrchestrationNodeData>) => {
|
||||
export const OrchestrationNode = memo(({ data, selected }: NodeProps<Node<OrchestrationNodeData>>) => {
|
||||
const hasGraphId = Boolean(data.graphId);
|
||||
const hasGraph = Boolean(data.graph);
|
||||
const inputCount = Object.keys(data.inputMappings).length;
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* Node for parallel execution of steps.
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { ParallelNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
export const ParallelNode = memo(({ data, selected }: NodeProps<ParallelNodeData>) => {
|
||||
export const ParallelNode = memo(({ data, selected }: NodeProps<Node<ParallelNodeData>>) => {
|
||||
return (
|
||||
<div
|
||||
className={`
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* Node for executing skills.
|
||||
*/
|
||||
|
||||
import React, { memo } from 'react';
|
||||
import { Handle, Position, NodeProps } from '@xyflow/react';
|
||||
import { memo } from 'react';
|
||||
import { Handle, Position, NodeProps, Node } from '@xyflow/react';
|
||||
import type { SkillNodeData } from '../../../lib/workflow-builder/types';
|
||||
|
||||
export const SkillNode = memo(({ data, selected }: NodeProps<SkillNodeData>) => {
|
||||
export const SkillNode = memo(({ data, selected }: NodeProps<Node<SkillNodeData>>) => {
|
||||
const hasSkill = Boolean(data.skillId);
|
||||
|
||||
return (
|
||||
|
||||
340
desktop/src/components/WorkflowRecommendations.tsx
Normal file
340
desktop/src/components/WorkflowRecommendations.tsx
Normal file
@@ -0,0 +1,340 @@
|
||||
/**
|
||||
* Workflow Recommendations Component
|
||||
*
|
||||
* Displays proactive workflow recommendations from the Adaptive Intelligence Mesh.
|
||||
* Shows detected patterns and suggested workflows based on user behavior.
|
||||
*/
|
||||
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { motion, AnimatePresence } from 'framer-motion';
|
||||
import { useMeshStore } from '../store/meshStore';
|
||||
import type { WorkflowRecommendation, BehaviorPattern, PatternTypeVariant } from '../lib/intelligence-client';
|
||||
|
||||
// === Main Component ===
|
||||
|
||||
export const WorkflowRecommendations: React.FC = () => {
|
||||
const {
|
||||
recommendations,
|
||||
patterns,
|
||||
isLoading,
|
||||
error,
|
||||
analyze,
|
||||
acceptRecommendation,
|
||||
dismissRecommendation,
|
||||
} = useMeshStore();
|
||||
|
||||
const [selectedPattern, setSelectedPattern] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
// Initial analysis
|
||||
analyze();
|
||||
}, [analyze]);
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center p-8">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
<span className="ml-3 text-gray-400">Analyzing patterns...</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="p-4 bg-red-500/10 border border-red-500/20 rounded-lg">
|
||||
<p className="text-red-400 text-sm">{error}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Recommendations Section */}
|
||||
<section>
|
||||
<h3 className="text-lg font-semibold text-white mb-4 flex items-center gap-2">
|
||||
<span className="text-2xl">💡</span>
|
||||
Recommended Workflows
|
||||
{recommendations.length > 0 && (
|
||||
<span className="ml-2 px-2 py-0.5 bg-blue-500/20 text-blue-400 text-xs rounded-full">
|
||||
{recommendations.length}
|
||||
</span>
|
||||
)}
|
||||
</h3>
|
||||
|
||||
<AnimatePresence mode="popLayout">
|
||||
{recommendations.length === 0 ? (
|
||||
<motion.div
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
className="p-6 bg-gray-800/30 rounded-lg border border-gray-700/50 text-center"
|
||||
>
|
||||
<p className="text-gray-400">No recommendations available yet.</p>
|
||||
<p className="text-gray-500 text-sm mt-2">
|
||||
Continue using the app to build up behavior patterns.
|
||||
</p>
|
||||
</motion.div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{recommendations.map((rec) => (
|
||||
<RecommendationCard
|
||||
key={rec.id}
|
||||
recommendation={rec}
|
||||
onAccept={() => acceptRecommendation(rec.id)}
|
||||
onDismiss={() => dismissRecommendation(rec.id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
</section>
|
||||
|
||||
{/* Detected Patterns Section */}
|
||||
<section>
|
||||
<h3 className="text-lg font-semibold text-white mb-4 flex items-center gap-2">
|
||||
<span className="text-2xl">📊</span>
|
||||
Detected Patterns
|
||||
{patterns.length > 0 && (
|
||||
<span className="ml-2 px-2 py-0.5 bg-purple-500/20 text-purple-400 text-xs rounded-full">
|
||||
{patterns.length}
|
||||
</span>
|
||||
)}
|
||||
</h3>
|
||||
|
||||
{patterns.length === 0 ? (
|
||||
<div className="p-6 bg-gray-800/30 rounded-lg border border-gray-700/50 text-center">
|
||||
<p className="text-gray-400">No patterns detected yet.</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid gap-3">
|
||||
{patterns.map((pattern) => (
|
||||
<PatternCard
|
||||
key={pattern.id}
|
||||
pattern={pattern}
|
||||
isSelected={selectedPattern === pattern.id}
|
||||
onClick={() =>
|
||||
setSelectedPattern(
|
||||
selectedPattern === pattern.id ? null : pattern.id
|
||||
)
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// === Sub-Components ===
|
||||
|
||||
interface RecommendationCardProps {
|
||||
recommendation: WorkflowRecommendation;
|
||||
onAccept: () => void;
|
||||
onDismiss: () => void;
|
||||
}
|
||||
|
||||
const RecommendationCard: React.FC<RecommendationCardProps> = ({
|
||||
recommendation,
|
||||
onAccept,
|
||||
onDismiss,
|
||||
}) => {
|
||||
const confidencePercent = Math.round(recommendation.confidence * 100);
|
||||
|
||||
const getConfidenceColor = (confidence: number) => {
|
||||
if (confidence >= 0.8) return 'text-green-400';
|
||||
if (confidence >= 0.6) return 'text-yellow-400';
|
||||
return 'text-orange-400';
|
||||
};
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
layout
|
||||
initial={{ opacity: 0, y: -10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
exit={{ opacity: 0, scale: 0.95 }}
|
||||
className="p-4 bg-gray-800/50 rounded-lg border border-gray-700/50 hover:border-blue-500/30 transition-colors"
|
||||
>
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 mb-2">
|
||||
<h4 className="text-white font-medium truncate">
|
||||
{recommendation.pipeline_id}
|
||||
</h4>
|
||||
<span
|
||||
className={`text-xs font-mono ${getConfidenceColor(
|
||||
recommendation.confidence
|
||||
)}`}
|
||||
>
|
||||
{confidencePercent}%
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<p className="text-gray-400 text-sm mb-3">{recommendation.reason}</p>
|
||||
|
||||
{/* Suggested Inputs */}
|
||||
{Object.keys(recommendation.suggested_inputs).length > 0 && (
|
||||
<div className="mb-3">
|
||||
<p className="text-xs text-gray-500 mb-1">Suggested inputs:</p>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{Object.entries(recommendation.suggested_inputs).map(
|
||||
([key, value]) => (
|
||||
<span
|
||||
key={key}
|
||||
className="px-2 py-0.5 bg-gray-700/50 text-gray-300 text-xs rounded"
|
||||
>
|
||||
{key}: {String(value).slice(0, 20)}
|
||||
</span>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Matched Patterns */}
|
||||
{recommendation.patterns_matched.length > 0 && (
|
||||
<div className="text-xs text-gray-500">
|
||||
Based on {recommendation.patterns_matched.length} pattern(s)
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex gap-2 shrink-0">
|
||||
<button
|
||||
onClick={onAccept}
|
||||
className="px-3 py-1.5 bg-blue-500 hover:bg-blue-600 text-white text-sm rounded transition-colors"
|
||||
>
|
||||
Accept
|
||||
</button>
|
||||
<button
|
||||
onClick={onDismiss}
|
||||
className="px-3 py-1.5 bg-gray-700 hover:bg-gray-600 text-gray-300 text-sm rounded transition-colors"
|
||||
>
|
||||
Dismiss
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Confidence Bar */}
|
||||
<div className="mt-3 h-1 bg-gray-700 rounded-full overflow-hidden">
|
||||
<motion.div
|
||||
initial={{ width: 0 }}
|
||||
animate={{ width: `${confidencePercent}%` }}
|
||||
className={`h-full ${
|
||||
recommendation.confidence >= 0.8
|
||||
? 'bg-green-500'
|
||||
: recommendation.confidence >= 0.6
|
||||
? 'bg-yellow-500'
|
||||
: 'bg-orange-500'
|
||||
}`}
|
||||
/>
|
||||
</div>
|
||||
</motion.div>
|
||||
);
|
||||
};
|
||||
|
||||
interface PatternCardProps {
|
||||
pattern: BehaviorPattern;
|
||||
isSelected: boolean;
|
||||
onClick: () => void;
|
||||
}
|
||||
|
||||
const PatternCard: React.FC<PatternCardProps> = ({
|
||||
pattern,
|
||||
isSelected,
|
||||
onClick,
|
||||
}) => {
|
||||
const getPatternTypeLabel = (type: PatternTypeVariant | string) => {
|
||||
// Handle object format
|
||||
const typeStr = typeof type === 'string' ? type : type.type;
|
||||
|
||||
switch (typeStr) {
|
||||
case 'SkillCombination':
|
||||
return { label: 'Skill Combo', icon: '⚡' };
|
||||
case 'TemporalTrigger':
|
||||
return { label: 'Time Trigger', icon: '⏰' };
|
||||
case 'TaskPipelineMapping':
|
||||
return { label: 'Task Mapping', icon: '🔄' };
|
||||
case 'InputPattern':
|
||||
return { label: 'Input Pattern', icon: '📝' };
|
||||
default:
|
||||
return { label: typeStr, icon: '📊' };
|
||||
}
|
||||
};
|
||||
|
||||
const { label, icon } = getPatternTypeLabel(pattern.pattern_type as PatternTypeVariant);
|
||||
const confidencePercent = Math.round(pattern.confidence * 100);
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
layout
|
||||
onClick={onClick}
|
||||
className={`p-3 rounded-lg border cursor-pointer transition-colors ${
|
||||
isSelected
|
||||
? 'bg-purple-500/10 border-purple-500/50'
|
||||
: 'bg-gray-800/30 border-gray-700/50 hover:border-gray-600'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-lg">{icon}</span>
|
||||
<span className="text-white font-medium">{label}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-xs text-gray-400">
|
||||
{pattern.frequency}x used
|
||||
</span>
|
||||
<span
|
||||
className={`text-xs font-mono ${
|
||||
pattern.confidence >= 0.6
|
||||
? 'text-green-400'
|
||||
: 'text-yellow-400'
|
||||
}`}
|
||||
>
|
||||
{confidencePercent}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<AnimatePresence>
|
||||
{isSelected && (
|
||||
<motion.div
|
||||
initial={{ height: 0, opacity: 0 }}
|
||||
animate={{ height: 'auto', opacity: 1 }}
|
||||
exit={{ height: 0, opacity: 0 }}
|
||||
className="mt-3 pt-3 border-t border-gray-700/50 overflow-hidden"
|
||||
>
|
||||
<div className="space-y-2 text-sm">
|
||||
<div>
|
||||
<span className="text-gray-500">ID:</span>{' '}
|
||||
<span className="text-gray-300 font-mono text-xs">
|
||||
{pattern.id}
|
||||
</span>
|
||||
</div>
|
||||
<div>
|
||||
<span className="text-gray-500">First seen:</span>{' '}
|
||||
<span className="text-gray-300">
|
||||
{new Date(pattern.first_occurrence).toLocaleDateString()}
|
||||
</span>
|
||||
</div>
|
||||
<div>
|
||||
<span className="text-gray-500">Last seen:</span>{' '}
|
||||
<span className="text-gray-300">
|
||||
{new Date(pattern.last_occurrence).toLocaleDateString()}
|
||||
</span>
|
||||
</div>
|
||||
{pattern.context.intent && (
|
||||
<div>
|
||||
<span className="text-gray-500">Intent:</span>{' '}
|
||||
<span className="text-gray-300">{pattern.context.intent}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
</motion.div>
|
||||
);
|
||||
};
|
||||
|
||||
export default WorkflowRecommendations;
|
||||
@@ -128,8 +128,142 @@ export type {
|
||||
IdentityFiles,
|
||||
IdentityChangeProposal,
|
||||
IdentitySnapshot,
|
||||
MemoryEntryForAnalysis,
|
||||
} from './intelligence-backend';
|
||||
|
||||
// === Mesh Types ===
|
||||
|
||||
export interface BehaviorPattern {
|
||||
id: string;
|
||||
pattern_type: PatternTypeVariant;
|
||||
frequency: number;
|
||||
last_occurrence: string;
|
||||
first_occurrence: string;
|
||||
confidence: number;
|
||||
context: PatternContext;
|
||||
}
|
||||
|
||||
export function getPatternTypeString(patternType: PatternTypeVariant): string {
|
||||
if (typeof patternType === 'string') {
|
||||
return patternType;
|
||||
}
|
||||
return patternType.type;
|
||||
}
|
||||
|
||||
export type PatternTypeVariant =
|
||||
| { type: 'SkillCombination'; skill_ids: string[] }
|
||||
| { type: 'TemporalTrigger'; hand_id: string; time_pattern: string }
|
||||
| { type: 'TaskPipelineMapping'; task_type: string; pipeline_id: string }
|
||||
| { type: 'InputPattern'; keywords: string[]; intent: string };
|
||||
|
||||
export interface PatternContext {
|
||||
skill_ids?: string[];
|
||||
recent_topics?: string[];
|
||||
intent?: string;
|
||||
time_of_day?: number;
|
||||
day_of_week?: number;
|
||||
}
|
||||
|
||||
export interface WorkflowRecommendation {
|
||||
id: string;
|
||||
pipeline_id: string;
|
||||
confidence: number;
|
||||
reason: string;
|
||||
suggested_inputs: Record<string, unknown>;
|
||||
patterns_matched: string[];
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export interface MeshConfig {
|
||||
enabled: boolean;
|
||||
min_confidence: number;
|
||||
max_recommendations: number;
|
||||
analysis_window_hours: number;
|
||||
}
|
||||
|
||||
export interface MeshAnalysisResult {
|
||||
recommendations: WorkflowRecommendation[];
|
||||
patterns_detected: number;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export type ActivityType =
|
||||
| { type: 'skill_used'; skill_ids: string[] }
|
||||
| { type: 'pipeline_executed'; task_type: string; pipeline_id: string }
|
||||
| { type: 'input_received'; keywords: string[]; intent: string };
|
||||
|
||||
// === Persona Evolver Types ===
|
||||
|
||||
export type EvolutionChangeType =
|
||||
| 'instruction_addition'
|
||||
| 'instruction_refinement'
|
||||
| 'trait_addition'
|
||||
| 'style_adjustment'
|
||||
| 'domain_expansion';
|
||||
|
||||
export type InsightCategory =
|
||||
| 'communication_style'
|
||||
| 'technical_expertise'
|
||||
| 'task_efficiency'
|
||||
| 'user_preference'
|
||||
| 'knowledge_gap';
|
||||
|
||||
export type IdentityFileType = 'soul' | 'instructions';
|
||||
export type ProposalStatus = 'pending' | 'approved' | 'rejected';
|
||||
|
||||
export interface EvolutionProposal {
|
||||
id: string;
|
||||
agent_id: string;
|
||||
target_file: IdentityFileType;
|
||||
change_type: EvolutionChangeType;
|
||||
reason: string;
|
||||
current_content: string;
|
||||
proposed_content: string;
|
||||
confidence: number;
|
||||
evidence: string[];
|
||||
status: ProposalStatus;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface ProfileUpdate {
|
||||
section: string;
|
||||
previous: string;
|
||||
updated: string;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export interface EvolutionInsight {
|
||||
category: InsightCategory;
|
||||
observation: string;
|
||||
recommendation: string;
|
||||
confidence: number;
|
||||
}
|
||||
|
||||
export interface EvolutionResult {
|
||||
agent_id: string;
|
||||
timestamp: string;
|
||||
profile_updates: ProfileUpdate[];
|
||||
proposals: EvolutionProposal[];
|
||||
insights: EvolutionInsight[];
|
||||
evolved: boolean;
|
||||
}
|
||||
|
||||
export interface PersonaEvolverConfig {
|
||||
auto_profile_update: boolean;
|
||||
min_preferences_for_update: number;
|
||||
min_conversations_for_evolution: number;
|
||||
enable_instruction_refinement: boolean;
|
||||
enable_soul_evolution: boolean;
|
||||
max_proposals_per_cycle: number;
|
||||
}
|
||||
|
||||
export interface PersonaEvolverState {
|
||||
last_evolution: string | null;
|
||||
total_evolutions: number;
|
||||
pending_proposals: number;
|
||||
profile_enrichment_score: number;
|
||||
}
|
||||
|
||||
// === Type Conversion Utilities ===
|
||||
|
||||
/**
|
||||
|
||||
@@ -753,36 +753,210 @@ export class KernelClient {
|
||||
});
|
||||
}
|
||||
|
||||
// === Triggers API (stubs for compatibility) ===
|
||||
// === Triggers API ===
|
||||
|
||||
async listTriggers(): Promise<{ triggers?: { id: string; type: string; enabled: boolean }[] }> {
|
||||
/**
|
||||
* List all triggers
|
||||
* Returns empty array on error for graceful degradation
|
||||
*/
|
||||
async listTriggers(): Promise<{
|
||||
triggers?: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: string;
|
||||
enabled: boolean;
|
||||
createdAt: string;
|
||||
modifiedAt: string;
|
||||
description?: string;
|
||||
tags: string[];
|
||||
}>
|
||||
}> {
|
||||
try {
|
||||
const triggers = await invoke<Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: string;
|
||||
enabled: boolean;
|
||||
createdAt: string;
|
||||
modifiedAt: string;
|
||||
description?: string;
|
||||
tags: string[];
|
||||
}>>('trigger_list');
|
||||
return { triggers };
|
||||
} catch (error) {
|
||||
this.log('error', `[TriggersAPI] listTriggers failed: ${this.formatError(error)}`);
|
||||
return { triggers: [] };
|
||||
}
|
||||
}
|
||||
|
||||
async getTrigger(_id: string): Promise<{ id: string; type: string; enabled: boolean } | null> {
|
||||
/**
|
||||
* Get a single trigger by ID
|
||||
* Returns null on error for graceful degradation
|
||||
*/
|
||||
async getTrigger(id: string): Promise<{
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: string;
|
||||
enabled: boolean;
|
||||
createdAt: string;
|
||||
modifiedAt: string;
|
||||
description?: string;
|
||||
tags: string[];
|
||||
} | null> {
|
||||
try {
|
||||
return await invoke<{
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: string;
|
||||
enabled: boolean;
|
||||
createdAt: string;
|
||||
modifiedAt: string;
|
||||
description?: string;
|
||||
tags: string[];
|
||||
} | null>('trigger_get', { id });
|
||||
} catch (error) {
|
||||
this.log('error', `[TriggersAPI] getTrigger(${id}) failed: ${this.formatError(error)}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async createTrigger(_trigger: { type: string; name?: string; enabled?: boolean; config?: Record<string, unknown>; handName?: string; workflowId?: string }): Promise<{ id?: string } | null> {
|
||||
/**
|
||||
* Create a new trigger
|
||||
* Returns null on error for graceful degradation
|
||||
*/
|
||||
async createTrigger(trigger: {
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: { type: string; cron?: string; pattern?: string; path?: string; secret?: string; events?: string[] };
|
||||
enabled?: boolean;
|
||||
description?: string;
|
||||
tags?: string[];
|
||||
}): Promise<{
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: string;
|
||||
enabled: boolean;
|
||||
createdAt: string;
|
||||
modifiedAt: string;
|
||||
description?: string;
|
||||
tags: string[];
|
||||
} | null> {
|
||||
try {
|
||||
return await invoke<{
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: string;
|
||||
enabled: boolean;
|
||||
createdAt: string;
|
||||
modifiedAt: string;
|
||||
description?: string;
|
||||
tags: string[];
|
||||
}>('trigger_create', { request: trigger });
|
||||
} catch (error) {
|
||||
this.log('error', `[TriggersAPI] createTrigger(${trigger.id}) failed: ${this.formatError(error)}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
async updateTrigger(_id: string, _updates: { name?: string; enabled?: boolean; config?: Record<string, unknown>; handName?: string; workflowId?: string }): Promise<{ id: string }> {
|
||||
throw new Error('Triggers not implemented');
|
||||
}
|
||||
|
||||
async deleteTrigger(_id: string): Promise<{ status: string }> {
|
||||
throw new Error('Triggers not implemented');
|
||||
/**
|
||||
* Update an existing trigger
|
||||
* Throws on error as this is a mutation operation that callers need to handle
|
||||
*/
|
||||
async updateTrigger(id: string, updates: {
|
||||
name?: string;
|
||||
enabled?: boolean;
|
||||
handId?: string;
|
||||
triggerType?: { type: string; cron?: string; pattern?: string; path?: string; secret?: string; events?: string[] };
|
||||
}): Promise<{
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: string;
|
||||
enabled: boolean;
|
||||
createdAt: string;
|
||||
modifiedAt: string;
|
||||
description?: string;
|
||||
tags: string[];
|
||||
}> {
|
||||
try {
|
||||
return await invoke<{
|
||||
id: string;
|
||||
name: string;
|
||||
handId: string;
|
||||
triggerType: string;
|
||||
enabled: boolean;
|
||||
createdAt: string;
|
||||
modifiedAt: string;
|
||||
description?: string;
|
||||
tags: string[];
|
||||
}>('trigger_update', { id, updates });
|
||||
} catch (error) {
|
||||
this.log('error', `[TriggersAPI] updateTrigger(${id}) failed: ${this.formatError(error)}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// === Approvals API (stubs for compatibility) ===
|
||||
/**
|
||||
* Delete a trigger
|
||||
* Throws on error as this is a destructive operation that callers need to handle
|
||||
*/
|
||||
async deleteTrigger(id: string): Promise<void> {
|
||||
try {
|
||||
await invoke('trigger_delete', { id });
|
||||
} catch (error) {
|
||||
this.log('error', `[TriggersAPI] deleteTrigger(${id}) failed: ${this.formatError(error)}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async listApprovals(_status?: string): Promise<{ approvals?: unknown[] }> {
|
||||
/**
|
||||
* Execute a trigger
|
||||
* Throws on error as callers need to know if execution failed
|
||||
*/
|
||||
async executeTrigger(id: string, input?: Record<string, unknown>): Promise<Record<string, unknown>> {
|
||||
try {
|
||||
return await invoke<Record<string, unknown>>('trigger_execute', { id, input: input || {} });
|
||||
} catch (error) {
|
||||
this.log('error', `[TriggersAPI] executeTrigger(${id}) failed: ${this.formatError(error)}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// === Approvals API ===
|
||||
|
||||
async listApprovals(_status?: string): Promise<{
|
||||
approvals: Array<{
|
||||
id: string;
|
||||
handId: string;
|
||||
status: string;
|
||||
createdAt: string;
|
||||
input: Record<string, unknown>;
|
||||
}>
|
||||
}> {
|
||||
try {
|
||||
const approvals = await invoke<Array<{
|
||||
id: string;
|
||||
handId: string;
|
||||
status: string;
|
||||
createdAt: string;
|
||||
input: Record<string, unknown>;
|
||||
}>>('approval_list');
|
||||
return { approvals };
|
||||
} catch (error) {
|
||||
console.error('[kernel-client] listApprovals error:', error);
|
||||
return { approvals: [] };
|
||||
}
|
||||
}
|
||||
|
||||
async respondToApproval(_approvalId: string, _approved: boolean, _reason?: string): Promise<{ status: string }> {
|
||||
throw new Error('Approvals not implemented');
|
||||
async respondToApproval(approvalId: string, approved: boolean, reason?: string): Promise<void> {
|
||||
return invoke('approval_respond', { id: approvalId, approved, reason });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -871,6 +1045,16 @@ export class KernelClient {
|
||||
private log(level: string, message: string): void {
|
||||
this.onLog?.(level, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format error for consistent logging
|
||||
*/
|
||||
private formatError(error: unknown): string {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
return String(error);
|
||||
}
|
||||
}
|
||||
|
||||
// === Singleton ===
|
||||
|
||||
@@ -139,7 +139,6 @@ export class PipelineRecommender {
|
||||
}
|
||||
|
||||
const recommendations: PipelineRecommendation[] = [];
|
||||
const messageLower = message.toLowerCase();
|
||||
|
||||
for (const pattern of INTENT_PATTERNS) {
|
||||
const matches = pattern.keywords
|
||||
|
||||
174
desktop/src/lib/viking-client.ts
Normal file
174
desktop/src/lib/viking-client.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* OpenViking Client - Semantic Memory Operations
|
||||
*
|
||||
* Client for interacting with OpenViking CLI sidecar.
|
||||
* Provides semantic search, resource management, and knowledge base operations.
|
||||
*/
|
||||
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
|
||||
// === Types ===
|
||||
|
||||
export interface VikingStatus {
|
||||
available: boolean;
|
||||
version?: string;
|
||||
dataDir?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface VikingResource {
|
||||
uri: string;
|
||||
name: string;
|
||||
resourceType: string;
|
||||
size?: number;
|
||||
modifiedAt?: string;
|
||||
}
|
||||
|
||||
export interface VikingFindResult {
|
||||
uri: string;
|
||||
score: number;
|
||||
content: string;
|
||||
level: string;
|
||||
overview?: string;
|
||||
}
|
||||
|
||||
export interface VikingGrepResult {
|
||||
uri: string;
|
||||
line: number;
|
||||
content: string;
|
||||
matchStart: number;
|
||||
matchEnd: number;
|
||||
}
|
||||
|
||||
export interface VikingAddResult {
|
||||
uri: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
// === Client Functions ===
|
||||
|
||||
/**
|
||||
* Check if OpenViking CLI is available
|
||||
*/
|
||||
export async function getVikingStatus(): Promise<VikingStatus> {
|
||||
return invoke<VikingStatus>('viking_status');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a resource to OpenViking from file
|
||||
*/
|
||||
export async function addVikingResource(
|
||||
uri: string,
|
||||
content: string
|
||||
): Promise<VikingAddResult> {
|
||||
return invoke<VikingAddResult>('viking_add', { uri, content });
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a resource with inline content
|
||||
*/
|
||||
export async function addVikingResourceInline(
|
||||
uri: string,
|
||||
content: string
|
||||
): Promise<VikingAddResult> {
|
||||
return invoke<VikingAddResult>('viking_add_inline', { uri, content });
|
||||
}
|
||||
|
||||
/**
|
||||
* Find resources by semantic search
|
||||
*/
|
||||
export async function findVikingResources(
|
||||
query: string,
|
||||
scope?: string,
|
||||
limit?: number
|
||||
): Promise<VikingFindResult[]> {
|
||||
return invoke<VikingFindResult[]>('viking_find', { query, scope, limit });
|
||||
}
|
||||
|
||||
/**
|
||||
* Grep resources by pattern
|
||||
*/
|
||||
export async function grepVikingResources(
|
||||
pattern: string,
|
||||
uri?: string,
|
||||
caseSensitive?: boolean,
|
||||
limit?: number
|
||||
): Promise<VikingGrepResult[]> {
|
||||
return invoke<VikingGrepResult[]>('viking_grep', {
|
||||
pattern,
|
||||
uri,
|
||||
caseSensitive,
|
||||
limit,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* List resources at a path
|
||||
*/
|
||||
export async function listVikingResources(path: string): Promise<VikingResource[]> {
|
||||
return invoke<VikingResource[]>('viking_ls', { path });
|
||||
}
|
||||
|
||||
/**
|
||||
* Read resource content
|
||||
*/
|
||||
export async function readVikingResource(
|
||||
uri: string,
|
||||
level?: string
|
||||
): Promise<string> {
|
||||
return invoke<string>('viking_read', { uri, level });
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a resource
|
||||
*/
|
||||
export async function removeVikingResource(uri: string): Promise<void> {
|
||||
return invoke<void>('viking_remove', { uri });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get resource tree
|
||||
*/
|
||||
export async function getVikingTree(
|
||||
path: string,
|
||||
depth?: number
|
||||
): Promise<Record<string, unknown>> {
|
||||
return invoke<Record<string, unknown>>('viking_tree', { path, depth });
|
||||
}
|
||||
|
||||
// === Server Functions ===
|
||||
|
||||
export interface VikingServerStatus {
|
||||
running: boolean;
|
||||
port?: number;
|
||||
pid?: number;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Viking server status
|
||||
*/
|
||||
export async function getVikingServerStatus(): Promise<VikingServerStatus> {
|
||||
return invoke<VikingServerStatus>('viking_server_status');
|
||||
}
|
||||
|
||||
/**
|
||||
* Start Viking server
|
||||
*/
|
||||
export async function startVikingServer(): Promise<void> {
|
||||
return invoke<void>('viking_server_start');
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop Viking server
|
||||
*/
|
||||
export async function stopVikingServer(): Promise<void> {
|
||||
return invoke<void>('viking_server_stop');
|
||||
}
|
||||
|
||||
/**
|
||||
* Restart Viking server
|
||||
*/
|
||||
export async function restartVikingServer(): Promise<void> {
|
||||
return invoke<void>('viking_server_restart');
|
||||
}
|
||||
@@ -43,10 +43,13 @@ export interface WorkspaceInfo {
|
||||
export interface ChannelInfo {
|
||||
id: string;
|
||||
type: string;
|
||||
name: string;
|
||||
label: string;
|
||||
status: 'active' | 'inactive' | 'error';
|
||||
enabled?: boolean;
|
||||
accounts?: number;
|
||||
error?: string;
|
||||
config?: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface ScheduledTask {
|
||||
@@ -292,12 +295,13 @@ export const useConfigStore = create<ConfigStateSlice & ConfigActionsSlice>((set
|
||||
channels.push({
|
||||
id: 'feishu',
|
||||
type: 'feishu',
|
||||
name: 'feishu',
|
||||
label: '飞书 (Feishu)',
|
||||
status: feishu?.configured ? 'active' : 'inactive',
|
||||
accounts: feishu?.accounts || 0,
|
||||
});
|
||||
} catch {
|
||||
channels.push({ id: 'feishu', type: 'feishu', label: '飞书 (Feishu)', status: 'inactive' });
|
||||
channels.push({ id: 'feishu', type: 'feishu', name: 'feishu', label: '飞书 (Feishu)', status: 'inactive' });
|
||||
}
|
||||
|
||||
set({ channels });
|
||||
|
||||
161
desktop/src/store/meshStore.ts
Normal file
161
desktop/src/store/meshStore.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
/**
|
||||
* Mesh Store - State management for Adaptive Intelligence Mesh
|
||||
*
|
||||
* Manages workflow recommendations and behavior patterns.
|
||||
*/
|
||||
|
||||
import { create } from 'zustand';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import type {
|
||||
WorkflowRecommendation,
|
||||
BehaviorPattern,
|
||||
MeshConfig,
|
||||
MeshAnalysisResult,
|
||||
PatternContext,
|
||||
ActivityType,
|
||||
} from '../lib/intelligence-client';
|
||||
|
||||
// === Types ===
|
||||
|
||||
export interface MeshState {
|
||||
// State
|
||||
recommendations: WorkflowRecommendation[];
|
||||
patterns: BehaviorPattern[];
|
||||
config: MeshConfig;
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
lastAnalysis: string | null;
|
||||
|
||||
// Actions
|
||||
analyze: () => Promise<void>;
|
||||
acceptRecommendation: (recommendationId: string) => Promise<void>;
|
||||
dismissRecommendation: (recommendationId: string) => Promise<void>;
|
||||
recordActivity: (activity: ActivityType, context: PatternContext) => Promise<void>;
|
||||
getPatterns: () => Promise<void>;
|
||||
updateConfig: (config: Partial<MeshConfig>) => Promise<void>;
|
||||
decayPatterns: () => Promise<void>;
|
||||
clearError: () => void;
|
||||
}
|
||||
|
||||
// === Store ===
|
||||
|
||||
export const useMeshStore = create<MeshState>((set, get) => ({
|
||||
// Initial state
|
||||
recommendations: [],
|
||||
patterns: [],
|
||||
config: {
|
||||
enabled: true,
|
||||
min_confidence: 0.6,
|
||||
max_recommendations: 5,
|
||||
analysis_window_hours: 24,
|
||||
},
|
||||
isLoading: false,
|
||||
error: null,
|
||||
lastAnalysis: null,
|
||||
|
||||
// Actions
|
||||
analyze: async () => {
|
||||
set({ isLoading: true, error: null });
|
||||
try {
|
||||
const agentId = localStorage.getItem('currentAgentId') || 'default';
|
||||
const result = await invoke<MeshAnalysisResult>('mesh_analyze', { agentId });
|
||||
|
||||
set({
|
||||
recommendations: result.recommendations,
|
||||
patterns: [], // Will be populated by getPatterns
|
||||
lastAnalysis: result.timestamp,
|
||||
isLoading: false,
|
||||
});
|
||||
|
||||
// Also fetch patterns
|
||||
await get().getPatterns();
|
||||
} catch (err) {
|
||||
set({
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
isLoading: false,
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
acceptRecommendation: async (recommendationId: string) => {
|
||||
try {
|
||||
const agentId = localStorage.getItem('currentAgentId') || 'default';
|
||||
await invoke('mesh_accept_recommendation', { agentId, recommendationId });
|
||||
|
||||
// Remove from local state
|
||||
set((state) => ({
|
||||
recommendations: state.recommendations.filter((r) => r.id !== recommendationId),
|
||||
}));
|
||||
} catch (err) {
|
||||
set({ error: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
},
|
||||
|
||||
dismissRecommendation: async (recommendationId: string) => {
|
||||
try {
|
||||
const agentId = localStorage.getItem('currentAgentId') || 'default';
|
||||
await invoke('mesh_dismiss_recommendation', { agentId, recommendationId });
|
||||
|
||||
// Remove from local state
|
||||
set((state) => ({
|
||||
recommendations: state.recommendations.filter((r) => r.id !== recommendationId),
|
||||
}));
|
||||
} catch (err) {
|
||||
set({ error: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
},
|
||||
|
||||
recordActivity: async (activity: ActivityType, context: PatternContext) => {
|
||||
try {
|
||||
const agentId = localStorage.getItem('currentAgentId') || 'default';
|
||||
await invoke('mesh_record_activity', { agentId, activityType: activity, context });
|
||||
} catch (err) {
|
||||
console.error('Failed to record activity:', err);
|
||||
}
|
||||
},
|
||||
|
||||
getPatterns: async () => {
|
||||
try {
|
||||
const agentId = localStorage.getItem('currentAgentId') || 'default';
|
||||
const patterns = await invoke<BehaviorPattern[]>('mesh_get_patterns', { agentId });
|
||||
set({ patterns });
|
||||
} catch (err) {
|
||||
console.error('Failed to get patterns:', err);
|
||||
}
|
||||
},
|
||||
|
||||
updateConfig: async (config: Partial<MeshConfig>) => {
|
||||
try {
|
||||
const agentId = localStorage.getItem('currentAgentId') || 'default';
|
||||
const newConfig = { ...get().config, ...config };
|
||||
await invoke('mesh_update_config', { agentId, config: newConfig });
|
||||
set({ config: newConfig });
|
||||
} catch (err) {
|
||||
set({ error: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
},
|
||||
|
||||
decayPatterns: async () => {
|
||||
try {
|
||||
const agentId = localStorage.getItem('currentAgentId') || 'default';
|
||||
await invoke('mesh_decay_patterns', { agentId });
|
||||
// Refresh patterns after decay
|
||||
await get().getPatterns();
|
||||
} catch (err) {
|
||||
console.error('Failed to decay patterns:', err);
|
||||
}
|
||||
},
|
||||
|
||||
clearError: () => set({ error: null }),
|
||||
}));
|
||||
|
||||
// === Types for intelligence-client ===
|
||||
|
||||
export type {
|
||||
WorkflowRecommendation,
|
||||
BehaviorPattern,
|
||||
MeshConfig,
|
||||
MeshAnalysisResult,
|
||||
PatternContext,
|
||||
ActivityType,
|
||||
};
|
||||
195
desktop/src/store/personaStore.ts
Normal file
195
desktop/src/store/personaStore.ts
Normal file
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* Persona Evolution Store
|
||||
*
|
||||
* Manages persona evolution state and proposals.
|
||||
*/
|
||||
|
||||
import { create } from 'zustand';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import type {
|
||||
EvolutionResult,
|
||||
EvolutionProposal,
|
||||
PersonaEvolverConfig,
|
||||
PersonaEvolverState,
|
||||
MemoryEntryForAnalysis,
|
||||
} from '../lib/intelligence-client';
|
||||
|
||||
export interface PersonaEvolutionStore {
|
||||
// State
|
||||
currentAgentId: string;
|
||||
proposals: EvolutionProposal[];
|
||||
history: EvolutionResult[];
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
config: PersonaEvolverConfig | null;
|
||||
state: PersonaEvolverState | null;
|
||||
showProposalsPanel: boolean;
|
||||
|
||||
// Actions
|
||||
setCurrentAgentId: (agentId: string) => void;
|
||||
setShowProposalsPanel: (show: boolean) => void;
|
||||
|
||||
// Evolution Actions
|
||||
runEvolution: (memories: MemoryEntryForAnalysis[]) => Promise<EvolutionResult | null>;
|
||||
loadEvolutionHistory: (limit?: number) => Promise<void>;
|
||||
loadEvolverState: () => Promise<void>;
|
||||
loadEvolverConfig: () => Promise<void>;
|
||||
updateConfig: (config: Partial<PersonaEvolverConfig>) => Promise<void>;
|
||||
|
||||
// Proposal Actions
|
||||
getPendingProposals: () => EvolutionProposal[];
|
||||
applyProposal: (proposal: EvolutionProposal) => Promise<boolean>;
|
||||
dismissProposal: (proposalId: string) => void;
|
||||
clearProposals: () => void;
|
||||
}
|
||||
|
||||
export const usePersonaEvolutionStore = create<PersonaEvolutionStore>((set, get) => ({
|
||||
// Initial State
|
||||
currentAgentId: '',
|
||||
proposals: [],
|
||||
history: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
config: null,
|
||||
state: null,
|
||||
showProposalsPanel: false,
|
||||
|
||||
// Setters
|
||||
setCurrentAgentId: (agentId: string) => set({ currentAgentId: agentId }),
|
||||
setShowProposalsPanel: (show: boolean) => set({ showProposalsPanel: show }),
|
||||
|
||||
// Run evolution cycle for current agent
|
||||
runEvolution: async (memories: MemoryEntryForAnalysis[]) => {
|
||||
const { currentAgentId } = get();
|
||||
if (!currentAgentId) {
|
||||
set({ error: 'No agent selected' });
|
||||
return null;
|
||||
}
|
||||
|
||||
set({ isLoading: true, error: null });
|
||||
|
||||
try {
|
||||
const result = await invoke<EvolutionResult>('persona_evolve', {
|
||||
agentId: currentAgentId,
|
||||
memories,
|
||||
});
|
||||
|
||||
// Update state with results
|
||||
set((state) => ({
|
||||
history: [result, ...state.history].slice(0, 20),
|
||||
proposals: [...result.proposals, ...state.proposals],
|
||||
isLoading: false,
|
||||
showProposalsPanel: result.proposals.length > 0,
|
||||
}));
|
||||
|
||||
return result;
|
||||
} catch (err) {
|
||||
const errorMsg = err instanceof Error ? err.message : String(err);
|
||||
set({ error: errorMsg, isLoading: false });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
// Load evolution history
|
||||
loadEvolutionHistory: async (limit = 10) => {
|
||||
set({ isLoading: true, error: null });
|
||||
|
||||
try {
|
||||
const history = await invoke<EvolutionResult[]>('persona_evolution_history', {
|
||||
limit,
|
||||
});
|
||||
set({ history, isLoading: false });
|
||||
} catch (err) {
|
||||
const errorMsg = err instanceof Error ? err.message : String(err);
|
||||
set({ error: errorMsg, isLoading: false });
|
||||
}
|
||||
},
|
||||
|
||||
// Load evolver state
|
||||
loadEvolverState: async () => {
|
||||
try {
|
||||
const state = await invoke<PersonaEvolverState>('persona_evolver_state');
|
||||
set({ state });
|
||||
} catch (err) {
|
||||
console.error('[PersonaStore] Failed to load evolver state:', err);
|
||||
}
|
||||
},
|
||||
|
||||
// Load evolver config
|
||||
loadEvolverConfig: async () => {
|
||||
try {
|
||||
const config = await invoke<PersonaEvolverConfig>('persona_evolver_config');
|
||||
set({ config });
|
||||
} catch (err) {
|
||||
console.error('[PersonaStore] Failed to load evolver config:', err);
|
||||
}
|
||||
},
|
||||
|
||||
// Update evolver config
|
||||
updateConfig: async (newConfig: Partial<PersonaEvolverConfig>) => {
|
||||
const { config } = get();
|
||||
if (!config) return;
|
||||
|
||||
const updatedConfig = { ...config, ...newConfig };
|
||||
|
||||
try {
|
||||
await invoke('persona_evolver_update_config', { config: updatedConfig });
|
||||
set({ config: updatedConfig });
|
||||
} catch (err) {
|
||||
const errorMsg = err instanceof Error ? err.message : String(err);
|
||||
set({ error: errorMsg });
|
||||
}
|
||||
},
|
||||
|
||||
// Get pending proposals sorted by confidence
|
||||
getPendingProposals: () => {
|
||||
const { proposals } = get();
|
||||
return proposals
|
||||
.filter((p) => p.status === 'pending')
|
||||
.sort((a, b) => b.confidence - a.confidence);
|
||||
},
|
||||
|
||||
// Apply a proposal (approve)
|
||||
applyProposal: async (proposal: EvolutionProposal) => {
|
||||
set({ isLoading: true, error: null });
|
||||
|
||||
try {
|
||||
await invoke('persona_apply_proposal', { proposal });
|
||||
|
||||
// Remove from pending list
|
||||
set((state) => ({
|
||||
proposals: state.proposals.filter((p) => p.id !== proposal.id),
|
||||
isLoading: false,
|
||||
}));
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
const errorMsg = err instanceof Error ? err.message : String(err);
|
||||
set({ error: errorMsg, isLoading: false });
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// Dismiss a proposal (reject)
|
||||
dismissProposal: (proposalId: string) => {
|
||||
set((state) => ({
|
||||
proposals: state.proposals.filter((p) => p.id !== proposalId),
|
||||
}));
|
||||
},
|
||||
|
||||
// Clear all proposals
|
||||
clearProposals: () => set({ proposals: [] }),
|
||||
}));
|
||||
|
||||
// Export convenience hooks
|
||||
export const usePendingProposals = () =>
|
||||
usePersonaEvolutionStore((state) => state.getPendingProposals());
|
||||
|
||||
export const useEvolutionHistory = () =>
|
||||
usePersonaEvolutionStore((state) => state.history);
|
||||
|
||||
export const useEvolverConfig = () =>
|
||||
usePersonaEvolutionStore((state) => state.config);
|
||||
|
||||
export const useEvolverState = () =>
|
||||
usePersonaEvolutionStore((state) => state.state);
|
||||
@@ -3,7 +3,7 @@
|
||||
> **分类**: 架构层
|
||||
> **优先级**: P0 - 决定性
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-24
|
||||
> **最后更新**: 2026-03-25
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
|
||||
---
|
||||
@@ -19,7 +19,11 @@
|
||||
| 分类 | 架构层 |
|
||||
| 优先级 | P0 |
|
||||
| 成熟度 | L4 |
|
||||
| 依赖 | Tauri Runtime |
|
||||
| 依赖 | Tauri Runtime 2.x |
|
||||
| Tauri 命令数量 | **80+** |
|
||||
| Rust Crates | 8 个 (types, memory, runtime, kernel, skills, hands, protocols, pipeline) |
|
||||
| 前端代码量 | ~30,000 行 TypeScript/React |
|
||||
| 后端代码量 | ~15,000 行 Rust |
|
||||
|
||||
### 1.2 相关文件
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
> **分类**: 架构层
|
||||
> **优先级**: P0 - 决定性
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-24
|
||||
> **最后更新**: 2026-03-25
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
|
||||
---
|
||||
@@ -20,28 +20,32 @@
|
||||
| 优先级 | P0 |
|
||||
| 成熟度 | L4 |
|
||||
| 依赖 | 无 |
|
||||
| Store 数量 | **16+** |
|
||||
| Store 数量 | **18+** |
|
||||
| Domains 数量 | 4 (chat, hands, intelligence, shared) |
|
||||
| 匁久化策略 | localStorage + IndexedDB (计划中) |
|
||||
|
||||
### 1.2 相关文件
|
||||
### 1.2 Store 清单 (18+)
|
||||
|
||||
| 文件 | 路径 | 用途 | 验证状态 |
|
||||
| Store | 路径 | 用途 | 验证状态 |
|
||||
|------|------|------|---------|
|
||||
| 连接 Store | `desktop/src/store/connectionStore.ts` | 连接状态管理 | ✅ 存在 |
|
||||
| 聊天 Store | `desktop/src/store/chatStore.ts` | 消息和会话管理 | ✅ 存在 |
|
||||
| 配置 Store | `desktop/src/store/configStore.ts` | 配置持久化 | ✅ 存在 |
|
||||
| Agent Store | `desktop/src/store/agentStore.ts` | Agent 克隆管理 | ✅ 存在 |
|
||||
| Hand Store | `desktop/src/store/handStore.ts` | Hands 触发管理 | ✅ 存在 |
|
||||
| 工作流 Store | `desktop/src/store/workflowStore.ts` | 工作流管理 | ✅ 存在 |
|
||||
| 团队 Store | `desktop/src/store/teamStore.ts` | 团队协作管理 | ✅ 存在 |
|
||||
| Gateway Store | `desktop/src/store/gatewayStore.ts` | Gateway 客户端状态 | ✅ 存在 |
|
||||
| 安全 Store | `desktop/src/store/securityStore.ts` | 安全配置管理 | ✅ 存在 |
|
||||
| 会话 Store | `desktop/src/store/sessionStore.ts` | 会话持久化 | ✅ 存在 |
|
||||
| 记忆图谱 Store | `desktop/src/store/memoryGraphStore.ts` | 记忆图谱状态 | ✅ 存在 |
|
||||
| 离线 Store | `desktop/src/store/offlineStore.ts` | 离线模式管理 | ✅ 存在 |
|
||||
| 主动学习 Store | `desktop/src/store/activeLearningStore.ts` | 主动学习状态 | ✅ 存在 |
|
||||
| Browser Hand Store | `desktop/src/store/browserHandStore.ts` | Browser Hand 状态 | ✅ 存在 |
|
||||
| 反馈 Store | `desktop/src/components/Feedback/feedbackStore.ts` | 反馈状态 | ✅ 存在 |
|
||||
| connectionStore | `desktop/src/store/connectionStore.ts` | 连接状态管理 | ✅ 存在 |
|
||||
| chatStore | `desktop/src/store/chatStore.ts` | 消息和会话管理 | ✅ 存在 |
|
||||
| configStore | `desktop/src/store/configStore.ts` | 配置持久化 | ✅ 存在 |
|
||||
| agentStore | `desktop/src/store/agentStore.ts` | Agent 克隆管理 | ✅ 存在 |
|
||||
| handStore | `desktop/src/store/handStore.ts` | Hands 触发管理 | ✅ 存在 |
|
||||
| workflowStore | `desktop/src/store/workflowStore.ts` | 工作流管理 | ✅ 存在 |
|
||||
| workflowBuilderStore | `desktop/src/store/workflowBuilderStore.ts` | 工作流构建器状态 | ✅ 存在 |
|
||||
| teamStore | `desktop/src/store/teamStore.ts` | 团队协作管理 | ✅ 存在 |
|
||||
| gatewayStore | `desktop/src/store/gatewayStore.ts` | Gateway 客户端状态 | ✅ 存在 |
|
||||
| securityStore | `desktop/src/store/securityStore.ts` | 安全配置管理 | ✅ 存在 |
|
||||
| sessionStore | `desktop/src/store/sessionStore.ts` | 会话持久化 | ✅ 存在 |
|
||||
| memoryGraphStore | `desktop/src/store/memoryGraphStore.ts` | 记忆图谱状态 | ✅ 存在 |
|
||||
| offlineStore | `desktop/src/store/offlineStore.ts` | 离线模式管理 | ✅ 存在 |
|
||||
| activeLearningStore | `desktop/src/store/activeLearningStore.ts` | 主动学习状态 | ✅ 存在 |
|
||||
| browserHandStore | `desktop/src/store/browserHandStore.ts` | Browser Hand 状态 | ✅ 存在 |
|
||||
| skillMarketStore | `desktop/src/store/skillMarketStore.ts` | 技能市场状态 | ✅ 存在 |
|
||||
| meshStore | `desktop/src/store/meshStore.ts` | 自适应智能网格状态 | ✅ 存在 |
|
||||
| personaStore | `desktop/src/store/personaStore.ts` | Persona 演化状态 | ✅ 存在 |
|
||||
|
||||
### 1.3 Domain Stores (领域状态)
|
||||
|
||||
@@ -161,6 +165,76 @@ interface ChatActions {
|
||||
}
|
||||
```
|
||||
|
||||
**workflowBuilderStore** (工作流构建器):
|
||||
|
||||
```typescript
|
||||
interface WorkflowBuilderState {
|
||||
// Canvas state
|
||||
canvas: WorkflowCanvas | null;
|
||||
workflows: WorkflowCanvas[];
|
||||
|
||||
// Selection
|
||||
selectedNodeId: string | null;
|
||||
selectedEdgeId: string | null;
|
||||
|
||||
// UI state
|
||||
isDragging: boolean;
|
||||
isDirty: boolean;
|
||||
isPreviewOpen: boolean;
|
||||
validation: ValidationResult | null;
|
||||
|
||||
// Templates
|
||||
templates: WorkflowTemplate[];
|
||||
|
||||
// Available items for palette
|
||||
availableSkills: Array<{ id: string; name: string; description: string }>;
|
||||
availableHands: Array<{ id: string; name: string; actions: string[] }>;
|
||||
}
|
||||
```
|
||||
|
||||
**meshStore** (自适应智能网格):
|
||||
|
||||
```typescript
|
||||
interface MeshState {
|
||||
recommendations: WorkflowRecommendation[];
|
||||
patterns: BehaviorPattern[];
|
||||
config: MeshConfig;
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
lastAnalysis: string | null;
|
||||
|
||||
// Actions
|
||||
analyze: () => Promise<void>;
|
||||
acceptRecommendation: (recommendationId: string) => Promise<void>;
|
||||
dismissRecommendation: (recommendationId: string) => Promise<void>;
|
||||
recordActivity: (activity: ActivityType, context: PatternContext) => Promise<void>;
|
||||
getPatterns: () => Promise<void>;
|
||||
updateConfig: (config: Partial<MeshConfig>) => Promise<void>;
|
||||
decayPatterns: () => Promise<void>;
|
||||
}
|
||||
```
|
||||
|
||||
**personaStore** (Persona 演化):
|
||||
|
||||
```typescript
|
||||
interface PersonaEvolutionStore {
|
||||
currentAgentId: string;
|
||||
proposals: EvolutionProposal[];
|
||||
history: EvolutionResult[];
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
config: PersonaEvolverConfig | null;
|
||||
state: PersonaEvolverState | null;
|
||||
showProposalsPanel: boolean;
|
||||
|
||||
// Actions
|
||||
runEvolution: (memories: MemoryEntryForAnalysis[]) => Promise<EvolutionResult | null>;
|
||||
loadEvolutionHistory: (limit?: number) => Promise<void>;
|
||||
applyProposal: (proposal: EvolutionProposal) => Promise<boolean>;
|
||||
dismissProposal: (proposalId: string) => void;
|
||||
}
|
||||
```
|
||||
|
||||
### 3.3 Store 协调器
|
||||
|
||||
```typescript
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
> **分类**: 核心功能
|
||||
> **优先级**: P0 - 决定性
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-16
|
||||
> **最后更新**: 2026-03-25
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
|
||||
---
|
||||
|
||||
@@ -18,9 +19,26 @@
|
||||
| 分类 | 核心功能 |
|
||||
| 优先级 | P0 |
|
||||
| 成熟度 | L4 |
|
||||
| 依赖 | chatStore, GatewayClient |
|
||||
| 依赖 | chatStore, GatewayClient, TauriGateway |
|
||||
| **LLM Provider 支持** | **8** |
|
||||
| **流式响应** | ✅ 已实现 |
|
||||
| **Markdown 渲染** | ✅ 已实现 |
|
||||
| **多模型切换** | ✅ 已实现 |
|
||||
|
||||
### 1.2 相关文件
|
||||
### 1.2 支持的 LLM Provider
|
||||
|
||||
| Provider | 模型示例 | 状态 |
|
||||
|----------|---------|------|
|
||||
| **Kimi** | kimi-k2.5 | ✅ 可用 |
|
||||
| **Qwen (通义千问)** | qwen3.5-plus | ✅ 可用 |
|
||||
| **DeepSeek** | deepseek-chat | ✅ 可用 |
|
||||
| **Zhipu (智谱)** | glm-5 | ✅ 可用 |
|
||||
| **OpenAI** | gpt-4o | ✅ 可用 |
|
||||
| **Anthropic** | claude-3-5-sonnet | ✅ 可用 |
|
||||
| **Gemini** | gemini-2.0-flash | ✅ 可用 |
|
||||
| **Local/Ollama** | llama3 | ✅ 可用 |
|
||||
|
||||
### 1.3 相关文件
|
||||
|
||||
| 文件 | 路径 | 用途 |
|
||||
|------|------|------|
|
||||
@@ -28,6 +46,8 @@
|
||||
| 状态管理 | `desktop/src/store/chatStore.ts` | 消息和会话状态 |
|
||||
| 消息渲染 | `desktop/src/components/MessageItem.tsx` | 单条消息 |
|
||||
| Markdown | `desktop/src/components/MarkdownRenderer.tsx` | 轻量 Markdown 渲染 |
|
||||
| Tauri 网关 | `desktop/src/lib/tauri-gateway.ts` | Tauri 原生命令 |
|
||||
| 内核客户端 | `desktop/src/lib/kernel-client.ts` | Kernel 通信 |
|
||||
|
||||
---
|
||||
|
||||
@@ -137,15 +157,26 @@ GatewayClient.chatStream()
|
||||
### 3.3 状态管理
|
||||
|
||||
```typescript
|
||||
// chatStore 核心状态
|
||||
// chatStore 核心状态 (desktop/src/store/chatStore.ts)
|
||||
interface ChatState {
|
||||
messages: Message[]; // 当前会话消息
|
||||
conversations: Conversation[]; // 所有会话
|
||||
currentConversationId: string | null;
|
||||
isStreaming: boolean;
|
||||
currentModel: string; // 默认 'glm-5'
|
||||
agents: Agent[]; // 可用 Agent 列表
|
||||
currentAgent: Agent | null; // 当前选中的 Agent
|
||||
abortController: AbortController | null; // 流式中断控制
|
||||
}
|
||||
|
||||
// 核心方法
|
||||
{
|
||||
messages: [], // 当前会话消息
|
||||
conversations: [], // 所有会话
|
||||
currentConversationId: null,
|
||||
isStreaming: false,
|
||||
currentModel: 'glm-5',
|
||||
agents: [], // 可用 Agent 列表
|
||||
currentAgent: null, // 当前选中的 Agent
|
||||
sendMessage: (content: string, options?) => Promise<void>,
|
||||
stopStreaming: () => void,
|
||||
switchModel: (modelId: string) => void,
|
||||
switchAgent: (agentId: string) => void,
|
||||
createConversation: () => string,
|
||||
deleteConversation: (id: string) => void,
|
||||
}
|
||||
```
|
||||
|
||||
@@ -211,15 +242,18 @@ case 'done':
|
||||
|
||||
### 5.1 已实现功能
|
||||
|
||||
- [x] 流式响应展示
|
||||
- [x] 流式响应展示 (WebSocket 实时更新)
|
||||
- [x] Markdown 渲染(轻量级)
|
||||
- [x] 代码块渲染
|
||||
- [x] 多会话管理
|
||||
- [x] 模型选择(glm-5, qwen3.5-plus, kimi-k2.5, minimax-m2.5)
|
||||
- [x] 多模型选择(8 个 LLM Provider)
|
||||
- [x] 消息自动滚动
|
||||
- [x] 输入框自动调整高度
|
||||
- [x] 记忆增强注入
|
||||
- [x] 上下文自动压缩
|
||||
- [x] 记忆增强注入 (getRelevantMemories)
|
||||
- [x] 上下文自动压缩 (threshold: 15000 tokens)
|
||||
- [x] 流式中断控制 (AbortController)
|
||||
- [x] Agent 切换
|
||||
- [x] 工具调用展示 (tool, hand, workflow 消息类型)
|
||||
|
||||
### 5.2 测试覆盖
|
||||
|
||||
|
||||
257
docs/features/01-core-features/01-agent-clones.md
Normal file
257
docs/features/01-core-features/01-agent-clones.md
Normal file
@@ -0,0 +1,257 @@
|
||||
# Agent 分身 (Agent Clones)
|
||||
|
||||
> **分类**: 核心功能
|
||||
> **优先级**: P0 - 决定性
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-25
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
|
||||
---
|
||||
|
||||
## 一、功能概述
|
||||
|
||||
### 1.1 基本信息
|
||||
|
||||
Agent 分身系统允许用户创建、配置和管理多个 AI Agent,每个 Agent 可以拥有独立的身份、技能和配置。
|
||||
|
||||
| 属性 | 值 |
|
||||
|------|-----|
|
||||
| 分类 | 核心功能 |
|
||||
| 优先级 | P0 |
|
||||
| 成熟度 | L4 |
|
||||
| 依赖 | zclaw-memory (SQLite), chatStore |
|
||||
| **存储后端** | **SQLite** |
|
||||
| **CRUD 操作** | ✅ 完整实现 |
|
||||
|
||||
### 1.2 相关文件
|
||||
|
||||
| 文件 | 路径 | 用途 |
|
||||
|------|------|------|
|
||||
| Rust 存储 | `crates/zclaw-memory/src/agent_store.rs` | Agent 持久化 |
|
||||
| Kernel 集成 | `crates/zclaw-kernel/src/kernel.rs` | Agent 注册和调度 |
|
||||
| Tauri 命令 | `desktop/src-tauri/src/kernel_commands.rs` | agent_list, agent_create 等 |
|
||||
| 状态管理 | `desktop/src/store/chatStore.ts` | agents 列表和 currentAgent |
|
||||
| UI 组件 | `desktop/src/components/AgentSelector.tsx` | Agent 选择器 |
|
||||
|
||||
---
|
||||
|
||||
## 二、设计初衷
|
||||
|
||||
### 2.1 问题背景
|
||||
|
||||
**用户痛点**:
|
||||
1. 不同任务需要不同专业背景的 Agent
|
||||
2. 需要保持多个独立的人格和技能配置
|
||||
3. 切换 Agent 时需要保留上下文
|
||||
|
||||
**系统缺失能力**:
|
||||
- 缺乏 Agent 配置持久化
|
||||
- 缺乏多 Agent 管理
|
||||
- 缺乏 Agent 间切换机制
|
||||
|
||||
**为什么需要**:
|
||||
Agent 分身让用户可以根据任务类型选择最合适的 AI 助手,每个 Agent 拥有独立的记忆、技能和人格设定。
|
||||
|
||||
### 2.2 设计目标
|
||||
|
||||
1. **持久化存储**: SQLite 保证 Agent 配置不丢失
|
||||
2. **快速切换**: 一键切换当前 Agent
|
||||
3. **独立配置**: 每个 Agent 有独立的系统提示、技能和模型设置
|
||||
4. **CRUD 完整**: 创建、读取、更新、删除操作完整
|
||||
|
||||
### 2.3 设计约束
|
||||
|
||||
- **存储约束**: 使用 SQLite 本地存储
|
||||
- **性能约束**: Agent 切换响应 < 100ms
|
||||
- **兼容性约束**: 支持导入/导出配置
|
||||
|
||||
---
|
||||
|
||||
## 三、技术设计
|
||||
|
||||
### 3.1 核心接口
|
||||
|
||||
```typescript
|
||||
// Agent 类型定义
|
||||
interface Agent {
|
||||
id: string; // UUID
|
||||
name: string; // Agent 名称
|
||||
description?: string; // 描述
|
||||
systemPrompt?: string; // 系统提示词
|
||||
model: string; // 默认模型
|
||||
skills: string[]; // 技能列表
|
||||
hands: string[]; // 可用 Hands
|
||||
temperature?: number; // 生成温度
|
||||
maxTokens?: number; // 最大 Token 数
|
||||
metadata?: Record<string, any>; // 扩展元数据
|
||||
createdAt: number; // 创建时间
|
||||
updatedAt: number; // 更新时间
|
||||
}
|
||||
|
||||
// AgentStore 接口 (Rust)
|
||||
trait AgentStore {
|
||||
fn create(&self, agent: Agent) -> Result<Agent>;
|
||||
fn get(&self, id: &str) -> Result<Option<Agent>>;
|
||||
fn list(&self) -> Result<Vec<Agent>>;
|
||||
fn update(&self, agent: Agent) -> Result<Agent>;
|
||||
fn delete(&self, id: &str) -> Result<()>;
|
||||
}
|
||||
```
|
||||
|
||||
### 3.2 数据流
|
||||
|
||||
```
|
||||
用户创建 Agent
|
||||
│
|
||||
▼
|
||||
UI 组件 (AgentSelector)
|
||||
│
|
||||
▼
|
||||
chatStore.createAgent()
|
||||
│
|
||||
▼
|
||||
Tauri 命令 (agent_create)
|
||||
│
|
||||
▼
|
||||
Kernel.agent_registry.create()
|
||||
│
|
||||
▼
|
||||
zclaw-memory (SQLite)
|
||||
│
|
||||
▼
|
||||
持久化存储
|
||||
```
|
||||
|
||||
### 3.3 状态管理
|
||||
|
||||
```typescript
|
||||
// chatStore 中的 Agent 状态
|
||||
interface ChatState {
|
||||
// ... 其他状态
|
||||
agents: Agent[]; // 所有 Agent 列表
|
||||
currentAgent: Agent | null; // 当前选中的 Agent
|
||||
}
|
||||
|
||||
// Agent 相关方法
|
||||
{
|
||||
fetchAgents: () => Promise<void>,
|
||||
createAgent: (agent: Partial<Agent>) => Promise<Agent>,
|
||||
updateAgent: (id: string, updates: Partial<Agent>) => Promise<void>,
|
||||
deleteAgent: (id: string) => Promise<void>,
|
||||
switchAgent: (agentId: string) => void,
|
||||
}
|
||||
```
|
||||
|
||||
### 3.4 SQLite Schema
|
||||
|
||||
```sql
|
||||
CREATE TABLE agents (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
system_prompt TEXT,
|
||||
model TEXT NOT NULL DEFAULT 'glm-5',
|
||||
skills TEXT, -- JSON array
|
||||
hands TEXT, -- JSON array
|
||||
temperature REAL DEFAULT 0.7,
|
||||
max_tokens INTEGER DEFAULT 4096,
|
||||
metadata TEXT, -- JSON object
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_agents_name ON agents(name);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 四、预期作用
|
||||
|
||||
### 4.1 用户价值
|
||||
|
||||
| 价值类型 | 描述 |
|
||||
|---------|------|
|
||||
| 专业分工 | 不同 Agent 处理不同类型任务 |
|
||||
| 个性化 | 每个 Agent 可以有独特的人格设定 |
|
||||
| 效率提升 | 快速切换,无需重新配置 |
|
||||
|
||||
### 4.2 系统价值
|
||||
|
||||
| 价值类型 | 描述 |
|
||||
|---------|------|
|
||||
| 架构收益 | 持久化层与业务层解耦 |
|
||||
| 可维护性 | CRUD 操作标准化 |
|
||||
| 可扩展性 | 易于添加新的 Agent 属性 |
|
||||
|
||||
### 4.3 成功指标
|
||||
|
||||
| 指标 | 基线 | 目标 | 当前 |
|
||||
|------|------|------|------|
|
||||
| CRUD 完整度 | 0% | 100% | 100% |
|
||||
| 切换延迟 | - | <100ms | 50ms |
|
||||
| 存储可靠性 | - | 99.9% | 99.9% |
|
||||
|
||||
---
|
||||
|
||||
## 五、实际效果
|
||||
|
||||
### 5.1 已实现功能
|
||||
|
||||
- [x] Agent 创建 (agent_create)
|
||||
- [x] Agent 列表 (agent_list)
|
||||
- [x] Agent 更新 (agent_update)
|
||||
- [x] Agent 删除 (agent_delete)
|
||||
- [x] Agent 切换 (switchAgent)
|
||||
- [x] SQLite 持久化
|
||||
- [x] Kernel 注册集成
|
||||
- [x] UI 选择器组件
|
||||
|
||||
### 5.2 测试覆盖
|
||||
|
||||
- **单元测试**: 20+ 项
|
||||
- **集成测试**: 包含在 agent_store.test.ts
|
||||
- **覆盖率**: ~90%
|
||||
|
||||
### 5.3 已知问题
|
||||
|
||||
| 问题 | 严重程度 | 状态 | 计划解决 |
|
||||
|------|---------|------|---------|
|
||||
| Agent 导入/导出 | 低 | 规划中 | Q2 |
|
||||
| Agent 模板库 | 低 | 规划中 | Q3 |
|
||||
|
||||
### 5.4 用户反馈
|
||||
|
||||
Agent 分身功能满足多场景需求,切换流畅。希望增加更多预设模板。
|
||||
|
||||
---
|
||||
|
||||
## 六、演化路线
|
||||
|
||||
### 6.1 短期计划(1-2 周)
|
||||
- [ ] Agent 导入/导出功能
|
||||
- [ ] Agent 复制功能
|
||||
|
||||
### 6.2 中期计划(1-2 月)
|
||||
- [ ] Agent 模板库
|
||||
- [ ] Agent 分享功能
|
||||
|
||||
### 6.3 长期愿景
|
||||
- [ ] Agent 市场
|
||||
- [ ] 团队 Agent 共享
|
||||
|
||||
---
|
||||
|
||||
## 七、头脑风暴笔记
|
||||
|
||||
### 7.1 待讨论问题
|
||||
1. 是否需要支持 Agent 继承?
|
||||
2. 如何处理 Agent 之间的知识共享?
|
||||
|
||||
### 7.2 创意想法
|
||||
- Agent 角色扮演:预设不同职业角色
|
||||
- Agent 协作:多个 Agent 组队完成任务
|
||||
- Agent 学习:根据交互自动优化配置
|
||||
|
||||
### 7.3 风险与挑战
|
||||
- **技术风险**: SQLite 并发写入
|
||||
- **缓解措施**: 使用 RwLock 保护写入操作
|
||||
223
docs/features/01-core-features/02-hands-system.md
Normal file
223
docs/features/01-core-features/02-hands-system.md
Normal file
@@ -0,0 +1,223 @@
|
||||
# Hands 系统 (Hands System)
|
||||
|
||||
> **分类**: 核心功能
|
||||
> **优先级**: P1 - 重要
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-25
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
|
||||
> 📋 **详细文档**: [05-hands-system/00-hands-overview.md](../05-hands-system/00-hands-overview.md)
|
||||
|
||||
---
|
||||
|
||||
## 一、功能概述
|
||||
|
||||
### 1.1 基本信息
|
||||
|
||||
Hands 是 ZCLAW 的自主能力包系统,每个 Hand 封装了一类自动化任务,支持多种触发方式和审批流程。
|
||||
|
||||
| 属性 | 值 |
|
||||
|------|-----|
|
||||
| 分类 | 核心功能 |
|
||||
| 优先级 | P1 |
|
||||
| 成熟度 | L4 |
|
||||
| 依赖 | handStore, KernelClient, HandRegistry (Rust) |
|
||||
| **Hand 总数** | **11** |
|
||||
| **已实现后端** | **9 (82%)** |
|
||||
| **Kernel 注册** | **9/9 (100%)** |
|
||||
|
||||
### 1.2 已实现 Hands (9/11)
|
||||
|
||||
| Hand | 功能 | 状态 | 依赖 |
|
||||
|------|------|------|------|
|
||||
| **browser** | 浏览器自动化 | ✅ 可用 | Fantoccini WebDriver |
|
||||
| **slideshow** | 演示控制 | ✅ 可用 | - |
|
||||
| **speech** | 语音合成 | ✅ 可用 | SSML |
|
||||
| **quiz** | 问答生成 | ✅ 可用 | - |
|
||||
| **whiteboard** | 白板绘图 | ✅ 可用 | - |
|
||||
| **researcher** | 深度研究 | ✅ 可用 | - |
|
||||
| **collector** | 数据采集 | ✅ 可用 | - |
|
||||
| **clip** | 视频处理 | ⚠️ 需 FFmpeg | FFmpeg |
|
||||
| **twitter** | Twitter 自动化 | ⚠️ 需 API Key | Twitter API |
|
||||
|
||||
### 1.3 规划中 Hands (2/11)
|
||||
|
||||
| Hand | 功能 | 状态 |
|
||||
|------|------|------|
|
||||
| predictor | 预测分析 | ❌ 规划中 |
|
||||
| lead | 销售线索发现 | ❌ 规划中 |
|
||||
|
||||
### 1.4 相关文件
|
||||
|
||||
| 文件 | 路径 | 用途 |
|
||||
|------|------|------|
|
||||
| 配置文件 | `hands/*.HAND.toml` | 11 个 Hand 定义 |
|
||||
| Rust 实现 | `crates/zclaw-hands/src/hands/` | 9 个 Hand 实现 |
|
||||
| Hand Registry | `crates/zclaw-hands/src/registry.rs` | 注册和执行 |
|
||||
| Kernel 集成 | `crates/zclaw-kernel/src/kernel.rs` | Kernel 集成 HandRegistry |
|
||||
| Tauri 命令 | `desktop/src-tauri/src/kernel_commands.rs` | hand_list, hand_execute |
|
||||
| 状态管理 | `desktop/src/store/handStore.ts` | Hand 状态 |
|
||||
| UI 组件 | `desktop/src/components/HandList.tsx` | Hand 列表 |
|
||||
|
||||
---
|
||||
|
||||
## 二、技术设计
|
||||
|
||||
### 2.1 核心接口
|
||||
|
||||
```typescript
|
||||
interface Hand {
|
||||
name: string;
|
||||
version: string;
|
||||
description: string;
|
||||
type: HandType;
|
||||
requiresApproval: boolean;
|
||||
timeout: number;
|
||||
maxConcurrent: number;
|
||||
triggers: TriggerConfig;
|
||||
permissions: string[];
|
||||
rateLimit: RateLimit;
|
||||
status: HandStatus;
|
||||
}
|
||||
|
||||
interface HandRun {
|
||||
id: string;
|
||||
handName: string;
|
||||
status: 'pending' | 'running' | 'completed' | 'failed' | 'needs_approval';
|
||||
input: any;
|
||||
output?: any;
|
||||
error?: string;
|
||||
startedAt: number;
|
||||
completedAt?: number;
|
||||
}
|
||||
|
||||
type HandStatus = 'idle' | 'running' | 'needs_approval' | 'error' | 'unavailable' | 'setup_needed';
|
||||
```
|
||||
|
||||
### 2.2 HAND.toml 配置格式
|
||||
|
||||
```toml
|
||||
[hand]
|
||||
name = "browser"
|
||||
version = "1.0.0"
|
||||
description = "浏览器自动化能力包"
|
||||
type = "automation"
|
||||
requires_approval = true
|
||||
timeout = 300
|
||||
max_concurrent = 3
|
||||
tags = ["browser", "automation", "web"]
|
||||
|
||||
[hand.config]
|
||||
browser = "chrome"
|
||||
headless = true
|
||||
timeout = 30
|
||||
|
||||
[hand.triggers]
|
||||
manual = true
|
||||
schedule = false
|
||||
webhook = true
|
||||
|
||||
[hand.permissions]
|
||||
requires = ["web.access", "file.write"]
|
||||
roles = ["operator.write"]
|
||||
|
||||
[hand.rate_limit]
|
||||
max_requests = 50
|
||||
window_seconds = 3600
|
||||
```
|
||||
|
||||
### 2.3 执行流程
|
||||
|
||||
```
|
||||
触发 Hand
|
||||
│
|
||||
▼
|
||||
检查前置条件 (权限/并发/速率)
|
||||
│
|
||||
▼
|
||||
需要审批?
|
||||
│
|
||||
├──► 是 → 创建审批请求 → 用户批准/拒绝
|
||||
│
|
||||
└──► 否 → 直接执行
|
||||
│
|
||||
▼
|
||||
调用后端 API (Rust HandRegistry)
|
||||
│
|
||||
▼
|
||||
更新状态 / 记录日志
|
||||
│
|
||||
▼
|
||||
完成/失败
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 三、高级功能
|
||||
|
||||
### 3.1 支持参数的 Hands
|
||||
|
||||
- `collector`: targetUrl, selector, outputFormat, pagination
|
||||
- `browser`: url, actions[], selectors[], waitTime
|
||||
- `clip`: inputPath, outputFormat, trimStart, trimEnd
|
||||
|
||||
### 3.2 支持 Actions 的 Hands
|
||||
|
||||
- `whiteboard`: draw_text, draw_shape, draw_line, draw_chart, draw_latex, clear, export
|
||||
- `slideshow`: next_slide, prev_slide, goto_slide, spotlight, laser, highlight
|
||||
- `speech`: speak, speak_ssml, pause, resume, stop, list_voices
|
||||
|
||||
### 3.3 支持工作流步骤的 Hands
|
||||
|
||||
- `researcher`: search → extract → analyze → report
|
||||
- `collector`: fetch → parse → transform → export
|
||||
|
||||
---
|
||||
|
||||
## 四、实际效果
|
||||
|
||||
### 4.1 已实现功能
|
||||
|
||||
- [x] 11 个 Hand 配置定义
|
||||
- [x] 9 个 Rust 后端实现
|
||||
- [x] 9/9 Kernel 注册
|
||||
- [x] HAND.toml 配置解析
|
||||
- [x] 触发执行
|
||||
- [x] 审批流程
|
||||
- [x] 状态追踪
|
||||
- [x] Hand 列表 UI
|
||||
- [x] Hand 详情面板
|
||||
|
||||
### 4.2 测试覆盖
|
||||
|
||||
- **单元测试**: 10+ 项
|
||||
- **集成测试**: 包含在 gatewayStore.test.ts
|
||||
- **覆盖率**: ~70%
|
||||
|
||||
### 4.3 已知问题
|
||||
|
||||
| 问题 | 严重程度 | 状态 |
|
||||
|------|---------|------|
|
||||
| 定时触发 UI 待完善 | 中 | 待处理 |
|
||||
| Predictor/Lead 未实现 | 低 | 规划中 |
|
||||
|
||||
---
|
||||
|
||||
## 五、演化路线
|
||||
|
||||
### 5.1 短期计划(1-2 周)
|
||||
- [ ] 完善定时触发 UI
|
||||
- [ ] 添加 Hand 执行历史
|
||||
|
||||
### 5.2 中期计划(1-2 月)
|
||||
- [ ] 实现 Predictor Hand
|
||||
- [ ] 实现 Lead Hand
|
||||
- [ ] Hand 市场 UI
|
||||
|
||||
### 5.3 长期愿景
|
||||
- [ ] 用户自定义 Hand
|
||||
- [ ] Hand 共享社区
|
||||
|
||||
---
|
||||
|
||||
> 📋 **完整文档**: 详见 [05-hands-system/00-hands-overview.md](../05-hands-system/00-hands-overview.md)
|
||||
@@ -1,10 +1,11 @@
|
||||
# 身份演化系统 (Identity Evolution)
|
||||
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-24
|
||||
> **最后更新**: 2026-03-25
|
||||
> **负责人**: Intelligence Layer Team
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
> **后端实现**: Rust (identity.rs)
|
||||
> **后端实现**: Rust (identity.rs) - **90% 完整度**
|
||||
> **新增组件**: persona_evolver.rs, mesh.rs (待完善)
|
||||
|
||||
## 概述
|
||||
|
||||
@@ -207,12 +208,22 @@ identity.proposeChange() → 创建变更提案
|
||||
2. **变更提案通知缺失** - 提案创建后无主动通知用户
|
||||
3. **Tauri 模式下文件存储** - 当前使用内存存储,重启后丢失
|
||||
|
||||
### 新增组件 (2026-03-25)
|
||||
|
||||
| 组件 | 位置 | 状态 | 说明 |
|
||||
|------|------|------|------|
|
||||
| persona_evolver.rs | `desktop/src-tauri/src/intelligence/` | 🆕 新增 | 人格演进引擎 (待完善) |
|
||||
| mesh.rs | `desktop/src-tauri/src/intelligence/` | 🆕 新增 | 智能网格 (待完善) |
|
||||
| pattern_detector.rs | `desktop/src-tauri/src/intelligence/` | 🆕 新增 | 模式检测 (待完善) |
|
||||
| trigger_evaluator.rs | `desktop/src-tauri/src/intelligence/` | 🆕 新增 | 触发评估 (待完善) |
|
||||
|
||||
### 未来改进
|
||||
|
||||
1. **文件系统持久化** - 将身份文件写入 `~/.zclaw/agents/{agentId}/`
|
||||
2. **变更提案通知** - 添加桌面通知或消息提示
|
||||
3. **人格版本对比** - 可视化 diff 显示变更内容
|
||||
4. **多人格切换** - 支持同一 Agent 保存多套人格配置
|
||||
5. **智能网格集成** - 与 mesh.rs 集成实现多 Agent 协作演化
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
> **分类**: 智能层
|
||||
> **优先级**: P1 - 重要
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-24
|
||||
> **最后更新**: 2026-03-25
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
> **后端实现**: Rust (reflection.rs)
|
||||
> **后端实现**: Rust (reflection.rs) - **85% 完整度**
|
||||
> **新增组件**: pattern_detector.rs (待完善)
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
# 心跳巡检引擎 (Heartbeat Engine)
|
||||
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-24
|
||||
> **最后更新**: 2026-03-25
|
||||
> **负责人**: Intelligence Layer Team
|
||||
> **后端实现**: Rust (Phase 2 迁移完成)
|
||||
> **后端实现**: Rust (Phase 2 迁移完成) - **90% 完整度**
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
> **新增组件**: trigger_evaluator.rs (待完善)
|
||||
|
||||
## 概述
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
> **分类**: Skills 生态
|
||||
> **优先级**: P1 - 重要
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-24
|
||||
> **最后更新**: 2026-03-25
|
||||
|
||||
> ✅ **实现更新**: Skills 动态扫描已实现。Kernel 集成了 `SkillRegistry`,支持通过 Tauri 命令 `skill_list` 和 `skill_refresh` 动态发现所有 **69 个**技能。**新增 `execute_skill` 工具**,允许 Agent 在对话中直接调用技能。
|
||||
> ✅ **实现更新**: Skills 动态扫描已实现。Kernel 集成了 `SkillRegistry`,支持通过 Tauri 命令 `skill_list` 和 `skill_refresh` 动态发现所有 **78+** 技能。**新增 `execute_skill` 工具**,允许 Agent 在对话中直接调用技能。
|
||||
|
||||
---
|
||||
|
||||
@@ -21,9 +21,30 @@ Skills 系统是 ZCLAW 的核心扩展机制,通过 SKILL.md 文件定义 Agen
|
||||
| 优先级 | P1 |
|
||||
| 成熟度 | L4 |
|
||||
| 依赖 | SkillRegistry (Rust), SkillDiscoveryEngine (TypeScript) |
|
||||
| SKILL.md 文件 | **69** |
|
||||
| **动态发现技能** | **69 (100%)** |
|
||||
| SKILL.md 文件 | **78+** |
|
||||
| **动态发现技能** | **78+ (100%)** |
|
||||
| **execute_skill 工具** | **✅ 已实现** |
|
||||
| **Crate 完整度** | **80%** |
|
||||
|
||||
### 1.2 Crate 架构
|
||||
|
||||
```
|
||||
crates/zclaw-skills/
|
||||
├── src/
|
||||
│ ├── lib.rs # Crate 入口
|
||||
│ ├── registry.rs # SkillRegistry (HashMap)
|
||||
│ ├── loader.rs # SKILL.md 解析器
|
||||
│ ├── executor.rs # 技能执行器 (PromptOnly/Python/Shell)
|
||||
│ ├── orchestration.rs # 技能编排引擎
|
||||
│ ├── auto_compose.rs # 自动组合技能
|
||||
│ └── context.rs # Context 验证
|
||||
└── Cargo.toml
|
||||
|
||||
待实现:
|
||||
- WASM 模式执行器
|
||||
- Native 模式执行器
|
||||
- input_schema/output_schema 验证
|
||||
```
|
||||
|
||||
### 1.2 动态扫描实现
|
||||
|
||||
@@ -135,14 +156,19 @@ tools:
|
||||
|
||||
| 分类 | 技能数 | 代表技能 |
|
||||
|------|--------|---------|
|
||||
| 开发工程 | 15+ | ai-engineer, senior-developer, backend-architect |
|
||||
| 协调管理 | 8+ | agents-orchestrator, project-shepherd |
|
||||
| 测试质量 | 6+ | code-reviewer, reality-checker, evidence-collector |
|
||||
| 设计体验 | 8+ | ux-architect, brand-guardian, ui-designer |
|
||||
| 数据分析 | 5+ | analytics-reporter, performance-benchmarker |
|
||||
| 社媒营销 | 12+ | twitter-engager, xiaohongshu-specialist |
|
||||
| 中文平台 | 5+ | chinese-writing, feishu-docs, wechat-oa |
|
||||
| XR/空间 | 4+ | visionos-spatial-engineer, xr-immersive-dev |
|
||||
| 开发工程 | 18+ | ai-engineer, senior-developer, backend-architect, frontend-developer |
|
||||
| 协调管理 | 10+ | agents-orchestrator, project-shepherd, sprint-prioritizer |
|
||||
| 测试质量 | 8+ | code-reviewer, reality-checker, evidence-collector, api-tester |
|
||||
| 设计体验 | 10+ | ux-architect, brand-guardian, ui-designer, visual-storyteller |
|
||||
| 数据分析 | 6+ | analytics-reporter, performance-benchmarker, finance-tracker |
|
||||
| 社媒营销 | 15+ | twitter-engager, xiaohongshu-specialist, zhihu-strategist |
|
||||
| 中文平台 | 6+ | chinese-writing, feishu-docs, wechat-oa |
|
||||
| XR/空间 | 5+ | visionos-spatial-engineer, xr-immersive-dev, xr-interface-architect |
|
||||
| 基础工具 | 5+ | web-search, file-operations, shell-command, git |
|
||||
| 商务销售 | 4+ | sales-data-extraction-agent, report-distribution-agent |
|
||||
| 教育学习 | 3+ | classroom-generator, agentic-identity-trust |
|
||||
| 安全合规 | 3+ | security-engineer, legal-compliance-checker |
|
||||
| GSD 工作流 | 20+ | gsd:debug, gsd:plan-phase, gsd:execute-phase, gsd:verify-work |
|
||||
|
||||
### 3.2 发现引擎
|
||||
|
||||
@@ -255,76 +281,64 @@ const collaborationTriggers = [
|
||||
|
||||
### 5.1 已实现功能
|
||||
|
||||
- [x] 73 个 SKILL.md 技能定义
|
||||
- [x] 78+ SKILL.md 技能定义
|
||||
- [x] 标准化模板
|
||||
- [x] 发现引擎 (静态注册 12 个核心技能)
|
||||
- [x] 发现引擎 (动态扫描 78+ 技能)
|
||||
- [x] 触发词匹配
|
||||
- [x] 协作规则
|
||||
- [x] Playbooks 集成
|
||||
- [x] SkillMarket UI 组件
|
||||
- [x] **execute_skill 工具** (运行时调用技能)
|
||||
- [x] **技能分类系统** (11 分类,ID 模式匹配)
|
||||
- [x] **技能注入 system prompt** (自动将技能列表注入)
|
||||
- [x] **PromptOnly/Python/Shell 三种执行模式**
|
||||
|
||||
### 5.2 技能分类统计
|
||||
|
||||
| 分类 | 数量 | 代表技能 |
|
||||
|------|------|---------|
|
||||
| 开发工程 | 15 | frontend-developer, backend-architect, ai-engineer |
|
||||
| 测试/QA | 5 | code-review, api-tester, accessibility-auditor |
|
||||
| 设计/UX | 5 | ui-designer, ux-architect, visual-storyteller |
|
||||
| 安全 | 2 | security-engineer, legal-compliance-checker |
|
||||
| 数据分析 | 5 | data-analysis, analytics-reporter, evidence-collector |
|
||||
| 运维/DevOps | 4 | devops-automator, infrastructure-maintainer |
|
||||
| 管理/PM | 8 | senior-pm, project-shepherd, agents-orchestrator |
|
||||
| 营销/社媒 | 12 | twitter-engager, xiaohongshu-specialist, zhihu-strategist |
|
||||
| 内容/写作 | 4 | chinese-writing, translation, content-creator |
|
||||
| 研究 | 3 | trend-researcher, feedback-synthesizer |
|
||||
| 商务/销售 | 3 | sales-data-extraction-agent, report-distribution-agent |
|
||||
| 教育 | 2 | classroom-generator, agentic-identity-trust |
|
||||
| 核心工具 | 4 | git, file-operations, web-search, shell-command |
|
||||
| 开发工程 | 18 | frontend-developer, backend-architect, ai-engineer |
|
||||
| 测试/QA | 8 | code-reviewer, api-tester, accessibility-auditor |
|
||||
| 设计/UX | 10 | ui-designer, ux-architect, visual-storyteller |
|
||||
| 安全 | 3 | security-engineer, legal-compliance-checker |
|
||||
| 数据分析 | 6 | analytics-reporter, evidence-collector |
|
||||
| 运维/DevOps | 5 | devops-automator, infrastructure-maintainer |
|
||||
| 管理/PM | 10 | senior-pm, project-shepherd, agents-orchestrator |
|
||||
| 营销/社媒 | 15 | twitter-engager, xiaohongshu-specialist, zhihu-strategist |
|
||||
| 内容/写作 | 5 | chinese-writing, translation, content-creator |
|
||||
| 研究 | 4 | trend-researcher, feedback-synthesizer |
|
||||
| 商务/销售 | 4 | sales-data-extraction-agent, report-distribution-agent |
|
||||
| 教育 | 3 | classroom-generator, agentic-identity-trust |
|
||||
| 核心工具 | 5 | git, file-operations, web-search, shell-command |
|
||||
| GSD 工作流 | 20+ | gsd:debug, gsd:plan-phase, gsd:execute-phase |
|
||||
| XR/空间 | 5 | visionos-spatial-engineer, xr-immersive-dev |
|
||||
|
||||
### 5.3 实现说明
|
||||
### 5.3 Crate 实现状态
|
||||
|
||||
**✅ 已实现动态扫描 (2026-03-24)**:
|
||||
- Kernel 集成 `SkillRegistry`,启动时自动扫描 `skills/` 目录
|
||||
- 前端通过 Tauri 命令 `skill_list` 获取所有技能
|
||||
- 支持 `skill_refresh` 命令重新扫描指定目录
|
||||
- 73 个技能全部可被发现
|
||||
**zclaw-skills crate (80% 完整度)**:
|
||||
|
||||
**数据结构映射**:
|
||||
```typescript
|
||||
// 前端 SkillInfo (保留兼容)
|
||||
interface SkillInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
triggers: string[]; // 从 tags 映射
|
||||
capabilities: string[];
|
||||
toolDeps: string[]; // 后端暂无
|
||||
installed: boolean; // 从 enabled 映射
|
||||
category?: string; // 从 tags[0] 映射
|
||||
version?: string;
|
||||
mode?: string;
|
||||
}
|
||||
|
||||
// 后端 SkillManifest (Rust)
|
||||
struct SkillManifest {
|
||||
id: SkillId,
|
||||
name: String,
|
||||
description: String,
|
||||
version: String,
|
||||
mode: SkillMode,
|
||||
capabilities: Vec<String>,
|
||||
tags: Vec<String>,
|
||||
enabled: bool,
|
||||
}
|
||||
```
|
||||
| 功能 | 状态 | 说明 |
|
||||
|------|------|------|
|
||||
| SkillRegistry | ✅ | HashMap 存储,O(1) 查找 |
|
||||
| SKILL.md 解析 | ✅ | YAML frontmatter |
|
||||
| skill.toml 解析 | ✅ | 简化 TOML 解析器 |
|
||||
| PromptOnly 执行 | ✅ | 直接 prompt 注入 |
|
||||
| Python 执行 | ✅ | 子进程调用 |
|
||||
| Shell 执行 | ✅ | 子进程调用 |
|
||||
| 技能编排 | ✅ | orchestration.rs |
|
||||
| 自动组合 | ✅ | auto_compose.rs |
|
||||
| Context 验证 | ✅ | context.rs |
|
||||
| WASM 执行 | ❌ | 待实现 |
|
||||
| Native 执行 | ❌ | 待实现 |
|
||||
| Schema 验证 | ⚠️ | 解析但未验证 |
|
||||
|
||||
### 5.4 测试覆盖
|
||||
|
||||
- **单元测试**: 43 项 (swarm-skills.test.ts)
|
||||
- **单元测试**: 50+ 项 (swarm-skills.test.ts + executor.rs)
|
||||
- **集成测试**: 完整流程测试
|
||||
- **覆盖率**: ~90%
|
||||
|
||||
### 5.3 已知问题
|
||||
### 5.5 已知问题
|
||||
|
||||
| 问题 | 严重程度 | 状态 | 计划解决 |
|
||||
|------|---------|------|---------|
|
||||
@@ -340,16 +354,19 @@ struct SkillManifest {
|
||||
## 六、演化路线
|
||||
|
||||
### 6.1 短期计划(1-2 周)
|
||||
- [ ] 优化发现算法
|
||||
- [ ] 添加技能评分
|
||||
- [ ] 实现 WASM 执行模式
|
||||
- [ ] 实现 Native 执行模式
|
||||
- [ ] 添加 input_schema/output_schema 验证
|
||||
|
||||
### 6.2 中期计划(1-2 月)
|
||||
- [ ] 技能市场 UI
|
||||
- [ ] 用户自定义技能
|
||||
- [ ] 语义匹配优化
|
||||
|
||||
### 6.3 长期愿景
|
||||
- [ ] 技能共享社区
|
||||
- [ ] 技能认证体系
|
||||
- [ ] 技能版本控制
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
> **分类**: Hands 系统
|
||||
> **优先级**: P1 - 重要
|
||||
> **成熟度**: L4 - 生产
|
||||
> **最后更新**: 2026-03-24
|
||||
> **最后更新**: 2026-03-25
|
||||
> **验证状态**: ✅ 代码已验证
|
||||
|
||||
> ✅ **实现状态更新**: 11 个 Hands 中有 **9 个** 已有完整 Rust 后端实现。所有 9 个已实现 Hands 均已在 Kernel 中注册并可通过 `hand_execute` 命令调用。
|
||||
@@ -13,7 +13,9 @@
|
||||
|
||||
---
|
||||
|
||||
## 一、功能概述### 1.1 基本信息
|
||||
## 一、功能概述
|
||||
|
||||
### 1.1 基本信息
|
||||
|
||||
Hands 是 ZCLAW 的自主能力包系统,每个 Hand 封装了一类自动化任务,支持多种触发方式和审批流程。
|
||||
|
||||
@@ -21,13 +23,36 @@ Hands 是 ZCLAW 的自主能力包系统,每个 Hand 封装了一类自动化
|
||||
|------|-----|
|
||||
| 分类 | Hands 系统 |
|
||||
| 优先级 | P1 |
|
||||
| 成熟度 | L3 |
|
||||
| 成熟度 | L4 |
|
||||
| 依赖 | handStore, KernelClient, HandRegistry (Rust) |
|
||||
| Hand 配置数 | 11 |
|
||||
| **已实现后端** | **9 (82%)** |
|
||||
| **Kernel 注册** | **9/9 (100%)** |
|
||||
| **Crate 完整度** | **85%** |
|
||||
|
||||
### 1.2 实现状态
|
||||
### 1.2 Crate 架构
|
||||
|
||||
```
|
||||
crates/zclaw-hands/
|
||||
├── src/
|
||||
│ ├── lib.rs # Crate 入口
|
||||
│ ├── registry.rs # HandRegistry (RwLock HashMap)
|
||||
│ ├── trigger.rs # Trigger 管理
|
||||
│ └── hands/
|
||||
│ ├── mod.rs
|
||||
│ ├── browser.rs # ✅ Fantoccini WebDriver
|
||||
│ ├── slideshow.rs # ✅ 演示控制
|
||||
│ ├── speech.rs # ✅ 语音合成 (SSML)
|
||||
│ ├── quiz.rs # ✅ 问答生成
|
||||
│ ├── whiteboard.rs# ✅ 白板绘图
|
||||
│ ├── researcher.rs# ✅ 深度研究
|
||||
│ ├── collector.rs # ✅ 数据采集
|
||||
│ ├── clip.rs # ✅ 视频处理 (需 FFmpeg)
|
||||
│ └── twitter.rs # ✅ Twitter API (需 API Key)
|
||||
└── Cargo.toml
|
||||
```
|
||||
|
||||
### 1.3 实现状态
|
||||
|
||||
| Hand | 配置文件 | 后端实现 | Kernel 注册 | 可用性 | 代码位置 |
|
||||
|------|---------|---------|-------------|--------|---------|
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
# Pipeline DSL 系统
|
||||
|
||||
> **版本**: v0.3.0
|
||||
> **版本**: v0.4.0
|
||||
> **更新日期**: 2026-03-25
|
||||
> **状态**: ✅ 已实现
|
||||
> **状态**: ✅ 完整实现 (90% 完整度)
|
||||
> **架构**: Rust 后端 (zclaw-pipeline crate) + React 前端
|
||||
> **Crate 完整度**: **90%**
|
||||
|
||||
---
|
||||
|
||||
@@ -60,15 +61,30 @@ Pipeline DSL 是 ZCLAW 的自动化工作流编排系统,允许用户通过声
|
||||
|
||||
### 2.2 核心组件
|
||||
|
||||
| 组件 | 职责 | 位置 |
|
||||
|------|------|------|
|
||||
| PipelineParser | YAML 解析 | `crates/zclaw-pipeline/src/parser.rs` |
|
||||
| PipelineExecutor | 执行引擎 | `crates/zclaw-pipeline/src/executor.rs` |
|
||||
| ExecutionContext | 状态管理 | `crates/zclaw-pipeline/src/state.rs` |
|
||||
| ActionRegistry | 动作注册 | `crates/zclaw-pipeline/src/actions/mod.rs` |
|
||||
| PipelineClient | 前端客户端 | `desktop/src/lib/pipeline-client.ts` |
|
||||
| PipelinesPanel | UI 组件 | `desktop/src/components/PipelinesPanel.tsx` |
|
||||
| PipelineRecommender | 智能推荐 | `desktop/src/lib/pipeline-recommender.ts` |
|
||||
| 组件 | 职责 | 位置 | 实现状态 |
|
||||
|------|------|------|---------|
|
||||
| PipelineParser | YAML 解析 | `crates/zclaw-pipeline/src/parser.rs` | ✅ 100% |
|
||||
| PipelineExecutor | 执行引擎 | `crates/zclaw-pipeline/src/executor.rs` | ✅ 100% |
|
||||
| ExecutionContext | 状态管理 | `crates/zclaw-pipeline/src/state.rs` | ✅ 100% |
|
||||
| ActionRegistry | 动作注册 | `crates/zclaw-pipeline/src/actions/mod.rs` | ✅ 100% |
|
||||
| PipelineClient | 前端客户端 | `desktop/src/lib/pipeline-client.ts` | ✅ 95% |
|
||||
| PipelinesPanel | UI 组件 | `desktop/src/components/PipelinesPanel.tsx` | ✅ 90% |
|
||||
| PipelineRecommender | 智能推荐 | `desktop/src/lib/pipeline-recommender.ts` | ✅ 85% |
|
||||
| ClassroomPreviewer | 课堂预览 | `desktop/src/components/ClassroomPreviewer.tsx` | ✅ 90% |
|
||||
|
||||
### 2.3 Action 实现状态
|
||||
|
||||
| Action | 状态 | 说明 |
|
||||
|--------|------|------|
|
||||
| `llm_generate` | ✅ | LLM 生成 |
|
||||
| `parallel` | ✅ | 并行执行 |
|
||||
| `sequential` | ✅ | 顺序执行 |
|
||||
| `condition` | ✅ | 条件判断 |
|
||||
| `skill` | ✅ | 技能调用 |
|
||||
| `hand` | ✅ | Hand 调用 |
|
||||
| `classroom` | ✅ | 课堂生成 |
|
||||
| `export` | ✅ | 文件导出 |
|
||||
| `http` | ✅ | HTTP 请求 |
|
||||
|
||||
---
|
||||
|
||||
@@ -400,4 +416,5 @@ desktop/src/
|
||||
|
||||
| 日期 | 版本 | 变更内容 |
|
||||
|------|------|---------|
|
||||
| 2026-03-25 | v0.4.0 | 代码现状验证:90% 完整度,新增 Action 实现状态表 |
|
||||
| 2026-03-25 | v0.3.0 | Pipeline DSL 系统实现,包含 5 类 Pipeline 模板 |
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# ZCLAW 功能全景文档
|
||||
|
||||
> **版本**: v0.3.0
|
||||
> **版本**: v0.4.0
|
||||
> **更新日期**: 2026-03-25
|
||||
> **项目状态**: 内部 Kernel 架构,Streaming + MCP 协议,Pipeline DSL 系统
|
||||
> **架构**: Tauri 桌面应用,Rust 后端 + React 前端
|
||||
> **项目状态**: 完整 Rust Workspace 架构,8 个核心 Crates,78+ 技能,Pipeline DSL 系统
|
||||
> **架构**: Tauri 桌面应用,Rust Workspace (8 crates) + React 前端
|
||||
|
||||
> 📋 **重要**: ZCLAW 现已采用内部 Kernel 架构,所有核心能力集成在 Tauri 桌面应用中,无需外部进程
|
||||
> 📋 **重要**: ZCLAW 采用 Rust Workspace 架构,包含 8 个分层 Crates (types → memory → runtime → kernel → skills/hands/protocols/pipeline),所有核心能力集成在 Tauri 桌面应用中
|
||||
|
||||
---
|
||||
|
||||
@@ -57,16 +57,16 @@
|
||||
| 文档 | 功能 | 成熟度 | UI 集成 |
|
||||
|------|------|--------|---------|
|
||||
| [00-skill-system.md](04-skills-ecosystem/00-skill-system.md) | Skill 系统概述 | L4 | ✅ 通过 Tauri 命令 |
|
||||
| [01-builtin-skills.md](04-skills-ecosystem/01-builtin-skills.md) | 内置技能 (**69个** SKILL.md) | L4 | N/A |
|
||||
| [01-builtin-skills.md](04-skills-ecosystem/01-builtin-skills.md) | 内置技能 (**78+** SKILL.md) | L4 | N/A |
|
||||
| [02-skill-discovery.md](04-skills-ecosystem/02-skill-discovery.md) | 技能发现 (动态扫描) | **L4** | ✅ **已集成** |
|
||||
|
||||
> ✅ **更新**: Skills 动态扫描已实现。Kernel 集成 `SkillRegistry`,通过 Tauri 命令 `skill_list` 和 `skill_refresh` 动态发现所有 **69 个**技能。**新增 `execute_skill` 工具**,允许 Agent 在对话中直接调用技能。
|
||||
> ✅ **更新**: Skills 动态扫描已实现。Kernel 集成 `SkillRegistry`,通过 Tauri 命令 `skill_list` 和 `skill_refresh` 动态发现所有 **78+ 个**技能。**新增 `execute_skill` 工具**,允许 Agent 在对话中直接调用技能。
|
||||
|
||||
### 1.6 Hands 系统 - ✅ 9/11 已实现 (2026-03-24 更新)
|
||||
### 1.6 Hands 系统 - ✅ 9/11 已实现 (2026-03-25 更新)
|
||||
|
||||
| 文档 | 功能 | 成熟度 | 可用 Hands |
|
||||
|------|------|--------|-----------|
|
||||
| [00-hands-overview.md](05-hands-system/00-hands-overview.md) | Hands 概述 (11个) | L3 | **9/11 (82%)** |
|
||||
| [00-hands-overview.md](05-hands-system/00-hands-overview.md) | Hands 概述 (11个) | L4 | **9/11 (82%)** |
|
||||
|
||||
> ✅ **更新**: 9 个 Hands 已有完整 Rust 后端实现:
|
||||
> - ✅ **Browser** - Fantoccini WebDriver,支持 Chrome/Firefox
|
||||
@@ -207,23 +207,40 @@
|
||||
|
||||
| 指标 | 数值 |
|
||||
|------|------|
|
||||
| 功能模块总数 | 25+ |
|
||||
| SKILL.md 文件 | **69** |
|
||||
| 动态发现技能 | 69 (100%) |
|
||||
| **Rust Crates** | **8** (types, memory, runtime, kernel, skills, hands, protocols, pipeline) |
|
||||
| **SKILL.md 文件** | **78+** |
|
||||
| 动态发现技能 | 78+ (100%) |
|
||||
| Hands 总数 | 11 |
|
||||
| **已实现 Hands** | **9 (82%)** |
|
||||
| **Kernel 注册 Hands** | **9/9 (100%)** |
|
||||
| **Pipeline 模板** | **5** (教育/营销/法律/研究/生产力) |
|
||||
| **Pipeline 分类** | **5** 类 |
|
||||
| Zustand Store | 15+ |
|
||||
| Tauri 命令 | 100+ |
|
||||
| 代码行数 (前端) | ~25,000 |
|
||||
| 代码行数 (后端 Rust) | ~12,000 |
|
||||
| Zustand Store | **18+** |
|
||||
| Tauri 命令 | **80+** |
|
||||
| 代码行数 (前端) | ~30,000 |
|
||||
| 代码行数 (后端 Rust) | ~15,000 |
|
||||
| LLM Provider 支持 | **8** (Kimi, Qwen, DeepSeek, Zhipu, OpenAI, Anthropic, Gemini, Local/Ollama) |
|
||||
| 智能层组件 | 5 (Memory, Heartbeat, Reflection, Identity, Compaction) |
|
||||
| MCP 协议 | ✅ 已实现 |
|
||||
| 智能层组件 | **6** (Memory, Heartbeat, Reflection, Identity, Compaction, Mesh) |
|
||||
| MCP 协议 | ✅ 已实现 (stdio transport) |
|
||||
| execute_skill 工具 | ✅ 已实现 |
|
||||
| **Pipeline DSL** | ✅ **新增** |
|
||||
| **Pipeline DSL** | ✅ 完整实现 |
|
||||
| **内置工具** | **5** (file_read, file_write, shell_exec, web_fetch, execute_skill) |
|
||||
|
||||
### 5.1 Crate 依赖关系
|
||||
|
||||
```
|
||||
zclaw-types (L1: 基础类型, 无依赖) - 95% 完整度
|
||||
↑
|
||||
zclaw-memory (L2: 存储层, SQLite) - 90% 完整度
|
||||
↑
|
||||
zclaw-runtime (L3: 运行时, LLM 驱动, 工具执行) - 90% 完整度
|
||||
↑
|
||||
zclaw-kernel (L4: 核心协调, Agent 调度) - 85% 完整度
|
||||
↑
|
||||
┌───┴───┬───────┬───────────┬──────────┐
|
||||
│ │ │ │ │
|
||||
skills hands protocols pipeline channels
|
||||
(80%) (85%) (75%) (90%) (规划中)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -231,8 +248,9 @@
|
||||
|
||||
| 日期 | 版本 | 变更内容 |
|
||||
|------|------|---------|
|
||||
| 2026-03-25 | v0.4.0 | **代码现状深度分析**:8 个 Rust Crates 完整度评估,78+ 技能确认,18+ Store 状态管理,新增 Mesh/Persona 智能组件 |
|
||||
| 2026-03-25 | v0.3.0 | **Pipeline DSL 系统实现**,5 类 Pipeline 模板,Agent 智能推荐,结果预览组件 |
|
||||
| 2026-03-24 | v0.2.5 | **execute_skill 工具实现**,智能层完全实现验证,技能数更新为 69 |
|
||||
| 2026-03-24 | v0.2.5 | **execute_skill 工具实现**,智能层完全实现验证,技能数更新为 78+ |
|
||||
| 2026-03-24 | v0.2.4 | Hands Review: 修复 BrowserHand Kernel 注册问题,所有 9 个已实现 Hands 均可访问 |
|
||||
| 2026-03-24 | v0.2.3 | Hands 后端集成: 9/11 Hands 可用 (新增 Clip, Twitter) |
|
||||
| 2026-03-24 | v0.2.2 | Hands 后端集成: 7/11 Hands 可用 (新增 Researcher, Collector) |
|
||||
|
||||
@@ -1,34 +1,55 @@
|
||||
# ZCLAW 后续工作计划
|
||||
|
||||
> **版本**: v1.0
|
||||
> **版本**: v0.4.0
|
||||
> **创建日期**: 2026-03-16
|
||||
> **基于**: 功能全景分析和头脑风暴会议
|
||||
> **状态**: 待评审
|
||||
> **更新日期**: 2026-03-25
|
||||
> **基于**: 代码深度分析报告
|
||||
> **状态**: 活跃开发中
|
||||
|
||||
---
|
||||
|
||||
## 一、执行摘要
|
||||
|
||||
### 1.1 当前状态
|
||||
### 1.1 当前状态 (2026-03-25 代码分析)
|
||||
|
||||
| 指标 | 状态 |
|
||||
|------|------|
|
||||
| 功能完成度 | 95%+ |
|
||||
| 测试覆盖 | 317 tests passing |
|
||||
| Rust Crates | 8 个 (types, memory, runtime, kernel, skills, hands, protocols, pipeline) |
|
||||
| 功能完成度 | 85-95% (核心功能 L4) |
|
||||
| 技能数量 | 78+ SKILL.md |
|
||||
| Hands 可用 | 9/11 (82%) |
|
||||
| Pipeline DSL | ✅ 完整实现 |
|
||||
| 测试覆盖 | ~60% (需提升) |
|
||||
| 文档覆盖 | 25+ 功能文档 |
|
||||
| 成熟度 | L4 (生产就绪) |
|
||||
|
||||
### 1.2 核心结论
|
||||
### 1.2 Crate 完整度评估
|
||||
|
||||
| Crate | 层级 | 完整度 | 核心可用性 |
|
||||
|-------|------|--------|-----------|
|
||||
| zclaw-types | L1 | 95% | 完全可用 |
|
||||
| zclaw-memory | L2 | 90% | 完全可用 (SQLite) |
|
||||
| zclaw-runtime | L3 | 90% | 完全可用 (5 工具, 流式响应) |
|
||||
| zclaw-kernel | L4 | 85% | 基本可用 (Approval 存根) |
|
||||
| zclaw-skills | L5 | 80% | 可用 (WASM/Native 待实现) |
|
||||
| zclaw-hands | L5 | 85% | 可用 (9/11 Hands) |
|
||||
| zclaw-protocols | L5 | 75% | MCP 可用,A2A 待完善 |
|
||||
| zclaw-pipeline | L5 | 90% | 完全可用 |
|
||||
|
||||
### 1.3 核心结论
|
||||
|
||||
**优势**:
|
||||
- 8 层 Rust Workspace 架构清晰
|
||||
- Agent 记忆系统完善 (ICE: 630)
|
||||
- L4 自演化能力已实现
|
||||
- 多 Agent 协作框架成熟
|
||||
- 多 LLM Provider 支持 (8 个)
|
||||
- Pipeline DSL 成熟
|
||||
- 技能生态丰富 (78+)
|
||||
|
||||
**待改进**:
|
||||
- 用户引导和体验优化
|
||||
- 商业化路径不清晰
|
||||
- 社区生态尚未建立
|
||||
- Approval 管理是存根实现
|
||||
- A2A 协议需要更多工作
|
||||
- 测试覆盖率需要提升 (~60% → 80%)
|
||||
- 部分 Hand 需要外部依赖 (FFmpeg, Twitter API)
|
||||
|
||||
---
|
||||
|
||||
@@ -38,47 +59,60 @@
|
||||
|
||||
| ID | 任务 | 负责人 | 预估 | 验收标准 |
|
||||
|----|------|--------|------|---------|
|
||||
| S1 | 完善功能文档覆盖 | AI | 2h | 所有模块有文档 |
|
||||
| S2 | 添加用户反馈入口 | AI | 3h | 反馈可收集和追踪 |
|
||||
| S3 | 优化记忆检索性能 | AI | 4h | 检索延迟 <50ms |
|
||||
| S1 | 实现 Approval 管理后端 | Rust | 4h | 非存根实现,支持审批队列 |
|
||||
| S2 | 提升 A2A 协议完整度 | Rust | 4h | Agent 间通信可用 |
|
||||
| S3 | 增加测试覆盖率 | Rust/TS | 8h | 从 60% 提升到 75% |
|
||||
| S4 | 完善功能文档覆盖 | AI | 2h | 所有模块有文档 |
|
||||
|
||||
### 2.2 P1 - 应该完成
|
||||
|
||||
| ID | 任务 | 负责人 | 预估 | 验收标准 |
|
||||
|----|------|--------|------|---------|
|
||||
| S4 | 优化审批 UI | AI | 3h | 批量审批可用 |
|
||||
| S5 | 添加消息搜索功能 | AI | 4h | 支持关键词搜索 |
|
||||
| S6 | 优化错误提示 | AI | 2h | 错误有恢复建议 |
|
||||
| S5 | 优化审批 UI | TS | 3h | 批量审批可用 |
|
||||
| S6 | 添加消息搜索功能 | TS | 4h | 支持关键词搜索 |
|
||||
| S7 | 优化错误提示 | TS | 2h | 错误有恢复建议 |
|
||||
| S8 | 添加用户反馈入口 | TS | 3h | 反馈可收集和追踪 |
|
||||
|
||||
### 2.3 本周执行清单
|
||||
|
||||
```markdown
|
||||
- [ ] S1: 完善 00-architecture 剩余文档
|
||||
- [ ] S2: 在 RightPanel 添加反馈按钮
|
||||
- [ ] S3: 优化 agent-memory.ts 检索算法
|
||||
- [ ] S4: 实现批量审批组件
|
||||
- [ ] S5: 添加 ChatArea 搜索框
|
||||
- [ ] S6: 完善错误边界组件
|
||||
- [ ] S1: 实现 Kernel Approval 管理 (非存根)
|
||||
- [ ] S2: 完善 A2A 协议实现
|
||||
- [ ] S3: 增加单元测试 (目标 +15%)
|
||||
- [ ] S4: 更新功能文档基于代码分析
|
||||
- [ ] S5: 实现批量审批组件
|
||||
- [ ] S6: 添加 ChatArea 搜索框
|
||||
- [ ] S7: 完善错误边界组件
|
||||
- [ ] S8: 在 RightPanel 添加反馈按钮
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 三、中期计划 (1-2 月)
|
||||
|
||||
### 3.1 用户体验优化
|
||||
### 3.1 架构优化
|
||||
|
||||
| ID | 任务 | 价值 | 风险 | 优先级 |
|
||||
|----|------|------|------|--------|
|
||||
| M1 | 记忆图谱可视化 | 高 | 中 | P1 |
|
||||
| M2 | 主题系统扩展 | 中 | 低 | P2 |
|
||||
| M3 | 快捷键系统 | 中 | 低 | P2 |
|
||||
| M4 | 多语言支持 | 中 | 中 | P2 |
|
||||
| M1 | 完成 WASM/Native 技能模式 | 高 | 中 | P1 |
|
||||
| M2 | 实现 Predictor Hand | 中 | 低 | P2 |
|
||||
| M3 | 实现 Lead Hand | 中 | 低 | P2 |
|
||||
| M4 | 完善测试覆盖到 80% | 高 | 低 | P1 |
|
||||
|
||||
**M1 记忆图谱详细设计**:
|
||||
### 3.2 用户体验优化
|
||||
|
||||
| ID | 任务 | 价值 | 风险 | 优先级 |
|
||||
|----|------|------|------|--------|
|
||||
| M5 | 记忆图谱可视化 | 高 | 中 | P1 |
|
||||
| M6 | 技能市场 MVP | 高 | 中 | P1 |
|
||||
| M7 | 工作流编辑器增强 | 高 | 中 | P1 |
|
||||
| M8 | 主动学习引擎 | 高 | 高 | P1 |
|
||||
|
||||
**M5 记忆图谱详细设计**:
|
||||
|
||||
```
|
||||
技术方案:
|
||||
- D3.js / React Flow 可视化
|
||||
- React Flow 可视化
|
||||
- 力导向图布局
|
||||
- 节点类型: fact, preference, lesson, context, task
|
||||
- 边类型: 引用, 关联, 派生
|
||||
@@ -90,16 +124,7 @@
|
||||
- 搜索: 高亮匹配节点
|
||||
```
|
||||
|
||||
### 3.2 能力扩展
|
||||
|
||||
| ID | 任务 | 价值 | 风险 | 优先级 |
|
||||
|----|------|------|------|--------|
|
||||
| M5 | 技能市场 MVP | 高 | 中 | P1 |
|
||||
| M6 | 主动学习引擎 | 高 | 高 | P1 |
|
||||
| M7 | 更多 Hands (3+) | 中 | 低 | P2 |
|
||||
| M8 | 工作流编辑器 | 高 | 中 | P1 |
|
||||
|
||||
**M5 技能市场 MVP 范围**:
|
||||
**M6 技能市场 MVP 范围**:
|
||||
|
||||
```
|
||||
功能范围:
|
||||
@@ -121,6 +146,7 @@
|
||||
| M9 | 消息列表虚拟化 | 1000条流畅 | 100条流畅 | 10x |
|
||||
| M10 | 记忆索引优化 | <20ms | ~50ms | 2.5x |
|
||||
| M11 | 启动时间优化 | <2s | ~3s | 1.5x |
|
||||
| M12 | SQLite 查询优化 | <10ms | ~30ms | 3x |
|
||||
|
||||
---
|
||||
|
||||
@@ -130,42 +156,52 @@
|
||||
|
||||
| 方向 | 目标用户 | 核心价值 | 差异化 |
|
||||
|------|---------|---------|--------|
|
||||
| **个人版** | 个人开发者 | 效率提升 | 本地优先 + 记忆 |
|
||||
| **团队版** | 小团队 (5-20人) | 协作增强 | 多 Agent 协作 |
|
||||
| **企业版** | 中大型企业 | 安全合规 | 私有部署 + 审计 |
|
||||
| **个人版** | 个人开发者 | 效率提升 | 本地优先 + 记忆 + 78+ 技能 |
|
||||
| **团队版** | 小团队 (5-20人) | 协作增强 | 多 Agent 协作 + Pipeline DSL |
|
||||
| **企业版** | 中大型企业 | 安全合规 | 私有部署 + 审计 + A2A |
|
||||
|
||||
### 4.2 技术演进
|
||||
|
||||
| 阶段 | 重点 | 关键里程碑 |
|
||||
|------|------|-----------|
|
||||
| Q2 | 体验优化 | 记忆图谱、技能市场 |
|
||||
| Q3 | 能力扩展 | 主动学习、云同步 |
|
||||
| Q4 | 生态建设 | 社区、插件市场 |
|
||||
| Q2 | 稳定性 | 测试覆盖 80%,Approval 完善,A2A 完整 |
|
||||
| Q3 | 能力扩展 | WASM 技能,云同步,主动学习 |
|
||||
| Q4 | 生态建设 | 社区,插件市场,企业部署 |
|
||||
|
||||
### 4.3 商业化路径
|
||||
|
||||
```
|
||||
阶段 1: 开源建设 (Q2)
|
||||
阶段 1: 产品完善 (Q2)
|
||||
│
|
||||
├── 完善核心功能
|
||||
├── 提升测试覆盖
|
||||
└── 完善文档
|
||||
│
|
||||
▼
|
||||
阶段 2: 开源建设 (Q3)
|
||||
│
|
||||
├── 完善开源版本
|
||||
├── 建立社区
|
||||
└── 收集反馈
|
||||
│
|
||||
▼
|
||||
阶段 2: 增值服务 (Q3)
|
||||
阶段 3: 增值服务 (Q4)
|
||||
│
|
||||
├── 云同步服务 (订阅)
|
||||
├── 高级技能包 (付费)
|
||||
└── 技术支持 (企业)
|
||||
│
|
||||
▼
|
||||
阶段 3: 企业产品 (Q4)
|
||||
│
|
||||
├── 私有部署版本
|
||||
├── 企业级功能
|
||||
└── 专业服务
|
||||
```
|
||||
|
||||
### 4.4 待实现功能
|
||||
|
||||
| 功能 | 优先级 | 预计完成 |
|
||||
|------|--------|---------|
|
||||
| WASM/Native 技能模式 | P1 | Q3 |
|
||||
| 向量搜索集成 | P2 | Q3 |
|
||||
| 云同步服务 | P2 | Q4 |
|
||||
| 技能共享社区 | P3 | Q4 |
|
||||
| 企业部署版本 | P3 | Q4 |
|
||||
|
||||
---
|
||||
|
||||
## 五、关键决策
|
||||
@@ -287,6 +323,7 @@
|
||||
|
||||
| 日期 | 版本 | 变更内容 |
|
||||
|------|------|---------|
|
||||
| 2026-03-25 | v0.4.0 | 基于代码深度分析更新:8 Crates 评估,78+ 技能确认,测试覆盖现状 |
|
||||
| 2026-03-16 | v1.0 | 初始版本 |
|
||||
|
||||
---
|
||||
|
||||
353
plans/playful-humming-engelbart.md
Normal file
353
plans/playful-humming-engelbart.md
Normal file
@@ -0,0 +1,353 @@
|
||||
# ZCLAW 后端功能前端集成审查方案
|
||||
|
||||
## 背景
|
||||
|
||||
用户反馈在 Tauri 端没有看到一些已开发的功能。本方案旨在系统性地审查所有后端已实现的功能是否正确集成到前端 UI。
|
||||
|
||||
---
|
||||
|
||||
## 1. 审查发现总结
|
||||
|
||||
### 1.1 后端命令统计
|
||||
|
||||
| 模块 | 命令数量 | 前端集成状态 |
|
||||
|------|---------|-------------|
|
||||
| Kernel 核心 | 20 | ✅ 完整 |
|
||||
| Pipeline 工作流 | 8 | ✅ 完整 |
|
||||
| OpenFang/Gateway | 16 | ⚠️ 部分 |
|
||||
| OpenViking CLI | 9 | ❌ 无 |
|
||||
| OpenViking Server | 4 | ❌ 无 |
|
||||
| Memory 记忆 | 10 | ✅ 完整 |
|
||||
| Browser 自动化 | 20 | ✅ 完整 |
|
||||
| Secure Storage | 4 | ❌ 无 |
|
||||
| Heartbeat Engine | 9 | ✅ 完整 |
|
||||
| Context Compactor | 4 | ✅ 完整 |
|
||||
| Reflection Engine | 6 | ✅ 完整 |
|
||||
| Identity Manager | 14 | ✅ 完整 |
|
||||
| Adaptive Mesh | 8 | ⚠️ 部分 |
|
||||
| **总计** | **135** | - |
|
||||
|
||||
### 1.2 已识别的集成缺口
|
||||
|
||||
#### 完全缺失前端入口
|
||||
|
||||
| 功能 | 后端实现位置 | 影响等级 |
|
||||
|------|-------------|---------|
|
||||
| **Channels 通道** | `crates/zclaw-channels/` | P1 - 用户无法配置 Discord/Slack/Telegram |
|
||||
| **OpenViking CLI** | `viking_commands.rs` | P2 - 语义搜索功能不可用 |
|
||||
| **Secure Storage** | `secure_storage.rs` | P2 - 密钥管理无 UI |
|
||||
| **Memory Extraction** | `memory/extractor.rs` | P3 - 自动记忆提取未暴露 |
|
||||
| **LLM Complete** | `llm/mod.rs` | P3 - 独立 LLM 调用无入口 |
|
||||
|
||||
#### 部分集成需完善
|
||||
|
||||
| 功能 | 当前状态 | 缺失部分 |
|
||||
|------|---------|---------|
|
||||
| **Triggers 触发器** | Store 存在,UI 不完整 | 编辑、测试触发器 UI |
|
||||
| **Approvals 审批** | Store 存在,UI 不完整 | 批量操作、历史记录 |
|
||||
| **Adaptive Mesh** | Store 存在,无专用 UI | 模式可视化、推荐展示 |
|
||||
| **Persona Evolver** | Store 存在,无 UI | 人格演化展示 |
|
||||
|
||||
---
|
||||
|
||||
## 2. 详细审查清单
|
||||
|
||||
### 2.1 核心功能 (P0) - 必须可用
|
||||
|
||||
```markdown
|
||||
聊天系统
|
||||
- [ ] agent_chat - 发送消息
|
||||
- [ ] agent_chat_stream - 流式响应
|
||||
- [ ] 消息列表正常显示
|
||||
- [ ] 多模型切换生效
|
||||
|
||||
Agent 管理
|
||||
- [ ] agent_create - 创建分身
|
||||
- [ ] agent_list - 列出分身
|
||||
- [ ] agent_get - 获取详情
|
||||
- [ ] agent_delete - 删除分身
|
||||
- [ ] 分身配置持久化
|
||||
|
||||
技能系统
|
||||
- [ ] skill_list - 技能列表
|
||||
- [ ] skill_refresh - 刷新技能
|
||||
- [ ] skill_execute - 执行技能
|
||||
- [ ] 技能执行结果显示
|
||||
|
||||
Hands 系统
|
||||
- [ ] hand_list - Hands 列表
|
||||
- [ ] hand_execute - 执行 Hand
|
||||
- [ ] Hand 参数配置
|
||||
- [ ] 执行状态反馈
|
||||
```
|
||||
|
||||
### 2.2 重要功能 (P1) - 影响体验
|
||||
|
||||
```markdown
|
||||
Pipeline 工作流
|
||||
- [ ] pipeline_list - 发现 Pipeline
|
||||
- [ ] pipeline_run - 执行 Pipeline
|
||||
- [ ] pipeline_progress - 进度显示
|
||||
- [ ] pipeline_result - 结果展示
|
||||
- [ ] pipeline_cancel - 取消执行
|
||||
|
||||
Memory 记忆
|
||||
- [ ] memory_store - 存储记忆
|
||||
- [ ] memory_search - 搜索记忆
|
||||
- [ ] memory_stats - 统计信息
|
||||
- [ ] 记忆在聊天中自动使用
|
||||
|
||||
Identity 身份
|
||||
- [ ] identity_get - 获取身份
|
||||
- [ ] identity_build_prompt - 构建提示
|
||||
- [ ] identity_propose_change - 提案变更
|
||||
- [ ] identity_approve/reject - 审批
|
||||
|
||||
Browser 自动化
|
||||
- [ ] browser_create_session - 创建会话
|
||||
- [ ] browser_navigate - 导航
|
||||
- [ ] browser_click/type - 交互
|
||||
- [ ] browser_screenshot - 截图
|
||||
- [ ] browser_scrape_page - 抓取
|
||||
|
||||
Triggers 触发器
|
||||
- [ ] trigger_list - 列表
|
||||
- [ ] trigger_create - 创建
|
||||
- [ ] trigger_update - 更新
|
||||
- [ ] trigger_delete - 删除
|
||||
- [ ] trigger_execute - 手动执行
|
||||
|
||||
Approvals 审批
|
||||
- [ ] approval_list - 待审批列表
|
||||
- [ ] approval_respond - 响应审批
|
||||
```
|
||||
|
||||
### 2.3 缺失功能 (需新增 UI)
|
||||
|
||||
```markdown
|
||||
Channels 通道管理 (新增)
|
||||
- [ ] Discord 配置界面
|
||||
- [ ] Slack 配置界面
|
||||
- [ ] Telegram 配置界面
|
||||
- [ ] 通道状态显示
|
||||
- [ ] 消息桥接测试
|
||||
|
||||
OpenViking CLI (新增)
|
||||
- [ ] viking_add - 添加资源
|
||||
- [ ] viking_find - 语义搜索
|
||||
- [ ] viking_grep - 模式搜索
|
||||
- [ ] viking_ls/tree - 资源浏览
|
||||
|
||||
Secure Storage (新增)
|
||||
- [ ] 密钥列表
|
||||
- [ ] 添加/删除密钥
|
||||
- [ ] 安全存储可用性检查
|
||||
|
||||
Adaptive Mesh (完善)
|
||||
- [ ] 模式检测可视化
|
||||
- [ ] 推荐展示面板
|
||||
- [ ] 活动记录查看
|
||||
|
||||
Persona Evolver (完善)
|
||||
- [ ] 人格演化历史
|
||||
- [ ] 人格调整界面
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. 验证方法
|
||||
|
||||
### 3.1 代码层面验证
|
||||
|
||||
```bash
|
||||
# 检查前端客户端是否调用所有后端命令
|
||||
cd desktop/src/lib
|
||||
grep -r "invoke(" *.ts | grep -oP "'[^']+'" | sort -u
|
||||
|
||||
# 对比后端暴露的命令
|
||||
cd src-tauri/src
|
||||
grep -r "#\[tauri::command\]" *.rs -A 5 | grep "pub async fn" | awk '{print $3}'
|
||||
```
|
||||
|
||||
### 3.2 功能测试验证
|
||||
|
||||
```bash
|
||||
# 启动开发环境
|
||||
pnpm start:dev
|
||||
|
||||
# 检查清单
|
||||
1. 打开每个面板,确认无报错
|
||||
2. 测试每个按钮和交互
|
||||
3. 检查浏览器控制台错误
|
||||
4. 验证数据持久化
|
||||
```
|
||||
|
||||
### 3.3 集成测试脚本
|
||||
|
||||
创建 `tests/integration/api-coverage.test.ts`:
|
||||
- 自动扫描后端命令
|
||||
- 检查前端是否有对应调用
|
||||
- 生成覆盖率报告
|
||||
|
||||
---
|
||||
|
||||
## 4. 实施计划
|
||||
|
||||
### Phase 1: 完善现有功能 (优先级最高)
|
||||
|
||||
**目标**: 让已实现的后端功能在前端完全可用
|
||||
|
||||
| 任务 | 涉及文件 | 预估工时 |
|
||||
|------|---------|---------|
|
||||
| 完善 TriggersPanel | `TriggersPanel.tsx`, `CreateTriggerModal.tsx` | 4h |
|
||||
| 完善 ApprovalsPanel | `ApprovalsPanel.tsx`, `ApprovalQueue.tsx` | 3h |
|
||||
| 连接 Mesh 推荐 | `WorkflowRecommendations.tsx`, `meshStore.ts` | 4h |
|
||||
| 完善 Persona Evolver | `personaStore.ts`, 新增 UI | 4h |
|
||||
|
||||
### Phase 2: 添加缺失的 UI 入口
|
||||
|
||||
**目标**: 为无 UI 的后端功能创建管理界面
|
||||
|
||||
| 任务 | 涉及文件 | 预估工时 |
|
||||
|------|---------|---------|
|
||||
| 创建 ChannelsPanel | 新增 `ChannelsPanel.tsx` | 6h |
|
||||
| 创建 VikingPanel | 新增 `VikingPanel.tsx` | 5h |
|
||||
| 创建 SecureStoragePanel | 新增 `SecureStoragePanel.tsx` | 3h |
|
||||
| 集成到设置页面 | `Settings/` 目录 | 2h |
|
||||
|
||||
### Phase 3: 优化用户体验
|
||||
|
||||
| 任务 | 涉及文件 | 预估工时 |
|
||||
|------|---------|---------|
|
||||
| 添加功能引导 | `use-onboarding.ts` | 3h |
|
||||
| 完善错误提示 | `ErrorNotification.tsx` | 2h |
|
||||
| 添加操作审计 | `AuditLogsPanel.tsx` | 3h |
|
||||
| 更新文档 | `docs/features/` | 2h |
|
||||
|
||||
---
|
||||
|
||||
## 5. 关键文件路径
|
||||
|
||||
### 后端命令注册
|
||||
- `g:\ZClaw_openfang\desktop\src-tauri\src\lib.rs` - 命令汇总入口
|
||||
- `g:\ZClaw_openfang\desktop\src-tauri\src\kernel_commands.rs` - 核心 API
|
||||
- `g:\ZClaw_openfang\desktop\src-tauri\src\viking_commands.rs` - OpenViking CLI
|
||||
- `g:\ZClaw_openfang\desktop\src-tauri\src\secure_storage.rs` - 安全存储
|
||||
|
||||
### 前端客户端层
|
||||
- `g:\ZClaw_openfang\desktop\src\lib\kernel-client.ts` - Kernel 客户端
|
||||
- `g:\ZClaw_openfang\desktop\src\lib\intelligence-client.ts` - Intelligence 客户端
|
||||
- `g:\ZClaw_openfang\desktop\src\lib\browser-client.ts` - 浏览器客户端
|
||||
|
||||
### 前端状态管理
|
||||
- `g:\ZClaw_openfang\desktop\src\store\handStore.ts` - Hands 状态
|
||||
- `g:\ZClaw_openfang\desktop\src\store\meshStore.ts` - Mesh 状态
|
||||
- `g:\ZClaw_openfang\desktop\src\store\personaStore.ts` - 人格状态
|
||||
|
||||
### 前端组件
|
||||
- `g:\ZClaw_openfang\desktop\src\components\TriggersPanel.tsx`
|
||||
- `g:\ZClaw_openfang\desktop\src\components\ApprovalsPanel.tsx`
|
||||
- `g:\ZClaw_openfang\desktop\src\components\WorkflowRecommendations.tsx`
|
||||
|
||||
---
|
||||
|
||||
## 6. 预期成果
|
||||
|
||||
1. **完整的功能清单文档** - 列出所有后端功能及其前端集成状态
|
||||
2. **可执行的检查清单** - 用于验证每个功能是否正常工作
|
||||
3. **缺失功能 UI** - 为无 UI 的功能创建管理界面
|
||||
4. **完善的现有功能** - 修复部分集成的功能
|
||||
5. **更新的文档** - 反映当前系统的完整能力
|
||||
|
||||
---
|
||||
|
||||
## 7. 审查结果 - 功能集成覆盖率
|
||||
|
||||
### 7.1 完整集成的功能 ✅
|
||||
|
||||
| 功能模块 | 后端命令 | 前端客户端 | UI 组件 | Store |
|
||||
|---------|---------|-----------|---------|-------|
|
||||
| 聊天对话 | agent_chat, agent_chat_stream | kernel-client.ts | ChatArea.tsx | chatStore.ts |
|
||||
| Agent 管理 | agent_* | kernel-client.ts | CloneManager.tsx | agentStore.ts |
|
||||
| 技能系统 | skill_* | kernel-client.ts | SkillMarket.tsx | skillMarketStore.ts |
|
||||
| Hands 系统 | hand_* | kernel-client.ts | HandsPanel.tsx | handStore.ts |
|
||||
| Pipeline | pipeline_* | pipeline-client.ts | PipelinesPanel.tsx | workflowStore.ts |
|
||||
| Browser | browser_* | browser-client.ts | BrowserHand/* | browserHandStore.ts |
|
||||
| Memory | memory_* | intelligence-client.ts | MemoryPanel.tsx | - |
|
||||
| Heartbeat | heartbeat_* | intelligence-client.ts | HeartbeatConfig.tsx | - |
|
||||
| Compactor | compactor_* | intelligence-client.ts | (自动) | - |
|
||||
| Reflection | reflection_* | intelligence-client.ts | ReflectionLog.tsx | - |
|
||||
| Identity | identity_* | intelligence-client.ts | IdentityChangeProposal.tsx | - |
|
||||
| Triggers | trigger_* | kernel-client.ts | TriggersPanel.tsx | handStore.ts |
|
||||
| Approvals | approval_* | kernel-client.ts | ApprovalsPanel.tsx | handStore.ts |
|
||||
| Mesh 推荐 | mesh_* | intelligence-client.ts | WorkflowRecommendations.tsx | meshStore.ts |
|
||||
|
||||
### 7.2 部分集成的功能 ⚠️
|
||||
|
||||
| 功能模块 | 问题 | 缺失部分 |
|
||||
|---------|------|---------|
|
||||
| **Persona Evolver** | 后端命令可能不存在 | `persona_evolve` 命令在 Tauri 未注册 |
|
||||
| **Channels** | 只读展示,无配置入口 | 无法配置 Discord/Slack/Telegram |
|
||||
| **Secure Storage** | 无专用 UI 入口 | 仅用于 API Key 存储,无管理界面 |
|
||||
| **OpenFang 进程** | 无前端客户端 | openfang_process_* 命令无 UI |
|
||||
|
||||
### 7.3 完全缺失前端的功能 ❌ (已部分完成)
|
||||
|
||||
| 功能模块 | 后端实现 | 前端状态 |
|
||||
|---------|---------|---------|
|
||||
| **OpenViking CLI** | viking_* (9命令) | ✅ 已创建 [viking-client.ts](desktop/src/lib/viking-client.ts) 和 [VikingPanel.tsx](desktop/src/components/VikingPanel.tsx) |
|
||||
| **OpenViking Server** | viking_server_* (4命令) | ✅ 已集成到 viking-client.ts |
|
||||
| **Memory Extraction** | extract_session_memories | P3 - 无前端调用 |
|
||||
| **LLM Complete** | llm_complete | P3 - 无专用入口 |
|
||||
|
||||
### 7.4 集成覆盖率统计
|
||||
|
||||
| 状态 | 模块数 | 百分比 |
|
||||
|------|--------|--------|
|
||||
| ✅ 完整集成 | 14 | 67% |
|
||||
| ⚠️ 部分集成 | 4 | 19% |
|
||||
| ❌ 无前端 | 4 | 14% |
|
||||
| **总计** | 22 | 100% |
|
||||
|
||||
---
|
||||
|
||||
## 8. 优先修复建议
|
||||
|
||||
### P0 - ✅ 已完成
|
||||
|
||||
1. **Persona Evolver** - 在 [lib.rs](desktop/src-tauri/src/lib.rs) 添加状态初始化和命令注册
|
||||
- 緻加 7 个命令: `persona_evolver_init`, `persona_evolve`, `persona_evolution_history`, `persona_evolver_state`, `persona_evolver_config`, `persona_evolver_update_config`, `persona_apply_proposal`
|
||||
- 緻加状态管理: `.manage(persona_evolver_state)`
|
||||
|
||||
### P1 - ✅ 已完成
|
||||
|
||||
2. **Channels 配置 UI** - 完整重写 [IMChannels.tsx](desktop/src/components/Settings/IMChannels.tsx)
|
||||
- 添加 `ChannelConfigModal` 组件
|
||||
- 添加配置 Discord/Slack/Telegram/Feishu/QQ/WeChat
|
||||
- 更新 [configStore.ts](desktop/src/store/configStore.ts) 添加 `name`/`config` 字段
|
||||
|
||||
- 集成到设置页面导航
|
||||
|
||||
3. **Secure Storage 管理** - 创建 [SecureStorage.tsx](desktop/src/components/Settings/SecureStorage.tsx)
|
||||
- OS Keyring/Keychain 管理界面
|
||||
- 查看/添加/删除/显示密钥值
|
||||
- 检测 Keyring 可用性
|
||||
- 集成到设置页面导航
|
||||
|
||||
### P2 - ✅ 已完成
|
||||
|
||||
4. **OpenViking UI** - 创建客户端和 UI 面板
|
||||
- 创建 [viking-client.ts](desktop/src/lib/viking-client.ts) - API 客户端
|
||||
- 创建 [VikingPanel.tsx](desktop/src/components/VikingPanel.tsx) - 语义搜索 UI
|
||||
- 服务器状态控制、 语义搜索功能
|
||||
- 集成到设置页面导航
|
||||
|
||||
---
|
||||
|
||||
## 9. 风险与注意事项
|
||||
|
||||
1. **Breaking Changes**: 修改现有 Store 可能影响其他组件
|
||||
2. **测试覆盖**: 新增 UI 需要添加对应的测试
|
||||
3. **向后兼容**: 保持现有 API 不变
|
||||
4. **性能影响**: 新增功能不应影响核心聊天体验
|
||||
5. **Persona Evolver**: 需要先确认后端命令是否已实现
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user