my-ai-chat-framework 2.0.0 → 3.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/CHANGELOG.md +65 -0
- package/LICENSE +20 -20
- package/README.md +329 -45
- package/README_ZH.md +375 -0
- package/dist/my-ai-chat-framework.browser.es.js +1581 -207
- package/dist/my-ai-chat-framework.browser.es.js.map +1 -1
- package/dist/my-ai-chat-framework.browser.umd.js +1594 -211
- package/dist/my-ai-chat-framework.browser.umd.js.map +1 -1
- package/dist/my-ai-chat-framework.node.cjs.js +1594 -211
- package/dist/my-ai-chat-framework.node.cjs.js.map +1 -1
- package/docs/DEVELOPER.md +479 -0
- package/package.json +26 -3
- package/src/adapters/openai.js +289 -64
- package/src/core/ChatService.js +652 -110
- package/src/core/Errors.js +60 -0
- package/src/core/EventEmitter.js +19 -0
- package/src/core/MessageStore.js +66 -0
- package/src/core/Pipeline.js +48 -0
- package/src/core/SystemPromptStore.js +118 -0
- package/src/index.js +12 -14
- package/src/plugins/model-registry.js +223 -0
- package/src/plugins/tool-calling.js +229 -105
- package/src/utils/MessageFormatter.js +204 -0
- package/src/utils/typeCheck.js +12 -0
- package/src/utils/url.js +18 -0
- package/.env +0 -15
- package/test.js +0 -107
package/test.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
// test.js - 测试新框架核心功能
|
|
2
|
-
import 'dotenv/config'; // 如果已安装 dotenv,否则手动读取 .env
|
|
3
|
-
import { ChatService, openaiAdapter, toolCallingPlugin } from './src/index.js';
|
|
4
|
-
|
|
5
|
-
// 从环境变量读取配置(确保 .env 文件存在)
|
|
6
|
-
const apiKey = process.env.DEEPSEEK_API_KEY;
|
|
7
|
-
const apiUrl = process.env.DEEPSEEK_API_URL || 'https://api.deepseek.com/v1/chat/completions';
|
|
8
|
-
const model = process.env.TEST_MODEL || 'deepseek-chat';
|
|
9
|
-
|
|
10
|
-
if (!apiKey) {
|
|
11
|
-
console.error('❌ 未找到 API Key,请设置 DEEPSEEK_API_KEY 环境变量或创建 .env 文件');
|
|
12
|
-
process.exit(1);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
console.log('🚀 开始测试新框架...\n');
|
|
16
|
-
|
|
17
|
-
// 创建聊天服务实例
|
|
18
|
-
const chat = new ChatService({
|
|
19
|
-
apiKey,
|
|
20
|
-
model,
|
|
21
|
-
apiUrl,
|
|
22
|
-
temperature: 0.7,
|
|
23
|
-
maxTokens: 2000
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
// 加载适配器和插件
|
|
27
|
-
chat.use(openaiAdapter);
|
|
28
|
-
chat.use(toolCallingPlugin);
|
|
29
|
-
|
|
30
|
-
// 监听事件(便于观察)
|
|
31
|
-
chat.on('sending', msg => {
|
|
32
|
-
console.log('📤 发送消息:', msg.content?.substring(0, 50) || msg);
|
|
33
|
-
});
|
|
34
|
-
chat.on('message', msg => {
|
|
35
|
-
console.log('📥 收到回复:', msg.content?.substring(0, 100) || msg);
|
|
36
|
-
});
|
|
37
|
-
chat.on('stream-progress', chunk => {
|
|
38
|
-
console.log('🌊 流式数据块:', chunk.content?.substring(0, 30) || '');
|
|
39
|
-
});
|
|
40
|
-
chat.on('error', err => {
|
|
41
|
-
console.error('❌ 错误:', err.message);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// 注册一个简单测试工具(不实际调用,只验证注册和工具调用流程)
|
|
45
|
-
chat.registerTool('greet', '打招呼工具', async (args) => {
|
|
46
|
-
console.log('🔧 工具被调用:', args);
|
|
47
|
-
return `你好,${args.name || '朋友'}!`;
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
// 辅助函数:等待指定毫秒
|
|
51
|
-
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
52
|
-
|
|
53
|
-
// 测试 1:基础发送消息(非流式)
|
|
54
|
-
console.log('===== 测试1:基础发送消息 =====');
|
|
55
|
-
const response = await chat.send('请简单说一句:你好,世界!');
|
|
56
|
-
console.log('最终回复:', response.content);
|
|
57
|
-
console.log();
|
|
58
|
-
|
|
59
|
-
// 等待一下,避免请求过快
|
|
60
|
-
await sleep(1000);
|
|
61
|
-
|
|
62
|
-
// 测试 2:流式发送消息
|
|
63
|
-
console.log('===== 测试2:流式发送消息 =====');
|
|
64
|
-
let streamContent = '';
|
|
65
|
-
await chat.stream('请用流式方式回答:告诉我一个好消息。',
|
|
66
|
-
(chunk) => {
|
|
67
|
-
streamContent += chunk.content;
|
|
68
|
-
// 实时打印已接收内容(可选)
|
|
69
|
-
// process.stdout.write(chunk.content);
|
|
70
|
-
},
|
|
71
|
-
(final) => {
|
|
72
|
-
console.log('流式完成,完整内容:', final.content);
|
|
73
|
-
}
|
|
74
|
-
);
|
|
75
|
-
console.log();
|
|
76
|
-
|
|
77
|
-
await sleep(1000);
|
|
78
|
-
|
|
79
|
-
// 测试 3:工具调用
|
|
80
|
-
console.log('===== 测试3:工具调用 =====');
|
|
81
|
-
// 注意:需要让 AI 主动调用 greet 工具。由于 deepseek-chat 支持函数调用,我们可以在系统提示或用户消息中引导。
|
|
82
|
-
// 我们在用户消息中明确要求调用工具,但 AI 是否调用取决于模型。这里做一个简单引导。
|
|
83
|
-
const toolResponse = await chat.send('请使用 greet 工具,向 "小明" 打招呼。');
|
|
84
|
-
console.log('工具调用后的回复:', toolResponse.content);
|
|
85
|
-
console.log();
|
|
86
|
-
|
|
87
|
-
await sleep(1000);
|
|
88
|
-
|
|
89
|
-
// 测试 4:验证消息存储
|
|
90
|
-
console.log('===== 测试4:消息存储验证 =====');
|
|
91
|
-
const allMessages = chat.messages.getAll();
|
|
92
|
-
console.log(`当前消息总数: ${allMessages.length}`);
|
|
93
|
-
allMessages.forEach((msg, i) => {
|
|
94
|
-
console.log(`[${i}] ${msg.role}: ${msg.content?.substring(0, 40)}${msg.content?.length > 40 ? '...' : ''}`);
|
|
95
|
-
if (msg.toolCalls) console.log(' 工具调用:', msg.toolCalls);
|
|
96
|
-
if (msg.toolCallId) console.log(' 工具调用ID:', msg.toolCallId);
|
|
97
|
-
});
|
|
98
|
-
console.log();
|
|
99
|
-
|
|
100
|
-
// 测试 5:撤回功能(可选)
|
|
101
|
-
console.log('===== 测试5:撤回最后一条助手消息 =====');
|
|
102
|
-
const removed = chat.messages.undoToLastAssistant();
|
|
103
|
-
console.log(`撤回了 ${removed.length} 条消息`);
|
|
104
|
-
const afterUndo = chat.messages.getAll();
|
|
105
|
-
console.log(`剩余消息数: ${afterUndo.length}`);
|
|
106
|
-
|
|
107
|
-
console.log('\n✅ 所有测试完成!');
|