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
// Standard
use std::collections::HashMap;
// Third Party
use anyhow::Result;
// Local
use crate::capabilities::CAPABILITY_REGISTRY;
pub struct CapabilityCommands;
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)| {
let name = CAPABILITY_REGISTRY
.get(id)
.map(|c| c.name.clone())
.unwrap_or_else(|| id.clone());
vec![id.clone(), name, cfg.enabled.to_string()]
})
.collect();
rows.sort_by(|a, b| a[0].cmp(&b[0]));
ctx.ui.table(
&format!("Configured Capabilities ({} capabilities)", rows.len()),
&["ID", "NAME", "ENABLED"],
&rows,
);
Ok(())
}
pub fn info(ctx: &crate::AppContext, capability_id: &str) -> Result<()> {
match CAPABILITY_REGISTRY.get(capability_id) {
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(", ")));
}
fields.push(("Execution Hooks", "on_setup, on_configure, on_pre_launch, on_post_launch, on_shutdown, runtime_bindings".to_string()));
if let Some(configured) = ctx.config.get_capability(capability_id) {
fields.push(("Config: Enabled", configured.enabled.to_string()));
for (k, v) in &configured.config {
fields.push(("Config", format!("{k} = {v}")));
}
}
ctx.ui.detail(capability_id, &fields);
Ok(())
}
None => {
if let Some(configured) = ctx.config.get_capability(capability_id) {
let fields: Vec<(&str, String)> = vec![
("Enabled", configured.enabled.to_string()),
(
"Note",
"Configured but not found in 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");
}
}
}
}
pub async fn setup(ctx: &mut crate::AppContext, capability_id: &str) -> Result<()> {
match CAPABILITY_REGISTRY.get(capability_id) {
Some(cap) => {
ctx.ui
.info(&format!("\nSetting up capability: {capability_id}"));
ctx.ui.info(&format!("Name: {}", cap.name));
ctx.ui.info(&format!("Description: {}", cap.description));
ctx.ui.info("");
// Check dependencies
ctx.ui.info("Checking dependencies:");
// let mut all_satisfied = true;
// for dep in &cap.dependencies {
// let status = Self::check_dep_status(ctx, dep);
// println!(" - {} {}", dep, status);
// if status == " [MISSING]" {
// all_satisfied = false;
// }
// }
// Use DI factory to validate dependencies
// TODO: Recursively resolve dependencies
// if !all_satisfied {
// ctx.ui.info("\nSome dependencies are missing. You may want to:");
// ctx.ui.info(" - Configure required models: granite-cli model setup <model-id>");
// ctx.ui.info(" - Configure required providers: granite-cli provider setup <provider-id>");
// ctx.ui.info(" - Install required external tools");
// ctx.ui.info("");
// let continue_anyway = ctx.ui.confirm("Continue with setup anyway?", false)?;
// if !continue_anyway {
// ctx.ui.info("Capability setup cancelled.");
// return Ok(());
// }
// }
// Run on_setup hook
// TODO: Recursively set up dependencies
// println!("\nRunning setup hooks...");
// if let Ok(capability) = crate::capabilities::resolve_capability_from_registry(capability_id) {
// let result = capability.on_setup(&factory).await;
// if let Err(e) = result {
// println!("Warning: on_setup hook failed: {}", e);
// }
// }
// Prompt for capability-specific configuration
let mut config_map = HashMap::new();
let enabled = ctx
.ui
.confirm(&format!("Enable '{}' capability?", cap.name), true)?;
if enabled {
ctx.ui.info(&format!(
"\nCapability {} will be available at tool launch time.",
cap.name
));
config_map.insert("enabled".to_string(), "true".to_string());
// Get runtime bindings to show what will be injected
// TODO: Resolve runtime bindings?
// if let Ok(capability) = crate::capabilities::resolve_capability_from_registry(capability_id) {
// let bindings = capability.runtime_bindings();
// if !bindings.is_empty() {
// println!("\nRuntime bindings (environment variables at launch):");
// for binding in &bindings {
// println!(" {}={}", binding.key, binding.value);
// }
// }
// }
} else {
ctx.ui
.info(&format!("\nCapability {} is disabled.", cap.name));
config_map.insert("enabled".to_string(), "false".to_string());
}
let capability_config = crate::config::CapabilityConfig {
capability_id: capability_id.to_string(),
enabled: config_map
.get("enabled")
.map(|v| v == "true")
.unwrap_or(true),
config: config_map,
};
if let Err(e) = ctx
.config
.insert_capability(capability_id, capability_config)
{
ctx.ui
.warn(&format!("failed to save capability config: {e}"));
}
ctx.ui.info(&format!(
"\nCapability '{capability_id}' configured successfully!"
));
Ok(())
}
None => {
// Check if it's a configured-only capability
if let Some(configured) = ctx.config.get_capability(capability_id) {
ctx.ui.info(&format!("\nCapability: {capability_id}"));
ctx.ui.info(&format!("Enabled: {}", configured.enabled));
if !configured.config.is_empty() {
ctx.ui.info("\nCurrent Settings:");
for (k, v) in &configured.config {
ctx.ui.info(&format!(" {k} = {v}"));
}
}
ctx.ui.info("\nNote: This capability is configured but not found in the bundled registry.");
let overwrite = ctx.ui.confirm("Reconfigure this capability?", false)?;
if overwrite {
ctx.ui.info("\nPlease remove the existing config first:");
ctx.ui
.info(&format!(" granite-cli capability remove {capability_id}"));
ctx.ui.info("Then run setup again.");
}
Ok(())
} else {
ctx.ui.error(&format!(
"Capability '{capability_id}' not found in registry."
));
let available: Vec<_> = CAPABILITY_REGISTRY
.entries()
.keys()
.map(|k| k.to_string())
.collect();
ctx.ui
.info(&format!("Available capabilities: {}", available.join(", ")));
anyhow::bail!("Capability not found");
}
}
}
}
// TODO: Use generic dependency checking
// fn check_dep_status(ctx: &crate::AppContext, dep: &crate::registry::CapabilityDependency) -> &'static str {
// match dep {
// crate::registry::CapabilityDependency::Model { id, required: _ } => {
// if registry::MODEL_REGISTRY.get(id).is_some()
// || ctx.config.models.contains_key(id.as_str())
// {
// " [OK]"
// } else {
// " [MISSING]"
// }
// }
// crate::registry::CapabilityDependency::Provider { id, required: _ } => {
// if ctx.config.providers.contains_key(id.as_str())
// || Registry::get(&*registry::PROVIDER_REGISTRY, id).is_some()
// {
// " [OK]"
// } else {
// " [MISSING]"
// }
// }
// crate::registry::CapabilityDependency::ExternalTool { name: _, check_command } => {
// let parts: Vec<&str> = check_command.split_whitespace().collect();
// let available = if parts.is_empty() {
// false
// } else {
// std::process::Command::new(&parts[0])
// .args(&parts[1..])
// .output()
// .map(|o| o.status.success())
// .unwrap_or(false)
// };
// if available {
// " [OK]"
// } else {
// println!(" (command: {})", check_command);
// " [MISSING]"
// }
// }
// crate::registry::CapabilityDependency::Capability { id, required: _ } => {
// if registry::CAPABILITY_REGISTRY.get(id).is_some()
// || ctx.config.capabilities.contains_key(id.as_str())
// {
// " [OK]"
// } else {
// " [MISSING]"
// }
// }
// }
// }
}
/*-- tests --*/
#[cfg(test)]
mod tests {
use super::*;
use crate::config::{CapabilityConfig, Config};
use crate::utils::ui::base::tests::CaptureUi;
fn test_ctx() -> crate::AppContext {
crate::AppContext {
config: Config::default(),
ui: Box::new(CaptureUi::default()),
}
}
fn ctx_with_capability(id: &str, enabled: bool) -> crate::AppContext {
let mut ctx = test_ctx();
ctx.config.capabilities.insert(
id.to_string(),
CapabilityConfig {
capability_id: id.to_string(),
enabled,
config: std::collections::HashMap::new(),
},
);
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()
};
}
// -- 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()));
}
// -- 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_enabled_state() {
let ctx = ctx_with_capability("my-cap", true);
CapabilityCommands::list(&ctx).unwrap();
let tables = tables!(ctx);
let (_, _, rows) = &tables[0];
assert_eq!(rows.len(), 1);
assert!(rows[0].iter().any(|c| c == "true"));
}
// -- 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", false);
let result = CapabilityCommands::info(&ctx, "custom-cap");
assert!(result.is_ok());
assert!(!details!(ctx).is_empty());
}
}