oxo-flow-cli 0.14.1

CLI for the oxo-flow bioinformatics pipeline engine
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
use anyhow::{Context, Result};
use colored::Colorize;
use oxo_flow_core::config::WorkflowConfig;

use crate::commands::print_banner;

use crate::{ConfigAction, EnvAction};

pub async fn env_command(action: EnvAction) -> Result<()> {
    print_banner();
    match action {
        EnvAction::List { workflow } => match workflow {
            Some(wf_path) => {
                let config = WorkflowConfig::from_file(&wf_path)
                    .with_context(|| format!("failed to parse {}", wf_path.display()))?;
                eprintln!(
                    "{} {}",
                    "Environments in".bold(),
                    wf_path.display().to_string().dimmed()
                );
                let mut seen = std::collections::HashSet::new();
                for rule in &config.rules {
                    let kind = rule.environment.kind();
                    let spec = kind.to_string();
                    if seen.insert(format!("{}:{spec}", rule.name)) {
                        eprintln!("  {} {} [{}]", "".green(), rule.name, spec);
                    }
                }
                if seen.is_empty() {
                    eprintln!(
                        "  (no environment specifications found — rules will use system environment)"
                    );
                }
            }
            None => {
                let resolver = oxo_flow_core::environment::EnvironmentResolver::new();
                let available = resolver.available_backends();
                eprintln!("{}", "Available environment backends:".bold());
                for backend in available {
                    eprintln!("  {} {}", "".green(), backend);
                }
            }
        },
        EnvAction::Check { workflow } => {
            let resolver = oxo_flow_core::environment::EnvironmentResolver::new();

            match workflow {
                Some(wf_path) => {
                    // Validate each rule's declared environment in the workflow.
                    let config = WorkflowConfig::from_file(&wf_path)
                        .with_context(|| format!("failed to parse {}", wf_path.display()))?;

                    let mut all_ok = true;
                    for rule in &config.rules {
                        match resolver.validate_spec(&rule.environment) {
                            Ok(()) => {
                                eprintln!(
                                    "  {} {} ({})",
                                    "".green(),
                                    rule.name,
                                    rule.environment.kind()
                                );
                            }
                            Err(e) => {
                                eprintln!("  {} {}{}", "".red(), rule.name, e);
                                all_ok = false;
                            }
                        }
                    }

                    if !all_ok {
                        std::process::exit(1);
                    }
                }
                None => {
                    // No workflow provided: report global backend availability.
                    eprintln!("{}", "Environment backend availability:".bold());
                    let available = resolver.available_backends();
                    for backend in
                        oxo_flow_core::environment::EnvironmentResolver::all_known_backends()
                    {
                        if available.contains(backend) {
                            eprintln!("  {} {}", "".green(), backend);
                        } else {
                            eprintln!("  {} {} (not found)", "".red(), backend);
                        }
                    }
                }
            }
        }
        EnvAction::Create {
            spec,
            name,
            ai,
            backend,
        } => {
            // AI mode: SPEC is a natural-language description, not a file path.
            if ai {
                return create_env_from_ai(&spec, name, &backend).await;
            }
            let name_str = name.clone().unwrap_or_else(|| {
                spec.file_stem()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string()
            });

            // Determine environment type from file extension or content
            let ext = spec.extension().and_then(|e| e.to_str()).unwrap_or("");
            let backend = match ext {
                "yaml" | "yml" => "conda",
                "toml" => "pixi",
                "lock" => "conda",
                _ => {
                    eprintln!(
                        "{} Unknown environment spec format: '{}'",
                        "Warning:".yellow(),
                        spec.display()
                    );
                    eprintln!(
                        "  Supported formats: .yaml/.yml (conda), .toml (pixi), .lock (conda-lock)"
                    );
                    anyhow::bail!("Unsupported environment spec format");
                }
            };

            eprintln!(
                "{} Creating {} environment '{}' from '{}'...",
                "Info:".bold().cyan(),
                backend,
                name_str,
                spec.display()
            );

            match backend {
                "conda" => {
                    // Use conda/mamba to create environment
                    // Prefer mamba for speed, fall back to conda
                    let tool = {
                        let mamba_exists = std::process::Command::new("mamba")
                            .arg("--version")
                            .stdout(std::process::Stdio::null())
                            .stderr(std::process::Stdio::null())
                            .status()
                            .is_ok();
                        let micromamba_exists = std::process::Command::new("micromamba")
                            .arg("--version")
                            .stdout(std::process::Stdio::null())
                            .stderr(std::process::Stdio::null())
                            .status()
                            .is_ok();
                        if mamba_exists {
                            "mamba"
                        } else if micromamba_exists {
                            "micromamba"
                        } else {
                            "conda"
                        }
                    };

                    let status = std::process::Command::new(tool)
                        .args([
                            "env",
                            "create",
                            "-f",
                            &spec.to_string_lossy(),
                            "-n",
                            &name_str,
                        ])
                        .status()
                        .with_context(|| format!("failed to run {} env create", tool))?;

                    if !status.success() {
                        anyhow::bail!(
                            "{} env create failed with exit code {:?}",
                            tool,
                            status.code()
                        );
                    }
                    eprintln!(
                        "  {} Environment '{}' created successfully.",
                        "".green(),
                        name_str
                    );
                    eprintln!("  Activate with: conda activate {}", name_str);
                }
                "pixi" => {
                    // The generated spec is itself a complete pixi project
                    // manifest ([project] + [dependencies]) — install it
                    // directly. `pixi init` would create an empty project
                    // and install nothing.
                    let pixi_toml = std::fs::read_to_string(&spec)
                        .with_context(|| format!("cannot read pixi spec: {}", spec.display()))?;

                    let project_dir = std::env::temp_dir().join(format!("oxo-flow-{}", name_str));
                    std::fs::create_dir_all(&project_dir)?;
                    std::fs::write(project_dir.join("pixi.toml"), &pixi_toml)
                        .with_context(|| "failed to write pixi.toml")?;

                    let status = std::process::Command::new("pixi")
                        .args(["install"])
                        .current_dir(&project_dir)
                        .status()
                        .with_context(|| "failed to run pixi install")?;

                    if !status.success() {
                        anyhow::bail!("pixi install failed");
                    }

                    eprintln!(
                        "  {} Pixi project created at: {}",
                        "".green(),
                        project_dir.display()
                    );
                    eprintln!(
                        "  Activate with: cd {} && pixi shell",
                        project_dir.display()
                    );
                }
                other => {
                    anyhow::bail!(
                        "Environment backend '{}' not supported for env create yet",
                        other
                    );
                }
            }
        }
    }
    Ok(())
}

pub fn handle_config(action: ConfigAction) -> Result<()> {
    print_banner();
    match action {
        ConfigAction::Show { workflow } => {
            let config = WorkflowConfig::from_file(&workflow)
                .with_context(|| format!("failed to parse {}", workflow.display()))?;

            eprintln!("{}", "Workflow Configuration:".bold());
            eprintln!("  Name:    {}", config.workflow.name);
            eprintln!("  Version: {}", config.workflow.version);
            if let Some(ref desc) = config.workflow.description {
                eprintln!("  Desc:    {}", desc);
            }
            if let Some(ref author) = config.workflow.author {
                eprintln!("  Author:  {}", author);
            }

            eprintln!("\n{}", "Config Variables:".bold());
            if config.config.is_empty() {
                eprintln!("  (none)");
            } else {
                for (k, v) in &config.config {
                    eprintln!("  {} = {}", k, v);
                }
            }
        }
        ConfigAction::Stats { workflow } => {
            let config = WorkflowConfig::from_file(&workflow)
                .with_context(|| format!("failed to parse {}", workflow.display()))?;

            let stats = oxo_flow_core::format::workflow_stats(&config);
            eprintln!("{}", "Workflow Statistics:".bold());
            eprintln!("  Workflow:           {}", config.workflow.name);
            eprintln!("  Rules:              {}", stats.rule_count);
            eprintln!("  Shell rules:        {}", stats.shell_rules);
            eprintln!("  Script rules:       {}", stats.script_rules);
            eprintln!("  Dependencies:       {}", stats.dependency_count);
            eprintln!("  Parallel groups:    {}", stats.parallel_groups);
            eprintln!("  Max depth:          {}", stats.max_depth);
            eprintln!("  Total threads:      {}", stats.total_threads);
            eprintln!(
                "  Wildcards:          {} ({:?})",
                stats.wildcard_count, stats.wildcard_names
            );
            if !stats.environments.is_empty() {
                eprintln!("  Environments:       {:?}", stats.environments);
            }
        }
        ConfigAction::Get { workflow, key } => {
            let config = WorkflowConfig::from_file(&workflow)?;
            if let Some(val) = config.config.get(&key) {
                println!("{}", val);
            } else {
                return Err(anyhow::anyhow!("config key '{}' not found", key));
            }
        }
    }
    Ok(())
}

/// Verify a license file, or display the current license status without one.
pub fn handle_license(path: Option<std::path::PathBuf>) -> Result<()> {
    let status = oxo_flow_web::check_license();
    if let Some(p) = path {
        match oxo_license::load_and_verify(Some(&p), &oxo_flow_web::OXO_FLOW_CONFIG) {
            Ok(license) => {
                println!("{} License verified successfully", "".green().bold());
                println!("  Type:    {}", license.payload.license_type);
                println!("  Issued:  {}", license.payload.issued_to_org);
                println!("  Schema:  {}", license.payload.schema);
                println!("  ID:      {}", license.payload.license_id);
            }
            Err(e) => anyhow::bail!("License verification failed: {e}"),
        }
    } else {
        println!("License status:");
        if status.valid {
            println!(
                "  Status:  {} ({})",
                "Valid".green().bold(),
                status.license_type.as_deref().unwrap_or("unknown")
            );
            if let Some(org) = &status.issued_to {
                println!("  Issued:  {org}");
            }
        } else {
            println!("  Status:  {}", "Invalid".red().bold());
        }
        println!("  Message: {}", status.message);
    }
    Ok(())
}

// ── AI-powered environment spec generation ────────────────────────────────

/// Generate an environment spec (conda YAML or pixi TOML) from a
/// natural-language description using the AI provider + built-in tool table.
async fn create_env_from_ai(
    description: &std::path::Path,
    name: Option<String>,
    backend: &str,
) -> Result<()> {
    let description = description.to_string_lossy().to_string();
    let provider = crate::commands::ai_template::resolve_ai_provider()?;

    let is_pixi = backend.eq_ignore_ascii_case("pixi");
    if !is_pixi && !backend.eq_ignore_ascii_case("conda") {
        anyhow::bail!("unsupported backend '{}'. Use 'conda' or 'pixi'.", backend);
    }

    println!("{}", "AI Environment Generator".bold().green());
    println!(
        "  Model: {}",
        provider.model().unwrap_or_else(|| "default".into())
    );
    println!("  Backend: {}", if is_pixi { "pixi" } else { "conda" });
    println!("  Description: {description}\n");

    // Pre-resolve tools mentioned in the description against the embedded
    // Bioconda database — inject real names + current versions so the model
    // pins accurately instead of guessing. Generic words and loose summary
    // matches are filtered out so unrelated tools don't pollute the prompt.
    const STOP_WORDS: &[&str] = &[
        "and", "the", "for", "with", "using", "into", "from", "your", "pipeline", "workflow",
        "analysis", "data", "files", "output", "input", "all", "new", "this", "that", "via",
        "then", "also", "based", "need", "want", "please",
    ];
    let mut matched = String::new();
    let mut seen = std::collections::HashSet::new();
    for word in description
        .split(|c: char| !c.is_alphanumeric() && c != '-' && c != '_')
        .filter(|w| w.len() >= 3 && !STOP_WORDS.contains(&w.to_ascii_lowercase().as_str()))
    {
        let word_lower = word.to_ascii_lowercase();
        for tool in oxo_flow_ai::knowledge::bioconda::search_tools(word, 3) {
            // Keep only matches where the tool name actually contains the
            // word — summary-only matches are too loose to inject.
            if !tool.name.to_ascii_lowercase().contains(&word_lower) {
                continue;
            }
            if seen.insert(tool.name.clone()) {
                matched.push_str(&format!(
                    "- {} {}{}\n",
                    tool.name,
                    tool.version,
                    if tool.summary.is_empty() {
                        "(no description)"
                    } else {
                        &tool.summary
                    }
                ));
            }
        }
    }
    if !matched.is_empty() {
        eprintln!("{}", "  Matched Bioconda tools:".bold().cyan());
        for line in matched.lines() {
            eprintln!("    {line}");
        }
    }

    let system = if is_pixi {
        format!(
            r#"## Role
You are a bioinformatics environment specialist. Generate a pixi environment
TOML file from the user's natural-language description.

## Tool Reference
{}

## Matched Bioconda Tools (real names + current versions)
{}

## Output Requirements
Generate ONLY valid pixi.toml inside ```toml code fences:

```toml
[project]
name = "env-name"
channels = ["conda-forge", "bioconda"]
platforms = ["linux-64"]

[dependencies]
tool = "version"
```

Rules:
- Pin every tool version using the Matched Bioconda Tools above when present; otherwise use your knowledge of current stable versions
- Include all tools the user mentions; add common companions if clearly needed
- channels order: conda-forge first, bioconda second (Bioconda's recommended channel setup)
- Do NOT add comments beyond the file header
"#,
            oxo_flow_ai::knowledge::builtin::format_tool_table(),
            if matched.is_empty() {
                "(none matched)"
            } else {
                &matched
            }
        )
    } else {
        format!(
            r#"## Role
You are a bioinformatics environment specialist. Generate a conda environment
YAML file from the user's natural-language description.

## Tool Reference
{}

## Matched Bioconda Tools (real names + current versions)
{}

## Output Requirements
Generate ONLY valid conda environment YAML inside ```yaml code fences:

```yaml
# envs/<name>.yaml
name: <kebab-case-env-name>
channels:
  - conda-forge
  - bioconda
dependencies:
  - <tool>=<pinned-version>
```

Rules:
- Pin every tool version using the Matched Bioconda Tools above when present; otherwise use your knowledge of current stable versions
- Include all tools the user mentions; add common companions if clearly needed
- channels order: conda-forge first, bioconda second (Bioconda's recommended channel setup)
- Do NOT add comments beyond the file header
"#,
            oxo_flow_ai::knowledge::builtin::format_tool_table(),
            if matched.is_empty() {
                "(none matched)"
            } else {
                &matched
            }
        )
    };

    println!("{}", "  Generating...".bold().cyan());
    use oxo_flow_ai::types::Message;
    let messages = vec![Message::system(&system), Message::user(&description)];
    let response = provider.chat_with_tools(&messages, &[]).await?;
    let response_text = response.content.unwrap_or_default();

    // Extract spec from code fence (YAML for conda, TOML for pixi)
    let spec_content = if is_pixi {
        extract_toml(&response_text)
            .ok_or_else(|| anyhow::anyhow!("AI response did not contain valid pixi TOML"))?
    } else {
        extract_yaml(&response_text)
            .ok_or_else(|| anyhow::anyhow!("AI response did not contain valid conda YAML"))?
    };

    // Basic structural validation
    let has_deps = spec_content.contains("dependencies");
    if !has_deps {
        anyhow::bail!("generated spec missing 'dependencies' section");
    }

    // Determine output path: -n <name> → envs/<name>.<ext>
    let ext = if is_pixi { "toml" } else { "yaml" };
    let env_name = name.unwrap_or_else(|| {
        let name_key = if is_pixi {
            spec_content.lines().find_map(|l| l.strip_prefix("name = "))
        } else {
            spec_content.lines().find_map(|l| l.strip_prefix("name:"))
        };
        name_key
            .map(|n| n.trim().trim_matches('"').trim_matches('\'').to_string())
            .filter(|n| !n.is_empty())
            .unwrap_or_else(|| "generated".to_string())
    });
    let out_path = std::path::PathBuf::from("envs").join(format!("{env_name}.{ext}"));
    if let Some(parent) = out_path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(&out_path, &spec_content)?;

    println!(
        "{} Environment spec written to {}",
        "".green(),
        out_path.display()
    );
    println!();
    println!(
        "  Review the spec: {}",
        out_path.display().to_string().dimmed()
    );
    println!(
        "  Then create it with: oxo-flow env create {}",
        out_path.display().to_string().dimmed()
    );
    Ok(())
}

/// Extract YAML content from an AI response (```yaml fence or raw).
fn extract_yaml(response: &str) -> Option<String> {
    if let Some(start) = response.find("```yaml") {
        let start = start + 7;
        if let Some(end) = response[start..].find("```") {
            let content = response[start..start + end].trim().to_string();
            if !content.is_empty() {
                return Some(content);
            }
        }
    }
    if let Some(pos) = response.find("name:") {
        return Some(response[pos..].trim().to_string());
    }
    None
}

/// Extract TOML content from an AI response (```toml fence or raw).
fn extract_toml(response: &str) -> Option<String> {
    if let Some(start) = response.find("```toml") {
        let start = start + 7;
        if let Some(end) = response[start..].find("```") {
            let content = response[start..start + end].trim().to_string();
            if !content.is_empty() {
                return Some(content);
            }
        }
    }
    if let Some(pos) = response.find("[project]") {
        return Some(response[pos..].trim().to_string());
    }
    None
}