@lune_99/pi-recap 0.0.0-stage → 0.2.0
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 +67 -0
- package/LICENSE +21 -0
- package/README.md +75 -2
- package/dist/extension.d.ts +5 -0
- package/dist/extension.js +75 -0
- package/dist/focus.d.ts +5 -0
- package/dist/focus.js +23 -0
- package/dist/generate.d.ts +31 -0
- package/dist/generate.js +157 -0
- package/dist/input.d.ts +50 -0
- package/dist/input.js +138 -0
- package/dist/panel.d.ts +10 -0
- package/dist/panel.js +54 -0
- package/dist/runtime.d.ts +124 -0
- package/dist/runtime.js +624 -0
- package/dist/settings.d.ts +28 -0
- package/dist/settings.js +39 -0
- package/docs/recap-settings.png +0 -0
- package/docs/recap-settings.svg +22 -0
- package/docs/recap-widget.png +0 -0
- package/docs/recap-widget.svg +9 -0
- package/package.json +69 -5
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
import { clampThinkingLevel } from "@earendil-works/pi-ai";
|
|
2
|
+
import { wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
3
|
+
import { observeTerminalFocus } from "./focus.js";
|
|
4
|
+
import { collectRecapInput, fingerprintRecapInput } from "./input.js";
|
|
5
|
+
import { createRecapFailure, generateRecap, recapErrorMessage, RecapGenerationError, } from "./generate.js";
|
|
6
|
+
import { maxTimeoutMs, resolveRecapModel } from "./settings.js";
|
|
7
|
+
const entryType = "pi-recap.v1";
|
|
8
|
+
// A round counts as substantial work with this many tool results or this much
|
|
9
|
+
// time from the prompt to the final reply.
|
|
10
|
+
const minTools = 5;
|
|
11
|
+
const minDuration = 60_000;
|
|
12
|
+
export class RecapRuntime {
|
|
13
|
+
ctx;
|
|
14
|
+
// Focus starts unknown so automation never assumes the terminal is away.
|
|
15
|
+
focus = "unknown";
|
|
16
|
+
timer;
|
|
17
|
+
request;
|
|
18
|
+
// Every invalidation advances the generation so late request results are discarded.
|
|
19
|
+
generation = 0;
|
|
20
|
+
disposed = false;
|
|
21
|
+
prompts = 0;
|
|
22
|
+
settledAt = 0;
|
|
23
|
+
outcome;
|
|
24
|
+
running = false;
|
|
25
|
+
pendingDisplay;
|
|
26
|
+
// Widget visibility belongs to this runtime, never to persisted session data.
|
|
27
|
+
displayedKey;
|
|
28
|
+
// Runtime-observed outcomes are authoritative when the persisted transcript
|
|
29
|
+
// cannot distinguish an aborted round from a clean completion.
|
|
30
|
+
outcomes = new Map();
|
|
31
|
+
// Records are keyed by the exact recap request identity.
|
|
32
|
+
records = new Map();
|
|
33
|
+
releases = [];
|
|
34
|
+
effectiveSettings;
|
|
35
|
+
// Last request usage for diagnostics. Recap usage is not part of session usage.
|
|
36
|
+
diagnostics;
|
|
37
|
+
releaseFocus;
|
|
38
|
+
focusInput;
|
|
39
|
+
pi;
|
|
40
|
+
view;
|
|
41
|
+
constructor(pi, ctx, view, focusInput = process.stdin) {
|
|
42
|
+
view.signal.throwIfAborted();
|
|
43
|
+
this.pi = pi;
|
|
44
|
+
this.ctx = ctx;
|
|
45
|
+
this.view = view;
|
|
46
|
+
this.focusInput = focusInput;
|
|
47
|
+
this.effectiveSettings = this.settingsKey();
|
|
48
|
+
this.restore();
|
|
49
|
+
this.releases.push(view.subscribe(() => {
|
|
50
|
+
const next = this.settingsKey();
|
|
51
|
+
if (next !== this.effectiveSettings) {
|
|
52
|
+
this.effectiveSettings = next;
|
|
53
|
+
this.invalidate();
|
|
54
|
+
this.bindFocus();
|
|
55
|
+
}
|
|
56
|
+
this.schedule();
|
|
57
|
+
}));
|
|
58
|
+
// Core owns the view; its retirement must also stop Recap's business resources.
|
|
59
|
+
const onRetire = () => this.dispose();
|
|
60
|
+
view.signal.addEventListener("abort", onRetire, { once: true });
|
|
61
|
+
this.releases.push(() => view.signal.removeEventListener("abort", onRetire));
|
|
62
|
+
this.bindFocus();
|
|
63
|
+
}
|
|
64
|
+
settings() {
|
|
65
|
+
return this.view.snapshot();
|
|
66
|
+
}
|
|
67
|
+
settingsKey() {
|
|
68
|
+
// Changing the delay only re-arms the timer, even if a recap is already in flight.
|
|
69
|
+
const { autoDelaySeconds: _autoDelaySeconds, ...settings } = this.settings();
|
|
70
|
+
return JSON.stringify(settings);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Capture the exact recap input and derive its identity from the same
|
|
74
|
+
* representation that will be sent to the model.
|
|
75
|
+
*/
|
|
76
|
+
captureInput() {
|
|
77
|
+
const input = collectRecapInput(this.ctx);
|
|
78
|
+
return {
|
|
79
|
+
input,
|
|
80
|
+
fingerprint: fingerprintRecapInput(input),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Focus observation exists only while automatic recap is enabled.
|
|
85
|
+
*/
|
|
86
|
+
bindFocus() {
|
|
87
|
+
if (this.ctx.mode === "tui" && this.view.get("enabled") && this.releaseFocus) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.releaseFocus?.();
|
|
91
|
+
this.releaseFocus = undefined;
|
|
92
|
+
this.focus = "unknown";
|
|
93
|
+
if (this.ctx.mode !== "tui" || !this.view.get("enabled")) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this.releaseFocus = observeTerminalFocus(this.focusInput, (focus) => {
|
|
97
|
+
this.focus = focus;
|
|
98
|
+
if (focus === "in") {
|
|
99
|
+
this.clearTimer();
|
|
100
|
+
this.showCached();
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
this.schedule();
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Manual and automatic entry points share the same runtime safety gate.
|
|
109
|
+
*/
|
|
110
|
+
ready() {
|
|
111
|
+
return (!this.disposed &&
|
|
112
|
+
this.ctx.mode === "tui" &&
|
|
113
|
+
!this.running &&
|
|
114
|
+
this.ctx.isIdle() &&
|
|
115
|
+
!this.ctx.hasPendingMessages() &&
|
|
116
|
+
this.prompts === 0);
|
|
117
|
+
}
|
|
118
|
+
clearTimer() {
|
|
119
|
+
if (this.timer) {
|
|
120
|
+
clearTimeout(this.timer);
|
|
121
|
+
}
|
|
122
|
+
this.timer = undefined;
|
|
123
|
+
}
|
|
124
|
+
update(ctx) {
|
|
125
|
+
this.ctx = ctx;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Abort in-flight work and remove any hint derived from stale state.
|
|
129
|
+
*/
|
|
130
|
+
invalidate() {
|
|
131
|
+
this.clearTimer();
|
|
132
|
+
this.generation++;
|
|
133
|
+
this.request?.controller.abort();
|
|
134
|
+
this.request = undefined;
|
|
135
|
+
this.pendingDisplay = undefined;
|
|
136
|
+
this.displayedKey = undefined;
|
|
137
|
+
clearRecap(this.ctx);
|
|
138
|
+
}
|
|
139
|
+
start(ctx) {
|
|
140
|
+
this.update(ctx);
|
|
141
|
+
this.invalidate();
|
|
142
|
+
this.running = true;
|
|
143
|
+
this.outcome = undefined;
|
|
144
|
+
}
|
|
145
|
+
beforeSettle(outcome, ctx) {
|
|
146
|
+
this.update(ctx);
|
|
147
|
+
this.outcome = outcome;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* A round is considered completed only when this runtime observed a clean
|
|
151
|
+
* settle and the corresponding assistant response stopped normally.
|
|
152
|
+
*/
|
|
153
|
+
settled(ctx) {
|
|
154
|
+
this.update(ctx);
|
|
155
|
+
this.running = false;
|
|
156
|
+
this.settledAt = Date.now();
|
|
157
|
+
const branch = ctx.sessionManager.getBranch();
|
|
158
|
+
const user = branch.findLast((entry) => entry.type === "message" && entry.message.role === "user");
|
|
159
|
+
const assistant = branch.findLast((entry) => entry.type === "message" && entry.message.role === "assistant");
|
|
160
|
+
if (user) {
|
|
161
|
+
const completed = this.outcome === "completed" &&
|
|
162
|
+
assistant?.type === "message" &&
|
|
163
|
+
assistant.message.role === "assistant" &&
|
|
164
|
+
assistant.message.stopReason === "stop";
|
|
165
|
+
this.outcomes.set(user.id, completed);
|
|
166
|
+
if (this.view.get("enabled")) {
|
|
167
|
+
this.pi.appendEntry(`${entryType}.settled`, {
|
|
168
|
+
userId: user.id,
|
|
169
|
+
at: this.settledAt,
|
|
170
|
+
completed,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
this.schedule();
|
|
175
|
+
}
|
|
176
|
+
input(ctx) {
|
|
177
|
+
this.update(ctx);
|
|
178
|
+
this.invalidate();
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Cached records belong to the previous branch view and must be rebuilt
|
|
182
|
+
* from the newly selected branch.
|
|
183
|
+
*/
|
|
184
|
+
branchChanged(ctx) {
|
|
185
|
+
this.update(ctx);
|
|
186
|
+
this.invalidate();
|
|
187
|
+
this.records.clear();
|
|
188
|
+
this.restore();
|
|
189
|
+
this.schedule();
|
|
190
|
+
}
|
|
191
|
+
prompt(start, ctx) {
|
|
192
|
+
this.update(ctx);
|
|
193
|
+
this.prompts = Math.max(0, this.prompts + (start ? 1 : -1));
|
|
194
|
+
if (start) {
|
|
195
|
+
this.clearTimer();
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
this.showCached();
|
|
199
|
+
this.schedule();
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Pi removes extension widgets before /reload, and a restarted process
|
|
203
|
+
* starts with none. Show the saved recap that still matches the restored
|
|
204
|
+
* session; with automatic recap off, only one the user already saw.
|
|
205
|
+
*/
|
|
206
|
+
sessionStarted(ctx) {
|
|
207
|
+
this.update(ctx);
|
|
208
|
+
if (ctx.mode !== "tui") {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
const data = this.records.get(this.snapshot().key);
|
|
213
|
+
if (data?.text && (this.view.get("enabled") || data.displayed)) {
|
|
214
|
+
this.display(data);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
// Resolution errors are reported by explicit request attempts.
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Restore persisted recap records and settle markers without re-running a
|
|
223
|
+
* request merely because the process or extension was reloaded.
|
|
224
|
+
*/
|
|
225
|
+
restore() {
|
|
226
|
+
this.outcomes.clear();
|
|
227
|
+
this.records.clear();
|
|
228
|
+
this.settledAt = 0;
|
|
229
|
+
let userId;
|
|
230
|
+
for (const entry of this.ctx.sessionManager.getBranch()) {
|
|
231
|
+
if (entry.type === "message") {
|
|
232
|
+
if (entry.message.role === "user") {
|
|
233
|
+
userId = entry.id;
|
|
234
|
+
this.settledAt = 0;
|
|
235
|
+
}
|
|
236
|
+
else if (entry.message.role === "assistant") {
|
|
237
|
+
// A restored round can lack a marker if automatic recap was off.
|
|
238
|
+
this.settledAt = Date.parse(entry.timestamp) || 0;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (entry.type !== "custom") {
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (entry.customType === entryType) {
|
|
245
|
+
const data = entry.data;
|
|
246
|
+
if (!data || typeof data.key !== "string") {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
this.records.set(data.key, data);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (entry.customType === `${entryType}.settled`) {
|
|
253
|
+
const data = entry.data;
|
|
254
|
+
if (userId !== undefined &&
|
|
255
|
+
data?.userId === userId &&
|
|
256
|
+
typeof data.at === "number" &&
|
|
257
|
+
Number.isFinite(data.at)) {
|
|
258
|
+
this.settledAt = data.at;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Automatic recap is worth a model request only after a cleanly completed
|
|
265
|
+
* round that did real work, measured on the latest such round because that
|
|
266
|
+
* is the work the returning user has not seen.
|
|
267
|
+
*
|
|
268
|
+
* Runtime-observed outcomes win because persisted assistant messages alone
|
|
269
|
+
* cannot always distinguish interrupted work from a completed round.
|
|
270
|
+
*/
|
|
271
|
+
hasSubstantialRound() {
|
|
272
|
+
const rounds = new Map();
|
|
273
|
+
let current;
|
|
274
|
+
for (const entry of this.ctx.sessionManager.getBranch()) {
|
|
275
|
+
if (entry.type === "message" && entry.message.role === "user") {
|
|
276
|
+
const startedAt = Date.parse(entry.timestamp);
|
|
277
|
+
current = { completed: false, startedAt, endedAt: startedAt, tools: 0 };
|
|
278
|
+
rounds.set(entry.id, current);
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (entry.type === "message" && current) {
|
|
282
|
+
if (entry.message.role === "toolResult") {
|
|
283
|
+
current.tools++;
|
|
284
|
+
}
|
|
285
|
+
else if (entry.message.role === "assistant") {
|
|
286
|
+
current.completed = entry.message.stopReason === "stop";
|
|
287
|
+
current.endedAt = Date.parse(entry.timestamp);
|
|
288
|
+
}
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (entry.type === "custom" && entry.customType === `${entryType}.settled`) {
|
|
292
|
+
const data = entry.data;
|
|
293
|
+
if (typeof data?.userId === "string") {
|
|
294
|
+
const round = rounds.get(data.userId);
|
|
295
|
+
if (round) {
|
|
296
|
+
round.completed = data.completed === true;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
for (const [id, completed] of this.outcomes) {
|
|
302
|
+
const round = rounds.get(id);
|
|
303
|
+
if (round) {
|
|
304
|
+
round.completed = completed;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const latest = [...rounds.values()].findLast((round) => round.completed);
|
|
308
|
+
// An unparsable timestamp yields NaN, which fails the duration test.
|
|
309
|
+
return (latest !== undefined &&
|
|
310
|
+
(latest.tools >= minTools || latest.endedAt - latest.startedAt >= minDuration));
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Build the identity of one model request.
|
|
314
|
+
*
|
|
315
|
+
* Only request-affecting state belongs in the cache key. Runtime scheduling
|
|
316
|
+
* state such as enabled/disabled or settledAt must not force regeneration.
|
|
317
|
+
*/
|
|
318
|
+
snapshot(captured = this.captureInput()) {
|
|
319
|
+
const settings = this.settings();
|
|
320
|
+
const model = resolveRecapModel(this.ctx, settings);
|
|
321
|
+
if (!model) {
|
|
322
|
+
throw new Error(settings.model
|
|
323
|
+
? `Recap model ${settings.model.provider}/${settings.model.id} is not registered`
|
|
324
|
+
: "Select a session or recap model first");
|
|
325
|
+
}
|
|
326
|
+
const thinking = clampThinkingLevel(model, settings.thinkingLevel);
|
|
327
|
+
const key = JSON.stringify([
|
|
328
|
+
this.ctx.sessionManager.getSessionId(),
|
|
329
|
+
captured.fingerprint,
|
|
330
|
+
model.provider,
|
|
331
|
+
model.id,
|
|
332
|
+
thinking,
|
|
333
|
+
]);
|
|
334
|
+
return {
|
|
335
|
+
...captured,
|
|
336
|
+
model,
|
|
337
|
+
thinking,
|
|
338
|
+
maxRetries: settings.maxRetries,
|
|
339
|
+
key,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Failed model resolution also needs a stable identity so automatic
|
|
344
|
+
* retries wait for meaningful state to change.
|
|
345
|
+
*/
|
|
346
|
+
unresolvedKey(captured) {
|
|
347
|
+
const settings = this.settings();
|
|
348
|
+
return JSON.stringify([
|
|
349
|
+
"unresolved",
|
|
350
|
+
this.ctx.sessionManager.getSessionId(),
|
|
351
|
+
captured.fingerprint,
|
|
352
|
+
settings.model,
|
|
353
|
+
settings.thinkingLevel,
|
|
354
|
+
this.ctx.model?.provider,
|
|
355
|
+
this.ctx.model?.id,
|
|
356
|
+
]);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Persistence receives a complete record. It never re-reads session input,
|
|
360
|
+
* because doing so could associate a result with a newer snapshot.
|
|
361
|
+
*/
|
|
362
|
+
remember(data) {
|
|
363
|
+
const record = {
|
|
364
|
+
...data,
|
|
365
|
+
settledAt: this.settledAt,
|
|
366
|
+
};
|
|
367
|
+
this.records.set(record.key, record);
|
|
368
|
+
this.pi.appendEntry(entryType, record);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* If the terminal is temporarily busy, retain the record key and display
|
|
372
|
+
* the same cached result once the runtime becomes ready.
|
|
373
|
+
*/
|
|
374
|
+
display(data) {
|
|
375
|
+
if (!data.text) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (!this.ready()) {
|
|
379
|
+
this.pendingDisplay = data.key;
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
this.pendingDisplay = undefined;
|
|
383
|
+
showRecap(this.ctx, data.text);
|
|
384
|
+
this.displayedKey = data.key;
|
|
385
|
+
// Showing a recap again after reload or focus-in must not grow the session.
|
|
386
|
+
if (!data.displayed) {
|
|
387
|
+
this.remember({
|
|
388
|
+
...data,
|
|
389
|
+
displayed: true,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
showCached() {
|
|
394
|
+
if (!this.ready() || (!this.view.get("enabled") && !this.pendingDisplay)) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
try {
|
|
398
|
+
const data = this.records.get(this.snapshot().key);
|
|
399
|
+
if (data?.text &&
|
|
400
|
+
(data.key !== this.displayedKey || data.key === this.pendingDisplay)) {
|
|
401
|
+
this.display(data);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
// Resolution errors are reported by explicit request attempts.
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
recapDeadline() {
|
|
409
|
+
return this.settledAt + this.view.get("autoDelaySeconds") * 1000;
|
|
410
|
+
}
|
|
411
|
+
schedule() {
|
|
412
|
+
this.clearTimer();
|
|
413
|
+
if (!this.ready() ||
|
|
414
|
+
!this.view.get("enabled") ||
|
|
415
|
+
this.focus !== "out" ||
|
|
416
|
+
!this.settledAt ||
|
|
417
|
+
!this.hasSubstantialRound() ||
|
|
418
|
+
this.request) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const wait = this.recapDeadline() - Date.now();
|
|
422
|
+
if (wait <= 0) {
|
|
423
|
+
void this.run(false);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
// Timers use a monotonic clock while the delay is measured against the
|
|
427
|
+
// wall clock, so a timer can fire a millisecond early or after a clock
|
|
428
|
+
// adjustment. Re-evaluating keeps that from silently dropping the recap.
|
|
429
|
+
this.timer = setTimeout(() => {
|
|
430
|
+
this.timer = undefined;
|
|
431
|
+
this.schedule();
|
|
432
|
+
}, Math.min(wait, maxTimeoutMs));
|
|
433
|
+
// Automatic recap must never keep the process alive.
|
|
434
|
+
this.timer.unref?.();
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Manual recap skips focus, delay, and completed-round thresholds while
|
|
438
|
+
* retaining the same idle and prompt-safety conditions.
|
|
439
|
+
*/
|
|
440
|
+
async manual(ctx) {
|
|
441
|
+
this.update(ctx);
|
|
442
|
+
if (ctx.mode !== "tui") {
|
|
443
|
+
ctx.ui.notify("Recap requires an interactive terminal", "warning");
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (!this.ready()) {
|
|
447
|
+
ctx.ui.notify("Run /recap after the current task and pending prompts have finished", "warning");
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
await this.run(true);
|
|
451
|
+
}
|
|
452
|
+
async run(manual) {
|
|
453
|
+
if (!this.ready()) {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (!manual &&
|
|
457
|
+
(!this.view.get("enabled") ||
|
|
458
|
+
this.focus !== "out" ||
|
|
459
|
+
!this.hasSubstantialRound() ||
|
|
460
|
+
Date.now() < this.recapDeadline())) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const captured = this.captureInput();
|
|
464
|
+
if (!captured.input.hasContent) {
|
|
465
|
+
if (manual) {
|
|
466
|
+
this.ctx.ui.notify("No conversation content to recap yet", "info");
|
|
467
|
+
}
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
let snapshot;
|
|
471
|
+
try {
|
|
472
|
+
snapshot = this.snapshot(captured);
|
|
473
|
+
}
|
|
474
|
+
catch (error) {
|
|
475
|
+
const key = this.unresolvedKey(captured);
|
|
476
|
+
// Automatic retries wait for the unresolved request identity to
|
|
477
|
+
// change; manual recap always permits an explicit retry.
|
|
478
|
+
if (!manual && this.records.get(key)?.failed) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const message = recapErrorMessage(error);
|
|
482
|
+
this.remember({
|
|
483
|
+
key,
|
|
484
|
+
failed: true,
|
|
485
|
+
fingerprint: captured.fingerprint,
|
|
486
|
+
failure: createRecapFailure({
|
|
487
|
+
kind: "model",
|
|
488
|
+
attempts: 0,
|
|
489
|
+
maxRetries: this.settings().maxRetries,
|
|
490
|
+
message,
|
|
491
|
+
}),
|
|
492
|
+
});
|
|
493
|
+
this.ctx.ui.notify(message, "warning");
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const previous = this.records.get(snapshot.key);
|
|
497
|
+
if (previous?.text) {
|
|
498
|
+
if (manual) {
|
|
499
|
+
this.display(previous);
|
|
500
|
+
}
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
if (!manual && previous?.failed) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (this.request?.key === snapshot.key) {
|
|
507
|
+
await this.request.promise;
|
|
508
|
+
const cached = this.records.get(snapshot.key);
|
|
509
|
+
if (manual && cached?.text && cached.key !== this.displayedKey) {
|
|
510
|
+
this.display(cached);
|
|
511
|
+
}
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
this.clearTimer();
|
|
515
|
+
const generation = this.generation;
|
|
516
|
+
const ctx = this.ctx;
|
|
517
|
+
const controller = new AbortController();
|
|
518
|
+
const signal = ctx.signal
|
|
519
|
+
? AbortSignal.any([ctx.signal, controller.signal])
|
|
520
|
+
: controller.signal;
|
|
521
|
+
const request = {
|
|
522
|
+
key: snapshot.key,
|
|
523
|
+
controller,
|
|
524
|
+
promise: Promise.resolve(),
|
|
525
|
+
};
|
|
526
|
+
this.request = request;
|
|
527
|
+
/**
|
|
528
|
+
* A completed provider request may still be stale if the branch,
|
|
529
|
+
* settings, model selection, or projected conversation changed while
|
|
530
|
+
* it was running.
|
|
531
|
+
*/
|
|
532
|
+
const valid = () => {
|
|
533
|
+
if (this.disposed || generation !== this.generation || signal.aborted) {
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
try {
|
|
537
|
+
return this.snapshot().key === snapshot.key;
|
|
538
|
+
}
|
|
539
|
+
catch {
|
|
540
|
+
return false;
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
request.promise = (async () => {
|
|
544
|
+
let attempts = 0;
|
|
545
|
+
try {
|
|
546
|
+
const result = await generateRecap(ctx, snapshot.input, snapshot.model, snapshot.thinking, signal, snapshot.maxRetries);
|
|
547
|
+
attempts = result.attempts;
|
|
548
|
+
if (!valid()) {
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
this.diagnostics = result;
|
|
552
|
+
const data = {
|
|
553
|
+
key: snapshot.key,
|
|
554
|
+
text: result.text,
|
|
555
|
+
fingerprint: snapshot.fingerprint,
|
|
556
|
+
};
|
|
557
|
+
this.remember(data);
|
|
558
|
+
if (manual || this.focus === "in") {
|
|
559
|
+
this.display(data);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
catch (error) {
|
|
563
|
+
if (!valid() || (error instanceof Error && error.name === "AbortError")) {
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
const failure = error instanceof RecapGenerationError
|
|
567
|
+
? error.failure
|
|
568
|
+
: createRecapFailure({
|
|
569
|
+
kind: "runtime",
|
|
570
|
+
attempts,
|
|
571
|
+
maxRetries: snapshot.maxRetries,
|
|
572
|
+
message: "Recap runtime failed",
|
|
573
|
+
});
|
|
574
|
+
this.remember({
|
|
575
|
+
key: snapshot.key,
|
|
576
|
+
failed: true,
|
|
577
|
+
fingerprint: snapshot.fingerprint,
|
|
578
|
+
failure,
|
|
579
|
+
});
|
|
580
|
+
const after = failure.attempts
|
|
581
|
+
? ` after ${failure.attempts} attempt${failure.attempts === 1 ? "" : "s"}`
|
|
582
|
+
: "";
|
|
583
|
+
ctx.ui.notify(`Recap failed${after}: ${recapErrorMessage(error)}`, "warning");
|
|
584
|
+
}
|
|
585
|
+
finally {
|
|
586
|
+
if (this.request === request) {
|
|
587
|
+
this.request = undefined;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
})();
|
|
591
|
+
await request.promise;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Shutdown and session reload may both dispose the same runtime.
|
|
595
|
+
*/
|
|
596
|
+
dispose() {
|
|
597
|
+
if (this.disposed) {
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
this.disposed = true;
|
|
601
|
+
this.invalidate();
|
|
602
|
+
this.releaseFocus?.();
|
|
603
|
+
this.releaseFocus = undefined;
|
|
604
|
+
for (const release of this.releases) {
|
|
605
|
+
release();
|
|
606
|
+
}
|
|
607
|
+
this.records.clear();
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* A widget can be removed when the next round starts; notify() would leave
|
|
612
|
+
* stale recap text in the terminal history.
|
|
613
|
+
*/
|
|
614
|
+
export function showRecap(ctx, text) {
|
|
615
|
+
ctx.ui.setWidget("pi-recap", (_tui, theme) => ({
|
|
616
|
+
render: (width) => wrapTextWithAnsi(`☾ recap · ${text}`, width).map((line) => theme.fg("dim", line)),
|
|
617
|
+
invalidate() { },
|
|
618
|
+
}));
|
|
619
|
+
}
|
|
620
|
+
export function clearRecap(ctx) {
|
|
621
|
+
if (ctx.mode === "tui") {
|
|
622
|
+
ctx.ui.setWidget("pi-recap", undefined);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Type, type Static } from "typebox";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { type SettingsView } from "@lune_99/pi-lune-extensions-core/settings";
|
|
4
|
+
export declare const maxTimeoutMs = 2147483647;
|
|
5
|
+
export declare const recapSchema: Type.TObject<{
|
|
6
|
+
enabled: Type.TBoolean;
|
|
7
|
+
autoDelaySeconds: Type.TInteger;
|
|
8
|
+
maxRetries: Type.TInteger;
|
|
9
|
+
model: Type.TUnion<[Type.TNull, Type.TObject<{
|
|
10
|
+
provider: Type.TString;
|
|
11
|
+
id: Type.TString;
|
|
12
|
+
}>]>;
|
|
13
|
+
thinkingLevel: Type.TUnion<[Type.TLiteral<"off">, Type.TLiteral<"minimal">, Type.TLiteral<"low">, Type.TLiteral<"medium">, Type.TLiteral<"high">, Type.TLiteral<"xhigh">, Type.TLiteral<"max">]>;
|
|
14
|
+
}>;
|
|
15
|
+
export type RecapSettings = Static<typeof recapSchema>;
|
|
16
|
+
export type RecapSettingsView = SettingsView<typeof recapSchema>;
|
|
17
|
+
/** Resolve references at use time so registry changes cannot leave a cached model behind. */
|
|
18
|
+
export declare function resolveRecapModel(ctx: ExtensionContext, settings: RecapSettings): import("@earendil-works/pi-ai").Model<any> | undefined;
|
|
19
|
+
export declare const recapSettings: import("@lune_99/pi-lune-extensions-core/settings").DefinedSettings<Type.TObject<{
|
|
20
|
+
enabled: Type.TBoolean;
|
|
21
|
+
autoDelaySeconds: Type.TInteger;
|
|
22
|
+
maxRetries: Type.TInteger;
|
|
23
|
+
model: Type.TUnion<[Type.TNull, Type.TObject<{
|
|
24
|
+
provider: Type.TString;
|
|
25
|
+
id: Type.TString;
|
|
26
|
+
}>]>;
|
|
27
|
+
thinkingLevel: Type.TUnion<[Type.TLiteral<"off">, Type.TLiteral<"minimal">, Type.TLiteral<"low">, Type.TLiteral<"medium">, Type.TLiteral<"high">, Type.TLiteral<"xhigh">, Type.TLiteral<"max">]>;
|
|
28
|
+
}>>;
|
package/dist/settings.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
import { defineSettings } from "@lune_99/pi-lune-extensions-core/settings";
|
|
3
|
+
// Node treats a delay above this limit as 1 ms.
|
|
4
|
+
export const maxTimeoutMs = 2_147_483_647;
|
|
5
|
+
export const recapSchema = Type.Object({
|
|
6
|
+
enabled: Type.Boolean({ default: false }),
|
|
7
|
+
autoDelaySeconds: Type.Integer({
|
|
8
|
+
minimum: 0,
|
|
9
|
+
maximum: Math.floor(maxTimeoutMs / 1000),
|
|
10
|
+
default: 180,
|
|
11
|
+
}),
|
|
12
|
+
maxRetries: Type.Integer({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER, default: 3 }),
|
|
13
|
+
model: Type.Union([
|
|
14
|
+
Type.Null(),
|
|
15
|
+
Type.Object({
|
|
16
|
+
provider: Type.String({ minLength: 1 }),
|
|
17
|
+
id: Type.String({ minLength: 1 }),
|
|
18
|
+
}),
|
|
19
|
+
], { default: null }),
|
|
20
|
+
thinkingLevel: Type.Union([
|
|
21
|
+
Type.Literal("off"),
|
|
22
|
+
Type.Literal("minimal"),
|
|
23
|
+
Type.Literal("low"),
|
|
24
|
+
Type.Literal("medium"),
|
|
25
|
+
Type.Literal("high"),
|
|
26
|
+
Type.Literal("xhigh"),
|
|
27
|
+
Type.Literal("max"),
|
|
28
|
+
], { default: "off" }),
|
|
29
|
+
});
|
|
30
|
+
/** Resolve references at use time so registry changes cannot leave a cached model behind. */
|
|
31
|
+
export function resolveRecapModel(ctx, settings) {
|
|
32
|
+
return settings.model
|
|
33
|
+
? ctx.modelRegistry.find(settings.model.provider, settings.model.id)
|
|
34
|
+
: ctx.model;
|
|
35
|
+
}
|
|
36
|
+
export const recapSettings = defineSettings({
|
|
37
|
+
id: "pi-recap",
|
|
38
|
+
schema: recapSchema,
|
|
39
|
+
});
|
|
Binary file
|