xonecas.io
79113
Ma

mini-coding-agent / files

mini-coding-agent / src / tools / read.zig

7.5 KB
1const std = @import("std");
2const filesystem = @import("../filesystem.zig");
3const text = @import("../text.zig");
4const message_mod = @import("../message.zig");
5const tools = @import("../tools.zig");
6
7const Args = struct {
8    path: []const u8,
9    offset: ?usize = null,
10    range: ?usize = null,
11};
12
13const description_base =
14    "Read a file and return its text.\n\n" ++
15    "Relative paths resolve against the working directory. Prefer this over cat, head, or sed. The whole " ++
16    "file is returned by default; pass `offset` (the 1-based first line) and/or `range` (the number of " ++
17    "lines) to read part of it.";
18
19const text_description = description_base ++
20    "\n\nText longer than 100,000 characters is truncated, and binary files are rejected; use bash, if available, to read those.";
21
22const image_description = description_base ++
23    "\n\nImages (png, jpg, jpeg, webp) are returned as image content. Text longer than 100,000 characters is " ++
24    "truncated.";
25
26const params = [_]tools.Param{
27    .{ .name = "path", .kind = .string, .description = "File to read. Relative paths resolve against the working directory." },
28    .{ .name = "offset", .kind = .integer, .required = false, .description = "First line to return, 1-based. Defaults to the beginning of the file." },
29    .{ .name = "range", .kind = .integer, .required = false, .description = "Number of lines to return from `offset`. Defaults to the end of the file." },
30};
31
32fn describe(_: std.mem.Allocator, ctx: tools.Describe) []const u8 {
33    return if (ctx.with_images) image_description else text_description;
34}
35
36pub const tool = tools.Descriptor{
37    .name = .read,
38    .description = describe,
39    .params = &params,
40    .run = run,
41};
42
43const image_mime = [_]struct { ext: []const u8, mime: []const u8 }{
44    .{ .ext = ".png", .mime = "image/png" },
45    .{ .ext = ".jpg", .mime = "image/jpeg" },
46    .{ .ext = ".jpeg", .mime = "image/jpeg" },
47    .{ .ext = ".webp", .mime = "image/webp" },
48};
49
50const max_text_chars = 100_000;
51const max_image_base64_bytes = 5 * 1024 * 1024;
52const max_file_bytes = 1 << 30;
53const clipped_file_bytes = max_text_chars * 4;
54
55fn fail(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) tools.Result {
56    return tools.fail(a, "read failed", fmt, args);
57}
58
59fn warn(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) tools.Result {
60    return .{ .text = std.fmt.allocPrint(a, fmt, args) catch "read", .is_error = false };
61}
62
63fn mimeFor(path: []const u8) ?[]const u8 {
64    const ext = std.fs.path.extension(path);
65    for (image_mime) |entry| {
66        if (std.ascii.eqlIgnoreCase(ext, entry.ext)) return entry.mime;
67    }
68    return null;
69}
70
71fn advance(content: []const u8, pos: usize, n: usize) ?usize {
72    var p = pos;
73    var left = n;
74    while (left > 0) : (left -= 1) {
75        const nl = std.mem.indexOfScalarPos(u8, content, p, '\n') orelse return null;
76        p = nl + 1;
77    }
78    return p;
79}
80
81fn lineSlice(content: []const u8, offset: usize, range: ?usize) ?[]const u8 {
82    const start = advance(content, 0, offset - 1) orelse return null;
83    if (offset > 1 and start >= content.len) return null;
84    if (range) |r| if (advance(content, start, r)) |end| return content[start..end];
85    return content[start..];
86}
87
88fn bodyLine(a: std.mem.Allocator, args: Args, lines: usize, truncated: bool) ?[]const u8 {
89    var out: std.Io.Writer.Allocating = .init(a);
90    const w = &out.writer;
91    var window = false;
92    if (args.offset) |o| {
93        w.print("offset {d}", .{o}) catch return null;
94        window = true;
95    }
96    if (args.range) |r| {
97        if (window) w.writeAll(", ") catch return null;
98        w.print("range {d}", .{r}) catch return null;
99        window = true;
100    }
101    if (window) w.writeAll(" · ") catch return null;
102    w.writeAll(text.plural(a, lines, "line", "lines")) catch return null;
103    if (truncated) w.writeAll(", truncated") catch return null;
104    return out.written();
105}
106
107pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const u8, ctx: tools.Context) tools.Result {
108    const args = std.json.parseFromSliceLeaky(Args, scratch, args_json, .{ .ignore_unknown_fields = true }) catch
109        return fail(a, "read failed: invalid arguments", .{});
110    const path = filesystem.expandTilde(scratch, args.path);
111    if (args.offset) |o| if (o < 1) return fail(a, "read failed: offset must be at least 1", .{});
112    if (args.range) |r| if (r < 1) return fail(a, "read failed: range must be at least 1", .{});
113
114    var clipped = false;
115    const data = read: {
116        break :read filesystem.readFileAlloc(a, path, max_file_bytes) catch |e| switch (e) {
117            error.StreamTooLong => {
118                clipped = true;
119                break :read filesystem.readFilePrefix(a, path, clipped_file_bytes) catch |e2|
120                    return tools.ioFail(a, "read", path, e2);
121            },
122            else => return tools.ioFail(a, "read", path, e),
123        };
124    };
125
126    if (mimeFor(path)) |mime| {
127        if (clipped) {
128            return fail(a, "read failed: {s} is over {d} bytes, too large to read as an image", .{ path, max_file_bytes });
129        }
130        if (!ctx.supports_images) {
131            return fail(a, "read failed: {s} is an image and the current model does not accept image input", .{path});
132        }
133        const encoded = std.base64.standard.Encoder.calcSize(data.len);
134        if (encoded > max_image_base64_bytes) {
135            return fail(a, "read failed: {s} encodes to {d} bytes, over the {d} byte image limit", .{ path, encoded, max_image_base64_bytes });
136        }
137        const b64 = a.alloc(u8, encoded) catch return fail(a, "read failed: out of memory", .{});
138        _ = std.base64.standard.Encoder.encode(b64, data);
139        const summary = std.fmt.allocPrint(a, "read {s} ({s}, {d} bytes)", .{ path, mime, data.len }) catch "read";
140        const images = a.alloc(message_mod.ImageContent, 1) catch return .{ .text = summary, .is_error = false };
141        images[0] = .{ .data = b64, .mime_type = mime };
142        return .{
143            .text = summary,
144            .is_error = false,
145            .images = images,
146            .body = std.fmt.allocPrint(a, "{s}, {d} bytes", .{ mime, data.len }) catch null,
147        };
148    }
149
150    if (text.isBinary(data)) {
151        return fail(a, "read failed: {s} is binary; use bash (file, xxd)", .{path});
152    }
153
154    const content = text.utf8Clean(a, data) catch return fail(a, "read failed: out of memory", .{});
155    const offset = args.offset orelse 1;
156    const slice = lineSlice(content, offset, args.range) orelse {
157        if (clipped) {
158            return warn(a, "{s}: only the first {d} bytes were read, and offset {d} is past them", .{ path, content.len, offset });
159        }
160        return warn(a, "{s} has {d} lines; offset {d} is past the end", .{ path, text.countLines(content), offset });
161    };
162
163    var sent = slice;
164    if (slice.len > max_text_chars) {
165        var cut: usize = max_text_chars;
166        while (cut > 0 and slice[cut] & 0xC0 == 0x80) cut -= 1;
167        sent = slice[0..cut];
168    }
169    const truncated = clipped or sent.len < slice.len;
170    const note = if (clipped)
171        std.fmt.allocPrint(a, "... truncated ... only the first {d} bytes of {s} were read\n", .{ content.len, path }) catch "truncated"
172    else
173        std.fmt.allocPrint(a, "... truncated ... {s} is {d} characters\n", .{ path, slice.len }) catch "truncated";
174    const result_text = if (truncated)
175        std.fmt.allocPrint(a, "{s}\n\n{s}", .{ sent, note }) catch "read failed"
176    else
177        sent;
178    return .{ .text = result_text, .is_error = false, .body = bodyLine(a, args, text.countLines(sent), truncated) };
179}