Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441//! The two ends of the connection between a core and a UI in two processes.//! Each end reads `wire` lines from one byte stream and writes them to//! another.
const std = @import("std");
const protocol = @import("protocol.zig");const wire = @import("wire.zig");const Request = protocol.ui.Request;const Message = protocol.core.Message;
const Allocator = std.mem.Allocator;const Io = std.Io;
pub const ReadError = error{ ReadFailed, OutOfMemory };
/// The end in the process of the core. Do not move it after `sink`.pub const CoreEnd = struct { gpa: Allocator, /// Gets each message for the UI as one line. out: *Io.Writer, /// Gets one line of text for each problem. log: *Io.Writer,
/// Where the core sends its messages. A message is on its way to the UI /// when `send` returns. Use it from one task at a time. pub fn sink(self: *CoreEnd) protocol.Sink(Message) { return .{ .ctx = self, .sendFn = send }; }
fn send(ctx: *anyopaque, message: Message) bool { const self: *CoreEnd = @ptrCast(@alignCast(ctx)); wire.writeMessage(self.out, message) catch return false; self.out.flush() catch return false; message.deinit(self.gpa); return true; }
/// Gives each request from `in` to `core` until `in` ends. An `attach` /// or a `detach` gets `sink()` as its sink. Reports a line that is not a /// request, and a request that `core` refuses, and goes on. pub fn serve(self: *CoreEnd, in: *Io.Reader, core: protocol.Sink(Request)) ReadError!void { var line: Io.Writer.Allocating = .init(self.gpa); defer line.deinit(); while (try nextLine(in, &line)) |text| { if (text.len == 0) { continue; } const request = wire.readRequest(self.gpa, text, self.sink()) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.Invalid => { self.report("not a request", text); continue; }, }; if (!core.send(request)) { request.deinit(self.gpa); self.report("the core refused the request", text); } } }
fn report(self: *CoreEnd, problem: []const u8, line: []const u8) void { const shown = line[0..@min(line.len, 200)]; self.log.print("jig: {s}: {s}\n", .{ problem, shown }) catch return; self.log.flush() catch {}; }};
/// The end in the process of the UI. Do not move it after `sink`.pub const UiEnd = struct { gpa: Allocator, /// Gets each request for the core as one line. out: *Io.Writer,
/// Where the UI sends its requests. A request is on its way to the core /// when `send` returns. Use it from one task at a time. pub fn sink(self: *UiEnd) protocol.Sink(Request) { return .{ .ctx = self, .sendFn = send }; }
fn send(ctx: *anyopaque, request: Request) bool { const self: *UiEnd = @ptrCast(@alignCast(ctx)); wire.writeRequest(self.out, request) catch return false; self.out.flush() catch return false; request.deinit(self.gpa); return true; }
/// Gives each message from `in` to `ui` until `in` ends or `ui` refuses /// a message. A line that is not a message becomes an error notice. pub fn pump(self: *UiEnd, in: *Io.Reader, ui: protocol.Sink(Message)) ReadError!void { var line: Io.Writer.Allocating = .init(self.gpa); defer line.deinit(); while (try nextLine(in, &line)) |text| { if (text.len == 0) { continue; } const message = wire.readMessage(self.gpa, text) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.Invalid => Message{ .notice = .{ .kind = .err, .text = try self.gpa.dupe(u8, "the core sent a line that this UI cannot read"), } }, }; if (!ui.send(message)) { message.deinit(self.gpa); return; } } }};
/// The next line of `in` without its line break, or null when `in` has no/// more lines. Drops a last line that has no line break, because it was cut.fn nextLine(in: *Io.Reader, line: *Io.Writer.Allocating) ReadError!?[]const u8 { line.clearRetainingCapacity(); _ = in.streamDelimiterEnding(&line.writer, '\n') catch |err| switch (err) { error.ReadFailed => return error.ReadFailed, error.WriteFailed => return error.OutOfMemory, }; if (in.bufferedLen() == 0) { return null; } in.toss(1); return line.written();}
const testing = std.testing;const helpers = @import("testing.zig");const models = @import("models.zig");const Agent = @import("agent.zig").Agent;
const Requests = helpers.EventLog(Request);const Messages = helpers.EventLog(Message);
/// A core end that writes to memory.const CoreFixture = struct { out: Io.Writer.Allocating, log: Io.Writer.Allocating, end: CoreEnd,
fn init(self: *CoreFixture) void { self.out = .init(testing.allocator); self.log = .init(testing.allocator); self.end = .{ .gpa = testing.allocator, .out = &self.out.writer, .log = &self.log.writer }; }
fn deinit(self: *CoreFixture) void { self.out.deinit(); self.log.deinit(); }};
test "the core's end gives each request to the core in order, with itself as the sink of an attach" { var fixture: CoreFixture = undefined; fixture.init(); defer fixture.deinit(); var requests: Requests = .{}; defer requests.deinit();
var in: Io.Reader = .fixed( \\{"id":0,"method":"attach","params":{"version":1}} \\ \\{"id":1,"method":"submit","params":"hi\nthere"} \\{"id":2,"method":"interrupt"} \\ ); try fixture.end.serve(&in, requests.sink());
requests.collect(); const got = requests.events.items; try testing.expectEqual(3, got.len); try testing.expect(got[0].method.attach.sink.eql(fixture.end.sink())); try testing.expectEqual(1, got[1].id); try testing.expectEqualStrings("hi\nthere", got[1].method.submit); try testing.expectEqual(.interrupt, std.meta.activeTag(got[2].method)); try testing.expectEqualStrings("", fixture.log.written());}
test "a line that is not a request is reported and the next line still arrives" { var fixture: CoreFixture = undefined; fixture.init(); defer fixture.deinit(); var requests: Requests = .{}; defer requests.deinit();
var in: Io.Reader = .fixed( \\{"id":0,"method":"fly"} \\{"id":1,"method":"interrupt"} \\ ); try fixture.end.serve(&in, requests.sink());
requests.collect(); try testing.expectEqual(1, requests.events.items.len); try testing.expectEqual(1, requests.events.items[0].id); try testing.expectEqualStrings("jig: not a request: {\"id\":0,\"method\":\"fly\"}\n", fixture.log.written());}
test "a last line that was cut is dropped" { var fixture: CoreFixture = undefined; fixture.init(); defer fixture.deinit(); var requests: Requests = .{}; defer requests.deinit();
var in: Io.Reader = .fixed("{\"id\":1,\"method\":\"interrupt\"}\n{\"id\":2,\"method\":\"interrupt\"}"); try fixture.end.serve(&in, requests.sink());
requests.collect(); try testing.expectEqual(1, requests.events.items.len);}
fn refuseRequest(_: *anyopaque, _: Request) bool { return false;}
test "a request that the core refuses is freed and reported" { var fixture: CoreFixture = undefined; fixture.init(); defer fixture.deinit();
var in: Io.Reader = .fixed("{\"id\":1,\"method\":\"submit\",\"params\":\"hi\"}\n"); try fixture.end.serve(&in, .{ .ctx = undefined, .sendFn = refuseRequest });
try testing.expectEqualStrings( "jig: the core refused the request: {\"id\":1,\"method\":\"submit\",\"params\":\"hi\"}\n", fixture.log.written(), );}
test "the core's end writes each message as one line and frees it" { var fixture: CoreFixture = undefined; fixture.init(); defer fixture.deinit();
const sink = fixture.end.sink(); try testing.expect(sink.send(.{ .text_delta = try testing.allocator.dupe(u8, "a\nb") })); try testing.expect(sink.send(.agent_end));
try testing.expectEqualStrings( "{\"method\":\"text_delta\",\"params\":\"a\\nb\"}\n{\"method\":\"agent_end\"}\n", fixture.out.written(), );}
test "the UI's end writes each request as one line and frees it" { var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); var end: UiEnd = .{ .gpa = testing.allocator, .out = &out.writer };
try testing.expect(end.sink().send(.{ .id = 4, .method = .{ .submit = try testing.allocator.dupe(u8, "hi") } }));
try testing.expectEqualStrings("{\"id\":4,\"method\":\"submit\",\"params\":\"hi\"}\n", out.written());}
test "the UI's end gives each message to the UI, and an error notice for a line it cannot read" { var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); var end: UiEnd = .{ .gpa = testing.allocator, .out = &out.writer }; var messages: Messages = .{}; defer messages.deinit();
var in: Io.Reader = .fixed( \\{"method":"text_delta","params":"hi"} \\{"method":"from_a_newer_core"} \\{"id":3,"result":{"kind":"ok"}} \\ ); try end.pump(&in, messages.sink());
messages.collect(); const got = messages.events.items; try testing.expectEqual(3, got.len); try testing.expectEqualStrings("hi", got[0].text_delta); try testing.expectEqual(.err, got[1].notice.kind); try testing.expectEqual(protocol.core.Response{ .id = 3, .result = .ok }, got[2].response);}
fn refuseMessage(_: *anyopaque, _: Message) bool { return false;}
test "the UI's end stops when the UI refuses a message" { var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); var end: UiEnd = .{ .gpa = testing.allocator, .out = &out.writer };
var in: Io.Reader = .fixed("{\"method\":\"text_delta\",\"params\":\"hi\"}\n{\"method\":\"agent_end\"}\n"); try end.pump(&in, .{ .ctx = undefined, .sendFn = refuseMessage });
try testing.expect(in.bufferedLen() > 0);}
/// A pipe of the operating system.pub const TestPipe = struct { read: Io.File, write: Io.File,
pub fn open() !TestPipe { const fds = try Io.Threaded.pipe2(.{ .CLOEXEC = true }); return .{ .read = .{ .handle = fds[0], .flags = .{ .nonblocking = false } }, .write = .{ .handle = fds[1], .flags = .{ .nonblocking = false } }, }; }};
/// A started agent with a scripted provider, behind two pipes, and the UI's/// end of the pipes. Do not move it after `init`.pub const TestLink = struct { script: helpers.Scripted, agent: *Agent, to_core: TestPipe, to_ui: TestPipe, core_in: Io.File.Reader, core_out: Io.File.Writer, core_log: Io.Writer.Allocating, core_end: CoreEnd, serving: ?Io.Future(ReadError!void), ui_in: Io.File.Reader, ui_out: Io.File.Writer, ui_end: UiEnd, // Small, so that most lines are longer than a buffer. buffers: [4][64]u8,
const test_model: models.Entry = .{ .name = "model", .id = "provider/model", .provider = .fake, .max_output = 0 };
pub fn init(self: *TestLink, steps: []const helpers.Scripted.Step) !void { const io = testing.io; self.script = .init(steps); var providers: Agent.Providers = .initFill(null); providers.set(.fake, self.script.asProvider()); self.agent = try Agent.init(testing.allocator, io, .{ .environ = testing.environ, .providers = providers, .system_prompt = "system", .models = (&test_model)[0..1], .model = 0, .cwd = "/work", }); try self.agent.start(); self.to_core = try .open(); self.to_ui = try .open(); self.core_in = self.to_core.read.readerStreaming(io, &self.buffers[0]); self.core_out = self.to_ui.write.writerStreaming(io, &self.buffers[1]); self.core_log = .init(testing.allocator); self.core_end = .{ .gpa = testing.allocator, .out = &self.core_out.interface, .log = &self.core_log.writer }; self.serving = try io.concurrent(CoreEnd.serve, .{ &self.core_end, &self.core_in.interface, self.agent.sink() }); self.ui_in = self.to_ui.read.readerStreaming(io, &self.buffers[2]); self.ui_out = self.to_core.write.writerStreaming(io, &self.buffers[3]); self.ui_end = .{ .gpa = testing.allocator, .out = &self.ui_out.interface }; }
/// Ends the input of the core and waits until the core's end stops. pub fn hangUp(self: *TestLink) ReadError!void { var serving = self.serving orelse return; self.serving = null; self.to_core.write.close(testing.io); return serving.await(testing.io); }
/// Call it before the task that reads `ui_in` is awaited. pub fn deinit(self: *TestLink) void { self.script.release(); self.hangUp() catch {}; self.agent.deinit(); self.to_ui.write.close(testing.io); self.to_core.read.close(testing.io); self.core_log.deinit(); self.script.deinit(); }};
test "a core behind pipes answers an attach, runs a prompt and stops when its input ends" { var arena_state: std.heap.ArenaAllocator = .init(testing.allocator); defer arena_state.deinit(); const io = testing.io;
var messages: Messages = .{}; defer messages.deinit(); var link: TestLink = undefined; try link.init(&.{.{ .text = &.{ "po", "ng" } }}); defer link.to_ui.read.close(io); var pumping = try io.concurrent(UiEnd.pump, .{ &link.ui_end, &link.ui_in.interface, messages.sink() }); defer pumping.await(io) catch {}; defer link.deinit();
const core = link.ui_end.sink(); try testing.expect(core.send(.{ .id = 0, .method = .{ .attach = .{ .sink = messages.sink() } } })); try messages.waitFor(.response, 1); try testing.expectEqualStrings("model", messages.events.items[0].response.result.attached.model);
// Longer than every buffer on the way. const prompt = try testing.allocator.alloc(u8, 100_000); @memset(prompt, 'x'); try testing.expect(core.send(.{ .id = 1, .method = .{ .submit = prompt } })); try messages.waitFor(.agent_end, 1); try testing.expectEqualStrings("pong", try messages.text(arena_state.allocator())); try testing.expectEqual(100_000, link.script.requests.items[0].messages[0].user.len);
try link.hangUp(); try testing.expectEqualStrings("", link.core_log.written());}
test "a TUI behind pipes shows the prompt and the reply of the core" { const tuis = @import("tui.zig"); const vaxis = @import("vaxis"); const io = testing.io;
const tui = try tuis.Tui.init(testing.allocator, io); defer tui.deinit(); var link: TestLink = undefined; try link.init(&.{.{ .text = &.{ "po", "ng" } }}); defer link.to_ui.read.close(io); var pumping = try io.concurrent(UiEnd.pump, .{ &link.ui_end, &link.ui_in.interface, tui.sink() }); defer pumping.await(io) catch {}; defer link.deinit();
try tui.attach(link.ui_end.sink()); try tui.editor.insert("ping"); try testing.expect(try tui.handle(.{ .key_press = .{ .codepoint = vaxis.Key.enter } })); { var watchdog: helpers.Watchdog(tuis.Queue) = try .start(&tui.loop.queue); defer watchdog.stop(); var ended = false; while (!ended) { const event = try tui.loop.queue.pop(); ended = event == .core and event.core == .agent_end; try testing.expect(try tui.handle(event)); } }
const items = tui.transcript.items.items; try testing.expectEqual(2, items.len); try testing.expectEqualStrings("ping", items[0].user); try testing.expectEqualStrings("pong", items[1].assistant.items); try testing.expectEqualStrings("model", tui.view.model);}