@n0ts123/mcplink-core 0.0.11 → 0.0.13

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/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
- * 用于检测 MCP 工具返回的特殊格式数据
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
- type ImmediateResultMatcher = Record<string, unknown>;
63
- /** MCPLink 配置 */
64
- interface MCPLinkConfig {
65
- /** AI 模型实例 (Vercel AI SDK) */
66
- model: LanguageModel;
67
- /** 模型名称(用于自动检测是否支持原生工具调用) */
68
- modelName?: string;
69
- /** MCP 服务器配置 */
70
- mcpServers?: Record<string, MCPServerConfig>;
71
- /** 系统提示词 */
72
- systemPrompt?: string;
73
- /** 最大迭代次数 (防止无限循环) */
74
- maxIterations?: number;
75
- /** 是否允许并行工具调用 */
76
- parallelToolCalls?: boolean;
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 MessageRole = 'system' | 'user' | 'assistant' | 'tool';
144
- /** 消息 */
145
- interface Message {
146
- role: MessageRole;
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
- declare enum MCPLinkEventType {
153
- /** AI 开始思考 */
154
- THINKING_START = "thinking_start",
155
- /** AI 思考内容 (流式) */
156
- THINKING_DELTA = "thinking_delta",
157
- /** AI 思考结束 */
158
- THINKING_END = "thinking_end",
159
- /** AI 开始回复 */
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 MCPLinkEventData {
186
- content?: string;
187
- toolName?: string;
188
- toolArgs?: Record<string, unknown>;
189
- toolResult?: unknown;
190
- toolCallId?: string;
191
- duration?: number;
192
- iteration?: number;
193
- maxIterations?: number;
194
- totalIterations?: number;
195
- totalDuration?: number;
196
- argsTextDelta?: string;
197
- isError?: boolean;
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 MCPLinkEvent {
204
- type: MCPLinkEventType;
205
- timestamp: number;
206
- data: MCPLinkEventData;
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 ChatCallbacks {
210
- /** AI 思考内容 */
211
- onThinking?: (content: string) => void;
212
- /** 开始调用工具 */
213
- onToolCallStart?: (toolName: string, args: Record<string, unknown>) => void;
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 ChatResult {
227
- /** 最终回复内容 */
228
- content: string;
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
- * AI Agent 工具调用框架的入口
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(message: string, callbacks?: ChatCallbacks): Promise<ChatResult>;
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
- chatStream(message: UserMessage, options?: {
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
- getToolCallingMode(): 'native' | 'prompt-based';
218
+ private streamChat;
312
219
  /**
313
- * 添加 MCP 服务器
220
+ * 流式对话 - 公共方法
221
+ * 直接发起流式请求并返回结果
314
222
  */
315
- addMCPServer(id: string, config: MCPServerConfig): void;
223
+ chatStream(messages: Message[], onStream?: (event: AIStreamEvent) => boolean | void): Promise<ChatResult>;
316
224
  /**
317
- * 移除 MCP 服务器
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,157 @@ 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
- declare class Agent {
417
- private model;
418
- private mcpManager;
419
- private systemPrompt;
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
- * 将 UserMessage 转换为 Vercel AI SDK 的消息内容格式
298
+ * 非流式请求
471
299
  */
472
- private convertUserMessageToContent;
300
+ chat(aiConfig: AIRequestConfig, adapter: AIAdapter, messages: Message[], tools?: ToolDefinition[]): Promise<AIResponse>;
473
301
  /**
474
- * UserMessage 提取纯文本内容(用于日志等)
302
+ * 流式请求 - SSE
475
303
  */
476
- private extractTextFromMessage;
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
- chatStream(userMessage: UserMessage, options?: {
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
- * 基于 Prompt 的 Agent
495
- * 通过 prompt 工程让任意模型支持工具调用和思考过程
496
- *
497
- * 设计原则:
498
- * 1. 简洁 - 不做过多干预,让 AI 自己思考和决策
499
- * 2. 通用 - 支持任何模型,不依赖特定 API
500
- * 3. 可靠 - 稳定的状态机解析
312
+ * OpenAI 适配器
313
+ * 支持任意自定义参数(如 enable_thinking)
501
314
  */
502
- declare class PromptBasedAgent {
503
- private model;
504
- private mcpManager;
505
- private systemPrompt;
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;
525
- /**
526
- * 生成工具列表描述
527
- */
528
- private generateToolsDescription;
529
- /**
530
- * 内置系统提示词 - 强调格式约束
531
- */
532
- private readonly BUILT_IN_PROMPT;
315
+
316
+ declare class OpenAIAdapter implements AIAdapter {
317
+ name: string;
533
318
  /**
534
- * 构建完整的系统提示词
319
+ * 构建请求体
320
+ * 完全开放:除必要字段外,其他参数原封不动传递
535
321
  */
536
- private buildSystemPrompt;
322
+ buildRequestBody(config: AIRequestConfig, messages: Message[], tools?: ToolDefinition[]): Record<string, unknown>;
537
323
  /**
538
- * 解析工具调用
324
+ * 获取请求头
539
325
  */
540
- private parseToolCall;
326
+ getHeaders(config: AIRequestConfig): Record<string, string>;
541
327
  /**
542
- * 智能压缩历史消息
543
- * - 用户消息完整保留
544
- * - AI 回复保留关键信息(ID、名称、数量、价格等)
545
- * - 去除冗长的 JSON 原始数据
328
+ * 获取请求端点
546
329
  */
547
- private compressHistory;
330
+ getEndpoint(baseURL: string): string;
548
331
  /**
549
- * 将 UserMessage 转换为 Vercel AI SDK 的消息内容格式
332
+ * 解析非流式响应
550
333
  */
551
- private convertUserMessageToContent;
334
+ parseResponse(data: unknown): AIResponse;
552
335
  /**
553
- * 从 UserMessage 提取纯文本内容(用于日志等)
336
+ * 解析流式数据块
554
337
  */
555
- private extractTextFromMessage;
338
+ parseStreamChunk(line: string): AIStreamEvent | null;
556
339
  /**
557
- * 流式对话(支持多模态消息)
340
+ * 转换消息格式
558
341
  */
559
- chatStream(userMessage: UserMessage, options?: {
560
- allowedTools?: string[];
561
- history?: Array<{
562
- role: 'user' | 'assistant';
563
- content: string;
564
- }>;
565
- }): AsyncGenerator<MCPLinkEvent>;
342
+ private convertMessage;
343
+ }
344
+ /** OpenAI 适配器实例 */
345
+ declare const openaiAdapter: OpenAIAdapter;
346
+
347
+ /**
348
+ * 标准事件流转换器
349
+ * 将底层 AI 事件转换为完整的标准事件流
350
+ * 用户可选择使用底层事件或标准事件
351
+ */
352
+
353
+ /** 标准流式事件类型 */
354
+ type StandardStreamEvent = {
355
+ type: 'text_start';
356
+ } | {
357
+ type: 'text_delta';
358
+ content: string;
359
+ } | {
360
+ type: 'text_end';
361
+ } | {
362
+ type: 'thinking_start';
363
+ } | {
364
+ type: 'thinking_delta';
365
+ content: string;
366
+ } | {
367
+ type: 'thinking_end';
368
+ } | {
369
+ type: 'tool_call_start';
370
+ toolCallId: string;
371
+ toolName: string;
372
+ toolArgs: Record<string, unknown>;
373
+ } | {
374
+ type: 'tool_call_delta';
375
+ toolCallId: string;
376
+ argsTextDelta: string;
377
+ } | {
378
+ type: 'tool_call_end';
379
+ toolCallId: string;
380
+ } | {
381
+ type: 'tool_executing';
382
+ toolCallId: string;
383
+ toolName: string;
384
+ } | {
385
+ type: 'tool_result';
386
+ toolCallId: string;
387
+ toolName: string;
388
+ toolResult: unknown;
389
+ duration: number;
390
+ isError?: boolean;
391
+ } | {
392
+ type: 'iteration_start';
393
+ iteration: number;
394
+ maxIterations: number;
395
+ } | {
396
+ type: 'iteration_end';
397
+ iteration: number;
398
+ } | {
399
+ type: 'complete';
400
+ totalIterations: number;
401
+ totalDuration: number;
402
+ } | {
403
+ type: 'error';
404
+ error: Error;
405
+ };
406
+ /**
407
+ * 标准事件流生成器选项
408
+ */
409
+ interface StandardStreamOptions {
410
+ /** 最大迭代次数 */
411
+ maxIterations?: number;
412
+ /** 工具执行函数 */
413
+ executeTool: (name: string, args: Record<string, unknown>) => Promise<unknown>;
414
+ /** 底层事件回调(可选,用于自定义处理) */
415
+ onRawEvent?: (event: AIStreamEvent) => boolean | void;
416
+ }
417
+ /**
418
+ * 将底层事件流转换为标准事件流
419
+ */
420
+ declare function toStandardStream(rawStream: AsyncGenerator<AIStreamEvent>, options: StandardStreamOptions): AsyncGenerator<StandardStreamEvent>;
421
+ /**
422
+ * 标准响应处理器 - 非流式
423
+ * 收集完整响应内容
424
+ */
425
+ interface StandardResponse {
426
+ content: string;
427
+ toolCalls: Array<{
428
+ id: string;
429
+ name: string;
430
+ arguments: Record<string, unknown>;
431
+ result?: unknown;
432
+ duration?: number;
433
+ isError?: boolean;
434
+ }>;
435
+ iterations: number;
436
+ duration: number;
566
437
  }
438
+ /**
439
+ * 从标准事件流收集完整响应
440
+ */
441
+ declare function collectStandardResponse(stream: AsyncGenerator<StandardStreamEvent>): Promise<StandardResponse>;
567
442
 
568
- export { Agent, type ChatCallbacks, type ChatResult, DEFAULT_SYSTEM_PROMPT, DEFAULT_THINKING_PHASE_PROMPT, type FilePart, type ImagePart, type ImmediateResultMatcher, MCPLink, type MCPLinkConfig, type MCPLinkEvent, type MCPLinkEventData, MCPLinkEventType, MCPManager, type MCPServerConfig, type MCPServerConfigStdio, type MCPServerConfigStreamableHTTP, type MCPServerStatus, type MCPTool, type Message, type MessageContentPart, type MessageRole, PromptBasedAgent, type TextPart, type ToolCall, type ToolResult, type UserMessage };
443
+ 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 };