@zhama/a2ui-core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,538 @@
1
+ import { TextComponent, ImageComponent, StringOrPath, RowComponent, ListComponent, DividerComponent, TextFieldComponent, ComponentInstance, ChildrenProperty, Action, BooleanOrPath, StringArrayOrPath, ChoicePickerComponent, NumberOrPath, CreateSurfaceMessage, UpdateComponentsMessage, UpdateDataModelMessage, DeleteSurfaceMessage, DataObject, ServerToClientMessageV09, BeginRenderingMessage, ComponentInstanceV08, SurfaceUpdateMessage, ValueMap, DataModelUpdateMessage, DataValue, ServerToClientMessageV08 } from '../types/index.js';
2
+
3
+ /**
4
+ * ID Generator
5
+ *
6
+ * 生成唯一的组件 ID
7
+ */
8
+ /**
9
+ * 生成唯一的组件 ID
10
+ *
11
+ * @param prefix - ID 前缀
12
+ * @returns 唯一的组件 ID
13
+ *
14
+ * @example
15
+ * generateId('text'); // 'text_1703123456789_0'
16
+ * generateId('btn'); // 'btn_1703123456789_1'
17
+ */
18
+ declare function generateId(prefix?: string): string;
19
+ /**
20
+ * 重置 ID 计数器
21
+ * 在渲染新场景时调用,确保 ID 从 0 开始
22
+ */
23
+ declare function resetIdCounter(): void;
24
+ /**
25
+ * 获取当前计数器值(用于测试)
26
+ */
27
+ declare function getIdCounter(): number;
28
+
29
+ /**
30
+ * Component Builder
31
+ *
32
+ * 提供 A2UI 组件定义的构建工具函数
33
+ * 用于创建符合 A2UI v0.9 协议的组件结构
34
+ */
35
+
36
+ /**
37
+ * 组件构建选项基类
38
+ */
39
+ interface ComponentOptions {
40
+ /** 组件 ID(可选,自动生成) */
41
+ id?: string;
42
+ /** 在 Row/Column 中的权重 */
43
+ weight?: number;
44
+ }
45
+ /**
46
+ * Text 组件选项
47
+ */
48
+ interface TextOptions extends ComponentOptions {
49
+ usageHint?: TextComponent['usageHint'];
50
+ }
51
+ /**
52
+ * Image 组件选项
53
+ */
54
+ interface ImageOptions extends ComponentOptions {
55
+ fit?: ImageComponent['fit'];
56
+ usageHint?: ImageComponent['usageHint'];
57
+ }
58
+ /**
59
+ * Icon 组件选项
60
+ */
61
+ interface IconOptions extends ComponentOptions {
62
+ }
63
+ /**
64
+ * Video 组件选项
65
+ */
66
+ interface VideoOptions extends ComponentOptions {
67
+ }
68
+ /**
69
+ * AudioPlayer 组件选项
70
+ */
71
+ interface AudioPlayerOptions extends ComponentOptions {
72
+ description?: StringOrPath;
73
+ }
74
+ /**
75
+ * 布局组件选项
76
+ */
77
+ interface LayoutOptions extends ComponentOptions {
78
+ alignment?: RowComponent['alignment'];
79
+ distribution?: RowComponent['distribution'];
80
+ }
81
+ /**
82
+ * List 组件选项
83
+ */
84
+ interface ListOptions extends ComponentOptions {
85
+ direction?: ListComponent['direction'];
86
+ alignment?: ListComponent['alignment'];
87
+ }
88
+ /**
89
+ * Card 组件选项
90
+ */
91
+ interface CardOptions extends ComponentOptions {
92
+ }
93
+ /**
94
+ * Tabs 组件选项
95
+ */
96
+ interface TabsOptions extends ComponentOptions {
97
+ }
98
+ /**
99
+ * Tab 项目
100
+ */
101
+ interface TabItem {
102
+ title: string;
103
+ childId: string;
104
+ }
105
+ /**
106
+ * Divider 组件选项
107
+ */
108
+ interface DividerOptions extends ComponentOptions {
109
+ axis?: DividerComponent['axis'];
110
+ }
111
+ /**
112
+ * Modal 组件选项
113
+ */
114
+ interface ModalOptions extends ComponentOptions {
115
+ }
116
+ /**
117
+ * Button 组件选项
118
+ */
119
+ interface ButtonOptions extends ComponentOptions {
120
+ primary?: boolean;
121
+ }
122
+ /**
123
+ * CheckBox 组件选项
124
+ */
125
+ interface CheckBoxOptions extends ComponentOptions {
126
+ }
127
+ /**
128
+ * TextField 组件选项
129
+ */
130
+ interface TextFieldOptions extends ComponentOptions {
131
+ usageHint?: TextFieldComponent['usageHint'];
132
+ validationRegexp?: string;
133
+ }
134
+ /**
135
+ * DateTimeInput 组件选项
136
+ */
137
+ interface DateTimeInputOptions extends ComponentOptions {
138
+ enableDate?: boolean;
139
+ enableTime?: boolean;
140
+ outputFormat?: string;
141
+ label?: StringOrPath;
142
+ }
143
+ /**
144
+ * ChoicePicker 组件选项
145
+ */
146
+ interface ChoicePickerOptions extends ComponentOptions {
147
+ label?: StringOrPath;
148
+ }
149
+ /**
150
+ * Slider 组件选项
151
+ */
152
+ interface SliderOptions extends ComponentOptions {
153
+ label?: StringOrPath;
154
+ min?: number;
155
+ max?: number;
156
+ }
157
+ /**
158
+ * 创建 Text 组件
159
+ *
160
+ * @param text - 文本内容或数据路径
161
+ * @param options - 组件选项
162
+ *
163
+ * @example
164
+ * // 静态文本
165
+ * text('Hello World', { usageHint: 'h1' });
166
+ *
167
+ * // 数据绑定
168
+ * text({ path: '/user/name' });
169
+ */
170
+ declare function text(content: StringOrPath, options?: TextOptions): ComponentInstance;
171
+ /**
172
+ * 创建 Image 组件
173
+ *
174
+ * @param url - 图片 URL 或数据路径
175
+ * @param options - 组件选项
176
+ */
177
+ declare function image(url: StringOrPath, options?: ImageOptions): ComponentInstance;
178
+ /**
179
+ * 创建 Icon 组件
180
+ *
181
+ * @param name - 图标名称或数据路径
182
+ * @param options - 组件选项
183
+ */
184
+ declare function icon(name: StringOrPath, options?: IconOptions): ComponentInstance;
185
+ /**
186
+ * 创建 Video 组件
187
+ *
188
+ * @param url - 视频 URL 或数据路径
189
+ * @param options - 组件选项
190
+ */
191
+ declare function video(url: StringOrPath, options?: VideoOptions): ComponentInstance;
192
+ /**
193
+ * 创建 AudioPlayer 组件
194
+ *
195
+ * @param url - 音频 URL 或数据路径
196
+ * @param options - 组件选项
197
+ */
198
+ declare function audioPlayer(url: StringOrPath, options?: AudioPlayerOptions): ComponentInstance;
199
+ /**
200
+ * 创建 Row 组件(水平布局)
201
+ *
202
+ * @param children - 子组件 ID 列表或动态模板
203
+ * @param options - 布局选项
204
+ *
205
+ * @example
206
+ * // 静态子组件
207
+ * row(['text1', 'text2', 'button1']);
208
+ *
209
+ * // 动态模板
210
+ * row({ componentId: 'itemTemplate', path: '/items' });
211
+ */
212
+ declare function row(children: ChildrenProperty, options?: LayoutOptions): ComponentInstance;
213
+ /**
214
+ * 创建 Column 组件(垂直布局)
215
+ *
216
+ * @param children - 子组件 ID 列表或动态模板
217
+ * @param options - 布局选项
218
+ */
219
+ declare function column(children: ChildrenProperty, options?: LayoutOptions): ComponentInstance;
220
+ /**
221
+ * 创建 List 组件
222
+ *
223
+ * @param children - 子组件 ID 列表或动态模板
224
+ * @param options - 列表选项
225
+ */
226
+ declare function list(children: ChildrenProperty, options?: ListOptions): ComponentInstance;
227
+ /**
228
+ * 创建 Card 组件
229
+ *
230
+ * @param childId - 子组件 ID
231
+ * @param options - 卡片选项
232
+ */
233
+ declare function card(childId: string, options?: CardOptions): ComponentInstance;
234
+ /**
235
+ * 创建 Tabs 组件
236
+ *
237
+ * @param items - 标签项列表
238
+ * @param options - Tabs 选项
239
+ */
240
+ declare function tabs(items: TabItem[], options?: TabsOptions): ComponentInstance;
241
+ /**
242
+ * 创建 Divider 组件
243
+ *
244
+ * @param options - 分割线选项
245
+ */
246
+ declare function divider(options?: DividerOptions): ComponentInstance;
247
+ /**
248
+ * 创建 Modal 组件
249
+ *
250
+ * @param entryPointChildId - 触发模态框的组件 ID
251
+ * @param contentChildId - 模态框内容组件 ID
252
+ * @param options - Modal 选项
253
+ */
254
+ declare function modal(entryPointChildId: string, contentChildId: string, options?: ModalOptions): ComponentInstance;
255
+ /**
256
+ * 创建 Button 组件
257
+ *
258
+ * @param childId - 按钮内容组件 ID(通常是 Text)
259
+ * @param action - 点击时触发的操作
260
+ * @param options - 按钮选项
261
+ */
262
+ declare function button(childId: string, action: Action, options?: ButtonOptions): ComponentInstance;
263
+ /**
264
+ * 创建 CheckBox 组件
265
+ *
266
+ * @param label - 标签
267
+ * @param value - 值(布尔值或数据路径)
268
+ * @param options - 选项
269
+ */
270
+ declare function checkbox(label: StringOrPath, value: BooleanOrPath, options?: CheckBoxOptions): ComponentInstance;
271
+ /**
272
+ * 创建 TextField 组件
273
+ *
274
+ * @param label - 标签
275
+ * @param textValue - 值(可选)
276
+ * @param options - 选项
277
+ */
278
+ declare function textField(label: StringOrPath, textValue?: StringOrPath, options?: TextFieldOptions): ComponentInstance;
279
+ /**
280
+ * 创建 DateTimeInput 组件
281
+ *
282
+ * @param value - 值(ISO 8601 格式字符串或数据路径)
283
+ * @param options - 选项
284
+ */
285
+ declare function dateTimeInput(value: StringOrPath, options?: DateTimeInputOptions): ComponentInstance;
286
+ /**
287
+ * 创建 ChoicePicker 组件
288
+ *
289
+ * @param optionsList - 选项列表
290
+ * @param value - 当前选中值
291
+ * @param usageHint - 使用提示
292
+ * @param options - 组件选项
293
+ */
294
+ declare function choicePicker(optionsList: Array<{
295
+ label: StringOrPath;
296
+ value: string;
297
+ }>, value: StringArrayOrPath, usageHint: ChoicePickerComponent['usageHint'], options?: ChoicePickerOptions): ComponentInstance;
298
+ /**
299
+ * 创建 Slider 组件
300
+ *
301
+ * @param value - 当前值
302
+ * @param options - 选项
303
+ */
304
+ declare function slider(value: NumberOrPath, options?: SliderOptions): ComponentInstance;
305
+ /**
306
+ * 创建带文本的按钮(返回按钮和文本组件)
307
+ *
308
+ * @param buttonText - 按钮文本
309
+ * @param action - 点击操作
310
+ * @param options - 按钮选项
311
+ * @returns 包含按钮和文本组件的数组
312
+ */
313
+ declare function textButton(buttonText: string, action: Action, options?: ButtonOptions & {
314
+ textId?: string;
315
+ }): [ComponentInstance, ComponentInstance];
316
+ /**
317
+ * 创建标题组件
318
+ */
319
+ declare function h1(content: StringOrPath, options?: Omit<TextOptions, 'usageHint'>): ComponentInstance;
320
+ declare function h2(content: StringOrPath, options?: Omit<TextOptions, 'usageHint'>): ComponentInstance;
321
+ declare function h3(content: StringOrPath, options?: Omit<TextOptions, 'usageHint'>): ComponentInstance;
322
+ declare function h4(content: StringOrPath, options?: Omit<TextOptions, 'usageHint'>): ComponentInstance;
323
+ declare function h5(content: StringOrPath, options?: Omit<TextOptions, 'usageHint'>): ComponentInstance;
324
+ declare function caption(content: StringOrPath, options?: Omit<TextOptions, 'usageHint'>): ComponentInstance;
325
+ declare function body(content: StringOrPath, options?: Omit<TextOptions, 'usageHint'>): ComponentInstance;
326
+
327
+ /**
328
+ * Message Builder
329
+ *
330
+ * 提供 A2UI 协议消息的构建工具函数
331
+ * 支持 v0.8 和 v0.9 两种消息格式
332
+ */
333
+
334
+ /**
335
+ * 创建 CreateSurface 消息 (v0.9)
336
+ *
337
+ * @param surfaceId - Surface ID
338
+ * @param catalogId - Catalog ID(默认为标准目录)
339
+ */
340
+ declare function createSurface(surfaceId: string, catalogId?: string): CreateSurfaceMessage;
341
+ /**
342
+ * 创建 UpdateComponents 消息 (v0.9)
343
+ *
344
+ * @param surfaceId - Surface ID
345
+ * @param components - 组件列表
346
+ */
347
+ declare function updateComponents(surfaceId: string, components: ComponentInstance[]): UpdateComponentsMessage;
348
+ /**
349
+ * 创建 UpdateDataModel 消息 (v0.9)
350
+ *
351
+ * @param surfaceId - Surface ID
352
+ * @param value - 数据值
353
+ * @param path - 数据路径(可选)
354
+ * @param op - 操作类型(默认为 replace)
355
+ */
356
+ declare function updateDataModel(surfaceId: string, value: unknown, path?: string, op?: 'add' | 'replace' | 'remove'): UpdateDataModelMessage;
357
+ /**
358
+ * 创建 DeleteSurface 消息 (v0.9)
359
+ *
360
+ * @param surfaceId - 要删除的 Surface ID
361
+ */
362
+ declare function deleteSurface(surfaceId: string): DeleteSurfaceMessage;
363
+ /**
364
+ * 创建完整的 v0.9 消息序列
365
+ *
366
+ * @param options - 选项
367
+ * @returns 消息数组(可直接作为 JSONL 流发送)
368
+ */
369
+ declare function createV09Messages(options: {
370
+ surfaceId: string;
371
+ catalogId?: string;
372
+ components: ComponentInstance[];
373
+ dataModel?: DataObject;
374
+ }): ServerToClientMessageV09[];
375
+ /**
376
+ * 创建 BeginRendering 消息 (v0.8)
377
+ *
378
+ * @param rootId - 根组件 ID
379
+ * @param surfaceId - Surface ID
380
+ * @param styles - 样式配置
381
+ */
382
+ declare function beginRendering(rootId: string, surfaceId?: string, styles?: Record<string, string>): BeginRenderingMessage;
383
+ /**
384
+ * 创建 SurfaceUpdate 消息 (v0.8)
385
+ *
386
+ * @param components - 组件定义数组
387
+ * @param surfaceId - Surface ID
388
+ */
389
+ declare function surfaceUpdate(components: ComponentInstanceV08[], surfaceId?: string): SurfaceUpdateMessage;
390
+ /**
391
+ * 创建 DataModelUpdate 消息 (v0.8)
392
+ *
393
+ * @param contents - ValueMap 数组
394
+ * @param surfaceId - Surface ID
395
+ * @param path - 数据路径(可选)
396
+ */
397
+ declare function dataModelUpdate(contents: ValueMap[], surfaceId?: string, path?: string): DataModelUpdateMessage;
398
+ /**
399
+ * 创建 DataModel 初始化消息 (v0.8)
400
+ *
401
+ * @param data - 初始数据对象
402
+ * @param surfaceId - Surface ID
403
+ */
404
+ declare function dataModelInit(data: DataObject, surfaceId?: string): DataModelUpdateMessage;
405
+ /**
406
+ * 创建路径更新消息 (v0.8)
407
+ *
408
+ * @param path - 数据路径
409
+ * @param value - 新值
410
+ * @param surfaceId - Surface ID
411
+ */
412
+ declare function pathUpdate(path: string, value: DataValue, surfaceId?: string): DataModelUpdateMessage;
413
+ /**
414
+ * 创建 DeleteSurface 消息 (v0.8)
415
+ *
416
+ * @param surfaceId - 要删除的 Surface ID
417
+ */
418
+ declare function deleteSurfaceV08(surfaceId: string): {
419
+ deleteSurface: {
420
+ surfaceId: string;
421
+ };
422
+ };
423
+ /**
424
+ * 创建完整的 v0.8 消息数组
425
+ *
426
+ * @param options - 选项
427
+ * @returns 消息数组
428
+ */
429
+ declare function createV08Messages(options: {
430
+ rootId: string;
431
+ components: ComponentInstanceV08[];
432
+ dataModel?: DataObject;
433
+ surfaceId?: string;
434
+ styles?: Record<string, string>;
435
+ }): ServerToClientMessageV08[];
436
+ /**
437
+ * 将消息数组转换为 JSONL 格式字符串
438
+ *
439
+ * @param messages - 消息数组
440
+ * @returns JSONL 格式字符串
441
+ */
442
+ declare function messagesToJsonl(messages: Array<ServerToClientMessageV08 | ServerToClientMessageV09>): string;
443
+ /**
444
+ * 从 JSONL 格式字符串解析消息数组
445
+ *
446
+ * @param jsonl - JSONL 格式字符串
447
+ * @returns 消息数组
448
+ */
449
+ declare function jsonlToMessages(jsonl: string): Array<ServerToClientMessageV08 | ServerToClientMessageV09>;
450
+
451
+ /**
452
+ * Data Model Builder
453
+ *
454
+ * 提供 DataModel 相关的构建工具函数
455
+ * 符合 A2UI 协议的 ValueMap 格式
456
+ */
457
+
458
+ /**
459
+ * 路径映射表类型
460
+ */
461
+ type PathMappings = Record<string, string>;
462
+ /**
463
+ * 默认路径映射
464
+ */
465
+ declare const DEFAULT_PATH_MAPPINGS: PathMappings;
466
+ /**
467
+ * 将 JavaScript 对象转换为 A2UI ValueMap 格式
468
+ *
469
+ * @param obj - JavaScript 对象
470
+ * @param prefix - 路径前缀(用于嵌套对象的 key 生成)
471
+ *
472
+ * @example
473
+ * objectToValueMap({ name: 'John', age: 30 });
474
+ * // Returns: [
475
+ * // { key: '/name', valueString: 'John' },
476
+ * // { key: '/age', valueNumber: 30 }
477
+ * // ]
478
+ */
479
+ declare function objectToValueMap(obj: DataObject, prefix?: string): ValueMap[];
480
+ /**
481
+ * 将单个值转换为 A2UI ValueMap 格式
482
+ *
483
+ * @param key - 数据路径/键
484
+ * @param value - JavaScript 值
485
+ */
486
+ declare function valueToValueMap(key: string, value: DataValue): ValueMap;
487
+ /**
488
+ * 规范化路径格式
489
+ * - 将 . 分隔符转换为 /
490
+ * - 确保路径以 / 开头
491
+ * - 可选路径映射
492
+ *
493
+ * @param path - 原始路径
494
+ * @param pathMappings - 可选路径映射表
495
+ *
496
+ * @example
497
+ * normalizePath('user.name'); // '/user/name'
498
+ * normalizePath('/user/name'); // '/user/name'
499
+ * normalizePath('stats.count', { stats: 'progress' }); // '/progress/count'
500
+ */
501
+ declare function normalizePath(path: string, pathMappings?: PathMappings): string;
502
+ /**
503
+ * 更新数据项定义
504
+ */
505
+ interface UpdateDataItem {
506
+ /** 数据路径 */
507
+ path: string;
508
+ /** 新值 */
509
+ value: DataValue;
510
+ /** 值类型(可选,用于类型提示) */
511
+ valueType?: 'string' | 'number' | 'boolean' | 'list' | 'map';
512
+ /** 操作类型(可选,用于增量更新) */
513
+ operation?: 'set' | 'increment' | 'decrement' | 'append' | 'remove';
514
+ }
515
+ /**
516
+ * 将更新数据项数组转换为 ValueMap
517
+ *
518
+ * @param updates - 更新数据项
519
+ * @param basePath - 基础路径
520
+ * @param pathMappings - 可选路径映射表
521
+ */
522
+ declare function updatesToValueMap(updates: UpdateDataItem[], basePath?: string, pathMappings?: PathMappings): ValueMap[];
523
+ /**
524
+ * 将嵌套对象扁平化为 ValueMap 条目
525
+ *
526
+ * @param obj - 要扁平化的对象
527
+ * @param basePath - 基础路径
528
+ */
529
+ declare function flattenObjectToValueMap(obj: DataObject, basePath: string): ValueMap[];
530
+ /**
531
+ * 从 ValueMap 数组中提取普通 JavaScript 对象
532
+ *
533
+ * @param valueMaps - ValueMap 数组
534
+ * @returns JavaScript 对象
535
+ */
536
+ declare function valueMapToObject(valueMaps: ValueMap[]): DataObject;
537
+
538
+ export { type AudioPlayerOptions, type ButtonOptions, type CardOptions, type CheckBoxOptions, type ChoicePickerOptions, type ComponentOptions, DEFAULT_PATH_MAPPINGS, type DateTimeInputOptions, type DividerOptions, type IconOptions, type ImageOptions, type LayoutOptions, type ListOptions, type ModalOptions, type PathMappings, type SliderOptions, type TabItem, type TabsOptions, type TextFieldOptions, type TextOptions, type UpdateDataItem, type VideoOptions, 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 };