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
//! 由 `file.rs` 拆分;与拆分前行为一致。
#![allow(clippy::manual_string_new)]

use regex::RegexBuilder;
use std::io::Write;
use std::path::{Path, PathBuf};

use super::path::{
    canonical_workspace_root, path_for_tool_display, resolve_for_read, resolve_for_write,
    tool_user_error_from_workspace_path,
};
use crate::cm_tools::tools::ToolContext;
use crate::cm_tools::tools::tool_param_types::{
    AppendFileArgs, CreateDirArgs, DeleteDirArgs, DeleteFileArgs, SearchReplaceArgs,
};
use crate::cm_tools::tools::write_sse_preview::{
    WORKSPACE_WRITE_DIFF_BUDGET_CHARS, WriteDiffFileState,
    format_tool_output_with_write_diff_preview,
};
use crate::cm_tools::workspace::changelist::record_file_state_after_write;
use crate::cm_tools::workspace::fs::{
    create_directory_under_root, open_file_append_under_root, unlink_file_under_root,
    write_bytes_under_root,
};

fn parse_delete_dir_args(args_json: &str) -> Result<DeleteDirArgs, String> {
    let v = crate::cm_tools::tools::parse_args_json(args_json)?;
    serde_json::from_value(v).map_err(|e| format!("参数解析错误: {e}"))
}

fn parse_append_file_args(args_json: &str) -> Result<AppendFileArgs, String> {
    let v = crate::cm_tools::tools::parse_args_json(args_json)?;
    serde_json::from_value(v).map_err(|e| format!("参数解析错误: {e}"))
}

fn delete_dir_validate_target(working_dir: &Path, path: &str) -> Result<PathBuf, String> {
    let target =
        resolve_for_read(working_dir, path).map_err(tool_user_error_from_workspace_path)?;
    if !target.is_dir() {
        return Err(format!(
            "错误:{} 不是目录",
            path_for_tool_display(working_dir, &target, Some(path))
        ));
    }
    let base_canonical =
        canonical_workspace_root(working_dir).map_err(tool_user_error_from_workspace_path)?;
    if target == base_canonical {
        return Err("错误:不能删除工作区根目录".to_string());
    }
    Ok(target)
}

fn delete_dir_result_message(
    recursive: bool,
    working_dir: &Path,
    target: &Path,
    path: &str,
    result: std::io::Result<()>,
) -> String {
    match result {
        Ok(()) => format!(
            "已删除目录{}{}",
            if recursive { "(递归)" } else { "" },
            path_for_tool_display(working_dir, target, Some(path))
        ),
        Err(e) => {
            if !recursive && e.kind() == std::io::ErrorKind::DirectoryNotEmpty {
                "删除失败:目录非空,需要 recursive=true 才能删除非空目录".to_string()
            } else {
                format!("删除目录失败:{}", e)
            }
        }
    }
}

fn append_resolve_target_path(
    working_dir: &Path,
    path: &str,
    create_if_missing: bool,
) -> Result<PathBuf, String> {
    if create_if_missing {
        resolve_for_write(working_dir, path).map_err(tool_user_error_from_workspace_path)
    } else {
        match resolve_for_read(working_dir, path) {
            Ok(p) => Ok(p),
            Err(e) => Err(format!(
                "文件不存在(可设置 create_if_missing=true):{}",
                e
            )),
        }
    }
}

fn append_create_parent_if_needed(
    base: &Path,
    create_if_missing: bool,
    target: &Path,
) -> Result<(), String> {
    if !create_if_missing {
        return Ok(());
    }
    if let Some(parent) = target.parent()
        && !parent.as_os_str().is_empty()
        && !parent.exists()
    {
        create_directory_under_root(base, parent, true)
            .map_err(|e| format!("创建父目录失败:{}", e))?;
    }
    Ok(())
}

fn append_file_needs_leading_newline(
    before: Option<&str>,
    content: &str,
    ensure_leading_newline: bool,
) -> bool {
    ensure_leading_newline
        && !content.is_empty()
        && !content.starts_with('\n')
        && before.is_some_and(|s| !s.is_empty() && !s.ends_with('\n'))
}

struct AppendFilePlan {
    path: String,
    target: PathBuf,
    base: PathBuf,
    before: Option<String>,
    append_body: String,
    inserted_leading_newline: bool,
    create_if_missing: bool,
}

fn append_file_display(working_dir: &Path, plan: &AppendFilePlan) -> String {
    path_for_tool_display(working_dir, &plan.target, Some(&plan.path))
}

fn append_file_build_body(prefix: &str, working_dir: &Path, plan: &AppendFilePlan) -> String {
    let mut body = format!(
        "{} {} 字节到 {}",
        prefix,
        plan.append_body.len(),
        append_file_display(working_dir, plan)
    );
    if plan.inserted_leading_newline {
        body.push_str("\n已为原文件末尾补 1 个前导换行,避免新内容接在旧末行后。");
    }
    body
}

fn append_file_after_preview(plan: &AppendFilePlan) -> Option<String> {
    plan.before
        .as_ref()
        .map(|b| {
            let mut s = b.clone();
            s.push_str(&plan.append_body);
            s
        })
        .or_else(|| plan.create_if_missing.then(|| plan.append_body.clone()))
}

fn append_file_prepare(
    args: AppendFileArgs,
    working_dir: &Path,
) -> Result<(bool, AppendFilePlan), String> {
    let path = match args.path.trim() {
        s if !s.is_empty() => s.to_string(),
        _ => return Err("缺少 path 参数".to_string()),
    };
    let target = append_resolve_target_path(working_dir, &path, args.create_if_missing)?;
    let base =
        canonical_workspace_root(working_dir).map_err(tool_user_error_from_workspace_path)?;
    append_create_parent_if_needed(&base, args.create_if_missing, &target)?;
    let before = std::fs::read_to_string(&target).ok();
    let inserted_leading_newline = append_file_needs_leading_newline(
        before.as_deref(),
        &args.content,
        args.ensure_leading_newline,
    );
    let mut append_body = String::new();
    if inserted_leading_newline {
        append_body.push('\n');
    }
    append_body.push_str(&args.content);
    Ok((
        args.dry_run,
        AppendFilePlan {
            path,
            target,
            base,
            before,
            append_body,
            inserted_leading_newline,
            create_if_missing: args.create_if_missing,
        },
    ))
}

fn append_file_preview(working_dir: &Path, plan: AppendFilePlan) -> String {
    let body =
        append_file_build_body("预览(dry_run=true):将追加", working_dir, &plan) + ",未写盘";
    let after = append_file_after_preview(&plan);
    format_tool_output_with_write_diff_preview(
        "append_file",
        body,
        vec![WriteDiffFileState {
            rel_path: plan.path,
            before: plan.before,
            after,
        }],
        WORKSPACE_WRITE_DIFF_BUDGET_CHARS,
    )
}

fn append_file_write(working_dir: &Path, ctx: &ToolContext<'_>, plan: AppendFilePlan) -> String {
    let mut file =
        match open_file_append_under_root(&plan.base, &plan.target, plan.create_if_missing) {
            Ok(f) => f,
            Err(e) => return format!("打开文件失败:{}", e),
        };
    if let Err(e) = file.write_all(plan.append_body.as_bytes()) {
        return format!("写入失败:{}", e);
    }
    let after = std::fs::read_to_string(&plan.target).ok();
    if let Some(c) = ctx.workspace_changelist {
        c.record_mutation(&plan.path, plan.before.clone(), after.clone());
    }
    let body = append_file_build_body("已追加", working_dir, &plan);
    format_tool_output_with_write_diff_preview(
        "append_file",
        body,
        vec![WriteDiffFileState {
            rel_path: plan.path,
            before: plan.before,
            after,
        }],
        WORKSPACE_WRITE_DIFF_BUDGET_CHARS,
    )
}

fn parse_confirmed_delete_path(args_json: &str) -> Result<String, String> {
    let v = crate::cm_tools::tools::parse_args_json(args_json)?;
    let args: DeleteFileArgs =
        serde_json::from_value(v).map_err(|e| format!("参数解析错误: {e}"))?;
    let path = match args.path.trim() {
        s if !s.is_empty() => s.to_string(),
        _ => return Err("缺少 path 参数".to_string()),
    };
    if !args.confirm.unwrap_or(false) {
        return Err("拒绝执行:delete_file 需要 confirm=true".to_string());
    }
    Ok(path)
}

fn delete_file_remove(working_dir: &Path, target: &Path) -> Result<(), String> {
    let base =
        canonical_workspace_root(working_dir).map_err(tool_user_error_from_workspace_path)?;
    #[cfg(unix)]
    {
        unlink_file_under_root(&base, target).map_err(|e| format!("删除文件失败:{}", e))
    }
    #[cfg(not(unix))]
    {
        let _ = base;
        std::fs::remove_file(target).map_err(|e| format!("删除文件失败:{}", e))
    }
}

pub fn delete_file(args_json: &str, working_dir: &Path, ctx: &ToolContext<'_>) -> String {
    let path = match parse_confirmed_delete_path(args_json) {
        Ok(p) => p,
        Err(e) => return e,
    };

    let target = match resolve_for_read(working_dir, &path) {
        Ok(p) => p,
        Err(e) => return tool_user_error_from_workspace_path(e),
    };
    if !target.is_file() {
        return format!(
            "错误:{} 不是文件(可能是目录,请用 delete_dir)",
            path_for_tool_display(working_dir, &target, Some(&path))
        );
    }
    let before = std::fs::read_to_string(&target).ok();
    match delete_file_remove(working_dir, &target) {
        Ok(()) => {
            let body = format!(
                "已删除文件:{}",
                path_for_tool_display(working_dir, &target, Some(&path))
            );
            let preview_before = before.clone();
            if let Some(c) = ctx.workspace_changelist {
                c.record_mutation(&path, before, None);
            }
            format_tool_output_with_write_diff_preview(
                "delete_file",
                body,
                vec![WriteDiffFileState {
                    rel_path: path.clone(),
                    before: preview_before,
                    after: None,
                }],
                WORKSPACE_WRITE_DIFF_BUDGET_CHARS,
            )
        }
        Err(e) => e,
    }
}

// ── delete_dir ──────────────────────────────────────────────

pub fn delete_dir(args_json: &str, working_dir: &Path) -> String {
    let args = match parse_delete_dir_args(args_json) {
        Ok(a) => a,
        Err(e) => return e,
    };
    let path = match args.path.trim() {
        s if !s.is_empty() => s.to_string(),
        _ => return "缺少 path 参数".to_string(),
    };
    let confirm = args.confirm.unwrap_or(false);
    if !confirm {
        return "拒绝执行:delete_dir 需要 confirm=true".to_string();
    }
    let recursive = args.recursive;

    let target = match delete_dir_validate_target(working_dir, &path) {
        Ok(p) => p,
        Err(e) => return e,
    };

    let result = if recursive {
        std::fs::remove_dir_all(&target)
    } else {
        std::fs::remove_dir(&target)
    };
    delete_dir_result_message(recursive, working_dir, &target, &path, result)
}

// ── append_file ─────────────────────────────────────────────

pub fn append_file(args_json: &str, working_dir: &Path, ctx: &ToolContext<'_>) -> String {
    let args = match parse_append_file_args(args_json) {
        Ok(a) => a,
        Err(e) => return e,
    };
    let (dry_run, plan) = match append_file_prepare(args, working_dir) {
        Ok(p) => p,
        Err(e) => return e,
    };
    if dry_run {
        return append_file_preview(working_dir, plan);
    }
    append_file_write(working_dir, ctx, plan)
}

// ── create_dir ──────────────────────────────────────────────

pub fn create_dir(args_json: &str, working_dir: &Path) -> String {
    let v = match crate::cm_tools::tools::parse_args_json(args_json) {
        Ok(v) => v,
        Err(e) => return e,
    };
    let args: CreateDirArgs = match serde_json::from_value(v) {
        Ok(a) => a,
        Err(e) => return format!("参数解析错误: {e}"),
    };
    let path = match args.path.trim() {
        s if !s.is_empty() => s.to_string(),
        _ => return "缺少 path 参数".to_string(),
    };
    let parents = args.parents;

    let target = match resolve_for_write(working_dir, &path) {
        Ok(p) => p,
        Err(e) => return tool_user_error_from_workspace_path(e),
    };
    if target.exists() {
        if target.is_dir() {
            return format!(
                "目录已存在:{}",
                path_for_tool_display(working_dir, &target, Some(&path))
            );
        }
        return format!(
            "错误:路径已存在且为文件:{}",
            path_for_tool_display(working_dir, &target, Some(&path))
        );
    }
    let base = match canonical_workspace_root(working_dir) {
        Ok(p) => p,
        Err(e) => return tool_user_error_from_workspace_path(e),
    };
    let result = create_directory_under_root(&base, &target, parents);
    match result {
        Ok(()) => format!(
            "已创建目录:{}",
            path_for_tool_display(working_dir, &target, Some(&path))
        ),
        Err(e) => format!("创建目录失败:{}", e),
    }
}

// ── search_replace ──────────────────────────────────────────

fn apply_search_replace_inner(
    content: &str,
    search: &str,
    replace: &str,
    is_regex: bool,
    max_replacements: usize,
) -> Result<(String, usize), String> {
    if is_regex {
        let re = RegexBuilder::new(search)
            .build()
            .map_err(|e| format!("正则表达式无效:{}", e))?;
        let mut count = 0usize;
        let new = if max_replacements == 0 {
            let result = re.replace_all(content, replace);
            count = re.find_iter(content).count();
            result.to_string()
        } else {
            let mut result = content.to_string();
            for _ in 0..max_replacements {
                if let Some(m) = re.find(&result) {
                    let before = &result[..m.start()];
                    let after = &result[m.end()..];
                    result = format!("{}{}{}", before, replace, after);
                    count += 1;
                } else {
                    break;
                }
            }
            result
        };
        Ok((new, count))
    } else {
        let mut count = 0usize;
        let new = if max_replacements == 0 {
            count = content.matches(search).count();
            content.replace(search, replace)
        } else {
            let mut result = content.to_string();
            for _ in 0..max_replacements {
                if let Some(pos) = result.find(search) {
                    result = format!(
                        "{}{}{}",
                        &result[..pos],
                        replace,
                        &result[pos + search.len()..]
                    );
                    count += 1;
                } else {
                    break;
                }
            }
            result
        };
        Ok((new, count))
    }
}

fn search_replace_dry_run_preview(
    display: &str,
    count: usize,
    content: &str,
    new_content: &str,
) -> String {
    let mut preview = format!("预览(dry-run):在 {} 中找到 {} 处匹配\n", display, count);
    let lines: Vec<&str> = new_content.lines().collect();
    let orig_lines: Vec<&str> = content.lines().collect();
    let mut shown = 0usize;
    for (i, (old, new)) in orig_lines.iter().zip(lines.iter()).enumerate() {
        if old != new && shown < 20 {
            preview.push_str(&format!(
                "  L{}: \"{}\"\"{}\"\n",
                i + 1,
                old.trim(),
                new.trim()
            ));
            shown += 1;
        }
    }
    if shown >= 20 {
        preview.push_str("  ... (更多变更已省略)\n");
    }
    preview.push_str("\n设置 dry_run=false, confirm=true 以实际写入");
    preview
}

fn parse_search_replace_args(args_json: &str) -> Result<SearchReplaceArgs, String> {
    let v = crate::cm_tools::tools::parse_args_json(args_json)?;
    serde_json::from_value(v).map_err(|e| format!("参数解析错误: {e}"))
}

fn search_replace_path_and_query(args: &SearchReplaceArgs) -> Result<(String, String), String> {
    let path = match args.path.trim() {
        s if !s.is_empty() => s.to_string(),
        _ => return Err("缺少 path 参数".to_string()),
    };
    let search = match args.search.trim() {
        s if !s.is_empty() => s.to_string(),
        _ => return Err("缺少 search 参数".to_string()),
    };
    Ok((path, search))
}

fn load_search_replace_file_bytes(
    working_dir: &Path,
    path: &str,
) -> Result<(PathBuf, String), String> {
    let target =
        resolve_for_read(working_dir, path).map_err(tool_user_error_from_workspace_path)?;
    if !target.is_file() {
        return Err(format!("错误:{} 不是文件", path));
    }
    let content = std::fs::read_to_string(&target).map_err(|e| format!("读取文件失败:{}", e))?;
    const MAX_FILE_SIZE: usize = 4 * 1024 * 1024;
    if content.len() > MAX_FILE_SIZE {
        return Err(format!(
            "错误:文件过大({} 字节,上限 4MiB)",
            content.len()
        ));
    }
    Ok((target, content))
}

pub fn search_replace(args_json: &str, working_dir: &Path, ctx: &ToolContext<'_>) -> String {
    let args = match parse_search_replace_args(args_json) {
        Ok(a) => a,
        Err(e) => return e,
    };
    let (path, search) = match search_replace_path_and_query(&args) {
        Ok(p) => p,
        Err(e) => return e,
    };
    let replace = args.replace;
    let is_regex = args.regex;
    let dry_run = args.dry_run;
    let confirm = args.confirm;
    let max_replacements = args.max_replacements.unwrap_or(0) as usize;

    let (target, content) = match load_search_replace_file_bytes(working_dir, &path) {
        Ok(x) => x,
        Err(e) => return e,
    };

    let (new_content, count) =
        match apply_search_replace_inner(&content, &search, &replace, is_regex, max_replacements) {
            Ok(x) => x,
            Err(e) => return e,
        };

    if count == 0 {
        return format!("未找到匹配:\"{}\"{}", search, path);
    }

    let display = path_for_tool_display(working_dir, &target, Some(&path));
    if dry_run {
        return search_replace_dry_run_preview(&display, count, &content, &new_content);
    }

    if !confirm {
        return "拒绝执行:search_replace 写盘需要 confirm=true".to_string();
    }

    let before = content.clone();
    let base = match canonical_workspace_root(working_dir) {
        Ok(p) => p,
        Err(e) => return tool_user_error_from_workspace_path(e),
    };
    match write_bytes_under_root(&base, &target, new_content.as_bytes(), false, false) {
        Ok(()) => {
            record_file_state_after_write(
                ctx.workspace_changelist,
                working_dir,
                &path,
                Some(before.clone()),
            );
            let body = format!(
                "已替换 {} 处匹配(\"{}\"\"{}\"):{}",
                count, search, replace, display
            );
            format_tool_output_with_write_diff_preview(
                "search_replace",
                body,
                vec![WriteDiffFileState {
                    rel_path: path,
                    before: Some(before),
                    after: Some(new_content),
                }],
                WORKSPACE_WRITE_DIFF_BUDGET_CHARS,
            )
        }
        Err(e) => format!("写入文件失败:{}", e),
    }
}