aethershell 0.3.1

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Agent Marketplace Registry
//!
//! This module provides a registry for sharing and discovering AetherShell agents.
//! Agents can be published, searched, installed, and versioned.

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Agent metadata for the marketplace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentManifest {
    /// Unique agent identifier (namespace/name format)
    pub name: String,
    /// Human-readable display name
    pub display_name: String,
    /// Agent description
    pub description: String,
    /// Semantic version
    pub version: String,
    /// Author information
    pub author: AuthorInfo,
    /// License (SPDX identifier)
    pub license: String,
    /// Repository URL
    pub repository: Option<String>,
    /// Homepage URL
    pub homepage: Option<String>,
    /// Keywords for search
    pub keywords: Vec<String>,
    /// Categories
    pub categories: Vec<String>,
    /// Agent capabilities
    pub capabilities: Vec<String>,
    /// Required AetherShell version
    pub aethershell_version: String,
    /// Dependencies on other agents
    pub dependencies: Vec<AgentDependency>,
    /// Agent definition
    pub agent: AgentDefinition,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorInfo {
    pub name: String,
    pub email: Option<String>,
    pub url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentDependency {
    pub name: String,
    pub version: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentDefinition {
    /// System prompt for the agent
    pub system_prompt: String,
    /// Model to use (optional, uses default if not specified)
    pub model: Option<String>,
    /// Available tools/builtins
    pub tools: Vec<String>,
    /// Tool calling mode
    pub tool_choice: Option<String>,
    /// Temperature for generation
    pub temperature: Option<f32>,
    /// Max tokens
    pub max_tokens: Option<u32>,
}

/// Published agent in the registry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PublishedAgent {
    pub manifest: AgentManifest,
    /// Download count
    pub downloads: u64,
    /// Star count
    pub stars: u64,
    /// Fork count
    pub forks: u64,
    /// Verification status
    pub verified: bool,
    /// Published timestamp
    pub published_at: u64,
    /// Last updated timestamp
    pub updated_at: u64,
    /// SHA-256 hash of the agent bundle
    pub checksum: String,
    /// Bundle size in bytes
    pub size: u64,
}

/// Search query for the marketplace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchQuery {
    pub query: Option<String>,
    pub category: Option<String>,
    pub keyword: Option<String>,
    pub author: Option<String>,
    pub sort_by: Option<SortBy>,
    pub page: Option<u32>,
    pub per_page: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SortBy {
    Downloads,
    Stars,
    Recent,
    Name,
}

/// Search results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResults {
    pub total: u64,
    pub page: u32,
    pub per_page: u32,
    pub results: Vec<PublishedAgent>,
}

/// Local registry client
pub struct RegistryClient {
    /// Registry URL
    registry_url: String,
    /// Local cache directory
    cache_dir: PathBuf,
    /// Installed agents
    installed: HashMap<String, InstalledAgent>,
}

impl std::fmt::Debug for RegistryClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RegistryClient")
            .field("registry_url", &self.registry_url)
            .field("cache_dir", &self.cache_dir)
            .field("installed_count", &self.installed.len())
            .finish()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstalledAgent {
    pub manifest: AgentManifest,
    pub installed_at: u64,
    pub path: PathBuf,
}

impl RegistryClient {
    /// Create a new registry client
    pub fn new(registry_url: Option<String>) -> Result<Self> {
        let registry_url =
            registry_url.unwrap_or_else(|| "https://registry.aethershell.dev".to_string());

        let cache_dir = dirs::cache_dir()
            .ok_or_else(|| anyhow!("Could not determine cache directory"))?
            .join("aethershell")
            .join("agents");

        std::fs::create_dir_all(&cache_dir)?;

        let installed = Self::load_installed(&cache_dir)?;

        Ok(Self {
            registry_url,
            cache_dir,
            installed,
        })
    }

    /// Search for agents in the registry
    pub async fn search(&self, query: SearchQuery) -> Result<SearchResults> {
        let client = reqwest::Client::new();

        let mut url = format!("{}/api/v1/agents/search", self.registry_url);
        let mut params = vec![];

        if let Some(q) = &query.query {
            params.push(format!("q={}", urlencoding::encode(q)));
        }
        if let Some(cat) = &query.category {
            params.push(format!("category={}", urlencoding::encode(cat)));
        }
        if let Some(kw) = &query.keyword {
            params.push(format!("keyword={}", urlencoding::encode(kw)));
        }
        if let Some(author) = &query.author {
            params.push(format!("author={}", urlencoding::encode(author)));
        }
        if let Some(sort) = &query.sort_by {
            params.push(format!("sort={}", serde_json::to_string(sort)?));
        }
        if let Some(page) = query.page {
            params.push(format!("page={}", page));
        }
        if let Some(per_page) = query.per_page {
            params.push(format!("per_page={}", per_page));
        }

        if !params.is_empty() {
            url = format!("{}?{}", url, params.join("&"));
        }

        let response = client.get(&url).send().await?;
        let results: SearchResults = response.json().await?;
        Ok(results)
    }

    /// Get agent details
    pub async fn get(&self, name: &str, version: Option<&str>) -> Result<PublishedAgent> {
        let client = reqwest::Client::new();

        let version_suffix = version.map(|v| format!("/{}", v)).unwrap_or_default();
        let url = format!(
            "{}/api/v1/agents/{}{}",
            self.registry_url, name, version_suffix
        );

        let response = client.get(&url).send().await?;
        if !response.status().is_success() {
            return Err(anyhow!("Agent not found: {}", name));
        }

        let agent: PublishedAgent = response.json().await?;
        Ok(agent)
    }

    /// Install an agent
    pub fn install<'a>(
        &'a mut self,
        name: &'a str,
        version: Option<&'a str>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<InstalledAgent>> + Send + 'a>>
    {
        Box::pin(async move {
            // Check if already installed
            if let Some(installed) = self.installed.get(name) {
                if version.is_none() || version == Some(&installed.manifest.version) {
                    return Ok(installed.clone());
                }
            }

            // Fetch agent from registry
            let agent = self.get(name, version).await?;

            // Install dependencies first
            for dep in &agent.manifest.dependencies {
                self.install(&dep.name, Some(&dep.version)).await?;
            }

            // Download and install
            let client = reqwest::Client::new();
            let url = format!(
                "{}/api/v1/agents/{}/{}/download",
                self.registry_url, name, agent.manifest.version
            );

            let response = client.get(&url).send().await?;
            let bytes = response.bytes().await?;

            // Verify checksum
            let checksum = format!("{:x}", md5::compute(&bytes));
            if checksum != agent.checksum {
                return Err(anyhow!("Checksum mismatch for agent {}", name));
            }

            // Save to local cache
            let agent_dir = self.cache_dir.join(name).join(&agent.manifest.version);
            std::fs::create_dir_all(&agent_dir)?;

            let manifest_path = agent_dir.join("manifest.json");
            std::fs::write(
                &manifest_path,
                serde_json::to_string_pretty(&agent.manifest)?,
            )?;

            let agent_path = agent_dir.join("agent.ae");
            std::fs::write(&agent_path, &bytes)?;

            let installed = InstalledAgent {
                manifest: agent.manifest.clone(),
                installed_at: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)?
                    .as_secs(),
                path: agent_dir.clone(),
            };

            self.installed.insert(name.to_string(), installed.clone());
            self.save_installed()?;

            Ok(installed)
        })
    }

    /// Uninstall an agent
    pub fn uninstall(&mut self, name: &str) -> Result<()> {
        if let Some(installed) = self.installed.remove(name) {
            std::fs::remove_dir_all(&installed.path)?;
            self.save_installed()?;
            Ok(())
        } else {
            Err(anyhow!("Agent not installed: {}", name))
        }
    }

    /// List installed agents
    pub fn list_installed(&self) -> Vec<&InstalledAgent> {
        self.installed.values().collect()
    }

    /// Publish an agent to the registry
    pub async fn publish(
        &self,
        manifest: &AgentManifest,
        agent_code: &str,
        api_key: &str,
    ) -> Result<PublishedAgent> {
        let client = reqwest::Client::new();

        let url = format!("{}/api/v1/agents/publish", self.registry_url);

        let response = client
            .post(&url)
            .header("Authorization", format!("Bearer {}", api_key))
            .json(&serde_json::json!({
                "manifest": manifest,
                "code": agent_code,
            }))
            .send()
            .await?;

        if !response.status().is_success() {
            let error: serde_json::Value = response.json().await?;
            return Err(anyhow!("Failed to publish: {}", error));
        }

        let published: PublishedAgent = response.json().await?;
        Ok(published)
    }

    /// Load installed agents from disk
    fn load_installed(cache_dir: &PathBuf) -> Result<HashMap<String, InstalledAgent>> {
        let index_path = cache_dir.join("installed.json");
        if index_path.exists() {
            let content = std::fs::read_to_string(&index_path)?;
            Ok(serde_json::from_str(&content)?)
        } else {
            Ok(HashMap::new())
        }
    }

    /// Save installed agents index
    fn save_installed(&self) -> Result<()> {
        let index_path = self.cache_dir.join("installed.json");
        let content = serde_json::to_string_pretty(&self.installed)?;
        std::fs::write(&index_path, content)?;
        Ok(())
    }

    /// Get agent by name (from installed or fetch)
    pub fn get_installed(&self, name: &str) -> Option<&InstalledAgent> {
        self.installed.get(name)
    }

    /// Load agent code
    pub fn load_agent_code(&self, name: &str) -> Result<String> {
        let installed = self
            .get_installed(name)
            .ok_or_else(|| anyhow!("Agent not installed: {}", name))?;

        let code_path = installed.path.join("agent.ae");
        Ok(std::fs::read_to_string(&code_path)?)
    }
}

/// CLI commands for the marketplace
#[derive(Debug, Clone)]
pub enum MarketplaceCommand {
    Search {
        query: String,
    },
    Install {
        name: String,
        version: Option<String>,
    },
    Uninstall {
        name: String,
    },
    List,
    Info {
        name: String,
    },
    Publish {
        path: PathBuf,
    },
    Update {
        name: Option<String>,
    },
}

/// Process marketplace CLI commands
pub async fn process_command(command: MarketplaceCommand) -> Result<String> {
    let mut client = RegistryClient::new(None)?;

    match command {
        MarketplaceCommand::Search { query } => {
            let results = client
                .search(SearchQuery {
                    query: Some(query),
                    ..Default::default()
                })
                .await?;

            let mut output = format!("Found {} agents:\n\n", results.total);
            for agent in results.results {
                output.push_str(&format!(
                    "  {} v{} - {}\n    by {} | ⬇ {} | ⭐ {}\n\n",
                    agent.manifest.name,
                    agent.manifest.version,
                    agent.manifest.description,
                    agent.manifest.author.name,
                    agent.downloads,
                    agent.stars
                ));
            }
            Ok(output)
        }

        MarketplaceCommand::Install { name, version } => {
            let installed = client.install(&name, version.as_deref()).await?;
            Ok(format!(
                "✓ Installed {} v{}\n  Location: {}",
                installed.manifest.name,
                installed.manifest.version,
                installed.path.display()
            ))
        }

        MarketplaceCommand::Uninstall { name } => {
            client.uninstall(&name)?;
            Ok(format!("✓ Uninstalled {}", name))
        }

        MarketplaceCommand::List => {
            let installed = client.list_installed();
            if installed.is_empty() {
                return Ok("No agents installed.".to_string());
            }

            let mut output = format!("Installed agents ({}):\n\n", installed.len());
            for agent in installed {
                output.push_str(&format!(
                    "  {} v{}\n    {}\n\n",
                    agent.manifest.name, agent.manifest.version, agent.manifest.description
                ));
            }
            Ok(output)
        }

        MarketplaceCommand::Info { name } => {
            let agent = client.get(&name, None).await?;
            Ok(format!(
                "{}\n{}\n\nVersion: {}\nAuthor: {} <{}>\nLicense: {}\n\nCapabilities: {}\nKeywords: {}\n\nDownloads: {}\nStars: {}\nVerified: {}",
                agent.manifest.display_name,
                agent.manifest.description,
                agent.manifest.version,
                agent.manifest.author.name,
                agent.manifest.author.email.unwrap_or_default(),
                agent.manifest.license,
                agent.manifest.capabilities.join(", "),
                agent.manifest.keywords.join(", "),
                agent.downloads,
                agent.stars,
                if agent.verified { "" } else { "" }
            ))
        }

        MarketplaceCommand::Publish { path } => {
            let manifest_path = path.join("manifest.json");
            let code_path = path.join("agent.ae");

            if !manifest_path.exists() {
                return Err(anyhow!("manifest.json not found in {}", path.display()));
            }
            if !code_path.exists() {
                return Err(anyhow!("agent.ae not found in {}", path.display()));
            }

            let manifest: AgentManifest =
                serde_json::from_str(&std::fs::read_to_string(&manifest_path)?)?;
            let code = std::fs::read_to_string(&code_path)?;

            let api_key = std::env::var("AETHERSHELL_API_KEY")
                .map_err(|_| anyhow!("AETHERSHELL_API_KEY environment variable not set"))?;

            let published = client.publish(&manifest, &code, &api_key).await?;
            Ok(format!(
                "✓ Published {} v{}\n  Registry: {}",
                published.manifest.name, published.manifest.version, client.registry_url
            ))
        }

        MarketplaceCommand::Update { name } => {
            let agents_to_update: Vec<_> = if let Some(name) = name {
                vec![name]
            } else {
                client.installed.keys().cloned().collect()
            };

            let mut output = String::new();
            for agent_name in agents_to_update {
                let latest = client.get(&agent_name, None).await?;
                let needs_update = client
                    .get_installed(&agent_name)
                    .map(|installed| latest.manifest.version != installed.manifest.version)
                    .unwrap_or(false);
                let old_version = client
                    .get_installed(&agent_name)
                    .map(|i| i.manifest.version.clone());

                if needs_update {
                    client.install(&agent_name, None).await?;
                    if let Some(old_ver) = old_version {
                        output.push_str(&format!(
                            "✓ Updated {} {}{}\n",
                            agent_name, old_ver, latest.manifest.version
                        ));
                    }
                } else if client.get_installed(&agent_name).is_some() {
                    output.push_str(&format!("  {} is up to date\n", agent_name));
                }
            }

            if output.is_empty() {
                output = "All agents are up to date.".to_string();
            }
            Ok(output)
        }
    }
}

impl Default for SearchQuery {
    fn default() -> Self {
        Self {
            query: None,
            category: None,
            keyword: None,
            author: None,
            sort_by: Some(SortBy::Downloads),
            page: Some(1),
            per_page: Some(20),
        }
    }
}

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

    #[test]
    fn test_agent_manifest_serialization() {
        let manifest = AgentManifest {
            name: "nervosys/code-reviewer".to_string(),
            display_name: "Code Reviewer".to_string(),
            description: "Automated code review agent".to_string(),
            version: "1.0.0".to_string(),
            author: AuthorInfo {
                name: "Nervosys".to_string(),
                email: Some("contact@nervosys.ai".to_string()),
                url: Some("https://nervosys.ai".to_string()),
            },
            license: "MIT".to_string(),
            repository: Some("https://github.com/nervosys/code-reviewer".to_string()),
            homepage: None,
            keywords: vec!["code".to_string(), "review".to_string()],
            categories: vec!["development".to_string()],
            capabilities: vec!["code_analysis".to_string(), "suggestions".to_string()],
            aethershell_version: ">=0.2.0".to_string(),
            dependencies: vec![],
            agent: AgentDefinition {
                system_prompt: "You are a code reviewer...".to_string(),
                model: Some("gpt-4o-mini".to_string()),
                tools: vec!["read_file".to_string(), "grep".to_string()],
                tool_choice: None,
                temperature: Some(0.3),
                max_tokens: Some(4096),
            },
        };

        let json = serde_json::to_string(&manifest).unwrap();
        let parsed: AgentManifest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.name, manifest.name);
    }

    #[test]
    fn test_search_query_default() {
        let query = SearchQuery::default();
        assert!(query.query.is_none());
        assert_eq!(query.page, Some(1));
        assert_eq!(query.per_page, Some(20));
    }
}