@@ -1,70 +0,0 @@
1-const std = @import("std");
2-const config = @import("../config.zig");
3-const common = @import("common.zig");
4-const read_tool = @import("read.zig");
5-const edit_tool = @import("edit.zig");
6-const bash_tool = @import("bash.zig");
7-
8-const read_params =
9- \\{"type":"object","required":["path"],"properties":{"path":{"type":"string","description":"File path"},"offset":{"type":"integer","description":"First line to return, 1-based"},"range":{"type":"integer","description":"Number of lines to return from offset"}},"additionalProperties":false}
10-;
11-
12-const edit_params =
13- \\{"type":"object","required":["path","oldText","newText"],"properties":{"path":{"type":"string","description":"File path"},"oldText":{"type":"string","description":"Exact text to replace; empty to create a file"},"newText":{"type":"string","description":"Replacement text"}},"additionalProperties":false}
14-;
15-
16-const bash_params =
17- \\{"type":"object","required":["command"],"properties":{"command":{"type":"string","description":"Command to run"},"timeout":{"type":"integer","description":"Timeout in seconds; defaults to 120"}},"additionalProperties":false}
18-;
19-
20-const read_description = "Read a file. Returns its text. Returns the whole file unless offset and range give a line window.";
21-const read_image_description = "Read a file. Returns its text, or the image itself when the file is a png, jpg, or webp. Returns the whole file unless offset and range give a line window.";
22-const edit_description = "Edit a file by exact text replacement. oldText must occur exactly once. With empty oldText, create a new file (fails if it exists).";
23-const bash_description = "Run a bash command. Commands run in dir: {s}";
24-
25-pub fn execute(a: std.mem.Allocator, scratch: std.mem.Allocator, name: []const u8, args_json: []const u8, ctx: common.Context) common.Result {
26- const enabled = for (ctx.tools) |tool| {
27- if (std.mem.eql(u8, name, @tagName(tool))) break true;
28- } else false;
29- if (enabled) {
30- if (std.mem.eql(u8, name, "read")) return read_tool.run(a, scratch, args_json, ctx);
31- if (std.mem.eql(u8, name, "edit")) return edit_tool.run(a, scratch, args_json, ctx);
32- if (std.mem.eql(u8, name, "bash")) return bash_tool.run(a, scratch, args_json, ctx);
33- }
34- return .{
35- .text = std.fmt.allocPrint(a, "unknown tool: {s}", .{name}) catch "unknown tool",
36- .is_error = true,
37- };
38-}
39-
40-fn writeTool(w: *std.Io.Writer, name: config.ToolName, with_images: bool, bash_desc: []const u8) !void {
41- const description = switch (name) {
42- .read => if (with_images) read_image_description else read_description,
43- .edit => edit_description,
44- .bash => bash_desc,
45- };
46- try w.writeAll("{\"name\":");
47- try std.json.Stringify.encodeJsonString(@tagName(name), .{}, w);
48- try w.writeAll(",\"description\":");
49- try std.json.Stringify.encodeJsonString(description, .{}, w);
50- try w.writeAll(",\"parameters\":");
51- try w.writeAll(switch (name) {
52- .read => read_params,
53- .edit => edit_params,
54- .bash => bash_params,
55- });
56- try w.writeByte('}');
57-}
58-
59-pub fn json(a: std.mem.Allocator, names: []const config.ToolName, with_images: bool, cwd: []const u8) []const u8 {
60- const bash_desc = std.fmt.allocPrint(a, bash_description, .{cwd}) catch "Run a bash command in the current working directory.";
61- var out: std.Io.Writer.Allocating = .init(a);
62- const w = &out.writer;
63- w.writeByte('[') catch return "[]";
64- for (names, 0..) |name, i| {
65- if (i > 0) w.writeByte(',') catch return out.written();
66- writeTool(w, name, with_images, bash_desc) catch return out.written();
67- }
68- w.writeByte(']') catch return out.written();
69- return out.written();
70-}