@bypane/cli 0.1.0 → 0.2.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.
@@ -0,0 +1,744 @@
1
+ import { CheckboxGroup, createCanvas, createChart, drawBox, drawCanvas, drawLines, drawMeter, drawSparkline, Input, inset, Listbox, split, Table, text, truncate, widthOf, wrap, } from "tui";
2
+ import { evalExpr, evalNumber, evalSeriesValues, interpolateText } from "./expr.js";
3
+ import { Button, Segmented, Slider, Timeline, Toggle } from "./primitives.js";
4
+ const SERIES_COLORS = ["#7aa2ff", "#5fd787", "#f5a623", "#ff7a90", "#bb9af7", "#7dcfff"];
5
+ function parseHexColor(hex, fallback = 0x7aa2ff) {
6
+ if (!hex)
7
+ return fallback;
8
+ const clean = hex.startsWith("#") ? hex.slice(1) : hex;
9
+ if (clean.length === 6) {
10
+ const parsed = Number.parseInt(clean, 16);
11
+ if (Number.isFinite(parsed))
12
+ return parsed;
13
+ }
14
+ return fallback;
15
+ }
16
+ function toneToStyle(frame, rawTone, scope, bold) {
17
+ const tone = rawTone ? interpolateText(rawTone, scope).trim().toLowerCase() : "default";
18
+ let base;
19
+ switch (tone) {
20
+ case "heading":
21
+ base = frame.theme.heading;
22
+ break;
23
+ case "muted":
24
+ base = frame.theme.muted;
25
+ break;
26
+ case "primary":
27
+ base = frame.theme.primary;
28
+ break;
29
+ case "success":
30
+ base = frame.theme.success;
31
+ break;
32
+ case "warn":
33
+ case "warning":
34
+ base = { fg: "#f5a623", bold: true };
35
+ break;
36
+ case "danger":
37
+ case "error":
38
+ case "destructive":
39
+ base = frame.theme.destructive;
40
+ break;
41
+ case "code":
42
+ base = frame.theme.code;
43
+ break;
44
+ default:
45
+ if (tone.startsWith("#") && tone.length === 7) {
46
+ base = { fg: tone };
47
+ }
48
+ else {
49
+ base = { fg: "#e6edf7" };
50
+ }
51
+ break;
52
+ }
53
+ if (bold)
54
+ return { ...base, bold: true };
55
+ return base;
56
+ }
57
+ function toneToHex(rawTone, scope) {
58
+ const tone = rawTone ? interpolateText(rawTone, scope).trim().toLowerCase() : "primary";
59
+ switch (tone) {
60
+ case "success":
61
+ return "#5fd787";
62
+ case "warn":
63
+ case "warning":
64
+ return "#f5a623";
65
+ case "danger":
66
+ case "error":
67
+ case "destructive":
68
+ return "#ff7a90";
69
+ case "code":
70
+ return "#7dcfff";
71
+ case "muted":
72
+ return "#8b93a6";
73
+ default:
74
+ return tone.startsWith("#") && tone.length === 7 ? tone : "#7aa2ff";
75
+ }
76
+ }
77
+ function isNodeVisible(node, scope) {
78
+ if (node.visible === undefined)
79
+ return true;
80
+ if (typeof node.visible === "boolean")
81
+ return node.visible;
82
+ const trimmed = node.visible.trim();
83
+ if (!trimmed)
84
+ return true;
85
+ const unwrapped = trimmed.startsWith("{{") && trimmed.endsWith("}}")
86
+ ? trimmed.slice(2, -2).trim()
87
+ : trimmed;
88
+ return Boolean(evalExpr(unwrapped, scope));
89
+ }
90
+ function normalizeOptions(options) {
91
+ return options.map((o) => (typeof o === "string" ? { value: o, label: o } : o));
92
+ }
93
+ /**
94
+ * Estimates a clean intrinsic row height for any node when placed inside a vertical `col`.
95
+ * Returns negative flex weights directly when the author requested flexible expansion.
96
+ */
97
+ function intrinsicRows(node, scope) {
98
+ if (node.size !== undefined)
99
+ return node.size;
100
+ switch (node.type) {
101
+ case "divider":
102
+ case "badge":
103
+ case "toggle":
104
+ case "button":
105
+ return 1;
106
+ case "text": {
107
+ const rendered = interpolateText(node.content, scope);
108
+ const explicit = rendered.split(/\r?\n/).length;
109
+ return Math.max(explicit, rendered.length > 56 ? 2 : 1);
110
+ }
111
+ case "stat":
112
+ return 2;
113
+ case "input":
114
+ return node.label ? 2 : 1;
115
+ case "slider":
116
+ return 2;
117
+ case "segmented":
118
+ return node.label ? 2 : 1;
119
+ case "meter":
120
+ return node.label ? 2 : 1;
121
+ case "sparkline":
122
+ return node.rows ?? (node.label ? 3 : 2);
123
+ case "markdown":
124
+ return node.rows ?? Math.max(2, Math.min(8, interpolateText(node.content, scope).split(/\r?\n/).length + 1));
125
+ case "chart":
126
+ return node.rows ?? -1;
127
+ case "canvas":
128
+ return node.rows ?? -1;
129
+ case "table":
130
+ return node.rowsHeight ?? Math.max(4, Math.min(8, node.rows.length + 2));
131
+ case "select":
132
+ case "checkboxes":
133
+ return node.rows ?? Math.max(3, Math.min(7, node.options.length + (node.label ? 1 : 0)));
134
+ case "timeline": {
135
+ if (node.rows !== undefined)
136
+ return node.rows;
137
+ const base = (node.mode ?? "steps") === "gantt"
138
+ ? node.items.length + 1
139
+ : Math.max(node.items.length, node.items.length * 2 - 1);
140
+ return Math.max(2, Math.min(12, base + (node.label ? 1 : 0)));
141
+ }
142
+ case "row": {
143
+ const visible = node.children.filter((c) => isNodeVisible(c, scope));
144
+ if (visible.length === 0)
145
+ return 1;
146
+ let maxFixed = 1;
147
+ let anyFlex = false;
148
+ for (const c of visible) {
149
+ const r = intrinsicRows(c, scope);
150
+ if (r < 0)
151
+ anyFlex = true;
152
+ else if (r > maxFixed)
153
+ maxFixed = r;
154
+ }
155
+ return anyFlex ? -1 : maxFixed;
156
+ }
157
+ case "col": {
158
+ const visible = node.children.filter((c) => isNodeVisible(c, scope));
159
+ if (visible.length === 0)
160
+ return 1;
161
+ const gap = node.gap ?? 0;
162
+ let sum = Math.max(0, visible.length - 1) * gap;
163
+ for (const c of visible) {
164
+ const r = intrinsicRows(c, scope);
165
+ if (r < 0)
166
+ return -1;
167
+ sum += r;
168
+ }
169
+ return Math.max(1, sum);
170
+ }
171
+ case "box": {
172
+ const pad = node.padding ?? 0;
173
+ const inner = node.child
174
+ ? intrinsicRows(node.child, scope)
175
+ : node.children && node.children.length > 0
176
+ ? intrinsicRows({ type: "col", gap: 0, children: node.children }, scope)
177
+ : 1;
178
+ if (inner < 0)
179
+ return -1;
180
+ return inner + 2 + pad * 2;
181
+ }
182
+ }
183
+ }
184
+ /**
185
+ * Resolves a node tree's default state keys (so `surface.state` always reflects initial widget defaults).
186
+ */
187
+ export function collectUiDefaults(node, target) {
188
+ switch (node.type) {
189
+ case "col":
190
+ case "row":
191
+ for (const c of node.children)
192
+ collectUiDefaults(c, target);
193
+ return;
194
+ case "box":
195
+ if (node.child)
196
+ collectUiDefaults(node.child, target);
197
+ if (node.children)
198
+ for (const c of node.children)
199
+ collectUiDefaults(c, target);
200
+ return;
201
+ case "slider":
202
+ if (target[node.bind] === undefined)
203
+ target[node.bind] = node.min;
204
+ return;
205
+ case "toggle":
206
+ if (target[node.bind] === undefined)
207
+ target[node.bind] = false;
208
+ return;
209
+ case "segmented":
210
+ case "select": {
211
+ const opts = normalizeOptions(node.options);
212
+ if (target[node.bind] === undefined)
213
+ target[node.bind] = opts[0]?.value ?? "";
214
+ return;
215
+ }
216
+ case "checkboxes":
217
+ if (target[node.bind] === undefined)
218
+ target[node.bind] = [];
219
+ return;
220
+ case "input":
221
+ if (target[node.bind] === undefined)
222
+ target[node.bind] = "";
223
+ return;
224
+ case "timeline":
225
+ if (node.bind && target[node.bind] === undefined) {
226
+ target[node.bind] = node.items[0]?.id ?? "";
227
+ }
228
+ return;
229
+ case "table":
230
+ if (node.bind && target[node.bind] === undefined) {
231
+ const first = node.rows[0];
232
+ target[node.bind] = Array.isArray(first)
233
+ ? "0"
234
+ : String(first?.id ?? "0");
235
+ }
236
+ return;
237
+ default:
238
+ return;
239
+ }
240
+ }
241
+ export function uiFooterHints(root) {
242
+ let focusableCount = 0;
243
+ let hasAdjust = false;
244
+ let hasTimeline = false;
245
+ let hasToggle = false;
246
+ function walk(node) {
247
+ switch (node.type) {
248
+ case "col":
249
+ case "row":
250
+ for (const c of node.children)
251
+ walk(c);
252
+ return;
253
+ case "box":
254
+ if (node.child)
255
+ walk(node.child);
256
+ if (node.children)
257
+ for (const c of node.children)
258
+ walk(c);
259
+ return;
260
+ case "slider":
261
+ case "segmented":
262
+ focusableCount++;
263
+ hasAdjust = true;
264
+ return;
265
+ case "timeline":
266
+ focusableCount++;
267
+ hasTimeline = true;
268
+ return;
269
+ case "toggle":
270
+ case "checkboxes":
271
+ focusableCount++;
272
+ hasToggle = true;
273
+ return;
274
+ case "select":
275
+ case "input":
276
+ case "table":
277
+ case "button":
278
+ focusableCount++;
279
+ return;
280
+ default:
281
+ return;
282
+ }
283
+ }
284
+ walk(root);
285
+ const hints = [];
286
+ if (focusableCount > 1)
287
+ hints.push("Tab controls");
288
+ if (hasAdjust)
289
+ hints.push("←/→ adjust");
290
+ if (hasTimeline)
291
+ hints.push("↑/↓ stages");
292
+ if (!hasAdjust && !hasTimeline && hasToggle)
293
+ hints.push("Space toggle");
294
+ return hints;
295
+ }
296
+ export function drawUiSurface(frame, body, surface, store, caches) {
297
+ const pad = inset(body, [0, 1]);
298
+ if (pad.rows < 2 || pad.columns < 12)
299
+ return;
300
+ const showStatusRow = surface.submission.status !== "idle";
301
+ const notesRows = surface.notes.length > 0 ? Math.min(4, surface.notes.length + 1) : 0;
302
+ const [statusRow, mainArea, noteArea] = split(pad, "rows", [showStatusRow ? 1 : 0, -1, notesRows]);
303
+ if (showStatusRow && statusRow) {
304
+ const sub = surface.submission;
305
+ if (sub.status === "pending") {
306
+ text(frame, statusRow.x, statusRow.y, "● ACTION REQUIRED", { fg: "#f5a623", bold: true }, statusRow);
307
+ text(frame, statusRow.x + 19, statusRow.y, "Adjust controls, then press Submit (or 's' to push draft)", frame.theme.muted, statusRow);
308
+ }
309
+ else if (sub.status === "submitted") {
310
+ const badge = `✓ SUBMITTED (${sub.action})`;
311
+ const x1 = text(frame, statusRow.x, statusRow.y, badge, frame.theme.success, statusRow);
312
+ text(frame, x1 + 2, statusRow.y, "Tweak controls and resubmit anytime", frame.theme.muted, statusRow);
313
+ }
314
+ else if (sub.status === "cancelled") {
315
+ text(frame, statusRow.x, statusRow.y, "× CANCELLED", frame.theme.destructive, statusRow);
316
+ }
317
+ }
318
+ // Enrich expression scope with any selected timeline item helper (`selected_<bind>`)
319
+ const scope = { ...surface.state };
320
+ enrichTimelineScope(surface.root, scope);
321
+ if (mainArea && mainArea.rows > 0) {
322
+ renderNode(frame, mainArea, surface.root, surface, store, caches, scope, "r");
323
+ }
324
+ if (noteArea && notesRows > 0) {
325
+ caches.drawNotesStrip(frame, noteArea, surface);
326
+ }
327
+ }
328
+ function enrichTimelineScope(node, scope) {
329
+ if (node.type === "timeline" && node.bind) {
330
+ const selId = String(scope[node.bind] ?? node.items[0]?.id ?? "");
331
+ const found = node.items.find((it) => it.id === selId) ?? node.items[0];
332
+ if (found) {
333
+ scope[`${node.bind}_item`] = found;
334
+ }
335
+ }
336
+ else if (node.type === "col" || node.type === "row") {
337
+ for (const c of node.children)
338
+ enrichTimelineScope(c, scope);
339
+ }
340
+ else if (node.type === "box") {
341
+ if (node.child)
342
+ enrichTimelineScope(node.child, scope);
343
+ if (node.children)
344
+ for (const c of node.children)
345
+ enrichTimelineScope(c, scope);
346
+ }
347
+ }
348
+ function renderNode(frame, rect, node, surface, store, caches, scope, path) {
349
+ if (rect.columns <= 0 || rect.rows <= 0)
350
+ return;
351
+ if (!isNodeVisible(node, scope))
352
+ return;
353
+ switch (node.type) {
354
+ case "col": {
355
+ const visible = node.children.filter((c) => isNodeVisible(c, scope));
356
+ if (visible.length === 0)
357
+ return;
358
+ const gap = node.gap ?? 0;
359
+ const sizes = visible.map((c) => intrinsicRows(c, scope));
360
+ // If all fixed sizes exceed available rows, gracefully compress non-critical tall items.
361
+ const totalFixed = sizes.reduce((acc, s) => (s > 0 ? acc + s : acc), 0);
362
+ const avail = rect.rows - Math.max(0, visible.length - 1) * gap;
363
+ const adjusted = totalFixed > avail && avail > 0
364
+ ? sizes.map((s) => (s > 0 ? Math.max(1, Math.floor((s * avail) / totalFixed)) : s))
365
+ : sizes;
366
+ const slots = split(rect, "rows", adjusted, gap);
367
+ for (let i = 0; i < visible.length; i++) {
368
+ const childRect = slots[i];
369
+ if (childRect && childRect.rows > 0) {
370
+ renderNode(frame, childRect, visible[i], surface, store, caches, scope, `${path}.${i}`);
371
+ }
372
+ }
373
+ return;
374
+ }
375
+ case "row": {
376
+ const visible = node.children.filter((c) => isNodeVisible(c, scope));
377
+ if (visible.length === 0)
378
+ return;
379
+ const gap = node.gap ?? 1;
380
+ const sizes = visible.map((c) => c.size ?? -1);
381
+ const slots = split(rect, "columns", sizes, gap);
382
+ for (let i = 0; i < visible.length; i++) {
383
+ const childRect = slots[i];
384
+ if (childRect && childRect.columns > 0) {
385
+ renderNode(frame, childRect, visible[i], surface, store, caches, scope, `${path}.${i}`);
386
+ }
387
+ }
388
+ return;
389
+ }
390
+ case "box": {
391
+ const title = node.title ? interpolateText(node.title, scope) : undefined;
392
+ const inside = drawBox(frame.cells, rect, {
393
+ title,
394
+ style: frame.styles.style(frame.theme.border),
395
+ });
396
+ const inner = node.padding ? inset(inside, node.padding) : inset(inside, [0, 1]);
397
+ if (inner.rows <= 0 || inner.columns <= 0)
398
+ return;
399
+ if (node.child) {
400
+ renderNode(frame, inner, node.child, surface, store, caches, scope, `${path}.c`);
401
+ }
402
+ else if (node.children && node.children.length > 0) {
403
+ renderNode(frame, inner, { type: "col", gap: 0, children: node.children }, surface, store, caches, scope, `${path}.g`);
404
+ }
405
+ return;
406
+ }
407
+ case "divider": {
408
+ text(frame, rect.x, rect.y, "─".repeat(Math.max(0, rect.columns)), frame.theme.border, rect);
409
+ if (node.label) {
410
+ const lbl = ` ${interpolateText(node.label, scope)} `;
411
+ text(frame, rect.x + 1, rect.y, truncate(lbl, Math.max(2, rect.columns - 2)), frame.theme.primary, rect);
412
+ }
413
+ return;
414
+ }
415
+ case "text": {
416
+ const rendered = interpolateText(node.content, scope);
417
+ const style = toneToStyle(frame, node.tone, scope, node.bold);
418
+ const wrapped = wrap(rendered, Math.max(4, rect.columns));
419
+ for (let i = 0; i < Math.min(rect.rows, wrapped.length); i++) {
420
+ text(frame, rect.x, rect.y + i, truncate(wrapped[i], rect.columns), style, rect);
421
+ }
422
+ return;
423
+ }
424
+ case "badge": {
425
+ const lbl = ` ${interpolateText(node.label, scope)} `;
426
+ const toneHex = toneToHex(node.tone, scope);
427
+ text(frame, rect.x, rect.y, truncate(lbl, rect.columns), { fg: "#0e131d", bg: toneHex, bold: true }, rect);
428
+ return;
429
+ }
430
+ case "stat": {
431
+ const lbl = interpolateText(node.label, scope);
432
+ const val = interpolateText(node.value, scope);
433
+ const delta = node.delta ? interpolateText(node.delta, scope) : "";
434
+ text(frame, rect.x, rect.y, truncate(lbl, rect.columns), frame.theme.muted, rect);
435
+ if (rect.rows >= 2) {
436
+ const valStyle = toneToStyle(frame, node.tone ?? "heading", scope, true);
437
+ const x1 = text(frame, rect.x, rect.y + 1, truncate(val, rect.columns), valStyle, rect);
438
+ if (delta && rect.x + rect.columns - x1 > 2) {
439
+ text(frame, x1 + 2, rect.y + 1, truncate(delta, Math.max(0, rect.x + rect.columns - x1 - 2)), frame.theme.muted, rect);
440
+ }
441
+ }
442
+ return;
443
+ }
444
+ case "markdown": {
445
+ const md = caches.getMarkdown(frame);
446
+ const lines = md.lines(interpolateText(node.content, scope), Math.max(8, rect.columns));
447
+ drawLines(frame, rect, lines, 0);
448
+ return;
449
+ }
450
+ case "meter": {
451
+ const min = node.min ?? 0;
452
+ const max = node.max ?? 1;
453
+ const raw = evalNumber(node.value, scope, min);
454
+ const ratio = Math.max(0, Math.min(1, (raw - min) / Math.max(1e-9, max - min)));
455
+ const fillHex = toneToHex(node.tone ?? "primary", scope);
456
+ const valStr = node.format
457
+ ? interpolateText(node.format, { ...scope, value: raw, percent: Math.round(ratio * 100) })
458
+ : `${Math.round(ratio * 100)}%`;
459
+ const lbl = node.label ? interpolateText(node.label, scope) : "";
460
+ if (lbl && rect.rows >= 2) {
461
+ const vw = widthOf(valStr);
462
+ text(frame, rect.x, rect.y, truncate(lbl, Math.max(4, rect.columns - vw - 2)), frame.theme.muted, rect);
463
+ text(frame, rect.x + rect.columns - vw, rect.y, valStr, { fg: fillHex, bold: true }, rect);
464
+ drawMeter(frame, { x: rect.x, y: rect.y + 1, columns: rect.columns, rows: 1 }, ratio, { style: { fg: fillHex }, track: { fg: "#2a3242" } });
465
+ }
466
+ else {
467
+ const vw = widthOf(valStr);
468
+ const lblW = lbl ? Math.min(widthOf(lbl) + 2, Math.floor(rect.columns * 0.38)) : 0;
469
+ if (lbl) {
470
+ text(frame, rect.x, rect.y, truncate(lbl, Math.max(2, lblW - 1)), frame.theme.muted, rect);
471
+ }
472
+ const barW = Math.max(4, rect.columns - lblW - vw - 2);
473
+ drawMeter(frame, { x: rect.x + lblW, y: rect.y, columns: barW, rows: 1 }, ratio, { style: { fg: fillHex }, track: { fg: "#2a3242" } });
474
+ text(frame, rect.x + rect.columns - vw, rect.y, valStr, { fg: fillHex, bold: true }, rect);
475
+ }
476
+ return;
477
+ }
478
+ case "sparkline": {
479
+ const values = evalSeriesValues(node.values, scope);
480
+ const color = toneToHex(node.tone ?? "primary", scope);
481
+ let sparkRect = rect;
482
+ if (node.label && rect.rows >= 2) {
483
+ const lbl = interpolateText(node.label, scope);
484
+ const last = values.length ? values[values.length - 1] : 0;
485
+ const lastStr = Number.isInteger(last) ? String(last) : last.toFixed(1);
486
+ text(frame, rect.x, rect.y, truncate(lbl, Math.max(4, rect.columns - widthOf(lastStr) - 2)), frame.theme.muted, rect);
487
+ text(frame, rect.x + rect.columns - widthOf(lastStr), rect.y, lastStr, { fg: color, bold: true }, rect);
488
+ sparkRect = { x: rect.x, y: rect.y + 1, columns: rect.columns, rows: rect.rows - 1 };
489
+ }
490
+ if (sparkRect.rows >= 1 && values.length > 0) {
491
+ drawSparkline(frame, sparkRect, values, { style: { fg: color } });
492
+ }
493
+ return;
494
+ }
495
+ case "chart": {
496
+ const headerRows = node.title || node.series.length > 0 ? 1 : 0;
497
+ const [hdr, plot] = split(rect, "rows", [headerRows, -1]);
498
+ if (hdr && headerRows > 0) {
499
+ let x = hdr.x;
500
+ if (node.title) {
501
+ x = text(frame, x, hdr.y, interpolateText(node.title, scope), frame.theme.heading, hdr) + 2;
502
+ }
503
+ for (let i = 0; i < node.series.length && x < hdr.x + hdr.columns - 4; i++) {
504
+ const s = node.series[i];
505
+ const color = (s.color || SERIES_COLORS[i % SERIES_COLORS.length]);
506
+ x = text(frame, x, hdr.y, `● ${s.name}`, { fg: color, bold: true }, hdr) + 2;
507
+ }
508
+ }
509
+ if (plot && plot.rows >= 3) {
510
+ const key = `${surface.id}:${path}`;
511
+ let chart = caches.charts.get(key);
512
+ if (!chart) {
513
+ chart = createChart();
514
+ caches.charts.set(key, chart);
515
+ }
516
+ chart.draw(frame, plot, {
517
+ series: node.series.map((s, i) => ({
518
+ values: evalSeriesValues(s.values, scope),
519
+ style: { fg: (s.color || SERIES_COLORS[i % SERIES_COLORS.length]) },
520
+ })),
521
+ min: node.min,
522
+ max: node.max,
523
+ format: (v) => `${Number(v.toFixed(1))}${node.unit ? node.unit : ""}`,
524
+ });
525
+ }
526
+ return;
527
+ }
528
+ case "canvas": {
529
+ if (rect.rows < 1 || rect.columns < 2)
530
+ return;
531
+ const key = `${surface.id}:${path}`;
532
+ let canvas = caches.canvases.get(key);
533
+ if (!canvas || canvas.mode !== (node.mode ?? "braille")) {
534
+ canvas = createCanvas(node.mode ?? "braille");
535
+ caches.canvases.set(key, canvas);
536
+ }
537
+ canvas.resize(rect.columns, rect.rows);
538
+ const pw = Math.max(1, canvas.width - 1);
539
+ const ph = Math.max(1, canvas.height - 1);
540
+ for (const sh of node.shapes) {
541
+ const color = parseHexColor(sh.color, 0x7aa2ff);
542
+ if (sh.kind === "line") {
543
+ canvas.line(Math.round(evalNumber(sh.x0, scope, 0) * pw), Math.round(evalNumber(sh.y0, scope, 0) * ph), Math.round(evalNumber(sh.x1, scope, 1) * pw), Math.round(evalNumber(sh.y1, scope, 1) * ph), color);
544
+ }
545
+ else if (sh.kind === "rect") {
546
+ canvas.rect(Math.round(evalNumber(sh.x, scope, 0) * pw), Math.round(evalNumber(sh.y, scope, 0) * ph), Math.max(1, Math.round(evalNumber(sh.width, scope, 0.2) * canvas.width)), Math.max(1, Math.round(evalNumber(sh.height, scope, 0.2) * canvas.height)), color);
547
+ }
548
+ else if (sh.kind === "circle") {
549
+ canvas.circle(Math.round(evalNumber(sh.x, scope, 0.5) * pw), Math.round(evalNumber(sh.y, scope, 0.5) * ph), Math.max(1, Math.round(evalNumber(sh.radius, scope, 0.2) * Math.min(canvas.width, canvas.height))), color);
550
+ }
551
+ else if (sh.kind === "dot") {
552
+ canvas.set(Math.round(evalNumber(sh.x, scope, 0.5) * pw), Math.round(evalNumber(sh.y, scope, 0.5) * ph), color);
553
+ }
554
+ }
555
+ drawCanvas(frame, rect, canvas);
556
+ return;
557
+ }
558
+ case "table": {
559
+ const cols = node.columns.map((c) => typeof c === "string" ? { key: c, label: c } : c);
560
+ const rows = node.rows.map((r, idx) => {
561
+ if (Array.isArray(r)) {
562
+ return { value: String(idx), cells: r.map((v) => interpolateText(String(v ?? ""), scope)) };
563
+ }
564
+ return {
565
+ value: String(r.id ?? idx),
566
+ cells: cols.map((col) => {
567
+ const val = r[col.key] ?? r[col.label];
568
+ return val === undefined || val === null ? "" : interpolateText(String(val), scope);
569
+ }),
570
+ };
571
+ });
572
+ const curVal = node.bind ? String(surface.state[node.bind] ?? rows[0]?.value ?? "") : rows[0]?.value ?? "";
573
+ Table.Root(frame, {
574
+ id: `ui-table-${surface.id}-${path}`,
575
+ label: node.bind || "Table",
576
+ columns: cols.map((c) => ({ label: c.label, width: c.width, align: c.align })),
577
+ rows,
578
+ value: curVal,
579
+ onValueChange: (val) => {
580
+ if (node.bind)
581
+ store.setUiValue(surface.id, node.bind, val);
582
+ },
583
+ }, (root) => Table.Content(root, rect));
584
+ return;
585
+ }
586
+ case "slider": {
587
+ const cur = typeof surface.state[node.bind] === "number" ? surface.state[node.bind] : node.min;
588
+ Slider.Root(frame, {
589
+ id: `ui-slider-${surface.id}-${node.bind}`,
590
+ label: interpolateText(node.label, scope),
591
+ min: node.min,
592
+ max: node.max,
593
+ step: node.step,
594
+ unit: node.unit,
595
+ value: cur,
596
+ onValueChange: (val) => {
597
+ store.setUiValue(surface.id, node.bind, val);
598
+ },
599
+ }, (root) => Slider.Track(root, rect));
600
+ return;
601
+ }
602
+ case "toggle": {
603
+ const cur = Boolean(surface.state[node.bind]);
604
+ Toggle.Root(frame, {
605
+ id: `ui-toggle-${surface.id}-${node.bind}`,
606
+ label: interpolateText(node.label, scope),
607
+ onLabel: node.onLabel,
608
+ offLabel: node.offLabel,
609
+ description: node.description ? interpolateText(node.description, scope) : undefined,
610
+ value: cur,
611
+ onValueChange: (val) => {
612
+ store.setUiValue(surface.id, node.bind, val);
613
+ },
614
+ }, (root) => Toggle.Switch(root, rect));
615
+ return;
616
+ }
617
+ case "segmented": {
618
+ const opts = normalizeOptions(node.options);
619
+ const cur = String(surface.state[node.bind] ?? opts[0]?.value ?? "");
620
+ Segmented.Root(frame, {
621
+ id: `ui-seg-${surface.id}-${node.bind}`,
622
+ label: node.label ? interpolateText(node.label, scope) : undefined,
623
+ options: opts,
624
+ value: cur,
625
+ onValueChange: (val) => {
626
+ store.setUiValue(surface.id, node.bind, val);
627
+ },
628
+ }, (root) => Segmented.Group(root, rect));
629
+ return;
630
+ }
631
+ case "select": {
632
+ const opts = normalizeOptions(node.options);
633
+ const cur = String(surface.state[node.bind] ?? opts[0]?.value ?? "");
634
+ let listRect = rect;
635
+ if (node.label && rect.rows >= 2) {
636
+ text(frame, rect.x, rect.y, interpolateText(node.label, scope), frame.theme.heading, rect);
637
+ listRect = { x: rect.x, y: rect.y + 1, columns: rect.columns, rows: rect.rows - 1 };
638
+ }
639
+ Listbox.Root(frame, {
640
+ id: `ui-select-${surface.id}-${node.bind}`,
641
+ label: node.label || node.bind,
642
+ items: opts,
643
+ value: cur,
644
+ onValueChange: (val) => {
645
+ store.setUiValue(surface.id, node.bind, val);
646
+ },
647
+ }, (root) => Listbox.Content(root, listRect));
648
+ return;
649
+ }
650
+ case "checkboxes": {
651
+ const opts = normalizeOptions(node.options);
652
+ const raw = surface.state[node.bind];
653
+ const cur = Array.isArray(raw) ? raw.map(String) : [];
654
+ let listRect = rect;
655
+ if (node.label && rect.rows >= 2) {
656
+ text(frame, rect.x, rect.y, interpolateText(node.label, scope), frame.theme.heading, rect);
657
+ listRect = { x: rect.x, y: rect.y + 1, columns: rect.columns, rows: rect.rows - 1 };
658
+ }
659
+ CheckboxGroup.Root(frame, {
660
+ id: `ui-check-${surface.id}-${node.bind}`,
661
+ label: node.label || node.bind,
662
+ items: opts,
663
+ value: cur,
664
+ onValueChange: (vals) => {
665
+ store.setUiValue(surface.id, node.bind, [...vals]);
666
+ },
667
+ }, (root) => CheckboxGroup.Content(root, listRect));
668
+ return;
669
+ }
670
+ case "input": {
671
+ const cur = String(surface.state[node.bind] ?? "");
672
+ let inputRect = rect;
673
+ if (node.label && rect.rows >= 2) {
674
+ text(frame, rect.x, rect.y, interpolateText(node.label, scope), frame.theme.heading, rect);
675
+ inputRect = { x: rect.x, y: rect.y + 1, columns: rect.columns, rows: 1 };
676
+ }
677
+ Input.Root(frame, {
678
+ id: `ui-input-${surface.id}-${node.bind}`,
679
+ label: node.label || node.bind,
680
+ value: cur,
681
+ placeholder: node.placeholder,
682
+ onValueChange: (val) => {
683
+ store.setUiValue(surface.id, node.bind, val);
684
+ },
685
+ }, (root) => Input.Field(root, inputRect));
686
+ return;
687
+ }
688
+ case "timeline": {
689
+ const cur = node.bind ? String(surface.state[node.bind] ?? node.items[0]?.id ?? "") : node.items[0]?.id ?? "";
690
+ let tlRect = rect;
691
+ if (node.label && rect.rows >= 2) {
692
+ text(frame, rect.x, rect.y, interpolateText(node.label, scope), frame.theme.heading, rect);
693
+ tlRect = { x: rect.x, y: rect.y + 1, columns: rect.columns, rows: rect.rows - 1 };
694
+ }
695
+ Timeline.Root(frame, {
696
+ id: `ui-timeline-${surface.id}-${node.bind ?? path}`,
697
+ label: node.label || "Timeline",
698
+ mode: node.mode ?? "steps",
699
+ items: node.items,
700
+ min: node.min,
701
+ max: node.max,
702
+ unit: node.unit,
703
+ value: cur,
704
+ onValueChange: (id) => {
705
+ if (node.bind)
706
+ store.setUiValue(surface.id, node.bind, id);
707
+ },
708
+ }, (root) => Timeline.Track(root, tlRect));
709
+ return;
710
+ }
711
+ case "button": {
712
+ const isSubmit = Boolean(node.submit);
713
+ const btnId = isSubmit
714
+ ? `ui-submit-${surface.id}-${node.action}`
715
+ : `ui-btn-${surface.id}-${node.action}`;
716
+ Button.Root(frame, {
717
+ id: btnId,
718
+ label: interpolateText(node.label, scope),
719
+ variant: node.variant ?? (isSubmit ? "primary" : "default"),
720
+ humanOnly: isSubmit,
721
+ onPress: ({ source }) => {
722
+ if (isSubmit && source === "agent")
723
+ return;
724
+ if (node.set) {
725
+ for (const [k, v] of Object.entries(node.set)) {
726
+ store.setUiValue(surface.id, k, v);
727
+ }
728
+ }
729
+ if (isSubmit) {
730
+ const hadWaiter = store.hasUiWaiter(surface.id);
731
+ store.submitUi(surface.id, node.action);
732
+ if (!hadWaiter) {
733
+ caches.onUiSubmit?.(frame, surface);
734
+ }
735
+ }
736
+ else {
737
+ store.recordUiAction(surface.id, node.action);
738
+ }
739
+ },
740
+ }, (root) => Button.Trigger(root, rect));
741
+ return;
742
+ }
743
+ }
744
+ }