feat(coding-agent): add extension compaction helpers
This commit is contained in:
32
packages/coding-agent/examples/extensions/trigger-compact.ts
Normal file
32
packages/coding-agent/examples/extensions/trigger-compact.ts
Normal 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);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user