import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent"; // Change this value to choose when the extension compacts. export const COMPACT_THRESHOLD_TOKENS = 150_000; export default function (pi: ExtensionAPI) { let compacting = false; const compactAtThreshold = (_event: unknown, ctx: ExtensionContext) => { const tokens = ctx.getContextUsage()?.tokens; if (tokens === null || tokens === undefined || tokens < COMPACT_THRESHOLD_TOKENS || compacting) return; compacting = true; ctx.compact({ onComplete: () => { compacting = false; pi.sendMessage( { customType: "preemptive-compact", content: "Compaction completed. Continue the current task from the summary.", display: false, }, { triggerTurn: true }, ); }, onError: () => { compacting = false; }, }); }; pi.on("turn_end", compactAtThreshold); }