Lightpanda: the headless browser designed for AI and automation
agent: route REPL output per tool and align page tables of any size
Adrià Arrufat2899bfef4ad8
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zigindex 82b2f4034..9417822d2 100644--- a/src/agent/Agent.zig+++ b/src/agent/Agent.zig@@ -184,7 +184,7 @@ stream_enabled: bool,/// by `endStreamedText`, which also emits the closing newline.stream_active: bool = false,/// True when any assistant text streamed during the current turn, so `runTurn`-/// skips the buffered `printAssistant` that would double-print it.+/// skips the buffered `printMarkdown` that would double-print it.streamed_text: bool = false,available_providers: []const []const u8,/// Cached reachability of each `local_providers` entry, so the per-keystroke@@ -587,7 +587,7 @@ fn runTurn(self: *Agent, input: TurnInput) bool {// turn-wide flag: the stream accumulator reconstructs `t` from the// same deltas it emitted, and the synthesis fallback also streams, so// `streamed_text` implies `t` was already shown in full.- if (!self.streamed_text) self.terminal.printAssistant(t);+ if (!self.streamed_text) self.terminal.printMarkdown(t);} else if (self.last_turn_refused)self.terminal.printInfo("(model declined to respond — safety refusal)", .{})else@@ -641,11 +641,11 @@ fn runRepl(self: *Agent) void {// Surface console output: slash commands (and thus /consoleLogs)// are unreachable in JS mode, so a console must echo logs itself.const logs = std.mem.trimEnd(u8, self.session.drainConsoleMessages(), "\n");- if (logs.len > 0) self.printData(logs);+ if (logs.len > 0) self.printData(.consoleLogs, logs);if (result.is_error) {self.terminal.printError("{s}", .{result.text});} else {- self.printData(result.text);+ self.printData(.evaluate, result.text);self.recordSaveRaw(line);}continue :repl;@@ -1510,17 +1510,19 @@ fn printCommandResult(self: *Agent, cmd: Command, result: browser_tools.ToolResuelse => return,};if (cmd.producesData() and !result.is_error) {- self.printData(result.text);+ self.printData(tc.tool, result.text);return;}self.terminal.printToolOutcome(tc.name(), result.text, result.is_error);}-/// Re-indent JSON for the terminal; MCP keeps renderJson's compact form.-fn printData(self: *Agent, text: []const u8) void {+/// Only `markdown` is rendered as markdown; the rest is JSON (re-indented+/// here, MCP keeps renderJson's compact form) or verbatim text.+fn printData(self: *Agent, tool: browser_tools.Tool, text: []const u8) void {+ if (tool == .markdown) return self.terminal.printMarkdown(text);var arena = std.heap.ArenaAllocator.init(self.allocator);defer arena.deinit();- self.terminal.printAssistant(Terminal.reindentJson(arena.allocator(), text) orelse text);+ self.terminal.printPlain(Terminal.reindentJson(arena.allocator(), text) orelse text);}/// Tracks whether a `/load`-run script emitted any `console.*` output, decidingdiff --git a/src/agent/Terminal.zig b/src/agent/Terminal.zigindex 94875f910..fef9cbec3 100644--- a/src/agent/Terminal.zig+++ b/src/agent/Terminal.zig@@ -241,9 +241,16 @@ fn renderStyled(self: *Terminal, text: []const u8, op: enum { full, delta, end }w.flush() catch {};}-pub fn printAssistant(self: *Terminal, text: []const u8) void {+/// Styled on a REPL tty, verbatim otherwise.+pub fn printMarkdown(self: *Terminal, text: []const u8) void {if (text.len == 0) return;if (self.styledOutput()) return self.renderStyled(text, .full);+ self.printPlain(text);+}++pub fn printPlain(self: *Terminal, text: []const u8) void {+ _ = self;+ if (text.len == 0) return;_ = std.c.write(std.posix.STDOUT_FILENO, (text).ptr, (text).len);_ = std.c.write(std.posix.STDOUT_FILENO, ("\n").ptr, ("\n").len);}diff --git a/src/agent/md_term.zig b/src/agent/md_term.zigindex 068b036a2..239db1029 100644--- a/src/agent/md_term.zig+++ b/src/agent/md_term.zig@@ -20,12 +20,19 @@ const std = @import("std");const ansi = @import("ansi.zig");const js_highlight = @import("js_highlight.zig");-/// Render markdown `src` as ANSI-styled terminal output to `w` — one-shot-/// wrapper over `Stream`, so batch and streamed output share one dispatcher.+/// Render markdown `src` as ANSI-styled terminal output to `w`. Tables are+/// aligned straight from `src`, so unlike `Stream` there is no size limit.pub fn render(w: *std.Io.Writer, src: []const u8) !void {- var s: Stream = .{};- try s.feed(w, src);- try s.close(w);+ var fence: Fence = .{};+ var it: LineIterator = std.mem.splitScalar(u8, src, '\n');+ while (it.next()) |line| {+ if (!fence.open and isTableRow(line) and isTableSeparator(it.peek() orelse "")) {+ try renderTable(w, line, &it);+ try w.writeByte('\n');+ continue;+ }+ try fence.emit(w, line, it.peek() != null);+ }}const LineIterator = std.mem.SplitIterator(u8, .scalar);@@ -236,6 +243,25 @@ fn renderFenceRule(w: *std.Io.Writer, opening: bool, delimiter: []const u8) !voitry w.writeAll(ansi.reset);}+const Fence = struct {+ open: bool = false,+ js: js_highlight.State = .normal,++ /// `terminated`: a newline followed the line; a trailing partial line gets none.+ fn emit(self: *Fence, w: *std.Io.Writer, line: []const u8, terminated: bool) std.Io.Writer.Error!void {+ if (isFenceDelimiter(line)) {+ self.open = !self.open;+ self.js = .normal;+ if (!self.open) try w.writeByte('\n');+ try renderFenceRule(w, self.open, line);+ if (terminated) try w.splatByteAll('\n', if (self.open) 2 else 1);+ return;+ }+ try renderLine(w, line, if (self.open) &self.js else null);+ if (terminated) try w.writeByte('\n');+ }+};+fn isTableRow(line: []const u8) bool {const trimmed = std.mem.trimStart(u8, line, " \t");return trimmed.len >= 1 and trimmed[0] == '|';@@ -255,6 +281,9 @@ fn isTableSeparator(line: []const u8) bool {return has_dash and has_pipe;}+/// A streamed table that outgrows this falls back to per-line rendering.+const table_buf_len = 16384;+/// Incremental renderer for streamed deltas: buffers until each newline,/// then renders the completed line. Fence state carries across lines. Table/// rows are withheld and re-rendered aligned once the table ends — alignment@@ -266,9 +295,7 @@ pub const Stream = struct {/// off, withheld tables stay silent until they render.show_table_placeholder: bool = false,len: usize = 0,- in_fence: bool = false,- /// Fenced-code lexer state, carried across lines and chunks.- js_state: js_highlight.State = .normal,+ fence: Fence = .{},/// A line that outgrew `buf` passes through unrendered to its newline.raw: bool = false,/// `held`: one pipe row buffered, pending the separator that confirms a@@ -276,8 +303,7 @@ pub const Stream = struct {mode: enum { text, held, table } = .text,table_len: usize = 0,buf: [4096]u8 = undefined,- /// A table that outgrows this falls back to per-line rendering.- table_buf: [16384]u8 = undefined,+ table_buf: [table_buf_len]u8 = undefined,pub fn feed(self: *Stream, w: *std.Io.Writer, data: []const u8) !void {var rest = data;@@ -339,32 +365,17 @@ pub const Stream = struct {.table => try self.renderBufferedTable(w),}if (self.len == 0) return;- const partial = self.buf[0..self.len];- if (isFenceDelimiter(partial)) {- if (self.in_fence) try w.writeByte('\n');- return renderFenceRule(w, !self.in_fence, partial);- }- try renderLine(w, partial, if (self.in_fence) &self.js_state else null);+ try self.fence.emit(w, self.buf[0..self.len], false);}fn emitLine(self: *Stream, w: *std.Io.Writer, text: []const u8) std.Io.Writer.Error!void {switch (self.mode) {.text => {- if (isFenceDelimiter(text)) {- self.in_fence = !self.in_fence;- self.js_state = .normal;- if (!self.in_fence) try w.writeByte('\n');- try renderFenceRule(w, self.in_fence, text);- try w.writeByte('\n');- if (self.in_fence) try w.writeByte('\n');- return;- }- if (!self.in_fence and isTableRow(text) and self.appendTableLine(text)) {+ if (!self.fence.open and isTableRow(text) and self.appendTableLine(text)) {self.mode = .held;return;}- try renderLine(w, text, if (self.in_fence) &self.js_state else null);- try w.writeByte('\n');+ try self.fence.emit(w, text, true);},.held => {if (isTableSeparator(text) and self.appendTableLine(text)) {@@ -549,12 +560,14 @@ fn renderInlineStyled(w: *std.Io.Writer, text: []const u8, active: ?*const Stylecontinue;}},- '[' => if (std.mem.indexOfPos(u8, text, i + 1, "](")) |mid| {- if (std.mem.indexOfScalarPos(u8, text, mid + 2, ')')) |end| {- try renderLink(w, text[i + 1 .. mid], text[mid + 2 .. end]);- try Style.applyOpt(active, w);- i = end + 1;- continue;+ // An image shows as its alt-text link; the `!` means nothing on a terminal.+ '!', '[' => |ch| {+ const open = if (ch == '!') i + 1 else i;+ if (open < text.len and text[open] == '[') {+ if (try renderLinkAt(w, text, open, active)) |end| {+ i = end;+ continue;+ }}},else => {},@@ -626,11 +639,10 @@ fn styled(w: *std.Io.Writer, inner: []const u8, style: []const u8) !void {try w.writeAll(ansi.reset);}+/// CommonMark's rule: any ASCII punctuation. Covers what the page dumper+/// escapes and leaves `C:\Users` alone.fn isEscapable(c: u8) bool {- return switch (c) {- '*', '_', '`', '~', '[', ']', '(', ')', '|', '\\' => true,- else => false,- };+ return c >= '!' and c <= '~' and !std.ascii.isAlphanumeric(c);}/// A left-trimmed line of 3+ identical `-`, `*` or `_` markers — the whole@@ -645,13 +657,20 @@ fn isHorizontalRule(line: []const u8) bool {return true;}-fn renderLink(w: *std.Io.Writer, label: []const u8, url: []const u8) !void {- // OSC 8 makes the label clickable where supported and is ignored elsewhere;- // the trailing dim url is the fallback for terminals without OSC 8.+/// Returns the index past the `)`, or null (nothing written) if unterminated.+fn renderLinkAt(w: *std.Io.Writer, text: []const u8, open: usize, active: ?*const Style) std.Io.Writer.Error!?usize {+ const mid = std.mem.indexOfPos(u8, text, open + 1, "](") orelse return null;+ const close = std.mem.indexOfScalarPos(u8, text, mid + 2, ')') orelse return null;+ try renderLink(w, text[open + 1 .. mid], text[mid + 2 .. close], active);+ return close + 1;+}++/// OSC 8 support can't be queried; like `ls --hyperlink=auto` we assume it+/// on a tty, so the url isn't repeated in the text.+fn renderLink(w: *std.Io.Writer, label: []const u8, url: []const u8, active: ?*const Style) !void {try w.print("\x1b]8;;{s}\x1b\\", .{url});- try styled(w, label, ansi.underline);+ try span(w, label, ansi.underline, active);try w.writeAll("\x1b]8;;\x1b\\");- if (!std.mem.eql(u8, label, url)) try w.print(" {s}({s}){s}", .{ ansi.dim, url, ansi.reset });}const testing = std.testing;@@ -742,18 +761,31 @@ test "md_term: fenced template literal spans lines" {}test "md_term: link" {- // OSC 8 hyperlink around the label, plus a dim fallback url.try expectRender(- "\x1b]8;;https://x.io\x1b\\\x1b[4mLP\x1b[0m\x1b]8;;\x1b\\ \x1b[2m(https://x.io)\x1b[0m",+ "\x1b]8;;https://x.io\x1b\\\x1b[4mLP\x1b[0m\x1b]8;;\x1b\\","[LP](https://x.io)",);- // A bare link (label == url) omits the redundant suffix.+}++test "md_term: link label is inline-rendered" {+ try expectRender(+ "\x1b]8;;u\x1b\\\x1b[4ma \x1b[1mb\x1b[0m\x1b[4m [c]\x1b[0m\x1b]8;;\x1b\\",+ "[a **b** \\[c\\]](u)",+ );try expectRender(- "\x1b]8;;https://x.io\x1b\\\x1b[4mhttps://x.io\x1b[0m\x1b]8;;\x1b\\",- "[https://x.io](https://x.io)",+ "\x1b[1m\x1b]8;;u\x1b\\\x1b[4mx\x1b[0m\x1b[1m\x1b]8;;\x1b\\ y\x1b[0m",+ "**[x](u) y**",);}+test "md_term: image renders as its alt-text link" {+ try expectRender(+ "\x1b]8;;https://x.io/a.png\x1b\\\x1b[4mLogo\x1b[0m\x1b]8;;\x1b\\",+ "",+ );+ try expectRender("wow! [x](y", "wow! [x](y");+}+test "md_term: blockquote" {try expectRender("\x1b[2m│\x1b[0m quoted \x1b[1mnote\x1b[0m", "> quoted **note**");}@@ -791,6 +823,35 @@ test "md_term: tables align columns and style cells" {try expectRender("| just \x1b[1mtext\x1b[0m |", "| just **text** |");}+test "md_term: pipe rows without a separator pass through" {+ try expectRender("| a |\nplain", "| a |\nplain");+ try expectRender("| a |\n", "| a |\n");+}++const big_table = "| A | B |\n|-|-|\n" ++ ("| " ++ "a" ** 16 ++ " | " ++ "b" ** 16 ++ " |\n") ** 500;++test "md_term: batch aligns tables beyond the stream table buffer" {+ try testing.expect(big_table.len > table_buf_len);+ var aw: std.Io.Writer.Allocating = .init(testing.allocator);+ defer aw.deinit();+ try render(&aw.writer, big_table);+ const out = aw.written();++ const pipe = "\x1b[2m│\x1b[0m";+ try testing.expectEqual(0, std.mem.count(u8, out, "| a"));+ try testing.expectEqual(500, std.mem.count(u8, out, pipe ++ " " ++ "a" ** 16 ++ " " ++ pipe));+ try testing.expect(std.mem.indexOf(u8, out, "\x1b[2m├" ++ "─" ** 18 ++ "┼" ++ "─" ** 18 ++ "┤\x1b[0m\n") != null);+}++test "md_term: stream falls back to raw rows past its table buffer" {+ var aw: std.Io.Writer.Allocating = .init(testing.allocator);+ defer aw.deinit();+ var s: Stream = .{};+ try s.feed(&aw.writer, big_table);+ try s.close(&aw.writer);+ try testing.expectEqual(500, std.mem.count(u8, aw.written(), "| a"));+}+test "md_term: overwide table falls back to verbatim rows" {const header = "|a" ** 17 ++ "|";const sep = "|-" ** 17 ++ "|";@@ -893,6 +954,7 @@ test "md_term: stream closing fence at message end still draws its rule" {test "md_term: backslash escapes" {try expectRender("a * b", "a \\* b");+ try expectRender("Part 8 - {x} #1 ok!", "Part 8 \\- \\{x\\} \\#1 ok\\!");try expectRender("keep \\d and C:\\Users", "keep \\d and C:\\Users");}