@@ -1,19 +1,116 @@
11 const std = @import("std");
22 const config = @import("config.zig");
3-const common = @import("tools/common.zig");
4-const spec = @import("tools/spec.zig");
3+const types = @import("types.zig");
54 const read_tool = @import("tools/read.zig");
65 const edit_tool = @import("tools/edit.zig");
76 const bash_tool = @import("tools/bash.zig");
87
9-const all = [_]spec.Descriptor{ read_tool.tool, edit_tool.tool, bash_tool.tool };
8+pub const OutputFn = struct {
9+ ctx: *anyopaque,
10+ on_chunk: *const fn (ctx: *anyopaque, chunk: []const u8) void,
1011
11-fn byName(name: config.ToolName) ?spec.Descriptor {
12+ pub fn call(self: OutputFn, chunk: []const u8) void {
13+ self.on_chunk(self.ctx, chunk);
14+ }
15+};
16+
17+pub const ToolPhase = enum { snapshotting, running };
18+
19+pub 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+
28+pub 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+};
37+
38+pub const FileDiff = struct {
39+ path: []const u8,
40+ patch: ?[]const u8 = null,
41+ note: ?[]const u8 = null,
42+};
43+
44+pub const Result = struct {
45+ text: []const u8,
46+ is_error: bool,
47+ images: []const types.ImageContent = &.{},
48+ diffs: []const FileDiff = &.{},
49+ body: ?[]const u8 = null,
50+};
51+
52+pub fn fail(a: std.mem.Allocator, fallback: []const u8, comptime fmt: []const u8, args: anytype) Result {
53+ return .{
54+ .text = std.fmt.allocPrint(a, fmt, args) catch fallback,
55+ .is_error = true,
56+ };
57+}
58+
59+pub const Kind = enum { string, integer };
60+
61+pub const Param = struct {
62+ name: []const u8,
63+ kind: Kind,
64+ description: []const u8,
65+ required: bool = true,
66+};
67+
68+pub const Describe = struct {
69+ with_images: bool = false,
70+ cwd: []const u8 = "",
71+};
72+
73+pub const Descriptor = struct {
74+ name: config.ToolName,
75+ description: *const fn (std.mem.Allocator, Describe) []const u8,
76+ params: []const Param,
77+ run: *const fn (std.mem.Allocator, std.mem.Allocator, []const u8, Context) Result,
78+};
79+
80+pub fn write(w: *std.Io.Writer, a: std.mem.Allocator, d: Descriptor, ctx: Describe) std.Io.Writer.Error!void {
81+ try w.writeAll("{\"name\":");
82+ try std.json.Stringify.encodeJsonString(@tagName(d.name), .{}, w);
83+ try w.writeAll(",\"description\":");
84+ try std.json.Stringify.encodeJsonString(d.description(a, ctx), .{}, w);
85+ try w.writeAll(",\"parameters\":{\"type\":\"object\",\"required\":[");
86+ var first = true;
87+ for (d.params) |p| {
88+ if (!p.required) continue;
89+ if (!first) try w.writeByte(',');
90+ first = false;
91+ try std.json.Stringify.encodeJsonString(p.name, .{}, w);
92+ }
93+ try w.writeAll("],\"properties\":{");
94+ for (d.params, 0..) |p, i| {
95+ if (i > 0) try w.writeByte(',');
96+ try std.json.Stringify.encodeJsonString(p.name, .{}, w);
97+ try w.writeAll(":{\"type\":");
98+ try std.json.Stringify.encodeJsonString(@tagName(p.kind), .{}, w);
99+ try w.writeAll(",\"description\":");
100+ try std.json.Stringify.encodeJsonString(p.description, .{}, w);
101+ try w.writeByte('}');
102+ }
103+ try w.writeAll("},\"additionalProperties\":false}}");
104+}
105+
106+const all = [_]Descriptor{ read_tool.tool, edit_tool.tool, bash_tool.tool };
107+
108+fn byName(name: config.ToolName) ?Descriptor {
12109 for (all) |tool| if (tool.name == name) return tool;
13110 return null;
14111 }
15112
16-pub fn execute(a: std.mem.Allocator, scratch: std.mem.Allocator, name: []const u8, args_json: []const u8, ctx: common.Context) common.Result {
113+pub fn execute(a: std.mem.Allocator, scratch: std.mem.Allocator, name: []const u8, args_json: []const u8, ctx: Context) Result {
17114 for (all) |tool| {
18115 if (!std.mem.eql(u8, name, @tagName(tool.name))) continue;
19116 for (ctx.tools) |enabled| if (enabled == tool.name) return tool.run(a, scratch, args_json, ctx);
@@ -26,7 +123,7 @@ pub fn execute(a: std.mem.Allocator, scratch: std.mem.Allocator, name: []const u
26123 }
27124
28125 pub fn json(a: std.mem.Allocator, names: []const config.ToolName, with_images: bool, cwd: []const u8) []const u8 {
29- const ctx = spec.Describe{ .with_images = with_images, .cwd = cwd };
126+ const ctx = Describe{ .with_images = with_images, .cwd = cwd };
30127 var out: std.Io.Writer.Allocating = .init(a);
31128 const w = &out.writer;
32129 w.writeByte('[') catch return "[]";
@@ -35,7 +132,7 @@ pub fn json(a: std.mem.Allocator, names: []const config.ToolName, with_images: b
35132 const tool = byName(name) orelse continue;
36133 if (!first) w.writeByte(',') catch return out.written();
37134 first = false;
38- spec.write(w, a, tool, ctx) catch return out.written();
135+ write(w, a, tool, ctx) catch return out.written();
39136 }
40137 w.writeByte(']') catch return out.written();
41138 return out.written();