@xiaozhi-client/endpoint 1.9.7-beta.3

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.
@@ -0,0 +1,542 @@
1
+ import { Tool } from '@modelcontextprotocol/sdk/types.js';
2
+ import { EventEmitter } from 'node:events';
3
+
4
+ /**
5
+ * 工具调用结果接口
6
+ */
7
+ interface ToolCallResult {
8
+ content: Array<{
9
+ type: string;
10
+ text: string;
11
+ }>;
12
+ isError?: boolean;
13
+ }
14
+ /**
15
+ * 工具调用参数接口
16
+ */
17
+ interface ToolCallParams {
18
+ name: string;
19
+ arguments?: Record<string, unknown>;
20
+ }
21
+ /**
22
+ * 验证后的工具调用参数
23
+ */
24
+ interface ValidatedToolCallParams {
25
+ name: string;
26
+ arguments?: Record<string, unknown>;
27
+ }
28
+ /**
29
+ * 工具调用错误码枚举
30
+ */
31
+ declare enum ToolCallErrorCode {
32
+ /** 无效参数 */
33
+ INVALID_PARAMS = -32602,
34
+ /** 工具不存在 */
35
+ TOOL_NOT_FOUND = -32601,
36
+ /** 服务不可用 */
37
+ SERVICE_UNAVAILABLE = -32001,
38
+ /** 调用超时 */
39
+ TIMEOUT = -32002,
40
+ /** 工具执行错误 */
41
+ TOOL_EXECUTION_ERROR = -32000
42
+ }
43
+ /**
44
+ * 工具调用错误类
45
+ */
46
+ declare class ToolCallError extends Error {
47
+ code: ToolCallErrorCode;
48
+ data?: unknown | undefined;
49
+ constructor(code: ToolCallErrorCode, message: string, data?: unknown | undefined);
50
+ }
51
+ /**
52
+ * JSON Schema 类型定义
53
+ * 兼容 MCP SDK 的 JSON Schema 格式
54
+ */
55
+ type JSONSchema = (Record<string, unknown> & {
56
+ type: "object";
57
+ properties?: Record<string, unknown>;
58
+ required?: string[];
59
+ additionalProperties?: boolean;
60
+ }) | Record<string, unknown>;
61
+ /**
62
+ * 确保对象符合 MCP Tool JSON Schema 格式
63
+ * 返回类型兼容 MCP SDK 的 Tool 类型
64
+ */
65
+ declare function ensureToolJSONSchema(schema: JSONSchema): {
66
+ type: "object";
67
+ properties?: Record<string, object>;
68
+ required?: string[];
69
+ additionalProperties?: boolean;
70
+ };
71
+ /**
72
+ * 增强的工具信息接口
73
+ * 包含工具的启用状态和使用统计信息
74
+ */
75
+ interface EnhancedToolInfo {
76
+ /** 工具唯一标识符,格式为 "{serviceName}__{originalName}" */
77
+ name: string;
78
+ /** 工具描述信息 */
79
+ description: string;
80
+ /** 工具输入参数的 JSON Schema 定义 */
81
+ inputSchema: JSONSchema;
82
+ /** 工具所属的 MCP 服务名称 */
83
+ serviceName: string;
84
+ /** 工具在 MCP 服务中的原始名称 */
85
+ originalName: string;
86
+ /** 工具是否启用 */
87
+ enabled: boolean;
88
+ /** 工具使用次数统计 */
89
+ usageCount: number;
90
+ /** 工具最后使用时间 (ISO 8601 格式字符串) */
91
+ lastUsedTime: string;
92
+ }
93
+ /**
94
+ * MCP 服务管理器接口
95
+ * 用于工具调用,避免循环依赖
96
+ */
97
+ interface IMCPServiceManager {
98
+ /** 获取所有工具列表 */
99
+ getAllTools(): EnhancedToolInfo[];
100
+ /** 调用工具 */
101
+ callTool(toolName: string, arguments_: Record<string, unknown>): Promise<ToolCallResult>;
102
+ }
103
+ /**
104
+ * 连接状态枚举
105
+ */
106
+ declare enum ConnectionState {
107
+ DISCONNECTED = "disconnected",
108
+ CONNECTING = "connecting",
109
+ CONNECTED = "connected",
110
+ FAILED = "failed"
111
+ }
112
+ /**
113
+ * 连接选项接口
114
+ */
115
+ interface ConnectionOptions {
116
+ /** 连接超时时间(毫秒),默认 10000 */
117
+ connectionTimeout?: number;
118
+ /** 重连延迟时间(毫秒),默认 2000 */
119
+ reconnectDelay?: number;
120
+ }
121
+ /**
122
+ * EndpointConnection 状态接口
123
+ */
124
+ interface EndpointConnectionStatus {
125
+ /** 是否已连接 */
126
+ connected: boolean;
127
+ /** 是否已初始化 */
128
+ initialized: boolean;
129
+ /** 接入点 URL */
130
+ url: string;
131
+ /** 可用工具数量 */
132
+ availableTools: number;
133
+ /** 连接状态 */
134
+ connectionState: ConnectionState;
135
+ /** 最后一次错误信息 */
136
+ lastError: string | null;
137
+ }
138
+ /**
139
+ * 简单连接状态接口
140
+ */
141
+ interface SimpleConnectionStatus {
142
+ /** 接入点地址 */
143
+ endpoint: string;
144
+ /** 是否已连接 */
145
+ connected: boolean;
146
+ /** 是否已初始化 */
147
+ initialized: boolean;
148
+ /** 最后连接时间 */
149
+ lastConnected?: Date;
150
+ /** 最后错误信息 */
151
+ lastError?: string;
152
+ }
153
+ /**
154
+ * 完整连接状态接口(扩展 SimpleConnectionStatus)
155
+ */
156
+ interface ConnectionStatus extends SimpleConnectionStatus {
157
+ }
158
+ /**
159
+ * 配置变更事件类型
160
+ */
161
+ interface ConfigChangeEvent {
162
+ type: "endpoints_added" | "endpoints_removed" | "endpoints_updated" | "options_updated";
163
+ data: {
164
+ added?: string[];
165
+ removed?: string[];
166
+ updated?: string[];
167
+ oldOptions?: Partial<ConnectionOptions>;
168
+ newOptions?: Partial<ConnectionOptions>;
169
+ };
170
+ timestamp: Date;
171
+ }
172
+ /**
173
+ * 重连结果接口
174
+ */
175
+ interface ReconnectResult {
176
+ successCount: number;
177
+ failureCount: number;
178
+ results: Array<{
179
+ endpoint: string;
180
+ success: boolean;
181
+ error?: string;
182
+ }>;
183
+ }
184
+ /**
185
+ * MCP 服务器配置类型
186
+ * 支持三种配置方式:
187
+ * 1. 本地命令 (stdio): { command: string; args: string[]; env?: Record<string, string> }
188
+ * 2. SSE: { type: "sse"; url: string; headers?: Record<string, string> }
189
+ * 3. Streamable HTTP: { type?: "streamable-http"; url: string; headers?: Record<string, string> }
190
+ */
191
+ type MCPServerConfig = LocalMCPServerConfig | SSEMCPServerConfig | StreamableHTTPMCPServerConfig;
192
+ /**
193
+ * 本地 MCP 服务器配置
194
+ */
195
+ interface LocalMCPServerConfig {
196
+ command: string;
197
+ args: string[];
198
+ env?: Record<string, string>;
199
+ }
200
+ /**
201
+ * SSE MCP 服务器配置
202
+ */
203
+ interface SSEMCPServerConfig {
204
+ type: "sse";
205
+ url: string;
206
+ headers?: Record<string, string>;
207
+ }
208
+ /**
209
+ * Streamable HTTP MCP 服务器配置
210
+ */
211
+ interface StreamableHTTPMCPServerConfig {
212
+ type?: "streamable-http";
213
+ url: string;
214
+ headers?: Record<string, string>;
215
+ }
216
+ /**
217
+ * Endpoint 配置接口
218
+ * 用于新 API:直接在构造函数中传入 MCP 服务器配置
219
+ */
220
+ interface EndpointConfig {
221
+ /** MCP 服务器配置(声明式) */
222
+ mcpServers: Record<string, MCPServerConfig>;
223
+ /** 可选:重连延迟(毫秒),默认 2000 */
224
+ reconnectDelay?: number;
225
+ /** 可选:ModelScope API Key(全局) */
226
+ modelscopeApiKey?: string;
227
+ }
228
+ /**
229
+ * EndpointManager 配置接口(新 API 简化版)
230
+ */
231
+ interface EndpointManagerConfig {
232
+ /** 可选:默认重连延迟(毫秒) */
233
+ defaultReconnectDelay?: number;
234
+ }
235
+
236
+ /**
237
+ * Endpoint 类(新 API)
238
+ * 管理单个小智接入点的 WebSocket 连接
239
+ * 实现 MCP (Model Context Protocol) 协议通信
240
+ *
241
+ * 与 EndpointConnection 的区别:
242
+ * - 构造函数直接传入 mcpServers 配置,不需要单独调用 setServiceManager()
243
+ * - 配置更简洁直观
244
+ */
245
+
246
+ /**
247
+ * Endpoint 类
248
+ * 负责管理单个小智接入点的 WebSocket 连接
249
+ * 使用新的配置方式:直接在构造函数中传入 mcpServers 配置
250
+ */
251
+ declare class Endpoint {
252
+ private endpointUrl;
253
+ private ws;
254
+ private connectionStatus;
255
+ private serverInitialized;
256
+ private mcpAdapter;
257
+ private connectionState;
258
+ private lastError;
259
+ private connectionTimeout;
260
+ private toolCallTimeout;
261
+ private reconnectDelay;
262
+ /**
263
+ * 构造函数
264
+ *
265
+ * @param endpointUrl - 小智接入点 URL
266
+ * @param config - Endpoint 配置(包含 mcpServers)
267
+ */
268
+ constructor(endpointUrl: string, config: EndpointConfig);
269
+ /**
270
+ * 获取 Endpoint URL
271
+ */
272
+ getUrl(): string;
273
+ /**
274
+ * 获取当前所有工具列表
275
+ */
276
+ getTools(): Tool[];
277
+ /**
278
+ * 连接小智接入点
279
+ */
280
+ connect(): Promise<void>;
281
+ /**
282
+ * 尝试建立连接
283
+ */
284
+ private attemptConnection;
285
+ /**
286
+ * 处理连接成功
287
+ */
288
+ private handleConnectionSuccess;
289
+ /**
290
+ * 处理连接错误
291
+ */
292
+ private handleConnectionError;
293
+ /**
294
+ * 处理连接关闭
295
+ */
296
+ private handleConnectionClose;
297
+ /**
298
+ * 清理连接资源
299
+ */
300
+ private cleanupConnection;
301
+ /**
302
+ * 处理 MCP 消息
303
+ */
304
+ private handleMessage;
305
+ /**
306
+ * 发送响应消息
307
+ */
308
+ private sendResponse;
309
+ /**
310
+ * 获取服务器状态
311
+ */
312
+ getStatus(): EndpointConnectionStatus;
313
+ /**
314
+ * 检查连接状态
315
+ */
316
+ isConnected(): boolean;
317
+ /**
318
+ * 主动断开小智连接
319
+ */
320
+ disconnect(): void;
321
+ /**
322
+ * 重连小智接入点
323
+ */
324
+ reconnect(): Promise<void>;
325
+ /**
326
+ * 处理工具调用请求
327
+ */
328
+ private handleToolCall;
329
+ /**
330
+ * 带超时控制的工具执行
331
+ */
332
+ private executeToolWithTimeout;
333
+ /**
334
+ * 处理工具调用错误
335
+ */
336
+ private handleToolCallError;
337
+ /**
338
+ * 发送错误响应
339
+ */
340
+ private sendErrorResponse;
341
+ }
342
+
343
+ /**
344
+ * EndpointManager(新 API)
345
+ * 管理多个小智接入点的连接,每个小智接入点独立运行
346
+ *
347
+ * 与旧 EndpointManager 的区别:
348
+ * - 使用 addEndpoint(endpoint) 添加 Endpoint 实例
349
+ * - 不需要 initialize() 和 setServiceManager()
350
+ * - 配置更简洁直观
351
+ */
352
+
353
+ /**
354
+ * 小智接入点管理器(新 API)
355
+ * 负责管理多个小智接入点的连接
356
+ */
357
+ declare class EndpointManager extends EventEmitter {
358
+ private config?;
359
+ private endpoints;
360
+ private connectionStates;
361
+ /**
362
+ * 构造函数
363
+ *
364
+ * @param config - 可选的配置
365
+ */
366
+ constructor(config?: EndpointManagerConfig | undefined);
367
+ /**
368
+ * 添加 Endpoint 实例
369
+ *
370
+ * @param endpoint - Endpoint 实例
371
+ */
372
+ addEndpoint(endpoint: Endpoint): void;
373
+ /**
374
+ * 移除 Endpoint 实例
375
+ *
376
+ * @param endpoint - Endpoint 实例
377
+ */
378
+ removeEndpoint(endpoint: Endpoint): void;
379
+ /**
380
+ * 连接所有 Endpoint
381
+ */
382
+ connect(): Promise<void>;
383
+ /**
384
+ * 断开所有连接
385
+ */
386
+ disconnect(): Promise<void>;
387
+ /**
388
+ * 获取所有 Endpoint URL
389
+ */
390
+ getEndpoints(): string[];
391
+ /**
392
+ * 获取指定 Endpoint 实例
393
+ *
394
+ * @param url - Endpoint URL
395
+ */
396
+ getEndpoint(url: string): Endpoint | undefined;
397
+ /**
398
+ * 获取所有连接状态
399
+ */
400
+ getConnectionStatus(): SimpleConnectionStatus[];
401
+ /**
402
+ * 检查是否有任何连接处于连接状态
403
+ */
404
+ isAnyConnected(): boolean;
405
+ /**
406
+ * 检查指定端点是否已连接
407
+ *
408
+ * @param url - 端点 URL
409
+ */
410
+ isEndpointConnected(url: string): boolean;
411
+ /**
412
+ * 获取指定端点的状态
413
+ *
414
+ * @param url - 端点 URL
415
+ */
416
+ getEndpointStatus(url: string): SimpleConnectionStatus | undefined;
417
+ /**
418
+ * 重连所有端点
419
+ */
420
+ reconnectAll(): Promise<void>;
421
+ /**
422
+ * 重连指定的端点
423
+ *
424
+ * @param url - 要重连的端点 URL
425
+ */
426
+ reconnectEndpoint(url: string): Promise<void>;
427
+ /**
428
+ * 清除所有端点
429
+ */
430
+ clearEndpoints(): Promise<void>;
431
+ /**
432
+ * 清理资源
433
+ */
434
+ cleanup(): Promise<void>;
435
+ /**
436
+ * 连接单个端点
437
+ */
438
+ private connectSingleEndpoint;
439
+ /**
440
+ * 重连单个端点
441
+ */
442
+ private reconnectSingleEndpoint;
443
+ }
444
+
445
+ /**
446
+ * 工具函数模块
447
+ *
448
+ * 内联的必要工具函数,确保包的独立性
449
+ */
450
+
451
+ /**
452
+ * 截断端点 URL 用于日志显示
453
+ *
454
+ * @param endpoint - 完整的端点 URL
455
+ * @returns 截断后的 URL
456
+ *
457
+ * @example
458
+ * ```typescript
459
+ * sliceEndpoint("ws://very-long-endpoint-url-here.example.com/endpoint")
460
+ * // 返回: "ws://very-long-endpoint-u...e.com/endpoint"
461
+ * ```
462
+ */
463
+ declare function sliceEndpoint(endpoint: string): string;
464
+ /**
465
+ * 验证工具调用参数
466
+ *
467
+ * @param params - 待验证的参数
468
+ * @returns 验证后的参数
469
+ * @throws {Error} 如果参数无效
470
+ *
471
+ * @example
472
+ * ```typescript
473
+ * const params = validateToolCallParams({
474
+ * name: "test_tool",
475
+ * arguments: { foo: "bar" }
476
+ * });
477
+ * ```
478
+ */
479
+ declare function validateToolCallParams(params: unknown): ValidatedToolCallParams;
480
+ /**
481
+ * 验证端点 URL 格式
482
+ *
483
+ * @param endpoint - 待验证的端点 URL
484
+ * @returns 是否为有效的 WebSocket URL
485
+ */
486
+ declare function isValidEndpointUrl(endpoint: string): boolean;
487
+ /**
488
+ * 深度合并对象
489
+ *
490
+ * @param target - 目标对象
491
+ * @param sources - 源对象
492
+ * @returns 合并后的对象
493
+ */
494
+ declare function deepMerge<T>(target: Partial<T>, ...sources: Array<Partial<T>>): T;
495
+ /**
496
+ * 延迟执行
497
+ *
498
+ * @param ms - 延迟时间(毫秒)
499
+ * @returns Promise
500
+ */
501
+ declare function sleep(ms: number): Promise<void>;
502
+ /**
503
+ * 格式化错误消息
504
+ *
505
+ * @param error - 错误对象
506
+ * @returns 格式化后的错误消息
507
+ */
508
+ declare function formatErrorMessage(error: unknown): string;
509
+
510
+ /**
511
+ * MCP 消息类型定义
512
+ *
513
+ * 定义最小化的 MCP 协议消息类型
514
+ */
515
+ /**
516
+ * MCP 消息接口
517
+ */
518
+ interface MCPMessage {
519
+ /** JSON-RPC 版本 */
520
+ jsonrpc: "2.0";
521
+ /** 方法名 */
522
+ method: string;
523
+ /** 参数 */
524
+ params?: unknown;
525
+ /** 请求 ID */
526
+ id: string | number;
527
+ }
528
+ /**
529
+ * 扩展的 MCP 消息接口(用于响应)
530
+ */
531
+ interface ExtendedMCPMessage {
532
+ jsonrpc: "2.0";
533
+ id: string | number;
534
+ result?: unknown;
535
+ error?: {
536
+ code: number;
537
+ message: string;
538
+ data?: unknown;
539
+ };
540
+ }
541
+
542
+ export { type ConfigChangeEvent, type ConnectionOptions, ConnectionState, type ConnectionStatus, Endpoint, type EndpointConfig, type EndpointConnectionStatus, EndpointManager, type EndpointManagerConfig, type ExtendedMCPMessage, type IMCPServiceManager, type JSONSchema, type LocalMCPServerConfig, type MCPMessage, type MCPServerConfig, type ReconnectResult, type SSEMCPServerConfig, type SimpleConnectionStatus, type StreamableHTTPMCPServerConfig, ToolCallError, ToolCallErrorCode, type ToolCallParams, type ToolCallResult, type ValidatedToolCallParams, deepMerge, ensureToolJSONSchema, formatErrorMessage, isValidEndpointUrl, sleep, sliceEndpoint, validateToolCallParams };