@@ -49,11 +49,18 @@ const image_mime = [_]struct { ext: []const u8, mime: []const u8 }{
4949
5050 const max_text_chars = 100_000;
5151 const max_image_base64_bytes = 5 * 1024 * 1024;
52+const max_file_bytes = 1 << 30;
53+const clipped_file_bytes = max_text_chars * 4;
5254
5355 fn fail(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) tools.Result {
5456 return tools.fail(a, "read failed", fmt, args);
5557 }
5658
59+/// A non-error result the model reads as information, not as a failure.
60+fn warn(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) tools.Result {
61+ return .{ .text = std.fmt.allocPrint(a, fmt, args) catch "read", .is_error = false };
62+}
63+
5764 fn mimeFor(path: []const u8) ?[]const u8 {
5865 const ext = std.fs.path.extension(path);
5966 for (image_mime) |entry| {
@@ -110,10 +117,22 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
110117 if (args.offset) |o| if (o < 1) return fail(a, "read failed: offset must be at least 1", .{});
111118 if (args.range) |r| if (r < 1) return fail(a, "read failed: range must be at least 1", .{});
112119
113- const data = filesystem.readFileAlloc(a, path, 1 << 30) catch |e|
114- return fail(a, "read failed: {s}: {s}", .{ path, @errorName(e) });
120+ var clipped = false;
121+ const data = read: {
122+ break :read filesystem.readFileAlloc(a, path, max_file_bytes) catch |e| switch (e) {
123+ error.StreamTooLong => {
124+ clipped = true;
125+ break :read filesystem.readFilePrefix(a, path, clipped_file_bytes) catch |e2|
126+ return tools.ioFail(a, "read", path, e2);
127+ },
128+ else => return tools.ioFail(a, "read", path, e),
129+ };
130+ };
115131
116132 if (mimeFor(path)) |mime| {
133+ if (clipped) {
134+ return fail(a, "read failed: {s} is over {d} bytes, too large to read as an image", .{ path, max_file_bytes });
135+ }
117136 if (!ctx.supports_images) {
118137 return fail(a, "read failed: {s} is an image and the current model does not accept image input", .{path});
119138 }
@@ -140,8 +159,12 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
140159
141160 const content = text.utf8Clean(a, data) catch return fail(a, "read failed: out of memory", .{});
142161 const offset = args.offset orelse 1;
143- const slice = lineSlice(content, offset, args.range) orelse
144- return fail(a, "read failed: {s} has fewer than {d} lines", .{ path, offset });
162+ const slice = lineSlice(content, offset, args.range) orelse {
163+ if (clipped) {
164+ return warn(a, "{s}: only the first {d} bytes were read, and offset {d} is past them", .{ path, content.len, offset });
165+ }
166+ return warn(a, "{s} has {d} lines; offset {d} is past the end", .{ path, countLines(content), offset });
167+ };
145168
146169 var sent = slice;
147170 if (slice.len > max_text_chars) {
@@ -149,9 +172,13 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
149172 while (cut > 0 and slice[cut] & 0xC0 == 0x80) cut -= 1;
150173 sent = slice[0..cut];
151174 }
152- const truncated = sent.len < slice.len;
175+ const truncated = clipped or sent.len < slice.len;
176+ const note = if (clipped)
177+ std.fmt.allocPrint(a, "... truncated ... only the first {d} bytes of {s} were read\n", .{ content.len, path }) catch "truncated"
178+ else
179+ std.fmt.allocPrint(a, "... truncated ... {s} is {d} characters\n", .{ path, slice.len }) catch "truncated";
153180 const result_text = if (truncated)
154- std.fmt.allocPrint(a, "{s}\n\n... truncated ... {s} is {d} characters\n", .{ sent, path, slice.len }) catch "read failed"
181+ std.fmt.allocPrint(a, "{s}\n\n{s}", .{ sent, note }) catch "read failed"
155182 else
156183 sent;
157184 return .{ .text = result_text, .is_error = false, .body = bodyLine(a, args, countLines(sent), truncated) };