@n0ts123/mcplink-core 0.0.12 → 0.0.14
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/README.md +490 -709
- package/dist/index.d.ts +295 -411
- package/dist/index.js +503 -1546
- package/dist/index.js.map +1 -1
- package/package.json +2 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,131 +1,36 @@
|
|
|
1
|
-
import { LanguageModel } from 'ai';
|
|
2
|
-
export { createOpenAI } from '@ai-sdk/openai';
|
|
3
|
-
export { createAnthropic } from '@ai-sdk/anthropic';
|
|
4
|
-
|
|
5
|
-
/** 文本内容部分 */
|
|
6
|
-
interface TextPart {
|
|
7
|
-
type: 'text';
|
|
8
|
-
text: string;
|
|
9
|
-
}
|
|
10
|
-
/** 图片内容部分 */
|
|
11
|
-
interface ImagePart {
|
|
12
|
-
type: 'image';
|
|
13
|
-
/** 图片 URL 或 base64 数据 */
|
|
14
|
-
image: string | URL;
|
|
15
|
-
/** 可选的 MIME 类型 */
|
|
16
|
-
mimeType?: string;
|
|
17
|
-
}
|
|
18
|
-
/** 文件内容部分 */
|
|
19
|
-
interface FilePart {
|
|
20
|
-
type: 'file';
|
|
21
|
-
/** 文件 URL 或 base64 数据 */
|
|
22
|
-
data: string | URL;
|
|
23
|
-
/** MIME 类型 */
|
|
24
|
-
mimeType: string;
|
|
25
|
-
}
|
|
26
|
-
/** 多模态消息内容部分 */
|
|
27
|
-
type MessageContentPart = TextPart | ImagePart | FilePart;
|
|
28
|
-
/** 用户消息类型 - 支持字符串或多模态数组 */
|
|
29
|
-
type UserMessage = string | MessageContentPart[];
|
|
30
|
-
/** MCP 服务器配置 - stdio 模式 */
|
|
31
|
-
interface MCPServerConfigStdio {
|
|
32
|
-
type?: 'stdio';
|
|
33
|
-
/** 启动命令 */
|
|
34
|
-
command: string;
|
|
35
|
-
/** 命令参数 */
|
|
36
|
-
args?: string[];
|
|
37
|
-
/** 环境变量 */
|
|
38
|
-
env?: Record<string, string>;
|
|
39
|
-
}
|
|
40
|
-
/** MCP 服务器配置 - Streamable HTTP 模式 */
|
|
41
|
-
interface MCPServerConfigStreamableHTTP {
|
|
42
|
-
type: 'streamable-http';
|
|
43
|
-
/** HTTP 服务地址 */
|
|
44
|
-
url: string;
|
|
45
|
-
/** 请求头 */
|
|
46
|
-
headers?: Record<string, string>;
|
|
47
|
-
}
|
|
48
|
-
/** MCP 服务器配置 */
|
|
49
|
-
type MCPServerConfig = MCPServerConfigStdio | MCPServerConfigStreamableHTTP;
|
|
50
1
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* 当工具返回的 JSON 包含匹配器中所有的 key-value 时,会立即发送 IMMEDIATE_RESULT 事件
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* // 匹配包含 { type: "card" } 的返回结果
|
|
57
|
-
* { type: "card" }
|
|
58
|
-
*
|
|
59
|
-
* // 匹配包含 { type: "product_list", isCard: true } 的返回结果
|
|
60
|
-
* { type: "product_list", isCard: true }
|
|
2
|
+
* MCPLink 核心类型定义
|
|
3
|
+
* 极简设计:只做 MCP + HTTP 桥接
|
|
61
4
|
*/
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
* 是否使用基于 Prompt 的工具调用
|
|
79
|
-
* - true: 使用 prompt 让模型输出特定格式来调用工具(支持所有模型)
|
|
80
|
-
* - false: 使用原生 function calling(需要模型支持)
|
|
81
|
-
* - 'auto': 自动检测(默认)
|
|
82
|
-
*/
|
|
83
|
-
usePromptBasedTools?: boolean | 'auto';
|
|
84
|
-
/**
|
|
85
|
-
* 即时结果匹配器列表
|
|
86
|
-
* 当 MCP 工具返回的结果匹配任意一个匹配器时,会立即发送 IMMEDIATE_RESULT 事件
|
|
87
|
-
* 匹配规则:结果 JSON 中包含匹配器的所有 key 且对应的 value 相等
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* immediateResultMatchers: [
|
|
91
|
-
* { type: "card" }, // 匹配 { type: "card", ... }
|
|
92
|
-
* { type: "product_list" }, // 匹配 { type: "product_list", ... }
|
|
93
|
-
* ]
|
|
94
|
-
*/
|
|
95
|
-
immediateResultMatchers?: ImmediateResultMatcher[];
|
|
96
|
-
/**
|
|
97
|
-
* 是否启用思考阶段(两阶段调用)
|
|
98
|
-
* - true: 每次迭代先让 AI 思考分析,再执行工具调用(默认,推荐)
|
|
99
|
-
* - false: 直接调用 AI 并执行工具(更快但可能不够准确)
|
|
100
|
-
*
|
|
101
|
-
* 启用后的流程:
|
|
102
|
-
* 1. 思考阶段:AI 分析需求,输出思考过程,决定调用什么工具
|
|
103
|
-
* 2. 执行阶段:根据思考结果执行工具调用
|
|
104
|
-
*
|
|
105
|
-
* 优点:
|
|
106
|
-
* - 任何模型都能看到思考过程
|
|
107
|
-
* - Chain-of-Thought 效应,显著提高复杂任务准确性
|
|
108
|
-
*
|
|
109
|
-
* 缺点:
|
|
110
|
-
* - 每次迭代多一次 API 调用,增加延迟和成本
|
|
111
|
-
*
|
|
112
|
-
* @default false
|
|
113
|
-
*/
|
|
114
|
-
enableThinkingPhase?: boolean;
|
|
115
|
-
/**
|
|
116
|
-
* 自定义思考阶段提示词
|
|
117
|
-
* 用于引导 AI 在调用工具前进行分析
|
|
118
|
-
*
|
|
119
|
-
* 如果不设置,使用默认提示词
|
|
120
|
-
*/
|
|
121
|
-
thinkingPhasePrompt?: string;
|
|
122
|
-
/**
|
|
123
|
-
* 思考阶段的最大 token 数
|
|
124
|
-
* 用于限制思考输出长度,不设置则不限制
|
|
125
|
-
*/
|
|
126
|
-
thinkingMaxTokens?: number;
|
|
5
|
+
/** AI 提供商适配器类型 */
|
|
6
|
+
type AIAdapterType = 'openai' | 'anthropic' | 'gemini' | 'ollama' | 'custom';
|
|
7
|
+
/** AI 请求配置 - 完全开放,任意参数 */
|
|
8
|
+
interface AIRequestConfig {
|
|
9
|
+
/** AI 服务地址 */
|
|
10
|
+
baseURL: string;
|
|
11
|
+
/** API 密钥 */
|
|
12
|
+
apiKey: string;
|
|
13
|
+
/** 模型名称 */
|
|
14
|
+
model: string;
|
|
15
|
+
/** 请求头(可选) */
|
|
16
|
+
headers?: Record<string, string>;
|
|
17
|
+
/** 请求超时(毫秒,默认 120000) */
|
|
18
|
+
timeout?: number;
|
|
19
|
+
/** 任意其他参数,原封不动传给 AI */
|
|
20
|
+
[key: string]: unknown;
|
|
127
21
|
}
|
|
128
|
-
/**
|
|
22
|
+
/** AI 消息角色 */
|
|
23
|
+
type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
24
|
+
/** AI 消息 */
|
|
25
|
+
interface Message {
|
|
26
|
+
role: MessageRole;
|
|
27
|
+
content: string;
|
|
28
|
+
/** 工具调用(assistant 消息) */
|
|
29
|
+
toolCalls?: ToolCall[];
|
|
30
|
+
/** 工具结果(tool 消息) */
|
|
31
|
+
toolResults?: ToolResult[];
|
|
32
|
+
}
|
|
33
|
+
/** 工具调用定义 */
|
|
129
34
|
interface ToolCall {
|
|
130
35
|
id: string;
|
|
131
36
|
name: string;
|
|
@@ -137,115 +42,80 @@ interface ToolResult {
|
|
|
137
42
|
toolName: string;
|
|
138
43
|
result: unknown;
|
|
139
44
|
isError?: boolean;
|
|
140
|
-
duration?: number;
|
|
141
45
|
}
|
|
142
|
-
/**
|
|
143
|
-
type
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
46
|
+
/** AI 流式事件 */
|
|
47
|
+
type AIStreamEvent = {
|
|
48
|
+
type: 'text';
|
|
49
|
+
content: string;
|
|
50
|
+
} | {
|
|
51
|
+
type: 'tool_call';
|
|
52
|
+
toolCall: ToolCall;
|
|
53
|
+
} | {
|
|
54
|
+
type: 'done';
|
|
55
|
+
} | {
|
|
56
|
+
type: 'error';
|
|
57
|
+
error: Error;
|
|
58
|
+
};
|
|
59
|
+
/** AI 响应(非流式) */
|
|
60
|
+
interface AIResponse {
|
|
147
61
|
content: string;
|
|
148
62
|
toolCalls?: ToolCall[];
|
|
149
|
-
toolResults?: ToolResult[];
|
|
150
63
|
}
|
|
151
|
-
/**
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
TEXT_START = "text_start",
|
|
161
|
-
/** AI 回复内容 (流式) */
|
|
162
|
-
TEXT_DELTA = "text_delta",
|
|
163
|
-
/** AI 回复结束 */
|
|
164
|
-
TEXT_END = "text_end",
|
|
165
|
-
/** 开始调用工具 */
|
|
166
|
-
TOOL_CALL_START = "tool_call_start",
|
|
167
|
-
/** 工具参数 (流式) */
|
|
168
|
-
TOOL_CALL_DELTA = "tool_call_delta",
|
|
169
|
-
/** 工具正在执行 */
|
|
170
|
-
TOOL_EXECUTING = "tool_executing",
|
|
171
|
-
/** 工具返回结果 */
|
|
172
|
-
TOOL_RESULT = "tool_result",
|
|
173
|
-
/** 工具返回的即时结果(匹配 immediateResultMatchers 时触发) */
|
|
174
|
-
IMMEDIATE_RESULT = "immediate_result",
|
|
175
|
-
/** 开始新一轮迭代 */
|
|
176
|
-
ITERATION_START = "iteration_start",
|
|
177
|
-
/** 迭代结束 */
|
|
178
|
-
ITERATION_END = "iteration_end",
|
|
179
|
-
/** 全部完成 */
|
|
180
|
-
COMPLETE = "complete",
|
|
181
|
-
/** 发生错误 */
|
|
182
|
-
ERROR = "error"
|
|
64
|
+
/** 工具定义(传递给 AI 的函数定义) */
|
|
65
|
+
interface ToolDefinition {
|
|
66
|
+
name: string;
|
|
67
|
+
description: string;
|
|
68
|
+
parameters: {
|
|
69
|
+
type: 'object';
|
|
70
|
+
properties: Record<string, unknown>;
|
|
71
|
+
required?: string[];
|
|
72
|
+
};
|
|
183
73
|
}
|
|
184
|
-
/**
|
|
185
|
-
interface
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
error?: Error;
|
|
199
|
-
/** 即时结果数据(原封不动的工具返回结果) */
|
|
200
|
-
immediateResult?: unknown;
|
|
74
|
+
/** 适配器接口 - 用于转换不同 AI 提供商的格式 */
|
|
75
|
+
interface AIAdapter {
|
|
76
|
+
/** 适配器名称 */
|
|
77
|
+
name: string;
|
|
78
|
+
/** 构建请求体 */
|
|
79
|
+
buildRequestBody: (config: AIRequestConfig, messages: Message[], tools?: ToolDefinition[]) => unknown;
|
|
80
|
+
/** 获取请求头 */
|
|
81
|
+
getHeaders: (config: AIRequestConfig) => Record<string, string>;
|
|
82
|
+
/** 获取请求端点 */
|
|
83
|
+
getEndpoint: (baseURL: string) => string;
|
|
84
|
+
/** 解析非流式响应 */
|
|
85
|
+
parseResponse: (data: unknown) => AIResponse;
|
|
86
|
+
/** 解析流式数据块(SSE) */
|
|
87
|
+
parseStreamChunk: (line: string) => AIStreamEvent | null;
|
|
201
88
|
}
|
|
202
|
-
/**
|
|
203
|
-
interface
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
89
|
+
/** 用户回调函数 - 处理 AI 响应 */
|
|
90
|
+
interface AIResponseHandler {
|
|
91
|
+
/**
|
|
92
|
+
* 处理 AI 流式响应
|
|
93
|
+
* @param event 流式事件
|
|
94
|
+
* @returns 是否继续接收(返回 false 可中止)
|
|
95
|
+
*/
|
|
96
|
+
onStream?: (event: AIStreamEvent) => boolean | void;
|
|
97
|
+
/**
|
|
98
|
+
* 处理需要工具调用的请求
|
|
99
|
+
* @param toolCalls 工具调用列表
|
|
100
|
+
* @returns 工具执行结果(MCPLink 会自动执行 MCP 工具后再次调用 AI)
|
|
101
|
+
*/
|
|
102
|
+
onToolCalls?: (toolCalls: ToolCall[]) => Promise<ToolResult[]> | ToolResult[];
|
|
207
103
|
}
|
|
208
|
-
/**
|
|
209
|
-
interface
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
/** 工具返回结果 */
|
|
215
|
-
onToolResult?: (toolName: string, result: unknown, duration: number) => void;
|
|
216
|
-
/** AI 输出文本 (流式) */
|
|
217
|
-
onTextDelta?: (delta: string) => void;
|
|
218
|
-
/** 迭代开始 */
|
|
219
|
-
onIterationStart?: (iteration: number) => void;
|
|
220
|
-
/** 迭代结束 */
|
|
221
|
-
onIterationEnd?: (iteration: number) => void;
|
|
222
|
-
/** 发生错误 */
|
|
223
|
-
onError?: (error: Error) => void;
|
|
104
|
+
/** MCP 服务器配置 - stdio 模式 */
|
|
105
|
+
interface MCPServerConfigStdio {
|
|
106
|
+
type?: 'stdio';
|
|
107
|
+
command: string;
|
|
108
|
+
args?: string[];
|
|
109
|
+
env?: Record<string, string>;
|
|
224
110
|
}
|
|
225
|
-
/**
|
|
226
|
-
interface
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
toolCalls: Array<{
|
|
231
|
-
name: string;
|
|
232
|
-
arguments: Record<string, unknown>;
|
|
233
|
-
result: unknown;
|
|
234
|
-
duration: number;
|
|
235
|
-
}>;
|
|
236
|
-
/** 完整消息历史 */
|
|
237
|
-
messages: Message[];
|
|
238
|
-
/** Token 使用量 */
|
|
239
|
-
usage: {
|
|
240
|
-
promptTokens: number;
|
|
241
|
-
completionTokens: number;
|
|
242
|
-
totalTokens: number;
|
|
243
|
-
};
|
|
244
|
-
/** 总迭代次数 */
|
|
245
|
-
iterations: number;
|
|
246
|
-
/** 总耗时 (ms) */
|
|
247
|
-
duration: number;
|
|
111
|
+
/** MCP 服务器配置 - Streamable HTTP 模式 */
|
|
112
|
+
interface MCPServerConfigStreamableHTTP {
|
|
113
|
+
type: 'streamable-http';
|
|
114
|
+
url: string;
|
|
115
|
+
headers?: Record<string, string>;
|
|
248
116
|
}
|
|
117
|
+
/** MCP 服务器配置 */
|
|
118
|
+
type MCPServerConfig = MCPServerConfigStdio | MCPServerConfigStreamableHTTP;
|
|
249
119
|
/** MCP 工具定义 */
|
|
250
120
|
interface MCPTool {
|
|
251
121
|
name: string;
|
|
@@ -265,19 +135,55 @@ interface MCPServerStatus {
|
|
|
265
135
|
tools: MCPTool[];
|
|
266
136
|
error?: string;
|
|
267
137
|
}
|
|
138
|
+
/** MCPLink 配置 */
|
|
139
|
+
interface MCPLinkConfig {
|
|
140
|
+
/** AI 请求配置 */
|
|
141
|
+
ai: AIRequestConfig;
|
|
142
|
+
/** 适配器类型或自定义适配器(默认 'openai') */
|
|
143
|
+
adapter?: AIAdapterType | AIAdapter;
|
|
144
|
+
/** MCP 服务器配置 */
|
|
145
|
+
mcpServers?: Record<string, MCPServerConfig>;
|
|
146
|
+
/** 最大迭代次数(防止无限循环,默认 10) */
|
|
147
|
+
maxIterations?: number;
|
|
148
|
+
}
|
|
149
|
+
/** 对话选项 */
|
|
150
|
+
interface ChatOptions {
|
|
151
|
+
/** 消息历史 */
|
|
152
|
+
messages: Message[];
|
|
153
|
+
/** 可用工具 */
|
|
154
|
+
tools?: ToolDefinition[];
|
|
155
|
+
/** 流式处理回调 */
|
|
156
|
+
onStream?: (event: AIStreamEvent) => boolean | void;
|
|
157
|
+
/** 是否启用流式(默认 true) */
|
|
158
|
+
stream?: boolean;
|
|
159
|
+
}
|
|
160
|
+
/** 对话结果 */
|
|
161
|
+
interface ChatResult {
|
|
162
|
+
content: string;
|
|
163
|
+
messages: Message[];
|
|
164
|
+
iterations: number;
|
|
165
|
+
duration: number;
|
|
166
|
+
}
|
|
268
167
|
|
|
269
168
|
/**
|
|
270
|
-
* MCPLink
|
|
271
|
-
*
|
|
169
|
+
* MCPLink - 极简 MCP + AI HTTP 桥接
|
|
170
|
+
*
|
|
171
|
+
* 职责:
|
|
172
|
+
* 1. 管理 MCP 服务器连接
|
|
173
|
+
* 2. 发起 AI HTTP 请求
|
|
174
|
+
* 3. 发现工具调用 → 执行 MCP 工具 → 回调结果
|
|
175
|
+
*
|
|
176
|
+
* 不职责:
|
|
177
|
+
* 1. 不管理消息历史(用户自己维护)
|
|
178
|
+
* 2. 不处理 AI 响应格式(用户通过 onStream 回调处理)
|
|
179
|
+
* 3. 不做复杂的 Agent 循环(用户控制迭代)
|
|
272
180
|
*/
|
|
273
181
|
declare class MCPLink {
|
|
274
|
-
private model;
|
|
275
|
-
private mcpManager;
|
|
276
|
-
private agent;
|
|
277
|
-
private promptBasedAgent;
|
|
278
182
|
private config;
|
|
183
|
+
private mcpManager;
|
|
184
|
+
private httpClient;
|
|
185
|
+
private adapter;
|
|
279
186
|
private initialized;
|
|
280
|
-
private detectedNativeSupport;
|
|
281
187
|
constructor(config: MCPLinkConfig);
|
|
282
188
|
/**
|
|
283
189
|
* 初始化 - 连接所有 MCP 服务器
|
|
@@ -288,63 +194,44 @@ declare class MCPLink {
|
|
|
288
194
|
*/
|
|
289
195
|
close(): Promise<void>;
|
|
290
196
|
/**
|
|
291
|
-
*
|
|
197
|
+
* 对话 - 极简设计
|
|
198
|
+
*
|
|
199
|
+
* 流程:
|
|
200
|
+
* 1. 发送消息给 AI
|
|
201
|
+
* 2. AI 返回文本/工具调用
|
|
202
|
+
* 3. 如果有工具调用,执行 MCP 工具
|
|
203
|
+
* 4. 返回结果(包括新的消息历史,用户可选择是否继续)
|
|
204
|
+
*
|
|
205
|
+
* 用户需要自己:
|
|
206
|
+
* - 维护 messages 历史
|
|
207
|
+
* - 处理流式响应(通过 onStream)
|
|
208
|
+
* - 决定是否继续迭代(如果返回了 toolCalls)
|
|
292
209
|
*/
|
|
293
|
-
chat(
|
|
210
|
+
chat(options: ChatOptions): Promise<ChatResult>;
|
|
294
211
|
/**
|
|
295
|
-
*
|
|
296
|
-
* @param message 用户消息(支持字符串或多模态数组)
|
|
297
|
-
* @param options 可选参数
|
|
298
|
-
* @param options.allowedTools 允许使用的工具名称列表
|
|
299
|
-
* @param options.history 历史消息列表
|
|
212
|
+
* 单次 AI 调用(非流式)
|
|
300
213
|
*/
|
|
301
|
-
|
|
302
|
-
allowedTools?: string[];
|
|
303
|
-
history?: Array<{
|
|
304
|
-
role: 'user' | 'assistant';
|
|
305
|
-
content: string;
|
|
306
|
-
}>;
|
|
307
|
-
}): AsyncGenerator<MCPLinkEvent>;
|
|
214
|
+
private singleChat;
|
|
308
215
|
/**
|
|
309
|
-
*
|
|
216
|
+
* 流式 AI 调用
|
|
310
217
|
*/
|
|
311
|
-
|
|
218
|
+
private streamChat;
|
|
312
219
|
/**
|
|
313
|
-
*
|
|
220
|
+
* 流式对话 - 公共方法
|
|
221
|
+
* 直接发起流式请求并返回结果
|
|
314
222
|
*/
|
|
315
|
-
|
|
223
|
+
chatStream(messages: Message[], onStream?: (event: AIStreamEvent) => boolean | void): Promise<ChatResult>;
|
|
316
224
|
/**
|
|
317
|
-
*
|
|
225
|
+
* 根据类型获取适配器
|
|
318
226
|
*/
|
|
227
|
+
private getAdapterByType;
|
|
228
|
+
addMCPServer(id: string, config: MCPServerConfig): void;
|
|
319
229
|
removeMCPServer(id: string): Promise<void>;
|
|
320
|
-
/**
|
|
321
|
-
* 启动指定 MCP 服务器
|
|
322
|
-
*/
|
|
323
230
|
startMCPServer(id: string): Promise<void>;
|
|
324
|
-
/**
|
|
325
|
-
* 停止指定 MCP 服务器
|
|
326
|
-
*/
|
|
327
231
|
stopMCPServer(id: string): Promise<void>;
|
|
328
|
-
/**
|
|
329
|
-
* 获取所有 MCP 服务器状态
|
|
330
|
-
*/
|
|
331
232
|
getMCPServerStatuses(): MCPServerStatus[];
|
|
332
|
-
/**
|
|
333
|
-
* 获取所有可用工具
|
|
334
|
-
*/
|
|
335
233
|
getTools(): MCPTool[];
|
|
336
|
-
/**
|
|
337
|
-
* 手动调用工具
|
|
338
|
-
*/
|
|
339
234
|
callTool(toolName: string, args: Record<string, unknown>): Promise<unknown>;
|
|
340
|
-
/**
|
|
341
|
-
* 更新系统提示词
|
|
342
|
-
*/
|
|
343
|
-
setSystemPrompt(prompt: string): void;
|
|
344
|
-
/**
|
|
345
|
-
* 更新 AI 模型
|
|
346
|
-
*/
|
|
347
|
-
setModel(model: LanguageModel): void;
|
|
348
235
|
}
|
|
349
236
|
|
|
350
237
|
/**
|
|
@@ -400,169 +287,166 @@ declare class MCPManager {
|
|
|
400
287
|
}
|
|
401
288
|
|
|
402
289
|
/**
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*/
|
|
406
|
-
declare const DEFAULT_SYSTEM_PROMPT = "\u4F60\u662F\u4E00\u4E2A\u4E13\u4E1A\u3001\u53CB\u597D\u7684\u667A\u80FD\u52A9\u624B\u3002\n\n## \u56DE\u590D\u8981\u6C42\n- \u7B80\u6D01\u6E05\u6670\uFF0C\u91CD\u70B9\u7A81\u51FA\n- \u7528\u5217\u8868\u5448\u73B0\u5173\u952E\u4FE1\u606F\n- \u8BED\u6C14\u793C\u8C8C\u81EA\u7136\uFF0C\u50CF\u4E13\u4E1A\u52A9\u624B\n- \u6709\u7ED3\u8BBA\u65F6\u76F4\u63A5\u7ED9\u51FA\uFF0C\u9700\u8981\u8865\u5145\u4FE1\u606F\u65F6\u7B80\u5355\u8BE2\u95EE";
|
|
407
|
-
/**
|
|
408
|
-
* 默认思考阶段提示词
|
|
409
|
-
* 用于引导 AI 进行内部思考(类似 Cursor 的思考风格)
|
|
410
|
-
*/
|
|
411
|
-
declare const DEFAULT_THINKING_PHASE_PROMPT = "\n---\n\u8FD9\u662F\u4F60\u7684\u5185\u5FC3\u72EC\u767D\uFF0C\u7528\u6237\u770B\u4E0D\u5230\u3002\n\n\u5224\u65AD\u5F53\u524D\u72B6\u6001\uFF1A\u6211\u62FF\u5230\u4E86\u4EC0\u4E48\uFF1F\u4EFB\u52A1\u5B8C\u6210\u4E86\u5417\uFF1F\u8FD8\u9700\u8981\u67E5\u4EC0\u4E48\uFF1F\n\n\u91CD\u8981\uFF1A\u8FD9\u91CC\u53EA\u662F\u601D\u8003\u5224\u65AD\uFF0C\u4E0D\u8981\u5728\u8FD9\u91CC\u5199\u56DE\u590D\u5185\u5BB9\uFF08\u56DE\u590D\u662F\u4E0B\u4E00\u6B65\u7684\u4E8B\uFF09\u3002\n---";
|
|
412
|
-
/**
|
|
413
|
-
* Agent 引擎
|
|
414
|
-
* 负责执行 AI 对话循环,处理工具调用
|
|
290
|
+
* HTTP 客户端 - 基于 axios
|
|
291
|
+
* 支持 SSE 流式响应
|
|
415
292
|
*/
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
private
|
|
419
|
-
|
|
420
|
-
private maxIterations;
|
|
421
|
-
private immediateResultMatchers;
|
|
422
|
-
private parallelToolCalls;
|
|
423
|
-
private enableThinkingPhase;
|
|
424
|
-
private thinkingPhasePrompt;
|
|
425
|
-
private thinkingMaxTokens?;
|
|
426
|
-
constructor(model: LanguageModel, mcpManager: MCPManager, options?: {
|
|
427
|
-
systemPrompt?: string;
|
|
428
|
-
maxIterations?: number;
|
|
429
|
-
immediateResultMatchers?: ImmediateResultMatcher[];
|
|
430
|
-
parallelToolCalls?: boolean;
|
|
431
|
-
enableThinkingPhase?: boolean;
|
|
432
|
-
thinkingPhasePrompt?: string;
|
|
433
|
-
thinkingMaxTokens?: number;
|
|
434
|
-
});
|
|
435
|
-
/**
|
|
436
|
-
* 生成工具描述文本(用于思考阶段)
|
|
437
|
-
*/
|
|
438
|
-
private generateToolsDescription;
|
|
439
|
-
/**
|
|
440
|
-
* 摘要化工具返回结果(用于思考阶段,避免 AI 直接格式化输出数据)
|
|
441
|
-
* @param toolName 工具名称
|
|
442
|
-
* @param result 工具返回的结果
|
|
443
|
-
* @returns 摘要字符串
|
|
444
|
-
*/
|
|
445
|
-
private summarizeToolResult;
|
|
446
|
-
/**
|
|
447
|
-
* 检查工具返回结果是否匹配即时结果匹配器
|
|
448
|
-
* @param result 工具返回的结果
|
|
449
|
-
* @returns 如果匹配返回 true,否则返回 false
|
|
450
|
-
*/
|
|
451
|
-
private matchImmediateResult;
|
|
452
|
-
/**
|
|
453
|
-
* 将 MCP 工具转换为 Vercel AI SDK 格式
|
|
454
|
-
*/
|
|
455
|
-
private convertMCPToolsToAITools;
|
|
456
|
-
/**
|
|
457
|
-
* JSON Schema 到 Zod 的完整递归转换
|
|
458
|
-
* 支持嵌套对象、对象数组、枚举等所有常见类型
|
|
459
|
-
*/
|
|
460
|
-
private jsonSchemaToZod;
|
|
461
|
-
/**
|
|
462
|
-
* 递归转换 JSON Schema 节点为 Zod 类型
|
|
463
|
-
*/
|
|
464
|
-
private convertSchemaToZod;
|
|
465
|
-
/**
|
|
466
|
-
* 执行对话
|
|
467
|
-
*/
|
|
468
|
-
chat(userMessage: string, callbacks?: ChatCallbacks): Promise<ChatResult>;
|
|
293
|
+
|
|
294
|
+
declare class HttpClient {
|
|
295
|
+
private client;
|
|
296
|
+
constructor();
|
|
469
297
|
/**
|
|
470
|
-
*
|
|
298
|
+
* 非流式请求
|
|
471
299
|
*/
|
|
472
|
-
|
|
300
|
+
chat(aiConfig: AIRequestConfig, adapter: AIAdapter, messages: Message[], tools?: ToolDefinition[]): Promise<AIResponse>;
|
|
473
301
|
/**
|
|
474
|
-
*
|
|
302
|
+
* 流式请求 - SSE
|
|
475
303
|
*/
|
|
476
|
-
|
|
304
|
+
streamChat(aiConfig: AIRequestConfig, adapter: AIAdapter, messages: Message[], tools?: ToolDefinition[]): AsyncGenerator<AIStreamEvent>;
|
|
477
305
|
/**
|
|
478
|
-
*
|
|
479
|
-
* @param userMessage 用户消息(支持字符串或多模态数组)
|
|
480
|
-
* @param options 可选参数
|
|
481
|
-
* @param options.allowedTools 允许使用的工具名称列表,为空或不传则使用所有工具
|
|
482
|
-
* @param options.history 历史消息列表
|
|
306
|
+
* 解析 SSE 流
|
|
483
307
|
*/
|
|
484
|
-
|
|
485
|
-
allowedTools?: string[];
|
|
486
|
-
history?: Array<{
|
|
487
|
-
role: 'user' | 'assistant';
|
|
488
|
-
content: string;
|
|
489
|
-
}>;
|
|
490
|
-
}): AsyncGenerator<MCPLinkEvent>;
|
|
308
|
+
private parseSSE;
|
|
491
309
|
}
|
|
492
310
|
|
|
493
311
|
/**
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* 设计原则:
|
|
498
|
-
* 1. 简洁 - 不做过多干预,让 AI 自己思考和决策
|
|
499
|
-
* 2. 通用 - 支持任何模型,不依赖特定 API
|
|
500
|
-
* 3. 可靠 - 稳定的状态机解析
|
|
312
|
+
* OpenAI 适配器
|
|
313
|
+
* 支持任意自定义参数(如 enable_thinking)
|
|
501
314
|
*/
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
private
|
|
506
|
-
private maxIterations;
|
|
507
|
-
private immediateResultMatchers;
|
|
508
|
-
private parallelToolCalls;
|
|
509
|
-
private enableThinkingPhase;
|
|
510
|
-
constructor(model: LanguageModel, mcpManager: MCPManager, options?: {
|
|
511
|
-
systemPrompt?: string;
|
|
512
|
-
maxIterations?: number;
|
|
513
|
-
immediateResultMatchers?: ImmediateResultMatcher[];
|
|
514
|
-
parallelToolCalls?: boolean;
|
|
515
|
-
enableThinkingPhase?: boolean;
|
|
516
|
-
thinkingPhasePrompt?: string;
|
|
517
|
-
thinkingMaxTokens?: number;
|
|
518
|
-
});
|
|
519
|
-
/**
|
|
520
|
-
* 检查工具返回结果是否匹配即时结果匹配器
|
|
521
|
-
* @param result 工具返回的结果
|
|
522
|
-
* @returns 如果匹配返回 true,否则返回 false
|
|
523
|
-
*/
|
|
524
|
-
private matchImmediateResult;
|
|
315
|
+
|
|
316
|
+
declare class OpenAIAdapter implements AIAdapter {
|
|
317
|
+
name: string;
|
|
318
|
+
private pendingToolCall;
|
|
525
319
|
/**
|
|
526
|
-
*
|
|
320
|
+
* 检查 tool_call 参数是否完整(可以解析为 JSON)
|
|
527
321
|
*/
|
|
528
|
-
private
|
|
322
|
+
private isToolCallComplete;
|
|
529
323
|
/**
|
|
530
|
-
*
|
|
324
|
+
* 返回并清空 pending tool_call
|
|
531
325
|
*/
|
|
532
|
-
private
|
|
326
|
+
private flushPendingToolCall;
|
|
533
327
|
/**
|
|
534
|
-
*
|
|
328
|
+
* 构建请求体
|
|
329
|
+
* 完全开放:除必要字段外,其他参数原封不动传递
|
|
535
330
|
*/
|
|
536
|
-
|
|
331
|
+
buildRequestBody(config: AIRequestConfig, messages: Message[], tools?: ToolDefinition[]): Record<string, unknown>;
|
|
537
332
|
/**
|
|
538
|
-
*
|
|
333
|
+
* 获取请求头
|
|
539
334
|
*/
|
|
540
|
-
|
|
335
|
+
getHeaders(config: AIRequestConfig): Record<string, string>;
|
|
541
336
|
/**
|
|
542
|
-
*
|
|
543
|
-
* - 用户消息完整保留
|
|
544
|
-
* - AI 回复保留关键信息(ID、名称、数量、价格等)
|
|
545
|
-
* - 去除冗长的 JSON 原始数据
|
|
337
|
+
* 获取请求端点
|
|
546
338
|
*/
|
|
547
|
-
|
|
339
|
+
getEndpoint(baseURL: string): string;
|
|
548
340
|
/**
|
|
549
|
-
*
|
|
341
|
+
* 解析非流式响应
|
|
550
342
|
*/
|
|
551
|
-
|
|
343
|
+
parseResponse(data: unknown): AIResponse;
|
|
552
344
|
/**
|
|
553
|
-
*
|
|
345
|
+
* 解析流式数据块
|
|
554
346
|
*/
|
|
555
|
-
|
|
347
|
+
parseStreamChunk(line: string): AIStreamEvent | null;
|
|
556
348
|
/**
|
|
557
|
-
*
|
|
349
|
+
* 转换消息格式
|
|
558
350
|
*/
|
|
559
|
-
|
|
560
|
-
allowedTools?: string[];
|
|
561
|
-
history?: Array<{
|
|
562
|
-
role: 'user' | 'assistant';
|
|
563
|
-
content: string;
|
|
564
|
-
}>;
|
|
565
|
-
}): AsyncGenerator<MCPLinkEvent>;
|
|
351
|
+
private convertMessage;
|
|
566
352
|
}
|
|
353
|
+
/** OpenAI 适配器实例 */
|
|
354
|
+
declare const openaiAdapter: OpenAIAdapter;
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 标准事件流转换器
|
|
358
|
+
* 将底层 AI 事件转换为完整的标准事件流
|
|
359
|
+
* 用户可选择使用底层事件或标准事件
|
|
360
|
+
*/
|
|
361
|
+
|
|
362
|
+
/** 标准流式事件类型 */
|
|
363
|
+
type StandardStreamEvent = {
|
|
364
|
+
type: 'text_start';
|
|
365
|
+
} | {
|
|
366
|
+
type: 'text_delta';
|
|
367
|
+
content: string;
|
|
368
|
+
} | {
|
|
369
|
+
type: 'text_end';
|
|
370
|
+
} | {
|
|
371
|
+
type: 'thinking_start';
|
|
372
|
+
} | {
|
|
373
|
+
type: 'thinking_delta';
|
|
374
|
+
content: string;
|
|
375
|
+
} | {
|
|
376
|
+
type: 'thinking_end';
|
|
377
|
+
} | {
|
|
378
|
+
type: 'tool_call_start';
|
|
379
|
+
toolCallId: string;
|
|
380
|
+
toolName: string;
|
|
381
|
+
toolArgs: Record<string, unknown>;
|
|
382
|
+
} | {
|
|
383
|
+
type: 'tool_call_delta';
|
|
384
|
+
toolCallId: string;
|
|
385
|
+
argsTextDelta: string;
|
|
386
|
+
} | {
|
|
387
|
+
type: 'tool_call_end';
|
|
388
|
+
toolCallId: string;
|
|
389
|
+
} | {
|
|
390
|
+
type: 'tool_executing';
|
|
391
|
+
toolCallId: string;
|
|
392
|
+
toolName: string;
|
|
393
|
+
} | {
|
|
394
|
+
type: 'tool_result';
|
|
395
|
+
toolCallId: string;
|
|
396
|
+
toolName: string;
|
|
397
|
+
toolResult: unknown;
|
|
398
|
+
duration: number;
|
|
399
|
+
isError?: boolean;
|
|
400
|
+
} | {
|
|
401
|
+
type: 'iteration_start';
|
|
402
|
+
iteration: number;
|
|
403
|
+
maxIterations: number;
|
|
404
|
+
} | {
|
|
405
|
+
type: 'iteration_end';
|
|
406
|
+
iteration: number;
|
|
407
|
+
} | {
|
|
408
|
+
type: 'complete';
|
|
409
|
+
totalIterations: number;
|
|
410
|
+
totalDuration: number;
|
|
411
|
+
} | {
|
|
412
|
+
type: 'error';
|
|
413
|
+
error: Error;
|
|
414
|
+
};
|
|
415
|
+
/**
|
|
416
|
+
* 标准事件流生成器选项
|
|
417
|
+
*/
|
|
418
|
+
interface StandardStreamOptions {
|
|
419
|
+
/** 最大迭代次数 */
|
|
420
|
+
maxIterations?: number;
|
|
421
|
+
/** 工具执行函数 */
|
|
422
|
+
executeTool: (name: string, args: Record<string, unknown>) => Promise<unknown>;
|
|
423
|
+
/** 底层事件回调(可选,用于自定义处理) */
|
|
424
|
+
onRawEvent?: (event: AIStreamEvent) => boolean | void;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* 将底层事件流转换为标准事件流
|
|
428
|
+
*/
|
|
429
|
+
declare function toStandardStream(rawStream: AsyncGenerator<AIStreamEvent>, options: StandardStreamOptions): AsyncGenerator<StandardStreamEvent>;
|
|
430
|
+
/**
|
|
431
|
+
* 标准响应处理器 - 非流式
|
|
432
|
+
* 收集完整响应内容
|
|
433
|
+
*/
|
|
434
|
+
interface StandardResponse {
|
|
435
|
+
content: string;
|
|
436
|
+
toolCalls: Array<{
|
|
437
|
+
id: string;
|
|
438
|
+
name: string;
|
|
439
|
+
arguments: Record<string, unknown>;
|
|
440
|
+
result?: unknown;
|
|
441
|
+
duration?: number;
|
|
442
|
+
isError?: boolean;
|
|
443
|
+
}>;
|
|
444
|
+
iterations: number;
|
|
445
|
+
duration: number;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* 从标准事件流收集完整响应
|
|
449
|
+
*/
|
|
450
|
+
declare function collectStandardResponse(stream: AsyncGenerator<StandardStreamEvent>): Promise<StandardResponse>;
|
|
567
451
|
|
|
568
|
-
export {
|
|
452
|
+
export { type AIAdapter, type AIAdapterType, type AIRequestConfig, type AIResponseHandler, type AIStreamEvent, type ChatOptions, type ChatResult, HttpClient, MCPLink, type MCPLinkConfig, MCPManager, type MCPServerConfig, type MCPServerConfigStdio, type MCPServerConfigStreamableHTTP, type MCPServerStatus, type MCPTool, type Message, type MessageRole, OpenAIAdapter, type StandardResponse, type StandardStreamEvent, type StandardStreamOptions, type ToolCall, type ToolDefinition, type ToolResult, collectStandardResponse, openaiAdapter, toStandardStream };
|