proper-base 0.2.5 → 0.2.6
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/index.ts +1 -1
- package/package.json +1 -1
- package/src/transcript-cleanup.ts +123 -88
package/index.ts
CHANGED
|
@@ -572,7 +572,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
572
572
|
activeTui = tui;
|
|
573
573
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Settled transcript]]
|
|
574
574
|
transcriptCleanup?.uninstall();
|
|
575
|
-
transcriptCleanup = installTranscriptCleanup(tui, ctx
|
|
575
|
+
transcriptCleanup = installTranscriptCleanup(tui, ctx);
|
|
576
576
|
removeSmartSelection?.();
|
|
577
577
|
removeSmartSelection = installSmartSelection(tui);
|
|
578
578
|
historyGuard = installHistoryGuard(editor);
|
package/package.json
CHANGED
|
@@ -10,13 +10,9 @@ import {
|
|
|
10
10
|
} from "@earendil-works/pi-tui";
|
|
11
11
|
|
|
12
12
|
const INSTALLED = Symbol.for("pi-proper-base.transcript-cleanup");
|
|
13
|
-
const PROCESS_LINK_PREFIX = "pi-proper-base://process/";
|
|
14
13
|
const SGR_MOUSE_TAIL = /^\[<(\d+);(\d+);(\d+)([Mm])$/;
|
|
15
14
|
const LEFT_BUTTON = "0";
|
|
16
15
|
|
|
17
|
-
type Keybindings = {
|
|
18
|
-
getKeys(action: "app.tools.expand"): readonly string[];
|
|
19
|
-
};
|
|
20
16
|
type ComponentContainer = Component & { children: Component[] };
|
|
21
17
|
type AssistantMessage = {
|
|
22
18
|
content: Array<{ type: string; text?: string; thinking?: string }>;
|
|
@@ -31,17 +27,20 @@ type AssistantComponent = Component & {
|
|
|
31
27
|
type InstalledContainer = ComponentContainer & {
|
|
32
28
|
[INSTALLED]?: TranscriptCleanupController;
|
|
33
29
|
};
|
|
30
|
+
type DetailHit = {
|
|
31
|
+
owner: object;
|
|
32
|
+
line: string;
|
|
33
|
+
end: number;
|
|
34
|
+
};
|
|
34
35
|
type State = {
|
|
35
36
|
ctx: ExtensionContext;
|
|
36
37
|
tui: TUI;
|
|
37
|
-
keybindings: Keybindings;
|
|
38
38
|
activeStart: number | undefined;
|
|
39
39
|
completed: Set<Component>;
|
|
40
40
|
owned: Set<Component>;
|
|
41
41
|
expanded: Map<object, boolean>;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
nextId: number;
|
|
42
|
+
hits: Map<number, DetailHit>;
|
|
43
|
+
pendingHits: DetailHit[];
|
|
45
44
|
globalExpanded: boolean;
|
|
46
45
|
assistantCache: WeakMap<AssistantComponent, AssistantRenderCache>;
|
|
47
46
|
};
|
|
@@ -68,6 +67,16 @@ type MouseTui = TUI & {
|
|
|
68
67
|
inputListeners?: Set<TuiInputListener>;
|
|
69
68
|
previousLines?: string[];
|
|
70
69
|
previousScreen?: string[];
|
|
70
|
+
getPrimaryScrollView?(): unknown;
|
|
71
|
+
getScrollSelectionPoint?(
|
|
72
|
+
scrollView: unknown,
|
|
73
|
+
x: number,
|
|
74
|
+
y: number,
|
|
75
|
+
): { row: number; col: number } | undefined;
|
|
76
|
+
};
|
|
77
|
+
type TranscriptContainers = {
|
|
78
|
+
document: ComponentContainer;
|
|
79
|
+
chat: InstalledContainer;
|
|
71
80
|
};
|
|
72
81
|
|
|
73
82
|
export type TranscriptCleanupController = {
|
|
@@ -75,33 +84,31 @@ export type TranscriptCleanupController = {
|
|
|
75
84
|
completeAssistant(message: object): void;
|
|
76
85
|
completeTool(toolCallId: string): void;
|
|
77
86
|
settle(): void;
|
|
78
|
-
update(ctx: ExtensionContext, tui: TUI
|
|
87
|
+
update(ctx: ExtensionContext, tui: TUI): void;
|
|
79
88
|
uninstall(): void;
|
|
80
89
|
};
|
|
81
90
|
|
|
82
91
|
export function installTranscriptCleanup(
|
|
83
92
|
tui: TUI,
|
|
84
93
|
ctx: ExtensionContext,
|
|
85
|
-
keybindings: Keybindings,
|
|
86
94
|
): TranscriptCleanupController | undefined {
|
|
87
|
-
const
|
|
88
|
-
if (!
|
|
95
|
+
const containers = findTranscriptContainers(tui);
|
|
96
|
+
if (!containers) return undefined;
|
|
97
|
+
const { document, chat } = containers;
|
|
89
98
|
if (chat[INSTALLED]) {
|
|
90
|
-
chat[INSTALLED].update(ctx, tui
|
|
99
|
+
chat[INSTALLED].update(ctx, tui);
|
|
91
100
|
return chat[INSTALLED];
|
|
92
101
|
}
|
|
93
102
|
|
|
94
103
|
let state: State = {
|
|
95
104
|
ctx,
|
|
96
105
|
tui,
|
|
97
|
-
keybindings,
|
|
98
106
|
activeStart: ctx.isIdle() ? undefined : chat.children.length,
|
|
99
107
|
completed: new Set(),
|
|
100
108
|
owned: new Set(chat.children),
|
|
101
109
|
expanded: new Map(),
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
nextId: 0,
|
|
110
|
+
hits: new Map(),
|
|
111
|
+
pendingHits: [],
|
|
105
112
|
globalExpanded: ctx.ui.getToolsExpanded(),
|
|
106
113
|
assistantCache: new WeakMap(),
|
|
107
114
|
};
|
|
@@ -163,12 +170,11 @@ export function installTranscriptCleanup(
|
|
|
163
170
|
};
|
|
164
171
|
state.tui.requestRender();
|
|
165
172
|
},
|
|
166
|
-
update(nextCtx, nextTui
|
|
173
|
+
update(nextCtx, nextTui) {
|
|
167
174
|
state = {
|
|
168
175
|
...state,
|
|
169
176
|
ctx: nextCtx,
|
|
170
177
|
tui: nextTui,
|
|
171
|
-
keybindings: nextKeybindings,
|
|
172
178
|
};
|
|
173
179
|
},
|
|
174
180
|
uninstall() {
|
|
@@ -190,15 +196,19 @@ export function installTranscriptCleanup(
|
|
|
190
196
|
if (globalExpanded !== state.globalExpanded) {
|
|
191
197
|
state = { ...state, globalExpanded, expanded: new Map() };
|
|
192
198
|
}
|
|
193
|
-
state.
|
|
199
|
+
state.pendingHits.length = 0;
|
|
200
|
+
let lines: string[];
|
|
194
201
|
if (state.activeStart === undefined && state.ctx.isIdle()) {
|
|
195
|
-
|
|
202
|
+
lines = renderCollapsed(chat.children, width, state);
|
|
203
|
+
} else {
|
|
204
|
+
const start = state.activeStart ?? chat.children.length;
|
|
205
|
+
lines = [
|
|
206
|
+
...renderCollapsed(chat.children.slice(0, start), width, state),
|
|
207
|
+
...renderActive(chat.children.slice(start), width, state),
|
|
208
|
+
];
|
|
196
209
|
}
|
|
197
|
-
|
|
198
|
-
return
|
|
199
|
-
...renderCollapsed(chat.children.slice(0, start), width, state),
|
|
200
|
-
...renderActive(chat.children.slice(start), width, state),
|
|
201
|
-
];
|
|
210
|
+
indexDetailHits(lines, documentRowOffset(document, chat, width), state);
|
|
211
|
+
return lines;
|
|
202
212
|
};
|
|
203
213
|
chat[INSTALLED] = controller;
|
|
204
214
|
return controller;
|
|
@@ -407,28 +417,20 @@ function hasSectionText(lines: string[]): boolean {
|
|
|
407
417
|
}
|
|
408
418
|
|
|
409
419
|
function renderDetail(detail: Detail, width: number, state: State): string[] {
|
|
410
|
-
let id = state.ids.get(detail.owner);
|
|
411
|
-
if (!id) {
|
|
412
|
-
id = String(++state.nextId);
|
|
413
|
-
state.ids.set(detail.owner, id);
|
|
414
|
-
}
|
|
415
|
-
state.groups.set(id, detail.owner);
|
|
416
420
|
const expanded = state.expanded.get(detail.owner) ?? state.globalExpanded;
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
? ` (click or ${keys} to ${action})`
|
|
421
|
-
: ` (click to ${action})`;
|
|
422
|
-
const label = `${expanded ? "▾" : "▸"} ${detail.label}${hint}`;
|
|
423
|
-
const styled = state.ctx.ui.theme.fg(detailColor(detail.kind), label);
|
|
424
|
-
const header = truncateToWidth(
|
|
425
|
-
`\x1b]8;;${PROCESS_LINK_PREFIX}${id}\x07${styled}\x1b]8;;\x07`,
|
|
426
|
-
width,
|
|
427
|
-
"",
|
|
421
|
+
const marker = state.ctx.ui.theme.fg(
|
|
422
|
+
"borderAccent",
|
|
423
|
+
state.ctx.ui.theme.bold(expanded ? "⌄" : "›"),
|
|
428
424
|
);
|
|
429
|
-
const
|
|
430
|
-
|
|
431
|
-
|
|
425
|
+
const label = state.ctx.ui.theme.fg(detailColor(detail.kind), detail.label);
|
|
426
|
+
const header = truncateToWidth(`${marker} ${label}`, width, "");
|
|
427
|
+
const headerLine = `${header}${" ".repeat(Math.max(0, width - visibleWidth(header)))}`;
|
|
428
|
+
state.pendingHits.push({
|
|
429
|
+
owner: detail.owner,
|
|
430
|
+
line: headerLine,
|
|
431
|
+
end: visibleWidth(header),
|
|
432
|
+
});
|
|
433
|
+
const lines = [headerLine];
|
|
432
434
|
const expandable = detail.component as ToolComponent;
|
|
433
435
|
// setExpanded rebuilds the tool's result renderer even when the state is
|
|
434
436
|
// unchanged, which re-sanitizes full outputs on every frame. Skip no-ops.
|
|
@@ -436,11 +438,14 @@ function renderDetail(detail: Detail, width: number, state: State): string[] {
|
|
|
436
438
|
if (expanded) {
|
|
437
439
|
lines.push(...detail.component.render(width));
|
|
438
440
|
const button = `\x1b[7m ${state.ctx.ui.theme.fg(detailColor(detail.kind), "collapse")} \x1b[27m`;
|
|
439
|
-
const
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
441
|
+
const rendered = truncateToWidth(button, width, "");
|
|
442
|
+
const buttonLine = `${rendered}${" ".repeat(Math.max(0, width - visibleWidth(rendered)))}`;
|
|
443
|
+
state.pendingHits.push({
|
|
444
|
+
owner: detail.owner,
|
|
445
|
+
line: buttonLine,
|
|
446
|
+
end: visibleWidth(rendered),
|
|
447
|
+
});
|
|
448
|
+
lines.push(buttonLine);
|
|
444
449
|
}
|
|
445
450
|
return lines;
|
|
446
451
|
}
|
|
@@ -459,7 +464,16 @@ function detailColor(
|
|
|
459
464
|
function describeTool(tool: ToolComponent, error: boolean): string {
|
|
460
465
|
const name = tool.toolName ?? "tool";
|
|
461
466
|
const args = asRecord(tool.args);
|
|
462
|
-
const value = [
|
|
467
|
+
const value = [
|
|
468
|
+
"tool",
|
|
469
|
+
"path",
|
|
470
|
+
"command",
|
|
471
|
+
"pattern",
|
|
472
|
+
"query",
|
|
473
|
+
"task",
|
|
474
|
+
"url",
|
|
475
|
+
"action",
|
|
476
|
+
]
|
|
463
477
|
.map((key) => args?.[key])
|
|
464
478
|
.find((candidate): candidate is string => typeof candidate === "string");
|
|
465
479
|
const target = value ? ` · ${oneLine(value)}` : "";
|
|
@@ -476,6 +490,29 @@ function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
|
476
490
|
: undefined;
|
|
477
491
|
}
|
|
478
492
|
|
|
493
|
+
function documentRowOffset(
|
|
494
|
+
document: ComponentContainer,
|
|
495
|
+
chat: ComponentContainer,
|
|
496
|
+
width: number,
|
|
497
|
+
): number {
|
|
498
|
+
const chatIndex = document.children.indexOf(chat);
|
|
499
|
+
if (chatIndex <= 0) return 0;
|
|
500
|
+
return document.children
|
|
501
|
+
.slice(0, chatIndex)
|
|
502
|
+
.reduce((rows, child) => rows + child.render(width).length, 0);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function indexDetailHits(lines: string[], offset: number, state: State): void {
|
|
506
|
+
state.hits.clear();
|
|
507
|
+
let nextRow = 0;
|
|
508
|
+
for (const hit of state.pendingHits) {
|
|
509
|
+
const relativeRow = lines.indexOf(hit.line, nextRow);
|
|
510
|
+
if (relativeRow < 0) continue;
|
|
511
|
+
state.hits.set(offset + relativeRow, hit);
|
|
512
|
+
nextRow = relativeRow + 1;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
479
516
|
function handleClick(
|
|
480
517
|
data: string,
|
|
481
518
|
state: State,
|
|
@@ -485,51 +522,44 @@ function handleClick(
|
|
|
485
522
|
if (!match || match[1] !== LEFT_BUTTON) return undefined;
|
|
486
523
|
const column = Number.parseInt(match[2] ?? "", 10) - 1;
|
|
487
524
|
const row = Number.parseInt(match[3] ?? "", 10) - 1;
|
|
488
|
-
const
|
|
489
|
-
if (!
|
|
490
|
-
const
|
|
491
|
-
if (!
|
|
492
|
-
const
|
|
493
|
-
if (!
|
|
525
|
+
const point = contentPoint(state.tui, column, row);
|
|
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
|
+
const screenLine = screenLines(state.tui)[row];
|
|
530
|
+
if (!screenLine || !sameHitText(screenLine, hit)) return undefined;
|
|
494
531
|
if (match[4] === "M") {
|
|
495
532
|
state.expanded.set(
|
|
496
|
-
owner,
|
|
497
|
-
!(state.expanded.get(owner) ?? state.globalExpanded),
|
|
533
|
+
hit.owner,
|
|
534
|
+
!(state.expanded.get(hit.owner) ?? state.globalExpanded),
|
|
498
535
|
);
|
|
499
536
|
state.tui.requestRender();
|
|
500
537
|
}
|
|
501
538
|
return { consume: true };
|
|
502
539
|
}
|
|
503
540
|
|
|
504
|
-
function
|
|
541
|
+
function contentPoint(
|
|
542
|
+
tui: TUI,
|
|
543
|
+
column: number,
|
|
544
|
+
row: number,
|
|
545
|
+
): { row: number; col: number } | undefined {
|
|
505
546
|
const host = tui as MouseTui;
|
|
506
|
-
|
|
547
|
+
const scrollView = host.getPrimaryScrollView?.();
|
|
548
|
+
return scrollView !== undefined && host.getScrollSelectionPoint
|
|
549
|
+
? host.getScrollSelectionPoint(scrollView, column, row)
|
|
550
|
+
: { row, col: column };
|
|
507
551
|
}
|
|
508
552
|
|
|
509
|
-
function
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
}
|
|
520
|
-
if (line[index] === "\x1b") {
|
|
521
|
-
const end = line.indexOf("m", index + 1);
|
|
522
|
-
if (end < 0) break;
|
|
523
|
-
index = end + 1;
|
|
524
|
-
continue;
|
|
525
|
-
}
|
|
526
|
-
const character = line[index] ?? "";
|
|
527
|
-
const width = visibleWidth(character);
|
|
528
|
-
if (column >= visible && column < visible + width) return active;
|
|
529
|
-
visible += width;
|
|
530
|
-
index += character.length;
|
|
531
|
-
}
|
|
532
|
-
return undefined;
|
|
553
|
+
function sameHitText(line: string, hit: DetailHit): boolean {
|
|
554
|
+
return (
|
|
555
|
+
truncateToWidth(stripTerminalSequences(line), hit.end, "") ===
|
|
556
|
+
truncateToWidth(stripTerminalSequences(hit.line), hit.end, "")
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
function screenLines(tui: TUI): string[] {
|
|
561
|
+
const host = tui as MouseTui;
|
|
562
|
+
return host.previousScreen ?? host.previousLines ?? [];
|
|
533
563
|
}
|
|
534
564
|
|
|
535
565
|
function prioritize(tui: TUI, listener: TuiInputListener): void {
|
|
@@ -541,7 +571,7 @@ function prioritize(tui: TUI, listener: TuiInputListener): void {
|
|
|
541
571
|
for (const entry of rest) listeners.add(entry);
|
|
542
572
|
}
|
|
543
573
|
|
|
544
|
-
function
|
|
574
|
+
function findTranscriptContainers(tui: TUI): TranscriptContainers | undefined {
|
|
545
575
|
for (const root of roots(tui)) {
|
|
546
576
|
const document = find(root, (component) => {
|
|
547
577
|
const children = childrenOf(component);
|
|
@@ -553,7 +583,12 @@ function findChatContainer(tui: TUI): ComponentContainer | undefined {
|
|
|
553
583
|
);
|
|
554
584
|
});
|
|
555
585
|
const chat = document ? childrenOf(document)[2] : undefined;
|
|
556
|
-
if (
|
|
586
|
+
if (document && chat) {
|
|
587
|
+
return {
|
|
588
|
+
document: document as ComponentContainer,
|
|
589
|
+
chat: chat as InstalledContainer,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
557
592
|
}
|
|
558
593
|
return undefined;
|
|
559
594
|
}
|