@causyne/agent 1.20.35 → 1.20.38

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.
@@ -0,0 +1,320 @@
1
+ // Modal layer: the thing that makes this an application rather than a log.
2
+ //
3
+ // opencode keeps a dialog STACK and pushes a "modal" entry onto a keybinding
4
+ // mode stack while any dialog is open (ui/dialog.tsx, keymap.tsx). That detail is
5
+ // the whole design. Without a mode stack, a dialog is just a box that happens to
6
+ // be painted on top, and keys leak through to the prompt underneath — you type
7
+ // to filter the palette and your text also lands in the message box. With one,
8
+ // the top layer owns the keyboard and pops cleanly.
9
+ //
10
+ // The backdrop is dimmed rather than blacked out. opencode paints an alpha fill
11
+ // over the screen; a cell grid cannot blend, but it can add the SGR dim
12
+ // attribute to every cell behind the panel, which reads the same way and keeps
13
+ // the conversation legible underneath.
14
+ //
15
+ // Pure Node over ./screen.js, ./theme.js and ./text.js. No dependency, no shell.
16
+
17
+ // @ts-expect-error theme.js has no TypeScript declaration yet.
18
+ import { paint, bold, dim, fill, GLYPHS, HAS_COLOR, HAS_UNICODE } from './theme.js';
19
+ import { truncate, visibleWidth } from './text.js';
20
+
21
+ export interface OverlayRect {
22
+ x: number;
23
+ y: number;
24
+ width: number;
25
+ height: number;
26
+ }
27
+
28
+ export interface OverlayScreenCell {
29
+ ch?: string;
30
+ style?: string;
31
+ }
32
+
33
+ export interface OverlayScreen {
34
+ width: number;
35
+ height: number;
36
+ cells: OverlayScreenCell[];
37
+ fill(x: number, y: number, width: number, height: number, style: string): void;
38
+ put(x: number, y: number, text: string, maxWidth: number): void;
39
+ setCursor(x: number, y: number): void;
40
+ }
41
+
42
+ export interface LayerOptions {
43
+ title?: string | undefined;
44
+ footer?: string | undefined;
45
+ width?: number | undefined;
46
+ height?: number | undefined;
47
+ onClose?: (() => void) | null | undefined;
48
+ onSelect?: ((item: unknown) => void) | null | undefined;
49
+ }
50
+
51
+ export interface PanelRenderableLayer {
52
+ title?: string | undefined;
53
+ footer?: string | undefined;
54
+ borderRole?: string | undefined;
55
+ surfaceRole?: string | undefined;
56
+ preferredHeight(): number;
57
+ body(width: number, height: number): string[];
58
+ }
59
+
60
+ // Box-drawing set, degrading with the rest of the glyphs.
61
+ export const FRAME: { tl: string; tr: string; bl: string; br: string; h: string; v: string } = HAS_UNICODE
62
+ ? { tl: '╭', tr: '╮', bl: '╰', br: '╯', h: '─', v: '│' }
63
+ : { tl: '+', tr: '+', bl: '+', br: '+', h: '-', v: '|' };
64
+
65
+ /**
66
+ * Add the SGR dim attribute to every cell in a region.
67
+ *
68
+ * Operates on the grid AFTER the page beneath has been painted, which is why the
69
+ * conversation stays readable behind a dialog instead of being erased.
70
+ */
71
+ export function dimBackdrop(screen: OverlayScreen, viewport: OverlayRect, hole?: OverlayRect | null): void {
72
+ if (!HAS_COLOR) return;
73
+ for (let row = viewport.y; row < viewport.y + viewport.height; row++) {
74
+ if (row < 0 || row >= screen.height) continue;
75
+ for (let column = viewport.x; column < viewport.x + viewport.width; column++) {
76
+ if (column < 0 || column >= screen.width) continue;
77
+ if (hole
78
+ && row >= hole.y && row < hole.y + hole.height
79
+ && column >= hole.x && column < hole.x + hole.width) continue;
80
+
81
+ const cell = screen.cells[row * screen.width + column];
82
+ if (!cell) continue;
83
+ if (cell.ch === ' ' && !cell.style) continue;
84
+ const parts = cell.style ? cell.style.split(';') : [];
85
+ if (!parts.includes('2')) parts.push('2');
86
+ cell.style = parts.join(';');
87
+ }
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Draw a framed panel and return the rect available INSIDE it.
93
+ *
94
+ * The frame is painted onto its own blank ground first, so a dialog never shows
95
+ * the transcript bleeding through the gaps between its rows.
96
+ */
97
+ export function drawPanel(
98
+ screen: OverlayScreen,
99
+ rect: OverlayRect,
100
+ { title = '', footer = '' }: { title?: string; footer?: string } = {}
101
+ ): OverlayRect {
102
+ if (rect.width < 4 || rect.height < 3) return { x: rect.x, y: rect.y, width: 0, height: 0 };
103
+
104
+ screen.fill(rect.x, rect.y, rect.width, rect.height, '');
105
+
106
+ const inner = rect.width - 2;
107
+ const line = FRAME.h.repeat(inner);
108
+
109
+ // Title sits in the top border, the way every terminal dialog has since curses.
110
+ let top = FRAME.tl + line + FRAME.tr;
111
+ if (title) {
112
+ const label = ` ${truncate(title, Math.max(0, inner - 4), GLYPHS.ellipsis)} `;
113
+ const width = visibleWidth(label);
114
+ top = FRAME.tl + FRAME.h + label + FRAME.h.repeat(Math.max(0, inner - width - 1)) + FRAME.tr;
115
+ }
116
+
117
+ let bottom = FRAME.bl + line + FRAME.br;
118
+ if (footer) {
119
+ const label = ` ${truncate(footer, Math.max(0, inner - 4), GLYPHS.ellipsis)} `;
120
+ const width = visibleWidth(label);
121
+ bottom = FRAME.bl + FRAME.h.repeat(Math.max(0, inner - width - 1)) + label + FRAME.h + FRAME.br;
122
+ }
123
+
124
+ screen.put(rect.x, rect.y, paint('rule', top), rect.width);
125
+ screen.put(rect.x, rect.y + rect.height - 1, paint('rule', bottom), rect.width);
126
+
127
+ for (let row = rect.y + 1; row < rect.y + rect.height - 1; row++) {
128
+ screen.put(rect.x, row, paint('rule', FRAME.v), 1);
129
+ screen.put(rect.x + rect.width - 1, row, paint('rule', FRAME.v), 1);
130
+ }
131
+
132
+ return {
133
+ x: rect.x + 2,
134
+ y: rect.y + 1,
135
+ width: Math.max(0, rect.width - 4),
136
+ height: Math.max(0, rect.height - 2)
137
+ };
138
+ }
139
+
140
+ /**
141
+ * A layer on the stack.
142
+ *
143
+ * Subclasses implement `body(width, height)` returning styled rows, and may
144
+ * implement `key(event)` returning 'close' | 'handled' | null. Returning null
145
+ * means "not mine" — but the stack still does NOT forward it downward, because a
146
+ * modal that leaks keys is the bug this class exists to prevent. null only means
147
+ * the layer chose to ignore it.
148
+ */
149
+ export class Layer {
150
+ title: string;
151
+ footer: string;
152
+ width: number;
153
+ height: number;
154
+ borderRole?: string | undefined;
155
+ surfaceRole?: string | undefined;
156
+ onClose: (() => void) | null;
157
+ onSelect: ((item: unknown) => void) | null;
158
+ cursor: { x: number; y: number } | null;
159
+
160
+ constructor(options: LayerOptions = {}) {
161
+ this.title = options.title || '';
162
+ this.footer = options.footer || '';
163
+ this.width = options.width || 64;
164
+ this.height = options.height || 16;
165
+ this.onClose = options.onClose || null;
166
+ this.onSelect = options.onSelect || null;
167
+ this.cursor = null;
168
+ }
169
+
170
+ /**
171
+ * Rows this layer would like, given its content.
172
+ *
173
+ * A dialog padded out to a fixed height with blank rows looks unfinished —
174
+ * it is the single clearest tell between a prototype and a shipped tool. Every
175
+ * layer reports what it actually needs and the frame shrinks to it.
176
+ */
177
+ preferredHeight(): number { return this.height; }
178
+
179
+ /** Panel rect, centred and clamped to the viewport. */
180
+ rect(viewport: OverlayRect): OverlayRect {
181
+ const width = Math.min(this.width, Math.max(20, viewport.width - 4));
182
+ const wanted = Math.max(5, Math.min(this.preferredHeight(), this.height));
183
+ const height = Math.min(wanted, Math.max(5, viewport.height - 4));
184
+ return {
185
+ x: viewport.x + Math.floor((viewport.width - width) / 2),
186
+ // A quarter down reads as centred; dead centre sits visually too low.
187
+ y: viewport.y + Math.max(1, Math.floor((viewport.height - height) / 4)),
188
+ width,
189
+ height
190
+ };
191
+ }
192
+
193
+ body(_width?: number, _height?: number): string[] { return []; }
194
+ key(_event?: any): string | null { return null; }
195
+ }
196
+
197
+ export class OverlayStack {
198
+ layers: Layer[];
199
+
200
+ constructor() {
201
+ this.layers = [];
202
+ }
203
+
204
+ get top(): Layer | null { return this.layers.length > 0 ? (this.layers[this.layers.length - 1] ?? null) : null; }
205
+ get active(): boolean { return this.layers.length > 0; }
206
+ get depth(): number { return this.layers.length; }
207
+
208
+ push<T extends Layer>(layer: T): T {
209
+ this.layers.push(layer);
210
+ return layer;
211
+ }
212
+
213
+ /** Pop exactly one layer. Nested dialogs unwind one esc at a time. */
214
+ pop(): Layer | null {
215
+ const layer = this.layers.pop();
216
+ if (layer && layer.onClose) layer.onClose();
217
+ return layer || null;
218
+ }
219
+
220
+ clear(): void {
221
+ while (this.layers.length > 0) this.pop();
222
+ }
223
+
224
+ /**
225
+ * Route a key.
226
+ *
227
+ * Returns true when a modal consumed it — which is ALWAYS, while one is open.
228
+ * The caller uses that to decide whether the prompt beneath ever sees the key,
229
+ * and the answer while a dialog is up must be no.
230
+ */
231
+ handleKey(key: unknown): boolean {
232
+ const layer = this.top;
233
+ if (!layer) return false;
234
+
235
+ const result = layer.key(key);
236
+ if (result === 'close') this.pop();
237
+ return true;
238
+ }
239
+
240
+ /** Paint every layer bottom to top, dimming what is behind the topmost. */
241
+ paint(screen: OverlayScreen, viewport: OverlayRect): void {
242
+ for (let index = 0; index < this.layers.length; index++) {
243
+ const layer = this.layers[index];
244
+ if (!layer) continue;
245
+ const rect = layer.rect(viewport);
246
+
247
+ dimBackdrop(screen, viewport, rect);
248
+ const inner = drawPanel(screen, rect, { title: layer.title, footer: layer.footer });
249
+ if (inner.height <= 0) continue;
250
+
251
+ const rows = layer.body(inner.width, inner.height) || [];
252
+ for (let row = 0; row < Math.min(rows.length, inner.height); row++) {
253
+ screen.put(inner.x, inner.y + row, rows[row] ?? '', inner.width);
254
+ }
255
+
256
+ // Only the topmost layer may own the caret.
257
+ if (index === this.layers.length - 1 && layer.cursor) {
258
+ screen.setCursor(inner.x + layer.cursor.x, inner.y + layer.cursor.y);
259
+ }
260
+ }
261
+ }
262
+ }
263
+
264
+ /**
265
+ * A layer as framed rows, for a normal-buffer live region.
266
+ *
267
+ * drawPanel() paints absolutely into a cell grid, which only exists inside an
268
+ * alternate-buffer app — and chat does not have one any more (UX-01). This is the
269
+ * same panel expressed as styled `string[]`, which is all a live region can
270
+ * append. It is the shape openclaw draws its pickers in: extra lines in the
271
+ * composed frame, not a floating window.
272
+ *
273
+ * Every row is wrapped with `fill('surface', ...)` so that when a panel (such as
274
+ * Message Actions) is overlaid onto the transcript viewport, its interior has an
275
+ * opaque surface background and legible header/footer typography.
276
+ */
277
+ export function panelRows(layer: PanelRenderableLayer, width: number, maxHeight: number): string[] {
278
+ const frameWidth = Math.max(8, width);
279
+ const inner = frameWidth - 4; // two borders, one pad each side
280
+ const bodyHeight = Math.max(1, Math.min(layer.preferredHeight(), maxHeight) - 2);
281
+ const body = layer.body(inner, bodyHeight);
282
+
283
+ const borderRole = layer.borderRole || 'rule';
284
+ const surfaceRole = layer.surfaceRole || 'surface';
285
+ const line = FRAME.h.repeat(frameWidth - 2);
286
+
287
+ let top = paint(borderRole, FRAME.tl + line + FRAME.tr);
288
+ if (layer.title) {
289
+ const label = ` ${truncate(layer.title, Math.max(0, inner - 4), GLYPHS.ellipsis)} `;
290
+ const trail = FRAME.h.repeat(Math.max(0, frameWidth - 3 - visibleWidth(label)));
291
+ top = paint(borderRole, FRAME.tl + FRAME.h) + bold(paint('ink', label)) + paint(borderRole, trail + FRAME.tr);
292
+ }
293
+
294
+ let bottom = paint(borderRole, FRAME.bl + line + FRAME.br);
295
+ if (layer.footer) {
296
+ const label = ` ${truncate(layer.footer, Math.max(0, inner - 4), GLYPHS.ellipsis)} `;
297
+ const lead = FRAME.h.repeat(Math.max(0, frameWidth - 3 - visibleWidth(label)));
298
+ bottom = paint(borderRole, FRAME.bl + lead) + dim(label) + paint(borderRole, FRAME.h + FRAME.br);
299
+ }
300
+
301
+ const edge = paint(borderRole, FRAME.v);
302
+ const rows: string[] = [fill(surfaceRole, top)];
303
+ for (let index = 0; index < bodyHeight; index++) {
304
+ const content = truncate(body[index] || '', inner, GLYPHS.ellipsis);
305
+ const pad = ' '.repeat(Math.max(0, inner - visibleWidth(content)));
306
+ rows.push(fill(surfaceRole, `${edge} ${content}${pad} ${edge}`));
307
+ }
308
+ rows.push(fill(surfaceRole, bottom));
309
+ return rows;
310
+ }
311
+
312
+ /** Shared footer text so every dialog teaches the same keys. */
313
+ export function dialogHint(extra = ''): string {
314
+ const parts = ['↑↓ move', 'enter select', 'esc close'];
315
+ if (extra) parts.push(extra);
316
+ return parts.join(` ${GLYPHS.dot} `);
317
+ }
318
+
319
+ export { bold, dim };
320
+
package/src/tui/prompt.js CHANGED
@@ -1473,7 +1473,10 @@ export class ChatPrompt {
1473
1473
  drawLayer(width) {
1474
1474
  if (this.layer.messageActions && this.surface?.presentMessageActions) {
1475
1475
  const availableWidth = sessionMainWidth(this.surface.width);
1476
- const panelWidth = dialogWidth(Math.max(8, availableWidth - 3), this.layer.width || 68);
1476
+ const requestedWidth = typeof this.layer.preferredWidth === 'function'
1477
+ ? this.layer.preferredWidth(Math.max(8, availableWidth - 3))
1478
+ : (this.layer.width || 64);
1479
+ const panelWidth = dialogWidth(Math.max(8, availableWidth - 3), requestedWidth);
1477
1480
  const panel = panelRows(this.layer, panelWidth, this.layer.preferredHeight());
1478
1481
  this.surface.presentMessageActions(panel, this.layer);
1479
1482
  return;
@@ -448,7 +448,10 @@ export class StageSurface {
448
448
  const overlay = this.messageActionOverlay;
449
449
  const placed = placeMessageActionPanel(rows, overlay.panel, contentWidth,
450
450
  (this.out.rows || height) - height, overlay.layer.anchorY,
451
- overlay.layer.items.length);
451
+ overlay.layer.items.length, {
452
+ messageBounds: overlay.layer.messageBounds,
453
+ dimBackdrop: true
454
+ });
452
455
  rows = placed.rows;
453
456
  overlay.layer.actionLayout = placed.layout;
454
457
  }