Skip to content

Lightpanda: the headless browser designed for AI and automation

agent: trim prompt-path comments, keep takesPrompt exhaustive

Adrià Arrufatcd1bd1e30c6a
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zig
index bc5ecea6d..6b132303a 100644
--- a/src/agent/Agent.zig
+++ b/src/agent/Agent.zig
@@ -193,7 +193,6 @@ synthetic_tool_call_id: u32 = 0,
/// and the navigation a read tool's `url` made.
save_calls: std.ArrayList(SavedCall) = .empty,
capturing_for_save: bool = false,
-/// Set while a prompt command (`/click the sign in button`) runs its turn.
prompt_target: ?PromptTarget = null,
/// Aggregate Anthropic/OpenAI/Gemini token usage across every model call.
/// Printed as a structured `$usage ...` line on stderr at the end of `--task`
@@ -674,7 +673,6 @@ fn runTurn(self: *Agent, input: TurnInput) bool {
self.terminal.printInfo("(no response from model)", .{});
}
if (self.prompt_target) |target| {
- // The model only summarizes; the data is what `/extract` would print.
if (target.data) |d| self.printData(target.tool, d);
}
self.conversation.prune();
@@ -1189,25 +1187,20 @@ const PathAndMode = struct { path: []const u8, mode: save.Mode };
const SavedCall = struct {
selector: ?[]const u8 = null,
navigated: bool = false,
- /// The arguments the call ran with when they differ from the model's
- /// (pinned prompt fields), in the conversation arena.
+ /// Pinned prompt args as the call ran (conversation arena).
args: ?std.json.Value = null,
};
+/// Active `/click the sign in…`-style turn: one target call, read-only tools only.
const PromptTarget = struct {
tool: BrowserTool,
- /// Fields typed beside the prompt; they override whatever the model passes.
pinned: ?std.json.Value,
- /// Scoped to the REPL line, which outlives the turn.
+ /// REPL line arena; outlives the turn.
arena: std.mem.Allocator,
- /// Set once a target call succeeds; later tool calls are refused, so the
- /// turn records exactly one.
done: bool = false,
- /// That call's output, kept for tools whose output is the answer.
data: ?[]const u8 = null,
};
-// What the model may inspect the page with before its one target call.
const prompt_read_tools = [_]BrowserTool{ .tree, .findElement, .nodeDetails, .interactiveElements, .detectForms, .markdown, .html };
fn isPromptReadTool(tool: BrowserTool) bool {
@@ -1222,7 +1215,6 @@ fn promptTools(arena: std.mem.Allocator, target: BrowserTool) ![]const ProviderT
return out;
}
-/// The call's `url` argument when it is a non-empty string.
fn urlArg(args: ?std.json.Value) ?std.json.Value {
const a = args orelse return null;
if (a != .object) return null;
@@ -1231,7 +1223,6 @@ fn urlArg(args: ?std.json.Value) ?std.json.Value {
return url;
}
-/// `args` with every `pinned` field set over it.
fn pinArgs(arena: std.mem.Allocator, args: ?std.json.Value, pinned: ?std.json.Value) error{OutOfMemory}!?std.json.Value {
const p = pinned orelse return args;
var obj: std.json.ObjectMap = if (args) |a| (if (a == .object) try a.object.clone(arena) else .empty) else .empty;
@@ -1240,8 +1231,8 @@ fn pinArgs(arena: std.mem.Allocator, args: ?std.json.Value, pinned: ?std.json.Va
return .{ .object = obj };
}
-/// The user turn for a prompt command. Pinned fields are named but their values
-/// stay out of the conversation: a `fill` value may be a literal secret.
+/// User turn for a prompt command. Names pinned fields but not their values
+/// (a `fill` value may be a literal secret).
fn promptCommandText(arena: std.mem.Allocator, p: Command.Prompt) ![]const u8 {
var aw: std.Io.Writer.Allocating = .init(arena);
const w = &aw.writer;
@@ -2153,10 +2144,9 @@ fn toolOutcome(self: *Agent, allocator: std.mem.Allocator, tool_name: []const u8
if (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 = if (tool) |tl| tl == t.tool else false;
+ is_target = tool == t.tool;
if (is_target) {
if (t.pinned != null) {
- // In the conversation arena: `/save` records the call as it ran.
args = try pinArgs(self.conversation.arena.allocator(), args, t.pinned);
saved.args = args;
}
diff --git a/src/script/Schema.zig b/src/script/Schema.zig
index 26dbe90b8..1bda62e4a 100644
--- a/src/script/Schema.zig
+++ b/src/script/Schema.zig
@@ -117,15 +117,11 @@ pub fn positionalFor(tool: BrowserTool) []const []const u8 {
};
}
-/// Tools whose slash form reads leading text as a prompt: the model locates the
-/// element or writes the script/schema, and the call it makes is what gets
-/// recorded. Their slash fields are keyword-only, so a prompt can't be
-/// mistaken for a selector.
+/// Slash form reads leading text as a prompt (fields are keyword-only).
pub fn takesPrompt(tool: BrowserTool) bool {
return switch (tool) {
.click, .fill, .hover, .selectOption, .setChecked, .extract, .evaluate => true,
- // A wait targets what is not on the page yet, so the model has nothing
- // to work from.
+ // A wait targets what is not on the page yet, so a prompt leaves the model nothing to pick from.
.waitForSelector, .waitForScript, .waitForState => false,
.goto, .search, .markdown, .html, .screenshot, .links, .tree, .nodeDetails, .interactiveElements, .structuredData, .detectForms, .scroll, .press, .findElement, .consoleLogs, .getUrl, .getCookies, .getEnv => false,
};
@@ -202,9 +198,8 @@ pub fn isBarePositional(self: Schema, args: std.json.ObjectMap) bool {
/// The field a single leading positional binds to: the lone required field,
/// or — when nothing is required — the tool's only field (e.g. getEnv binds to
/// `name`). Null when there's no required field and more than one optional
-/// field, since the target would be ambiguous (click's selector/backendNodeId),
-/// and for `takesPrompt` tools, whose leading text is a prompt. Slash input
-/// only; the script form's positionals are `positional`.
+/// field, since the target would be ambiguous (click's selector/backendNodeId).
+/// Slash input only; the script form's positionals are `positional`.
pub fn leadingPositionalField(self: Schema) ?[]const u8 {
if (takesPrompt(self.tool)) return null;
if (self.required.len == 1) return self.required[0];
@@ -231,22 +226,19 @@ pub fn parseValueDiag(self: Schema, arena: std.mem.Allocator, rest_raw: []const
return self.parseArgs(arena, rest_raw, diag, true);
}
-/// A slash command's leading prompt and the `key=value` tail pinned beside it.
pub const PromptSplit = struct {
prompt: []const u8,
args: ?std.json.Value,
};
-/// Splits `rest` for a `takesPrompt` tool. Null when there is no leading text,
-/// so the caller parses `rest` as ordinary arguments. Required fields may be
-/// missing from `args`: the model supplies them.
+/// Null when there is no leading text (caller parses `rest` as ordinary args).
+/// Required fields may be missing from `args`: the model supplies them.
pub fn parsePrompt(self: Schema, arena: std.mem.Allocator, rest_raw: []const u8, diag: ?*Diag) ParseError!?PromptSplit {
std.debug.assert(takesPrompt(self.tool));
const rest = std.mem.trim(u8, rest_raw, &std.ascii.whitespace);
if (rest.len == 0 or rest[0] == '{') return null;
- // Split on bare words, not `tokenize`: a prompt is prose, and the
- // apostrophe in "the user's menu" is not an opening quote.
+ // tokenizeAny: prose may contain apostrophes ("the user's menu").
var tail_start = rest.len;
var it = std.mem.tokenizeAny(u8, rest, &std.ascii.whitespace);
while (it.next()) |word| {
@@ -312,8 +304,7 @@ fn parseArgs(self: Schema, arena: std.mem.Allocator, rest_raw: []const u8, diag:
// selector='#a'` works without `checked=true`.
required: for (self.required) |req| {
for (list.items) |p| if (std.mem.eql(u8, p.key, req)) continue :required;
- // Beside a prompt, a missing field is the model's to fill: defaulting
- // `checked=true` would pin it and rule out "uncheck the newsletter box".
+ // Prompt path: don't pin default-true fields (e.g. "uncheck …").
if (!require) continue;
if (!self.isFieldDefaultTrue(req)) return error.MissingRequired;
list.appendAssumeCapacity(.{ .key = req, .value = "true" });
@@ -481,8 +472,6 @@ fn buildOne(arena: std.mem.Allocator, tool: BrowserTool, td: BrowserTool.Definit
return info;
}
-/// `keyword_only`: a required field renders as `name=…`, since bare text is a
-/// prompt rather than its value.
fn buildHints(arena: std.mem.Allocator, required: []const []const u8, fields: []const FieldEntry, keyword_only: bool) ![]const HintSlot {
if (fields.len == 0 and required.len == 0) return &.{};
var optional_count: usize = 0;
@@ -950,11 +939,9 @@ 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);
- // A wholly quoted prompt loses its quotes; one that only starts quoted keeps them.
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);
- // No leading text, or a JSON payload: ordinary arguments.
try testing.expect((try fill.parsePrompt(aa, "selector=#e value=x", null)) == null);
try testing.expect((try fill.parsePrompt(aa, "{\"selector\":\"#e\",\"value\":\"x\"}", null)) == null);
try testing.expect((try fill.parsePrompt(aa, "", null)) == null);
diff --git a/src/script/command.zig b/src/script/command.zig
index a180c5afd..d0748722d 100644
--- a/src/script/command.zig
+++ b/src/script/command.zig
@@ -16,10 +16,10 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-//! A parsed slash command: a tool slash command, a tool command given a
-//! prompt instead of its fields, a `#`-comment, or an `LlmCommand` trigger
-//! (`/login`, `/logout`, `/acceptCookies`). Multi-line
-//! `'''…'''` blocks are assembled by the REPL before parse.
+//! A parsed slash command: a tool call, a prompt-form tool command, a
+//! `#`-comment, or an `LlmCommand` trigger (`/login`, `/logout`,
+//! `/acceptCookies`). Multi-line `'''…'''` blocks are assembled by the REPL
+//! before parse.
const std = @import("std");
const lp = @import("lightpanda");
@@ -68,13 +68,11 @@ pub const Command = union(enum) {
prompt: Prompt,
comment: void,
- /// A `takesPrompt` tool given leading text (`/click the sign in button`):
- /// the model fills in the arguments `args` leaves out, and its call is
- /// what gets recorded.
+ /// Leading text for a `takesPrompt` tool (`/click the sign in button`).
pub const Prompt = struct {
tool: BrowserTool,
text: []const u8,
- /// Fields typed as `key=value` beside the prompt; the model can't change them.
+ /// Pinned key=value beside the prompt.
args: ?std.json.Value,
};
@@ -406,7 +404,6 @@ test "parse: leading text on a prompt tool is a prompt, key=value stays literal"
try testing.expectString("Login", quoted.text);
try testing.expect(quoted.args == null);
- // An apostrophe in prose is not an opening quote.
try testing.expectString("the user's menu", (try Command.parse(aa, "/click the user's menu")).prompt.text);
const fill = (try Command.parse(aa, "/fill the email field value=$LP_EMAIL")).prompt;
@@ -414,7 +411,6 @@ test "parse: leading text on a prompt tool is a prompt, key=value stays literal"
try testing.expectString("$LP_EMAIL", fill.args.?.object.get("value").?.string);
try testing.expect(fill.args.?.object.get("selector") == null);
- // `checked` is left to the model rather than defaulted to true.
try testing.expect((try Command.parse(aa, "/setChecked uncheck the newsletter box")).prompt.args == null);
const extract = (try Command.parse(aa, "/extract all products with prices save=products")).prompt;
@@ -449,7 +445,6 @@ test "parse: positional must come before key=value" {
var arena: std.heap.ArenaAllocator = .init(testing.allocator);
defer arena.deinit();
try testing.expectError(error.PositionalMustComeFirst, Command.parse(arena.allocator(), "/goto timeout=5000 https://example.com"));
- // A prompt tool has no positional, so a bare value after key=value is malformed.
try testing.expectError(error.MalformedKv, Command.parse(arena.allocator(), "/extract save=front '{\"karma\":\"#karma\"}'"));
const cmd = try Command.parse(arena.allocator(), "/extract schema='{\"karma\":\"#karma\"}' save=front");
try testing.expectString("front", cmd.tool_call.args.?.object.get("save").?.string);
@@ -548,7 +543,6 @@ test "isRecorded / producesData via tool flags" {
const login: Command = .{ .llm = .login };
try testing.expect(!login.isRecorded());
- // Only the call the model makes is recorded, never the prompt itself.
const prompt = try Command.parse(arena.allocator(), "/click the sign in button");
try testing.expect(!prompt.isRecorded());
}