resq-cli 0.3.0

Developer CLI for the ResQ autonomous drone platform
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
/*
 * Copyright 2026 ResQ
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! AI-powered commit message generation from staged diffs.

use anyhow::{bail, Context, Result};
use clap::Args;
use crossterm::{
    cursor,
    event::{self, Event, KeyCode},
    execute, style,
    terminal::{self, ClearType},
};
use regex::Regex;
use std::io::{self, IsTerminal, Write};
use std::process::Command;

/// Arguments for the `resq commit` command.
#[derive(Args, Debug)]
pub struct CommitArgs {
    /// Number of candidate messages to generate
    #[arg(short = 'n', long, default_value = "3")]
    count: u8,

    /// Max diff size in estimated tokens before truncation
    #[arg(short = 'm', long, default_value = "2000")]
    max_diff: usize,

    /// Hint for commit scope (e.g., "auth", "ui")
    #[arg(short, long)]
    scope: Option<String>,

    /// Auto-pick first candidate without prompting
    #[arg(long)]
    yes: bool,

    /// Print message(s) but don't commit
    #[arg(long)]
    dry_run: bool,

    /// Override AI provider (anthropic, openai, gemini)
    #[arg(long)]
    provider: Option<String>,

    /// Override AI model
    #[arg(long)]
    model: Option<String>,

    /// API request timeout in seconds
    #[arg(long, default_value = "30")]
    timeout: u64,

    /// Show prompt, token count, and raw LLM response
    #[arg(long)]
    verbose: bool,
}

// -------------------------------------------------------------------------
// Git helpers
// -------------------------------------------------------------------------

fn git(args: &[&str]) -> Result<String> {
    let output = Command::new("git")
        .args(args)
        .output()
        .context("Failed to run git")?;
    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!("git {}: {stderr}", args.join(" "));
    }
}

fn get_staged_diff() -> Result<String> {
    let stat = git(&["diff", "--staged", "--stat"])?;
    if stat.trim().is_empty() {
        bail!(
            "No staged changes found.\n\
             Stage files first with: git add <files>\n\
             Then run: resq commit"
        );
    }
    git(&["diff", "--staged"])
}

fn check_unstaged_warning() {
    if let Ok(diff) = git(&["diff", "--stat"]) {
        if !diff.trim().is_empty() {
            eprintln!(
                "\x1b[33mWarning:\x1b[0m You have unstaged changes. \
                 Only staged changes will be included in the commit."
            );
        }
    }
}

fn get_recent_commits() -> String {
    git(&["log", "--oneline", "-10"]).unwrap_or_default()
}

// -------------------------------------------------------------------------
// Prompt building + response parsing
// -------------------------------------------------------------------------

/// Conventional Commits regex (from templates/git-hooks/commit-msg).
const CC_PATTERN: &str =
    r"^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?(!)?: .+$";

fn validate_conventional_commit(msg: &str) -> bool {
    Regex::new(CC_PATTERN)
        .map(|re| re.is_match(msg.lines().next().unwrap_or("")))
        .unwrap_or(false)
}

fn build_prompt(
    diff: &str,
    recent_commits: &str,
    scope: Option<&str>,
    count: u8,
) -> (String, String) {
    let scope_hint = scope
        .map(|s| format!("\nSuggested scope: {s}"))
        .unwrap_or_default();

    let system = format!(
        "You are a commit message generator for a project that uses Conventional Commits.\n\
         \n\
         Format: <type>(<scope>): <description>\n\
         \n\
         Allowed types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert\n\
         {scope_hint}\n\
         \n\
         Rules:\n\
         - Subject line must be under 72 characters\n\
         - Use imperative mood (\"add\" not \"added\")\n\
         - Be specific about what changed and why\n\
         - Match the style of recent commits shown below\n\
         \n\
         Recent commits for style reference:\n\
         {recent_commits}\n\
         \n\
         Generate exactly {count} commit message candidates. \
         Return them as a JSON array of strings. No markdown fences."
    );

    let user = format!("Staged diff:\n\n{diff}");
    (system, user)
}

fn strip_code_fences(text: &str) -> &str {
    let trimmed = text.trim();
    if let Some(rest) = trimmed.strip_prefix("```json") {
        rest.trim()
            .strip_suffix("```")
            .map_or_else(|| rest.trim(), str::trim)
    } else if let Some(rest) = trimmed.strip_prefix("```") {
        rest.trim()
            .strip_suffix("```")
            .map_or_else(|| rest.trim(), str::trim)
    } else {
        trimmed
    }
}

fn parse_candidates(response: &str) -> Result<Vec<String>> {
    let cleaned = strip_code_fences(response);
    let candidates: Vec<String> =
        serde_json::from_str(cleaned).context("Failed to parse LLM response as JSON array")?;
    Ok(candidates)
}

// -------------------------------------------------------------------------
// Interactive selector (crossterm-based)
// -------------------------------------------------------------------------

/// RAII guard to restore terminal raw mode on drop.
struct RawModeGuard;

impl RawModeGuard {
    fn enable() -> Result<Self> {
        terminal::enable_raw_mode()?;
        Ok(Self)
    }
}

impl Drop for RawModeGuard {
    fn drop(&mut self) {
        let _ = terminal::disable_raw_mode();
    }
}

fn select_candidate(candidates: &[String]) -> Result<Option<usize>> {
    if !io::stdout().is_terminal() {
        bail!("Interactive selection requires a TTY. Use --yes or --dry-run in non-interactive contexts.");
    }

    let _guard = RawModeGuard::enable()?;
    let mut stdout = io::stdout();
    let mut selected: usize = 0;
    let total = candidates.len();

    // Initial render
    render_selector(&mut stdout, candidates, selected)?;

    loop {
        if let Event::Key(key) = event::read()? {
            match key.code {
                KeyCode::Up | KeyCode::Char('k') if selected > 0 => selected -= 1,
                KeyCode::Down | KeyCode::Char('j') if selected < total - 1 => selected += 1,
                KeyCode::Enter => {
                    clear_selector(&mut stdout, total)?;
                    return Ok(Some(selected));
                }
                KeyCode::Esc | KeyCode::Char('q') => {
                    clear_selector(&mut stdout, total)?;
                    return Ok(None);
                }
                _ => continue,
            }
            // Move up and re-render
            execute!(stdout, cursor::MoveUp((total + 2) as u16))?;
            render_selector(&mut stdout, candidates, selected)?;
        }
    }
}

fn render_selector(stdout: &mut io::Stdout, candidates: &[String], selected: usize) -> Result<()> {
    for (i, candidate) in candidates.iter().enumerate() {
        if i == selected {
            execute!(stdout, style::SetForegroundColor(style::Color::Cyan))?;
            write!(stdout, "\r  > {}. {}\n", i + 1, candidate)?;
            execute!(stdout, style::ResetColor)?;
        } else {
            write!(stdout, "\r    {}. {}\n", i + 1, candidate)?;
        }
    }
    write!(
        stdout,
        "\r\n  \x1b[2m[↑/↓/j/k] Navigate  [Enter] Select  [Esc/q] Cancel\x1b[0m"
    )?;
    stdout.flush()?;
    Ok(())
}

fn clear_selector(stdout: &mut io::Stdout, total: usize) -> Result<()> {
    execute!(
        stdout,
        cursor::MoveUp((total + 2) as u16),
        terminal::Clear(ClearType::FromCursorDown),
    )?;
    Ok(())
}

// -------------------------------------------------------------------------
// Main entry point
// -------------------------------------------------------------------------

/// Run the commit command.
pub async fn run(args: CommitArgs) -> Result<()> {
    // 1. Check staged changes
    let diff = get_staged_diff()?;
    check_unstaged_warning();

    // 2. Truncate diff to token budget
    let truncated = resq_ai::truncate_to_budget(&diff, args.max_diff);
    let recent = get_recent_commits();

    // 3. Load AI config with CLI overrides
    let mut config = resq_ai::load_config()?;
    if let Some(ref p) = args.provider {
        config.provider = match p.to_lowercase().as_str() {
            "anthropic" => resq_ai::Provider::Anthropic,
            "openai" => resq_ai::Provider::OpenAi,
            "gemini" => resq_ai::Provider::Gemini,
            other => bail!("Unknown provider: {other}. Use: anthropic, openai, gemini"),
        };
    }
    if let Some(ref m) = args.model {
        config.model = m.clone();
    }
    config.timeout_secs = args.timeout;

    // 4. Build prompt
    let (system, user_prompt) = build_prompt(truncated, &recent, args.scope.as_deref(), args.count);

    if args.verbose {
        eprintln!("--- System prompt ---\n{system}\n---");
        eprintln!(
            "Estimated tokens: {}",
            resq_ai::estimate_tokens(&user_prompt)
        );
    }

    // 5. Call LLM
    eprintln!("Generating commit messages...");
    let response = resq_ai::complete(&config, &system, &user_prompt).await?;

    if args.verbose {
        eprintln!("--- Raw response ---\n{response}\n---");
    }

    // 6. Parse + validate candidates
    let candidates = parse_candidates(&response)?;
    let valid: Vec<String> = candidates
        .into_iter()
        .filter(|c| validate_conventional_commit(c))
        .collect();

    if valid.is_empty() {
        bail!("LLM returned no valid Conventional Commit messages. Try again or write manually.");
    }

    // 7. Select
    if args.dry_run {
        for (i, c) in valid.iter().enumerate() {
            println!("{}. {c}", i + 1);
        }
        return Ok(());
    }

    let message = if args.yes {
        valid.into_iter().next().unwrap()
    } else {
        let idx = select_candidate(&valid)?;
        match idx {
            Some(i) => valid.into_iter().nth(i).unwrap(),
            None => {
                eprintln!("Cancelled.");
                return Ok(());
            }
        }
    };

    // 8. Commit
    git(&["commit", "-m", &message])?;
    eprintln!("Committed: {message}");
    Ok(())
}

// -------------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------------

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

    #[test]
    fn strip_fences_json() {
        let input = "```json\n[\"feat: add thing\"]\n```";
        assert_eq!(strip_code_fences(input), "[\"feat: add thing\"]");
    }

    #[test]
    fn strip_fences_plain() {
        let input = "```\n[\"feat: add thing\"]\n```";
        assert_eq!(strip_code_fences(input), "[\"feat: add thing\"]");
    }

    #[test]
    fn strip_fences_none() {
        let input = "[\"feat: add thing\"]";
        assert_eq!(strip_code_fences(input), input);
    }

    #[test]
    fn validate_cc_valid() {
        assert!(validate_conventional_commit("feat: add new feature"));
        assert!(validate_conventional_commit(
            "fix(ui): correct button color"
        ));
        assert!(validate_conventional_commit("feat!: remove deprecated API"));
        assert!(validate_conventional_commit(
            "chore(deps): bump serde to 1.0.200"
        ));
    }

    #[test]
    fn validate_cc_invalid() {
        assert!(!validate_conventional_commit("Add new feature"));
        assert!(!validate_conventional_commit("FEAT: uppercase type"));
        assert!(!validate_conventional_commit("feat:missing space"));
        assert!(!validate_conventional_commit(""));
    }

    #[test]
    fn parse_candidates_valid_json() {
        let input = r#"["feat: add thing", "fix: repair thing"]"#;
        let result = parse_candidates(input).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], "feat: add thing");
    }

    #[test]
    fn parse_candidates_with_fences() {
        let input = "```json\n[\"feat: add thing\"]\n```";
        let result = parse_candidates(input).unwrap();
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn build_prompt_includes_scope() {
        let (system, _user) = build_prompt("diff content", "recent commits", Some("auth"), 3);
        assert!(system.contains("auth"));
    }

    #[test]
    fn build_prompt_without_scope() {
        let (system, _user) = build_prompt("diff content", "recent commits", None, 3);
        assert!(!system.contains("Suggested scope"));
    }

    #[test]
    fn build_prompt_includes_count() {
        let (system, _user) = build_prompt("diff", "commits", None, 5);
        assert!(system.contains("exactly 5"));
    }
}