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
//! Registry module for RustChain marketplace functionality
//! 
//! This module provides registry capabilities for the RustChain ecosystem.
//! In the enterprise edition, this would contain the full marketplace implementation.

use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};

/// Registry client for accessing RustChain marketplace
#[derive(Debug, Clone)]
pub struct RegistryClient {
    base_url: String,
    api_key: Option<String>,
    client: reqwest::Client,
}

/// Represents a package in the registry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistryPackage {
    pub id: Uuid,
    pub name: String,
    pub version: String,
    pub description: String,
    pub author: String,
    pub tags: Vec<String>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub download_count: u64,
    pub checksum: String,
}

/// Package metadata for registry operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageMetadata {
    pub name: String,
    pub version: String,
    pub description: String,
    pub author: String,
    pub license: String,
    pub repository: Option<String>,
    pub homepage: Option<String>,
    pub keywords: Vec<String>,
    pub dependencies: HashMap<String, String>,
}

/// Registry search query parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchQuery {
    pub query: String,
    pub category: Option<String>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

/// Search results from registry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResults {
    pub packages: Vec<RegistryPackage>,
    pub total: usize,
    pub has_more: bool,
}

impl RegistryClient {
    /// Create a new registry client
    pub fn new(base_url: String) -> Self {
        Self {
            base_url,
            api_key: None,
            client: reqwest::Client::new(),
        }
    }

    /// Set API key for authenticated requests
    pub fn with_api_key(mut self, api_key: String) -> Self {
        self.api_key = Some(api_key);
        self
    }

    /// Search for packages in the registry
    pub async fn search(&self, query: &SearchQuery) -> Result<SearchResults, RegistryError> {
        #[cfg(feature = "enterprise")]
        {
            // Enterprise edition: actual API call
            let url = format!("{}/api/search", self.base_url);
            let response = self.client
                .get(&url)
                .query(&query)
                .send()
                .await?;

            if !response.status().is_success() {
                return Err(RegistryError::ApiError(response.status().as_u16()));
            }

            let results = response
                .json::<SearchResults>()
                .await?;

            Ok(results)
        }

        #[cfg(not(feature = "enterprise"))]
        {
            // Community edition: return mock results for demonstration
            let _ = query; // Suppress unused warning
            Ok(SearchResults {
                packages: vec![],
                total: 0,
                has_more: false,
            })
        }
    }

    /// Get package details by name and version
    pub async fn get_package(&self, name: &str, version: &str) -> Result<RegistryPackage, RegistryError> {
        #[cfg(feature = "enterprise")]
        {
            let url = format!("{}/api/packages/{}/{}", self.base_url, name, version);
            let response = self.client
                .get(&url)
                .send()
                .await?;

            if !response.status().is_success() {
                return Err(RegistryError::ApiError(response.status().as_u16()));
            }

            let package = response
                .json::<RegistryPackage>()
                .await?;

            Ok(package)
        }

        #[cfg(not(feature = "enterprise"))]
        {
            // Community edition: return error indicating enterprise feature needed
            let _ = (name, version); // Suppress unused warnings
            Err(RegistryError::FeatureNotAvailable("package_download".to_string()))
        }
    }

    /// Download a package from the registry
    pub async fn download_package(&self, name: &str, version: &str) -> Result<Vec<u8>, RegistryError> {
        #[cfg(feature = "enterprise")]
        {
            let url = format!("{}/api/packages/{}/{}/download", self.base_url, name, version);
            let response = self.client
                .get(&url)
                .send()
                .await?;

            if !response.status().is_success() {
                return Err(RegistryError::ApiError(response.status().as_u16()));
            }

            let bytes = response
                .bytes()
                .await?
                .to_vec();

            Ok(bytes)
        }

        #[cfg(not(feature = "enterprise"))]
        {
            let _ = (name, version); // Suppress unused warnings
            Err(RegistryError::FeatureNotAvailable("package_download".to_string()))
        }
    }

    /// Publish a package to the registry (requires authentication)
    pub async fn publish_package(&self, metadata: &PackageMetadata, package_data: Vec<u8>) -> Result<RegistryPackage, RegistryError> {
        #[cfg(feature = "enterprise")]
        {
            let api_key = self.api_key.as_ref()
                .ok_or_else(|| RegistryError::AuthenticationRequired)?;

            let url = format!("{}/api/packages", self.base_url);
            let form = reqwest::multipart::Form::new()
                .text("metadata", serde_json::to_string(metadata)?)
                .part("package", reqwest::multipart::Part::bytes(package_data));

            let response = self.client
                .post(&url)
                .header("Authorization", format!("Bearer {}", api_key))
                .multipart(form)
                .send()
                .await?;

            if !response.status().is_success() {
                return Err(RegistryError::ApiError(response.status().as_u16()));
            }

            let package = response
                .json::<RegistryPackage>()
                .await?;

            Ok(package)
        }

        #[cfg(not(feature = "enterprise"))]
        {
            let _ = (metadata, package_data); // Suppress unused warnings
            Err(RegistryError::FeatureNotAvailable("package_publish".to_string()))
        }
    }
}

/// Registry operation errors
#[derive(Debug, thiserror::Error)]
pub enum RegistryError {
    #[error("Network error: {0}")]
    NetworkError(reqwest::Error),
    
    #[error("API error: HTTP {0}")]
    ApiError(u16),
    
    #[error("Parse error: {0}")]
    ParseError(reqwest::Error),
    
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
    
    #[error("Feature '{0}' is not available in community edition")]
    FeatureNotAvailable(String),
    
    #[error("Authentication required for this operation")]
    AuthenticationRequired,
    
    #[error("Package not found: {name}@{version}")]
    PackageNotFound { name: String, version: String },
    
    #[error("Invalid package format")]
    InvalidPackageFormat,
}

impl From<reqwest::Error> for RegistryError {
    fn from(err: reqwest::Error) -> Self {
        if err.is_decode() {
            RegistryError::ParseError(err)
        } else {
            RegistryError::NetworkError(err)
        }
    }
}

/// Get the default registry URL
pub fn default_registry_url() -> String {
    std::env::var("RUSTCHAIN_REGISTRY_URL")
        .unwrap_or_else(|_| "https://registry.rustchain.ai".to_string())
}

/// Create a default registry client
pub fn create_registry_client() -> RegistryClient {
    RegistryClient::new(default_registry_url())
}

/// Initialize registry with authentication
pub fn create_authenticated_client(api_key: String) -> RegistryClient {
    RegistryClient::new(default_registry_url())
        .with_api_key(api_key)
}

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

    #[test]
    fn test_registry_client_creation() {
        let client = RegistryClient::new("https://test.example.com".to_string());
        assert_eq!(client.base_url, "https://test.example.com");
        assert!(client.api_key.is_none());
    }

    #[test]
    fn test_registry_client_with_api_key() {
        let client = RegistryClient::new("https://test.example.com".to_string())
            .with_api_key("test-key".to_string());
        
        assert_eq!(client.api_key, Some("test-key".to_string()));
    }

    #[test]
    fn test_default_registry_url() {
        let url = default_registry_url();
        assert!(!url.is_empty());
        assert!(url.starts_with("http"));
    }

    #[tokio::test]
    async fn test_search_behavior() {
        let client = create_registry_client();
        let query = SearchQuery {
            query: "test".to_string(),
            category: None,
            limit: Some(10),
            offset: Some(0),
        };

        let results = client.search(&query).await;
        
        #[cfg(feature = "enterprise")]
        {
            // Enterprise edition may succeed or fail with network error
            // Both are acceptable in tests
            match results {
                Ok(search_results) => {
                    // API call succeeded - validate structure
                    assert!(search_results.packages.len() >= 0);
                    assert!(search_results.total >= 0);
                }
                Err(_) => {
                    // Network error is acceptable in test environment
                    // where registry.rustchain.ai doesn't exist
                }
            }
        }
        
        #[cfg(not(feature = "enterprise"))]
        {
            // Community edition should return empty results
            let search_results = results.unwrap();
            assert_eq!(search_results.packages.len(), 0);
            assert_eq!(search_results.total, 0);
            assert!(!search_results.has_more);
        }
    }

    #[tokio::test]
    async fn test_get_package_behavior() {
        let client = create_registry_client();
        
        let result = client.get_package("test-package", "1.0.0").await;
        
        #[cfg(feature = "enterprise")]
        {
            // Enterprise edition may succeed or fail with network error
            match result {
                Ok(_) => {
                    // API call succeeded
                }
                Err(RegistryError::NetworkError(_)) => {
                    // Network error is acceptable in test environment
                }
                Err(RegistryError::ApiError(_)) => {
                    // API error (404, etc) is acceptable
                }
                Err(e) => {
                    // Other errors should be investigated
                    panic!("Unexpected error: {:?}", e);
                }
            }
        }
        
        #[cfg(not(feature = "enterprise"))]
        {
            // Community edition should return feature not available error
            match result {
                Err(RegistryError::FeatureNotAvailable(_)) => {
                    // Expected behavior
                }
                _ => panic!("Expected FeatureNotAvailable error"),
            }
        }
    }

    #[tokio::test]
    async fn test_download_package_behavior() {
        let client = create_registry_client();
        
        let result = client.download_package("test-package", "1.0.0").await;
        
        #[cfg(feature = "enterprise")]
        {
            // Enterprise edition may succeed or fail with network error
            match result {
                Ok(_) => {
                    // API call succeeded
                }
                Err(RegistryError::NetworkError(_)) => {
                    // Network error is acceptable in test environment
                }
                Err(RegistryError::ApiError(_)) => {
                    // API error (404, etc) is acceptable
                }
                Err(e) => {
                    // Other errors should be investigated
                    panic!("Unexpected error: {:?}", e);
                }
            }
        }
        
        #[cfg(not(feature = "enterprise"))]
        {
            // Community edition should return feature not available error
            match result {
                Err(RegistryError::FeatureNotAvailable(_)) => {
                    // Expected behavior
                }
                _ => panic!("Expected FeatureNotAvailable error"),
            }
        }
    }
}