summaryrefslogtreecommitdiff
path: root/src/compaction.zig
diff options
context:
space:
mode:
authort <t@tjp.lol>2026-06-02 15:00:44 -0600
committert <t@tjp.lol>2026-06-02 16:37:32 -0600
commit8b88b886346460b1ab29edb03ad85bc3bb565a45 (patch)
tree13b9ab56061a722641389b5fc8ac1113d09b66e5 /src/compaction.zig
parent7268c0b8e8bcf4b325581fabe7476a394f167738 (diff)
compaction
Diffstat (limited to 'src/compaction.zig')
-rw-r--r--src/compaction.zig48
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();
+}