//! Transcript model owned by the UI thread. const std = @import("std"); const protocol = @import("protocol.zig"); pub const State = enum { running, ok, failed }; pub const ToolResult = struct { id: []const u8, state: State = .running, call: protocol.core.Block, collapsed: protocol.core.Block = .{}, expanded: protocol.core.Block = .{}, /// Whether the user opened the call to see its expanded result. open: bool = false, }; pub const Transcript = struct { gpa: std.mem.Allocator, items: std.ArrayList(Item) = .empty, /// Whether tool calls start expanded. Toggling every call flips this, so /// later calls follow the same choice. expanded_all: bool = false, /// Whether a view shows thinking in full or as one short label. show_thinking: bool = true, pub fn init(gpa: std.mem.Allocator) Transcript { return .{ .gpa = gpa }; } pub fn deinit(self: *Transcript) void { for (self.items.items) |item| item.deinit(self.gpa); self.items.deinit(self.gpa); } pub fn apply(self: *Transcript, event: protocol.core.Message) void { switch (event) { .text_delta => |text| self.appendText(.assistant, text), .thinking_delta => |text| self.appendText(.thinking, text), .tool_call => |call| self.items.append(self.gpa, .{ .tool = .{ .id = call.id, .call = call.call, .open = self.expanded_all, } }) catch event.deinit(self.gpa), .tool_result => |result| { const tool = self.findToolResult(result.call_id) orelse return event.deinit(self.gpa); self.gpa.free(result.call_id); tool.collapsed.deinit(self.gpa); tool.expanded.deinit(self.gpa); tool.state = if (result.is_error) .failed else .ok; tool.collapsed = result.collapsed; tool.expanded = result.expanded; }, .notice => |notice| { self.items.append(self.gpa, .{ .notice = notice }) catch self.gpa.free(notice.text); }, .response, .selection, .usage, .prompt_start, .prompts_returned, .agent_start, .agent_end => event.deinit(self.gpa), } } fn appendText(self: *Transcript, comptime tag: std.meta.Tag(Item), text: []const u8) void { defer self.gpa.free(text); const items = self.items.items; if (items.len == 0 or std.meta.activeTag(items[items.len - 1]) != tag) { self.items.append(self.gpa, @unionInit(Item, @tagName(tag), .empty)) catch return; } const last = &self.items.items[self.items.items.len - 1]; @field(last, @tagName(tag)).appendSlice(self.gpa, text) catch {}; } pub fn toggleThinking(self: *Transcript) void { self.show_thinking = !self.show_thinking; } /// Expands or collapses every tool call, and makes later calls follow. pub fn toggleExpanded(self: *Transcript) void { self.expanded_all = !self.expanded_all; for (self.items.items) |*item| switch (item.*) { .tool => |*tool| tool.open = self.expanded_all, else => {}, }; } /// Expands or collapses one tool call. pub fn toggleItem(self: *Transcript, index: usize) void { if (index >= self.items.items.len) { return; } switch (self.items.items[index]) { .tool => |*tool| tool.open = !tool.open, else => {}, } } fn findToolResult(self: *Transcript, call_id: []const u8) ?*ToolResult { var index = self.items.items.len; while (index > 0) { index -= 1; switch (self.items.items[index]) { .tool => |*tool| if (std.mem.eql(u8, tool.id, call_id)) { return tool; }, else => {}, } } return null; } pub const Item = union(enum) { user: []const u8, assistant: std.ArrayList(u8), thinking: std.ArrayList(u8), tool: ToolResult, notice: protocol.core.Notice, fn deinit(self: Item, gpa: std.mem.Allocator) void { switch (self) { .user => |text| gpa.free(text), .assistant, .thinking => |text| gpa.free(text.allocatedSlice()), .tool => |tool| { gpa.free(tool.id); tool.call.deinit(gpa); tool.collapsed.deinit(gpa); tool.expanded.deinit(gpa); }, .notice => |notice| gpa.free(notice.text), } } }; }; const testing = std.testing; fn makeCall(gpa: std.mem.Allocator, id: []const u8) !protocol.core.Message { return .{ .tool_call = .{ .id = try gpa.dupe(u8, id), .call = try .init(gpa, &.{.{ .text = id }}) } }; } fn makeResult(gpa: std.mem.Allocator, id: []const u8, collapsed: []const u8, expanded: []const u8, is_error: bool) !protocol.core.Message { return .{ .tool_result = .{ .call_id = try gpa.dupe(u8, id), .is_error = is_error, .collapsed = try .init(gpa, &.{.{ .text = collapsed }}), .expanded = try .init(gpa, &.{.{ .text = expanded }}), } }; } test "text deltas join one assistant item" { const gpa = testing.allocator; var transcript = Transcript.init(gpa); defer transcript.deinit(); transcript.apply(.{ .text_delta = try gpa.dupe(u8, "po") }); transcript.apply(.{ .text_delta = try gpa.dupe(u8, "ng") }); try testing.expectEqual(@as(usize, 1), transcript.items.items.len); try testing.expectEqualStrings("pong", transcript.items.items[0].assistant.items); } test "thinking deltas join their own item" { const gpa = testing.allocator; var transcript = Transcript.init(gpa); defer transcript.deinit(); transcript.apply(.{ .thinking_delta = try gpa.dupe(u8, "th") }); transcript.apply(.{ .thinking_delta = try gpa.dupe(u8, "ink") }); try testing.expectEqualStrings("think", transcript.items.items[0].thinking.items); transcript.apply(.{ .text_delta = try gpa.dupe(u8, "answer") }); try testing.expectEqual(@as(usize, 2), transcript.items.items.len); try testing.expectEqualStrings("answer", transcript.items.items[1].assistant.items); } test "a tool result finds its call and sets the state" { const gpa = testing.allocator; var transcript = Transcript.init(gpa); defer transcript.deinit(); transcript.apply(try makeCall(gpa, "call-1")); transcript.apply(try makeResult(gpa, "call-1", "exit 0", "output", false)); try testing.expectEqual(@as(usize, 1), transcript.items.items.len); const tool = transcript.items.items[0].tool; try testing.expectEqual(State.ok, tool.state); try testing.expectEqualStrings("call-1", tool.call.text); try testing.expectEqualStrings("exit 0", tool.collapsed.text); try testing.expectEqualStrings("output", tool.expanded.text); try testing.expect(!tool.open); } test "a failed result marks the tool failed and notices are kept" { const gpa = testing.allocator; var transcript = Transcript.init(gpa); defer transcript.deinit(); transcript.apply(try makeCall(gpa, "call-1")); transcript.apply(try makeResult(gpa, "call-1", "bad", "bad", true)); transcript.apply(.{ .notice = .{ .kind = .interrupted, .text = try gpa.dupe(u8, "interrupted") } }); try testing.expectEqual(State.failed, transcript.items.items[0].tool.state); try testing.expectEqual(@as(usize, 2), transcript.items.items.len); try testing.expectEqual(protocol.core.Notice.Kind.interrupted, transcript.items.items[1].notice.kind); } test "toggling every tool call flips each block and later calls follow" { const gpa = testing.allocator; var transcript = Transcript.init(gpa); defer transcript.deinit(); transcript.apply(try makeCall(gpa, "call-1")); transcript.apply(try makeCall(gpa, "call-2")); try testing.expect(!transcript.items.items[0].tool.open); try testing.expect(!transcript.items.items[1].tool.open); transcript.toggleExpanded(); try testing.expect(transcript.items.items[0].tool.open); try testing.expect(transcript.items.items[1].tool.open); transcript.apply(try makeCall(gpa, "call-3")); try testing.expect(transcript.items.items[2].tool.open); transcript.toggleExpanded(); try testing.expect(!transcript.items.items[0].tool.open); try testing.expect(!transcript.items.items[2].tool.open); } test "toggling one item leaves the others alone" { const gpa = testing.allocator; var transcript = Transcript.init(gpa); defer transcript.deinit(); transcript.apply(try makeCall(gpa, "call-1")); transcript.apply(.{ .text_delta = try gpa.dupe(u8, "text") }); transcript.apply(try makeCall(gpa, "call-2")); transcript.toggleItem(0); try testing.expect(transcript.items.items[0].tool.open); try testing.expect(!transcript.items.items[2].tool.open); transcript.toggleItem(1); try testing.expectEqualStrings("text", transcript.items.items[1].assistant.items); transcript.toggleItem(9); } test "a result with no call and a repeated result free their strings" { const gpa = testing.allocator; var transcript = Transcript.init(gpa); defer transcript.deinit(); transcript.apply(try makeResult(gpa, "gone", "orphan", "orphan", false)); transcript.apply(try makeCall(gpa, "call-1")); for ([_][]const u8{ "first", "second" }) |text| { transcript.apply(try makeResult(gpa, "call-1", text, text, false)); } try testing.expectEqual(@as(usize, 1), transcript.items.items.len); try testing.expectEqualStrings("second", transcript.items.items[0].tool.expanded.text); }