@huyooo/ai-chat-core 0.2.13 → 0.2.15
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 +3 -3
- package/dist/index.d.ts +653 -304
- package/dist/index.js +1 -2846
- package/package.json +7 -2
- package/dist/index.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,107 +1,197 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
4
|
+
* 模型注册表
|
|
3
5
|
*
|
|
4
|
-
*
|
|
6
|
+
* 核心设计:Protocol + Family 分离
|
|
7
|
+
* - Protocol:负责 API 通信协议(HTTP/SSE/认证)
|
|
8
|
+
* - Family:负责模型行为差异(thinking格式、搜索方式、工具格式)
|
|
5
9
|
*
|
|
6
|
-
*
|
|
7
|
-
* 1.
|
|
8
|
-
* 2.
|
|
9
|
-
* 3.
|
|
10
|
-
*/
|
|
11
|
-
/**
|
|
12
|
-
type
|
|
13
|
-
/**
|
|
14
|
-
type
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
10
|
+
* 这样设计的好处:
|
|
11
|
+
* 1. 同协议不同行为的模型可以复用协议层(如 ARK 上的豆包和 DeepSeek)
|
|
12
|
+
* 2. 新增模型只需配置 family,无需修改 adapter 代码
|
|
13
|
+
* 3. 行为差异集中管理,易于维护
|
|
14
|
+
*/
|
|
15
|
+
/** 模型家族 ID */
|
|
16
|
+
type ModelFamilyId = 'doubao' | 'deepseek' | 'qwen' | 'gemini' | 'gpt' | 'claude';
|
|
17
|
+
/** 协议类型 */
|
|
18
|
+
type ProtocolId = 'ark' | 'deepseek' | 'qwen' | 'gemini' | 'openai' | 'anthropic';
|
|
19
|
+
/** Thinking 输出格式 */
|
|
20
|
+
type ThinkingFormat = 'reasoning' | 'thinking_enabled' | 'thought_signature' | 'none';
|
|
21
|
+
/** 搜索实现方式 */
|
|
22
|
+
type SearchStrategy = 'native_web_search' | 'native_enable' | 'google_search' | 'tool_based';
|
|
23
|
+
/** 工具调用格式 */
|
|
24
|
+
type ToolCallFormat = 'responses' | 'openai' | 'gemini';
|
|
25
|
+
/**
|
|
26
|
+
* 模型家族配置
|
|
27
|
+
* 定义同一家族模型的共同行为特征
|
|
28
|
+
*/
|
|
29
|
+
interface ModelFamilyConfig {
|
|
30
|
+
/** 家族 ID */
|
|
31
|
+
id: ModelFamilyId;
|
|
32
|
+
/** 显示名称 */
|
|
33
|
+
displayName: string;
|
|
34
|
+
/** 是否支持图片理解 */
|
|
35
|
+
supportsVision: boolean;
|
|
36
|
+
/** 是否支持 thinking */
|
|
37
|
+
supportsThinking: boolean;
|
|
38
|
+
/** Thinking 输出格式 */
|
|
39
|
+
thinkingFormat: ThinkingFormat;
|
|
40
|
+
/** 是否支持原生搜索 */
|
|
41
|
+
supportsNativeSearch: boolean;
|
|
42
|
+
/** 搜索实现方式 */
|
|
43
|
+
searchStrategy: SearchStrategy;
|
|
44
|
+
/** 工具调用格式 */
|
|
45
|
+
toolCallFormat: ToolCallFormat;
|
|
46
|
+
/** 默认最大输出 token */
|
|
47
|
+
defaultMaxTokens?: number;
|
|
48
|
+
/** 是否需要特殊处理(如 Gemini 的 thought_signature) */
|
|
49
|
+
requiresSpecialHandling?: string[];
|
|
50
|
+
}
|
|
51
|
+
/** 豆包家族 */
|
|
52
|
+
declare const DOUBAO_FAMILY: ModelFamilyConfig;
|
|
53
|
+
/** DeepSeek 家族 */
|
|
54
|
+
declare const DEEPSEEK_FAMILY: ModelFamilyConfig;
|
|
55
|
+
/** 通义千问家族 */
|
|
56
|
+
/** Qwen 家族(使用 Tavily 统一搜索) */
|
|
57
|
+
declare const QWEN_FAMILY: ModelFamilyConfig;
|
|
58
|
+
/** Gemini 家族(注意:googleSearch 不能与其他工具同时使用,有工具时走 Tavily) */
|
|
59
|
+
declare const GEMINI_FAMILY: ModelFamilyConfig;
|
|
60
|
+
/** GPT 家族 */
|
|
61
|
+
declare const GPT_FAMILY: ModelFamilyConfig;
|
|
62
|
+
/** Claude 家族(使用 Vercel AI SDK / @ai-sdk/anthropic) */
|
|
63
|
+
declare const CLAUDE_FAMILY: ModelFamilyConfig;
|
|
64
|
+
/** 家族配置映射 */
|
|
65
|
+
declare const MODEL_FAMILIES: Record<ModelFamilyId, ModelFamilyConfig>;
|
|
66
|
+
/**
|
|
67
|
+
* 模型注册项
|
|
68
|
+
*/
|
|
69
|
+
interface ModelRegistryEntry {
|
|
70
|
+
/** 模型 ID(API 调用用) */
|
|
71
|
+
id: string;
|
|
72
|
+
/** 显示名称 */
|
|
73
|
+
displayName: string;
|
|
74
|
+
/** 所属家族 */
|
|
75
|
+
family: ModelFamilyId;
|
|
76
|
+
/** 使用的协议 */
|
|
77
|
+
protocol: ProtocolId;
|
|
78
|
+
/** 是否在前端显示 */
|
|
79
|
+
visible?: boolean;
|
|
80
|
+
/** 是否支持图片理解(优先级高于 family.supportsVision) */
|
|
81
|
+
supportsVision?: boolean;
|
|
82
|
+
/** 上下文窗口大小(如 "256K") */
|
|
83
|
+
contextWindow?: string;
|
|
84
|
+
/** 价格信息(数组,分行显示) */
|
|
85
|
+
pricing?: string[];
|
|
27
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* 全局模型注册表
|
|
89
|
+
*
|
|
90
|
+
* 每个模型只有一家供应商,无需分组
|
|
91
|
+
*/
|
|
92
|
+
declare const MODEL_REGISTRY: ModelRegistryEntry[];
|
|
93
|
+
/**
|
|
94
|
+
* 根据模型 ID 获取注册信息
|
|
95
|
+
*/
|
|
96
|
+
declare function getModelEntry(modelId: string): ModelRegistryEntry | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* 根据模型 ID 获取家族配置
|
|
99
|
+
*/
|
|
100
|
+
declare function getModelFamily(modelId: string): ModelFamilyConfig | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* 根据模型 ID 获取协议类型
|
|
103
|
+
*/
|
|
104
|
+
declare function getModelProtocol(modelId: string): ProtocolId | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* 获取所有可见模型(用于前端显示)
|
|
107
|
+
*/
|
|
108
|
+
declare function getVisibleModels(): ModelRegistryEntry[];
|
|
109
|
+
/**
|
|
110
|
+
* 根据家族 ID 获取所有模型
|
|
111
|
+
*/
|
|
112
|
+
declare function getModelsByFamily(familyId: ModelFamilyId): ModelRegistryEntry[];
|
|
113
|
+
/**
|
|
114
|
+
* 根据协议获取所有模型
|
|
115
|
+
*/
|
|
116
|
+
declare function getModelsByProtocol(protocol: ProtocolId): ModelRegistryEntry[];
|
|
117
|
+
/**
|
|
118
|
+
* 检查模型是否支持 thinking
|
|
119
|
+
*/
|
|
120
|
+
declare function modelSupportsThinking(modelId: string): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* 检查模型是否支持原生搜索
|
|
123
|
+
*/
|
|
124
|
+
declare function modelSupportsNativeSearch(modelId: string): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* 获取模型的搜索策略
|
|
127
|
+
*/
|
|
128
|
+
declare function getModelSearchStrategy(modelId: string): SearchStrategy;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 模型路由模块
|
|
132
|
+
*
|
|
133
|
+
* 根据模型 ID 查询对应的 Protocol 和 Family 配置
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
/** Provider 类型 = Protocol 类型 */
|
|
137
|
+
type ProviderType = ProtocolId;
|
|
28
138
|
/** 路由结果 */
|
|
29
139
|
interface RouteResult {
|
|
30
|
-
/**
|
|
140
|
+
/** Protocol 类型 */
|
|
31
141
|
provider: ProviderType;
|
|
32
|
-
/**
|
|
33
|
-
matchedRule?: RouteRule;
|
|
34
|
-
/** 是否使用了默认路由 */
|
|
142
|
+
/** 是否未注册(使用默认 Provider) */
|
|
35
143
|
isDefault: boolean;
|
|
144
|
+
/** 模型注册信息 */
|
|
145
|
+
registryEntry?: ModelRegistryEntry;
|
|
146
|
+
/** 模型家族配置 */
|
|
147
|
+
familyConfig?: ModelFamilyConfig;
|
|
36
148
|
}
|
|
37
149
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* @param model - 模型名称
|
|
41
|
-
* @returns Provider 类型
|
|
150
|
+
* 获取模型的 Provider
|
|
42
151
|
*/
|
|
43
152
|
declare function routeModelToProvider(model: string): ProviderType;
|
|
44
153
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* @param model - 模型名称
|
|
48
|
-
* @returns 路由结果(包含匹配规则信息)
|
|
154
|
+
* 获取模型的完整路由信息
|
|
49
155
|
*/
|
|
50
156
|
declare function routeModelWithDetails(model: string): RouteResult;
|
|
51
|
-
/**
|
|
52
|
-
* 获取所有路由规则(调试用)
|
|
53
|
-
*/
|
|
54
|
-
declare function getRouteRules(): readonly RouteRule[];
|
|
55
157
|
/**
|
|
56
158
|
* 获取默认 Provider
|
|
57
159
|
*/
|
|
58
160
|
declare function getDefaultProvider(): ProviderType;
|
|
59
161
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @param model - 模型名称
|
|
63
|
-
* @param provider - Provider 类型
|
|
64
|
-
* @returns 是否匹配
|
|
162
|
+
* 检查模型是否属于指定 Provider
|
|
65
163
|
*/
|
|
66
164
|
declare function isModelForProvider(model: string, provider: ProviderType): boolean;
|
|
67
165
|
|
|
68
166
|
/**
|
|
69
167
|
* AI Chat Core 类型定义
|
|
70
|
-
*
|
|
71
|
-
* 注意:事件类型已迁移到 events.ts
|
|
72
168
|
*/
|
|
169
|
+
|
|
73
170
|
/** 对话模式 */
|
|
74
|
-
type ChatMode = 'agent' | '
|
|
171
|
+
type ChatMode = 'agent' | 'ask';
|
|
75
172
|
|
|
76
|
-
/**
|
|
77
|
-
* 简化的模型配置
|
|
78
|
-
*
|
|
79
|
-
* - modelId: 发送给 API 的模型 ID
|
|
80
|
-
* - displayName: 前端显示名称
|
|
81
|
-
* - group: 分组名称(由后端决定,前端只负责渲染)
|
|
82
|
-
*/
|
|
173
|
+
/** 模型选项(前端显示用) */
|
|
83
174
|
interface ModelOption {
|
|
84
175
|
/** 模型 ID(发送给 API) */
|
|
85
176
|
modelId: string;
|
|
86
177
|
/** 显示名称 */
|
|
87
178
|
displayName: string;
|
|
88
|
-
/**
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
|
|
179
|
+
/** 是否支持深度思考 */
|
|
180
|
+
supportsThinking: boolean;
|
|
181
|
+
/** 是否支持图片理解 */
|
|
182
|
+
supportsVision: boolean;
|
|
183
|
+
/** 用于模型项 hover 的特性描述(由后端定义,前端只渲染) */
|
|
184
|
+
tooltip?: {
|
|
185
|
+
features?: string[];
|
|
186
|
+
/** 开销信息(数组,分行显示) */
|
|
187
|
+
cost?: string[];
|
|
188
|
+
description?: string;
|
|
189
|
+
};
|
|
94
190
|
}
|
|
95
191
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* 分组信息由后端决定,前端只负责渲染
|
|
192
|
+
* 预定义模型列表
|
|
99
193
|
*/
|
|
100
194
|
declare const MODELS: ModelOption[];
|
|
101
|
-
/** 获取原生模型 */
|
|
102
|
-
declare function getNativeModels(): ModelOption[];
|
|
103
|
-
/** 获取 OpenRouter 模型 */
|
|
104
|
-
declare function getOpenRouterModels(): ModelOption[];
|
|
105
195
|
/** 根据 modelId 获取模型 */
|
|
106
196
|
declare function getModelByModelId(modelId: string): ModelOption | undefined;
|
|
107
197
|
/** 工具批准回调函数 */
|
|
@@ -124,6 +214,10 @@ interface AgentConfig {
|
|
|
124
214
|
openrouterApiKey?: string;
|
|
125
215
|
/** OpenRouter API URL */
|
|
126
216
|
openrouterApiUrl?: string;
|
|
217
|
+
/** Vercel API Key(用于 Vercel AI Gateway 访问 Claude) */
|
|
218
|
+
vercelApiKey?: string;
|
|
219
|
+
/** Tavily API Key(用于统一 Web Search) */
|
|
220
|
+
tavilyApiKey?: string;
|
|
127
221
|
/** Gemini API Key (用于图片/视频,注意:中国大陆无法直接访问) */
|
|
128
222
|
geminiApiKey: string;
|
|
129
223
|
/** 当前工作目录(Current Working Directory) */
|
|
@@ -146,11 +240,6 @@ interface AgentConfig {
|
|
|
146
240
|
* ```
|
|
147
241
|
*/
|
|
148
242
|
tools?: ToolConfigItem[];
|
|
149
|
-
/**
|
|
150
|
-
* plan 模式的系统提示词(可选,有默认值)
|
|
151
|
-
* 用于指导 AI 如何制定执行计划
|
|
152
|
-
*/
|
|
153
|
-
planPrompt?: string;
|
|
154
243
|
/**
|
|
155
244
|
* 工具批准回调(manual 模式使用)
|
|
156
245
|
* 返回 true 表示批准执行,false 表示跳过
|
|
@@ -196,6 +285,8 @@ interface ChatOptions {
|
|
|
196
285
|
* - 'disabled': 强制关闭深度思考
|
|
197
286
|
*/
|
|
198
287
|
thinkingMode?: ThinkingMode;
|
|
288
|
+
/** 启用的工具名称列表(agent 模式有效,ask 模式强制禁用) */
|
|
289
|
+
enabledTools?: string[];
|
|
199
290
|
/** 自动运行配置 */
|
|
200
291
|
autoRunConfig?: AutoRunConfig$1;
|
|
201
292
|
/** 对话历史(从数据库加载,无状态架构) */
|
|
@@ -260,6 +351,7 @@ interface ToolContext {
|
|
|
260
351
|
* description: '我的自定义工具',
|
|
261
352
|
* parameters: { type: 'object', properties: {}, required: [] },
|
|
262
353
|
* sideEffects: ['filesystem'], // 声明副作用
|
|
354
|
+
* resultType: 'my_result', // 结果类型(用于前端渲染)
|
|
263
355
|
* execute: async (args, ctx) => '执行结果'
|
|
264
356
|
* };
|
|
265
357
|
* ```
|
|
@@ -293,6 +385,15 @@ interface Tool {
|
|
|
293
385
|
* 注意:如果 execute 返回 ToolResult 带有 sideEffects,会覆盖此静态声明
|
|
294
386
|
*/
|
|
295
387
|
sideEffects?: SideEffect$1[];
|
|
388
|
+
/**
|
|
389
|
+
* 结果类型(用于前端渲染)
|
|
390
|
+
*
|
|
391
|
+
* 当工具执行成功时,前端会根据此类型生成对应的 ContentPart
|
|
392
|
+
* 例如:resultType: 'weather' 会生成 { type: 'weather', ...result }
|
|
393
|
+
*
|
|
394
|
+
* 如果不指定,工具结果仅显示在 ToolCallPart 的折叠面板中
|
|
395
|
+
*/
|
|
396
|
+
resultType?: string;
|
|
296
397
|
/**
|
|
297
398
|
* 执行函数
|
|
298
399
|
*
|
|
@@ -305,7 +406,10 @@ interface Tool {
|
|
|
305
406
|
/** 工具执行器接口(可由使用方实现,用于自定义命令执行环境) */
|
|
306
407
|
interface ToolExecutor$1 {
|
|
307
408
|
/** 执行 Shell 命令 */
|
|
308
|
-
executeCommand(command: string, cwd?: string, signal?: AbortSignal
|
|
409
|
+
executeCommand(command: string, cwd?: string, signal?: AbortSignal, hooks?: {
|
|
410
|
+
onStdout?: (chunk: string) => void;
|
|
411
|
+
onStderr?: (chunk: string) => void;
|
|
412
|
+
}): Promise<{
|
|
309
413
|
success: boolean;
|
|
310
414
|
output?: string;
|
|
311
415
|
error?: string;
|
|
@@ -406,6 +510,8 @@ interface ChatMessage {
|
|
|
406
510
|
name: string;
|
|
407
511
|
arguments: string;
|
|
408
512
|
};
|
|
513
|
+
/** Gemini 模型需要的 thought_signature(用于工具调用循环) */
|
|
514
|
+
thought_signature?: string;
|
|
409
515
|
}>;
|
|
410
516
|
}
|
|
411
517
|
/** 火山引擎 Responses API 工具定义 */
|
|
@@ -592,6 +698,29 @@ interface ToolCallResultEvent {
|
|
|
592
698
|
* 前端可根据此字段处理通知、刷新文件列表等
|
|
593
699
|
*/
|
|
594
700
|
sideEffects?: SideEffect[];
|
|
701
|
+
/**
|
|
702
|
+
* 结果类型(用于前端渲染)
|
|
703
|
+
*
|
|
704
|
+
* 当工具定义了 resultType 且执行成功时,前端会根据此类型生成对应的 ContentPart
|
|
705
|
+
* 例如:resultType: 'weather' 会生成 { type: 'weather', ...result }
|
|
706
|
+
*/
|
|
707
|
+
resultType?: string;
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
/** 工具输出增量事件(用于 stdout/stderr 流式展示) */
|
|
711
|
+
interface ToolCallOutputEvent {
|
|
712
|
+
type: 'tool_call_output';
|
|
713
|
+
data: {
|
|
714
|
+
/** 工具调用 ID */
|
|
715
|
+
id: string;
|
|
716
|
+
/** 工具名称 */
|
|
717
|
+
name: string;
|
|
718
|
+
/** 输出流类型 */
|
|
719
|
+
stream: 'stdout' | 'stderr';
|
|
720
|
+
/** 输出增量内容 */
|
|
721
|
+
chunk: string;
|
|
722
|
+
/** 时间戳 */
|
|
723
|
+
at: number;
|
|
595
724
|
};
|
|
596
725
|
}
|
|
597
726
|
/** 工具执行批准请求事件(manual 模式) */
|
|
@@ -609,7 +738,7 @@ interface ToolApprovalRequestEvent {
|
|
|
609
738
|
};
|
|
610
739
|
}
|
|
611
740
|
/** 工具相关事件联合类型 */
|
|
612
|
-
type ToolEvent = ToolCallStartEvent | ToolCallResultEvent | ToolApprovalRequestEvent;
|
|
741
|
+
type ToolEvent = ToolCallStartEvent | ToolCallResultEvent | ToolApprovalRequestEvent | ToolCallOutputEvent;
|
|
613
742
|
/** 文本增量事件 */
|
|
614
743
|
interface TextDeltaEvent {
|
|
615
744
|
type: 'text_delta';
|
|
@@ -744,7 +873,7 @@ declare function createToolCallStart(id: string, name: string, args: Record<stri
|
|
|
744
873
|
/**
|
|
745
874
|
* 创建工具调用结果事件
|
|
746
875
|
*/
|
|
747
|
-
declare function createToolCallResult(id: string, name: string, result: string, success: boolean, startedAt: number, error?: string, sideEffects?: SideEffect[]): ToolCallResultEvent;
|
|
876
|
+
declare function createToolCallResult(id: string, name: string, result: string, success: boolean, startedAt: number, error?: string, sideEffects?: SideEffect[], resultType?: string): ToolCallResultEvent;
|
|
748
877
|
/**
|
|
749
878
|
* 创建文本增量事件
|
|
750
879
|
*/
|
|
@@ -842,22 +971,22 @@ interface RuntimeConfig {
|
|
|
842
971
|
qwenApiUrl: string;
|
|
843
972
|
openrouterApiKey: string;
|
|
844
973
|
openrouterApiUrl: string;
|
|
974
|
+
vercelApiKey: string;
|
|
975
|
+
tavilyApiKey: string;
|
|
845
976
|
geminiApiKey: string;
|
|
846
977
|
cwd: string;
|
|
847
|
-
planPrompt: string;
|
|
848
978
|
}
|
|
849
979
|
/**
|
|
850
980
|
* 混合 Agent 类
|
|
851
981
|
*
|
|
852
982
|
* 职责:
|
|
853
|
-
* 1.
|
|
854
|
-
* 2.
|
|
855
|
-
* 3.
|
|
856
|
-
* 4. 管理对话历史和工具注册
|
|
983
|
+
* 1. 管理 UnifiedAdapter(统一适配器)
|
|
984
|
+
* 2. 使用 ChatOrchestrator 统一处理工具调用
|
|
985
|
+
* 3. 管理工具注册
|
|
857
986
|
*/
|
|
858
987
|
declare class HybridAgent {
|
|
859
988
|
private config;
|
|
860
|
-
private
|
|
989
|
+
private adapter;
|
|
861
990
|
private orchestrator;
|
|
862
991
|
private geminiClient;
|
|
863
992
|
private toolExecutor;
|
|
@@ -869,19 +998,18 @@ declare class HybridAgent {
|
|
|
869
998
|
constructor(config: AgentConfig, toolExecutor?: ToolExecutor$1);
|
|
870
999
|
/** 异步初始化工具(在第一次 chat 前调用) */
|
|
871
1000
|
private asyncInit;
|
|
872
|
-
/**
|
|
873
|
-
|
|
1001
|
+
/** 获取模型的家族配置 */
|
|
1002
|
+
getModelFamilyConfig(model: string): ModelFamilyConfig | undefined;
|
|
874
1003
|
/**
|
|
875
|
-
*
|
|
1004
|
+
* 判断模型提供商(兼容旧接口)
|
|
876
1005
|
*/
|
|
877
1006
|
private getModelProvider;
|
|
878
|
-
/** 获取 Adapter */
|
|
879
|
-
private getAdapter;
|
|
880
1007
|
/**
|
|
881
1008
|
* 调试:获取模型路由信息
|
|
882
1009
|
*/
|
|
883
1010
|
getModelRouteInfo(model: string): RouteResult & {
|
|
884
1011
|
available: boolean;
|
|
1012
|
+
familyConfig?: ModelFamilyConfig;
|
|
885
1013
|
};
|
|
886
1014
|
/** 构建系统提示词 */
|
|
887
1015
|
private buildSystemPrompt;
|
|
@@ -893,6 +1021,11 @@ declare class HybridAgent {
|
|
|
893
1021
|
private executeTool;
|
|
894
1022
|
/** 获取工具定义列表 */
|
|
895
1023
|
private getToolDefinitions;
|
|
1024
|
+
/** 获取所有工具列表(用于设置面板) */
|
|
1025
|
+
getAllTools(): Array<{
|
|
1026
|
+
name: string;
|
|
1027
|
+
description: string;
|
|
1028
|
+
}>;
|
|
896
1029
|
/**
|
|
897
1030
|
* 聊天入口
|
|
898
1031
|
*
|
|
@@ -942,6 +1075,8 @@ interface ToolCallRequest {
|
|
|
942
1075
|
id: string;
|
|
943
1076
|
name: string;
|
|
944
1077
|
arguments: string;
|
|
1078
|
+
/** Gemini 模型需要的 thought_signature(用于工具调用循环) */
|
|
1079
|
+
thought_signature?: string;
|
|
945
1080
|
}
|
|
946
1081
|
/** 搜索结果项 */
|
|
947
1082
|
interface SearchResultItem {
|
|
@@ -972,6 +1107,8 @@ interface StandardMessage {
|
|
|
972
1107
|
interface AdapterConfig {
|
|
973
1108
|
apiKey: string;
|
|
974
1109
|
apiUrl?: string;
|
|
1110
|
+
/** Tavily API Key(用于统一 Web Search) */
|
|
1111
|
+
tavilyApiKey?: string;
|
|
975
1112
|
}
|
|
976
1113
|
/** 单次调用选项 */
|
|
977
1114
|
interface StreamOnceOptions {
|
|
@@ -1018,7 +1155,17 @@ interface ProviderAdapter {
|
|
|
1018
1155
|
* - string: 简单结果,使用工具的静态 sideEffects
|
|
1019
1156
|
* - ToolResult: 带动态副作用的结果
|
|
1020
1157
|
*/
|
|
1021
|
-
|
|
1158
|
+
interface ToolExecutionHooks {
|
|
1159
|
+
/** 工具调用 ID(用于把输出归属到具体 tool_call 卡片) */
|
|
1160
|
+
toolCallId: string;
|
|
1161
|
+
/** 工具名称 */
|
|
1162
|
+
toolName: string;
|
|
1163
|
+
/** stdout 增量 */
|
|
1164
|
+
onStdout?: (chunk: string) => void;
|
|
1165
|
+
/** stderr 增量 */
|
|
1166
|
+
onStderr?: (chunk: string) => void;
|
|
1167
|
+
}
|
|
1168
|
+
type ToolExecutor = (name: string, args: Record<string, unknown>, signal?: AbortSignal, hooks?: ToolExecutionHooks) => Promise<string | ToolResult>;
|
|
1022
1169
|
/** 自动运行配置 */
|
|
1023
1170
|
interface AutoRunConfig {
|
|
1024
1171
|
/**
|
|
@@ -1086,345 +1233,547 @@ interface OrchestratorOptions {
|
|
|
1086
1233
|
}
|
|
1087
1234
|
|
|
1088
1235
|
/**
|
|
1089
|
-
*
|
|
1236
|
+
* UnifiedAdapter - 统一适配器
|
|
1090
1237
|
*
|
|
1091
|
-
*
|
|
1092
|
-
* 1. 工具调用循环 (while iterations < MAX)
|
|
1093
|
-
* 2. 消息历史维护 (assistant + tool 消息)
|
|
1094
|
-
* 3. 事件发射 (ChatEvent)
|
|
1095
|
-
* 4. 错误处理
|
|
1238
|
+
* 组合 Protocol + FamilyConfig,产出标准 StreamChunk
|
|
1096
1239
|
*
|
|
1097
|
-
*
|
|
1240
|
+
* 设计原则:
|
|
1241
|
+
* 1. Protocol 只负责 HTTP/SSE 通信,产出 RawEvent
|
|
1242
|
+
* 2. UnifiedAdapter 根据 FamilyConfig 将 RawEvent 转换为 StreamChunk
|
|
1243
|
+
* 3. 所有模型都使用同一个 UnifiedAdapter,行为差异由 FamilyConfig 决定
|
|
1244
|
+
* 4. 实现 ProviderAdapter 接口,可与现有 Orchestrator 无缝集成
|
|
1098
1245
|
*/
|
|
1099
1246
|
|
|
1100
1247
|
/**
|
|
1101
|
-
*
|
|
1102
|
-
*
|
|
1103
|
-
* 核心职责:统一处理工具调用循环,所有 Provider 共享相同的逻辑
|
|
1248
|
+
* UnifiedAdapter 配置
|
|
1104
1249
|
*/
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1250
|
+
interface UnifiedAdapterConfig {
|
|
1251
|
+
/** ARK API Key(豆包/DeepSeek) */
|
|
1252
|
+
arkApiKey?: string;
|
|
1253
|
+
arkApiUrl?: string;
|
|
1254
|
+
/** Qwen API Key(通义千问) */
|
|
1255
|
+
qwenApiKey?: string;
|
|
1256
|
+
qwenApiUrl?: string;
|
|
1257
|
+
/** Gemini API Key */
|
|
1258
|
+
geminiApiKey?: string;
|
|
1259
|
+
geminiApiUrl?: string;
|
|
1260
|
+
/** OpenRouter API Key */
|
|
1261
|
+
openrouterApiKey?: string;
|
|
1262
|
+
openrouterApiUrl?: string;
|
|
1263
|
+
/** Vercel API Key(用于 Vercel AI Gateway 访问 Claude) */
|
|
1264
|
+
vercelApiKey?: string;
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* 流式调用选项
|
|
1268
|
+
*/
|
|
1269
|
+
interface StreamOptions {
|
|
1270
|
+
/** 模型 ID */
|
|
1271
|
+
model: string;
|
|
1272
|
+
/** 是否启用 thinking */
|
|
1273
|
+
enableThinking?: boolean;
|
|
1274
|
+
/** 是否启用搜索 */
|
|
1275
|
+
enableSearch?: boolean;
|
|
1276
|
+
/** 中断信号 */
|
|
1277
|
+
signal: AbortSignal;
|
|
1278
|
+
}
|
|
1279
|
+
/**
|
|
1280
|
+
* 统一适配器
|
|
1281
|
+
*
|
|
1282
|
+
* 实现 ProviderAdapter 接口,根据模型自动选择 Protocol,并根据 FamilyConfig 转换事件
|
|
1283
|
+
*/
|
|
1284
|
+
declare class UnifiedAdapter implements ProviderAdapter {
|
|
1285
|
+
readonly name = "unified";
|
|
1286
|
+
/** 支持的模型列表(从 Registry 获取) */
|
|
1287
|
+
get supportedModels(): string[];
|
|
1288
|
+
private protocols;
|
|
1289
|
+
constructor(config: UnifiedAdapterConfig);
|
|
1108
1290
|
/**
|
|
1109
|
-
*
|
|
1291
|
+
* 检查是否支持指定模型
|
|
1292
|
+
*/
|
|
1293
|
+
supportsModel(model: string): boolean;
|
|
1294
|
+
/**
|
|
1295
|
+
* 获取模型的家族配置
|
|
1296
|
+
*/
|
|
1297
|
+
getModelFamilyConfig(model: string): ModelFamilyConfig | undefined;
|
|
1298
|
+
/**
|
|
1299
|
+
* 实现 ProviderAdapter.streamOnce 接口
|
|
1110
1300
|
*
|
|
1111
|
-
* @param
|
|
1112
|
-
* @param
|
|
1113
|
-
* @param
|
|
1114
|
-
* @
|
|
1301
|
+
* @param messages 标准消息列表
|
|
1302
|
+
* @param tools 工具定义
|
|
1303
|
+
* @param options 调用选项
|
|
1304
|
+
* @returns 标准 StreamChunk 流
|
|
1115
1305
|
*/
|
|
1116
|
-
|
|
1306
|
+
streamOnce(messages: StandardMessage[], tools: SimpleToolDefinition[], options: StreamOnceOptions): AsyncGenerator<StreamChunk>;
|
|
1117
1307
|
/**
|
|
1118
|
-
*
|
|
1308
|
+
* 流式调用(内部方法)
|
|
1119
1309
|
*/
|
|
1120
|
-
private
|
|
1310
|
+
private stream;
|
|
1311
|
+
/**
|
|
1312
|
+
* 将 RawEvent 转换为 StreamChunk
|
|
1313
|
+
*
|
|
1314
|
+
* 根据 FamilyConfig 处理行为差异
|
|
1315
|
+
*/
|
|
1316
|
+
private transformEvents;
|
|
1121
1317
|
}
|
|
1122
1318
|
/**
|
|
1123
|
-
* 创建
|
|
1319
|
+
* 创建 UnifiedAdapter
|
|
1124
1320
|
*/
|
|
1125
|
-
declare function
|
|
1321
|
+
declare function createUnifiedAdapter(config: UnifiedAdapterConfig): UnifiedAdapter;
|
|
1126
1322
|
|
|
1127
1323
|
/**
|
|
1128
|
-
*
|
|
1324
|
+
* Protocol Layer 类型定义
|
|
1129
1325
|
*
|
|
1130
|
-
*
|
|
1131
|
-
*
|
|
1132
|
-
*
|
|
1133
|
-
*
|
|
1326
|
+
* Protocol 只负责:
|
|
1327
|
+
* 1. HTTP 请求发送
|
|
1328
|
+
* 2. SSE 流解析
|
|
1329
|
+
* 3. 认证处理
|
|
1330
|
+
*
|
|
1331
|
+
* 不负责:
|
|
1332
|
+
* - thinking 格式转换(由 Family 处理)
|
|
1333
|
+
* - 搜索行为差异(由 Family 处理)
|
|
1334
|
+
* - 工具调用格式差异(由 Family 处理)
|
|
1335
|
+
*/
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* Protocol 原始事件类型
|
|
1339
|
+
*
|
|
1340
|
+
* 这是 Protocol 层产出的"原始"事件,
|
|
1341
|
+
* 后续由 FamilyBehavior 转换为标准 StreamChunk
|
|
1342
|
+
*/
|
|
1343
|
+
type RawEventType = 'text_delta' | 'thinking_delta' | 'thinking_done' | 'tool_call_start' | 'tool_call_delta' | 'tool_call_done' | 'search_query' | 'search_result' | 'done' | 'error';
|
|
1344
|
+
/**
|
|
1345
|
+
* 原始工具调用
|
|
1346
|
+
*/
|
|
1347
|
+
interface RawToolCall {
|
|
1348
|
+
id: string;
|
|
1349
|
+
name: string;
|
|
1350
|
+
arguments: string;
|
|
1351
|
+
/** Gemini 需要的 thoughtSignature(用于工具调用循环) */
|
|
1352
|
+
thoughtSignature?: string;
|
|
1353
|
+
}
|
|
1354
|
+
/**
|
|
1355
|
+
* Protocol 消息中的工具调用
|
|
1356
|
+
*/
|
|
1357
|
+
interface ProtocolToolCall {
|
|
1358
|
+
id: string;
|
|
1359
|
+
name: string;
|
|
1360
|
+
arguments: string;
|
|
1361
|
+
/** Gemini 需要的 thoughtSignature */
|
|
1362
|
+
thoughtSignature?: string;
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* 原始搜索结果
|
|
1366
|
+
*/
|
|
1367
|
+
interface RawSearchResult {
|
|
1368
|
+
title: string;
|
|
1369
|
+
url: string;
|
|
1370
|
+
snippet: string;
|
|
1371
|
+
}
|
|
1372
|
+
/**
|
|
1373
|
+
* Protocol 原始事件
|
|
1374
|
+
*/
|
|
1375
|
+
interface RawEvent {
|
|
1376
|
+
type: RawEventType;
|
|
1377
|
+
delta?: string;
|
|
1378
|
+
toolCall?: Partial<RawToolCall>;
|
|
1379
|
+
searchQuery?: string;
|
|
1380
|
+
searchResults?: RawSearchResult[];
|
|
1381
|
+
finishReason?: 'stop' | 'tool_calls' | 'length' | 'error';
|
|
1382
|
+
error?: string;
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* Protocol 请求消息(标准化格式)
|
|
1386
|
+
*/
|
|
1387
|
+
interface ProtocolMessage {
|
|
1388
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
1389
|
+
content: string;
|
|
1390
|
+
/** 图片列表(base64 或 URL) */
|
|
1391
|
+
images?: string[];
|
|
1392
|
+
/** 工具调用(assistant 消息) */
|
|
1393
|
+
toolCalls?: ProtocolToolCall[];
|
|
1394
|
+
/** 工具调用 ID(tool 消息) */
|
|
1395
|
+
toolCallId?: string;
|
|
1396
|
+
/** 工具名称(tool 消息,Gemini 需要) */
|
|
1397
|
+
toolName?: string;
|
|
1398
|
+
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Protocol 工具定义(简化格式)
|
|
1401
|
+
*/
|
|
1402
|
+
interface ProtocolToolDefinition {
|
|
1403
|
+
name: string;
|
|
1404
|
+
description: string;
|
|
1405
|
+
parameters: {
|
|
1406
|
+
type: 'object';
|
|
1407
|
+
properties: Record<string, {
|
|
1408
|
+
type: string;
|
|
1409
|
+
description: string;
|
|
1410
|
+
enum?: string[];
|
|
1411
|
+
items?: {
|
|
1412
|
+
type: string;
|
|
1413
|
+
};
|
|
1414
|
+
}>;
|
|
1415
|
+
required: string[];
|
|
1416
|
+
};
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Protocol 请求选项
|
|
1420
|
+
*/
|
|
1421
|
+
interface ProtocolRequestOptions {
|
|
1422
|
+
/** 模型 ID */
|
|
1423
|
+
model: string;
|
|
1424
|
+
/** 模型家族配置 */
|
|
1425
|
+
familyConfig: ModelFamilyConfig;
|
|
1426
|
+
/** 是否启用 thinking */
|
|
1427
|
+
enableThinking: boolean;
|
|
1428
|
+
/** 是否启用搜索 */
|
|
1429
|
+
enableSearch: boolean;
|
|
1430
|
+
/** 中断信号 */
|
|
1431
|
+
signal: AbortSignal;
|
|
1432
|
+
}
|
|
1433
|
+
/**
|
|
1434
|
+
* Protocol 配置
|
|
1134
1435
|
*/
|
|
1436
|
+
interface ProtocolConfig {
|
|
1437
|
+
apiKey: string;
|
|
1438
|
+
apiUrl?: string;
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* Protocol 接口
|
|
1442
|
+
*
|
|
1443
|
+
* 只负责 HTTP/SSE 通信,不处理任何业务逻辑
|
|
1444
|
+
*/
|
|
1445
|
+
interface Protocol {
|
|
1446
|
+
/** 协议名称 */
|
|
1447
|
+
readonly name: string;
|
|
1448
|
+
/**
|
|
1449
|
+
* 发送请求并返回原始事件流
|
|
1450
|
+
*
|
|
1451
|
+
* @param messages 消息列表
|
|
1452
|
+
* @param tools 工具定义
|
|
1453
|
+
* @param options 请求选项
|
|
1454
|
+
* @returns 原始事件流
|
|
1455
|
+
*/
|
|
1456
|
+
stream(messages: ProtocolMessage[], tools: ProtocolToolDefinition[], options: ProtocolRequestOptions): AsyncGenerator<RawEvent>;
|
|
1457
|
+
}
|
|
1135
1458
|
|
|
1136
|
-
/** ARK 支持的模型 */
|
|
1137
|
-
declare const ARK_MODELS: string[];
|
|
1138
1459
|
/**
|
|
1139
|
-
* ARK
|
|
1460
|
+
* ARK Protocol(火山引擎 Responses API)
|
|
1140
1461
|
*
|
|
1141
|
-
*
|
|
1462
|
+
* 只负责:
|
|
1463
|
+
* - HTTP 请求发送
|
|
1464
|
+
* - SSE 流解析
|
|
1465
|
+
* - 原始事件产出
|
|
1142
1466
|
*/
|
|
1143
|
-
|
|
1467
|
+
|
|
1468
|
+
/**
|
|
1469
|
+
* ARK Protocol 实现
|
|
1470
|
+
*/
|
|
1471
|
+
declare class ArkProtocol implements Protocol {
|
|
1144
1472
|
readonly name = "ark";
|
|
1145
|
-
readonly supportedModels: string[];
|
|
1146
1473
|
private apiKey;
|
|
1147
1474
|
private apiUrl;
|
|
1148
|
-
constructor(config:
|
|
1149
|
-
supportsModel(model: string): boolean;
|
|
1475
|
+
constructor(config: ProtocolConfig);
|
|
1150
1476
|
/**
|
|
1151
|
-
*
|
|
1477
|
+
* 发送请求并返回原始事件流
|
|
1152
1478
|
*/
|
|
1153
|
-
|
|
1479
|
+
stream(messages: ProtocolMessage[], tools: ProtocolToolDefinition[], options: ProtocolRequestOptions): AsyncGenerator<RawEvent>;
|
|
1154
1480
|
/**
|
|
1155
|
-
*
|
|
1481
|
+
* 构建请求体
|
|
1482
|
+
*/
|
|
1483
|
+
private buildRequestBody;
|
|
1484
|
+
/**
|
|
1485
|
+
* 转换消息格式
|
|
1156
1486
|
*/
|
|
1157
1487
|
private convertMessages;
|
|
1158
1488
|
/**
|
|
1159
|
-
* 解析
|
|
1489
|
+
* 解析 SSE 流
|
|
1160
1490
|
*/
|
|
1161
|
-
private
|
|
1491
|
+
private parseSSE;
|
|
1162
1492
|
}
|
|
1163
1493
|
/**
|
|
1164
|
-
* 创建 ARK
|
|
1494
|
+
* 创建 ARK Protocol
|
|
1165
1495
|
*/
|
|
1166
|
-
declare function
|
|
1496
|
+
declare function createArkProtocol(config: ProtocolConfig): ArkProtocol;
|
|
1167
1497
|
|
|
1168
1498
|
/**
|
|
1169
|
-
*
|
|
1499
|
+
* DeepSeek Protocol(通过火山引擎 ARK API 访问)
|
|
1170
1500
|
*
|
|
1171
|
-
*
|
|
1172
|
-
* -
|
|
1173
|
-
* -
|
|
1174
|
-
* -
|
|
1501
|
+
* 特点:
|
|
1502
|
+
* - 支持 reasoning(深度思考)
|
|
1503
|
+
* - 支持 web_search 原生搜索
|
|
1504
|
+
* - 与豆包共用 ARK API,但行为可能有差异
|
|
1175
1505
|
*/
|
|
1176
1506
|
|
|
1177
|
-
/** OpenRouter 支持的模型 */
|
|
1178
|
-
declare const OPENROUTER_MODELS: string[];
|
|
1179
1507
|
/**
|
|
1180
|
-
*
|
|
1181
|
-
*
|
|
1182
|
-
* 使用 OpenAI 兼容 API 格式
|
|
1508
|
+
* DeepSeek Protocol 实现
|
|
1183
1509
|
*/
|
|
1184
|
-
declare class
|
|
1185
|
-
readonly name = "
|
|
1186
|
-
readonly supportedModels: string[];
|
|
1510
|
+
declare class DeepSeekProtocol implements Protocol {
|
|
1511
|
+
readonly name = "deepseek";
|
|
1187
1512
|
private apiKey;
|
|
1188
1513
|
private apiUrl;
|
|
1189
|
-
constructor(config:
|
|
1190
|
-
|
|
1191
|
-
/**
|
|
1192
|
-
* 单次流式调用
|
|
1193
|
-
*/
|
|
1194
|
-
streamOnce(messages: StandardMessage[], tools: SimpleToolDefinition[], options: StreamOnceOptions): AsyncGenerator<StreamChunk>;
|
|
1514
|
+
constructor(config: ProtocolConfig);
|
|
1515
|
+
stream(messages: ProtocolMessage[], tools: ProtocolToolDefinition[], options: ProtocolRequestOptions): AsyncGenerator<RawEvent>;
|
|
1195
1516
|
/**
|
|
1196
|
-
*
|
|
1517
|
+
* 构建请求体(DeepSeek 专用)
|
|
1197
1518
|
*/
|
|
1519
|
+
private buildRequestBody;
|
|
1198
1520
|
private convertMessages;
|
|
1199
1521
|
/**
|
|
1200
|
-
* 解析
|
|
1522
|
+
* 解析 SSE 流(DeepSeek 专用)
|
|
1201
1523
|
*/
|
|
1202
|
-
private
|
|
1524
|
+
private parseSSE;
|
|
1203
1525
|
}
|
|
1204
|
-
|
|
1205
|
-
* 创建 OpenRouter Adapter
|
|
1206
|
-
*/
|
|
1207
|
-
declare function createOpenRouterAdapter(config: AdapterConfig): OpenRouterAdapter;
|
|
1526
|
+
declare function createDeepSeekProtocol(config: ProtocolConfig): DeepSeekProtocol;
|
|
1208
1527
|
|
|
1209
1528
|
/**
|
|
1210
|
-
* Qwen
|
|
1529
|
+
* Qwen Protocol(通义千问 OpenAI 兼容模式 API)
|
|
1211
1530
|
*
|
|
1212
|
-
*
|
|
1213
|
-
* -
|
|
1214
|
-
* - 调用 API 并解析响应为标准 StreamChunk
|
|
1215
|
-
* - 不处理工具执行和消息历史
|
|
1531
|
+
* 使用 OpenAI 兼容端点以支持工具调用:
|
|
1532
|
+
* https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
|
|
1216
1533
|
*/
|
|
1217
1534
|
|
|
1218
|
-
/** Qwen 支持的模型 */
|
|
1219
|
-
declare const QWEN_MODELS: string[];
|
|
1220
1535
|
/**
|
|
1221
|
-
* Qwen
|
|
1222
|
-
*
|
|
1223
|
-
* 使用 DashScope 原生协议
|
|
1536
|
+
* Qwen Protocol 实现(OpenAI 兼容模式)
|
|
1224
1537
|
*/
|
|
1225
|
-
declare class
|
|
1538
|
+
declare class QwenProtocol implements Protocol {
|
|
1226
1539
|
readonly name = "qwen";
|
|
1227
|
-
readonly supportedModels: string[];
|
|
1228
1540
|
private apiKey;
|
|
1229
1541
|
private apiUrl;
|
|
1230
|
-
constructor(config:
|
|
1231
|
-
supportsModel(model: string): boolean;
|
|
1542
|
+
constructor(config: ProtocolConfig);
|
|
1232
1543
|
/**
|
|
1233
|
-
*
|
|
1544
|
+
* 发送请求并返回原始事件流
|
|
1234
1545
|
*/
|
|
1235
|
-
|
|
1546
|
+
stream(messages: ProtocolMessage[], tools: ProtocolToolDefinition[], options: ProtocolRequestOptions): AsyncGenerator<RawEvent>;
|
|
1236
1547
|
/**
|
|
1237
|
-
*
|
|
1548
|
+
* 构建请求体(OpenAI 格式)
|
|
1549
|
+
*/
|
|
1550
|
+
private buildRequestBody;
|
|
1551
|
+
/**
|
|
1552
|
+
* 转换消息格式(OpenAI 标准格式)
|
|
1238
1553
|
*/
|
|
1239
1554
|
private convertMessages;
|
|
1240
1555
|
/**
|
|
1241
|
-
* 解析
|
|
1556
|
+
* 解析 SSE 流(OpenAI 格式)
|
|
1242
1557
|
*/
|
|
1243
|
-
private
|
|
1558
|
+
private parseSSE;
|
|
1244
1559
|
}
|
|
1245
1560
|
/**
|
|
1246
|
-
* 创建 Qwen
|
|
1561
|
+
* 创建 Qwen Protocol
|
|
1247
1562
|
*/
|
|
1248
|
-
declare function
|
|
1563
|
+
declare function createQwenProtocol(config: ProtocolConfig): QwenProtocol;
|
|
1249
1564
|
|
|
1250
1565
|
/**
|
|
1251
|
-
* Gemini
|
|
1566
|
+
* Gemini Protocol(Google Gemini API)
|
|
1252
1567
|
*
|
|
1253
|
-
*
|
|
1254
|
-
* -
|
|
1255
|
-
* -
|
|
1256
|
-
* -
|
|
1568
|
+
* 只负责:
|
|
1569
|
+
* - HTTP 请求发送
|
|
1570
|
+
* - SSE 流解析
|
|
1571
|
+
* - 原始事件产出
|
|
1257
1572
|
*/
|
|
1258
1573
|
|
|
1259
|
-
/** Gemini 支持的模型 */
|
|
1260
|
-
declare const GEMINI_MODELS: string[];
|
|
1261
|
-
/** Gemini Adapter 配置 */
|
|
1262
|
-
interface GeminiAdapterConfig {
|
|
1263
|
-
apiKey: string;
|
|
1264
|
-
}
|
|
1265
1574
|
/**
|
|
1266
|
-
* Gemini
|
|
1267
|
-
*
|
|
1268
|
-
* 使用 @google/genai SDK
|
|
1575
|
+
* Gemini Protocol 实现
|
|
1269
1576
|
*/
|
|
1270
|
-
declare class
|
|
1577
|
+
declare class GeminiProtocol implements Protocol {
|
|
1271
1578
|
readonly name = "gemini";
|
|
1272
|
-
readonly supportedModels: string[];
|
|
1273
|
-
private client;
|
|
1274
1579
|
private apiKey;
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
private getClient;
|
|
1278
|
-
supportsModel(model: string): boolean;
|
|
1580
|
+
private apiUrl;
|
|
1581
|
+
constructor(config: ProtocolConfig);
|
|
1279
1582
|
/**
|
|
1280
|
-
*
|
|
1583
|
+
* 发送请求并返回原始事件流
|
|
1281
1584
|
*/
|
|
1282
|
-
|
|
1585
|
+
stream(messages: ProtocolMessage[], tools: ProtocolToolDefinition[], options: ProtocolRequestOptions): AsyncGenerator<RawEvent>;
|
|
1283
1586
|
/**
|
|
1284
|
-
*
|
|
1587
|
+
* 构建请求体
|
|
1588
|
+
*/
|
|
1589
|
+
private buildRequestBody;
|
|
1590
|
+
/**
|
|
1591
|
+
* 转换消息格式
|
|
1285
1592
|
*/
|
|
1286
1593
|
private convertMessages;
|
|
1594
|
+
/**
|
|
1595
|
+
* 解析 SSE 流
|
|
1596
|
+
*/
|
|
1597
|
+
private parseSSE;
|
|
1287
1598
|
}
|
|
1288
1599
|
/**
|
|
1289
|
-
* 创建 Gemini
|
|
1600
|
+
* 创建 Gemini Protocol
|
|
1290
1601
|
*/
|
|
1291
|
-
declare function
|
|
1292
|
-
|
|
1293
|
-
declare const DEFAULT_MODEL: string;
|
|
1602
|
+
declare function createGeminiProtocol(config: ProtocolConfig): GeminiProtocol;
|
|
1294
1603
|
|
|
1295
1604
|
/**
|
|
1296
|
-
*
|
|
1297
|
-
*/
|
|
1298
|
-
|
|
1299
|
-
/**
|
|
1300
|
-
* 创建默认工具执行器(Node.js 环境)
|
|
1605
|
+
* OpenAI Protocol(通过 OpenRouter 访问 OpenAI/GPT 模型)
|
|
1301
1606
|
*
|
|
1302
|
-
*
|
|
1303
|
-
*
|
|
1304
|
-
|
|
1305
|
-
declare function createDefaultToolExecutor(defaultCwd?: string): ToolExecutor$1;
|
|
1306
|
-
|
|
1307
|
-
/**
|
|
1308
|
-
* 获取当前工作目录工具(Vite 插件风格)
|
|
1309
|
-
*/
|
|
1310
|
-
|
|
1311
|
-
declare function getCwdTool(): ToolPlugin;
|
|
1312
|
-
|
|
1313
|
-
/**
|
|
1314
|
-
* 获取平台信息工具(Vite 插件风格)
|
|
1607
|
+
* 特点:
|
|
1608
|
+
* - GPT-5.x 支持 reasoning(思考模式)
|
|
1609
|
+
* - 支持工具调用
|
|
1315
1610
|
*/
|
|
1316
1611
|
|
|
1317
|
-
declare function getPlatformTool(): ToolPlugin;
|
|
1318
|
-
|
|
1319
1612
|
/**
|
|
1320
|
-
*
|
|
1613
|
+
* OpenAI Protocol 实现(通过 OpenRouter)
|
|
1321
1614
|
*/
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1615
|
+
declare class OpenAIProtocol implements Protocol {
|
|
1616
|
+
readonly name = "openai";
|
|
1617
|
+
private apiKey;
|
|
1618
|
+
private apiUrl;
|
|
1619
|
+
constructor(config: ProtocolConfig);
|
|
1620
|
+
stream(messages: ProtocolMessage[], tools: ProtocolToolDefinition[], options: ProtocolRequestOptions): AsyncGenerator<RawEvent>;
|
|
1621
|
+
/**
|
|
1622
|
+
* 构建请求体(OpenAI/GPT 专用)
|
|
1623
|
+
*/
|
|
1624
|
+
private buildRequestBody;
|
|
1625
|
+
private convertMessages;
|
|
1626
|
+
/**
|
|
1627
|
+
* 解析 SSE 流(OpenAI/GPT 专用)
|
|
1628
|
+
*/
|
|
1629
|
+
private parseSSE;
|
|
1630
|
+
}
|
|
1631
|
+
declare function createOpenAIProtocol(config: ProtocolConfig): OpenAIProtocol;
|
|
1330
1632
|
|
|
1331
1633
|
/**
|
|
1332
|
-
*
|
|
1634
|
+
* Anthropic Protocol(通过 Vercel AI Gateway)
|
|
1635
|
+
*
|
|
1636
|
+
* 特点:
|
|
1637
|
+
* - 使用 Vercel AI SDK 的 createGateway
|
|
1638
|
+
* - 支持 extended thinking
|
|
1639
|
+
* - 支持工具调用
|
|
1640
|
+
* - 自动处理流式输出
|
|
1641
|
+
*
|
|
1642
|
+
* 参考: https://vercel.com/docs/ai-gateway
|
|
1333
1643
|
*/
|
|
1334
1644
|
|
|
1335
|
-
declare function generateImageTool(): ToolPlugin;
|
|
1336
|
-
|
|
1337
1645
|
/**
|
|
1338
|
-
*
|
|
1646
|
+
* Anthropic Protocol 实现(通过 Vercel AI Gateway)
|
|
1339
1647
|
*/
|
|
1340
|
-
|
|
1341
|
-
|
|
1648
|
+
declare class AnthropicProtocol implements Protocol {
|
|
1649
|
+
readonly name = "anthropic";
|
|
1650
|
+
private gateway;
|
|
1651
|
+
constructor(config: ProtocolConfig);
|
|
1652
|
+
stream(messages: ProtocolMessage[], tools: ProtocolToolDefinition[], options: ProtocolRequestOptions): AsyncGenerator<RawEvent>;
|
|
1653
|
+
/**
|
|
1654
|
+
* 转换消息格式(ProtocolMessage → AI SDK CoreMessage)
|
|
1655
|
+
* 简化版:主要处理文本消息,工具调用通过历史上下文传递
|
|
1656
|
+
*/
|
|
1657
|
+
private convertMessages;
|
|
1658
|
+
/**
|
|
1659
|
+
* 转换工具格式(ProtocolToolDefinition → AI SDK tools)
|
|
1660
|
+
* 注意:Vercel Gateway 对工具格式有特殊要求,这里使用标准格式
|
|
1661
|
+
*/
|
|
1662
|
+
private convertTools;
|
|
1663
|
+
/**
|
|
1664
|
+
* 将 JSON Schema 转换为 Zod Schema
|
|
1665
|
+
*/
|
|
1666
|
+
private jsonSchemaToZod;
|
|
1667
|
+
/**
|
|
1668
|
+
* 解析 AI SDK 流式响应
|
|
1669
|
+
*/
|
|
1670
|
+
private parseStream;
|
|
1671
|
+
}
|
|
1672
|
+
declare function createAnthropicProtocol(config: ProtocolConfig): AnthropicProtocol;
|
|
1342
1673
|
|
|
1343
1674
|
/**
|
|
1344
|
-
*
|
|
1675
|
+
* Chat Orchestrator
|
|
1345
1676
|
*
|
|
1346
|
-
*
|
|
1347
|
-
*
|
|
1348
|
-
*
|
|
1349
|
-
*
|
|
1677
|
+
* 统一处理:
|
|
1678
|
+
* 1. 工具调用循环 (while iterations < MAX)
|
|
1679
|
+
* 2. 消息历史维护 (assistant + tool 消息)
|
|
1680
|
+
* 3. 事件发射 (ChatEvent)
|
|
1681
|
+
* 4. 错误处理
|
|
1350
1682
|
*
|
|
1351
|
-
*
|
|
1352
|
-
*/
|
|
1353
|
-
|
|
1354
|
-
declare function getWeatherTool(): ToolPlugin;
|
|
1355
|
-
|
|
1356
|
-
/**
|
|
1357
|
-
* 显示 Toast 通知
|
|
1683
|
+
* 所有 Provider 通过此类执行,确保行为一致
|
|
1358
1684
|
*/
|
|
1359
|
-
declare function showToastTool(): ToolPlugin;
|
|
1360
1685
|
|
|
1361
1686
|
/**
|
|
1362
|
-
*
|
|
1687
|
+
* Chat Orchestrator
|
|
1688
|
+
*
|
|
1689
|
+
* 核心职责:统一处理工具调用循环,所有 Provider 共享相同的逻辑
|
|
1363
1690
|
*/
|
|
1364
|
-
declare
|
|
1365
|
-
|
|
1691
|
+
declare class ChatOrchestrator {
|
|
1692
|
+
private config;
|
|
1693
|
+
constructor(config: OrchestratorConfig);
|
|
1694
|
+
/**
|
|
1695
|
+
* 执行聊天
|
|
1696
|
+
*
|
|
1697
|
+
* @param adapter Provider 适配器
|
|
1698
|
+
* @param message 用户消息
|
|
1699
|
+
* @param context Orchestrator 上下文
|
|
1700
|
+
* @param options 选项
|
|
1701
|
+
*/
|
|
1702
|
+
chat(adapter: ProviderAdapter, message: string, context: OrchestratorContext, options: OrchestratorOptions): AsyncGenerator<ChatEvent>;
|
|
1703
|
+
/**
|
|
1704
|
+
* 构建标准化消息列表
|
|
1705
|
+
*/
|
|
1706
|
+
private buildMessages;
|
|
1707
|
+
}
|
|
1366
1708
|
/**
|
|
1367
|
-
*
|
|
1709
|
+
* 创建 Orchestrator 实例
|
|
1368
1710
|
*/
|
|
1369
|
-
declare function
|
|
1711
|
+
declare function createOrchestrator(config: OrchestratorConfig): ChatOrchestrator;
|
|
1370
1712
|
|
|
1371
|
-
|
|
1372
|
-
* 跳转到指定目录(由宿主 UI 消费 ui:navigate 副作用实现)
|
|
1373
|
-
*/
|
|
1374
|
-
declare function navigateToDirectoryTool(): ToolPlugin;
|
|
1713
|
+
declare const DEFAULT_MODEL: string;
|
|
1375
1714
|
|
|
1376
1715
|
/**
|
|
1377
|
-
*
|
|
1716
|
+
* 工具执行器
|
|
1378
1717
|
*/
|
|
1379
|
-
declare const uiActionTools: ToolPlugin[];
|
|
1380
1718
|
|
|
1381
1719
|
/**
|
|
1382
|
-
*
|
|
1383
|
-
*
|
|
1384
|
-
* 可选依赖 @huyooo/ai-search
|
|
1385
|
-
* 如果未安装,工具会返回友好提示
|
|
1720
|
+
* 创建默认工具执行器(Node.js 环境)
|
|
1386
1721
|
*
|
|
1387
|
-
*
|
|
1722
|
+
* 用于执行 shell 命令,包含基本安全检查
|
|
1723
|
+
* 支持通过 AbortSignal 取消命令执行
|
|
1388
1724
|
*/
|
|
1725
|
+
declare function createDefaultToolExecutor(defaultCwd?: string): ToolExecutor$1;
|
|
1389
1726
|
|
|
1390
1727
|
/**
|
|
1391
|
-
*
|
|
1392
|
-
*/
|
|
1393
|
-
interface SearchToolsOptions {
|
|
1394
|
-
/** 搜索数据存储目录 */
|
|
1395
|
-
dataDir?: string;
|
|
1396
|
-
/** 默认索引目录(首次使用时自动索引) */
|
|
1397
|
-
defaultDirectories?: string[];
|
|
1398
|
-
/** 是否在初始化时自动索引 */
|
|
1399
|
-
autoIndex?: boolean;
|
|
1400
|
-
}
|
|
1401
|
-
/**
|
|
1402
|
-
* 创建文档搜索工具
|
|
1403
|
-
*
|
|
1404
|
-
* 如果 @huyooo/ai-search 未安装,返回占位工具(提示用户安装)
|
|
1728
|
+
* 调试日志工具
|
|
1405
1729
|
*
|
|
1406
|
-
*
|
|
1407
|
-
*
|
|
1408
|
-
*
|
|
1409
|
-
*
|
|
1410
|
-
* const searchTools = await createDocumentSearchTools({
|
|
1411
|
-
* dataDir: './.search-data',
|
|
1412
|
-
* defaultDirectories: ['~/Documents'],
|
|
1413
|
-
* });
|
|
1414
|
-
*
|
|
1415
|
-
* const agent = new HybridAgent({
|
|
1416
|
-
* ...config,
|
|
1417
|
-
* tools: [...otherTools, ...searchTools],
|
|
1418
|
-
* });
|
|
1419
|
-
* ```
|
|
1730
|
+
* 用法:
|
|
1731
|
+
* - 设置环境变量 AI_CHAT_DEBUG=true 启用调试日志
|
|
1732
|
+
* - 设置环境变量 AI_CHAT_DEBUG_FILE=/path/to/log.jsonl 启用文件输出
|
|
1733
|
+
* - 或者调用 DebugLogger.enable() / DebugLogger.setLogFile()
|
|
1420
1734
|
*/
|
|
1421
|
-
declare function createDocumentSearchTools(options?: SearchToolsOptions): Promise<Tool[]>;
|
|
1422
1735
|
/**
|
|
1423
|
-
*
|
|
1736
|
+
* 调试日志工具
|
|
1424
1737
|
*/
|
|
1425
|
-
declare
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1738
|
+
declare const DebugLogger: {
|
|
1739
|
+
/**
|
|
1740
|
+
* 启用/禁用调试日志
|
|
1741
|
+
*/
|
|
1742
|
+
enable(enabled?: boolean): void;
|
|
1743
|
+
/**
|
|
1744
|
+
* 检查是否启用
|
|
1745
|
+
*/
|
|
1746
|
+
isEnabled(): boolean;
|
|
1747
|
+
/**
|
|
1748
|
+
* 直接设置 fs 模块(用于 Electron 主进程等打包环境)
|
|
1749
|
+
*/
|
|
1750
|
+
setFsModule(fs: typeof fs): void;
|
|
1751
|
+
/**
|
|
1752
|
+
* 设置日志文件路径(仅 Node.js 环境有效)
|
|
1753
|
+
* @param path 日志文件路径,null 表示禁用文件输出
|
|
1754
|
+
*/
|
|
1755
|
+
setLogFile(path: string | null): void;
|
|
1756
|
+
/**
|
|
1757
|
+
* 获取日志文件路径
|
|
1758
|
+
*/
|
|
1759
|
+
getLogFile(): string | null;
|
|
1760
|
+
/**
|
|
1761
|
+
* 立即刷新日志到文件
|
|
1762
|
+
*/
|
|
1763
|
+
flush(): void;
|
|
1764
|
+
/**
|
|
1765
|
+
* 创建模块专用的 logger
|
|
1766
|
+
*/
|
|
1767
|
+
module(moduleName: string): {
|
|
1768
|
+
debug: (message: string, data?: unknown) => void;
|
|
1769
|
+
info: (message: string, data?: unknown) => void;
|
|
1770
|
+
warn: (message: string, data?: unknown) => void;
|
|
1771
|
+
error: (message: string, data?: unknown) => void;
|
|
1772
|
+
};
|
|
1773
|
+
debug: (module: string, message: string, data?: unknown) => void;
|
|
1774
|
+
info: (module: string, message: string, data?: unknown) => void;
|
|
1775
|
+
warn: (module: string, message: string, data?: unknown) => void;
|
|
1776
|
+
error: (module: string, message: string, data?: unknown) => void;
|
|
1777
|
+
};
|
|
1429
1778
|
|
|
1430
|
-
export {
|
|
1779
|
+
export { type AbortEvent, type AdapterConfig, type AgentConfig, AnthropicProtocol, ArkProtocol, type AutoRunConfig$1 as AutoRunConfig, type AutoRunMode, CLAUDE_FAMILY, type ChatEvent, type ChatEventType, type ChatMessage, type ChatMode, type ChatOptions, ChatOrchestrator, DEEPSEEK_FAMILY, DEFAULT_MODEL, DOUBAO_FAMILY, DebugLogger, DeepSeekProtocol, type DoneEvent, type ErrorCategory, type ErrorDetails, type ErrorEvent, GEMINI_FAMILY, GPT_FAMILY, GeminiProtocol, HybridAgent, type ImageEvent, MODELS, MODEL_FAMILIES, MODEL_REGISTRY, type MediaEvent, type ModelFamilyConfig, type ModelFamilyId, type ModelOption, type ModelRegistryEntry, OpenAIProtocol, type OrchestratorConfig, type OrchestratorContext, type OrchestratorOptions, type ToolExecutor as OrchestratorToolExecutor, type Protocol, type ProtocolConfig, type ProtocolId, type ProtocolMessage, type ProtocolToolCall, type ProtocolToolDefinition, type ProviderAdapter, type ProviderType, QWEN_FAMILY, QwenProtocol, type RawEvent, type RawEventType, type RawSearchResult, type RawToolCall, type ResponsesApiTool, type RouteResult, type RuntimeConfig, type SearchEndEvent, type SearchEvent, type SearchResult, type SearchResultEvent, type SearchResultItem, type SearchStartEvent, type SearchStrategy, type SideEffect$1 as SideEffect, type SimpleToolDefinition, type StandardMessage, type StatusEvent, type StepEndEvent, type StepEvent, type StepStartEvent, type StreamChunk, type StreamChunkType, type StreamOnceOptions, type StreamOptions, type StreamStartEvent, type TextDeltaEvent, type TextEvent, type ThinkingDeltaEvent, type ThinkingEndEvent, type ThinkingEvent, type ThinkingFormat, type ThinkingMode, type ThinkingStartEvent, type TokenUsage, type Tool, type ToolCallFormat, type ToolCallInfo, type ToolCallRequest, type ToolCallResultEvent, type ToolCallStartEvent, type ToolCallStatus, type ToolConfigItem, type ToolContext, type ToolDefinition, type ToolEvent, type ToolExecutor$1 as ToolExecutor, type ToolPlugin, type ToolResult, UnifiedAdapter, type UnifiedAdapterConfig, type VideoEvent, createAbort, createAnthropicProtocol, createApiError, createArkProtocol, createDeepSeekProtocol, createDefaultToolExecutor, createDone, createError, createGeminiProtocol, createImage, createOpenAIProtocol, createOrchestrator, createParseError, createQwenProtocol, createRateLimitError, createSearchEnd, createSearchResult, createSearchStart, createStepEnd, createStepStart, createStreamStart, createTextDelta, createThinkingDelta, createThinkingEnd, createThinkingStart, createTimeoutError, createToolCallResult, createToolCallStart, createToolError, createUnifiedAdapter, createVideo, getDefaultProvider, getModelByModelId, getModelEntry, getModelFamily, getModelProtocol, getModelSearchStrategy, getModelsByFamily, getModelsByProtocol, getVisibleModels, isAbortEvent, isErrorEvent, isMediaEvent, isModelForProvider, isRetryableError, isSearchEvent, isStatusEvent, isStepEvent, isTextEvent, isThinkingEvent, isToolEvent, modelSupportsNativeSearch, modelSupportsThinking, resolveTools, routeModelToProvider, routeModelWithDetails, tool, tools };
|