agentkernel 0.18.1

Run AI coding agents in secure, isolated microVMs
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
606
607
608
609
//! Multi-agent support for agentkernel.
//!
//! Provides adapters for different AI coding agents: Claude Code, Gemini CLI, Codex, OpenCode.

#![allow(dead_code)]

use anyhow::Result;
use std::collections::HashMap;

/// Agent type enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AgentType {
    #[default]
    Claude,
    Gemini,
    Codex,
    OpenCode,
    Amp,
    Pi,
    Hermes,
    Symphony,
}

impl AgentType {
    /// Parse agent type from string
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "claude" | "claude-code" => Some(Self::Claude),
            "gemini" | "gemini-cli" => Some(Self::Gemini),
            "codex" | "openai-codex" => Some(Self::Codex),
            "opencode" | "open-code" => Some(Self::OpenCode),
            "amp" | "ampcode" => Some(Self::Amp),
            "pi" | "pi-coding-agent" => Some(Self::Pi),
            "hermes" | "hermes-agent" => Some(Self::Hermes),
            "symphony" | "openai-symphony" => Some(Self::Symphony),
            _ => None,
        }
    }

    /// Get the display name for this agent
    pub fn name(&self) -> &'static str {
        match self {
            Self::Claude => "Claude Code",
            Self::Gemini => "Gemini CLI",
            Self::Codex => "Codex",
            Self::OpenCode => "OpenCode",
            Self::Amp => "Amp",
            Self::Pi => "Pi",
            Self::Hermes => "Hermes Agent",
            Self::Symphony => "Symphony",
        }
    }

    /// Get the command to launch this agent
    pub fn command(&self) -> &'static str {
        match self {
            Self::Claude => "claude",
            Self::Gemini => "gemini",
            Self::Codex => "codex",
            Self::OpenCode => "opencode",
            Self::Amp => "amp",
            Self::Pi => "pi",
            Self::Hermes => "hermes",
            Self::Symphony => "symphony",
        }
    }
}

/// Configuration for an agent
#[derive(Debug, Clone, Default)]
#[allow(dead_code)]
pub struct AgentConfig {
    pub agent_type: AgentType,
    pub env_vars: HashMap<String, String>,
    pub args: Vec<String>,
    pub working_dir: Option<String>,
}

#[allow(dead_code)]
impl AgentConfig {
    /// Create config for a specific agent type
    pub fn for_agent(agent_type: AgentType) -> Self {
        Self {
            agent_type,
            ..Default::default()
        }
    }

    /// Add an environment variable
    pub fn with_env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.env_vars.insert(key.into(), value.into());
        self
    }

    /// Add command-line arguments
    pub fn with_args(mut self, args: Vec<String>) -> Self {
        self.args = args;
        self
    }

    /// Set the working directory
    pub fn with_working_dir(mut self, dir: impl Into<String>) -> Self {
        self.working_dir = Some(dir.into());
        self
    }
}

/// Agent adapter trait - defines how to interact with each agent
pub trait Agent {
    /// Get the agent type
    fn agent_type(&self) -> AgentType;

    /// Get the command to launch this agent
    fn launch_command(&self) -> Vec<String>;

    /// Get environment variables to set
    fn env_vars(&self) -> &HashMap<String, String>;

    /// Get the required API key environment variable name (if any)
    fn api_key_env_var(&self) -> Option<&'static str>;

    /// Check if the agent is available (installed and configured)
    fn is_available(&self) -> bool;

    /// Get install instructions
    fn install_instructions(&self) -> &'static str;
}

/// Claude Code adapter
pub struct ClaudeAgent {
    config: AgentConfig,
}

impl ClaudeAgent {
    pub fn new(config: AgentConfig) -> Self {
        Self { config }
    }
}

impl Agent for ClaudeAgent {
    fn agent_type(&self) -> AgentType {
        AgentType::Claude
    }

    fn launch_command(&self) -> Vec<String> {
        let mut cmd = vec!["claude".to_string()];
        cmd.extend(self.config.args.clone());
        cmd
    }

    fn env_vars(&self) -> &HashMap<String, String> {
        &self.config.env_vars
    }

    fn api_key_env_var(&self) -> Option<&'static str> {
        Some("ANTHROPIC_API_KEY")
    }

    fn is_available(&self) -> bool {
        std::process::Command::new("claude")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn install_instructions(&self) -> &'static str {
        "Install Claude Code: npm install -g @anthropic-ai/claude-code"
    }
}

/// Gemini CLI adapter
pub struct GeminiAgent {
    config: AgentConfig,
}

impl GeminiAgent {
    pub fn new(config: AgentConfig) -> Self {
        Self { config }
    }
}

impl Agent for GeminiAgent {
    fn agent_type(&self) -> AgentType {
        AgentType::Gemini
    }

    fn launch_command(&self) -> Vec<String> {
        let mut cmd = vec!["gemini".to_string()];
        cmd.extend(self.config.args.clone());
        cmd
    }

    fn env_vars(&self) -> &HashMap<String, String> {
        &self.config.env_vars
    }

    fn api_key_env_var(&self) -> Option<&'static str> {
        Some("GOOGLE_API_KEY")
    }

    fn is_available(&self) -> bool {
        std::process::Command::new("gemini")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn install_instructions(&self) -> &'static str {
        "Install Gemini CLI: pip install google-generativeai"
    }
}

/// Codex adapter
pub struct CodexAgent {
    config: AgentConfig,
}

impl CodexAgent {
    pub fn new(config: AgentConfig) -> Self {
        Self { config }
    }
}

impl Agent for CodexAgent {
    fn agent_type(&self) -> AgentType {
        AgentType::Codex
    }

    fn launch_command(&self) -> Vec<String> {
        let mut cmd = vec!["codex".to_string()];
        cmd.extend(self.config.args.clone());
        cmd
    }

    fn env_vars(&self) -> &HashMap<String, String> {
        &self.config.env_vars
    }

    fn api_key_env_var(&self) -> Option<&'static str> {
        Some("OPENAI_API_KEY")
    }

    fn is_available(&self) -> bool {
        std::process::Command::new("codex")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn install_instructions(&self) -> &'static str {
        "Install Codex CLI: npm install -g @openai/codex"
    }
}

/// OpenCode adapter
pub struct OpenCodeAgent {
    config: AgentConfig,
}

impl OpenCodeAgent {
    pub fn new(config: AgentConfig) -> Self {
        Self { config }
    }
}

impl Agent for OpenCodeAgent {
    fn agent_type(&self) -> AgentType {
        AgentType::OpenCode
    }

    fn launch_command(&self) -> Vec<String> {
        let mut cmd = vec!["opencode".to_string()];
        cmd.extend(self.config.args.clone());
        cmd
    }

    fn env_vars(&self) -> &HashMap<String, String> {
        &self.config.env_vars
    }

    fn api_key_env_var(&self) -> Option<&'static str> {
        // OpenCode supports multiple providers
        None
    }

    fn is_available(&self) -> bool {
        std::process::Command::new("opencode")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn install_instructions(&self) -> &'static str {
        "Install OpenCode: cargo install opencode"
    }
}

/// Amp adapter
pub struct AmpAgent {
    config: AgentConfig,
}

impl AmpAgent {
    pub fn new(config: AgentConfig) -> Self {
        Self { config }
    }
}

impl Agent for AmpAgent {
    fn agent_type(&self) -> AgentType {
        AgentType::Amp
    }

    fn launch_command(&self) -> Vec<String> {
        let mut cmd = vec!["amp".to_string()];
        cmd.extend(self.config.args.clone());
        cmd
    }

    fn env_vars(&self) -> &HashMap<String, String> {
        &self.config.env_vars
    }

    fn api_key_env_var(&self) -> Option<&'static str> {
        Some("ANTHROPIC_API_KEY")
    }

    fn is_available(&self) -> bool {
        std::process::Command::new("amp")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn install_instructions(&self) -> &'static str {
        "Install Amp: npm install -g @sourcegraph/amp"
    }
}

/// Pi adapter
pub struct PiAgent {
    config: AgentConfig,
}

impl PiAgent {
    pub fn new(config: AgentConfig) -> Self {
        Self { config }
    }
}

impl Agent for PiAgent {
    fn agent_type(&self) -> AgentType {
        AgentType::Pi
    }

    fn launch_command(&self) -> Vec<String> {
        let mut cmd = vec!["pi".to_string()];
        cmd.extend(self.config.args.clone());
        cmd
    }

    fn env_vars(&self) -> &HashMap<String, String> {
        &self.config.env_vars
    }

    fn api_key_env_var(&self) -> Option<&'static str> {
        // Pi supports multiple providers, no single required key
        None
    }

    fn is_available(&self) -> bool {
        std::process::Command::new("pi")
            .arg("--version")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn install_instructions(&self) -> &'static str {
        "Install Pi: npm install -g @mariozechner/pi-coding-agent"
    }
}

/// Hermes Agent adapter
pub struct HermesAgent {
    config: AgentConfig,
}

impl HermesAgent {
    pub fn new(config: AgentConfig) -> Self {
        Self { config }
    }
}

impl Agent for HermesAgent {
    fn agent_type(&self) -> AgentType {
        AgentType::Hermes
    }

    fn launch_command(&self) -> Vec<String> {
        let mut cmd = vec!["hermes".to_string()];
        cmd.extend(self.config.args.clone());
        cmd
    }

    fn env_vars(&self) -> &HashMap<String, String> {
        &self.config.env_vars
    }

    fn api_key_env_var(&self) -> Option<&'static str> {
        // Hermes supports multiple providers; OPENROUTER_API_KEY is the recommended default
        None
    }

    fn is_available(&self) -> bool {
        std::process::Command::new("hermes")
            .arg("--help")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn install_instructions(&self) -> &'static str {
        "Install Hermes Agent: curl -fsSL https://raw.githubusercontent.com/NousResearch/hermes-agent/main/scripts/install.sh | bash"
    }
}

/// Symphony adapter
pub struct SymphonyAgent {
    config: AgentConfig,
}

impl SymphonyAgent {
    pub fn new(config: AgentConfig) -> Self {
        Self { config }
    }
}

impl Agent for SymphonyAgent {
    fn agent_type(&self) -> AgentType {
        AgentType::Symphony
    }

    fn launch_command(&self) -> Vec<String> {
        let mut cmd = vec!["symphony".to_string()];
        cmd.extend(self.config.args.clone());
        cmd
    }

    fn env_vars(&self) -> &HashMap<String, String> {
        &self.config.env_vars
    }

    fn api_key_env_var(&self) -> Option<&'static str> {
        Some("OPENAI_API_KEY")
    }

    fn is_available(&self) -> bool {
        std::process::Command::new("symphony")
            .arg("--help")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    }

    fn install_instructions(&self) -> &'static str {
        "Install Symphony: git clone https://github.com/openai/symphony && cd symphony/elixir && mix setup && mix build"
    }
}

/// Create an agent adapter from type
pub fn create_agent(agent_type: AgentType, config: Option<AgentConfig>) -> Box<dyn Agent> {
    let config = config.unwrap_or_else(|| AgentConfig::for_agent(agent_type));

    match agent_type {
        AgentType::Claude => Box::new(ClaudeAgent::new(config)),
        AgentType::Gemini => Box::new(GeminiAgent::new(config)),
        AgentType::Codex => Box::new(CodexAgent::new(config)),
        AgentType::OpenCode => Box::new(OpenCodeAgent::new(config)),
        AgentType::Amp => Box::new(AmpAgent::new(config)),
        AgentType::Pi => Box::new(PiAgent::new(config)),
        AgentType::Hermes => Box::new(HermesAgent::new(config)),
        AgentType::Symphony => Box::new(SymphonyAgent::new(config)),
    }
}

/// Create an agent from a string type name
pub fn create_agent_from_str(agent_name: &str) -> Result<Box<dyn Agent>> {
    let agent_type = AgentType::from_str(agent_name)
        .ok_or_else(|| anyhow::anyhow!("Unknown agent type: {}", agent_name))?;
    Ok(create_agent(agent_type, None))
}

/// Check agent availability
pub fn check_agent_availability(agent_type: AgentType) -> AgentStatus {
    let agent = create_agent(agent_type, None);

    let installed = agent.is_available();
    let api_key_set = agent
        .api_key_env_var()
        .map(|var| std::env::var(var).is_ok())
        .unwrap_or(true);

    AgentStatus {
        agent_type,
        installed,
        api_key_set,
        install_instructions: agent.install_instructions().to_string(),
    }
}

/// Status of an agent's availability
#[derive(Debug, Clone)]
pub struct AgentStatus {
    pub agent_type: AgentType,
    pub installed: bool,
    pub api_key_set: bool,
    pub install_instructions: String,
}

impl AgentStatus {
    /// Check if agent is fully ready
    pub fn is_ready(&self) -> bool {
        self.installed && self.api_key_set
    }

    /// Print status
    pub fn print(&self) {
        let status = if self.is_ready() {
            "ready"
        } else if self.installed {
            "api key missing"
        } else {
            "not installed"
        };

        println!("{:<15} {}", self.agent_type.name(), status);

        if !self.installed {
            println!("  {}", self.install_instructions);
        }
    }
}

/// List all available agents and their status
pub fn list_agents() -> Vec<AgentStatus> {
    vec![
        check_agent_availability(AgentType::Claude),
        check_agent_availability(AgentType::Gemini),
        check_agent_availability(AgentType::Codex),
        check_agent_availability(AgentType::OpenCode),
        check_agent_availability(AgentType::Amp),
        check_agent_availability(AgentType::Pi),
        check_agent_availability(AgentType::Hermes),
        check_agent_availability(AgentType::Symphony),
    ]
}

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

    #[test]
    fn test_agent_type_from_str() {
        assert_eq!(AgentType::from_str("claude"), Some(AgentType::Claude));
        assert_eq!(AgentType::from_str("Claude"), Some(AgentType::Claude));
        assert_eq!(AgentType::from_str("gemini"), Some(AgentType::Gemini));
        assert_eq!(AgentType::from_str("codex"), Some(AgentType::Codex));
        assert_eq!(AgentType::from_str("opencode"), Some(AgentType::OpenCode));
        assert_eq!(AgentType::from_str("amp"), Some(AgentType::Amp));
        assert_eq!(AgentType::from_str("ampcode"), Some(AgentType::Amp));
        assert_eq!(AgentType::from_str("pi"), Some(AgentType::Pi));
        assert_eq!(AgentType::from_str("pi-coding-agent"), Some(AgentType::Pi));
        assert_eq!(AgentType::from_str("hermes"), Some(AgentType::Hermes));
        assert_eq!(AgentType::from_str("hermes-agent"), Some(AgentType::Hermes));
        assert_eq!(AgentType::from_str("symphony"), Some(AgentType::Symphony));
        assert_eq!(
            AgentType::from_str("openai-symphony"),
            Some(AgentType::Symphony)
        );
        assert_eq!(AgentType::from_str("unknown"), None);
    }

    #[test]
    fn test_agent_config() {
        let config = AgentConfig::for_agent(AgentType::Claude)
            .with_env("CUSTOM_VAR", "value")
            .with_args(vec!["--flag".to_string()]);

        assert_eq!(config.agent_type, AgentType::Claude);
        assert_eq!(
            config.env_vars.get("CUSTOM_VAR"),
            Some(&"value".to_string())
        );
        assert_eq!(config.args, vec!["--flag".to_string()]);
    }

    #[test]
    fn test_create_agent() {
        let agent = create_agent(AgentType::Claude, None);
        assert_eq!(agent.agent_type(), AgentType::Claude);
        assert_eq!(agent.api_key_env_var(), Some("ANTHROPIC_API_KEY"));
    }
}