@antglobal/copilot-cards-core 1.0.0 → 1.0.2
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/README.md +152 -1
- package/dist/index.cjs +2497 -0
- package/dist/index.d.ts +398 -2
- package/dist/index.js +2466 -0
- package/package.json +10 -9
- package/dist/index.cjs.js +0 -2100
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js +0 -2072
- package/dist/index.esm.js.map +0 -1
package/README.md
CHANGED
|
@@ -1 +1,152 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @antglobal/copilot-cards-core
|
|
2
|
+
|
|
3
|
+
Core logic for schema-driven cards in AI conversations. This package has no UI or DOM dependency and can run in browsers, Node.js, WebViews, and mini-program runtimes.
|
|
4
|
+
|
|
5
|
+
It provides schema parsing, expression resolution, action execution, lifecycle management, streaming commands, legacy-schema conversion, and the shared built-in icon registry used by the Web and mini-program renderers.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @antglobal/copilot-cards-core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import {
|
|
17
|
+
parseSchema,
|
|
18
|
+
resolveExpression,
|
|
19
|
+
validateSchema,
|
|
20
|
+
type CardSchema,
|
|
21
|
+
} from "@antglobal/copilot-cards-core";
|
|
22
|
+
|
|
23
|
+
const schema: CardSchema = {
|
|
24
|
+
version: "1.0",
|
|
25
|
+
rootID: "root",
|
|
26
|
+
elements: {
|
|
27
|
+
root: {
|
|
28
|
+
id: "root",
|
|
29
|
+
type: "Text",
|
|
30
|
+
props: {
|
|
31
|
+
content: { type: "static", value: "Hello, Copilot Cards!" },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
variables: {
|
|
36
|
+
user: { name: "Alice" },
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const errors = validateSchema(schema);
|
|
41
|
+
if (errors.length > 0) {
|
|
42
|
+
throw new Error(errors.join("\n"));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const tree = parseSchema(schema);
|
|
46
|
+
const greeting = resolveExpression("Hello, ${user.name}!", schema.variables);
|
|
47
|
+
|
|
48
|
+
console.log(tree, greeting);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Capabilities
|
|
52
|
+
|
|
53
|
+
| Module | Purpose |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| Schema parser | Validates, normalizes, and converts flat schemas into render trees |
|
|
56
|
+
| Expression engine | Resolves `${path}` templates, ternaries, logical expressions, and comparisons |
|
|
57
|
+
| Action runner | Executes declarative action chains in order |
|
|
58
|
+
| Action registry | Registers custom action handlers, optionally scoped by bot ID |
|
|
59
|
+
| Action provider | Resolves named action chains supplied by the host |
|
|
60
|
+
| Lifecycle manager | Coordinates `onMount`, `onExposed`, and `onDestroy` hooks |
|
|
61
|
+
| Streaming engine | Parses and applies incremental card commands and A2UI envelopes |
|
|
62
|
+
| Legacy compatibility | Detects and converts legacy card schemas |
|
|
63
|
+
| Icon registry | Exposes shared built-in icon names and SVG definitions |
|
|
64
|
+
|
|
65
|
+
## Expressions
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import {
|
|
69
|
+
interpolate,
|
|
70
|
+
resolveDeep,
|
|
71
|
+
resolveExpression,
|
|
72
|
+
} from "@antglobal/copilot-cards-core";
|
|
73
|
+
|
|
74
|
+
const context = {
|
|
75
|
+
user: { name: "Alice", role: "admin" },
|
|
76
|
+
count: 3,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
resolveExpression("${user.name}", context); // "Alice"
|
|
80
|
+
resolveExpression("${count > 2}", context); // true
|
|
81
|
+
resolveExpression(
|
|
82
|
+
"${user.role === 'admin' ? 'Administrator' : 'User'}",
|
|
83
|
+
context,
|
|
84
|
+
); // "Administrator"
|
|
85
|
+
interpolate("Hello, ${user.name}!", context); // "Hello, Alice!"
|
|
86
|
+
resolveDeep({ title: "${user.name}", visible: "${count > 0}" }, context);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Actions
|
|
90
|
+
|
|
91
|
+
Built-in action types are host-independent. The host supplies capabilities such as `fetch`, navigation, toast notifications, clipboard access, and event emission through an `ActionRunnerContext`.
|
|
92
|
+
|
|
93
|
+
| Action | Behavior |
|
|
94
|
+
| --- | --- |
|
|
95
|
+
| `emit` | Emits an event to the host |
|
|
96
|
+
| `request` | Runs an HTTP request with response mapping, polling, timeout, and deduplication support |
|
|
97
|
+
| `setVariable` | Updates a card variable |
|
|
98
|
+
| `toast` | Requests a toast notification |
|
|
99
|
+
| `url` | Requests navigation |
|
|
100
|
+
| `copy` | Requests clipboard access |
|
|
101
|
+
|
|
102
|
+
Register a custom action handler:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import {
|
|
106
|
+
registerActionHandler,
|
|
107
|
+
runActionSteps,
|
|
108
|
+
} from "@antglobal/copilot-cards-core";
|
|
109
|
+
|
|
110
|
+
registerActionHandler("support-bot", "trackEvent", async (step) => {
|
|
111
|
+
console.log("track", step.params);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
await runActionSteps(
|
|
115
|
+
[{ type: "trackEvent", params: { event: "card_opened" } }],
|
|
116
|
+
{ botId: "support-bot", variables: {} },
|
|
117
|
+
);
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Streaming
|
|
121
|
+
|
|
122
|
+
The package exports `StreamingParser`, `StreamingEngine`, `extractPartialSchema`, and A2UI conversion helpers for incrementally generated cards.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import {
|
|
126
|
+
StreamingEngine,
|
|
127
|
+
StreamingParser,
|
|
128
|
+
} from "@antglobal/copilot-cards-core";
|
|
129
|
+
|
|
130
|
+
const parser = new StreamingParser();
|
|
131
|
+
const engine = new StreamingEngine({
|
|
132
|
+
onSurfaceCreated: (surfaceId) => console.log("created", surfaceId),
|
|
133
|
+
onComponentsUpdated: (surfaceId, changes) =>
|
|
134
|
+
console.log("components", surfaceId, changes),
|
|
135
|
+
onDataModelUpdated: (surfaceId, path, value) =>
|
|
136
|
+
console.log("data", surfaceId, path, value),
|
|
137
|
+
onContentAppended: (surfaceId, elementId, content) =>
|
|
138
|
+
console.log("content", surfaceId, elementId, content),
|
|
139
|
+
onSurfaceDeleted: (surfaceId) => console.log("deleted", surfaceId),
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
engine.applyBatch(parser.parse(chunk));
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Related packages
|
|
146
|
+
|
|
147
|
+
- `@antglobal/copilot-cards-web` — Web Component renderer for browser and WebView applications.
|
|
148
|
+
- `@antglobal/copilot-cards-mini-program` — native renderer for Alipay and WeChat mini-programs.
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT
|