granite-cli 0.1.4

CLI for discovering, configuring, and launching AI workflows powered by IBM Granite models.
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
// Third Party
use alog::{MessageLevel, alog_channel, use_channel};
use anyhow::Result;

// Local
use super::{ModelCommands, ProviderCommands};
use crate::capabilities::{CAPABILITY_REGISTRY, Dependency, ModelRequirement, ProviderRequirement};
use crate::dependency::{self, Configured};
use crate::utils::prompt_from_schema;

pub struct CapabilityCommands;

use_channel!("CAPBL");

impl CapabilityCommands {
    pub fn catalog(ctx: &crate::AppContext) -> Result<()> {
        let capabilities = CAPABILITY_REGISTRY.entries();

        let mut rows: Vec<Vec<String>> = capabilities
            .iter()
            .map(|(cap_id, cap)| {
                let deps: Vec<_> = cap.dependencies.iter().map(|d| d.to_string()).collect();
                let deps_str = if deps.is_empty() {
                    "None".to_string()
                } else {
                    deps.join(", ")
                };
                vec![cap_id.to_string(), cap.name.clone(), deps_str]
            })
            .collect();
        rows.sort_by(|a, b| a[0].cmp(&b[0]));

        ctx.ui.table(
            &format!("Capability Catalog ({} capabilities)", capabilities.len()),
            &["ID", "NAME", "DEPENDENCIES"],
            &rows,
        );
        Ok(())
    }

    pub fn list(ctx: &crate::AppContext) -> Result<()> {
        let mut rows: Vec<Vec<String>> = ctx
            .config
            .capabilities
            .iter()
            .map(|(id, cfg)| vec![id.clone(), cfg.capability_type.clone()])
            .collect();
        rows.sort_by(|a, b| {
            let type_cmp = a[1].cmp(&b[1]);
            if type_cmp != std::cmp::Ordering::Equal {
                return type_cmp;
            }
            a[0].cmp(&b[0])
        });

        ctx.ui.table(
            &format!("Configured Capabilities ({} capabilities)", rows.len()),
            &["ID", "TYPE"],
            &rows,
        );
        Ok(())
    }

    pub fn info(ctx: &crate::AppContext, capability_id: &str) -> Result<()> {
        let configured = ctx.config.get_capability(capability_id);

        let catalog_entry = configured
            .and_then(|c| CAPABILITY_REGISTRY.get(&c.capability_type))
            .or_else(|| CAPABILITY_REGISTRY.get(capability_id));

        match catalog_entry {
            Some(cap) => {
                let mut fields: Vec<(&str, String)> = vec![
                    ("Name", cap.name.clone()),
                    ("Description", cap.description.clone()),
                ];

                if !cap.tags.is_empty() {
                    fields.push(("Tags", cap.tags.join(", ")));
                }

                if let Some(configured) = configured {
                    fields.push(("Type", configured.capability_type.clone()));
                    if let Some(obj) = configured.config.as_object() {
                        for (k, v) in obj {
                            fields.push(("Config", format!("{k} = {v}")));
                        }
                    }
                }

                ctx.ui.detail(capability_id, &fields);
                Ok(())
            }
            None => {
                if configured.is_some() {
                    let fields: Vec<(&str, String)> = vec![(
                        "Note",
                        "Configured but its type is not found in the bundled registry.".to_string(),
                    )];
                    ctx.ui.detail(capability_id, &fields);
                    Ok(())
                } else {
                    ctx.ui.error(&format!(
                        "Capability '{capability_id}' not found in registry."
                    ));
                    anyhow::bail!("Capability not found");
                }
            }
        }
    }

    /// Interactive capability setup wizard.
    ///
    /// `capability_type` is the catalog/registry key (e.g. `agent-model`).
    /// `instance_id` is the nickname for this instance; defaults to
    /// `capability_type` when not given.
    pub async fn setup(
        ctx: &mut crate::AppContext,
        capability_type: &str,
        instance_id: Option<&str>,
    ) -> Result<()> {
        let cap_def = match CAPABILITY_REGISTRY.get(capability_type) {
            Some(def) => def,
            None => {
                ctx.ui.error(&format!(
                    "Capability type '{capability_type}' not found in registry."
                ));
                let available: Vec<String> = {
                    let mut entries: Vec<String> = CAPABILITY_REGISTRY
                        .entries()
                        .iter()
                        .map(|(id, c)| format!("{} ({})", id, c.name))
                        .collect();
                    entries.sort();
                    entries
                };
                ctx.ui
                    .info(&format!("Available types: {}", available.join(", ")));
                anyhow::bail!("Capability type not found");
            }
        };

        ctx.ui
            .info(&format!("\nSetting up capability: {capability_type}"));
        ctx.ui.info(&cap_def.description);

        let instance_id = match instance_id {
            Some(id) => id.to_string(),
            None => ctx.ui.text("Instance name: ", capability_type)?,
        };

        let existing_config = ctx.config.get_capability(&instance_id);
        if existing_config.is_some() {
            let overwrite = ctx.ui.confirm(
                &format!("Capability '{instance_id}' is already configured. Overwrite?"),
                false,
            )?;
            if !overwrite {
                ctx.ui.info("Capability setup skipped.");
                return Ok(());
            }
        }

        let mut schema = CAPABILITY_REGISTRY
            .config_schema(capability_type)
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "No config schema registered for capability type '{capability_type}'"
                )
            })?;
        let defaults = existing_config
            .map(|c| c.config.clone())
            .or_else(|| CAPABILITY_REGISTRY.default_config(capability_type))
            .unwrap_or_else(|| serde_json::json!({}));

        // Phase A: prompt for everything except dependency-resolved fields --
        // those are picked from configured instances below, never free-typed.
        let dependency_keys: std::collections::HashSet<&str> = cap_def
            .dependencies
            .iter()
            .filter_map(|d| match d {
                Dependency::Model { config_key, .. } | Dependency::Provider { config_key, .. } => {
                    Some(config_key.as_str())
                }
                Dependency::ExternalTool { .. } => None,
            })
            .collect();
        if let Some(serde_json::Value::Object(props)) = schema.get_mut("properties") {
            props.retain(|k, _| !dependency_keys.contains(k.as_str()));
        }
        if let Some(serde_json::Value::Array(req)) = schema.get_mut("required") {
            req.retain(|v| !v.as_str().is_some_and(|s| dependency_keys.contains(s)));
        }
        let mut config = prompt_from_schema(&*ctx.ui, &schema, &defaults)?;

        // Phase B: resolve the capability's dependencies (model/provider)
        // against currently configured models/providers. Use the capability
        // definition's metadata rather than a preview instance, since some
        // dependency fields (like model_id) may not be set yet.
        for dep in &cap_def.dependencies {
            match dep {
                Dependency::Model {
                    config_key,
                    requirement,
                    required,
                    ..
                } => {
                    if let Some(id) =
                        Self::resolve_model_dependency(ctx, requirement, *required).await?
                    {
                        config
                            // NOTE: Safe since config MUST be an object when registered
                            .as_object_mut()
                            .unwrap()
                            .insert(config_key.clone(), serde_json::Value::String(id));
                    }
                }
                Dependency::Provider {
                    config_key,
                    requirement,
                    required,
                    ..
                } => {
                    if let Some(id) =
                        Self::resolve_provider_dependency(ctx, requirement, *required).await?
                    {
                        config
                            // NOTE: Safe since config MUST be an object when registered
                            .as_object_mut()
                            .unwrap()
                            .insert(config_key.clone(), serde_json::Value::String(id));
                    }
                }
                Dependency::ExternalTool {
                    requirement,
                    required,
                } => {
                    if *required && !requirement.is_satisfied() {
                        anyhow::bail!(
                            "Required external command '{}' is not available.",
                            requirement.command
                        );
                    }
                }
            }
        }

        let capability_config = crate::config::CapabilityConfig {
            capability_id: instance_id.clone(),
            capability_type: capability_type.to_string(),
            config,
        };

        if let Err(e) = ctx
            .config
            .insert_capability(&instance_id, capability_config)
        {
            ctx.ui
                .warn(&format!("failed to save capability config: {e}"));
        }

        ctx.ui.info(&format!(
            "\nCapability '{instance_id}' configured successfully!"
        ));

        Ok(())
    }

    /// Resolve a capability's model dependency against currently configured
    /// models, narrowed to those whose attached provider also supports every
    /// function the requirement asks for. Always offers a "configure a new
    /// model" option alongside any usable existing instances -- a freshly
    /// configured model is re-checked against the same narrowing before
    /// being accepted, since the user could configure one whose provider
    /// doesn't actually satisfy the requirement. Returns the chosen model
    /// id, or `None` if the dependency isn't required and nothing (existing
    /// or configurable) satisfies it.
    async fn resolve_model_dependency(
        ctx: &mut crate::AppContext,
        requirement: &ModelRequirement,
        required: bool,
    ) -> Result<Option<String>> {
        alog_channel!(
            MessageLevel::Debug2,
            "Resolving requirement: {:?}",
            requirement
        );
        let (usable, configurable_types) = Self::model_candidates(ctx, requirement);
        alog_channel!(
            MessageLevel::Debug2,
            "Usable: {:?}, Configurable Types: {:?}",
            usable,
            configurable_types
        );
        if usable.is_empty() && configurable_types.is_empty() {
            if required {
                anyhow::bail!(
                    "No configured model satisfies this capability's requirements yet, and none can be configured. Configure a compatible model and provider first."
                );
            }
            return Ok(None);
        }

        const CONFIGURE_NEW: &str = "Configure a new model...";
        let mut options = usable;
        let mut configure_new_idx: Option<usize> = None;
        if !configurable_types.is_empty() {
            configure_new_idx = Some(options.len());
            options.push(CONFIGURE_NEW.to_string());
        }

        let choice_idx = if options.len() == 1 {
            0
        } else {
            ctx.ui
                .select("Select a model for this capability:", &options, 0)?
        };
        let choice = options[choice_idx].clone();
        if configure_new_idx.is_none_or(|v| v != choice_idx) {
            return Ok(Some(choice));
        }

        let model_type = if configurable_types.len() == 1 {
            configurable_types[0]
        } else {
            let type_options: Vec<String> =
                configurable_types.iter().map(|s| s.to_string()).collect();
            let index = ctx
                .ui
                .select("Select a model type to configure:", &type_options, 0)?;
            configurable_types[index]
        };

        ModelCommands::setup(ctx, model_type, None).await?;

        let (usable_after, _) = Self::model_candidates(ctx, requirement);
        let new_usable: Vec<_> = usable_after
            .iter()
            .filter(|x| !options.contains(x))
            .collect();
        if new_usable.len() == 1 {
            return Ok(Some(new_usable[0].to_string()));
        }
        if required {
            anyhow::bail!(
                "The newly configured model '{model_type}' does not satisfy this capability's requirements (its provider may not support what's needed). Configure a compatible model/provider combination and try again."
            );
        }
        ctx.ui.warn(&format!(
            "The newly configured model '{model_type}' does not satisfy this capability's requirements; skipping."
        ));
        Ok(None)
    }

    /// Existing configured models that satisfy `requirement` (narrowed to
    /// those whose attached provider also supports every requested
    /// function), and catalog model types that could satisfy it if
    /// configured. Both lists are sorted for deterministic display/tests.
    fn model_candidates(
        ctx: &crate::AppContext,
        requirement: &ModelRequirement,
    ) -> (Vec<String>, Vec<&'static str>) {
        let source = crate::models::ModelSource::from_config(&ctx.config);
        let resolution = dependency::resolve(requirement, &source);
        let instances = source.instances();
        let mut usable: Vec<String> = resolution
            .existing_instances
            .into_iter()
            .filter(|id| {
                instances
                    .iter()
                    .find(|(i, _)| i == id)
                    .is_some_and(|(_, model)| {
                        let model_ok = requirement
                            .supported_functions
                            .iter()
                            .all(|f| model.supported_functions().contains(f));
                        match model.provider() {
                            Ok(p) => {
                                model_ok
                                    && requirement
                                        .supported_functions
                                        .iter()
                                        .all(|f| p.supports_function(f))
                            }
                            Err(_) => false,
                        }
                    })
            })
            .collect();
        usable.sort();
        let mut configurable_types = resolution.configurable_types;
        configurable_types.sort();
        (usable, configurable_types)
    }

    /// Resolve a capability's provider dependency against currently
    /// configured providers. Always offers a "configure a new provider"
    /// option alongside any satisfying existing instances -- a freshly
    /// configured provider is re-checked before being accepted, since the
    /// user could configure one that doesn't actually satisfy the
    /// requirement. Returns the chosen provider id, or `None` if the
    /// dependency isn't required and nothing (existing or configurable)
    /// satisfies it.
    async fn resolve_provider_dependency(
        ctx: &mut crate::AppContext,
        requirement: &ProviderRequirement,
        required: bool,
    ) -> Result<Option<String>> {
        let (existing, configurable_types) = Self::provider_candidates(ctx, requirement);
        if existing.is_empty() && configurable_types.is_empty() {
            if required {
                anyhow::bail!(
                    "No configured provider satisfies this capability's requirements yet, and none can be configured. Configure a compatible provider first."
                );
            }
            return Ok(None);
        }

        const CONFIGURE_NEW: &str = "Configure a new provider...";
        let mut options = existing;
        let mut configure_new_idx: Option<usize> = None;
        if !configurable_types.is_empty() {
            configure_new_idx = Some(options.len());
            options.push(CONFIGURE_NEW.to_string());
        }

        let choice_idx = if options.len() == 1 {
            0
        } else {
            ctx.ui
                .select("Select a provider for this capability:", &options, 0)?
        };
        let choice = options[choice_idx].clone();
        if configure_new_idx.is_none_or(|v| v != choice_idx) {
            return Ok(Some(choice));
        }

        let provider_type = if configurable_types.len() == 1 {
            configurable_types[0]
        } else {
            let type_options: Vec<String> =
                configurable_types.iter().map(|s| s.to_string()).collect();
            let index = ctx
                .ui
                .select("Select a provider type to configure:", &type_options, 0)?;
            configurable_types[index]
        };

        let nickname = ctx.ui.text("Name this provider instance", provider_type)?;
        ProviderCommands::setup(ctx, provider_type, Some(&nickname)).await?;

        let (existing_after, _) = Self::provider_candidates(ctx, requirement);
        if existing_after.contains(&nickname) {
            return Ok(Some(nickname));
        }
        if required {
            anyhow::bail!(
                "The newly configured provider '{nickname}' does not satisfy this capability's requirements. Configure a different provider and try again."
            );
        }
        ctx.ui.warn(&format!(
            "The newly configured provider '{nickname}' does not satisfy this capability's requirements; skipping."
        ));
        Ok(None)
    }

    /// Existing configured providers that satisfy `requirement`, and
    /// catalog provider types that could satisfy it if configured. Both
    /// lists are sorted for deterministic display/tests.
    fn provider_candidates(
        ctx: &crate::AppContext,
        requirement: &ProviderRequirement,
    ) -> (Vec<String>, Vec<&'static str>) {
        let source = crate::providers::ProviderSource::from_config(&ctx.config);
        let resolution = dependency::resolve(requirement, &source);
        let mut existing = resolution.existing_instances;
        existing.sort();
        let mut configurable_types = resolution.configurable_types;
        configurable_types.sort();
        (existing, configurable_types)
    }

    /// Remove a configured capability instance by ID.
    ///
    /// Deletes the capability's config file and removes it from the
    /// in-memory config. After this call `capability list` will no longer
    /// show the entry.
    pub fn remove(ctx: &mut crate::AppContext, capability_id: &str) -> Result<()> {
        if ctx.config.get_capability(capability_id).is_none() {
            anyhow::bail!("No capability configured with id '{capability_id}'. Nothing to remove.");
        }

        if let Err(e) = ctx.config.remove_capability(capability_id) {
            ctx.ui
                .warn(&format!("failed to persist capability removal: {e}"));
        }
        ctx.ui
            .info(&format!("Capability '{capability_id}' removed."));
        Ok(())
    }
}

/*-- tests --*/

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{CapabilityConfig, Config};
    use crate::utils::ui::base::tests::CaptureUi;
    use std::sync::Arc;

    fn test_ctx() -> crate::AppContext {
        crate::AppContext {
            config: Config::default(),
            ui: Arc::new(CaptureUi::default()),
        }
    }

    fn ctx_with_capability(id: &str, capability_type: &str) -> crate::AppContext {
        let mut ctx = test_ctx();
        ctx.config.capabilities.insert(
            id.to_string(),
            CapabilityConfig {
                capability_id: id.to_string(),
                capability_type: capability_type.to_string(),
                config: serde_json::json!({}),
            },
        );
        ctx
    }

    macro_rules! tables {
        ($ctx:expr) => {
            (&*($ctx.ui) as &dyn std::any::Any)
                .downcast_ref::<CaptureUi>()
                .unwrap()
                .tables
                .borrow()
        };
    }

    macro_rules! details {
        ($ctx:expr) => {
            (&*($ctx.ui) as &dyn std::any::Any)
                .downcast_ref::<CaptureUi>()
                .unwrap()
                .details
                .borrow()
        };
    }

    macro_rules! infos {
        ($ctx:expr) => {
            (&*($ctx.ui) as &dyn std::any::Any)
                .downcast_ref::<CaptureUi>()
                .unwrap()
                .infos
                .borrow()
        };
    }

    // -- catalog --------------------------------------------------------------

    #[test]
    fn catalog_table_has_id_name_dependencies_columns() {
        let ctx = test_ctx();
        CapabilityCommands::catalog(&ctx).unwrap();
        let tables = tables!(ctx);
        assert_eq!(tables.len(), 1);
        let (_, headers, _) = &tables[0];
        assert!(headers.contains(&"ID".to_string()));
        assert!(headers.contains(&"NAME".to_string()));
        assert!(headers.contains(&"DEPENDENCIES".to_string()));
    }

    #[test]
    fn catalog_contains_agent_model() {
        let ctx = test_ctx();
        CapabilityCommands::catalog(&ctx).unwrap();
        let tables = tables!(ctx);
        let (_, _, rows) = &tables[0];
        assert!(rows.iter().any(|r| r[0] == "agent-model"));
    }

    // -- list -----------------------------------------------------------------

    #[test]
    fn list_empty_config_has_zero_rows() {
        let ctx = test_ctx();
        CapabilityCommands::list(&ctx).unwrap();
        let tables = tables!(ctx);
        let (_, _, rows) = &tables[0];
        assert_eq!(rows.len(), 0);
    }

    #[test]
    fn list_configured_capability_shows_row() {
        let ctx = ctx_with_capability("my-cap", "agent-model");
        CapabilityCommands::list(&ctx).unwrap();
        let tables = tables!(ctx);
        let (_, _, rows) = &tables[0];
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0][0], "my-cap");
        assert_eq!(rows[0][1], "agent-model");
    }

    // -- info -----------------------------------------------------------------

    #[test]
    fn info_unknown_capability_returns_err() {
        let ctx = test_ctx();
        let result = CapabilityCommands::info(&ctx, "does-not-exist");
        assert!(result.is_err());
    }

    #[test]
    fn info_configured_only_capability_renders_detail_not_err() {
        let ctx = ctx_with_capability("custom-cap", "not-a-real-type");
        let result = CapabilityCommands::info(&ctx, "custom-cap");
        assert!(result.is_ok());
        assert!(!details!(ctx).is_empty());
    }

    #[test]
    fn info_configured_agent_model_resolves_via_catalog_type() {
        let ctx = ctx_with_capability("chat", "agent-model");
        let result = CapabilityCommands::info(&ctx, "chat");
        assert!(result.is_ok());
        assert!(!details!(ctx).is_empty());
    }

    // -- setup ------------------------------------------------------------------

    #[tokio::test]
    async fn setup_unknown_type_returns_err() {
        let mut ctx = test_ctx();
        let result = CapabilityCommands::setup(&mut ctx, "no-such-type", Some("test")).await;
        assert!(result.is_err());
    }

    fn ctx_with_chat_capable_model() -> crate::AppContext {
        use crate::config::{ModelConfig, ProviderConfig};

        let mut ctx = test_ctx();
        ctx.config.providers.insert(
            "ollama".to_string(),
            ProviderConfig {
                provider_id: "ollama".to_string(),
                provider_type: "ollama".to_string(),
                config: serde_json::json!({}),
            },
        );
        ctx.config.models.insert(
            "granite-3.1-8b-instruct".to_string(),
            ModelConfig {
                model_id: "granite-3.1-8b-instruct".to_string(),
                model_type: "granite-3.1-8b-instruct".to_string(),
                config: serde_json::json!({}),
                provider_id: Some("ollama".to_string()),
                variant: None,
            },
        );
        ctx
    }

    #[tokio::test]
    async fn setup_agent_model_persists_config() {
        let _home = crate::config::TestConfigHome::new();
        let mut ctx = ctx_with_chat_capable_model();
        // CaptureUi's text() echoes back the default when prompted; here we
        // pass an explicit instance id so no prompt is needed. Exactly one
        // configured model satisfies the Chat requirement, so it's picked
        // automatically without a select prompt.
        let result = CapabilityCommands::setup(&mut ctx, "agent-model", Some("chat")).await;
        assert!(result.is_ok());
        let configured = ctx.config.get_capability("chat").unwrap();
        assert_eq!(
            configured.config.get("model_id").and_then(|v| v.as_str()),
            Some("granite-3.1-8b-instruct")
        );
        let infos = infos!(ctx);
        assert!(
            infos
                .iter()
                .any(|m| m.contains("chat") && m.contains("configured successfully"))
        );
    }

    // These exercise the pure decision helpers directly rather than driving
    // them through `setup()`: with nothing configured, `configurable_types`
    // is never empty (the catalog always has something), so the "configure
    // a new instance" option would auto-select and recurse into a real,
    // live `ModelCommands::setup`/`ProviderCommands::setup` call against the
    // real registries -- unsafe/nondeterministic for a unit test.

    #[test]
    fn model_candidates_excludes_providerless_model() {
        use crate::config::ModelConfig;
        use crate::models::ModelFunction;

        let mut ctx = test_ctx();
        // Configured model with no provider_id -- Model::provider() errs, so
        // it must not be offered as a usable candidate.
        ctx.config.models.insert(
            "granite-3.1-8b-instruct".to_string(),
            ModelConfig {
                model_id: "granite-3.1-8b-instruct".to_string(),
                model_type: "granite-3.1-8b-instruct".to_string(),
                config: serde_json::json!({}),
                provider_id: None,
                variant: None,
            },
        );

        let requirement = ModelRequirement {
            supported_functions: vec![ModelFunction::Chat],
            ..Default::default()
        };
        let (usable, _) = CapabilityCommands::model_candidates(&ctx, &requirement);
        assert!(!usable.contains(&"granite-3.1-8b-instruct".to_string()));
    }

    #[test]
    fn model_candidates_offers_configurable_types_when_nothing_configured() {
        let ctx = test_ctx();
        let requirement = ModelRequirement::default();
        let (usable, configurable_types) = CapabilityCommands::model_candidates(&ctx, &requirement);
        assert!(usable.is_empty());
        assert!(!configurable_types.is_empty());
    }

    #[tokio::test]
    async fn resolve_model_dependency_fails_when_unsatisfiable_and_required() {
        let mut ctx = test_ctx();
        // No catalog model type has this family, so both `usable` and
        // `configurable_types` come back empty -- the true "nothing can
        // satisfy this, not even by configuring something new" path.
        let requirement = ModelRequirement {
            family: Some("NoSuchFamilyXYZ".to_string()),
            ..Default::default()
        };
        let result =
            CapabilityCommands::resolve_model_dependency(&mut ctx, &requirement, true).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("No configured model satisfies")
        );
    }

    #[tokio::test]
    async fn resolve_model_dependency_returns_none_when_unsatisfiable_and_optional() {
        let mut ctx = test_ctx();
        let requirement = ModelRequirement {
            family: Some("NoSuchFamilyXYZ".to_string()),
            ..Default::default()
        };
        let result = CapabilityCommands::resolve_model_dependency(&mut ctx, &requirement, false)
            .await
            .unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn provider_candidates_offers_configurable_types_when_nothing_configured() {
        let ctx = test_ctx();
        let requirement = ProviderRequirement::default();
        let (existing, configurable_types) =
            CapabilityCommands::provider_candidates(&ctx, &requirement);
        assert!(existing.is_empty());
        assert!(!configurable_types.is_empty());
    }

    #[tokio::test]
    async fn resolve_provider_dependency_fails_when_unsatisfiable_and_required() {
        use crate::models::ModelFunction;
        // NOTE: Eventually, all functions will be supported by at least one provider,
        // so this test will be impossible to implement without a dummy function.

        let mut ctx = test_ctx();
        // No registered provider type supports Thinking, so both `existing`
        // and `configurable_types` come back empty -- the true "nothing can
        // satisfy this, not even by configuring something new" path.
        let requirement = ProviderRequirement {
            functions: vec![ModelFunction::KeywordBiasing],
            ..Default::default()
        };
        let result =
            CapabilityCommands::resolve_provider_dependency(&mut ctx, &requirement, true).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("No configured provider satisfies")
        );
    }

    #[tokio::test]
    async fn resolve_provider_dependency_returns_none_when_unsatisfiable_and_optional() {
        // NOTE: Eventually, all functions will be supported by at least one provider,
        // so this test will be impossible to implement without a dummy function.
        use crate::models::ModelFunction;

        let mut ctx = test_ctx();
        let requirement = ProviderRequirement {
            functions: vec![ModelFunction::KeywordBiasing],
            ..Default::default()
        };
        let result = CapabilityCommands::resolve_provider_dependency(&mut ctx, &requirement, false)
            .await
            .unwrap();
        assert!(result.is_none());
    }

    // -- remove -----------------------------------------------------------------

    #[test]
    fn remove_existing_capability_succeeds_and_disappears_from_list() {
        let _home = crate::config::TestConfigHome::new();
        let mut ctx = ctx_with_capability("my-cap", "agent-model");
        assert!(ctx.config.get_capability("my-cap").is_some());

        CapabilityCommands::remove(&mut ctx, "my-cap").unwrap();

        assert!(ctx.config.get_capability("my-cap").is_none());
        let infos = infos!(ctx);
        assert!(
            infos
                .iter()
                .any(|m| m.contains("my-cap") && m.contains("removed"))
        );
    }

    #[test]
    fn remove_nonexistent_capability_returns_err() {
        let mut ctx = test_ctx();
        let result = CapabilityCommands::remove(&mut ctx, "doesnt-exist");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Nothing to remove")
        );
    }

    #[test]
    fn list_does_not_show_removed_capability() {
        let _home = crate::config::TestConfigHome::new();
        let mut ctx = ctx_with_capability("my-cap", "agent-model");
        CapabilityCommands::remove(&mut ctx, "my-cap").unwrap();
        CapabilityCommands::list(&ctx).unwrap();
        let tables = tables!(ctx);
        let (_, _, rows) = &tables[0];
        assert!(rows.is_empty());
    }
}