repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(cmprss): show saved context percent

649ebe57be3d34a9356639abc21bb94745a5ea87

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/cmprss/__tests__/cmprss.test.ts

Modified
8 8 DEFAULT_CONFIG,
9 9 compressToolResult,
10 10 createRuntime,
11 + formatStatusText,
11 12 replayState,
12 13 runCommand,
13 14 } = __test;
38 39 "session_start",
39 40 "session_shutdown",
40 41 "tool_result",
42 + "model_select",
42 44 expect(tools).toEqual(["cmprss_retrieve"]);
43 45 expect(commands).toEqual(["cmprss"]);
48 + test("formats status as saved context percentage when context window is known", () => {
49 + const runtime = createRuntime(DEFAULT_CONFIG);
50 + runtime.stats = {
51 + blocks: 1,
52 + originalBytes: 400_000,
53 + compressedBytes: 200_000,
54 + };
56 + expect(formatStatusText(runtime, { contextWindow: 200_000 })).toBe(
57 + "cmprss: -25%",
58 + );
59 + });
46 61 test("leaves small output unchanged", () => {
47 62 const runtime = createRuntime(DEFAULT_CONFIG);
48 63 const result = compressToolResult(runtime, {

extensions/cmprss/index.ts

Modified
1 1 import { createHash } from "node:crypto";
2 2 import { readFileSync } from "node:fs";
3 -import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
3 +import type {
4 + ContextUsage,
5 + ExtensionAPI,
6 +} from "@earendil-works/pi-coding-agent";
4 7 import { Type } from "typebox";
6 9 const ENTRY_TYPE = "cmprss-state";
481 484 return "cmprss: cleared";
483 486 if (command === "stats" || command === "status") {
484 - const saved = Math.max(
485 - 0,
486 - Math.floor(
487 - (runtime.stats.originalBytes - runtime.stats.compressedBytes) / 4,
488 - ),
489 - );
487 + const saved = savedTokens(runtime);
490 488 return `cmprss: ${runtime.enabled ? "on" : "off"}; blocks=${runtime.stats.blocks}; saved~${saved}t; threshold=${runtime.config.thresholdBytes}B`;
492 490 return "cmprss: usage: /cmprss [status|stats|on|off|clear]";
493 +function savedTokens(runtime: Runtime): number {
494 + return Math.max(
495 + 0,
496 + Math.floor(
497 + (runtime.stats.originalBytes - runtime.stats.compressedBytes) / 4,
498 + ),
499 + );
502 +function formatStatusText(
503 + runtime: Runtime,
504 + usage?: Pick<ContextUsage, "contextWindow">,
505 +): string {
506 + const saved = savedTokens(runtime);
507 + const contextWindow = usage?.contextWindow;
508 + if (
509 + typeof contextWindow !== "number" ||
510 + !Number.isFinite(contextWindow) ||
511 + contextWindow <= 0
513 + return `cmprss: ${saved}t`;
514 + const percent = Math.round((saved / contextWindow) * 100);
515 + return `cmprss: ${percent === 0 ? "0" : `-${percent}`}%`;
495 518 function updateStatus(
496 519 runtime: Runtime,
497 520 ctx: {
498 521 hasUI?: boolean;
499 522 ui?: { setStatus?: (key: string, text: string | undefined) => void };
523 + getContextUsage?: () => ContextUsage | undefined;
501 525 ): void {
502 526 if (!ctx.hasUI || !runtime.config.status) return;
504 528 ctx.ui?.setStatus?.(STATUS_KEY, "cmprss: off");
505 529 return;
507 - const saved = Math.max(
508 - 0,
509 - Math.floor(
510 - (runtime.stats.originalBytes - runtime.stats.compressedBytes) / 4,
511 - ),
531 + ctx.ui?.setStatus?.(
532 + STATUS_KEY,
533 + formatStatusText(runtime, ctx.getContextUsage?.()),
513 - ctx.ui?.setStatus?.(STATUS_KEY, `cmprss: ${saved}t`);
516 537 export default function cmprssExtension(pi: ExtensionAPI) {
543 564 return result;
544 565 }) as any);
567 + pi.on("model_select", (_event, ctx) => {
568 + updateStatus(runtime, ctx);
569 + });
546 571 pi.registerTool({
547 572 name: "cmprss_retrieve",
548 573 label: "cmprss retrieve",
578 603 replayState,
579 604 runCommand,
580 605 compressContent,
606 + formatStatusText,