proper-base 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/LICENSE +21 -0
- package/README.md +186 -0
- package/index.ts +596 -0
- package/install.mjs +84 -0
- package/package.json +68 -0
- package/src/autocomplete-details.ts +406 -0
- package/src/editor-navigation.ts +451 -0
- package/src/footer-colors.ts +382 -0
- package/src/history-guard.ts +32 -0
- package/src/history.ts +87 -0
- package/src/image-context.ts +44 -0
- package/src/image-preview.ts +296 -0
- package/src/jump-to-bottom.ts +126 -0
- package/src/prompt-display.ts +101 -0
- package/src/recorder.ts +61 -0
- package/src/smart-selection.ts +155 -0
- package/src/store.ts +162 -0
- package/src/transcript-cleanup.ts +561 -0
- package/src/transient-retry.ts +40 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
type Component,
|
|
4
|
+
sliceByColumn,
|
|
5
|
+
stripTerminalSequences,
|
|
6
|
+
type TUI,
|
|
7
|
+
truncateToWidth,
|
|
8
|
+
visibleWidth,
|
|
9
|
+
} from "@earendil-works/pi-tui";
|
|
10
|
+
|
|
11
|
+
const INSTALLED = Symbol.for("pi-proper-base.footer-colors");
|
|
12
|
+
const FIELD_COLORS = {
|
|
13
|
+
path: [137, 152, 163],
|
|
14
|
+
branch: [134, 165, 128],
|
|
15
|
+
input: [111, 159, 190],
|
|
16
|
+
output: [126, 174, 132],
|
|
17
|
+
cacheRead: [153, 143, 196],
|
|
18
|
+
cacheWrite: [194, 143, 114],
|
|
19
|
+
cacheHit: [115, 176, 160],
|
|
20
|
+
cost: [202, 165, 103],
|
|
21
|
+
context: [181, 143, 168],
|
|
22
|
+
contextWarning: [213, 160, 84],
|
|
23
|
+
contextDanger: [218, 122, 122],
|
|
24
|
+
model: [192, 132, 252],
|
|
25
|
+
} as const;
|
|
26
|
+
const RAINBOW = [
|
|
27
|
+
[255, 95, 175],
|
|
28
|
+
[255, 215, 95],
|
|
29
|
+
[95, 215, 255],
|
|
30
|
+
[175, 135, 255],
|
|
31
|
+
[95, 215, 135],
|
|
32
|
+
] as const;
|
|
33
|
+
const ANIMATION_INTERVAL_MS = 120;
|
|
34
|
+
const HIGHLIGHT_CYCLE_MS = 4000;
|
|
35
|
+
const USAGE_THROUGH_COST =
|
|
36
|
+
/^((?:(?:↑|↓|R|W|CH)\S+\s+)*\$\S+(?:\s+\(sub\))?)(?:\s+|$)/;
|
|
37
|
+
const USAGE_FIELD =
|
|
38
|
+
/(?:^|\s)(CH\S+|↑\S+|↓\S+|R\S+|W\S+|\$\S+(?:\s+\(sub\))?)(?=\s|$)/g;
|
|
39
|
+
const CONTEXT_FIELD = /(?:\d+(?:\.\d+)?%|\?)\/\S+(?:\s+\(auto\))?/;
|
|
40
|
+
|
|
41
|
+
type FooterTheme = ExtensionContext["ui"]["theme"];
|
|
42
|
+
type FooterState = {
|
|
43
|
+
ctx: ExtensionContext;
|
|
44
|
+
tui: TUI;
|
|
45
|
+
};
|
|
46
|
+
type FooterController = {
|
|
47
|
+
update(state: FooterState): void;
|
|
48
|
+
uninstall(): void;
|
|
49
|
+
};
|
|
50
|
+
type DecoratedFooter = Component & {
|
|
51
|
+
dispose?: () => void;
|
|
52
|
+
[INSTALLED]?: FooterController;
|
|
53
|
+
};
|
|
54
|
+
type ComponentWithChildren = Component & { children?: Component[] };
|
|
55
|
+
type FooterThinkingLevel = ExtensionContext["thinkingLevel"] | "ultra";
|
|
56
|
+
type FooterColorState = {
|
|
57
|
+
level: FooterThinkingLevel;
|
|
58
|
+
model: string | undefined;
|
|
59
|
+
now: number;
|
|
60
|
+
theme: FooterTheme;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export function installFooterColors(
|
|
64
|
+
tui: TUI,
|
|
65
|
+
ctx: ExtensionContext,
|
|
66
|
+
): (() => void) | undefined {
|
|
67
|
+
const footer = findFooter(tui);
|
|
68
|
+
if (!footer) return undefined;
|
|
69
|
+
|
|
70
|
+
const existing = footer[INSTALLED];
|
|
71
|
+
if (existing) {
|
|
72
|
+
existing.update({ ctx, tui });
|
|
73
|
+
return existing.uninstall;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let state = { ctx, tui };
|
|
77
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
78
|
+
const render = footer.render;
|
|
79
|
+
const dispose = footer.dispose;
|
|
80
|
+
const stopAnimation = () => {
|
|
81
|
+
if (!timer) return;
|
|
82
|
+
clearInterval(timer);
|
|
83
|
+
timer = undefined;
|
|
84
|
+
};
|
|
85
|
+
const startAnimation = () => {
|
|
86
|
+
if (timer) return;
|
|
87
|
+
timer = setInterval(() => {
|
|
88
|
+
if (!isMounted(state.tui, footer)) {
|
|
89
|
+
stopAnimation();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
state.tui.requestRender();
|
|
93
|
+
}, ANIMATION_INTERVAL_MS);
|
|
94
|
+
timer.unref?.();
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const controller: FooterController = {
|
|
98
|
+
update(next) {
|
|
99
|
+
state = next;
|
|
100
|
+
},
|
|
101
|
+
uninstall() {
|
|
102
|
+
if (footer[INSTALLED] !== controller) return;
|
|
103
|
+
stopAnimation();
|
|
104
|
+
footer.render = render;
|
|
105
|
+
if (dispose) footer.dispose = dispose;
|
|
106
|
+
else delete footer.dispose;
|
|
107
|
+
delete footer[INSTALLED];
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
footer.render = (width: number) => {
|
|
111
|
+
const level = state.ctx.thinkingLevel as FooterThinkingLevel;
|
|
112
|
+
if (level === "max" || level === "ultra") startAnimation();
|
|
113
|
+
else stopAnimation();
|
|
114
|
+
const theme = state.ctx.ui.theme;
|
|
115
|
+
return colorFooter(layoutFooter(render.call(footer, width), width, theme), {
|
|
116
|
+
level,
|
|
117
|
+
model: state.ctx.model?.id,
|
|
118
|
+
now: Date.now(),
|
|
119
|
+
theme,
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
footer.dispose = () => {
|
|
123
|
+
controller.uninstall();
|
|
124
|
+
dispose?.call(footer);
|
|
125
|
+
};
|
|
126
|
+
footer[INSTALLED] = controller;
|
|
127
|
+
return controller.uninstall;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function layoutFooter(
|
|
131
|
+
lines: string[],
|
|
132
|
+
width: number,
|
|
133
|
+
theme: FooterTheme,
|
|
134
|
+
): string[] {
|
|
135
|
+
if (lines.length < 2 || !Number.isFinite(width) || width < 20) return lines;
|
|
136
|
+
|
|
137
|
+
const statsLine = lines[1];
|
|
138
|
+
if (!statsLine) return lines;
|
|
139
|
+
const match = stripTerminalSequences(statsLine).match(USAGE_THROUGH_COST);
|
|
140
|
+
const usage = match?.[1];
|
|
141
|
+
if (!match || !usage) return lines;
|
|
142
|
+
|
|
143
|
+
const contentWidth = width - 1;
|
|
144
|
+
const usageWidth = visibleWidth(usage);
|
|
145
|
+
const availablePathWidth = contentWidth - usageWidth - 1;
|
|
146
|
+
if (availablePathWidth < 8) return lines;
|
|
147
|
+
|
|
148
|
+
const result = [...lines];
|
|
149
|
+
const path = truncateToWidth(
|
|
150
|
+
result[0] ?? "",
|
|
151
|
+
availablePathWidth,
|
|
152
|
+
theme.fg("dim", "..."),
|
|
153
|
+
);
|
|
154
|
+
const topPadding = " ".repeat(
|
|
155
|
+
Math.max(1, contentWidth - visibleWidth(path) - usageWidth),
|
|
156
|
+
);
|
|
157
|
+
result[0] = `${path}${topPadding}${theme.fg("dim", usage)}`;
|
|
158
|
+
|
|
159
|
+
const consumed = visibleWidth(match[0]);
|
|
160
|
+
const remainder = sliceByColumn(
|
|
161
|
+
statsLine,
|
|
162
|
+
consumed,
|
|
163
|
+
Math.max(0, visibleWidth(statsLine) - consumed),
|
|
164
|
+
true,
|
|
165
|
+
);
|
|
166
|
+
result[1] = realignFooterRemainder(remainder, contentWidth, theme);
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function realignFooterRemainder(
|
|
171
|
+
line: string,
|
|
172
|
+
width: number,
|
|
173
|
+
theme: FooterTheme,
|
|
174
|
+
): string {
|
|
175
|
+
const plain = stripTerminalSequences(line);
|
|
176
|
+
const gaps = [...plain.matchAll(/ {2,}/g)];
|
|
177
|
+
const gap = gaps.at(-1);
|
|
178
|
+
if (!gap || gap.index === undefined) {
|
|
179
|
+
return truncateToWidth(line, width, theme.fg("dim", "..."));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const rightStart = gap.index + gap[0].length;
|
|
183
|
+
const left = sliceByColumn(line, 0, gap.index, true);
|
|
184
|
+
const right = sliceByColumn(
|
|
185
|
+
line,
|
|
186
|
+
rightStart,
|
|
187
|
+
Math.max(0, visibleWidth(line) - rightStart),
|
|
188
|
+
true,
|
|
189
|
+
);
|
|
190
|
+
const padding = " ".repeat(
|
|
191
|
+
Math.max(2, width - visibleWidth(left) - visibleWidth(right)),
|
|
192
|
+
);
|
|
193
|
+
return truncateToWidth(
|
|
194
|
+
`${left}${padding}${right}`,
|
|
195
|
+
width,
|
|
196
|
+
theme.fg("dim", "..."),
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function colorFooter(
|
|
201
|
+
lines: string[],
|
|
202
|
+
{ level, model, now, theme }: FooterColorState,
|
|
203
|
+
): string[] {
|
|
204
|
+
const result = [...lines];
|
|
205
|
+
if (result[0]) result[0] = colorPathAndUsage(result[0], theme);
|
|
206
|
+
if (result[1]) result[1] = colorUsageAndContext(result[1], theme);
|
|
207
|
+
|
|
208
|
+
if (!model) return result;
|
|
209
|
+
const index = result.findIndex((line) =>
|
|
210
|
+
stripTerminalSequences(line).includes(model),
|
|
211
|
+
);
|
|
212
|
+
if (index < 0) return result;
|
|
213
|
+
|
|
214
|
+
let line = result[index] ?? "";
|
|
215
|
+
line = line.replace(model, paintField(model, FIELD_COLORS.model, theme));
|
|
216
|
+
if (level) {
|
|
217
|
+
const label = level === "off" ? "thinking off" : level;
|
|
218
|
+
const styled =
|
|
219
|
+
level === "max" || level === "ultra"
|
|
220
|
+
? rainbowHighlight(label, now)
|
|
221
|
+
: theme.getThinkingBorderColor(level)(label);
|
|
222
|
+
line = replaceLast(line, label, emphasize(styled, theme));
|
|
223
|
+
}
|
|
224
|
+
result[index] = line;
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function colorPathAndUsage(line: string, theme: FooterTheme): string {
|
|
229
|
+
const plain = stripTerminalSequences(line);
|
|
230
|
+
const usageStart = usageFields(plain)[0]?.index ?? -1;
|
|
231
|
+
const pathArea = (
|
|
232
|
+
usageStart < 0 ? plain : plain.slice(0, usageStart)
|
|
233
|
+
).trimEnd();
|
|
234
|
+
const pathMatch = pathArea.match(/^(.*?)(?:\s+(\([^()]+\)))?(?:\s+•\s+.*)?$/);
|
|
235
|
+
let colored = line;
|
|
236
|
+
const path = pathMatch?.[1]?.trimEnd();
|
|
237
|
+
const branch = pathMatch?.[2];
|
|
238
|
+
if (path)
|
|
239
|
+
colored = colored.replace(path, paintField(path, FIELD_COLORS.path, theme));
|
|
240
|
+
if (branch) {
|
|
241
|
+
colored = colored.replace(
|
|
242
|
+
branch,
|
|
243
|
+
paintField(branch, FIELD_COLORS.branch, theme),
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
return colorUsage(colored, theme);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function colorUsageAndContext(line: string, theme: FooterTheme): string {
|
|
250
|
+
let colored = colorUsage(line, theme);
|
|
251
|
+
const context = stripTerminalSequences(colored).match(CONTEXT_FIELD)?.[0];
|
|
252
|
+
if (context) {
|
|
253
|
+
colored = colored.replace(
|
|
254
|
+
context,
|
|
255
|
+
paintField(context, contextColor(context), theme),
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
return colored;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function colorUsage(line: string, theme: FooterTheme): string {
|
|
262
|
+
let colored = line;
|
|
263
|
+
for (const { value } of usageFields(stripTerminalSequences(line))) {
|
|
264
|
+
colored = colored.replace(
|
|
265
|
+
value,
|
|
266
|
+
paintField(value, usageColor(value), theme),
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
return colored;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function usageFields(line: string): Array<{ value: string; index: number }> {
|
|
273
|
+
return [...line.matchAll(USAGE_FIELD)].flatMap((match) => {
|
|
274
|
+
const value = match[1];
|
|
275
|
+
if (!value) return [];
|
|
276
|
+
return [
|
|
277
|
+
{ value, index: (match.index ?? 0) + match[0].length - value.length },
|
|
278
|
+
];
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function usageColor(field: string): readonly [number, number, number] {
|
|
283
|
+
if (field.startsWith("↑")) return FIELD_COLORS.input;
|
|
284
|
+
if (field.startsWith("↓")) return FIELD_COLORS.output;
|
|
285
|
+
if (field.startsWith("CH")) return FIELD_COLORS.cacheHit;
|
|
286
|
+
if (field.startsWith("R")) return FIELD_COLORS.cacheRead;
|
|
287
|
+
if (field.startsWith("W")) return FIELD_COLORS.cacheWrite;
|
|
288
|
+
return FIELD_COLORS.cost;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function contextColor(context: string): readonly [number, number, number] {
|
|
292
|
+
const percent = Number.parseFloat(context);
|
|
293
|
+
if (percent > 90) return FIELD_COLORS.contextDanger;
|
|
294
|
+
if (percent > 70) return FIELD_COLORS.contextWarning;
|
|
295
|
+
return FIELD_COLORS.context;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function paintField(
|
|
299
|
+
text: string,
|
|
300
|
+
color: readonly [number, number, number],
|
|
301
|
+
theme: FooterTheme,
|
|
302
|
+
): string {
|
|
303
|
+
return emphasize(rgb(color, text), theme);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function rainbowHighlight(text: string, now: number): string {
|
|
307
|
+
const characters = [...text];
|
|
308
|
+
const travel = characters.length + 4;
|
|
309
|
+
const highlight =
|
|
310
|
+
((now % HIGHLIGHT_CYCLE_MS) / HIGHLIGHT_CYCLE_MS) * travel - 2;
|
|
311
|
+
return characters
|
|
312
|
+
.map((character, index) => {
|
|
313
|
+
const base = RAINBOW[index % RAINBOW.length] ?? RAINBOW[0];
|
|
314
|
+
const strength = Math.max(0, 1 - Math.abs(index - highlight) / 1.15);
|
|
315
|
+
const color = base.map((channel: number) =>
|
|
316
|
+
Math.round(channel + (255 - channel) * strength * 0.9),
|
|
317
|
+
) as unknown as readonly [number, number, number];
|
|
318
|
+
const bold = strength > 0.6;
|
|
319
|
+
return `${bold ? "\x1b[1m" : ""}${rgb(color, character)}${
|
|
320
|
+
bold ? "\x1b[22m" : ""
|
|
321
|
+
}`;
|
|
322
|
+
})
|
|
323
|
+
.join("");
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function rgb(color: readonly [number, number, number], text: string): string {
|
|
327
|
+
return `\x1b[38;2;${color.join(";")}m${text}\x1b[39m`;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function emphasize(text: string, theme: FooterTheme): string {
|
|
331
|
+
return `${text}${theme.getFgAnsi("dim")}`;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function replaceLast(
|
|
335
|
+
text: string,
|
|
336
|
+
search: string,
|
|
337
|
+
replacement: string,
|
|
338
|
+
): string {
|
|
339
|
+
const index = text.lastIndexOf(search);
|
|
340
|
+
if (index < 0) return text;
|
|
341
|
+
return `${text.slice(0, index)}${replacement}${text.slice(index + search.length)}`;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function findFooter(tui: TUI): DecoratedFooter | undefined {
|
|
345
|
+
for (const root of roots(tui)) {
|
|
346
|
+
const footer = find(
|
|
347
|
+
root,
|
|
348
|
+
(component) =>
|
|
349
|
+
(component as { constructor?: { name?: string } }).constructor?.name ===
|
|
350
|
+
"FooterComponent",
|
|
351
|
+
);
|
|
352
|
+
if (footer) return footer as DecoratedFooter;
|
|
353
|
+
}
|
|
354
|
+
return undefined;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function isMounted(tui: TUI, target: Component): boolean {
|
|
358
|
+
return roots(tui).some((root) =>
|
|
359
|
+
find(root, (component) => component === target),
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function find(
|
|
364
|
+
root: Component,
|
|
365
|
+
matches: (component: Component) => boolean,
|
|
366
|
+
): Component | undefined {
|
|
367
|
+
if (matches(root)) return root;
|
|
368
|
+
for (const child of childrenOf(root)) {
|
|
369
|
+
const match = find(child, matches);
|
|
370
|
+
if (match) return match;
|
|
371
|
+
}
|
|
372
|
+
return undefined;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function roots(tui: TUI): Component[] {
|
|
376
|
+
const layoutRoot = (tui as TUI & { layoutRoot?: Component }).layoutRoot;
|
|
377
|
+
return layoutRoot ? [layoutRoot] : tui.children;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function childrenOf(component: Component): Component[] {
|
|
381
|
+
return (component as ComponentWithChildren).children ?? [];
|
|
382
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const INSTALLED = Symbol.for("pi-proper-base.history-guard");
|
|
2
|
+
|
|
3
|
+
type HistoryEditor = {
|
|
4
|
+
addToHistory?(text: string): void;
|
|
5
|
+
[INSTALLED]?: HistoryGuard;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type HistoryGuard = {
|
|
9
|
+
add(text: string): void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function installHistoryGuard(
|
|
13
|
+
editor: HistoryEditor,
|
|
14
|
+
): HistoryGuard | undefined {
|
|
15
|
+
if (editor[INSTALLED]) return editor[INSTALLED];
|
|
16
|
+
if (!editor.addToHistory) return undefined;
|
|
17
|
+
|
|
18
|
+
const addToHistory = editor.addToHistory.bind(editor);
|
|
19
|
+
const controller: HistoryGuard = {
|
|
20
|
+
add(text) {
|
|
21
|
+
const prompt = text.trim();
|
|
22
|
+
if (prompt) addToHistory(prompt);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Pi replays transformed session messages through this method on startup.
|
|
27
|
+
// Block those calls; only editor submissions and the raw prompt store use
|
|
28
|
+
// the captured method through controller.add().
|
|
29
|
+
editor.addToHistory = () => {};
|
|
30
|
+
editor[INSTALLED] = controller;
|
|
31
|
+
return controller;
|
|
32
|
+
}
|
package/src/history.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure prompt-history logic.
|
|
3
|
+
*
|
|
4
|
+
* Kept free of pi imports so it can be unit tested on its own. `index.ts`
|
|
5
|
+
* adapts pi's session types onto the structural types below.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** One recalled prompt and when it was sent. */
|
|
9
|
+
export interface Prompt {
|
|
10
|
+
text: string;
|
|
11
|
+
ts: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Marks our editor wrapper and remembers the factory it wraps.
|
|
16
|
+
*
|
|
17
|
+
* `Symbol.for` keeps the tag stable when pi reloads the extension module on
|
|
18
|
+
* session replacement, so a wrapper installed by a previous module instance is
|
|
19
|
+
* still recognised.
|
|
20
|
+
*/
|
|
21
|
+
export const WRAPPED = Symbol.for("pi-proper-history.wrapped");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Find the editor factory to wrap.
|
|
25
|
+
*
|
|
26
|
+
* `session_start` fires again on reload, resume, and fork. Wrapping our own
|
|
27
|
+
* wrapper each time would stack them and seed every prompt once more per pass,
|
|
28
|
+
* so unwrap back to the factory we originally wrapped.
|
|
29
|
+
*/
|
|
30
|
+
export function resolveBase<F extends object>(
|
|
31
|
+
current: F | undefined,
|
|
32
|
+
): F | undefined {
|
|
33
|
+
if (current && WRAPPED in current) {
|
|
34
|
+
return (
|
|
35
|
+
((current as Record<symbol, unknown>)[WRAPPED] as F | null | undefined) ??
|
|
36
|
+
undefined
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return current;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* True when a submission is worth recalling from history.
|
|
44
|
+
*
|
|
45
|
+
* A leading `/` is either a prompt template (`/file fix the bug`), which is a
|
|
46
|
+
* real prompt, or a UI command (`/model …`, `/new`, `/reload`,
|
|
47
|
+
* `/llm-router-config`), which is not. Recalling a UI command is noise, and
|
|
48
|
+
* re-submitting a recalled `/model …` silently changes the session model.
|
|
49
|
+
* pi's `getCommands()` lists prompt templates and skills but not built-ins or
|
|
50
|
+
* extension commands, so membership there is the whole test.
|
|
51
|
+
*/
|
|
52
|
+
export function isRecallable(
|
|
53
|
+
text: string,
|
|
54
|
+
commands: readonly { name: string; source: string }[],
|
|
55
|
+
): boolean {
|
|
56
|
+
const command = /^\/(\S+)/.exec(text.trimStart());
|
|
57
|
+
if (!command) return true;
|
|
58
|
+
return commands.some(
|
|
59
|
+
(c) => c.source !== "extension" && c.name === command[1],
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Normalize recorded prompts into editor history order.
|
|
65
|
+
*
|
|
66
|
+
* Duplicate text keeps its newest timestamp. Returns oldest first, the order
|
|
67
|
+
* the trusted history append method expects, so Up yields the newest prompt.
|
|
68
|
+
*/
|
|
69
|
+
export function mergePrompts(
|
|
70
|
+
prompts: readonly Prompt[],
|
|
71
|
+
limit: number,
|
|
72
|
+
): string[] {
|
|
73
|
+
if (limit <= 0) return [];
|
|
74
|
+
|
|
75
|
+
const newest = new Map<string, number>();
|
|
76
|
+
for (const prompt of prompts) {
|
|
77
|
+
const known = newest.get(prompt.text);
|
|
78
|
+
if (known === undefined || prompt.ts > known)
|
|
79
|
+
newest.set(prompt.text, prompt.ts);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Insertion order breaks timestamp ties, keeping same-millisecond prompts
|
|
83
|
+
// in the order they were seen rather than reordering them arbitrarily.
|
|
84
|
+
const ordered = [...newest].map(([text, ts], index) => ({ text, ts, index }));
|
|
85
|
+
ordered.sort((a, b) => a.ts - b.ts || a.index - b.index);
|
|
86
|
+
return ordered.slice(-limit).map((entry) => entry.text);
|
|
87
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const OMITTED_IMAGE_TEXT = "[image omitted after its original user turn]";
|
|
2
|
+
|
|
3
|
+
type ContextMessage = {
|
|
4
|
+
role: string;
|
|
5
|
+
content?: unknown;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
function isImagePart(value: unknown): value is { type: "image" } {
|
|
9
|
+
return (
|
|
10
|
+
typeof value === "object" &&
|
|
11
|
+
value !== null &&
|
|
12
|
+
(value as { type?: unknown }).type === "image"
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Replace image bytes from earlier user turns without mutating session messages. */
|
|
17
|
+
export function omitPriorTurnImages<T extends ContextMessage>(
|
|
18
|
+
messages: T[],
|
|
19
|
+
): T[] {
|
|
20
|
+
let latestUser = -1;
|
|
21
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
22
|
+
if (messages[index]?.role === "user") {
|
|
23
|
+
latestUser = index;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (latestUser <= 0) return messages;
|
|
28
|
+
|
|
29
|
+
let changed = false;
|
|
30
|
+
const next = messages.map((message, index) => {
|
|
31
|
+
if (index >= latestUser || !Array.isArray(message.content)) return message;
|
|
32
|
+
|
|
33
|
+
let replaced = false;
|
|
34
|
+
const content = message.content.map((part) => {
|
|
35
|
+
if (!isImagePart(part)) return part;
|
|
36
|
+
replaced = true;
|
|
37
|
+
changed = true;
|
|
38
|
+
return { type: "text", text: OMITTED_IMAGE_TEXT };
|
|
39
|
+
});
|
|
40
|
+
return replaced ? ({ ...message, content } as T) : message;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return changed ? next : messages;
|
|
44
|
+
}
|