From d0c36f7e3ece9f5eec2b3970e227cb2992df5b4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Fri, 10 Apr 2026 11:59:55 +0200 Subject: [PATCH] terminal: add persistent REPL history Persists REPL history to .lp-history, except in script mode. --- src/agent/Agent.zig | 6 +++++- src/agent/Terminal.zig | 14 +++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/agent/Agent.zig b/src/agent/Agent.zig index fa77ca3d..b32ab2a6 100644 --- a/src/agent/Agent.zig +++ b/src/agent/Agent.zig @@ -111,11 +111,15 @@ pub fn init(allocator: std.mem.Allocator, app: *App, opts: Config.Agent) !*Self return error.ToolInitFailed; }; + // Persist REPL history in a cwd-relative `.lp-history`. Skipped in pure + // replay mode (no REPL is opened). + const history_path: ?[:0]const u8 = if (is_script_mode) null else ".lp-history"; + self.* = .{ .allocator = allocator, .ai_client = ai_client, .tool_executor = tool_executor, - .terminal = Terminal.init(), + .terminal = Terminal.init(history_path), .cmd_executor = undefined, .recorder = Recorder.init(if (opts.save) opts.script_file else null), .messages = .empty, diff --git a/src/agent/Terminal.zig b/src/agent/Terminal.zig index a5672052..c05093af 100644 --- a/src/agent/Terminal.zig +++ b/src/agent/Terminal.zig @@ -13,6 +13,8 @@ const ansi_green = "\x1b[32m"; const ansi_yellow = "\x1b[33m"; const ansi_red = "\x1b[31m"; +history_path: ?[:0]const u8, + const CommandInfo = struct { name: [:0]const u8, hint: [:0]const u8 }; const commands = [_]CommandInfo{ @@ -31,11 +33,14 @@ const commands = [_]CommandInfo{ .{ .name = "EXIT", .hint = "" }, }; -pub fn init() Self { +pub fn init(history_path: ?[:0]const u8) Self { c.linenoiseSetMultiLine(1); c.linenoiseSetCompletionCallback(&completionCallback); c.linenoiseSetHintsCallback(&hintsCallback); - return .{}; + if (history_path) |path| { + _ = c.linenoiseHistoryLoad(path.ptr); + } + return .{ .history_path = history_path }; } fn completionCallback(buf: [*c]const u8, lc: [*c]c.linenoiseCompletions) callconv(.c) void { @@ -63,11 +68,14 @@ fn hintsCallback(buf: [*c]const u8, color: [*c]c_int, bold: [*c]c_int) callconv( return null; } -pub fn readLine(_: *Self, prompt: [*:0]const u8) ?[]const u8 { +pub fn readLine(self: *Self, prompt: [*:0]const u8) ?[]const u8 { const line = c.linenoise(prompt) orelse return null; const slice = std.mem.sliceTo(line, 0); if (slice.len > 0) { _ = c.linenoiseHistoryAdd(line); + if (self.history_path) |path| { + _ = c.linenoiseHistorySave(path.ptr); + } } return slice; }