Lightpanda: the headless browser designed for AI and automation
agent: keep the full tool list in prompt turns, fewer prompt-parse rules
Adrià Arrufatb5e939b54425
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zigindex 1e1df0755..74b516b34 100644--- a/src/agent/Agent.zig+++ b/src/agent/Agent.zig@@ -673,7 +673,7 @@ fn runTurn(self: *Agent, input: TurnInput) bool {self.terminal.printInfo("(no response from model)", .{});}if (self.prompt_target) |target| {- if (target.data) |d| self.printData(target.tool, d);+ if (target.data) |d| self.printData(target.cmd.tool, d);}self.conversation.prune();return true;@@ -782,7 +782,7 @@ fn runRepl(self: *Agent) void {continue :repl;}const prompt = promptCommandText(aa, p) catch continue :repl;- self.prompt_target = .{ .tool = p.tool, .pinned = p.args, .arena = aa };+ self.prompt_target = .{ .cmd = p, .arena = aa };defer self.prompt_target = null;_ = self.runTurn(.{ .prompt = prompt, .record_comment = line, .capture_for_save = true, .label = label });},@@ -1193,28 +1193,13 @@ const SavedCall = struct {/// Active `/click the sign in…`-style turn: one target call, read-only tools only.const PromptTarget = struct {- tool: BrowserTool,- pinned: ?std.json.Value,+ cmd: Command.Prompt,/// REPL line arena; outlives the turn.arena: std.mem.Allocator,done: bool = false,data: ?[]const u8 = null,};-const prompt_read_tools = [_]BrowserTool{ .tree, .findElement, .nodeDetails, .interactiveElements, .detectForms, .markdown, .html };--fn isPromptReadTool(tool: BrowserTool) bool {- return std.mem.findScalar(BrowserTool, &prompt_read_tools, tool) != null;-}--fn promptTools(arena: std.mem.Allocator, target: BrowserTool) ![]const ProviderTool {- const all = globalTools();- const out = try arena.alloc(ProviderTool, prompt_read_tools.len + 1);- for (prompt_read_tools, out[0..prompt_read_tools.len]) |t, *o| o.* = all[@backingInt(t)];- out[prompt_read_tools.len] = all[@backingInt(target)];- return out;-}-fn urlArg(args: ?std.json.Value) ?std.json.Value {const a = args orelse return null;if (a != .object) return null;@@ -1897,7 +1882,9 @@ fn processUserMessage(self: *Agent, input: TurnInput) !?[]const u8 {ma,.{ .context = @ptrCast(self), .callFn = handleToolCall },.{- .tools = if (self.prompt_target) |t| try promptTools(ma, t.tool) else globalTools(),+ // The full list even in a prompt turn: a narrower one would change+ // the cached prefix; `toolOutcome` refuses the rest.+ .tools = globalTools(),.before_request = .{ .context = @ptrCast(self), .callFn = beforeTurnRequest },.after_response = .{ .context = @ptrCast(self), .callFn = afterResponse },.max_turns = 100,@@ -2146,14 +2133,14 @@ fn toolOutcome(self: *Agent, allocator: std.mem.Allocator, tool_name: []const u8if (self.prompt_target) |t| {if (t.done) return .{ .content = "Error: the request is already done. Reply without calling more tools.", .is_error = true };const tool = std.meta.stringToEnum(BrowserTool, tool_name);- is_target = tool == t.tool;+ is_target = tool == t.cmd.tool;if (is_target) {- if (t.pinned != null) {- args = try pinArgs(self.conversation.arena.allocator(), args, t.pinned);+ if (t.cmd.args != null) {+ args = try pinArgs(self.conversation.arena.allocator(), args, t.cmd.args);saved.args = args;}- } else if (tool == null or !isPromptReadTool(tool.?)) {- return .{ .content = allocator.print("Error: only {s} and the read-only tools are available for this request.", .{@tagName(t.tool)}) catch "Error: tool not available", .is_error = true };+ } else if (tool == null or tool.?.group() != .reading) {+ return .{ .content = allocator.print("Error: only {s} and the read-only tools are available for this request.", .{@tagName(t.cmd.tool)}) catch "Error: tool not available", .is_error = true };} else if (tool.?.navigatesToUrl() and urlArg(args) != null) {return .{ .content = "Error: read the current page; this request must not navigate.", .is_error = true };}@@ -2170,7 +2157,7 @@ fn toolOutcome(self: *Agent, allocator: std.mem.Allocator, tool_name: []const u8if (is_target and !result.is_error) {const t = &self.prompt_target.?;t.done = true;- if (t.tool.producesData()) t.data = try t.arena.dupe(u8, result.text);+ if (t.cmd.tool.producesData()) t.data = try t.arena.dupe(u8, result.text);}const content = capToolOutput(allocator, tool_name, result.text);return .{diff --git a/src/script/Schema.zig b/src/script/Schema.zigindex c588ecaa9..65df02a4e 100644--- a/src/script/Schema.zig+++ b/src/script/Schema.zig@@ -264,9 +264,8 @@ pub fn parsePrompt(self: Schema, arena: std.mem.Allocator, rest_raw: []const u8,const text = std.mem.trimEnd(u8, rest[0..tail_start], &std.ascii.whitespace);if (text.len == 0) return null;- const whole_quoted = (text[0] == '\'' or text[0] == '"') and quotedSpanEnd(text, 0) == text.len;return .{- .prompt = if (whole_quoted) stripQuotes(text) else text,+ .prompt = text,.args = try self.parseArgs(arena, rest[tail_start..], diag, false),};}@@ -485,13 +484,13 @@ fn buildOne(arena: std.mem.Allocator, tool: BrowserTool, td: BrowserTool.Definit}}- info.hints = try buildHints(arena, info.required, info.fields, takesPrompt(tool));+ info.hints = try buildHints(arena, info.required, info.fields, info.leadingPositionalField());std.debug.assert(info.hints.len <= max_hint_slots);return info;}-fn buildHints(arena: std.mem.Allocator, required: []const []const u8, fields: []const FieldEntry, keyword_only: bool) ![]const HintSlot {+fn buildHints(arena: std.mem.Allocator, required: []const []const u8, fields: []const FieldEntry, positional: ?[]const u8) ![]const HintSlot {if (fields.len == 0 and required.len == 0) return &.{};var optional_count: usize = 0;for (fields) |f| {@@ -504,7 +503,7 @@ fn buildHints(arena: std.mem.Allocator, required: []const []const u8, fields: []out[idx] = .{.name = name,.required = true,- .fragment = if (keyword_only) try arena.print("{s}=…", .{name}) else try arena.print("<{s}>", .{name}),+ .fragment = if (positional != null and std.mem.eql(u8, name, positional.?)) try arena.print("<{s}>", .{name}) else try arena.print("{s}=…", .{name}),};idx += 1;}@@ -958,7 +957,7 @@ test "parsePrompt: splits the prompt from the key=value tail" {try testing.expectString("the email field", p.prompt);try testing.expectString("a b", p.args.?.object.get("value").?.string);- try testing.expectString("the email field", (try fill.parsePrompt(aa, "\"the email field\"", null)).?.prompt);+ try testing.expectString("\"the email field\"", (try fill.parsePrompt(aa, "\"the email field\"", null)).?.prompt);try testing.expectString("'a' then b", (try fill.parsePrompt(aa, "'a' then b", null)).?.prompt);try testing.expect((try fill.parsePrompt(aa, "selector=#e value=x", null)) == null);@@ -970,7 +969,7 @@ test "parsePrompt: splits the prompt from the key=value tail" {try testing.expectString("valu", diag.bad_field);const click = Schema.find(Schema.all(), "click").?;- try testing.expectString("the item with id=5", (try click.parsePrompt(aa, "\"the item with id=5\"", null)).?.prompt);+ try testing.expectString("\"the item with id=5\"", (try click.parsePrompt(aa, "\"the item with id=5\"", null)).?.prompt);try testing.expectString("the user's menu", (try click.parsePrompt(aa, "the user's menu", null)).?.prompt);const cause = (try fill.parsePrompt(aa, "'cause value=x", null)).?;try testing.expectString("'cause", cause.prompt);diff --git a/src/script/command.zig b/src/script/command.zigindex 90418ff4e..9a34ccc15 100644--- a/src/script/command.zig+++ b/src/script/command.zig@@ -401,7 +401,7 @@ test "parse: leading text on a prompt tool is a prompt, key=value stays literal"const quoted = (try Command.parse(aa, "/click 'Login'")).prompt;try testing.expectEqual(BrowserTool.click, quoted.tool);- try testing.expectString("Login", quoted.text);+ try testing.expectString("'Login'", quoted.text);try testing.expect(quoted.args == null);try testing.expectString("the user's menu", (try Command.parse(aa, "/click the user's menu")).prompt.text);