pi-context-kit 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -7
- package/LICENSE +1 -1
- package/README.md +110 -110
- package/context-kit.schema.json +4 -1
- package/extensions/index.ts +87 -157
- package/package.json +37 -45
- package/src/background.ts +2 -3
- package/src/config.ts +12 -41
- package/src/context.ts +43 -16
- package/src/files.ts +6 -13
- package/src/handoff.ts +3 -7
- package/src/maintain.ts +6 -19
- package/src/migrate.ts +6 -18
- package/src/note.ts +5 -6
- package/src/panel.ts +7 -21
- package/src/prune.ts +2 -1
- package/src/recall.ts +14 -37
- package/src/settings-file.ts +7 -20
- package/src/shake.ts +6 -12
- package/src/stale.ts +22 -26
- package/src/status.ts +2 -6
- package/src/tool-calls.ts +2 -3
package/extensions/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { Type } from "typebox";
|
|
2
|
+
|
|
3
|
+
import { type Api, getCurrentSystemMessage, type Message, type Model } from "@earendil-works/pi-ai";
|
|
5
4
|
import {
|
|
6
5
|
convertToLlm,
|
|
7
6
|
type CompactionEntryDraft,
|
|
@@ -17,7 +16,11 @@ import {
|
|
|
17
16
|
type SessionEntry,
|
|
18
17
|
SettingsManager,
|
|
19
18
|
type Theme,
|
|
19
|
+
type TurnEndEvent,
|
|
20
20
|
} from "@earendil-works/pi-coding-agent";
|
|
21
|
+
import { Container, SettingsList, Text } from "@earendil-works/pi-tui";
|
|
22
|
+
import { Type } from "typebox";
|
|
23
|
+
|
|
21
24
|
import {
|
|
22
25
|
BackgroundHandoff,
|
|
23
26
|
type BackgroundNote,
|
|
@@ -41,10 +44,10 @@ import {
|
|
|
41
44
|
} from "../src/context.ts";
|
|
42
45
|
import { handoffSummary } from "../src/handoff.ts";
|
|
43
46
|
import { type MaintenancePlan, planMaintenance } from "../src/maintain.ts";
|
|
44
|
-
import { requestNote } from "../src/note.ts";
|
|
45
47
|
import { migrateLegacy, migrateProject } from "../src/migrate.ts";
|
|
46
|
-
import {
|
|
48
|
+
import { requestNote } from "../src/note.ts";
|
|
47
49
|
import { panelItems, settingValue, summaryText } from "../src/panel.ts";
|
|
50
|
+
import { readEntry, searchEntries } from "../src/recall.ts";
|
|
48
51
|
import {
|
|
49
52
|
createSettings,
|
|
50
53
|
loadSettings,
|
|
@@ -108,26 +111,18 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
108
111
|
};
|
|
109
112
|
|
|
110
113
|
/** Pi still compacts at its own threshold, so a configured threshold can only come earlier. */
|
|
111
|
-
const threshold = (
|
|
112
|
-
model: Model<Api> | undefined,
|
|
113
|
-
config: ContextKitConfig,
|
|
114
|
-
): number => {
|
|
114
|
+
const threshold = (model: Model<Api> | undefined, config: ContextKitConfig): number => {
|
|
115
115
|
const piLimit = piThreshold(model);
|
|
116
116
|
return model && piLimit < Number.POSITIVE_INFINITY
|
|
117
117
|
? Math.min(piLimit, ownThreshold(config, model.contextWindow))
|
|
118
118
|
: piLimit;
|
|
119
119
|
};
|
|
120
120
|
|
|
121
|
-
const epoch = (branch: SessionEntry[]) =>
|
|
122
|
-
getLatestCompactionEntry(branch)?.id ?? null;
|
|
121
|
+
const epoch = (branch: SessionEntry[]) => getLatestCompactionEntry(branch)?.id ?? null;
|
|
123
122
|
|
|
124
|
-
const statusOf = (
|
|
125
|
-
ctx: ExtensionContext,
|
|
126
|
-
config: ContextKitConfig,
|
|
127
|
-
tokens: number,
|
|
128
|
-
) => {
|
|
123
|
+
const statusOf = (ctx: ExtensionContext, config: ContextKitConfig, tokens: number) => {
|
|
129
124
|
if (!config.enabled) return "Context cleanup off";
|
|
130
|
-
const model = ctx.model;
|
|
125
|
+
const model: Model<Api> | undefined = ctx.model;
|
|
131
126
|
if (!model || model.contextWindow <= 0) return undefined;
|
|
132
127
|
return statusText({
|
|
133
128
|
tokens,
|
|
@@ -137,11 +132,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
137
132
|
});
|
|
138
133
|
};
|
|
139
134
|
|
|
140
|
-
function showStatus(
|
|
141
|
-
ctx: ExtensionContext,
|
|
142
|
-
config = settings(ctx),
|
|
143
|
-
tokens = liveTokens(ctx),
|
|
144
|
-
) {
|
|
135
|
+
function showStatus(ctx: ExtensionContext, config = settings(ctx), tokens = liveTokens(ctx)) {
|
|
145
136
|
statusContext = ctx;
|
|
146
137
|
ctx.ui.setStatus("context-kit", statusOf(ctx, config, tokens));
|
|
147
138
|
}
|
|
@@ -155,25 +146,14 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
155
146
|
// Maintenance needs a model with a context window, so `window` is set whenever `output` is.
|
|
156
147
|
const window = turn.ctx.model?.contextWindow;
|
|
157
148
|
if (output && window)
|
|
158
|
-
turn.ctx.ui.notify(
|
|
159
|
-
freedNotice(count([]), count(turn.result.edits), window),
|
|
160
|
-
"info",
|
|
161
|
-
);
|
|
149
|
+
turn.ctx.ui.notify(freedNotice(count([]), count(turn.result.edits), window), "info");
|
|
162
150
|
showStatus(turn.ctx, turn.config, turn.result.tokens);
|
|
163
151
|
return output;
|
|
164
152
|
};
|
|
165
153
|
|
|
166
|
-
const warn = (
|
|
167
|
-
ctx: ExtensionContext,
|
|
168
|
-
error: unknown,
|
|
169
|
-
fallback: string,
|
|
170
|
-
what = "handoff note",
|
|
171
|
-
) => {
|
|
154
|
+
const warn = (ctx: ExtensionContext, error: unknown, fallback: string, what = "handoff note") => {
|
|
172
155
|
const message = error instanceof Error ? error.message : String(error);
|
|
173
|
-
ctx.ui.notify(
|
|
174
|
-
`pi-context-kit: ${what} failed (${message}). ${fallback}`,
|
|
175
|
-
"warning",
|
|
176
|
-
);
|
|
156
|
+
ctx.ui.notify(`pi-context-kit: ${what} failed (${message}). ${fallback}`, "warning");
|
|
177
157
|
};
|
|
178
158
|
|
|
179
159
|
/** Pi's summary at the extension's threshold, because Pi's own hook waits for Pi's threshold. */
|
|
@@ -190,14 +170,12 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
190
170
|
const keptEntryId = recentStart(ctx, keepRecentTokens);
|
|
191
171
|
if (!keptEntryId) return undefined;
|
|
192
172
|
const retained = keptTokens(ctx, keptEntryId);
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
);
|
|
173
|
+
const availableTokens = Math.floor(limit * config.progressRatio - retained);
|
|
174
|
+
if (availableTokens <= 0) return undefined;
|
|
175
|
+
const start = input.entries.findIndex((entry) => entry.sourceEntry.id === keptEntryId);
|
|
197
176
|
// As in Pi, the previous summary goes to the update prompt, not into the history.
|
|
198
177
|
const previous = input.entries.findIndex(
|
|
199
|
-
(entry) =>
|
|
200
|
-
entry.sourceEntry.type === "compaction" && entry.messages.length > 0,
|
|
178
|
+
(entry) => entry.sourceEntry.type === "compaction" && entry.messages.length > 0,
|
|
201
179
|
);
|
|
202
180
|
if (start <= previous + 1) return undefined;
|
|
203
181
|
const source = input.entries[previous]?.sourceEntry;
|
|
@@ -211,9 +189,13 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
211
189
|
undefined,
|
|
212
190
|
source?.type === "compaction" ? source.summary : undefined,
|
|
213
191
|
pi.getThinkingLevel(),
|
|
214
|
-
// The registry resolves auth per request, as for handoff notes.
|
|
192
|
+
// The registry resolves auth per request, as for handoff notes. Pi caps the summary only by
|
|
193
|
+
// the model, so a longer one would be generated and then rejected by `fits` below.
|
|
215
194
|
(model, context, options) =>
|
|
216
|
-
ctx.modelRegistry.streamSimple(model, context,
|
|
195
|
+
ctx.modelRegistry.streamSimple(model, context, {
|
|
196
|
+
...options,
|
|
197
|
+
maxTokens: Math.min(model.maxTokens, availableTokens),
|
|
198
|
+
}),
|
|
217
199
|
undefined,
|
|
218
200
|
undefined,
|
|
219
201
|
undefined,
|
|
@@ -239,7 +221,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
239
221
|
limit: number;
|
|
240
222
|
/** Undefined when only the background note may be used. */
|
|
241
223
|
messages: Message[] | undefined;
|
|
242
|
-
focus?: string;
|
|
224
|
+
focus?: string | undefined;
|
|
243
225
|
signal: AbortSignal;
|
|
244
226
|
cancelBackground: () => void;
|
|
245
227
|
}) => {
|
|
@@ -250,18 +232,12 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
250
232
|
usage: note.usage,
|
|
251
233
|
});
|
|
252
234
|
if (ready) {
|
|
253
|
-
const result = compaction(
|
|
254
|
-
ready,
|
|
255
|
-
firstEntryAfter(branch, ready.leafId) ?? input.keptEntryId,
|
|
256
|
-
);
|
|
235
|
+
const result = compaction(ready, firstEntryAfter(branch, ready.leafId) ?? input.keptEntryId);
|
|
257
236
|
const kept = keptTokens(ctx, result.firstKeptEntryId);
|
|
258
|
-
if (fits(result.summary, kept, input.limit, input.config.progressRatio))
|
|
259
|
-
return result;
|
|
237
|
+
if (fits(result.summary, kept, input.limit, input.config.progressRatio)) return result;
|
|
260
238
|
}
|
|
261
239
|
const retained = keptTokens(ctx, input.keptEntryId);
|
|
262
|
-
const availableTokens = Math.floor(
|
|
263
|
-
input.limit * input.config.progressRatio - retained,
|
|
264
|
-
);
|
|
240
|
+
const availableTokens = Math.floor(input.limit * input.config.progressRatio - retained);
|
|
265
241
|
if (!input.messages || availableTokens <= 0) {
|
|
266
242
|
input.cancelBackground();
|
|
267
243
|
return undefined;
|
|
@@ -279,12 +255,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
279
255
|
});
|
|
280
256
|
if (!note) return undefined;
|
|
281
257
|
const result = compaction(note, input.keptEntryId);
|
|
282
|
-
return fits(
|
|
283
|
-
result.summary,
|
|
284
|
-
retained,
|
|
285
|
-
input.limit,
|
|
286
|
-
input.config.progressRatio,
|
|
287
|
-
)
|
|
258
|
+
return fits(result.summary, retained, input.limit, input.config.progressRatio)
|
|
288
259
|
? result
|
|
289
260
|
: undefined;
|
|
290
261
|
};
|
|
@@ -300,9 +271,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
300
271
|
if (!config.background || !keptEntryId) return;
|
|
301
272
|
if (result.tokens <= limit - speculationLead(limit)) return;
|
|
302
273
|
if (result.tokens > limit) return;
|
|
303
|
-
const budget = Math.floor(
|
|
304
|
-
limit * config.progressRatio - keptTokens(ctx, keptEntryId),
|
|
305
|
-
);
|
|
274
|
+
const budget = Math.floor(limit * config.progressRatio - keptTokens(ctx, keptEntryId));
|
|
306
275
|
if (budget <= 0) return;
|
|
307
276
|
const context = messages();
|
|
308
277
|
const maxTokens = Math.min(model.maxTokens, budget);
|
|
@@ -328,14 +297,12 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
328
297
|
const { ctx, model, limit, current, result } = turn;
|
|
329
298
|
const leafId = ctx.sessionManager.getLeafId();
|
|
330
299
|
if (!leafId) return undefined;
|
|
331
|
-
const messages = () =>
|
|
332
|
-
convertToLlm(projectedMessages(applyEdits(turn.entries, result.edits)));
|
|
300
|
+
const messages = () => convertToLlm(projectedMessages(applyEdits(turn.entries, result.edits)));
|
|
333
301
|
const recent = recentStart(ctx, compactionSettings(model).keepRecentTokens);
|
|
334
302
|
speculate(turn, leafId, recent, messages);
|
|
335
|
-
if (result.tokens <= limit || !recent || failedEpoch === current)
|
|
336
|
-
return undefined;
|
|
303
|
+
if (result.tokens <= limit || !recent || failedEpoch === current) return undefined;
|
|
337
304
|
const writing = background.writing(current);
|
|
338
|
-
if (writing &&
|
|
305
|
+
if (writing && result.tokens < piThreshold(model)) return "waiting" as const;
|
|
339
306
|
return tryHandoff(turn, recent, writing ? undefined : messages);
|
|
340
307
|
};
|
|
341
308
|
|
|
@@ -364,8 +331,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
364
331
|
return compacted(turn.result, compaction);
|
|
365
332
|
}
|
|
366
333
|
} catch (error) {
|
|
367
|
-
if (!signal.aborted)
|
|
368
|
-
warn(ctx, error, "The next methods of the strategy are tried.");
|
|
334
|
+
if (!signal.aborted) warn(ctx, error, "The next methods of the strategy are tried.");
|
|
369
335
|
}
|
|
370
336
|
failedEpoch = current;
|
|
371
337
|
return undefined;
|
|
@@ -397,16 +363,15 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
397
363
|
return undefined;
|
|
398
364
|
};
|
|
399
365
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
}
|
|
366
|
+
const maintainTurn = async (
|
|
367
|
+
event: TurnEndEvent,
|
|
368
|
+
ctx: ExtensionContext,
|
|
369
|
+
config: ContextKitConfig,
|
|
370
|
+
) => {
|
|
406
371
|
const branch = ctx.sessionManager.getBranch();
|
|
407
372
|
const current = epoch(branch);
|
|
408
373
|
background.invalidate(current, branch);
|
|
409
|
-
const model = ctx.model;
|
|
374
|
+
const model: Model<Api> | undefined = ctx.model;
|
|
410
375
|
// Without a model the threshold is infinite, so maintenance plans no edits.
|
|
411
376
|
if (!model) return undefined;
|
|
412
377
|
const limit = threshold(model, config);
|
|
@@ -435,18 +400,32 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
435
400
|
};
|
|
436
401
|
|
|
437
402
|
// Try ordered strategies at Pi's threshold before its native hook can compact.
|
|
438
|
-
const handed = before(methods, "handoff", "soft")
|
|
439
|
-
? await handoffAtTurn(turn)
|
|
440
|
-
: undefined;
|
|
403
|
+
const handed = before(methods, "handoff", "soft") ? await handoffAtTurn(turn) : undefined;
|
|
441
404
|
if (handed === "waiting") return persist(turn);
|
|
442
405
|
if (handed) return handed;
|
|
443
|
-
if (
|
|
444
|
-
turn.result.tokens > limit &&
|
|
445
|
-
!shakeFirst &&
|
|
446
|
-
before(methods, "shake", "soft")
|
|
447
|
-
)
|
|
406
|
+
if (turn.result.tokens > limit && !shakeFirst && before(methods, "shake", "soft"))
|
|
448
407
|
turn.result = plan(true);
|
|
449
408
|
return (await softAtTurn(turn)) ?? persist(turn);
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
pi.on("turn_end", async (event, ctx) => {
|
|
412
|
+
const config = settings(ctx);
|
|
413
|
+
if (!config.enabled || event.outcome === "aborted" || ctx.signal?.aborted) {
|
|
414
|
+
background.cancel();
|
|
415
|
+
showStatus(ctx, config);
|
|
416
|
+
return undefined;
|
|
417
|
+
}
|
|
418
|
+
if (
|
|
419
|
+
event.outcome === "error" ||
|
|
420
|
+
(event.message.role === "assistant" && event.message.stopReason === "length")
|
|
421
|
+
)
|
|
422
|
+
return undefined;
|
|
423
|
+
const result = await maintainTurn(event, ctx, config);
|
|
424
|
+
if (ctx.signal?.aborted) {
|
|
425
|
+
background.cancel();
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
return result;
|
|
450
429
|
});
|
|
451
430
|
|
|
452
431
|
const handoffBeforeCompact = async (
|
|
@@ -465,14 +444,11 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
465
444
|
config,
|
|
466
445
|
branch: branchEntries,
|
|
467
446
|
// A focused /compact needs a note written for that focus.
|
|
468
|
-
ready: customInstructions
|
|
469
|
-
? undefined
|
|
470
|
-
: await background.takeReady(current, branchEntries),
|
|
447
|
+
ready: customInstructions ? undefined : await background.takeReady(current, branchEntries),
|
|
471
448
|
keptEntryId: preparation.firstKeptEntryId,
|
|
472
449
|
limit: Math.min(threshold(model, config), model.contextWindow),
|
|
473
450
|
// The live context no longer fits the model after an overflow.
|
|
474
|
-
messages:
|
|
475
|
-
event.reason === "overflow" || writing ? undefined : liveMessages(ctx),
|
|
451
|
+
messages: event.reason === "overflow" || writing ? undefined : liveMessages(ctx),
|
|
476
452
|
focus: customInstructions,
|
|
477
453
|
signal: event.signal,
|
|
478
454
|
cancelBackground: () => background.cancel(),
|
|
@@ -485,7 +461,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
485
461
|
};
|
|
486
462
|
|
|
487
463
|
pi.on("session_before_compact", async (event, ctx) => {
|
|
488
|
-
const model = ctx.model;
|
|
464
|
+
const model: Model<Api> | undefined = ctx.model;
|
|
489
465
|
const config = settings(ctx);
|
|
490
466
|
if (!model || !handoffEnabled(config)) return undefined;
|
|
491
467
|
const failed = failedEpoch === epoch(event.branchEntries);
|
|
@@ -493,8 +469,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
493
469
|
try {
|
|
494
470
|
return await handoffBeforeCompact(event, ctx, model, config);
|
|
495
471
|
} catch (error) {
|
|
496
|
-
if (!event.signal.aborted)
|
|
497
|
-
warn(ctx, error, "Pi's summary is used instead.");
|
|
472
|
+
if (!event.signal.aborted) warn(ctx, error, "Pi's summary is used instead.");
|
|
498
473
|
return undefined;
|
|
499
474
|
}
|
|
500
475
|
});
|
|
@@ -513,22 +488,15 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
513
488
|
// Pi reports its own unreadable settings. The project file depends on the global block, so
|
|
514
489
|
// neither moves until Pi reads both.
|
|
515
490
|
if (piSettings.drainErrors().length > 0) return;
|
|
516
|
-
const oldGlobal: unknown = Reflect.get(
|
|
517
|
-
|
|
518
|
-
"contextKit",
|
|
519
|
-
);
|
|
520
|
-
const oldProject: unknown = Reflect.get(
|
|
521
|
-
piSettings.getProjectSettings(),
|
|
522
|
-
"contextKit",
|
|
523
|
-
);
|
|
491
|
+
const oldGlobal: unknown = Reflect.get(piSettings.getGlobalSettings(), "contextKit");
|
|
492
|
+
const oldProject: unknown = Reflect.get(piSettings.getProjectSettings(), "contextKit");
|
|
524
493
|
const scopes = [
|
|
525
494
|
{ old: oldGlobal, file: global, content: () => migrateLegacy(oldGlobal) },
|
|
526
495
|
{
|
|
527
496
|
old: oldProject,
|
|
528
497
|
file: project,
|
|
529
498
|
// Runs after the global file is written, which the project file then overrides.
|
|
530
|
-
content: () =>
|
|
531
|
-
migrateProject(oldGlobal, oldProject, readSettingsFile(global)),
|
|
499
|
+
content: () => migrateProject(oldGlobal, oldProject, readSettingsFile(global)),
|
|
532
500
|
},
|
|
533
501
|
];
|
|
534
502
|
for (const { old, file, content } of scopes) {
|
|
@@ -580,9 +548,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
580
548
|
const status = statusOf(ctx, settings(ctx), liveTokens(ctx)) ?? "";
|
|
581
549
|
const { global, project } = paths(ctx);
|
|
582
550
|
const files =
|
|
583
|
-
project && existsSync(project)
|
|
584
|
-
? `${global}, and for this project ${project}`
|
|
585
|
-
: global;
|
|
551
|
+
project && existsSync(project) ? `${global}, and for this project ${project}` : global;
|
|
586
552
|
return `${theme.fg("accent", theme.bold("pi-context-kit"))} ${status}\n${theme.fg("dim", `Saved in ${files}`)}`;
|
|
587
553
|
};
|
|
588
554
|
|
|
@@ -598,9 +564,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
598
564
|
}
|
|
599
565
|
await ctx.ui.custom<undefined>((tui, theme, _keybindings, done) => {
|
|
600
566
|
const items = panelItems(config);
|
|
601
|
-
const saved = new Map<string, string>(
|
|
602
|
-
items.map((item) => [item.id, item.currentValue]),
|
|
603
|
-
);
|
|
567
|
+
const saved = new Map<string, string>(items.map((item) => [item.id, item.currentValue]));
|
|
604
568
|
const header = new Text(panelHeader(ctx, theme));
|
|
605
569
|
const list = new SettingsList(
|
|
606
570
|
items,
|
|
@@ -637,19 +601,12 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
637
601
|
"Other branches are excluded. Images are represented only by MIME type. " +
|
|
638
602
|
"Pass `query` to list matching entries with their IDs, newest first. Pass `entryId` to read one entry's text, with pagination for large entries.",
|
|
639
603
|
parameters: Type.Object({
|
|
640
|
-
query: Type.Optional(
|
|
641
|
-
|
|
642
|
-
),
|
|
643
|
-
regex: Type.Optional(
|
|
644
|
-
Type.Boolean({ description: "Treat query as a regular expression." }),
|
|
645
|
-
),
|
|
646
|
-
entryId: Type.Optional(
|
|
647
|
-
Type.String({ description: "ID of the entry to read." }),
|
|
648
|
-
),
|
|
604
|
+
query: Type.Optional(Type.String({ description: "Text to find, case-insensitive." })),
|
|
605
|
+
regex: Type.Optional(Type.Boolean({ description: "Treat query as a regular expression." })),
|
|
606
|
+
entryId: Type.Optional(Type.String({ description: "ID of the entry to read." })),
|
|
649
607
|
offset: Type.Optional(
|
|
650
608
|
Type.Number({
|
|
651
|
-
description:
|
|
652
|
-
"Matches or characters to skip, as given in the previous result.",
|
|
609
|
+
description: "Matches or characters to skip, as given in the previous result.",
|
|
653
610
|
}),
|
|
654
611
|
),
|
|
655
612
|
limit: Type.Optional(
|
|
@@ -663,11 +620,7 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
663
620
|
let text: string;
|
|
664
621
|
if (params.entryId) text = readEntry(branch, params.entryId, params);
|
|
665
622
|
else if (params.query)
|
|
666
|
-
text = await searchEntries(
|
|
667
|
-
branch,
|
|
668
|
-
{ ...params, query: params.query },
|
|
669
|
-
signal,
|
|
670
|
-
);
|
|
623
|
+
text = await searchEntries(branch, { ...params, query: params.query }, signal);
|
|
671
624
|
else throw new Error("Pass query or entryId.");
|
|
672
625
|
return { content: [{ type: "text", text }], details: undefined };
|
|
673
626
|
},
|
|
@@ -693,25 +646,14 @@ function persisted(result: MaintenancePlan, limit: number) {
|
|
|
693
646
|
: undefined;
|
|
694
647
|
}
|
|
695
648
|
|
|
696
|
-
function compacted(
|
|
697
|
-
result: MaintenancePlan,
|
|
698
|
-
compaction: Omit<CompactionEntryDraft, "type">,
|
|
699
|
-
) {
|
|
649
|
+
function compacted(result: MaintenancePlan, compaction: Omit<CompactionEntryDraft, "type">) {
|
|
700
650
|
return {
|
|
701
|
-
entries: [
|
|
702
|
-
...toDrafts(result.edits),
|
|
703
|
-
{ type: "compaction" as const, ...compaction },
|
|
704
|
-
],
|
|
651
|
+
entries: [...toDrafts(result.edits), { type: "compaction" as const, ...compaction }],
|
|
705
652
|
};
|
|
706
653
|
}
|
|
707
654
|
|
|
708
|
-
function currentTokens(
|
|
709
|
-
ctx
|
|
710
|
-
entries: readonly ProjectedSessionEntry[],
|
|
711
|
-
): number {
|
|
712
|
-
return (
|
|
713
|
-
ctx.getContextUsage()?.tokens ?? contextTokens(projectedMessages(entries))
|
|
714
|
-
);
|
|
655
|
+
function currentTokens(ctx: ExtensionContext, entries: readonly ProjectedSessionEntry[]): number {
|
|
656
|
+
return ctx.getContextUsage()?.tokens ?? contextTokens(projectedMessages(entries));
|
|
715
657
|
}
|
|
716
658
|
|
|
717
659
|
function handoffEnabled(config: ContextKitConfig): boolean {
|
|
@@ -729,33 +671,21 @@ function liveMessages(ctx: ExtensionContext): Message[] {
|
|
|
729
671
|
/** Tokens of the system prompt plus the projected context from `firstKeptEntryId` on. */
|
|
730
672
|
function keptTokens(ctx: ExtensionContext, firstKeptEntryId: string): number {
|
|
731
673
|
const entries = ctx.sessionManager.buildSessionProjection().entries;
|
|
732
|
-
const start = entries.findIndex(
|
|
733
|
-
(entry) => entry.sourceEntry.id === firstKeptEntryId,
|
|
734
|
-
);
|
|
674
|
+
const start = entries.findIndex((entry) => entry.sourceEntry.id === firstKeptEntryId);
|
|
735
675
|
if (start < 0) return Number.POSITIVE_INFINITY;
|
|
736
|
-
const system = projectedMessages(entries)
|
|
737
|
-
.filter((message) => message.role === "system")
|
|
738
|
-
.at(-1);
|
|
676
|
+
const system = getCurrentSystemMessage(projectedMessages(entries));
|
|
739
677
|
return contextTokens([
|
|
740
678
|
...(system ? [system] : []),
|
|
741
|
-
...projectedMessages(entries.slice(start)),
|
|
679
|
+
...projectedMessages(entries.slice(start)).filter((message) => message.role !== "system"),
|
|
742
680
|
]);
|
|
743
681
|
}
|
|
744
682
|
|
|
745
|
-
function fits(
|
|
746
|
-
summary: string,
|
|
747
|
-
kept: number,
|
|
748
|
-
limit: number,
|
|
749
|
-
ratio: number,
|
|
750
|
-
): boolean {
|
|
683
|
+
function fits(summary: string, kept: number, limit: number, ratio: number): boolean {
|
|
751
684
|
return Math.ceil(summary.length / 4) + kept <= limit * ratio;
|
|
752
685
|
}
|
|
753
686
|
|
|
754
687
|
/** Finds the retained boundary from model-visible entries, including prior compaction tails and edits. */
|
|
755
|
-
function recentStart(
|
|
756
|
-
ctx: ExtensionContext,
|
|
757
|
-
keepRecentTokens: number,
|
|
758
|
-
): string | undefined {
|
|
688
|
+
function recentStart(ctx: ExtensionContext, keepRecentTokens: number): string | undefined {
|
|
759
689
|
const entries: SessionEntry[] = [];
|
|
760
690
|
for (const projected of ctx.sessionManager.buildSessionProjection().entries) {
|
|
761
691
|
const source = projected.sourceEntry;
|
package/package.json
CHANGED
|
@@ -1,73 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-context-kit",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Keep long Pi coding agent sessions sharp. Clears old file reads and tool outputs from the context, writes the compaction summary in the background, and lets the agent recall the original text.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"author": "HugoLopes45 (https://github.com/HugoLopes45)",
|
|
8
|
-
"repository": {
|
|
9
|
-
"type": "git",
|
|
10
|
-
"url": "git+https://github.com/HugoLopes45/pi-context-kit.git"
|
|
11
|
-
},
|
|
12
|
-
"homepage": "https://github.com/HugoLopes45/pi-context-kit#readme",
|
|
13
|
-
"bugs": {
|
|
14
|
-
"url": "https://github.com/HugoLopes45/pi-context-kit/issues"
|
|
15
|
-
},
|
|
16
5
|
"keywords": [
|
|
17
|
-
"
|
|
18
|
-
"pi",
|
|
19
|
-
"pi-extension",
|
|
20
|
-
"pi-coding-agent",
|
|
6
|
+
"coding-agent",
|
|
21
7
|
"compaction",
|
|
22
8
|
"context",
|
|
23
|
-
"context-window",
|
|
24
9
|
"context-management",
|
|
25
|
-
"
|
|
26
|
-
"llm",
|
|
10
|
+
"context-window",
|
|
27
11
|
"handoff",
|
|
12
|
+
"llm",
|
|
13
|
+
"pi",
|
|
14
|
+
"pi-coding-agent",
|
|
15
|
+
"pi-extension",
|
|
16
|
+
"pi-package",
|
|
28
17
|
"token-optimization"
|
|
29
18
|
],
|
|
19
|
+
"homepage": "https://github.com/HugoLopes45/pi-extensions/tree/main/packages/pi-context-kit#readme",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/HugoLopes45/pi-extensions/issues"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "Hugo Lopes (https://github.com/HugoLopes45)",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/HugoLopes45/pi-extensions.git",
|
|
28
|
+
"directory": "packages/pi-context-kit"
|
|
29
|
+
},
|
|
30
30
|
"files": [
|
|
31
31
|
"extensions",
|
|
32
32
|
"src",
|
|
33
33
|
"context-kit.schema.json",
|
|
34
34
|
"CHANGELOG.md"
|
|
35
35
|
],
|
|
36
|
-
"
|
|
37
|
-
"node": ">=22.19.0"
|
|
38
|
-
},
|
|
39
|
-
"pi": {
|
|
40
|
-
"extensions": [
|
|
41
|
-
"./extensions/index.ts"
|
|
42
|
-
]
|
|
43
|
-
},
|
|
36
|
+
"type": "module",
|
|
44
37
|
"scripts": {
|
|
45
|
-
"check": "tsc --noEmit &&
|
|
46
|
-
"lint": "node scripts/lint.ts",
|
|
38
|
+
"check": "tsc --noEmit && vitest --run",
|
|
47
39
|
"prepublishOnly": "npm run check"
|
|
48
40
|
},
|
|
49
|
-
"peerDependencies": {
|
|
50
|
-
"@earendil-works/pi-ai": "*",
|
|
51
|
-
"@earendil-works/pi-coding-agent": "*",
|
|
52
|
-
"@earendil-works/pi-tui": "*",
|
|
53
|
-
"typebox": "*"
|
|
54
|
-
},
|
|
55
41
|
"devDependencies": {
|
|
56
42
|
"@earendil-works/pi-ai": "^1.5.2",
|
|
57
43
|
"@earendil-works/pi-coding-agent": "^1.5.2",
|
|
58
44
|
"@earendil-works/pi-tui": "^1.5.2",
|
|
59
|
-
"@types/node": "^26.6.
|
|
60
|
-
"
|
|
61
|
-
"typebox": "^1.3.27",
|
|
45
|
+
"@types/node": "^26.6.6",
|
|
46
|
+
"typebox": "^1.4.3",
|
|
62
47
|
"typescript": "^7.0.2",
|
|
63
48
|
"vitest": "^5.0.3"
|
|
64
49
|
},
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"
|
|
70
|
-
|
|
71
|
-
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@earendil-works/pi-ai": "*",
|
|
52
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
53
|
+
"@earendil-works/pi-tui": "*",
|
|
54
|
+
"typebox": "*"
|
|
55
|
+
},
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=22.19.0",
|
|
58
|
+
"pi": ">=1.0.3"
|
|
59
|
+
},
|
|
60
|
+
"pi": {
|
|
61
|
+
"extensions": [
|
|
62
|
+
"./extensions/index.ts"
|
|
63
|
+
]
|
|
72
64
|
}
|
|
73
65
|
}
|
package/src/background.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
|
3
3
|
|
|
4
4
|
export interface Note {
|
|
5
5
|
note: string;
|
|
6
|
-
usage
|
|
6
|
+
usage: Usage;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface BackgroundNote extends Note {
|
|
@@ -103,8 +103,7 @@ export class BackgroundHandoff {
|
|
|
103
103
|
invalidate(epoch: string | null, branch: readonly SessionEntry[]): void {
|
|
104
104
|
if (
|
|
105
105
|
this.#job &&
|
|
106
|
-
(this.#job.epoch !== epoch ||
|
|
107
|
-
!branch.some((entry) => entry.id === this.#job?.leafId))
|
|
106
|
+
(this.#job.epoch !== epoch || !branch.some((entry) => entry.id === this.#job?.leafId))
|
|
108
107
|
) {
|
|
109
108
|
this.cancel();
|
|
110
109
|
}
|