Lightpanda: the headless browser designed for AI and automation
agent: add --save flag to synthesize scripts from tasks
Adrià Arrufat69d302fabe36
diff --git a/src/Config.zig b/src/Config.zigindex 749722057..13379089c 100644--- a/src/Config.zig+++ b/src/Config.zig@@ -231,6 +231,7 @@ const Commands = cli.Builder(.{.{ .name = "base_url", .type = ?[:0]const u8 },.{ .name = "system_prompt", .type = ?[:0]const u8 },.{ .name = "task", .type = ?[]const u8 },+ .{ .name = "save", .type = ?[]const u8 },.{ .name = "attach", .short = 'a', .type = []const u8, .multiple = true },.{ .name = "verbosity", .type = ?AgentVerbosity },.{ .name = "effort", .type = ?Effort },diff --git a/src/agent/Agent.zig b/src/agent/Agent.zigindex 536061328..7ba1ca12e 100644--- a/src/agent/Agent.zig+++ b/src/agent/Agent.zig@@ -246,6 +246,7 @@ model: []u8,effort: Config.Effort,script_file: ?[]const u8,one_shot_task: ?[]const u8,+one_shot_save: ?[]const u8,one_shot_attachments: ?[]const []const u8,cancel_requested: std.atomic.Value(bool) = .init(false),/// Shuts down the in-flight LLM socket on Ctrl-C so an agent turn aborts@@ -286,6 +287,20 @@ pub fn init(allocator: std.mem.Allocator, app: *App, opts: Config.Agent) !*Agent});return error.ConflictingFlags;}+ if (opts.save) |save_path| {+ if (opts.task == null) {+ log.fatal(.app, "conflicting flags", .{+ .hint = "--save synthesizes a script from the --task run; pass --task too",+ });+ return error.ConflictingFlags;+ }+ save.validateFilename(save_path) catch {+ log.fatal(.app, "invalid --save filename", .{+ .hint = "--save takes a local file name, not a path (e.g. script.js)",+ });+ return error.InvalidFilename;+ };+ }if (opts.no_llm and opts.provider != null) {log.warn(.app, "ignoring --provider", .{ .reason = "--no-llm takes precedence" });}@@ -394,6 +409,7 @@ pub fn init(allocator: std.mem.Allocator, app: *App, opts: Config.Agent) !*Agent.effort = effort,.script_file = opts.script_file,.one_shot_task = opts.task,+ .one_shot_save = opts.save,.one_shot_attachments = if (opts.attach.items.len == 0) null else opts.attach.items,.available_providers = available_providers,};@@ -540,6 +556,7 @@ pub const TurnInput = struct {prompt: []const u8,record_comment: ?[]const u8 = null,capture_for_save: bool = false,+ suppress_answer: bool = false,attachments: ?[]const []const u8 = null,label: []const u8 = "Request",};@@ -547,10 +564,17 @@ pub const TurnInput = struct {/// Returns true on success.pub fn run(self: *Agent) bool {if (self.one_shot_task) |task| {+ const saving = self.one_shot_save != null;const ok = self.runTurn(.{.prompt = task,.attachments = self.one_shot_attachments,+ .capture_for_save = saving,+ .record_comment = if (saving) task else null,+ .suppress_answer = saving,});+ // Synthesis is a second LLM call that adds to total_usage, so save+ // before printing the cumulative summary.+ if (ok and saving) self.saveOneShot();self.printUsageSummary();return ok;}@@ -593,12 +617,14 @@ fn runTurn(self: *Agent, input: TurnInput) bool {return false;},};- if (text) |t|- self.terminal.printAssistant(t)- else if (self.last_turn_refused)- self.terminal.printInfo("(model declined to respond — safety refusal)", .{})- else- self.terminal.printInfo("(no response from model)", .{});+ if (!input.suppress_answer) {+ if (text) |t|+ self.terminal.printAssistant(t)+ else if (self.last_turn_refused)+ self.terminal.printInfo("(model declined to respond — safety refusal)", .{})+ else+ self.terminal.printInfo("(no response from model)", .{});+ }self.conversation.prune();return true;}@@ -1086,8 +1112,6 @@ fn bumpedEffort(effort: Config.Effort) Config.Effort {/// conversation, and the deterministic record of what ran, then write the/// idiomatic script it returns.fn synthesizeSave(self: *Agent, arena: std.mem.Allocator, filename: ?[]const u8, prompt: ?[]const u8) void {- const provider_client = self.ai_client.?;-// With nothing recorded and no prompt, a re-synthesis has nothing to act// on — it would just re-roll the previous output.if (self.save_buffer.bytes().len == 0 and prompt == null) {@@ -1100,12 +1124,27 @@ fn synthesizeSave(self: *Agent, arena: std.mem.Allocator, filename: ?[]const u8,}const resolved = self.resolveSavePathAndMode(arena, filename) orelse return;- const path = resolved.path;+ self.synthesizeSaveTo(arena, resolved.path, resolved.mode, prompt);+}++/// One-shot `--task ... --save`: overwrites the destination without the REPL's+/// interactive file-exists prompt. The task doubles as the synthesis prompt so+/// the empty-buffer guard in `synthesizeSaveTo` never trips.+fn saveOneShot(self: *Agent) void {+ var arena = std.heap.ArenaAllocator.init(self.allocator);+ defer arena.deinit();+ self.synthesizeSaveTo(arena.allocator(), self.one_shot_save.?, .replace, self.one_shot_task.?);+}++/// LLM synthesis + write for an already-resolved destination. Shared by the+/// interactive `/save` and one-shot `--save`.+fn synthesizeSaveTo(self: *Agent, arena: std.mem.Allocator, path: []const u8, mode: save.Mode, prompt: ?[]const u8) void {+ const provider_client = self.ai_client.?;// Only update feeds the saved script back to the model; append stays// blind — the script is synthesized from this session alone and written// after the existing content.- const previous_script: ?[]const u8 = if (resolved.mode == .update)+ const previous_script: ?[]const u8 = if (mode == .update)save.readScript(arena, path) catch |err| {self.terminal.printError("failed to read {s}: {s}", .{ path, @errorName(err) });return;@@ -1173,7 +1212,7 @@ fn synthesizeSave(self: *Agent, arena: std.mem.Allocator, filename: ?[]const u8,// The save turn is a meta-action; keep it out of the ongoing conversation.self.conversation.rollback(baseline);- save.writeContentFile(path, script, resolved.mode) catch |err| {+ save.writeContentFile(path, script, mode) catch |err| {self.terminal.printError("failed to save {s}: {s}", .{ path, @errorName(err) });return;};diff --git a/src/agent/save.zig b/src/agent/save.zigindex b8613ce26..48649c0f5 100644--- a/src/agent/save.zig+++ b/src/agent/save.zig@@ -60,7 +60,7 @@ pub fn parseCommand(arena: std.mem.Allocator, rest: []const u8) !Command {return .{ .filename = name, .prompt = if (prompt.len == 0) null else prompt };}-fn validateFilename(name: []const u8) !void {+pub fn validateFilename(name: []const u8) !void {if (name.len == 0) return error.EmptyFilename;if (std.fs.path.isAbsolute(name)) return error.InvalidFilename;if (std.mem.indexOfScalar(u8, name, '/') != null) return error.InvalidFilename;diff --git a/src/help.zon b/src/help.zonindex 27bdc5310..3891afaf7 100644--- a/src/help.zon+++ b/src/help.zon@@ -141,6 +141,7 @@\\ {0s} agent --provider ollama --model qwen3.5:latest\\ {0s} agent --no-llm (basic slash-command-only REPL)\\ {0s} agent script.js (run a saved script, then exit)+ \\ {0s} agent --task "..." --save out.js (synthesize a replayable script)\\\\Arguments:\\[SCRIPT] Optional path to a .js script. Runs the script@@ -201,7 +202,14 @@\\\\--task <STRING> One-shot mode: run a single user turn, print the\\ final answer to stdout, and exit. Conflicts with- \\ the positional script.+ \\ the positional script. With --save, the answer is+ \\ suppressed and a script is written instead.+ \\+ \\--save <PATH> Synthesize a replayable .js script from the --task+ \\ run and write it to PATH, instead of printing the+ \\ answer. Replay it later with `agent PATH` (no LLM+ \\ calls). Overwrites PATH if it exists. PATH must be a+ \\ local file name, not a path. Requires --task.\\\\-a, --attach <PATH> Feed a local file to the model alongside --task.\\ Repeatable, one file per flag. Text files are