@leeoohoo/aichat 1.0.0

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 ADDED
@@ -0,0 +1,274 @@
1
+ # @ai-chat/react-component
2
+
3
+ 一个功能完整的React AI聊天组件,支持会话管理、MCP集成和SQLite持久化存储。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 **开箱即用** - 一个标签即可使用完整的AI聊天功能
8
+ - 💬 **会话管理** - 支持多会话切换、创建、删除和重命名
9
+ - 🤖 **AI模型支持** - 支持OpenAI GPT系列模型
10
+ - 🔧 **MCP集成** - 支持Model Context Protocol工具调用
11
+ - 💾 **数据持久化** - 基于SQLite的本地数据存储
12
+ - 🎨 **主题切换** - 支持明暗主题切换
13
+ - 📱 **响应式设计** - 适配各种屏幕尺寸
14
+ - 🔒 **类型安全** - 完整的TypeScript类型定义
15
+
16
+ ## 🚀 快速开始
17
+
18
+ ### 安装
19
+
20
+ ```bash
21
+ npm install @ai-chat/react-component
22
+ # 或
23
+ yarn add @ai-chat/react-component
24
+ # 或
25
+ pnpm add @ai-chat/react-component
26
+ ```
27
+
28
+ ### 启动后端服务
29
+
30
+ 组件需要后端API服务支持,请先启动后端服务:
31
+
32
+ ```bash
33
+ # 方式一:使用npm脚本启动
34
+ npx @ai-chat/react-component start:server
35
+
36
+ # 方式二:直接运行服务器文件
37
+ node node_modules/@ai-chat/react-component/server/index.js
38
+ ```
39
+
40
+ 后端服务默认运行在 `http://localhost:3001`,提供以下API:
41
+ - 会话管理 (`/api/sessions`)
42
+ - 消息管理 (`/api/messages`)
43
+ - MCP配置 (`/api/mcp-configs`)
44
+ - AI模型配置 (`/api/ai-model-configs`)
45
+ - 系统上下文 (`/api/system-context`)
46
+
47
+ ### 基础使用(推荐)
48
+
49
+ 使用独立聊天组件,无需任何配置,开箱即用:
50
+
51
+ ```tsx
52
+ import React from 'react';
53
+ import StandaloneChatInterface from '@ai-chat/react-component';
54
+ import '@ai-chat/react-component/styles';
55
+
56
+ function App() {
57
+ return (
58
+ <div className="h-screen w-full">
59
+ <StandaloneChatInterface className="h-full" />
60
+ </div>
61
+ );
62
+ }
63
+
64
+ export default App;
65
+ ```
66
+
67
+ ## 高级配置
68
+
69
+ ### 自定义事件处理
70
+
71
+ ```tsx
72
+ import React from 'react';
73
+ import { ChatInterface, type Message, type Attachment } from '@ai-chat/react-component';
74
+
75
+ function App() {
76
+ const handleMessageSend = (content: string, attachments?: Attachment[]) => {
77
+ console.log('发送消息:', content, attachments);
78
+ };
79
+
80
+ const handleSessionChange = (sessionId: string) => {
81
+ console.log('切换会话:', sessionId);
82
+ };
83
+
84
+ return (
85
+ <ChatInterface
86
+ onMessageSend={handleMessageSend}
87
+ onSessionChange={handleSessionChange}
88
+ />
89
+ );
90
+ }
91
+ ```
92
+
93
+ ### 自定义渲染器
94
+
95
+ ```tsx
96
+ import React from 'react';
97
+ import { ChatInterface, type Message, type Attachment } from '@ai-chat/react-component';
98
+
99
+ function App() {
100
+ const customRenderer = {
101
+ renderMessage: (message: Message) => (
102
+ <div className="custom-message">
103
+ {message.content}
104
+ </div>
105
+ ),
106
+ renderAttachment: (attachment: Attachment) => (
107
+ <div className="custom-attachment">
108
+ {attachment.name}
109
+ </div>
110
+ ),
111
+ };
112
+
113
+ return (
114
+ <ChatInterface customRenderer={customRenderer} />
115
+ );
116
+ }
117
+ ```
118
+
119
+ ## 组件API
120
+
121
+ ### ChatInterface
122
+
123
+ 主要的聊天界面组件。
124
+
125
+ #### Props
126
+
127
+ | 属性 | 类型 | 默认值 | 描述 |
128
+ |------|------|--------|------|
129
+ | `className` | `string` | - | 自定义CSS类名 |
130
+ | `onMessageSend` | `(content: string, attachments?: Attachment[]) => void` | - | 消息发送回调 |
131
+ | `onSessionChange` | `(sessionId: string) => void` | - | 会话切换回调 |
132
+ | `customRenderer` | `CustomRenderer` | - | 自定义渲染器 |
133
+
134
+ ### 其他组件
135
+
136
+ - `MessageList` - 消息列表组件
137
+ - `InputArea` - 输入区域组件
138
+ - `SessionList` - 会话列表组件
139
+ - `ThemeToggle` - 主题切换组件
140
+ - `McpManager` - MCP管理组件
141
+ - `AiModelManager` - AI模型管理组件
142
+
143
+ ## Hooks
144
+
145
+ ### useChatStore
146
+
147
+ 聊天状态管理Hook。
148
+
149
+ ```tsx
150
+ import { useChatStore } from '@ai-chat/react-component';
151
+
152
+ function MyComponent() {
153
+ const {
154
+ currentSession,
155
+ messages,
156
+ isLoading,
157
+ sendMessage,
158
+ createSession,
159
+ switchSession,
160
+ } = useChatStore();
161
+
162
+ // 使用状态和方法
163
+ }
164
+ ```
165
+
166
+ ### useTheme
167
+
168
+ 主题管理Hook。
169
+
170
+ ```tsx
171
+ import { useTheme } from '@ai-chat/react-component';
172
+
173
+ function MyComponent() {
174
+ const { theme, setTheme, actualTheme } = useTheme();
175
+
176
+ return (
177
+ <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
178
+ 切换到 {theme === 'light' ? '暗色' : '亮色'} 主题
179
+ </button>
180
+ );
181
+ }
182
+ ```
183
+
184
+ ## 配置
185
+
186
+ ### 环境变量
187
+
188
+ 在使用前,请确保设置以下环境变量:
189
+
190
+ ```env
191
+ # OpenAI配置
192
+ VITE_OPENAI_API_KEY=your_openai_api_key
193
+ VITE_OPENAI_BASE_URL=https://api.openai.com/v1
194
+
195
+ # 服务器配置
196
+ VITE_API_BASE_URL=http://localhost:3001
197
+ ```
198
+
199
+ ### 数据库初始化
200
+
201
+ ```tsx
202
+ import { initDatabase } from '@ai-chat/react-component';
203
+
204
+ // 在应用启动时初始化数据库
205
+ initDatabase().then(() => {
206
+ console.log('数据库初始化完成');
207
+ });
208
+ ```
209
+
210
+ ## 样式定制
211
+
212
+ 组件使用Tailwind CSS构建,你可以通过以下方式定制样式:
213
+
214
+ 1. **覆盖CSS变量**
215
+
216
+ ```css
217
+ :root {
218
+ --chat-primary-color: #your-color;
219
+ --chat-background-color: #your-background;
220
+ }
221
+ ```
222
+
223
+ 2. **使用自定义CSS类**
224
+
225
+ ```tsx
226
+ <ChatInterface className="my-custom-chat" />
227
+ ```
228
+
229
+ 3. **主题定制**
230
+
231
+ ```css
232
+ .dark {
233
+ --chat-background: #1a1a1a;
234
+ --chat-text: #ffffff;
235
+ }
236
+
237
+ .light {
238
+ --chat-background: #ffffff;
239
+ --chat-text: #000000;
240
+ }
241
+ ```
242
+
243
+ ## 类型定义
244
+
245
+ 包含完整的TypeScript类型定义:
246
+
247
+ ```tsx
248
+ import type {
249
+ Message,
250
+ Session,
251
+ Attachment,
252
+ ToolCall,
253
+ ChatConfig,
254
+ AiModelConfig,
255
+ McpConfig,
256
+ Theme,
257
+ ChatInterfaceProps,
258
+ MessageListProps,
259
+ InputAreaProps,
260
+ SessionListProps,
261
+ } from '@ai-chat/react-component';
262
+ ```
263
+
264
+ ## 许可证
265
+
266
+ MIT
267
+
268
+ ## 贡献
269
+
270
+ 欢迎提交Issue和Pull Request!
271
+
272
+ ## 支持
273
+
274
+ 如果你觉得这个项目有用,请给它一个⭐️!