diff options
| author | Lucas Faria Mendes <dsn.lucas@proton.me> | 2026-02-19 18:03:45 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <dsn.lucas@proton.me> | 2026-02-19 18:03:45 +0000 |
| commit | 9c51912e1c0905e36001fd137631b591940364a7 (patch) | |
| tree | cf41fbd8ab2e4611d48e567979ff37c5489e13f9 | |
| parent | e3024d5779e26252f86e4a00d4d5b07708891e3e (diff) | |
| download | claude-zig-9c51912e1c0905e36001fd137631b591940364a7.tar.gz claude-zig-9c51912e1c0905e36001fd137631b591940364a7.zip | |
simple, i think
| -rw-r--r-- | src/main.zig | 57 |
1 files changed, 39 insertions, 18 deletions
diff --git a/src/main.zig b/src/main.zig index 169f5b2..8192432 100644 --- a/src/main.zig +++ b/src/main.zig @@ -121,23 +121,9 @@ pub fn main() !void { try writeMessages(&jw, messages.items); try jw.objectField("tools"); - try jw.write(&[_]struct { - type: []const u8, - function: struct { - name: []const u8, - description: []const u8, - parameters: struct { - type: []const u8, - properties: struct { - file_path: struct { - type: []const u8, - description: []const u8, - }, - }, - required: []const []const u8, - }, - }, - }{.{ + try jw.beginArray(); + // Read tool + try jw.write(.{ .type = "function", .function = .{ .name = "Read", @@ -153,7 +139,30 @@ pub fn main() !void { .required = &[_][]const u8{"file_path"}, }, }, - }}); + }); + // Write tool + try jw.write(.{ + .type = "function", + .function = .{ + .name = "Write", + .description = "Write content to a file", + .parameters = .{ + .type = "object", + .properties = .{ + .file_path = .{ + .type = "string", + .description = "The path of the file to write to", + }, + .content = .{ + .type = "string", + .description = "The content to write to the file", + }, + }, + .required = &[_][]const u8{ "file_path", "content" }, + }, + }, + }); + try jw.endArray(); try jw.endObject(); const body = body_out.written(); @@ -219,6 +228,18 @@ pub fn main() !void { const file_path = args_parsed.value.object.get("file_path").?.string; std.debug.print("Reading file: {s}\n", .{file_path}); result_owned = try std.fs.cwd().readFileAlloc(allocator, file_path, std.math.maxInt(usize)); + } else if (std.mem.eql(u8, func_name, "Write")) { + const args_parsed = try std.json.parseFromSlice(std.json.Value, allocator, arguments_str, .{}); + defer args_parsed.deinit(); + const file_path = args_parsed.value.object.get("file_path").?.string; + const content = args_parsed.value.object.get("content").?.string; + std.debug.print("Writing file: {s}\n", .{file_path}); + // Create parent directories if needed + if (std.fs.path.dirname(file_path)) |dir| { + try std.fs.cwd().makePath(dir); + } + try std.fs.cwd().writeFile(.{ .sub_path = file_path, .data = content }); + result_owned = try allocator.dupe(u8, "OK"); } try messages.append(allocator, .{ |