@bridgebrew/sessions 0.0.0-stage → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +43 -2
- package/answers.ts +167 -0
- package/browser/branch-actions.tsx +166 -0
- package/browser/changes.tsx +415 -0
- package/browser/children.tsx +650 -0
- package/browser/conversation.tsx +2381 -0
- package/browser/decision-questions.tsx +191 -0
- package/browser/elapsed.tsx +44 -0
- package/browser/gallery.tsx +218 -0
- package/browser/index.tsx +3434 -0
- package/browser/media-origins.tsx +38 -0
- package/browser/mobile.tsx +663 -0
- package/browser/open-link.tsx +30 -0
- package/browser/pull-request.tsx +116 -0
- package/browser/questions.tsx +156 -0
- package/browser/session-scheduled.tsx +203 -0
- package/browser/subagent-view.tsx +136 -0
- package/browser/subagents.tsx +339 -0
- package/browser/tabs.tsx +228 -0
- package/browser/tsconfig.json +15 -0
- package/browser/views/view.tsx +1064 -0
- package/browser/views.tsx +166 -0
- package/contract.ts +287 -0
- package/delegations.ts +1975 -0
- package/diff.ts +88 -0
- package/docs/en/images.mdx +51 -0
- package/docs/en/index.mdx +22 -0
- package/docs/en/meta.json +4 -0
- package/docs/en/organize-sessions.mdx +53 -0
- package/docs/en/start-a-session.mdx +63 -0
- package/docs/meta.json +4 -0
- package/git.ts +223 -0
- package/listing.ts +382 -0
- package/package.json +1023 -4
- package/peers.ts +677 -0
- package/pull-request-watch.ts +282 -0
- package/pull-requests.ts +606 -0
- package/server.ts +5915 -0
- package/text.ts +19 -0
- package/tsconfig.json +3 -0
- package/viewer-activity.ts +57 -0
- package/views/examples.ts +153 -0
- package/views/library.ts +1116 -0
package/views/library.ts
ADDED
|
@@ -0,0 +1,1116 @@
|
|
|
1
|
+
// research-036 (OpenUI POC): the components an agent composes a view from, and the language it writes them in.
|
|
2
|
+
//
|
|
3
|
+
// Server-safe: no React and no @bridge/ui here, so a host can build the prompt and check a program without a page.
|
|
4
|
+
// The kit components that draw these live in view.tsx; this file only declares their names, props (in positional
|
|
5
|
+
// order) and descriptions. The prompt is generated by lang-core from that declaration.
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
action,
|
|
9
|
+
createLibrary,
|
|
10
|
+
createParser,
|
|
11
|
+
createStore,
|
|
12
|
+
defineComponent,
|
|
13
|
+
evaluate,
|
|
14
|
+
evaluateRoot,
|
|
15
|
+
markReactive,
|
|
16
|
+
mergeStatements,
|
|
17
|
+
type ElementNode,
|
|
18
|
+
type EvaluationContext,
|
|
19
|
+
type OpenUIError,
|
|
20
|
+
type ParseResult,
|
|
21
|
+
type StateField,
|
|
22
|
+
type ValidationError,
|
|
23
|
+
} from '@openuidev/lang-core';
|
|
24
|
+
import { z } from 'zod/v4';
|
|
25
|
+
|
|
26
|
+
/** `value`'s items when it is an array, else none (read as `unknown`, not `any`). */
|
|
27
|
+
const listOf = (value: unknown): unknown[] => (Array.isArray(value) ? value : []);
|
|
28
|
+
|
|
29
|
+
/** A prop that binds a `$variable` two-way: the runtime hands the renderer the binding, not just its value. */
|
|
30
|
+
function reactive<T extends z.ZodType>(schema: T): z.ZodType<StateField<z.infer<T>>> {
|
|
31
|
+
markReactive(schema);
|
|
32
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- markReactive only marks the schema; the runtime hands the binding
|
|
33
|
+
return schema as unknown as z.ZodType<StateField<z.infer<T>>>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The icons a view may name: each one of the kit's (`ICON_NAMES` of @bridge/ui, checked by view.test.tsx), which may
|
|
37
|
+
* draw more. */
|
|
38
|
+
export const ICONS = [
|
|
39
|
+
'activity',
|
|
40
|
+
'alarm-clock',
|
|
41
|
+
'alarm-clock-off',
|
|
42
|
+
'archive',
|
|
43
|
+
'archive-restore',
|
|
44
|
+
'arrow-down',
|
|
45
|
+
'arrow-left',
|
|
46
|
+
'arrow-right',
|
|
47
|
+
'arrow-up',
|
|
48
|
+
'arrow-up-right',
|
|
49
|
+
'at-sign',
|
|
50
|
+
'ban',
|
|
51
|
+
'bell',
|
|
52
|
+
'book',
|
|
53
|
+
'book-open',
|
|
54
|
+
'bot',
|
|
55
|
+
'box',
|
|
56
|
+
'briefcase',
|
|
57
|
+
'brain',
|
|
58
|
+
'building',
|
|
59
|
+
'cake',
|
|
60
|
+
'calendar',
|
|
61
|
+
'chart-bar',
|
|
62
|
+
'check',
|
|
63
|
+
'chevron-down',
|
|
64
|
+
'chevron-left',
|
|
65
|
+
'chevron-right',
|
|
66
|
+
'chevron-up',
|
|
67
|
+
'circle-dashed',
|
|
68
|
+
'circle-dot',
|
|
69
|
+
'circle-help',
|
|
70
|
+
'copy',
|
|
71
|
+
'corner-down-left',
|
|
72
|
+
'dashboard',
|
|
73
|
+
'database',
|
|
74
|
+
'download',
|
|
75
|
+
'ellipsis',
|
|
76
|
+
'eye',
|
|
77
|
+
'file',
|
|
78
|
+
'file-text',
|
|
79
|
+
'files',
|
|
80
|
+
'filter',
|
|
81
|
+
'folder',
|
|
82
|
+
'folder-open',
|
|
83
|
+
'folder-plus',
|
|
84
|
+
'folder-root',
|
|
85
|
+
'image',
|
|
86
|
+
'git-branch',
|
|
87
|
+
'globe',
|
|
88
|
+
'hand',
|
|
89
|
+
'hard-drive',
|
|
90
|
+
'home',
|
|
91
|
+
'inbox',
|
|
92
|
+
'layout-dashboard',
|
|
93
|
+
'link',
|
|
94
|
+
'list',
|
|
95
|
+
'list-checks',
|
|
96
|
+
'list-plus',
|
|
97
|
+
'loader-circle',
|
|
98
|
+
'lock',
|
|
99
|
+
'mail',
|
|
100
|
+
'map-pin',
|
|
101
|
+
'maximize-2',
|
|
102
|
+
'messages-square',
|
|
103
|
+
'minimize-2',
|
|
104
|
+
'monitor',
|
|
105
|
+
'move',
|
|
106
|
+
'notebook',
|
|
107
|
+
'package',
|
|
108
|
+
'panel-left-close',
|
|
109
|
+
'panel-left-open',
|
|
110
|
+
'pause',
|
|
111
|
+
'pen-line',
|
|
112
|
+
'phone',
|
|
113
|
+
'pin',
|
|
114
|
+
'pin-off',
|
|
115
|
+
'play',
|
|
116
|
+
'plug',
|
|
117
|
+
'plus',
|
|
118
|
+
'power',
|
|
119
|
+
'puzzle',
|
|
120
|
+
'receipt',
|
|
121
|
+
'rotate-ccw',
|
|
122
|
+
'rotate-cw',
|
|
123
|
+
'scroll-text',
|
|
124
|
+
'search',
|
|
125
|
+
'settings',
|
|
126
|
+
'shield',
|
|
127
|
+
'shield-check',
|
|
128
|
+
'shield-x',
|
|
129
|
+
'smartphone',
|
|
130
|
+
'square',
|
|
131
|
+
'square-pen',
|
|
132
|
+
'star',
|
|
133
|
+
'tag',
|
|
134
|
+
'terminal',
|
|
135
|
+
'text-wrap',
|
|
136
|
+
'trash',
|
|
137
|
+
'triangle-alert',
|
|
138
|
+
'undo',
|
|
139
|
+
'user',
|
|
140
|
+
'user-plus',
|
|
141
|
+
'users',
|
|
142
|
+
'video',
|
|
143
|
+
'wrench',
|
|
144
|
+
'x',
|
|
145
|
+
] as const;
|
|
146
|
+
const icon = z.enum(ICONS);
|
|
147
|
+
|
|
148
|
+
/** Any library component. A union of every `.ref` would be circular (Card holds Card children), so slots take any. */
|
|
149
|
+
const component = z.any();
|
|
150
|
+
/** Several components, in the order they are written. */
|
|
151
|
+
const components = z.array(z.any());
|
|
152
|
+
|
|
153
|
+
// ─── Leaf components (no component props) ───────────────────────────────────
|
|
154
|
+
|
|
155
|
+
const Col = defineComponent({
|
|
156
|
+
name: 'Col',
|
|
157
|
+
description: 'A table column: its heading and its CSS grid track.',
|
|
158
|
+
props: z.object({
|
|
159
|
+
label: z.string(),
|
|
160
|
+
width: z.string().optional(),
|
|
161
|
+
}),
|
|
162
|
+
component: null,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const Tab = defineComponent({
|
|
166
|
+
name: 'Tab',
|
|
167
|
+
description: 'One tab: its label and the content shown under it.',
|
|
168
|
+
props: z.object({
|
|
169
|
+
label: z.string(),
|
|
170
|
+
children: component,
|
|
171
|
+
}),
|
|
172
|
+
component: null,
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const Option = defineComponent({
|
|
176
|
+
name: 'Option',
|
|
177
|
+
description: 'One choice in a Select or a SegmentGroup.',
|
|
178
|
+
props: z.object({
|
|
179
|
+
value: z.string(),
|
|
180
|
+
label: z.string(),
|
|
181
|
+
}),
|
|
182
|
+
component: null,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const Heading = defineComponent({
|
|
186
|
+
name: 'Heading',
|
|
187
|
+
description: 'A heading above the content it names.',
|
|
188
|
+
props: z.object({
|
|
189
|
+
text: z.string(),
|
|
190
|
+
level: z.union([z.literal(1), z.literal(2), z.literal(3)]).optional(),
|
|
191
|
+
}),
|
|
192
|
+
component: null,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const Text = defineComponent({
|
|
196
|
+
name: 'Text',
|
|
197
|
+
description: 'A line of text.',
|
|
198
|
+
props: z.object({
|
|
199
|
+
text: z.string(),
|
|
200
|
+
tone: z.enum(['default', 'muted', 'destructive']).optional(),
|
|
201
|
+
}),
|
|
202
|
+
component: null,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const Kbd = defineComponent({
|
|
206
|
+
name: 'Kbd',
|
|
207
|
+
description: 'A shortcut next to a button’s label (Create ⌘↵, Clear search Esc, New contact C).',
|
|
208
|
+
props: z.object({
|
|
209
|
+
keys: z.string(),
|
|
210
|
+
}),
|
|
211
|
+
component: null,
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const Icon = defineComponent({
|
|
215
|
+
name: 'Icon',
|
|
216
|
+
description: 'An icon of the kit, by name.',
|
|
217
|
+
props: z.object({
|
|
218
|
+
name: icon,
|
|
219
|
+
}),
|
|
220
|
+
component: null,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
const Avatar = defineComponent({
|
|
224
|
+
name: 'Avatar',
|
|
225
|
+
description:
|
|
226
|
+
'A person’s initials in a circle, in lists and detail cards (the ListItem leading piece); an organization’s in a square. Sizes xs to 3xl (2xl or 3xl atop a record’s card).',
|
|
227
|
+
props: z.object({
|
|
228
|
+
name: z.string(),
|
|
229
|
+
size: z.enum(['xs', 'sm', 'default', 'lg', 'xl', '2xl', '3xl']).optional(),
|
|
230
|
+
shape: z.enum(['circle', 'square']).optional(),
|
|
231
|
+
}),
|
|
232
|
+
component: null,
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const Badge = defineComponent({
|
|
236
|
+
name: 'Badge',
|
|
237
|
+
description: 'A small count next to a label (unread messages, open issues).',
|
|
238
|
+
props: z.object({
|
|
239
|
+
text: z.union([z.string(), z.number()]),
|
|
240
|
+
}),
|
|
241
|
+
component: null,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
const TagPill = defineComponent({
|
|
245
|
+
name: 'TagPill',
|
|
246
|
+
description: 'A tag of a record, its colour derived from its label; sm in dense rows.',
|
|
247
|
+
props: z.object({
|
|
248
|
+
label: z.string(),
|
|
249
|
+
size: z.enum(['sm', 'default']).optional(),
|
|
250
|
+
}),
|
|
251
|
+
component: null,
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
const StatusIcon = defineComponent({
|
|
255
|
+
name: 'StatusIcon',
|
|
256
|
+
description: 'For displaying issue/task workflow status (backlog, todo, in-progress, etc.).',
|
|
257
|
+
props: z.object({
|
|
258
|
+
status: z.enum(['backlog', 'todo', 'in-progress', 'in-review', 'done', 'cancelled']),
|
|
259
|
+
}),
|
|
260
|
+
component: null,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const EmptyState = defineComponent({
|
|
264
|
+
name: 'EmptyState',
|
|
265
|
+
description:
|
|
266
|
+
'An empty list (no records yet, nothing matches a search or a filter), with the action that helps.',
|
|
267
|
+
props: z.object({
|
|
268
|
+
title: z.string(),
|
|
269
|
+
description: z.string().optional(),
|
|
270
|
+
icon: icon.optional(),
|
|
271
|
+
}),
|
|
272
|
+
component: null,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
const Button = defineComponent({
|
|
276
|
+
name: 'Button',
|
|
277
|
+
description: 'For triggering actions and submitting forms.',
|
|
278
|
+
props: z.object({
|
|
279
|
+
label: z.string(),
|
|
280
|
+
action: action().optional(),
|
|
281
|
+
variant: z.enum(['primary', 'outline', 'ghost', 'destructive']).optional(),
|
|
282
|
+
}),
|
|
283
|
+
component: null,
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
const Buttons = defineComponent({
|
|
287
|
+
name: 'Buttons',
|
|
288
|
+
description: 'The actions at a form’s end.',
|
|
289
|
+
props: z.object({
|
|
290
|
+
children: z.array(Button.ref),
|
|
291
|
+
}),
|
|
292
|
+
component: null,
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// ─── Layout ─────────────────────────────────────────────────────────────────
|
|
296
|
+
|
|
297
|
+
const Stack = defineComponent({
|
|
298
|
+
name: 'Stack',
|
|
299
|
+
description: 'Lay content out top to bottom on one column.',
|
|
300
|
+
props: z.object({
|
|
301
|
+
children: components,
|
|
302
|
+
gap: z.enum(['sm', 'md', 'lg']).optional(),
|
|
303
|
+
}),
|
|
304
|
+
component: null,
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const Cluster = defineComponent({
|
|
308
|
+
name: 'Cluster',
|
|
309
|
+
description:
|
|
310
|
+
'Things side by side on a line, centred, 8px apart, wrapping when they must (a control, then its badge).',
|
|
311
|
+
props: z.object({
|
|
312
|
+
children: components,
|
|
313
|
+
justify: z.enum(['start', 'between', 'end']).optional(),
|
|
314
|
+
}),
|
|
315
|
+
component: null,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
const Card = defineComponent({
|
|
319
|
+
name: 'Card',
|
|
320
|
+
description:
|
|
321
|
+
'A bordered region with an optional title and description (a dashboard widget, a record’s card).',
|
|
322
|
+
props: z.object({
|
|
323
|
+
children: components,
|
|
324
|
+
title: z.string().optional(),
|
|
325
|
+
description: z.string().optional(),
|
|
326
|
+
}),
|
|
327
|
+
component: null,
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const Section = defineComponent({
|
|
331
|
+
name: 'Section',
|
|
332
|
+
description: 'A titled group of content, with an optional one-line lead.',
|
|
333
|
+
props: z.object({
|
|
334
|
+
title: z.string(),
|
|
335
|
+
children: components,
|
|
336
|
+
description: z.string().optional(),
|
|
337
|
+
}),
|
|
338
|
+
component: null,
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
const Tabs = defineComponent({
|
|
342
|
+
name: 'Tabs',
|
|
343
|
+
description: 'For switching between views or content sections.',
|
|
344
|
+
props: z.object({
|
|
345
|
+
tabs: z.array(Tab.ref),
|
|
346
|
+
}),
|
|
347
|
+
component: null,
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
// ─── Data ───────────────────────────────────────────────────────────────────
|
|
351
|
+
|
|
352
|
+
const Table = defineComponent({
|
|
353
|
+
name: 'Table',
|
|
354
|
+
description: 'For displaying structured data in rows and columns.',
|
|
355
|
+
props: z.object({
|
|
356
|
+
columns: z.array(Col.ref),
|
|
357
|
+
rows: z.array(z.array(z.any())),
|
|
358
|
+
}),
|
|
359
|
+
component: null,
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
const Property = defineComponent({
|
|
363
|
+
name: 'Property',
|
|
364
|
+
description: 'One label and its value in a Properties list.',
|
|
365
|
+
props: z.object({
|
|
366
|
+
label: z.string(),
|
|
367
|
+
value: component,
|
|
368
|
+
}),
|
|
369
|
+
component: null,
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
const Properties = defineComponent({
|
|
373
|
+
name: 'Properties',
|
|
374
|
+
description: 'For displaying entity details (issue properties, user profiles, settings).',
|
|
375
|
+
props: z.object({
|
|
376
|
+
items: z.array(Property.ref),
|
|
377
|
+
}),
|
|
378
|
+
component: null,
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
const ListItem = defineComponent({
|
|
382
|
+
name: 'ListItem',
|
|
383
|
+
description:
|
|
384
|
+
'One row of a list: a title, and optionally a subtitle, a leading piece, a trailing piece and an action.',
|
|
385
|
+
props: z.object({
|
|
386
|
+
title: z.string(),
|
|
387
|
+
subtitle: z.string().optional(),
|
|
388
|
+
leading: component.optional(),
|
|
389
|
+
trailing: component.optional(),
|
|
390
|
+
action: action().optional(),
|
|
391
|
+
}),
|
|
392
|
+
component: null,
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
const List = defineComponent({
|
|
396
|
+
name: 'List',
|
|
397
|
+
description: 'A list of records, one ListItem per row (people, projects, notifications).',
|
|
398
|
+
props: z.object({
|
|
399
|
+
items: z.array(ListItem.ref),
|
|
400
|
+
}),
|
|
401
|
+
component: null,
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
// ─── Forms ──────────────────────────────────────────────────────────────────
|
|
405
|
+
|
|
406
|
+
const Form = defineComponent({
|
|
407
|
+
name: 'Form',
|
|
408
|
+
description: 'A form: named fields and the buttons that submit them.',
|
|
409
|
+
props: z.object({
|
|
410
|
+
name: z.string(),
|
|
411
|
+
children: components,
|
|
412
|
+
buttons: z.optional(Buttons.ref),
|
|
413
|
+
}),
|
|
414
|
+
component: null,
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
const Field = defineComponent({
|
|
418
|
+
name: 'Field',
|
|
419
|
+
description: 'Around any form control that needs a label, a hint, or an error.',
|
|
420
|
+
props: z.object({
|
|
421
|
+
label: z.string(),
|
|
422
|
+
control: component,
|
|
423
|
+
description: z.string().optional(),
|
|
424
|
+
}),
|
|
425
|
+
component: null,
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
const Input = defineComponent({
|
|
429
|
+
name: 'Input',
|
|
430
|
+
description:
|
|
431
|
+
'Single-line text entry. rules: {required, email, url, numeric, minLength, maxLength, min, max, pattern}, checked before a button of its Form acts.',
|
|
432
|
+
props: z.object({
|
|
433
|
+
name: z.string(),
|
|
434
|
+
placeholder: z.string().optional(),
|
|
435
|
+
type: z.enum(['text', 'email', 'number', 'url']).optional(),
|
|
436
|
+
rules: z.record(z.string(), z.any()).optional(),
|
|
437
|
+
value: reactive(z.string().optional()),
|
|
438
|
+
}),
|
|
439
|
+
component: null,
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
const Textarea = defineComponent({
|
|
443
|
+
name: 'Textarea',
|
|
444
|
+
description: 'For multiline text entry (descriptions, comments, notes).',
|
|
445
|
+
props: z.object({
|
|
446
|
+
name: z.string(),
|
|
447
|
+
placeholder: z.string().optional(),
|
|
448
|
+
rules: z.record(z.string(), z.any()).optional(),
|
|
449
|
+
value: reactive(z.string().optional()),
|
|
450
|
+
}),
|
|
451
|
+
component: null,
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const Select = defineComponent({
|
|
455
|
+
name: 'Select',
|
|
456
|
+
description: 'For choosing one value from a short fixed list in a form.',
|
|
457
|
+
props: z.object({
|
|
458
|
+
name: z.string(),
|
|
459
|
+
options: z.array(Option.ref),
|
|
460
|
+
placeholder: z.string().optional(),
|
|
461
|
+
value: reactive(z.string().optional()),
|
|
462
|
+
}),
|
|
463
|
+
component: null,
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
const Checkbox = defineComponent({
|
|
467
|
+
name: 'Checkbox',
|
|
468
|
+
description: 'For boolean selections in forms.',
|
|
469
|
+
props: z.object({
|
|
470
|
+
name: z.string(),
|
|
471
|
+
label: z.string(),
|
|
472
|
+
value: reactive(z.boolean().optional()),
|
|
473
|
+
}),
|
|
474
|
+
component: null,
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
const Toggle = defineComponent({
|
|
478
|
+
name: 'Toggle',
|
|
479
|
+
description: 'For on/off settings and preferences.',
|
|
480
|
+
props: z.object({
|
|
481
|
+
name: z.string(),
|
|
482
|
+
label: z.string(),
|
|
483
|
+
value: reactive(z.boolean().optional()),
|
|
484
|
+
}),
|
|
485
|
+
component: null,
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
const Slider = defineComponent({
|
|
489
|
+
name: 'Slider',
|
|
490
|
+
description:
|
|
491
|
+
'For a bounded number whose exact value matters less than its feel (an opacity, a blur, a size).',
|
|
492
|
+
props: z.object({
|
|
493
|
+
name: z.string(),
|
|
494
|
+
min: z.number(),
|
|
495
|
+
max: z.number(),
|
|
496
|
+
step: z.number().optional(),
|
|
497
|
+
value: reactive(z.number().optional()),
|
|
498
|
+
}),
|
|
499
|
+
component: null,
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
const SegmentGroup = defineComponent({
|
|
503
|
+
name: 'SegmentGroup',
|
|
504
|
+
description: 'For single-choice selection between 2-5 options.',
|
|
505
|
+
props: z.object({
|
|
506
|
+
name: z.string(),
|
|
507
|
+
options: z.array(Option.ref),
|
|
508
|
+
value: reactive(z.string().optional()),
|
|
509
|
+
}),
|
|
510
|
+
component: null,
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
/** The library a view is parsed and checked against; Stack is the entry point. */
|
|
514
|
+
export const library = createLibrary({
|
|
515
|
+
root: 'Stack',
|
|
516
|
+
components: [
|
|
517
|
+
Stack,
|
|
518
|
+
Cluster,
|
|
519
|
+
Card,
|
|
520
|
+
Section,
|
|
521
|
+
Tabs,
|
|
522
|
+
Tab,
|
|
523
|
+
Heading,
|
|
524
|
+
Text,
|
|
525
|
+
Kbd,
|
|
526
|
+
Icon,
|
|
527
|
+
Table,
|
|
528
|
+
Col,
|
|
529
|
+
Properties,
|
|
530
|
+
Property,
|
|
531
|
+
List,
|
|
532
|
+
ListItem,
|
|
533
|
+
Badge,
|
|
534
|
+
TagPill,
|
|
535
|
+
Avatar,
|
|
536
|
+
StatusIcon,
|
|
537
|
+
EmptyState,
|
|
538
|
+
Form,
|
|
539
|
+
Field,
|
|
540
|
+
Input,
|
|
541
|
+
Textarea,
|
|
542
|
+
Select,
|
|
543
|
+
Option,
|
|
544
|
+
Checkbox,
|
|
545
|
+
Toggle,
|
|
546
|
+
Slider,
|
|
547
|
+
SegmentGroup,
|
|
548
|
+
Button,
|
|
549
|
+
Buttons,
|
|
550
|
+
],
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
/** The component names, in the order the language lists them. */
|
|
554
|
+
export const COMPONENTS: string[] = Object.keys(library.components);
|
|
555
|
+
|
|
556
|
+
/** A plugin's component as a view's library takes it: the declaration and the plugin whose view it is. */
|
|
557
|
+
/** A component a plugin adds to the views an agent composes (its manifest's `views.components`, as the host checked it),
|
|
558
|
+
* with its plugin's key. */
|
|
559
|
+
export type PluginComponent = {
|
|
560
|
+
name: string;
|
|
561
|
+
description: string;
|
|
562
|
+
props: {
|
|
563
|
+
type: 'object';
|
|
564
|
+
properties?: Record<string, unknown> | undefined;
|
|
565
|
+
required?: string[] | undefined;
|
|
566
|
+
} & Record<string, unknown>;
|
|
567
|
+
plugin: string;
|
|
568
|
+
} & Record<string, unknown>;
|
|
569
|
+
|
|
570
|
+
/** The libraries built for plugin components, by the components' JSON, with their parser: the last few only, since a
|
|
571
|
+
* plugin's update or reload changes the key. */
|
|
572
|
+
const built = new Map<
|
|
573
|
+
string,
|
|
574
|
+
{ library: ReturnType<typeof createLibrary>; parser: ReturnType<typeof createParser> }
|
|
575
|
+
>();
|
|
576
|
+
const BUILT_MAX = 16;
|
|
577
|
+
|
|
578
|
+
const parser = createParser(library.toJSONSchema());
|
|
579
|
+
|
|
580
|
+
/** A JSON Schema object as zod's reader takes it. */
|
|
581
|
+
type JsonSchemaObject = Exclude<Parameters<typeof z.fromJSONSchema>[0], boolean>;
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* The kit's library, plus one component per plugin component (research-036 P1), cached by the components' JSON.
|
|
585
|
+
*
|
|
586
|
+
* A plugin component's props are its JSON Schema, read by zod as it is (its property order is the positional order the
|
|
587
|
+
* language writes); a name the kit has is skipped, the kit's own kept. Each description names its plugin.
|
|
588
|
+
*/
|
|
589
|
+
export function libraryFor(components: PluginComponent[] = []) {
|
|
590
|
+
return builtFor(components).library;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/** `components`' library and parser; the kit's without any. Two plugins with one name (install refuses it): the first. */
|
|
594
|
+
function builtFor(components: PluginComponent[]) {
|
|
595
|
+
if (components.length === 0) return { library, parser };
|
|
596
|
+
const key = JSON.stringify(components);
|
|
597
|
+
let found = built.get(key);
|
|
598
|
+
if (found === undefined) {
|
|
599
|
+
const names = new Set<string>(COMPONENTS);
|
|
600
|
+
const own = components.filter(({ name }) => !names.has(name) && names.add(name));
|
|
601
|
+
const extended = createLibrary({
|
|
602
|
+
root: 'Stack',
|
|
603
|
+
components: [
|
|
604
|
+
...Object.values(library.components),
|
|
605
|
+
...own.map(({ name, description, props, plugin }) =>
|
|
606
|
+
defineComponent({
|
|
607
|
+
name,
|
|
608
|
+
description: `${description} (plugin ${plugin})`,
|
|
609
|
+
props: z.fromJSONSchema(props as JsonSchemaObject) as z.ZodObject,
|
|
610
|
+
component: null,
|
|
611
|
+
}),
|
|
612
|
+
),
|
|
613
|
+
],
|
|
614
|
+
});
|
|
615
|
+
found = { library: extended, parser: createParser(extended.toJSONSchema()) };
|
|
616
|
+
if (built.size >= BUILT_MAX) built.delete(built.keys().next().value!);
|
|
617
|
+
built.set(key, found);
|
|
618
|
+
}
|
|
619
|
+
return found;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** `components`' parser, built beside its library. */
|
|
623
|
+
const parserFor = (components: PluginComponent[] = []) => builtFor(components).parser;
|
|
624
|
+
|
|
625
|
+
/** A parse error as the correction loop needs it: which statement, what, and how to fix it. */
|
|
626
|
+
export interface CheckError {
|
|
627
|
+
statement?: string;
|
|
628
|
+
code: string;
|
|
629
|
+
message: string;
|
|
630
|
+
hint?: string;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export interface CheckResult {
|
|
634
|
+
ok: boolean;
|
|
635
|
+
errors: CheckError[];
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
function toError(error: ValidationError): CheckError {
|
|
639
|
+
return {
|
|
640
|
+
...(error.statementId !== undefined ? { statement: error.statementId } : {}),
|
|
641
|
+
code: error.code,
|
|
642
|
+
message: error.message,
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
const NO_ROOT: CheckError = {
|
|
647
|
+
code: 'no-root',
|
|
648
|
+
message: 'No root statement: start the program with root = Stack([...]).',
|
|
649
|
+
hint: 'Every view needs a root that renders a component.',
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Parse a program and say whether it has something to draw.
|
|
654
|
+
*
|
|
655
|
+
* `ok` means a root element renders; a statement that fails validation is dropped and reported, while the rest of
|
|
656
|
+
* the view still draws. An empty program, no root, or a root that is not a component is not ok.
|
|
657
|
+
*/
|
|
658
|
+
export function check(program: string, components: PluginComponent[] = []): CheckResult {
|
|
659
|
+
if (!program.trim()) return { ok: false, errors: [NO_ROOT] };
|
|
660
|
+
|
|
661
|
+
let result: ParseResult;
|
|
662
|
+
try {
|
|
663
|
+
result = parserFor(components).parse(program);
|
|
664
|
+
} catch (error) {
|
|
665
|
+
return {
|
|
666
|
+
ok: false,
|
|
667
|
+
errors: [
|
|
668
|
+
{
|
|
669
|
+
code: 'parse-exception',
|
|
670
|
+
message: `The program could not be parsed: ${error instanceof Error ? error.message : String(error)}`,
|
|
671
|
+
},
|
|
672
|
+
],
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
const errors = result.meta.errors.map(toError);
|
|
677
|
+
if (!result.root) {
|
|
678
|
+
if (errors.length === 0) errors.push(NO_ROOT);
|
|
679
|
+
return { ok: false, errors };
|
|
680
|
+
}
|
|
681
|
+
return { ok: true, errors };
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* The plugin components a program actually calls, in `components`' order (research-036 P2).
|
|
686
|
+
*
|
|
687
|
+
* The program is parsed with the library `components` extend, so a component the parser drops (unknown, or one the kit
|
|
688
|
+
* has) is not one; a name that only appears inside a string is not a call.
|
|
689
|
+
*/
|
|
690
|
+
export function used(program: string, components: PluginComponent[] = []): PluginComponent[] {
|
|
691
|
+
if (components.length === 0) return [];
|
|
692
|
+
let result: ParseResult;
|
|
693
|
+
try {
|
|
694
|
+
result = parserFor(components).parse(program);
|
|
695
|
+
} catch {
|
|
696
|
+
return [];
|
|
697
|
+
}
|
|
698
|
+
if (!result.root) return [];
|
|
699
|
+
|
|
700
|
+
const called = new Set<string>();
|
|
701
|
+
const visit = (value: unknown): void => {
|
|
702
|
+
if (isElement(value)) {
|
|
703
|
+
called.add(value.typeName);
|
|
704
|
+
for (const child of Object.values(value.props)) visit(child);
|
|
705
|
+
} else if (Array.isArray(value)) {
|
|
706
|
+
for (const item of value) visit(item);
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
visit(result.root);
|
|
710
|
+
const kit = new Set<string>(COMPONENTS);
|
|
711
|
+
return components.filter(({ name }) => !kit.has(name) && called.has(name));
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/** A JSON string's escapes decoded: `"` at `start`, up to its closing quote or the end of `text` (then incomplete). */
|
|
715
|
+
const readString = (
|
|
716
|
+
text: string,
|
|
717
|
+
start: number,
|
|
718
|
+
): { value: string; end: number; complete: boolean } => {
|
|
719
|
+
const simple: Record<string, string> = {
|
|
720
|
+
'"': '"',
|
|
721
|
+
'\\': '\\',
|
|
722
|
+
'/': '/',
|
|
723
|
+
b: '\b',
|
|
724
|
+
f: '\f',
|
|
725
|
+
n: '\n',
|
|
726
|
+
r: '\r',
|
|
727
|
+
t: '\t',
|
|
728
|
+
};
|
|
729
|
+
let value = '';
|
|
730
|
+
let index = start + 1;
|
|
731
|
+
while (index < text.length) {
|
|
732
|
+
const char = text[index]!;
|
|
733
|
+
if (char === '"') return { value, end: index + 1, complete: true };
|
|
734
|
+
if (char !== '\\') {
|
|
735
|
+
value += char;
|
|
736
|
+
index++;
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
const escaped = text[index + 1];
|
|
740
|
+
if (escaped === undefined) return { value, end: text.length, complete: false };
|
|
741
|
+
if (escaped === 'u') {
|
|
742
|
+
const hex = text.slice(index + 2, index + 6);
|
|
743
|
+
if (!/^[0-9a-fA-F]{4}$/.test(hex)) return { value, end: text.length, complete: false };
|
|
744
|
+
value += String.fromCharCode(Number.parseInt(hex, 16));
|
|
745
|
+
index += 6;
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
const decoded = simple[escaped];
|
|
749
|
+
if (decoded === undefined) return { value, end: text.length, complete: false };
|
|
750
|
+
value += decoded;
|
|
751
|
+
index += 2;
|
|
752
|
+
}
|
|
753
|
+
return { value, end: text.length, complete: false };
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
/** The string value of property `key` in `text`, so far: its escapes decoded, `complete` once its quote closed. */
|
|
757
|
+
const stringProperty = (
|
|
758
|
+
text: string,
|
|
759
|
+
key: string,
|
|
760
|
+
): { value: string; complete: boolean } | undefined => {
|
|
761
|
+
let index = 0;
|
|
762
|
+
while (index < text.length) {
|
|
763
|
+
if (text[index] !== '"') {
|
|
764
|
+
index++;
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
const read = readString(text, index);
|
|
768
|
+
if (!read.complete) return undefined;
|
|
769
|
+
let after = read.end;
|
|
770
|
+
while (after < text.length && /\s/.test(text[after]!)) after++;
|
|
771
|
+
if (text[after] === ':' && read.value === key) {
|
|
772
|
+
let value = after + 1;
|
|
773
|
+
while (value < text.length && /\s/.test(text[value]!)) value++;
|
|
774
|
+
if (text[value] !== '"') return undefined;
|
|
775
|
+
return readString(text, value);
|
|
776
|
+
}
|
|
777
|
+
index = read.end;
|
|
778
|
+
}
|
|
779
|
+
return undefined;
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
/** A `show_view` call's input as the model writes it (research-036 streaming): the `program` so far with its JSON
|
|
783
|
+
* escapes decoded (`''` before it starts), and `title` once a complete one came. A tolerant scan, not `JSON.parse:` the
|
|
784
|
+
* text may be cut anywhere, even inside an escape. */
|
|
785
|
+
export function draftProgram(text: string): { title?: string; program: string } {
|
|
786
|
+
const program = stringProperty(text, 'program');
|
|
787
|
+
const title = stringProperty(text, 'title');
|
|
788
|
+
return {
|
|
789
|
+
...(title?.complete && { title: title.value }),
|
|
790
|
+
program: program?.value ?? '',
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/** Merge a patch into the shown program: named statements replace, `null` deletes, unreachable ones are dropped. */
|
|
795
|
+
export function apply(base: string, patch: string): string {
|
|
796
|
+
return mergeStatements(base, patch);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
/** One Query() a preview ran, with what it answered. */
|
|
800
|
+
export interface PreviewQuery {
|
|
801
|
+
statement: string;
|
|
802
|
+
plugin?: string;
|
|
803
|
+
path?: string;
|
|
804
|
+
ok: boolean;
|
|
805
|
+
error?: string;
|
|
806
|
+
count?: number;
|
|
807
|
+
fields?: string[];
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/** What a view draws: each query's outcome, the data kinds drawn, and the fields a guessed name left empty. */
|
|
811
|
+
export interface Preview {
|
|
812
|
+
queries: PreviewQuery[];
|
|
813
|
+
drawn: string[];
|
|
814
|
+
warnings: string[];
|
|
815
|
+
/** Each successful query's answer by statement id (research-036 P5): a frozen view draws from it, no route called. */
|
|
816
|
+
snapshot?: Record<string, unknown>;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/** A value that draws nothing: null, a blank or null/undefined word, an empty list, or an empty element. */
|
|
820
|
+
const EMPTY_TEXT = /^(\s*(null|undefined))*\s*$/;
|
|
821
|
+
|
|
822
|
+
function isElement(value: unknown): value is ElementNode {
|
|
823
|
+
return (
|
|
824
|
+
typeof value === 'object' && value !== null && (value as { type?: unknown }).type === 'element'
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
function isBlank(text: string): boolean {
|
|
829
|
+
return EMPTY_TEXT.test(text);
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
/** The first text a value draws, or null when its text, label, title and children all draw nothing. */
|
|
833
|
+
function textOf(value: unknown): string | null {
|
|
834
|
+
if (typeof value === 'string') return isBlank(value) ? null : value;
|
|
835
|
+
if (typeof value === 'number') return String(value);
|
|
836
|
+
if (Array.isArray(value)) {
|
|
837
|
+
for (const item of value) {
|
|
838
|
+
const text = textOf(item);
|
|
839
|
+
if (text !== null) return text;
|
|
840
|
+
}
|
|
841
|
+
return null;
|
|
842
|
+
}
|
|
843
|
+
if (isElement(value)) {
|
|
844
|
+
for (const key of ['text', 'label', 'title']) {
|
|
845
|
+
const text = textOf(value.props[key]);
|
|
846
|
+
if (text !== null) return text;
|
|
847
|
+
}
|
|
848
|
+
for (const child of Object.values(value.props)) {
|
|
849
|
+
const text = textOf(child);
|
|
850
|
+
if (text !== null) return text;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
return null;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
function isEmpty(value: unknown): boolean {
|
|
857
|
+
if (value === null || value === undefined) return true;
|
|
858
|
+
if (typeof value === 'string') return isBlank(value);
|
|
859
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
860
|
+
if (isElement(value)) return textOf(value) === null;
|
|
861
|
+
return false;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/** The size and first item's fields of an array or an `{ items: [...] }` answer; null for any other shape. */
|
|
865
|
+
function shapeOf(value: unknown): { count: number; fields?: string[] } | null {
|
|
866
|
+
let items: unknown[];
|
|
867
|
+
if (Array.isArray(value)) {
|
|
868
|
+
items = value;
|
|
869
|
+
} else if (
|
|
870
|
+
value !== null &&
|
|
871
|
+
typeof value === 'object' &&
|
|
872
|
+
Array.isArray((value as { items?: unknown }).items)
|
|
873
|
+
) {
|
|
874
|
+
items = (value as { items: unknown[] }).items;
|
|
875
|
+
} else {
|
|
876
|
+
return null;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
const first = items[0];
|
|
880
|
+
if (first !== null && typeof first === 'object' && !Array.isArray(first)) {
|
|
881
|
+
return { count: items.length, fields: Object.keys(first) };
|
|
882
|
+
}
|
|
883
|
+
return { count: items.length };
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/** Wait for a read, but give up with a "timed out" error after `timeoutMs`. */
|
|
887
|
+
function withTimeout(promise: Promise<unknown>, timeoutMs: number): Promise<unknown> {
|
|
888
|
+
return new Promise((resolve, reject) => {
|
|
889
|
+
const timer = setTimeout(
|
|
890
|
+
() => reject(new Error(`Query timed out after ${timeoutMs}ms`)),
|
|
891
|
+
timeoutMs,
|
|
892
|
+
);
|
|
893
|
+
promise.then(
|
|
894
|
+
(value) => {
|
|
895
|
+
clearTimeout(timer);
|
|
896
|
+
resolve(value);
|
|
897
|
+
},
|
|
898
|
+
(error: unknown) => {
|
|
899
|
+
clearTimeout(timer);
|
|
900
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
901
|
+
},
|
|
902
|
+
);
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/** Walk a drawn tree, noting the tables and lists it draws and the fields it leaves empty. */
|
|
907
|
+
function walk(node: ElementNode, out: { drawn: string[]; warnings: string[] }): void {
|
|
908
|
+
if (node.typeName === 'Table') {
|
|
909
|
+
const columns = listOf(node.props.columns);
|
|
910
|
+
const rows = listOf(node.props.rows);
|
|
911
|
+
out.drawn.push(`Table: ${rows.length} ${rows.length === 1 ? 'row' : 'rows'}`);
|
|
912
|
+
columns.forEach((column, index) => {
|
|
913
|
+
if (!isElement(column)) return;
|
|
914
|
+
const label = String(column.props.label ?? '');
|
|
915
|
+
let empty = 0;
|
|
916
|
+
for (const row of rows) {
|
|
917
|
+
const cell = listOf(row)[index];
|
|
918
|
+
if (isEmpty(cell)) empty += 1;
|
|
919
|
+
}
|
|
920
|
+
if (empty > 0) {
|
|
921
|
+
out.warnings.push(
|
|
922
|
+
`Table column "${label}" is empty in ${empty} of ${rows.length} rows: a field missing from its query's fields is a wrong name; a listed one is data the records lack.`,
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
} else if (node.typeName === 'List') {
|
|
927
|
+
const items = listOf(node.props.items);
|
|
928
|
+
out.drawn.push(`List: ${items.length} ${items.length === 1 ? 'item' : 'items'}`);
|
|
929
|
+
} else if (node.typeName === 'Properties') {
|
|
930
|
+
const items = listOf(node.props.items);
|
|
931
|
+
for (const item of items) {
|
|
932
|
+
if (!isElement(item) || !isEmpty(item.props.value)) continue;
|
|
933
|
+
const label = String(item.props.label ?? '');
|
|
934
|
+
out.warnings.push(
|
|
935
|
+
`Property "${label}" is drawn empty: a field missing from its query's fields is a wrong name; a listed one is data the records lack.`,
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
for (const value of Object.values(node.props)) walkValue(value, out);
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
function walkValue(value: unknown, out: { drawn: string[]; warnings: string[] }): void {
|
|
944
|
+
if (isElement(value)) walk(value, out);
|
|
945
|
+
else if (Array.isArray(value)) for (const item of value) walkValue(item, out);
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
/** What a preview may wait for a query, in ms, and the plugin components the view may use. */
|
|
949
|
+
interface PreviewOptions {
|
|
950
|
+
timeoutMs?: number;
|
|
951
|
+
components?: PluginComponent[];
|
|
952
|
+
}
|
|
953
|
+
type PreviewRead = (
|
|
954
|
+
plugin: string,
|
|
955
|
+
path: string,
|
|
956
|
+
query?: Record<string, string>,
|
|
957
|
+
) => Promise<unknown>;
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* Run the reads a view makes, then evaluate it without a page and say what it draws.
|
|
961
|
+
*
|
|
962
|
+
* Reads run in parallel, each with its state defaults and a timeout; mutations never run. Every answer names the
|
|
963
|
+
* queries' outcomes, the tables and lists drawn, and the cells or properties a guessed field left empty.
|
|
964
|
+
*/
|
|
965
|
+
export async function preview(
|
|
966
|
+
program: string,
|
|
967
|
+
read: PreviewRead,
|
|
968
|
+
options?: PreviewOptions,
|
|
969
|
+
): Promise<Preview> {
|
|
970
|
+
const components = options?.components ?? [];
|
|
971
|
+
const timeoutMs = options?.timeoutMs ?? 5000;
|
|
972
|
+
|
|
973
|
+
let result: ParseResult;
|
|
974
|
+
try {
|
|
975
|
+
result = parserFor(components).parse(program);
|
|
976
|
+
} catch {
|
|
977
|
+
return { queries: [], drawn: [], warnings: [] };
|
|
978
|
+
}
|
|
979
|
+
if (!result.root) return { queries: [], drawn: [], warnings: [] };
|
|
980
|
+
|
|
981
|
+
const store = createStore();
|
|
982
|
+
store.initialize(result.stateDeclarations, {});
|
|
983
|
+
|
|
984
|
+
const data = new Map<string, unknown>();
|
|
985
|
+
// What each successful read answered, kept for a frozen view (research-036 P5); a failed one is left out.
|
|
986
|
+
const snapshot: Record<string, unknown> = {};
|
|
987
|
+
const ctx: EvaluationContext = {
|
|
988
|
+
getState: (name) => store.get(name),
|
|
989
|
+
resolveRef: (name) => data.get(name) ?? null,
|
|
990
|
+
};
|
|
991
|
+
|
|
992
|
+
// Read each query's tool and arguments before any read starts, so all of them see the same state.
|
|
993
|
+
const pending: {
|
|
994
|
+
statement: string;
|
|
995
|
+
plugin: string;
|
|
996
|
+
path: string;
|
|
997
|
+
query?: Record<string, string>;
|
|
998
|
+
defaults: unknown;
|
|
999
|
+
}[] = [];
|
|
1000
|
+
for (const statement of result.queryStatements) {
|
|
1001
|
+
let toolName = '';
|
|
1002
|
+
try {
|
|
1003
|
+
toolName = statement.toolAST ? String(evaluate(statement.toolAST, ctx) ?? '') : '';
|
|
1004
|
+
} catch {
|
|
1005
|
+
toolName = '';
|
|
1006
|
+
}
|
|
1007
|
+
if (toolName !== 'bridge_read') continue;
|
|
1008
|
+
|
|
1009
|
+
let args: Record<string, unknown> = {};
|
|
1010
|
+
try {
|
|
1011
|
+
const evaluated = statement.argsAST ? evaluate(statement.argsAST, ctx) : null;
|
|
1012
|
+
if (evaluated !== null && typeof evaluated === 'object') {
|
|
1013
|
+
args = evaluated as Record<string, unknown>;
|
|
1014
|
+
}
|
|
1015
|
+
} catch {
|
|
1016
|
+
args = {};
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
pending.push({
|
|
1020
|
+
statement: statement.statementId,
|
|
1021
|
+
plugin: String(args.plugin ?? ''),
|
|
1022
|
+
path: String(args.path ?? ''),
|
|
1023
|
+
...(args.query !== undefined ? { query: args.query as Record<string, string> } : {}),
|
|
1024
|
+
defaults: statement.defaultsAST ? evaluate(statement.defaultsAST, ctx) : null,
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
const queries = await Promise.all(
|
|
1029
|
+
pending.map(async (each): Promise<PreviewQuery> => {
|
|
1030
|
+
let value: unknown;
|
|
1031
|
+
try {
|
|
1032
|
+
value = await withTimeout(
|
|
1033
|
+
each.query === undefined
|
|
1034
|
+
? read(each.plugin, each.path)
|
|
1035
|
+
: read(each.plugin, each.path, each.query),
|
|
1036
|
+
timeoutMs,
|
|
1037
|
+
);
|
|
1038
|
+
} catch (error) {
|
|
1039
|
+
data.set(each.statement, each.defaults);
|
|
1040
|
+
return {
|
|
1041
|
+
statement: each.statement,
|
|
1042
|
+
plugin: each.plugin,
|
|
1043
|
+
path: each.path,
|
|
1044
|
+
ok: false,
|
|
1045
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
data.set(each.statement, value);
|
|
1050
|
+
snapshot[each.statement] = value;
|
|
1051
|
+
const record: PreviewQuery = {
|
|
1052
|
+
statement: each.statement,
|
|
1053
|
+
plugin: each.plugin,
|
|
1054
|
+
path: each.path,
|
|
1055
|
+
ok: true,
|
|
1056
|
+
};
|
|
1057
|
+
const shape = shapeOf(value);
|
|
1058
|
+
if (shape !== null) {
|
|
1059
|
+
record.count = shape.count;
|
|
1060
|
+
if (shape.fields !== undefined) record.fields = shape.fields;
|
|
1061
|
+
}
|
|
1062
|
+
return record;
|
|
1063
|
+
}),
|
|
1064
|
+
);
|
|
1065
|
+
|
|
1066
|
+
const errors: OpenUIError[] = [];
|
|
1067
|
+
const tree = evaluateRoot(result, { ctx, library: libraryFor(components), store, errors });
|
|
1068
|
+
|
|
1069
|
+
const drawn: string[] = [];
|
|
1070
|
+
const warnings: string[] = [];
|
|
1071
|
+
if (tree !== null) walk(tree, { drawn, warnings });
|
|
1072
|
+
return {
|
|
1073
|
+
queries,
|
|
1074
|
+
drawn,
|
|
1075
|
+
warnings,
|
|
1076
|
+
...(Object.keys(snapshot).length > 0 && { snapshot }),
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
const EXAMPLES = [
|
|
1081
|
+
`root = Card([h, t])
|
|
1082
|
+
h = Heading("Monthly revenue")
|
|
1083
|
+
t = Text("Up 12% on last month")`,
|
|
1084
|
+
`$q = ""
|
|
1085
|
+
people = Query("bridge_read", {plugin: "contacts", path: "/contacts", query: {text: $q}}, {items: []})
|
|
1086
|
+
root = Stack([search, list])
|
|
1087
|
+
search = Input("q", "Search contacts", "text", null, $q)
|
|
1088
|
+
list = List(@Each(people.items, "p", ListItem(p.name)))`,
|
|
1089
|
+
`$name = ""
|
|
1090
|
+
save = Mutation("bridge_write", {plugin: "contacts", method: "POST", path: "/contacts", body: {name: $name}})
|
|
1091
|
+
root = Form("contact", [field], Buttons([Button("Save", Action([@Run(save), @Reset($name)]), "primary")]))
|
|
1092
|
+
field = Field("Name", Input("name", "Full name", "text", null, $name))`,
|
|
1093
|
+
];
|
|
1094
|
+
|
|
1095
|
+
/**
|
|
1096
|
+
* The language the agent writes a view in.
|
|
1097
|
+
*
|
|
1098
|
+
* It is the language of the `program` argument of the `show_view` tool, not the text of the reply, and it calls
|
|
1099
|
+
* Bridge's own tools with the same arguments as bridge_read and bridge_write.
|
|
1100
|
+
*/
|
|
1101
|
+
export function language(components: PluginComponent[] = []): string {
|
|
1102
|
+
return libraryFor(components).prompt({
|
|
1103
|
+
preamble:
|
|
1104
|
+
'You compose a view in OpenUI Lang. The text is the value of the `program` argument of the `show_view` tool, not the whole reply: keep the reply itself to a sentence or two, and put only this language in the program argument.',
|
|
1105
|
+
additionalRules: [
|
|
1106
|
+
'Read data with Query("bridge_read", {plugin, path, query}, defaults) and write with Mutation("bridge_write", {plugin, method, path, body, query}). They take the same arguments as the Bridge MCP tools bridge_read and bridge_write; call bridge_routes first when you do not know a plugin’s routes. A form’s button that writes ends with @Reset of its fields, so the next entry starts empty (the button says Saved, or the refusal, by itself).',
|
|
1107
|
+
'Never guess a field or a query parameter: before you write the view, call bridge_read once on the same route (with take: "1" when it pages) and use exactly the field names of the item it answers, and the query parameters bridge_routes gives. A guessed name draws an empty cell.',
|
|
1108
|
+
'Values that are lists (tags, emails) are drawn whole; pick one element with an index (p.emails[0].value) or a field of each (p.tags).',
|
|
1109
|
+
'Run a query again with @Run(queryName); a Button’s action can run a mutation with @Run(mutationName). Give every Query a defaults value so the view draws before the data arrives.',
|
|
1110
|
+
'To open a page of this workspace (a contact’s card), a Button’s action is @OpenUrl with the address bridge_link gives: the button is a link, Bridge shows that page in place (⌘-click: in a window beside), in a frozen view too. Another site’s address opens in a new tab.',
|
|
1111
|
+
'To change a view that is already shown, send only the statements that change, with `base` set to the seq of the shown view. A statement set to null is deleted; names you do not send are kept.',
|
|
1112
|
+
'Keep the root first: root = Stack([...]) or root = Card([...], "Title"). Use Heading and Text for prose, Table for rows, Properties for one record, and List for a set of records.',
|
|
1113
|
+
],
|
|
1114
|
+
examples: EXAMPLES,
|
|
1115
|
+
});
|
|
1116
|
+
}
|