@zhama/a2ui 0.10.1
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/LICENSE +22 -0
- package/README.md +891 -0
- package/dist/context/index.cjs +1 -0
- package/dist/context/index.js +1 -0
- package/dist/data/index.cjs +2 -0
- package/dist/data/index.js +2 -0
- package/dist/events/index.cjs +1 -0
- package/dist/events/index.js +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.js +2 -0
- package/dist/styles/index.cjs +1 -0
- package/dist/styles/index.js +1 -0
- package/dist/types/index.cjs +1 -0
- package/dist/types/index.js +0 -0
- package/dist/ui/index.cjs +2 -0
- package/dist/ui/index.js +2 -0
- package/package.json +168 -0
- package/src/styles/a2ui-variables.css +106 -0
- package/tailwind.preset.js +326 -0
package/README.md
ADDED
|
@@ -0,0 +1,891 @@
|
|
|
1
|
+
# @zhama/a2ui
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+
**A2UI Protocol - React Implementation for AI-Driven Dynamic User Interfaces**
|
|
6
|
+
|
|
7
|
+
**A2UI 协议 - 用于 AI 驱动动态用户界面的 React 实现**
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/@zhama/a2ui)
|
|
10
|
+
[](./LICENSE)
|
|
11
|
+
[](https://www.typescriptlang.org/)
|
|
12
|
+
[](https://reactjs.org/)
|
|
13
|
+
|
|
14
|
+
[English](#english) | [中文](#中文)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## English
|
|
21
|
+
|
|
22
|
+
### 📖 Overview
|
|
23
|
+
|
|
24
|
+
A2UI (Agent-to-User Interface) is a protocol that enables AI agents to dynamically generate and control user interfaces. This library provides a complete React implementation based on Google's A2UI protocol specification, allowing AI agents to create rich, interactive UIs through structured data instead of plain text responses.
|
|
25
|
+
|
|
26
|
+
### 🎬 Live Demo
|
|
27
|
+
|
|
28
|
+
See A2UI in action! The demo showcases an AI training assistant that generates dynamic, interactive UIs based on user conversations.
|
|
29
|
+
|
|
30
|
+
**Try it yourself:**
|
|
31
|
+
```bash
|
|
32
|
+
cd examples/basic-demo
|
|
33
|
+
npm install
|
|
34
|
+
npm run dev
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Then open http://localhost:5174 and try these commands:
|
|
38
|
+
- Type `hello` - Get a welcome screen with topic cards
|
|
39
|
+
- Click a topic button - See a structured learning path
|
|
40
|
+
- Type `practice` - Get interactive quiz questions
|
|
41
|
+
- Type `progress` - View learning statistics
|
|
42
|
+
|
|
43
|
+
**What you'll see:**
|
|
44
|
+
|
|
45
|
+
<table>
|
|
46
|
+
<tr>
|
|
47
|
+
<td width="33%" align="center">
|
|
48
|
+
<img src="docs/welcome.jpg" alt="Welcome Screen" width="100%"/>
|
|
49
|
+
<br/>
|
|
50
|
+
<strong>Welcome Screen</strong>
|
|
51
|
+
<br/>
|
|
52
|
+
<em>AI generates topic cards with actions</em>
|
|
53
|
+
</td>
|
|
54
|
+
<td width="33%" align="center">
|
|
55
|
+
<img src="docs/learning.jpg" alt="Learning Path" width="100%"/>
|
|
56
|
+
<br/>
|
|
57
|
+
<strong>Learning Path</strong>
|
|
58
|
+
<br/>
|
|
59
|
+
<em>Structured content with sections</em>
|
|
60
|
+
</td>
|
|
61
|
+
<td width="33%" align="center">
|
|
62
|
+
<img src="docs/answer.jpg" alt="Practice Quiz" width="100%"/>
|
|
63
|
+
<br/>
|
|
64
|
+
<strong>Practice Quiz</strong>
|
|
65
|
+
<br/>
|
|
66
|
+
<em>Interactive questions with buttons</em>
|
|
67
|
+
</td>
|
|
68
|
+
</tr>
|
|
69
|
+
</table>
|
|
70
|
+
|
|
71
|
+
> 💡 **Note:** All UIs above are generated dynamically by the AI agent using A2UI protocol - no hardcoded components!
|
|
72
|
+
|
|
73
|
+
### ✨ Features
|
|
74
|
+
|
|
75
|
+
- 🤖 **AI-First Design** - Built specifically for AI-driven UI generation
|
|
76
|
+
- 📦 **Complete Protocol Support** - Full implementation of A2UI protocol specifications
|
|
77
|
+
- 🎨 **Themeable** - Customizable theme system based on Tailwind CSS
|
|
78
|
+
- 🧩 **Rich Components** - Comprehensive set of UI components (Text, Button, Card, List, TextField, etc.)
|
|
79
|
+
- 🔌 **Extensible** - Support for custom component registration
|
|
80
|
+
- 📱 **Responsive** - Mobile-first responsive design
|
|
81
|
+
- ⚡ **Performance** - Optimized rendering with React best practices
|
|
82
|
+
- 🎯 **Type-Safe** - Full TypeScript support with comprehensive type definitions
|
|
83
|
+
- 🌐 **Framework Agnostic** - Core protocol can be adapted to other frameworks
|
|
84
|
+
- 🎭 **Event Handling** - Built-in action handling system for interactive components
|
|
85
|
+
|
|
86
|
+
### 🚀 Quick Start
|
|
87
|
+
|
|
88
|
+
#### Installation
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
npm install @zhama/a2ui
|
|
92
|
+
# or
|
|
93
|
+
yarn add @zhama/a2ui
|
|
94
|
+
# or
|
|
95
|
+
pnpm add @zhama/a2ui
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### Peer Dependencies
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
npm install react react-dom react-markdown
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
#### Basic Setup
|
|
105
|
+
|
|
106
|
+
1. **Configure Tailwind CSS**
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
// tailwind.config.js
|
|
110
|
+
import a2uiPreset from '@zhama/a2ui/tailwind.preset';
|
|
111
|
+
|
|
112
|
+
export default {
|
|
113
|
+
presets: [a2uiPreset],
|
|
114
|
+
content: [
|
|
115
|
+
'./src/**/*.{js,ts,jsx,tsx}',
|
|
116
|
+
'./node_modules/@zhama/a2ui/**/*.{js,ts,jsx,tsx}',
|
|
117
|
+
],
|
|
118
|
+
};
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
2. **Import CSS Variables**
|
|
122
|
+
|
|
123
|
+
```css
|
|
124
|
+
/* globals.css */
|
|
125
|
+
@import '@zhama/a2ui/styles/a2ui-variables.css';
|
|
126
|
+
@import '@zhama/a2ui/styles/a2ui.css';
|
|
127
|
+
|
|
128
|
+
@tailwind base;
|
|
129
|
+
@tailwind components;
|
|
130
|
+
@tailwind utilities;
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
3. **Use A2UIRoot Component**
|
|
134
|
+
|
|
135
|
+
```tsx
|
|
136
|
+
import { A2UIRoot } from '@zhama/a2ui';
|
|
137
|
+
import type { AnyComponentNode } from '@zhama/a2ui/types';
|
|
138
|
+
|
|
139
|
+
function App() {
|
|
140
|
+
const components: AnyComponentNode[] = [
|
|
141
|
+
{
|
|
142
|
+
id: 'text-1',
|
|
143
|
+
type: 'Text',
|
|
144
|
+
properties: {
|
|
145
|
+
text: { literal: 'Hello from A2UI!' },
|
|
146
|
+
usageHint: { literal: 'h1' },
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
id: 'button-1',
|
|
151
|
+
type: 'Button',
|
|
152
|
+
properties: {
|
|
153
|
+
child: {
|
|
154
|
+
id: 'btn-text',
|
|
155
|
+
type: 'Text',
|
|
156
|
+
properties: {
|
|
157
|
+
text: { literal: 'Click Me' },
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
action: {
|
|
161
|
+
type: 'postback',
|
|
162
|
+
payload: 'button_clicked',
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
];
|
|
167
|
+
|
|
168
|
+
const handleAction = (event) => {
|
|
169
|
+
console.log('Action triggered:', event);
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<A2UIRoot
|
|
174
|
+
childComponents={components}
|
|
175
|
+
processor={null}
|
|
176
|
+
surfaceId={null}
|
|
177
|
+
onAction={handleAction}
|
|
178
|
+
/>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### 📦 Package Exports
|
|
184
|
+
|
|
185
|
+
The library provides multiple entry points for different use cases:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
// Main entry - everything
|
|
189
|
+
import { A2UIRoot, createButton, createText } from '@zhama/a2ui';
|
|
190
|
+
|
|
191
|
+
// Type definitions only
|
|
192
|
+
import type { AnyComponentNode, ButtonNode } from '@zhama/a2ui/types';
|
|
193
|
+
|
|
194
|
+
// Data utilities
|
|
195
|
+
import { A2UIRegistry } from '@zhama/a2ui/data';
|
|
196
|
+
|
|
197
|
+
// Event types
|
|
198
|
+
import type { A2UIAction } from '@zhama/a2ui/events';
|
|
199
|
+
|
|
200
|
+
// Styles
|
|
201
|
+
import '@zhama/a2ui/styles';
|
|
202
|
+
|
|
203
|
+
// UI components individually
|
|
204
|
+
import { Button, Card, List } from '@zhama/a2ui/ui';
|
|
205
|
+
|
|
206
|
+
// Context providers
|
|
207
|
+
import { A2UIProvider, useA2UI } from '@zhama/a2ui/context';
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### 🎨 Theming
|
|
211
|
+
|
|
212
|
+
A2UI comes with a customizable theme system:
|
|
213
|
+
|
|
214
|
+
```tsx
|
|
215
|
+
import { A2UIProvider } from '@zhama/a2ui/context';
|
|
216
|
+
import { defaultTheme } from '@zhama/a2ui/styles/default-theme';
|
|
217
|
+
|
|
218
|
+
const customTheme = {
|
|
219
|
+
...defaultTheme,
|
|
220
|
+
components: {
|
|
221
|
+
...defaultTheme.components,
|
|
222
|
+
Button: 'custom-button-class',
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
function App() {
|
|
227
|
+
return (
|
|
228
|
+
<A2UIProvider theme={customTheme}>
|
|
229
|
+
<YourApp />
|
|
230
|
+
</A2UIProvider>
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### 🧩 Available Components
|
|
236
|
+
|
|
237
|
+
| Component | Description | Use Case |
|
|
238
|
+
|-----------|-------------|----------|
|
|
239
|
+
| **Text** | Render formatted text with markdown support | Headings, paragraphs, captions |
|
|
240
|
+
| **Button** | Interactive button with action support | User actions, navigation |
|
|
241
|
+
| **Card** | Container for grouped content | Content grouping, layouts |
|
|
242
|
+
| **List** | Vertical or horizontal list layout | Multiple items display |
|
|
243
|
+
| **Row** | Horizontal layout container | Side-by-side elements |
|
|
244
|
+
| **Column** | Vertical layout container | Stacked elements |
|
|
245
|
+
| **TextField** | Text input field | User input, forms |
|
|
246
|
+
| **CheckBox** | Checkbox input | Boolean selections |
|
|
247
|
+
| **Image** | Display images | Visual content |
|
|
248
|
+
| **Tabs** | Tabbed interface | Multiple views |
|
|
249
|
+
| **Surface** | Container with elevation | Modal dialogs, panels |
|
|
250
|
+
|
|
251
|
+
### 💼 Use Cases
|
|
252
|
+
|
|
253
|
+
A2UI is perfect for scenarios where AI agents need to provide rich, interactive experiences:
|
|
254
|
+
|
|
255
|
+
#### 🎓 **AI Training & Education**
|
|
256
|
+
- Dynamic course content generation
|
|
257
|
+
- Interactive quizzes and assessments
|
|
258
|
+
- Personalized learning paths
|
|
259
|
+
- Progress tracking dashboards
|
|
260
|
+
|
|
261
|
+
#### 🤖 **Conversational AI Assistants**
|
|
262
|
+
- Customer support with form filling
|
|
263
|
+
- Product recommendations with cards
|
|
264
|
+
- Multi-step workflows
|
|
265
|
+
- Data visualization
|
|
266
|
+
|
|
267
|
+
#### 📊 **Business Applications**
|
|
268
|
+
- Report generation with charts
|
|
269
|
+
- Data entry forms
|
|
270
|
+
- Approval workflows
|
|
271
|
+
- Dashboard creation
|
|
272
|
+
|
|
273
|
+
#### 🛠️ **Development Tools**
|
|
274
|
+
- Code generation with syntax highlighting
|
|
275
|
+
- Interactive documentation
|
|
276
|
+
- Configuration wizards
|
|
277
|
+
- Debugging interfaces
|
|
278
|
+
|
|
279
|
+
### 🏗️ Architecture
|
|
280
|
+
|
|
281
|
+
A2UI follows a two-layer architecture inspired by modern AI applications:
|
|
282
|
+
|
|
283
|
+
1. **ContentModel Layer** (High-level Semantic)
|
|
284
|
+
- Scene-based content structure (Welcome, Learning, Practice, etc.)
|
|
285
|
+
- Business logic representation
|
|
286
|
+
- LLM-friendly format - AI outputs meaningful scenes, not UI components
|
|
287
|
+
- Easy to understand and modify
|
|
288
|
+
|
|
289
|
+
2. **A2UI Protocol Layer** (Low-level UI)
|
|
290
|
+
- ComponentNode structure (Button, Card, List, etc.)
|
|
291
|
+
- UI rendering specification
|
|
292
|
+
- Framework implementation
|
|
293
|
+
- Optimized for rendering
|
|
294
|
+
|
|
295
|
+
**Flow:**
|
|
296
|
+
```
|
|
297
|
+
┌─────────────┐ ┌──────────────┐ ┌─────────────┐ ┌──────────┐
|
|
298
|
+
│ LLM Output │ → │ ContentModel │ → │ Scene │ → │ A2UI UI │
|
|
299
|
+
│ (JSON) │ │ (Semantic) │ │ Renderer │ │ (React) │
|
|
300
|
+
└─────────────┘ └──────────────┘ └─────────────┘ └──────────┘
|
|
301
|
+
↓ ↓
|
|
302
|
+
Scene: "welcome" ComponentNodes
|
|
303
|
+
Topics: [...] [Card, Button, ...]
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
**Benefits:**
|
|
307
|
+
- ✅ **Separation of Concerns**: LLM focuses on content, not UI details
|
|
308
|
+
- ✅ **Flexibility**: Change UI rendering without changing LLM prompts
|
|
309
|
+
- ✅ **Testability**: Test content logic separately from UI
|
|
310
|
+
- ✅ **Maintainability**: Easier to update and extend
|
|
311
|
+
|
|
312
|
+
### 📚 Examples
|
|
313
|
+
|
|
314
|
+
Check out the `/examples` directory for complete examples:
|
|
315
|
+
|
|
316
|
+
- **basic-demo**: Full-featured demo showcasing all A2UI capabilities
|
|
317
|
+
- Includes 7 different scenes (Welcome, Learning, Practice, Feedback, Progress, Conversation)
|
|
318
|
+
- Interactive components with action handling
|
|
319
|
+
- ContentModel to ComponentNode rendering pipeline
|
|
320
|
+
|
|
321
|
+
To run the examples:
|
|
322
|
+
|
|
323
|
+
```bash
|
|
324
|
+
cd examples/basic-demo
|
|
325
|
+
npm install
|
|
326
|
+
npm run dev
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### 🛠️ Development
|
|
330
|
+
|
|
331
|
+
#### Setup
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
# Install dependencies
|
|
335
|
+
pnpm install
|
|
336
|
+
|
|
337
|
+
# Build the library
|
|
338
|
+
pnpm run build
|
|
339
|
+
|
|
340
|
+
# Run type checking
|
|
341
|
+
pnpm run type-check
|
|
342
|
+
|
|
343
|
+
# Lint
|
|
344
|
+
pnpm run lint
|
|
345
|
+
|
|
346
|
+
# Format code
|
|
347
|
+
pnpm run format
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
#### Project Structure
|
|
351
|
+
|
|
352
|
+
```
|
|
353
|
+
zhama/a2ui/
|
|
354
|
+
├── src/
|
|
355
|
+
│ ├── types/ # TypeScript type definitions
|
|
356
|
+
│ ├── data/ # Data structures and utilities
|
|
357
|
+
│ ├── events/ # Event handling
|
|
358
|
+
│ ├── ui/ # React components
|
|
359
|
+
│ ├── context/ # React context providers
|
|
360
|
+
│ ├── styles/ # CSS and theme definitions
|
|
361
|
+
│ └── index.ts # Main entry point
|
|
362
|
+
├── examples/ # Example applications
|
|
363
|
+
│ └── basic-demo/ # Full-featured demo
|
|
364
|
+
├── dist/ # Build output
|
|
365
|
+
└── README.md
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### 🤝 Contributing
|
|
369
|
+
|
|
370
|
+
We welcome contributions! Please follow these guidelines:
|
|
371
|
+
|
|
372
|
+
1. **Fork** the repository
|
|
373
|
+
2. **Create** a feature branch (`git checkout -b feature/amazing-feature`)
|
|
374
|
+
3. **Commit** your changes (`git commit -m 'Add amazing feature'`)
|
|
375
|
+
4. **Push** to the branch (`git push origin feature/amazing-feature`)
|
|
376
|
+
5. **Open** a Pull Request
|
|
377
|
+
|
|
378
|
+
#### Development Guidelines
|
|
379
|
+
|
|
380
|
+
- Write clear, self-documenting code
|
|
381
|
+
- Follow the existing code style (enforced by ESLint/Prettier)
|
|
382
|
+
- Add tests for new features
|
|
383
|
+
- Update documentation as needed
|
|
384
|
+
- Ensure all tests pass before submitting PR
|
|
385
|
+
|
|
386
|
+
### 📄 License
|
|
387
|
+
|
|
388
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
389
|
+
|
|
390
|
+
### 🙏 Acknowledgments
|
|
391
|
+
|
|
392
|
+
- Inspired by [Google's A2UI Protocol](https://developers.google.com/assistant/conversational/a2ui)
|
|
393
|
+
- Built with [React](https://reactjs.org/) and [TypeScript](https://www.typescriptlang.org/)
|
|
394
|
+
- Styled with [Tailwind CSS](https://tailwindcss.com/)
|
|
395
|
+
|
|
396
|
+
### 📞 Support
|
|
397
|
+
|
|
398
|
+
- 🐛 **Issues**: [GitHub Issues](https://github.com/zhama/a2ui/issues)
|
|
399
|
+
- 💬 **Discussions**: [GitHub Discussions](https://github.com/zhama/a2ui/discussions)
|
|
400
|
+
- 📧 **Email**: support@zhama.com
|
|
401
|
+
- 🌐 **Website**: [https://zhama.com](https://zhama.com)
|
|
402
|
+
|
|
403
|
+
### ❓ FAQ
|
|
404
|
+
|
|
405
|
+
<details>
|
|
406
|
+
<summary><b>What's the difference between A2UI and regular React components?</b></summary>
|
|
407
|
+
|
|
408
|
+
A2UI components are designed to be generated dynamically by AI agents, not manually coded. They use a protocol-based approach where AI outputs structured data (ContentModel) that gets rendered as UI, rather than AI generating component code.
|
|
409
|
+
|
|
410
|
+
</details>
|
|
411
|
+
|
|
412
|
+
<details>
|
|
413
|
+
<summary><b>Can I use A2UI without AI / LLMs?</b></summary>
|
|
414
|
+
|
|
415
|
+
Yes! While designed for AI-driven UIs, you can manually create ComponentNode structures and render them with A2UIRoot. It's just more tedious than traditional React development.
|
|
416
|
+
|
|
417
|
+
</details>
|
|
418
|
+
|
|
419
|
+
<details>
|
|
420
|
+
<summary><b>How does this compare to shadcn/ui or other component libraries?</b></summary>
|
|
421
|
+
|
|
422
|
+
A2UI is protocol-first, designed for AI generation. shadcn/ui is developer-first, designed for manual coding. You can use both together - A2UI for AI-generated content, shadcn for developer-built features.
|
|
423
|
+
|
|
424
|
+
</details>
|
|
425
|
+
|
|
426
|
+
<details>
|
|
427
|
+
<summary><b>What LLM models work best with A2UI?</b></summary>
|
|
428
|
+
|
|
429
|
+
Any LLM that can output structured JSON works. We've tested with:
|
|
430
|
+
- GPT-4, GPT-3.5-turbo (OpenAI)
|
|
431
|
+
- Claude 3 (Anthropic)
|
|
432
|
+
- Gemini Pro (Google)
|
|
433
|
+
- Open-source models via local inference
|
|
434
|
+
|
|
435
|
+
</details>
|
|
436
|
+
|
|
437
|
+
<details>
|
|
438
|
+
<summary><b>Can I customize the styling?</b></summary>
|
|
439
|
+
|
|
440
|
+
Yes! A2UI uses Tailwind CSS and provides a theme system. You can override default styles through the A2UIProvider theme prop or by modifying Tailwind classes.
|
|
441
|
+
|
|
442
|
+
</details>
|
|
443
|
+
|
|
444
|
+
<details>
|
|
445
|
+
<summary><b>Is this production-ready?</b></summary>
|
|
446
|
+
|
|
447
|
+
Yes! A2UI is stable and being used in production applications. However, as with any v1.0 library, expect minor API refinements in future versions.
|
|
448
|
+
|
|
449
|
+
</details>
|
|
450
|
+
|
|
451
|
+
---
|
|
452
|
+
|
|
453
|
+
## 中文
|
|
454
|
+
|
|
455
|
+
### 📖 概述
|
|
456
|
+
|
|
457
|
+
A2UI(Agent-to-User Interface,智能体到用户界面)是一种协议,使 AI 智能体能够动态生成和控制用户界面。本库基于 Google 的 A2UI 协议规范提供了完整的 React 实现,允许 AI 智能体通过结构化数据而非纯文本响应来创建丰富的交互式 UI。
|
|
458
|
+
|
|
459
|
+
### 🎬 在线演示
|
|
460
|
+
|
|
461
|
+
查看 A2UI 的实际效果!演示展示了一个 AI 培训助手,它根据用户对话动态生成交互式 UI。
|
|
462
|
+
|
|
463
|
+
**亲自试用:**
|
|
464
|
+
```bash
|
|
465
|
+
cd examples/basic-demo
|
|
466
|
+
npm install
|
|
467
|
+
npm run dev
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
然后打开 http://localhost:5174 并尝试以下命令:
|
|
471
|
+
- 输入 `hello` - 获取带主题卡片的欢迎屏幕
|
|
472
|
+
- 点击主题按钮 - 查看结构化学习路径
|
|
473
|
+
- 输入 `practice` - 获取交互式练习题
|
|
474
|
+
- 输入 `progress` - 查看学习统计
|
|
475
|
+
|
|
476
|
+
**你将看到:**
|
|
477
|
+
|
|
478
|
+
<table>
|
|
479
|
+
<tr>
|
|
480
|
+
<td width="33%" align="center">
|
|
481
|
+
<img src="docs/welcome.jpg" alt="欢迎界面" width="100%"/>
|
|
482
|
+
<br/>
|
|
483
|
+
<strong>欢迎界面</strong>
|
|
484
|
+
<br/>
|
|
485
|
+
<em>AI 生成带操作的主题卡片</em>
|
|
486
|
+
</td>
|
|
487
|
+
<td width="33%" align="center">
|
|
488
|
+
<img src="docs/learning.jpg" alt="学习路径" width="100%"/>
|
|
489
|
+
<br/>
|
|
490
|
+
<strong>学习路径</strong>
|
|
491
|
+
<br/>
|
|
492
|
+
<em>带章节的结构化内容</em>
|
|
493
|
+
</td>
|
|
494
|
+
<td width="33%" align="center">
|
|
495
|
+
<img src="docs/answer.jpg" alt="练习题" width="100%"/>
|
|
496
|
+
<br/>
|
|
497
|
+
<strong>练习题</strong>
|
|
498
|
+
<br/>
|
|
499
|
+
<em>带按钮的交互式问题</em>
|
|
500
|
+
</td>
|
|
501
|
+
</tr>
|
|
502
|
+
</table>
|
|
503
|
+
|
|
504
|
+
> 💡 **注意:** 上面所有 UI 都是由 AI 智能体使用 A2UI 协议动态生成的 - 没有硬编码组件!
|
|
505
|
+
|
|
506
|
+
### ✨ 特性
|
|
507
|
+
|
|
508
|
+
- 🤖 **AI 优先设计** - 专为 AI 驱动的 UI 生成而构建
|
|
509
|
+
- 📦 **完整协议支持** - 完整实现 A2UI 协议规范
|
|
510
|
+
- 🎨 **主题化** - 基于 Tailwind CSS 的可定制主题系统
|
|
511
|
+
- 🧩 **丰富组件** - 全面的 UI 组件集(Text、Button、Card、List、TextField 等)
|
|
512
|
+
- 🔌 **可扩展** - 支持自定义组件注册
|
|
513
|
+
- 📱 **响应式** - 移动优先的响应式设计
|
|
514
|
+
- ⚡ **高性能** - 遵循 React 最佳实践的优化渲染
|
|
515
|
+
- 🎯 **类型安全** - 完整的 TypeScript 支持和全面的类型定义
|
|
516
|
+
- 🌐 **框架无关** - 核心协议可适配其他框架
|
|
517
|
+
- 🎭 **事件处理** - 内置交互组件的动作处理系统
|
|
518
|
+
|
|
519
|
+
### 🚀 快速开始
|
|
520
|
+
|
|
521
|
+
#### 安装
|
|
522
|
+
|
|
523
|
+
```bash
|
|
524
|
+
npm install @zhama/a2ui
|
|
525
|
+
# 或
|
|
526
|
+
yarn add @zhama/a2ui
|
|
527
|
+
# 或
|
|
528
|
+
pnpm add @zhama/a2ui
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
#### 对等依赖
|
|
532
|
+
|
|
533
|
+
```bash
|
|
534
|
+
npm install react react-dom react-markdown
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
#### 基础设置
|
|
538
|
+
|
|
539
|
+
1. **配置 Tailwind CSS**
|
|
540
|
+
|
|
541
|
+
```js
|
|
542
|
+
// tailwind.config.js
|
|
543
|
+
import a2uiPreset from '@zhama/a2ui/tailwind.preset';
|
|
544
|
+
|
|
545
|
+
export default {
|
|
546
|
+
presets: [a2uiPreset],
|
|
547
|
+
content: [
|
|
548
|
+
'./src/**/*.{js,ts,jsx,tsx}',
|
|
549
|
+
'./node_modules/@zhama/a2ui/**/*.{js,ts,jsx,tsx}',
|
|
550
|
+
],
|
|
551
|
+
};
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
2. **导入 CSS 变量**
|
|
555
|
+
|
|
556
|
+
```css
|
|
557
|
+
/* globals.css */
|
|
558
|
+
@import '@zhama/a2ui/styles/a2ui-variables.css';
|
|
559
|
+
@import '@zhama/a2ui/styles/a2ui.css';
|
|
560
|
+
|
|
561
|
+
@tailwind base;
|
|
562
|
+
@tailwind components;
|
|
563
|
+
@tailwind utilities;
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
3. **使用 A2UIRoot 组件**
|
|
567
|
+
|
|
568
|
+
```tsx
|
|
569
|
+
import { A2UIRoot } from '@zhama/a2ui';
|
|
570
|
+
import type { AnyComponentNode } from '@zhama/a2ui/types';
|
|
571
|
+
|
|
572
|
+
function App() {
|
|
573
|
+
const components: AnyComponentNode[] = [
|
|
574
|
+
{
|
|
575
|
+
id: 'text-1',
|
|
576
|
+
type: 'Text',
|
|
577
|
+
properties: {
|
|
578
|
+
text: { literal: 'Hello from A2UI!' },
|
|
579
|
+
usageHint: { literal: 'h1' },
|
|
580
|
+
},
|
|
581
|
+
},
|
|
582
|
+
{
|
|
583
|
+
id: 'button-1',
|
|
584
|
+
type: 'Button',
|
|
585
|
+
properties: {
|
|
586
|
+
child: {
|
|
587
|
+
id: 'btn-text',
|
|
588
|
+
type: 'Text',
|
|
589
|
+
properties: {
|
|
590
|
+
text: { literal: '点击我' },
|
|
591
|
+
},
|
|
592
|
+
},
|
|
593
|
+
action: {
|
|
594
|
+
type: 'postback',
|
|
595
|
+
payload: 'button_clicked',
|
|
596
|
+
},
|
|
597
|
+
},
|
|
598
|
+
},
|
|
599
|
+
];
|
|
600
|
+
|
|
601
|
+
const handleAction = (event) => {
|
|
602
|
+
console.log('动作触发:', event);
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
return (
|
|
606
|
+
<A2UIRoot
|
|
607
|
+
childComponents={components}
|
|
608
|
+
processor={null}
|
|
609
|
+
surfaceId={null}
|
|
610
|
+
onAction={handleAction}
|
|
611
|
+
/>
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
```
|
|
615
|
+
|
|
616
|
+
### 📦 包导出
|
|
617
|
+
|
|
618
|
+
库提供多个入口点用于不同用例:
|
|
619
|
+
|
|
620
|
+
```typescript
|
|
621
|
+
// 主入口 - 所有内容
|
|
622
|
+
import { A2UIRoot, createButton, createText } from '@zhama/a2ui';
|
|
623
|
+
|
|
624
|
+
// 仅类型定义
|
|
625
|
+
import type { AnyComponentNode, ButtonNode } from '@zhama/a2ui/types';
|
|
626
|
+
|
|
627
|
+
// 数据工具
|
|
628
|
+
import { A2UIRegistry } from '@zhama/a2ui/data';
|
|
629
|
+
|
|
630
|
+
// 事件类型
|
|
631
|
+
import type { A2UIAction } from '@zhama/a2ui/events';
|
|
632
|
+
|
|
633
|
+
// 样式
|
|
634
|
+
import '@zhama/a2ui/styles';
|
|
635
|
+
|
|
636
|
+
// 单独的 UI 组件
|
|
637
|
+
import { Button, Card, List } from '@zhama/a2ui/ui';
|
|
638
|
+
|
|
639
|
+
// Context 提供者
|
|
640
|
+
import { A2UIProvider, useA2UI } from '@zhama/a2ui/context';
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
### 🎨 主题化
|
|
644
|
+
|
|
645
|
+
A2UI 自带可定制的主题系统:
|
|
646
|
+
|
|
647
|
+
```tsx
|
|
648
|
+
import { A2UIProvider } from '@zhama/a2ui/context';
|
|
649
|
+
import { defaultTheme } from '@zhama/a2ui/styles/default-theme';
|
|
650
|
+
|
|
651
|
+
const customTheme = {
|
|
652
|
+
...defaultTheme,
|
|
653
|
+
components: {
|
|
654
|
+
...defaultTheme.components,
|
|
655
|
+
Button: 'custom-button-class',
|
|
656
|
+
},
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
function App() {
|
|
660
|
+
return (
|
|
661
|
+
<A2UIProvider theme={customTheme}>
|
|
662
|
+
<YourApp />
|
|
663
|
+
</A2UIProvider>
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
### 🧩 可用组件
|
|
669
|
+
|
|
670
|
+
| 组件 | 描述 | 使用场景 |
|
|
671
|
+
|------|------|---------|
|
|
672
|
+
| **Text** | 支持 markdown 的格式化文本渲染 | 标题、段落、说明文字 |
|
|
673
|
+
| **Button** | 支持动作的交互按钮 | 用户操作、导航 |
|
|
674
|
+
| **Card** | 分组内容的容器 | 内容分组、布局 |
|
|
675
|
+
| **List** | 垂直或水平列表布局 | 多项目展示 |
|
|
676
|
+
| **Row** | 水平布局容器 | 并排元素 |
|
|
677
|
+
| **Column** | 垂直布局容器 | 堆叠元素 |
|
|
678
|
+
| **TextField** | 文本输入框 | 用户输入、表单 |
|
|
679
|
+
| **CheckBox** | 复选框输入 | 布尔选择 |
|
|
680
|
+
| **Image** | 图片显示 | 视觉内容 |
|
|
681
|
+
| **Tabs** | 选项卡界面 | 多视图 |
|
|
682
|
+
| **Surface** | 带高度的容器 | 模态对话框、面板 |
|
|
683
|
+
|
|
684
|
+
### 💼 使用场景
|
|
685
|
+
|
|
686
|
+
A2UI 非常适合 AI 智能体需要提供丰富交互体验的场景:
|
|
687
|
+
|
|
688
|
+
#### 🎓 **AI 培训与教育**
|
|
689
|
+
- 动态课程内容生成
|
|
690
|
+
- 交互式测验和评估
|
|
691
|
+
- 个性化学习路径
|
|
692
|
+
- 进度追踪仪表板
|
|
693
|
+
|
|
694
|
+
#### 🤖 **对话式 AI 助手**
|
|
695
|
+
- 带表单填写的客户支持
|
|
696
|
+
- 带卡片的产品推荐
|
|
697
|
+
- 多步骤工作流程
|
|
698
|
+
- 数据可视化
|
|
699
|
+
|
|
700
|
+
#### 📊 **商业应用**
|
|
701
|
+
- 带图表的报告生成
|
|
702
|
+
- 数据录入表单
|
|
703
|
+
- 审批工作流程
|
|
704
|
+
- 仪表板创建
|
|
705
|
+
|
|
706
|
+
#### 🛠️ **开发工具**
|
|
707
|
+
- 带语法高亮的代码生成
|
|
708
|
+
- 交互式文档
|
|
709
|
+
- 配置向导
|
|
710
|
+
- 调试界面
|
|
711
|
+
|
|
712
|
+
### 🏗️ 架构
|
|
713
|
+
|
|
714
|
+
A2UI 采用受现代 AI 应用启发的两层架构:
|
|
715
|
+
|
|
716
|
+
1. **ContentModel 层**(高级语义)
|
|
717
|
+
- 基于场景的内容结构(欢迎、学习、练习等)
|
|
718
|
+
- 业务逻辑表示
|
|
719
|
+
- LLM 友好格式 - AI 输出有意义的场景,而非 UI 组件
|
|
720
|
+
- 易于理解和修改
|
|
721
|
+
|
|
722
|
+
2. **A2UI 协议层**(低级 UI)
|
|
723
|
+
- ComponentNode 结构(Button、Card、List 等)
|
|
724
|
+
- UI 渲染规范
|
|
725
|
+
- 框架实现
|
|
726
|
+
- 为渲染优化
|
|
727
|
+
|
|
728
|
+
**流程:**
|
|
729
|
+
```
|
|
730
|
+
┌─────────────┐ ┌──────────────┐ ┌─────────────┐ ┌──────────┐
|
|
731
|
+
│ LLM 输出 │ → │ ContentModel │ → │ 场景 │ → │ A2UI UI │
|
|
732
|
+
│ (JSON) │ │ (语义) │ │ 渲染器 │ │ (React) │
|
|
733
|
+
└─────────────┘ └──────────────┘ └─────────────┘ └──────────┘
|
|
734
|
+
↓ ↓
|
|
735
|
+
场景: "welcome" ComponentNodes
|
|
736
|
+
主题: [...] [Card, Button, ...]
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
**优势:**
|
|
740
|
+
- ✅ **关注点分离**: LLM 专注于内容,而非 UI 细节
|
|
741
|
+
- ✅ **灵活性**: 更改 UI 渲染而无需更改 LLM 提示词
|
|
742
|
+
- ✅ **可测试性**: 独立测试内容逻辑和 UI
|
|
743
|
+
- ✅ **可维护性**: 更容易更新和扩展
|
|
744
|
+
|
|
745
|
+
### 📚 示例
|
|
746
|
+
|
|
747
|
+
查看 `/examples` 目录获取完整示例:
|
|
748
|
+
|
|
749
|
+
- **basic-demo**: 展示所有 A2UI 功能的完整演示
|
|
750
|
+
- 包含 7 个不同场景(欢迎、学习、练习、反馈、进度、对话)
|
|
751
|
+
- 带动作处理的交互组件
|
|
752
|
+
- ContentModel 到 ComponentNode 的渲染流程
|
|
753
|
+
|
|
754
|
+
运行示例:
|
|
755
|
+
|
|
756
|
+
```bash
|
|
757
|
+
cd examples/basic-demo
|
|
758
|
+
npm install
|
|
759
|
+
npm run dev
|
|
760
|
+
```
|
|
761
|
+
|
|
762
|
+
### 🛠️ 开发
|
|
763
|
+
|
|
764
|
+
#### 设置
|
|
765
|
+
|
|
766
|
+
```bash
|
|
767
|
+
# 安装依赖
|
|
768
|
+
pnpm install
|
|
769
|
+
|
|
770
|
+
# 构建库
|
|
771
|
+
pnpm run build
|
|
772
|
+
|
|
773
|
+
# 运行类型检查
|
|
774
|
+
pnpm run type-check
|
|
775
|
+
|
|
776
|
+
# 代码检查
|
|
777
|
+
pnpm run lint
|
|
778
|
+
|
|
779
|
+
# 格式化代码
|
|
780
|
+
pnpm run format
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
#### 项目结构
|
|
784
|
+
|
|
785
|
+
```
|
|
786
|
+
zhama/a2ui/
|
|
787
|
+
├── src/
|
|
788
|
+
│ ├── types/ # TypeScript 类型定义
|
|
789
|
+
│ ├── data/ # 数据结构和工具
|
|
790
|
+
│ ├── events/ # 事件处理
|
|
791
|
+
│ ├── ui/ # React 组件
|
|
792
|
+
│ ├── context/ # React context 提供者
|
|
793
|
+
│ ├── styles/ # CSS 和主题定义
|
|
794
|
+
│ └── index.ts # 主入口点
|
|
795
|
+
├── examples/ # 示例应用
|
|
796
|
+
│ └── basic-demo/ # 完整功能演示
|
|
797
|
+
├── dist/ # 构建输出
|
|
798
|
+
└── README.md
|
|
799
|
+
```
|
|
800
|
+
|
|
801
|
+
### 🤝 贡献
|
|
802
|
+
|
|
803
|
+
我们欢迎贡献!请遵循以下指南:
|
|
804
|
+
|
|
805
|
+
1. **Fork** 仓库
|
|
806
|
+
2. **创建** 特性分支 (`git checkout -b feature/amazing-feature`)
|
|
807
|
+
3. **提交** 你的更改 (`git commit -m 'Add amazing feature'`)
|
|
808
|
+
4. **推送** 到分支 (`git push origin feature/amazing-feature`)
|
|
809
|
+
5. **打开** Pull Request
|
|
810
|
+
|
|
811
|
+
#### 开发指南
|
|
812
|
+
|
|
813
|
+
- 编写清晰、自文档化的代码
|
|
814
|
+
- 遵循现有代码风格(由 ESLint/Prettier 强制执行)
|
|
815
|
+
- 为新功能添加测试
|
|
816
|
+
- 根据需要更新文档
|
|
817
|
+
- 提交 PR 前确保所有测试通过
|
|
818
|
+
|
|
819
|
+
### 📄 许可证
|
|
820
|
+
|
|
821
|
+
本项目采用 MIT 许可证 - 详见 [LICENSE](./LICENSE) 文件。
|
|
822
|
+
|
|
823
|
+
### 🙏 致谢
|
|
824
|
+
|
|
825
|
+
- 受 [Google 的 A2UI 协议](https://developers.google.com/assistant/conversational/a2ui)启发
|
|
826
|
+
- 使用 [React](https://reactjs.org/) 和 [TypeScript](https://www.typescriptlang.org/) 构建
|
|
827
|
+
- 使用 [Tailwind CSS](https://tailwindcss.com/) 样式化
|
|
828
|
+
|
|
829
|
+
### 📞 支持
|
|
830
|
+
|
|
831
|
+
- 🐛 **问题反馈**: [GitHub Issues](https://github.com/zhama-ai/a2ui-react/issues)
|
|
832
|
+
- 💬 **讨论**: [GitHub Discussions](https://github.com/zhama-ai/a2ui-react/discussions)
|
|
833
|
+
- 📧 **邮箱**: support@zhama.com
|
|
834
|
+
|
|
835
|
+
### ❓ 常见问题
|
|
836
|
+
|
|
837
|
+
<details>
|
|
838
|
+
<summary><b>A2UI 和普通 React 组件有什么区别?</b></summary>
|
|
839
|
+
|
|
840
|
+
A2UI 组件专为 AI 智能体动态生成而设计,而非手动编码。它使用基于协议的方法,AI 输出结构化数据(ContentModel)并渲染为 UI,而非 AI 生成组件代码。
|
|
841
|
+
|
|
842
|
+
</details>
|
|
843
|
+
|
|
844
|
+
<details>
|
|
845
|
+
<summary><b>可以不使用 AI / LLM 就使用 A2UI 吗?</b></summary>
|
|
846
|
+
|
|
847
|
+
可以!虽然专为 AI 驱动的 UI 设计,你也可以手动创建 ComponentNode 结构并使用 A2UIRoot 渲染。只是比传统 React 开发更繁琐。
|
|
848
|
+
|
|
849
|
+
</details>
|
|
850
|
+
|
|
851
|
+
<details>
|
|
852
|
+
<summary><b>这和 shadcn/ui 或其他组件库相比如何?</b></summary>
|
|
853
|
+
|
|
854
|
+
A2UI 是协议优先,专为 AI 生成设计。shadcn/ui 是开发者优先,专为手动编码设计。你可以同时使用 - A2UI 用于 AI 生成内容,shadcn 用于开发者构建功能。
|
|
855
|
+
|
|
856
|
+
</details>
|
|
857
|
+
|
|
858
|
+
<details>
|
|
859
|
+
<summary><b>哪些 LLM 模型最适合 A2UI?</b></summary>
|
|
860
|
+
|
|
861
|
+
任何能输出结构化 JSON 的 LLM 都可以。我们已测试过:
|
|
862
|
+
- GPT-4, GPT-3.5-turbo (OpenAI)
|
|
863
|
+
- Claude 3 (Anthropic)
|
|
864
|
+
- Gemini Pro (Google)
|
|
865
|
+
- 通过本地推理的开源模型
|
|
866
|
+
|
|
867
|
+
</details>
|
|
868
|
+
|
|
869
|
+
<details>
|
|
870
|
+
<summary><b>可以自定义样式吗?</b></summary>
|
|
871
|
+
|
|
872
|
+
可以!A2UI 使用 Tailwind CSS 并提供主题系统。你可以通过 A2UIProvider 的 theme 属性覆盖默认样式,或修改 Tailwind 类。
|
|
873
|
+
|
|
874
|
+
</details>
|
|
875
|
+
|
|
876
|
+
<details>
|
|
877
|
+
<summary><b>这个库生产可用吗?</b></summary>
|
|
878
|
+
|
|
879
|
+
可以!A2UI 稳定且已在生产应用中使用。但作为 v1.0 库,未来版本可能会有小的 API 改进。
|
|
880
|
+
|
|
881
|
+
</details>
|
|
882
|
+
|
|
883
|
+
---
|
|
884
|
+
|
|
885
|
+
<div align="center">
|
|
886
|
+
|
|
887
|
+
Made with ❤️ by [Zhama AI](https://zhama.com)
|
|
888
|
+
|
|
889
|
+
**如果你觉得有帮助,请给个 Star ⭐!**
|
|
890
|
+
|
|
891
|
+
</div>
|