diff options
Diffstat (limited to 'src/compaction.zig')
| -rw-r--r-- | src/compaction.zig | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/compaction.zig b/src/compaction.zig new file mode 100644 index 0000000..a1f6e89 --- /dev/null +++ b/src/compaction.zig @@ -0,0 +1,48 @@ +//! The builtin `/compact` slash command. +//! +//! Folds the conversation's older turns into a summary (via +//! `Agent.compact`), persists the result to the session log, and reports +//! the outcome to the user. Registered into the CLI's command registry by +//! `register`. + +const std = @import("std"); +const panto = @import("panto"); +const command = @import("command.zig"); +const session_persist = @import("session_persist.zig"); + +/// Install the `/compact` command into `registry`. +pub fn register(registry: *command.Registry) !void { + try registry.register(.{ + .name = "compact", + .description = "Summarize older turns to free up context. Optional args become extra compaction instructions.", + .run = run, + }); +} + +/// `/compact [extra instructions]` — compact the conversation now. +fn run(args: []const u8, ctx: *command.Context) anyerror!void { + const extra: ?[]const u8 = if (args.len > 0) args else null; + + const res = ctx.agent.compact(ctx.conv, ctx.compaction_prompt, extra) catch |err| { + try ctx.stdout.print("\n[compaction failed: {s}]\n> ", .{@errorName(err)}); + try ctx.stdout_file.flush(); + return; + }; + + if (res.compacted) { + try session_persist.persistCompaction( + ctx.allocator, + ctx.session_mgr, + ctx.conv, + ctx.provider_name, + ctx.model_name, + ); + try ctx.stdout.print( + "\n[compacted: summarized {d} message(s), kept {d} recent turn(s)]\n> ", + .{ res.summarized_messages, res.kept_turns }, + ); + } else { + try ctx.stdout.writeAll("\n[nothing to compact: conversation already fits]\n> "); + } + try ctx.stdout_file.flush(); +} |
