aster-a2ui 0.11.0

A2UI (Agent-to-User Interface) protocol implementation for Aster
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! A2UI 标准组件目录
//!
//! 对应 A2UI 规范中的 standard_catalog.json

use serde::{Deserialize, Serialize};

use crate::common::{
    AccessibilityAttributes, Action, Checkable, ChildList, ComponentId, DynamicBoolean,
    DynamicNumber, DynamicString, DynamicStringList,
};

/// 标准组件目录 ID
pub const STANDARD_CATALOG_ID: &str = "https://a2ui.org/specification/v0_10/standard_catalog.json";

// ============================================================================
// 组件通用属性
// ============================================================================

/// 组件通用属性
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ComponentCommon {
    /// 组件唯一标识符
    pub id: ComponentId,
    /// 无障碍属性
    #[serde(skip_serializing_if = "Option::is_none")]
    pub accessibility: Option<AccessibilityAttributes>,
    /// 布局权重(仅在 Row/Column 子组件中有效)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub weight: Option<f64>,
}

// ============================================================================
// 组件枚举
// ============================================================================

/// 所有标准组件的枚举
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "component")]
pub enum Component {
    Text(TextComponent),
    Image(ImageComponent),
    Icon(IconComponent),
    Video(VideoComponent),
    AudioPlayer(AudioPlayerComponent),
    Row(RowComponent),
    Column(ColumnComponent),
    List(ListComponent),
    Card(CardComponent),
    Tabs(TabsComponent),
    Modal(ModalComponent),
    Divider(DividerComponent),
    Button(ButtonComponent),
    TextField(TextFieldComponent),
    CheckBox(CheckBoxComponent),
    ChoicePicker(ChoicePickerComponent),
    Slider(SliderComponent),
    DateTimeInput(DateTimeInputComponent),
}

impl Component {
    /// 获取组件 ID
    pub fn id(&self) -> &str {
        match self {
            Component::Text(c) => &c.common.id,
            Component::Image(c) => &c.common.id,
            Component::Icon(c) => &c.common.id,
            Component::Video(c) => &c.common.id,
            Component::AudioPlayer(c) => &c.common.id,
            Component::Row(c) => &c.common.id,
            Component::Column(c) => &c.common.id,
            Component::List(c) => &c.common.id,
            Component::Card(c) => &c.common.id,
            Component::Tabs(c) => &c.common.id,
            Component::Modal(c) => &c.common.id,
            Component::Divider(c) => &c.common.id,
            Component::Button(c) => &c.common.id,
            Component::TextField(c) => &c.common.id,
            Component::CheckBox(c) => &c.common.id,
            Component::ChoicePicker(c) => &c.common.id,
            Component::Slider(c) => &c.common.id,
            Component::DateTimeInput(c) => &c.common.id,
        }
    }
}

// ============================================================================
// 展示组件
// ============================================================================

/// 文本组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TextComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 文本内容(支持简单 Markdown)
    pub text: DynamicString,
    /// 文本样式变体
    #[serde(skip_serializing_if = "Option::is_none")]
    pub variant: Option<TextVariant>,
}

/// 文本样式变体
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TextVariant {
    H1,
    H2,
    H3,
    H4,
    H5,
    Caption,
    Body,
}

/// 图片组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ImageComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 图片 URL
    pub url: DynamicString,
    /// 图片适应方式
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fit: Option<ImageFit>,
    /// 图片样式变体
    #[serde(skip_serializing_if = "Option::is_none")]
    pub variant: Option<ImageVariant>,
}

/// 图片适应方式
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum ImageFit {
    Contain,
    Cover,
    Fill,
    None,
    ScaleDown,
}

/// 图片样式变体
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ImageVariant {
    Icon,
    Avatar,
    SmallFeature,
    MediumFeature,
    LargeFeature,
    Header,
}

/// 图标组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct IconComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 图标名称或自定义路径
    pub name: IconName,
}

/// 图标名称
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum IconName {
    /// 预定义图标
    Preset(PresetIcon),
    /// 自定义 SVG 路径
    Custom { path: String },
}

/// 预定义图标列表
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum PresetIcon {
    AccountCircle,
    Add,
    ArrowBack,
    ArrowForward,
    AttachFile,
    CalendarToday,
    Call,
    Camera,
    Check,
    Close,
    Delete,
    Download,
    Edit,
    Event,
    Error,
    FastForward,
    Favorite,
    FavoriteOff,
    Folder,
    Help,
    Home,
    Info,
    LocationOn,
    Lock,
    LockOpen,
    Mail,
    Menu,
    MoreVert,
    MoreHoriz,
    NotificationsOff,
    Notifications,
    Pause,
    Payment,
    Person,
    Phone,
    Photo,
    Play,
    Print,
    Refresh,
    Rewind,
    Search,
    Send,
    Settings,
    Share,
    ShoppingCart,
    SkipNext,
    SkipPrevious,
    Star,
    StarHalf,
    StarOff,
    Stop,
    Upload,
    Visibility,
    VisibilityOff,
    VolumeDown,
    VolumeMute,
    VolumeOff,
    VolumeUp,
    Warning,
}

/// 视频组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct VideoComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 视频 URL
    pub url: DynamicString,
}

/// 音频播放器组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AudioPlayerComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 音频 URL
    pub url: DynamicString,
    /// 音频描述
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<DynamicString>,
}

// ============================================================================
// 布局组件
// ============================================================================

/// 水平布局组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RowComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 子组件列表
    pub children: ChildList,
    /// 主轴对齐方式
    #[serde(skip_serializing_if = "Option::is_none")]
    pub justify: Option<JustifyContent>,
    /// 交叉轴对齐方式
    #[serde(skip_serializing_if = "Option::is_none")]
    pub align: Option<AlignItems>,
}

/// 垂直布局组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ColumnComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 子组件列表
    pub children: ChildList,
    /// 主轴对齐方式
    #[serde(skip_serializing_if = "Option::is_none")]
    pub justify: Option<JustifyContent>,
    /// 交叉轴对齐方式
    #[serde(skip_serializing_if = "Option::is_none")]
    pub align: Option<AlignItems>,
}

/// 主轴对齐方式
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum JustifyContent {
    Start,
    Center,
    End,
    SpaceBetween,
    SpaceAround,
    SpaceEvenly,
    Stretch,
}

/// 交叉轴对齐方式
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum AlignItems {
    Start,
    Center,
    End,
    Stretch,
}

/// 列表组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ListComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 子组件列表
    pub children: ChildList,
    /// 列表方向
    #[serde(skip_serializing_if = "Option::is_none")]
    pub direction: Option<ListDirection>,
    /// 交叉轴对齐方式
    #[serde(skip_serializing_if = "Option::is_none")]
    pub align: Option<AlignItems>,
}

/// 列表方向
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ListDirection {
    Vertical,
    Horizontal,
}

/// 卡片组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct CardComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 子组件 ID
    pub child: ComponentId,
}

/// 标签页组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TabsComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 标签页列表
    pub tabs: Vec<TabItem>,
}

/// 标签页项
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TabItem {
    /// 标签标题
    pub title: DynamicString,
    /// 标签内容组件 ID
    pub child: ComponentId,
}

/// 模态框组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ModalComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 触发器组件 ID
    pub trigger: ComponentId,
    /// 内容组件 ID
    pub content: ComponentId,
}

/// 分隔线组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DividerComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 分隔线方向
    #[serde(skip_serializing_if = "Option::is_none")]
    pub axis: Option<DividerAxis>,
}

/// 分隔线方向
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum DividerAxis {
    Horizontal,
    Vertical,
}

// ============================================================================
// 交互组件
// ============================================================================

/// 按钮组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ButtonComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 按钮内容组件 ID
    pub child: ComponentId,
    /// 按钮动作
    pub action: Action,
    /// 按钮样式变体
    #[serde(skip_serializing_if = "Option::is_none")]
    pub variant: Option<ButtonVariant>,
    /// 验证规则
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub checkable: Option<Checkable>,
}

/// 按钮样式变体
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ButtonVariant {
    Primary,
    Borderless,
}

/// 文本输入框组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TextFieldComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 输入框标签
    pub label: DynamicString,
    /// 输入框值
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<DynamicString>,
    /// 输入框类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub variant: Option<TextFieldVariant>,
    /// 验证规则
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub checkable: Option<Checkable>,
}

/// 文本输入框类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum TextFieldVariant {
    ShortText,
    LongText,
    Number,
    Obscured,
}

/// 复选框组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct CheckBoxComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 复选框标签
    pub label: DynamicString,
    /// 复选框值
    pub value: DynamicBoolean,
    /// 验证规则
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub checkable: Option<Checkable>,
}

/// 选择器组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ChoicePickerComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 选择器标签
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<DynamicString>,
    /// 选项列表
    pub options: Vec<ChoiceOption>,
    /// 当前选中值
    pub value: DynamicStringList,
    /// 选择器类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub variant: Option<ChoicePickerVariant>,
    /// 验证规则
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub checkable: Option<Checkable>,
}

/// 选择器选项
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ChoiceOption {
    /// 选项显示文本
    pub label: DynamicString,
    /// 选项值
    pub value: String,
}

/// 选择器类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ChoicePickerVariant {
    MultipleSelection,
    MutuallyExclusive,
}

/// 滑块组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SliderComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 滑块标签
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<DynamicString>,
    /// 最小值
    pub min: f64,
    /// 最大值
    pub max: f64,
    /// 当前值
    pub value: DynamicNumber,
    /// 验证规则
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub checkable: Option<Checkable>,
}

/// 日期时间输入组件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DateTimeInputComponent {
    #[serde(flatten)]
    pub common: ComponentCommon,
    /// 输入框标签
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<DynamicString>,
    /// 当前值(ISO 8601 格式)
    pub value: DynamicString,
    /// 是否启用日期选择
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_date: Option<bool>,
    /// 是否启用时间选择
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_time: Option<bool>,
    /// 最小日期时间
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min: Option<DynamicString>,
    /// 最大日期时间
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max: Option<DynamicString>,
    /// 验证规则
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub checkable: Option<Checkable>,
}