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 +21 -0
- package/README.md +305 -0
- package/README_ZH.md +401 -0
- package/dist/my-ai-chat-framework.browser.es.js +2630 -0
- package/dist/my-ai-chat-framework.browser.es.js.map +1 -0
- package/dist/my-ai-chat-framework.browser.umd.js +2634 -0
- package/dist/my-ai-chat-framework.browser.umd.js.map +1 -0
- package/dist/my-ai-chat-framework.cjs.js +2630 -0
- package/dist/my-ai-chat-framework.cjs.js.map +1 -0
- package/dist/my-ai-chat-framework.es.js +2630 -0
- package/dist/my-ai-chat-framework.es.js.map +1 -0
- package/dist/my-ai-chat-framework.node.cjs.js +1381 -0
- package/dist/my-ai-chat-framework.node.cjs.js.map +1 -0
- package/dist/my-ai-chat-framework.umd.js +2634 -0
- package/dist/my-ai-chat-framework.umd.js.map +1 -0
- package/package.json +54 -0
- package/src/core/ApiClient.js +288 -0
- package/src/core/EventEmitter.js +126 -0
- package/src/core/MessageFormatter.js +126 -0
- package/src/core/Messages.js +562 -0
- package/src/core/RequestBuilder.js +96 -0
- package/src/core/ToolManager.js +175 -0
- package/src/index.js +419 -0
- package/src/utils/index.js +21 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import EventEmitter from './EventEmitter'
|
|
2
|
+
/**
|
|
3
|
+
* 工具管理器
|
|
4
|
+
* 负责注册、管理和执行工具
|
|
5
|
+
*/
|
|
6
|
+
class ToolManager extends EventEmitter {
|
|
7
|
+
constructor() {
|
|
8
|
+
super()
|
|
9
|
+
this.tools = new Map() // 工具名 -> 工具函数
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 注册工具
|
|
13
|
+
* @param {string} name - 工具名称
|
|
14
|
+
* @param {object} toolDefinition - 工具定义(包含描述和参数信息)
|
|
15
|
+
* @param {Function} executor - 工具执行函数,接受参数对象并返回结果
|
|
16
|
+
*/
|
|
17
|
+
registerTool(name, toolDefinition, executor) {
|
|
18
|
+
// 判断是否已经注册过同名工具
|
|
19
|
+
if (this.tools.has(name)) {
|
|
20
|
+
throw new Error(`[系统提示异常] 工具 "${name}" 已经注册过了`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//在工具管理器中注册工具的函数
|
|
24
|
+
this.tools.set(name, {
|
|
25
|
+
definition: toolDefinition,
|
|
26
|
+
executor: executor
|
|
27
|
+
})
|
|
28
|
+
// 触发工具注册事件,供外部监听
|
|
29
|
+
this.emit('tool-registered', { name, definition: toolDefinition })
|
|
30
|
+
// 返回this以支持链式调用
|
|
31
|
+
return this
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 执行工具调用
|
|
36
|
+
* @param {object} toolCall - 工具调用信息,包含工具名称和参数
|
|
37
|
+
* @returns {Promise<any>} - 工具执行结果
|
|
38
|
+
*/
|
|
39
|
+
async executeToolCall(toolCall) {
|
|
40
|
+
const { id, function: func } = toolCall;
|
|
41
|
+
const { name, arguments: argsStr } = func;
|
|
42
|
+
|
|
43
|
+
if (!this.tools.has(name)) {
|
|
44
|
+
throw new Error(`未找到工具: ${name}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const tool = this.tools.get(name);
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
// 解析参数
|
|
51
|
+
const args = JSON.parse(argsStr);
|
|
52
|
+
|
|
53
|
+
// 触发事件:开始执行工具
|
|
54
|
+
this.emit('tool-execute-start', { name, args });
|
|
55
|
+
|
|
56
|
+
// 执行工具
|
|
57
|
+
const result = await tool.executor(args);
|
|
58
|
+
|
|
59
|
+
// 触发事件:工具执行成功
|
|
60
|
+
this.emit('tool-execute-success', { name, args, result });
|
|
61
|
+
|
|
62
|
+
// 确保返回正确的格式
|
|
63
|
+
return {
|
|
64
|
+
tool_call_id: id, // 必须包含tool_call_id
|
|
65
|
+
name,
|
|
66
|
+
result: JSON.stringify(result),
|
|
67
|
+
success: true
|
|
68
|
+
};
|
|
69
|
+
} catch (error) {
|
|
70
|
+
// 触发事件:工具执行失败
|
|
71
|
+
this.emit('tool-execute-error', { name, error });
|
|
72
|
+
|
|
73
|
+
// 即使失败也要返回tool_call_id
|
|
74
|
+
return {
|
|
75
|
+
tool_call_id: id,
|
|
76
|
+
name,
|
|
77
|
+
error: error.message,
|
|
78
|
+
success: false
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 获取所有工具定义(用于发送给AI)
|
|
85
|
+
*/
|
|
86
|
+
getToolDefinitions() {
|
|
87
|
+
const definitions = [];
|
|
88
|
+
for (const [name, tool] of this.tools) {
|
|
89
|
+
definitions.push(tool.definition);
|
|
90
|
+
}
|
|
91
|
+
return definitions;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 检查是否有工具
|
|
96
|
+
*/
|
|
97
|
+
hasTools() {
|
|
98
|
+
return this.tools.size > 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 批量执行工具调用
|
|
103
|
+
* @param {Array} toolCalls - 工具调用数组
|
|
104
|
+
* @returns {Promise<Array>} - 所有工具的执行结果
|
|
105
|
+
*/
|
|
106
|
+
async executeToolCalls(toolCalls) {
|
|
107
|
+
if (!Array.isArray(toolCalls)) {
|
|
108
|
+
throw new Error('toolCalls必须是数组');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const results = [];
|
|
112
|
+
|
|
113
|
+
// 并行执行所有工具调用
|
|
114
|
+
const promises = toolCalls.map(async (toolCall) => {
|
|
115
|
+
try {
|
|
116
|
+
const result = await this.executeToolCall(toolCall);
|
|
117
|
+
return result;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
// 某个工具失败,返回错误信息
|
|
120
|
+
return {
|
|
121
|
+
tool_call_id: toolCall.id,
|
|
122
|
+
name: toolCall.function.name,
|
|
123
|
+
error: error.message,
|
|
124
|
+
success: false
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// 等待所有工具执行完成
|
|
130
|
+
const settledResults = await Promise.allSettled(promises);
|
|
131
|
+
|
|
132
|
+
// 处理结果
|
|
133
|
+
for (const settled of settledResults) {
|
|
134
|
+
if (settled.status === 'fulfilled') {
|
|
135
|
+
results.push(settled.value);
|
|
136
|
+
} else {
|
|
137
|
+
// 理论上不会到这里,因为错误已经在上面捕获了
|
|
138
|
+
results.push({
|
|
139
|
+
error: settled.reason.message,
|
|
140
|
+
success: false
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return results;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 串行执行工具调用(如果需要顺序执行)
|
|
150
|
+
*/
|
|
151
|
+
async executeToolCallsSequentially(toolCalls) {
|
|
152
|
+
const results = [];
|
|
153
|
+
|
|
154
|
+
for (const toolCall of toolCalls) {
|
|
155
|
+
try {
|
|
156
|
+
const result = await this.executeToolCall(toolCall);
|
|
157
|
+
results.push(result);
|
|
158
|
+
} catch (error) {
|
|
159
|
+
// 某个工具失败,可以选择停止或继续
|
|
160
|
+
results.push({
|
|
161
|
+
tool_call_id: toolCall.id,
|
|
162
|
+
name: toolCall.function.name,
|
|
163
|
+
error: error.message,
|
|
164
|
+
success: false
|
|
165
|
+
});
|
|
166
|
+
// 可以选择继续执行下一个工具
|
|
167
|
+
// 或者抛出错误停止执行:throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return results;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export default ToolManager
|
package/src/index.js
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
// 这是 AI 聊天框架的入口文件
|
|
2
|
+
// 导出所有核心组件供外部使用
|
|
3
|
+
|
|
4
|
+
import Messages from './core/Messages.js';
|
|
5
|
+
import RequestBuilder from './core/RequestBuilder.js';
|
|
6
|
+
import ApiClient from './core/ApiClient.js';
|
|
7
|
+
import EventEmitter from './core/EventEmitter.js';
|
|
8
|
+
import MessageFormatter from './core/MessageFormatter.js';
|
|
9
|
+
import ToolManager from './core/ToolManager.js'; // 新增导入
|
|
10
|
+
|
|
11
|
+
// 导出所有核心类
|
|
12
|
+
export { Messages, RequestBuilder, ApiClient, EventEmitter, MessageFormatter, ToolManager };
|
|
13
|
+
|
|
14
|
+
// ChatService类,继承EventEmitter
|
|
15
|
+
class ChatService extends EventEmitter {
|
|
16
|
+
constructor(apiKey, model = 'deepseek-chat', config = {}) {
|
|
17
|
+
super();
|
|
18
|
+
|
|
19
|
+
this.messages = new Messages();
|
|
20
|
+
this.apiClient = new ApiClient(apiKey);
|
|
21
|
+
this.requestBuilder = new RequestBuilder(model, this.messages, config);
|
|
22
|
+
this.toolManager = new ToolManager(); // 新增:工具管理器
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
this._setupDefaultListeners();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_setupDefaultListeners() {
|
|
29
|
+
// 监听API客户端的事件
|
|
30
|
+
this.apiClient.on('request-start', (data) => {
|
|
31
|
+
this.emit('request-start', data);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
this.apiClient.on('request-success', (data) => {
|
|
35
|
+
this.emit('request-success', data);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
this.apiClient.on('request-error', (data) => {
|
|
39
|
+
this.emit('request-error', data);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
this.apiClient.on('stream-chunk', (data) => {
|
|
43
|
+
this.emit('stream-chunk', data);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// 监听工具管理器的事件
|
|
47
|
+
this.toolManager.on('tool-execute-start', (data) => {
|
|
48
|
+
this.emit('tool-execute-start', data);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
this.toolManager.on('tool-execute-success', (data) => {
|
|
52
|
+
this.emit('tool-execute-success', data);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
this.toolManager.on('tool-execute-error', (data) => {
|
|
56
|
+
this.emit('tool-execute-error', data);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 注册工具
|
|
62
|
+
*/
|
|
63
|
+
registerTool(name, definition, executor) {
|
|
64
|
+
// 1. 在ToolManager中注册
|
|
65
|
+
this.toolManager.registerTool(name, definition, executor);
|
|
66
|
+
|
|
67
|
+
// 2. 在Messages中添加工具定义(用于发送给AI)
|
|
68
|
+
this.messages.addTool(definition);
|
|
69
|
+
|
|
70
|
+
return this; // 支持链式调用
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 发送消息(支持工具调用)
|
|
75
|
+
*/
|
|
76
|
+
async send(userMessage, options = {}) {
|
|
77
|
+
try {
|
|
78
|
+
// 触发事件:开始发送
|
|
79
|
+
this.emit('sending', {
|
|
80
|
+
role: 'user',
|
|
81
|
+
content: userMessage,
|
|
82
|
+
timestamp: new Date()
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// 添加用户消息
|
|
86
|
+
this.messages.addUserMessage(userMessage);
|
|
87
|
+
|
|
88
|
+
// 构建请求体(会自动包含工具定义)
|
|
89
|
+
// 注意:这里需要确保RequestBuilder的配置正确
|
|
90
|
+
const requestBody = this.requestBuilder.toJSON(
|
|
91
|
+
options.baseRounds,
|
|
92
|
+
options.cycleRounds
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
// 发送请求
|
|
96
|
+
const response = await this.apiClient.send(requestBody);
|
|
97
|
+
const aiMessage = response.choices[0].message;
|
|
98
|
+
|
|
99
|
+
// 检查是否有工具调用
|
|
100
|
+
if (aiMessage.tool_calls && aiMessage.tool_calls.length > 0) {
|
|
101
|
+
// 处理工具调用
|
|
102
|
+
return await this._handleToolCalls(aiMessage);
|
|
103
|
+
} else {
|
|
104
|
+
// 普通回复
|
|
105
|
+
return await this._handleNormalResponse(aiMessage);
|
|
106
|
+
}
|
|
107
|
+
} catch (error) {
|
|
108
|
+
this.emit('error', {
|
|
109
|
+
error: error,
|
|
110
|
+
message: userMessage,
|
|
111
|
+
timestamp: new Date()
|
|
112
|
+
});
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 处理工具调用(支持循环调用)
|
|
119
|
+
*/
|
|
120
|
+
async _handleToolCalls(aiMessage, maxIterations = 5) {
|
|
121
|
+
console.log('🔍 处理工具调用,aiMessage:', aiMessage);
|
|
122
|
+
|
|
123
|
+
let iteration = 0;
|
|
124
|
+
let currentMessage = aiMessage;
|
|
125
|
+
|
|
126
|
+
// 循环处理工具调用,直到没有工具调用或达到最大迭代次数
|
|
127
|
+
while (iteration < maxIterations) {
|
|
128
|
+
iteration++;
|
|
129
|
+
console.log(`🔄 工具调用迭代 ${iteration}/${maxIterations}`);
|
|
130
|
+
|
|
131
|
+
// 1. 添加助手消息(包含工具调用和思考内容)
|
|
132
|
+
this.messages.addAssistantMessage(currentMessage.content || '', {
|
|
133
|
+
tool_calls: currentMessage.tool_calls,
|
|
134
|
+
reasoning_content: currentMessage.reasoning_content || '' // ✅ 新增:保存思考内容
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// 触发事件:AI请求工具调用
|
|
138
|
+
this.emit('tool-call-requested', {
|
|
139
|
+
tool_calls: currentMessage.tool_calls,
|
|
140
|
+
reasoning_content: currentMessage.reasoning_content, // ✅ 新增:包含思考内容
|
|
141
|
+
timestamp: new Date(),
|
|
142
|
+
iteration: iteration
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// 2. 执行工具
|
|
146
|
+
const toolResults = await this.toolManager.executeToolCalls(currentMessage.tool_calls);
|
|
147
|
+
console.log('🔍 工具执行结果:', toolResults);
|
|
148
|
+
|
|
149
|
+
// 3. 添加工具结果消息
|
|
150
|
+
for (const result of toolResults) {
|
|
151
|
+
console.log('🔍 添加工具消息,result:', result);
|
|
152
|
+
this.messages.addToolMessage(
|
|
153
|
+
result.result || result.error || '',
|
|
154
|
+
result.tool_call_id
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 4. 检查当前消息状态
|
|
159
|
+
console.log('🔍 当前所有消息:', this.messages.getMessages());
|
|
160
|
+
|
|
161
|
+
// 5. 再次发送请求,获取AI对工具结果的响应
|
|
162
|
+
const requestBody = this.requestBuilder.toJSON();
|
|
163
|
+
console.log('🔍 第', iteration, '次请求体:', requestBody);
|
|
164
|
+
|
|
165
|
+
const response = await this.apiClient.send(requestBody);
|
|
166
|
+
currentMessage = response.choices[0].message;
|
|
167
|
+
|
|
168
|
+
// 6. 检查AI是否还有新的工具调用请求
|
|
169
|
+
if (!currentMessage.tool_calls || currentMessage.tool_calls.length === 0) {
|
|
170
|
+
// 没有更多工具调用,结束循环
|
|
171
|
+
console.log('✅ 工具调用完成,AI给出最终回复');
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
console.log('🔄 AI请求了新的工具调用,继续处理...');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 7. 添加最终助手消息
|
|
179
|
+
this.messages.addAssistantMessage(currentMessage.content || '', {
|
|
180
|
+
reasoning_content: currentMessage.reasoning_content || '' // ✅ 新增:保存最终思考内容
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// 触发事件:收到最终回复
|
|
184
|
+
this.emit('message', {
|
|
185
|
+
role: 'assistant',
|
|
186
|
+
content: currentMessage.content,
|
|
187
|
+
reasoning_content: currentMessage.reasoning_content, // ✅ 新增:包含思考内容
|
|
188
|
+
timestamp: new Date(),
|
|
189
|
+
response: { choices: [{ message: currentMessage }] },
|
|
190
|
+
toolIterations: iteration
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
return currentMessage.content;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 处理普通回复
|
|
198
|
+
*/
|
|
199
|
+
async _handleNormalResponse(aiMessage) {
|
|
200
|
+
// 添加助手消息,处理空内容
|
|
201
|
+
this.messages.addAssistantMessage(aiMessage.content || '', {
|
|
202
|
+
reasoning_content: aiMessage.reasoning_content
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// 触发事件:收到回复
|
|
206
|
+
this.emit('message', {
|
|
207
|
+
role: 'assistant',
|
|
208
|
+
content: aiMessage.content,
|
|
209
|
+
timestamp: new Date(),
|
|
210
|
+
response: { choices: [{ message: aiMessage }] }
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
return aiMessage.content;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 流式发送消息(支持工具调用)
|
|
218
|
+
*/
|
|
219
|
+
async stream(userMessage, onProgress, onDone, options = {}) {
|
|
220
|
+
try {
|
|
221
|
+
// 触发事件:开始发送
|
|
222
|
+
this.emit('sending', {
|
|
223
|
+
role: 'user',
|
|
224
|
+
content: userMessage,
|
|
225
|
+
timestamp: new Date()
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// 添加用户消息
|
|
229
|
+
this.messages.addUserMessage(userMessage);
|
|
230
|
+
|
|
231
|
+
// 构建请求体 - 关键:设置stream: true
|
|
232
|
+
const requestBuilder = new RequestBuilder(this.requestBuilder.model, this.messages, {
|
|
233
|
+
...this.requestBuilder.config,
|
|
234
|
+
stream: true // ✅ 必须设置为true!
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const requestBody = requestBuilder.toJSON(
|
|
238
|
+
options.baseRounds,
|
|
239
|
+
options.cycleRounds
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
// 流式发送
|
|
243
|
+
const response = await this.apiClient.strSend(
|
|
244
|
+
requestBody,
|
|
245
|
+
(chunk) => {
|
|
246
|
+
// 流式进度回调
|
|
247
|
+
this.emit('stream-progress', chunk);
|
|
248
|
+
onProgress(chunk);
|
|
249
|
+
},
|
|
250
|
+
async (finalMessage) => {
|
|
251
|
+
// 流式完成回调
|
|
252
|
+
if (finalMessage.tool_calls && finalMessage.tool_calls.length > 0) {
|
|
253
|
+
// 处理流式中的工具调用
|
|
254
|
+
await this._handleStreamToolCalls(finalMessage, onProgress, onDone);
|
|
255
|
+
} else {
|
|
256
|
+
// 普通流式回复
|
|
257
|
+
this.messages.addAssistantMessage(finalMessage.content || '', {
|
|
258
|
+
reasoning_content: finalMessage.reasoning_content
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
this.emit('stream-done', { message: finalMessage });
|
|
262
|
+
onDone(finalMessage);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
return response;
|
|
268
|
+
} catch (error) {
|
|
269
|
+
this.emit('error', {
|
|
270
|
+
error: error,
|
|
271
|
+
message: userMessage,
|
|
272
|
+
timestamp: new Date()
|
|
273
|
+
});
|
|
274
|
+
throw error;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 处理流式传输中的工具调用(支持循环调用)
|
|
280
|
+
*/
|
|
281
|
+
async _handleStreamToolCalls(aiMessage, onProgress, onDone, maxIterations = 5) {
|
|
282
|
+
let iteration = 0;
|
|
283
|
+
let currentMessage = aiMessage;
|
|
284
|
+
|
|
285
|
+
// 循环处理工具调用
|
|
286
|
+
while (iteration < maxIterations) {
|
|
287
|
+
iteration++;
|
|
288
|
+
|
|
289
|
+
// 1. 添加助手消息(包含工具调用和思考内容)
|
|
290
|
+
this.messages.addAssistantMessage(currentMessage.content || '', {
|
|
291
|
+
tool_calls: currentMessage.tool_calls,
|
|
292
|
+
reasoning_content: currentMessage.reasoning_content || '' // ✅ 新增:保存思考内容
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// 触发事件:AI请求工具调用
|
|
296
|
+
this.emit('tool-call-requested', {
|
|
297
|
+
tool_calls: currentMessage.tool_calls,
|
|
298
|
+
reasoning_content: currentMessage.reasoning_content, // ✅ 新增:包含思考内容
|
|
299
|
+
timestamp: new Date(),
|
|
300
|
+
iteration: iteration
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// 2. 执行工具
|
|
304
|
+
const toolResults = await this.toolManager.executeToolCalls(currentMessage.tool_calls);
|
|
305
|
+
|
|
306
|
+
// 3. 添加工具结果消息
|
|
307
|
+
for (const result of toolResults) {
|
|
308
|
+
this.messages.addToolMessage(result.result || result.error, result.tool_call_id);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 4. 再次发送流式请求(包含工具结果)
|
|
312
|
+
const requestBody = this.requestBuilder.toJSON();
|
|
313
|
+
|
|
314
|
+
// 使用Promise包装流式请求,等待完成
|
|
315
|
+
await new Promise((resolve, reject) => {
|
|
316
|
+
this.apiClient.strSend(
|
|
317
|
+
requestBody,
|
|
318
|
+
(chunk) => {
|
|
319
|
+
this.emit('stream-progress', chunk);
|
|
320
|
+
onProgress(chunk);
|
|
321
|
+
},
|
|
322
|
+
(finalMessage) => {
|
|
323
|
+
currentMessage = finalMessage;
|
|
324
|
+
|
|
325
|
+
// 检查是否还有工具调用
|
|
326
|
+
if (!currentMessage.tool_calls || currentMessage.tool_calls.length === 0) {
|
|
327
|
+
// 没有更多工具调用,结束循环
|
|
328
|
+
resolve();
|
|
329
|
+
} else {
|
|
330
|
+
// 还有工具调用,继续循环
|
|
331
|
+
resolve();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
).catch(reject);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
// 如果没有工具调用了,跳出循环
|
|
338
|
+
if (!currentMessage.tool_calls || currentMessage.tool_calls.length === 0) {
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// 添加最终助手消息
|
|
344
|
+
this.messages.addAssistantMessage(currentMessage.content, {
|
|
345
|
+
reasoning_content: currentMessage.reasoning_content || '' // ✅ 新增:保存最终思考内容
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
this.emit('stream-done', {
|
|
349
|
+
message: currentMessage,
|
|
350
|
+
reasoning_content: currentMessage.reasoning_content, // ✅ 新增:包含思考内容
|
|
351
|
+
toolIterations: iteration
|
|
352
|
+
});
|
|
353
|
+
onDone(currentMessage);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 撤回消息
|
|
358
|
+
*/
|
|
359
|
+
undo() {
|
|
360
|
+
const removed = this.messages.undoToAssistant();
|
|
361
|
+
if (removed.length > 0) {
|
|
362
|
+
this.emit('undo', {
|
|
363
|
+
removedMessages: removed,
|
|
364
|
+
timestamp: new Date()
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
return removed;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* 清空消息
|
|
372
|
+
*/
|
|
373
|
+
clear() {
|
|
374
|
+
const removed = this.messages.clearMessages();
|
|
375
|
+
this.emit('clear', {
|
|
376
|
+
removedMessages: removed,
|
|
377
|
+
timestamp: new Date()
|
|
378
|
+
});
|
|
379
|
+
return removed;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* 导出对话数据
|
|
384
|
+
*/
|
|
385
|
+
export() {
|
|
386
|
+
const data = this.messages.export();
|
|
387
|
+
this.emit('export', {
|
|
388
|
+
data: data,
|
|
389
|
+
timestamp: new Date()
|
|
390
|
+
});
|
|
391
|
+
return data;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* 导入对话数据
|
|
396
|
+
*/
|
|
397
|
+
import(data) {
|
|
398
|
+
this.messages.import(data);
|
|
399
|
+
this.emit('import', {
|
|
400
|
+
data: data,
|
|
401
|
+
timestamp: new Date()
|
|
402
|
+
});
|
|
403
|
+
return this;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// 导出 ChatService
|
|
408
|
+
export { ChatService };
|
|
409
|
+
|
|
410
|
+
// 导出默认对象
|
|
411
|
+
export default {
|
|
412
|
+
Messages,
|
|
413
|
+
RequestBuilder,
|
|
414
|
+
ApiClient,
|
|
415
|
+
EventEmitter,
|
|
416
|
+
MessageFormatter,
|
|
417
|
+
ToolManager,
|
|
418
|
+
ChatService
|
|
419
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// This file contains utility functions for the AI chat framework.
|
|
2
|
+
// You can add common functions such as data formatting, error handling, and other helper methods here.
|
|
3
|
+
|
|
4
|
+
export const formatDate = (date) => {
|
|
5
|
+
if (!(date instanceof Date)) {
|
|
6
|
+
throw new Error('Invalid date object');
|
|
7
|
+
}
|
|
8
|
+
return date.toISOString();
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const handleError = (error) => {
|
|
12
|
+
console.error('An error occurred:', error);
|
|
13
|
+
return {
|
|
14
|
+
message: error.message || 'An unknown error occurred',
|
|
15
|
+
stack: error.stack || null,
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const isEmpty = (value) => {
|
|
20
|
+
return value === null || value === undefined || value.trim() === '';
|
|
21
|
+
};
|