my-ai-chat-framework 1.0.2 → 2.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/.env +15 -0
- package/README.md +70 -268
- package/dist/my-ai-chat-framework.browser.es.js +427 -2627
- package/dist/my-ai-chat-framework.browser.es.js.map +1 -1
- package/dist/my-ai-chat-framework.browser.umd.js +442 -2633
- package/dist/my-ai-chat-framework.browser.umd.js.map +1 -1
- package/dist/my-ai-chat-framework.node.cjs.js +436 -1378
- package/dist/my-ai-chat-framework.node.cjs.js.map +1 -1
- package/package.json +15 -32
- package/src/adapters/openai.js +130 -0
- package/src/core/ChatService.js +110 -0
- package/src/core/EventEmitter.js +32 -126
- package/src/core/MessageStore.js +73 -0
- package/src/index.js +10 -414
- package/src/plugins/tool-calling.js +119 -0
- package/test.js +107 -0
- package/README_ZH.md +0 -401
- package/dist/my-ai-chat-framework.cjs.js +0 -2630
- package/dist/my-ai-chat-framework.cjs.js.map +0 -1
- package/dist/my-ai-chat-framework.es.js +0 -2630
- package/dist/my-ai-chat-framework.es.js.map +0 -1
- package/dist/my-ai-chat-framework.umd.js +0 -2634
- package/dist/my-ai-chat-framework.umd.js.map +0 -1
- package/src/core/ApiClient.js +0 -288
- package/src/core/MessageFormatter.js +0 -126
- package/src/core/Messages.js +0 -562
- package/src/core/RequestBuilder.js +0 -96
- package/src/core/ToolManager.js +0 -175
- package/src/utils/index.js +0 -21
package/.env
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# AI Chat Framework 环境变量配置
|
|
2
|
+
|
|
3
|
+
# DeepSeek API 配置
|
|
4
|
+
DEEPSEEK_API_KEY=sk-3b396e7fd64b463390d4ff033c6e7e0b
|
|
5
|
+
DEEPSEEK_API_URL=https://api.deepseek.com/v1/chat/completions
|
|
6
|
+
|
|
7
|
+
# 测试配置
|
|
8
|
+
TEST_API_KEY=sk-3b396e7fd64b463390d4ff033c6e7e0b
|
|
9
|
+
TEST_MODEL=deepseek-chat
|
|
10
|
+
TEST_TEMPERATURE=0.7
|
|
11
|
+
TEST_MAX_TOKENS=2000
|
|
12
|
+
|
|
13
|
+
# 调试配置
|
|
14
|
+
DEBUG=false
|
|
15
|
+
LOG_LEVEL=info
|
package/README.md
CHANGED
|
@@ -1,305 +1,107 @@
|
|
|
1
|
-
# 🤖 My AI Chat Framework / 我的AI聊天框架
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
**⚠️ Note**: This project is created for **learning purposes** and is **AI‑generated**. It is not intended for production use. No backward compatibility is guaranteed. Use at your own risk.
|
|
3
|
+
|
|
4
|
+
# 🤖 My AI Chat Framework
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
A lightweight, modular AI chat framework with plugin system, unified message format, and tool calling support.
|
|
7
7
|
|
|
8
|
-
## ✨
|
|
9
|
-
- **🔧 工具调用系统** / **Tool Calling System**: 完整的OpenAI兼容工具调用支持,包括工具注册、执行和错误处理
|
|
10
|
-
- **🌊 流式传输** / **Streaming**: 实时流式传输,支持工具调用和进度回调
|
|
11
|
-
- **⚡ 事件驱动架构** / **Event-Driven Architecture**: 基于EventEmitter的丰富事件系统
|
|
12
|
-
- **📦 模块化设计** / **Modular Design**: 清晰的关注点分离,易于扩展和维护
|
|
8
|
+
## ✨ Features
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
- **
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
10
|
+
- **Lightweight Core** – Only ~300 lines, easy to understand and extend.
|
|
11
|
+
- **Plugin System** – Add features like tool calling, reasoning, or custom adapters without touching the core.
|
|
12
|
+
- **Unified Message Format** – Consistent data structure across all components.
|
|
13
|
+
- **Multi‑Environment** – Builds ES module, UMD, and CommonJS formats for browser and Node.js.
|
|
14
|
+
- **No External Dependencies** – Uses native `fetch` (Node 18+ & modern browsers).
|
|
15
|
+
- **Tool Calling** – Built‑in plugin to handle function calls from AI models.
|
|
16
|
+
- **Streaming** – Full support for real‑time responses.
|
|
21
17
|
|
|
22
|
-
##
|
|
23
|
-
```
|
|
24
|
-
my-ai-chat-framework/
|
|
25
|
-
├── dist/ # 打包输出目录 / Build output
|
|
26
|
-
│ ├── my-ai-chat-framework.es.js # ES模块 / ES Module
|
|
27
|
-
│ ├── my-ai-chat-framework.umd.js # UMD模块(注册到window.AIChatFramework)/ UMD Module
|
|
28
|
-
│ └── my-ai-chat-framework.cjs.js # CommonJS模块 / CommonJS Module
|
|
29
|
-
├── src/ # 源代码 / Source code
|
|
30
|
-
│ ├── core/ # 核心类 / Core classes
|
|
31
|
-
│ │ ├── EventEmitter.js # 事件系统 / Event System
|
|
32
|
-
│ │ ├── Messages.js # 消息状态管理 / Message State Management
|
|
33
|
-
│ │ ├── MessageFormatter.js # 消息格式转换 / Message Format Conversion
|
|
34
|
-
│ │ ├── RequestBuilder.js # 请求构建器 / Request Builder
|
|
35
|
-
│ │ ├── ApiClient.js # API客户端(使用axios)/ API Client (axios)
|
|
36
|
-
│ │ └── ToolManager.js # 工具管理器 / Tool Manager
|
|
37
|
-
│ ├── utils/ # 工具函数 / Utility functions
|
|
38
|
-
│ └── index.js # 主入口文件 / Main entry point
|
|
39
|
-
├── test-*.html # 各种测试页面 / Various test pages
|
|
40
|
-
├── README_ZH.md # 详细中文文档 / Detailed Chinese documentation
|
|
41
|
-
├── FIX_SUMMARY.md # 修复总结 / Fix summary
|
|
42
|
-
├── vite.config.js # Vite配置 / Vite configuration
|
|
43
|
-
└── package.json # 项目配置 / Project configuration
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## 📦 安装和使用 / Installation and Usage
|
|
47
|
-
|
|
48
|
-
### 浏览器中使用 / For Browser Usage
|
|
49
|
-
|
|
50
|
-
#### 方式1:直接引入UMD版本(推荐)
|
|
51
|
-
```html
|
|
52
|
-
<script src="https://unpkg.com/my-ai-chat-framework@latest/dist/my-ai-chat-framework.browser.umd.js"></script>
|
|
53
|
-
<script>
|
|
54
|
-
// 全局变量 AIChatFramework 可用
|
|
55
|
-
const { ChatService, Messages } = AIChatFramework;
|
|
56
|
-
|
|
57
|
-
// 创建聊天服务
|
|
58
|
-
const chat = new ChatService('your-api-key');
|
|
59
|
-
</script>
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
#### 方式2:使用ES模块
|
|
63
|
-
```html
|
|
64
|
-
<script type="module">
|
|
65
|
-
import { ChatService, Messages } from 'https://unpkg.com/my-ai-chat-framework@latest/dist/my-ai-chat-framework.browser.es.js';
|
|
66
|
-
|
|
67
|
-
const chat = new ChatService('your-api-key');
|
|
68
|
-
</script>
|
|
69
|
-
```
|
|
18
|
+
## 📦 Installation
|
|
70
19
|
|
|
71
|
-
### Node.js中使用 / For Node.js Usage
|
|
72
|
-
|
|
73
|
-
#### 安装
|
|
74
20
|
```bash
|
|
75
|
-
npm install my-ai-chat-framework
|
|
76
|
-
# 或
|
|
77
|
-
yarn add my-ai-chat-framework axios
|
|
78
|
-
# 或
|
|
79
|
-
pnpm add my-ai-chat-framework axios
|
|
21
|
+
npm install my-ai-chat-framework
|
|
80
22
|
```
|
|
81
23
|
|
|
82
|
-
|
|
24
|
+
## 🚀 Quick Start
|
|
25
|
+
|
|
83
26
|
```javascript
|
|
84
|
-
|
|
85
|
-
const { ChatService, Messages } = require('my-ai-chat-framework');
|
|
27
|
+
import { ChatService, openaiAdapter, toolCallingPlugin } from 'my-ai-chat-framework';
|
|
86
28
|
|
|
87
|
-
|
|
88
|
-
|
|
29
|
+
const chat = new ChatService({
|
|
30
|
+
apiKey: 'your-api-key',
|
|
31
|
+
model: 'deepseek-chat',
|
|
32
|
+
apiUrl: 'https://api.deepseek.com/v1/chat/completions'
|
|
33
|
+
});
|
|
89
34
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
```
|
|
35
|
+
chat.use(openaiAdapter);
|
|
36
|
+
chat.use(toolCallingPlugin);
|
|
93
37
|
|
|
94
|
-
|
|
38
|
+
chat.registerTool('greet', 'Say hello', async (args) => {
|
|
39
|
+
return `Hello, ${args.name}!`;
|
|
40
|
+
});
|
|
95
41
|
|
|
96
|
-
|
|
97
|
-
```bash
|
|
98
|
-
npm install
|
|
99
|
-
```
|
|
42
|
+
chat.on('message', msg => console.log(msg.content));
|
|
100
43
|
|
|
101
|
-
|
|
102
|
-
```bash
|
|
103
|
-
npm run build
|
|
44
|
+
await chat.send('Please greet Alice.');
|
|
104
45
|
```
|
|
105
46
|
|
|
106
|
-
|
|
107
|
-
构建后会在 `dist/` 目录生成以下文件:
|
|
108
|
-
- `my-ai-chat-framework.es.js` - ES模块格式(包含axios依赖)
|
|
109
|
-
- `my-ai-chat-framework.umd.js` - UMD格式(包含axios依赖)
|
|
110
|
-
- `my-ai-chat-framework.cjs.js` - CommonJS格式(包含axios依赖)
|
|
111
|
-
|
|
112
|
-
**注意**: axios已被打包进所有输出文件中,用户无需额外安装axios依赖。
|
|
47
|
+
## 🔌 Plugins & Adapters
|
|
113
48
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
npm run preview
|
|
117
|
-
```
|
|
49
|
+
- **openaiAdapter** – Converts internal messages to OpenAI-compatible format.
|
|
50
|
+
- **toolCallingPlugin** – Detects `tool_calls` in responses, executes tools, and continues the conversation.
|
|
118
51
|
|
|
119
|
-
|
|
52
|
+
You can easily create your own adapter for other APIs (Anthropic, Cohere, etc.) or plugins for logging, caching, etc.
|
|
120
53
|
|
|
121
|
-
|
|
122
|
-
```html
|
|
123
|
-
<script src="./dist/my-ai-chat-framework.umd.js"></script>
|
|
124
|
-
<script>
|
|
125
|
-
// 使用全局变量 / Use global variable
|
|
126
|
-
const { Messages, ApiClient, ChatRequestBuilder, ChatService } = window.AIChatFramework;
|
|
127
|
-
const chat = new ChatService('your-api-key');
|
|
128
|
-
</script>
|
|
129
|
-
```
|
|
54
|
+
## 📚 API
|
|
130
55
|
|
|
131
|
-
|
|
132
|
-
```html
|
|
133
|
-
<script type="module">
|
|
134
|
-
import { Messages, ChatService } from './dist/my-ai-chat-framework.es.js';
|
|
135
|
-
const chat = new ChatService('your-api-key');
|
|
136
|
-
</script>
|
|
137
|
-
```
|
|
56
|
+
### ChatService
|
|
138
57
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
58
|
+
```typescript
|
|
59
|
+
new ChatService(config: {
|
|
60
|
+
apiKey: string;
|
|
61
|
+
model?: string;
|
|
62
|
+
apiUrl?: string;
|
|
63
|
+
temperature?: number;
|
|
64
|
+
maxTokens?: number;
|
|
65
|
+
})
|
|
143
66
|
```
|
|
144
67
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
- 消息的推送、修改、撤回
|
|
150
|
-
- 系统提示词管理
|
|
151
|
-
- 工具定义管理
|
|
152
|
-
- 数据导入/导出
|
|
68
|
+
- `use(plugin)` – Load a plugin.
|
|
69
|
+
- `send(message)` – Send a message and wait for the complete response.
|
|
70
|
+
- `stream(message, onProgress, onDone)` – Stream the response.
|
|
71
|
+
- `registerTool(name, description, executor)` – Register a tool (only available after loading `toolCallingPlugin`).
|
|
153
72
|
|
|
154
|
-
###
|
|
155
|
-
构建符合API规范的请求体,支持:
|
|
156
|
-
- 自动格式化消息
|
|
157
|
-
- 配置模型参数
|
|
158
|
-
- 生成JSON请求体
|
|
73
|
+
### MessageStore
|
|
159
74
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
-
|
|
163
|
-
-
|
|
164
|
-
-
|
|
165
|
-
-
|
|
75
|
+
- `add(message)` – Add a message (unified format).
|
|
76
|
+
- `addUser(content, metadata)` – Convenience method.
|
|
77
|
+
- `addAssistant(content, metadata)`
|
|
78
|
+
- `addSystem(content, metadata)`
|
|
79
|
+
- `addTool(content, toolCallId, metadata)`
|
|
80
|
+
- `getAll()` – Get all messages.
|
|
81
|
+
- `undoToLastAssistant()` – Rollback to the last assistant message.
|
|
166
82
|
|
|
167
|
-
###
|
|
168
|
-
高级封装类,简化使用:
|
|
169
|
-
- 集成所有核心功能
|
|
170
|
-
- 提供简单API接口
|
|
171
|
-
- 自动管理消息状态
|
|
83
|
+
### EventEmitter
|
|
172
84
|
|
|
173
|
-
|
|
85
|
+
- `on(event, handler)` – Subscribe to events.
|
|
86
|
+
- `off(event, handler)` – Unsubscribe.
|
|
87
|
+
- `emit(event, data)` – Emit an event.
|
|
174
88
|
|
|
175
|
-
|
|
176
|
-
// 创建聊天服务 / Create chat service
|
|
177
|
-
const chat = new ChatService('your-api-key', 'deepseek-chat');
|
|
178
|
-
|
|
179
|
-
// 发送消息 / Send message
|
|
180
|
-
const response = await chat.send('你好,世界!');
|
|
181
|
-
console.log('AI回复:', response);
|
|
182
|
-
|
|
183
|
-
// 流式消息 / Stream message
|
|
184
|
-
await chat.stream('告诉我一个故事',
|
|
185
|
-
(chunk) => console.log('收到数据:', chunk),
|
|
186
|
-
() => console.log('流式传输完成')
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
// 流式传输中的工具调用 / Tool calling in streaming
|
|
190
|
-
await chat.stream('查询北京天气',
|
|
191
|
-
(chunk) => console.log('实时数据:', chunk.content),
|
|
192
|
-
(final) => console.log('最终结果:', final.content)
|
|
193
|
-
);
|
|
89
|
+
Events: `sending`, `message`, `stream-progress`, `error`, etc.
|
|
194
90
|
|
|
195
|
-
|
|
196
|
-
chat.undo();
|
|
91
|
+
## 🛠️ Development
|
|
197
92
|
|
|
198
|
-
// 导出对话 / Export conversation
|
|
199
|
-
const exportData = chat.export();
|
|
200
|
-
|
|
201
|
-
## 📤 npm发布 / npm Publishing
|
|
202
|
-
|
|
203
|
-
### 发布准备
|
|
204
|
-
1. 确保已登录npm账号:
|
|
205
93
|
```bash
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
npm version patch # 小版本更新
|
|
212
|
-
npm version minor # 中版本更新
|
|
213
|
-
npm version major # 大版本更新
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
3. 构建项目:
|
|
217
|
-
```bash
|
|
218
|
-
npm run build
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
### 发布到npm
|
|
222
|
-
```bash
|
|
223
|
-
npm publish
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
### 发布后验证
|
|
227
|
-
1. 检查包是否发布成功:
|
|
228
|
-
```bash
|
|
229
|
-
npm view my-ai-chat-framework
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
2. 测试安装:
|
|
233
|
-
```bash
|
|
234
|
-
npm install my-ai-chat-framework@latest
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### 包信息
|
|
238
|
-
- **包名**: `my-ai-chat-framework`
|
|
239
|
-
- **当前版本**: 1.0.2
|
|
240
|
-
- **许可证**: MIT
|
|
241
|
-
- **关键词**: AI, chat, framework, messages, API, deepseek, openai, tool-calling
|
|
242
|
-
|
|
243
|
-
## 📄 许可证 / License
|
|
244
|
-
MIT License - 详见 [LICENSE](LICENSE) 文件
|
|
245
|
-
|
|
246
|
-
## 🤝 贡献 / Contributing
|
|
247
|
-
欢迎提交Issue和Pull Request!
|
|
248
|
-
|
|
249
|
-
## 📞 支持 / Support
|
|
250
|
-
如有问题,请提交Issue或联系作者。
|
|
251
|
-
|
|
252
|
-
---
|
|
253
|
-
|
|
254
|
-
**✨ 感谢使用 My AI Chat Framework! ✨**
|
|
255
|
-
|
|
256
|
-
// 导入对话 / Import conversation
|
|
257
|
-
chat.import(exportData);
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
## 🌊 流式传输下的工具调用 / Tool Calling in Streaming
|
|
261
|
-
|
|
262
|
-
### 工作原理 / How It Works
|
|
263
|
-
框架自动处理流式传输中的工具调用:
|
|
264
|
-
1. **流式收集** / **Stream Collection**: 在流式传输过程中收集完整的工具调用信息
|
|
265
|
-
2. **工具执行** / **Tool Execution**: 执行所有请求的工具
|
|
266
|
-
3. **继续流式** / **Continue Streaming**: 将工具结果发送给AI并继续流式传输
|
|
267
|
-
|
|
268
|
-
### 代码示例 / Code Example
|
|
269
|
-
```javascript
|
|
270
|
-
// 注册工具 / Register tool
|
|
271
|
-
chat.registerTool('get_weather', toolDefinition, async (args) => {
|
|
272
|
-
return { temperature: '25°C', condition: 'Sunny' };
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
// 流式传输会自动处理工具调用 / Streaming automatically handles tool calls
|
|
276
|
-
await chat.stream('What is the weather in Beijing?',
|
|
277
|
-
(chunk) => {
|
|
278
|
-
// 实时接收流式数据 / Receive streaming data in real-time
|
|
279
|
-
if (chunk.content) console.log('Content:', chunk.content);
|
|
280
|
-
if (chunk.tool_calls) console.log('Tool calls:', chunk.tool_calls);
|
|
281
|
-
},
|
|
282
|
-
(final) => {
|
|
283
|
-
// 流式传输完成 / Streaming completed
|
|
284
|
-
console.log('Final result:', final.content);
|
|
285
|
-
}
|
|
286
|
-
);
|
|
94
|
+
git clone https://github.com/your-username/my-ai-chat-framework.git
|
|
95
|
+
cd my-ai-chat-framework
|
|
96
|
+
npm install
|
|
97
|
+
npm run build # build the library
|
|
98
|
+
npm test # run the test script
|
|
287
99
|
```
|
|
288
100
|
|
|
289
|
-
##
|
|
290
|
-
- **[详细中文文档](README_ZH.md)** - 完整的功能说明和使用示例
|
|
291
|
-
- **[修复总结](FIX_SUMMARY.md)** - 技术修复细节和架构改进
|
|
292
|
-
|
|
293
|
-
## 许可证 / License
|
|
294
|
-
本项目采用MIT许可证。详见LICENSE文件。
|
|
295
|
-
|
|
296
|
-
This project is licensed under the MIT License. See the LICENSE file for more details.
|
|
101
|
+
## 📄 License
|
|
297
102
|
|
|
298
|
-
|
|
299
|
-
- [axios](https://github.com/axios/axios) - MIT License
|
|
300
|
-
- [vite](https://vitejs.dev/) - MIT License
|
|
103
|
+
MIT
|
|
301
104
|
|
|
302
|
-
|
|
105
|
+
## 🤝 Contributing
|
|
303
106
|
|
|
304
|
-
|
|
305
|
-
*This document is AI-assisted and may be incomplete or contain errors. Please refer to the actual code for accurate information.*
|
|
107
|
+
Contributions are welcome! Please open an issue or submit a pull request.
|