@notrico/pi-fold 0.0.0-stage → 3.0.1

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.
Files changed (76) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/LICENSE +21 -0
  3. package/README.md +71 -2
  4. package/docs/implementation.md +47 -0
  5. package/docs/performance.md +118 -0
  6. package/docs/renderer-reference.md +110 -0
  7. package/index.ts +53 -0
  8. package/package.json +55 -4
  9. package/scripts/performance-review.mjs +273 -0
  10. package/scripts/preview-inspection.mjs +9 -0
  11. package/scripts/preview-work-strip.mjs +101 -0
  12. package/scripts/preview.mjs +232 -0
  13. package/scripts/retained-memory-review.mjs +46 -0
  14. package/scripts/test.mjs +21 -0
  15. package/src/action-intent.ts +93 -0
  16. package/src/display-card.ts +66 -0
  17. package/src/display-line-cache.ts +26 -0
  18. package/src/display-mode.ts +33 -0
  19. package/src/fold-command.ts +61 -0
  20. package/src/inspection.ts +87 -0
  21. package/src/modern-code-content.ts +131 -0
  22. package/src/modern-code-pane.ts +103 -0
  23. package/src/modern-native-tools.ts +79 -0
  24. package/src/modern-prompts-adapter.ts +41 -0
  25. package/src/modern-prompts.ts +185 -0
  26. package/src/modern-recorded-pane.ts +68 -0
  27. package/src/modern-rich-text.ts +48 -0
  28. package/src/modern-skill-prompts.ts +152 -0
  29. package/src/modern-subagent-groups.ts +158 -0
  30. package/src/modern-subagent-messages.ts +176 -0
  31. package/src/modern-subagent-spacing.ts +35 -0
  32. package/src/modern-subagents.ts +52 -0
  33. package/src/modern-thinking-adapter.ts +85 -0
  34. package/src/modern-thinking.ts +258 -0
  35. package/src/modern-tool-content.ts +28 -0
  36. package/src/modern-tool-images.ts +63 -0
  37. package/src/modern-tool-pane.ts +219 -0
  38. package/src/modern-tools.ts +509 -0
  39. package/src/modern-workflows.ts +212 -0
  40. package/src/native-method-chain.ts +31 -0
  41. package/src/permission-presentation.ts +280 -0
  42. package/src/recorded-summary.ts +13 -0
  43. package/src/work-strip-subagents.ts +221 -0
  44. package/src/work-strip-view.ts +112 -0
  45. package/src/work-strip.ts +443 -0
  46. package/tests/action-intent.test.mjs +44 -0
  47. package/tests/codemode.test.mjs +388 -0
  48. package/tests/display-cache-lifecycle.test.mjs +55 -0
  49. package/tests/display-line-cache.test.mjs +48 -0
  50. package/tests/display-mode.test.mjs +25 -0
  51. package/tests/fixtures/preview-fixture.ts +81 -0
  52. package/tests/fold-command.test.mjs +150 -0
  53. package/tests/fold-shortcut.test.mjs +143 -0
  54. package/tests/input-focus.test.mjs +95 -0
  55. package/tests/inspection-wheel.test.mjs +73 -0
  56. package/tests/inspection.test.mjs +50 -0
  57. package/tests/mixed-thinking.test.mjs +371 -0
  58. package/tests/modern-prompts.test.mjs +84 -0
  59. package/tests/modern-skill-prompts.test.mjs +174 -0
  60. package/tests/modern-subagent-groups.test.mjs +132 -0
  61. package/tests/modern-subagent-messages.test.mjs +137 -0
  62. package/tests/modern-thinking.test.mjs +202 -0
  63. package/tests/modern-tools.test.mjs +961 -0
  64. package/tests/modern-workflows.test.mjs +189 -0
  65. package/tests/ordinary-prompts.test.mjs +167 -0
  66. package/tests/permission-presentation.test.mjs +257 -0
  67. package/tests/preview-example.test.mjs +171 -0
  68. package/tests/reload-lifecycle.test.mjs +161 -0
  69. package/tests/subagent-spacing.test.mjs +34 -0
  70. package/tests/test-runtime.mjs +58 -0
  71. package/tests/work-strip-final-surface.test.mjs +321 -0
  72. package/tests/work-strip-hierarchy.test.mjs +258 -0
  73. package/tests/work-strip-preview.test.mjs +120 -0
  74. package/tests/work-strip-spacing.test.mjs +208 -0
  75. package/tests/work-strip-subagents.test.mjs +236 -0
  76. package/tests/work-strip.test.mjs +148 -0
@@ -0,0 +1,219 @@
1
+ import { matchesKey, truncateToWidth, visibleWidth, type TuiMouseEvent, type TuiMouseEventResult } from "@earendil-works/pi-tui";
2
+ import { suppressRepeatedSummary } from "./recorded-summary.ts";
3
+ import { richTextRows, type TextFormat } from "./modern-rich-text.ts";
4
+ import { activityRow, cardContentWidth } from "./display-card.ts";
5
+
6
+ type RowKind = "section" | "field" | "answer";
7
+ export type PaneContent = string | { text: string; kind?: RowKind; indent?: number; label?: string } & TextFormat;
8
+ export type PaneAction = { id: string; label: string; repeatedSummary?: string; sections: () => Iterable<PaneContent>; details?: () => Iterable<PaneContent> };
9
+ // Render only visited text chunks. Never split/highlight an entire result to paint a viewport.
10
+ export function* textRows(text: string, width: number): Generator<string> {
11
+ yield* richTextRows(text, width);
12
+ }
13
+
14
+ /** Inline viewport. Expansion belongs to each action, never to a native renderer cache. */
15
+ export class ToolPane {
16
+ open = false;
17
+ focused = false;
18
+ expandedTitle?: string;
19
+ styleHeader = (text: string) => text;
20
+ theme?: any;
21
+ background = "toolSuccessBg";
22
+ headingTone = "mdLink";
23
+ itemName = "action";
24
+ directContent = false;
25
+ selected = 0;
26
+ private anchor = 0;
27
+ offset = 0;
28
+ expanded = new Set<string>();
29
+ detailsExpanded = new Set<string>();
30
+ private detailsFocused = false;
31
+ private rows: string[] = [];
32
+ private iterator?: Iterator<string>;
33
+ private key = "";
34
+ private done = false;
35
+ private actionRows = new Map<number, number>();
36
+ private detailsRows = new Map<number, number>();
37
+ private contentAnchoredAbove = false;
38
+ private rowKinds = new Map<number, RowKind>();
39
+ private lastWidth = 0;
40
+ private lastHeight = 0;
41
+ private lastBodyHeight = 0;
42
+ private lastMore = false;
43
+ private contentFocused = false;
44
+ private preserveFocusLayout = false;
45
+ // Instrumentation also makes the large-output contract testable without wall-clock assertions.
46
+ paintedRows = 0;
47
+ constructor(public actions: PaneAction[], public title: string, private height = () => 12) {}
48
+ private get directDetails() { return this.directContent || this.itemName === "action" && this.actions.length === 1; }
49
+ reset() { this.preserveFocusLayout = false; this.anchor = Math.min(this.anchor, Math.max(0, this.actions.length - 1)); this.key = ""; this.rows = []; this.iterator = undefined; this.done = false; this.actionRows.clear(); this.detailsRows.clear(); this.rowKinds.clear(); }
50
+ collapse() { this.open = false; this.focused = false; this.selected = 0; this.anchor = 0; this.offset = 0; this.expanded.clear(); this.detailsExpanded.clear(); this.detailsFocused = false; this.reset(); }
51
+ releaseInspection() { if (this.detailsFocused) { this.detailsFocused = false; this.offset = 0; this.reset(); } }
52
+ handle(data: string): boolean {
53
+ if (matchesKey(data, "home")) { this.open = true; this.selected = 0; this.detailsFocused = false; this.anchor = 0; this.offset = 0; this.reset(); return true; }
54
+ if (matchesKey(data, "enter")) {
55
+ const action = this.actions[this.selected];
56
+ if (this.detailsFocused && action?.details) {
57
+ this.detailsExpanded.has(action.id) ? this.detailsExpanded.delete(action.id) : this.detailsExpanded.add(action.id);
58
+ }
59
+ else if (this.directDetails) this.open = !this.open;
60
+ else if (!this.open) this.open = true;
61
+ else { const id = this.actions[this.selected]?.id; if (id) this.expanded.has(id) ? this.expanded.delete(id) : this.expanded.add(id); }
62
+ this.offset = 0; this.reset(); return true;
63
+ }
64
+ if ((matchesKey(data, "tab") || matchesKey(data, "shift+tab")) && this.actions[this.selected]?.details) {
65
+ this.open = true; this.anchor = this.selected; this.expanded.add(this.actions[this.selected].id);
66
+ this.detailsFocused = !this.detailsFocused;
67
+ this.offset = 0; this.reset(); return true;
68
+ }
69
+ if (matchesKey(data, "right") || matchesKey(data, "tab") || matchesKey(data, "left") || matchesKey(data, "shift+tab")) {
70
+ const direction = matchesKey(data, "left") || matchesKey(data, "shift+tab") ? -1 : 1;
71
+ this.selected = (this.selected + direction + this.actions.length) % Math.max(1, this.actions.length);
72
+ this.detailsFocused = false; this.anchor = this.selected; this.open = true; this.offset = 0; this.reset(); return true;
73
+ }
74
+ if (["up", "down", "pageUp", "pageDown"].some(key => matchesKey(data, key as any))) {
75
+ const distance = matchesKey(data, "pageUp") || matchesKey(data, "pageDown") ? Math.max(1, this.height() - 4) : 1;
76
+ const down = matchesKey(data, "down") || matchesKey(data, "pageDown");
77
+ if (!down && this.contentAnchoredAbove && this.detailsFocused && this.offset === 0) { this.releaseInspection(); return true; }
78
+ this.open = true; this.offset = Math.max(0, this.offset + (down ? distance : -distance)); return true;
79
+ }
80
+ return false;
81
+ }
82
+ private *body(width: number): Generator<string> {
83
+ this.contentAnchoredAbove = false;
84
+ const indent = width >= 12 ? " " : "";
85
+ // Selection anchors the viewport at an action, avoiding scans through an earlier huge output.
86
+ for (let index = this.anchor; index < this.actions.length; index++) {
87
+ const action = this.actions[index];
88
+ const prefix = `${indent}${index === this.selected ? "›" : " "} ${this.expanded.has(action.id) ? "▾" : "▸"} ${index + 1}. `;
89
+ if (!this.directDetails) {
90
+ this.actionRows.set(this.rows.length, index);
91
+ yield prefix + action.label;
92
+ }
93
+ const detailsIndent = this.directDetails ? indent : width >= 12 ? " ".repeat(Math.min(prefix.length, width - 1)) : "";
94
+ if (this.directDetails || this.expanded.has(action.id)) {
95
+ const sectionRows = function* (this: ToolPane, sections: Iterable<PaneContent>, summary?: string) {
96
+ let firstSection = true;
97
+ for (const section of sections) {
98
+ const content = typeof section === "string" ? { text: section } : section;
99
+ const indent = detailsIndent + " ".repeat(Math.min(content.indent || 0, Math.max(0, width - detailsIndent.length - 1)));
100
+ const label = content.label ? `${content.label} ` : "";
101
+ const text = firstSection && summary ? suppressRepeatedSummary(content.text, summary) : content.text;
102
+ firstSection = false;
103
+ let first = true;
104
+ for (const row of richTextRows(text, Math.max(1, width - indent.length - label.length), content)) {
105
+ yield { text: indent + (first && label ? this.theme ? this.theme.fg("muted", label) : label : " ".repeat(label.length)) + row, kind: content.kind };
106
+ first = false;
107
+ }
108
+ }
109
+ }.bind(this);
110
+ const emitRows = function* (this: ToolPane, rows: Iterable<{ text: string; kind?: RowKind }>) {
111
+ for (const row of rows) { if (row.kind) this.rowKinds.set(this.rows.length, row.kind); yield row.text; }
112
+ }.bind(this);
113
+ const fullLabel = this.directDetails ? `▾ ${this.expandedTitle ?? this.title}${this.contentFocused ? " · focused" : ""}` : prefix + action.label;
114
+ const summary = visibleWidth(fullLabel) <= width ? action.repeatedSummary : undefined;
115
+ if (this.detailsFocused && index === this.selected) {
116
+ // Keep short output inline; probe at most one viewport before anchoring at Details.
117
+ const primary = sectionRows(action.sections(), summary), preview = [];
118
+ const budget = Math.max(1, Math.min(20, this.height()) - (this.contentFocused ? 2 : 1) - (this.directDetails ? 0 : 1) - 1);
119
+ let complete = false;
120
+ while (preview.length <= budget) { const row = primary.next(); if (row.done) { complete = true; break; } preview.push(row.value); }
121
+ if (complete) yield* emitRows(preview);
122
+ else { this.contentAnchoredAbove = true; yield detailsIndent + (this.theme ? this.theme.fg("dim", "content above ↑") : "content above ↑"); }
123
+ } else yield* emitRows(sectionRows(action.sections(), summary));
124
+ if (action.details) {
125
+ this.detailsRows.set(this.rows.length, index);
126
+ yield detailsIndent + `${this.detailsExpanded.has(action.id) ? "▾" : "▸"} Details`;
127
+ if (this.detailsExpanded.has(action.id)) yield* emitRows(sectionRows(action.details()));
128
+ }
129
+ }
130
+ }
131
+ }
132
+ render(width: number): string[] {
133
+ width = Math.max(1, width);
134
+ this.lastWidth = width;
135
+ const outerWidth = width;
136
+ width = cardContentWidth(width);
137
+ const paint = (line: string, selected = false) => {
138
+ line = this.theme ? this.theme.fg("text", line) : line;
139
+ return activityRow(line, outerWidth, this.theme, selected);
140
+ };
141
+ const header = this.styleHeader(`${this.open ? "▾" : "▸"} ${this.open ? this.expandedTitle ?? this.title : this.title}${this.focused ? " · focused" : ""}`);
142
+ if (!this.open) { this.lastHeight = 1; this.lastBodyHeight = 0; this.lastMore = false; return [paint(truncateToWidth(header, width), this.focused && !this.detailsFocused)]; }
143
+ const height = Math.max(5, Math.min(20, this.height()));
144
+ if (this.focused || this.offset === 0) this.preserveFocusLayout = false;
145
+ // Mouse scrolling releases keyboard focus. Keep the already visited source layout
146
+ // so that release cannot reset its offset or reformat an unbounded prefix.
147
+ const contentFocused = this.preserveFocusLayout ? this.contentFocused : this.focused;
148
+ const key = `${width}:${contentFocused}:${this.expandedTitle ?? this.title}:${this.directDetails}:${this.anchor}:${this.selected}:${[...this.expanded].join("|")}:${[...this.detailsExpanded].join("|")}:${this.detailsFocused}`;
149
+ if (key !== this.key) { this.reset(); this.key = key; this.contentFocused = contentFocused; this.iterator = this.body(width); }
150
+ let bodyHeight = height - (this.focused ? 2 : 1);
151
+ // Clamp a seek to one viewport beyond visited content; a key press cannot scan unbounded data.
152
+ this.offset = Math.min(this.offset, this.rows.length);
153
+ while (!this.done && this.rows.length < this.offset + bodyHeight + 1) {
154
+ const next = this.iterator!.next();
155
+ if (next.done) this.done = true;
156
+ else { this.rows.push(truncateToWidth(next.value, width)); this.paintedRows++; }
157
+ }
158
+ // Keep the footer and body height stable after paging to the end. Otherwise
159
+ // the pointer can leave the pane when the downward control disappears.
160
+ if (!this.focused && (!this.done || this.rows.length > bodyHeight)) bodyHeight--;
161
+ if (this.done) this.offset = Math.min(this.offset, Math.max(0, this.rows.length - bodyHeight));
162
+ const more = !this.done || this.offset + bodyHeight < this.rows.length;
163
+ const selectedAction = this.actions[this.selected];
164
+ const detailsTarget = this.detailsFocused && !!selectedAction?.details;
165
+ const isExpanded = detailsTarget ? this.detailsExpanded.has(selectedAction.id) : this.directDetails || this.expanded.has(selectedAction?.id);
166
+ const controls = detailsTarget ? `Enter ${isExpanded ? "collapse" : "expand"} Details · Tab action` : `${this.directDetails ? "Enter collapse" : `←/→ ${this.itemName} · Enter ${isExpanded ? "collapse" : "expand"}`}${selectedAction?.details ? " · Tab Details" : ""}`;
167
+ const footer = [more ? "more ↓" : this.offset > 0 ? "more ↑" : "", this.focused ? `${controls} · ↑/↓ scroll · Esc release` : ""].filter(Boolean).join(" · ");
168
+ const body = this.rows.slice(this.offset, this.offset + bodyHeight).map((line, index) => {
169
+ const action = this.actionRows.get(this.offset + index);
170
+ const details = this.detailsRows.get(this.offset + index);
171
+ const selected = this.focused && (this.detailsFocused ? details === this.selected : action === this.selected);
172
+ if (this.theme && (action !== undefined || details !== undefined)) line = this.theme.fg(selected ? "accent" : this.headingTone, this.theme.bold(line));
173
+ else if (this.theme && this.rowKinds.has(this.offset + index)) {
174
+ const kind = this.rowKinds.get(this.offset + index);
175
+ line = this.theme.fg(kind === "answer" ? "success" : "muted", kind === "field" ? line : this.theme.bold(line));
176
+ }
177
+ return paint(line, selected);
178
+ });
179
+ const visible = [paint(truncateToWidth(header, width), this.focused && !this.detailsFocused), ...body, ...(footer ? [paint(truncateToWidth(this.theme ? this.theme.fg("dim", footer) : footer, width))] : [])];
180
+ this.lastBodyHeight = body.length; this.lastMore = more;
181
+ this.lastHeight = visible.length;
182
+ return visible;
183
+ }
184
+ handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
185
+ if (event.x < 0 || event.x >= this.lastWidth || event.y < 0 || event.y >= this.lastHeight) return undefined;
186
+ if (event.type === "wheel") {
187
+ if (!this.open || !Number.isFinite(event.wheelDelta) || !event.wheelDelta) return undefined;
188
+ this.preserveFocusLayout = true;
189
+ if (event.wheelDelta < 0 && this.contentAnchoredAbove && this.offset === 0) this.releaseInspection();
190
+ else this.offset = Math.max(0, this.offset + Math.max(-20, Math.min(20, Math.trunc(event.wheelDelta))));
191
+ return { handled: true };
192
+ }
193
+ if (event.type !== "click" || event.button !== "left") return undefined;
194
+ // Keep text selection gestures and output clicks from falling through to native whole-row expansion.
195
+ if (event.shift || event.alt || event.ctrl) return { handled: true, render: false };
196
+ if (event.y === 0) { this.preserveFocusLayout = false; this.detailsFocused = false; this.open = !this.open; return { handled: true }; }
197
+ if (this.open && (this.lastMore || this.offset > 0) && event.y === this.lastHeight - 1) { this.preserveFocusLayout = true; this.offset = Math.max(0, this.offset + (this.lastMore ? 1 : -1) * Math.max(1, this.lastBodyHeight)); return { handled: true }; }
198
+ const details = this.detailsRows.get(this.offset + event.y - 1);
199
+ if (this.open && event.y >= 1 && event.y < 1 + this.lastBodyHeight && details !== undefined) {
200
+ const id = this.actions[details]?.id;
201
+ if (id) {
202
+ this.selected = details; this.anchor = details; this.detailsFocused = true; this.offset = 0;
203
+ this.detailsExpanded.has(id) ? this.detailsExpanded.delete(id) : this.detailsExpanded.add(id);
204
+ this.reset(); return { handled: true };
205
+ }
206
+ }
207
+ const selected = this.actionRows.get(this.offset + event.y - 1);
208
+ if (this.open && event.y >= 1 && event.y < 1 + this.lastBodyHeight && selected !== undefined) {
209
+ const id = this.actions[selected]?.id;
210
+ if (id) {
211
+ this.selected = selected; this.detailsFocused = false;
212
+ this.expanded.has(id) ? this.expanded.delete(id) : this.expanded.add(id);
213
+ this.offset = 0; this.reset(); return { handled: true };
214
+ }
215
+ }
216
+ return { handled: true, render: false };
217
+ }
218
+ invalidate() {}
219
+ }