my-ai-chat-framework 1.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 My Name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,305 @@
1
+ # 🤖 My AI Chat Framework / 我的AI聊天框架
2
+
3
+ ## 🎯 概述 / Overview
4
+ 这是一个现代化的AI聊天框架,支持工具调用、流式传输和事件驱动架构。旨在帮助开发者构建功能丰富的AI聊天应用。
5
+
6
+ This is a modern AI chat framework with tool calling, streaming, and event-driven architecture. Designed to help developers build feature-rich AI chat applications.
7
+
8
+ ## ✨ 新功能特性 / New Features
9
+ - **🔧 工具调用系统** / **Tool Calling System**: 完整的OpenAI兼容工具调用支持,包括工具注册、执行和错误处理
10
+ - **🌊 流式传输** / **Streaming**: 实时流式传输,支持工具调用和进度回调
11
+ - **⚡ 事件驱动架构** / **Event-Driven Architecture**: 基于EventEmitter的丰富事件系统
12
+ - **📦 模块化设计** / **Modular Design**: 清晰的关注点分离,易于扩展和维护
13
+
14
+ ## ✅ 核心功能 / Core Features
15
+ - **消息管理** / **Message Management**: 包含一个`Messages`类,用于处理聊天消息的存储和操作
16
+ - **请求构建** / **Request Building**: `RequestBuilder`类构建发送到聊天API的请求体
17
+ - **API交互** / **API Interaction**: `ApiClient`类管理与聊天API的HTTP请求,支持标准和流式请求
18
+ - **工具管理** / **Tool Management**: `ToolManager`类负责工具的注册、管理和执行
19
+ - **事件系统** / **Event System**: `EventEmitter`类提供完整的事件驱动架构
20
+ - **格式转换** / **Format Conversion**: `MessageFormatter`类负责消息格式转换
21
+
22
+ ## 📁 项目结构 / Project Structure
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
+ ```
70
+
71
+ ### Node.js中使用 / For Node.js Usage
72
+
73
+ #### 安装
74
+ ```bash
75
+ npm install my-ai-chat-framework axios
76
+ # 或
77
+ yarn add my-ai-chat-framework axios
78
+ # 或
79
+ pnpm add my-ai-chat-framework axios
80
+ ```
81
+
82
+ #### 使用
83
+ ```javascript
84
+ // CommonJS
85
+ const { ChatService, Messages } = require('my-ai-chat-framework');
86
+
87
+ // ES模块
88
+ import { ChatService, Messages } from 'my-ai-chat-framework';
89
+
90
+ // 创建聊天服务
91
+ const chat = new ChatService('your-api-key');
92
+ ```
93
+
94
+ ### 本地开发 / Local Development
95
+
96
+ #### 安装依赖
97
+ ```bash
98
+ npm install
99
+ ```
100
+
101
+ ### 构建项目 / Build Project
102
+ ```bash
103
+ npm run build
104
+ ```
105
+
106
+ ### 输出文件 / Output Files
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依赖。
113
+
114
+ ### 预览构建结果 / Preview Build
115
+ ```bash
116
+ npm run preview
117
+ ```
118
+
119
+ ### 在浏览器中使用 / Usage in Browser
120
+
121
+ #### 传统浏览器(UMD模块)/ Traditional Browser (UMD Module)
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
+ ```
130
+
131
+ #### 现代浏览器(ES模块)/ Modern Browser (ES Module)
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
+ ```
138
+
139
+ ### 在Node.js中使用 / Usage in Node.js
140
+ ```javascript
141
+ const { Messages, ChatService } = require('./dist/my-ai-chat-framework.cjs.js');
142
+ const chat = new ChatService('your-api-key');
143
+ ```
144
+
145
+ ## 核心类说明 / Core Classes Description
146
+
147
+ ### Messages 类
148
+ 管理对话消息的核心类,支持:
149
+ - 消息的推送、修改、撤回
150
+ - 系统提示词管理
151
+ - 工具定义管理
152
+ - 数据导入/导出
153
+
154
+ ### ChatRequestBuilder 类
155
+ 构建符合API规范的请求体,支持:
156
+ - 自动格式化消息
157
+ - 配置模型参数
158
+ - 生成JSON请求体
159
+
160
+ ### ApiClient 类
161
+ HTTP客户端,支持:
162
+ - 普通请求和流式请求
163
+ - 请求中断
164
+ - 错误处理
165
+ - 前后端通用(使用axios)
166
+
167
+ ### ChatService 类
168
+ 高级封装类,简化使用:
169
+ - 集成所有核心功能
170
+ - 提供简单API接口
171
+ - 自动管理消息状态
172
+
173
+ ## 示例代码 / Example Code
174
+
175
+ ```javascript
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
+ );
194
+
195
+ // 撤回消息 / Undo message
196
+ chat.undo();
197
+
198
+ // 导出对话 / Export conversation
199
+ const exportData = chat.export();
200
+
201
+ ## 📤 npm发布 / npm Publishing
202
+
203
+ ### 发布准备
204
+ 1. 确保已登录npm账号:
205
+ ```bash
206
+ npm login
207
+ ```
208
+
209
+ 2. 更新版本号(如果需要):
210
+ ```bash
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
+ );
287
+ ```
288
+
289
+ ## 📚 更多文档 / More Documentation
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.
297
+
298
+ ## 依赖库 / Dependencies
299
+ - [axios](https://github.com/axios/axios) - MIT License
300
+ - [vite](https://vitejs.dev/) - MIT License
301
+
302
+ ---
303
+
304
+ *此文档由AI辅助生成,内容可能不完整或存在错误,请以实际代码为准。*
305
+ *This document is AI-assisted and may be incomplete or contain errors. Please refer to the actual code for accurate information.*