react-ai-renderer 0.1.4 → 0.1.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.
package/README.md CHANGED
@@ -1,25 +1,50 @@
1
- # React AI Renderer
1
+ # React AI Renderer - AI 应用的专属渲染器
2
2
 
3
3
  [English](./README_en.md) | 中文
4
4
 
5
- 一个专为 AI 应用程序设计的 React 渲染器,支持流式 Markdown MDX 内容渲染,提供代码高亮、数学公式、Mermaid 图表等增强功能。
5
+ > 一个专为 AI 应用程序设计的 UI 渲染器,可以极大增强 AI 与前端的交互方式。支持流式 JSX/XML 渲染、自定义组件、生命周期钩子,让 AI 输出直接变为交互式 UI。
6
6
 
7
- ## 📋 环境要求
8
7
 
9
- - Node.js 20.x 或更高版本
10
- - React 18.x 或更高版本
8
+ <p align="center">
9
+ <img src="./examples/demo-assets/react-ai-renderer.gif" alt="React AI Renderer Demo" width="800" />
10
+ </p>
11
+ <p align="center"><em>React AI Renderer - 实现 AI 输出到交互式 UI 的无缝转换</em></p>
11
12
 
12
- ## 🌟 特性
13
+ ## 🚀 项目愿景
13
14
 
14
- - **流式渲染**:支持逐步渲染 AI 流式输出的 Markdown/MDX 内容
15
+ AI 应用井喷式爆发的今天,以 Chat Bot 为核心的交互已成为主流。然而,传统的文本化输出方式远远无法满足现代 AI 应用对视觉渲染效果的需求。React AI Renderer 提供了完美的解决方案:
16
+
17
+ ### 传统方式的痛点
18
+
19
+ 1. **等待问题**:LLM 是流式输出的,但 JSON 必须等流结束才能完整交付给前端渲染
20
+ 2. **构建成本高**:构建 JSON 和约定渲染本身成本较高,也容易出错
21
+ 3. **缺乏生命周期管理**:无法在渲染之后触发动作,传统 Markdown 渲染器无法满足复杂交互需求
22
+
23
+ ### React AI Renderer 的解决方案
24
+
25
+ React AI Renderer 彻底解决了上述问题,让 AI 可以直接输出标准的 JSX/XML 语法糖,由前端实时渲染为交互式 UI,同时支持完整的生命周期管理。
26
+
27
+ ## 🌟 核心特性
28
+
29
+ ### ✅ 无缝兼容 React Markdown
30
+ 只需将引入名称从 `react-markdown` 改为 `react-ai-renderer`,即可无感知继承现有能力,零成本迁移。
31
+
32
+ ### ✅ 自定义组件渲染
33
+ 支持基于项目已有的前端 UI 进行渲染,无限拉高产品体验上限,复用现有视觉主题。
34
+
35
+ ### ✅ 真正的流式渲染
36
+ 动态 token 对应动态解析,无需像 JSON 格式那样等待模型输出完毕才能看到视觉效果。
37
+
38
+ ### ✅ 组件生命周期钩子
39
+ 单一组件渲染完毕后可触发下一个事件,轻松实现文件列表+文件内容左右分栏等复杂交互效果。
40
+
41
+ ### ✅ 丰富的内置组件
15
42
  - **AI 推理展示**:内置 Think 组件展示 AI 思考过程
16
43
  - **代码高亮**:支持多种编程语言的代码高亮显示
17
44
  - **数学公式**:使用 KaTeX 渲染数学公式
18
- - **Mermaid 图表**:支持 Mermaid 流程图、时序图等(可选功能)
19
- - **自定义组件**:支持用户注册自定义组件和生命周期钩子
20
- - **兼容性**:兼容现代 React 版本,提供 TypeScript 类型定义
45
+ - **Mermaid 图表**:支持流程图、时序图等(可选功能)
21
46
 
22
- ## 📦 安装
47
+ ## 📦 安装使用
23
48
 
24
49
  ```bash
25
50
  npm install react-ai-renderer
@@ -41,9 +66,7 @@ npm install mermaid
41
66
 
42
67
  如果不安装 mermaid,图表将不会渲染,但不会影响其他功能。
43
68
 
44
- ## 🚀 快速开始
45
-
46
- 查看 [examples/example.tsx](./examples/example.tsx) 文件了解完整的使用示例。
69
+ ## 🎯 快速开始
47
70
 
48
71
  ### 基础使用
49
72
 
@@ -51,8 +74,9 @@ npm install mermaid
51
74
  import ReactAIRenderer from 'react-ai-renderer';
52
75
 
53
76
  const content = `
54
- # 标题
55
- 这是一个段落。
77
+ # 欢迎使用 React AI Renderer
78
+
79
+ 这是一个 **粗体** 文本和 *斜体* 文本。
56
80
 
57
81
  \`\`\`javascript
58
82
  console.log('Hello World');
@@ -64,288 +88,242 @@ function App() {
64
88
  }
65
89
  ```
66
90
 
67
- ### AI 推理展示
91
+ ### AI 流式输出渲染
68
92
 
69
93
  ```tsx
70
94
  import ReactAIRenderer from 'react-ai-renderer';
71
95
 
72
- const content = `
73
- <Think>
96
+ // 模拟 AI 流式输出内容
97
+ const aiContent = `
74
98
  正在分析用户需求...
75
99
 
76
- 根据分析结果,我建议您使用以下方案:
77
-
78
- 1. 方案A
79
- 2. 方案B
80
- </Think>
100
+ <Table>
101
+ <TableHeader>
102
+ <TableCell header>产品名称</TableCell>
103
+ <TableCell header>价格</TableCell>
104
+ <TableCell header>库存</TableCell>
105
+ </TableHeader>
106
+ <TableRow>
107
+ <TableCell>iPhone 15</TableCell>
108
+ <TableCell>¥5999</TableCell>
109
+ <TableCell>50</TableCell>
110
+ </TableRow>
111
+ </Table>
81
112
  `;
82
113
 
83
114
  function App() {
84
- return <ReactAIRenderer>{content}</ReactAIRenderer>;
115
+ return <ReactAIRenderer>{aiContent}</ReactAIRenderer>;
85
116
  }
86
117
  ```
87
118
 
88
- ### 自定义组件
119
+ ### 自定义组件与生命周期
89
120
 
90
121
  ```tsx
91
122
  import ReactAIRenderer from 'react-ai-renderer';
92
123
 
93
- const customComponents = {
94
- Button: (props) => <button {...props} style={{ backgroundColor: 'blue', color: 'white' }} />
95
- };
124
+ // 自定义按钮组件
125
+ const Button = ({ onClick, children, style }) => (
126
+ <button
127
+ onClick={onClick}
128
+ style={{
129
+ backgroundColor: 'blue',
130
+ color: 'white',
131
+ padding: '8px 16px',
132
+ border: 'none',
133
+ borderRadius: '4px',
134
+ cursor: 'pointer',
135
+ ...style
136
+ }}
137
+ >
138
+ {children}
139
+ </button>
140
+ );
141
+
142
+ // 组件生命周期钩子
143
+ const componentHandlers = [
144
+ {
145
+ name: 'Button',
146
+ component: Button,
147
+ selfClosing: false,
148
+ onRender: (item, scope) => {
149
+ console.log('Button 组件已渲染完成');
150
+ // 可以在这里触发下一个动作
151
+ // 例如:更新状态、发送请求等
152
+ }
153
+ }
154
+ ];
96
155
 
97
156
  const content = `
98
157
  <Button onClick={() => alert('点击了按钮')}>点击我</Button>
99
158
  `;
100
159
 
101
160
  function App() {
102
- return <ReactAIRenderer components={customComponents}>{content}</ReactAIRenderer>;
161
+ return (
162
+ <ReactAIRenderer
163
+ components={{ Button }}
164
+ componentHandlers={componentHandlers}
165
+ >
166
+ {content}
167
+ </ReactAIRenderer>
168
+ );
103
169
  }
104
170
  ```
105
171
 
106
- ### 图表渲染
107
-
108
- ```tsx
109
- import ReactAIRenderer from 'react-ai-renderer';
110
-
111
- const content = `
112
- \`\`\`mermaid
113
- graph TD
114
- A[开始] --> B[处理]
115
- B --> C[结束]
116
- \`\`\`
117
- `;
118
-
119
- function App() {
120
- return <ReactAIRenderer>{content}</ReactAIRenderer>;
121
- }
122
- ```
123
-
124
- ## 🎨 UI 组件示例
125
-
126
- React AI Renderer 支持丰富的 UI 组件渲染,包括表单、表格、卡片等常见组件。
127
-
128
- ### 表单组件
129
-
130
- ```mdx
131
- <form>
132
- <Input label="姓名" placeholder="请输入姓名" />
133
- <Input label="邮箱" type="email" placeholder="请输入邮箱" />
134
- <Select label="性别">
135
- <option value="">请选择</option>
136
- <option value="male">男</option>
137
- <option value="female">女</option>
138
- </Select>
139
- <Textarea label="个人简介" placeholder="请简单介绍一下自己" />
140
- <Checkbox label="同意用户协议" />
141
- <Button type="submit">提交</Button>
142
- <Button type="button" style="backgroundColor: #6c757d">取消</Button>
143
- </form>
144
- ```
145
-
146
- ### 表格组件
147
-
148
- ```mdx
149
- <Table>
150
- <TableHeader>
151
- <TableCell header>产品名称</TableCell>
152
- <TableCell header>价格</TableCell>
153
- <TableCell header>库存</TableCell>
154
- <TableCell header>状态</TableCell>
155
- </TableHeader>
156
- <TableRow>
157
- <TableCell>iPhone 15</TableCell>
158
- <TableCell>¥5999</TableCell>
159
- <TableCell>50</TableCell>
160
- <TableCell>在售</TableCell>
161
- </TableRow>
162
- <TableRow>
163
- <TableCell>MacBook Pro</TableCell>
164
- <TableCell>¥12999</TableCell>
165
- <TableCell>20</TableCell>
166
- <TableCell>在售</TableCell>
167
- </TableRow>
168
- <TableRow>
169
- <TableCell>iPad Air</TableCell>
170
- <TableCell>¥4399</TableCell>
171
- <TableCell>0</TableCell>
172
- <TableCell>缺货</TableCell>
173
- </TableRow>
174
- </Table>
175
- ```
176
-
177
- ### 卡片组件
178
-
179
- ```mdx
180
- <Card>
181
- <CardTitle>MacBook Pro</CardTitle>
182
- <CardContent>
183
- <p>强大的性能,适用于专业工作负载</p>
184
- <p><strong>价格:</strong> ¥12999</p>
185
- <p><strong>库存:</strong> 20台</p>
186
- </CardContent>
187
- <CardActions>
188
- <Button>加入购物车</Button>
189
- <Button>查看详情</Button>
190
- <Button style="backgroundColor: #28a745">立即购买</Button>
191
- </CardActions>
192
- </Card>
193
- ```
194
-
195
- ## 🛠️ API
172
+ ## 🛠️ API 参考
196
173
 
197
174
  ### ReactAIRenderer 组件
198
175
 
199
- 主渲染组件,兼容 ReactMarkdown API。
176
+ 主渲染组件,完全兼容 ReactMarkdown API。
200
177
 
201
- ```tsx
202
- interface ReactAIRendererProps {
203
- children: string;
204
- components?: Record<string, React.ComponentType<any>>;
205
- componentHandlers?: ComponentHandler[];
206
- remarkPlugins?: any[];
207
- rehypePlugins?: any[];
208
- scope?: any;
209
- [key: string]: any;
210
- }
211
- ```
178
+ Props:
179
+ - `content`: Markdown/MDX 内容字符串
180
+ - `scope`: 传递给 MDX 的作用域对象
181
+ - `components`: 自定义组件映射
182
+ - `componentHandlers`: 组件处理器数组(支持生命周期钩子)
183
+ - `children`: 作为内容的子元素(替代 content)
212
184
 
213
- ### ComponentHandler 接口
185
+ ### 组件生命周期钩子
214
186
 
215
- 用于注册自定义组件和生命周期钩子。
187
+ 通过 `componentHandlers` 属性可以为组件添加生命周期钩子:
216
188
 
217
189
  ```tsx
218
- interface ComponentHandler {
219
- component: React.ComponentType<any>;
220
- name: string;
221
- selfClosing: boolean;
222
- onRender?: (item: ComponentData, scope: any) => void;
223
- }
190
+ const componentHandlers = [
191
+ {
192
+ name: 'MyComponent',
193
+ component: MyComponent,
194
+ selfClosing: false,
195
+ onRender: (item, scope) => {
196
+ // 组件渲染完成后的回调
197
+ console.log('组件已渲染:', item);
198
+ }
199
+ }
200
+ ];
224
201
  ```
225
202
 
226
- ## 🧩 内置组件
203
+ ## 性能优势
227
204
 
228
- ### Think 组件
205
+ ### 实时渲染
206
+ - 流式解析,无需等待完整内容
207
+ - 动态组件渲染,提升用户体验
208
+ - 内存优化,避免重复渲染
229
209
 
230
- 用于展示 AI 推理过程的组件。使用 `<Think>` 标签包围需要展示为推理过程的内容。
210
+ ### 兼容性保障
211
+ - 完全兼容 React 18+
212
+ - 支持 TypeScript 类型安全
213
+ - 无依赖冲突,轻松集成
231
214
 
232
- ```tsx
233
- <Think>
234
- 正在分析用户需求...
235
- </Think>
236
- ```
215
+ ## 💡 应用场景
237
216
 
238
- Think 组件会自动识别 `<Think>` 标签并将其内容渲染为可折叠的推理过程展示。
217
+ ### 1. AI 助手应用
218
+ 让 AI 助手直接输出交互式 UI,提升用户体验
239
219
 
240
- ### CodeHighlight 组件
220
+ ### 2. 代码生成工具
221
+ AI 生成的代码可以直接渲染为可交互的组件
241
222
 
242
- 支持多种编程语言的代码高亮显示。
223
+ ### 3. 数据分析报告
224
+ AI 分析结果可直接渲染为图表和表格
243
225
 
244
- ### MermaidRenderer 组件
226
+ ### 4. 教育培训系统
227
+ AI 生成的教学内容可直接渲染为交互式课件
245
228
 
246
- 动态加载和渲染 Mermaid 图表。
229
+ ### 5. 智能文档系统
230
+ AI 生成的文档可直接渲染为富交互页面
247
231
 
248
- > 注意:Mermaid 是一个 peer dependency,需要在项目中单独安装。如果未安装 mermaid,图表将显示为代码块并提示安装信息。
232
+ ### 6. 低代码平台
233
+ AI 辅助生成界面,直接渲染为可用组件
249
234
 
250
- ## 🏗️ 构建配置
235
+ ### 7. 自动化运维
236
+ AI 分析结果可直接渲染为操作界面
251
237
 
252
- 使用 Rollup 进行构建,输出 UMD 和 ESM 两种格式。
238
+ ## 📖 示例运行
253
239
 
254
- ### 构建命令
240
+ 运行示例:
255
241
 
256
242
  ```bash
257
- npm run build
243
+ npm run example
258
244
  ```
259
245
 
260
- ### 开发模式
246
+ 然后在浏览器中访问 http://localhost:3000
247
+
248
+ ## 🧪 测试
261
249
 
262
250
  ```bash
263
- npm run dev
251
+ npm test
264
252
  ```
265
253
 
266
- ### 运行示例
254
+ ## 🚀 高级特性
267
255
 
268
- #### 浏览器示例(推荐)
256
+ ### 流式渲染优化
257
+ React AI Renderer 采用先进的流式解析技术,能够在 AI 模型输出的同时实时渲染内容,无需等待完整输出。
269
258
 
270
- 构建项目和示例:
259
+ ### 组件注册机制
260
+ 支持动态组件注册,可以灵活扩展自定义组件:
271
261
 
272
- ```bash
273
- npm run build
274
- npm run build:examples
275
- ```
276
-
277
- 然后使用以下命令启动示例:
262
+ ```tsx
263
+ const customComponents = {
264
+ MyButton: ({ onClick, children }) => (
265
+ <button onClick={onClick} className="custom-button">
266
+ {children}
267
+ </button>
268
+ )
269
+ };
278
270
 
279
- ```bash
280
- npm run example
271
+ <ReactAIRenderer components={customComponents}>
272
+ {content}
273
+ </ReactAIRenderer>
281
274
  ```
282
275
 
283
- 这将启动一个本地服务器,并在浏览器中打开示例页面。
276
+ ### 作用域传递
277
+ 支持向组件传递作用域变量,实现更复杂的数据交互:
284
278
 
285
- 您也可以直接在浏览器中打开 [examples/example.html](./examples/example.html) 文件查看示例效果。
279
+ ```tsx
280
+ const scope = {
281
+ userData: { name: "张三", age: 25 },
282
+ onAction: (data) => console.log("Action triggered:", data)
283
+ };
286
284
 
287
- 示例页面现在使用本地构建的 React 依赖,不再依赖外部 CDN 链接,确保在任何环境下都能正常运行。
285
+ <ReactAIRenderer scope={scope}>
286
+ {content}
287
+ </ReactAIRenderer>
288
+ ```
288
289
 
289
- #### Node.js 示例
290
+ ## 📊 架构设计
290
291
 
291
- 如果您想快速查看 API 使用方式,可以运行 Node.js 示例:
292
+ ### 系统架构图
292
293
 
293
- ```bash
294
- npm run example:node
294
+ ```mermaid
295
+ graph TD
296
+ A[AI Model] -->|Streaming Output| B[React AI Renderer]
297
+ B -->|Parse JSX/XML| C[MDX Streaming Parser]
298
+ C -->|Component Data| D[Component Renderer]
299
+ D -->|Render Components| E[Custom UI Components]
300
+ D -->|Lifecycle Hooks| F[Event Triggers]
301
+ E --> G[Interactive UI]
302
+ F --> H[Next Actions]
295
303
  ```
296
304
 
297
- 这将展示如何在 Node.js 环境中使用该库,并演示服务端渲染的基本用法。注意:由于 ReactAIRenderer 是一个 React 组件,它需要浏览器环境才能完全渲染,在 Node.js 环境中只能查看 API 使用方式和基本的服务端渲染输出。
305
+ ### 工作流程
298
306
 
299
- ## 📁 项目结构
307
+ 1. AI 模型流式输出 JSX/XML 格式的内容
308
+ 2. React AI Renderer 实时接收并解析内容
309
+ 3. MDX Streaming Parser 动态解析组件数据
310
+ 4. Component Renderer 渲染自定义 UI 组件
311
+ 5. 触发生命周期钩子,执行后续动作
300
312
 
301
- ```
302
- react-ai-renderer/
303
- ├── src/
304
- │ ├── index.ts # 入口文件
305
- │ ├── ReactAIRenderer.tsx # 主渲染组件
306
- │ ├── types.ts # 类型定义
307
- │ ├── components/ # 内置组件
308
- │ │ ├── BuiltInComponents.tsx # 内置组件集合
309
- │ │ ├── CodeHighlight.tsx # 代码高亮组件
310
- │ │ ├── ErrorBoundary.tsx # 错误边界组件
311
- │ │ ├── ErrorUI.tsx # 错误展示组件
312
- │ │ ├── MdxLayout.tsx # MDX 布局组件
313
- │ │ ├── MermaidRenderer.tsx # Mermaid 图表渲染组件
314
- │ │ ├── Think.tsx # AI 推理展示组件
315
- │ │ └── ToolCall.tsx # 工具调用展示组件
316
- │ └── utils/ # 工具函数
317
- │ ├── contentProcessor.ts # 内容处理工具
318
- │ ├── mdxRenderer.ts # MDX 渲染工具
319
- │ └── mdxStreamParse.ts # MDX 流式解析工具
320
- ├── dist/ # 构建输出目录
321
- ├── examples/ # 示例文件
322
- │ ├── components/ # UI 组件示例
323
- │ │ ├── FormComponents.tsx # 表单组件
324
- │ │ ├── TableComponents.tsx # 表格组件
325
- │ │ ├── CardComponents.tsx # 卡片组件
326
- │ │ └── index.ts # 组件导出
327
- │ ├── example.tsx # React 示例
328
- │ ├── example.html # HTML 示例
329
- │ ├── run-example.js # Node.js 示例
330
- │ ├── tsconfig.json # 示例编译配置
331
- │ └── dist/ # 编译后的示例文件
332
- ├── package.json # 包配置文件
333
- ├── tsconfig.json # TypeScript 配置
334
- ├── rollup.config.js # Rollup 构建配置
335
- └── README.md # 项目说明文档
336
- ```
313
+ ## ❓ 常见问题
337
314
 
338
- ## 🤝 贡献
315
+ ### Q: React AI Renderer 与 react-markdown 有什么区别?
316
+ A: React AI Renderer 完全兼容 react-markdown,但增加了对 JSX/XML 组件的支持、流式渲染和生命周期钩子等高级功能。
339
317
 
340
- 欢迎提交 Issue 和 Pull Request 来改进这个项目。
318
+ ### Q: 如何处理未注册的组件?
319
+ A: 未注册的组件会自动转换为对应的文本表示形式,不会导致页面崩溃。
341
320
 
342
- ## 📄 许可证
321
+ ### Q: 是否支持不同组件库如antd,shadcn等
322
+ A: 是的,React AI Renderer 无组件依赖,你可以使用任意组件库以及自定义组件
343
323
 
344
- MIT
324
+ ### Q: 性能如何优化?
325
+ A: 建议合理使用组件生命周期钩子,避免在渲染过程中执行耗时操作。
345
326
 
346
- ## 🙏 致谢
327
+ ## 📄 许可证
347
328
 
348
- - [MDX](https://mdxjs.com/) - Markdown for the component era
349
- - [Remark](https://remark.js.org/) - Markdown processor powered by plugins
350
- - [Rehype](https://github.com/rehypejs/rehype) - HTML processor powered by plugins
351
- - [Mermaid](https://mermaid-js.github.io/) - Generation of diagram and flowchart from text in a similar manner as markdown
329
+ MIT