Skip to content

Lightpanda: the headless browser designed for AI and automation

agent: continue slash commands across lines inside '''...''' blocks

Adrià Arrufat03f6577efa4f
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zig
index ea52e7435..31d1be4cd 100644
--- a/src/agent/Agent.zig
+++ b/src/agent/Agent.zig
@@ -692,7 +692,15 @@ fn runRepl(self: *Agent) void {
continue :repl;
}
- const slash_split: ?Schema.Split = Schema.parseSlashCommand(trimmed);
+ // A slash command whose `'''…'''` body is still open continues on the
+ // following lines until the block closes (the multi-line /extract
+ // form). Ctrl-D on the continuation prompt abandons the command.
+ const command_text: []const u8 = if (trimmed[0] == '/' and Schema.hasUnclosedTripleQuote(trimmed))
+ Terminal.readContinuation(aa, trimmed) orelse continue :repl
+ else
+ trimmed;
+
+ const slash_split: ?Schema.Split = Schema.parseSlashCommand(command_text);
if (slash_split) |split| {
if (SlashCommand.findMeta(split.name)) |meta| {
if (self.handleMeta(aa, meta, split.rest)) break :repl;
@@ -701,7 +709,7 @@ fn runRepl(self: *Agent) void {
}
var diag: Schema.Diag = .{};
- const cmd = Command.parseDiag(aa, line, &diag) catch |err| switch (err) {
+ const cmd = Command.parseDiag(aa, command_text, &diag) catch |err| switch (err) {
error.NotASlashCommand => {
if (self.ai_client == null) {
self.terminal.printError("Basic REPL (LLM disabled) accepts only commands. Try /help, or " ++ llm_setup_hint ++ " to enable natural-language prompts.", .{});
@@ -733,7 +741,7 @@ fn runRepl(self: *Agent) void {
if (!result.is_error) {
self.recordSaveCommand(navigationGoto(aa, tc.tool, tc.args) orelse cmd);
}
- self.recordSlashToolCall(trimmed, tc.name(), tc.args, result) catch |err| {
+ self.recordSlashToolCall(command_text, tc.name(), tc.args, result) catch |err| {
self.terminal.printWarning("LLM conversation out of sync (/{s}: {s}); next prompt may not see this action", .{ tc.name(), @errorName(err) });
};
},
diff --git a/src/agent/Terminal.zig b/src/agent/Terminal.zig
index a81716bca..c260fc447 100644
--- a/src/agent/Terminal.zig
+++ b/src/agent/Terminal.zig
@@ -995,6 +995,23 @@ pub fn freeLine(line: []const u8) void {
c.ic_free(@ptrCast(@constCast(line.ptr)));
}
+const continuation_prompt = "... ";
+
+/// Read the follow-up lines of an input whose `'''…'''` body is still open,
+/// joined with newlines until the block closes. Null abandons the input
+/// (EOF on the continuation prompt, or out of memory).
+pub fn readContinuation(arena: std.mem.Allocator, first: []const u8) ?[]const u8 {
+ var buf: std.ArrayList(u8) = .empty;
+ buf.appendSlice(arena, first) catch return null;
+ while (Schema.hasUnclosedTripleQuote(buf.items)) {
+ const next = readLine(continuation_prompt) orelse return null;
+ defer freeLine(next);
+ buf.append(arena, '\n') catch return null;
+ buf.appendSlice(arena, next) catch return null;
+ }
+ return buf.items;
+}
+
// Free-function `lp.log.sink` can't capture self; the agent sets this
// before installing the sink and clears it on teardown.
var active_for_log: ?*Terminal = null;