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
//! 工作区内的代码格式化工具。
//!
//! 根据文件扩展名自动选择本地格式化器:
//! - `.rs`   -> `rustfmt`
//! - `.py`   -> `ruff format`
//! - `.c` / `.h` / `.cpp` / `.cc` / `.cxx` / `.hpp` / `.hh` -> `clang-format`
//! - `.ts` / `.tsx` / `.js` / `.jsx` / `.json` -> `npx prettier --write`
//! - `.go`   -> `gofmt`
//! - `.sh` / `.bash` / `.zsh` -> `shfmt`
//! - `.md` / `.yaml` / `.yml` / `.css` / `.scss` / `.less` / `.html` / `.vue` / `.svelte` / `.graphql` -> `npx prettier`
//! - `.xml`  -> `xmllint --format`
//! - `.sql`  -> `sqlfluff fix` / `pg_format`
//!
//! 参数:{ "path": "相对工作区根目录的文件路径" }
//! 会直接对目标文件就地格式化,并返回简要的结果说明。

use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use super::output_util;
use super::python_tools;
use super::tool_param_types::FormatOnePathArgs;

pub fn run(args_json: &str, workspace_root: &Path) -> String {
    let v = match crate::cm_tools::tools::parse_args_json(args_json) {
        Ok(v) => v,
        Err(e) => return e,
    };
    let FormatOnePathArgs { path } = match serde_json::from_value(v) {
        Ok(a) => a,
        Err(e) => return format!("参数 JSON 与 format_file 形状不一致: {e}"),
    };
    let path = path.trim();
    if path.is_empty() {
        return "错误:缺少 path 参数".to_string();
    }

    let target = match resolve_target(workspace_root, path) {
        Ok(p) => p,
        Err(e) => return e,
    };

    if !target.exists() {
        return "错误:指定的文件不存在".to_string();
    }
    if !target.is_file() {
        return "错误:只能格式化普通文件,不能对目录执行格式化".to_string();
    }

    let ext = target
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_lowercase();

    let formatter = select_formatter(&ext);
    match formatter {
        Some(f) => match run_formatter(f, &target, workspace_root, false) {
            Ok(msg) => msg,
            Err(e) => e,
        },
        None => format!("错误:暂不支持扩展名为 .{} 的文件格式化", ext),
    }
}

/// 对单个文件做格式「检查」(不写入)。
pub fn run_check(args_json: &str, workspace_root: &Path) -> String {
    let v = match crate::cm_tools::tools::parse_args_json(args_json) {
        Ok(v) => v,
        Err(e) => return e,
    };
    let FormatOnePathArgs { path } = match serde_json::from_value(v) {
        Ok(a) => a,
        Err(e) => return format!("参数 JSON 与 format_check_file 形状不一致: {e}"),
    };
    let path = path.trim();
    if path.is_empty() {
        return "错误:缺少 path 参数".to_string();
    }

    let target = match resolve_target(workspace_root, path) {
        Ok(p) => p,
        Err(e) => return e,
    };

    if !target.exists() {
        return "错误:指定的文件不存在".to_string();
    }
    if !target.is_file() {
        return "错误:只能检查普通文件".to_string();
    }

    let ext = target
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("")
        .to_lowercase();

    let formatter = select_formatter(&ext);
    match formatter {
        Some(f) => match run_formatter(f, &target, workspace_root, true) {
            Ok(msg) => msg,
            Err(e) => e,
        },
        None => format!("错误:暂不支持扩展名为 .{} 的格式检查", ext),
    }
}

#[derive(Copy, Clone)]
enum Formatter {
    Rustfmt,
    Prettier,
    Ruff,
    ClangFormat,
    Gofmt,
    Shfmt,
    XmlLint,
    SqlFormat,
}

fn is_c_cpp_extension(ext: &str) -> bool {
    matches!(ext, "c" | "h" | "cpp" | "cc" | "cxx" | "hpp" | "hh")
}

fn select_formatter(ext: &str) -> Option<Formatter> {
    if ext == "rs" {
        Some(Formatter::Rustfmt)
    } else if ext == "py" {
        Some(Formatter::Ruff)
    } else if is_c_cpp_extension(ext) {
        Some(Formatter::ClangFormat)
    } else if ext == "go" {
        Some(Formatter::Gofmt)
    } else if matches!(ext, "sh" | "bash" | "zsh") {
        Some(Formatter::Shfmt)
    } else if ext == "xml" {
        Some(Formatter::XmlLint)
    } else if ext == "sql" {
        Some(Formatter::SqlFormat)
    } else if matches!(
        ext,
        "ts" | "tsx"
            | "js"
            | "jsx"
            | "json"
            | "md"
            | "markdown"
            | "yaml"
            | "yml"
            | "css"
            | "scss"
            | "less"
            | "html"
            | "htm"
            | "vue"
            | "svelte"
            | "graphql"
    ) {
        Some(Formatter::Prettier)
    } else {
        None
    }
}

/// 工具返回说明中的路径:相对工作区根(POSIX),不输出绝对路径。
fn display_in_workspace(workspace_root: &Path, target: &Path) -> String {
    let Ok(base) = workspace_root.canonicalize() else {
        return target.display().to_string();
    };
    match target.strip_prefix(&base) {
        Ok(rel) => {
            let s = rel.to_string_lossy().replace('\\', "/");
            if s.is_empty() { ".".to_string() } else { s }
        }
        Err(_) => target.display().to_string(),
    }
}

fn resolve_target(base: &Path, sub: &str) -> Result<PathBuf, String> {
    let sub_path = Path::new(sub);
    if sub_path.is_absolute() {
        return Err("路径必须是相对于工作区根目录的相对路径,不能使用绝对路径".to_string());
    }
    let base_canonical = base
        .canonicalize()
        .map_err(|e| format!("工作区根目录无法解析: {}", e))?;
    let joined = base_canonical.join(sub_path);
    let canonical = joined
        .canonicalize()
        .map_err(|e| format!("目标文件路径无法解析: {}", e))?;
    if !canonical.starts_with(&base_canonical) {
        return Err("目标文件路径不能超出工作区根目录".to_string());
    }
    Ok(canonical)
}

fn run_formatter(
    formatter: Formatter,
    target: &Path,
    workspace_root: &Path,
    check_only: bool,
) -> Result<String, String> {
    match formatter {
        Formatter::Rustfmt => run_rustfmt(target, workspace_root, check_only),
        Formatter::Prettier => run_prettier(target, workspace_root, check_only),
        Formatter::Ruff => python_tools::ruff_format_file(target, workspace_root, check_only),
        Formatter::ClangFormat => run_clang_format(target, workspace_root, check_only),
        Formatter::Gofmt => run_gofmt(target, workspace_root, check_only),
        Formatter::Shfmt => run_shfmt(target, workspace_root, check_only),
        Formatter::XmlLint => run_xmllint(target, workspace_root, check_only),
        Formatter::SqlFormat => run_sql_format(target, workspace_root, check_only),
    }
}

fn run_rustfmt(target: &Path, workspace_root: &Path, check_only: bool) -> Result<String, String> {
    let mut cmd = Command::new("rustfmt");
    if check_only {
        cmd.arg("--check");
    } else {
        cmd.arg("--emit").arg("files");
    }
    cmd.arg(target);
    // TUI 全屏下若继承 stdout/stderr,子进程输出会直接画到终端(常落在输入框区域),必须捕获。
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let output = cmd.output().map_err(|e| {
        let b = format!("无法执行 rustfmt:{}(请确认已安装 rustfmt)", e);
        output_util::append_notfound_install_hint(b, &e, "rustfmt")
    })?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let stdout = String::from_utf8_lossy(&output.stdout);
        let detail = if !stderr.trim().is_empty() {
            stderr.trim_end().to_string()
        } else if !stdout.trim().is_empty() {
            stdout.trim_end().to_string()
        } else {
            String::new()
        };
        let suffix = if detail.is_empty() {
            String::new()
        } else {
            format!("\n{}", detail)
        };
        return Err(format!(
            "rustfmt {}失败,退出码:{}{}",
            if check_only { "检查" } else { "格式化" },
            output.status.code().unwrap_or(-1),
            suffix
        ));
    }
    Ok(format!(
        "已使用 rustfmt {}{}",
        if check_only {
            "检查通过"
        } else {
            "格式化"
        },
        display_in_workspace(workspace_root, target)
    ))
}

fn run_prettier(target: &Path, workspace_root: &Path, check_only: bool) -> Result<String, String> {
    // 使用项目内的 prettier(若存在),否则依赖全局 npx
    let relative = target
        .strip_prefix(
            workspace_root
                .canonicalize()
                .map_err(|e| format!("工作区根目录无法解析: {}", e))?,
        )
        .unwrap_or(target);

    let mut cmd = Command::new("npx");
    cmd.arg("prettier");
    if check_only {
        cmd.arg("--check");
    } else {
        cmd.arg("--write");
    }
    cmd.arg(relative).current_dir(workspace_root);
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let output = cmd.output().map_err(|e| {
        let b = format!(
            "无法执行 prettier:{}(请确认已在工作区内安装 prettier 或可通过 npx 调用)",
            e
        );
        output_util::append_notfound_install_hint(b, &e, "npx")
    })?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let stdout = String::from_utf8_lossy(&output.stdout);
        let detail = if !stderr.trim().is_empty() {
            stderr.trim_end().to_string()
        } else if !stdout.trim().is_empty() {
            stdout.trim_end().to_string()
        } else {
            String::new()
        };
        let suffix = if detail.is_empty() {
            String::new()
        } else {
            format!("\n{}", detail)
        };
        return Err(format!(
            "prettier {}失败,退出码:{}{}",
            if check_only { "检查" } else { "格式化" },
            output.status.code().unwrap_or(-1),
            suffix
        ));
    }
    Ok(format!(
        "已使用 prettier {}{}",
        if check_only {
            "检查通过"
        } else {
            "格式化"
        },
        display_in_workspace(workspace_root, target)
    ))
}

fn run_clang_format(
    target: &Path,
    workspace_root: &Path,
    check_only: bool,
) -> Result<String, String> {
    let mut cmd = Command::new("clang-format");
    if check_only {
        cmd.args(["--dry-run", "--Werror"]);
    } else {
        cmd.arg("-i");
    }
    cmd.arg(target);
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    let output = cmd.output().map_err(|e| {
        let b = format!(
            "无法执行 clang-format:{}(请确认已安装 LLVM/Clang 的 clang-format,且检查模式需支持 --dry-run --Werror)",
            e
        );
        output_util::append_notfound_install_hint(b, &e, "clang-format")
    })?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let stdout = String::from_utf8_lossy(&output.stdout);
        let detail = if !stderr.trim().is_empty() {
            stderr.trim_end().to_string()
        } else if !stdout.trim().is_empty() {
            stdout.trim_end().to_string()
        } else {
            String::new()
        };
        let suffix = if detail.is_empty() {
            String::new()
        } else {
            format!("\n{}", detail)
        };
        return Err(format!(
            "clang-format {}失败,退出码:{}{}",
            if check_only { "检查" } else { "格式化" },
            output.status.code().unwrap_or(-1),
            suffix
        ));
    }
    Ok(format!(
        "已使用 clang-format {}{}",
        if check_only {
            "检查通过"
        } else {
            "格式化"
        },
        display_in_workspace(workspace_root, target)
    ))
}

fn run_gofmt(target: &Path, workspace_root: &Path, check_only: bool) -> Result<String, String> {
    if check_only {
        let output = Command::new("gofmt")
            .arg("-l")
            .arg(target)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
            .map_err(|e| {
                let b = format!("无法执行 gofmt:{}(请确认已安装 Go)", e);
                output_util::append_notfound_install_hint(b, &e, "gofmt")
            })?;
        let stdout = String::from_utf8_lossy(&output.stdout);
        if stdout.trim().is_empty() {
            Ok(format!(
                "gofmt 检查通过:{}",
                display_in_workspace(workspace_root, target)
            ))
        } else {
            Err(format!(
                "gofmt 检查失败(文件需格式化):{}",
                display_in_workspace(workspace_root, target)
            ))
        }
    } else {
        let output = Command::new("gofmt")
            .arg("-w")
            .arg(target)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
            .map_err(|e| {
                let b = format!("无法执行 gofmt:{}(请确认已安装 Go)", e);
                output_util::append_notfound_install_hint(b, &e, "gofmt")
            })?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(format!("gofmt 格式化失败:{}", stderr.trim_end()));
        }
        Ok(format!(
            "已使用 gofmt 格式化:{}",
            display_in_workspace(workspace_root, target)
        ))
    }
}

fn run_shfmt(target: &Path, workspace_root: &Path, check_only: bool) -> Result<String, String> {
    let output = Command::new("shfmt")
        .arg(if check_only { "-d" } else { "-w" })
        .arg(target)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .map_err(|e| {
            let b = format!(
                "无法执行 shfmt:{}(请安装 shfmt: https://github.com/mvdan/sh)",
                e
            );
            output_util::append_notfound_install_hint(b, &e, "shfmt")
        })?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let stdout = String::from_utf8_lossy(&output.stdout);
        let detail = if !stdout.trim().is_empty() {
            stdout.trim_end()
        } else {
            stderr.trim_end()
        };
        return Err(format!(
            "shfmt {}失败,退出码:{}\n{}",
            if check_only { "检查" } else { "格式化" },
            output.status.code().unwrap_or(-1),
            detail
        ));
    }
    Ok(format!(
        "已使用 shfmt {}{}",
        if check_only {
            "检查通过"
        } else {
            "格式化"
        },
        display_in_workspace(workspace_root, target)
    ))
}

fn run_xmllint(target: &Path, workspace_root: &Path, check_only: bool) -> Result<String, String> {
    if check_only {
        let output = Command::new("xmllint")
            .arg("--noout")
            .arg(target)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
            .map_err(|e| {
                let b = format!("无法执行 xmllint:{}(请安装 libxml2-utils)", e);
                output_util::append_notfound_install_hint(b, &e, "xmllint")
            })?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(format!("xmllint 检查失败:{}", stderr.trim_end()));
        }
        Ok(format!(
            "xmllint 检查通过:{}",
            display_in_workspace(workspace_root, target)
        ))
    } else {
        let output = Command::new("xmllint")
            .arg("--format")
            .arg(target)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
            .map_err(|e| {
                let b = format!("无法执行 xmllint:{}(请安装 libxml2-utils)", e);
                output_util::append_notfound_install_hint(b, &e, "xmllint")
            })?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(format!("xmllint 格式化失败:{}", stderr.trim_end()));
        }
        let formatted = String::from_utf8_lossy(&output.stdout);
        std::fs::write(target, formatted.as_bytes()).map_err(|e| format!("写回文件失败:{}", e))?;
        Ok(format!(
            "已使用 xmllint 格式化:{}",
            display_in_workspace(workspace_root, target)
        ))
    }
}

fn run_sql_format(
    target: &Path,
    workspace_root: &Path,
    check_only: bool,
) -> Result<String, String> {
    if let Ok(output) = Command::new("sqlfluff")
        .arg(if check_only { "lint" } else { "fix" })
        .arg("--dialect")
        .arg("ansi")
        .arg(target)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
    {
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        let detail = if !stdout.trim().is_empty() {
            stdout.trim_end().to_string()
        } else {
            stderr.trim_end().to_string()
        };
        return Ok(format!(
            "sqlfluff {}{}\n{}",
            if check_only {
                "检查完成"
            } else {
                "格式化完成"
            },
            display_in_workspace(workspace_root, target),
            detail
        ));
    }

    if let Ok(output) = Command::new("pg_format")
        .arg(if check_only {
            "--no-space"
        } else {
            "--inplace"
        })
        .arg(target)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        && output.status.success()
    {
        return Ok(format!(
            "已使用 pg_format {}{}",
            if check_only { "检查" } else { "格式化" },
            display_in_workspace(workspace_root, target)
        ));
    }

    Err("SQL 格式化需要安装 sqlfluff 或 pg_format".to_string())
}