Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172const std = @import("std");const tools = @import("../tools.zig");const schema = @import("schema.zig");
pub const definition = .{ .name = "write", .description = \\Write a file. Parent directories are created and an existing file is \\overwritten. The result is the number of bytes written. , .Args = Args, .execute = execute,};
pub const Args = struct { path: schema.String(.{ .description = "File path, relative to the working directory or absolute." }), content: schema.String(.{ .description = "Full file content." }),};
pub const Output = struct { /// The text the model sees. text: []const u8,};
pub fn execute(ctx: tools.Context, arena: std.mem.Allocator, args: Args) !Output { const path = args.path.value; if (path.len > 0 and std.fs.path.isSep(path[path.len - 1])) { return tools.pathError(ctx, arena, path, error.IsDir); } // Opening the parent follows a symlinked directory. var dir = std.Io.Dir.cwd().createDirPathOpen(ctx.io, std.fs.path.dirname(path) orelse ".", .{}) catch |err| return tools.pathError(ctx, arena, path, err); defer dir.close(ctx.io); tools.replaceFile(ctx.io, arena, dir, std.fs.path.basename(path), args.content.value) catch |err| return tools.pathError(ctx, arena, path, err);
const text = try std.fmt.allocPrint(arena, "wrote {d} bytes to {s}", .{ args.content.value.len, path }); return .{ .text = text };}const testing = std.testing;
test "write creates parent directories, follows a symlinked parent, overwrites, rejects a directory path and shows its text when expanded" { var dir = try tools.TestDir.init(); defer dir.deinit(); const path = try dir.path("a/b/c.txt");
const created = dir.call("write", .{ .path = path, .content = "hi" }); try testing.expect(!created.is_error); try testing.expectEqualStrings(try std.fmt.allocPrint(dir.arena(), "wrote 2 bytes to {s}", .{path}), created.text); try testing.expectEqualStrings(created.text, try tools.content.plain(dir.arena(), created.expanded)); try testing.expectEqualStrings("hi", try dir.read("a/b/c.txt"));
const overwritten = dir.call("write", .{ .path = path, .content = "yo" }); try testing.expect(!overwritten.is_error); try testing.expectEqualStrings("yo", try dir.read("a/b/c.txt"));
try dir.tmp.dir.symLink(testing.io, "a", "link", .{ .is_directory = true }); const linked = dir.call("write", .{ .path = try dir.path("link/d.txt"), .content = "ok" }); try testing.expect(!linked.is_error); try testing.expectEqualStrings("ok", try dir.read("a/d.txt"));
try testing.expect(dir.call("write", .{ .path = try dir.path("a/e/"), .content = "no" }).is_error);}
/// The path being written.pub fn renderCall(arena: std.mem.Allocator, args: Args) std.mem.Allocator.Error!tools.content.Content { return tools.content.callLine(arena, definition.name, args.path.value);}
/// The text the model sees, once expanded.pub fn renderResult(arena: std.mem.Allocator, output: Output) std.mem.Allocator.Error!tools.Rendered { return .{ .expanded = try tools.content.text(arena, output.text) };}