feat(coding-agent): add extension compaction helpers

This commit is contained in:
Mario Zechner 2026-01-17 11:39:46 +01:00
parent 673916f63c
commit 9d3f8117a4
11 changed files with 190 additions and 4 deletions

View file

@ -77,6 +77,7 @@ cp permission-gate.ts ~/.pi/agent/extensions/
| `pirate.ts` | Demonstrates `systemPromptAppend` to dynamically modify system prompt |
| `claude-rules.ts` | Scans `.claude/rules/` folder and lists rules in system prompt |
| `custom-compaction.ts` | Custom compaction that summarizes entire conversation |
| `trigger-compact.ts` | Triggers compaction when context usage exceeds 100k tokens and adds `/trigger-compact` command |
### System Integration

View file

@ -0,0 +1,32 @@
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
const COMPACT_THRESHOLD_TOKENS = 100_000;
export default function (pi: ExtensionAPI) {
const triggerCompaction = (ctx: ExtensionContext, customInstructions?: string) => {
ctx.compact({
customInstructions,
onError: (error) => {
if (ctx.hasUI) {
ctx.ui.notify(`Compaction failed: ${error.message}`, "error");
}
},
});
};
pi.on("turn_end", (_event, ctx) => {
const usage = ctx.getContextUsage();
if (!usage || usage.tokens <= COMPACT_THRESHOLD_TOKENS) {
return;
}
triggerCompaction(ctx);
});
pi.registerCommand("trigger-compact", {
description: "Trigger compaction immediately",
handler: async (args, ctx) => {
const instructions = args.trim() || undefined;
triggerCompaction(ctx, instructions);
},
});
}