79113
Ma
mini-coding-agent / files
mini-coding-agent / src / tools / edit.zig
3.9 KB
1const std = @import("std"); 2const filesystem = @import("../filesystem.zig"); 3const text = @import("../text.zig"); 4const diff = @import("../diff.zig"); 5const tools = @import("../tools.zig"); 6 7const Args = struct { 8 path: []const u8, 9 oldText: []const u8, 10 newText: []const u8, 11}; 12 13const description = 14 "Edit a file by replacing exact text.\n\n" ++ 15 "`oldText` must match the file byte-for-byte and occur exactly once; include surrounding lines to make " ++ 16 "it unique. `newText` replaces it. If `oldText` is missing or matches more than once, the edit fails " ++ 17 "without changing anything.\n\n" ++ 18 "To create a new file, pass an empty `oldText`; this fails if the file already exists. The result " ++ 19 "includes a unified diff of the change."; 20 21const params = [_]tools.Param{ 22 .{ .name = "path", .kind = .string, .description = "File to edit, or to create when `oldText` is empty." }, 23 .{ .name = "oldText", .kind = .string, .description = "Exact text to replace. Must occur exactly once. Leave empty to create a new file." }, 24 .{ .name = "newText", .kind = .string, .description = "Replacement text." }, 25}; 26 27fn describe(_: std.mem.Allocator, _: tools.Describe) []const u8 { 28 return description; 29} 30 31pub const tool = tools.Descriptor{ 32 .name = .edit, 33 .description = describe, 34 .params = ¶ms, 35 .run = run, 36}; 37 38fn fail(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) tools.Result { 39 return tools.fail(a, "edit failed", fmt, args); 40} 41 42pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const u8, ctx: tools.Context) tools.Result { 43 const args = std.json.parseFromSliceLeaky(Args, scratch, args_json, .{ .ignore_unknown_fields = true }) catch { 44 return fail(a, "edit failed: path, oldText and newText must be strings", .{}); 45 }; 46 const path = filesystem.expandTilde(scratch, args.path); 47 48 if (args.oldText.len == 0) { 49 if (filesystem.fileExists(path)) return fail(a, "edit failed: {s} already exists", .{path}); 50 if (ctx.cancel.load(.acquire)) return fail(a, "edit cancelled", .{}); 51 filesystem.writeFile(path, args.newText) catch |e| return tools.ioFail(a, "edit", path, e); 52 const cleaned = text.utf8Clean(scratch, args.newText) catch return fail(a, "edit failed: out of memory", .{}); 53 const patch = diff.unified(scratch, "", cleaned) catch return fail(a, "edit failed: out of memory", .{}); 54 return .{ 55 .text = std.fmt.allocPrint(a, "created {s}\n{s}", .{ path, patch }) catch "created", 56 .is_error = false, 57 }; 58 } 59 60 const before = filesystem.readFileAlloc(scratch, path, 1 << 30) catch |e| return tools.ioFail(a, "edit", path, e); 61 const at = std.mem.indexOf(u8, before, args.oldText) orelse 62 return fail(a, "edit failed: oldText not found in {s}", .{path}); 63 var count: usize = 1; 64 var search = at + args.oldText.len; 65 while (std.mem.indexOfPos(u8, before, search, args.oldText)) |next| { 66 count += 1; 67 search = next + args.oldText.len; 68 } 69 if (count > 1) return fail(a, "edit failed: oldText matches {d} times in {s}", .{ count, path }); 70 71 const after = std.mem.concat(scratch, u8, &.{ before[0..at], args.newText, before[at + args.oldText.len ..] }) catch 72 return fail(a, "edit failed: out of memory", .{}); 73 if (ctx.cancel.load(.acquire)) return fail(a, "edit cancelled", .{}); 74 filesystem.writeFile(path, after) catch |e| return tools.ioFail(a, "edit", path, e); 75 const clean_before = text.utf8Clean(scratch, before) catch return fail(a, "edit failed: out of memory", .{}); 76 const clean_after = text.utf8Clean(scratch, after) catch return fail(a, "edit failed: out of memory", .{}); 77 const patch = diff.unified(scratch, clean_before, clean_after) catch return fail(a, "edit failed: out of memory", .{}); 78 return .{ 79 .text = std.fmt.allocPrint(a, "edited {s}\n{s}", .{ path, patch }) catch "edited", 80 .is_error = false, 81 }; 82}