Lightpanda: the headless browser designed for AI and automation
agent: improve REPL syntax highlighting and terminal colors
Adrià Arrufat430750909431
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zigindex 6b368bae9..bf7749dbf 100644--- a/src/agent/Agent.zig+++ b/src/agent/Agent.zig@@ -35,6 +35,7 @@ const App = @import("../App.zig");const CDPNode = @import("../cdp/Node.zig");const Conversation = @import("Conversation.zig");const Terminal = @import("Terminal.zig");+const ansi = @import("ansi.zig");const SlashCommand = @import("SlashCommand.zig");const settings = @import("settings.zig");const save = @import("save.zig");@@ -75,7 +76,8 @@ const default_system_prompt = browser_tools.driver_guidance ++\\- If the user asks for account-scoped data (karma, profile, inbox, …)\\ and the page shows you're not signed in, log in proactively (per\\ the Credentials section above) before reporting unavailable.-;+ \\+++ lp.skill.semantics_note;// System prompt of the `/save` command: the save instructions plus the// script skill (`lp.skill`), whose primitives reference is rendered from@@ -596,8 +598,7 @@ fn runRepl(self: *Agent) void {log.debug(.app, "tools loaded", .{ .count = globalTools().len });if (self.ai_client != null) {- const a = Terminal.ansi;- std.debug.print(" model: {s}{s} {s}effort: {s}{s} {s}stream: {s}{s}{s}\n", .{ a.dim, self.model, a.reset, a.dim, @tagName(self.effort), a.reset, a.dim, if (self.stream_enabled) "on" else "off", a.reset });+ std.debug.print(" model: {s}{s} {s}effort: {s}{s} {s}stream: {s}{s}{s}\n", .{ ansi.dim, self.model, ansi.reset, ansi.dim, @tagName(self.effort), ansi.reset, ansi.dim, if (self.stream_enabled) "on" else "off", ansi.reset });}repl: while (true) {diff --git a/src/agent/Spinner.zig b/src/agent/Spinner.zigindex aa548edff..dfd789e85 100644--- a/src/agent/Spinner.zig+++ b/src/agent/Spinner.zig@@ -20,7 +20,7 @@ const std = @import("std");const lp = @import("lightpanda");const log = lp.log;const Terminal = @import("Terminal.zig");-const ansi = Terminal.ansi;+const ansi = @import("ansi.zig");const truncateUtf8 = @import("../string.zig").truncateUtf8;const Spinner = @This();diff --git a/src/agent/Terminal.zig b/src/agent/Terminal.zigindex 764a73ce0..f39c3802d 100644--- a/src/agent/Terminal.zig+++ b/src/agent/Terminal.zig@@ -43,12 +43,15 @@ const style_jsmode = "ps-jsmode";const style_keyword = "ps-keyword";const style_comment = "ps-comment";const style_jsglobal = "ps-jsglobal";+const style_fn = "ps-fn";+const style_method = "ps-method";+const style_type = "ps-type";-pub const ansi = @import("ansi.zig");+const ansi = @import("ansi.zig");/// Command styling shared with the `/help` listing.pub fn highlightCmd(comptime fragment: []const u8) []const u8 {- return ansi.bold ++ ansi.cyan ++ fragment ++ ansi.reset;+ return ansi.bold ++ ansi.teal ++ fragment ++ ansi.reset;}const Verbosity = Config.AgentVerbosity;@@ -70,8 +73,9 @@ js_mode: bool = false,/// active one so JS and normal recall stay separate.history_paths: ?HistoryPaths = null,/// Line-buffered markdown state for streamed assistant deltas; its `close`-/// resets everything so fence state can't leak into the next message.-md_stream: md_term.Stream = .{},+/// resets everything so fence state can't leak into the next message. Only+/// used on the styled (REPL tty) path, hence the placeholder.+md_stream: md_term.Stream = .{ .show_table_placeholder = true },/// Lets the completer/hinter pull dynamic candidates from the `Agent` without/// `Terminal` depending on it (same idiom as `Session.cancel_hook`).@@ -148,7 +152,7 @@ pub fn init(allocator: std.mem.Allocator, history_paths: ?HistoryPaths, verbosit// `ps-*` namespace avoids colliding with isocline's built-in `ic-*` styles.c.ic_style_def(style_slash, "ansi-teal bold");c.ic_style_def(style_string, "ansi-green");- c.ic_style_def(style_var, "ansi-yellow bold");+ c.ic_style_def(style_var, "ansi-yellow");c.ic_style_def(style_url, "ansi-blue underline");c.ic_style_def(style_key, "ansi-blue");c.ic_style_def(style_num, "ansi-magenta");@@ -157,6 +161,9 @@ pub fn init(allocator: std.mem.Allocator, history_paths: ?HistoryPaths, verbositc.ic_style_def(style_keyword, "ansi-blue bold");c.ic_style_def(style_comment, "ansi-darkgray italic");c.ic_style_def(style_jsglobal, "ansi-cyan");+ c.ic_style_def(style_fn, "ansi-teal");+ c.ic_style_def(style_method, "ansi-teal italic");+ c.ic_style_def(style_type, "ansi-cyan");// lighten the ghost/inline-hint color from isocline's default ansi-darkgrayc.ic_style_def("ic-hint", "ansi-color=244");// `!` on an empty prompt toggles JS mode; state callback wired in attachCompleter.@@ -225,7 +232,7 @@ pub fn agentToolDone(self: *Terminal, name: []const u8, args: []const u8, ok: bo} else {std.debug.print("{s}{s}[tool: {s}]{s} {s}\n",- .{ ansi.dim, ansi.cyan, name, ansi.reset, args },+ .{ ansi.dim, ansi.teal, name, ansi.reset, args },);}}@@ -814,21 +821,13 @@ fn highlightBareToken(henv: ?*c.ic_highlight_env_t, text: []const u8, start: usi} else |_| {}}-/// Highlight `$LP_*` tokens appearing from `start` onward.+/// Highlight `$LP_*` tokens appearing from `start` onward. `${…}` is not a+/// prompt substitution form, so interpolation stays off.fn highlightDollarVars(henv: ?*c.ic_highlight_env_t, text: []const u8, start: usize) void {var i = start;- while (i < text.len) {- if (text[i] != '$') {- i += 1;- continue;- }- const tok_start = i;- i = js_highlight.dollarRefEnd(text, i, text.len);- if (i > tok_start + 1) {- c.ic_highlight(henv, @intCast(tok_start), @intCast(i - tok_start), style_var.ptr);- }- // Don't post-step: dollarRefEnd already landed on the char after the- // identifier (or end-of-text); auto-advancing would skip an adjacent `$LP_*`.+ while (js_highlight.nextDollarRef(text, i, text.len, false)) |ref| {+ c.ic_highlight(henv, @intCast(ref.start), @intCast(ref.end - ref.start), style_var.ptr);+ i = ref.end;}}@@ -840,10 +839,13 @@ const IcSink = struct {const style: []const u8 = switch (kind) {.comment => style_comment,.string => style_string,- .variable => style_var,+ .variable, .interpolation => style_var,.number => style_num,.keyword => style_keyword,.global => style_jsglobal,+ .function => style_fn,+ .method => style_method,+ .type_name => style_type,};c.ic_highlight(self.henv, @intCast(start), @intCast(len), style.ptr);}@@ -862,7 +864,7 @@ fn highlightSlashArgs(henv: ?*c.ic_highlight_env_t, text: []const u8, start: usiwhile (skipWhitespace(text, i)) |tok_start| {i = tok_start;if (text[i] == '\'' or text[i] == '"') {- i = js_highlight.scanString(text, i).end;+ i = Schema.quotedSpanEnd(text, i);c.ic_highlight(henv, @intCast(tok_start), @intCast(i - tok_start), style_string.ptr);continue;}@@ -873,7 +875,7 @@ fn highlightSlashArgs(henv: ?*c.ic_highlight_env_t, text: []const u8, start: usii += 1;const val_start = i;if (i < text.len and (text[i] == '\'' or text[i] == '"')) {- i = js_highlight.scanString(text, i).end;+ i = Schema.quotedSpanEnd(text, i);c.ic_highlight(henv, @intCast(val_start), @intCast(i - val_start), style_string.ptr);} else {while (i < text.len and !std.ascii.isWhitespace(text[i])) i += 1;@@ -1123,7 +1125,7 @@ fn renderChoice(header: []const u8, items: []const [:0]const u8, default: ?usizefor (items, 0..) |item, idx| {const on_row = idx == selected;const marker: []const u8 = if (on_row) ">" else " ";- const style: []const u8 = if (on_row) ansi.bold ++ ansi.cyan else "";+ const style: []const u8 = if (on_row) ansi.bold ++ ansi.teal else "";const reset: []const u8 = if (on_row) ansi.reset else "";const default_marker: []const u8 = if (default) |d| (if (d == idx) " (default)" else "") else "";std.debug.print(ansi.clear_line ++ " {s} {s}{s}{s}{s}\r\n", .{ marker, style, item, default_marker, reset });diff --git a/src/agent/ansi.zig b/src/agent/ansi.zigindex c1ab453f2..47f87d129 100644--- a/src/agent/ansi.zig+++ b/src/agent/ansi.zig@@ -22,7 +22,10 @@ pub const dim = "\x1b[2m";pub const italic = "\x1b[3m";pub const underline = "\x1b[4m";pub const strike = "\x1b[9m";-pub const cyan = "\x1b[36m";+// Color names follow isocline's (bbcode_colors.c): teal is the dark cyan+// pair of bright cyan, like maroon/red or olive/yellow.+pub const teal = "\x1b[36m";+pub const cyan = "\x1b[96m";pub const green = "\x1b[32m";pub const yellow = "\x1b[33m";pub const red = "\x1b[31m";diff --git a/src/agent/js_highlight.zig b/src/agent/js_highlight.zigindex b9cfd6338..ce79c8d9b 100644--- a/src/agent/js_highlight.zig+++ b/src/agent/js_highlight.zig@@ -17,8 +17,9 @@// along with this program. If not, see <https://www.gnu.org/licenses/>.const std = @import("std");+const string = @import("../string.zig");-pub const Kind = enum { comment, string, variable, number, keyword, global };+pub const Kind = enum { comment, string, variable, interpolation, number, keyword, global, function, method, type_name };/// Carried between calls because fenced code is tokenized one line at a time:/// block comments and template literals outlive a line boundary.@@ -26,18 +27,11 @@ pub const State = enum { normal, block_comment, template };pub const StringSpan = struct { end: usize, closed: bool };-/// Scan the quoted run opening at `text[start]`, including the triple-delimiter-/// form. Escapes are not honored, matching the prompt highlighter's behavior.+/// Scan the quoted run opening at `text[start]`. Escapes are not honored —+/// good enough for coloring, not parsing.pub fn scanString(text: []const u8, start: usize) StringSpan {if (start >= text.len) return .{ .end = start, .closed = false };- const ch = text[start];- if (start + 2 < text.len and text[start + 1] == ch and text[start + 2] == ch) {- const delim = text[start .. start + 3];- const close = std.mem.indexOfPos(u8, text, start + 3, delim) orelse- return .{ .end = text.len, .closed = false };- return .{ .end = close + 3, .closed = true };- }- const close = std.mem.indexOfScalarPos(u8, text, start + 1, ch) orelse+ const close = std.mem.indexOfScalarPos(u8, text, start + 1, text[start]) orelsereturn .{ .end = text.len, .closed = false };return .{ .end = close + 1, .closed = true };}@@ -45,12 +39,36 @@ pub fn scanString(text: []const u8, start: usize) StringSpan {/// Index just past the `$name` ref opening at `text[start]` (scanning within/// `text[..end]`), or `start + 1` when the `$` is bare. A ref is a `$` followed/// by at least one `[A-Za-z0-9_]`.-pub fn dollarRefEnd(text: []const u8, start: usize, end: usize) usize {+fn dollarRefEnd(text: []const u8, start: usize, end: usize) usize {var i = start + 1;while (i < end and (std.ascii.isAlphanumeric(text[i]) or text[i] == '_')) i += 1;return i;}+pub const DollarRef = struct { start: usize, end: usize, kind: Kind };++/// Next `$name` (`.variable`) ref at or after `from` within `text[..end]`,+/// or null; bare `$`s are skipped. `interpolation` additionally recognizes+/// `${…}` (`.interpolation`, unclosed runs to `end`) — only template+/// literals interpolate, so callers pass their quote kind.+pub fn nextDollarRef(text: []const u8, from: usize, end: usize, interpolation: bool) ?DollarRef {+ var i = from;+ while (i < end) {+ if (text[i] != '$') {+ i += 1;+ continue;+ }+ if (interpolation and i + 1 < end and text[i + 1] == '{') {+ const close = std.mem.indexOfScalarPos(u8, text[0..end], i + 2, '}');+ return .{ .start = i, .end = if (close) |c| c + 1 else end, .kind = .interpolation };+ }+ const ref_end = dollarRefEnd(text, i, end);+ if (ref_end > i + 1) return .{ .start = i, .end = ref_end, .kind = .variable };+ i += 1;+ }+ return null;+}+/// Tokenize `text` as JavaScript starting from `state`, reporting spans to/// `sink.emit(start, len, kind)` and returning the state at end of input./// Spans arrive in order and never overlap — the gaps between them are plain@@ -69,7 +87,7 @@ pub fn tokenize(text: []const u8, state: State, sink: anytype) State {.template => {const close = std.mem.indexOfScalarPos(u8, text, 0, '`');i = if (close) |p| p + 1 else text.len;- emitString(text, 0, i, sink);+ emitString(text, 0, i, true, sink);if (close == null) return .template;},.normal => {},@@ -92,7 +110,7 @@ pub fn tokenize(text: []const u8, state: State, sink: anytype) State {}if (ch == '\'' or ch == '"' or ch == '`') {const span = scanString(text, i);- emitString(text, i, span.end, sink);+ emitString(text, i, span.end, ch == '`', sink);i = span.end;// Only a template literal may legally continue on the next line.if (!span.closed and ch == '`') return .template;@@ -116,11 +134,19 @@ pub fn tokenize(text: []const u8, state: State, sink: anytype) State {i += 1;while (i < text.len and isIdChar(text[i])) i += 1;const tok = text[start..i];- if (isKeyword(tok)) {+ if (string.isOneOf(tok, &keywords)) {sink.emit(start, i - start, .keyword);- } else if (isGlobal(tok) and (start == 0 or text[start - 1] != '.')) {+ } else if (string.isOneOf(tok, &globals) and (start == 0 or text[start - 1] != '.')) {// `.document` is a property access, not the globalsink.emit(start, i - start, .global);+ } else if (i < text.len and text[i] == '(') {+ const kind: Kind = if (std.ascii.isUpper(tok[0]))+ .type_name+ else if (start > 0 and text[start - 1] == '.')+ .method+ else+ .function;+ sink.emit(start, i - start, kind);}continue;}@@ -132,21 +158,12 @@ pub fn tokenize(text: []const u8, state: State, sink: anytype) State {/// Emit `text[start..end]` as string spans split around any `$name` refs —/// spans must never overlap, so a sequential writer (ANSI) works as well as/// isocline's last-write-wins cell painting.-fn emitString(text: []const u8, start: usize, end: usize, sink: anytype) void {+fn emitString(text: []const u8, start: usize, end: usize, interpolation: bool, sink: anytype) void {var seg = start;- var i = start;- while (i < end) {- if (text[i] != '$') {- i += 1;- continue;- }- const tok = i;- i = dollarRefEnd(text, i, end);- if (i > tok + 1) {- if (tok > seg) sink.emit(seg, tok - seg, .string);- sink.emit(tok, i - tok, .variable);- seg = i;- }+ while (nextDollarRef(text, seg, end, interpolation)) |ref| {+ if (ref.start > seg) sink.emit(seg, ref.start - seg, .string);+ sink.emit(ref.start, ref.end - ref.start, ref.kind);+ seg = ref.end;}if (end > seg) sink.emit(seg, end - seg, .string);}@@ -164,20 +181,6 @@ const keywords = [_][]const u8{// at the prompt that they're in scope.const globals = [_][]const u8{ "document", "window", "globalThis", "console", "lp" };-fn isKeyword(tok: []const u8) bool {- for (keywords) |kw| {- if (std.mem.eql(u8, kw, tok)) return true;- }- return false;-}--fn isGlobal(tok: []const u8) bool {- for (globals) |g| {- if (std.mem.eql(u8, g, tok)) return true;- }- return false;-}-fn isIdChar(ch: u8) bool {return std.ascii.isAlphanumeric(ch) or ch == '_' or ch == '$' or ch >= 0x80;}@@ -221,11 +224,28 @@ test "js_highlight: comments and strings" {try expectTokens("string:'hi'", "'hi'");}+test "js_highlight: calls color by identifier case" {+ try expectTokens("function:markdown string:'x'", "markdown('x')");+ try expectTokens("global:console method:log", "console.log(x)");+ try expectTokens("keyword:new type_name:URL", "new URL(u)");+ // A keyword before `(` stays a keyword; a bare identifier stays plain.+ try expectTokens("keyword:if", "if (x)");+ try expectTokens("", "markdown");+}+test "js_highlight: $refs inside strings do not overlap" {try expectTokens("string:'a variable:$LP_KEY string:'", "'a $LP_KEY'");try expectTokens("variable:$LP_KEY", "$LP_KEY");}+test "js_highlight: template interpolations split string spans" {+ try expectTokens("string:`a interpolation:${x.y} string:`", "`a ${x.y}`");+ // Unclosed interpolation runs to end of line.+ try expectTokens("string:`a interpolation:${x", "`a ${x");+ // Only template literals interpolate.+ try expectTokens("string:'a ${x}'", "'a ${x}'");+}+test "js_highlight: block comment spans lines" {var sink: TestSink = .{ .text = "/* open" };defer sink.buf.deinit(testing.allocator);diff --git a/src/agent/md_term.zig b/src/agent/md_term.zigindex 989dfacca..e4f1d1449 100644--- a/src/agent/md_term.zig+++ b/src/agent/md_term.zig@@ -20,33 +20,12 @@ 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`.+/// Render markdown `src` as ANSI-styled terminal output to `w` — one-shot+/// wrapper over `Stream`, so batch and streamed output share one dispatcher.pub fn render(w: *std.Io.Writer, src: []const u8) !void {- var in_fence = false;- var js: js_highlight.State = .normal;- var wrote_any = false;- var it = std.mem.splitScalar(u8, src, '\n');- while (it.next()) |line| {- if (wrote_any) try w.writeByte('\n');- wrote_any = true;- if (isFenceDelimiter(line)) {- in_fence = !in_fence;- js = .normal;- // Pad the block interior: blank line after the opening rule and- // before the closing one.- if (!in_fence) try w.writeByte('\n');- try renderFenceRule(w, in_fence, line);- if (in_fence) try w.writeByte('\n');- continue;- }- if (!in_fence and isTableRow(line)) {- if (it.peek()) |next| if (isTableSeparator(next)) {- try renderTable(w, line, &it);- continue;- };- }- try renderLine(w, line, if (in_fence) &js else null);- }+ var s: Stream = .{};+ try s.feed(w, src);+ try s.close(w);}const LineIterator = std.mem.SplitIterator(u8, .scalar);@@ -62,22 +41,8 @@ const clear_placeholder = "\r" ++ ansi.clear_line;/// count codepoints, so double-width glyphs (CJK, emoji) may misalign.fn renderTable(w: *std.Io.Writer, header: []const u8, it: *LineIterator) !void {var widths: [max_table_columns]usize = @splat(0);- var ncols: ?usize = measureRow(header, &widths);- if (ncols != null) {- var scan = it.*;- _ = scan.next();- while (scan.peek()) |line| {- if (!isTableRow(line)) break;- _ = scan.next();- const n = measureRow(line, &widths) orelse {- ncols = null;- break;- };- ncols = @max(ncols.?, n);- }- }- const cols = ncols orelse 0;- if (cols == 0) return renderTableVerbatim(w, header, it);+ const cols = measureTable(header, it.*, &widths) orelse+ return renderTableVerbatim(w, header, it);try emitRow(w, header, widths[0..cols], true);_ = it.next();@@ -107,14 +72,29 @@ fn renderTableVerbatim(w: *std.Io.Writer, header: []const u8, it: *LineIterator)}}+/// Per-column visible widths for the whole table (`it` positioned on the+/// separator row), or null when it can't be aligned: no cells, too many+/// columns, or a cell overflowing the rendering buffer.+fn measureTable(header: []const u8, it: LineIterator, widths: *[max_table_columns]usize) ?usize {+ var cols = measureRow(header, widths, true) orelse return null;+ var scan = it;+ _ = scan.next();+ while (scan.peek()) |line| {+ if (!isTableRow(line)) break;+ _ = scan.next();+ cols = @max(cols, measureRow(line, widths, false) orelse return null);+ }+ return if (cols == 0) null else cols;+}+/// Column count of `row`, or null when the table can't be aligned (too many-/// columns, or a cell overflowing the measuring buffer).-fn measureRow(row: []const u8, widths: *[max_table_columns]usize) ?usize {+/// columns, or a cell overflowing the rendering buffer).+fn measureRow(row: []const u8, widths: *[max_table_columns]usize, is_header: bool) ?usize {var cells = cellIterator(row);var col: usize = 0;while (cells.next()) |cell| {if (col == max_table_columns) return null;- const width = cellDisplayWidth(cell) orelse return null;+ const width = cellDisplayWidth(cell, is_header) orelse return null;widths[col] = @max(widths[col], width);col += 1;}@@ -129,20 +109,14 @@ fn emitRow(w: *std.Io.Writer, row: []const u8, widths: []const usize, is_header:const cell = cells.next() orelse "";var used: usize = 0;if (cell.len > 0) {- // Render once into scratch so `used` is measured from the exact- // bytes written. The header's bold re-applies add escapes on top- // of what measureRow saw, hence the extra headroom.- var buf: [1024]u8 = undefined;+ // Render into scratch so `used` is measured from the exact bytes+ // written. measureRow rendered the identical bytes into an+ // identically-sized buffer, so this cannot overflow here.+ var buf: [cell_buf_len]u8 = undefined;var fw: std.Io.Writer = .fixed(&buf);- if (renderCell(&fw, cell, is_header)) {- try w.writeAll(fw.buffered());- used = visibleWidth(fw.buffered());- } else |_| {- // Overflow, possible only for a marker-dense header cell:- // keep the content, sacrifice the padding.- try renderCell(w, cell, is_header);- used = width;- }+ try renderCell(&fw, cell, is_header);+ try w.writeAll(fw.buffered());+ used = visibleWidth(fw.buffered());}try w.splatByteAll(' ', width - used + 1);}@@ -194,10 +168,12 @@ fn cellIterator(row: []const u8) CellIterator {return .{ .row = row, .pos = if (first_pipe) |p| p + 1 else 0 };}-fn cellDisplayWidth(cell: []const u8) ?usize {- var buf: [512]u8 = undefined;+const cell_buf_len = 1024;++fn cellDisplayWidth(cell: []const u8, is_header: bool) ?usize {+ var buf: [cell_buf_len]u8 = undefined;var fw: std.Io.Writer = .fixed(&buf);- renderInline(&fw, cell) catch return null;+ renderCell(&fw, cell, is_header) catch return null;return visibleWidth(fw.buffered());}@@ -237,14 +213,15 @@ fn isFenceDelimiter(line: []const u8) bool {return std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), "```");}-const fence_rule_width = 24;+/// Shared by the fence rules and the `---` horizontal rule so they line up.+const rule_width = 24;/// Dim `╭── lang ───` / `╰─────` rule marking where a fenced code block starts/// and ends. The opening rule carries the fence's info-string language, if any.fn renderFenceRule(w: *std.Io.Writer, opening: bool, delimiter: []const u8) !void {try w.writeAll(ansi.dim);try w.writeAll(if (opening) "╭" else "╰");- var fill: usize = fence_rule_width - 1;+ var fill: usize = rule_width - 1;if (opening) {const info = std.mem.trim(u8, std.mem.trimLeft(u8, delimiter, " \t`"), " \t");const lang = info[0 .. std.mem.indexOfAny(u8, info, " \t") orelse info.len];@@ -283,6 +260,11 @@ fn isTableSeparator(line: []const u8) bool {/// rows are withheld and re-rendered aligned once the table ends — alignment/// needs the whole table, so it can't stream row by row.pub const Stream = struct {+ /// Show a progress marker while a streamed table is withheld, erased in+ /// place before the table renders. Only for streaming to an interactive+ /// terminal (the erase assumes the cursor still sits on the marker);+ /// 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.@@ -337,6 +319,7 @@ pub const Stream = struct {/// Flush any withheld table and a trailing partial line (no newline is/// written for it), then reset all state for the next message.pub fn close(self: *Stream, w: *std.Io.Writer) !void {+ defer self.* = .{ .show_table_placeholder = self.show_table_placeholder };// A message often ends inside a table: adopt the partial last row.if (self.len > 0 and !self.raw) {const partial = self.buf[0..self.len];@@ -355,19 +338,13 @@ pub const Stream = struct {.held => try self.releaseHeldRow(w),.table => try self.renderBufferedTable(w),}+ if (self.len == 0) return;const partial = self.buf[0..self.len];- const in_fence = self.in_fence;- var js = self.js_state;- self.len = 0;- self.raw = false;- self.in_fence = false;- self.js_state = .normal;- if (partial.len == 0) return;if (isFenceDelimiter(partial)) {- if (in_fence) try w.writeByte('\n');- return renderFenceRule(w, !in_fence, partial);+ if (self.in_fence) try w.writeByte('\n');+ return renderFenceRule(w, !self.in_fence, partial);}- try renderLine(w, partial, if (in_fence) &js else null);+ try renderLine(w, partial, if (self.in_fence) &self.js_state else null);}fn emitLine(self: *Stream, w: *std.Io.Writer, text: []const u8) std.Io.Writer.Error!void {@@ -394,7 +371,7 @@ pub const Stream = struct {self.mode = .table;// Withholding goes quiet; leave a marker until the// table renders over it.- try w.writeAll(table_placeholder);+ if (self.show_table_placeholder) try w.writeAll(table_placeholder);return;}try self.releaseHeldRow(w);@@ -422,7 +399,7 @@ pub const Stream = struct {}fn renderBufferedTable(self: *Stream, w: *std.Io.Writer) std.Io.Writer.Error!void {- try w.writeAll(clear_placeholder);+ if (self.show_table_placeholder) try w.writeAll(clear_placeholder);var it: LineIterator = std.mem.splitScalar(u8, self.tableText(), '\n');const header = it.first();try renderTable(w, header, &it);@@ -431,7 +408,7 @@ pub const Stream = struct {}fn flushTableUnaligned(self: *Stream, w: *std.Io.Writer) std.Io.Writer.Error!void {- try w.writeAll(clear_placeholder);+ if (self.show_table_placeholder) try w.writeAll(clear_placeholder);var it = std.mem.splitScalar(u8, self.tableText(), '\n');while (it.next()) |line| {try renderLine(w, line, null);@@ -482,7 +459,7 @@ fn renderLine(w: *std.Io.Writer, line: []const u8, js: ?*js_highlight.State) !vo// Dashed, unlike the solid fence rules, so adjacent ones read differently.if (isHorizontalRule(trimmed)) {- try styled(w, "┄" ** 24, ansi.dim);+ try styled(w, "┄" ** rule_width, ansi.dim);return;}@@ -546,7 +523,7 @@ fn renderInlineStyled(w: *std.Io.Writer, text: []const u8, active: ?*const Stylecontinue;},'`' => if (std.mem.indexOfPos(u8, text, i + 1, "`")) |end| {- try styled(w, text[i + 1 .. end], ansi.cyan);+ try styled(w, text[i + 1 .. end], ansi.teal);try Style.applyOpt(active, w);i = end + 1;continue;@@ -607,10 +584,12 @@ const JsSink = struct {return switch (kind) {.comment => ansi.dim ++ ansi.italic,.string => ansi.green,- .variable => ansi.yellow ++ ansi.bold,+ .variable, .interpolation => ansi.yellow,.number => ansi.magenta,.keyword => ansi.blue ++ ansi.bold,- .global => ansi.cyan,+ .global, .type_name => ansi.cyan,+ .function => ansi.teal,+ .method => ansi.teal ++ ansi.italic,};}@@ -797,7 +776,7 @@ test "md_term: tables align columns and style cells" {try expectRender(pipe ++ " " ++ B ++ "Tool" ++ R ++ " " ++ pipe ++ " " ++ B ++ "Use" ++ R ++ " " ++ pipe ++ "\n" ++D ++ "├──────┼─────┤" ++ R ++ "\n" ++- pipe ++ " " ++ C ++ "goto" ++ R ++ " " ++ pipe ++ " nav " ++ pipe,+ pipe ++ " " ++ C ++ "goto" ++ R ++ " " ++ pipe ++ " nav " ++ pipe ++ "\n","| Tool | Use |\n|--------|-----|\n| `goto` | nav |",);// Gemini-style unpadded source: columns are padded to the widest@@ -805,7 +784,7 @@ test "md_term: tables align columns and style cells" {try expectRender(pipe ++ " " ++ B ++ "Tool" ++ R ++ " " ++ pipe ++ " " ++ B ++ "Usage" ++ R ++ " " ++ pipe ++ "\n" ++D ++ "├──────┼─────────────┤" ++ R ++ "\n" ++- pipe ++ " goto " ++ pipe ++ " " ++ B ++ "Nav:" ++ R ++ " to url " ++ pipe,+ pipe ++ " goto " ++ pipe ++ " " ++ B ++ "Nav:" ++ R ++ " to url " ++ pipe ++ "\n","| Tool | Usage |\n| :--- | :--- |\n| goto | **Nav:** to url |",);// A pipe line without a separator row underneath is not a table.@@ -816,7 +795,7 @@ test "md_term: overwide table falls back to verbatim rows" {const header = "|a" ** 17 ++ "|";const sep = "|-" ** 17 ++ "|";try expectRender(- "\x1b[1m" ++ header ++ "\x1b[0m\n\x1b[2m" ++ sep ++ "\x1b[0m",+ "\x1b[1m" ++ header ++ "\x1b[0m\n\x1b[2m" ++ sep ++ "\x1b[0m\n",header ++ "\n" ++ sep,);}@@ -844,7 +823,7 @@ test "md_term: stream withholds tables and renders them aligned" {var aw: std.Io.Writer.Allocating = .init(testing.allocator);defer aw.deinit();- var s: Stream = .{};+ var s: Stream = .{ .show_table_placeholder = true };try s.feed(&aw.writer, "| Tool | Use |\n| :--- | :-");try testing.expectEqualStrings("", aw.written());try s.feed(&aw.writer, "-- |\n| `goto` | nav |\ndone\n");@@ -866,7 +845,7 @@ test "md_term: stream table ending at close adopts the partial row" {var aw: std.Io.Writer.Allocating = .init(testing.allocator);defer aw.deinit();- var s: Stream = .{};+ var s: Stream = .{ .show_table_placeholder = true };try s.feed(&aw.writer, "| A | B |\n|-|-|\n| x | y |");try s.close(&aw.writer);try testing.expectEqualStrings(diff --git a/src/agent/welcome.zig b/src/agent/welcome.zigindex 403a7c7a3..517f045e5 100644--- a/src/agent/welcome.zig+++ b/src/agent/welcome.zig@@ -22,7 +22,7 @@const std = @import("std");const lp = @import("lightpanda");-const Terminal = @import("Terminal.zig");+const ansi = @import("ansi.zig");// A pre-colored (truecolor braille) panda. Each line carries its own ANSI and// resets at the end, so it prints as-is; non-empty lines are the visible rows.@@ -82,30 +82,28 @@ comptime {/// Prints the welcome banner: the logo on the left with the title and command/// hints beside it, vertically centered. `llm_active` picks the tagline.pub fn print(llm_active: bool) void {- const a = Terminal.ansi;-var version_buf: [192]u8 = undefined;- const version: []const u8 = std.fmt.bufPrint(&version_buf, a.dim ++ "{s}" ++ a.reset, .{lp.build_config.version}) catch "";+ const version: []const u8 = std.fmt.bufPrint(&version_buf, ansi.dim ++ "{s}" ++ ansi.reset, .{lp.build_config.version}) catch "";var lines: [9][]const u8 = undefined;var n: usize = 0;- lines[n] = a.bold ++ "Lightpanda Agent" ++ a.reset;+ lines[n] = ansi.bold ++ "Lightpanda Agent" ++ ansi.reset;n += 1;lines[n] = version;n += 1;lines[n] = "";n += 1;if (llm_active) {- lines[n] = a.italic ++ banner_tagline_llm ++ a.reset;+ lines[n] = ansi.italic ++ banner_tagline_llm ++ ansi.reset;n += 1;} else {- lines[n] = a.italic ++ banner_tagline_basic ++ a.reset;+ lines[n] = ansi.italic ++ banner_tagline_basic ++ ansi.reset;n += 1;- lines[n] = a.dim ++ banner_setup ++ a.reset;+ lines[n] = ansi.dim ++ banner_setup ++ ansi.reset;n += 1;}inline for (banner_hints) |t| {- lines[n] = a.dim ++ t ++ a.reset;+ lines[n] = ansi.dim ++ t ++ ansi.reset;n += 1;}const text = lines[0..n];diff --git a/src/script/Schema.zig b/src/script/Schema.zigindex d2cf955de..504419bb7 100644--- a/src/script/Schema.zig+++ b/src/script/Schema.zig@@ -590,6 +590,21 @@ pub fn hasUnclosedTripleQuote(input: []const u8) bool {return open != null;}+/// End of the quoted run opening at `input[start]`, for the REPL's slash-arg+/// highlighting: recognizes this grammar's `'''…'''` triple delimiter+/// alongside plain quotes, mirroring `tokenize`'s quoting. Escapes are not+/// honored (`tokenize` rejects them); an unterminated run extends to the end.+pub fn quotedSpanEnd(input: []const u8, start: usize) usize {+ const ch = input[start];+ if (start + 2 < input.len and input[start + 1] == ch and input[start + 2] == ch) {+ const close = std.mem.indexOfPos(u8, input, start + 3, input[start .. start + 3]) orelse+ return input.len;+ return close + 3;+ }+ const close = std.mem.indexOfScalarPos(u8, input, start + 1, ch) orelse return input.len;+ return close + 1;+}+/// `body=true`: string is emitted as a `'''…'''` block (newlines OK)./// `body=false`: single-line kv quoting (no newlines representable).pub fn quotableInline(s: []const u8, body: bool) bool {@@ -835,6 +850,13 @@ test "tokenize: even-count backslashes before close-quote are literal" {try testing.expectString("value=\"\\\\\"", tokens[0]);}+test "quotedSpanEnd" {+ try testing.expectEqual(@as(usize, 4), quotedSpanEnd("'ab' x", 0));+ try testing.expectEqual(@as(usize, 9), quotedSpanEnd("'''a b''' x", 0));+ try testing.expectEqual(@as(usize, 5), quotedSpanEnd("'open", 0));+ try testing.expectEqual(@as(usize, 7), quotedSpanEnd("'''open", 0));+}+test "hasUnclosedTripleQuote" {try testing.expect(!hasUnclosedTripleQuote(""));try testing.expect(!hasUnclosedTripleQuote("/goto https://x"));diff --git a/src/script/skill.zig b/src/script/skill.zigindex e5cb0519c..307b1cd10 100644--- a/src/script/skill.zig+++ b/src/script/skill.zig@@ -30,6 +30,20 @@ const Schema = @import("Schema.zig");pub const name = "pandascript";pub const description = "Write PandaScript agent scripts (.js) — Lightpanda's replayable browser-automation format, run token-free with `lightpanda agent script.js`.";+/// Semantics summary for the agent's default system prompt, so the agent+/// answers user questions about PandaScript from these rules instead of+/// inventing them. Kept here beside the full skill text ("Mental model"+/// below) so the two stay in sync when the Runtime contract changes.+pub const semantics_note =+ \\- PandaScript (the saved-script language of `/save`): scripts run+ \\ wrapped in an async function, so a script's output is ONLY its+ \\ top-level `return <value>` (objects/arrays printed as JSON) — a bare+ \\ trailing expression is NOT printed. `Page` is the only global:+ \\ `const page = new Page(); await page.goto(url);` then synchronous+ \\ `page.extract({...})`. Only page-side `evaluate` yields the value of+ \\ a bare trailing expression.+;+/// The skill body (no frontmatter). Lazily rendered once, process lifetime.pub fn text() []const u8 {text_once.call();