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/src/core/ToolManager.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
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/utils/index.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
};
|