79113
Ma
mini-coding-agent / files
mini-coding-agent / src / tools.zig
5.2 KB
1const std = @import("std"); 2const config = @import("config.zig"); 3const message_mod = @import("message.zig"); 4const read_tool = @import("tools/read.zig"); 5const edit_tool = @import("tools/edit.zig"); 6const bash_tool = @import("tools/bash.zig"); 7 8pub const OutputFn = struct { 9 ctx: *anyopaque, 10 on_chunk: *const fn (ctx: *anyopaque, chunk: []const u8) void, 11 12 pub fn call(self: OutputFn, chunk: []const u8) void { 13 self.on_chunk(self.ctx, chunk); 14 } 15}; 16 17pub const ToolPhase = enum { snapshotting, running }; 18 19pub const PhaseFn = struct { 20 ctx: *anyopaque, 21 on_phase: *const fn (ctx: *anyopaque, phase: ToolPhase) void, 22 23 pub fn call(self: PhaseFn, phase: ToolPhase) void { 24 self.on_phase(self.ctx, phase); 25 } 26}; 27 28pub const Context = struct { 29 cancel: *const std.atomic.Value(bool), 30 supports_images: bool, 31 tools: []const config.ToolName = &.{}, 32 on_output: ?OutputFn = null, 33 on_phase: ?PhaseFn = null, 34 snapshot_ignore_dirs: []const []const u8 = &.{}, 35 snapshot_uses_gitignore: bool = true, 36 snapshot_diffs_in_context: bool = false, 37}; 38 39pub const FileDiff = struct { 40 path: []const u8, 41 patch: ?[]const u8 = null, 42 note: ?[]const u8 = null, 43}; 44 45pub const Result = struct { 46 text: []const u8, 47 is_error: bool, 48 images: []const message_mod.ImageContent = &.{}, 49 diffs: []const FileDiff = &.{}, 50 body: ?[]const u8 = null, 51 model_text: ?[]const u8 = null, 52}; 53 54pub fn fail(a: std.mem.Allocator, fallback: []const u8, comptime fmt: []const u8, args: anytype) Result { 55 return .{ 56 .text = std.fmt.allocPrint(a, fmt, args) catch fallback, 57 .is_error = true, 58 }; 59} 60 61pub fn ioFail(a: std.mem.Allocator, what: []const u8, path: []const u8, e: anyerror) Result { 62 const message = switch (e) { 63 error.FileNotFound => std.fmt.allocPrint(a, "{s} failed: {s} does not exist", .{ what, path }), 64 error.IsDir => std.fmt.allocPrint(a, "{s} failed: {s} is a directory", .{ what, path }), 65 error.AccessDenied, error.PermissionDenied => std.fmt.allocPrint(a, "{s} failed: {s}: permission denied", .{ what, path }), 66 else => std.fmt.allocPrint(a, "{s} failed: {s}: {s}", .{ what, path, @errorName(e) }), 67 } catch return .{ .text = what, .is_error = true }; 68 return .{ .text = message, .is_error = true }; 69} 70 71pub const Kind = enum { string, integer }; 72 73pub const Param = struct { 74 name: []const u8, 75 kind: Kind, 76 description: []const u8, 77 required: bool = true, 78}; 79 80pub const Describe = struct { 81 with_images: bool = false, 82 cwd: []const u8 = "", 83}; 84 85pub const Descriptor = struct { 86 name: config.ToolName, 87 description: *const fn (std.mem.Allocator, Describe) []const u8, 88 params: []const Param, 89 run: *const fn (std.mem.Allocator, std.mem.Allocator, []const u8, Context) Result, 90}; 91 92pub fn write(w: *std.Io.Writer, a: std.mem.Allocator, d: Descriptor, ctx: Describe) std.Io.Writer.Error!void { 93 try w.writeAll("{\"name\":"); 94 try std.json.Stringify.encodeJsonString(@tagName(d.name), .{}, w); 95 try w.writeAll(",\"description\":"); 96 try std.json.Stringify.encodeJsonString(d.description(a, ctx), .{}, w); 97 try w.writeAll(",\"parameters\":{\"type\":\"object\",\"required\":["); 98 var first = true; 99 for (d.params) |p| { 100 if (!p.required) continue; 101 if (!first) try w.writeByte(','); 102 first = false; 103 try std.json.Stringify.encodeJsonString(p.name, .{}, w); 104 } 105 try w.writeAll("],\"properties\":{"); 106 for (d.params, 0..) |p, i| { 107 if (i > 0) try w.writeByte(','); 108 try std.json.Stringify.encodeJsonString(p.name, .{}, w); 109 try w.writeAll(":{\"type\":"); 110 try std.json.Stringify.encodeJsonString(@tagName(p.kind), .{}, w); 111 try w.writeAll(",\"description\":"); 112 try std.json.Stringify.encodeJsonString(p.description, .{}, w); 113 try w.writeByte('}'); 114 } 115 try w.writeAll("},\"additionalProperties\":false}}"); 116} 117 118const all = [_]Descriptor{ read_tool.tool, edit_tool.tool, bash_tool.tool }; 119 120fn byName(name: config.ToolName) ?Descriptor { 121 for (all) |tool| if (tool.name == name) return tool; 122 return null; 123} 124 125pub fn execute(a: std.mem.Allocator, scratch: std.mem.Allocator, name: []const u8, args_json: []const u8, ctx: Context) Result { 126 for (all) |tool| { 127 if (!std.mem.eql(u8, name, @tagName(tool.name))) continue; 128 for (ctx.tools) |enabled| if (enabled == tool.name) return tool.run(a, scratch, args_json, ctx); 129 break; 130 } 131 return .{ 132 .text = std.fmt.allocPrint(a, "unknown tool: {s}", .{name}) catch "unknown tool", 133 .is_error = true, 134 }; 135} 136 137pub fn json(a: std.mem.Allocator, names: []const config.ToolName, with_images: bool, cwd: []const u8) []const u8 { 138 const ctx = Describe{ .with_images = with_images, .cwd = cwd }; 139 var out: std.Io.Writer.Allocating = .init(a); 140 writeJson(&out.writer, a, names, ctx) catch return "[]"; 141 return out.written(); 142} 143 144fn writeJson(w: *std.Io.Writer, a: std.mem.Allocator, names: []const config.ToolName, ctx: Describe) !void { 145 try w.writeByte('['); 146 var first = true; 147 for (names) |name| { 148 const tool = byName(name) orelse continue; 149 if (!first) try w.writeByte(','); 150 first = false; 151 try write(w, a, tool, ctx); 152 } 153 try w.writeByte(']'); 154}