proper-base 0.2.7 → 0.2.8
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/README.md +22 -0
- package/index.ts +16 -2
- package/package.json +1 -1
- package/src/jump-to-bottom.ts +1 -1
- package/src/prompt-jump.ts +282 -0
- package/src/skill-context.ts +200 -0
- package/src/transcript-cleanup.ts +25 -3
package/README.md
CHANGED
|
@@ -103,6 +103,12 @@ Scrolling away from current output adds a `↓ jump to bottom` row above the
|
|
|
103
103
|
prompt. Clicking it returns to the newest output without disabling scrollbar
|
|
104
104
|
dragging.
|
|
105
105
|
|
|
106
|
+
The transcript's top-right corner carries muted `↑` and `↓` arrows that walk
|
|
107
|
+
the viewport between your own prompts. Clicking `↓` past the last prompt scrolls
|
|
108
|
+
to the bottom. While you are scrolled up, a dimmer `position/total` reading sits
|
|
109
|
+
centred under the arrows and counts the prompts in the session; it disappears
|
|
110
|
+
once the viewport is following output again.
|
|
111
|
+
|
|
106
112
|
### Clipboard and model image context
|
|
107
113
|
|
|
108
114
|
Ctrl+V and Ctrl+Shift+V both use Pi's image-or-text clipboard action. Readable
|
|
@@ -126,6 +132,22 @@ in model context for every tool loop in the turn that introduced them. A later
|
|
|
126
132
|
user message replaces older image blocks only in the outbound context copy, so
|
|
127
133
|
saved sessions, exports, resumes, and branches retain the originals.
|
|
128
134
|
|
|
135
|
+
### Skill context
|
|
136
|
+
|
|
137
|
+
Pi expands `/skill:<name>` into a user message holding the whole `SKILL.md`
|
|
138
|
+
body, then treats it like any other message. Invoking the same skill twice
|
|
139
|
+
therefore sends two full copies, and compaction summarizes the body away while
|
|
140
|
+
the model keeps following instructions it can no longer read.
|
|
141
|
+
|
|
142
|
+
proper-base keeps each invoked skill present exactly once in the outbound
|
|
143
|
+
context. A repeat invocation of an unchanged body keeps its request and shows
|
|
144
|
+
the model a one-line already-loaded note instead of a second copy; a body that
|
|
145
|
+
changed with its arguments is sent in full. After a compaction, the newest body
|
|
146
|
+
of each dropped skill is restored on the first user turn following the summary,
|
|
147
|
+
truncated to a character ceiling per skill and in total so the restored text
|
|
148
|
+
cannot re-trigger the compaction that just ran. Only the outbound copy changes,
|
|
149
|
+
so saved sessions, exports, resumes, and branches retain the originals.
|
|
150
|
+
|
|
129
151
|
### Footer
|
|
130
152
|
|
|
131
153
|
The built-in footer keeps path, branch, cumulative input, output, cache, cost,
|
package/index.ts
CHANGED
|
@@ -49,7 +49,9 @@ import {
|
|
|
49
49
|
createPromptDisplay,
|
|
50
50
|
PROMPT_DISPLAY_ENTRY,
|
|
51
51
|
} from "./src/prompt-display.ts";
|
|
52
|
+
import { installPromptJump } from "./src/prompt-jump.ts";
|
|
52
53
|
import { installRecorder } from "./src/recorder.ts";
|
|
54
|
+
import { pinSkillContext } from "./src/skill-context.ts";
|
|
53
55
|
import { installSmartSelection } from "./src/smart-selection.ts";
|
|
54
56
|
import {
|
|
55
57
|
appendPrompt,
|
|
@@ -215,6 +217,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
215
217
|
|
|
216
218
|
let removeFooterColors: (() => void) | undefined;
|
|
217
219
|
let removeJumpToBottom: (() => void) | undefined;
|
|
220
|
+
let removePromptJump: (() => void) | undefined;
|
|
218
221
|
let removePromptClear: (() => void) | undefined;
|
|
219
222
|
let removeSmartSelection: (() => void) | undefined;
|
|
220
223
|
let imagePreview: ImagePreviewController | undefined;
|
|
@@ -355,8 +358,12 @@ export default function (pi: ExtensionAPI) {
|
|
|
355
358
|
});
|
|
356
359
|
|
|
357
360
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Model image context]]
|
|
358
|
-
|
|
359
|
-
|
|
361
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Skill context]]
|
|
362
|
+
pi.on("context", (event, ctx) => {
|
|
363
|
+
const messages = pinSkillContext(
|
|
364
|
+
omitPriorTurnImages(event.messages),
|
|
365
|
+
ctx.sessionManager.getBranch(),
|
|
366
|
+
);
|
|
360
367
|
if (messages !== event.messages) return { messages };
|
|
361
368
|
});
|
|
362
369
|
|
|
@@ -476,6 +483,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
476
483
|
removeFooterColors = undefined;
|
|
477
484
|
removeJumpToBottom?.();
|
|
478
485
|
removeJumpToBottom = undefined;
|
|
486
|
+
removePromptJump?.();
|
|
487
|
+
removePromptJump = undefined;
|
|
479
488
|
removePromptClear?.();
|
|
480
489
|
removePromptClear = undefined;
|
|
481
490
|
removeSmartSelection?.();
|
|
@@ -581,6 +590,11 @@ export default function (pi: ExtensionAPI) {
|
|
|
581
590
|
historyGuard = installHistoryGuard(editor);
|
|
582
591
|
removeJumpToBottom?.();
|
|
583
592
|
removeJumpToBottom = installJumpToBottom(editor, tui);
|
|
593
|
+
removePromptJump?.();
|
|
594
|
+
removePromptJump = installPromptJump(tui, {
|
|
595
|
+
color: (value) => ctx.ui.theme.fg("muted", value),
|
|
596
|
+
subtle: (value) => ctx.ui.theme.fg("dim", value),
|
|
597
|
+
});
|
|
584
598
|
imagePreview?.dispose();
|
|
585
599
|
imagePreview = installImagePreview(editor, tui, {
|
|
586
600
|
fallbackColor: (value) => ctx.ui.theme.fg("dim", value),
|
package/package.json
CHANGED
package/src/jump-to-bottom.ts
CHANGED
|
@@ -38,7 +38,7 @@ function scrollableTui(tui: TUI): ScrollableTui | undefined {
|
|
|
38
38
|
* respond to the mouse; if that internal field ever changes shape the button
|
|
39
39
|
* keeps rendering and simply stops reacting.
|
|
40
40
|
*/
|
|
41
|
-
function prioritize(tui: TUI, listener: TuiInputListener): void {
|
|
41
|
+
export function prioritize(tui: TUI, listener: TuiInputListener): void {
|
|
42
42
|
const listeners = (tui as ListenerHost).inputListeners;
|
|
43
43
|
if (!listeners?.has(listener)) return;
|
|
44
44
|
const rest = [...listeners].filter((entry) => entry !== listener);
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import type { TUI, TuiInputListener } from "@earendil-works/pi-tui";
|
|
2
|
+
import {
|
|
3
|
+
compositeTuiLine,
|
|
4
|
+
sliceByColumn,
|
|
5
|
+
visibleWidth,
|
|
6
|
+
} from "@earendil-works/pi-tui";
|
|
7
|
+
|
|
8
|
+
import { prioritize } from "./jump-to-bottom.ts";
|
|
9
|
+
|
|
10
|
+
const INSTALLED = Symbol.for("pi-proper-base.prompt-jump");
|
|
11
|
+
// Plain arrows rather than arrowheads or chevrons: U+2303/U+2304 and the
|
|
12
|
+
// box-drawing carets are missing from common monospace fonts and render as a
|
|
13
|
+
// blank cell there.
|
|
14
|
+
const UP = " ↑ ";
|
|
15
|
+
const DOWN = " ↓ ";
|
|
16
|
+
/** Columns kept clear to the right of the chips, sparing the scrollbar. */
|
|
17
|
+
const RIGHT_GAP = 1;
|
|
18
|
+
// ESC prefix is matched separately: biome bans control characters in
|
|
19
|
+
// regexes, and string literals carry it fine.
|
|
20
|
+
const SGR_MOUSE_TAIL = /^\[<(\d+);(\d+);(\d+)([Mm])$/;
|
|
21
|
+
const LEFT_BUTTON = "0";
|
|
22
|
+
// The ESC is matched separately for the same reason as the mouse sequence
|
|
23
|
+
// above: biome bans control characters in regexes, whichever escape spells
|
|
24
|
+
// them, while string literals carry them fine.
|
|
25
|
+
const ESC = "\x1b";
|
|
26
|
+
const SGR_TAIL = /^\[[0-9;]*m/;
|
|
27
|
+
// OSC 133 zone markers open both user and assistant transcript blocks. A user
|
|
28
|
+
// block opens with its background-padded box row, an assistant block with an
|
|
29
|
+
// empty spacer row, so trailing content after the markers identifies a prompt.
|
|
30
|
+
const ZONE_START = "\x1b]133;A\x07";
|
|
31
|
+
const ZONE_MARK = "\x1b]133;";
|
|
32
|
+
|
|
33
|
+
/** Alternate-screen renderer surface used for the chips. */
|
|
34
|
+
type Scroller = TUI & {
|
|
35
|
+
readonly viewportTop: number;
|
|
36
|
+
readonly isFollowingOutput: boolean;
|
|
37
|
+
scrollBy(lines: number): void;
|
|
38
|
+
scrollToBottom(): void;
|
|
39
|
+
compositeFlashes(screen: string[], width: number, height: number): string[];
|
|
40
|
+
currentLayout?: LayoutFrame;
|
|
41
|
+
};
|
|
42
|
+
type LayoutBox = {
|
|
43
|
+
scrollView?: unknown;
|
|
44
|
+
scrollContentLines?: readonly string[];
|
|
45
|
+
children?: LayoutBox[];
|
|
46
|
+
};
|
|
47
|
+
type LayoutFrame = { root: LayoutBox; primaryScrollView?: unknown };
|
|
48
|
+
type Installed = Scroller & { [INSTALLED]?: () => void };
|
|
49
|
+
/** Screen columns of the rendered chips, in 0-based terminal cells. */
|
|
50
|
+
type Hit = { start: number; middle: number; end: number };
|
|
51
|
+
export type PromptJumpOptions = {
|
|
52
|
+
color(value: string): string;
|
|
53
|
+
/** Weaker than `color`, so the reading recedes behind the chips. */
|
|
54
|
+
subtle(value: string): string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function scroller(tui: TUI): Scroller | undefined {
|
|
58
|
+
const candidate = tui as Partial<Scroller>;
|
|
59
|
+
return typeof candidate.scrollBy === "function" &&
|
|
60
|
+
typeof candidate.scrollToBottom === "function" &&
|
|
61
|
+
typeof candidate.compositeFlashes === "function" &&
|
|
62
|
+
typeof candidate.isFollowingOutput === "boolean" &&
|
|
63
|
+
typeof candidate.viewportTop === "number"
|
|
64
|
+
? (candidate as Scroller)
|
|
65
|
+
: undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function contentLines(view: Scroller): readonly string[] | undefined {
|
|
69
|
+
const layout = view.currentLayout;
|
|
70
|
+
const scrollView = layout?.primaryScrollView;
|
|
71
|
+
if (!layout || !scrollView) return undefined;
|
|
72
|
+
const visit = (box: LayoutBox): readonly string[] | undefined => {
|
|
73
|
+
if (box.scrollView === scrollView) return box.scrollContentLines;
|
|
74
|
+
for (const child of box.children ?? []) {
|
|
75
|
+
const found = visit(child);
|
|
76
|
+
if (found) return found;
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
};
|
|
80
|
+
return visit(layout.root);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Leading SGR sequences of a slice, which carry the colors already active at
|
|
85
|
+
* that column. Replaying them under the glyphs keeps the transcript's own
|
|
86
|
+
* background intact, because compositing otherwise resets the covered cells to
|
|
87
|
+
* the terminal default and cuts a hole in the row.
|
|
88
|
+
*
|
|
89
|
+
* Each step consumes a whole matched sequence, so the walk always advances and
|
|
90
|
+
* a truncated escape ends it. That matters because this runs inside the
|
|
91
|
+
* renderer's frame, where a loop that fails to terminate freezes the whole
|
|
92
|
+
* application rather than degrading one row.
|
|
93
|
+
*/
|
|
94
|
+
function activeStyle(slice: string): string {
|
|
95
|
+
let index = 0;
|
|
96
|
+
for (;;) {
|
|
97
|
+
if (slice[index] !== ESC) break;
|
|
98
|
+
const tail = SGR_TAIL.exec(slice.slice(index + 1));
|
|
99
|
+
if (!tail) break;
|
|
100
|
+
index += 1 + tail[0].length;
|
|
101
|
+
}
|
|
102
|
+
return slice.slice(0, index);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Composite content onto one screen row at a column, keeping its colors. */
|
|
106
|
+
function paintRow(
|
|
107
|
+
screen: string[],
|
|
108
|
+
row: number,
|
|
109
|
+
content: string,
|
|
110
|
+
contentWidth: number,
|
|
111
|
+
start: number,
|
|
112
|
+
width: number,
|
|
113
|
+
): void {
|
|
114
|
+
if (start < 0 || row >= screen.length) return;
|
|
115
|
+
const line = screen[row] ?? "";
|
|
116
|
+
const beneath = activeStyle(sliceByColumn(line, start, contentWidth, true));
|
|
117
|
+
screen[row] = compositeTuiLine(
|
|
118
|
+
line,
|
|
119
|
+
`${beneath}${content}`,
|
|
120
|
+
start,
|
|
121
|
+
contentWidth,
|
|
122
|
+
width,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isPromptRow(line: string): boolean {
|
|
127
|
+
if (!line.startsWith(ZONE_START)) return false;
|
|
128
|
+
let index = 0;
|
|
129
|
+
while (line.startsWith(ZONE_MARK, index)) index += ZONE_START.length;
|
|
130
|
+
return index < line.length;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Position of the prompt the viewport currently sits in, counted from the top
|
|
135
|
+
* of the transcript, alongside the session's total prompt count. Scrolling
|
|
136
|
+
* above the first prompt reports position zero, so one down click always
|
|
137
|
+
* advances the reading by one.
|
|
138
|
+
*/
|
|
139
|
+
function promptCount(view: Scroller): string | undefined {
|
|
140
|
+
const lines = contentLines(view) ?? [];
|
|
141
|
+
const top = view.viewportTop;
|
|
142
|
+
let current = 0;
|
|
143
|
+
let total = 0;
|
|
144
|
+
for (let row = 0; row < lines.length; row++) {
|
|
145
|
+
if (!isPromptRow(lines[row] ?? "")) continue;
|
|
146
|
+
total += 1;
|
|
147
|
+
if (row <= top) current = total;
|
|
148
|
+
}
|
|
149
|
+
return total > 0 ? `${current}/${total}` : undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Scroll to the neighbouring user prompt, or to the transcript end when the
|
|
154
|
+
* last prompt is already above the viewport.
|
|
155
|
+
*/
|
|
156
|
+
function jump(view: Scroller, direction: 1 | -1): void {
|
|
157
|
+
const lines = contentLines(view) ?? [];
|
|
158
|
+
const from = view.viewportTop;
|
|
159
|
+
for (
|
|
160
|
+
let row = from + direction;
|
|
161
|
+
row >= 0 && row < lines.length;
|
|
162
|
+
row += direction
|
|
163
|
+
) {
|
|
164
|
+
if (!isPromptRow(lines[row] ?? "")) continue;
|
|
165
|
+
view.scrollBy(row - from);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (direction === 1) view.scrollToBottom();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Render clickable previous/next prompt chips in the top-right corner of the
|
|
173
|
+
* transcript viewport.
|
|
174
|
+
*
|
|
175
|
+
* The chips composite into the finished screen rather than an overlay: any
|
|
176
|
+
* visible overlay disables Pi's scrollbar hit testing, which is a gesture
|
|
177
|
+
* users reach for in the same scrolled-up state. They carry no background of
|
|
178
|
+
* their own and replay the covered row's colors, so the transcript reads
|
|
179
|
+
* through as two quiet arrows instead of a punched-out block.
|
|
180
|
+
*
|
|
181
|
+
* Regular-mode terminals scroll themselves, so nothing is installed there.
|
|
182
|
+
*
|
|
183
|
+
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Prompt jump chips]]
|
|
184
|
+
*/
|
|
185
|
+
export function installPromptJump(
|
|
186
|
+
tui: TUI,
|
|
187
|
+
options: PromptJumpOptions,
|
|
188
|
+
): (() => void) | undefined {
|
|
189
|
+
const view = scroller(tui);
|
|
190
|
+
if (!view) return undefined;
|
|
191
|
+
const target = view as Installed;
|
|
192
|
+
// A reload runs the new instance's editor factory before the outgoing
|
|
193
|
+
// instance shuts down, so take over any wrapper it left on the renderer.
|
|
194
|
+
target[INSTALLED]?.();
|
|
195
|
+
|
|
196
|
+
let hit: Hit | undefined;
|
|
197
|
+
const chipWidth = visibleWidth(UP) + visibleWidth(DOWN);
|
|
198
|
+
const hadOwnMethod = Object.hasOwn(view, "compositeFlashes");
|
|
199
|
+
const flashes = view.compositeFlashes;
|
|
200
|
+
|
|
201
|
+
const decorate = (screen: string[], width: number): string[] => {
|
|
202
|
+
const start = width - RIGHT_GAP - chipWidth;
|
|
203
|
+
if (start < 0) {
|
|
204
|
+
hit = undefined;
|
|
205
|
+
return screen;
|
|
206
|
+
}
|
|
207
|
+
hit = { start, middle: start + visibleWidth(UP), end: start + chipWidth };
|
|
208
|
+
const painted = [...screen];
|
|
209
|
+
// Colored per frame rather than once at install, so a theme that rejects
|
|
210
|
+
// the request fails inside the guard instead of out of the editor factory.
|
|
211
|
+
paintRow(
|
|
212
|
+
painted,
|
|
213
|
+
0,
|
|
214
|
+
options.color(`${UP}${DOWN}`),
|
|
215
|
+
chipWidth,
|
|
216
|
+
start,
|
|
217
|
+
width,
|
|
218
|
+
);
|
|
219
|
+
// The reading only answers "where am I", so it stays out of the way while
|
|
220
|
+
// the viewport is already following the newest output.
|
|
221
|
+
const count = view.isFollowingOutput ? undefined : promptCount(view);
|
|
222
|
+
if (count) {
|
|
223
|
+
const countWidth = visibleWidth(count);
|
|
224
|
+
// Centred on the chips rather than the screen edge: the chips carry a
|
|
225
|
+
// padding column each, so sharing the right margin would sit the
|
|
226
|
+
// reading off to one side of the arrows.
|
|
227
|
+
const centred = Math.min(
|
|
228
|
+
start + Math.round((chipWidth - countWidth) / 2),
|
|
229
|
+
width - RIGHT_GAP - countWidth,
|
|
230
|
+
);
|
|
231
|
+
paintRow(painted, 1, options.subtle(count), countWidth, centred, width);
|
|
232
|
+
}
|
|
233
|
+
return painted;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const wrapped = (screen: string[], width: number, height: number) => {
|
|
237
|
+
// This runs inside the renderer's own frame, where anything thrown leaves
|
|
238
|
+
// the process with no handler and takes the session down. A decoration is
|
|
239
|
+
// never worth that, so a failure drops the chips for the frame instead.
|
|
240
|
+
let painted = screen;
|
|
241
|
+
try {
|
|
242
|
+
painted = decorate(screen, width);
|
|
243
|
+
} catch {
|
|
244
|
+
hit = undefined;
|
|
245
|
+
}
|
|
246
|
+
// Flashes composite last, so a transient message still wins the row.
|
|
247
|
+
return flashes.call(view, painted, width, height);
|
|
248
|
+
};
|
|
249
|
+
view.compositeFlashes = wrapped;
|
|
250
|
+
|
|
251
|
+
const listener: TuiInputListener = (data) => {
|
|
252
|
+
const button = hit;
|
|
253
|
+
if (!button) return undefined;
|
|
254
|
+
if (!data.startsWith("\x1b")) return undefined;
|
|
255
|
+
const match = SGR_MOUSE_TAIL.exec(data.slice(1));
|
|
256
|
+
if (!match || match[1] !== LEFT_BUTTON) return undefined;
|
|
257
|
+
const column = Number.parseInt(match[2] ?? "", 10) - 1;
|
|
258
|
+
const row = Number.parseInt(match[3] ?? "", 10) - 1;
|
|
259
|
+
if (row !== 0 || column < button.start || column >= button.end) {
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
// Consume the release too, so the renderer never sees a half press and
|
|
263
|
+
// starts a text selection at the chips.
|
|
264
|
+
if (match[4] === "M") jump(view, column < button.middle ? -1 : 1);
|
|
265
|
+
return { consume: true };
|
|
266
|
+
};
|
|
267
|
+
const unsubscribe = tui.addInputListener(listener);
|
|
268
|
+
prioritize(tui, listener);
|
|
269
|
+
|
|
270
|
+
// A disposer left over from a previous extension instance must not unwrap
|
|
271
|
+
// the installation that replaced it.
|
|
272
|
+
const dispose = () => {
|
|
273
|
+
if (view.compositeFlashes !== wrapped) return;
|
|
274
|
+
unsubscribe();
|
|
275
|
+
if (hadOwnMethod) view.compositeFlashes = flashes;
|
|
276
|
+
else delete (view as Partial<Scroller>).compositeFlashes;
|
|
277
|
+
hit = undefined;
|
|
278
|
+
delete target[INSTALLED];
|
|
279
|
+
};
|
|
280
|
+
target[INSTALLED] = dispose;
|
|
281
|
+
return dispose;
|
|
282
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { parseSkillBlock } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Characters kept from a single skill body when it is carried across a
|
|
5
|
+
* compaction. Roughly four characters per token, matching the estimate Pi's
|
|
6
|
+
* own compaction uses.
|
|
7
|
+
*/
|
|
8
|
+
const MAX_SKILL_CHARS = 16000;
|
|
9
|
+
/** Combined character budget for every skill carried across one compaction. */
|
|
10
|
+
const MAX_CARRY_CHARS = 24000;
|
|
11
|
+
const TRUNCATED = "\n\n[skill content truncated to fit the context budget]";
|
|
12
|
+
|
|
13
|
+
type ContextMessage = {
|
|
14
|
+
role: string;
|
|
15
|
+
content?: unknown;
|
|
16
|
+
};
|
|
17
|
+
type TextPart = { type: "text"; text: string };
|
|
18
|
+
type SessionEntry = {
|
|
19
|
+
type: string;
|
|
20
|
+
message?: { role?: string; content?: unknown };
|
|
21
|
+
};
|
|
22
|
+
type Invocation = { name: string; block: string };
|
|
23
|
+
|
|
24
|
+
function isTextPart(value: unknown): value is TextPart {
|
|
25
|
+
return (
|
|
26
|
+
typeof value === "object" &&
|
|
27
|
+
value !== null &&
|
|
28
|
+
(value as { type?: unknown }).type === "text" &&
|
|
29
|
+
typeof (value as { text?: unknown }).text === "string"
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Pi renders a skill invocation as one text part holding the block followed by
|
|
35
|
+
* the user's own request, so both live in a single message.
|
|
36
|
+
*/
|
|
37
|
+
function readInvocation(
|
|
38
|
+
text: string,
|
|
39
|
+
): { name: string; block: string; request: string } | undefined {
|
|
40
|
+
const parsed = parseSkillBlock(text);
|
|
41
|
+
if (!parsed) return undefined;
|
|
42
|
+
return {
|
|
43
|
+
name: parsed.name,
|
|
44
|
+
block: `<skill name="${parsed.name}" location="${parsed.location}">\n${parsed.content}\n</skill>`,
|
|
45
|
+
request: parsed.userMessage ?? "",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function messageInvocation(
|
|
50
|
+
message: ContextMessage | SessionEntry["message"],
|
|
51
|
+
): ReturnType<typeof readInvocation> {
|
|
52
|
+
const content = message?.content;
|
|
53
|
+
if (typeof content === "string") return readInvocation(content);
|
|
54
|
+
if (!Array.isArray(content)) return undefined;
|
|
55
|
+
for (const part of content) {
|
|
56
|
+
if (!isTextPart(part)) continue;
|
|
57
|
+
const invocation = readInvocation(part.text);
|
|
58
|
+
if (invocation) return invocation;
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function replaceInvocationText(
|
|
64
|
+
message: ContextMessage,
|
|
65
|
+
replacement: string,
|
|
66
|
+
): ContextMessage {
|
|
67
|
+
const content = message.content;
|
|
68
|
+
if (typeof content === "string") {
|
|
69
|
+
return { ...message, content: replacement };
|
|
70
|
+
}
|
|
71
|
+
if (!Array.isArray(content)) return message;
|
|
72
|
+
let done = false;
|
|
73
|
+
const next = content.map((part) => {
|
|
74
|
+
if (done || !isTextPart(part) || !parseSkillBlock(part.text)) return part;
|
|
75
|
+
done = true;
|
|
76
|
+
return { ...part, text: replacement };
|
|
77
|
+
});
|
|
78
|
+
return done ? { ...message, content: next } : message;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function prependText(message: ContextMessage, prefix: string): ContextMessage {
|
|
82
|
+
const content = message.content;
|
|
83
|
+
if (typeof content === "string") {
|
|
84
|
+
return { ...message, content: `${prefix}${content}` };
|
|
85
|
+
}
|
|
86
|
+
if (!Array.isArray(content)) return message;
|
|
87
|
+
const index = content.findIndex(isTextPart);
|
|
88
|
+
if (index < 0) {
|
|
89
|
+
return {
|
|
90
|
+
...message,
|
|
91
|
+
content: [{ type: "text", text: prefix }, ...content],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const part = content[index] as TextPart;
|
|
95
|
+
const next = [...content];
|
|
96
|
+
next[index] = { ...part, text: `${prefix}${part.text}` };
|
|
97
|
+
return { ...message, content: next };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function truncate(block: string, limit: number): string {
|
|
101
|
+
return block.length <= limit ? block : block.slice(0, limit) + TRUNCATED;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Keep every invoked skill present exactly once in the outgoing context.
|
|
106
|
+
*
|
|
107
|
+
* Pi inlines the whole `SKILL.md` body into a user message and then treats that
|
|
108
|
+
* message like any other: re-invoking a skill appends a second full copy, and
|
|
109
|
+
* compaction summarizes the copy away while the model keeps working under
|
|
110
|
+
* instructions it can no longer read. This collapses repeats to a short note
|
|
111
|
+
* and carries the most recent body across a compaction boundary, bounded by a
|
|
112
|
+
* character budget so the restored text cannot re-trigger the compaction that
|
|
113
|
+
* just ran.
|
|
114
|
+
*
|
|
115
|
+
* The first copy is never rewritten, so the cached request prefix stays byte
|
|
116
|
+
* stable across turns.
|
|
117
|
+
*/
|
|
118
|
+
export function pinSkillContext<T extends ContextMessage>(
|
|
119
|
+
messages: T[],
|
|
120
|
+
branch: SessionEntry[],
|
|
121
|
+
): T[] {
|
|
122
|
+
const seen = new Set<string>();
|
|
123
|
+
let changed = false;
|
|
124
|
+
|
|
125
|
+
let next = messages.map((message) => {
|
|
126
|
+
if (message.role !== "user") return message;
|
|
127
|
+
const invocation = messageInvocation(message);
|
|
128
|
+
if (!invocation) return message;
|
|
129
|
+
if (!seen.has(invocation.block)) {
|
|
130
|
+
seen.add(invocation.block);
|
|
131
|
+
return message;
|
|
132
|
+
}
|
|
133
|
+
changed = true;
|
|
134
|
+
const note = `[skill "${invocation.name}" is already loaded earlier in this conversation]`;
|
|
135
|
+
return replaceInvocationText(
|
|
136
|
+
message,
|
|
137
|
+
invocation.request ? `${note}\n\n${invocation.request}` : note,
|
|
138
|
+
) as T;
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const carried = carryAcrossCompaction(next, branch, seen);
|
|
142
|
+
if (!carried) return changed ? next : messages;
|
|
143
|
+
|
|
144
|
+
const anchor = restoreAnchor(next);
|
|
145
|
+
if (anchor < 0) return changed ? next : messages;
|
|
146
|
+
next = [...next];
|
|
147
|
+
next[anchor] = prependText(next[anchor] as ContextMessage, carried) as T;
|
|
148
|
+
return next;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Collect the newest body of each skill that the branch invoked but the
|
|
153
|
+
* compacted context no longer carries, newest first until the budget runs out.
|
|
154
|
+
*/
|
|
155
|
+
function carryAcrossCompaction(
|
|
156
|
+
messages: ContextMessage[],
|
|
157
|
+
branch: SessionEntry[],
|
|
158
|
+
present: Set<string>,
|
|
159
|
+
): string | undefined {
|
|
160
|
+
if (!messages.some((message) => message.role === "compactionSummary")) {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
const missing = new Map<string, Invocation>();
|
|
164
|
+
for (const entry of branch) {
|
|
165
|
+
if (entry.type !== "message" || entry.message?.role !== "user") continue;
|
|
166
|
+
const invocation = messageInvocation(entry.message);
|
|
167
|
+
if (!invocation) continue;
|
|
168
|
+
// A skill file edited mid-session yields a new body, so the newest state
|
|
169
|
+
// of each name decides: a present body cancels an earlier missing one.
|
|
170
|
+
if (present.has(invocation.block)) missing.delete(invocation.name);
|
|
171
|
+
else missing.set(invocation.name, invocation);
|
|
172
|
+
}
|
|
173
|
+
if (!missing.size) return undefined;
|
|
174
|
+
|
|
175
|
+
const blocks: string[] = [];
|
|
176
|
+
let used = 0;
|
|
177
|
+
for (const invocation of [...missing.values()].reverse()) {
|
|
178
|
+
const block = truncate(invocation.block, MAX_SKILL_CHARS);
|
|
179
|
+
if (used + block.length > MAX_CARRY_CHARS) continue;
|
|
180
|
+
used += block.length;
|
|
181
|
+
blocks.unshift(block);
|
|
182
|
+
}
|
|
183
|
+
return blocks.length ? `${blocks.join("\n\n")}\n\n` : undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The first user turn after the newest compaction summary. */
|
|
187
|
+
function restoreAnchor(messages: ContextMessage[]): number {
|
|
188
|
+
let summary = -1;
|
|
189
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
190
|
+
if (messages[index]?.role === "compactionSummary") {
|
|
191
|
+
summary = index;
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (summary < 0) return -1;
|
|
196
|
+
for (let index = summary + 1; index < messages.length; index += 1) {
|
|
197
|
+
if (messages[index]?.role === "user") return index;
|
|
198
|
+
}
|
|
199
|
+
return -1;
|
|
200
|
+
}
|
|
@@ -524,10 +524,10 @@ function handleClick(
|
|
|
524
524
|
const row = Number.parseInt(match[3] ?? "", 10) - 1;
|
|
525
525
|
const point = contentPoint(state.tui, column, row);
|
|
526
526
|
if (!point) return undefined;
|
|
527
|
-
const hit = state.hits.get(point.row);
|
|
528
|
-
if (!hit || point.col < 0 || point.col >= hit.end) return undefined;
|
|
529
527
|
const screenLine = screenLines(state.tui)[row];
|
|
530
|
-
if (!screenLine
|
|
528
|
+
if (!screenLine) return undefined;
|
|
529
|
+
const hit = findHit(state, point.row, screenLine);
|
|
530
|
+
if (!hit || point.col < 0 || point.col >= hit.end) return undefined;
|
|
531
531
|
if (match[4] === "M") {
|
|
532
532
|
state.expanded.set(
|
|
533
533
|
hit.owner,
|
|
@@ -538,6 +538,28 @@ function handleClick(
|
|
|
538
538
|
return { consume: true };
|
|
539
539
|
}
|
|
540
540
|
|
|
541
|
+
/**
|
|
542
|
+
* A wheel or keyboard scroll moves `scrollTop` immediately while the repaint
|
|
543
|
+
* is throttled, so the mapped content row can name a different item than the
|
|
544
|
+
* frame the click landed on. Fall back to the painted screen text whenever it
|
|
545
|
+
* identifies exactly one control.
|
|
546
|
+
*/
|
|
547
|
+
function findHit(
|
|
548
|
+
state: State,
|
|
549
|
+
contentRow: number,
|
|
550
|
+
screenLine: string,
|
|
551
|
+
): DetailHit | undefined {
|
|
552
|
+
const direct = state.hits.get(contentRow);
|
|
553
|
+
if (direct && sameHitText(screenLine, direct)) return direct;
|
|
554
|
+
let match: DetailHit | undefined;
|
|
555
|
+
for (const hit of state.hits.values()) {
|
|
556
|
+
if (!sameHitText(screenLine, hit)) continue;
|
|
557
|
+
if (match) return undefined;
|
|
558
|
+
match = hit;
|
|
559
|
+
}
|
|
560
|
+
return match;
|
|
561
|
+
}
|
|
562
|
+
|
|
541
563
|
function contentPoint(
|
|
542
564
|
tui: TUI,
|
|
543
565
|
column: number,
|