@zhama/a2ui-core 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.
@@ -1,445 +1,3 @@
1
- // src/builders/id-generator.ts
2
- var componentIdCounter = 0;
3
- function generateId(prefix = "comp") {
4
- return `${prefix}_${Date.now()}_${componentIdCounter++}`;
5
- }
6
- function resetIdCounter() {
7
- componentIdCounter = 0;
8
- }
9
- function getIdCounter() {
10
- return componentIdCounter;
11
- }
12
-
13
- // src/builders/component-builder.ts
14
- function text(content, options = {}) {
15
- const { id = generateId("text"), weight, usageHint } = options;
16
- return {
17
- id,
18
- component: "Text",
19
- text: content,
20
- ...weight !== void 0 && { weight },
21
- ...usageHint && { usageHint }
22
- };
23
- }
24
- function image(url, options = {}) {
25
- const { id = generateId("image"), weight, fit, usageHint } = options;
26
- return {
27
- id,
28
- component: "Image",
29
- url,
30
- ...weight !== void 0 && { weight },
31
- ...fit && { fit },
32
- ...usageHint && { usageHint }
33
- };
34
- }
35
- function icon(name, options = {}) {
36
- const { id = generateId("icon"), weight } = options;
37
- return {
38
- id,
39
- component: "Icon",
40
- name,
41
- ...weight !== void 0 && { weight }
42
- };
43
- }
44
- function video(url, options = {}) {
45
- const { id = generateId("video"), weight } = options;
46
- return {
47
- id,
48
- component: "Video",
49
- url,
50
- ...weight !== void 0 && { weight }
51
- };
52
- }
53
- function audioPlayer(url, options = {}) {
54
- const { id = generateId("audio"), weight, description } = options;
55
- return {
56
- id,
57
- component: "AudioPlayer",
58
- url,
59
- ...weight !== void 0 && { weight },
60
- ...description && { description }
61
- };
62
- }
63
- function row(children, options = {}) {
64
- const { id = generateId("row"), weight, alignment, distribution } = options;
65
- return {
66
- id,
67
- component: "Row",
68
- children,
69
- ...weight !== void 0 && { weight },
70
- ...alignment && { alignment },
71
- ...distribution && { distribution }
72
- };
73
- }
74
- function column(children, options = {}) {
75
- const { id = generateId("column"), weight, alignment, distribution } = options;
76
- return {
77
- id,
78
- component: "Column",
79
- children,
80
- ...weight !== void 0 && { weight },
81
- ...alignment && { alignment },
82
- ...distribution && { distribution }
83
- };
84
- }
85
- function list(children, options = {}) {
86
- const { id = generateId("list"), weight, direction, alignment } = options;
87
- return {
88
- id,
89
- component: "List",
90
- children,
91
- ...weight !== void 0 && { weight },
92
- ...direction && { direction },
93
- ...alignment && { alignment }
94
- };
95
- }
96
- function card(childId, options = {}) {
97
- const { id = generateId("card"), weight } = options;
98
- return {
99
- id,
100
- component: "Card",
101
- child: childId,
102
- ...weight !== void 0 && { weight }
103
- };
104
- }
105
- function tabs(items, options = {}) {
106
- const { id = generateId("tabs"), weight } = options;
107
- return {
108
- id,
109
- component: "Tabs",
110
- tabItems: items.map((item) => ({
111
- title: item.title,
112
- child: item.childId
113
- })),
114
- ...weight !== void 0 && { weight }
115
- };
116
- }
117
- function divider(options = {}) {
118
- const { id = generateId("divider"), weight, axis } = options;
119
- return {
120
- id,
121
- component: "Divider",
122
- ...weight !== void 0 && { weight },
123
- ...axis && { axis }
124
- };
125
- }
126
- function modal(entryPointChildId, contentChildId, options = {}) {
127
- const { id = generateId("modal"), weight } = options;
128
- return {
129
- id,
130
- component: "Modal",
131
- entryPointChild: entryPointChildId,
132
- contentChild: contentChildId,
133
- ...weight !== void 0 && { weight }
134
- };
135
- }
136
- function button(childId, action, options = {}) {
137
- const { id = generateId("button"), weight, primary } = options;
138
- return {
139
- id,
140
- component: "Button",
141
- child: childId,
142
- action,
143
- ...weight !== void 0 && { weight },
144
- ...primary !== void 0 && { primary }
145
- };
146
- }
147
- function checkbox(label, value, options = {}) {
148
- const { id = generateId("checkbox"), weight } = options;
149
- return {
150
- id,
151
- component: "CheckBox",
152
- label,
153
- value,
154
- ...weight !== void 0 && { weight }
155
- };
156
- }
157
- function textField(label, textValue, options = {}) {
158
- const { id = generateId("textfield"), weight, usageHint, validationRegexp } = options;
159
- return {
160
- id,
161
- component: "TextField",
162
- label,
163
- ...textValue !== void 0 && { text: textValue },
164
- ...weight !== void 0 && { weight },
165
- ...usageHint && { usageHint },
166
- ...validationRegexp && { validationRegexp }
167
- };
168
- }
169
- function dateTimeInput(value, options = {}) {
170
- const { id = generateId("datetime"), weight, enableDate, enableTime, outputFormat, label } = options;
171
- return {
172
- id,
173
- component: "DateTimeInput",
174
- value,
175
- ...weight !== void 0 && { weight },
176
- ...enableDate !== void 0 && { enableDate },
177
- ...enableTime !== void 0 && { enableTime },
178
- ...outputFormat && { outputFormat },
179
- ...label && { label }
180
- };
181
- }
182
- function choicePicker(optionsList, value, usageHint, options = {}) {
183
- const { id = generateId("choice"), weight, label } = options;
184
- return {
185
- id,
186
- component: "ChoicePicker",
187
- options: optionsList,
188
- value,
189
- usageHint,
190
- ...weight !== void 0 && { weight },
191
- ...label && { label }
192
- };
193
- }
194
- function slider(value, options = {}) {
195
- const { id = generateId("slider"), weight, label, min, max } = options;
196
- return {
197
- id,
198
- component: "Slider",
199
- value,
200
- ...weight !== void 0 && { weight },
201
- ...label && { label },
202
- ...min !== void 0 && { min },
203
- ...max !== void 0 && { max }
204
- };
205
- }
206
- function textButton(buttonText, action, options = {}) {
207
- const textId = options.textId ?? generateId("btn_text");
208
- const textComp = text(buttonText, { id: textId });
209
- const buttonComp = button(textId, action, options);
210
- return [textComp, buttonComp];
211
- }
212
- function h1(content, options = {}) {
213
- return text(content, { ...options, usageHint: "h1" });
214
- }
215
- function h2(content, options = {}) {
216
- return text(content, { ...options, usageHint: "h2" });
217
- }
218
- function h3(content, options = {}) {
219
- return text(content, { ...options, usageHint: "h3" });
220
- }
221
- function h4(content, options = {}) {
222
- return text(content, { ...options, usageHint: "h4" });
223
- }
224
- function h5(content, options = {}) {
225
- return text(content, { ...options, usageHint: "h5" });
226
- }
227
- function caption(content, options = {}) {
228
- return text(content, { ...options, usageHint: "caption" });
229
- }
230
- function body(content, options = {}) {
231
- return text(content, { ...options, usageHint: "body" });
232
- }
233
-
234
- // src/types/messages.ts
235
- var STANDARD_CATALOG_ID = "https://a2ui.dev/specification/0.9/standard_catalog_definition.json";
236
-
237
- // src/builders/data-model-builder.ts
238
- var DEFAULT_PATH_MAPPINGS = {};
239
- function objectToValueMap(obj, prefix = "") {
240
- const entries = [];
241
- for (const [key, value] of Object.entries(obj)) {
242
- const fullKey = prefix ? `${prefix}/${key}` : `/${key}`;
243
- entries.push(valueToValueMap(fullKey, value));
244
- }
245
- return entries;
246
- }
247
- function valueToValueMap(key, value) {
248
- if (value === null || value === void 0) {
249
- return { key, valueString: "" };
250
- }
251
- if (typeof value === "string") {
252
- return { key, valueString: value };
253
- }
254
- if (typeof value === "number") {
255
- return { key, valueNumber: value };
256
- }
257
- if (typeof value === "boolean") {
258
- return { key, valueBoolean: value };
259
- }
260
- if (Array.isArray(value)) {
261
- return {
262
- key,
263
- valueMap: value.map((item, index) => valueToValueMap(String(index), item))
264
- };
265
- }
266
- if (typeof value === "object") {
267
- const nestedMaps = [];
268
- for (const [k, v] of Object.entries(value)) {
269
- nestedMaps.push(valueToValueMap(k, v));
270
- }
271
- return { key, valueMap: nestedMaps };
272
- }
273
- return { key, valueString: String(value) };
274
- }
275
- function normalizePath(path, pathMappings = {}) {
276
- let normalizedPath = path.replace(/\./g, "/");
277
- if (!normalizedPath.startsWith("/")) {
278
- normalizedPath = `/${normalizedPath}`;
279
- }
280
- for (const [from, to] of Object.entries(pathMappings)) {
281
- const fromPattern = new RegExp(`^/${from}(/|$)`);
282
- if (fromPattern.test(normalizedPath)) {
283
- normalizedPath = normalizedPath.replace(fromPattern, `/${to}$1`);
284
- }
285
- }
286
- return normalizedPath;
287
- }
288
- function updatesToValueMap(updates, basePath = "", pathMappings = DEFAULT_PATH_MAPPINGS) {
289
- const entries = [];
290
- for (const update of updates) {
291
- const rawPath = update.path.startsWith("/") ? update.path : `${basePath}/${update.path}`;
292
- const normalizedPath = normalizePath(rawPath, pathMappings);
293
- if (update.value !== null && typeof update.value === "object" && !Array.isArray(update.value)) {
294
- const flattenedEntries = flattenObjectToValueMap(
295
- update.value,
296
- normalizedPath
297
- );
298
- entries.push(...flattenedEntries);
299
- } else {
300
- entries.push(valueToValueMap(normalizedPath, update.value));
301
- }
302
- }
303
- return entries;
304
- }
305
- function flattenObjectToValueMap(obj, basePath) {
306
- const entries = [];
307
- for (const [key, value] of Object.entries(obj)) {
308
- const fullPath = `${basePath}/${key}`;
309
- if (value !== null && typeof value === "object" && !Array.isArray(value)) {
310
- const nestedEntries = flattenObjectToValueMap(value, fullPath);
311
- entries.push(...nestedEntries);
312
- } else {
313
- entries.push(valueToValueMap(fullPath, value));
314
- }
315
- }
316
- return entries;
317
- }
318
- function valueMapToObject(valueMaps) {
319
- const result = {};
320
- for (const valueMap of valueMaps) {
321
- const key = valueMap.key.startsWith("/") ? valueMap.key.slice(1) : valueMap.key;
322
- if (valueMap.valueString !== void 0) {
323
- result[key] = valueMap.valueString;
324
- } else if (valueMap.valueNumber !== void 0) {
325
- result[key] = valueMap.valueNumber;
326
- } else if (valueMap.valueBoolean !== void 0) {
327
- result[key] = valueMap.valueBoolean;
328
- } else if (valueMap.valueMap !== void 0) {
329
- result[key] = valueMapToObject(valueMap.valueMap);
330
- }
331
- }
332
- return result;
333
- }
334
-
335
- // src/builders/message-builder.ts
336
- function createSurface(surfaceId, catalogId = STANDARD_CATALOG_ID) {
337
- return {
338
- createSurface: {
339
- surfaceId,
340
- catalogId
341
- }
342
- };
343
- }
344
- function updateComponents(surfaceId, components) {
345
- return {
346
- updateComponents: {
347
- surfaceId,
348
- components
349
- }
350
- };
351
- }
352
- function updateDataModel(surfaceId, value, path, op = "replace") {
353
- return {
354
- updateDataModel: {
355
- surfaceId,
356
- ...path && { path },
357
- op,
358
- ...op !== "remove" && { value }
359
- }
360
- };
361
- }
362
- function deleteSurface(surfaceId) {
363
- return {
364
- deleteSurface: {
365
- surfaceId
366
- }
367
- };
368
- }
369
- function createV09Messages(options) {
370
- const { surfaceId, catalogId = STANDARD_CATALOG_ID, components, dataModel } = options;
371
- const messages = [
372
- createSurface(surfaceId, catalogId),
373
- updateComponents(surfaceId, components)
374
- ];
375
- if (dataModel) {
376
- messages.push(updateDataModel(surfaceId, dataModel));
377
- }
378
- return messages;
379
- }
380
- function beginRendering(rootId, surfaceId = "@default", styles) {
381
- return {
382
- beginRendering: {
383
- surfaceId,
384
- root: rootId,
385
- ...styles && { styles }
386
- }
387
- };
388
- }
389
- function surfaceUpdate(components, surfaceId = "@default") {
390
- return {
391
- surfaceUpdate: {
392
- surfaceId,
393
- components
394
- }
395
- };
396
- }
397
- function dataModelUpdate(contents, surfaceId = "@default", path) {
398
- return {
399
- dataModelUpdate: {
400
- surfaceId,
401
- contents,
402
- ...path && { path }
403
- }
404
- };
405
- }
406
- function dataModelInit(data, surfaceId = "@default") {
407
- return dataModelUpdate(objectToValueMap(data), surfaceId);
408
- }
409
- function pathUpdate(path, value, surfaceId = "@default") {
410
- return {
411
- dataModelUpdate: {
412
- surfaceId,
413
- path,
414
- contents: [valueToValueMap("", value)]
415
- }
416
- };
417
- }
418
- function deleteSurfaceV08(surfaceId) {
419
- return {
420
- deleteSurface: {
421
- surfaceId
422
- }
423
- };
424
- }
425
- function createV08Messages(options) {
426
- const { rootId, components, dataModel, surfaceId = "@default", styles } = options;
427
- const messages = [
428
- surfaceUpdate(components, surfaceId)
429
- ];
430
- if (dataModel) {
431
- messages.push(dataModelInit(dataModel, surfaceId));
432
- }
433
- messages.push(beginRendering(rootId, surfaceId, styles));
434
- return messages;
435
- }
436
- function messagesToJsonl(messages) {
437
- return messages.map((msg) => JSON.stringify(msg)).join("\n");
438
- }
439
- function jsonlToMessages(jsonl) {
440
- return jsonl.split("\n").filter((line) => line.trim()).map((line) => JSON.parse(line));
441
- }
442
-
443
- export { DEFAULT_PATH_MAPPINGS, audioPlayer, beginRendering, body, button, caption, card, checkbox, choicePicker, column, createSurface, createV08Messages, createV09Messages, dataModelInit, dataModelUpdate, dateTimeInput, deleteSurface, deleteSurfaceV08, divider, flattenObjectToValueMap, generateId, getIdCounter, h1, h2, h3, h4, h5, icon, image, jsonlToMessages, list, messagesToJsonl, modal, normalizePath, objectToValueMap, pathUpdate, resetIdCounter, row, slider, surfaceUpdate, tabs, text, textButton, textField, updateComponents, updateDataModel, updatesToValueMap, valueMapToObject, valueToValueMap, video };
444
- //# sourceMappingURL=index.js.map
445
- //# sourceMappingURL=index.js.map
1
+ var g=0;function i(t="comp"){return `${t}_${Date.now()}_${g++}`}function V(){g=0;}function D(){return g}function p(t,e={}){let{id:n=i("text"),weight:o,usageHint:a}=e;return {id:n,component:"Text",text:t,...o!==void 0&&{weight:o},...a&&{usageHint:a}}}function v(t,e={}){let{id:n=i("image"),weight:o,fit:a,usageHint:r}=e;return {id:n,component:"Image",url:t,...o!==void 0&&{weight:o},...a&&{fit:a},...r&&{usageHint:r}}}function P(t,e={}){let{id:n=i("icon"),weight:o}=e;return {id:n,component:"Icon",name:t,...o!==void 0&&{weight:o}}}function A(t,e={}){let{id:n=i("video"),weight:o}=e;return {id:n,component:"Video",url:t,...o!==void 0&&{weight:o}}}function U(t,e={}){let{id:n=i("audio"),weight:o,description:a}=e;return {id:n,component:"AudioPlayer",url:t,...o!==void 0&&{weight:o},...a&&{description:a}}}function w(t,e={}){let{id:n=i("row"),weight:o,alignment:a,distribution:r}=e;return {id:n,component:"Row",children:t,...o!==void 0&&{weight:o},...a&&{alignment:a},...r&&{distribution:r}}}function j(t,e={}){let{id:n=i("column"),weight:o,alignment:a,distribution:r}=e;return {id:n,component:"Column",children:t,...o!==void 0&&{weight:o},...a&&{alignment:a},...r&&{distribution:r}}}function R(t,e={}){let{id:n=i("list"),weight:o,direction:a,alignment:r}=e;return {id:n,component:"List",children:t,...o!==void 0&&{weight:o},...a&&{direction:a},...r&&{alignment:r}}}function _(t,e={}){let{id:n=i("card"),weight:o}=e;return {id:n,component:"Card",child:t,...o!==void 0&&{weight:o}}}function k(t,e={}){let{id:n=i("tabs"),weight:o}=e;return {id:n,component:"Tabs",tabItems:t.map(a=>({title:a.title,child:a.childId})),...o!==void 0&&{weight:o}}}function B(t={}){let{id:e=i("divider"),weight:n,axis:o}=t;return {id:e,component:"Divider",...n!==void 0&&{weight:n},...o&&{axis:o}}}function H(t,e,n={}){let{id:o=i("modal"),weight:a}=n;return {id:o,component:"Modal",entryPointChild:t,contentChild:e,...a!==void 0&&{weight:a}}}function f(t,e,n={}){let{id:o=i("button"),weight:a,primary:r}=n;return {id:o,component:"Button",child:t,action:e,...a!==void 0&&{weight:a},...r!==void 0&&{primary:r}}}function N(t,e,n={}){let{id:o=i("checkbox"),weight:a}=n;return {id:o,component:"CheckBox",label:t,value:e,...a!==void 0&&{weight:a}}}function E(t,e,n={}){let{id:o=i("textfield"),weight:a,usageHint:r,validationRegexp:s}=n;return {id:o,component:"TextField",label:t,...e!==void 0&&{text:e},...a!==void 0&&{weight:a},...r&&{usageHint:r},...s&&{validationRegexp:s}}}function L(t,e={}){let{id:n=i("datetime"),weight:o,enableDate:a,enableTime:r,outputFormat:s,label:c}=e;return {id:n,component:"DateTimeInput",value:t,...o!==void 0&&{weight:o},...a!==void 0&&{enableDate:a},...r!==void 0&&{enableTime:r},...s&&{outputFormat:s},...c&&{label:c}}}function $(t,e,n,o={}){let{id:a=i("choice"),weight:r,label:s}=o;return {id:a,component:"ChoicePicker",options:t,value:e,usageHint:n,...r!==void 0&&{weight:r},...s&&{label:s}}}function F(t,e={}){let{id:n=i("slider"),weight:o,label:a,min:r,max:s}=e;return {id:n,component:"Slider",value:t,...o!==void 0&&{weight:o},...a&&{label:a},...r!==void 0&&{min:r},...s!==void 0&&{max:s}}}function G(t,e,n={}){let o=n.textId??i("btn_text"),a=p(t,{id:o}),r=f(o,e,n);return [a,r]}function z(t,e={}){return p(t,{...e,usageHint:"h1"})}function J(t,e={}){return p(t,{...e,usageHint:"h2"})}function X(t,e={}){return p(t,{...e,usageHint:"h3"})}function W(t,e={}){return p(t,{...e,usageHint:"h4"})}function Y(t,e={}){return p(t,{...e,usageHint:"h5"})}function K(t,e={}){return p(t,{...e,usageHint:"caption"})}function q(t,e={}){return p(t,{...e,usageHint:"body"})}var d="https://a2ui.dev/specification/0.9/standard_catalog_definition.json";var C={};function l(t,e=""){let n=[];for(let[o,a]of Object.entries(t)){let r=e?`${e}/${o}`:`/${o}`;n.push(u(r,a));}return n}function u(t,e){if(e==null)return {key:t,valueString:""};if(typeof e=="string")return {key:t,valueString:e};if(typeof e=="number")return {key:t,valueNumber:e};if(typeof e=="boolean")return {key:t,valueBoolean:e};if(Array.isArray(e))return {key:t,valueMap:e.map((n,o)=>u(String(o),n))};if(typeof e=="object"){let n=[];for(let[o,a]of Object.entries(e))n.push(u(o,a));return {key:t,valueMap:n}}return {key:t,valueString:String(e)}}function x(t,e={}){let n=t.replace(/\./g,"/");n.startsWith("/")||(n=`/${n}`);for(let[o,a]of Object.entries(e)){let r=new RegExp(`^/${o}(/|$)`);r.test(n)&&(n=n.replace(r,`/${a}$1`));}return n}function Q(t,e="",n=C){let o=[];for(let a of t){let r=a.path.startsWith("/")?a.path:`${e}/${a.path}`,s=x(r,n);if(a.value!==null&&typeof a.value=="object"&&!Array.isArray(a.value)){let c=m(a.value,s);o.push(...c);}else o.push(u(s,a.value));}return o}function m(t,e){let n=[];for(let[o,a]of Object.entries(t)){let r=`${e}/${o}`;if(a!==null&&typeof a=="object"&&!Array.isArray(a)){let s=m(a,r);n.push(...s);}else n.push(u(r,a));}return n}function M(t){let e={};for(let n of t){let o=n.key.startsWith("/")?n.key.slice(1):n.key;n.valueString!==void 0?e[o]=n.valueString:n.valueNumber!==void 0?e[o]=n.valueNumber:n.valueBoolean!==void 0?e[o]=n.valueBoolean:n.valueMap!==void 0&&(e[o]=M(n.valueMap));}return e}function O(t,e=d){return {createSurface:{surfaceId:t,catalogId:e}}}function h(t,e){return {updateComponents:{surfaceId:t,components:e}}}function I(t,e,n,o="replace"){return {updateDataModel:{surfaceId:t,...n&&{path:n},op:o,...o!=="remove"&&{value:e}}}}function Z(t){return {deleteSurface:{surfaceId:t}}}function ee(t){let{surfaceId:e,catalogId:n=d,components:o,dataModel:a}=t,r=[O(e,n),h(e,o)];return a&&r.push(I(e,a)),r}function S(t,e="@default",n){return {beginRendering:{surfaceId:e,root:t,...n&&{styles:n}}}}function T(t,e="@default"){return {surfaceUpdate:{surfaceId:e,components:t}}}function y(t,e="@default",n){return {dataModelUpdate:{surfaceId:e,contents:t,...n&&{path:n}}}}function b(t,e="@default"){return y(l(t),e)}function te(t,e,n="@default"){return {dataModelUpdate:{surfaceId:n,path:t,contents:[u("",e)]}}}function ne(t){return {deleteSurface:{surfaceId:t}}}function oe(t){let{rootId:e,components:n,dataModel:o,surfaceId:a="@default",styles:r}=t,s=[T(n,a)];return o&&s.push(b(o,a)),s.push(S(e,a,r)),s}function ae(t){return t.map(e=>JSON.stringify(e)).join(`
2
+ `)}function re(t){return t.split(`
3
+ `).filter(e=>e.trim()).map(e=>JSON.parse(e))}export{C as DEFAULT_PATH_MAPPINGS,U as audioPlayer,S as beginRendering,q as body,f as button,K as caption,_ as card,N as checkbox,$ as choicePicker,j as column,O as createSurface,oe as createV08Messages,ee as createV09Messages,b as dataModelInit,y as dataModelUpdate,L as dateTimeInput,Z as deleteSurface,ne as deleteSurfaceV08,B as divider,m as flattenObjectToValueMap,i as generateId,D as getIdCounter,z as h1,J as h2,X as h3,W as h4,Y as h5,P as icon,v as image,re as jsonlToMessages,R as list,ae as messagesToJsonl,H as modal,x as normalizePath,l as objectToValueMap,te as pathUpdate,V as resetIdCounter,w as row,F as slider,T as surfaceUpdate,k as tabs,p as text,G as textButton,E as textField,h as updateComponents,I as updateDataModel,Q as updatesToValueMap,M as valueMapToObject,u as valueToValueMap,A as video};