Skip to content

Lightpanda: the headless browser designed for AI and automation

`Agent`: add `lightpanda run -`

Halil Durak901e5eaf4dab
diff --git a/src/Config.zig b/src/Config.zig
index e00dac697..a94ecb6df 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -1200,7 +1200,7 @@ pub fn parseArgs(allocator: Allocator, proc_args: std.process.Args) !Config {
if (command == .run) {
const run = command.run;
if (run.script_file == null) {
- log.fatal(.app, "missing script file", .{ .hint = "usage: lightpanda run <script.js>" });
+ log.fatal(.app, "missing script file", .{ .hint = "usage: lightpanda run <script.js | ->" });
return error.MissingArgument;
}
// run's fields are a strict subset of Agent's (compile error otherwise).
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zig
index 9c059a54d..e4d6b495c 100644
--- a/src/agent/Agent.zig
+++ b/src/agent/Agent.zig
@@ -1527,12 +1527,20 @@ const ScriptOutput = struct {
}
};
+/// Upper bound on script source, whether read from a file or piped in.
+const max_script_bytes = 10 * 1024 * 1024;
+
+/// `lightpanda run -` reads the script from stdin.
+const stdin_script_path = "-";
+
fn runScript(self: *Agent, path: []const u8) bool {
var script_arena: std.heap.ArenaAllocator = .init(self.allocator);
defer script_arena.deinit();
- const content = std.Io.Dir.cwd().readFileAlloc(lp.io, path, script_arena.allocator(), .limited(10 * 1024 * 1024)) catch |err| {
- self.terminal.printError("Failed to read script '{s}': {s}", .{ path, @errorName(err) });
+ const from_stdin = std.mem.eql(u8, path, stdin_script_path);
+ const name = if (from_stdin) "<stdin>" else path;
+ const content = readScriptSource(script_arena.allocator(), path, from_stdin) catch |err| {
+ self.terminal.printError("Failed to read script '{s}': {s}", .{ name, @errorName(err) });
return false;
};
@@ -1555,8 +1563,8 @@ fn runScript(self: *Agent, path: []const u8) bool {
var output: ScriptOutput = .{ .terminal = &self.terminal };
runtime.console_observer = .{ .context = @ptrCast(&output), .notify = ScriptOutput.observe };
- self.terminal.beginTool("script", path);
- const result = runtime.runSource(content, path);
+ self.terminal.beginTool("script", name);
+ const result = runtime.runSource(content, name);
self.terminal.endTool();
if (result catch |err| {
@@ -1569,10 +1577,19 @@ fn runScript(self: *Agent, path: []const u8) bool {
// A script that printed nothing leaves no trace, so freeze the spinner into
// a green bullet (like /goto); one that printed already showed its result.
- if (!output.emitted) self.terminal.printScriptDone("script", path);
+ if (!output.emitted) self.terminal.printScriptDone("script", name);
return true;
}
+fn readScriptSource(allocator: std.mem.Allocator, path: []const u8, from_stdin: bool) ![]u8 {
+ if (!from_stdin) {
+ return std.Io.Dir.cwd().readFileAlloc(lp.io, path, allocator, .limited(max_script_bytes));
+ }
+ var buf: [64 * 1024]u8 = undefined;
+ var stdin = std.Io.File.stdin().readerStreaming(lp.io, &buf);
+ return stdin.interface.allocRemaining(allocator, .limited(max_script_bytes));
+}
+
/// Mirror a user-typed slash command into `self.conversation.messages` as if the
/// LLM had called the tool itself, so the next natural-language turn sees the
/// same conversation shape either way.
diff --git a/src/help.zon b/src/help.zon
index 0ee486c95..c3278e221 100644
--- a/src/help.zon
+++ b/src/help.zon
@@ -207,10 +207,10 @@
\\
\\Arguments:
\\[SCRIPT]
- \\ Optional path to a .js script. Runs the script (no LLM calls) and
- \\ exits; `{0s} run SCRIPT` is the preferred spelling. With no script
- \\ and no --task, the REPL starts; from there /load runs a script and
- \\ /save exports the session to a file.
+ \\ Optional path to a .js script, or `-` for stdin. Runs the script
+ \\ (no LLM calls) and exits; `{0s} run SCRIPT` is the preferred
+ \\ spelling. With no script and no --task, the REPL starts; from
+ \\ there /load runs a script and /save exports the session to a file.
\\ Caution: .js files can contain evaluate(...) calls that run
\\ arbitrary JavaScript in the page. Only run scripts you trust, the
\\ same way you would a shell script.
@@ -302,16 +302,17 @@
\\Runs a saved script, then exits. No LLM calls, no API key needed.
\\
\\Usage:
- \\ {0s} run <SCRIPT> [COMMON_OPTIONS]
+ \\ {0s} run <SCRIPT | -> [COMMON_OPTIONS]
\\
\\Examples:
\\ {0s} run script.js (replay a saved script)
+ \\ cat script.js | {0s} run - (read the script from stdin)
\\
\\Arguments:
\\<SCRIPT>
\\ Path to a .js script to run, then exit. Produce one with
\\ `{0s} agent --task "..." --save script.js`, or `/save` from the
- \\ agent REPL.
+ \\ agent REPL. Pass `-` to read the script from stdin instead.
\\ Caution: .js files can contain evaluate(...) calls that run
\\ arbitrary JavaScript in the page. Only run scripts you trust, the
\\ same way you would a shell script.