@zhin.js/ai 0.0.1 → 1.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/LICENSE +21 -0
- package/lib/agent.d.ts +84 -0
- package/lib/agent.d.ts.map +1 -0
- package/lib/agent.js +384 -0
- package/lib/agent.js.map +1 -0
- package/lib/context-manager.d.ts +213 -0
- package/lib/context-manager.d.ts.map +1 -0
- package/lib/context-manager.js +306 -0
- package/lib/context-manager.js.map +1 -0
- package/lib/index.d.ts +167 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +1126 -0
- package/lib/index.js.map +1 -0
- package/lib/providers/anthropic.d.ts +23 -0
- package/lib/providers/anthropic.d.ts.map +1 -0
- package/lib/providers/anthropic.js +322 -0
- package/lib/providers/anthropic.js.map +1 -0
- package/lib/providers/base.d.ts +43 -0
- package/lib/providers/base.d.ts.map +1 -0
- package/lib/providers/base.js +135 -0
- package/lib/providers/base.js.map +1 -0
- package/lib/providers/index.d.ts +12 -0
- package/lib/providers/index.d.ts.map +1 -0
- package/lib/providers/index.js +9 -0
- package/lib/providers/index.js.map +1 -0
- package/lib/providers/ollama.d.ts +25 -0
- package/lib/providers/ollama.d.ts.map +1 -0
- package/lib/providers/ollama.js +236 -0
- package/lib/providers/ollama.js.map +1 -0
- package/lib/providers/openai.d.ts +46 -0
- package/lib/providers/openai.d.ts.map +1 -0
- package/lib/providers/openai.js +132 -0
- package/lib/providers/openai.js.map +1 -0
- package/lib/session.d.ts +186 -0
- package/lib/session.d.ts.map +1 -0
- package/lib/session.js +436 -0
- package/lib/session.js.map +1 -0
- package/lib/tools.d.ts +45 -0
- package/lib/tools.d.ts.map +1 -0
- package/lib/tools.js +194 -0
- package/lib/tools.js.map +1 -0
- package/lib/types.d.ts +243 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +6 -0
- package/lib/types.js.map +1 -0
- package/package.json +10 -10
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 凉菜
|
|
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/lib/agent.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zhin.js/ai - Agent System
|
|
3
|
+
* AI Agent 实现,支持工具调用和多轮对话
|
|
4
|
+
*/
|
|
5
|
+
import type { AIProvider, AgentConfig, AgentTool, AgentResult, ChatMessage, Usage } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Agent 执行状态
|
|
8
|
+
*/
|
|
9
|
+
export interface AgentState {
|
|
10
|
+
messages: ChatMessage[];
|
|
11
|
+
toolCalls: {
|
|
12
|
+
tool: string;
|
|
13
|
+
args: Record<string, any>;
|
|
14
|
+
result: any;
|
|
15
|
+
}[];
|
|
16
|
+
usage: Usage;
|
|
17
|
+
iterations: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Agent 事件
|
|
21
|
+
*/
|
|
22
|
+
export interface AgentEvents {
|
|
23
|
+
'thinking': (message: string) => void;
|
|
24
|
+
'tool_call': (tool: string, args: Record<string, any>) => void;
|
|
25
|
+
'tool_result': (tool: string, result: any) => void;
|
|
26
|
+
'streaming': (content: string) => void;
|
|
27
|
+
'complete': (result: AgentResult) => void;
|
|
28
|
+
'error': (error: Error) => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* AI Agent 类
|
|
32
|
+
* 支持工具调用、多轮对话、流式输出
|
|
33
|
+
*/
|
|
34
|
+
export declare class Agent {
|
|
35
|
+
private provider;
|
|
36
|
+
private config;
|
|
37
|
+
private tools;
|
|
38
|
+
private eventHandlers;
|
|
39
|
+
constructor(provider: AIProvider, config: AgentConfig);
|
|
40
|
+
/**
|
|
41
|
+
* 默认系统提示词
|
|
42
|
+
*/
|
|
43
|
+
private getDefaultSystemPrompt;
|
|
44
|
+
/**
|
|
45
|
+
* 注册事件处理器
|
|
46
|
+
*/
|
|
47
|
+
on<K extends keyof AgentEvents>(event: K, handler: AgentEvents[K]): () => void;
|
|
48
|
+
/**
|
|
49
|
+
* 触发事件
|
|
50
|
+
*/
|
|
51
|
+
private emit;
|
|
52
|
+
/**
|
|
53
|
+
* 添加工具
|
|
54
|
+
*/
|
|
55
|
+
addTool(tool: AgentTool): void;
|
|
56
|
+
/**
|
|
57
|
+
* 移除工具
|
|
58
|
+
*/
|
|
59
|
+
removeTool(name: string): void;
|
|
60
|
+
/**
|
|
61
|
+
* 获取工具定义
|
|
62
|
+
*/
|
|
63
|
+
private getToolDefinitions;
|
|
64
|
+
/**
|
|
65
|
+
* 执行工具调用
|
|
66
|
+
*/
|
|
67
|
+
private executeToolCall;
|
|
68
|
+
/**
|
|
69
|
+
* 运行 Agent
|
|
70
|
+
*/
|
|
71
|
+
run(userMessage: string, context?: ChatMessage[]): Promise<AgentResult>;
|
|
72
|
+
/**
|
|
73
|
+
* 流式运行 Agent
|
|
74
|
+
*/
|
|
75
|
+
runStream(userMessage: string, context?: ChatMessage[]): AsyncIterable<{
|
|
76
|
+
type: 'content' | 'tool_call' | 'tool_result' | 'done';
|
|
77
|
+
data: any;
|
|
78
|
+
}>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 创建 Agent 实例
|
|
82
|
+
*/
|
|
83
|
+
export declare function createAgent(provider: AIProvider, config: Omit<AgentConfig, 'provider'>): Agent;
|
|
84
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EACV,UAAU,EACV,WAAW,EACX,SAAS,EACT,WAAW,EACX,WAAW,EAGX,KAAK,EACN,MAAM,YAAY,CAAC;AAIpB;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,SAAS,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAAC,MAAM,EAAE,GAAG,CAAA;KAAE,EAAE,CAAC;IACtE,KAAK,EAAE,KAAK,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACtC,WAAW,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC;IAC/D,aAAa,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAA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E,MAAM,EAAE,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,GAAG,KAAK,CAK9F"}
|
package/lib/agent.js
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zhin.js/ai - Agent System
|
|
3
|
+
* AI Agent 实现,支持工具调用和多轮对话
|
|
4
|
+
*/
|
|
5
|
+
import { Logger } from '@zhin.js/core';
|
|
6
|
+
const logger = new Logger(null, 'Agent');
|
|
7
|
+
/**
|
|
8
|
+
* AI Agent 类
|
|
9
|
+
* 支持工具调用、多轮对话、流式输出
|
|
10
|
+
*/
|
|
11
|
+
export class Agent {
|
|
12
|
+
provider;
|
|
13
|
+
config;
|
|
14
|
+
tools = new Map();
|
|
15
|
+
eventHandlers = new Map();
|
|
16
|
+
constructor(provider, config) {
|
|
17
|
+
this.provider = provider;
|
|
18
|
+
this.config = {
|
|
19
|
+
provider: config.provider,
|
|
20
|
+
model: config.model || provider.models[0],
|
|
21
|
+
systemPrompt: config.systemPrompt || this.getDefaultSystemPrompt(),
|
|
22
|
+
tools: config.tools || [],
|
|
23
|
+
maxIterations: config.maxIterations || 10,
|
|
24
|
+
temperature: config.temperature ?? 0.7,
|
|
25
|
+
};
|
|
26
|
+
// 注册工具
|
|
27
|
+
for (const tool of this.config.tools) {
|
|
28
|
+
this.tools.set(tool.name, tool);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 默认系统提示词
|
|
33
|
+
*/
|
|
34
|
+
getDefaultSystemPrompt() {
|
|
35
|
+
return `你是一个智能助手,可以使用工具来帮助用户完成任务。
|
|
36
|
+
请遵循以下原则:
|
|
37
|
+
1. 理解用户的意图,选择合适的工具
|
|
38
|
+
2. 如果需要多个步骤,逐步执行
|
|
39
|
+
3. 清晰地解释你的行动和结果
|
|
40
|
+
4. 如果无法完成任务,诚实地告知用户`;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 注册事件处理器
|
|
44
|
+
*/
|
|
45
|
+
on(event, handler) {
|
|
46
|
+
const handlers = this.eventHandlers.get(event) || [];
|
|
47
|
+
handlers.push(handler);
|
|
48
|
+
this.eventHandlers.set(event, handlers);
|
|
49
|
+
return () => {
|
|
50
|
+
const idx = handlers.indexOf(handler);
|
|
51
|
+
if (idx !== -1)
|
|
52
|
+
handlers.splice(idx, 1);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 触发事件
|
|
57
|
+
*/
|
|
58
|
+
emit(event, ...args) {
|
|
59
|
+
const handlers = this.eventHandlers.get(event) || [];
|
|
60
|
+
for (const handler of handlers) {
|
|
61
|
+
try {
|
|
62
|
+
handler(...args);
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
logger.error('事件处理器错误:', e);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 添加工具
|
|
71
|
+
*/
|
|
72
|
+
addTool(tool) {
|
|
73
|
+
this.tools.set(tool.name, tool);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 移除工具
|
|
77
|
+
*/
|
|
78
|
+
removeTool(name) {
|
|
79
|
+
this.tools.delete(name);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 获取工具定义
|
|
83
|
+
*/
|
|
84
|
+
getToolDefinitions() {
|
|
85
|
+
return Array.from(this.tools.values()).map(tool => ({
|
|
86
|
+
type: 'function',
|
|
87
|
+
function: {
|
|
88
|
+
name: tool.name,
|
|
89
|
+
description: tool.description,
|
|
90
|
+
parameters: tool.parameters,
|
|
91
|
+
},
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 执行工具调用
|
|
96
|
+
*/
|
|
97
|
+
async executeToolCall(toolCall) {
|
|
98
|
+
const tool = this.tools.get(toolCall.function.name);
|
|
99
|
+
if (!tool) {
|
|
100
|
+
return JSON.stringify({ error: `Unknown tool: ${toolCall.function.name}` });
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
const args = JSON.parse(toolCall.function.arguments);
|
|
104
|
+
this.emit('tool_call', tool.name, args);
|
|
105
|
+
const result = await tool.execute(args);
|
|
106
|
+
this.emit('tool_result', tool.name, result);
|
|
107
|
+
return typeof result === 'string' ? result : JSON.stringify(result);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
111
|
+
return JSON.stringify({ error: errorMsg });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 运行 Agent
|
|
116
|
+
*/
|
|
117
|
+
async run(userMessage, context) {
|
|
118
|
+
const state = {
|
|
119
|
+
messages: [
|
|
120
|
+
{ role: 'system', content: this.config.systemPrompt },
|
|
121
|
+
...(context || []),
|
|
122
|
+
{ role: 'user', content: userMessage },
|
|
123
|
+
],
|
|
124
|
+
toolCalls: [],
|
|
125
|
+
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
|
|
126
|
+
iterations: 0,
|
|
127
|
+
};
|
|
128
|
+
const toolDefinitions = this.getToolDefinitions();
|
|
129
|
+
while (state.iterations < this.config.maxIterations) {
|
|
130
|
+
state.iterations++;
|
|
131
|
+
try {
|
|
132
|
+
const response = await this.provider.chat({
|
|
133
|
+
model: this.config.model,
|
|
134
|
+
messages: state.messages,
|
|
135
|
+
tools: toolDefinitions.length > 0 ? toolDefinitions : undefined,
|
|
136
|
+
tool_choice: toolDefinitions.length > 0 ? 'auto' : undefined,
|
|
137
|
+
temperature: this.config.temperature,
|
|
138
|
+
});
|
|
139
|
+
// 更新用量
|
|
140
|
+
if (response.usage) {
|
|
141
|
+
state.usage.prompt_tokens += response.usage.prompt_tokens;
|
|
142
|
+
state.usage.completion_tokens += response.usage.completion_tokens;
|
|
143
|
+
state.usage.total_tokens += response.usage.total_tokens;
|
|
144
|
+
}
|
|
145
|
+
const choice = response.choices[0];
|
|
146
|
+
if (!choice)
|
|
147
|
+
break;
|
|
148
|
+
// 如果包含工具调用,不立即将模型原始内容暴露给上层
|
|
149
|
+
if (choice.message.tool_calls?.length) {
|
|
150
|
+
this.emit('thinking', '正在执行工具调用...');
|
|
151
|
+
// 将 assistant 消息加入会话上下文,但避免直接展示纯 JSON 的工具调用原始内容
|
|
152
|
+
let assistantContent = '';
|
|
153
|
+
if (typeof choice.message.content === 'string' && choice.message.content) {
|
|
154
|
+
const rawContent = choice.message.content;
|
|
155
|
+
const trimmed = rawContent.trim();
|
|
156
|
+
// 如果内容整体看起来是 JSON(常见于模型将工具调用以 JSON 形式返回),则不暴露给上层
|
|
157
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
158
|
+
try {
|
|
159
|
+
JSON.parse(trimmed);
|
|
160
|
+
// 解析成功,说明是纯 JSON;保持 assistantContent 为空字符串
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
// 解析失败,当作普通文本保留
|
|
164
|
+
assistantContent = rawContent;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
// 非纯 JSON 内容,直接保留
|
|
169
|
+
assistantContent = rawContent;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
state.messages.push({
|
|
173
|
+
role: 'assistant',
|
|
174
|
+
content: assistantContent,
|
|
175
|
+
tool_calls: choice.message.tool_calls,
|
|
176
|
+
});
|
|
177
|
+
// 检测重复工具调用
|
|
178
|
+
let hasDuplicateCall = false;
|
|
179
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
180
|
+
const toolName = toolCall.function.name;
|
|
181
|
+
const toolArgs = toolCall.function.arguments;
|
|
182
|
+
// 检查是否重复调用相同工具(相同名称和参数)
|
|
183
|
+
const isDuplicate = state.toolCalls.some(tc => tc.tool === toolName && JSON.stringify(tc.args) === toolArgs);
|
|
184
|
+
if (isDuplicate) {
|
|
185
|
+
logger.debug(`重复工具调用: ${toolName},跳过`);
|
|
186
|
+
hasDuplicateCall = true;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const result = await this.executeToolCall(toolCall);
|
|
190
|
+
// 尝试解析 JSON,如果失败则使用原始字符串
|
|
191
|
+
let parsedResult;
|
|
192
|
+
try {
|
|
193
|
+
parsedResult = JSON.parse(result);
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
parsedResult = result;
|
|
197
|
+
}
|
|
198
|
+
state.toolCalls.push({
|
|
199
|
+
tool: toolName,
|
|
200
|
+
args: JSON.parse(toolArgs),
|
|
201
|
+
result: parsedResult,
|
|
202
|
+
});
|
|
203
|
+
// 添加工具结果消息(供模型下一步使用)
|
|
204
|
+
state.messages.push({
|
|
205
|
+
role: 'tool',
|
|
206
|
+
content: result,
|
|
207
|
+
tool_call_id: toolCall.id,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
// 如果所有调用都是重复的,强制模型生成最终回答
|
|
211
|
+
if (hasDuplicateCall && choice.message.tool_calls.every(tc => state.toolCalls.some(stc => stc.tool === tc.function.name && JSON.stringify(stc.args) === tc.function.arguments))) {
|
|
212
|
+
// 添加提示让模型总结结果
|
|
213
|
+
state.messages.push({
|
|
214
|
+
role: 'user',
|
|
215
|
+
content: '请根据已获取的工具结果,用中文总结回答我的问题。',
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
// 继续循环,让模型处理工具结果并生成最终回答
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
// 没有工具调用,返回结果
|
|
222
|
+
let content = typeof choice.message.content === 'string'
|
|
223
|
+
? choice.message.content
|
|
224
|
+
: '';
|
|
225
|
+
// 如果内容为空但有工具调用结果,生成基于工具结果的回复
|
|
226
|
+
if (!content.trim() && state.toolCalls.length > 0) {
|
|
227
|
+
const lastToolCall = state.toolCalls[state.toolCalls.length - 1];
|
|
228
|
+
const resultStr = typeof lastToolCall.result === 'string'
|
|
229
|
+
? lastToolCall.result
|
|
230
|
+
: JSON.stringify(lastToolCall.result, null, 2);
|
|
231
|
+
content = `根据查询结果:\n\n${resultStr}`;
|
|
232
|
+
}
|
|
233
|
+
const result = {
|
|
234
|
+
content,
|
|
235
|
+
toolCalls: state.toolCalls,
|
|
236
|
+
usage: state.usage,
|
|
237
|
+
iterations: state.iterations,
|
|
238
|
+
};
|
|
239
|
+
this.emit('complete', result);
|
|
240
|
+
return result;
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
244
|
+
this.emit('error', err);
|
|
245
|
+
throw err;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// 达到最大迭代次数
|
|
249
|
+
const result = {
|
|
250
|
+
content: '达到最大迭代次数,任务可能未完成。',
|
|
251
|
+
toolCalls: state.toolCalls,
|
|
252
|
+
usage: state.usage,
|
|
253
|
+
iterations: state.iterations,
|
|
254
|
+
};
|
|
255
|
+
this.emit('complete', result);
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* 流式运行 Agent
|
|
260
|
+
*/
|
|
261
|
+
async *runStream(userMessage, context) {
|
|
262
|
+
const messages = [
|
|
263
|
+
{ role: 'system', content: this.config.systemPrompt },
|
|
264
|
+
...(context || []),
|
|
265
|
+
{ role: 'user', content: userMessage },
|
|
266
|
+
];
|
|
267
|
+
const toolDefinitions = this.getToolDefinitions();
|
|
268
|
+
let iterations = 0;
|
|
269
|
+
const toolCallHistory = [];
|
|
270
|
+
const usage = { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 };
|
|
271
|
+
while (iterations < this.config.maxIterations) {
|
|
272
|
+
iterations++;
|
|
273
|
+
let content = '';
|
|
274
|
+
const pendingToolCalls = [];
|
|
275
|
+
let finishReason = null;
|
|
276
|
+
// 流式获取响应
|
|
277
|
+
for await (const chunk of this.provider.chatStream({
|
|
278
|
+
model: this.config.model,
|
|
279
|
+
messages,
|
|
280
|
+
tools: toolDefinitions.length > 0 ? toolDefinitions : undefined,
|
|
281
|
+
tool_choice: toolDefinitions.length > 0 ? 'auto' : undefined,
|
|
282
|
+
temperature: this.config.temperature,
|
|
283
|
+
})) {
|
|
284
|
+
const choice = chunk.choices[0];
|
|
285
|
+
if (!choice)
|
|
286
|
+
continue;
|
|
287
|
+
// 处理内容
|
|
288
|
+
if (choice.delta.content) {
|
|
289
|
+
content += choice.delta.content;
|
|
290
|
+
yield { type: 'content', data: choice.delta.content };
|
|
291
|
+
}
|
|
292
|
+
// 处理工具调用
|
|
293
|
+
if (choice.delta.tool_calls) {
|
|
294
|
+
for (const tc of choice.delta.tool_calls) {
|
|
295
|
+
// 合并工具调用片段
|
|
296
|
+
let existing = pendingToolCalls.find(p => p.id === tc.id);
|
|
297
|
+
if (!existing && tc.id) {
|
|
298
|
+
existing = {
|
|
299
|
+
id: tc.id,
|
|
300
|
+
type: 'function',
|
|
301
|
+
function: { name: '', arguments: '' },
|
|
302
|
+
};
|
|
303
|
+
pendingToolCalls.push(existing);
|
|
304
|
+
}
|
|
305
|
+
if (existing && tc.function) {
|
|
306
|
+
if (tc.function.name)
|
|
307
|
+
existing.function.name += tc.function.name;
|
|
308
|
+
if (tc.function.arguments)
|
|
309
|
+
existing.function.arguments += tc.function.arguments;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (choice.finish_reason) {
|
|
314
|
+
finishReason = choice.finish_reason;
|
|
315
|
+
}
|
|
316
|
+
if (chunk.usage) {
|
|
317
|
+
usage.prompt_tokens += chunk.usage.prompt_tokens;
|
|
318
|
+
usage.completion_tokens += chunk.usage.completion_tokens;
|
|
319
|
+
usage.total_tokens += chunk.usage.total_tokens;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
// 添加 assistant 消息。若存在 pendingToolCalls,则不要在流式期间把模型原始包含工具调用的内容直接发送给消费者。
|
|
323
|
+
const assistantMessage = {
|
|
324
|
+
role: 'assistant',
|
|
325
|
+
content,
|
|
326
|
+
tool_calls: pendingToolCalls.length > 0 ? pendingToolCalls : undefined,
|
|
327
|
+
};
|
|
328
|
+
messages.push(assistantMessage);
|
|
329
|
+
// 处理工具调用(流式模式)
|
|
330
|
+
if (pendingToolCalls.length > 0 && finishReason === 'tool_calls') {
|
|
331
|
+
for (const toolCall of pendingToolCalls) {
|
|
332
|
+
// 通知上层开始执行工具(上层可用于显示“正在执行工具”而非工具原始内容)
|
|
333
|
+
yield { type: 'tool_call', data: { name: toolCall.function.name, args: toolCall.function.arguments } };
|
|
334
|
+
const result = await this.executeToolCall(toolCall);
|
|
335
|
+
toolCallHistory.push({
|
|
336
|
+
tool: toolCall.function.name,
|
|
337
|
+
args: JSON.parse(toolCall.function.arguments),
|
|
338
|
+
result: JSON.parse(result),
|
|
339
|
+
});
|
|
340
|
+
// 返回工具结果(上层可选择把工具执行过程隐藏,仅在最终完成时展示润色结果)
|
|
341
|
+
yield { type: 'tool_result', data: { name: toolCall.function.name, result } };
|
|
342
|
+
messages.push({
|
|
343
|
+
role: 'tool',
|
|
344
|
+
content: result,
|
|
345
|
+
tool_call_id: toolCall.id,
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
// 继续循环,让模型以工具结果为上下文生成最终回答
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
// 完成
|
|
352
|
+
yield {
|
|
353
|
+
type: 'done',
|
|
354
|
+
data: {
|
|
355
|
+
content,
|
|
356
|
+
toolCalls: toolCallHistory,
|
|
357
|
+
usage,
|
|
358
|
+
iterations,
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
// 达到最大迭代次数
|
|
364
|
+
yield {
|
|
365
|
+
type: 'done',
|
|
366
|
+
data: {
|
|
367
|
+
content: '达到最大迭代次数',
|
|
368
|
+
toolCalls: toolCallHistory,
|
|
369
|
+
usage,
|
|
370
|
+
iterations,
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* 创建 Agent 实例
|
|
377
|
+
*/
|
|
378
|
+
export function createAgent(provider, config) {
|
|
379
|
+
return new Agent(provider, {
|
|
380
|
+
...config,
|
|
381
|
+
provider: provider.name,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
//# sourceMappingURL=agent.js.map
|
package/lib/agent.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAYvC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAwBzC;;;GAGG;AACH,MAAM,OAAO,KAAK;IACR,QAAQ,CAAa;IACrB,MAAM,CAAwB;IAC9B,KAAK,GAA2B,IAAI,GAAG,EAAE,CAAC;IAC1C,aAAa,GAAuC,IAAI,GAAG,EAAE,CAAC;IAEtE,YAAY,QAAoB,EAAE,MAAmB;QACnD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG;YACZ,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;YACzC,YAAY,EAAE,MAAM,CAAC,YAAY,IAAI,IAAI,CAAC,sBAAsB,EAAE;YAClE,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,EAAE;YACzB,aAAa,EAAE,MAAM,CAAC,aAAa,IAAI,EAAE;YACzC,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,GAAG;SACvC,CAAC;QAEF,OAAO;QACP,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;YACrC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IA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