roboticus-cli 0.11.4

CLI commands and migration engine for the Roboticus agent runtime
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
use super::*;

// ── Models ───────────────────────────────────────────────────

pub async fn cmd_models_list(base_url: &str, json: bool) -> Result<(), Box<dyn std::error::Error>> {
    let (DIM, BOLD, ACCENT, GREEN, YELLOW, RED, CYAN, RESET, MONO) = colors();
    let (OK, ACTION, WARN, DETAIL, ERR) = icons();
    let resp = super::http_client()?
        .get(format!("{base_url}/api/config"))
        .send()
        .await?;
    let config: serde_json::Value = resp.json().await?;
    if json {
        println!("{}", serde_json::to_string_pretty(&config)?);
        return Ok(());
    }

    println!("\n  {BOLD}Configured Models{RESET}\n");

    let primary = config
        .pointer("/models/primary")
        .and_then(|v| v.as_str())
        .unwrap_or("not set");
    println!("  {:<12} {}", format!("{GREEN}primary{RESET}"), primary);

    if let Some(fallbacks) = config
        .pointer("/models/fallbacks")
        .and_then(|v| v.as_array())
    {
        for (i, fb) in fallbacks.iter().enumerate() {
            let name = fb.as_str().unwrap_or("?");
            println!(
                "  {:<12} {}",
                format!("{YELLOW}fallback {}{RESET}", i + 1),
                name
            );
        }
    }

    let mode = config
        .pointer("/models/routing/mode")
        .and_then(|v| v.as_str())
        .unwrap_or("rule");
    let threshold = config
        .pointer("/models/routing/confidence_threshold")
        .and_then(|v| v.as_f64())
        .unwrap_or(0.9);
    let local_first = config
        .pointer("/models/routing/local_first")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    println!();
    println!(
        "  {DIM}Routing: mode={mode}, threshold={threshold}, local_first={local_first}{RESET}"
    );
    println!();
    Ok(())
}

pub async fn cmd_models_scan(
    base_url: &str,
    provider: Option<&str>,
) -> Result<(), Box<dyn std::error::Error>> {
    let (DIM, BOLD, ACCENT, GREEN, YELLOW, RED, CYAN, RESET, MONO) = colors();
    let (OK, ACTION, WARN, DETAIL, ERR) = icons();
    println!("\n  {BOLD}Scanning for available models...{RESET}\n");

    let resp = super::http_client()?
        .get(format!("{base_url}/api/config"))
        .send()
        .await?;
    let config: serde_json::Value = resp.json().await?;

    let providers = config
        .get("providers")
        .and_then(|v| v.as_object())
        .cloned()
        .unwrap_or_default();

    if providers.is_empty() {
        println!("  No providers configured.");
        println!();
        return Ok(());
    }

    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(10))
        .build()?;

    for (name, prov_config) in &providers {
        if let Some(filter) = provider
            && name != filter
        {
            continue;
        }

        let url = prov_config
            .get("url")
            .and_then(|v| v.as_str())
            .unwrap_or("");

        if url.is_empty() {
            println!("  {YELLOW}{name}{RESET}: no URL configured");
            continue;
        }

        let name_l = name.to_lowercase();
        let url_l = url.to_lowercase();
        let ollama_like = name_l.contains("ollama") || url_l.contains("11434");
        let models_url = if ollama_like {
            format!("{url}/api/tags")
        } else {
            format!("{url}/v1/models")
        };

        let scan_result =
            super::spin_while(&format!("Probing {name}"), client.get(&models_url).send()).await;

        print!("  {CYAN}{name}{RESET} ({url}): ");
        match scan_result {
            Ok(resp) if resp.status().is_success() => {
                let body: serde_json::Value = resp.json().await.unwrap_or_default();
                let models: Vec<String> =
                    if let Some(arr) = body.get("models").and_then(|v| v.as_array()) {
                        arr.iter()
                            .filter_map(|m| {
                                m.get("name")
                                    .or_else(|| m.get("model"))
                                    .and_then(|v| v.as_str())
                            })
                            .map(String::from)
                            .collect()
                    } else if let Some(arr) = body.get("data").and_then(|v| v.as_array()) {
                        arr.iter()
                            .filter_map(|m| m.get("id").and_then(|v| v.as_str()))
                            .map(String::from)
                            .collect()
                    } else {
                        vec![]
                    };

                if models.is_empty() {
                    println!("no models found");
                } else {
                    println!("{} model(s)", models.len());
                    for model in &models {
                        println!("    - {model}");
                    }
                }
            }
            Ok(resp) => {
                println!("{RED}error: {}{RESET}", resp.status());
            }
            Err(e) => {
                println!("{RED}unreachable: {e}{RESET}");
            }
        }
    }

    println!();
    Ok(())
}

/// Exercise a model across the task class matrix (5 complexity x 4 intent)
/// to populate per-(model, intent_class) quality observations.
///
/// `iterations` controls how many full matrix passes to run. Each pass is
/// 20 prompts. Use iterations=20 for 100 observations per intent class.
pub async fn cmd_models_exercise(
    base_url: &str,
    model: &str,
    iterations: usize,
) -> Result<(), Box<dyn std::error::Error>> {
    let (_dim, bold, _accent, green, yellow, red, cyan, reset, _mono) = colors();
    let (ok, _action, warn, _detail, err) = icons();
    let total_prompts = roboticus_llm::exercise::EXERCISE_MATRIX.len() * iterations;
    println!(
        "\n  {bold}Exercising model: {cyan}{model}{reset} ({iterations} iteration(s), {total_prompts} prompts)\n"
    );

    let (pass, fail) = exercise_single_model_iterations(base_url, model, iterations).await;

    println!();
    let fail_color = if fail > 0 { red } else { _dim };
    println!(
        "  {bold}Results:{reset} {green}{pass} passed{reset}, {fail_color}{fail} failed{reset}",
    );
    let obs_per_cell = iterations * 5; // 5 prompts per intent class
    println!("  Observations per intent class: {obs_per_cell}");
    if fail == 0 {
        println!("  {ok} Quality observations recorded for all {pass} prompts.");
    } else {
        println!("  {warn} Some prompts failed — partial observations recorded.");
    }
    println!();
    Ok(())
}

/// Suggest a fallback chain based on available providers and discovered models.
pub async fn cmd_models_suggest(base_url: &str) -> Result<(), Box<dyn std::error::Error>> {
    let (_dim, bold, _accent, green, _yellow, _red, cyan, reset, _mono) = colors();
    let (_ok, _action, warn, _detail, _err) = icons();
    println!("\n  {bold}Scanning for available models...{reset}\n");

    let resp = super::http_client()?
        .get(format!("{base_url}/api/config"))
        .send()
        .await?;
    let config: serde_json::Value = resp.json().await?;

    let providers = config
        .get("providers")
        .and_then(|v| v.as_object())
        .cloned()
        .unwrap_or_default();

    if providers.is_empty() {
        println!("  {warn} No providers configured. Nothing to suggest.");
        println!();
        return Ok(());
    }

    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(10))
        .build()?;

    let mut available: Vec<(String, bool, f64)> = Vec::new();

    for (name, prov_config) in &providers {
        let url = prov_config
            .get("url")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        if url.is_empty() {
            continue;
        }
        let is_local = prov_config
            .get("is_local")
            .and_then(|v| v.as_bool())
            .unwrap_or_else(|| {
                let nl = name.to_lowercase();
                nl.contains("ollama") || nl.contains("local") || nl.contains("lmstudio")
            });
        let cost = prov_config
            .get("cost_per_input_token")
            .and_then(|v| v.as_f64())
            .unwrap_or(0.0)
            + prov_config
                .get("cost_per_output_token")
                .and_then(|v| v.as_f64())
                .unwrap_or(0.0);

        let name_l = name.to_lowercase();
        let url_l = url.to_lowercase();
        let ollama_like = name_l.contains("ollama") || url_l.contains("11434");
        let models_url = if ollama_like {
            format!("{url}/api/tags")
        } else {
            format!("{url}/v1/models")
        };

        if let Ok(resp) = client.get(&models_url).send().await
            && resp.status().is_success()
        {
            let body: serde_json::Value = resp.json().await.unwrap_or_default();
            let models: Vec<String> =
                if let Some(arr) = body.get("models").and_then(|v| v.as_array()) {
                    arr.iter()
                        .filter_map(|m| {
                            m.get("name")
                                .or_else(|| m.get("model"))
                                .and_then(|v| v.as_str())
                        })
                        .map(|m| format!("{name}/{m}"))
                        .collect()
                } else if let Some(arr) = body.get("data").and_then(|v| v.as_array()) {
                    arr.iter()
                        .filter_map(|m| m.get("id").and_then(|v| v.as_str()))
                        .map(|m| format!("{name}/{m}"))
                        .collect()
                } else {
                    vec![]
                };

            for model in models {
                available.push((model, is_local, cost));
            }
        }
    }

    if available.is_empty() {
        println!("  {warn} No models discovered from any provider.");
        println!();
        return Ok(());
    }

    // Rank: local models first, then cloud by cost ascending.
    available.sort_by(|a, b| {
        b.1.cmp(&a.1)
            .then(a.2.partial_cmp(&b.2).unwrap_or(std::cmp::Ordering::Equal))
    });

    println!("  {bold}Suggested fallback chain:{reset}\n");
    for (i, (model, is_local, _cost)) in available.iter().take(6).enumerate() {
        let role = if i == 0 {
            "primary  ".to_string()
        } else {
            format!("fallback{i}")
        };
        let locality = if *is_local {
            format!("{green}local{reset}")
        } else {
            format!("{cyan}cloud{reset}")
        };
        println!("  {role:<10} {model}  ({locality})");
    }

    println!("\n  {_dim}TOML:{reset}\n");
    if let Some((primary, _, _)) = available.first() {
        println!("  [models]");
        println!("  primary = \"{primary}\"");
        let fallbacks: Vec<&str> = available
            .iter()
            .skip(1)
            .take(5)
            .map(|(m, _, _)| m.as_str())
            .collect();
        if !fallbacks.is_empty() {
            println!("  fallbacks = {fallbacks:?}");
        }
    }

    println!();
    Ok(())
}

/// Reset quality observations for a model (or all) to allow re-benchmarking.
pub async fn cmd_models_reset(
    base_url: &str,
    model: Option<&str>,
) -> Result<(), Box<dyn std::error::Error>> {
    let (_dim, bold, _accent, green, _yellow, _red, _cyan, reset, _mono) = colors();
    let (ok, _action, _warn, _detail, _err) = icons();
    let client = super::http_client()?;
    let mut req = client.post(format!("{base_url}/api/models/reset"));
    if let Some(m) = model {
        req = req.query(&[("model", m)]);
    }
    let resp = req.send().await?;
    let data: serde_json::Value = resp.json().await?;
    let msg = data["message"].as_str().unwrap_or("done");
    println!("\n  {bold}{ok}{reset} {green}{msg}{reset}\n");
    if model.is_some() {
        println!(
            "  Run {bold}roboticus models exercise {}{reset} to re-benchmark.",
            model.unwrap_or("?")
        );
    } else {
        println!("  Run {bold}roboticus models exercise <model>{reset} per model to re-benchmark.");
    }
    println!();
    Ok(())
}

/// Full baseline: flush all scores, scan providers, exercise every model.
pub async fn cmd_models_baseline(base_url: &str) -> Result<(), Box<dyn std::error::Error>> {
    let (_dim, bold, _accent, green, yellow, red, cyan, reset, _mono) = colors();
    let (ok, _action, warn, _detail, err) = icons();

    // Step 1: Discover available models
    println!("\n  {bold}Step 1: Discovering available models...{reset}\n");
    let resp = super::http_client()?
        .get(format!("{base_url}/api/config"))
        .send()
        .await?;
    let config: serde_json::Value = resp.json().await?;
    let mut configured: Vec<String> = Vec::new();
    if let Some(primary) = config.pointer("/models/primary").and_then(|v| v.as_str()) {
        configured.push(primary.to_string());
    }
    if let Some(fbs) = config
        .pointer("/models/fallbacks")
        .and_then(|v| v.as_array())
    {
        for fb in fbs {
            if let Some(name) = fb.as_str()
                && !name.is_empty()
                && !configured.contains(&name.to_string())
            {
                configured.push(name.to_string());
            }
        }
    }

    if configured.is_empty() {
        println!("  {warn} No models configured. Nothing to baseline.");
        return Ok(());
    }

    println!(
        "  Found {bold}{}{reset} configured model(s):\n",
        configured.len()
    );
    for (i, model) in configured.iter().enumerate() {
        let role = if i == 0 { "primary" } else { "fallback" };
        println!("    {cyan}{role:<10}{reset} {model}");
    }

    // Step 2: Confirm with user
    println!();
    print!(
        "  This will flush all quality scores and exercise each model \
         across 20 prompts.\n  Proceed? [Y/n] "
    );
    use std::io::Write;
    std::io::stdout().flush().ok();
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).ok();
    let answer = input.trim().to_lowercase();
    if !answer.is_empty() && !matches!(answer.as_str(), "y" | "yes") {
        println!("  Cancelled.");
        return Ok(());
    }

    // Step 3: Flush all scores
    println!("\n  {bold}Step 2: Flushing all quality scores...{reset}");
    let resp = super::http_client()?
        .post(format!("{base_url}/api/models/reset"))
        .send()
        .await?;
    let data: serde_json::Value = resp.json().await?;
    let cleared = data["cleared"].as_u64().unwrap_or(0);
    println!("  {ok} Cleared {cleared} observation entries.\n");

    // Step 4: Exercise each model
    println!("  {bold}Step 3: Exercising models...{reset}\n");
    let mut results: Vec<(String, usize, usize)> = Vec::new();
    for model in &configured {
        println!("  {cyan}--- {model} ---{reset}");
        let (pass, fail) = exercise_single_model_iterations(base_url, model, 20).await;
        results.push((model.clone(), pass, fail));
        println!();
    }

    // Step 5: Summary
    println!("  {bold}Baseline Results:{reset}\n");
    for (model, pass, fail) in &results {
        let status = if *fail == 0 {
            format!("{green}{ok}{reset}")
        } else {
            format!("{yellow}{warn}{reset}")
        };
        println!(
            "    {status} {model}: {green}{pass} passed{reset}, {}{fail} failed{reset}",
            if *fail > 0 { red } else { _dim }
        );
    }
    println!();
    Ok(())
}

async fn exercise_single_model_iterations(
    base_url: &str,
    model: &str,
    iterations: usize,
) -> (usize, usize) {
    let (_dim, bold, _accent, green, _yellow, red, _cyan, reset, _mono) = colors();
    let (ok, _action, _warn, _detail, err) = icons();
    let matrix = roboticus_llm::exercise::EXERCISE_MATRIX;
    // Use a longer timeout than the default 10s — local models can take 60-120s
    let client = match reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(180))
        .build()
    {
        Ok(c) => c,
        Err(_) => return (0, matrix.len() * iterations),
    };
    let mut pass = 0usize;
    let mut fail = 0usize;
    let total = matrix.len() * iterations;

    // Create a dedicated session for exercising (avoids scope_mode errors)
    let session_id: String = match client
        .post(format!("{base_url}/api/sessions"))
        .json(&serde_json::json!({}))
        .send()
        .await
    {
        Ok(resp) => resp
            .json::<serde_json::Value>()
            .await
            .ok()
            .and_then(|v| {
                v.get("session_id")
                    .or_else(|| v.get("id"))
                    .and_then(|s| s.as_str())
                    .map(String::from)
            })
            .unwrap_or_default(),
        Err(_) => String::new(),
    };

    // Per-intent latency tracking: (intent_class, Vec<latency_ms>)
    let mut latencies: std::collections::HashMap<String, Vec<u64>> =
        std::collections::HashMap::new();

    for iter in 0..iterations {
        for (i, prompt) in matrix.iter().enumerate() {
            let n = iter * matrix.len() + i + 1;
            let label = format!(
                "[{n}/{total}] {}:{}",
                prompt.complexity, prompt.intent_class
            );
            eprint!("    {_dim}{label}{reset} ... ");

            let mut body = serde_json::json!({
                "content": prompt.prompt,
                "model_override": model,
            });
            if !session_id.is_empty() {
                body["session_id"] = serde_json::Value::String(session_id.clone());
            }

            let started = std::time::Instant::now();
            let result = tokio::time::timeout(
                std::time::Duration::from_secs(600),
                client
                    .post(format!("{base_url}/api/agent/message"))
                    .json(&body)
                    .send(),
            )
            .await;
            let elapsed_ms = started.elapsed().as_millis() as u64;

            match result {
                Ok(Ok(resp)) if resp.status().is_success() => {
                    pass += 1;
                    latencies
                        .entry(prompt.intent_class.to_string())
                        .or_default()
                        .push(elapsed_ms);
                    let secs = elapsed_ms as f64 / 1000.0;
                    eprintln!("{green}{ok}{reset} {_dim}{secs:.1}s{reset}");
                }
                Ok(Ok(resp)) => {
                    fail += 1;
                    let status = resp.status();
                    eprintln!("{red}{err} {status}{reset}");
                }
                Ok(Err(e)) => {
                    fail += 1;
                    eprintln!("{red}{err} {e}{reset}");
                }
                Err(_) => {
                    fail += 1;
                    eprintln!("{red}{err} timeout (>600s){reset}");
                }
            }
        }
    }

    // Print latency scorecard
    if !latencies.is_empty() {
        eprintln!();
        eprintln!("    {_dim}┌──────────────────┬────────┬────────┬────────┐{reset}");
        eprintln!("    {_dim}│ Intent Class     │  Avg   │  P50   │  P95   │{reset}");
        eprintln!("    {_dim}├──────────────────┼────────┼────────┼────────┤{reset}");
        let mut all_latencies: Vec<u64> = Vec::new();
        let mut intents: Vec<_> = latencies.iter().collect();
        intents.sort_by_key(|(k, _)| (*k).clone());
        for (intent, times) in &intents {
            all_latencies.extend(times.iter().copied());
            let mut sorted = (*times).clone();
            sorted.sort();
            let avg = sorted.iter().sum::<u64>() as f64 / sorted.len() as f64 / 1000.0;
            let p50 = sorted[sorted.len() / 2] as f64 / 1000.0;
            let p95_idx = (sorted.len() as f64 * 0.95) as usize;
            let p95 = sorted[p95_idx.min(sorted.len() - 1)] as f64 / 1000.0;
            eprintln!(
                "    {_dim}{reset} {:<16} {_dim}{reset} {avg:5.1}s {_dim}{reset} {p50:5.1}s {_dim}{reset} {p95:5.1}s {_dim}{reset}",
                intent
            );
        }
        all_latencies.sort();
        if !all_latencies.is_empty() {
            let avg_all =
                all_latencies.iter().sum::<u64>() as f64 / all_latencies.len() as f64 / 1000.0;
            let p50_all = all_latencies[all_latencies.len() / 2] as f64 / 1000.0;
            let p95_idx = (all_latencies.len() as f64 * 0.95) as usize;
            let p95_all = all_latencies[p95_idx.min(all_latencies.len() - 1)] as f64 / 1000.0;
            eprintln!("    {_dim}├──────────────────┼────────┼────────┼────────┤{reset}");
            eprintln!(
                "    {_dim}{reset} {bold}ALL{reset}              {_dim}{reset} {avg_all:5.1}s {_dim}{reset} {p50_all:5.1}s {_dim}{reset} {p95_all:5.1}s {_dim}{reset}"
            );
        }
        eprintln!("    {_dim}└──────────────────┴────────┴────────┴────────┘{reset}");
    }

    (pass, fail)
}