//! A scripted provider for offline UI and loop work. const std = @import("std"); const provider = @import("../provider.zig"); const tools = @import("../tools.zig"); const Abort = @import("../abort.zig").Abort; pub const Fake = struct { io: std.Io, /// The directory the tool calls work in. cwd: []const u8 = ".", /// Delay between characters for the `slow` script. slow_delay: std.Io.Duration = .fromSeconds(1), pub fn asProvider(self: *Fake) provider.Provider { return .{ .ctx = self, .streamFn = stream }; } fn stream( ctx: *anyopaque, request: provider.Request, out: provider.StreamSink, abort: *Abort, diagnostics: *provider.Diagnostics, ) provider.StreamError!provider.Finish { _ = diagnostics; const self: *Fake = @ptrCast(@alignCast(ctx)); // A message after a tool result is the closing answer of the script. if (request.messages.len > 0 and request.messages[request.messages.len - 1] == .tool_result) { return say(out, "done"); } const prompt = lastUser(request.messages); if (std.mem.eql(u8, prompt, "ping")) { return say(out, "pong"); } if (std.mem.eql(u8, prompt, "tools")) { return self.toolTour(out); } if (std.mem.eql(u8, prompt, "sleep")) { return self.sleepCall(out); } if (std.mem.eql(u8, prompt, "slow")) { return self.slow(out, abort); } out.textDelta(prompt); return .stop; } /// Writes, reads, edits and prints `fake.txt` in `cwd`. fn toolTour(self: *Fake, out: provider.StreamSink) provider.StreamError!provider.Finish { var path_buffer: [std.fs.max_path_bytes]u8 = undefined; const path = std.fmt.bufPrint(&path_buffer, "{s}/fake.txt", .{self.cwd}) catch return error.Provider; // The sink copies each call, so the arguments can live on the stack. var buffers: [4][std.fs.max_path_bytes + 256]u8 = undefined; const calls = [_]struct { []const u8, []const u8, []const u8 }{ .{ "call-write", "write", std.fmt.bufPrint(&buffers[0], "{f}", .{std.json.fmt(.{ .path = path, .content = "hi" }, .{})}) catch return error.Provider }, .{ "call-read", "read", std.fmt.bufPrint(&buffers[1], "{f}", .{std.json.fmt(.{ .files = &.{.{ .path = path }} }, .{})}) catch return error.Provider }, .{ "call-edit", "edit", std.fmt.bufPrint(&buffers[2], "{f}", .{std.json.fmt(.{ .path = path, .edits = &.{.{ .old = "hi", .new = "ho" }} }, .{})}) catch return error.Provider }, .{ "call-bash", "bash", std.fmt.bufPrint(&buffers[3], "{f}", .{std.json.fmt(.{ .command = "cat fake.txt", .timeout = 10, .cwd = self.cwd }, .{})}) catch return error.Provider }, }; out.textDelta("running the tools"); for (calls) |call| out.toolCall(.{ .id = call[0], .name = call[1], .args_json = call[2] }); return .tool_calls; } fn slow(self: *Fake, out: provider.StreamSink, abort: *Abort) provider.StreamError!provider.Finish { var index: usize = 0; while (index < 60) : (index += 1) { out.textDelta("."); // The wait ends at once when the abort signal is set. if (abort.wait(self.io, .{ .duration = .{ .raw = self.slow_delay, .clock = .awake } })) { return error.Aborted; } } return .stop; } fn say(out: provider.StreamSink, text: []const u8) provider.Finish { out.textDelta(text); return .stop; } fn sleepCall(self: *Fake, out: provider.StreamSink) provider.StreamError!provider.Finish { var buffer: [std.fs.max_path_bytes + 256]u8 = undefined; const args = std.fmt.bufPrint(&buffer, "{f}", .{std.json.fmt(.{ .command = "sleep 60", .timeout = 120, .cwd = self.cwd }, .{})}) catch return error.Provider; out.toolCall(.{ .id = "call-sleep", .name = "bash", .args_json = args }); return .tool_calls; } fn lastUser(messages: []const provider.Message) []const u8 { var index = messages.len; while (index > 0) { index -= 1; switch (messages[index]) { .user => |text| return text, else => {}, } } return ""; } };