crabmate 0.4.0

Rust AI agent: OpenAI-compatible chat/completions, function calling, HTTP serve, ops CLI
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
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
// ── gh * ──────────────────────────────────────────────────────

fn gh_repo_suffix(repo: Option<String>) -> String {
    repo.as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(|r| format!(" ({})", r))
        .unwrap_or_default()
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrListSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    #[serde(default)]
    state: Option<String>,
    #[serde(default)]
    limit: Option<u64>,
}

impl ToolSummaryLine for GhPrListSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let st = self.state.as_deref().unwrap_or("open");
        let lim = self.limit.unwrap_or(30);
        Some(format!(
            "gh pr list{} state={} limit={}",
            gh_repo_suffix(self.repo),
            st,
            lim
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrNumberSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    number: u64,
}

impl ToolSummaryLine for GhPrNumberSummaryArgs {
    fn summary_line(self) -> Option<String> {
        Some(format!(
            "gh pr view #{}{}",
            self.number,
            gh_repo_suffix(self.repo)
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrChecksSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    #[serde(default)]
    number: Option<u64>,
}

impl ToolSummaryLine for GhPrChecksSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let suffix = gh_repo_suffix(self.repo);
        match self.number {
            Some(n) if n > 0 => Some(format!("gh pr checks #{}{}", n, suffix)),
            _ => Some(format!("gh pr checks{}", suffix)),
        }
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrCreateSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    title: String,
    #[serde(default)]
    draft: Option<bool>,
}

impl ToolSummaryLine for GhPrCreateSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let t = self.title.trim();
        if t.is_empty() {
            return None;
        }
        let mut head: String = t.chars().take(40).collect();
        if t.chars().count() > 40 {
            head.push('');
        }
        let draft = self.draft == Some(true);
        Some(format!(
            "gh pr create{}{}{}",
            gh_repo_suffix(self.repo),
            if draft { " draft" } else { "" },
            if head.is_empty() {
                String::new()
            } else {
                format!(": {}", head)
            }
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrMergeSummaryArgs {
    #[serde(default)]
    number: Option<u64>,
    #[serde(default)]
    merge_method: Option<String>,
}

impl ToolSummaryLine for GhPrMergeSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let method = self.merge_method.as_deref().unwrap_or("rebase");
        match self.number {
            Some(n) if n > 0 => Some(format!("gh pr merge #{n} ({method})")),
            _ => Some(format!("gh pr merge ({method})")),
        }
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrReviewSummaryArgs {
    event: String,
    #[serde(default)]
    number: Option<u64>,
}

impl ToolSummaryLine for GhPrReviewSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let ev = self.event.trim();
        if ev.is_empty() {
            return None;
        }
        match self.number {
            Some(n) if n > 0 => Some(format!("gh pr review #{n} {ev}")),
            _ => Some(format!("gh pr review {ev}")),
        }
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrCommentSummaryArgs {
    #[serde(default)]
    number: Option<u64>,
}

impl ToolSummaryLine for GhPrCommentSummaryArgs {
    fn summary_line(self) -> Option<String> {
        match self.number {
            Some(n) if n > 0 => Some(format!("gh pr comment #{n}")),
            _ => Some("gh pr comment".to_string()),
        }
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrBodyDraftSummaryArgs {
    #[serde(default)]
    base: Option<String>,
}

impl ToolSummaryLine for GhPrBodyDraftSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let base = self.base.as_deref().unwrap_or("main");
        Some(format!("gh pr body draft (base={base})"))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrEditSummaryArgs {
    #[serde(default)]
    number: Option<u64>,
    #[serde(default)]
    title: Option<String>,
}

impl ToolSummaryLine for GhPrEditSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let num = match self.number {
            Some(n) if n > 0 => format!("#{n}"),
            _ => String::new(),
        };
        let t = self.title.unwrap_or_default();
        let t = t.trim();
        if t.is_empty() {
            return Some(format!("gh pr edit {num}").trim_end().to_string());
        }
        let mut head: String = t.chars().take(40).collect();
        if t.chars().count() > 40 {
            head.push('');
        }
        Some(format!("gh pr edit {num}: {head}").trim_end().to_string())
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhIssueListSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    #[serde(default)]
    state: Option<String>,
    #[serde(default)]
    limit: Option<u64>,
}

impl ToolSummaryLine for GhIssueListSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let st = self.state.as_deref().unwrap_or("open");
        let lim = self.limit.unwrap_or(30);
        Some(format!(
            "gh issue list{} state={} limit={}",
            gh_repo_suffix(self.repo),
            st,
            lim
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhIssueViewSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    number: u64,
}

impl ToolSummaryLine for GhIssueViewSummaryArgs {
    fn summary_line(self) -> Option<String> {
        Some(format!(
            "gh issue view #{}{}",
            self.number,
            gh_repo_suffix(self.repo)
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhIssueCreateSummaryArgs {
    title: String,
    #[serde(default)]
    repo: Option<String>,
}

impl ToolSummaryLine for GhIssueCreateSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let t = self.title.trim();
        if t.is_empty() {
            return None;
        }
        let mut head: String = t.chars().take(40).collect();
        if t.chars().count() > 40 {
            head.push('');
        }
        Some(format!(
            "gh issue create{}: {}",
            gh_repo_suffix(self.repo),
            head
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhRunListSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    #[serde(default)]
    limit: Option<u64>,
}

impl ToolSummaryLine for GhRunListSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let lim = self.limit.unwrap_or(30);
        Some(format!(
            "gh run list{} limit={}",
            gh_repo_suffix(self.repo),
            lim
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhPrDiffSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    number: u64,
}

impl ToolSummaryLine for GhPrDiffSummaryArgs {
    fn summary_line(self) -> Option<String> {
        Some(format!(
            "gh pr diff #{}{}",
            self.number,
            gh_repo_suffix(self.repo)
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhRunViewSummaryArgs {
    run_id: String,
    #[serde(default)]
    log: bool,
}

impl ToolSummaryLine for GhRunViewSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let id = self.run_id.trim();
        if id.is_empty() {
            return None;
        }
        Some(format!(
            "gh run view {}{}",
            id,
            if self.log { " --log" } else { "" }
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhRunRerunSummaryArgs {
    run_id: String,
    #[serde(default)]
    failed: bool,
}

impl ToolSummaryLine for GhRunRerunSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let id = self.run_id.trim();
        if id.is_empty() {
            return None;
        }
        Some(format!(
            "gh run rerun {}{}",
            id,
            if self.failed { " --failed" } else { "" }
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhRunFailureSummarySummaryArgs {
    run_id: String,
}

impl ToolSummaryLine for GhRunFailureSummarySummaryArgs {
    fn summary_line(self) -> Option<String> {
        let id = self.run_id.trim();
        if id.is_empty() {
            return None;
        }
        Some(format!("gh run failure summary {id}"))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhReleaseListSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    #[serde(default)]
    limit: Option<u64>,
}

impl ToolSummaryLine for GhReleaseListSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let lim = self.limit.unwrap_or(30);
        Some(format!(
            "gh release list{} limit={}",
            gh_repo_suffix(self.repo),
            lim
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhReleaseViewSummaryArgs {
    #[serde(default)]
    repo: Option<String>,
    tag: String,
}

impl ToolSummaryLine for GhReleaseViewSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let tag = self.tag.trim();
        if tag.is_empty() {
            return None;
        }
        let mut t: String = tag.chars().take(32).collect();
        if tag.chars().count() > 32 {
            t.push('');
        }
        Some(format!(
            "gh release view {}{}",
            t,
            gh_repo_suffix(self.repo)
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhReleaseCreateSummaryArgs {
    tag: String,
    #[serde(default)]
    draft: Option<bool>,
}

impl ToolSummaryLine for GhReleaseCreateSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let tag = self.tag.trim();
        if tag.is_empty() {
            return None;
        }
        let draft = self.draft == Some(true);
        Some(format!(
            "gh release create {}{}",
            tag,
            if draft { " (draft)" } else { "" }
        ))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhSearchSummaryArgs {
    scope: String,
    query: String,
}

impl ToolSummaryLine for GhSearchSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let scope = self.scope.trim();
        let q = self.query.trim();
        if scope.is_empty() || q.is_empty() {
            return None;
        }
        let mut qs: String = q.chars().take(40).collect();
        if q.chars().count() > 40 {
            qs.push('');
        }
        Some(format!("gh search {} {}", scope, qs))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct GhApiSummaryArgs {
    path: String,
    #[serde(default)]
    method: Option<String>,
}

impl ToolSummaryLine for GhApiSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let path = self.path.trim();
        if path.is_empty() {
            return None;
        }
        let method = self
            .method
            .as_deref()
            .unwrap_or("GET")
            .trim()
            .to_ascii_uppercase();
        let mut p: String = path.chars().take(40).collect();
        if path.chars().count() > 40 {
            p.push('');
        }
        Some(format!("gh api {} {}", method, p))
    }
}

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

    #[test]
    fn git_rebase_continue_json_key_maps() {
        let v = json!({ "continue": true });
        let s = summarize_from_value::<GitRebaseSummaryArgs>(&v).expect("summary");
        assert_eq!(s, "git rebase --continue");
    }

    #[test]
    fn run_command_summary_joins_command_and_string_args() {
        let v = json!({ "command": "cargo", "args": ["test", "--all"] });
        let s = summarize_from_value::<RunCommandSummaryArgs>(&v).expect("summary");
        assert_eq!(s, "cargo test --all");
    }

    #[test]
    fn run_command_summary_accepts_args_as_single_string() {
        let v = json!({ "command": "bash", "args": "-c echo hi" });
        let s = summarize_from_value::<RunCommandSummaryArgs>(&v).expect("summary");
        assert!(s.starts_with("bash "), "{s}");
        assert!(s.contains("echo hi"), "{s}");
    }

    #[test]
    fn run_command_summary_quotes_args_with_spaces() {
        let v = json!({ "command": "bash", "args": ["-c", "echo hello world"] });
        let s = summarize_from_value::<RunCommandSummaryArgs>(&v).expect("summary");
        assert_eq!(s, r#"bash -c "echo hello world""#);
    }

    #[test]
    fn ast_grep_rewrite_dry_run_defaults_true() {
        let v = json!({ "lang": "rust", "pattern": "foo" });
        let s = summarize_from_value::<AstGrepRewriteSummaryArgs>(&v).expect("summary");
        assert!(s.contains("(dry-run)"), "got {s}");
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct FileExistsSummaryArgs {
    path: String,
}

impl ToolSummaryLine for FileExistsSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let path = self.path.trim();
        if path.is_empty() {
            return None;
        }
        Some(format!("file exists: {}", path))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct ReadBinaryMetaSummaryArgs {
    path: String,
}

impl ToolSummaryLine for ReadBinaryMetaSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let path = self.path.trim();
        if path.is_empty() {
            return None;
        }
        Some(format!("binary metadata: {}", path))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct DeleteFileSummaryArgs {
    path: String,
}

impl ToolSummaryLine for DeleteFileSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let path = self.path.trim();
        if path.is_empty() {
            return None;
        }
        Some(format!("delete file: {}", path))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct AppendFileSummaryArgs {
    path: String,
}

impl ToolSummaryLine for AppendFileSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let path = self.path.trim();
        if path.is_empty() {
            return None;
        }
        Some(format!("append to file: {}", path))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct CreateDirSummaryArgs {
    path: String,
}

impl ToolSummaryLine for CreateDirSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let path = self.path.trim();
        if path.is_empty() {
            return None;
        }
        Some(format!("create directory: {}", path))
    }
}

#[derive(Debug, Deserialize)]
pub(super) struct SymlinkInfoSummaryArgs {
    path: String,
}

impl ToolSummaryLine for SymlinkInfoSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let path = self.path.trim();
        if path.is_empty() {
            return None;
        }
        Some(format!("symlink info: {}", path))
    }
}

// ── archive_pack ─────────────────────────────────────────────

#[derive(Debug, Deserialize)]
pub(super) struct ArchivePackSummaryArgs {
    output: String,
    #[serde(default)]
    sources: Vec<String>,
}

impl ToolSummaryLine for ArchivePackSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let output = self.output.trim();
        let count = self.sources.len();
        if output.is_empty() {
            return None;
        }
        Some(format!("pack {} items into {}", count, output))
    }
}

// ── archive_unpack ───────────────────────────────────────────

#[derive(Debug, Deserialize)]
pub(super) struct ArchiveUnpackSummaryArgs {
    archive: String,
    #[serde(default)]
    output_dir: Option<String>,
}

impl ToolSummaryLine for ArchiveUnpackSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let archive = self.archive.trim();
        if archive.is_empty() {
            return None;
        }
        let dir = self
            .output_dir
            .as_deref()
            .filter(|s| !s.is_empty())
            .unwrap_or(".");
        Some(format!("unpack {} to {}", archive, dir))
    }
}

// ── archive_list ────────────────────────────────────────────

#[derive(Debug, Deserialize)]
pub(super) struct ArchiveListSummaryArgs {
    archive: String,
}

impl ToolSummaryLine for ArchiveListSummaryArgs {
    fn summary_line(self) -> Option<String> {
        let archive = self.archive.trim();
        if archive.is_empty() {
            return None;
        }
        Some(format!("list archive: {}", archive))
    }
}