repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

show forget context preview

35a92221d42faa3db4e24231b30ea28d89b6b0d7

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

extensions/forget/__tests__/unit.test.ts

Modified
1 1 import { describe, expect, test } from "bun:test";
2 2 import forgetExtension, { __test } from "../index.ts";
4 -const { forgetSummary, normalizeTarget, REMINDER, resolveTarget, userIndexes } =
5 - __test;
4 +const {
5 + entryPreview,
6 + forgetSummary,
7 + normalizeTarget,
8 + REMINDER,
9 + renderForgetCall,
10 + renderForgetResult,
11 + resolveTarget,
12 + userIndexes,
13 +} = __test;
15 +const theme = {
16 + bold: (text: string) => text,
17 + fg: (_tone: string, text: string) => text,
18 +};
20 +function rendered(component: { render(width: number): string[] }) {
21 + return component.render(120).join("\n");
7 24 function msg(id: string, role: string, text: string) {
8 25 return {
99 116 expect(summary).toContain("new task");
100 117 expect(summary).not.toContain("secret");
120 + test("renders call and result with kept context preview", () => {
121 + const branch = [msg("u1", "user", "keep this setup text")];
122 + const resolution = resolveTarget(branch);
123 + const preview = entryPreview(resolution.entry);
124 + expect(preview).toBe("keep this setup text");
126 + const call = rendered(
127 + renderForgetCall(
128 + { target: "previous_user", reason: "new direction" },
129 + theme,
130 + ),
131 + );
132 + expect(call).toContain("forget previous_user");
133 + expect(call).toContain("new direction");
135 + const result = rendered(
136 + renderForgetResult(
138 + content: [{ type: "text", text: "done" }],
139 + details: {
140 + target: "current_user",
141 + keptPreview: preview,
142 + keptFromEntryId: "u1",
143 + forgottenBefore: 4,
144 + reason: "new direction",
145 + compactionId: "c1",
146 + },
147 + },
148 + { expanded: true },
149 + theme,
150 + ),
151 + );
152 + expect(result).toContain("kept current_user");
153 + expect(result).toContain("dropped 4 entries");
154 + expect(result).toContain("keep this setup text");
155 + expect(result).toContain("compaction: c1");
156 + });

extensions/forget/index.ts

Modified
1 1 import { StringEnum } from "@earendil-works/pi-ai";
2 2 import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
3 +import { Text } from "@earendil-works/pi-tui";
3 4 import { Type } from "typebox";
5 6 const TOOL = "forget";
17 18 interface BranchEntry {
18 19 type?: string;
19 20 id?: string;
20 - message?: { role?: string };
21 + message?: { role?: string; content?: unknown };
21 22 [key: string]: unknown;
32 33 hint?: string;
36 +interface ForgetResultDetails {
37 + kind?: typeof DETAILS_KIND;
38 + version?: number;
39 + code?: string;
40 + skipped?: boolean;
41 + compactionId?: string;
42 + target?: ForgetTarget | string;
43 + offset?: 0 | 1;
44 + keptFromEntryId?: string;
45 + keptPreview?: string;
46 + forgottenBefore?: number;
47 + reason?: string;
35 50 function textResult(text: string, details?: Record<string, unknown>) {
36 51 return { content: [{ type: "text" as const, text }], details };
44 59 return {
45 60 content: [{ type: "text" as const, text: `${code}. ${hint}` }],
46 61 isError: true,
47 - details: { code, ...details },
62 + details: { kind: DETAILS_KIND, code, ...details },
66 +function compactWhitespace(text: string): string {
67 + return text.replace(/\s+/g, " ").trim();
70 +function safeInline(text: unknown): string {
71 + return compactWhitespace(
72 + String(text ?? "").replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, ""),
73 + );
76 +function contentText(content: unknown): string {
77 + if (typeof content === "string") return content;
78 + if (Array.isArray(content)) {
79 + return content
80 + .map((part) => {
81 + if (typeof part === "string") return part;
82 + if (part && typeof part === "object" && "text" in part) {
83 + const text = (part as { text?: unknown }).text;
84 + return typeof text === "string" ? text : "";
85 + }
86 + return "";
87 + })
88 + .filter(Boolean)
89 + .join(" ");
90 + }
91 + return "";
94 +function entryPreview(
95 + entry: BranchEntry | undefined,
96 + max = 180,
97 +): string | undefined {
98 + const text = safeInline(contentText(entry?.message?.content));
99 + if (!text) return undefined;
100 + return text.length > max ? `${text.slice(0, max - 1)}…` : text;
103 +function firstText(result: {
104 + content?: Array<{ type?: string; text?: string }>;
105 +}) {
106 + return result.content?.find((part) => part?.type === "text")?.text ?? "";
51 109 function userIndexes(branch: BranchEntry[]): number[] {
52 110 const indexes: number[] = [];
53 111 for (let i = 0; i < branch.length; i++) {
225 +function renderForgetCall(args: ForgetParams, theme: any) {
226 + const target =
227 + normalizeTarget(args.target) ?? safeInline(args.target ?? "current_user");
228 + let text = theme.fg("toolTitle", theme.bold("◌ forget "));
229 + text += theme.fg("accent", target);
230 + const reason = safeInline(args.reason);
231 + if (reason) text += theme.fg("dim", ` — ${reason}`);
232 + return new Text(text, 0, 0);
235 +function renderForgetResult(
236 + result: {
237 + content?: Array<{ type?: string; text?: string }>;
238 + details?: unknown;
239 + isError?: boolean;
240 + },
241 + options: { expanded?: boolean; isPartial?: boolean },
242 + theme: any,
243 +) {
244 + if (options.isPartial)
245 + return new Text(theme.fg("warning", "◌ forgetting context…"), 0, 0);
247 + const details = result.details as ForgetResultDetails | undefined;
248 + if (result.isError || details?.code) {
249 + const msg = safeInline(
250 + firstText(result) || details?.code || "forget failed",
251 + );
252 + return new Text(theme.fg("error", `✗ ${msg}`), 0, 0);
255 + if (details?.skipped) {
256 + let text = theme.fg("muted", "↷ nothing to forget");
257 + if (details.target) text += theme.fg("dim", ` · ${details.target}`);
258 + return new Text(text, 0, 0);
261 + let text = theme.fg("success", "✓ ");
262 + text += theme.fg("accent", `kept ${details?.target ?? "boundary"}`);
263 + if (typeof details?.forgottenBefore === "number")
264 + text += theme.fg("dim", ` · dropped ${details.forgottenBefore} entries`);
266 + if (details?.keptPreview)
267 + text += `\n ${theme.fg("muted", "↳ kept context:")} ${theme.fg("dim", details.keptPreview)}`;
269 + if (options.expanded) {
270 + if (details?.reason)
271 + text += `\n ${theme.fg("muted", "reason:")} ${theme.fg("dim", safeInline(details.reason))}`;
272 + if (details?.keptFromEntryId)
273 + text += `\n ${theme.fg("muted", "entry:")} ${theme.fg("dim", details.keptFromEntryId)}`;
274 + if (details?.compactionId)
275 + text += `\n ${theme.fg("muted", "compaction:")} ${theme.fg("dim", details.compactionId)}`;
276 + text += `\n ${theme.fg("muted", "history append-only; active context prefix-cut only")}`;
279 + return new Text(text, 0, 0);
167 282 export const __test = {
168 283 appendForgetCompaction,
284 + entryPreview,
169 285 forgetSummary,
170 286 normalizeTarget,
171 287 REMINDER,
288 + renderForgetCall,
289 + renderForgetResult,
172 290 resolveTarget,
173 291 userIndexes,
201 319 Type.String({ description: "Short reason for context compaction" }),
322 + renderCall: renderForgetCall,
323 + renderResult: renderForgetResult,
204 324 async execute(_toolCallId, params: ForgetParams, _signal, _onUpdate, ctx) {
205 325 const branch = ctx.sessionManager.getBranch() as BranchEntry[];
206 326 const resolution = resolveTarget(branch, params.target);
213 333 if (resolution.target === "current_user" && resolution.userCount <= 1)
214 334 return textResult("already_first. nothing to forget", {
335 + kind: DETAILS_KIND,
336 + version: 3,
215 337 skipped: true,
216 338 target: resolution.target,
339 + keptFromEntryId: resolution.entry?.id,
340 + keptPreview: entryPreview(resolution.entry),
217 341 forgottenBefore: 0,
219 343 const compactionId = appendForgetCompaction(
224 348 return textResult(
225 349 `done. kept=${resolution.target} dropped_entries=${resolution.index}`,
351 + kind: DETAILS_KIND,
352 + version: 3,
227 353 compactionId,
228 354 target: resolution.target,
229 355 offset: resolution.offset,
230 356 keptFromEntryId: resolution.entry?.id,
357 + keptPreview: entryPreview(resolution.entry),
231 358 forgottenBefore: resolution.index,
359 + reason: params.reason,

extensions/forget/package.json

Modified
22 22 "peerDependencies": {
23 23 "@earendil-works/pi-ai": "*",
24 24 "@earendil-works/pi-coding-agent": "*",
25 + "@earendil-works/pi-tui": "*",
25 26 "typebox": "*"
27 28 "devDependencies": {