@mxmweb/aichat 1.9.7 → 1.9.8
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 +200 -199
- package/assets/style.css +1 -1
- package/index.js +3100 -3093
- package/package.json +1 -1
- package/stats.html +1 -1
package/README.md
CHANGED
|
@@ -1,35 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mxmweb/aichat
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> 基于 React 的轻量级 AI 聊天对话组件库,提供核心聊天功能和灵活的扩展能力
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/@mxmweb/aichat)
|
|
6
|
+
[](https://www.npmjs.com/package/@mxmweb/aichat)
|
|
7
|
+
[](https://github.com/your-org/aichat/blob/main/LICENSE)
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
## ✨ 核心特性
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
- ✅ **轻量级设计**:Gzip 压缩后仅 ~78KB,专注于核心聊天功能
|
|
12
|
+
- ✅ **灵活的组件系统**:支持自定义消息卡片、欢迎页、加载状态等
|
|
13
|
+
- ✅ **完整的文件上传**:支持拖拽上传、多文件管理、上传状态跟踪
|
|
14
|
+
- ✅ **动态主题系统**:支持运行时主题切换,所有样式可配置
|
|
15
|
+
- ✅ **事件驱动架构**:通过 eventsEmit 统一处理所有交互事件
|
|
16
|
+
- ✅ **TypeScript 支持**:完整的类型定义,提供良好的开发体验
|
|
17
|
+
- ✅ **侧边栏扩展**:支持自定义侧边栏组件和动态表单
|
|
10
18
|
|
|
11
|
-
|
|
12
|
-
```bash
|
|
13
|
-
pnpm add @mxmweb/aichat \
|
|
14
|
-
react react-dom @mxmweb/zui
|
|
15
|
-
```
|
|
19
|
+
## 📦 安装
|
|
16
20
|
|
|
17
|
-
#### 使用 yarn
|
|
18
21
|
```bash
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
# 使用 pnpm(推荐)
|
|
23
|
+
pnpm add @mxmweb/aichat react react-dom @mxmweb/zui
|
|
24
|
+
|
|
25
|
+
# 使用 npm
|
|
26
|
+
npm install @mxmweb/aichat react react-dom @mxmweb/zui
|
|
27
|
+
|
|
28
|
+
# 使用 yarn
|
|
29
|
+
yarn add @mxmweb/aichat react react-dom @mxmweb/zui
|
|
21
30
|
```
|
|
22
31
|
|
|
23
|
-
|
|
32
|
+
### 必需依赖
|
|
33
|
+
|
|
34
|
+
本库需要以下 peerDependencies:
|
|
35
|
+
|
|
24
36
|
```bash
|
|
25
|
-
|
|
26
|
-
react react-dom @mxmweb/zui
|
|
37
|
+
pnpm add @mxmweb/zui@^1.* react@>=18 react-dom@>=18
|
|
27
38
|
```
|
|
28
39
|
|
|
29
40
|
### 样式引入
|
|
30
41
|
|
|
31
42
|
在项目入口文件中引入样式:
|
|
32
|
-
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
33
45
|
import '@mxmweb/aichat/style.css';
|
|
34
46
|
```
|
|
35
47
|
|
|
@@ -41,215 +53,204 @@ import '@mxmweb/aichat/style.css';
|
|
|
41
53
|
|
|
42
54
|
> 💡 **优化说明**:AIChat 已移除不必要的依赖,专注于核心聊天功能,确保快速加载和简洁的安装体验。
|
|
43
55
|
|
|
44
|
-
|
|
45
|
-
1. **业务层集成方案(推荐,开箱即用)**
|
|
46
|
-
2. **核心组件层用法(自定义交互/高级用法)**
|
|
47
|
-
|
|
48
|
-
---
|
|
49
|
-
|
|
50
|
-
## 1. 业务层集成方案(推荐)
|
|
56
|
+
## 🚀 快速开始
|
|
51
57
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
### 快速开始
|
|
58
|
+
### 基础用法
|
|
55
59
|
|
|
56
60
|
```tsx
|
|
57
|
-
import {
|
|
61
|
+
import React, { useState } from 'react';
|
|
62
|
+
import { AiChat } from '@mxmweb/aichat';
|
|
63
|
+
import '@mxmweb/aichat/style.css';
|
|
58
64
|
|
|
59
|
-
|
|
65
|
+
function App() {
|
|
66
|
+
const [chatData, setChatData] = useState({
|
|
67
|
+
id: 'session-1',
|
|
68
|
+
messages: [],
|
|
69
|
+
isNew: true,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const [status, setStatus] = useState({
|
|
73
|
+
display: 'ready',
|
|
74
|
+
sender: 'ready',
|
|
75
|
+
app: 'ready',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const handleEventsEmit = (eventName: string, data?: any) => {
|
|
79
|
+
console.log('Event:', eventName, data);
|
|
80
|
+
|
|
81
|
+
if (eventName === 'sender:send') {
|
|
82
|
+
// 处理消息发送
|
|
83
|
+
const newMessage = {
|
|
84
|
+
id: `msg-${Date.now()}`,
|
|
85
|
+
type: 'user',
|
|
86
|
+
time: new Date().toISOString(),
|
|
87
|
+
content: data.content,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
setChatData(prev => ({
|
|
91
|
+
...prev,
|
|
92
|
+
messages: [...prev.messages, newMessage],
|
|
93
|
+
}));
|
|
94
|
+
|
|
95
|
+
// 模拟 AI 回复
|
|
96
|
+
setTimeout(() => {
|
|
97
|
+
const aiMessage = {
|
|
98
|
+
id: `msg-${Date.now()}`,
|
|
99
|
+
type: 'ai',
|
|
100
|
+
time: new Date().toISOString(),
|
|
101
|
+
content: '这是一条 AI 回复',
|
|
102
|
+
};
|
|
103
|
+
setChatData(prev => ({
|
|
104
|
+
...prev,
|
|
105
|
+
messages: [...prev.messages, aiMessage],
|
|
106
|
+
}));
|
|
107
|
+
}, 1000);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
60
110
|
|
|
61
|
-
const ChatBox =GientechChat()
|
|
62
111
|
return (
|
|
63
|
-
<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
error: '#F53F3F', // 错误色
|
|
77
|
-
shadow: '#000', // 阴影色
|
|
78
|
-
appBackground: '#F7F8FA', // 整体背景色
|
|
79
|
-
// ...可扩展更多
|
|
112
|
+
<div style={{ width: '100%', height: '100vh' }}>
|
|
113
|
+
<AiChat
|
|
114
|
+
activeSessionId="session-1"
|
|
115
|
+
chatData={chatData}
|
|
116
|
+
status={status}
|
|
117
|
+
eventsEmit={handleEventsEmit}
|
|
118
|
+
styles={{
|
|
119
|
+
theme: {
|
|
120
|
+
colors: {
|
|
121
|
+
primary: '#409EFF',
|
|
122
|
+
background: '#F7F8FA',
|
|
123
|
+
text: '#222',
|
|
124
|
+
},
|
|
80
125
|
},
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
},
|
|
86
|
-
space: {
|
|
87
|
-
radius: 'md', // 圆角,可用 'sm' | 'md' | 'lg' 或具体像素
|
|
88
|
-
size: 'md', // 字体大小
|
|
89
|
-
shadow: 'md', // 阴影
|
|
90
|
-
padding: 'md', // 内边距
|
|
91
|
-
margin: 'md', // 外边距
|
|
92
|
-
sidebar: '260px', // 侧边栏宽度
|
|
93
|
-
lineHeight: 'normal', // 行高
|
|
94
|
-
// ...
|
|
95
|
-
},
|
|
96
|
-
others: {
|
|
97
|
-
baseFontWeight: 400, // 字重
|
|
98
|
-
// ...
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
mode: 'light', // 或 'dark'
|
|
102
|
-
}}
|
|
103
|
-
eventsEmit={(event, data) => {
|
|
104
|
-
// 监听所有交互事件
|
|
105
|
-
}}
|
|
106
|
-
/>
|
|
126
|
+
mode: 'light',
|
|
127
|
+
}}
|
|
128
|
+
/>
|
|
129
|
+
</div>
|
|
107
130
|
);
|
|
108
131
|
}
|
|
132
|
+
|
|
133
|
+
export default App;
|
|
109
134
|
```
|
|
110
135
|
|
|
111
|
-
|
|
112
|
-
- `theme.colors`:所有颜色相关配置,**建议全部覆盖**,否则用默认色。
|
|
113
|
-
- `theme.icons`:AI、用户等头像图标,支持自定义。
|
|
114
|
-
- `theme.space`:圆角、阴影、字体、边距、侧边栏宽度等。
|
|
115
|
-
- `theme.others`:其他扩展项,如字体粗细。
|
|
116
|
-
- `mode`:'light' 或 'dark',影响部分默认色。
|
|
117
|
-
|
|
118
|
-
#### eventsEmit 可监听事件及参数
|
|
119
|
-
业务层已封装常用事件,所有交互事件均通过 `eventsEmit(eventName, data)` 回调分发,常见事件如下:
|
|
120
|
-
|
|
121
|
-
| 事件名 | 说明 | data 参数结构示例 |
|
|
122
|
-
|------------------------------|----------------------|------------------------------------|
|
|
123
|
-
| sender:send | 发送消息 | { content, files, status, ... } |
|
|
124
|
-
| sender:send_recommandQuestion| 发送推荐问题 | { content, ... } |
|
|
125
|
-
| sender:clear | 清空输入框 | { clearFn } |
|
|
126
|
-
| sender:action_history | 打开文件管理器 | - |
|
|
127
|
-
| action_copy:click | 复制消息内容 | { content, sessionContext } |
|
|
128
|
-
| action_thumbsup | 点赞 | { restult, queryId } |
|
|
129
|
-
| action_thumbsdown | 点踩 | { restult, queryId } |
|
|
130
|
-
| action_retry | 重新发送 | { idx, content, ... } |
|
|
131
|
-
| fileManager:change | 文件管理器文件变化 | { uploadedFiles, setUploadedFiles, removeFile } |
|
|
132
|
-
| conversations:delete_icon_clicked | 删除会话 | { data, async_deleteConversation, ... } |
|
|
133
|
-
| conversations:rename_icon_clicked | 重命名会话 | { data, async_renameConversation, ... } |
|
|
134
|
-
| conversation:item_click | 切换会话 | { sessionId, ... } |
|
|
135
|
-
| ... | 其他事件 | - |
|
|
136
|
-
|
|
137
|
-
- **所有事件参数均为对象,具体结构可参考源码或控制台输出。**
|
|
138
|
-
- 你可以在 `eventsEmit` 里拦截、扩展、上报等。
|
|
136
|
+
## 🔗 链接
|
|
139
137
|
|
|
140
|
-
|
|
138
|
+
- **NPM**: <https://www.npmjs.com/package/@mxmweb/aichat>
|
|
139
|
+
- **GitHub**: <https://github.com/your-org/aichat>
|
|
141
140
|
|
|
142
|
-
##
|
|
141
|
+
## 📚 API 文档
|
|
143
142
|
|
|
144
|
-
|
|
143
|
+
> 📖 完整 API 文档请查看:[doc_assets/接口/](./doc_assets/接口/)
|
|
145
144
|
|
|
146
|
-
|
|
145
|
+
- **接口文档**: [AiChat API](./doc_assets/接口/AiChat.md)
|
|
146
|
+
- **更新说明**: [CHANGELOG](./doc_assets/更新说明/CHANGELOG.md)
|
|
147
|
+
- **演示说明**: [演示文档](./doc_assets/演示/index.md)
|
|
147
148
|
|
|
148
|
-
|
|
149
|
-
import { AiChat } from '@mxmweb/aichat';
|
|
149
|
+
### 核心组件(Core Components)
|
|
150
150
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
AiChatBox: (msg, idx) => <MyAiChatBox {...msg} />, // 自定义AI消息卡片
|
|
173
|
-
UserChatBox: (msg, idx) => <MyUserChatBox {...msg} />, // 自定义用户消息卡片
|
|
174
|
-
WelcomeComponent: () => <MyWelcome />,
|
|
175
|
-
LogoBox: () => <MyLogo />,
|
|
176
|
-
// ...
|
|
177
|
-
}}
|
|
178
|
-
onSenderConfigChange={fn} // 发送器配置变化回调(可选)
|
|
179
|
-
/>
|
|
180
|
-
);
|
|
151
|
+
#### AiChat
|
|
152
|
+
|
|
153
|
+
核心聊天组件,提供基础的聊天界面和交互能力。
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
interface AiChatProps {
|
|
157
|
+
activeSessionId: string | undefined;
|
|
158
|
+
chatData: ConversationData;
|
|
159
|
+
sidebar?: SidebarTabConfig[];
|
|
160
|
+
status: AppStatusManager;
|
|
161
|
+
recommandQuestions?: string[];
|
|
162
|
+
eventsEmit?: (eventName: string, data?: any, callback?: () => void) => void;
|
|
163
|
+
CustomComponents?: any;
|
|
164
|
+
fileUploadStatus?: any;
|
|
165
|
+
styles?: Styles;
|
|
166
|
+
senderConfig?: SenderConfig;
|
|
167
|
+
onSenderConfigChange?: (data: {
|
|
168
|
+
name: string;
|
|
169
|
+
checked: boolean;
|
|
170
|
+
all: Record<string, boolean>;
|
|
171
|
+
}) => void;
|
|
181
172
|
}
|
|
182
173
|
```
|
|
183
174
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
|
189
|
-
|
|
|
190
|
-
|
|
|
191
|
-
|
|
|
192
|
-
| recommandQuestions
|
|
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
|
-
|
|
175
|
+
**Props 说明**:
|
|
176
|
+
|
|
177
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
178
|
+
|------|------|--------|------|
|
|
179
|
+
| activeSessionId | string \| undefined | - | 当前激活的会话ID |
|
|
180
|
+
| chatData | ConversationData | 必填 | 聊天数据,包含消息列表 |
|
|
181
|
+
| sidebar | SidebarTabConfig[] | [] | 侧边栏配置数组 |
|
|
182
|
+
| status | AppStatusManager | 必填 | 应用状态管理对象 |
|
|
183
|
+
| recommandQuestions | string[] | [] | 推荐问题列表 |
|
|
184
|
+
| eventsEmit | (eventName, data, callback) => void | - | 事件回调函数 |
|
|
185
|
+
| CustomComponents | object | - | 自定义组件配置 |
|
|
186
|
+
| fileUploadStatus | any[] | [] | 文件上传状态数组 |
|
|
187
|
+
| styles | Styles | - | 主题样式配置 |
|
|
188
|
+
| senderConfig | SenderConfig | - | 发送器配置 |
|
|
189
|
+
| onSenderConfigChange | (data) => void | - | 发送器配置变化回调 |
|
|
190
|
+
|
|
191
|
+
## 🛠️ 技术栈
|
|
192
|
+
|
|
193
|
+
- **React** `>=18 <20` - 前端框架
|
|
194
|
+
- **@mxmweb/zui** `^1.*` - UI 组件库
|
|
195
|
+
- **TypeScript** - 类型支持
|
|
196
|
+
- **anime.js** - 动画库
|
|
197
|
+
- **lucide-react** - 图标库
|
|
198
|
+
|
|
199
|
+
## 📁 项目结构
|
|
200
|
+
|
|
201
|
+
```text
|
|
202
|
+
src/
|
|
203
|
+
├── lib_enter.ts # 库入口,导出所有公共 API
|
|
204
|
+
├── style.css # 样式文件
|
|
205
|
+
├── core/ # 核心组件
|
|
206
|
+
│ ├── AiChat.tsx # AiChat 核心组件
|
|
207
|
+
│ ├── AiChat.types.tsx # 类型定义
|
|
208
|
+
│ ├── Sender/ # 发送器组件
|
|
209
|
+
│ │ ├── index.tsx
|
|
210
|
+
│ │ ├── FilesDisplay.tsx
|
|
211
|
+
│ │ └── FileUpload.tsx
|
|
212
|
+
│ ├── Displayer/ # 消息展示组件
|
|
213
|
+
│ │ ├── index.tsx
|
|
214
|
+
│ │ ├── DefaultAiChatBox.tsx
|
|
215
|
+
│ │ └── DefaultUserChatBox.tsx
|
|
216
|
+
│ ├── DynamicForm.tsx # 动态表单组件
|
|
217
|
+
│ ├── defaultStyleSet.tsx # 默认主题配置
|
|
218
|
+
│ └── ...
|
|
219
|
+
├── adopters/ # 业务扩展组件(不导出)
|
|
220
|
+
│ ├── GientechChat/
|
|
221
|
+
│ └── HeadLessChat/
|
|
222
|
+
└── examples/ # 演示实例
|
|
223
|
+
├── BaseChat/
|
|
224
|
+
└── ProChat/
|
|
232
225
|
```
|
|
233
226
|
|
|
234
|
-
|
|
227
|
+
## 🎯 使用场景
|
|
235
228
|
|
|
236
|
-
|
|
237
|
-
-
|
|
238
|
-
-
|
|
239
|
-
-
|
|
229
|
+
- AI 对话聊天界面
|
|
230
|
+
- 智能客服系统
|
|
231
|
+
- 知识库问答系统
|
|
232
|
+
- 文档对话系统
|
|
233
|
+
- 自定义聊天应用
|
|
240
234
|
|
|
241
|
-
|
|
235
|
+
## 📝 示例
|
|
242
236
|
|
|
243
|
-
|
|
244
|
-
- 所有交互事件(如发送、复制、点赞、文件上传等)均通过 `eventsEmit` 回调分发
|
|
245
|
-
- 业务层已封装常用事件,核心层需自行处理
|
|
237
|
+
查看 `src/examples/` 目录获取更多完整示例:
|
|
246
238
|
|
|
247
|
-
|
|
239
|
+
- **BaseChat** - 核心组件基础用法
|
|
240
|
+
- **ProChat** - 高级配置完整示例
|
|
241
|
+
|
|
242
|
+
## 🤝 贡献
|
|
243
|
+
|
|
244
|
+
欢迎提交 Issue 和 Pull Request。
|
|
245
|
+
|
|
246
|
+
## 📄 许可证
|
|
247
|
+
|
|
248
|
+
MIT License
|
|
249
|
+
|
|
250
|
+
## 👥 作者
|
|
248
251
|
|
|
249
|
-
|
|
250
|
-
- 支持多文件上传、历史文件、会话文件、上传进度、去重
|
|
251
|
-
- 业务层已自动处理,核心层需自行实现
|
|
252
|
+
- **hanfeng_Zhang**
|
|
252
253
|
|
|
253
254
|
---
|
|
254
255
|
|
|
255
|
-
|
|
256
|
+
⭐ 如果这个项目对你有帮助,请给它一个 Star!
|