@antglobal/copilot-cards-core 1.0.4 → 1.0.6

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,215 @@
1
+ /**
2
+ * Action Runner — executes ActionStep chains defined in card schemas.
3
+ *
4
+ * Supported action types:
5
+ * - **request**: HTTP request (fetch / API call)
6
+ * - **toast**: Display a toast notification
7
+ * - **url**: Navigate to a URL
8
+ * - **setVariable**: Update a variable in the card context
9
+ * - **emit**: Emit a custom event to the host environment
10
+ * - **copy**: Copy text to clipboard
11
+ *
12
+ * Each step can have `onSuccess` / `onFail` branches for chaining.
13
+ */
14
+ interface ActionStep {
15
+ type: 'request' | 'emit' | 'url' | 'setVariable' | 'toast' | 'copy' | (string & {});
16
+ params: Record<string, any>;
17
+ /** Action chain to run on success (optional). */
18
+ onSuccess?: ActionStep[];
19
+ /** Action chain to run on failure (optional). */
20
+ onFail?: ActionStep[];
21
+ }
22
+
23
+ /**
24
+ * Legacy Schema Compatibility — converts old-format card JSON into
25
+ * the current CardSchema format so it can be rendered by the SDK.
26
+ *
27
+ * Old format:
28
+ * ```json
29
+ * {
30
+ * "cardType": "common",
31
+ * "cardContents": [{ "type": "text", "content": { "text": "hello" } }],
32
+ * "text": "card text",
33
+ * "extInfo": { "language": "zh-CN" }
34
+ * }
35
+ * ```
36
+ *
37
+ * Usage:
38
+ * ```ts
39
+ * import { convertLegacySchema } from '@antglobal/copilot-cards-core';
40
+ * const schema = convertLegacySchema(oldJson);
41
+ * renderCard(container, schema);
42
+ * ```
43
+ */
44
+
45
+ type LegacyTrackingType = 'click' | 'expo';
46
+ interface LegacyTracking {
47
+ spm: string;
48
+ type: LegacyTrackingType;
49
+ }
50
+ interface LegacyCardSchema {
51
+ cardType: 'common' | 'special' | string;
52
+ cardName?: string;
53
+ cardContents: LegacyCardContentItem[] | LegacyCardContentItem;
54
+ text: string;
55
+ extInfo: {
56
+ language: string;
57
+ [key: string]: any;
58
+ };
59
+ description?: string;
60
+ tracking?: LegacyTracking;
61
+ }
62
+ interface LegacyCardContentItem {
63
+ type: 'timeline' | 'button' | 'image' | 'form' | 'group' | 'text' | 'custom' | string;
64
+ groupInfo?: Record<string, any>;
65
+ content: Record<string, any>;
66
+ }
67
+
68
+ /**
69
+ * Schema Parser — resolves a CardSchema into a renderable tree.
70
+ *
71
+ * A CardSchema contains a flat `elements` map plus a `rootID` entry point,
72
+ * global `variables`, and optional global `actions`.
73
+ */
74
+
75
+ /** Value that can be a static literal or a `${...}` expression. */
76
+ interface ExpressionValue {
77
+ type: 'static' | 'expression';
78
+ value: string;
79
+ }
80
+ /**
81
+ * Slot layout mode — the key name in `props.slots` determines the layout:
82
+ * - default: no special layout, children appended directly
83
+ * - flex: one-dimensional Flexbox layout applied to the host container
84
+ * - columns: multi-column layout using groups (each group = one column)
85
+ * - grid: CSS grid with configurable column count and gap
86
+ * - horizontalScroll: horizontal scrollable container with drag and snap
87
+ * - carousel: center-focused carousel with scale/opacity effects
88
+ * - list: vertical list with optional ordered/unordered markers
89
+ * - float: relative container with absolutely positioned overlays
90
+ */
91
+ type SlotLayout = 'default' | 'flex' | 'columns' | 'grid' | 'horizontalScroll' | 'carousel' | 'list' | 'float' | 'keyValue' | 'table' | 'chart';
92
+ /** Binds a slot to a template that is instantiated once per source item. */
93
+ interface RepeatBinding {
94
+ source: string;
95
+ template: string;
96
+ item?: string;
97
+ index?: string;
98
+ emptyTemplate?: string;
99
+ }
100
+ /**
101
+ * Content of a named slot.
102
+ *
103
+ * - `children` — flat list of child element IDs (default, grid, horizontalScroll, carousel, list)
104
+ * - `groups` — grouped child IDs, each sub-array is one region (columns)
105
+ * - `config` — layout-specific parameters (grid columns, list style, float overlays, etc.)
106
+ *
107
+ * Grid `config` fields:
108
+ * - `columns` — column count (default 2)
109
+ * - `gap` — track gap (default '8px')
110
+ * - `rows` — opt-in: explicit row count. When set, empty cells keep their
111
+ * height and children can be pinned to a cell via their own
112
+ * `style.gridColumn` / `style.gridRow`. Omit for auto-flow.
113
+ * - `rowHeight` — min height per row when `rows` is set (default '48px')
114
+ *
115
+ * Horizontal scroll `config` fields include:
116
+ * - `scrollbar` — `auto` leaves presentation to the host/browser, `hidden` hides
117
+ * the native scrollbar, and `visible` explicitly restores it.
118
+ * Omitted/invalid values preserve the legacy hidden output.
119
+ */
120
+ interface SlotContent {
121
+ children?: string[];
122
+ groups?: string[][];
123
+ repeat?: RepeatBinding;
124
+ config?: Record<string, any>;
125
+ }
126
+ /** A single element inside the card. */
127
+ interface ElementNode {
128
+ id: string;
129
+ type: string;
130
+ props: {
131
+ style?: Record<string, any>;
132
+ content?: ExpressionValue | string;
133
+ slots?: Partial<Record<SlotLayout, SlotContent>>;
134
+ [key: string]: any;
135
+ };
136
+ /** Lifecycle hooks — each is a chain of ActionSteps. */
137
+ lifecycle?: {
138
+ onMount?: ActionStep[];
139
+ onExposed?: ActionStep[];
140
+ onDestroy?: ActionStep[];
141
+ };
142
+ /** Interaction events — each is a chain of ActionSteps or a string reference to schema.actions. */
143
+ events?: {
144
+ onClick?: ActionStep[] | string;
145
+ onChange?: ActionStep[] | string;
146
+ onSubmit?: ActionStep[] | string;
147
+ [key: string]: ActionStep[] | string | undefined;
148
+ };
149
+ /** Directives that control visibility, disabled state, loops, etc. */
150
+ directives?: {
151
+ visible?: string;
152
+ /** Expression that resolves to truthy → element is disabled (greyed out, no events). */
153
+ disabled?: string;
154
+ [key: string]: any;
155
+ };
156
+ }
157
+ /** Top-level card schema handed to the SDK. */
158
+ interface CardSchema {
159
+ version: string;
160
+ rootID: string;
161
+ elements: Record<string, ElementNode>;
162
+ variables: Record<string, any>;
163
+ /** Global action definitions reusable across elements. */
164
+ actions?: Record<string, ActionStep[]>;
165
+ }
166
+ /** Accepted input — either the current CardSchema or a legacy format. */
167
+ type CardSchemaInput = CardSchema | LegacyCardSchema;
168
+ type SchemaValidationIssueCode = 'SCHEMA_TYPE_MISMATCH' | 'SCHEMA_REQUIRED_FIELD' | 'LEGACY_CONVERSION_FAILED' | 'ROOT_TYPE_MISMATCH' | 'ROOT_REFERENCE_NOT_FOUND' | 'ELEMENTS_UNINSPECTABLE' | 'ELEMENT_ACCESSOR_NOT_ALLOWED' | 'ELEMENT_TYPE_MISMATCH' | 'ELEMENT_TYPE_MISSING' | 'ELEMENT_PROPS_TYPE_MISMATCH' | 'SLOTS_TYPE_MISMATCH' | 'SLOT_ACCESSOR_NOT_ALLOWED' | 'SLOTS_UNINSPECTABLE' | 'SLOT_TYPE_MISMATCH' | 'SLOT_CHILDREN_TYPE_MISMATCH' | 'SLOT_CHILD_ID_INVALID' | 'SLOT_GROUPS_TYPE_MISMATCH' | 'SLOT_GROUP_TYPE_MISMATCH' | 'SLOT_GROUP_CHILD_ID_INVALID' | 'ELEMENT_REFERENCE_NOT_FOUND' | 'DYNAMIC_SLOT_MULTIPLE' | 'DYNAMIC_TEMPLATE_INVALID' | 'DYNAMIC_TEMPLATE_NOT_FOUND' | 'REPEAT_TYPE_MISMATCH' | 'REPEAT_SLOT_UNSUPPORTED' | 'REPEAT_CONTENT_CONFLICT' | 'REPEAT_SOURCE_TYPE_MISMATCH' | 'REPEAT_SOURCE_INVALID' | 'REPEAT_TEMPLATE_TYPE_MISMATCH' | 'REPEAT_TEMPLATE_INVALID' | 'REPEAT_TEMPLATE_NOT_FOUND' | 'REPEAT_ALIAS_TYPE_MISMATCH' | 'REPEAT_ALIAS_INVALID' | 'REPEAT_ALIAS_RESERVED' | 'REPEAT_ALIAS_COLLISION' | 'REPEAT_EMPTY_TEMPLATE_TYPE_MISMATCH' | 'REPEAT_EMPTY_TEMPLATE_INVALID' | 'REPEAT_EMPTY_TEMPLATE_NOT_FOUND' | 'REPEAT_TEMPLATE_CYCLE' | 'DYNAMIC_TEMPLATE_CYCLE' | 'DYNAMIC_LIFECYCLE_UNSUPPORTED' | 'DYNAMIC_VARIABLE_KEY_UNSUPPORTED';
169
+
170
+ /**
171
+ * Optional, editor-oriented validation for JSON source text.
172
+ *
173
+ * Kept out of the Core main entry so render-only consumers do not bundle a
174
+ * fault-tolerant JSON parser they never use.
175
+ */
176
+
177
+ declare const SCHEMA_TEXT_MAX_CHARACTERS = 1000000;
178
+ declare const SCHEMA_TEXT_MAX_DEPTH = 128;
179
+ declare const SCHEMA_TEXT_MAX_ERRORS = 50;
180
+ type SchemaTextValidationIssueCode = SchemaValidationIssueCode | 'JSON_SYNTAX_ERROR' | 'JSON_DUPLICATE_PROPERTY' | 'JSON_INPUT_TOO_LARGE' | 'JSON_MAX_DEPTH_EXCEEDED';
181
+ interface SourcePosition {
182
+ /** 1-based source line. */
183
+ line: number;
184
+ /** 1-based UTF-16 source column. */
185
+ column: number;
186
+ /** 0-based UTF-16 source offset. */
187
+ offset: number;
188
+ }
189
+ interface SourceRange {
190
+ start: SourcePosition;
191
+ /** Exclusive end position. */
192
+ end: SourcePosition;
193
+ }
194
+ interface SchemaTextValidationIssue {
195
+ code: SchemaTextValidationIssueCode;
196
+ path: string;
197
+ anchorPath?: string;
198
+ message: string;
199
+ params?: Record<string, unknown>;
200
+ range: SourceRange;
201
+ }
202
+ type SchemaTextValidationResult = {
203
+ valid: true;
204
+ schema: CardSchemaInput;
205
+ errors: [];
206
+ } | {
207
+ valid: false;
208
+ errors: SchemaTextValidationIssue[];
209
+ truncated: boolean;
210
+ };
211
+ /** Parse strict JSON and return syntax plus SDK-owned schema diagnostics. */
212
+ declare function validateSchemaText(text: string): SchemaTextValidationResult;
213
+
214
+ export { SCHEMA_TEXT_MAX_CHARACTERS, SCHEMA_TEXT_MAX_DEPTH, SCHEMA_TEXT_MAX_ERRORS, validateSchemaText };
215
+ export type { SchemaTextValidationIssue, SchemaTextValidationIssueCode, SchemaTextValidationResult, SourcePosition, SourceRange };