daa-ai 0.2.1

AI integration layer for DAA system with MCP protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! # DAA AI
//!
//! AI integration layer for the Decentralized Autonomous Agents (DAA) system.
//! Provides Claude AI integration via QuDAG MCP (Model Context Protocol) for 
//! intelligent decision making and task automation.

mod qudag_stubs;

use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

// Re-export QuDAG MCP types
pub use crate::qudag_stubs::qudag_mcp::{MCPClient, MCPMessage, MCPError, Tool, ToolCall, ToolResult};

pub mod claude;
pub mod agents;
pub mod tools;
pub mod tasks;
pub mod memory;

#[cfg(feature = "rules-integration")]
pub mod rules_integration;

#[cfg(feature = "database")]
pub mod database;

/// AI system error types
#[derive(Error, Debug)]
pub enum AIError {
    #[error("MCP error: {0}")]
    MCP(#[from] MCPError),
    
    #[error("Claude API error: {0}")]
    Claude(String),
    
    #[error("Agent not found: {0}")]
    AgentNotFound(String),
    
    #[error("Task execution error: {0}")]
    TaskExecution(String),
    
    #[error("Tool error: {0}")]
    Tool(String),
    
    #[error("Memory error: {0}")]
    Memory(String),
    
    #[error("Configuration error: {0}")]
    Configuration(String),
    
    #[error("Network error: {0}")]
    Network(#[from] reqwest::Error),
    
    #[error("Serialization error: {0}")]
    Serialization(#[from] serde_json::Error),
}

pub type Result<T> = std::result::Result<T, AIError>;

/// Configuration for the DAA AI system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AIConfig {
    /// Claude API configuration
    pub claude: claude::ClaudeConfig,
    
    /// MCP client configuration
    pub mcp: MCPClientConfig,
    
    /// Agent configuration
    pub agents: AgentConfig,
    
    /// Memory configuration
    pub memory: MemoryConfig,
    
    /// Database configuration
    #[cfg(feature = "database")]
    pub database_url: Option<String>,
}

impl Default for AIConfig {
    fn default() -> Self {
        Self {
            claude: claude::ClaudeConfig::default(),
            mcp: MCPClientConfig::default(),
            agents: AgentConfig::default(),
            memory: MemoryConfig::default(),
            #[cfg(feature = "database")]
            database_url: None,
        }
    }
}

/// MCP client configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MCPClientConfig {
    /// MCP server endpoint
    pub server_url: String,
    
    /// Connection timeout in seconds
    pub timeout: u64,
    
    /// Maximum concurrent connections
    pub max_connections: usize,
    
    /// Retry configuration
    pub retry_attempts: u32,
    
    /// Tool registry
    pub available_tools: Vec<String>,
}

impl Default for MCPClientConfig {
    fn default() -> Self {
        Self {
            server_url: "http://localhost:3000".to_string(),
            timeout: 30,
            max_connections: 10,
            retry_attempts: 3,
            available_tools: vec![
                "code_execution".to_string(),
                "file_operations".to_string(),
                "web_search".to_string(),
                "data_analysis".to_string(),
            ],
        }
    }
}

/// Agent configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentConfig {
    /// Maximum number of agents
    pub max_agents: usize,
    
    /// Default agent capabilities
    pub default_capabilities: Vec<String>,
    
    /// Agent spawn configuration
    pub spawn_config: SpawnConfig,
}

impl Default for AgentConfig {
    fn default() -> Self {
        Self {
            max_agents: 100,
            default_capabilities: vec![
                "reasoning".to_string(),
                "code_generation".to_string(),
                "data_analysis".to_string(),
                "communication".to_string(),
            ],
            spawn_config: SpawnConfig::default(),
        }
    }
}

/// Agent spawn configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpawnConfig {
    /// Default model to use for new agents
    pub default_model: String,
    
    /// Default system prompt
    pub default_system_prompt: String,
    
    /// Default temperature for responses
    pub default_temperature: f32,
    
    /// Maximum tokens per response
    pub max_tokens: u32,
}

impl Default for SpawnConfig {
    fn default() -> Self {
        Self {
            default_model: "claude-3-opus-20240229".to_string(),
            default_system_prompt: "You are a helpful AI agent in the DAA system. You can execute tasks, analyze data, and collaborate with other agents.".to_string(),
            default_temperature: 0.7,
            max_tokens: 4096,
        }
    }
}

/// Memory configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    /// Maximum memory entries per agent
    pub max_entries_per_agent: usize,
    
    /// Memory retention period in hours
    pub retention_hours: u64,
    
    /// Enable persistent memory
    pub persistent: bool,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            max_entries_per_agent: 1000,
            retention_hours: 24 * 7, // 1 week
            persistent: true,
        }
    }
}

/// Main AI system coordinating all AI operations
pub struct AISystem {
    /// System configuration
    config: AIConfig,
    
    /// MCP client for Claude integration
    mcp_client: MCPClient,
    
    /// Claude API client
    claude_client: claude::ClaudeClient,
    
    /// Agent manager
    agent_manager: agents::AgentManager,
    
    /// Task manager
    task_manager: tasks::TaskManager,
    
    /// Tool registry
    tool_registry: tools::ToolRegistry,
    
    /// Memory system
    memory: memory::MemorySystem,
    
    /// Database connection
    #[cfg(feature = "database")]
    database: Option<database::DatabaseManager>,
}

impl AISystem {
    /// Create a new AI system
    pub async fn new(config: AIConfig) -> Result<Self> {
        // Initialize MCP client
        let mcp_client = MCPClient::new(&config.mcp.server_url).await
            .map_err(AIError::MCP)?;
        
        // Initialize Claude client
        let claude_client = claude::ClaudeClient::new(config.claude.clone()).await?;
        
        // Initialize managers
        let agent_manager = agents::AgentManager::new(config.agents.clone());
        let task_manager = tasks::TaskManager::new();
        let tool_registry = tools::ToolRegistry::new();
        let memory = memory::MemorySystem::new(config.memory.clone());
        
        // Initialize database if enabled
        #[cfg(feature = "database")]
        let database = if let Some(db_url) = &config.database_url {
            Some(database::DatabaseManager::new(db_url).await?)
        } else {
            None
        };

        Ok(Self {
            config,
            mcp_client,
            claude_client,
            agent_manager,
            task_manager,
            tool_registry,
            memory,
            #[cfg(feature = "database")]
            database,
        })
    }

    /// Initialize the AI system
    pub async fn initialize(&mut self) -> Result<()> {
        tracing::info!("Initializing DAA AI System");
        
        // Initialize MCP connection
        self.mcp_client.connect().await.map_err(AIError::MCP)?;
        
        // Register default tools
        self.register_default_tools().await?;
        
        // Initialize memory system
        self.memory.initialize().await?;
        
        // Initialize database if enabled
        #[cfg(feature = "database")]
        if let Some(db) = &mut self.database {
            db.initialize().await?;
        }
        
        tracing::info!("DAA AI System initialized successfully");
        Ok(())
    }

    /// Spawn a new AI agent
    pub async fn spawn_agent(
        &mut self,
        agent_type: agents::AgentType,
        capabilities: Option<Vec<String>>,
        custom_config: Option<HashMap<String, String>>,
    ) -> Result<String> {
        let agent = self.agent_manager.spawn_agent(
            agent_type,
            capabilities.unwrap_or_else(|| self.config.agents.default_capabilities.clone()),
            custom_config,
        ).await?;
        
        // Store agent in memory
        self.memory.store_agent_metadata(&agent.id, &agent).await?;
        
        tracing::info!("Spawned new agent: {} ({})", agent.id, agent.agent_type);
        Ok(agent.id)
    }

    /// Execute a task with an agent
    pub async fn execute_task(
        &mut self,
        agent_id: &str,
        task: tasks::Task,
    ) -> Result<tasks::TaskResult> {
        // Get agent
        let agent = self.agent_manager.get_agent(agent_id).await?;
        
        // Execute task through Claude
        let result = self.claude_client.execute_task(&agent, &task).await?;
        
        // Store task result in memory
        self.memory.store_task_result(agent_id, &task.id, &result).await?;
        
        // Record in database if enabled
        #[cfg(feature = "database")]
        if let Some(db) = &mut self.database {
            db.record_task_execution(agent_id, &task, &result).await?;
        }
        
        tracing::info!("Task {} executed by agent {}", task.id, agent_id);
        Ok(result)
    }

    /// Use a tool via MCP
    pub async fn use_tool(
        &mut self,
        agent_id: &str,
        tool_name: &str,
        parameters: HashMap<String, serde_json::Value>,
    ) -> Result<ToolResult> {
        // Create tool call
        let tool_call = ToolCall {
            id: Uuid::new_v4().to_string(),
            name: tool_name.to_string(),
            parameters,
        };
        
        // Execute via MCP
        let result = self.mcp_client.call_tool(tool_call).await
            .map_err(AIError::MCP)?;
        
        // Store in memory
        self.memory.store_tool_usage(agent_id, tool_name, &result).await?;
        
        tracing::info!("Tool {} used by agent {}", tool_name, agent_id);
        Ok(result)
    }

    /// Get agent memory
    pub async fn get_agent_memory(&self, agent_id: &str) -> Result<Vec<memory::MemoryEntry>> {
        self.memory.get_agent_memory(agent_id).await
    }

    /// Store information in agent memory
    pub async fn store_memory(
        &mut self,
        agent_id: &str,
        key: String,
        data: serde_json::Value,
        metadata: Option<HashMap<String, String>>,
    ) -> Result<()> {
        self.memory.store(agent_id, key, data, metadata).await
    }

    /// Get system statistics
    pub async fn get_statistics(&self) -> AIStatistics {
        AIStatistics {
            total_agents: self.agent_manager.get_agent_count().await,
            active_tasks: self.task_manager.get_active_task_count().await,
            total_tools: self.tool_registry.get_tool_count().await,
            memory_entries: self.memory.get_total_entries().await,
            uptime_seconds: 0, // Would be calculated from start time
        }
    }

    /// Register default tools
    async fn register_default_tools(&mut self) -> Result<()> {
        // Register built-in tools
        for tool_name in &self.config.mcp.available_tools {
            let tool = tools::create_default_tool(tool_name)?;
            self.tool_registry.register_tool(tool).await?;
        }
        
        Ok(())
    }
}

/// AI system statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AIStatistics {
    /// Total number of spawned agents
    pub total_agents: u64,
    
    /// Number of active tasks
    pub active_tasks: u64,
    
    /// Total available tools
    pub total_tools: u64,
    
    /// Memory entries count
    pub memory_entries: u64,
    
    /// System uptime in seconds
    pub uptime_seconds: u64,
}

impl std::fmt::Display for AIStatistics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "AI Stats: Agents={}, Active Tasks={}, Tools={}, Memory={}, Uptime={}s",
            self.total_agents,
            self.active_tasks,
            self.total_tools,
            self.memory_entries,
            self.uptime_seconds
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_ai_system_creation() {
        let config = AIConfig::default();
        // Note: This would fail in real test due to missing MCP server
        // let system = AISystem::new(config).await;
        // In actual implementation, we'd use mock clients for testing
        assert!(true); // Placeholder test
    }

    #[test]
    fn test_config_defaults() {
        let config = AIConfig::default();
        assert_eq!(config.claude.model, "claude-3-opus-20240229");
        assert_eq!(config.mcp.timeout, 30);
        assert_eq!(config.agents.max_agents, 100);
    }

    #[test]
    fn test_statistics_display() {
        let stats = AIStatistics {
            total_agents: 5,
            active_tasks: 3,
            total_tools: 10,
            memory_entries: 100,
            uptime_seconds: 3600,
        };
        
        let display = stats.to_string();
        assert!(display.contains("Agents=5"));
        assert!(display.contains("Active Tasks=3"));
        assert!(display.contains("Tools=10"));
    }
}