agentoven-cli 0.6.0

CLI for AgentOven — bake production-ready AI agents from the terminal
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
//! `agentoven apply` — declarative resource management via YAML/JSON/TOML manifests.
//!
//! Supports multi-document YAML (`---` separators) and single-document JSON/TOML.
//! Each document must have a `kind` field to indicate the resource type.
//!
//! # Example YAML
//!
//! ```yaml
//! kind: Agent
//! name: summarizer
//! description: "Summarizes documents"
//! model_provider: openai
//! model_name: gpt-4o
//! ---
//! kind: ToolSet
//! tools:
//!   - name: web-search
//!     description: "Search the web"
//!     endpoint: https://tools.example.com/search
//! ---
//! kind: Recipe
//! name: support-flow
//! steps:
//!   - name: classify
//!     agent: classifier
//!     kind: agent
//! ```

use clap::Args;
use colored::Colorize;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

use agentoven_core::agent::{Agent, AgentFramework, AgentMode};
use agentoven_core::ingredient::Ingredient;
use agentoven_core::recipe::{Recipe, Step, StepKind};
use uuid::Uuid;

// ── Manifest Types ──────────────────────────────────────────

/// Top-level discriminator for manifest documents.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "kind")]
pub enum Manifest {
    Agent(AgentManifest),
    Recipe(RecipeManifest),
    ToolSet(ToolSetManifest),
}

/// Agent manifest — mirrors the agent register fields.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AgentManifest {
    pub name: String,
    #[serde(default)]
    pub version: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub framework: Option<String>,
    #[serde(default)]
    pub mode: Option<String>,
    #[serde(default)]
    pub model_provider: Option<String>,
    #[serde(default)]
    pub model_name: Option<String>,
    #[serde(default)]
    pub backup_provider: Option<String>,
    #[serde(default)]
    pub backup_model: Option<String>,
    #[serde(default)]
    pub system_prompt: Option<String>,
    #[serde(default)]
    pub max_turns: Option<u32>,
    #[serde(default)]
    pub a2a_endpoint: Option<String>,
    #[serde(default)]
    pub skills: Option<Vec<String>>,
    #[serde(default)]
    pub tags: Option<Vec<String>>,
    #[serde(default)]
    pub ingredients: Option<IngredientsManifest>,
}

/// Ingredients section within an agent manifest.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IngredientsManifest {
    #[serde(default)]
    pub models: Option<Vec<ModelIngredient>>,
    #[serde(default)]
    pub tools: Option<Vec<ToolIngredient>>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ModelIngredient {
    pub name: String,
    #[serde(default)]
    pub provider: Option<String>,
    #[serde(default)]
    pub role: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ToolIngredient {
    pub name: String,
    #[serde(default)]
    pub protocol: Option<String>,
}

/// Recipe manifest — maps to recipe creation.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RecipeManifest {
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub steps: Vec<StepManifest>,
}

/// A step within a recipe manifest.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StepManifest {
    pub name: String,
    #[serde(default)]
    pub agent: Option<String>,
    #[serde(default, rename = "kind")]
    pub step_kind: Option<String>,
    #[serde(default)]
    pub input: Option<String>,
    #[serde(default)]
    pub depends_on: Option<Vec<String>>,
    #[serde(default)]
    pub condition: Option<String>,
    #[serde(default)]
    pub retries: Option<u32>,
    #[serde(default)]
    pub timeout_secs: Option<u64>,
    #[serde(default)]
    pub parallel_inputs: Option<Vec<String>>,
    #[serde(default)]
    pub routes: Option<Vec<RouteManifest>>,
    #[serde(default)]
    pub sub_recipe: Option<String>,
}

/// A route within a router step.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RouteManifest {
    pub condition: String,
    pub target: String,
}

/// ToolSet manifest — bulk tool registration.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ToolSetManifest {
    pub tools: Vec<ToolManifestEntry>,
}

/// A single tool in a ToolSet.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ToolManifestEntry {
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub endpoint: Option<String>,
    #[serde(default)]
    pub transport: Option<String>,
    #[serde(default)]
    pub schema: Option<JsonValue>,
    #[serde(default)]
    pub capabilities: Option<Vec<String>>,
}

// ── CLI Args ────────────────────────────────────────────────

#[derive(Args)]
pub struct ApplyArgs {
    /// Path to manifest file (YAML, JSON, or TOML).
    #[arg(short = 'f', long = "file")]
    pub file: String,

    /// Preview changes without applying (dry-run).
    #[arg(long)]
    pub dry_run: bool,
}

// ── Execute ─────────────────────────────────────────────────

pub async fn execute(args: ApplyArgs) -> anyhow::Result<()> {
    println!("\n  🏺 Applying manifest: {}\n", args.file.cyan());

    let content = tokio::fs::read_to_string(&args.file).await?;
    let manifests = parse_manifests(&args.file, &content)?;

    if manifests.is_empty() {
        anyhow::bail!("No valid documents found in {}", args.file);
    }

    println!(
        "  {} Parsed {} resource(s):",
        "".dimmed(),
        manifests.len()
    );

    for m in &manifests {
        match m {
            Manifest::Agent(a) => println!("    {} Agent/{}", "".dimmed(), a.name.bold()),
            Manifest::Recipe(r) => println!(
                "    {} Recipe/{} ({} steps)",
                "".dimmed(),
                r.name.bold(),
                r.steps.len()
            ),
            Manifest::ToolSet(t) => println!(
                "    {} ToolSet ({} tools)",
                "".dimmed(),
                t.tools.len()
            ),
        }
    }
    println!();

    if args.dry_run {
        println!(
            "  {} Dry run — no changes applied.",
            "".blue().bold()
        );
        return Ok(());
    }

    let client = agentoven_core::AgentOvenClient::from_env()?;

    let mut success = 0u32;
    let mut failed = 0u32;

    for m in manifests {
        match m {
            Manifest::Agent(a) => match apply_agent(&client, a).await {
                Ok(name) => {
                    println!("  {} Agent/{} applied", "".green().bold(), name);
                    success += 1;
                }
                Err(e) => {
                    println!(
                        "  {} Agent apply failed: {}",
                        "".red().bold(),
                        e.to_string().dimmed()
                    );
                    failed += 1;
                }
            },
            Manifest::Recipe(r) => match apply_recipe(&client, r).await {
                Ok(name) => {
                    println!("  {} Recipe/{} applied", "".green().bold(), name);
                    success += 1;
                }
                Err(e) => {
                    println!(
                        "  {} Recipe apply failed: {}",
                        "".red().bold(),
                        e.to_string().dimmed()
                    );
                    failed += 1;
                }
            },
            Manifest::ToolSet(t) => match apply_toolset(&client, t).await {
                Ok(count) => {
                    println!(
                        "  {} ToolSet applied ({} tools)",
                        "".green().bold(),
                        count
                    );
                    success += 1;
                }
                Err(e) => {
                    println!(
                        "  {} ToolSet apply failed: {}",
                        "".red().bold(),
                        e.to_string().dimmed()
                    );
                    failed += 1;
                }
            },
        }
    }

    println!();
    if failed == 0 {
        println!(
            "  {} All {} resource(s) applied successfully.",
            "".green().bold(),
            success
        );
    } else {
        println!(
            "  {} {} applied, {} failed.",
            "".yellow().bold(),
            success,
            failed
        );
    }

    Ok(())
}

// ── Parsing ─────────────────────────────────────────────────

fn parse_manifests(path: &str, content: &str) -> anyhow::Result<Vec<Manifest>> {
    if path.ends_with(".yaml") || path.ends_with(".yml") {
        parse_yaml_manifests(content)
    } else if path.ends_with(".json") {
        parse_json_manifest(content)
    } else if path.ends_with(".toml") {
        parse_toml_manifest(content)
    } else {
        // Try YAML first (most common for multi-doc), then JSON, then TOML
        parse_yaml_manifests(content)
            .or_else(|_| parse_json_manifest(content))
            .or_else(|_| parse_toml_manifest(content))
            .map_err(|_| {
                anyhow::anyhow!(
                    "Could not detect format. Use .yaml, .json, or .toml extension."
                )
            })
    }
}

/// Parse YAML with multi-document support (--- separators).
fn parse_yaml_manifests(content: &str) -> anyhow::Result<Vec<Manifest>> {
    let mut manifests = Vec::new();

    for doc in serde_yaml::Deserializer::from_str(content) {
        let value = serde_yaml::Value::deserialize(doc)?;
        let manifest: Manifest = serde_yaml::from_value(value)?;
        manifests.push(manifest);
    }

    if manifests.is_empty() {
        anyhow::bail!("No YAML documents found");
    }

    Ok(manifests)
}

fn parse_json_manifest(content: &str) -> anyhow::Result<Vec<Manifest>> {
    // Try array first
    if let Ok(arr) = serde_json::from_str::<Vec<Manifest>>(content) {
        return Ok(arr);
    }
    // Then single document
    let m: Manifest = serde_json::from_str(content)?;
    Ok(vec![m])
}

fn parse_toml_manifest(content: &str) -> anyhow::Result<Vec<Manifest>> {
    // TOML doesn't support multi-document, so parse as single
    let value: toml::Value = content.parse()?;

    // TOML needs the `kind` key to determine type. Convert via JSON for serde(tag).
    let json_str = serde_json::to_string(&value)?;
    let m: Manifest = serde_json::from_str(&json_str)?;
    Ok(vec![m])
}

// ── Applying ────────────────────────────────────────────────

fn parse_framework(s: &str) -> AgentFramework {
    match s.to_lowercase().as_str() {
        "langchain" | "langgraph" => AgentFramework::Langchain,
        "crewai" => AgentFramework::Crewai,
        "openai" | "openai-sdk" => AgentFramework::Openai,
        "autogen" => AgentFramework::Autogen,
        "managed" => AgentFramework::Managed,
        _ => AgentFramework::Custom,
    }
}

async fn apply_agent(
    client: &agentoven_core::AgentOvenClient,
    manifest: AgentManifest,
) -> anyhow::Result<String> {
    let name = manifest.name.clone();

    let framework = parse_framework(manifest.framework.as_deref().unwrap_or("managed"));
    let mode = match manifest.mode.as_deref() {
        Some("external") => AgentMode::External,
        _ => AgentMode::Managed,
    };

    let mut builder = Agent::builder(&manifest.name)
        .version(manifest.version.as_deref().unwrap_or("0.1.0"))
        .description(manifest.description.as_deref().unwrap_or(""))
        .framework(framework)
        .mode(mode)
        .model_provider(manifest.model_provider.as_deref().unwrap_or(""))
        .model_name(manifest.model_name.as_deref().unwrap_or(""));

    if let Some(bp) = &manifest.backup_provider {
        builder = builder.backup_provider(bp);
    }
    if let Some(bm) = &manifest.backup_model {
        builder = builder.backup_model(bm);
    }
    if let Some(sp) = &manifest.system_prompt {
        builder = builder.system_prompt(sp);
    }
    if let Some(mt) = manifest.max_turns {
        builder = builder.max_turns(mt);
    }
    if let Some(ep) = &manifest.a2a_endpoint {
        builder = builder.a2a_endpoint(ep);
    }
    if let Some(skills) = &manifest.skills {
        for s in skills {
            builder = builder.skill(s);
        }
    }
    if let Some(tags) = &manifest.tags {
        for t in tags {
            builder = builder.tag(t);
        }
    }

    // Parse ingredients
    let mut ingredients = Vec::new();
    if let Some(ing) = &manifest.ingredients {
        if let Some(models) = &ing.models {
            for m in models {
                let mut ib = Ingredient::model(&m.name);
                if let Some(p) = &m.provider {
                    ib = ib.provider(p);
                }
                if let Some(r) = &m.role {
                    ib = ib.role(r);
                }
                ingredients.push(ib.build());
            }
        }
        if let Some(tools) = &ing.tools {
            for t in tools {
                let mut ib = Ingredient::tool(&t.name);
                if let Some(p) = &t.protocol {
                    ib = ib.provider(p);
                }
                ingredients.push(ib.build());
            }
        }
    }

    let agent = ingredients
        .into_iter()
        .fold(builder, |a, i| a.ingredient(i))
        .build();

    client.register(&agent).await?;
    Ok(name)
}

async fn apply_recipe(
    client: &agentoven_core::AgentOvenClient,
    manifest: RecipeManifest,
) -> anyhow::Result<String> {
    let name = manifest.name.clone();

    let steps: Vec<Step> = manifest
        .steps
        .into_iter()
        .map(|s| {
            let kind = match s.step_kind.as_deref() {
                Some("human-gate") | Some("gate") => StepKind::HumanGate,
                Some("evaluator") => StepKind::Evaluator,
                Some("condition") | Some("branch") => StepKind::Condition,
                Some("fan-out") | Some("parallel") | Some("map") => StepKind::FanOut,
                Some("fan-in") | Some("join") => StepKind::FanIn,
                _ => StepKind::Agent,
            };
            Step {
                id: Uuid::new_v4().to_string(),
                name: s.name,
                agent: s.agent,
                kind,
                parallel: false,
                timeout: s.timeout_secs.map(|s| format!("{}s", s)),
                depends_on: s.depends_on.unwrap_or_default(),
                retry: None,
                notify: Vec::new(),
                config: None,
            }
        })
        .collect();

    let recipe = Recipe::new(&manifest.name, steps);
    client.create_recipe(&recipe).await?;
    Ok(name)
}

async fn apply_toolset(
    client: &agentoven_core::AgentOvenClient,
    manifest: ToolSetManifest,
) -> anyhow::Result<usize> {
    let count = manifest.tools.len();

    // Try bulk endpoint first, fall back to individual POSTs
    let tools_json: Vec<JsonValue> = manifest
        .tools
        .iter()
        .map(|t| {
            let mut obj = serde_json::json!({ "name": t.name });
            if let Some(desc) = &t.description {
                obj["description"] = serde_json::json!(desc);
            }
            if let Some(ep) = &t.endpoint {
                obj["endpoint"] = serde_json::json!(ep);
            }
            if let Some(tr) = &t.transport {
                obj["transport"] = serde_json::json!(tr);
            }
            if let Some(s) = &t.schema {
                obj["schema"] = s.clone();
            }
            if let Some(caps) = &t.capabilities {
                obj["capabilities"] = serde_json::json!(caps);
            }
            obj
        })
        .collect();

    // Try bulk endpoint
    match client.bulk_add_tools(serde_json::json!({ "tools": tools_json })).await {
        Ok(_) => return Ok(count),
        Err(_) => {
            // Fall back to individual adds
            for tool_json in tools_json {
                client.add_tool(tool_json).await?;
            }
        }
    }

    Ok(count)
}