@leeoohoo/aichat 1.0.10 → 1.0.11
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 +157 -122
- package/dist/index.cjs.js +42 -62
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.css +2 -2
- package/dist/index.d.ts +37 -15
- package/dist/index.esm.js +5179 -5988
- package/dist/index.esm.js.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,179 +1,214 @@
|
|
|
1
1
|
# @leeoohoo/aichat
|
|
2
2
|
|
|
3
|
-
React AI
|
|
3
|
+
一个功能完整的React AI聊天组件,支持会话管理、MCP集成和SQLite持久化存储。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## 特性
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- 🚀 **开箱即用** - 一个标签即可使用完整的AI聊天功能
|
|
8
|
+
- 💬 **会话管理** - 支持多会话切换、创建、删除和重命名
|
|
9
|
+
- 🤖 **AI模型支持** - 支持OpenAI GPT系列模型
|
|
10
|
+
- 🔧 **MCP集成** - 支持Model Context Protocol工具调用
|
|
11
|
+
- 💾 **数据持久化** - 基于SQLite的本地数据存储
|
|
12
|
+
- 🎨 **主题切换** - 支持明暗主题切换
|
|
13
|
+
- 📱 **响应式设计** - 适配各种屏幕尺寸
|
|
14
|
+
- 🔒 **类型安全** - 完整的TypeScript类型定义
|
|
8
15
|
|
|
9
|
-
##
|
|
16
|
+
## 🚀 快速开始
|
|
10
17
|
|
|
11
|
-
|
|
12
|
-
- 会话管理 Sessions CRUD, multi-session switching
|
|
13
|
-
- AI 模型配置 AI model configs (OpenAI-compatible)
|
|
14
|
-
- MCP 集成 Model Context Protocol tool calls (HTTP/STDIO)
|
|
15
|
-
- 数据持久化 SQLite by default (MongoDB optional in server)
|
|
16
|
-
- 主题切换 Light/Dark/Auto, responsive UI
|
|
17
|
-
- 类型安全 Full TypeScript types
|
|
18
|
-
|
|
19
|
-
## 代码结构 Repository Structure
|
|
20
|
-
|
|
21
|
-
- 根目录 Root: React 组件库 React component library (Vite + TS)
|
|
22
|
-
- server/chat_app_node_server: Node API 服务 Node API server (Express, SSE, MCP, SQLite/MongoDB)
|
|
23
|
-
- examples/complete-example: 集成示例 Example app (web/Electron)
|
|
24
|
-
|
|
25
|
-
## 快速开始 Quick Start
|
|
26
|
-
|
|
27
|
-
1) 作为依赖使用 Use as a dependency
|
|
18
|
+
### 安装
|
|
28
19
|
|
|
29
20
|
```bash
|
|
21
|
+
# 使用 npm
|
|
30
22
|
npm install @leeoohoo/aichat
|
|
31
|
-
# or
|
|
32
|
-
yarn add @leeoohoo/aichat
|
|
33
|
-
# or
|
|
34
|
-
pnpm add @leeoohoo/aichat
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
最简用法 Minimal usage
|
|
38
23
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
import '@leeoohoo/aichat/styles';
|
|
24
|
+
# 或使用 yarn
|
|
25
|
+
yarn add @leeoohoo/aichat
|
|
42
26
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
27
|
+
# 或使用 pnpm
|
|
28
|
+
pnpm add @leeoohoo/aichat
|
|
46
29
|
```
|
|
47
30
|
|
|
48
|
-
|
|
31
|
+
### 启动后端服务
|
|
49
32
|
|
|
50
|
-
|
|
33
|
+
组件需要后端API服务支持,请先启动后端服务:
|
|
51
34
|
|
|
52
35
|
```bash
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
36
|
+
# 方式一:使用npm脚本启动
|
|
37
|
+
npx @leeoohoo/aichat start:server
|
|
38
|
+
|
|
39
|
+
# 方式二:直接运行服务器文件
|
|
40
|
+
node node_modules/@leeoohoo/aichat/server/index.js
|
|
57
41
|
```
|
|
58
42
|
|
|
59
|
-
|
|
43
|
+
后端服务默认运行在 `http://localhost:3001`,提供以下API:
|
|
44
|
+
- 会话管理 (`/api/sessions`)
|
|
45
|
+
- 消息管理 (`/api/messages`)
|
|
46
|
+
- MCP配置 (`/api/mcp-configs`)
|
|
47
|
+
- AI模型配置 (`/api/ai-model-configs`)
|
|
48
|
+
- 系统上下文 (`/api/system-context`)
|
|
60
49
|
|
|
61
|
-
|
|
62
|
-
cd /path/to/chat_app
|
|
63
|
-
npm install
|
|
64
|
-
npm run dev # http://localhost:5173 (proxy /api → 3001)
|
|
65
|
-
```
|
|
50
|
+
### 基础使用(推荐)
|
|
66
51
|
|
|
67
|
-
|
|
68
|
-
In production, pass `apiBaseUrl` or `port` to the component to point at your server.
|
|
52
|
+
使用独立聊天组件,无需任何配置,开箱即用:
|
|
69
53
|
|
|
70
54
|
```tsx
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
55
|
+
import React from 'react';
|
|
56
|
+
import StandaloneChatInterface from '@leeoohoo/aichat';
|
|
57
|
+
import '@leeoohoo/aichat/styles';
|
|
58
|
+
|
|
59
|
+
function App() {
|
|
60
|
+
return (
|
|
61
|
+
<div className="h-screen w-full">
|
|
62
|
+
<StandaloneChatInterface className="h-full" />
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export default App;
|
|
74
68
|
```
|
|
75
69
|
|
|
76
|
-
##
|
|
70
|
+
## 高级配置
|
|
77
71
|
|
|
78
|
-
|
|
72
|
+
### 自定义事件处理
|
|
79
73
|
|
|
80
74
|
```tsx
|
|
81
|
-
import
|
|
82
|
-
import '@leeoohoo/aichat
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
75
|
+
import React from 'react';
|
|
76
|
+
import { ChatInterface, type Message, type Attachment } from '@leeoohoo/aichat';
|
|
77
|
+
|
|
78
|
+
function App() {
|
|
79
|
+
const handleMessageSend = (content: string, attachments?: Attachment[]) => {
|
|
80
|
+
console.log('发送消息:', content, attachments);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const handleSessionChange = (sessionId: string) => {
|
|
84
|
+
console.log('切换会话:', sessionId);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<ChatInterface
|
|
89
|
+
onMessageSend={handleMessageSend}
|
|
90
|
+
onSessionChange={handleSessionChange}
|
|
91
|
+
/>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
93
94
|
```
|
|
94
95
|
|
|
95
|
-
|
|
96
|
+
### 自定义渲染器
|
|
96
97
|
|
|
97
98
|
```tsx
|
|
98
|
-
import
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
99
|
+
import React from 'react';
|
|
100
|
+
import { ChatInterface, type Message, type Attachment } from '@leeoohoo/aichat';
|
|
101
|
+
|
|
102
|
+
function App() {
|
|
103
|
+
const customRenderer = {
|
|
104
|
+
renderMessage: (message: Message) => (
|
|
105
|
+
<div className="custom-message">
|
|
106
|
+
{message.content}
|
|
107
|
+
</div>
|
|
108
|
+
),
|
|
109
|
+
renderAttachment: (attachment: Attachment) => (
|
|
110
|
+
<div className="custom-attachment">
|
|
111
|
+
{attachment.name}
|
|
112
|
+
</div>
|
|
113
|
+
),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<ChatInterface customRenderer={customRenderer} />
|
|
118
|
+
);
|
|
111
119
|
}
|
|
112
120
|
```
|
|
113
121
|
|
|
114
|
-
|
|
122
|
+
## 组件API
|
|
115
123
|
|
|
116
|
-
|
|
117
|
-
- Hooks: `useTheme`, `useChatStore`
|
|
118
|
-
- 工具 Utils/API: `lib/api`, `lib/services`, `lib/utils`
|
|
119
|
-
- 类型 Types: 从 `@leeoohoo/aichat` 导入类型 Import types from the package
|
|
124
|
+
### ChatInterface
|
|
120
125
|
|
|
121
|
-
|
|
126
|
+
主要的聊天界面组件。
|
|
122
127
|
|
|
123
|
-
|
|
128
|
+
#### Props
|
|
124
129
|
|
|
125
|
-
|
|
126
|
-
|
|
130
|
+
| 属性 | 类型 | 默认值 | 描述 |
|
|
131
|
+
|------|------|--------|------|
|
|
132
|
+
| `className` | `string` | - | 自定义CSS类名 |
|
|
133
|
+
| `onMessageSend` | `(content: string, attachments?: Attachment[]) => void` | - | 消息发送回调 |
|
|
134
|
+
| `onSessionChange` | `(sessionId: string) => void` | - | 会话切换回调 |
|
|
135
|
+
| `customRenderer` | `CustomRenderer` | - | 自定义渲染器 |
|
|
127
136
|
|
|
128
|
-
|
|
137
|
+
### 其他组件
|
|
129
138
|
|
|
130
|
-
-
|
|
139
|
+
- `MessageList` - 消息列表组件
|
|
140
|
+
- `InputArea` - 输入区域组件
|
|
141
|
+
- `SessionList` - 会话列表组件
|
|
142
|
+
- `ThemeToggle` - 主题切换组件
|
|
143
|
+
- `McpManager` - MCP管理组件
|
|
144
|
+
- `AiModelManager` - AI模型管理组件
|
|
131
145
|
|
|
132
|
-
|
|
133
|
-
OPENAI_API_KEY=your_api_key
|
|
134
|
-
OPENAI_BASE_URL=https://api.openai.com/v1
|
|
135
|
-
PORT=3001
|
|
136
|
-
NODE_ENV=development
|
|
137
|
-
```
|
|
146
|
+
## Hooks
|
|
138
147
|
|
|
139
|
-
|
|
148
|
+
### useChatStore
|
|
140
149
|
|
|
141
|
-
|
|
150
|
+
聊天状态管理Hook。
|
|
142
151
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
152
|
+
```tsx
|
|
153
|
+
import { useChatStore } from '@leeoohoo/aichat';
|
|
154
|
+
|
|
155
|
+
function MyComponent() {
|
|
156
|
+
const {
|
|
157
|
+
currentSession,
|
|
158
|
+
messages,
|
|
159
|
+
isLoading,
|
|
160
|
+
sendMessage,
|
|
161
|
+
createSession,
|
|
162
|
+
switchSession,
|
|
163
|
+
} = useChatStore();
|
|
164
|
+
|
|
165
|
+
// 使用状态和方法
|
|
166
|
+
}
|
|
167
|
+
```
|
|
149
168
|
|
|
150
|
-
|
|
169
|
+
### useTheme
|
|
151
170
|
|
|
152
|
-
|
|
171
|
+
主题管理Hook。
|
|
153
172
|
|
|
154
|
-
|
|
173
|
+
```tsx
|
|
174
|
+
import { useTheme } from '@leeoohoo/aichat';
|
|
155
175
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
176
|
+
function MyComponent() {
|
|
177
|
+
const { theme, setTheme, actualTheme } = useTheme();
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
|
|
181
|
+
切换到 {theme === 'light' ? '暗色' : '亮色'} 主题
|
|
182
|
+
</button>
|
|
183
|
+
);
|
|
184
|
+
}
|
|
160
185
|
```
|
|
161
186
|
|
|
162
|
-
##
|
|
187
|
+
## 配置
|
|
163
188
|
|
|
164
|
-
|
|
165
|
-
- `npm run build:lib` 构建库 Build library bundle
|
|
166
|
-
- `npm run test`/`test:ui` 单测 Vitest
|
|
167
|
-
- `npm run lint`/`lint:fix` ESLint
|
|
168
|
-
- `npm run storybook` 组件调试 Storybook
|
|
189
|
+
### 环境变量
|
|
169
190
|
|
|
170
|
-
|
|
191
|
+
在使用前,请确保设置以下环境变量:
|
|
171
192
|
|
|
172
|
-
|
|
193
|
+
```env
|
|
194
|
+
# OpenAI配置
|
|
195
|
+
VITE_OPENAI_API_KEY=your_openai_api_key
|
|
196
|
+
VITE_OPENAI_BASE_URL=https://api.openai.com/v1
|
|
173
197
|
|
|
174
|
-
|
|
198
|
+
# 服务器配置
|
|
199
|
+
VITE_API_BASE_URL=http://localhost:3001
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### 数据库初始化
|
|
175
203
|
|
|
176
|
-
|
|
204
|
+
```tsx
|
|
205
|
+
import { initDatabase } from '@leeoohoo/aichat';
|
|
206
|
+
|
|
207
|
+
// 在应用启动时初始化数据库
|
|
208
|
+
initDatabase().then(() => {
|
|
209
|
+
console.log('数据库初始化完成');
|
|
210
|
+
});
|
|
211
|
+
```
|
|
177
212
|
|
|
178
213
|
## 样式定制
|
|
179
214
|
|
|
@@ -239,4 +274,4 @@ MIT
|
|
|
239
274
|
|
|
240
275
|
## 支持
|
|
241
276
|
|
|
242
|
-
如果你觉得这个项目有用,请给它一个⭐️!
|
|
277
|
+
如果你觉得这个项目有用,请给它一个⭐️!
|