@visual-z/xeno 0.1.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.
Files changed (180) hide show
  1. package/README.md +498 -0
  2. package/docs/install.md +130 -0
  3. package/package.json +43 -0
  4. package/src/apps/approve.ts +49 -0
  5. package/src/apps/cli.ts +37 -0
  6. package/src/apps/commands.ts +226 -0
  7. package/src/apps/completions.ts +28 -0
  8. package/src/apps/input.ts +297 -0
  9. package/src/apps/render.ts +207 -0
  10. package/src/apps/shell.ts +2353 -0
  11. package/src/apps/startup.ts +1302 -0
  12. package/src/core/benchmark.ts +195 -0
  13. package/src/core/boundary.ts +174 -0
  14. package/src/core/churn.ts +59 -0
  15. package/src/core/completion-review.ts +66 -0
  16. package/src/core/context-policy.ts +60 -0
  17. package/src/core/context-window.ts +54 -0
  18. package/src/core/continuation.ts +114 -0
  19. package/src/core/cycle.ts +17 -0
  20. package/src/core/data-query.ts +201 -0
  21. package/src/core/deadline.ts +39 -0
  22. package/src/core/derive.ts +489 -0
  23. package/src/core/edit-plan.ts +200 -0
  24. package/src/core/environment.ts +29 -0
  25. package/src/core/event-validation.ts +99 -0
  26. package/src/core/events.ts +486 -0
  27. package/src/core/evidence-links.ts +71 -0
  28. package/src/core/evidence.ts +89 -0
  29. package/src/core/execution.ts +12 -0
  30. package/src/core/exhaustive.ts +18 -0
  31. package/src/core/experiment.ts +40 -0
  32. package/src/core/export.ts +169 -0
  33. package/src/core/file-references.ts +35 -0
  34. package/src/core/frontmatter.ts +213 -0
  35. package/src/core/handoff.ts +397 -0
  36. package/src/core/harness-comparison.ts +90 -0
  37. package/src/core/history-query.ts +103 -0
  38. package/src/core/history.ts +100 -0
  39. package/src/core/json-event.ts +135 -0
  40. package/src/core/json-pointer.ts +46 -0
  41. package/src/core/keymap.ts +241 -0
  42. package/src/core/observation-view.ts +87 -0
  43. package/src/core/outcome.ts +45 -0
  44. package/src/core/overflow.ts +59 -0
  45. package/src/core/permissions.ts +182 -0
  46. package/src/core/policy-evaluation.ts +55 -0
  47. package/src/core/progress.ts +101 -0
  48. package/src/core/project.ts +372 -0
  49. package/src/core/prompts.ts +83 -0
  50. package/src/core/queue.ts +60 -0
  51. package/src/core/read-bounds.ts +15 -0
  52. package/src/core/recall-analysis.ts +90 -0
  53. package/src/core/recall-trial.ts +153 -0
  54. package/src/core/response-recovery.ts +46 -0
  55. package/src/core/scheduling.ts +8 -0
  56. package/src/core/settings.ts +174 -0
  57. package/src/core/skills.ts +121 -0
  58. package/src/core/stall.ts +48 -0
  59. package/src/core/text-normalize.ts +81 -0
  60. package/src/core/theme.ts +179 -0
  61. package/src/core/tokens.ts +149 -0
  62. package/src/core/tool-contract.ts +102 -0
  63. package/src/core/tool-input.ts +38 -0
  64. package/src/core/transcript.ts +60 -0
  65. package/src/core/window-trial.ts +65 -0
  66. package/src/core/work-state.ts +103 -0
  67. package/src/core/yaml.ts +282 -0
  68. package/src/index.ts +10 -0
  69. package/src/runtime/auth.ts +255 -0
  70. package/src/runtime/benchmark.ts +187 -0
  71. package/src/runtime/catalog.ts +365 -0
  72. package/src/runtime/evidence-notes.ts +33 -0
  73. package/src/runtime/exec-sessions.ts +214 -0
  74. package/src/runtime/execution-control.ts +39 -0
  75. package/src/runtime/harness-comparison.ts +356 -0
  76. package/src/runtime/history-tools.ts +108 -0
  77. package/src/runtime/loop.ts +1983 -0
  78. package/src/runtime/model.ts +614 -0
  79. package/src/runtime/models-fetch.ts +155 -0
  80. package/src/runtime/oauth-device.ts +86 -0
  81. package/src/runtime/oauth.ts +759 -0
  82. package/src/runtime/openai-responses.ts +286 -0
  83. package/src/runtime/openai.ts +675 -0
  84. package/src/runtime/provider.ts +498 -0
  85. package/src/runtime/recall-trial.ts +226 -0
  86. package/src/runtime/resume.ts +66 -0
  87. package/src/runtime/retry.ts +297 -0
  88. package/src/runtime/tools.ts +1193 -0
  89. package/src/runtime/usage.ts +225 -0
  90. package/src/runtime/window-trial.ts +140 -0
  91. package/src/runtime/working-notes.ts +30 -0
  92. package/src/store/atomic.ts +31 -0
  93. package/src/store/auth-store.ts +50 -0
  94. package/src/store/blobs.ts +142 -0
  95. package/src/store/command.ts +75 -0
  96. package/src/store/config.ts +69 -0
  97. package/src/store/context-files.ts +356 -0
  98. package/src/store/file-index.ts +81 -0
  99. package/src/store/git-branch.ts +35 -0
  100. package/src/store/history-search.ts +19 -0
  101. package/src/store/log.ts +642 -0
  102. package/src/store/models.ts +41 -0
  103. package/src/store/observations.ts +181 -0
  104. package/src/store/query-source.ts +44 -0
  105. package/src/store/recovery.ts +39 -0
  106. package/src/store/replay.ts +63 -0
  107. package/src/store/resources.ts +361 -0
  108. package/src/store/restore.ts +302 -0
  109. package/src/store/search.ts +644 -0
  110. package/src/store/serial.ts +13 -0
  111. package/src/store/sessions.ts +463 -0
  112. package/src/store/trust.ts +69 -0
  113. package/src/store/workspace.ts +256 -0
  114. package/src/store/worktree.ts +233 -0
  115. package/src/tui/ansi.ts +67 -0
  116. package/src/tui/autocomplete.ts +29 -0
  117. package/src/tui/complete.ts +87 -0
  118. package/src/tui/diff.ts +160 -0
  119. package/src/tui/editor.ts +893 -0
  120. package/src/tui/exhaustive.ts +14 -0
  121. package/src/tui/fuzzy.ts +75 -0
  122. package/src/tui/host.ts +223 -0
  123. package/src/tui/input.ts +69 -0
  124. package/src/tui/keys.ts +378 -0
  125. package/src/tui/markdown.ts +17 -0
  126. package/src/tui/render.ts +153 -0
  127. package/src/tui/select.ts +240 -0
  128. package/src/tui/settings.ts +40 -0
  129. package/src/tui/statusline.ts +162 -0
  130. package/src/tui/style.ts +205 -0
  131. package/src/tui/terminal.ts +30 -0
  132. package/src/tui/text.ts +52 -0
  133. package/src/tui/toolview.ts +386 -0
  134. package/src/tui/transcript.ts +71 -0
  135. package/src/tui/tree.ts +405 -0
  136. package/src/tui/width.ts +567 -0
  137. package/src/vendor/pi-tui/LICENSE.upstream +21 -0
  138. package/src/vendor/pi-tui/VENDOR.md +23 -0
  139. package/src/vendor/pi-tui/alt-screen-search.ts +327 -0
  140. package/src/vendor/pi-tui/autocomplete.ts +835 -0
  141. package/src/vendor/pi-tui/components/alt-screen-flash.ts +51 -0
  142. package/src/vendor/pi-tui/components/box.ts +167 -0
  143. package/src/vendor/pi-tui/components/cancellable-loader.ts +40 -0
  144. package/src/vendor/pi-tui/components/editor.ts +2470 -0
  145. package/src/vendor/pi-tui/components/h-stack.ts +44 -0
  146. package/src/vendor/pi-tui/components/image.ts +127 -0
  147. package/src/vendor/pi-tui/components/input.ts +494 -0
  148. package/src/vendor/pi-tui/components/loader.ts +101 -0
  149. package/src/vendor/pi-tui/components/markdown.ts +1015 -0
  150. package/src/vendor/pi-tui/components/mouse-region.ts +33 -0
  151. package/src/vendor/pi-tui/components/scroll-view.ts +224 -0
  152. package/src/vendor/pi-tui/components/select-list.ts +273 -0
  153. package/src/vendor/pi-tui/components/settings-list.ts +328 -0
  154. package/src/vendor/pi-tui/components/spacer.ts +28 -0
  155. package/src/vendor/pi-tui/components/stack.ts +154 -0
  156. package/src/vendor/pi-tui/components/text.ts +107 -0
  157. package/src/vendor/pi-tui/components/truncated-text.ts +65 -0
  158. package/src/vendor/pi-tui/components/v-stack.ts +33 -0
  159. package/src/vendor/pi-tui/editor-component.ts +74 -0
  160. package/src/vendor/pi-tui/fuzzy.ts +138 -0
  161. package/src/vendor/pi-tui/index.ts +156 -0
  162. package/src/vendor/pi-tui/keybindings.ts +320 -0
  163. package/src/vendor/pi-tui/keys.ts +1401 -0
  164. package/src/vendor/pi-tui/kill-ring.ts +46 -0
  165. package/src/vendor/pi-tui/latex.ts +1506 -0
  166. package/src/vendor/pi-tui/layout-node.ts +51 -0
  167. package/src/vendor/pi-tui/layout.ts +449 -0
  168. package/src/vendor/pi-tui/native-modifiers.ts +13 -0
  169. package/src/vendor/pi-tui/native-module-path.ts +31 -0
  170. package/src/vendor/pi-tui/native-platform.ts +63 -0
  171. package/src/vendor/pi-tui/stdin-buffer.ts +444 -0
  172. package/src/vendor/pi-tui/terminal-colors.ts +73 -0
  173. package/src/vendor/pi-tui/terminal-image.ts +696 -0
  174. package/src/vendor/pi-tui/terminal.ts +547 -0
  175. package/src/vendor/pi-tui/tui-alt-screen.ts +1742 -0
  176. package/src/vendor/pi-tui/tui-main-screen.ts +655 -0
  177. package/src/vendor/pi-tui/tui.ts +1456 -0
  178. package/src/vendor/pi-tui/undo-stack.ts +28 -0
  179. package/src/vendor/pi-tui/utils.ts +1345 -0
  180. package/src/vendor/pi-tui/word-navigation.ts +117 -0
@@ -0,0 +1,51 @@
1
+ import type { Component } from "./tui.ts";
2
+
3
+ export const LAYOUT_NODE = Symbol.for("@earendil-works/pi-tui/layout-node");
4
+
5
+ export interface LayoutViewport {
6
+ width: number;
7
+ height: number;
8
+ }
9
+
10
+ export interface StackLayoutEntry {
11
+ component: Component;
12
+ basis?: number | "auto";
13
+ grow?: number;
14
+ shrink?: number;
15
+ minSize?: number;
16
+ maxSize?: number;
17
+ visible?: (viewport: LayoutViewport) => boolean;
18
+ }
19
+
20
+ export interface StackLayoutNode {
21
+ type: "vstack" | "hstack";
22
+ entries: readonly StackLayoutEntry[];
23
+ gap: number;
24
+ align: "stretch" | "start" | "center" | "end";
25
+ }
26
+
27
+ export interface ScrollLayoutState {
28
+ readonly scrollTop: number;
29
+ readonly primary: boolean;
30
+ readonly overscroll: "chain" | "contain";
31
+ readonly viewportHeight: number;
32
+ getContentWidth(width: number): number;
33
+ updateLayout(contentHeight: number, viewportHeight: number, requestRender: () => void): void;
34
+ }
35
+
36
+ export interface ScrollLayoutNode {
37
+ type: "scroll";
38
+ component: Component;
39
+ state: ScrollLayoutState;
40
+ }
41
+
42
+ export type LayoutNode = StackLayoutNode | ScrollLayoutNode;
43
+
44
+ export interface LayoutComponent extends Component {
45
+ [LAYOUT_NODE](): LayoutNode;
46
+ }
47
+
48
+ export function getLayoutNode(component: Component): LayoutNode | undefined {
49
+ const candidate = component as Partial<LayoutComponent>;
50
+ return typeof candidate[LAYOUT_NODE] === "function" ? candidate[LAYOUT_NODE]() : undefined;
51
+ }
@@ -0,0 +1,449 @@
1
+ import type { ScrollView } from "./components/scroll-view.ts";
2
+ import { allocateStackSizes, visibleStackEntries } from "./components/stack.ts";
3
+ import { getLayoutNode } from "./layout-node.ts";
4
+ import { cropKittyImageLine, getKittyImageMetadata, isImageLine } from "./terminal-image.ts";
5
+ import { type Component, CURSOR_MARKER, compositeTuiLine } from "./tui.ts";
6
+ import {
7
+ extractAnsiCode,
8
+ getActiveBackgroundAnsi,
9
+ getGraphemeCellRange,
10
+ sliceByColumn,
11
+ visibleWidth,
12
+ } from "./utils.ts";
13
+
14
+ const OSC133_ZONE_PREFIX = /^(?:\x1b\]133;[ABC](?:\x07|\x1b\\))+/;
15
+
16
+ export interface LayoutRect {
17
+ x: number;
18
+ y: number;
19
+ width: number;
20
+ height: number;
21
+ }
22
+
23
+ export interface LayoutBox {
24
+ component: Component;
25
+ rect: LayoutRect;
26
+ clip: LayoutRect;
27
+ children: LayoutBox[];
28
+ parent?: LayoutBox;
29
+ lines?: readonly string[];
30
+ lineOffset?: number;
31
+ scrollView?: ScrollView;
32
+ scrollContentLines?: readonly string[];
33
+ layer: number;
34
+ }
35
+
36
+ export interface LayoutFrame {
37
+ root: LayoutBox;
38
+ width: number;
39
+ height: number;
40
+ lines: string[];
41
+ primaryScrollView?: ScrollView;
42
+ }
43
+
44
+ export interface ScrollbarGeometry {
45
+ column: number;
46
+ trackTop: number;
47
+ trackHeight: number;
48
+ thumbTop: number;
49
+ thumbHeight: number;
50
+ maxScrollTop: number;
51
+ }
52
+
53
+ interface LayoutContext {
54
+ viewport: { width: number; height: number };
55
+ renderCache: Map<Component, Map<number, string[]>>;
56
+ requestRender: () => void;
57
+ primaryScrollView: ScrollView | undefined;
58
+ }
59
+
60
+ function intersect(a: LayoutRect, b: LayoutRect): LayoutRect {
61
+ const x = Math.max(a.x, b.x);
62
+ const y = Math.max(a.y, b.y);
63
+ const right = Math.min(a.x + a.width, b.x + b.width);
64
+ const bottom = Math.min(a.y + a.height, b.y + b.height);
65
+ return { x, y, width: Math.max(0, right - x), height: Math.max(0, bottom - y) };
66
+ }
67
+
68
+ function renderCached(context: LayoutContext, component: Component, width: number): string[] {
69
+ const safeWidth = Math.max(1, Math.floor(width));
70
+ let widths = context.renderCache.get(component);
71
+ if (!widths) {
72
+ widths = new Map<number, string[]>();
73
+ context.renderCache.set(component, widths);
74
+ }
75
+ let lines = widths.get(safeWidth);
76
+ if (!lines) {
77
+ lines = component.render(safeWidth);
78
+ widths.set(safeWidth, lines);
79
+ }
80
+ return lines;
81
+ }
82
+
83
+ function measureHeight(context: LayoutContext, component: Component, width: number): number {
84
+ return renderCached(context, component, width).length;
85
+ }
86
+
87
+ function measureWidth(context: LayoutContext, component: Component, width: number): number {
88
+ return renderCached(context, component, width).reduce((max, line) => Math.max(max, visibleWidth(line)), 0);
89
+ }
90
+
91
+ function withParent(box: LayoutBox, parent: LayoutBox): LayoutBox {
92
+ box.parent = parent;
93
+ return box;
94
+ }
95
+
96
+ function translateBox(box: LayoutBox, deltaY: number): void {
97
+ box.rect.y += deltaY;
98
+ for (const child of box.children) translateBox(child, deltaY);
99
+ }
100
+
101
+ function updateClips(box: LayoutBox, parentClip: LayoutRect): void {
102
+ box.clip = intersect(parentClip, box.rect);
103
+ for (const child of box.children) updateClips(child, box.clip);
104
+ }
105
+
106
+ function layoutComponent(
107
+ context: LayoutContext,
108
+ component: Component,
109
+ x: number,
110
+ y: number,
111
+ width: number,
112
+ height: number | undefined,
113
+ clip: LayoutRect,
114
+ ): LayoutBox {
115
+ const safeWidth = Math.max(1, Math.floor(width));
116
+ const node = getLayoutNode(component);
117
+ if (!node) {
118
+ const lines = renderCached(context, component, safeWidth);
119
+ const allocatedHeight = height === undefined ? lines.length : Math.max(0, Math.floor(height));
120
+ let lineOffset = 0;
121
+ if (lines.length > allocatedHeight && allocatedHeight > 0) {
122
+ const cursorLine = lines.findIndex((line) => line.includes(CURSOR_MARKER));
123
+ if (cursorLine >= allocatedHeight) lineOffset = cursorLine - allocatedHeight + 1;
124
+ }
125
+ return {
126
+ component,
127
+ rect: { x, y, width: safeWidth, height: allocatedHeight },
128
+ clip: intersect(clip, { x, y, width: safeWidth, height: allocatedHeight }),
129
+ children: [],
130
+ lines,
131
+ lineOffset,
132
+ layer: 0,
133
+ };
134
+ }
135
+
136
+ if (node.type === "scroll") {
137
+ const previousScrollTop = node.state.scrollTop;
138
+ const contentWidth = node.state.getContentWidth(safeWidth);
139
+ const childBox = layoutComponent(
140
+ context,
141
+ node.component,
142
+ x,
143
+ y - previousScrollTop,
144
+ contentWidth,
145
+ undefined,
146
+ clip,
147
+ );
148
+ const contentHeight = childBox.rect.height;
149
+ const viewportHeight = height === undefined ? contentHeight : Math.max(0, Math.floor(height));
150
+ node.state.updateLayout(contentHeight, viewportHeight, context.requestRender);
151
+ translateBox(childBox, previousScrollTop - node.state.scrollTop);
152
+ const scrollView = node.state as ScrollView;
153
+ if (node.state.primary || !context.primaryScrollView) context.primaryScrollView = scrollView;
154
+ const rect = { x, y, width: safeWidth, height: viewportHeight };
155
+ const childClip = intersect(clip, rect);
156
+ const box: LayoutBox = {
157
+ component,
158
+ rect,
159
+ clip: childClip,
160
+ children: [childBox],
161
+ scrollView,
162
+ scrollContentLines: renderCached(context, node.component, contentWidth),
163
+ layer: 0,
164
+ };
165
+ childBox.parent = box;
166
+ updateClips(childBox, childClip);
167
+ return box;
168
+ }
169
+
170
+ const entries = visibleStackEntries(node.entries, context.viewport);
171
+ const gapTotal = Math.max(0, entries.length - 1) * node.gap;
172
+ if (node.type === "vstack") {
173
+ const intrinsicHeights = entries.map((entry) =>
174
+ typeof entry.basis === "number" ? entry.basis : measureHeight(context, entry.component, safeWidth),
175
+ );
176
+ const sizes = allocateStackSizes(entries, intrinsicHeights, height, node.gap);
177
+ const naturalHeight = sizes.reduce((sum, size) => sum + size, 0) + gapTotal;
178
+ const allocatedHeight = height === undefined ? naturalHeight : Math.max(0, Math.floor(height));
179
+ const rect = { x, y, width: safeWidth, height: allocatedHeight };
180
+ const box: LayoutBox = {
181
+ component,
182
+ rect,
183
+ clip: intersect(clip, rect),
184
+ children: [],
185
+ layer: 0,
186
+ };
187
+ let childY = y;
188
+ for (let index = 0; index < entries.length; index++) {
189
+ box.children.push(
190
+ withParent(
191
+ layoutComponent(context, entries[index]!.component, x, childY, safeWidth, sizes[index]!, box.clip),
192
+ box,
193
+ ),
194
+ );
195
+ childY += sizes[index]! + node.gap;
196
+ }
197
+ return box;
198
+ }
199
+
200
+ const intrinsicWidths = entries.map((entry) =>
201
+ typeof entry.basis === "number" ? entry.basis : measureWidth(context, entry.component, safeWidth),
202
+ );
203
+ const widths = allocateStackSizes(entries, intrinsicWidths, safeWidth, node.gap);
204
+ const intrinsicHeights = entries.map((entry, index) =>
205
+ measureHeight(context, entry.component, Math.max(1, widths[index]!)),
206
+ );
207
+ const allocatedHeight =
208
+ height === undefined
209
+ ? intrinsicHeights.reduce((max, childHeight) => Math.max(max, childHeight), 0)
210
+ : Math.max(0, height);
211
+ const rect = { x, y, width: safeWidth, height: allocatedHeight };
212
+ const box: LayoutBox = {
213
+ component,
214
+ rect,
215
+ clip: intersect(clip, rect),
216
+ children: [],
217
+ layer: 0,
218
+ };
219
+ let childX = x;
220
+ for (let index = 0; index < entries.length; index++) {
221
+ const naturalChildHeight = intrinsicHeights[index]!;
222
+ const childHeight = node.align === "stretch" ? allocatedHeight : Math.min(allocatedHeight, naturalChildHeight);
223
+ let childY = y;
224
+ if (node.align === "center") childY += Math.floor((allocatedHeight - childHeight) / 2);
225
+ else if (node.align === "end") childY += allocatedHeight - childHeight;
226
+ const childWidth = widths[index]!;
227
+ if (childWidth === 0) {
228
+ box.children.push({
229
+ component: entries[index]!.component,
230
+ rect: { x: childX, y: childY, width: 0, height: childHeight },
231
+ clip: { x: childX, y: childY, width: 0, height: 0 },
232
+ children: [],
233
+ parent: box,
234
+ layer: 0,
235
+ });
236
+ } else {
237
+ box.children.push(
238
+ withParent(
239
+ layoutComponent(context, entries[index]!.component, childX, childY, childWidth, childHeight, box.clip),
240
+ box,
241
+ ),
242
+ );
243
+ }
244
+ childX += childWidth + node.gap;
245
+ }
246
+ return box;
247
+ }
248
+
249
+ function replaceScrollbarCell(
250
+ line: string,
251
+ column: number,
252
+ totalWidth: number,
253
+ replacement: string,
254
+ preserveTargetBackground: boolean,
255
+ ): string {
256
+ if (isImageLine(line)) return line;
257
+
258
+ const graphemeRange = getGraphemeCellRange(line, column);
259
+ const start = graphemeRange?.start ?? column;
260
+ const end = graphemeRange?.end ?? column + 1;
261
+ const before = sliceByColumn(line, 0, start, true);
262
+ const target = sliceByColumn(line, start, end - start, true);
263
+ const after = sliceByColumn(line, end, Math.max(0, totalWidth - end), true);
264
+
265
+ let targetPrefix = "";
266
+ let targetIndex = 0;
267
+ while (targetIndex < target.length) {
268
+ const ansi = extractAnsiCode(target, targetIndex);
269
+ if (!ansi) break;
270
+ targetPrefix += ansi.code;
271
+ targetIndex += ansi.length;
272
+ }
273
+ const beforePadding = " ".repeat(Math.max(0, start - visibleWidth(before)));
274
+ const cellPaddingBefore = " ".repeat(Math.max(0, column - start));
275
+ const cellPaddingAfter = " ".repeat(Math.max(0, end - column - 1));
276
+ const targetStyle = `\x1b[0m\x1b]8;;\x07${preserveTargetBackground ? getActiveBackgroundAnsi(targetPrefix) : ""}`;
277
+ return `${before}${beforePadding}${targetStyle}${cellPaddingBefore}${replacement}${cellPaddingAfter}${after}`;
278
+ }
279
+
280
+ export function getScrollbarGeometry(box: LayoutBox, includeHiddenAuto = false): ScrollbarGeometry | undefined {
281
+ if (!box.scrollView || box.rect.width <= 0 || box.rect.height <= 0) return undefined;
282
+
283
+ const contentHeight = box.children[0]?.rect.height ?? box.scrollContentLines?.length ?? 0;
284
+ const trackHeight = box.rect.height;
285
+ const canRevealHiddenAuto = includeHiddenAuto && box.scrollView.scrollbar === "auto" && contentHeight > trackHeight;
286
+ if (!box.scrollView.isScrollbarVisible && !canRevealHiddenAuto) return undefined;
287
+
288
+ const minThumbHeight = Math.min(2, trackHeight);
289
+ const thumbHeight = Math.max(
290
+ minThumbHeight,
291
+ Math.min(trackHeight, Math.round((trackHeight * trackHeight) / contentHeight)),
292
+ );
293
+ const maxScrollTop = Math.max(0, contentHeight - trackHeight);
294
+ const maxThumbTop = trackHeight - thumbHeight;
295
+ const thumbOffset = maxScrollTop === 0 ? 0 : Math.round((box.scrollView.scrollTop / maxScrollTop) * maxThumbTop);
296
+ const column = box.rect.x + box.rect.width - 1;
297
+ if (column < box.clip.x || column >= box.clip.x + box.clip.width) return undefined;
298
+
299
+ return {
300
+ column,
301
+ trackTop: box.rect.y,
302
+ trackHeight,
303
+ thumbTop: box.rect.y + thumbOffset,
304
+ thumbHeight,
305
+ maxScrollTop,
306
+ };
307
+ }
308
+
309
+ function paintScrollbar(box: LayoutBox, screen: string[], totalWidth: number): void {
310
+ const geometry = getScrollbarGeometry(box);
311
+ if (!geometry || !box.scrollView) return;
312
+
313
+ for (let offset = 0; offset < geometry.trackHeight; offset++) {
314
+ const row = geometry.trackTop + offset;
315
+ if (row < box.clip.y || row >= box.clip.y + box.clip.height || row < 0 || row >= screen.length) continue;
316
+ const isThumb = row >= geometry.thumbTop && row < geometry.thumbTop + geometry.thumbHeight;
317
+ const replacement = isThumb
318
+ ? box.scrollView.scrollbarThumbStyle(box.scrollView.isScrollbarActive ? "█" : "┃")
319
+ : box.scrollView.scrollbarTrackStyle("│");
320
+ screen[row] = replaceScrollbarCell(
321
+ screen[row] ?? "",
322
+ geometry.column,
323
+ totalWidth,
324
+ replacement,
325
+ box.scrollView.scrollbar !== "always",
326
+ );
327
+ }
328
+ }
329
+
330
+ function paintBox(box: LayoutBox, screen: string[], totalWidth: number): void {
331
+ if (box.lines) {
332
+ const offset = box.lineOffset ?? 0;
333
+ const firstRow = Math.max(box.rect.y, box.clip.y, 0);
334
+ const lastRow = Math.min(box.rect.y + box.rect.height, box.clip.y + box.clip.height, screen.length);
335
+ for (let row = firstRow; row < lastRow; row++) {
336
+ const sourceLine = box.lines[offset + row - box.rect.y];
337
+ if (sourceLine === undefined) continue;
338
+ let line = sourceLine.replace(OSC133_ZONE_PREFIX, "");
339
+ const imageMetadata = getKittyImageMetadata(line);
340
+ if (imageMetadata) {
341
+ const clipBottom = Math.min(screen.length, box.clip.y + box.clip.height);
342
+ const visibleRows = Math.min(imageMetadata.rows, clipBottom - row);
343
+ if (visibleRows < imageMetadata.rows) line = cropKittyImageLine(line, 0, visibleRows);
344
+ }
345
+ // Fast path: a full-width box painting onto an untouched row can use the
346
+ // source line reference directly. Compositing here would rebuild the row
347
+ // string through ANSI/grapheme segmentation every frame; padding is
348
+ // unnecessary because rows are written with erase-line and the final
349
+ // width clamp still truncates over-wide lines.
350
+ if (box.rect.x === 0 && box.rect.width >= totalWidth && (isImageLine(line) || !screen[row])) {
351
+ screen[row] = line;
352
+ } else {
353
+ screen[row] = compositeTuiLine(screen[row] ?? "", line, box.rect.x, box.rect.width, totalWidth);
354
+ }
355
+ }
356
+ }
357
+ for (const child of box.children) paintBox(child, screen, totalWidth);
358
+
359
+ if (box.scrollView && box.scrollContentLines && box.scrollView.scrollTop > 0 && box.rect.height > 0) {
360
+ for (let imageRow = box.scrollView.scrollTop - 1; imageRow >= 0; imageRow--) {
361
+ const imageLine = box.scrollContentLines[imageRow] ?? "";
362
+ const metadata = getKittyImageMetadata(imageLine);
363
+ if (metadata) {
364
+ const hiddenRows = box.scrollView.scrollTop - imageRow;
365
+ if (hiddenRows < metadata.rows) {
366
+ const visibleRows = Math.min(box.rect.height, metadata.rows - hiddenRows);
367
+ const cropped = cropKittyImageLine(imageLine, hiddenRows, visibleRows);
368
+ if (box.rect.x === 0 && box.rect.width >= totalWidth) screen[box.rect.y] = cropped;
369
+ }
370
+ break;
371
+ }
372
+ if (imageLine !== "") break;
373
+ }
374
+ }
375
+
376
+ paintScrollbar(box, screen, totalWidth);
377
+ }
378
+
379
+ export function renderLayoutFrame(
380
+ root: Component,
381
+ width: number,
382
+ height: number,
383
+ requestRender: () => void,
384
+ ): LayoutFrame {
385
+ const safeWidth = Math.max(1, Math.floor(width));
386
+ const safeHeight = Math.max(1, Math.floor(height));
387
+ const context: LayoutContext = {
388
+ viewport: { width: safeWidth, height: safeHeight },
389
+ renderCache: new Map(),
390
+ requestRender,
391
+ primaryScrollView: undefined,
392
+ };
393
+ const rootBox = layoutComponent(context, root, 0, 0, safeWidth, safeHeight, {
394
+ x: 0,
395
+ y: 0,
396
+ width: safeWidth,
397
+ height: safeHeight,
398
+ });
399
+ const lines = Array.from({ length: safeHeight }, () => "");
400
+ paintBox(rootBox, lines, safeWidth);
401
+ return {
402
+ root: rootBox,
403
+ width: safeWidth,
404
+ height: safeHeight,
405
+ lines,
406
+ ...(context.primaryScrollView === undefined ? {} : { primaryScrollView: context.primaryScrollView }),
407
+ };
408
+ }
409
+
410
+ function containsPoint(rect: LayoutRect, x: number, y: number): boolean {
411
+ return x >= rect.x && x < rect.x + rect.width && y >= rect.y && y < rect.y + rect.height;
412
+ }
413
+
414
+ /** Return the visual hit path from the deepest component to the layout root. */
415
+ export function getLayoutBoxesAt(frame: LayoutFrame, x: number, y: number): LayoutBox[] {
416
+ const result: Array<{ box: LayoutBox; depth: number }> = [];
417
+ const visit = (box: LayoutBox, depth: number): void => {
418
+ if (!containsPoint(box.clip, x, y)) return;
419
+ result.push({ box, depth });
420
+ for (const child of box.children) visit(child, depth + 1);
421
+ };
422
+ visit(frame.root, 0);
423
+ result.sort((a, b) => b.box.layer - a.box.layer || b.depth - a.depth);
424
+ return result.map(({ box }) => box);
425
+ }
426
+
427
+ export function getScrollViewBox(frame: LayoutFrame, scrollView: ScrollView): LayoutBox | undefined {
428
+ const visit = (box: LayoutBox): LayoutBox | undefined => {
429
+ if (box.scrollView === scrollView) return box;
430
+ for (const child of box.children) {
431
+ const match = visit(child);
432
+ if (match) return match;
433
+ }
434
+ return undefined;
435
+ };
436
+ return visit(frame.root);
437
+ }
438
+
439
+ export function getScrollViewsAt(frame: LayoutFrame, x: number, y: number): ScrollView[] {
440
+ const result: Array<{ scrollView: ScrollView; depth: number }> = [];
441
+ const visit = (box: LayoutBox, depth: number): void => {
442
+ if (!containsPoint(box.clip, x, y)) return;
443
+ if (box.scrollView && containsPoint(box.rect, x, y)) result.push({ scrollView: box.scrollView, depth });
444
+ for (const child of box.children) visit(child, depth + 1);
445
+ };
446
+ visit(frame.root, 0);
447
+ result.sort((a, b) => b.depth - a.depth);
448
+ return result.map((entry) => entry.scrollView);
449
+ }
@@ -0,0 +1,13 @@
1
+ import { getNativePlatformHelper, type ModifierKey } from "./native-platform.ts";
2
+
3
+ export type { ModifierKey } from "./native-platform.ts";
4
+
5
+ export function isNativeModifierPressed(key: ModifierKey): boolean {
6
+ const helper = getNativePlatformHelper();
7
+ if (!helper?.isModifierPressed) return false;
8
+ try {
9
+ return helper.isModifierPressed(key) === true;
10
+ } catch {
11
+ return false;
12
+ }
13
+ }
@@ -0,0 +1,31 @@
1
+ import { createRequire } from "node:module";
2
+ import { dirname, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ const moduleRequire = createRequire(import.meta.url);
6
+ const TUI_PACKAGE_NAME = "@earendil-works/pi-tui";
7
+
8
+ export interface NativeModuleCandidateOptions {
9
+ moduleUrl?: string;
10
+ execPath?: string;
11
+ resolvePackage?: (specifier: string) => string;
12
+ }
13
+
14
+ export function getNativeModuleCandidates(nativePath: string, options: NativeModuleCandidateOptions = {}): string[] {
15
+ const moduleDir = dirname(fileURLToPath(options.moduleUrl ?? import.meta.url));
16
+ const candidates: string[] = [];
17
+
18
+ try {
19
+ const packageEntry = (options.resolvePackage ?? moduleRequire.resolve)(TUI_PACKAGE_NAME);
20
+ candidates.push(join(dirname(packageEntry), "..", nativePath));
21
+ } catch {
22
+ // Standalone binaries do not have an installed TUI package.
23
+ }
24
+
25
+ candidates.push(
26
+ join(moduleDir, "..", nativePath),
27
+ join(moduleDir, nativePath),
28
+ join(dirname(options.execPath ?? process.execPath), nativePath),
29
+ );
30
+ return Array.from(new Set(candidates));
31
+ }
@@ -0,0 +1,63 @@
1
+ import { createRequire } from "node:module";
2
+ import * as path from "node:path";
3
+ import { getNativeModuleCandidates } from "./native-module-path.ts";
4
+
5
+ const cjsRequire = createRequire(import.meta.url);
6
+
7
+ export type ModifierKey = "shift" | "command" | "control" | "option";
8
+
9
+ export interface NativeClipboard {
10
+ /** Undefined means unavailable, null means no text; transfer failures reject. */
11
+ getText(): Promise<string | null | undefined>;
12
+ /** Undefined means unavailable, null means no image; transfer failures reject. */
13
+ getImage(): Promise<Uint8Array | null | undefined>;
14
+ /** Linux uses command-line tools to retain clipboard ownership instead. */
15
+ setText?(text: string): Promise<void>;
16
+ }
17
+
18
+ type NativePlatformHelper = NativeClipboard & {
19
+ enableVirtualTerminalInput?: () => boolean;
20
+ isModifierPressed?: (name: ModifierKey) => boolean;
21
+ };
22
+
23
+ // Cache module loading, not display availability: a disconnected display can recover.
24
+ const helpers = new Map<string, NativePlatformHelper | undefined>();
25
+
26
+ function loadNativePlatformHelper(platform: string, suffix = ""): NativePlatformHelper | undefined {
27
+ const arch = process.arch;
28
+ if (arch !== "x64" && arch !== "arm64") return undefined;
29
+ const nativePath = path.join(
30
+ "native",
31
+ platform,
32
+ "prebuilds",
33
+ `${platform}-${arch}`,
34
+ `${platform}-platform${suffix}.node`,
35
+ );
36
+ if (helpers.has(nativePath)) return helpers.get(nativePath);
37
+
38
+ for (const modulePath of getNativeModuleCandidates(nativePath)) {
39
+ try {
40
+ const helper = cjsRequire(modulePath) as Partial<NativePlatformHelper> | null;
41
+ if (typeof helper?.getText === "function" && typeof helper.getImage === "function") {
42
+ helpers.set(nativePath, helper as NativePlatformHelper);
43
+ return helper as NativePlatformHelper;
44
+ }
45
+ } catch {
46
+ // Try the next possible packaging location.
47
+ }
48
+ }
49
+ helpers.set(nativePath, undefined);
50
+ return undefined;
51
+ }
52
+
53
+ export function getNativePlatformHelper(): NativePlatformHelper | undefined {
54
+ if (process.platform !== "darwin" && process.platform !== "win32") return undefined;
55
+ return loadNativePlatformHelper(process.platform);
56
+ }
57
+
58
+ /** Load a clipboard helper without opening the display until a read is requested. */
59
+ export function getNativeClipboard(): NativeClipboard | undefined {
60
+ if (process.platform !== "linux") return getNativePlatformHelper();
61
+ if (!process.env.DISPLAY) return undefined;
62
+ return loadNativePlatformHelper("linux", "-x11");
63
+ }