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 }
= 16 + bold: (text
: string)
=> text,
17 + fg: (_tone
: string, text
: string)
=> text,
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) {
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(
128 + { target:
"previous_user", reason:
"new direction" },
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" }],
140 + target:
"current_user",
141 + keptPreview: preview,
142 + keptFromEntryId:
"u1",
143 + forgottenBefore:
4,
144 + reason:
"new direction",
145 + compactionId:
"c1",
148 + { expanded:
true },
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");
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 {
20 - message
?: { role
?: string };
21 + message
?: { role
?: string; content
?: unknown };
21 22 [key
: string]
: unknown;
36 +interface ForgetResultDetails {
37 + kind
?: typeof DETAILS_KIND;
41 + compactionId
?: string;
42 + target
?: ForgetTarget | string;
44 + keptFromEntryId
?: string;
45 + keptPreview
?: string;
46 + forgottenBefore
?: number;
35 50 function textResult(text
: string, details
?: Record<
string,
unknown>) {
36 51 return { content: [{ type:
"text" as const, text }], details };
45 60 content: [{ type:
"text" as const, text:
`${code}. ${hint}` }],
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,
""),
76 +function contentText(content
: unknown)
: string {
77 + if (
typeof content
=== "string")
return content;
78 + if (
Array.
isArray(content)) {
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
: "";
94 +function entryPreview(
95 + entry
: BranchEntry | undefined,
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 }>;
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) {
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(
237 + content
?: Array<{ type
?: string; text
?: string }>;
241 + options
: { expanded
?: boolean; isPartial
?: boolean },
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",
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,
289 + renderForgetResult,
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,
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,
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
Modified22 22 "peerDependencies": {
23 23 "@earendil-works/pi-ai":
"*",
24 24 "@earendil-works/pi-coding-agent":
"*",
25 + "@earendil-works/pi-tui":
"*",
27 28 "devDependencies": {