@huyooo/ai-chat-core 0.2.38 → 0.2.41
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/events-GWl1-vbT.d.ts +1073 -0
- package/dist/events.d.ts +1 -477
- package/dist/events.js +1 -1
- package/dist/index.d.ts +5 -644
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/agent.ts +15 -11
- package/src/events.ts +20 -66
- package/src/index.ts +14 -5
- package/src/internal/web-search.ts +4 -5
- package/src/mcp/client-manager.ts +2 -1
- package/src/providers/orchestrator.ts +40 -24
- package/src/providers/types.ts +3 -13
- package/src/types.ts +101 -69
|
@@ -0,0 +1,1073 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP (Model Context Protocol) 相关类型定义
|
|
3
|
+
*
|
|
4
|
+
* 支持两种传输方式:
|
|
5
|
+
* - stdio: 启动本地子进程,通过 stdin/stdout 通信(操作本地文件、命令、应用)
|
|
6
|
+
* - sse: 连接远程 HTTP 服务器(调用远程 API/服务)
|
|
7
|
+
*/
|
|
8
|
+
/** MCP Server 配置 */
|
|
9
|
+
interface McpServerConfig {
|
|
10
|
+
/** 服务器唯一标识(用于日志和工具命名空间) */
|
|
11
|
+
name: string;
|
|
12
|
+
/** 传输方式 */
|
|
13
|
+
transport: 'stdio' | 'sse';
|
|
14
|
+
/** stdio 模式:启动命令(如 "npx", "python3", "/usr/local/bin/my-server") */
|
|
15
|
+
command?: string;
|
|
16
|
+
/** stdio 模式:命令参数 */
|
|
17
|
+
args?: string[];
|
|
18
|
+
/** stdio 模式:环境变量 */
|
|
19
|
+
env?: Record<string, string>;
|
|
20
|
+
/** stdio 模式:工作目录 */
|
|
21
|
+
cwd?: string;
|
|
22
|
+
/** sse 模式:服务器 URL */
|
|
23
|
+
url?: string;
|
|
24
|
+
}
|
|
25
|
+
/** MCP 连接状态 */
|
|
26
|
+
type McpConnectionStatus = 'connecting' | 'connected' | 'disconnected' | 'error';
|
|
27
|
+
/** 单个 MCP 连接信息 */
|
|
28
|
+
interface McpConnectionInfo {
|
|
29
|
+
/** 服务器名称 */
|
|
30
|
+
name: string;
|
|
31
|
+
/** 连接状态 */
|
|
32
|
+
status: McpConnectionStatus;
|
|
33
|
+
/** 提供的工具数量 */
|
|
34
|
+
toolCount: number;
|
|
35
|
+
/** 错误信息(status === 'error' 时) */
|
|
36
|
+
error?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 模型注册表
|
|
41
|
+
*
|
|
42
|
+
* 核心设计:Protocol + Family 分离
|
|
43
|
+
* - Protocol:负责 API 通信协议(HTTP/SSE/认证)
|
|
44
|
+
* - Family:负责模型行为差异(thinking格式、搜索方式、工具格式)
|
|
45
|
+
*
|
|
46
|
+
* 这样设计的好处:
|
|
47
|
+
* 1. 同协议不同行为的模型可以复用协议层(如 ARK 上的豆包和 DeepSeek)
|
|
48
|
+
* 2. 新增模型只需配置 family,无需修改 adapter 代码
|
|
49
|
+
* 3. 行为差异集中管理,易于维护
|
|
50
|
+
*/
|
|
51
|
+
/** 模型家族 ID */
|
|
52
|
+
type ModelFamilyId = 'doubao' | 'deepseek' | 'qwen' | 'gemini' | 'gpt' | 'claude';
|
|
53
|
+
/** 协议类型 */
|
|
54
|
+
type ProtocolId = 'ark' | 'deepseek' | 'qwen' | 'gemini' | 'openai' | 'anthropic';
|
|
55
|
+
/** Thinking 输出格式 */
|
|
56
|
+
type ThinkingFormat = 'reasoning' | 'thinking_enabled' | 'thought_signature' | 'none';
|
|
57
|
+
/**
|
|
58
|
+
* 搜索实现方式(谁提供搜索、行为是否由我们统一)
|
|
59
|
+
* - provider_native:厂商原生,协议层各自实现(ARK type:web_search / Gemini googleSearch 等),行为各异
|
|
60
|
+
* - tavily:我们注入 Tavily web_search 工具,行为一致(GPT/Claude/Qwen/Gemini 有工具时)
|
|
61
|
+
*/
|
|
62
|
+
type SearchStrategy = 'provider_native' | 'tavily';
|
|
63
|
+
/** 工具调用格式 */
|
|
64
|
+
type ToolCallFormat = 'responses' | 'openai' | 'gemini';
|
|
65
|
+
/**
|
|
66
|
+
* 模型家族配置
|
|
67
|
+
* 定义同一家族模型的共同行为特征
|
|
68
|
+
*/
|
|
69
|
+
interface ModelFamilyConfig {
|
|
70
|
+
/** 家族 ID */
|
|
71
|
+
id: ModelFamilyId;
|
|
72
|
+
/** 显示名称 */
|
|
73
|
+
displayName: string;
|
|
74
|
+
/** 是否支持图片理解 */
|
|
75
|
+
supportsVision: boolean;
|
|
76
|
+
/** 是否支持 thinking */
|
|
77
|
+
supportsThinking: boolean;
|
|
78
|
+
/** Thinking 输出格式 */
|
|
79
|
+
thinkingFormat: ThinkingFormat;
|
|
80
|
+
/** 是否支持原生搜索 */
|
|
81
|
+
supportsNativeSearch: boolean;
|
|
82
|
+
/** 搜索实现方式 */
|
|
83
|
+
searchStrategy: SearchStrategy;
|
|
84
|
+
/** 工具调用格式 */
|
|
85
|
+
toolCallFormat: ToolCallFormat;
|
|
86
|
+
/** 默认最大输出 token */
|
|
87
|
+
defaultMaxTokens?: number;
|
|
88
|
+
/** 是否需要特殊处理(如 Gemini 的 thought_signature) */
|
|
89
|
+
requiresSpecialHandling?: string[];
|
|
90
|
+
}
|
|
91
|
+
/** 豆包家族(联网搜索统一走 web_search_ai/Tavily,与其它模型事件与数据格式一致) */
|
|
92
|
+
declare const DOUBAO_FAMILY: ModelFamilyConfig;
|
|
93
|
+
/** DeepSeek 家族(联网搜索统一走 web_search_ai/Tavily,与其它模型事件与数据格式一致) */
|
|
94
|
+
declare const DEEPSEEK_FAMILY: ModelFamilyConfig;
|
|
95
|
+
/** 通义千问家族 */
|
|
96
|
+
/** Qwen 家族(使用 Tavily 统一搜索) */
|
|
97
|
+
declare const QWEN_FAMILY: ModelFamilyConfig;
|
|
98
|
+
/** Gemini 家族(注意:googleSearch 不能与其他工具同时使用,有工具时走 Tavily) */
|
|
99
|
+
declare const GEMINI_FAMILY: ModelFamilyConfig;
|
|
100
|
+
/** GPT 家族 */
|
|
101
|
+
declare const GPT_FAMILY: ModelFamilyConfig;
|
|
102
|
+
/** Claude 家族(使用 Vercel AI SDK / @ai-sdk/anthropic) */
|
|
103
|
+
declare const CLAUDE_FAMILY: ModelFamilyConfig;
|
|
104
|
+
/** 家族配置映射 */
|
|
105
|
+
declare const MODEL_FAMILIES: Record<ModelFamilyId, ModelFamilyConfig>;
|
|
106
|
+
/**
|
|
107
|
+
* 模型注册项
|
|
108
|
+
*/
|
|
109
|
+
interface ModelRegistryEntry {
|
|
110
|
+
/** 模型 ID(API 调用用) */
|
|
111
|
+
id: string;
|
|
112
|
+
/** 显示名称 */
|
|
113
|
+
displayName: string;
|
|
114
|
+
/** 所属家族 */
|
|
115
|
+
family: ModelFamilyId;
|
|
116
|
+
/** 使用的协议 */
|
|
117
|
+
protocol: ProtocolId;
|
|
118
|
+
/** 是否在前端显示 */
|
|
119
|
+
visible?: boolean;
|
|
120
|
+
/** 是否支持图片理解(优先级高于 family.supportsVision) */
|
|
121
|
+
supportsVision?: boolean;
|
|
122
|
+
/** 上下文窗口大小(如 "256K") */
|
|
123
|
+
contextWindow?: string;
|
|
124
|
+
/** 价格信息(数组,分行显示) */
|
|
125
|
+
pricing?: string[];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 全局模型注册表
|
|
129
|
+
*
|
|
130
|
+
* 每个模型只有一家供应商,无需分组
|
|
131
|
+
*/
|
|
132
|
+
declare const MODEL_REGISTRY: ModelRegistryEntry[];
|
|
133
|
+
/**
|
|
134
|
+
* 根据模型 ID 获取注册信息
|
|
135
|
+
*/
|
|
136
|
+
declare function getModelEntry(modelId: string): ModelRegistryEntry | undefined;
|
|
137
|
+
/**
|
|
138
|
+
* 根据模型 ID 获取家族配置
|
|
139
|
+
*/
|
|
140
|
+
declare function getModelFamily(modelId: string): ModelFamilyConfig | undefined;
|
|
141
|
+
/**
|
|
142
|
+
* 根据模型 ID 获取协议类型
|
|
143
|
+
*/
|
|
144
|
+
declare function getModelProtocol(modelId: string): ProtocolId | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* 获取所有可见模型(用于前端显示)
|
|
147
|
+
*/
|
|
148
|
+
declare function getVisibleModels(): ModelRegistryEntry[];
|
|
149
|
+
/**
|
|
150
|
+
* 根据家族 ID 获取所有模型
|
|
151
|
+
*/
|
|
152
|
+
declare function getModelsByFamily(familyId: ModelFamilyId): ModelRegistryEntry[];
|
|
153
|
+
/**
|
|
154
|
+
* 根据协议获取所有模型
|
|
155
|
+
*/
|
|
156
|
+
declare function getModelsByProtocol(protocol: ProtocolId): ModelRegistryEntry[];
|
|
157
|
+
/**
|
|
158
|
+
* 检查模型是否支持 thinking
|
|
159
|
+
*/
|
|
160
|
+
declare function modelSupportsThinking(modelId: string): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* 检查模型是否支持原生搜索
|
|
163
|
+
*/
|
|
164
|
+
declare function modelSupportsNativeSearch(modelId: string): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* 获取模型的搜索策略
|
|
167
|
+
*/
|
|
168
|
+
declare function getModelSearchStrategy(modelId: string): SearchStrategy;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 模型路由模块
|
|
172
|
+
*
|
|
173
|
+
* 根据模型 ID 查询对应的 Protocol 和 Family 配置
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
/** Provider 类型 = Protocol 类型 */
|
|
177
|
+
type ProviderType = ProtocolId;
|
|
178
|
+
/** 路由结果 */
|
|
179
|
+
interface RouteResult {
|
|
180
|
+
/** Protocol 类型 */
|
|
181
|
+
provider: ProviderType;
|
|
182
|
+
/** 是否未注册(使用默认 Provider) */
|
|
183
|
+
isDefault: boolean;
|
|
184
|
+
/** 模型注册信息 */
|
|
185
|
+
registryEntry?: ModelRegistryEntry;
|
|
186
|
+
/** 模型家族配置 */
|
|
187
|
+
familyConfig?: ModelFamilyConfig;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 获取模型的 Provider
|
|
191
|
+
*/
|
|
192
|
+
declare function routeModelToProvider(model: string): ProviderType;
|
|
193
|
+
/**
|
|
194
|
+
* 获取模型的完整路由信息
|
|
195
|
+
*/
|
|
196
|
+
declare function routeModelWithDetails(model: string): RouteResult;
|
|
197
|
+
/**
|
|
198
|
+
* 获取默认 Provider
|
|
199
|
+
*/
|
|
200
|
+
declare function getDefaultProvider(): ProviderType;
|
|
201
|
+
/**
|
|
202
|
+
* 检查模型是否属于指定 Provider
|
|
203
|
+
*/
|
|
204
|
+
declare function isModelForProvider(model: string, provider: ProviderType): boolean;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* AI Chat Core 类型定义
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
/** 对话模式 */
|
|
211
|
+
type ChatMode = 'agent' | 'ask';
|
|
212
|
+
|
|
213
|
+
/** 模型选项(前端显示用) */
|
|
214
|
+
interface ModelOption {
|
|
215
|
+
/** 模型 ID(发送给 API) */
|
|
216
|
+
modelId: string;
|
|
217
|
+
/** 显示名称 */
|
|
218
|
+
displayName: string;
|
|
219
|
+
/** 是否支持深度思考 */
|
|
220
|
+
supportsThinking: boolean;
|
|
221
|
+
/** 是否支持图片理解 */
|
|
222
|
+
supportsVision: boolean;
|
|
223
|
+
/** 用于模型项 hover 的特性描述(由后端定义,前端只渲染) */
|
|
224
|
+
tooltip?: {
|
|
225
|
+
features?: string[];
|
|
226
|
+
/** 开销信息(数组,分行显示) */
|
|
227
|
+
cost?: string[];
|
|
228
|
+
description?: string;
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* 预定义模型列表
|
|
233
|
+
*/
|
|
234
|
+
declare const MODELS: ModelOption[];
|
|
235
|
+
/** 根据 modelId 获取模型 */
|
|
236
|
+
declare function getModelByModelId(modelId: string): ModelOption | undefined;
|
|
237
|
+
/** 工具批准回调函数 */
|
|
238
|
+
type ToolApprovalCallback = (toolCall: {
|
|
239
|
+
id: string;
|
|
240
|
+
name: string;
|
|
241
|
+
args: Record<string, unknown>;
|
|
242
|
+
}) => Promise<boolean>;
|
|
243
|
+
/** Agent 配置 */
|
|
244
|
+
interface AgentConfig {
|
|
245
|
+
/** 豆包/火山引擎 API Key (用于豆包和 DeepSeek) */
|
|
246
|
+
arkApiKey: string;
|
|
247
|
+
/** 豆包 API URL */
|
|
248
|
+
arkApiUrl?: string;
|
|
249
|
+
/** 通义千问 API Key */
|
|
250
|
+
qwenApiKey?: string;
|
|
251
|
+
/** 通义千问 API URL */
|
|
252
|
+
qwenApiUrl?: string;
|
|
253
|
+
/** OpenRouter API Key */
|
|
254
|
+
openrouterApiKey?: string;
|
|
255
|
+
/** OpenRouter API URL */
|
|
256
|
+
openrouterApiUrl?: string;
|
|
257
|
+
/** Vercel API Key(用于 Vercel AI Gateway 访问 Claude) */
|
|
258
|
+
vercelApiKey?: string;
|
|
259
|
+
/** Tavily API Key(用于统一 Web Search) */
|
|
260
|
+
tavilyApiKey?: string;
|
|
261
|
+
/** Gemini API Key (用于图片/视频,注意:中国大陆无法直接访问) */
|
|
262
|
+
geminiApiKey: string;
|
|
263
|
+
/** 当前工作目录(Current Working Directory) */
|
|
264
|
+
cwd?: string;
|
|
265
|
+
/**
|
|
266
|
+
* 工具列表(Vite 插件风格)
|
|
267
|
+
*
|
|
268
|
+
* 支持多种形式:
|
|
269
|
+
* - 单个工具:getCwdTool
|
|
270
|
+
* - 工具插件:searchPlugin({ dataDir, workspace })
|
|
271
|
+
* - Promise:await asyncPlugin()
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```typescript
|
|
275
|
+
* tools: [
|
|
276
|
+
* getCwdTool,
|
|
277
|
+
* executeCommandTool,
|
|
278
|
+
* searchPlugin({ dataDir: '/path', workspace: '/project' }),
|
|
279
|
+
* ]
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
tools?: ToolConfigItem[];
|
|
283
|
+
/**
|
|
284
|
+
* 工具批准回调(manual 模式使用)
|
|
285
|
+
* 返回 true 表示批准执行,false 表示跳过
|
|
286
|
+
*/
|
|
287
|
+
onToolApprovalRequest?: ToolApprovalCallback;
|
|
288
|
+
/**
|
|
289
|
+
* 动态获取自动运行配置回调
|
|
290
|
+
* 每次检查工具批准时调用,获取最新配置
|
|
291
|
+
*/
|
|
292
|
+
getAutoRunConfig?: () => Promise<AutoRunConfig | undefined>;
|
|
293
|
+
/**
|
|
294
|
+
* MCP Server 配置列表
|
|
295
|
+
*
|
|
296
|
+
* 初始化时自动连接所有 MCP Server,发现工具并注册到 Agent。
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```typescript
|
|
300
|
+
* mcpServers: [
|
|
301
|
+
* { name: 'filesystem', transport: 'stdio', command: 'npx', args: ['-y', '@modelcontextprotocol/server-filesystem', '/Users/me'] },
|
|
302
|
+
* { name: 'github', transport: 'stdio', command: 'npx', args: ['-y', '@modelcontextprotocol/server-github'], env: { GITHUB_TOKEN: '...' } },
|
|
303
|
+
* { name: 'remote-db', transport: 'sse', url: 'http://localhost:3001/mcp' },
|
|
304
|
+
* ]
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
mcpServers?: McpServerConfig[];
|
|
308
|
+
}
|
|
309
|
+
/** 深度思考模式 */
|
|
310
|
+
type ThinkingMode = 'enabled' | 'disabled';
|
|
311
|
+
/** 自动运行模式 */
|
|
312
|
+
type AutoRunMode = 'run-everything' | 'manual';
|
|
313
|
+
/** 自动运行配置 */
|
|
314
|
+
interface AutoRunConfig {
|
|
315
|
+
/**
|
|
316
|
+
* 自动运行模式
|
|
317
|
+
* - 'run-everything': 运行所有内容(自动执行)
|
|
318
|
+
* - 'manual': 手动批准(每次执行前询问)
|
|
319
|
+
*/
|
|
320
|
+
mode?: AutoRunMode;
|
|
321
|
+
}
|
|
322
|
+
/** 聊天消息格式(用于传递历史) */
|
|
323
|
+
interface ChatHistoryMessage {
|
|
324
|
+
role: 'user' | 'assistant' | 'system' | 'tool';
|
|
325
|
+
content: string;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* 用户工具定义(透传模式)
|
|
329
|
+
*
|
|
330
|
+
* 只包含 schema,不包含 execute 函数
|
|
331
|
+
* 由客户端执行,服务端透传 tool_call_request 事件
|
|
332
|
+
*/
|
|
333
|
+
interface UserToolDefinition {
|
|
334
|
+
/** 工具名称 */
|
|
335
|
+
name: string;
|
|
336
|
+
/** 工具描述(供 AI 理解) */
|
|
337
|
+
description: string;
|
|
338
|
+
/** 参数定义(JSON Schema,支持嵌套) */
|
|
339
|
+
parameters: JsonSchemaObject;
|
|
340
|
+
}
|
|
341
|
+
/** 聊天配置(每次聊天可变的选项) */
|
|
342
|
+
interface ChatOptions {
|
|
343
|
+
/** 对话模式 */
|
|
344
|
+
mode?: ChatMode;
|
|
345
|
+
/** 使用的模型 */
|
|
346
|
+
model?: string;
|
|
347
|
+
/** 模型提供商 */
|
|
348
|
+
provider?: ProviderType;
|
|
349
|
+
/** 是否启用联网搜索 */
|
|
350
|
+
enableWebSearch?: boolean;
|
|
351
|
+
/**
|
|
352
|
+
* 深度思考模式
|
|
353
|
+
* - 'enabled': 强制开启深度思考
|
|
354
|
+
* - 'disabled': 强制关闭深度思考
|
|
355
|
+
*/
|
|
356
|
+
thinkingMode?: ThinkingMode;
|
|
357
|
+
/** 启用的工具名称列表(agent 模式有效,ask 模式强制禁用) */
|
|
358
|
+
enabledTools?: string[];
|
|
359
|
+
/** 自动运行配置 */
|
|
360
|
+
autoRunConfig?: AutoRunConfig;
|
|
361
|
+
/** 对话历史(从数据库加载,无状态架构) */
|
|
362
|
+
history?: ChatHistoryMessage[];
|
|
363
|
+
/**
|
|
364
|
+
* 用户自定义工具(透传模式)
|
|
365
|
+
*
|
|
366
|
+
* 这些工具不在服务端执行,而是:
|
|
367
|
+
* 1. 传递给 AI 模型
|
|
368
|
+
* 2. AI 返回 tool_call 时,发送 tool_call_request 事件给客户端
|
|
369
|
+
* 3. 客户端执行后,发新请求继续对话
|
|
370
|
+
*/
|
|
371
|
+
userTools?: UserToolDefinition[];
|
|
372
|
+
/**
|
|
373
|
+
* 上次请求的计划快照(可选,仅用于断点续传)
|
|
374
|
+
*
|
|
375
|
+
* 正常流程不需要传:AI 会在单次请求内自主创建和管理计划。
|
|
376
|
+
*
|
|
377
|
+
* 仅在请求被中断后需要恢复时使用:
|
|
378
|
+
* - 客户端工具透传模式(请求中断,等客户端执行后重新发请求)
|
|
379
|
+
* - 用户 abort 后想继续
|
|
380
|
+
*/
|
|
381
|
+
/**
|
|
382
|
+
* 宿主平台提示词(可选)
|
|
383
|
+
*
|
|
384
|
+
* 由宿主应用(如 smart-finder)注入的平台特定 system prompt 扩展。
|
|
385
|
+
* 用于描述平台特有的能力、工具编排、记忆管理等,避免在核心库中硬编码具体工具名。
|
|
386
|
+
*
|
|
387
|
+
* 注入位置:基础 prompt 之后、Skills 内容之前。
|
|
388
|
+
*/
|
|
389
|
+
platformPrompt?: string;
|
|
390
|
+
/**
|
|
391
|
+
* Skills 内容(启用的 Skills 的 content 数组,由前端/服务端拼好传入)
|
|
392
|
+
*
|
|
393
|
+
* 自动注入到 system prompt 的【用户指令】段落。
|
|
394
|
+
* 前端合并「默认启用的 skills + @ 手动选的 skills」后传入。
|
|
395
|
+
*/
|
|
396
|
+
skillContents?: string[];
|
|
397
|
+
}
|
|
398
|
+
/** 前端渲染组件类型(可扩展) */
|
|
399
|
+
type RenderType = 'weather' | 'chart' | 'browser' | 'plan' | string;
|
|
400
|
+
/** 前端动作类型(可扩展) */
|
|
401
|
+
type ActionType = 'toast' | 'notification' | 'reload' | string;
|
|
402
|
+
/**
|
|
403
|
+
* 工具 UI 声明(互斥)
|
|
404
|
+
* - render: 在对话流中渲染自定义组件
|
|
405
|
+
* - action: 触发前端动作(通知、刷新等)
|
|
406
|
+
*/
|
|
407
|
+
type ToolUI = {
|
|
408
|
+
type: 'render';
|
|
409
|
+
name: RenderType;
|
|
410
|
+
props?: Record<string, unknown>;
|
|
411
|
+
} | {
|
|
412
|
+
type: 'action';
|
|
413
|
+
name: ActionType;
|
|
414
|
+
};
|
|
415
|
+
/** Shell 命令执行结果 */
|
|
416
|
+
interface ExecResult {
|
|
417
|
+
stdout: string;
|
|
418
|
+
stderr: string;
|
|
419
|
+
exitCode: number;
|
|
420
|
+
}
|
|
421
|
+
/** 工具执行上下文 */
|
|
422
|
+
interface ToolContext {
|
|
423
|
+
/** 当前工作目录(可选,工具需要时由框架注入) */
|
|
424
|
+
cwd?: string;
|
|
425
|
+
/** 执行 Shell 命令(可选,工具需要时由框架注入) */
|
|
426
|
+
exec?: (cmd: string, args?: string[]) => Promise<ExecResult>;
|
|
427
|
+
/** 中断信号(用于取消长时间操作) */
|
|
428
|
+
signal?: AbortSignal;
|
|
429
|
+
}
|
|
430
|
+
/** 工具错误码 */
|
|
431
|
+
type ToolErrorCode = 'INVALID_PARAMS' | 'NOT_FOUND' | 'PERMISSION_DENIED' | 'OPERATION_FAILED' | 'TIMEOUT' | 'NETWORK_ERROR' | string;
|
|
432
|
+
/** 工具错误结构 */
|
|
433
|
+
interface ToolError {
|
|
434
|
+
message: string;
|
|
435
|
+
code?: ToolErrorCode;
|
|
436
|
+
suggestion?: string;
|
|
437
|
+
retryable?: boolean;
|
|
438
|
+
}
|
|
439
|
+
/** 判断是否为结构化工具错误 */
|
|
440
|
+
declare function isToolError(e: unknown): e is Error & {
|
|
441
|
+
toolError: ToolError;
|
|
442
|
+
};
|
|
443
|
+
/** 抛出结构化工具错误 */
|
|
444
|
+
declare function throwToolError(message: string, code?: ToolErrorCode, opts?: {
|
|
445
|
+
suggestion?: string;
|
|
446
|
+
retryable?: boolean;
|
|
447
|
+
}): never;
|
|
448
|
+
/** 重新抛出工具错误(非工具错误则包装后抛出) */
|
|
449
|
+
declare function rethrowToolError(error: unknown, code?: ToolErrorCode, opts?: {
|
|
450
|
+
suggestion?: string;
|
|
451
|
+
retryable?: boolean;
|
|
452
|
+
}): never;
|
|
453
|
+
/** 从 args 中安全提取类型化参数 */
|
|
454
|
+
declare function getArg<T>(args: Record<string, unknown>, key: string): T | undefined;
|
|
455
|
+
/** JSON Schema 属性定义(支持嵌套,符合 JSON Schema 规范) */
|
|
456
|
+
interface JsonSchemaProperty {
|
|
457
|
+
type?: string;
|
|
458
|
+
description?: string;
|
|
459
|
+
enum?: unknown[];
|
|
460
|
+
items?: JsonSchemaProperty;
|
|
461
|
+
properties?: Record<string, JsonSchemaProperty>;
|
|
462
|
+
required?: string[];
|
|
463
|
+
default?: unknown;
|
|
464
|
+
}
|
|
465
|
+
/** JSON Schema 对象类型(工具参数的根类型) */
|
|
466
|
+
interface JsonSchemaObject {
|
|
467
|
+
type: 'object';
|
|
468
|
+
properties: Record<string, JsonSchemaProperty>;
|
|
469
|
+
required?: string[];
|
|
470
|
+
description?: string;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* 工具接口
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```typescript
|
|
477
|
+
* const myTool: Tool = {
|
|
478
|
+
* name: 'my_tool',
|
|
479
|
+
* description: '我的自定义工具',
|
|
480
|
+
* parameters: { type: 'object', properties: { query: { type: 'string' } }, required: ['query'] },
|
|
481
|
+
* execute: async (args) => ({ result: getArg<string>(args, 'query') })
|
|
482
|
+
* };
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
interface Tool {
|
|
486
|
+
/** 工具名称 */
|
|
487
|
+
name: string;
|
|
488
|
+
/** 工具描述(供 AI 理解) */
|
|
489
|
+
description: string;
|
|
490
|
+
/** 参数定义(JSON Schema) */
|
|
491
|
+
parameters: JsonSchemaObject;
|
|
492
|
+
/**
|
|
493
|
+
* UI 声明(可选)
|
|
494
|
+
* - render: 在对话流中渲染自定义组件
|
|
495
|
+
* - action: 执行成功后触发前端动作
|
|
496
|
+
*/
|
|
497
|
+
ui?: ToolUI;
|
|
498
|
+
/**
|
|
499
|
+
* 是否需要用户手动批准(覆盖全局 autoRunMode)
|
|
500
|
+
*
|
|
501
|
+
* 设为 true 时无论全局配置如何,该工具必须用户确认才执行
|
|
502
|
+
*/
|
|
503
|
+
requiresApproval?: boolean;
|
|
504
|
+
/** 超时时间(毫秒),由框架通过 AbortSignal 强制执行 */
|
|
505
|
+
timeout?: number;
|
|
506
|
+
/** 扩展字段,供宿主应用存放元数据 */
|
|
507
|
+
meta?: Record<string, unknown>;
|
|
508
|
+
/**
|
|
509
|
+
* 执行函数
|
|
510
|
+
*
|
|
511
|
+
* 成功返回 object,框架自动 JSON.stringify 后传给 LLM。
|
|
512
|
+
* 失败统一 throw(推荐使用 throwToolError())。
|
|
513
|
+
*/
|
|
514
|
+
execute: (args: Record<string, unknown>, context: ToolContext) => Promise<object>;
|
|
515
|
+
}
|
|
516
|
+
/** 工具执行器接口(供 Agent 内部使用,自定义命令执行环境) */
|
|
517
|
+
interface ToolExecutor {
|
|
518
|
+
/** 执行 Shell 命令 */
|
|
519
|
+
executeCommand(command: string, cwd?: string, signal?: AbortSignal, hooks?: {
|
|
520
|
+
onStdout?: (chunk: string) => void;
|
|
521
|
+
onStderr?: (chunk: string) => void;
|
|
522
|
+
}): Promise<{
|
|
523
|
+
success: boolean;
|
|
524
|
+
output?: string;
|
|
525
|
+
error?: string;
|
|
526
|
+
}>;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* 工具插件实例
|
|
530
|
+
*
|
|
531
|
+
* 由 createXxxPlugin() 返回,可直接在 tools 数组中展开使用
|
|
532
|
+
*/
|
|
533
|
+
interface ToolPlugin {
|
|
534
|
+
/** 插件提供的工具数组 */
|
|
535
|
+
tools: Tool[];
|
|
536
|
+
/** 插件控制方法(可选,由各插件自定义) */
|
|
537
|
+
[key: string]: unknown;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* 创建单个工具插件(辅助函数)
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
* ```typescript
|
|
544
|
+
* export function getCwdTool(): ToolPlugin {
|
|
545
|
+
* return tool({
|
|
546
|
+
* name: 'get_cwd',
|
|
547
|
+
* description: '...',
|
|
548
|
+
* parameters: { ... },
|
|
549
|
+
* execute: async (args, context) => { ... }
|
|
550
|
+
* });
|
|
551
|
+
* }
|
|
552
|
+
* ```
|
|
553
|
+
*/
|
|
554
|
+
declare function tool(t: Tool): ToolPlugin;
|
|
555
|
+
/**
|
|
556
|
+
* 创建多个工具插件(辅助函数)
|
|
557
|
+
*
|
|
558
|
+
* @example
|
|
559
|
+
* ```typescript
|
|
560
|
+
* export function myPlugin(): ToolPlugin {
|
|
561
|
+
* return tools([
|
|
562
|
+
* { name: 'tool1', ... },
|
|
563
|
+
* { name: 'tool2', ... },
|
|
564
|
+
* ]);
|
|
565
|
+
* }
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
declare function tools(ts: Tool[]): ToolPlugin;
|
|
569
|
+
/**
|
|
570
|
+
* 工具配置项(Vite 插件风格,只支持插件形式)
|
|
571
|
+
*
|
|
572
|
+
* @example
|
|
573
|
+
* ```typescript
|
|
574
|
+
* tools: [
|
|
575
|
+
* getCwdTool(), // 工具插件(函数调用)
|
|
576
|
+
* searchPlugin({ dataDir, workspace }), // 异步插件(返回 Promise<ToolPlugin>)
|
|
577
|
+
* ]
|
|
578
|
+
* ```
|
|
579
|
+
*/
|
|
580
|
+
type ToolConfigItem = Tool | ToolPlugin | Promise<Tool | ToolPlugin>;
|
|
581
|
+
/**
|
|
582
|
+
* 解析工具配置,统一转换为 Tool[]
|
|
583
|
+
*
|
|
584
|
+
* 支持三种形式:
|
|
585
|
+
* - Tool: 单个工具(直接使用)
|
|
586
|
+
* - ToolPlugin: 工具插件(展开 .tools 数组)
|
|
587
|
+
* - Promise<Tool | ToolPlugin>: 异步工具/插件
|
|
588
|
+
*/
|
|
589
|
+
declare function resolveTools(items: ToolConfigItem[]): Promise<Tool[]>;
|
|
590
|
+
/** 工具定义(OpenAI 格式,内部使用) */
|
|
591
|
+
interface ToolDefinition {
|
|
592
|
+
type: 'function';
|
|
593
|
+
function: {
|
|
594
|
+
name: string;
|
|
595
|
+
description: string;
|
|
596
|
+
parameters: JsonSchemaObject;
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
/** 聊天消息(内部格式) */
|
|
600
|
+
interface ChatMessage {
|
|
601
|
+
/** 消息 ID */
|
|
602
|
+
id?: string;
|
|
603
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
604
|
+
content: string;
|
|
605
|
+
/** 深度思考内容 */
|
|
606
|
+
reasoning_content?: string;
|
|
607
|
+
tool_call_id?: string;
|
|
608
|
+
tool_calls?: Array<{
|
|
609
|
+
id: string;
|
|
610
|
+
type: 'function';
|
|
611
|
+
function: {
|
|
612
|
+
name: string;
|
|
613
|
+
arguments: string;
|
|
614
|
+
};
|
|
615
|
+
/** Gemini 模型需要的 thought_signature(用于工具调用循环) */
|
|
616
|
+
thought_signature?: string;
|
|
617
|
+
}>;
|
|
618
|
+
}
|
|
619
|
+
/** 火山引擎 Responses API 工具定义 */
|
|
620
|
+
interface ResponsesApiTool {
|
|
621
|
+
type: 'function' | 'web_search';
|
|
622
|
+
name?: string;
|
|
623
|
+
description?: string;
|
|
624
|
+
parameters?: Record<string, unknown>;
|
|
625
|
+
max_keyword?: number;
|
|
626
|
+
limit?: number;
|
|
627
|
+
sources?: string[];
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* AI Chat 事件系统
|
|
632
|
+
*
|
|
633
|
+
* 模块化的事件类型定义,用于 Agent 与前端之间的通信。
|
|
634
|
+
*
|
|
635
|
+
* 设计原则:
|
|
636
|
+
* 1. 每种事件类型有明确的数据结构
|
|
637
|
+
* 2. 事件分组便于管理和扩展
|
|
638
|
+
* 3. 类型安全,避免 unknown
|
|
639
|
+
* 4. 统一的时间追踪(开始/结束/耗时)
|
|
640
|
+
*/
|
|
641
|
+
/**
|
|
642
|
+
* 搜索结果项(统一契约)
|
|
643
|
+
* 无论搜索来源是厂商原生(ARK annotation)还是 Tavily 工具,协议/编排层都会归一为此结构,
|
|
644
|
+
* 前端只需依赖 title/url/snippet 即可。
|
|
645
|
+
*/
|
|
646
|
+
interface SearchResult {
|
|
647
|
+
title: string;
|
|
648
|
+
url: string;
|
|
649
|
+
snippet: string;
|
|
650
|
+
}
|
|
651
|
+
/** 工具调用状态 */
|
|
652
|
+
type ToolCallStatus = 'pending' | 'running' | 'success' | 'error';
|
|
653
|
+
/** 工具调用信息(用于前端状态管理) */
|
|
654
|
+
interface ToolCallInfo {
|
|
655
|
+
id: string;
|
|
656
|
+
name: string;
|
|
657
|
+
args: Record<string, unknown>;
|
|
658
|
+
status: ToolCallStatus;
|
|
659
|
+
result?: string;
|
|
660
|
+
error?: string;
|
|
661
|
+
startedAt?: number;
|
|
662
|
+
duration?: number;
|
|
663
|
+
}
|
|
664
|
+
/** Token 使用统计 */
|
|
665
|
+
interface TokenUsage {
|
|
666
|
+
promptTokens: number;
|
|
667
|
+
completionTokens: number;
|
|
668
|
+
totalTokens: number;
|
|
669
|
+
/** 思考 token 数(如果有) */
|
|
670
|
+
reasoningTokens?: number;
|
|
671
|
+
/** 缓存命中 token 数(如果有) */
|
|
672
|
+
cachedTokens?: number;
|
|
673
|
+
}
|
|
674
|
+
/** 错误类别 - 参考 AI SDK 设计 */
|
|
675
|
+
type ErrorCategory = 'api' | 'rate_limit' | 'validation' | 'tool' | 'timeout' | 'abort' | 'parse' | 'unknown';
|
|
676
|
+
|
|
677
|
+
/** 内部使用的 ToolErrorShape 别名 */
|
|
678
|
+
type ToolErrorShape = ToolError;
|
|
679
|
+
/** 错误详情 - 结构化错误信息 */
|
|
680
|
+
interface ErrorDetails {
|
|
681
|
+
/** 错误分类 */
|
|
682
|
+
category: ErrorCategory;
|
|
683
|
+
/** 人类可读的错误信息 */
|
|
684
|
+
message: string;
|
|
685
|
+
/** 错误代码(如 'RATE_LIMIT', 'NETWORK_ERROR') */
|
|
686
|
+
code?: string;
|
|
687
|
+
/** HTTP 状态码(如果适用) */
|
|
688
|
+
statusCode?: number;
|
|
689
|
+
/** HTTP 状态文本 */
|
|
690
|
+
statusText?: string;
|
|
691
|
+
/** 是否可重试 */
|
|
692
|
+
retryable?: boolean;
|
|
693
|
+
/** 重试等待时间(秒) */
|
|
694
|
+
retryAfter?: number;
|
|
695
|
+
/** 原始错误信息(用于调试) */
|
|
696
|
+
cause?: string;
|
|
697
|
+
/** 错误发生的上下文(如模型名、工具名) */
|
|
698
|
+
context?: string;
|
|
699
|
+
}
|
|
700
|
+
/** 思考开始事件 */
|
|
701
|
+
interface ThinkingStartEvent {
|
|
702
|
+
type: 'thinking_start';
|
|
703
|
+
data: {
|
|
704
|
+
/** 开始时间戳(毫秒) */
|
|
705
|
+
startedAt: number;
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
/** 思考内容增量事件 */
|
|
709
|
+
interface ThinkingDeltaEvent {
|
|
710
|
+
type: 'thinking_delta';
|
|
711
|
+
data: {
|
|
712
|
+
/** 增量内容 */
|
|
713
|
+
content: string;
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
/** 思考结束事件 */
|
|
717
|
+
interface ThinkingEndEvent {
|
|
718
|
+
type: 'thinking_end';
|
|
719
|
+
data: {
|
|
720
|
+
/** 结束时间戳(毫秒) */
|
|
721
|
+
endedAt: number;
|
|
722
|
+
/** 思考耗时(毫秒) */
|
|
723
|
+
duration: number;
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
/** 思考相关事件联合类型 */
|
|
727
|
+
type ThinkingEvent = ThinkingStartEvent | ThinkingDeltaEvent | ThinkingEndEvent;
|
|
728
|
+
/** 搜索开始事件 */
|
|
729
|
+
interface SearchStartEvent {
|
|
730
|
+
type: 'search_start';
|
|
731
|
+
data: {
|
|
732
|
+
/** 搜索查询词 */
|
|
733
|
+
query: string;
|
|
734
|
+
/** 开始时间戳(毫秒) */
|
|
735
|
+
startedAt: number;
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
/** 搜索结果事件(成功完成) */
|
|
739
|
+
interface SearchResultEvent {
|
|
740
|
+
type: 'search_result';
|
|
741
|
+
data: {
|
|
742
|
+
/** 搜索结果列表 */
|
|
743
|
+
results: SearchResult[];
|
|
744
|
+
/** 结束时间戳(毫秒) */
|
|
745
|
+
endedAt: number;
|
|
746
|
+
/** 搜索耗时(毫秒) */
|
|
747
|
+
duration: number;
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
/** 搜索结束事件(失败或取消) */
|
|
751
|
+
interface SearchEndEvent {
|
|
752
|
+
type: 'search_end';
|
|
753
|
+
data: {
|
|
754
|
+
/** 是否成功 */
|
|
755
|
+
success: boolean;
|
|
756
|
+
/** 错误信息(失败时) */
|
|
757
|
+
error?: string;
|
|
758
|
+
/** 结束时间戳(毫秒) */
|
|
759
|
+
endedAt: number;
|
|
760
|
+
/** 搜索耗时(毫秒) */
|
|
761
|
+
duration: number;
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
/** 搜索相关事件联合类型 */
|
|
765
|
+
type SearchEvent = SearchStartEvent | SearchResultEvent | SearchEndEvent;
|
|
766
|
+
/** 工具调用开始事件 */
|
|
767
|
+
interface ToolCallStartEvent {
|
|
768
|
+
type: 'tool_call_start';
|
|
769
|
+
data: {
|
|
770
|
+
/** 工具调用 ID */
|
|
771
|
+
id: string;
|
|
772
|
+
/** 工具名称 */
|
|
773
|
+
name: string;
|
|
774
|
+
/** 工具参数 */
|
|
775
|
+
args: Record<string, unknown>;
|
|
776
|
+
/** 开始时间戳(毫秒) */
|
|
777
|
+
startedAt: number;
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
/** 工具调用结果事件 */
|
|
781
|
+
interface ToolCallResultEvent {
|
|
782
|
+
type: 'tool_call_result';
|
|
783
|
+
data: {
|
|
784
|
+
/** 工具调用 ID */
|
|
785
|
+
id: string;
|
|
786
|
+
/** 工具名称 */
|
|
787
|
+
name: string;
|
|
788
|
+
/** 执行结果(JSON 字符串) */
|
|
789
|
+
result: string;
|
|
790
|
+
/** 是否成功 */
|
|
791
|
+
success: boolean;
|
|
792
|
+
/** 错误信息(失败时) */
|
|
793
|
+
error?: ToolErrorShape;
|
|
794
|
+
/** 结束时间戳(毫秒) */
|
|
795
|
+
endedAt: number;
|
|
796
|
+
/** 执行耗时(毫秒) */
|
|
797
|
+
duration: number;
|
|
798
|
+
/** 工具 UI 声明(成功时) */
|
|
799
|
+
ui?: ToolUIShape;
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
/** ToolUI 形状(对应 types.ts 中的 ToolUI,避免循环依赖) */
|
|
803
|
+
interface ToolUIShape {
|
|
804
|
+
type: 'render' | 'action';
|
|
805
|
+
name: string;
|
|
806
|
+
props?: Record<string, unknown>;
|
|
807
|
+
}
|
|
808
|
+
/** 工具输出增量事件(用于 stdout/stderr 流式展示) */
|
|
809
|
+
interface ToolCallOutputEvent {
|
|
810
|
+
type: 'tool_call_output';
|
|
811
|
+
data: {
|
|
812
|
+
/** 工具调用 ID */
|
|
813
|
+
id: string;
|
|
814
|
+
/** 工具名称 */
|
|
815
|
+
name: string;
|
|
816
|
+
/** 输出流类型 */
|
|
817
|
+
stream: 'stdout' | 'stderr';
|
|
818
|
+
/** 输出增量内容 */
|
|
819
|
+
chunk: string;
|
|
820
|
+
/** 时间戳 */
|
|
821
|
+
at: number;
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
/** 工具执行批准请求事件(manual 模式) */
|
|
825
|
+
interface ToolApprovalRequestEvent {
|
|
826
|
+
type: 'tool_approval_request';
|
|
827
|
+
data: {
|
|
828
|
+
/** 工具调用 ID */
|
|
829
|
+
id: string;
|
|
830
|
+
/** 工具名称 */
|
|
831
|
+
name: string;
|
|
832
|
+
/** 工具参数 */
|
|
833
|
+
args: Record<string, unknown>;
|
|
834
|
+
/** 请求时间戳 */
|
|
835
|
+
requestedAt: number;
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* 客户端工具调用请求事件(透传模式)
|
|
840
|
+
*
|
|
841
|
+
* 用于用户自定义工具:
|
|
842
|
+
* - AI 返回 tool_call
|
|
843
|
+
* - 服务端不执行,透传给客户端
|
|
844
|
+
* - 客户端执行后,发新请求继续对话
|
|
845
|
+
*/
|
|
846
|
+
interface ToolCallRequestEvent {
|
|
847
|
+
type: 'tool_call_request';
|
|
848
|
+
data: {
|
|
849
|
+
/** 工具调用 ID */
|
|
850
|
+
id: string;
|
|
851
|
+
/** 工具名称 */
|
|
852
|
+
name: string;
|
|
853
|
+
/** 工具参数 */
|
|
854
|
+
args: Record<string, unknown>;
|
|
855
|
+
/** 请求时间戳 */
|
|
856
|
+
requestedAt: number;
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
/** 工具相关事件联合类型 */
|
|
860
|
+
type ToolEvent = ToolCallStartEvent | ToolCallResultEvent | ToolApprovalRequestEvent | ToolCallOutputEvent | ToolCallRequestEvent;
|
|
861
|
+
/** 文本增量事件 */
|
|
862
|
+
interface TextDeltaEvent {
|
|
863
|
+
type: 'text_delta';
|
|
864
|
+
data: {
|
|
865
|
+
/** 增量文本 */
|
|
866
|
+
content: string;
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
/** 文本相关事件联合类型 */
|
|
870
|
+
type TextEvent = TextDeltaEvent;
|
|
871
|
+
/** 计划步骤状态 */
|
|
872
|
+
type PlanStepStatus = 'pending' | 'in_progress' | 'done' | 'failed';
|
|
873
|
+
/** 计划步骤 */
|
|
874
|
+
interface PlanStep {
|
|
875
|
+
/** 步骤 ID(模型生成,如 "1", "scan", "move-images") */
|
|
876
|
+
id: string;
|
|
877
|
+
/** 步骤描述 */
|
|
878
|
+
title: string;
|
|
879
|
+
/** 步骤状态 */
|
|
880
|
+
status: PlanStepStatus;
|
|
881
|
+
}
|
|
882
|
+
/** 完成事件 */
|
|
883
|
+
interface DoneEvent {
|
|
884
|
+
type: 'done';
|
|
885
|
+
data: {
|
|
886
|
+
/** 完整的最终文本 */
|
|
887
|
+
text: string;
|
|
888
|
+
/** Token 使用统计 */
|
|
889
|
+
usage?: TokenUsage;
|
|
890
|
+
/** 总耗时(毫秒) */
|
|
891
|
+
duration?: number;
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
/** 错误事件 - 结构化错误信息 */
|
|
895
|
+
interface ErrorEvent {
|
|
896
|
+
type: 'error';
|
|
897
|
+
data: ErrorDetails;
|
|
898
|
+
}
|
|
899
|
+
/** 中止事件 - 用户主动取消 */
|
|
900
|
+
interface AbortEvent {
|
|
901
|
+
type: 'abort';
|
|
902
|
+
data: {
|
|
903
|
+
/** 中止原因 */
|
|
904
|
+
reason?: string;
|
|
905
|
+
/** 中止时间戳 */
|
|
906
|
+
abortedAt: number;
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
/** 状态相关事件联合类型 */
|
|
910
|
+
type StatusEvent = DoneEvent | ErrorEvent | AbortEvent;
|
|
911
|
+
/** 步骤开始事件 */
|
|
912
|
+
interface StepStartEvent {
|
|
913
|
+
type: 'step_start';
|
|
914
|
+
data: {
|
|
915
|
+
/** 步骤编号(从 1 开始) */
|
|
916
|
+
stepNumber: number;
|
|
917
|
+
/** 步骤描述(可选) */
|
|
918
|
+
description?: string;
|
|
919
|
+
/** 开始时间戳 */
|
|
920
|
+
startedAt: number;
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
/** 步骤结束事件 */
|
|
924
|
+
interface StepEndEvent {
|
|
925
|
+
type: 'step_end';
|
|
926
|
+
data: {
|
|
927
|
+
/** 步骤编号 */
|
|
928
|
+
stepNumber: number;
|
|
929
|
+
/** 结束时间戳 */
|
|
930
|
+
endedAt: number;
|
|
931
|
+
/** 耗时(毫秒) */
|
|
932
|
+
duration: number;
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
/** 步骤相关事件联合类型 */
|
|
936
|
+
type StepEvent = StepStartEvent | StepEndEvent;
|
|
937
|
+
/**
|
|
938
|
+
* Agent 当前阶段:前端根据此字段直接渲染 loading 状态,不需要「猜」。
|
|
939
|
+
*
|
|
940
|
+
* - thinking:模型正在思考(初始请求 / 工具执行完后等待下一轮)→ 显示「正在思考...」
|
|
941
|
+
* - null:有可见活动(思考/搜索/工具/文字流),不需要额外 loading
|
|
942
|
+
*/
|
|
943
|
+
type AgentPhase = 'thinking' | null;
|
|
944
|
+
/** Agent 状态事件(前端用于渲染 loading 提示) */
|
|
945
|
+
interface AgentStatusEvent {
|
|
946
|
+
type: 'agent_status';
|
|
947
|
+
data: {
|
|
948
|
+
/** 当前阶段 */
|
|
949
|
+
phase: AgentPhase;
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
/** 创建 agent_status 事件 */
|
|
953
|
+
declare function createAgentStatus(phase: AgentPhase): AgentStatusEvent;
|
|
954
|
+
/** 所有事件类型 */
|
|
955
|
+
type ChatEvent = ThinkingEvent | SearchEvent | ToolEvent | TextEvent | StatusEvent | StepEvent | AgentStatusEvent;
|
|
956
|
+
/** 事件类型字符串 */
|
|
957
|
+
type ChatEventType = ChatEvent['type'];
|
|
958
|
+
/** 所有合法事件类型(用于契约测试:Orchestrator/适配器产出必须在此集合内) */
|
|
959
|
+
declare const CHAT_EVENT_TYPES: readonly ChatEventType[];
|
|
960
|
+
/**
|
|
961
|
+
* 创建思考开始事件
|
|
962
|
+
*/
|
|
963
|
+
declare function createThinkingStart(): ThinkingStartEvent;
|
|
964
|
+
/**
|
|
965
|
+
* 创建思考增量事件
|
|
966
|
+
*/
|
|
967
|
+
declare function createThinkingDelta(content: string): ThinkingDeltaEvent;
|
|
968
|
+
/**
|
|
969
|
+
* 创建思考结束事件
|
|
970
|
+
*/
|
|
971
|
+
declare function createThinkingEnd(startedAt: number): ThinkingEndEvent;
|
|
972
|
+
/**
|
|
973
|
+
* 创建搜索开始事件
|
|
974
|
+
*/
|
|
975
|
+
declare function createSearchStart(query: string): SearchStartEvent;
|
|
976
|
+
/**
|
|
977
|
+
* 创建搜索结果事件(成功)
|
|
978
|
+
*/
|
|
979
|
+
declare function createSearchResult(results: SearchResult[], startedAt: number): SearchResultEvent;
|
|
980
|
+
/**
|
|
981
|
+
* 创建搜索结束事件(失败或取消)
|
|
982
|
+
*/
|
|
983
|
+
declare function createSearchEnd(success: boolean, startedAt: number, error?: string): SearchEndEvent;
|
|
984
|
+
/**
|
|
985
|
+
* 创建工具调用开始事件
|
|
986
|
+
*/
|
|
987
|
+
declare function createToolCallStart(id: string, name: string, args: Record<string, unknown>): ToolCallStartEvent;
|
|
988
|
+
/**
|
|
989
|
+
* 创建工具调用结果事件
|
|
990
|
+
*/
|
|
991
|
+
declare function createToolCallResult(id: string, name: string, result: string, success: boolean, startedAt: number, error?: ToolErrorShape, ui?: ToolUIShape): ToolCallResultEvent;
|
|
992
|
+
/**
|
|
993
|
+
* 创建工具输出增量事件
|
|
994
|
+
*/
|
|
995
|
+
declare function createToolCallOutput(id: string, name: string, stream: 'stdout' | 'stderr', chunk: string): ToolCallOutputEvent;
|
|
996
|
+
/**
|
|
997
|
+
* 创建客户端工具调用请求事件(透传模式)
|
|
998
|
+
*
|
|
999
|
+
* 用于用户自定义工具,服务端不执行,透传给客户端
|
|
1000
|
+
*/
|
|
1001
|
+
declare function createToolCallRequest(id: string, name: string, args: Record<string, unknown>): ToolCallRequestEvent;
|
|
1002
|
+
/**
|
|
1003
|
+
* 创建文本增量事件
|
|
1004
|
+
*/
|
|
1005
|
+
declare function createTextDelta(content: string): TextDeltaEvent;
|
|
1006
|
+
/**
|
|
1007
|
+
* 创建完成事件
|
|
1008
|
+
*/
|
|
1009
|
+
declare function createDone(text: string, usage?: TokenUsage, duration?: number): DoneEvent;
|
|
1010
|
+
/**
|
|
1011
|
+
* 创建错误事件(完整版)
|
|
1012
|
+
*/
|
|
1013
|
+
declare function createError(details: ErrorDetails): ErrorEvent;
|
|
1014
|
+
/**
|
|
1015
|
+
* 创建 API 错误事件
|
|
1016
|
+
*/
|
|
1017
|
+
declare function createApiError(message: string, options?: {
|
|
1018
|
+
code?: string;
|
|
1019
|
+
statusCode?: number;
|
|
1020
|
+
statusText?: string;
|
|
1021
|
+
retryable?: boolean;
|
|
1022
|
+
retryAfter?: number;
|
|
1023
|
+
cause?: string;
|
|
1024
|
+
context?: string;
|
|
1025
|
+
}): ErrorEvent;
|
|
1026
|
+
/**
|
|
1027
|
+
* 创建速率限制错误事件
|
|
1028
|
+
*/
|
|
1029
|
+
declare function createRateLimitError(message: string, retryAfter?: number, context?: string): ErrorEvent;
|
|
1030
|
+
/**
|
|
1031
|
+
* 创建工具错误事件
|
|
1032
|
+
*/
|
|
1033
|
+
declare function createToolError(message: string, toolName: string, cause?: string): ErrorEvent;
|
|
1034
|
+
/**
|
|
1035
|
+
* 创建超时错误事件
|
|
1036
|
+
*/
|
|
1037
|
+
declare function createTimeoutError(message: string, context?: string): ErrorEvent;
|
|
1038
|
+
/**
|
|
1039
|
+
* 创建解析错误事件
|
|
1040
|
+
*/
|
|
1041
|
+
declare function createParseError(message: string, cause?: string): ErrorEvent;
|
|
1042
|
+
/**
|
|
1043
|
+
* 创建中止事件
|
|
1044
|
+
*/
|
|
1045
|
+
declare function createAbort(reason?: string): AbortEvent;
|
|
1046
|
+
/**
|
|
1047
|
+
* 创建步骤开始事件
|
|
1048
|
+
*/
|
|
1049
|
+
declare function createStepStart(stepNumber: number, description?: string): StepStartEvent;
|
|
1050
|
+
/**
|
|
1051
|
+
* 创建步骤结束事件
|
|
1052
|
+
*/
|
|
1053
|
+
declare function createStepEnd(stepNumber: number, startedAt: number): StepEndEvent;
|
|
1054
|
+
/** 检查是否为思考事件 */
|
|
1055
|
+
declare function isThinkingEvent(event: ChatEvent): event is ThinkingEvent;
|
|
1056
|
+
/** 检查是否为搜索事件 */
|
|
1057
|
+
declare function isSearchEvent(event: ChatEvent): event is SearchEvent;
|
|
1058
|
+
/** 检查是否为工具事件 */
|
|
1059
|
+
declare function isToolEvent(event: ChatEvent): event is ToolEvent;
|
|
1060
|
+
/** 检查是否为文本事件 */
|
|
1061
|
+
declare function isTextEvent(event: ChatEvent): event is TextEvent;
|
|
1062
|
+
/** 检查是否为状态事件 */
|
|
1063
|
+
declare function isStatusEvent(event: ChatEvent): event is StatusEvent;
|
|
1064
|
+
/** 检查是否为错误事件 */
|
|
1065
|
+
declare function isErrorEvent(event: ChatEvent): event is ErrorEvent;
|
|
1066
|
+
/** 检查是否为中止事件 */
|
|
1067
|
+
declare function isAbortEvent(event: ChatEvent): event is AbortEvent;
|
|
1068
|
+
/** 检查是否为步骤事件 */
|
|
1069
|
+
declare function isStepEvent(event: ChatEvent): event is StepEvent;
|
|
1070
|
+
/** 检查错误是否可重试 */
|
|
1071
|
+
declare function isRetryableError(event: ChatEvent): boolean;
|
|
1072
|
+
|
|
1073
|
+
export { resolveTools as $, type AgentConfig as A, type AutoRunConfig as B, type ChatOptions as C, DOUBAO_FAMILY as D, type ToolDefinition as E, type ResponsesApiTool as F, GEMINI_FAMILY as G, type ProviderType as H, type ToolContext as I, type JsonSchemaObject as J, type ToolUI as K, type RenderType as L, type ModelFamilyConfig as M, type ActionType as N, type ExecResult as O, type ProtocolId as P, QWEN_FAMILY as Q, type RouteResult as R, type SearchStrategy as S, type ToolExecutor as T, type ToolError as U, type ToolErrorCode as V, type ToolPlugin as W, type ToolConfigItem as X, type UserToolDefinition as Y, MODELS as Z, getModelByModelId as _, type ChatEvent as a, isTextEvent as a$, tool as a0, tools as a1, throwToolError as a2, rethrowToolError as a3, isToolError as a4, getArg as a5, type SearchResult as a6, type ToolCallStatus as a7, type ToolCallInfo as a8, type TokenUsage as a9, type StepEndEvent as aA, type StepEvent as aB, type ChatEventType as aC, createThinkingStart as aD, createThinkingDelta as aE, createThinkingEnd as aF, createSearchStart as aG, createSearchResult as aH, createSearchEnd as aI, createToolCallStart as aJ, createToolCallResult as aK, createToolCallOutput as aL, createToolCallRequest as aM, createTextDelta as aN, createDone as aO, createError as aP, createApiError as aQ, createRateLimitError as aR, createToolError as aS, createTimeoutError as aT, createParseError as aU, createAbort as aV, createStepStart as aW, createStepEnd as aX, isThinkingEvent as aY, isSearchEvent as aZ, isToolEvent as a_, type ErrorCategory as aa, type ErrorDetails as ab, type ToolUIShape as ac, type ThinkingStartEvent as ad, type ThinkingDeltaEvent as ae, type ThinkingEndEvent as af, type ThinkingEvent as ag, type SearchStartEvent as ah, type SearchResultEvent as ai, type SearchEndEvent as aj, type SearchEvent as ak, type ToolCallStartEvent as al, type ToolCallResultEvent as am, type ToolCallOutputEvent as an, type ToolApprovalRequestEvent as ao, type ToolCallRequestEvent as ap, type ToolEvent as aq, type TextDeltaEvent as ar, type TextEvent as as, type PlanStep as at, type PlanStepStatus as au, type DoneEvent as av, type ErrorEvent as aw, type AbortEvent as ax, type StatusEvent as ay, type StepStartEvent as az, type ModelOption as b, isStatusEvent as b0, isErrorEvent as b1, isAbortEvent as b2, isStepEvent as b3, isRetryableError as b4, CHAT_EVENT_TYPES as b5, routeModelToProvider as b6, routeModelWithDetails as b7, getDefaultProvider as b8, isModelForProvider as b9, type McpServerConfig as ba, type McpConnectionStatus as bb, type AgentPhase as bc, type AgentStatusEvent as bd, createAgentStatus as be, type McpConnectionInfo as c, type Tool as d, type ChatMessage as e, MODEL_FAMILIES as f, DEEPSEEK_FAMILY as g, GPT_FAMILY as h, CLAUDE_FAMILY as i, MODEL_REGISTRY as j, getModelEntry as k, getModelFamily as l, getModelProtocol as m, getVisibleModels as n, getModelsByFamily as o, getModelsByProtocol as p, modelSupportsThinking as q, modelSupportsNativeSearch as r, getModelSearchStrategy as s, type ModelFamilyId as t, type ThinkingFormat as u, type ToolCallFormat as v, type ModelRegistryEntry as w, type ChatMode as x, type ThinkingMode as y, type AutoRunMode as z };
|