rustchain-community 1.0.0

Open-source AI agent framework with core functionality and plugin system
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
use crate::assert_invariant;
#[cfg(feature = "rag")]
use crate::rag::RagSystem;
#[cfg(feature = "sandbox")]
use crate::sandbox::EnhancedSandbox;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;
use uuid::Uuid;

// Error handling
pub mod error;
pub mod error_formatting;
pub use error::*;
pub use error_formatting::*;

// Mission system
pub mod mission;
pub use mission::*;

// Executor system
pub mod executor;
pub use executor::*;

// Enhanced audit system
pub mod audit;
pub use audit::*;

// Memory system
pub mod memory;
pub use memory::*;

// Agent system
pub mod agent;
pub use agent::*;

// Chain system
pub mod chain;
pub use chain::*;

// LLM system
pub mod llm;
pub use llm::*;

// Tools system
pub mod tools;
pub use tools::*;

// Web search tools
#[cfg(feature = "tools")]
pub mod web_search_tools;

// Document loaders
#[cfg(feature = "tools")]
pub mod document_loaders;
#[cfg(feature = "tools")]
pub use document_loaders::*;

// Vector stores
#[cfg(feature = "rag")]
pub mod pinecone_vector_store;

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

// Code interpreters
#[cfg(feature = "tools")]
pub mod python_interpreter;
#[cfg(feature = "tools")]
pub use python_interpreter::*;

// Developer toolkits
#[cfg(feature = "tools")]
pub mod github_toolkit;

// Plugin system for enterprise features
pub mod plugin;
pub use plugin::*;

// Feature detection and boundary enforcement
pub mod features;
pub use features::*;

/// Central runtime context that holds all system state
#[derive(Clone)]
pub struct RuntimeContext {
    pub config: Arc<RwLock<Config>>,
    pub audit: Arc<AuditSink>,
    pub tool_registry: Arc<RwLock<ToolRegistry>>,
    pub model_manager: Option<Arc<ModelManager>>,
    pub sandbox: Arc<AgentSandbox>,
    pub policy_engine: Arc<PolicyEngine>,
    pub perf_collector: Arc<RwLock<PerfCollector>>,
    pub plugin_manager: Arc<RwLock<PluginManager>>,
    pub feature_detector: Arc<FeatureDetector>,
    #[cfg(feature = "rag")]
    pub rag_system: Option<Arc<RwLock<RagSystem>>>,
    #[cfg(feature = "sandbox")]
    pub enhanced_sandbox: Option<Arc<EnhancedSandbox>>,
}

impl RuntimeContext {
    pub fn new() -> Self {
        assert_invariant!(true, "RuntimeContext created", Some("core"));

        Self {
            config: Arc::new(RwLock::new(Config::default())),
            audit: Arc::new(AuditSink::new()),
            tool_registry: Arc::new(RwLock::new(ToolRegistry::new())),
            model_manager: None,
            sandbox: Arc::new(AgentSandbox::new()),
            policy_engine: Arc::new(PolicyEngine::new()),
            perf_collector: Arc::new(RwLock::new(PerfCollector::new())),
            plugin_manager: Arc::new(RwLock::new(PluginManager::new())),
            feature_detector: Arc::new(FeatureDetector::new()),
            #[cfg(feature = "rag")]
            rag_system: None,
            #[cfg(feature = "sandbox")]
            enhanced_sandbox: None,
        }
    }

    pub async fn audit_action(&self, agent_id: &str, action: &str, outcome: &str) {
        let entry = AuditEntry {
            id: Uuid::new_v4(),
            timestamp: Utc::now(),
            actor: agent_id.to_string(),
            action: action.to_string(),
            outcome: outcome.to_string(),
            reason: None,
        };
        self.audit.log(entry).await;
    }

    /// Check if an enterprise feature is available through plugins
    pub async fn has_enterprise_feature(&self, feature: &str) -> bool {
        if cfg!(feature = "enterprise") {
            self.plugin_manager.read().await.has_feature(feature)
        } else {
            false
        }
    }

    /// Get list of all available enterprise features
    pub async fn get_enterprise_features(&self) -> Vec<String> {
        if cfg!(feature = "enterprise") {
            self.plugin_manager.read().await.enabled_features()
        } else {
            vec![]
        }
    }

    /// Get list of all available core features
    pub async fn get_available_features(&self) -> Vec<String> {
        let mut features = Vec::new();
        
        // Core features that are always available
        features.push("mission_execution".to_string());
        features.push("safety_validation".to_string());
        features.push("audit_logging".to_string());
        features.push("policy_engine".to_string());
        
        // Feature-gated components
        #[cfg(feature = "llm")]
        features.push("llm_integration".to_string());
        
        #[cfg(feature = "tools")]
        features.push("tool_system".to_string());
        
        #[cfg(feature = "rag")]
        features.push("rag_system".to_string());
        
        #[cfg(feature = "sandbox")]
        features.push("sandbox".to_string());
        
        #[cfg(feature = "server")]
        features.push("api_server".to_string());
        
        #[cfg(feature = "compliance")]
        features.push("compliance_checking".to_string());
        
        features
    }

    /// Load enterprise plugins (not available in community edition)
    pub async fn load_enterprise_plugins(&self) -> crate::core::error::Result<()> {
        // Community edition: No enterprise plugins available
        Ok(())
    }

    /// Enhanced feature detection with detailed status
    pub async fn check_feature_status(&self, feature: &str) -> FeatureStatus {
        self.feature_detector.is_feature_available(self, feature).await
    }

    /// Require a feature or return detailed error
    pub async fn require_feature(&self, feature: &str) -> crate::core::error::Result<()> {
        self.feature_detector.require_feature(self, feature).await
    }

    /// Get comprehensive feature summary for this installation
    pub async fn get_feature_summary(&self) -> FeatureSummary {
        self.feature_detector.get_feature_summary(self).await
    }

    /// Get status for all features in a category
    pub async fn get_category_status(&self, category: &str) -> Vec<FeatureStatus> {
        self.feature_detector.get_category_status(self, category).await
    }

    /// Check if running enterprise edition with full features
    pub async fn is_enterprise_complete(&self) -> bool {
        self.get_feature_summary().await.is_enterprise_complete()
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
    pub mission_timeout_seconds: u64,
    pub max_parallel_steps: usize,
    pub audit_enabled: bool,
    pub network_policy: NetworkPolicy,
    pub agent_id: String,
    pub max_tool_calls: usize,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            mission_timeout_seconds: 300,
            max_parallel_steps: 4,
            audit_enabled: true,
            network_policy: NetworkPolicy::Offline,
            agent_id: "rustchain-agent".to_string(),
            max_tool_calls: 100,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum NetworkPolicy {
    Offline,
    AllowList(Vec<String>),
}

/// Enhanced audit sink with cryptographic chain integrity
pub struct AuditSink {
    entries: Arc<RwLock<Vec<AuditEntry>>>,
}

impl AuditSink {
    pub fn new() -> Self {
        Self {
            entries: Arc::new(RwLock::new(Vec::new())),
        }
    }

    pub async fn log(&self, entry: AuditEntry) {
        self.entries.write().await.push(entry);
    }

    pub async fn get_chain_hash(&self) -> String {
        let entries = self.entries.read().await;
        if entries.is_empty() {
            return "genesis".to_string();
        }

        let mut hasher = Sha256::new();
        for entry in entries.iter() {
            hasher.update(
                format!(
                    "{}{}{}{}",
                    entry.timestamp.to_rfc3339(),
                    entry.actor,
                    entry.action,
                    entry.outcome
                )
                .as_bytes(),
            );
        }
        format!("{:x}", hasher.finalize())
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuditEntry {
    pub id: Uuid,
    pub timestamp: DateTime<Utc>,
    pub actor: String,
    pub action: String,
    pub outcome: String,
    pub reason: Option<String>,
}

pub struct ToolRegistry {
    tools: HashMap<String, Box<dyn Tool + Send + Sync>>,
}

impl ToolRegistry {
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
        }
    }

    pub fn register(&mut self, name: String, tool: Box<dyn Tool + Send + Sync>) {
        self.tools.insert(name, tool);
    }

    pub fn get(&self, name: &str) -> Option<&Box<dyn Tool + Send + Sync>> {
        self.tools.get(name)
    }
}

pub trait Tool {
    fn name(&self) -> &str;
    fn invoke(&self, args: serde_json::Value) -> anyhow::Result<serde_json::Value>;
}

/// Performance metrics collection
#[derive(Debug, Clone)]
pub struct PerfMetric {
    pub name: String,
    pub duration_ms: u128,
}

pub struct PerfCollector {
    active: HashMap<String, Instant>,
    pub completed: Vec<PerfMetric>,
}

impl PerfCollector {
    pub fn new() -> Self {
        Self {
            active: HashMap::new(),
            completed: vec![],
        }
    }

    pub fn start(&mut self, name: &str) {
        self.active.insert(name.to_string(), Instant::now());
    }

    pub fn end(&mut self, name: &str) {
        if let Some(start) = self.active.remove(name) {
            let duration = start.elapsed().as_millis();
            self.completed.push(PerfMetric {
                name: name.to_string(),
                duration_ms: duration,
            });
        }
    }

    pub fn summary(&self) -> String {
        self.completed
            .iter()
            .map(|m| format!("{}: {}ms", m.name, m.duration_ms))
            .collect::<Vec<_>>()
            .join("\n")
    }
}

pub struct ModelManager {
    // Will be implemented in Gate 6
    #[cfg(feature = "llm")]
    llm_manager: Option<crate::llm::LLMManager>,
}

impl ModelManager {
    pub fn new() -> Self {
        Self {
            #[cfg(feature = "llm")]
            llm_manager: None,
        }
    }

    #[cfg(feature = "llm")]
    pub fn with_llm_manager(mut self, manager: crate::llm::LLMManager) -> Self {
        self.llm_manager = Some(manager);
        self
    }

    #[cfg(feature = "llm")]
    pub async fn complete(
        &self,
        request: crate::llm::LLMRequest,
        provider: Option<&str>,
    ) -> anyhow::Result<crate::llm::LLMResponse> {
        if let Some(ref manager) = self.llm_manager {
            manager.complete(request, provider).await
        } else {
            Err(anyhow::anyhow!("LLM manager not initialized"))
        }
    }
}

pub struct AgentSandbox {
    #[allow(dead_code)]
    allowed_paths: Vec<std::path::PathBuf>,
    #[allow(dead_code)]
    timeout_seconds: u64,
}

impl AgentSandbox {
    pub fn new() -> Self {
        // Safe default path handling - fallback to current directory or root
        let current_dir = std::env::current_dir()
            .unwrap_or_else(|_| std::path::PathBuf::from("."));
            
        Self {
            allowed_paths: vec![current_dir],
            timeout_seconds: 30,
        }
    }

    pub fn execute(&self, code: &str) -> std::result::Result<String, String> {
        // Placeholder sandbox execution
        Ok(format!("Executed in sandbox: {}", code))
    }
}

pub struct PolicyEngine {
    policies: Vec<String>,
}

impl PolicyEngine {
    pub fn new() -> Self {
        Self {
            policies: Vec::new(),
        }
    }

    pub fn validate(&self, action: &str) -> bool {
        // Basic policy validation - will be replaced by enhanced engine
        !self.policies.iter().any(|p| action.contains(p))
    }

    pub fn add_policy(&mut self, policy: String) {
        self.policies.push(policy);
    }
}