cosq 0.6.1

A CLI to query your Azure Cosmos DB instances
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
//! Run command — execute stored queries from .cosq files
//!
//! Resolves parameters from CLI arguments or interactive prompts,
//! validates them, and executes the query against Cosmos DB.

use std::collections::BTreeMap;

use anyhow::{Context, Result, bail};
use colored::Colorize;
use cosq_client::cosmos::CosmosClient;
use cosq_core::config::Config;
use cosq_core::stored_query::{StoredQuery, find_stored_query, list_stored_queries};
use inquire::{Confirm, Select, Text};
use serde_json::Value;

use super::common;
use crate::output::{OutputFormat, render_multi_step_template, render_template, write_results};

pub struct RunArgs {
    pub name: Option<String>,
    pub params: Vec<String>,
    pub output: Option<OutputFormat>,
    pub db: Option<String>,
    pub container: Option<String>,
    pub template: Option<String>,
    pub quiet: bool,
}

pub async fn run(args: RunArgs) -> Result<()> {
    // Resolve query: from name argument or interactive picker
    let query = if let Some(ref name) = args.name {
        find_stored_query(name)
            .map_err(|e| anyhow::anyhow!("Failed to load query '{name}': {e}"))?
    } else {
        pick_query_interactive()?
    };

    if !args.quiet {
        eprintln!("{} {}", "Running:".bold(), query.name.cyan());
        if !query.metadata.description.is_empty() {
            eprintln!("  {}", query.metadata.description.dimmed());
        }
    }

    // Parse CLI params (--key value pairs from the raw args)
    let cli_params = parse_cli_params(&args.params)?;

    // Resolve parameters: CLI > interactive > default
    let resolved = resolve_params_interactive(&query, &cli_params)?;

    // Load config for connection details
    let mut config = Config::load()?;
    let client = CosmosClient::new(&config.account.endpoint).await?;

    let (database, db_changed) = common::resolve_database(
        &client,
        &mut config,
        args.db,
        query.metadata.database.as_deref(),
    )
    .await?;

    if query.is_multi_step() {
        // Multi-step execution: resolve database only (containers are per-step)
        if db_changed {
            config.save()?;
        }

        if !args.quiet {
            eprintln!("{}", "Executing steps:".dimmed());
        }

        let pipeline_result =
            super::pipeline::execute(&client, &database, &query, &resolved, args.quiet).await?;

        // Output multi-step results
        let has_template = args.template.is_some()
            || query.metadata.template.is_some()
            || query.metadata.template_file.is_some();

        let effective_output = args.output.unwrap_or(if has_template {
            OutputFormat::Template
        } else {
            OutputFormat::Json
        });

        match effective_output {
            OutputFormat::Template => {
                let template_str = resolve_template_str(&args.template, &query)?;
                if let Some(tmpl) = template_str {
                    // Flatten all step results for rendering recovery
                    let all_docs: Vec<Value> = pipeline_result
                        .step_results
                        .values()
                        .flat_map(|v| v.clone())
                        .collect();
                    match render_multi_step_template(
                        &tmpl,
                        &pipeline_result.step_results,
                        &resolved,
                    ) {
                        Ok(rendered) => print!("{rendered}"),
                        Err(_) => {
                            let rendered =
                                render_with_ai_recovery(&tmpl, &all_docs, &resolved, &query)
                                    .await?;
                            print!("{rendered}");
                        }
                    }
                } else {
                    // No template — output all step results as JSON
                    let combined: serde_json::Value =
                        serde_json::to_value(&pipeline_result.step_results)?;
                    let json = serde_json::to_string_pretty(&combined)?;
                    println!("{json}");
                }
            }
            _ => {
                // For non-template formats, combine all step results
                let combined: serde_json::Value =
                    serde_json::to_value(&pipeline_result.step_results)?;
                let json = serde_json::to_string_pretty(&combined)?;
                println!("{json}");
            }
        }

        if !args.quiet {
            eprintln!(
                "\n{} {:.2} RUs",
                "Request charge:".dimmed(),
                pipeline_result.total_charge
            );
        }
    } else {
        // Single-step execution (original path)
        let (container, ctr_changed) = common::resolve_container(
            &client,
            &mut config,
            &database,
            args.container,
            query.metadata.container.as_deref(),
        )
        .await?;

        if db_changed || ctr_changed {
            config.save()?;
        }

        let cosmos_params = StoredQuery::build_cosmos_params(&resolved);
        let result = client
            .query_with_params(&database, &container, &query.sql, cosmos_params)
            .await?;

        let has_template = args.template.is_some()
            || query.metadata.template.is_some()
            || query.metadata.template_file.is_some();

        let effective_output = args.output.unwrap_or(if has_template {
            OutputFormat::Template
        } else {
            OutputFormat::Json
        });

        match effective_output {
            OutputFormat::Template => {
                let template_str = resolve_template_str(&args.template, &query)?;
                if let Some(tmpl) = template_str {
                    let rendered =
                        render_with_ai_recovery(&tmpl, &result.documents, &resolved, &query)
                            .await?;
                    print!("{rendered}");
                } else {
                    write_results(
                        &mut std::io::stdout(),
                        &result.documents,
                        &OutputFormat::Json,
                    )?;
                }
            }
            _ => {
                write_results(&mut std::io::stdout(), &result.documents, &effective_output)?;
            }
        }

        if !args.quiet {
            eprintln!(
                "\n{} {:.2} RUs",
                "Request charge:".dimmed(),
                result.request_charge
            );
        }
    }

    Ok(())
}

/// Attempt to render a template, and if it fails, offer AI-assisted fix.
/// Returns the rendered output or propagates the error if the user declines.
async fn render_with_ai_recovery(
    template_str: &str,
    documents: &[Value],
    params: &std::collections::BTreeMap<String, Value>,
    query: &StoredQuery,
) -> Result<String> {
    match render_template(template_str, documents, params) {
        Ok(rendered) => Ok(rendered),
        Err(e) => {
            let error_msg = format!("{e}");
            eprintln!("\n{} {}", "Template error:".red().bold(), error_msg);

            // Check if AI is configured
            let config = Config::load().ok();
            let ai_config = config.as_ref().and_then(|c| c.ai.clone());

            if let Some(ai) = ai_config {
                let fix = Confirm::new("Would you like AI to fix this?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(false);

                if fix {
                    return fix_template_with_ai(
                        &ai,
                        template_str,
                        &error_msg,
                        documents,
                        params,
                        query,
                    )
                    .await;
                }
            }

            Err(e)
        }
    }
}

/// Use AI to fix a broken template and re-render
async fn fix_template_with_ai(
    ai_config: &cosq_core::config::AiConfig,
    broken_template: &str,
    error_msg: &str,
    documents: &[Value],
    params: &std::collections::BTreeMap<String, Value>,
    query: &StoredQuery,
) -> Result<String> {
    eprintln!(
        "{}",
        format!("Fixing via {}...", ai_config.provider.display_name()).dimmed()
    );

    let sample = if documents.is_empty() {
        "(no documents)".to_string()
    } else {
        serde_json::to_string_pretty(&documents[0]).unwrap_or_default()
    };

    let system_prompt = format!(
        "You fix MiniJinja templates for cosq query output. \
         Respond with ONLY the corrected template — no explanation, no markdown fences.\n\n\
         Available filters: truncate(length), pad(width), length, upper, lower, title, trim, replace, default, join, first, last, round.\n\
         Available variables: documents (array of results), and named step arrays for multi-step queries.\n\n\
         Sample document:\n{sample}"
    );

    let user_prompt = format!(
        "This template has an error:\n\n{broken_template}\n\nError: {error_msg}\n\nFix the template."
    );

    let response = cosq_client::ai::generate_text(ai_config, &system_prompt, &user_prompt)
        .await
        .context("AI fix failed")?;

    let fixed = response.trim().to_string();
    let fixed = fixed
        .strip_prefix("```")
        .unwrap_or(&fixed)
        .strip_suffix("```")
        .unwrap_or(&fixed)
        .trim();

    // Try rendering with the fixed template
    match render_template(fixed, documents, params) {
        Ok(rendered) => {
            eprintln!("{} Template fixed successfully.", "OK".green().bold());

            // Offer to save the fix
            if query.metadata.template.is_some() {
                let save = Confirm::new("Save the fixed template to the query file?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(false);

                if save {
                    if let Err(e) = save_fixed_template(query, fixed) {
                        eprintln!("{} Could not save fix: {e}", "Warning:".yellow().bold());
                    }
                }
            }

            Ok(rendered)
        }
        Err(e2) => {
            eprintln!("{} AI fix still has errors: {}", "Error:".red().bold(), e2);
            Err(e2)
        }
    }
}

/// Save a fixed template back to the query's .cosq file
fn save_fixed_template(query: &StoredQuery, fixed_template: &str) -> Result<()> {
    let mut updated = query.clone();
    updated.metadata.template = Some(fixed_template.to_string());
    let contents = updated.to_file_contents()?;
    let path = cosq_core::stored_query::query_file_path(&query.name, false)?;
    std::fs::write(&path, &contents)?;
    eprintln!("{} Saved fix to {}", "OK".green().bold(), path.display());
    Ok(())
}

/// Resolve the template string from CLI arg, query metadata, or template file
fn resolve_template_str(
    cli_template: &Option<String>,
    query: &StoredQuery,
) -> Result<Option<String>> {
    if let Some(path) = cli_template {
        let content = std::fs::read_to_string(path)
            .with_context(|| format!("failed to read template file: {path}"))?;
        Ok(Some(content))
    } else if let Some(ref tmpl) = query.metadata.template {
        Ok(Some(tmpl.clone()))
    } else if let Some(ref tmpl_file) = query.metadata.template_file {
        let content = std::fs::read_to_string(tmpl_file)
            .with_context(|| format!("failed to read template file: {tmpl_file}"))?;
        Ok(Some(content))
    } else {
        Ok(None)
    }
}

/// Interactively pick a stored query from a fuzzy-select list.
fn pick_query_interactive() -> Result<StoredQuery> {
    let queries = list_stored_queries().unwrap_or_default();
    if queries.is_empty() {
        bail!(
            "No stored queries found.\n\n  \
             Create one with: cosq queries create <name>"
        );
    }

    let display_items: Vec<String> = queries
        .iter()
        .map(|q| {
            if q.metadata.description.is_empty() {
                q.name.clone()
            } else {
                format!("{}{}", q.name, q.metadata.description)
            }
        })
        .collect();

    let selection = Select::new("Select a stored query:", display_items.clone())
        .prompt()
        .context("query selection cancelled")?;

    let idx = display_items.iter().position(|d| d == &selection).unwrap();
    Ok(queries.into_iter().nth(idx).unwrap())
}

/// Parse --key value pairs from the raw parameter strings.
/// Expects alternating --name value pairs.
fn parse_cli_params(params: &[String]) -> Result<BTreeMap<String, String>> {
    let mut map = BTreeMap::new();
    let mut iter = params.iter();

    while let Some(key) = iter.next() {
        let name = key
            .strip_prefix("--")
            .ok_or_else(|| anyhow::anyhow!("expected parameter in --name format, got: {key}"))?;

        let value = iter
            .next()
            .ok_or_else(|| anyhow::anyhow!("missing value for parameter --{name}"))?;

        map.insert(name.to_string(), value.to_string());
    }

    Ok(map)
}

/// Resolve parameters, prompting interactively for any that aren't provided via CLI.
fn resolve_params_interactive(
    query: &StoredQuery,
    cli_params: &BTreeMap<String, String>,
) -> Result<BTreeMap<String, Value>> {
    let mut resolved = BTreeMap::new();

    for param in &query.metadata.params {
        let value = if let Some(raw) = cli_params.get(&param.name) {
            cosq_core::stored_query::parse_param_value_public(&param.name, &param.param_type, raw)?
        } else if let Some(ref choices) = param.choices {
            let choice_strs: Vec<String> = choices
                .iter()
                .map(|c| match c {
                    Value::String(s) => s.clone(),
                    other => other.to_string(),
                })
                .collect();

            let default_idx = param
                .default
                .as_ref()
                .and_then(|d| choices.iter().position(|c| c == d))
                .unwrap_or(0);

            let prompt = if let Some(ref desc) = param.description {
                format!("{} ({})", param.name, desc)
            } else {
                param.name.clone()
            };

            let select_prompt = format!("{prompt}:");
            let mut select = Select::new(&select_prompt, choice_strs.clone());
            if default_idx < choice_strs.len() {
                select = select.with_starting_cursor(default_idx);
            }
            let selected = select.prompt().context("parameter selection cancelled")?;

            let idx = choice_strs.iter().position(|c| c == &selected).unwrap();
            choices[idx].clone()
        } else if param.is_required() || param.default.is_some() {
            let prompt = if let Some(ref desc) = param.description {
                format!("{} ({})", param.name, desc)
            } else {
                param.name.clone()
            };

            let default_str = param.default.as_ref().map(|d| match d {
                Value::String(s) => s.clone(),
                other => other.to_string(),
            });

            let text_prompt = format!("{prompt}:");
            let mut text = Text::new(&text_prompt);
            if let Some(ref def) = default_str {
                text = text.with_default(def);
            }
            let raw = text.prompt().context("input cancelled")?;

            cosq_core::stored_query::parse_param_value_public(&param.name, &param.param_type, &raw)?
        } else {
            continue;
        };

        param.validate(&value).map_err(|e| anyhow::anyhow!("{e}"))?;
        resolved.insert(param.name.clone(), value);
    }

    Ok(resolved)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_cli_params() {
        let params = vec![
            "--days".to_string(),
            "7".to_string(),
            "--status".to_string(),
            "active".to_string(),
        ];
        let parsed = parse_cli_params(&params).unwrap();
        assert_eq!(parsed.get("days"), Some(&"7".to_string()));
        assert_eq!(parsed.get("status"), Some(&"active".to_string()));
    }

    #[test]
    fn test_parse_cli_params_empty() {
        let parsed = parse_cli_params(&[]).unwrap();
        assert!(parsed.is_empty());
    }

    #[test]
    fn test_parse_cli_params_missing_value() {
        let params = vec!["--days".to_string()];
        let result = parse_cli_params(&params);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_cli_params_bad_format() {
        let params = vec!["days".to_string(), "7".to_string()];
        let result = parse_cli_params(&params);
        assert!(result.is_err());
    }
}