@xiaozhi-client/config 1.9.4-beta.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 shenjingnan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,580 @@
1
+ interface LocalMCPServerConfig {
2
+ command: string;
3
+ args: string[];
4
+ env?: Record<string, string>;
5
+ }
6
+ interface SSEMCPServerConfig {
7
+ type: "sse";
8
+ url: string;
9
+ headers?: Record<string, string>;
10
+ }
11
+ interface StreamableHTTPMCPServerConfig {
12
+ type?: "streamable-http";
13
+ url: string;
14
+ headers?: Record<string, string>;
15
+ }
16
+ type MCPServerConfig = LocalMCPServerConfig | SSEMCPServerConfig | StreamableHTTPMCPServerConfig;
17
+ interface MCPToolConfig {
18
+ description?: string;
19
+ enable: boolean;
20
+ usageCount?: number;
21
+ lastUsedTime?: string;
22
+ }
23
+ interface MCPServerToolsConfig {
24
+ tools: Record<string, MCPToolConfig>;
25
+ }
26
+ interface ConnectionConfig {
27
+ heartbeatInterval?: number;
28
+ heartbeatTimeout?: number;
29
+ reconnectInterval?: number;
30
+ }
31
+ interface ModelScopeConfig {
32
+ apiKey?: string;
33
+ }
34
+ interface WebUIConfig {
35
+ port?: number;
36
+ autoRestart?: boolean;
37
+ }
38
+ interface ToolCallLogConfig {
39
+ maxRecords?: number;
40
+ logFilePath?: string;
41
+ }
42
+ interface ProxyHandlerConfig {
43
+ type: "proxy";
44
+ platform: "coze" | "openai" | "anthropic" | "custom";
45
+ config: {
46
+ workflow_id?: string;
47
+ bot_id?: string;
48
+ api_key?: string;
49
+ base_url?: string;
50
+ timeout?: number;
51
+ retry_count?: number;
52
+ retry_delay?: number;
53
+ headers?: Record<string, string>;
54
+ params?: Record<string, unknown>;
55
+ };
56
+ }
57
+ interface HttpHandlerConfig {
58
+ type: "http";
59
+ url: string;
60
+ method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
61
+ headers?: Record<string, string>;
62
+ timeout?: number;
63
+ retry_count?: number;
64
+ retry_delay?: number;
65
+ auth?: {
66
+ type: "bearer" | "basic" | "api_key";
67
+ token?: string;
68
+ username?: string;
69
+ password?: string;
70
+ api_key?: string;
71
+ api_key_header?: string;
72
+ };
73
+ body_template?: string;
74
+ response_mapping?: {
75
+ success_path?: string;
76
+ error_path?: string;
77
+ data_path?: string;
78
+ };
79
+ }
80
+ interface FunctionHandlerConfig {
81
+ type: "function";
82
+ module: string;
83
+ function: string;
84
+ timeout?: number;
85
+ context?: Record<string, unknown>;
86
+ }
87
+ interface ScriptHandlerConfig {
88
+ type: "script";
89
+ script: string;
90
+ interpreter?: "node" | "python" | "bash";
91
+ timeout?: number;
92
+ env?: Record<string, string>;
93
+ }
94
+ interface ChainHandlerConfig {
95
+ type: "chain";
96
+ tools: string[];
97
+ mode: "sequential" | "parallel";
98
+ error_handling: "stop" | "continue" | "retry";
99
+ }
100
+ interface MCPHandlerConfig {
101
+ type: "mcp";
102
+ config: {
103
+ serviceName: string;
104
+ toolName: string;
105
+ };
106
+ }
107
+ type HandlerConfig = ProxyHandlerConfig | HttpHandlerConfig | FunctionHandlerConfig | ScriptHandlerConfig | ChainHandlerConfig | MCPHandlerConfig;
108
+ interface CustomMCPTool {
109
+ name: string;
110
+ description: string;
111
+ inputSchema: Record<string, unknown>;
112
+ handler: HandlerConfig;
113
+ stats?: {
114
+ usageCount?: number;
115
+ lastUsedTime?: string;
116
+ };
117
+ }
118
+ interface CustomMCPConfig {
119
+ tools: CustomMCPTool[];
120
+ }
121
+ interface WebServerInstance {
122
+ broadcastConfigUpdate(config: AppConfig): void;
123
+ }
124
+ interface PlatformsConfig {
125
+ [platformName: string]: PlatformConfig;
126
+ }
127
+ interface PlatformConfig {
128
+ token?: string;
129
+ }
130
+ /**
131
+ * 扣子平台配置接口
132
+ */
133
+ interface CozePlatformConfig extends PlatformConfig {
134
+ /** 扣子 API Token */
135
+ token: string;
136
+ }
137
+ interface AppConfig {
138
+ mcpEndpoint: string | string[];
139
+ mcpServers: Record<string, MCPServerConfig>;
140
+ mcpServerConfig?: Record<string, MCPServerToolsConfig>;
141
+ customMCP?: CustomMCPConfig;
142
+ connection?: ConnectionConfig;
143
+ modelscope?: ModelScopeConfig;
144
+ webUI?: WebUIConfig;
145
+ platforms?: PlatformsConfig;
146
+ toolCallLog?: ToolCallLogConfig;
147
+ }
148
+ /**
149
+ * 配置管理类
150
+ * 负责管理应用配置,提供只读访问和安全的配置更新功能
151
+ */
152
+ declare class ConfigManager {
153
+ private static instance;
154
+ private defaultConfigPath;
155
+ private config;
156
+ private currentConfigPath;
157
+ private json5Writer;
158
+ private statsUpdateLocks;
159
+ private statsUpdateLockTimeouts;
160
+ private readonly STATS_UPDATE_TIMEOUT;
161
+ private eventCallbacks;
162
+ private constructor();
163
+ /**
164
+ * 注册事件监听器
165
+ */
166
+ on(eventName: string, callback: (data: unknown) => void): void;
167
+ /**
168
+ * 发射事件
169
+ */
170
+ private emitEvent;
171
+ /**
172
+ * 获取配置文件路径(动态计算)
173
+ * 支持多种配置文件格式:json5 > jsonc > json
174
+ */
175
+ private getConfigFilePath;
176
+ /**
177
+ * 获取配置文件格式
178
+ */
179
+ private getConfigFileFormat;
180
+ /**
181
+ * 获取配置管理器单例实例
182
+ */
183
+ static getInstance(): ConfigManager;
184
+ /**
185
+ * 检查配置文件是否存在
186
+ */
187
+ configExists(): boolean;
188
+ /**
189
+ * 初始化配置文件
190
+ * 从 config.default.json 复制到 config.json
191
+ * @param format 配置文件格式,默认为 json
192
+ */
193
+ initConfig(format?: "json" | "json5" | "jsonc"): void;
194
+ /**
195
+ * 加载配置文件
196
+ */
197
+ private loadConfig;
198
+ /**
199
+ * 验证配置文件结构
200
+ */
201
+ validateConfig(config: unknown): void;
202
+ /**
203
+ * 获取配置(只读)
204
+ */
205
+ getConfig(): Readonly<AppConfig>;
206
+ /**
207
+ * 获取可修改的配置对象(内部使用,保留注释信息)
208
+ */
209
+ private getMutableConfig;
210
+ /**
211
+ * 获取 MCP 端点(向后兼容)
212
+ * @deprecated 使用 getMcpEndpoints() 获取所有端点
213
+ */
214
+ getMcpEndpoint(): string;
215
+ /**
216
+ * 获取所有 MCP 端点
217
+ */
218
+ getMcpEndpoints(): string[];
219
+ /**
220
+ * 获取 MCP 服务配置
221
+ */
222
+ getMcpServers(): Readonly<Record<string, MCPServerConfig>>;
223
+ /**
224
+ * 获取 MCP 服务工具配置
225
+ */
226
+ getMcpServerConfig(): Readonly<Record<string, MCPServerToolsConfig>>;
227
+ /**
228
+ * 获取指定服务的工具配置
229
+ */
230
+ getServerToolsConfig(serverName: string): Readonly<Record<string, MCPToolConfig>>;
231
+ /**
232
+ * 检查工具是否启用
233
+ */
234
+ isToolEnabled(serverName: string, toolName: string): boolean;
235
+ /**
236
+ * 更新 MCP 端点(支持字符串或数组)
237
+ */
238
+ updateMcpEndpoint(endpoint: string | string[]): void;
239
+ /**
240
+ * 添加 MCP 端点
241
+ */
242
+ addMcpEndpoint(endpoint: string): void;
243
+ /**
244
+ * 移除 MCP 端点
245
+ */
246
+ removeMcpEndpoint(endpoint: string): void;
247
+ /**
248
+ * 更新 MCP 服务配置
249
+ */
250
+ updateMcpServer(serverName: string, serverConfig: MCPServerConfig): void;
251
+ /**
252
+ * 删除 MCP 服务配置
253
+ */
254
+ removeMcpServer(serverName: string): void;
255
+ /**
256
+ * 批量更新配置(由 Handler 调用)
257
+ */
258
+ updateConfig(newConfig: Partial<AppConfig>): void;
259
+ /**
260
+ * 更新服务工具配置
261
+ */
262
+ updateServerToolsConfig(serverName: string, toolsConfig: Record<string, MCPToolConfig>): void;
263
+ /**
264
+ * 删除指定服务器的工具配置
265
+ */
266
+ removeServerToolsConfig(serverName: string): void;
267
+ /**
268
+ * 清理无效的服务器工具配置
269
+ * 删除在 mcpServerConfig 中存在但在 mcpServers 中不存在的服务配置
270
+ */
271
+ cleanupInvalidServerToolsConfig(): void;
272
+ /**
273
+ * 设置工具启用状态
274
+ */
275
+ setToolEnabled(serverName: string, toolName: string, enabled: boolean, description?: string): void;
276
+ /**
277
+ * 保存配置到文件
278
+ * 保存到原始配置文件路径,保持文件格式一致性
279
+ */
280
+ private saveConfig;
281
+ /**
282
+ * 重新加载配置(清除缓存)
283
+ */
284
+ reloadConfig(): void;
285
+ /**
286
+ * 获取配置文件路径
287
+ */
288
+ getConfigPath(): string;
289
+ /**
290
+ * 获取默认配置文件路径
291
+ */
292
+ getDefaultConfigPath(): string;
293
+ /**
294
+ * 获取连接配置(包含默认值)
295
+ */
296
+ getConnectionConfig(): Required<ConnectionConfig>;
297
+ /**
298
+ * 获取心跳检测间隔(毫秒)
299
+ */
300
+ getHeartbeatInterval(): number;
301
+ /**
302
+ * 获取心跳超时时间(毫秒)
303
+ */
304
+ getHeartbeatTimeout(): number;
305
+ /**
306
+ * 获取重连间隔(毫秒)
307
+ */
308
+ getReconnectInterval(): number;
309
+ /**
310
+ * 更新连接配置
311
+ */
312
+ updateConnectionConfig(connectionConfig: Partial<ConnectionConfig>): void;
313
+ /**
314
+ * 更新工具使用统计信息(MCP 服务工具)
315
+ * @param serverName 服务名称
316
+ * @param toolName 工具名称
317
+ * @param callTime 调用时间(ISO 8601 格式)
318
+ */
319
+ updateToolUsageStats(serverName: string, toolName: string, callTime: string): Promise<void>;
320
+ /**
321
+ * 更新工具使用统计信息(CustomMCP 工具)
322
+ * @param toolName 工具名称(customMCP 工具名称)
323
+ * @param incrementUsageCount 是否增加使用计数,默认为 true
324
+ */
325
+ updateToolUsageStats(toolName: string, incrementUsageCount?: boolean): Promise<void>;
326
+ /**
327
+ * 更新 MCP 服务工具统计信息(重载方法)
328
+ * @param serviceName 服务名称
329
+ * @param toolName 工具名称
330
+ * @param callTime 调用时间(ISO 8601 格式)
331
+ * @param incrementUsageCount 是否增加使用计数,默认为 true
332
+ */
333
+ updateMCPServerToolStats(serviceName: string, toolName: string, callTime: string, incrementUsageCount?: boolean): Promise<void>;
334
+ /**
335
+ * 设置心跳检测间隔
336
+ */
337
+ setHeartbeatInterval(interval: number): void;
338
+ /**
339
+ * 设置心跳超时时间
340
+ */
341
+ setHeartbeatTimeout(timeout: number): void;
342
+ /**
343
+ * 设置重连间隔
344
+ */
345
+ setReconnectInterval(interval: number): void;
346
+ /**
347
+ * 获取 ModelScope 配置
348
+ */
349
+ getModelScopeConfig(): Readonly<ModelScopeConfig>;
350
+ /**
351
+ * 获取 ModelScope API Key
352
+ * 优先从配置文件读取,其次从环境变量读取
353
+ */
354
+ getModelScopeApiKey(): string | undefined;
355
+ /**
356
+ * 更新 ModelScope 配置
357
+ */
358
+ updateModelScopeConfig(modelScopeConfig: Partial<ModelScopeConfig>): void;
359
+ /**
360
+ * 设置 ModelScope API Key
361
+ */
362
+ setModelScopeApiKey(apiKey: string): void;
363
+ /**
364
+ * 获取 customMCP 配置
365
+ */
366
+ getCustomMCPConfig(): CustomMCPConfig | null;
367
+ /**
368
+ * 获取 customMCP 工具列表
369
+ */
370
+ getCustomMCPTools(): CustomMCPTool[];
371
+ /**
372
+ * 验证 customMCP 工具配置
373
+ */
374
+ validateCustomMCPTools(tools: CustomMCPTool[]): boolean;
375
+ /**
376
+ * 验证处理器配置
377
+ */
378
+ private validateHandlerConfig;
379
+ /**
380
+ * 验证代理处理器配置
381
+ */
382
+ private validateProxyHandler;
383
+ /**
384
+ * 验证 HTTP 处理器配置
385
+ */
386
+ private validateHttpHandler;
387
+ /**
388
+ * 验证函数处理器配置
389
+ */
390
+ private validateFunctionHandler;
391
+ /**
392
+ * 验证脚本处理器配置
393
+ */
394
+ private validateScriptHandler;
395
+ /**
396
+ * 验证链式处理器配置
397
+ */
398
+ private validateChainHandler;
399
+ /**
400
+ * 验证 MCP 处理器配置
401
+ */
402
+ private validateMCPHandler;
403
+ /**
404
+ * 检查是否配置了有效的 customMCP 工具
405
+ */
406
+ hasValidCustomMCPTools(): boolean;
407
+ /**
408
+ * 添加自定义 MCP 工具
409
+ */
410
+ addCustomMCPTool(tool: CustomMCPTool): void;
411
+ /**
412
+ * 批量添加自定义 MCP 工具
413
+ * @param tools 要添加的工具数组
414
+ */
415
+ addCustomMCPTools(tools: CustomMCPTool[]): Promise<void>;
416
+ /**
417
+ * 删除自定义 MCP 工具
418
+ */
419
+ removeCustomMCPTool(toolName: string): void;
420
+ /**
421
+ * 更新单个自定义 MCP 工具配置
422
+ * @param toolName 工具名称
423
+ * @param updatedTool 更新后的工具配置
424
+ */
425
+ updateCustomMCPTool(toolName: string, updatedTool: CustomMCPTool): void;
426
+ /**
427
+ * 更新自定义 MCP 工具配置
428
+ */
429
+ updateCustomMCPTools(tools: CustomMCPTool[]): void;
430
+ /**
431
+ * 获取 Web UI 配置
432
+ */
433
+ getWebUIConfig(): Readonly<WebUIConfig>;
434
+ /**
435
+ * 获取 Web UI 端口号
436
+ */
437
+ getWebUIPort(): number;
438
+ /**
439
+ * 通知 Web 界面配置已更新
440
+ * 如果 Web 服务器正在运行,通过 WebSocket 广播配置更新
441
+ */
442
+ private notifyConfigUpdate;
443
+ /**
444
+ * 更新 Web UI 配置
445
+ */
446
+ updateWebUIConfig(webUIConfig: Partial<WebUIConfig>): void;
447
+ /**
448
+ * 设置 Web UI 端口号
449
+ */
450
+ setWebUIPort(port: number): void;
451
+ updatePlatformConfig(platformName: string, platformConfig: PlatformConfig): void;
452
+ /**
453
+ * 获取扣子平台配置
454
+ */
455
+ getCozePlatformConfig(): CozePlatformConfig | null;
456
+ /**
457
+ * 获取扣子 API Token
458
+ */
459
+ getCozeToken(): string | null;
460
+ /**
461
+ * 设置扣子平台配置
462
+ */
463
+ setCozePlatformConfig(config: CozePlatformConfig): void;
464
+ /**
465
+ * 检查扣子平台配置是否有效
466
+ */
467
+ isCozeConfigValid(): boolean;
468
+ /**
469
+ * 更新 mcpServerConfig 中的工具使用统计信息(内部实现)
470
+ * @param serverName 服务名称
471
+ * @param toolName 工具名称
472
+ * @param callTime 调用时间(ISO 8601 格式)
473
+ * @param incrementUsageCount 是否增加使用计数
474
+ * @private
475
+ */
476
+ private _updateMCPServerToolStats;
477
+ /**
478
+ * 更新 customMCP 中的工具使用统计信息(服务名+工具名版本)
479
+ * @param serverName 服务名称
480
+ * @param toolName 工具名称
481
+ * @param callTime 调用时间(ISO 8601 格式)
482
+ * @private
483
+ */
484
+ private updateCustomMCPToolStats;
485
+ /**
486
+ * 获取统计更新锁(确保同一工具的统计更新串行执行)
487
+ * @param toolKey 工具键
488
+ * @private
489
+ */
490
+ private acquireStatsUpdateLock;
491
+ /**
492
+ * 释放统计更新锁
493
+ * @param toolKey 工具键
494
+ * @private
495
+ */
496
+ private releaseStatsUpdateLock;
497
+ /**
498
+ * 带并发控制的工具统计更新(CustomMCP 工具)
499
+ * @param toolName 工具名称
500
+ * @param incrementUsageCount 是否增加使用计数
501
+ */
502
+ updateToolUsageStatsWithLock(toolName: string, incrementUsageCount?: boolean): Promise<void>;
503
+ /**
504
+ * 带并发控制的工具统计更新(MCP 服务工具)
505
+ * @param serviceName 服务名称
506
+ * @param toolName 工具名称
507
+ * @param callTime 调用时间
508
+ * @param incrementUsageCount 是否增加使用计数
509
+ */
510
+ updateMCPServerToolStatsWithLock(serviceName: string, toolName: string, callTime: string, incrementUsageCount?: boolean): Promise<void>;
511
+ /**
512
+ * 清理所有统计更新锁(用于异常恢复)
513
+ */
514
+ clearAllStatsUpdateLocks(): void;
515
+ /**
516
+ * 获取统计更新锁状态(用于调试和监控)
517
+ */
518
+ getStatsUpdateLocks(): string[];
519
+ /**
520
+ * 获取工具调用日志配置
521
+ */
522
+ getToolCallLogConfig(): Readonly<ToolCallLogConfig>;
523
+ /**
524
+ * 更新工具调用日志配置
525
+ */
526
+ updateToolCallLogConfig(toolCallLogConfig: Partial<ToolCallLogConfig>): void;
527
+ /**
528
+ * 获取配置目录路径(与配置文件同级目录)
529
+ */
530
+ getConfigDir(): string;
531
+ }
532
+ declare const configManager: ConfigManager;
533
+
534
+ /**
535
+ * 配置适配器
536
+ * 将旧的配置格式转换为新的 MCPServiceConfig 格式,确保向后兼容性
537
+ */
538
+
539
+ /**
540
+ * 配置验证错误类
541
+ */
542
+ declare class ConfigValidationError extends Error {
543
+ readonly configName?: string | undefined;
544
+ constructor(message: string, configName?: string | undefined);
545
+ }
546
+ declare enum MCPTransportType {
547
+ STDIO = "stdio",
548
+ SSE = "sse",
549
+ STREAMABLE_HTTP = "streamable-http"
550
+ }
551
+ interface MCPServiceConfig {
552
+ name: string;
553
+ type: MCPTransportType;
554
+ command?: string;
555
+ args?: string[];
556
+ env?: Record<string, string>;
557
+ url?: string;
558
+ timeout?: number;
559
+ headers?: Record<string, string>;
560
+ modelScopeAuth?: boolean;
561
+ }
562
+ /**
563
+ * 将旧的 MCPServerConfig 转换为新的 MCPServiceConfig
564
+ */
565
+ declare function convertLegacyToNew(serviceName: string, legacyConfig: MCPServerConfig): MCPServiceConfig;
566
+ /**
567
+ * 批量转换配置
568
+ */
569
+ declare function convertLegacyConfigBatch(legacyConfigs: Record<string, MCPServerConfig>): Record<string, MCPServiceConfig>;
570
+ /**
571
+ * 检查是否为 ModelScope URL
572
+ * 使用 URL hostname 检查而非简单的字符串包含检查,防止安全绕过
573
+ */
574
+ declare function isModelScopeURL(url: string): boolean;
575
+ /**
576
+ * 获取配置类型描述
577
+ */
578
+ declare function getConfigTypeDescription(config: MCPServerConfig): string;
579
+
580
+ export { type AppConfig, type ChainHandlerConfig, ConfigManager, ConfigValidationError, type ConnectionConfig, type CozePlatformConfig, type CustomMCPConfig, type CustomMCPTool, type FunctionHandlerConfig, type HandlerConfig, type HttpHandlerConfig, type LocalMCPServerConfig, type MCPHandlerConfig, type MCPServerConfig, type MCPServerToolsConfig, type MCPServiceConfig, type MCPToolConfig, MCPTransportType, type ModelScopeConfig, type PlatformConfig, type PlatformsConfig, type ProxyHandlerConfig, type SSEMCPServerConfig, type ScriptHandlerConfig, type StreamableHTTPMCPServerConfig, type ToolCallLogConfig, type WebServerInstance, type WebUIConfig, configManager, convertLegacyConfigBatch, convertLegacyToNew, getConfigTypeDescription, isModelScopeURL };