crabmate 0.5.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
//! 发版辅助:变更日志 Markdown 草稿、依赖许可证摘要(只生成文本,不写仓库)。

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::path::Path;
use std::process::Command;

use crate::cm_tools::cargo_metadata::cargo_metadata_command;

use super::git;
use super::output_util;

fn truncate_str(s: &str, max_bytes: usize) -> String {
    if s.len() <= max_bytes {
        return s.to_string();
    }
    let keep = max_bytes.saturating_sub(80);
    let truncated = output_util::truncate_to_char_boundary(s, keep);
    format!("{}\n\n... (输出已按 {} 字节截断)", truncated, max_bytes)
}

/// 仅允许常见 Git 引用字符,降低注入风险(传给 `git log` 等子进程)。
fn is_safe_git_ref(s: &str) -> bool {
    !s.is_empty()
        && s.len() <= 256
        && !s.chars().any(|c| {
            matches!(
                c,
                ';' | '|' | '&' | '$' | '`' | '\n' | '\r' | '(' | ')' | '<' | '>'
            )
        })
}

fn run_git_stdout(working_dir: &Path, args: &[&str]) -> Result<String, String> {
    let mut cmd = Command::new("git");
    for a in args {
        cmd.arg(a);
    }
    cmd.current_dir(working_dir);
    let out = cmd.output().map_err(|e| {
        let b = format!("无法执行 git: {}", e);
        output_util::append_notfound_install_hint(b, &e, "git")
    })?;
    if !out.status.success() {
        let stderr = String::from_utf8_lossy(&out.stderr);
        return Err(format!(
            "git 失败 (exit={}): {}",
            out.status.code().unwrap_or(-1),
            stderr.trim()
        ));
    }
    Ok(String::from_utf8_lossy(&out.stdout).to_string())
}

/// 基于 `git log` 生成 **Markdown 草稿**(不写任何文件)。
///
/// 参数:
/// - `since` / `until`:可选引用,范围 `since..until`;皆空则自 HEAD 起;仅 `until` 时 `git log until`
/// - `max_commits`:默认 500,上限 2000
/// - `group_by`:`date`(按提交日聚合)| `flat`(单层列表)| `tag_ranges`(按相邻 tag 分段,tag 按 `-v:refname` 降序取最近若干)
/// - `max_tag_sections`:`tag_ranges` 时最多展示几段(默认 25)
pub fn changelog_draft(args_json: &str, working_dir: &Path, max_output_len: usize) -> String {
    let v = match crate::cm_tools::tools::parse_args_json(args_json) {
        Ok(v) => v,
        Err(e) => return e,
    };
    if let Err(e) = git::ensure_git_repo(working_dir) {
        return e;
    }

    let since = v
        .get("since")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .unwrap_or("");
    let until = v
        .get("until")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .unwrap_or("");
    if !since.is_empty() && !is_safe_git_ref(since) {
        return "错误:since 含非法字符".to_string();
    }
    if !until.is_empty() && !is_safe_git_ref(until) {
        return "错误:until 含非法字符".to_string();
    }

    let max_commits = v
        .get("max_commits")
        .and_then(|x| x.as_u64())
        .unwrap_or(500)
        .clamp(1, 2000) as usize;

    let group_by = v
        .get("group_by")
        .and_then(|x| x.as_str())
        .map(|s| s.trim().to_lowercase())
        .unwrap_or_else(|| "date".to_string());

    let max_tag_sections = v
        .get("max_tag_sections")
        .and_then(|x| x.as_u64())
        .unwrap_or(25)
        .clamp(1, 100) as usize;

    let mut md = String::from(
        "> 以下为 **Markdown 草稿**,由 `changelog_draft` 生成;**未写入**任何文件,请人工审阅后使用。\n\n",
    );

    let body = match group_by.as_str() {
        "flat" => changelog_flat(working_dir, since, until, max_commits),
        "tag_ranges" | "tags" => {
            changelog_by_tag_ranges(working_dir, max_commits, max_tag_sections)
        }
        "date" => changelog_by_date(working_dir, since, until, max_commits),
        _ => changelog_by_date(working_dir, since, until, max_commits),
    };

    match body {
        Ok(s) => {
            md.push_str(&s);
            truncate_str(&md, max_output_len)
        }
        Err(e) => e,
    }
}

/// 追加 `git log` 的 rev 参数(若有);`(空,空)` 表示从 HEAD 走祖先。
fn push_log_rev(args: &mut Vec<String>, since: &str, until: &str) {
    match (since.is_empty(), until.is_empty()) {
        (true, true) => {}
        (true, false) => args.push(until.to_string()),
        (false, true) => args.push(format!("{}..HEAD", since)),
        (false, false) => args.push(format!("{}..{}", since, until)),
    }
}

fn changelog_sections_by_date(text: &str) -> BTreeMap<String, Vec<String>> {
    let mut sections: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let Some((date, rest)) = line.split_once('|') else {
            continue;
        };
        sections
            .entry(date.to_string())
            .or_default()
            .push(format!("- {}", rest.trim()));
    }
    sections
}

fn format_changelog_by_date(
    since: &str,
    until: &str,
    sections: BTreeMap<String, Vec<String>>,
) -> String {
    let mut out = String::from("# 变更日志(草稿)\n\n");
    if !since.is_empty() || !until.is_empty() {
        out.push_str(&format!(
            "_范围:`{}` … `{}`(Git 范围语法)_\n\n",
            if since.is_empty() { "(起)" } else { since },
            if until.is_empty() { "HEAD" } else { until }
        ));
    }
    for (date, items) in sections.into_iter().rev() {
        out.push_str(&format!("## {}\n\n", date));
        for it in items {
            out.push_str(&it);
            out.push('\n');
        }
        out.push('\n');
    }
    if out.ends_with("\n\n") {
        out.pop();
    }
    out
}

fn changelog_by_date(
    working_dir: &Path,
    since: &str,
    until: &str,
    max_commits: usize,
) -> Result<String, String> {
    let mut args: Vec<String> = vec![
        "log".into(),
        format!("--max-count={}", max_commits),
        "--format=%cs|%h %s".into(),
    ];
    push_log_rev(&mut args, since, until);
    let argv: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
    let text = run_git_stdout(working_dir, &argv)?;
    Ok(format_changelog_by_date(
        since,
        until,
        changelog_sections_by_date(&text),
    ))
}

fn changelog_flat(
    working_dir: &Path,
    since: &str,
    until: &str,
    max_commits: usize,
) -> Result<String, String> {
    let mut args: Vec<String> = vec![
        "log".into(),
        format!("--max-count={}", max_commits),
        "--format=- %h %s".into(),
    ];
    push_log_rev(&mut args, since, until);
    let argv: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
    let text = run_git_stdout(working_dir, &argv)?;
    let mut out = String::from("# 变更日志(草稿 · 平铺)\n\n");
    out.push_str(&text);
    Ok(out)
}

fn changelog_by_tag_ranges(
    working_dir: &Path,
    max_commits_per_section: usize,
    max_sections: usize,
) -> Result<String, String> {
    let tags_out = run_git_stdout(working_dir, &["tag", "--sort=-v:refname"])?;
    let tags: Vec<String> = tags_out
        .lines()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(String::from)
        .collect();
    if tags.is_empty() {
        return Ok("# 变更日志(草稿 · 按 tag)\n\n_未找到任何 tag,可改用 `group_by: \"date\"` 或 `flat`._\n"
            .to_string());
    }
    if tags.len() < 2 {
        return Ok(
            "# 变更日志(草稿 · 按 tag 区间)\n\n_仅有一个 tag,无法形成相邻区间;请改用 `group_by: \"date\"` / `flat`,或增加 tag 后再试。_\n"
                .to_string(),
        );
    }

    let take_tags = (max_sections + 1).min(tags.len());
    let tags: Vec<String> = tags.into_iter().take(take_tags).collect();
    let pairs = tags.len() - 1;

    let mut out = String::from("# 变更日志(草稿 · 按 tag 区间)\n\n");
    out.push_str("_说明:每段为「较旧 tag .. 较新 tag」之间的提交(`--no-merges`);tag 按 `-v:refname` 降序取前 `max_tag_sections+1` 个后两两相邻配对。_\n\n");

    for i in 0..pairs {
        let hi = &tags[i];
        let lo = &tags[i + 1];
        if !is_safe_git_ref(hi) || !is_safe_git_ref(lo) {
            continue;
        }
        let range = format!("{}..{}", lo, hi);
        let mc = format!("--max-count={}", max_commits_per_section);
        let log = run_git_stdout(
            working_dir,
            &["log", &mc, "--no-merges", "--format=- %h %s", &range],
        )?;

        out.push_str(&format!("## `{}` ← `{}`\n\n", hi, lo));
        if log.trim().is_empty() {
            out.push_str("_(此区间内无提交)_\n\n");
        } else {
            out.push_str(log.trim_end());
            out.push_str("\n\n");
        }
    }

    Ok(out)
}

/// 使用 `cargo metadata` 解析依赖包,生成 **crate → 许可证** Markdown 表(只读,不写文件)。
///
/// - `workspace_only`:仅工作区成员包(默认 false,含传递依赖)
/// - `max_crates`:最多输出行数(默认 500,上限 3000)
pub fn license_notice(args_json: &str, workspace_root: &Path, max_output_len: usize) -> String {
    let v = match crate::cm_tools::tools::parse_args_json(args_json) {
        Ok(v) => v,
        Err(e) => return e,
    };
    if !workspace_root.join("Cargo.toml").is_file() {
        return "错误:当前工作目录未找到 Cargo.toml".to_string();
    }

    let workspace_only = v
        .get("workspace_only")
        .and_then(|x| x.as_bool())
        .unwrap_or(false);
    let max_crates = v
        .get("max_crates")
        .and_then(|x| x.as_u64())
        .unwrap_or(500)
        .clamp(1, 3000) as usize;

    let output = match cargo_metadata_command(workspace_root, false, 1).output() {
        Ok(o) => o,
        Err(e) => return format!("无法执行 cargo metadata: {}", e),
    };
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return format!(
            "cargo metadata 失败 (exit={}): {}",
            output.status.code().unwrap_or(-1),
            stderr.trim()
        );
    }

    let meta: serde_json::Value = match serde_json::from_slice(&output.stdout) {
        Ok(m) => m,
        Err(e) => return format!("解析 cargo metadata JSON 失败: {}", e),
    };

    let by_name = match collect_licenses(&meta, workspace_only) {
        Ok(m) => m,
        Err(e) => return e,
    };

    truncate_str(&render_license_table(&by_name, max_crates), max_output_len)
}

/// 解析 cargo metadata 中的 workspace_members / packages,按 crate 名聚合多版本 license。
fn collect_licenses(
    meta: &serde_json::Value,
    workspace_only: bool,
) -> Result<BTreeMap<String, BTreeSet<String>>, String> {
    let workspace_ids: HashSet<String> = meta
        .get("workspace_members")
        .and_then(|x| x.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|x| x.as_str().map(String::from))
                .collect()
        })
        .unwrap_or_default();

    let Some(packages) = meta.get("packages").and_then(|x| x.as_array()) else {
        return Err("错误:metadata 中无 packages 数组".to_string());
    };

    let mut by_name: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    for pkg in packages {
        let Some(id) = pkg.get("id").and_then(|x| x.as_str()) else {
            continue;
        };
        if workspace_only && !workspace_ids.contains(id) {
            continue;
        }
        let Some(name) = pkg.get("name").and_then(|x| x.as_str()) else {
            continue;
        };
        let lic = pkg
            .get("license")
            .and_then(|x| x.as_str())
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(String::from)
            .unwrap_or_else(|| "(Cargo.toml 未声明 license)".to_string());
        by_name.entry(name.to_string()).or_default().insert(lic);
    }
    Ok(by_name)
}

/// 将聚合结果渲染为 Markdown 表格(限 `max_crates` 行)。
fn render_license_table(
    by_name: &BTreeMap<String, BTreeSet<String>>,
    max_crates: usize,
) -> String {
    let mut md = String::from(
        "> 以下为 **许可证摘要草稿**(来自 `cargo metadata`),**非法律意见**;未声明项已标注。发版前请人工核对。\n\n",
    );
    md.push_str("# 依赖许可证摘要\n\n");
    md.push_str("| crate | license(合并多版本) |\n|---|---|\n");

    for (count, (name, set)) in by_name.iter().enumerate() {
        if count >= max_crates {
            break;
        }
        let lic_cell = set.iter().cloned().collect::<Vec<_>>().join(" · ");
        let name_esc = name.replace('|', "\\|");
        let lic_esc = lic_cell.replace('|', "\\|");
        md.push_str(&format!("| {} | {} |\n", name_esc, lic_esc));
    }

    if by_name.len() > max_crates {
        md.push_str(&format!(
            "\n_(仅展示前 {} 条,共 {} 个 crate 名;可调大 `max_crates`)_\n",
            max_crates,
            by_name.len()
        ));
    }

    md
}

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

    #[test]
    fn license_table_from_sample_metadata() {
        let sample = r#"{
            "packages": [
                {"id":"a","name":"serde","license":"MIT"},
                {"id":"b","name":"serde","license":"MIT OR Apache-2.0"},
                {"id":"c","name":"nolic","license":null}
            ],
            "workspace_members": []
        }"#;
        let meta: serde_json::Value = serde_json::from_str(sample).unwrap();
        let packages = meta["packages"].as_array().unwrap();
        let mut by_name: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
        for pkg in packages {
            let name = pkg["name"].as_str().unwrap();
            let lic = pkg["license"]
                .as_str()
                .filter(|s| !s.is_empty())
                .map(String::from)
                .unwrap_or_else(|| "(Cargo.toml 未声明 license)".to_string());
            by_name.entry(name.to_string()).or_default().insert(lic);
        }
        assert!(by_name["serde"].len() == 2);
        assert!(by_name["nolic"].contains("(Cargo.toml 未声明 license)"));
    }

    #[test]
    fn safe_git_ref_rejects_shell() {
        assert!(!is_safe_git_ref("main;rm -rf"));
        assert!(is_safe_git_ref("v1.0.0"));
        assert!(is_safe_git_ref("feature/foo-bar"));
    }
}