@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 CHANGED
@@ -1,179 +1,214 @@
1
1
  # @leeoohoo/aichat
2
2
 
3
- React AI Chat Component and Node server. Bilingual README (中文/English).
3
+ 一个功能完整的React AI聊天组件,支持会话管理、MCP集成和SQLite持久化存储。
4
4
 
5
-
5
+ ## 特性
6
6
 
7
- 一个功能完整的 React AI 聊天组件库,配套 Node 后端服务,支持会话管理、MCP 工具集成与持久化存储。
7
+ - 🚀 **开箱即用** - 一个标签即可使用完整的AI聊天功能
8
+ - 💬 **会话管理** - 支持多会话切换、创建、删除和重命名
9
+ - 🤖 **AI模型支持** - 支持OpenAI GPT系列模型
10
+ - 🔧 **MCP集成** - 支持Model Context Protocol工具调用
11
+ - 💾 **数据持久化** - 基于SQLite的本地数据存储
12
+ - 🎨 **主题切换** - 支持明暗主题切换
13
+ - 📱 **响应式设计** - 适配各种屏幕尺寸
14
+ - 🔒 **类型安全** - 完整的TypeScript类型定义
8
15
 
9
- ## 功能 Features
16
+ ## 🚀 快速开始
10
17
 
11
- - 开箱即用 Standalone chat UI, drop-in usage
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
- ```tsx
40
- import StandaloneChatInterface from '@leeoohoo/aichat';
41
- import '@leeoohoo/aichat/styles';
24
+ # 或使用 yarn
25
+ yarn add @leeoohoo/aichat
42
26
 
43
- export default function App() {
44
- return <StandaloneChatInterface className="h-screen" />;
45
- }
27
+ # 或使用 pnpm
28
+ pnpm add @leeoohoo/aichat
46
29
  ```
47
30
 
48
- 2) 本仓库本地开发 Run locally (this repo)
31
+ ### 启动后端服务
49
32
 
50
- - 启动后端 Start the API server
33
+ 组件需要后端API服务支持,请先启动后端服务:
51
34
 
52
35
  ```bash
53
- cd server/chat_app_node_server
54
- npm install
55
- cp .env.example .env # 设置 OPENAI_API_KEY 等 set your OPENAI_API_KEY
56
- npm start # http://localhost:3001
36
+ # 方式一:使用npm脚本启动
37
+ npx @leeoohoo/aichat start:server
38
+
39
+ # 方式二:直接运行服务器文件
40
+ node node_modules/@leeoohoo/aichat/server/index.js
57
41
  ```
58
42
 
59
- - 启动前端 Start the frontend demo
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
- ```bash
62
- cd /path/to/chat_app
63
- npm install
64
- npm run dev # http://localhost:5173 (proxy /api → 3001)
65
- ```
50
+ ### 基础使用(推荐)
66
51
 
67
- 生产环境可通过组件传入 `apiBaseUrl` 或 `port` 覆盖后端地址。
68
- In production, pass `apiBaseUrl` or `port` to the component to point at your server.
52
+ 使用独立聊天组件,无需任何配置,开箱即用:
69
53
 
70
54
  ```tsx
71
- <StandaloneChatInterface apiBaseUrl="https://your.domain/api" />
72
- // or
73
- <StandaloneChatInterface port={3001} />
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
- ## 使用 Usage
70
+ ## 高级配置
77
71
 
78
- 推荐 Recommended: 独立组件 Standalone component
72
+ ### 自定义事件处理
79
73
 
80
74
  ```tsx
81
- import StandaloneChatInterface from '@leeoohoo/aichat';
82
- import '@leeoohoo/aichat/styles';
83
-
84
- <StandaloneChatInterface
85
- className="h-full"
86
- userId="user_123"
87
- projectId="proj_456"
88
- showMcpManager
89
- showAiModelManager
90
- showSystemContextEditor
91
- showAgentManager
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
- 可选:类方式 API Optional: class-style API
96
+ ### 自定义渲染器
96
97
 
97
98
  ```tsx
98
- import { AiChat } from '@leeoohoo/aichat';
99
-
100
- const aiChat = new AiChat(
101
- 'user_123',
102
- 'proj_456',
103
- '/api', // configUrl (API base)
104
- 'h-full', // className
105
- true, true, true, // showMcpManager, showAiModelManager, showSystemContextEditor
106
- true // showAgentManager
107
- );
108
-
109
- export default function App() {
110
- return aiChat.render();
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
- 常用导出 Common exports
122
+ ## 组件API
115
123
 
116
- - 组件 Components: `StandaloneChatInterface`, `ChatInterface`, `MessageList`, `InputArea`, `SessionList`, `ThemeToggle`, `McpManager`, `AiModelManager`, `SystemContextEditor`
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
- ## 环境与配置 Env & Config
126
+ 主要的聊天界面组件。
122
127
 
123
- 前端 Frontend
128
+ #### Props
124
129
 
125
- - 开发环境 dev: Vite 代理将 `/api` 指向 `http://localhost:3001`
126
- - 生产 prod: 通过 `apiBaseUrl`/`port` 或自行配置反向代理 Configure `apiBaseUrl`/`port` or reverse proxy
130
+ | 属性 | 类型 | 默认值 | 描述 |
131
+ |------|------|--------|------|
132
+ | `className` | `string` | - | 自定义CSS类名 |
133
+ | `onMessageSend` | `(content: string, attachments?: Attachment[]) => void` | - | 消息发送回调 |
134
+ | `onSessionChange` | `(sessionId: string) => void` | - | 会话切换回调 |
135
+ | `customRenderer` | `CustomRenderer` | - | 自定义渲染器 |
127
136
 
128
- 后端 Server (`server/chat_app_node_server`)
137
+ ### 其他组件
129
138
 
130
- - `.env`
139
+ - `MessageList` - 消息列表组件
140
+ - `InputArea` - 输入区域组件
141
+ - `SessionList` - 会话列表组件
142
+ - `ThemeToggle` - 主题切换组件
143
+ - `McpManager` - MCP管理组件
144
+ - `AiModelManager` - AI模型管理组件
131
145
 
132
- ```env
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
- - 数据库 Database: 默认 SQLite,配置见 `server/chat_app_node_server/config/database.json`;可切换 MongoDB
148
+ ### useChatStore
140
149
 
141
- ## API 概览 API Overview
150
+ 聊天状态管理Hook。
142
151
 
143
- - Sessions: `/api/sessions` CRUD, messages: `/api/sessions/:id/messages`
144
- - Agents: `/api/agents` CRUD, stream: `/api/agents/chat/stream`
145
- - MCP: `/api/mcp-configs` CRUD, profiles, resource reading
146
- - AI Model Configs: `/api/ai-model-configs` CRUD
147
- - System Contexts: `/api/system-contexts` CRUD, active context
148
- - Streaming chat: `/api/agent_v2/chat/stream` (SSE)
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
- 详细见 See details: `server/chat_app_node_server/README.md`
169
+ ### useTheme
151
170
 
152
- ## 示例 Examples
171
+ 主题管理Hook。
153
172
 
154
- - `examples/complete-example`: 完整示例(含 Electron) Complete sample (with Electron)
173
+ ```tsx
174
+ import { useTheme } from '@leeoohoo/aichat';
155
175
 
156
- ```bash
157
- cd examples/complete-example
158
- npm install
159
- npm run dev:full # 前端 + 后端 Frontend + Server
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
- ## 开发脚本 Scripts (root)
187
+ ## 配置
163
188
 
164
- - `npm run dev` Vite 本地预览 Local dev
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
- ## 许可证 License
191
+ 在使用前,请确保设置以下环境变量:
171
192
 
172
- MIT
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
- 如需更细的接入与排障,参见 Also see: `USAGE.md`, `INTEGRATION_EXAMPLE.md`, `MODULE_CONTROL.md`。
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
+ 如果你觉得这个项目有用,请给它一个⭐️!