forked from earendil-works/pi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger-compact.ts
More file actions
40 lines (36 loc) · 1.02 KB
/
Copy pathtrigger-compact.ts
File metadata and controls
40 lines (36 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { ExtensionAPI, ExtensionContext } from "open-pi-coding-agent";
const COMPACT_THRESHOLD_TOKENS = 100_000;
export default function (pi: ExtensionAPI) {
const triggerCompaction = (ctx: ExtensionContext, customInstructions?: string) => {
if (ctx.hasUI) {
ctx.ui.notify("Compaction started", "info");
}
ctx.compact({
customInstructions,
onComplete: () => {
if (ctx.hasUI) {
ctx.ui.notify("Compaction completed", "info");
}
},
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 === null || 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);
},
});
}