xiaozhi-client 1.6.4-beta.21 → 1.6.4-beta.22
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/Logger.d.ts +94 -0
- package/dist/Logger.js +3 -0
- package/dist/Logger.js.map +1 -0
- package/dist/MCPService-Tcm30Q3R.d.ts +237 -0
- package/dist/ProxyMCPServer.d.ts +294 -0
- package/dist/ProxyMCPServer.js +3 -0
- package/dist/ProxyMCPServer.js.map +1 -0
- package/dist/WebServer.d.ts +83 -0
- package/dist/WebServer.js +70 -0
- package/dist/WebServer.js.map +1 -0
- package/dist/WebServerStandalone.d.ts +1 -0
- package/dist/WebServerStandalone.js +71 -0
- package/dist/WebServerStandalone.js.map +1 -0
- package/dist/adapters/ConfigAdapter.d.ts +30 -0
- package/dist/adapters/ConfigAdapter.js +3 -0
- package/dist/adapters/ConfigAdapter.js.map +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +103 -0
- package/dist/cli.js.map +1 -0
- package/dist/configManager.d.ts +253 -0
- package/dist/configManager.js +3 -0
- package/dist/configManager.js.map +1 -0
- package/dist/mcpCommands.d.ts +27 -0
- package/dist/mcpCommands.js +3 -0
- package/dist/mcpCommands.js.map +1 -0
- package/dist/mcpServerProxy.d.ts +12 -0
- package/dist/mcpServerProxy.js +11 -0
- package/dist/mcpServerProxy.js.map +1 -0
- package/dist/package.json +109 -0
- package/dist/services/MCPServer.d.ts +265 -0
- package/dist/services/MCPServer.js +8 -0
- package/dist/services/MCPServer.js.map +1 -0
- package/dist/templates/default/mcpServers/calculator.js +106 -0
- package/dist/templates/default/mcpServers/datetime.js +390 -0
- package/dist/templates/default/package.json +13 -0
- package/dist/templates/default/xiaozhi.config.json +21 -0
- package/dist/templates/docker/mcpServers/calculator.js +106 -0
- package/dist/templates/docker/mcpServers/datetime.js +390 -0
- package/dist/templates/docker/package.json +13 -0
- package/dist/templates/docker/requirements.txt +16 -0
- package/dist/templates/docker/xiaozhi.config.json +21 -0
- package/dist/templates/hello-world/mcpServers/calculator.js +106 -0
- package/dist/templates/hello-world/mcpServers/datetime.js +390 -0
- package/dist/templates/hello-world/package.json +13 -0
- package/dist/templates/hello-world/xiaozhi.config.json +21 -0
- package/dist/templates/json5/mcpServers/calculator.js +106 -0
- package/dist/templates/json5/mcpServers/datetime.js +390 -0
- package/dist/templates/json5/package.json +13 -0
- package/dist/templates/json5/xiaozhi.config.json5 +41 -0
- package/dist/templates/jsonc/mcpServers/calculator.js +106 -0
- package/dist/templates/jsonc/mcpServers/datetime.js +390 -0
- package/dist/templates/jsonc/package.json +13 -0
- package/dist/templates/jsonc/xiaozhi.config.jsonc +41 -0
- package/dist/templates/modelscope/xiaozhi.config.json +20 -0
- package/package.json +1 -1
- package/web/dist/assets/form-utils-BAESeaiO.js +22 -0
- package/web/dist/assets/form-utils-BAESeaiO.js.map +1 -0
- package/web/dist/assets/index-CCieCKu1.js +31 -0
- package/web/dist/assets/index-CCieCKu1.js.map +1 -0
- package/web/dist/assets/index-eZDZS6qd.css +1 -0
- package/web/dist/assets/radix-ui-DW48STyt.js +2 -0
- package/web/dist/assets/radix-ui-DW48STyt.js.map +1 -0
- package/web/dist/assets/react-vendor-Bm2cC0fo.js +184 -0
- package/web/dist/assets/react-vendor-Bm2cC0fo.js.map +1 -0
- package/web/dist/assets/utils-DE0xezXx.js +2 -0
- package/web/dist/assets/utils-DE0xezXx.js.map +1 -0
- package/web/dist/assets/vendor-C4NtkA-O.js +10 -0
- package/web/dist/assets/vendor-C4NtkA-O.js.map +1 -0
- package/web/dist/index.html +19 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
var He=Object.defineProperty;var cn=Object.getOwnPropertyDescriptor;var ln=Object.getOwnPropertyNames;var gn=Object.prototype.hasOwnProperty;var a=(i,e)=>He(i,"name",{value:e,configurable:!0});var g=(i,e)=>()=>(i&&(e=i(i=0)),e);var H=(i,e)=>{for(var t in e)He(i,t,{get:e[t],enumerable:!0})},hn=(i,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of ln(e))!gn.call(i,n)&&n!==t&&He(i,n,{get:()=>e[n],enumerable:!(r=cn(e,n))||r.enumerable});return i};var V=i=>hn(He({},"__esModule",{value:!0}),i);import*as x from"fs";import*as F from"path";import Me from"chalk";import de from"pino";function pn(i){let e=i.getFullYear(),t=String(i.getMonth()+1).padStart(2,"0"),r=String(i.getDate()).padStart(2,"0"),n=String(i.getHours()).padStart(2,"0"),o=String(i.getMinutes()).padStart(2,"0"),s=String(i.getSeconds()).padStart(2,"0");return`${e}-${t}-${r} ${n}:${o}:${s}`}var Pe,h,C=g(()=>{"use strict";a(pn,"formatDateTime");Pe=class{static{a(this,"Logger")}logFilePath=null;pinoInstance;isDaemonMode;maxLogFileSize=10*1024*1024;maxLogFiles=5;constructor(){this.isDaemonMode=process.env.XIAOZHI_DAEMON==="true",this.pinoInstance=this.createPinoInstance()}createPinoInstance(){let e=[];if(!this.isDaemonMode){let t=this.createOptimizedConsoleStream();e.push({level:"debug",stream:t})}return this.logFilePath&&e.push({level:"debug",stream:de.destination({dest:this.logFilePath,sync:!1,append:!0,mkdir:!0})}),e.length===0&&e.push({level:"debug",stream:de.destination({dest:"/dev/null"})}),de({level:"debug",timestamp:de.stdTimeFunctions?.isoTime||(()=>`,"time":${Date.now()}`),formatters:{level:a((t,r)=>({level:r}),"level")},base:null,serializers:{err:de.stdSerializers?.err||(t=>t)}},de.multistream(e,{dedupe:!0}))}createOptimizedConsoleStream(){let e=new Map([[20,{name:"DEBUG",color:Me.gray}],[30,{name:"INFO",color:Me.blue}],[40,{name:"WARN",color:Me.yellow}],[50,{name:"ERROR",color:Me.red}],[60,{name:"FATAL",color:Me.red}]]);return{write:a(t=>{try{let r=JSON.parse(t),n=this.formatConsoleMessageOptimized(r,e);this.safeWrite(`${n}
|
|
3
|
+
`)}catch{this.safeWrite(t)}},"write")}}safeWrite(e){try{process.stderr&&typeof process.stderr.write=="function"?process.stderr.write(e):console&&typeof console.error=="function"&&console.error(e.trim())}catch{}}formatConsoleMessageOptimized(e,t){let r=pn(new Date),n=t.get(e.level)||{name:"UNKNOWN",color:a(c=>c,"color")},o=n.color(`[${n.name}]`),s=e.msg;if(e.args&&Array.isArray(e.args)){let c=e.args.map(l=>typeof l=="object"?JSON.stringify(l):String(l)).join(" ");s=`${s} ${c}`}return`[${r}] ${o} ${s}`}initLogFile(e){this.logFilePath=F.join(e,"xiaozhi.log"),this.rotateLogFileIfNeeded(),x.existsSync(this.logFilePath)||x.writeFileSync(this.logFilePath,""),this.pinoInstance=this.createPinoInstance()}enableFileLogging(e){e&&this.logFilePath&&(this.pinoInstance=this.createPinoInstance())}info(e,...t){typeof e=="string"?t.length===0?this.pinoInstance.info(e):this.pinoInstance.info({args:t},e):this.pinoInstance.info(e,t[0]||"")}success(e,...t){typeof e=="string"?t.length===0?this.pinoInstance.info(e):this.pinoInstance.info({args:t},e):this.pinoInstance.info(e,t[0]||"")}warn(e,...t){typeof e=="string"?t.length===0?this.pinoInstance.warn(e):this.pinoInstance.warn({args:t},e):this.pinoInstance.warn(e,t[0]||"")}error(e,...t){if(typeof e=="string")if(t.length===0)this.pinoInstance.error(e);else{let r=t.map(n=>n instanceof Error?{message:n.message,stack:n.stack,name:n.name,cause:n.cause}:n);this.pinoInstance.error({args:r},e)}else{let r=this.enhanceErrorObject(e);this.pinoInstance.error(r,t[0]||"")}}debug(e,...t){typeof e=="string"?t.length===0?this.pinoInstance.debug(e):this.pinoInstance.debug({args:t},e):this.pinoInstance.debug(e,t[0]||"")}log(e,...t){typeof e=="string"?t.length===0?this.pinoInstance.info(e):this.pinoInstance.info({args:t},e):this.pinoInstance.info(e,t[0]||"")}enhanceErrorObject(e){let t={...e};for(let[r,n]of Object.entries(t))n instanceof Error&&(t[r]={message:n.message,stack:n.stack,name:n.name,cause:n.cause});return t}rotateLogFileIfNeeded(){if(!(!this.logFilePath||!x.existsSync(this.logFilePath)))try{x.statSync(this.logFilePath).size>this.maxLogFileSize&&this.rotateLogFile()}catch{}}rotateLogFile(){if(this.logFilePath)try{let e=F.dirname(this.logFilePath),t=F.basename(this.logFilePath,".log");for(let n=this.maxLogFiles-1;n>=1;n--){let o=F.join(e,`${t}.${n}.log`),s=F.join(e,`${t}.${n+1}.log`);x.existsSync(o)&&(n===this.maxLogFiles-1?x.unlinkSync(o):x.renameSync(o,s))}let r=F.join(e,`${t}.1.log`);x.renameSync(this.logFilePath,r)}catch{}}cleanupOldLogs(){if(this.logFilePath)try{let e=F.dirname(this.logFilePath),t=F.basename(this.logFilePath,".log");for(let r=this.maxLogFiles+1;r<=this.maxLogFiles+10;r++){let n=F.join(e,`${t}.${r}.log`);x.existsSync(n)&&x.unlinkSync(n)}}catch{}}setLogFileOptions(e,t){this.maxLogFileSize=e,this.maxLogFiles=t}withTag(e){return this}close(){}},h=new Pe});function dn(i){if(!i||typeof i!="object")throw new Error("\u670D\u52A1\u914D\u7F6E\u5FC5\u987B\u662F\u4E00\u4E2A\u6709\u6548\u7684\u5BF9\u8C61");if("command"in i&&typeof i.command=="string")return"stdio";if("type"in i&&i.type==="sse")return"sse";if("type"in i&&i.type==="streamable-http"||"url"in i&&typeof i.url=="string")return"streamable-http";throw new Error("\u65E0\u6CD5\u8BC6\u522B\u7684 MCP \u670D\u52A1\u914D\u7F6E\u7C7B\u578B\u3002\u914D\u7F6E\u5FC5\u987B\u5305\u542B command \u5B57\u6BB5\uFF08stdio\uFF09\u3001type: 'sse' \u5B57\u6BB5\uFF08sse\uFF09\u6216 url \u5B57\u6BB5\uFF08streamable-http\uFF09")}function ut(i,e){if(!e||typeof e!="object")return{valid:!1,error:`\u670D\u52A1 "${i}" \u7684\u914D\u7F6E\u5FC5\u987B\u662F\u4E00\u4E2A\u5BF9\u8C61`};try{switch(dn(e)){case"stdio":if(!e.command||typeof e.command!="string")return{valid:!1,error:`\u670D\u52A1 "${i}" \u7F3A\u5C11\u5FC5\u9700\u7684 command \u5B57\u6BB5\u6216\u5B57\u6BB5\u7C7B\u578B\u4E0D\u6B63\u786E`};if(!Array.isArray(e.args))return{valid:!1,error:`\u670D\u52A1 "${i}" \u7684 args \u5B57\u6BB5\u5FC5\u987B\u662F\u6570\u7EC4`};if(e.env&&typeof e.env!="object")return{valid:!1,error:`\u670D\u52A1 "${i}" \u7684 env \u5B57\u6BB5\u5FC5\u987B\u662F\u5BF9\u8C61`};break;case"sse":if(e.type!=="sse")return{valid:!1,error:`\u670D\u52A1 "${i}" \u7684 type \u5B57\u6BB5\u5FC5\u987B\u662F "sse"`};if(!e.url||typeof e.url!="string")return{valid:!1,error:`\u670D\u52A1 "${i}" \u7F3A\u5C11\u5FC5\u9700\u7684 url \u5B57\u6BB5\u6216\u5B57\u6BB5\u7C7B\u578B\u4E0D\u6B63\u786E`};break;case"streamable-http":if(!e.url||typeof e.url!="string")return{valid:!1,error:`\u670D\u52A1 "${i}" \u7F3A\u5C11\u5FC5\u9700\u7684 url \u5B57\u6BB5\u6216\u5B57\u6BB5\u7C7B\u578B\u4E0D\u6B63\u786E`};if(e.type&&e.type!=="streamable-http")return{valid:!1,error:`\u670D\u52A1 "${i}" \u7684 type \u5B57\u6BB5\u5982\u679C\u5B58\u5728\uFF0C\u5FC5\u987B\u662F "streamable-http"`};break;default:return{valid:!1,error:`\u670D\u52A1 "${i}" \u7684\u914D\u7F6E\u7C7B\u578B\u65E0\u6CD5\u8BC6\u522B`}}return{valid:!0}}catch(t){return{valid:!1,error:`\u670D\u52A1 "${i}" \u7684\u914D\u7F6E\u65E0\u6548: ${t instanceof Error?t.message:"\u672A\u77E5\u9519\u8BEF"}`}}}var Qt=g(()=>{"use strict";a(dn,"getMcpServerCommunicationType");a(ut,"validateMcpServerConfig")});import{copyFileSync as un,existsSync as je,readFileSync as mn,writeFileSync as fn}from"fs";import{dirname as vn,resolve as oe}from"path";import{fileURLToPath as Sn}from"url";import*as Fe from"comment-json";import Cn from"dayjs";import mt from"json5";import*as er from"json5-writer";var Yt,ft,vt,d,ie=g(()=>{"use strict";C();Qt();Yt=vn(Sn(import.meta.url)),ft={heartbeatInterval:3e4,heartbeatTimeout:1e4,reconnectInterval:5e3},vt=class i{static{a(this,"ConfigManager")}static instance;defaultConfigPath;config=null;currentConfigPath=null;json5Writer=null;constructor(){let e=[oe(Yt,"templates","default","xiaozhi.config.json"),oe(Yt,"..","templates","default","xiaozhi.config.json"),oe(process.cwd(),"templates","default","xiaozhi.config.json")];this.defaultConfigPath=e.find(t=>je(t))||e[0]}getConfigFilePath(){let e=process.env.XIAOZHI_CONFIG_DIR||process.cwd(),t=["xiaozhi.config.json5","xiaozhi.config.jsonc","xiaozhi.config.json"];for(let r of t){let n=oe(e,r);if(je(n))return n}return oe(e,"xiaozhi.config.json")}getConfigFileFormat(e){return e.endsWith(".json5")?"json5":e.endsWith(".jsonc")?"jsonc":"json"}static getInstance(){return i.instance||(i.instance=new i),i.instance}configExists(){let e=process.env.XIAOZHI_CONFIG_DIR||process.cwd(),t=["xiaozhi.config.json5","xiaozhi.config.jsonc","xiaozhi.config.json"];for(let r of t){let n=oe(e,r);if(je(n))return!0}return!1}initConfig(e="json"){if(!je(this.defaultConfigPath))throw new Error(`\u9ED8\u8BA4\u914D\u7F6E\u6A21\u677F\u6587\u4EF6\u4E0D\u5B58\u5728: ${this.defaultConfigPath}`);if(this.configExists())throw new Error("\u914D\u7F6E\u6587\u4EF6\u5DF2\u5B58\u5728\uFF0C\u65E0\u9700\u91CD\u590D\u521D\u59CB\u5316");let t=process.env.XIAOZHI_CONFIG_DIR||process.cwd(),r=`xiaozhi.config.${e}`,n=oe(t,r);un(this.defaultConfigPath,n),this.config=null,this.json5Writer=null}loadConfig(){if(!this.configExists())throw new Error("\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8BF7\u5148\u8FD0\u884C xiaozhi init \u521D\u59CB\u5316\u914D\u7F6E");try{let e=this.getConfigFilePath();this.currentConfigPath=e;let t=this.getConfigFileFormat(e),n=mn(e,"utf8").replace(/^\uFEFF/,""),o;switch(t){case"json5":o=mt.parse(n),this.json5Writer=er.load(n);break;case"jsonc":o=Fe.parse(n);break;default:o=JSON.parse(n);break}return this.validateConfig(o),o}catch(e){throw e instanceof SyntaxError?new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF: ${e.message}`):e}}validateConfig(e){if(!e||typeof e!="object")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1A\u6839\u5BF9\u8C61\u65E0\u6548");let t=e;if(t.mcpEndpoint===void 0||t.mcpEndpoint===null)throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpEndpoint \u5B57\u6BB5\u65E0\u6548");if(typeof t.mcpEndpoint!="string")if(Array.isArray(t.mcpEndpoint)){if(t.mcpEndpoint.length===0)throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpEndpoint \u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A");for(let r of t.mcpEndpoint)if(typeof r!="string"||r.trim()==="")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpEndpoint \u6570\u7EC4\u4E2D\u7684\u6BCF\u4E2A\u5143\u7D20\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32")}else throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpEndpoint \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6216\u5B57\u7B26\u4E32\u6570\u7EC4");if(!t.mcpServers||typeof t.mcpServers!="object")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers \u5B57\u6BB5\u65E0\u6548");for(let[r,n]of Object.entries(t.mcpServers)){if(!n||typeof n!="object")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${r} \u65E0\u6548`);let o=ut(r,n);if(!o.valid)throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1A${o.error}`)}}getConfig(){return this.config=this.loadConfig(),JSON.parse(JSON.stringify(this.config))}getMutableConfig(){return this.config||(this.config=this.loadConfig()),this.config}getMcpEndpoint(){let e=this.getConfig();return Array.isArray(e.mcpEndpoint)?e.mcpEndpoint[0]||"":e.mcpEndpoint}getMcpEndpoints(){let e=this.getConfig();return Array.isArray(e.mcpEndpoint)?[...e.mcpEndpoint]:e.mcpEndpoint?[e.mcpEndpoint]:[]}getMcpServers(){return this.getConfig().mcpServers}getMcpServerConfig(){return this.getConfig().mcpServerConfig||{}}getServerToolsConfig(e){return this.getMcpServerConfig()[e]?.tools||{}}isToolEnabled(e,t){return this.getServerToolsConfig(e)[t]?.enable!==!1}updateMcpEndpoint(e){if(Array.isArray(e)){if(e.length===0)throw new Error("MCP \u7AEF\u70B9\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A");for(let r of e)if(!r||typeof r!="string")throw new Error("MCP \u7AEF\u70B9\u6570\u7EC4\u4E2D\u7684\u6BCF\u4E2A\u5143\u7D20\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32")}else if(!e||typeof e!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let t=this.getMutableConfig();t.mcpEndpoint=e,this.saveConfig(t)}addMcpEndpoint(e){if(!e||typeof e!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let t=this.getMutableConfig(),r=this.getMcpEndpoints();if(r.includes(e))throw new Error(`MCP \u7AEF\u70B9 ${e} \u5DF2\u5B58\u5728`);let n=[...r,e];t.mcpEndpoint=n,this.saveConfig(t)}removeMcpEndpoint(e){if(!e||typeof e!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let t=this.getMutableConfig(),r=this.getMcpEndpoints();if(r.indexOf(e)===-1)throw new Error(`MCP \u7AEF\u70B9 ${e} \u4E0D\u5B58\u5728`);if(r.length===1)throw new Error("\u4E0D\u80FD\u5220\u9664\u6700\u540E\u4E00\u4E2A MCP \u7AEF\u70B9");let o=r.filter(s=>s!==e);t.mcpEndpoint=o,this.saveConfig(t)}updateMcpServer(e,t){if(!e||typeof e!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let r=ut(e,t);if(!r.valid)throw new Error(r.error||"\u670D\u52A1\u914D\u7F6E\u9A8C\u8BC1\u5931\u8D25");let n=this.getMutableConfig();n.mcpServers[e]=t,this.saveConfig(n)}removeMcpServer(e){if(!e||typeof e!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let t=this.getConfig();if(!t.mcpServers[e])throw new Error(`\u670D\u52A1 ${e} \u4E0D\u5B58\u5728`);let r={...t.mcpServers};delete r[e];let n={...t,mcpServers:r};this.saveConfig(n)}updateServerToolsConfig(e,t){let r=this.getMutableConfig();r.mcpServerConfig||(r.mcpServerConfig={}),Object.keys(t).length===0?delete r.mcpServerConfig[e]:r.mcpServerConfig[e]={tools:t},this.saveConfig(r)}removeServerToolsConfig(e){let r={...this.getConfig()};r.mcpServerConfig&&(delete r.mcpServerConfig[e],this.saveConfig(r))}cleanupInvalidServerToolsConfig(){let e=this.getMutableConfig();if(!e.mcpServerConfig)return;let t=Object.keys(e.mcpServers),n=Object.keys(e.mcpServerConfig).filter(o=>!t.includes(o));if(n.length>0){for(let o of n)delete e.mcpServerConfig[o];this.saveConfig(e),h.info(`\u5DF2\u6E05\u7406 ${n.length} \u4E2A\u65E0\u6548\u7684\u670D\u52A1\u5DE5\u5177\u914D\u7F6E: ${n.join(", ")}`)}}setToolEnabled(e,t,r,n){let o=this.getMutableConfig();o.mcpServerConfig||(o.mcpServerConfig={}),o.mcpServerConfig[e]||(o.mcpServerConfig[e]={tools:{}}),o.mcpServerConfig[e].tools[t]={...o.mcpServerConfig[e].tools[t],enable:r,...n&&{description:n}},this.saveConfig(o)}saveConfig(e){try{this.validateConfig(e);let t;this.currentConfigPath?t=this.currentConfigPath:(t=this.getConfigFilePath(),this.currentConfigPath=t);let r=this.getConfigFileFormat(t),n;switch(r){case"json5":try{this.json5Writer?(this.json5Writer.write(e),n=this.json5Writer.toSource()):(console.warn("\u6CA1\u6709 json5Writer \u5B9E\u4F8B\uFF0C\u56DE\u9000\u5230\u6807\u51C6 JSON5 \u683C\u5F0F"),n=mt.stringify(e,null,2))}catch(o){console.warn("\u4F7F\u7528 json5-writer \u4FDD\u5B58\u5931\u8D25\uFF0C\u56DE\u9000\u5230\u6807\u51C6 JSON5 \u683C\u5F0F:",o),n=mt.stringify(e,null,2)}break;case"jsonc":try{n=Fe.stringify(e,null,2)}catch(o){console.warn("\u4F7F\u7528 comment-json \u4FDD\u5B58\u5931\u8D25\uFF0C\u56DE\u9000\u5230\u6807\u51C6 JSON \u683C\u5F0F:",o),n=JSON.stringify(e,null,2)}break;default:n=JSON.stringify(e,null,2);break}fn(t,n,"utf8"),this.config=e,this.notifyConfigUpdate(e)}catch(t){throw new Error(`\u4FDD\u5B58\u914D\u7F6E\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`)}}reloadConfig(){this.config=null,this.currentConfigPath=null,this.json5Writer=null}getConfigPath(){return this.getConfigFilePath()}getDefaultConfigPath(){return this.defaultConfigPath}getConnectionConfig(){let t=this.getConfig().connection||{};return{heartbeatInterval:t.heartbeatInterval??ft.heartbeatInterval,heartbeatTimeout:t.heartbeatTimeout??ft.heartbeatTimeout,reconnectInterval:t.reconnectInterval??ft.reconnectInterval}}getHeartbeatInterval(){return this.getConnectionConfig().heartbeatInterval}getHeartbeatTimeout(){return this.getConnectionConfig().heartbeatTimeout}getReconnectInterval(){return this.getConnectionConfig().reconnectInterval}updateConnectionConfig(e){let t=this.getMutableConfig();t.connection||(t.connection={}),Object.assign(t.connection,e),this.saveConfig(t)}async updateToolUsageStats(e,t,r){try{let n=this.getMutableConfig();n.mcpServerConfig||(n.mcpServerConfig={}),n.mcpServerConfig[e]||(n.mcpServerConfig[e]={tools:{}}),n.mcpServerConfig[e].tools[t]||(n.mcpServerConfig[e].tools[t]={enable:!0});let o=n.mcpServerConfig[e].tools[t],s=o.usageCount||0,c=o.lastUsedTime;o.usageCount=s+1,(!c||new Date(r)>new Date(c))&&(o.lastUsedTime=Cn(r).format("YYYY-MM-DD HH:mm:ss")),this.saveConfig(n),h.debug(`\u5DE5\u5177\u4F7F\u7528\u7EDF\u8BA1\u5DF2\u66F4\u65B0: ${e}/${t}, \u4F7F\u7528\u6B21\u6570: ${o.usageCount}`)}catch(n){h.error(`\u66F4\u65B0\u5DE5\u5177\u4F7F\u7528\u7EDF\u8BA1\u5931\u8D25 (${e}/${t}): ${n instanceof Error?n.message:String(n)}`)}}setHeartbeatInterval(e){if(e<=0)throw new Error("\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatInterval:e})}setHeartbeatTimeout(e){if(e<=0)throw new Error("\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatTimeout:e})}setReconnectInterval(e){if(e<=0)throw new Error("\u91CD\u8FDE\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({reconnectInterval:e})}getModelScopeConfig(){return this.getConfig().modelscope||{}}getModelScopeApiKey(){return this.getModelScopeConfig().apiKey||process.env.MODELSCOPE_API_TOKEN}updateModelScopeConfig(e){let t=this.getMutableConfig();t.modelscope||(t.modelscope={}),Object.assign(t.modelscope,e),this.saveConfig(t)}setModelScopeApiKey(e){if(!e||typeof e!="string")throw new Error("API Key \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");this.updateModelScopeConfig({apiKey:e})}getWebUIConfig(){return this.getConfig().webUI||{}}getWebUIPort(){return this.getWebUIConfig().port??9999}notifyConfigUpdate(e){try{let t=global.__webServer;t&&typeof t.broadcastConfigUpdate=="function"&&(t.broadcastConfigUpdate(e),console.log("\u5DF2\u901A\u8FC7 WebSocket \u5E7F\u64AD\u914D\u7F6E\u66F4\u65B0"))}catch(t){console.warn("\u901A\u77E5 Web \u754C\u9762\u914D\u7F6E\u66F4\u65B0\u5931\u8D25:",t instanceof Error?t.message:String(t))}}updateWebUIConfig(e){let t=this.getMutableConfig();t.webUI||(t.webUI={}),Object.assign(t.webUI,e),this.saveConfig(t)}setWebUIPort(e){if(!Number.isInteger(e)||e<=0||e>65535)throw new Error("\u7AEF\u53E3\u53F7\u5FC5\u987B\u662F 1-65535 \u4E4B\u95F4\u7684\u6574\u6570");this.updateWebUIConfig({port:e})}},d=vt.getInstance()});var St,ue,Re,f,Ct,Te=g(()=>{"use strict";St={NAME:"xiaozhi-mcp-service",DEFAULT_PORT:3e3,DEFAULT_WEB_UI_PORT:9999,PID_FILE:"xiaozhi.pid",LOG_FILE:"xiaozhi.log"},ue={FILE_NAMES:["xiaozhi.config.json5","xiaozhi.config.jsonc","xiaozhi.config.json"],DEFAULT_FILE:"xiaozhi.config.default.json",DIR_ENV_VAR:"XIAOZHI_CONFIG_DIR"},Re={WORK_DIR:".xiaozhi",TEMPLATES_DIR:"templates",LOGS_DIR:"logs"},f={GENERAL_ERROR:"GENERAL_ERROR",CONFIG_ERROR:"CONFIG_ERROR",SERVICE_ERROR:"SERVICE_ERROR",VALIDATION_ERROR:"VALIDATION_ERROR",FILE_ERROR:"FILE_ERROR",PROCESS_ERROR:"PROCESS_ERROR",NETWORK_ERROR:"NETWORK_ERROR",PERMISSION_ERROR:"PERMISSION_ERROR"},Ct={PROCESS_STOP:3e3,SERVICE_START:1e4,NETWORK_REQUEST:5e3,FILE_OPERATION:2e3}});var yn,En,Le,tr=g(()=>{"use strict";Te();yn={[f.CONFIG_ERROR]:'\u8FD0\u884C "xiaozhi --help" \u67E5\u770B\u914D\u7F6E\u76F8\u5173\u547D\u4EE4',[f.SERVICE_ERROR]:'\u8FD0\u884C "xiaozhi status" \u68C0\u67E5\u670D\u52A1\u72B6\u6001',[f.VALIDATION_ERROR]:"\u68C0\u67E5\u8F93\u5165\u53C2\u6570\u662F\u5426\u6B63\u786E",[f.FILE_ERROR]:"\u68C0\u67E5\u6587\u4EF6\u8DEF\u5F84\u548C\u6743\u9650",[f.PROCESS_ERROR]:"\u68C0\u67E5\u8FDB\u7A0B\u72B6\u6001\u548C\u6743\u9650",[f.NETWORK_ERROR]:"\u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5\u548C\u9632\u706B\u5899\u8BBE\u7F6E",[f.PERMISSION_ERROR]:"\u5C1D\u8BD5\u4F7F\u7528\u7BA1\u7406\u5458\u6743\u9650\u8FD0\u884C"},En={config_not_found:['\u8FD0\u884C "xiaozhi init" \u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6',"\u68C0\u67E5\u5F53\u524D\u76EE\u5F55\u662F\u5426\u4E3A\u9879\u76EE\u6839\u76EE\u5F55","\u8BBE\u7F6E XIAOZHI_CONFIG_DIR \u73AF\u5883\u53D8\u91CF\u6307\u5B9A\u914D\u7F6E\u76EE\u5F55"],service_port_occupied:["\u68C0\u67E5\u7AEF\u53E3\u662F\u5426\u88AB\u5176\u4ED6\u7A0B\u5E8F\u5360\u7528",'\u4F7F\u7528 "lsof -i :\u7AEF\u53E3\u53F7" \u67E5\u770B\u7AEF\u53E3\u4F7F\u7528\u60C5\u51B5',"\u66F4\u6539\u914D\u7F6E\u6587\u4EF6\u4E2D\u7684\u7AEF\u53E3\u8BBE\u7F6E"],permission_denied:["\u68C0\u67E5\u6587\u4EF6\u548C\u76EE\u5F55\u6743\u9650","\u4F7F\u7528 sudo \u6216\u7BA1\u7406\u5458\u6743\u9650\u8FD0\u884C","\u786E\u4FDD\u5F53\u524D\u7528\u6237\u6709\u8DB3\u591F\u7684\u6743\u9650"],service_start_failed:["\u68C0\u67E5\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u662F\u5426\u6B63\u786E","\u67E5\u770B\u65E5\u5FD7\u6587\u4EF6\u83B7\u53D6\u8BE6\u7EC6\u9519\u8BEF\u4FE1\u606F","\u786E\u4FDD\u6240\u6709\u4F9D\u8D56\u670D\u52A1\u6B63\u5E38\u8FD0\u884C"]},Le=class{static{a(this,"ERROR_MESSAGES")}static getHelpMessage(e){return yn[e]}static getSolutions(e){return En[e]||[]}static formatError(e,t){return`${t?`[${t}] `:""}${e.message}`}static getFriendlyMessage(e){return{[f.CONFIG_ERROR]:"\u914D\u7F6E\u6587\u4EF6\u76F8\u5173\u9519\u8BEF",[f.SERVICE_ERROR]:"\u670D\u52A1\u8FD0\u884C\u76F8\u5173\u9519\u8BEF",[f.VALIDATION_ERROR]:"\u8F93\u5165\u9A8C\u8BC1\u9519\u8BEF",[f.FILE_ERROR]:"\u6587\u4EF6\u64CD\u4F5C\u9519\u8BEF",[f.PROCESS_ERROR]:"\u8FDB\u7A0B\u7BA1\u7406\u9519\u8BEF",[f.NETWORK_ERROR]:"\u7F51\u7EDC\u8FDE\u63A5\u9519\u8BEF",[f.PERMISSION_ERROR]:"\u6743\u9650\u4E0D\u8DB3\u9519\u8BEF"}[e]||"\u672A\u77E5\u9519\u8BEF"}static isRecoverable(e){return[f.NETWORK_ERROR,f.FILE_ERROR,f.SERVICE_ERROR].includes(e)}static getSeverity(e){return{[f.VALIDATION_ERROR]:"low",[f.FILE_ERROR]:"medium",[f.CONFIG_ERROR]:"medium",[f.NETWORK_ERROR]:"medium",[f.SERVICE_ERROR]:"high",[f.PROCESS_ERROR]:"high",[f.PERMISSION_ERROR]:"critical"}[e]||"medium"}}});var O,se,w,v,m,Y,G=g(()=>{"use strict";Te();O=class i extends Error{constructor(t,r,n=1,o){super(t);this.code=r;this.exitCode=n;this.suggestions=o;this.name="CLIError",Error.captureStackTrace&&Error.captureStackTrace(this,i)}static{a(this,"CLIError")}static withSuggestions(t,r,n){return new i(t,r,1,n)}},se=class i extends O{static{a(this,"ConfigError")}constructor(e,t){super(e,f.CONFIG_ERROR,1,t),this.name="ConfigError"}static configNotFound(){return new i("\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728",['\u8BF7\u8FD0\u884C "xiaozhi init" \u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6'])}static invalidFormat(e){return new i(`\u65E0\u6548\u7684\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F: ${e}`,["\u652F\u6301\u7684\u683C\u5F0F: json, json5, jsonc"])}},w=class i extends O{static{a(this,"ServiceError")}constructor(e,t){super(e,f.SERVICE_ERROR,1,t),this.name="ServiceError"}static alreadyRunning(e){return new i(`\u670D\u52A1\u5DF2\u7ECF\u5728\u8FD0\u884C (PID: ${e})`,['\u8BF7\u5148\u8FD0\u884C "xiaozhi stop" \u505C\u6B62\u73B0\u6709\u670D\u52A1','\u6216\u8005\u4F7F\u7528 "xiaozhi restart" \u91CD\u542F\u670D\u52A1'])}static autoRestarting(e){return new i(`\u68C0\u6D4B\u5230\u670D\u52A1\u5DF2\u5728\u8FD0\u884C (PID: ${e})\uFF0C\u6B63\u5728\u81EA\u52A8\u91CD\u542F...`,["\u5982\u679C\u4E0D\u5E0C\u671B\u81EA\u52A8\u91CD\u542F\uFF0C\u8BF7\u4F7F\u7528 xiaozhi stop \u624B\u52A8\u505C\u6B62\u670D\u52A1"])}static notRunning(){return new i("\u670D\u52A1\u672A\u8FD0\u884C",['\u8BF7\u8FD0\u884C "xiaozhi start" \u542F\u52A8\u670D\u52A1'])}static startFailed(e){return new i(`\u670D\u52A1\u542F\u52A8\u5931\u8D25: ${e}`,["\u68C0\u67E5\u914D\u7F6E\u6587\u4EF6\u662F\u5426\u6B63\u786E","\u786E\u4FDD\u7AEF\u53E3\u672A\u88AB\u5360\u7528","\u67E5\u770B\u65E5\u5FD7\u6587\u4EF6\u83B7\u53D6\u8BE6\u7EC6\u4FE1\u606F"])}},v=class i extends O{static{a(this,"ValidationError")}constructor(e,t){super(`\u9A8C\u8BC1\u5931\u8D25: ${t} - ${e}`,f.VALIDATION_ERROR,1),this.name="ValidationError"}static invalidPort(e){return new i(`\u7AEF\u53E3\u53F7\u5FC5\u987B\u5728 1-65535 \u8303\u56F4\u5185\uFF0C\u5F53\u524D\u503C: ${e}`,"port")}static requiredField(e){return new i("\u5FC5\u586B\u5B57\u6BB5\u4E0D\u80FD\u4E3A\u7A7A",e)}},m=class i extends O{static{a(this,"FileError")}constructor(e,t,r){let n=t?`${e}: ${t}`:e;super(n,f.FILE_ERROR,1,r),this.name="FileError"}static notFound(e){return new i("\u6587\u4EF6\u4E0D\u5B58\u5728",e,["\u68C0\u67E5\u6587\u4EF6\u8DEF\u5F84\u662F\u5426\u6B63\u786E"])}static permissionDenied(e){return new i("\u6743\u9650\u4E0D\u8DB3",e,["\u68C0\u67E5\u6587\u4EF6\u6743\u9650\u6216\u4F7F\u7528\u7BA1\u7406\u5458\u6743\u9650\u8FD0\u884C"])}static alreadyExists(e){return new i("\u6587\u4EF6\u5DF2\u5B58\u5728",e,["\u4F7F\u7528\u4E0D\u540C\u7684\u6587\u4EF6\u540D\u6216\u5220\u9664\u73B0\u6709\u6587\u4EF6"])}},Y=class i extends O{static{a(this,"ProcessError")}constructor(e,t,r){let n=t?`${e} (PID: ${t})`:e;super(n,f.PROCESS_ERROR,1,r),this.name="ProcessError"}static killFailed(e){return new i("\u65E0\u6CD5\u7EC8\u6B62\u8FDB\u7A0B",e,["\u8FDB\u7A0B\u53EF\u80FD\u5DF2\u7ECF\u505C\u6B62\u6216\u6743\u9650\u4E0D\u8DB3"])}static notFound(e){return new i("\u8FDB\u7A0B\u4E0D\u5B58\u5728",e)}}});import A from"chalk";var X,yt=g(()=>{"use strict";tr();G();X=class i{static{a(this,"ErrorHandler")}static handle(e){e instanceof O?i.handleCLIError(e):i.handleUnknownError(e),process.exit(1)}static handleCLIError(e){if(console.error(A.red(`\u274C \u9519\u8BEF: ${e.message}`)),process.env.DEBUG&&console.error(A.gray(`\u9519\u8BEF\u7801: ${e.code}`)),e.suggestions&&e.suggestions.length>0){console.log(A.yellow("\u{1F4A1} \u5EFA\u8BAE:"));for(let r of e.suggestions)console.log(A.gray(` ${r}`))}let t=Le.getHelpMessage(e.code);t&&console.log(A.blue(`\u2139\uFE0F ${t}`))}static handleUnknownError(e){console.error(A.red(`\u274C \u672A\u77E5\u9519\u8BEF: ${e.message}`)),process.env.DEBUG||process.env.NODE_ENV==="development"?(console.error(A.gray("\u5806\u6808\u4FE1\u606F:")),console.error(A.gray(e.stack))):console.log(A.yellow("\u{1F4A1} \u63D0\u793A: \u8BBE\u7F6E DEBUG=1 \u73AF\u5883\u53D8\u91CF\u67E5\u770B\u8BE6\u7EC6\u9519\u8BEF\u4FE1\u606F"))}static async handleAsync(e,t){try{return await e()}catch(r){throw r instanceof O?r:r instanceof Error?new O(`${t}\u5931\u8D25: ${r.message}`,"OPERATION_FAILED",1):new O(`${t}\u5931\u8D25: \u672A\u77E5\u9519\u8BEF`,"OPERATION_FAILED",1)}}static handleSync(e,t){try{return e()}catch(r){throw r instanceof O?r:r instanceof Error?new O(`${t}\u5931\u8D25: ${r.message}`,"OPERATION_FAILED",1):new O(`${t}\u5931\u8D25: \u672A\u77E5\u9519\u8BEF`,"OPERATION_FAILED",1)}}static warn(e,t){if(console.warn(A.yellow(`\u26A0\uFE0F \u8B66\u544A: ${e}`)),t&&t.length>0){console.log(A.yellow("\u{1F4A1} \u5EFA\u8BAE:"));for(let r of t)console.log(A.gray(` ${r}`))}}static info(e){console.log(A.blue(`\u2139\uFE0F ${e}`))}static success(e){console.log(A.green(`\u2705 ${e}`))}}});import R from"fs";import L from"path";var S,Ie=g(()=>{"use strict";G();S=class i{static{a(this,"FileUtils")}static exists(e){try{return R.existsSync(e)}catch{return!1}}static ensureDir(e){try{R.existsSync(e)||R.mkdirSync(e,{recursive:!0})}catch{throw new m("\u65E0\u6CD5\u521B\u5EFA\u76EE\u5F55",e)}}static readFile(e,t="utf8"){try{if(!i.exists(e))throw m.notFound(e);return R.readFileSync(e,t)}catch(r){throw r instanceof m?r:new m("\u65E0\u6CD5\u8BFB\u53D6\u6587\u4EF6",e)}}static writeFile(e,t,r){try{if(!r?.overwrite&&i.exists(e))throw m.alreadyExists(e);let n=L.dirname(e);i.ensureDir(n),R.writeFileSync(e,t,"utf8")}catch(n){throw n instanceof m?n:new m("\u65E0\u6CD5\u5199\u5165\u6587\u4EF6",e)}}static copyFile(e,t,r){try{if(!i.exists(e))throw m.notFound(e);if(!r?.overwrite&&i.exists(t))throw m.alreadyExists(t);let n=L.dirname(t);i.ensureDir(n),R.copyFileSync(e,t)}catch(n){throw n instanceof m?n:new m("\u65E0\u6CD5\u590D\u5236\u6587\u4EF6",e)}}static deleteFile(e){try{i.exists(e)&&R.unlinkSync(e)}catch{throw new m("\u65E0\u6CD5\u5220\u9664\u6587\u4EF6",e)}}static copyDirectory(e,t,r={}){try{if(!i.exists(e))throw m.notFound(e);i.ensureDir(t);let n=R.readdirSync(e);for(let o of n){if(r.exclude?.includes(o))continue;let s=L.join(e,o),c=L.join(t,o);R.statSync(s).isDirectory()?r.recursive!==!1&&i.copyDirectory(s,c,r):i.copyFile(s,c,{overwrite:r.overwrite})}}catch(n){throw n instanceof m?n:new m("\u65E0\u6CD5\u590D\u5236\u76EE\u5F55",e)}}static deleteDirectory(e,t={}){try{i.exists(e)&&R.rmSync(e,{recursive:t.recursive??!0,force:!0})}catch{throw new m("\u65E0\u6CD5\u5220\u9664\u76EE\u5F55",e)}}static getFileInfo(e){try{if(!i.exists(e))throw m.notFound(e);let t=R.statSync(e);return{size:t.size,isFile:t.isFile(),isDirectory:t.isDirectory(),mtime:t.mtime,ctime:t.ctime}}catch(t){throw t instanceof m?t:new m("\u65E0\u6CD5\u83B7\u53D6\u6587\u4EF6\u4FE1\u606F",e)}}static listDirectory(e,t={}){try{if(!i.exists(e))throw m.notFound(e);let r=R.readdirSync(e),n=[];for(let o of r){if(!t.includeHidden&&o.startsWith("."))continue;let s=L.join(e,o);if(n.push(s),t.recursive&&R.statSync(s).isDirectory()){let c=i.listDirectory(s,t);n=n.concat(c)}}return n}catch(r){throw r instanceof m?r:new m("\u65E0\u6CD5\u5217\u51FA\u76EE\u5F55\u5185\u5BB9",e)}}static createTempFile(e="xiaozhi-",t=".tmp"){let r=process.env.TMPDIR||process.env.TEMP||"/tmp",n=Date.now(),o=Math.random().toString(36).substring(2),s=`${e}${n}-${o}${t}`;return L.join(r,s)}static checkPermissions(e,t=R.constants.R_OK|R.constants.W_OK){try{return R.accessSync(e,t),!0}catch{return!1}}static getExtension(e){return L.extname(e).toLowerCase()}static getBaseName(e){return L.basename(e,L.extname(e))}static normalizePath(e){return L.normalize(e)}static resolvePath(e,t){return t?L.resolve(t,e):L.resolve(e)}}});var me,Et=g(()=>{"use strict";me=class{static{a(this,"FormatUtils")}static formatUptime(e){let t=Math.floor(e/1e3),r=Math.floor(t/60),n=Math.floor(r/60),o=Math.floor(n/24);return o>0?`${o}\u5929 ${n%24}\u5C0F\u65F6 ${r%60}\u5206\u949F`:n>0?`${n}\u5C0F\u65F6 ${r%60}\u5206\u949F`:r>0?`${r}\u5206\u949F ${t%60}\u79D2`:`${t}\u79D2`}static formatFileSize(e){let t=["B","KB","MB","GB","TB"],r=e,n=0;for(;r>=1024&&n<t.length-1;)r/=1024,n++;return`${r.toFixed(n===0?0:1)} ${t[n]}`}static formatTimestamp(e,t="full"){let r=new Date(e);switch(t){case"date":return r.toLocaleDateString("zh-CN");case"time":return r.toLocaleTimeString("zh-CN");default:return r.toLocaleString("zh-CN")}}static formatPid(e){return`PID: ${e}`}static formatPort(e){return`\u7AEF\u53E3: ${e}`}static formatUrl(e,t,r,n){let o=`${e}://${t}:${r}`;return n?`${o}${n}`:o}static formatConfigPair(e,t){return typeof t=="object"?`${e}: ${JSON.stringify(t,null,2)}`:`${e}: ${t}`}static formatError(e,t=!1){let r=`\u9519\u8BEF: ${e.message}`;return t&&e.stack&&(r+=`
|
|
4
|
+
\u5806\u6808\u4FE1\u606F:
|
|
5
|
+
${e.stack}`),r}static formatList(e,t="\u2022"){return e.map(r=>`${t} ${r}`).join(`
|
|
6
|
+
`)}static formatTable(e){if(e.length===0)return"";let t=Object.keys(e[0]),r=t.map(c=>Math.max(c.length,...e.map(l=>String(l[c]).length))),n=t.map((c,l)=>c.padEnd(r[l])).join(" | "),o=r.map(c=>"-".repeat(c)).join("-|-"),s=e.map(c=>t.map((l,u)=>String(c[l]).padEnd(r[u])).join(" | "));return[n,o,...s].join(`
|
|
7
|
+
`)}static formatProgressBar(e,t,r=20){let n=Math.min(e/t,1),o=Math.floor(n*r),s=r-o,c="\u2588".repeat(o)+"\u2591".repeat(s),l=Math.floor(n*100);return`[${c}] ${l}% (${e}/${t})`}static formatCommandArgs(e,t){let r=t.map(n=>n.includes(" ")?`"${n}"`:n);return`${e} ${r.join(" ")}`}static truncateText(e,t,r="..."){return e.length<=t?e:e.substring(0,t-r.length)+r}static formatJson(e,t=2){try{return JSON.stringify(e,null,t)}catch{return String(e)}}static formatBoolean(e,t="\u662F",r="\u5426"){return e?t:r}}});import{tmpdir as bn}from"os";import E from"path";import{fileURLToPath as rr}from"url";var b,fe=g(()=>{"use strict";Te();Ie();b=class i{static{a(this,"PathUtils")}static getPidFile(){let e=process.env[ue.DIR_ENV_VAR]||process.cwd();return E.join(e,`.${St.NAME}.pid`)}static getLogFile(e){let t=e||process.cwd();return E.join(t,St.LOG_FILE)}static getConfigDir(){return process.env[ue.DIR_ENV_VAR]||process.cwd()}static getWorkDir(){let e=i.getConfigDir();return E.join(e,Re.WORK_DIR)}static getTemplatesDir(){let e=rr(import.meta.url),t=E.dirname(e);return[E.join(t,Re.TEMPLATES_DIR),E.join(t,"..","..","..",Re.TEMPLATES_DIR),E.join(t,"..","..","..","..",Re.TEMPLATES_DIR)]}static findTemplatesDir(){let e=i.getTemplatesDir();for(let t of e)if(S.exists(t))return t;return null}static getTemplatePath(e){let t=i.findTemplatesDir();if(!t)return null;let r=E.join(t,e);return S.exists(r)?r:null}static getScriptDir(){let e=rr(import.meta.url);return E.dirname(e)}static getProjectRoot(){let e=i.getScriptDir();return E.join(e,"..","..","..")}static getDistDir(){let e=i.getProjectRoot();return E.join(e,"dist")}static getRelativePath(e){let t=i.getProjectRoot();return E.relative(t,e)}static resolveConfigPath(e){let t=i.getConfigDir();if(e)return E.join(t,`xiaozhi.config.${e}`);for(let r of ue.FILE_NAMES){let n=E.join(t,r);if(S.exists(n))return n}return E.join(t,ue.FILE_NAMES[2])}static getDefaultConfigPath(){let e=i.getProjectRoot();return E.join(e,ue.DEFAULT_FILE)}static validatePath(e){return!E.normalize(e).includes("..")}static ensurePathWithin(e,t){let r=E.resolve(t,e),n=E.resolve(t);if(!r.startsWith(n))throw new Error(`\u8DEF\u5F84 ${e} \u8D85\u51FA\u4E86\u5141\u8BB8\u7684\u8303\u56F4`);return r}static getExecutablePath(e){let t=process.argv[1],r=E.dirname(t);return E.join(r,`${e}.js`)}static getMcpServerProxyPath(){return i.getExecutablePath("mcpServerProxy")}static getWebServerStandalonePath(){return i.getExecutablePath("WebServerStandalone")}static createSafePath(...e){let t=E.join(...e),r=E.normalize(t);if(r.includes("..")||r.includes("~"))throw new Error(`\u4E0D\u5B89\u5168\u7684\u8DEF\u5F84: ${r}`);return r}static getTempDir(){return process.env.TMPDIR||process.env.TEMP||bn()}static getHomeDir(){return process.env.HOME||process.env.USERPROFILE||""}}});import{execSync as nr}from"child_process";var j,xe=g(()=>{"use strict";Te();G();j=class i{static{a(this,"PlatformUtils")}static getCurrentPlatform(){return process.platform}static isWindows(){return process.platform==="win32"}static isMacOS(){return process.platform==="darwin"}static isLinux(){return process.platform==="linux"}static isUnixLike(){return!i.isWindows()}static isXiaozhiProcess(e){try{if(process.env.XIAOZHI_CONTAINER==="true"||process.env.NODE_ENV==="test")return process.kill(e,0),!0;try{let t="";return i.isWindows()?t=nr(`tasklist /FI "PID eq ${e}" /FO CSV /NH`,{encoding:"utf8",timeout:Ct.PROCESS_STOP}).toLowerCase():t=nr(`ps -p ${e} -o comm=`,{encoding:"utf8",timeout:Ct.PROCESS_STOP}).toLowerCase(),t.includes("node")||t.includes("xiaozhi")}catch{return process.kill(e,0),!0}}catch{return!1}}static async killProcess(e,t="SIGTERM"){try{process.kill(e,t);let r=0,n=30;for(;r<n;){await new Promise(o=>setTimeout(o,100));try{process.kill(e,0),r++}catch{return}}try{process.kill(e,0),process.kill(e,"SIGKILL"),await new Promise(o=>setTimeout(o,500))}catch{}}catch(r){throw new Y(`\u65E0\u6CD5\u7EC8\u6B62\u8FDB\u7A0B: ${r instanceof Error?r.message:String(r)}`,e)}}static processExists(e){try{return process.kill(e,0),!0}catch{return!1}}static getSystemInfo(){return{platform:i.getCurrentPlatform(),arch:process.arch,nodeVersion:process.version,isContainer:process.env.XIAOZHI_CONTAINER==="true"}}static getEnvVar(e,t){return process.env[e]||t}static setEnvVar(e,t){process.env[e]=t}static isContainerEnvironment(){return process.env.XIAOZHI_CONTAINER==="true"}static isTestEnvironment(){return process.env.NODE_ENV==="test"}static isDevelopmentEnvironment(){return process.env.NODE_ENV==="development"}static getTailCommand(e){return i.isWindows()?{command:"powershell",args:["-Command",`Get-Content -Path "${e}" -Wait`]}:{command:"tail",args:["-f",e]}}}});var z,ze=g(()=>{"use strict";G();z=class i{static{a(this,"Validation")}static validatePort(e){if(!Number.isInteger(e)||e<1||e>65535)throw v.invalidPort(e)}static validateConfigFormat(e){let t=["json","json5","jsonc"];if(!t.includes(e))throw new v(`\u65E0\u6548\u7684\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F: ${e}\uFF0C\u652F\u6301\u7684\u683C\u5F0F: ${t.join(", ")}`,"format");return e}static validateRequired(e,t){if(e==null||e==="")throw v.requiredField(t)}static validateStringLength(e,t,r={}){if(r.min!==void 0&&e.length<r.min)throw new v(`\u957F\u5EA6\u4E0D\u80FD\u5C11\u4E8E ${r.min} \u4E2A\u5B57\u7B26\uFF0C\u5F53\u524D\u957F\u5EA6: ${e.length}`,t);if(r.max!==void 0&&e.length>r.max)throw new v(`\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 ${r.max} \u4E2A\u5B57\u7B26\uFF0C\u5F53\u524D\u957F\u5EA6: ${e.length}`,t)}static validateUrl(e,t="url"){try{new URL(e)}catch{throw new v(`\u65E0\u6548\u7684 URL \u683C\u5F0F: ${e}`,t)}}static validateWebSocketUrl(e,t="websocket_url"){i.validateUrl(e,t);let r=new URL(e);if(!["ws:","wss:"].includes(r.protocol))throw new v(`WebSocket URL \u5FC5\u987B\u4F7F\u7528 ws:// \u6216 wss:// \u534F\u8BAE\uFF0C\u5F53\u524D\u534F\u8BAE: ${r.protocol}`,t)}static validateHttpUrl(e,t="http_url"){i.validateUrl(e,t);let r=new URL(e);if(!["http:","https:"].includes(r.protocol))throw new v(`HTTP URL \u5FC5\u987B\u4F7F\u7528 http:// \u6216 https:// \u534F\u8BAE\uFF0C\u5F53\u524D\u534F\u8BAE: ${r.protocol}`,t)}static validateProjectName(e){i.validateRequired(e,"projectName"),i.validateStringLength(e,"projectName",{min:1,max:100});let t=/[<>:"/\\|?*]/,r=e.split("").some(n=>n.charCodeAt(0)<32);if(t.test(e)||r)throw new v('\u9879\u76EE\u540D\u79F0\u4E0D\u80FD\u5305\u542B\u4EE5\u4E0B\u5B57\u7B26: < > : " / \\ | ? * \u4EE5\u53CA\u63A7\u5236\u5B57\u7B26',"projectName");if(e.startsWith("."))throw new v("\u9879\u76EE\u540D\u79F0\u4E0D\u80FD\u4EE5\u70B9\u5F00\u5934","projectName")}static validateTemplateName(e){if(i.validateRequired(e,"templateName"),i.validateStringLength(e,"templateName",{min:1,max:50}),!/^[a-zA-Z0-9_-]+$/.test(e))throw new v("\u6A21\u677F\u540D\u79F0\u53EA\u80FD\u5305\u542B\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u8FDE\u5B57\u7B26\u548C\u4E0B\u5212\u7EBF","templateName")}static validateEnvVarName(e){if(i.validateRequired(e,"envVarName"),!/^[A-Z_][A-Z0-9_]*$/.test(e))throw new v("\u73AF\u5883\u53D8\u91CF\u540D\u79F0\u53EA\u80FD\u5305\u542B\u5927\u5199\u5B57\u6BCD\u3001\u6570\u5B57\u548C\u4E0B\u5212\u7EBF\uFF0C\u4E14\u4E0D\u80FD\u4EE5\u6570\u5B57\u5F00\u5934","envVarName")}static validateJson(e,t="json"){try{return JSON.parse(e)}catch(r){throw new v(`\u65E0\u6548\u7684 JSON \u683C\u5F0F: ${r instanceof Error?r.message:String(r)}`,t)}}static validateNumberRange(e,t,r={}){if(r.min!==void 0&&e<r.min)throw new v(`\u503C\u4E0D\u80FD\u5C0F\u4E8E ${r.min}\uFF0C\u5F53\u524D\u503C: ${e}`,t);if(r.max!==void 0&&e>r.max)throw new v(`\u503C\u4E0D\u80FD\u5927\u4E8E ${r.max}\uFF0C\u5F53\u524D\u503C: ${e}`,t)}static validateArrayLength(e,t,r={}){if(r.min!==void 0&&e.length<r.min)throw new v(`\u6570\u7EC4\u957F\u5EA6\u4E0D\u80FD\u5C11\u4E8E ${r.min}\uFF0C\u5F53\u524D\u957F\u5EA6: ${e.length}`,t);if(r.max!==void 0&&e.length>r.max)throw new v(`\u6570\u7EC4\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 ${r.max}\uFF0C\u5F53\u524D\u957F\u5EA6: ${e.length}`,t)}static validateObjectProperties(e,t,r="object"){for(let n of t)if(!(n in e))throw new v(`\u7F3A\u5C11\u5FC5\u9700\u7684\u5C5E\u6027: ${n}`,r)}static validateEnum(e,t,r){if(!t.includes(e))throw new v(`\u65E0\u6548\u7684\u503C: ${e}\uFF0C\u6709\u6548\u503C: ${t.join(", ")}`,r);return e}static validateRegex(e,t="regex"){try{return new RegExp(e)}catch(r){throw new v(`\u65E0\u6548\u7684\u6B63\u5219\u8868\u8FBE\u5F0F: ${r instanceof Error?r.message:String(r)}`,t)}}}});import _e from"fs";import K from"path";import{fileURLToPath as or}from"url";var Ue,ir=g(()=>{"use strict";G();Ue=class i{static{a(this,"VersionUtils")}static cachedVersion=null;static getVersion(){if(i.cachedVersion)return i.cachedVersion;try{let e=or(import.meta.url),t=K.dirname(e),r=[K.join(t,"package.json"),K.join(t,"..","package.json"),K.join(t,"..","..","..","package.json"),K.join(t,"..","..","..","..","package.json")];for(let n of r)if(_e.existsSync(n)){let o=JSON.parse(_e.readFileSync(n,"utf8"));if(o.version)return i.cachedVersion=o.version,o.version}return i.cachedVersion="unknown","unknown"}catch(e){return console.warn("\u65E0\u6CD5\u4ECE package.json \u8BFB\u53D6\u7248\u672C\u4FE1\u606F:",e),i.cachedVersion="unknown","unknown"}}static getVersionInfo(){try{let e=or(import.meta.url),t=K.dirname(e),r=[K.join(t,"package.json"),K.join(t,"..","package.json"),K.join(t,"..","..","..","package.json"),K.join(t,"..","..","..","..","package.json")];for(let n of r)if(_e.existsSync(n)){let o=JSON.parse(_e.readFileSync(n,"utf8"));return{version:o.version||"unknown",name:o.name,description:o.description,author:o.author}}return{version:"unknown"}}catch{throw new m("\u65E0\u6CD5\u8BFB\u53D6\u7248\u672C\u4FE1\u606F","package.json")}}static compareVersions(e,t){let r=e.split(".").map(Number),n=t.split(".").map(Number),o=Math.max(r.length,n.length);for(let s=0;s<o;s++){let c=r[s]||0,l=n[s]||0;if(c>l)return 1;if(c<l)return-1}return 0}static isValidVersion(e){return/^\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?$/.test(e)}static clearCache(){i.cachedVersion=null}}});var sr={};H(sr,{ProcessManagerImpl:()=>bt});var bt,ar=g(()=>{"use strict";G();Ie();Et();fe();xe();bt=class{static{a(this,"ProcessManagerImpl")}getPidFilePath(){return b.getPidFile()}readPidFile(){try{let e=this.getPidFilePath();if(!S.exists(e))return null;let t=S.readFile(e).trim(),[r,n,o]=t.split("|"),s=Number.parseInt(r),c=Number.parseInt(n);return Number.isNaN(s)||Number.isNaN(c)?(this.cleanupPidFile(),null):{pid:s,startTime:c,mode:o||"foreground"}}catch{return this.cleanupPidFile(),null}}writePidFile(e,t){try{let r=`${e}|${Date.now()}|${t}`,n=this.getPidFilePath();S.writeFile(n,r,{overwrite:!0})}catch{throw new m("\u65E0\u6CD5\u5199\u5165 PID \u6587\u4EF6",this.getPidFilePath())}}isXiaozhiProcess(e){return j.isXiaozhiProcess(e)}getServiceStatus(){try{let e=this.readPidFile();if(!e)return{running:!1};if(!this.isXiaozhiProcess(e.pid))return this.cleanupPidFile(),{running:!1};let t=me.formatUptime(Date.now()-e.startTime);return{running:!0,pid:e.pid,uptime:t,mode:e.mode}}catch{return{running:!1}}}savePidInfo(e,t){this.writePidFile(e,t)}async killProcess(e){try{await j.killProcess(e)}catch(t){throw new Y(`\u65E0\u6CD5\u7EC8\u6B62\u8FDB\u7A0B: ${t instanceof Error?t.message:String(t)}`,e)}}async gracefulKillProcess(e){try{process.kill(e,"SIGTERM");let t=0,r=30;for(;t<r;){await new Promise(n=>setTimeout(n,100));try{process.kill(e,0),t++}catch{return}}try{process.kill(e,0),process.kill(e,"SIGKILL"),await new Promise(n=>setTimeout(n,500))}catch{}}catch(t){throw new Y(`\u65E0\u6CD5\u505C\u6B62\u8FDB\u7A0B: ${t instanceof Error?t.message:String(t)}`,e)}}cleanupPidFile(){try{let e=this.getPidFilePath();S.exists(e)&&S.deleteFile(e)}catch(e){console.warn("\u6E05\u7406 PID \u6587\u4EF6\u5931\u8D25:",e)}}processExists(e){return j.processExists(e)}cleanupContainerState(){if(j.isContainerEnvironment())try{this.cleanupPidFile()}catch{}}getProcessInfo(e){let t=this.processExists(e),r=t?this.isXiaozhiProcess(e):!1;return{exists:t,isXiaozhi:r}}validatePidFile(){try{return this.readPidFile()!==null}catch{return!1}}}});var lr={};H(lr,{DaemonManagerImpl:()=>wt});import{spawn as cr}from"child_process";import We from"fs";var wt,gr=g(()=>{"use strict";G();fe();xe();wt=class{constructor(e,t){this.processManager=e;this.logger=t}static{a(this,"DaemonManagerImpl")}currentDaemon=null;async startDaemon(e,t={}){try{let r=this.processManager.getServiceStatus();if(r.running)throw w.alreadyRunning(r.pid);let n=await this.spawnDaemonProcess(e,t);this.currentDaemon=n,this.processManager.savePidInfo(n.pid,"daemon"),await this.setupLogging(n,t.logFileName||"xiaozhi.log"),this.setupEventHandlers(n),n.unref(),this.logger.info(`\u5B88\u62A4\u8FDB\u7A0B\u5DF2\u542F\u52A8 (PID: ${n.pid})`)}catch(r){throw new w(`\u542F\u52A8\u5B88\u62A4\u8FDB\u7A0B\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`)}}async stopDaemon(){try{let e=this.processManager.getServiceStatus();if(!e.running)throw w.notRunning();await this.processManager.gracefulKillProcess(e.pid),this.processManager.cleanupPidFile(),this.currentDaemon=null,this.logger.info("\u5B88\u62A4\u8FDB\u7A0B\u5DF2\u505C\u6B62")}catch(e){throw new w(`\u505C\u6B62\u5B88\u62A4\u8FDB\u7A0B\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}}async restartDaemon(e,t={}){try{this.processManager.getServiceStatus().running&&(await this.stopDaemon(),await new Promise(n=>setTimeout(n,1e3))),await this.startDaemon(e,t)}catch(r){throw new w(`\u91CD\u542F\u5B88\u62A4\u8FDB\u7A0B\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`)}}getDaemonStatus(){return this.processManager.getServiceStatus()}async attachToLogs(e="xiaozhi.log"){try{let t=b.getLogFile();if(!We.existsSync(t))throw new w("\u65E5\u5FD7\u6587\u4EF6\u4E0D\u5B58\u5728");let{command:r,args:n}=j.getTailCommand(t),o=cr(r,n,{stdio:"inherit"});process.on("SIGINT",()=>{console.log(`
|
|
8
|
+
\u65AD\u5F00\u8FDE\u63A5\uFF0C\u670D\u52A1\u7EE7\u7EED\u5728\u540E\u53F0\u8FD0\u884C`),o.kill(),process.exit(0)}),o.on("exit",()=>{process.exit(0)}),o.on("error",s=>{throw new w(`\u8FDE\u63A5\u65E5\u5FD7\u5931\u8D25: ${s.message}`)})}catch(t){throw new w(`\u8FDE\u63A5\u65E5\u5FD7\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`)}}async spawnDaemonProcess(e,t){let n=[b.getWebServerStandalonePath()];t.openBrowser&&n.push("--open-browser");let o={...process.env,XIAOZHI_CONFIG_DIR:b.getConfigDir(),XIAOZHI_DAEMON:"true",...t.env},s=cr("node",n,{detached:!0,stdio:["ignore","pipe","pipe"],env:o,cwd:t.cwd||process.cwd()});if(!s.pid)throw new Y("\u65E0\u6CD5\u542F\u52A8\u5B88\u62A4\u8FDB\u7A0B",0);return s}async setupLogging(e,t){try{let r=b.getLogFile(),o=(await import("path")).dirname(r);We.existsSync(o)||We.mkdirSync(o,{recursive:!0});let s=We.createWriteStream(r,{flags:"a"});e.stdout?.pipe(s),e.stderr?.pipe(s);let c=new Date().toISOString();s.write(`
|
|
9
|
+
[${c}] \u5B88\u62A4\u8FDB\u7A0B\u542F\u52A8 (PID: ${e.pid})
|
|
10
|
+
`)}catch(r){this.logger.warn(`\u8BBE\u7F6E\u65E5\u5FD7\u91CD\u5B9A\u5411\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`)}}setupEventHandlers(e){e.on("exit",(t,r)=>{t!==0&&t!==null?this.logger.error(`\u5B88\u62A4\u8FDB\u7A0B\u5F02\u5E38\u9000\u51FA (\u4EE3\u7801: ${t}, \u4FE1\u53F7: ${r})`):this.logger.info("\u5B88\u62A4\u8FDB\u7A0B\u6B63\u5E38\u9000\u51FA"),this.processManager.cleanupPidFile(),this.currentDaemon=null}),e.on("error",t=>{this.logger.error(`\u5B88\u62A4\u8FDB\u7A0B\u9519\u8BEF: ${t.message}`),this.processManager.cleanupPidFile(),this.currentDaemon=null}),e.on("disconnect",()=>{this.logger.info("\u5B88\u62A4\u8FDB\u7A0B\u65AD\u5F00\u8FDE\u63A5")})}async checkHealth(){try{let e=this.getDaemonStatus();if(!e.running||!e.pid)return!1;let t=this.processManager.getProcessInfo(e.pid);return t.exists&&t.isXiaozhi}catch{return!1}}getCurrentDaemon(){return this.currentDaemon}cleanup(){if(this.currentDaemon){try{this.currentDaemon.kill("SIGTERM")}catch(e){this.logger.warn(`\u6E05\u7406\u5B88\u62A4\u8FDB\u7A0B\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}this.currentDaemon=null}}}});import q from"ws";var P,re,Be=g(()=>{"use strict";C();P=class extends Error{constructor(t,r,n){super(r);this.code=t;this.data=n;this.name="ToolCallError"}static{a(this,"ToolCallError")}},re=class{static{a(this,"ProxyMCPServer")}endpointUrl;ws=null;logger;isConnected=!1;serverInitialized=!1;tools=new Map;connectionState="disconnected";reconnectOptions;reconnectState={attempts:0,nextInterval:0,timer:null,lastError:null,isManualDisconnect:!1};connectionTimeout=null;performanceMetrics={totalCalls:0,successfulCalls:0,failedCalls:0,averageResponseTime:0,minResponseTime:Number.MAX_VALUE,maxResponseTime:0,successRate:0,lastUpdated:new Date};callRecords=[];maxCallRecords=100;retryConfig={maxAttempts:3,initialDelay:1e3,maxDelay:1e4,backoffMultiplier:2,retryableErrors:[-32001,-32002]};toolCallConfig={timeout:3e4,retryAttempts:3,retryDelay:1e3};constructor(e,t){this.endpointUrl=e,this.logger=h,this.reconnectOptions={enabled:!0,maxAttempts:10,initialInterval:3e3,maxInterval:3e4,backoffStrategy:"exponential",backoffMultiplier:1.5,timeout:1e4,jitter:!0,...t?.reconnect},this.reconnectState.nextInterval=this.reconnectOptions.initialInterval}setServiceManager(e){this.serviceManager=e,this.logger.info("\u5DF2\u8BBE\u7F6E MCPServiceManager"),this.syncToolsFromServiceManager()}syncToolsFromServiceManager(){let e=this.serviceManager;if(!e){this.logger.debug("MCPServiceManager \u672A\u8BBE\u7F6E\uFF0C\u8DF3\u8FC7\u5DE5\u5177\u540C\u6B65");return}try{let t=e.getAllTools(),r=new Map;for(let n of t)r.set(n.name,{name:n.name,description:n.description,inputSchema:n.inputSchema});this.tools=r,this.logger.info(`\u5DF2\u4ECE MCPServiceManager \u540C\u6B65 ${this.tools.size} \u4E2A\u5DE5\u5177`)}catch(t){this.logger.error(`\u540C\u6B65\u5DE5\u5177\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`)}}addTool(e,t){return this.validateTool(e,t),this.tools.set(e,t),this.logger.debug(`\u5DE5\u5177 '${e}' \u5DF2\u6DFB\u52A0`),this}addTools(e){for(let[t,r]of Object.entries(e))this.addTool(t,r);return this}removeTool(e){return this.tools.delete(e)?this.logger.debug(`\u5DE5\u5177 '${e}' \u5DF2\u79FB\u9664`):this.logger.warn(`\u5C1D\u8BD5\u79FB\u9664\u4E0D\u5B58\u5728\u7684\u5DE5\u5177: '${e}'`),this}getTools(){try{this.syncToolsFromServiceManager()}catch{}return Array.from(this.tools.values())}hasTool(e){return this.tools.has(e)}validateTool(e,t){if(!e||typeof e!="string"||e.trim()==="")throw new Error("\u5DE5\u5177\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(this.tools.has(e))throw new Error(`\u5DE5\u5177 '${e}' \u5DF2\u5B58\u5728`);if(!t||typeof t!="object")throw new Error("\u5DE5\u5177\u5FC5\u987B\u662F\u6709\u6548\u7684\u5BF9\u8C61");if(!t.name||typeof t.name!="string")throw new Error("\u5DE5\u5177\u5FC5\u987B\u5305\u542B\u6709\u6548\u7684 'name' \u5B57\u6BB5");if(!t.description||typeof t.description!="string")throw new Error("\u5DE5\u5177\u5FC5\u987B\u5305\u542B\u6709\u6548\u7684 'description' \u5B57\u6BB5");if(!t.inputSchema||typeof t.inputSchema!="object")throw new Error("\u5DE5\u5177\u5FC5\u987B\u5305\u542B\u6709\u6548\u7684 'inputSchema' \u5B57\u6BB5");if(!t.inputSchema.type||!t.inputSchema.properties)throw new Error("\u5DE5\u5177\u7684 inputSchema \u5FC5\u987B\u5305\u542B 'type' \u548C 'properties' \u5B57\u6BB5")}async connect(){if(this.tools.size===0)throw new Error("\u672A\u914D\u7F6E\u4EFB\u4F55\u5DE5\u5177\u3002\u8BF7\u5728\u8FDE\u63A5\u524D\u81F3\u5C11\u6DFB\u52A0\u4E00\u4E2A\u5DE5\u5177\u3002");if(this.connectionState==="connecting")throw new Error("\u8FDE\u63A5\u6B63\u5728\u8FDB\u884C\u4E2D\uFF0C\u8BF7\u7B49\u5F85\u8FDE\u63A5\u5B8C\u6210");return this.cleanupConnection(),this.reconnectState.isManualDisconnect=!1,this.attemptConnection()}async attemptConnection(){return this.connectionState="connecting",this.logger.info(`\u6B63\u5728\u8FDE\u63A5 MCP \u63A5\u5165\u70B9: ${this.endpointUrl} (\u5C1D\u8BD5 ${this.reconnectState.attempts+1}/${this.reconnectOptions.maxAttempts})`),new Promise((e,t)=>{this.connectionTimeout=setTimeout(()=>{let r=new Error(`\u8FDE\u63A5\u8D85\u65F6 (${this.reconnectOptions.timeout}ms)`);this.handleConnectionError(r),t(r)},this.reconnectOptions.timeout),this.ws=new q(this.endpointUrl),this.ws.on("open",()=>{this.handleConnectionSuccess(),e()}),this.ws.on("message",r=>{try{let n=JSON.parse(r.toString());this.handleMessage(n)}catch(n){this.logger.error("MCP \u6D88\u606F\u89E3\u6790\u9519\u8BEF:",n)}}),this.ws.on("close",(r,n)=>{this.handleConnectionClose(r,n.toString())}),this.ws.on("error",r=>{this.handleConnectionError(r),t(r)})})}handleConnectionSuccess(){this.connectionTimeout&&(clearTimeout(this.connectionTimeout),this.connectionTimeout=null),this.isConnected=!0,this.connectionState="connected",this.reconnectState.attempts=0,this.reconnectState.nextInterval=this.reconnectOptions.initialInterval,this.reconnectState.lastError=null,this.logger.info("MCP WebSocket \u8FDE\u63A5\u5DF2\u5EFA\u7ACB")}handleConnectionError(e){this.connectionTimeout&&(clearTimeout(this.connectionTimeout),this.connectionTimeout=null),this.reconnectState.lastError=e,this.logger.error("MCP WebSocket \u9519\u8BEF:",e.message),this.cleanupConnection()}handleConnectionClose(e,t){if(this.isConnected=!1,this.serverInitialized=!1,this.logger.info(`MCP \u8FDE\u63A5\u5DF2\u5173\u95ED (\u4EE3\u7801: ${e}, \u539F\u56E0: ${t})`),this.reconnectState.isManualDisconnect){this.connectionState="disconnected";return}this.shouldReconnect()?this.scheduleReconnect():(this.connectionState="failed",this.logger.warn(`\u5DF2\u8FBE\u5230\u6700\u5927\u91CD\u8FDE\u6B21\u6570 (${this.reconnectOptions.maxAttempts})\uFF0C\u505C\u6B62\u91CD\u8FDE`))}shouldReconnect(){return this.reconnectOptions.enabled&&this.reconnectState.attempts<this.reconnectOptions.maxAttempts&&!this.reconnectState.isManualDisconnect}scheduleReconnect(){this.connectionState="reconnecting",this.reconnectState.attempts++,this.calculateNextInterval(),this.logger.info(`\u5C06\u5728 ${this.reconnectState.nextInterval}ms \u540E\u8FDB\u884C\u7B2C ${this.reconnectState.attempts} \u6B21\u91CD\u8FDE`),this.reconnectState.timer&&clearTimeout(this.reconnectState.timer),this.reconnectState.timer=setTimeout(async()=>{try{await this.attemptConnection()}catch{}},this.reconnectState.nextInterval)}calculateNextInterval(){let e;switch(this.reconnectOptions.backoffStrategy){case"fixed":e=this.reconnectOptions.initialInterval;break;case"linear":e=this.reconnectOptions.initialInterval+this.reconnectState.attempts*this.reconnectOptions.backoffMultiplier*1e3;break;case"exponential":e=this.reconnectOptions.initialInterval*this.reconnectOptions.backoffMultiplier**(this.reconnectState.attempts-1);break;default:e=this.reconnectOptions.initialInterval}if(e=Math.min(e,this.reconnectOptions.maxInterval),this.reconnectOptions.jitter){let t=e*.1,r=(Math.random()-.5)*2*t;e+=r}this.reconnectState.nextInterval=Math.max(e,1e3)}cleanupConnection(){if(this.ws){this.ws.removeAllListeners();try{this.ws.readyState===q.OPEN?this.ws.close(1e3,"Cleaning up connection"):this.ws.readyState===q.CONNECTING&&this.ws.terminate()}catch(e){this.logger.debug("WebSocket \u5173\u95ED\u65F6\u51FA\u73B0\u9519\u8BEF\uFF08\u5DF2\u5FFD\u7565\uFF09:",e)}this.ws=null}this.connectionTimeout&&(clearTimeout(this.connectionTimeout),this.connectionTimeout=null),this.isConnected=!1,this.serverInitialized=!1}stopReconnect(){this.reconnectState.timer&&(clearTimeout(this.reconnectState.timer),this.reconnectState.timer=null)}handleMessage(e){this.logger.debug("\u6536\u5230 MCP \u6D88\u606F:",JSON.stringify(e,null,2)),e.method&&this.handleServerRequest(e)}handleServerRequest(e){switch(e.method){case"initialize":case"notifications/initialized":this.sendResponse(e.id,{protocolVersion:"2024-11-05",capabilities:{tools:{listChanged:!0},logging:{}},serverInfo:{name:"xiaozhi-mcp-server",version:"1.0.0"}}),this.serverInitialized=!0,this.logger.info("MCP \u670D\u52A1\u5668\u521D\u59CB\u5316\u5B8C\u6210");break;case"tools/list":{let t=this.getTools();this.sendResponse(e.id,{tools:t}),this.logger.info(`MCP \u5DE5\u5177\u5217\u8868\u5DF2\u53D1\u9001 (${t.length}\u4E2A\u5DE5\u5177)`);break}case"tools/call":{this.handleToolCall(e).catch(t=>{this.logger.error("\u5904\u7406\u5DE5\u5177\u8C03\u7528\u65F6\u53D1\u751F\u672A\u6355\u83B7\u9519\u8BEF:",t)});break}case"ping":this.sendResponse(e.id,{}),this.logger.debug("\u56DE\u5E94 MCP ping \u6D88\u606F");break;default:this.logger.warn(`\u672A\u77E5\u7684 MCP \u8BF7\u6C42: ${e.method}`)}}sendResponse(e,t){if(this.logger.debug(`\u5C1D\u8BD5\u53D1\u9001\u54CD\u5E94: id=${e}, isConnected=${this.isConnected}, wsReadyState=${this.ws?.readyState}`),this.isConnected&&this.ws?.readyState===q.OPEN){let r={jsonrpc:"2.0",id:e,result:t};try{this.ws.send(JSON.stringify(r)),this.logger.info(`\u54CD\u5E94\u5DF2\u53D1\u9001: id=${e}`,{responseSize:JSON.stringify(r).length})}catch(n){this.logger.error(`\u53D1\u9001\u54CD\u5E94\u5931\u8D25: id=${e}`,n)}}else this.logger.error(`\u65E0\u6CD5\u53D1\u9001\u54CD\u5E94: id=${e}, \u8FDE\u63A5\u72B6\u6001\u68C0\u67E5\u5931\u8D25`,{isConnected:this.isConnected,wsReadyState:this.ws?.readyState,wsReadyStateText:this.ws?.readyState===q.OPEN?"OPEN":this.ws?.readyState===q.CONNECTING?"CONNECTING":this.ws?.readyState===q.CLOSING?"CLOSING":this.ws?.readyState===q.CLOSED?"CLOSED":"UNKNOWN"}),(!this.isConnected||this.ws?.readyState!==q.OPEN)&&(this.logger.warn(`\u5C1D\u8BD5\u91CD\u65B0\u8FDE\u63A5\u4EE5\u53D1\u9001\u54CD\u5E94: id=${e}`),this.scheduleReconnect())}getStatus(){return{connected:this.isConnected,initialized:this.serverInitialized,url:this.endpointUrl,availableTools:this.tools.size,connectionState:this.connectionState,reconnectAttempts:this.reconnectState.attempts,lastError:this.reconnectState.lastError?.message||null}}disconnect(){this.logger.info("\u4E3B\u52A8\u65AD\u5F00 MCP \u8FDE\u63A5"),this.reconnectState.isManualDisconnect=!0,this.stopReconnect(),this.cleanupConnection(),this.connectionState="disconnected"}async reconnect(){this.logger.info("\u624B\u52A8\u91CD\u8FDE MCP \u63A5\u5165\u70B9"),this.stopReconnect(),this.reconnectState.attempts=0,this.reconnectState.nextInterval=this.reconnectOptions.initialInterval,this.reconnectState.isManualDisconnect=!1,this.cleanupConnection(),await this.connect()}enableReconnect(){this.reconnectOptions.enabled=!0,this.logger.info("\u81EA\u52A8\u91CD\u8FDE\u5DF2\u542F\u7528")}disableReconnect(){this.reconnectOptions.enabled=!1,this.stopReconnect(),this.logger.info("\u81EA\u52A8\u91CD\u8FDE\u5DF2\u7981\u7528")}updateReconnectOptions(e){this.reconnectOptions={...this.reconnectOptions,...e},this.logger.info("\u91CD\u8FDE\u914D\u7F6E\u5DF2\u66F4\u65B0",e)}getReconnectOptions(){return{...this.reconnectOptions}}resetReconnectState(){this.stopReconnect(),this.reconnectState.attempts=0,this.reconnectState.nextInterval=this.reconnectOptions.initialInterval,this.reconnectState.lastError=null,this.logger.info("\u91CD\u8FDE\u72B6\u6001\u5DF2\u91CD\u7F6E")}async handleToolCall(e){if(e.id===void 0||e.id===null)throw new P(-32602,"\u8BF7\u6C42 ID \u4E0D\u80FD\u4E3A\u7A7A");let t=e.id,r=null;try{let n=this.validateToolCallParams(e.params);r=this.recordCallStart(n.name,t),this.logger.info(`\u5F00\u59CB\u5904\u7406\u5DE5\u5177\u8C03\u7528: ${n.name}`,{requestId:t,toolName:n.name,hasArguments:!!n.arguments});let o=this.serviceManager;if(!o)throw new P(-32001,"MCPServiceManager \u672A\u8BBE\u7F6E");let s=await this.executeToolWithRetry(o,n.name,n.arguments||{});this.sendResponse(t,{content:s.content||[{type:"text",text:JSON.stringify(s)}],isError:s.isError||!1}),r&&this.recordCallEnd(r,!0),this.logger.info(`\u5DE5\u5177\u8C03\u7528\u6210\u529F: ${n.name}`,{requestId:t,duration:r?.duration?`${r.duration}ms`:"unknown"})}catch(n){if(r){let o=n instanceof P?n.code:-32e3,s=n instanceof Error?n.message:"\u672A\u77E5\u9519\u8BEF";this.recordCallEnd(r,!1,o,s)}this.handleToolCallError(n,t,r?.duration||0)}}validateToolCallParams(e){if(!e||typeof e!="object")throw new P(-32602,"\u8BF7\u6C42\u53C2\u6570\u5FC5\u987B\u662F\u5BF9\u8C61");if(!e.name||typeof e.name!="string")throw new P(-32602,"\u5DE5\u5177\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(e.arguments!==void 0&&(typeof e.arguments!="object"||Array.isArray(e.arguments)))throw new P(-32602,"\u5DE5\u5177\u53C2\u6570\u5FC5\u987B\u662F\u5BF9\u8C61");return{name:e.name,arguments:e.arguments}}async executeToolWithRetry(e,t,r){let n=null;for(let o=1;o<=this.retryConfig.maxAttempts;o++)try{return await this.executeToolWithTimeout(e,t,r,this.toolCallConfig.timeout)}catch(s){if(s instanceof P?n=s:n=new P(-32e3,s instanceof Error?s.message:"\u672A\u77E5\u9519\u8BEF"),this.retryConfig.retryableErrors.includes(n.code)&&o<this.retryConfig.maxAttempts){let c=Math.min(this.retryConfig.initialDelay*this.retryConfig.backoffMultiplier**(o-1),this.retryConfig.maxDelay);this.logger.warn(`\u5DE5\u5177\u8C03\u7528\u5931\u8D25\uFF0C\u5C06\u5728 ${c}ms \u540E\u91CD\u8BD5 (${o}/${this.retryConfig.maxAttempts})`,{toolName:t,error:n.message,attempt:o,delay:c}),await new Promise(l=>setTimeout(l,c));continue}break}throw n}async executeToolWithTimeout(e,t,r,n=3e4){return new Promise((o,s)=>{let c=setTimeout(()=>{s(new P(-32002,`\u5DE5\u5177\u8C03\u7528\u8D85\u65F6 (${n}ms): ${t}`))},n);e.callTool(t,r).then(l=>{clearTimeout(c),o(l)}).catch(l=>{clearTimeout(c),l.message?.includes("\u672A\u627E\u5230\u5DE5\u5177")?s(new P(-32601,`\u5DE5\u5177\u4E0D\u5B58\u5728: ${t}`)):l.message?.includes("\u670D\u52A1")&&l.message?.includes("\u4E0D\u53EF\u7528")?s(new P(-32001,l.message)):l.message?.includes("\u6682\u65F6\u4E0D\u53EF\u7528")?s(new P(-32001,l.message)):l.message?.includes("\u6301\u7EED\u4E0D\u53EF\u7528")?s(new P(-32001,l.message)):s(new P(-32e3,`\u5DE5\u5177\u6267\u884C\u5931\u8D25: ${l.message}`))})})}handleToolCallError(e,t,r){let n;e instanceof P?n={code:e.code,message:e.message,data:e.data}:n={code:-32e3,message:e?.message||"\u672A\u77E5\u9519\u8BEF",data:{originalError:e?.toString()||"null"}},this.sendErrorResponse(t,n),this.logger.error("\u5DE5\u5177\u8C03\u7528\u5931\u8D25",{requestId:t,duration:`${r}ms`,error:n})}sendErrorResponse(e,t){if(this.isConnected&&this.ws?.readyState===q.OPEN){let r={jsonrpc:"2.0",id:e,error:t};this.ws.send(JSON.stringify(r)),this.logger.debug("\u5DF2\u53D1\u9001\u9519\u8BEF\u54CD\u5E94:",r)}}recordCallStart(e,t){let r={id:String(t),toolName:e,startTime:new Date,success:!1};return this.callRecords.push(r),this.callRecords.length>this.maxCallRecords&&this.callRecords.shift(),r}recordCallEnd(e,t,r,n){e.endTime=new Date,e.duration=e.endTime.getTime()-e.startTime.getTime(),e.success=t,e.errorCode=r,e.errorMessage=n,this.updatePerformanceMetrics(e)}updatePerformanceMetrics(e){if(this.performanceMetrics.totalCalls++,e.success?this.performanceMetrics.successfulCalls++:this.performanceMetrics.failedCalls++,e.duration!==void 0){e.duration<this.performanceMetrics.minResponseTime&&(this.performanceMetrics.minResponseTime=e.duration),e.duration>this.performanceMetrics.maxResponseTime&&(this.performanceMetrics.maxResponseTime=e.duration);let t=this.callRecords.filter(n=>n.duration!==void 0).reduce((n,o)=>n+(o.duration||0),0),r=this.callRecords.filter(n=>n.duration!==void 0).length;this.performanceMetrics.averageResponseTime=r>0?t/r:0}this.performanceMetrics.successRate=this.performanceMetrics.totalCalls>0?this.performanceMetrics.successfulCalls/this.performanceMetrics.totalCalls*100:0,this.performanceMetrics.lastUpdated=new Date}getPerformanceMetrics(){return{...this.performanceMetrics}}getCallRecords(e){let t=[...this.callRecords].reverse();return e?t.slice(0,e):t}resetPerformanceMetrics(){this.performanceMetrics={totalCalls:0,successfulCalls:0,failedCalls:0,averageResponseTime:0,minResponseTime:Number.MAX_VALUE,maxResponseTime:0,successRate:0,lastUpdated:new Date},this.callRecords=[]}updateToolCallConfig(e){this.toolCallConfig={...this.toolCallConfig,...e},this.logger.info("\u5DE5\u5177\u8C03\u7528\u914D\u7F6E\u5DF2\u66F4\u65B0",this.toolCallConfig)}updateRetryConfig(e){this.retryConfig={...this.retryConfig,...e},this.logger.info("\u91CD\u8BD5\u914D\u7F6E\u5DF2\u66F4\u65B0",this.retryConfig)}getConfiguration(){return{toolCall:{...this.toolCallConfig},retry:{...this.retryConfig}}}getEnhancedStatus(){return{connected:this.isConnected,initialized:this.serverInitialized,url:this.endpointUrl,availableTools:this.tools.size,connectionState:this.connectionState,reconnectAttempts:this.reconnectState.attempts,lastError:this.reconnectState.lastError?.message||null,performance:this.getPerformanceMetrics(),configuration:this.getConfiguration()}}}});var ve,Oe=g(()=>{"use strict";C();ve=class{static{a(this,"TransportAdapter")}logger;messageHandler;connectionId;config;state="disconnected";constructor(e,t){this.messageHandler=e,this.config=t,this.connectionId=this.generateConnectionId(),this.logger=h}async handleIncomingMessage(e){try{this.logger.debug(`\u5904\u7406\u63A5\u6536\u5230\u7684\u6D88\u606F: ${e.method}`,e);let t=await this.messageHandler.handleMessage(e);this.logger.debug("\u53D1\u9001\u54CD\u5E94\u6D88\u606F:",t),await this.sendMessage(t)}catch(t){this.logger.error(`\u5904\u7406\u6D88\u606F\u65F6\u51FA\u9519: ${e.method}`,t);let r=this.createErrorResponse(t,e.id);await this.sendMessage(r)}}createErrorResponse(e,t){let r=-32603;return e.message.includes("\u672A\u627E\u5230\u5DE5\u5177")||e.message.includes("\u672A\u77E5\u7684\u65B9\u6CD5")?r=-32601:(e.message.includes("\u53C2\u6570")||e.message.includes("\u4E0D\u80FD\u4E3A\u7A7A"))&&(r=-32602),{jsonrpc:"2.0",error:{code:r,message:e.message,data:{stack:e.stack}},id:t||null}}generateConnectionId(){let e=Date.now(),t=Math.random().toString(36).substr(2,9);return`${this.config.name}_${e}_${t}`}getConnectionId(){return this.connectionId}getState(){return this.state}setState(e){let t=this.state;this.state=e,t!==e&&(this.logger.info(`\u8FDE\u63A5\u72B6\u6001\u53D8\u66F4: ${t} -> ${e}`),this.onStateChange(t,e))}onStateChange(e,t){}getConfig(){return{...this.config}}getMessageHandler(){return this.messageHandler}parseMessage(e){try{let t=JSON.parse(e.trim());return!t.jsonrpc||t.jsonrpc!=="2.0"?(this.logger.warn("\u6536\u5230\u975E JSON-RPC 2.0 \u683C\u5F0F\u7684\u6D88\u606F",t),null):t.method?t:(this.logger.warn("\u6536\u5230\u6CA1\u6709 method \u5B57\u6BB5\u7684\u6D88\u606F",t),null)}catch(t){return this.logger.error("\u89E3\u6790 JSON \u6D88\u606F\u5931\u8D25",{data:e,error:t}),null}}serializeMessage(e){try{return JSON.stringify(e)}catch(t){this.logger.error("\u5E8F\u5217\u5316\u6D88\u606F\u5931\u8D25",{message:e,error:t});let r=t instanceof Error?t.message:String(t);throw new Error(`\u6D88\u606F\u5E8F\u5217\u5316\u5931\u8D25: ${r}`)}}validateMessage(e){return!(!e||typeof e!="object"||e.jsonrpc!=="2.0"||e.method&&typeof e.method!="string"||!e.method&&!e.result&&!e.error)}createTimeoutPromise(e,t){return Promise.race([e,new Promise((r,n)=>{setTimeout(()=>{n(new Error(`\u64CD\u4F5C\u8D85\u65F6: ${t}ms`))},t)})])}}});import{randomUUID as wn}from"crypto";import Mt from"express";var Ve,hr=g(()=>{"use strict";Oe();Ve=class extends ve{static{a(this,"HTTPAdapter")}app;server=null;clients=new Map;port;host;enableSSE;enableRPC;corsOrigin;maxClients;constructor(e,t={name:"http"}){super(e,t),this.port=t.port||3e3,this.host=t.host||"0.0.0.0",this.enableSSE=t.enableSSE!==!1,this.enableRPC=t.enableRPC!==!1,this.corsOrigin=t.corsOrigin||"*",this.maxClients=t.maxClients!==void 0?t.maxClients:100,this.app=Mt(),this.setupMiddleware()}async initialize(){this.logger.info("\u521D\u59CB\u5316 HTTP \u9002\u914D\u5668");try{this.setupRoutes(),this.setState("connecting"),this.logger.info("HTTP \u9002\u914D\u5668\u521D\u59CB\u5316\u5B8C\u6210")}catch(e){throw this.logger.error("HTTP \u9002\u914D\u5668\u521D\u59CB\u5316\u5931\u8D25",e),this.setState("error"),e}}async start(){if(this.server){this.logger.warn("HTTP \u670D\u52A1\u5668\u5DF2\u5728\u8FD0\u884C");return}return this.logger.info(`\u542F\u52A8 HTTP \u670D\u52A1\u5668\u5728 ${this.host}:${this.port}`),new Promise((e,t)=>{this.server=this.app.listen(this.port,this.host,()=>{this.setState("connected"),this.logger.info("HTTP \u9002\u914D\u5668\u542F\u52A8\u6210\u529F"),this.logger.info(`- RPC \u7AEF\u70B9: http://${this.host}:${this.port}/rpc`),this.enableSSE&&(this.logger.info(`- SSE \u7AEF\u70B9: http://${this.host}:${this.port}/sse`),this.logger.info(`- \u6D88\u606F\u7AEF\u70B9: http://${this.host}:${this.port}/messages`)),e()}),this.server?.on("error",r=>{this.logger.error("HTTP \u670D\u52A1\u5668\u9519\u8BEF",r),this.setState("error"),t(r)})})}async stop(){if(this.server)return this.logger.info("\u505C\u6B62 HTTP \u670D\u52A1\u5668"),new Promise(e=>{for(let t of this.clients.values())t.response.end();this.clients.clear(),this.server.close(()=>{this.server=null,this.setState("disconnected"),this.logger.info("HTTP \u670D\u52A1\u5668\u5DF2\u505C\u6B62"),e()})})}async sendMessage(e){this.clients.size>0&&this.broadcastToClients(e)}setupMiddleware(){this.app.use(Mt.json({limit:"10mb"})),this.app.use(Mt.urlencoded({extended:!0})),this.app.use((e,t,r)=>{t.header("Access-Control-Allow-Origin",this.corsOrigin),t.header("Access-Control-Allow-Methods","GET, POST, OPTIONS"),t.header("Access-Control-Allow-Headers","Content-Type, Accept"),t.header("Cache-Control","no-cache"),r()}),this.app.use((e,t,r)=>{this.logger.debug(`${e.method} ${e.path}`,{query:e.query,headers:e.headers}),r()})}setupRoutes(){this.enableSSE&&(this.app.get("/sse",this.handleSSE.bind(this)),this.app.post("/messages",this.handleMessages.bind(this))),this.enableRPC&&this.app.post("/rpc",this.handleRPC.bind(this)),this.app.get("/status",this.handleStatus.bind(this)),this.app.get("/health",this.handleHealth.bind(this))}handleSSE(e,t){if(this.clients.size>=this.maxClients){t.status(503).json({error:"\u670D\u52A1\u5668\u7E41\u5FD9\uFF0C\u5BA2\u6237\u7AEF\u8FDE\u63A5\u6570\u5DF2\u8FBE\u4E0A\u9650",maxClients:this.maxClients});return}let r=Date.now().toString(),n=wn();t.setHeader("Content-Type","text/event-stream"),t.setHeader("Cache-Control","no-cache, no-transform"),t.setHeader("Connection","keep-alive"),t.setHeader("X-Accel-Buffering","no");let o={id:r,sessionId:n,response:t,connectedAt:new Date};this.clients.set(n,o),this.logger.info(`SSE \u5BA2\u6237\u7AEF\u5DF2\u8FDE\u63A5: ${r} (\u4F1A\u8BDD: ${n})`),t.write(`event: endpoint
|
|
11
|
+
data: /messages?sessionId=${n}
|
|
12
|
+
|
|
13
|
+
`),e.on("close",()=>{this.clients.delete(n),this.logger.info(`SSE \u5BA2\u6237\u7AEF\u5DF2\u65AD\u5F00\u8FDE\u63A5: ${r} (\u4F1A\u8BDD: ${n})`)}),e.on("error",s=>{this.logger.error(`SSE \u5BA2\u6237\u7AEF\u8FDE\u63A5\u9519\u8BEF: ${r}`,s),this.clients.delete(n)})}async handleMessages(e,t){try{let r=e.query.sessionId,n=e.body;if(this.logger.debug(`\u6536\u5230 SSE \u6D88\u606F (\u4F1A\u8BDD: ${r}):`,n),!r||!this.clients.has(r)){t.status(400).json({jsonrpc:"2.0",error:{code:-32600,message:"\u65E0\u6548\u6216\u7F3A\u5C11 sessionId"},id:n.id});return}let o=await this.messageHandler.handleMessage(n);this.logger.debug("SSE \u6D88\u606F\u5904\u7406\u54CD\u5E94:",o);let s=this.clients.get(r);s&&this.sendToClient(s,o),t.status(202).send()}catch(r){this.logger.error("\u5904\u7406 SSE \u6D88\u606F\u65F6\u51FA\u9519",r),t.status(500).json({jsonrpc:"2.0",error:{code:-32603,message:r.message}})}}async handleRPC(e,t){try{let r=e.body;this.logger.debug("\u6536\u5230 RPC \u6D88\u606F:",r);let n=await this.messageHandler.handleMessage(r);t.json(n)}catch(r){this.logger.error("\u5904\u7406 RPC \u6D88\u606F\u65F6\u51FA\u9519",r),t.status(500).json({jsonrpc:"2.0",error:{code:-32603,message:r.message},id:e.body?.id||null})}}handleStatus(e,t){t.json({status:"ok",mode:"mcp-server",serviceManager:"running",clients:this.clients.size,tools:0,maxClients:this.maxClients,enableSSE:this.enableSSE,enableRPC:this.enableRPC,uptime:process.uptime()})}handleHealth(e,t){t.json({status:"ok",mode:"mcp-server",timestamp:new Date().toISOString()})}sendToClient(e,t){try{let r=this.serializeMessage(t);e.response.write(`data: ${r}
|
|
14
|
+
|
|
15
|
+
`),this.logger.debug(`\u6D88\u606F\u5DF2\u53D1\u9001\u5230\u5BA2\u6237\u7AEF ${e.id}`,{sessionId:e.sessionId,messageId:t.id})}catch(r){this.logger.error(`\u5411\u5BA2\u6237\u7AEF ${e.id} \u53D1\u9001\u6D88\u606F\u5931\u8D25`,r),this.clients.delete(e.sessionId)}}broadcastToClients(e){for(let t of this.clients.values())this.sendToClient(t,e)}getStatus(){return{isRunning:this.server!==null,port:this.port,host:this.host,clientCount:this.clients.size,maxClients:this.maxClients,enableSSE:this.enableSSE,enableRPC:this.enableRPC,connectionId:this.connectionId,state:this.state}}getClients(){return Array.from(this.clients.values()).map(e=>({id:e.id,sessionId:e.sessionId,connectedAt:e.connectedAt}))}}});var pr=g(()=>{"use strict";Oe()});import hs,{WebSocketServer as ps}from"ws";var dr=g(()=>{"use strict";Oe()});import{SSEClientTransport as ur}from"@modelcontextprotocol/sdk/client/sse.js";import{StdioClientTransport as Mn}from"@modelcontextprotocol/sdk/client/stdio.js";import{StreamableHTTPClientTransport as Pn}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{EventSource as Rn}from"eventsource";function Tn(){return h}function In(i){switch(Tn().info(`[TransportFactory] \u521B\u5EFA ${i.type} transport for ${i.name}`),i.type){case"stdio":return xn(i);case"sse":return On(i);case"modelscope-sse":return $n(i);case"streamable-http":return An(i);default:throw new Error(`\u4E0D\u652F\u6301\u7684\u4F20\u8F93\u7C7B\u578B: ${i.type}`)}}function xn(i){if(!i.command)throw new Error("stdio transport \u9700\u8981 command \u914D\u7F6E");return new Mn({command:i.command,args:i.args||[]})}function On(i){if(!i.url)throw new Error("SSE transport \u9700\u8981 URL \u914D\u7F6E");let e=new URL(i.url),t=Nn(i);return new ur(e,t)}function $n(i){if(!i.url)throw new Error("ModelScope SSE transport \u9700\u8981 URL \u914D\u7F6E");if(!i.apiKey)throw new Error("ModelScope SSE transport \u9700\u8981 apiKey \u914D\u7F6E");let e=new URL(i.url),t=Dn(i);return new ur(e,t)}function An(i){if(!i.url)throw new Error("StreamableHTTP transport \u9700\u8981 URL \u914D\u7F6E");let e=new URL(i.url),t=kn(i);return new Pn(e,t)}function Nn(i){let e={};return i.apiKey?e.headers={Authorization:`Bearer ${i.apiKey}`,...i.headers}:i.headers&&(e.headers=i.headers),e}function Dn(i){let e=i.apiKey;return i.customSSEOptions?i.customSSEOptions:{eventSourceInit:{fetch:a(async(t,r)=>{let n={...r?.headers,Authorization:`Bearer ${e}`};return fetch(t,{...r,headers:n})},"fetch")},requestInit:{headers:{Authorization:`Bearer ${e}`,...i.headers}}}}function kn(i){let e={};return i.apiKey?e.headers={Authorization:`Bearer ${i.apiKey}`,...i.headers}:i.headers&&(e.headers=i.headers),e}function Hn(i){if(!i.name||typeof i.name!="string")throw new Error("\u914D\u7F6E\u5FC5\u987B\u5305\u542B\u6709\u6548\u7684 name \u5B57\u6BB5");if(!i.type)throw new Error("\u914D\u7F6E\u5FC5\u987B\u5305\u542B type \u5B57\u6BB5");switch(i.type){case"stdio":if(!i.command)throw new Error("stdio \u7C7B\u578B\u9700\u8981 command \u5B57\u6BB5");break;case"sse":case"streamable-http":if(!i.url)throw new Error(`${i.type} \u7C7B\u578B\u9700\u8981 url \u5B57\u6BB5`);break;case"modelscope-sse":if(!i.url)throw new Error("modelscope-sse \u7C7B\u578B\u9700\u8981 url \u5B57\u6BB5");if(!i.apiKey)throw new Error("modelscope-sse \u7C7B\u578B\u9700\u8981 apiKey \u5B57\u6BB5\u3002\u8BF7\u5728\u914D\u7F6E\u6587\u4EF6\u4E2D\u8BBE\u7F6E modelscope.apiKey \u6216\u786E\u4FDD\u670D\u52A1\u914D\u7F6E\u5305\u542B apiKey");break;default:throw new Error(`\u4E0D\u652F\u6301\u7684\u4F20\u8F93\u7C7B\u578B: ${i.type}`)}}function jn(){return["stdio","sse","modelscope-sse","streamable-http"]}var Rt,mr=g(()=>{"use strict";C();Ge();typeof global<"u"&&!global.EventSource&&(global.EventSource=Rn);a(Tn,"getLogger");a(In,"createTransport");a(xn,"createStdioTransport");a(On,"createSSETransport");a($n,"createModelScopeSSETransport");a(An,"createStreamableHTTPTransport");a(Nn,"createSSEOptions");a(Dn,"createModelScopeSSEOptions");a(kn,"createStreamableHTTPOptions");a(Hn,"validateConfig");a(jn,"getSupportedTypes");Rt={create:In,validateConfig:Hn,getSupportedTypes:jn}});import{Client as Fn}from"@modelcontextprotocol/sdk/client/index.js";var $e,Xe,Ge=g(()=>{"use strict";C();mr();$e=(n=>(n.STDIO="stdio",n.SSE="sse",n.STREAMABLE_HTTP="streamable-http",n.MODELSCOPE_SSE="modelscope-sse",n))($e||{}),Xe=class{static{a(this,"MCPService")}config;client=null;transport=null;tools=new Map;connectionState="disconnected";reconnectOptions;reconnectState;logger;connectionTimeout=null;initialized=!1;pingOptions;pingTimer=null;pingFailureCount=0;lastPingTime=null;isPinging=!1;constructor(e,t){this.config=e,this.logger=h,this.validateConfig(),this.reconnectOptions={enabled:!0,maxAttempts:10,initialInterval:3e3,maxInterval:3e4,backoffStrategy:"exponential",backoffMultiplier:1.5,timeout:1e4,jitter:!0,...t?.reconnect,...e.reconnect},this.pingOptions={enabled:!0,interval:3e4,timeout:5e3,maxFailures:3,startDelay:5e3,...e.ping},this.reconnectState={attempts:0,nextInterval:this.reconnectOptions.initialInterval,timer:null,lastError:null,isManualDisconnect:!1}}logWithTag(e,t,...r){let n=`[MCP-${this.config.name}] ${t}`;this.logger[e](n,...r)}validateConfig(){Rt.validateConfig(this.config)}async connect(){if(this.connectionState==="connecting")throw new Error("\u8FDE\u63A5\u6B63\u5728\u8FDB\u884C\u4E2D\uFF0C\u8BF7\u7B49\u5F85\u8FDE\u63A5\u5B8C\u6210");return this.cleanupConnection(),this.reconnectState.isManualDisconnect=!1,this.attemptConnection()}async attemptConnection(){return this.connectionState="connecting",this.logWithTag("info",`\u6B63\u5728\u8FDE\u63A5 MCP \u670D\u52A1: ${this.config.name} (\u5C1D\u8BD5 ${this.reconnectState.attempts+1}/${this.reconnectOptions.maxAttempts})`),new Promise((e,t)=>{this.connectionTimeout=setTimeout(()=>{let r=new Error(`\u8FDE\u63A5\u8D85\u65F6 (${this.reconnectOptions.timeout}ms)`);this.handleConnectionError(r),t(r)},this.reconnectOptions.timeout);try{this.client=new Fn({name:`xiaozhi-${this.config.name}-client`,version:"1.0.0"},{capabilities:{tools:{}}}),this.transport=Rt.create(this.config),this.client.connect(this.transport).then(async()=>{this.handleConnectionSuccess(),await this.refreshTools(),e()}).catch(r=>{this.handleConnectionError(r),t(r)})}catch(r){this.handleConnectionError(r),t(r)}})}handleConnectionSuccess(){this.connectionTimeout&&(clearTimeout(this.connectionTimeout),this.connectionTimeout=null),this.connectionState="connected",this.initialized=!0,this.reconnectState.attempts=0,this.reconnectState.nextInterval=this.reconnectOptions.initialInterval,this.reconnectState.lastError=null,this.resetPingState(),this.logWithTag("info",`MCP \u670D\u52A1 ${this.config.name} \u8FDE\u63A5\u5DF2\u5EFA\u7ACB`),this.startPingMonitoring()}handleConnectionError(e){this.connectionTimeout&&(clearTimeout(this.connectionTimeout),this.connectionTimeout=null),this.reconnectState.lastError=e,this.logger.error(`MCP \u670D\u52A1 ${this.config.name} \u8FDE\u63A5\u9519\u8BEF:`,e.message),this.cleanupConnection(),this.shouldReconnect()?this.scheduleReconnect():(this.connectionState="failed",this.logger.warn(`${this.config.name} \u5DF2\u8FBE\u5230\u6700\u5927\u91CD\u8FDE\u6B21\u6570 (${this.reconnectOptions.maxAttempts})\uFF0C\u505C\u6B62\u91CD\u8FDE`))}shouldReconnect(){return this.reconnectOptions.enabled&&this.reconnectState.attempts<this.reconnectOptions.maxAttempts&&!this.reconnectState.isManualDisconnect}scheduleReconnect(){this.connectionState="reconnecting",this.reconnectState.attempts++,this.calculateNextInterval(),this.logger.info(`${this.config.name} \u5C06\u5728 ${this.reconnectState.nextInterval}ms \u540E\u8FDB\u884C\u7B2C ${this.reconnectState.attempts} \u6B21\u91CD\u8FDE`),this.reconnectState.timer&&clearTimeout(this.reconnectState.timer),this.reconnectState.timer=setTimeout(async()=>{try{await this.attemptConnection()}catch{}},this.reconnectState.nextInterval)}calculateNextInterval(){let e;switch(this.reconnectOptions.backoffStrategy){case"fixed":e=this.reconnectOptions.initialInterval;break;case"linear":e=this.reconnectOptions.initialInterval+this.reconnectState.attempts*this.reconnectOptions.backoffMultiplier*1e3;break;case"exponential":e=this.reconnectOptions.initialInterval*this.reconnectOptions.backoffMultiplier**(this.reconnectState.attempts-1);break;default:e=this.reconnectOptions.initialInterval}if(e=Math.min(e,this.reconnectOptions.maxInterval),this.reconnectOptions.jitter){let t=e*.1,r=(Math.random()-.5)*2*t;e+=r}this.reconnectState.nextInterval=Math.max(e,1e3)}cleanupConnection(){if(this.stopPingMonitoring(),this.client){try{this.client.close().catch(()=>{})}catch{}this.client=null}this.transport=null,this.connectionTimeout&&(clearTimeout(this.connectionTimeout),this.connectionTimeout=null),this.initialized=!1}stopReconnect(){this.reconnectState.timer&&(clearTimeout(this.reconnectState.timer),this.reconnectState.timer=null)}async refreshTools(){if(!this.client)throw new Error("\u5BA2\u6237\u7AEF\u672A\u521D\u59CB\u5316");try{let t=(await this.client.listTools()).tools||[];this.tools.clear();for(let r of t)this.tools.set(r.name,r);this.logger.info(`${this.config.name} \u670D\u52A1\u52A0\u8F7D\u4E86 ${t.length} \u4E2A\u5DE5\u5177: ${t.map(r=>r.name).join(", ")}`)}catch(e){throw this.logger.error(`${this.config.name} \u83B7\u53D6\u5DE5\u5177\u5217\u8868\u5931\u8D25:`,e instanceof Error?e.message:String(e)),e}}async disconnect(){this.logger.info(`\u4E3B\u52A8\u65AD\u5F00 MCP \u670D\u52A1 ${this.config.name} \u8FDE\u63A5`),this.reconnectState.isManualDisconnect=!0,this.stopPingMonitoring(),this.stopReconnect(),this.cleanupConnection(),this.connectionState="disconnected"}async reconnect(){this.logger.info(`\u624B\u52A8\u91CD\u8FDE MCP \u670D\u52A1 ${this.config.name}`),this.stopReconnect(),this.reconnectState.attempts=0,this.reconnectState.nextInterval=this.reconnectOptions.initialInterval,this.reconnectState.isManualDisconnect=!1,this.cleanupConnection(),await this.connect()}getTools(){return Array.from(this.tools.values())}async callTool(e,t){if(!this.client)throw new Error(`\u670D\u52A1 ${this.config.name} \u672A\u8FDE\u63A5`);if(!this.tools.has(e))throw new Error(`\u5DE5\u5177 ${e} \u5728\u670D\u52A1 ${this.config.name} \u4E2D\u4E0D\u5B58\u5728`);this.logger.info(`\u8C03\u7528 ${this.config.name} \u670D\u52A1\u7684\u5DE5\u5177 ${e}\uFF0C\u53C2\u6570:`,JSON.stringify(t));try{let r=await this.client.callTool({name:e,arguments:t||{}});return this.logger.info(`\u5DE5\u5177 ${e} \u8C03\u7528\u6210\u529F\uFF0C\u7ED3\u679C:`,`${JSON.stringify(r).substring(0,500)}...`),r}catch(r){throw this.logger.error(`\u5DE5\u5177 ${e} \u8C03\u7528\u5931\u8D25:`,r instanceof Error?r.message:String(r)),r}}getConfig(){return this.config}getStatus(){return{name:this.config.name,connected:this.connectionState==="connected",initialized:this.initialized,transportType:this.config.type,toolCount:this.tools.size,lastError:this.reconnectState.lastError?.message,reconnectAttempts:this.reconnectState.attempts,connectionState:this.connectionState,pingEnabled:this.pingOptions.enabled,lastPingTime:this.lastPingTime||void 0,pingFailureCount:this.pingFailureCount,isPinging:this.isPinging}}isConnected(){return this.connectionState==="connected"&&this.initialized}enableReconnect(){this.reconnectOptions.enabled=!0,this.logger.info(`${this.config.name} \u81EA\u52A8\u91CD\u8FDE\u5DF2\u542F\u7528`)}disableReconnect(){this.reconnectOptions.enabled=!1,this.stopReconnect(),this.logger.info(`${this.config.name} \u81EA\u52A8\u91CD\u8FDE\u5DF2\u7981\u7528`)}updateReconnectOptions(e){this.reconnectOptions={...this.reconnectOptions,...e},this.logger.info(`${this.config.name} \u91CD\u8FDE\u914D\u7F6E\u5DF2\u66F4\u65B0`,e)}getReconnectOptions(){return{...this.reconnectOptions}}resetReconnectState(){this.stopReconnect(),this.reconnectState.attempts=0,this.reconnectState.nextInterval=this.reconnectOptions.initialInterval,this.reconnectState.lastError=null,this.logger.info(`${this.config.name} \u91CD\u8FDE\u72B6\u6001\u5DF2\u91CD\u7F6E`)}startPingMonitoring(){!this.pingOptions.enabled||this.pingTimer||!this.isConnected()||(this.logger.info(`${this.config.name} \u542F\u52A8ping\u76D1\u63A7\uFF0C\u95F4\u9694: ${this.pingOptions.interval}ms`),setTimeout(()=>{this.isConnected()&&!this.pingTimer&&(this.pingTimer=setInterval(()=>{this.performPing()},this.pingOptions.interval))},this.pingOptions.startDelay))}stopPingMonitoring(){this.pingTimer&&(clearInterval(this.pingTimer),this.pingTimer=null,this.logger.debug(`${this.config.name} \u505C\u6B62ping\u76D1\u63A7`))}async performPing(){if(!this.client||this.isPinging||!this.isConnected())return;this.isPinging=!0;let e=performance.now();try{this.logger.debug(`${this.config.name} \u53D1\u9001ping\u8BF7\u6C42\uFF08\u901A\u8FC7listTools\u68C0\u6D4B\u8FDE\u63A5\uFF09`);let t=this.client.listTools(),r=new Promise((o,s)=>{setTimeout(()=>{s(new Error(`Ping\u8D85\u65F6 (${this.pingOptions.timeout}ms)`))},this.pingOptions.timeout)});await Promise.race([t,r]);let n=performance.now()-e;this.handlePingSuccess(n)}catch(t){let r=performance.now()-e;this.handlePingFailure(t,r)}finally{this.isPinging=!1}}handlePingSuccess(e){this.pingFailureCount=0,this.lastPingTime=new Date,this.logger.debug(`${this.config.name} ping\u6210\u529F\uFF0C\u5EF6\u8FDF: ${e.toFixed(2)}ms`)}handlePingFailure(e,t){if(this.pingFailureCount++,this.logger.warn(`${this.config.name} ping\u5931\u8D25 (${this.pingFailureCount}/${this.pingOptions.maxFailures})\uFF0C\u5EF6\u8FDF: ${t.toFixed(2)}ms\uFF0C\u9519\u8BEF: ${e.message}`),this.pingFailureCount>=this.pingOptions.maxFailures){this.logger.error(`${this.config.name} \u8FDE\u7EEDping\u5931\u8D25\u8FBE\u5230\u9608\u503C\uFF0C\u89E6\u53D1\u91CD\u8FDE\u673A\u5236`),this.stopPingMonitoring();let r=new Error(`Ping\u68C0\u6D4B\u5931\u8D25\uFF0C\u8FDE\u7EED\u5931\u8D25${this.pingFailureCount}\u6B21\uFF0C\u8FDE\u63A5\u53EF\u80FD\u5DF2\u65AD\u5F00`);this.handleConnectionError(r)}}resetPingState(){this.pingFailureCount=0,this.lastPingTime=null,this.isPinging=!1}enablePing(){this.pingOptions.enabled=!0,this.logger.info(`${this.config.name} ping\u76D1\u63A7\u5DF2\u542F\u7528`),this.isConnected()&&this.startPingMonitoring()}disablePing(){this.pingOptions.enabled=!1,this.stopPingMonitoring(),this.logger.info(`${this.config.name} ping\u76D1\u63A7\u5DF2\u7981\u7528`)}updatePingOptions(e){let t=this.pingOptions.enabled;this.pingOptions={...this.pingOptions,...e},this.logger.info(`${this.config.name} ping\u914D\u7F6E\u5DF2\u66F4\u65B0`,e),t!==this.pingOptions.enabled&&(this.pingOptions.enabled&&this.isConnected()?this.startPingMonitoring():this.pingOptions.enabled||this.stopPingMonitoring())}getPingOptions(){return{...this.pingOptions}}}});var Ae,fr,Tt=g(()=>{"use strict";C();ie();Ge();Ae=class{static{a(this,"MCPServiceManager")}services=new Map;configs={};logger;tools=new Map;constructor(e){this.logger=h,this.configs=e||{}}async startAllServices(){this.logger.info("[MCPManager] \u6B63\u5728\u542F\u52A8\u6240\u6709 MCP \u670D\u52A1...");let e=Object.entries(this.configs);if(e.length===0){this.logger.warn("[MCPManager] \u6CA1\u6709\u914D\u7F6E\u4EFB\u4F55 MCP \u670D\u52A1\uFF0C\u8BF7\u4F7F\u7528 addServiceConfig() \u6DFB\u52A0\u670D\u52A1\u914D\u7F6E");return}for(let[t]of e)await this.startService(t);this.logger.info("[MCPManager] \u6240\u6709 MCP \u670D\u52A1\u542F\u52A8\u5B8C\u6210")}async startService(e){this.logger.info(`[MCPManager] \u542F\u52A8 MCP \u670D\u52A1: ${e}`);let t=this.configs[e];if(!t)throw new Error(`\u672A\u627E\u5230\u670D\u52A1\u914D\u7F6E: ${e}`);try{this.services.has(e)&&await this.stopService(e);let r=new Xe(t);await r.connect(),this.services.set(e,r),await this.refreshToolsCache();let n=r.getTools();this.logger.info(`[MCPManager] ${e} \u670D\u52A1\u542F\u52A8\u6210\u529F\uFF0C\u52A0\u8F7D\u4E86 ${n.length} \u4E2A\u5DE5\u5177:`,n.map(o=>o.name).join(", "))}catch(r){throw this.logger.error(`[MCPManager] \u542F\u52A8 ${e} \u670D\u52A1\u5931\u8D25:`,r.message),r}}async stopService(e){this.logger.info(`[MCPManager] \u505C\u6B62 MCP \u670D\u52A1: ${e}`);let t=this.services.get(e);if(!t){this.logger.warn(`[MCPManager] \u670D\u52A1 ${e} \u4E0D\u5B58\u5728\u6216\u672A\u542F\u52A8`);return}try{await t.disconnect(),this.services.delete(e),await this.refreshToolsCache(),this.logger.info(`[MCPManager] ${e} \u670D\u52A1\u5DF2\u505C\u6B62`)}catch(r){throw this.logger.error(`[MCPManager] \u505C\u6B62 ${e} \u670D\u52A1\u5931\u8D25:`,r.message),r}}async refreshToolsCache(){this.tools.clear();for(let[e,t]of this.services)if(t.isConnected()){let r=t.getTools();for(let n of r){let o=`${e}__${n.name}`;this.tools.set(o,{serviceName:e,originalName:n.name,tool:n})}}await this.syncToolsConfigToFile()}getAllTools(){let e=[];for(let[t,r]of this.tools)d.isToolEnabled(r.serviceName,r.originalName)&&e.push({name:t,description:r.tool.description||"",inputSchema:r.tool.inputSchema,serviceName:r.serviceName,originalName:r.originalName});return e}async callTool(e,t){this.logger.info(`[MCPManager] \u8C03\u7528\u5DE5\u5177: ${e}\uFF0C\u53C2\u6570:`,t);let r=this.tools.get(e);if(!r)throw new Error(`\u672A\u627E\u5230\u5DE5\u5177: ${e}`);let n=this.services.get(r.serviceName);if(!n)throw new Error(`\u670D\u52A1 ${r.serviceName} \u4E0D\u53EF\u7528`);if(!n.isConnected())throw new Error(`\u670D\u52A1 ${r.serviceName} \u672A\u8FDE\u63A5`);try{let o=await n.callTool(r.originalName,t||{});return this.logger.info(`[MCPManager] \u5DE5\u5177 ${e} \u8C03\u7528\u6210\u529F\uFF0C\u7ED3\u679C:`,o),o}catch(o){throw this.logger.error(`[MCPManager] \u5DE5\u5177 ${e} \u8C03\u7528\u5931\u8D25:`,o.message),o}}async stopAllServices(){this.logger.info("[MCPManager] \u6B63\u5728\u505C\u6B62\u6240\u6709 MCP \u670D\u52A1...");for(let[e,t]of this.services)try{await t.disconnect(),this.logger.info(`[MCPManager] ${e} \u670D\u52A1\u5DF2\u505C\u6B62`)}catch(r){this.logger.error(`[MCPManager] \u505C\u6B62 ${e} \u670D\u52A1\u5931\u8D25:`,r.message)}this.services.clear(),this.tools.clear(),this.logger.info("[MCPManager] \u6240\u6709 MCP \u670D\u52A1\u5DF2\u505C\u6B62")}getStatus(){let e={services:{},totalTools:this.tools.size,availableTools:Array.from(this.tools.keys())};for(let[t,r]of this.services){let n=r.getStatus();e.services[t]={connected:n.connected,clientName:`xiaozhi-${t}-client`}}return e}getService(e){return this.services.get(e)}getAllServices(){return new Map(this.services)}enhanceServiceConfig(e){let t={...e};try{if(e.type==="modelscope-sse"){let r=d.getModelScopeApiKey();if(r)t.apiKey=r,this.logger.info(`[MCPManager] \u4E3A ${e.name} \u670D\u52A1\u6DFB\u52A0 ModelScope API Key`);else throw this.logger.warn(`[MCPManager] ${e.name} \u670D\u52A1\u9700\u8981 ModelScope API Key\uFF0C\u4F46\u672A\u5728\u914D\u7F6E\u4E2D\u627E\u5230`),new Error(`ModelScope SSE \u670D\u52A1 ${e.name} \u9700\u8981 API Key\uFF0C\u8BF7\u5728\u914D\u7F6E\u6587\u4EF6\u4E2D\u8BBE\u7F6E modelscope.apiKey`)}return t}catch(r){throw this.logger.error(`[MCPManager] \u914D\u7F6E\u589E\u5F3A\u5931\u8D25: ${e.name}`,r),r}}addServiceConfig(e,t){let r,n;if(typeof e=="string"&&t)n=e,r=t;else if(typeof e=="object")n=e.name,r=e;else throw new Error("Invalid arguments for addServiceConfig");let o=this.enhanceServiceConfig(r);this.configs[n]=o,this.logger.info(`[MCPManager] \u5DF2\u6DFB\u52A0\u670D\u52A1\u914D\u7F6E: ${n}`)}updateServiceConfig(e,t){let r=this.enhanceServiceConfig(t);this.configs[e]=r,this.logger.info(`[MCPManager] \u5DF2\u66F4\u65B0\u5E76\u589E\u5F3A\u670D\u52A1\u914D\u7F6E: ${e}`)}removeServiceConfig(e){delete this.configs[e],this.logger.info(`[MCPManager] \u5DF2\u79FB\u9664\u670D\u52A1\u914D\u7F6E: ${e}`)}async syncToolsConfigToFile(){try{this.logger.debug("[MCPManager] \u5F00\u59CB\u540C\u6B65\u5DE5\u5177\u914D\u7F6E\u5230\u914D\u7F6E\u6587\u4EF6");let e=d.getMcpServerConfig();for(let[t,r]of this.services){if(!r.isConnected())continue;let n=r.getTools();if(n.length===0)continue;let o=e[t]?.tools||{},s={};for(let I of n){let te=o[I.name];te?s[I.name]={...te,description:I.description||te.description||""}:s[I.name]={description:I.description||"",enable:!0}}let c=n.map(I=>I.name),u=Object.keys(o).filter(I=>!c.includes(I));if(u.length>0&&this.logger.info(`[MCPManager] \u68C0\u6D4B\u5230\u670D\u52A1 ${t} \u79FB\u9664\u4E86 ${u.length} \u4E2A\u5DE5\u5177: ${u.join(", ")}`),this.hasToolsConfigChanged(o,s)){d.updateServerToolsConfig(t,s);let I=Object.keys(s).filter(pe=>!o[pe]),te=Object.keys(s).filter(pe=>{let Zt=o[pe],an=s[pe];return Zt&&Zt.description!==an.description});this.logger.info(`[MCPManager] \u5DF2\u540C\u6B65\u670D\u52A1 ${t} \u7684\u5DE5\u5177\u914D\u7F6E:`),I.length>0&&this.logger.info(` - \u65B0\u589E\u5DE5\u5177: ${I.join(", ")}`),te.length>0&&this.logger.info(` - \u66F4\u65B0\u5DE5\u5177: ${te.join(", ")}`),u.length>0&&this.logger.info(` - \u79FB\u9664\u5DE5\u5177: ${u.join(", ")}`)}}this.logger.debug("[MCPManager] \u5DE5\u5177\u914D\u7F6E\u540C\u6B65\u5B8C\u6210")}catch(e){this.logger.error("[MCPManager] \u540C\u6B65\u5DE5\u5177\u914D\u7F6E\u5230\u914D\u7F6E\u6587\u4EF6\u5931\u8D25:",e)}}hasToolsConfigChanged(e,t){let r=Object.keys(e),n=Object.keys(t);if(r.length!==n.length)return!0;let o=n.filter(c=>!r.includes(c)),s=r.filter(c=>!n.includes(c));if(o.length>0||s.length>0)return!0;for(let c of r){let l=e[c],u=t[c];if(l.description!==u.description)return!0}return!1}},fr=Ae});var Ke,vr=g(()=>{"use strict";C();Ke=class{static{a(this,"MCPMessageHandler")}logger;serviceManager;constructor(e){this.serviceManager=e,this.logger=h}async handleMessage(e){this.logger.debug(`\u5904\u7406 MCP \u6D88\u606F: ${e.method}`,e);try{switch(e.method){case"initialize":return await this.handleInitialize(e.params,e.id);case"tools/list":return await this.handleToolsList(e.id);case"tools/call":return await this.handleToolCall(e.params,e.id);case"ping":return await this.handlePing(e.id);default:throw new Error(`\u672A\u77E5\u7684\u65B9\u6CD5: ${e.method}`)}}catch(t){return this.logger.error(`\u5904\u7406\u6D88\u606F\u65F6\u51FA\u9519: ${e.method}`,t),this.createErrorResponse(t,e.id)}}async handleInitialize(e,t){return this.logger.info("\u5904\u7406 initialize \u8BF7\u6C42",e),{jsonrpc:"2.0",result:{serverInfo:{name:"xiaozhi-mcp-server",version:"1.0.0"},capabilities:{tools:{},logging:{}},protocolVersion:"2024-11-05"},id:t||null}}async handleToolsList(e){this.logger.info("\u5904\u7406 tools/list \u8BF7\u6C42");try{let r=this.serviceManager.getAllTools().map(n=>({name:n.name,description:n.description,inputSchema:n.inputSchema}));return this.logger.info(`\u8FD4\u56DE ${r.length} \u4E2A\u5DE5\u5177`),{jsonrpc:"2.0",result:{tools:r},id:e||null}}catch(t){throw this.logger.error("\u83B7\u53D6\u5DE5\u5177\u5217\u8868\u5931\u8D25",t),t}}async handleToolCall(e,t){this.logger.info(`\u5904\u7406 tools/call \u8BF7\u6C42: ${e.name}`,e);try{if(!e.name)throw new Error("\u5DE5\u5177\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A");let r=await this.serviceManager.callTool(e.name,e.arguments||{});return this.logger.info(`\u5DE5\u5177 ${e.name} \u8C03\u7528\u6210\u529F`),{jsonrpc:"2.0",result:{content:r.content,isError:r.isError||!1},id:t||null}}catch(r){throw this.logger.error(`\u5DE5\u5177\u8C03\u7528\u5931\u8D25: ${e.name}`,r),r}}async handlePing(e){return this.logger.debug("\u5904\u7406 ping \u8BF7\u6C42"),{jsonrpc:"2.0",result:{status:"ok",timestamp:new Date().toISOString()},id:e||null}}createErrorResponse(e,t){let r=-32603;return e.message.includes("\u672A\u627E\u5230\u5DE5\u5177")||e.message.includes("\u672A\u77E5\u7684\u65B9\u6CD5")?r=-32601:(e.message.includes("\u53C2\u6570")||e.message.includes("\u4E0D\u80FD\u4E3A\u7A7A"))&&(r=-32602),{jsonrpc:"2.0",error:{code:r,message:e.message,data:{stack:e.stack}},id:t||null}}getServiceManager(){return this.serviceManager}}});import{EventEmitter as Sr}from"events";var It,xt,qe,Cr=g(()=>{"use strict";C();Tt();Oe();vr();It=class{static{a(this,"ToolRegistry")}serviceManager;logger;constructor(e){this.serviceManager=e,this.logger=h}async initialize(){this.logger.info("\u521D\u59CB\u5316\u5DE5\u5177\u6CE8\u518C\u8868")}getAllTools(){return this.serviceManager.getAllTools().map(e=>({name:e.name,description:e.description,inputSchema:e.inputSchema,serviceName:e.serviceName,originalName:e.originalName}))}findTool(e){return this.getAllTools().find(r=>r.name===e)||null}hasTool(e){return this.findTool(e)!==null}},xt=class extends Sr{static{a(this,"ConnectionManager")}connections=new Map;logger;constructor(){super(),this.logger=h}async initialize(){this.logger.info("\u521D\u59CB\u5316\u8FDE\u63A5\u7BA1\u7406\u5668")}registerConnection(e,t,r){let n={id:e,transportName:t,state:r,connectedAt:new Date,lastActivity:new Date};this.connections.set(e,n),this.emit("connectionRegistered",n),this.logger.debug(`\u8FDE\u63A5\u5DF2\u6CE8\u518C: ${e} (${t})`)}updateConnectionState(e,t){let r=this.connections.get(e);r&&(r.state=t,r.lastActivity=new Date,this.emit("connectionStateChanged",r),this.logger.debug(`\u8FDE\u63A5\u72B6\u6001\u66F4\u65B0: ${e} -> ${t}`))}removeConnection(e){let t=this.connections.get(e);t&&(this.connections.delete(e),this.emit("connectionRemoved",t),this.logger.debug(`\u8FDE\u63A5\u5DF2\u79FB\u9664: ${e}`))}getAllConnections(){return Array.from(this.connections.values())}getActiveConnectionCount(){return Array.from(this.connections.values()).filter(e=>e.state==="connected").length}async closeAllConnections(){this.logger.info("\u5173\u95ED\u6240\u6709\u8FDE\u63A5"),this.connections.clear(),this.emit("allConnectionsClosed")}},qe=class extends Sr{static{a(this,"UnifiedMCPServer")}serviceManager;messageHandler;transportAdapters=new Map;toolRegistry;connectionManager;isRunning=!1;logger;config;constructor(e={}){super(),this.config={name:"UnifiedMCPServer",enableLogging:!0,logLevel:"info",maxConnections:100,connectionTimeout:3e4,...e},this.logger=h,this.serviceManager=new Ae,this.messageHandler=new Ke(this.serviceManager),this.toolRegistry=new It(this.serviceManager),this.connectionManager=new xt,this.setupEventListeners()}setupEventListeners(){this.connectionManager.on("connectionRegistered",e=>{this.emit("connectionRegistered",e)}),this.connectionManager.on("connectionStateChanged",e=>{this.emit("connectionStateChanged",e)}),this.connectionManager.on("connectionRemoved",e=>{this.emit("connectionRemoved",e)})}async initialize(){this.logger.info("\u521D\u59CB\u5316\u7EDF\u4E00 MCP \u670D\u52A1\u5668");try{await this.serviceManager.startAllServices(),await this.toolRegistry.initialize(),await this.connectionManager.initialize(),this.logger.info("\u7EDF\u4E00 MCP \u670D\u52A1\u5668\u521D\u59CB\u5316\u5B8C\u6210"),this.emit("initialized")}catch(e){throw this.logger.error("\u7EDF\u4E00 MCP \u670D\u52A1\u5668\u521D\u59CB\u5316\u5931\u8D25",e),e}}async registerTransport(e,t){if(this.transportAdapters.has(e))throw new Error(`\u4F20\u8F93\u9002\u914D\u5668 ${e} \u5DF2\u5B58\u5728`);this.logger.info(`\u6CE8\u518C\u4F20\u8F93\u9002\u914D\u5668: ${e}`);try{await t.initialize(),this.transportAdapters.set(e,t),this.connectionManager.registerConnection(t.getConnectionId(),e,t.getState()),this.logger.info(`\u4F20\u8F93\u9002\u914D\u5668 ${e} \u6CE8\u518C\u6210\u529F`),this.emit("transportRegistered",{name:e,adapter:t})}catch(r){throw this.logger.error(`\u6CE8\u518C\u4F20\u8F93\u9002\u914D\u5668 ${e} \u5931\u8D25`,r),r}}async start(){if(this.isRunning)throw new Error("\u670D\u52A1\u5668\u5DF2\u5728\u8FD0\u884C");this.logger.info("\u542F\u52A8\u7EDF\u4E00 MCP \u670D\u52A1\u5668");try{for(let[e,t]of this.transportAdapters)try{await t.start(),this.connectionManager.updateConnectionState(t.getConnectionId(),t.getState()),this.logger.info(`\u4F20\u8F93\u9002\u914D\u5668 ${e} \u542F\u52A8\u6210\u529F`)}catch(r){throw this.logger.error(`\u4F20\u8F93\u9002\u914D\u5668 ${e} \u542F\u52A8\u5931\u8D25`,r),r}this.isRunning=!0,this.logger.info("\u7EDF\u4E00 MCP \u670D\u52A1\u5668\u542F\u52A8\u6210\u529F"),this.emit("started")}catch(e){throw this.logger.error("\u7EDF\u4E00 MCP \u670D\u52A1\u5668\u542F\u52A8\u5931\u8D25",e),e}}async stop(){if(this.isRunning){this.logger.info("\u505C\u6B62\u7EDF\u4E00 MCP \u670D\u52A1\u5668");try{for(let[e,t]of this.transportAdapters)try{await t.stop(),this.connectionManager.updateConnectionState(t.getConnectionId(),t.getState()),this.logger.info(`\u4F20\u8F93\u9002\u914D\u5668 ${e} \u505C\u6B62\u6210\u529F`)}catch(r){this.logger.error(`\u4F20\u8F93\u9002\u914D\u5668 ${e} \u505C\u6B62\u5931\u8D25`,r)}await this.connectionManager.closeAllConnections(),await this.serviceManager.stopAllServices(),this.isRunning=!1,this.logger.info("\u7EDF\u4E00 MCP \u670D\u52A1\u5668\u505C\u6B62\u6210\u529F"),this.emit("stopped")}catch(e){throw this.logger.error("\u7EDF\u4E00 MCP \u670D\u52A1\u5668\u505C\u6B62\u5931\u8D25",e),e}}}getServiceManager(){return this.serviceManager}getToolRegistry(){return this.toolRegistry}getConnectionManager(){return this.connectionManager}getMessageHandler(){return this.messageHandler}getStatus(){return{isRunning:this.isRunning,transportCount:this.transportAdapters.size,activeConnections:this.connectionManager.getActiveConnectionCount(),toolCount:this.toolRegistry.getAllTools().length,config:this.config}}getTransportAdapters(){return new Map(this.transportAdapters)}isServerRunning(){return this.isRunning}}});async function yr(i={name:"http"}){h.info("\u521B\u5EFA HTTP \u6A21\u5F0F\u670D\u52A1\u5668");let e=new qe;await e.initialize();let t=e.getMessageHandler(),r=new Ve(t,i);return await e.registerTransport("http",r),h.info("HTTP \u6A21\u5F0F\u670D\u52A1\u5668\u521B\u5EFA\u6210\u529F"),e}var Er=g(()=>{"use strict";C();hr();pr();dr();Cr();a(yr,"createHTTPServer")});var br={};H(br,{MCPServer:()=>Ot});import{EventEmitter as Ln}from"events";var T,Ot,wr=g(()=>{"use strict";C();Be();ie();Er();T=new Pe,Ot=class extends Ln{static{a(this,"MCPServer")}unifiedServer=null;proxyMCPServer=null;port;isStarted=!1;constructor(e=3e3){super(),this.port=e}async initializeUnifiedServer(){if(!this.unifiedServer){T.info("\u521D\u59CB\u5316\u7EDF\u4E00 MCP \u670D\u52A1\u5668");try{let e={name:"http",port:this.port,host:"0.0.0.0",enableSSE:!0,enableRPC:!0};this.unifiedServer=await yr(e),this.unifiedServer.on("started",()=>this.emit("started")),this.unifiedServer.on("stopped",()=>this.emit("stopped")),this.unifiedServer.on("connectionRegistered",t=>{this.emit("connectionRegistered",t)}),T.info("\u7EDF\u4E00 MCP \u670D\u52A1\u5668\u521D\u59CB\u5316\u5B8C\u6210")}catch(e){throw T.error("\u521D\u59CB\u5316\u7EDF\u4E00 MCP \u670D\u52A1\u5668\u5931\u8D25",e),e}}}async initializeMCPClient(){try{let e=null;try{d.configExists()&&(e=d.getMcpEndpoints().find(r=>r&&!r.includes("<\u8BF7\u586B\u5199"))||null)}catch(t){T.warn("\u4ECE\u914D\u7F6E\u4E2D\u8BFB\u53D6\u5C0F\u667A\u63A5\u5165\u70B9\u5931\u8D25:",t)}e?(this.proxyMCPServer=new re(e),this.unifiedServer&&this.proxyMCPServer.setServiceManager(this.unifiedServer.getServiceManager()),await this.proxyMCPServer.connect(),T.info("\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5\u6210\u529F")):T.info("\u672A\u914D\u7F6E\u6709\u6548\u7684\u5C0F\u667A\u63A5\u5165\u70B9\uFF0C\u8DF3\u8FC7\u8FDE\u63A5")}catch(e){T.error("\u521D\u59CB\u5316\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5\u5931\u8D25:",e)}}async start(){if(this.isStarted){T.warn("\u670D\u52A1\u5668\u5DF2\u542F\u52A8");return}try{T.info("\u542F\u52A8 MCP \u670D\u52A1\u5668"),await this.initializeUnifiedServer(),this.unifiedServer&&await this.unifiedServer.start(),this.initializeMCPClient().catch(e=>{T.error("\u521D\u59CB\u5316\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5\u5931\u8D25:",e)}),this.isStarted=!0,this.emit("started"),T.info("MCP \u670D\u52A1\u5668\u542F\u52A8\u6210\u529F")}catch(e){throw T.error("\u542F\u52A8 MCP \u670D\u52A1\u5668\u5931\u8D25:",e),e}}async stop(){if(!this.isStarted){T.warn("\u670D\u52A1\u5668\u672A\u542F\u52A8");return}try{T.info("\u505C\u6B62 MCP \u670D\u52A1\u5668"),this.unifiedServer&&await this.unifiedServer.stop(),this.proxyMCPServer&&(this.proxyMCPServer.disconnect(),this.proxyMCPServer=null),this.isStarted=!1,this.emit("stopped"),T.info("MCP \u670D\u52A1\u5668\u5DF2\u505C\u6B62")}catch(e){throw T.error("\u505C\u6B62 MCP \u670D\u52A1\u5668\u5931\u8D25:",e),e}}getServiceManager(){return this.unifiedServer?.getServiceManager()||null}getMessageHandler(){return this.unifiedServer?.getMessageHandler()||null}getStatus(){return this.unifiedServer?{...this.unifiedServer.getStatus(),port:this.port,mode:"mcp-server",proxyConnected:this.proxyMCPServer!==null}:{isRunning:!1,port:this.port,mode:"mcp-server"}}isRunning(){return this.isStarted&&(this.unifiedServer?.isServerRunning()||!1)}}});import{isAbsolute as zn,resolve as _n}from"path";function Mr(i,e){Je.debug(`\u8F6C\u6362\u914D\u7F6E: ${i}`,e);try{if(!i||typeof i!="string")throw new $("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(!e||typeof e!="object")throw new $("\u914D\u7F6E\u5BF9\u8C61\u4E0D\u80FD\u4E3A\u7A7A",i);let t=Un(i,e);return Zn(t),Je.info(`\u914D\u7F6E\u8F6C\u6362\u6210\u529F: ${i} -> ${t.type}`),t}catch(t){throw Je.error(`\u914D\u7F6E\u8F6C\u6362\u5931\u8D25: ${i}`,t),t instanceof $?t:new $(`\u914D\u7F6E\u8F6C\u6362\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,i)}}function Un(i,e){if(Xn(e))return Wn(i,e);if(Kn(e))return Bn(i,e);if(qn(e))return Vn(i,e);throw new $("\u65E0\u6CD5\u8BC6\u522B\u7684\u914D\u7F6E\u7C7B\u578B",i)}function Wn(i,e){if(!e.command)throw new $("\u672C\u5730\u914D\u7F6E\u5FC5\u987B\u5305\u542B command \u5B57\u6BB5",i);let t=process.env.XIAOZHI_CONFIG_DIR||process.cwd(),r=(e.args||[]).map(n=>{if(Gn(n)){let o=_n(t,n);return Je.debug(`\u89E3\u6790\u76F8\u5BF9\u8DEF\u5F84: ${n} -> ${o}`),o}return n});return{name:i,type:"stdio",command:e.command,args:r,reconnect:{enabled:!0,maxAttempts:5,initialInterval:3e3,maxInterval:3e4,backoffStrategy:"exponential",backoffMultiplier:1.5,timeout:1e4,jitter:!0},ping:{enabled:!0,interval:3e4,timeout:5e3,maxFailures:3,startDelay:5e3},timeout:3e4}}function Bn(i,e){if(!e.url)throw new $("SSE \u914D\u7F6E\u5FC5\u987B\u5305\u542B url \u5B57\u6BB5",i);let t=Jn(e.url),r={name:i,type:t?"modelscope-sse":"sse",url:e.url,reconnect:{enabled:!0,maxAttempts:10,initialInterval:3e3,maxInterval:3e4,backoffStrategy:"exponential",backoffMultiplier:1.5,timeout:15e3,jitter:!0},ping:{enabled:!0,interval:3e4,timeout:5e3,maxFailures:3,startDelay:5e3},timeout:3e4};return t&&(r.modelScopeAuth=!0),r}function Vn(i,e){if(!e.url)throw new $("Streamable HTTP \u914D\u7F6E\u5FC5\u987B\u5305\u542B url \u5B57\u6BB5",i);return{name:i,type:"streamable-http",url:e.url,reconnect:{enabled:!0,maxAttempts:5,initialInterval:3e3,maxInterval:3e4,backoffStrategy:"exponential",backoffMultiplier:1.5,timeout:15e3,jitter:!0},ping:{enabled:!1,interval:6e4,timeout:1e4,maxFailures:3,startDelay:1e4},timeout:3e4}}function Gn(i){return zn(i)?!1:!!(i.startsWith("./")||i.startsWith("../")||/\.(js|py|ts|mjs|cjs)$/i.test(i))}function Xn(i){return"command"in i&&typeof i.command=="string"}function Kn(i){return"type"in i&&i.type==="sse"&&"url"in i}function qn(i){return"url"in i&&(!("type"in i)||i.type==="streamable-http")}function Jn(i){return i.includes("modelscope.net")||i.includes("modelscope.cn")}function Zn(i){if(!i.name||typeof i.name!="string")throw new $("\u914D\u7F6E\u5FC5\u987B\u5305\u542B\u6709\u6548\u7684 name \u5B57\u6BB5");if(!Object.values($e).includes(i.type))throw new $(`\u65E0\u6548\u7684\u4F20\u8F93\u7C7B\u578B: ${i.type}`);switch(i.type){case"stdio":if(!i.command)throw new $("STDIO \u914D\u7F6E\u5FC5\u987B\u5305\u542B command \u5B57\u6BB5");break;case"sse":case"modelscope-sse":case"streamable-http":if(!i.url)throw new $(`${i.type} \u914D\u7F6E\u5FC5\u987B\u5305\u542B url \u5B57\u6BB5`);break;default:throw new $(`\u4E0D\u652F\u6301\u7684\u4F20\u8F93\u7C7B\u578B: ${i.type}`)}}var Je,$,Pr=g(()=>{"use strict";C();Ge();Je=h.withTag("ConfigAdapter"),$=class extends Error{constructor(t,r){super(t);this.configName=r;this.name="ConfigValidationError"}static{a(this,"ConfigValidationError")}};a(Mr,"convertLegacyToNew");a(Un,"convertByConfigType");a(Wn,"convertLocalConfig");a(Bn,"convertSSEConfig");a(Vn,"convertStreamableHTTPConfig");a(Gn,"isRelativePath");a(Xn,"isLocalConfig");a(Kn,"isSSEConfig");a(qn,"isStreamableHTTPConfig");a(Jn,"isModelScopeURL");a(Zn,"validateNewConfig")});async function Qn(){return console.log("\u{1F680} \u6B63\u5728\u521D\u59CB\u5316 MCPServiceManager \u5355\u4F8B..."),new fr}async function Rr(){if(J&&N==="initialized")return J;if(B&&N==="initializing")return B;N==="failed"&&$t(),N="initializing",B=Qn();try{return J=await B,N="initialized",Se=`mcp-manager-${Date.now()}-${Math.random().toString(36).substring(2,11)}`,Ne=null,console.log(`\u2705 MCPServiceManager \u5355\u4F8B\u521D\u59CB\u5316\u6210\u529F\uFF0C\u5B9E\u4F8BID: ${Se}`),J}catch(i){throw N="failed",Ne=i,B=null,console.error("\u274C MCPServiceManager \u5355\u4F8B\u521D\u59CB\u5316\u5931\u8D25:",i.message),i}}async function Tr(){if(N==="cleanup"){console.log("\u26A0\uFE0F MCPServiceManager \u5355\u4F8B\u5DF2\u5728\u6E05\u7406\u4E2D\uFF0C\u8DF3\u8FC7\u91CD\u590D\u6E05\u7406");return}console.log("\u{1F9F9} \u6B63\u5728\u6E05\u7406 MCPServiceManager \u5355\u4F8B\u8D44\u6E90..."),N="cleanup";try{if(B){try{await(await B).stopAllServices()}catch(i){console.error("\u6E05\u7406\u521D\u59CB\u5316\u4E2D\u7684\u5B9E\u4F8B\u5931\u8D25:",i.message)}B=null}J&&(await J.stopAllServices(),J=null),N="not_initialized",Ne=null,Se=null,console.log("\u2705 MCPServiceManager \u5355\u4F8B\u8D44\u6E90\u6E05\u7406\u5B8C\u6210")}catch(i){throw console.error("\u274C MCPServiceManager \u5355\u4F8B\u6E05\u7406\u5931\u8D25:",i.message),$t(),i}}function $t(){console.log("\u{1F504} \u91CD\u7F6E MCPServiceManager \u5355\u4F8B\u72B6\u6001"),J=null,B=null,N="not_initialized",Ne=null,Se=null}function Yn(){return N==="initialized"&&J!==null}function eo(){return{state:N,initializationTime:Se?new Date:void 0,lastError:Ne||void 0,instanceId:Se||void 0}}async function to(){return console.log("\u{1F504} \u5F3A\u5236\u91CD\u65B0\u521D\u59CB\u5316 MCPServiceManager \u5355\u4F8B..."),await Tr(),Rr()}function ro(){return J}async function no(){if(N==="initialized")return!0;if(N==="initializing"&&B)try{return await B,!0}catch{return!1}return!1}var J,B,N,Ne,Se,Ce,Ir=g(()=>{"use strict";Tt();J=null,B=null,N="not_initialized",Ne=null,Se=null;a(Qn,"createInstance");a(Rr,"getInstance");a(Tr,"cleanup");a($t,"reset");a(Yn,"isInitialized");a(eo,"getStatus");a(to,"forceReinitialize");a(ro,"getCurrentInstance");a(no,"waitForInitialization");Ce={getInstance:Rr,cleanup:Tr,reset:$t,isInitialized:Yn,getStatus:eo,forceReinitialize:to,getCurrentInstance:ro,waitForInitialization:no};process.on("exit",()=>{Ce.isInitialized()&&(console.log("\u{1F504} \u8FDB\u7A0B\u9000\u51FA\uFF0C\u6B63\u5728\u6E05\u7406 MCPServiceManager \u5355\u4F8B..."),Ce.reset())});process.on("uncaughtException",async i=>{console.error("\u{1F4A5} \u672A\u6355\u83B7\u7684\u5F02\u5E38\uFF0C\u6E05\u7406 MCPServiceManager \u5355\u4F8B:",i);try{await Ce.cleanup()}catch(e){console.error("\u6E05\u7406\u8FC7\u7A0B\u4E2D\u53D1\u751F\u9519\u8BEF:",e)}});process.on("unhandledRejection",async i=>{console.error("\u{1F4A5} \u672A\u5904\u7406\u7684Promise\u62D2\u7EDD\uFF0C\u6E05\u7406 MCPServiceManager \u5355\u4F8B:",i);try{await Ce.cleanup()}catch(e){console.error("\u6E05\u7406\u8FC7\u7A0B\u4E2D\u53D1\u751F\u9519\u8BEF:",e)}})});import{EventEmitter as oo}from"events";var xr,io,Ze,Or=g(()=>{"use strict";C();Be();xr=(n=>(n.EXPONENTIAL_BACKOFF="exponential_backoff",n.LINEAR_BACKOFF="linear_backoff",n.FIXED_INTERVAL="fixed_interval",n.ADAPTIVE="adaptive",n))(xr||{}),io={healthCheckInterval:3e4,reconnectInterval:5e3,maxReconnectAttempts:10,loadBalanceStrategy:"round-robin",connectionTimeout:1e4,reconnectStrategy:"exponential_backoff",maxReconnectDelay:3e4,reconnectBackoffMultiplier:2,jitterEnabled:!0},Ze=class extends oo{static{a(this,"XiaozhiConnectionManager")}connections=new Map;connectionStates=new Map;mcpServiceManager=null;logger;isInitialized=!1;isConnecting=!1;options;healthCheckInterval=null;reconnectTimers=new Map;roundRobinIndex=0;lastSelectedEndpoint=null;performanceMetrics={connectionStartTime:0,totalConnectionTime:0,averageConnectionTime:0,connectionCount:0,memoryUsage:{initial:0,current:0,peak:0},prewarmedConnections:new Set};constructor(e){super(),this.logger=h,this.options={...io,...e},this.logger.info("[XiaozhiConnectionManager] \u5B9E\u4F8B\u5DF2\u521B\u5EFA"),this.logger.debug("[XiaozhiConnectionManager] \u914D\u7F6E\u9009\u9879:",this.options)}async initialize(e,t){if(this.isInitialized){this.logger.warn("XiaozhiConnectionManager \u5DF2\u7ECF\u521D\u59CB\u5316\uFF0C\u8DF3\u8FC7\u91CD\u590D\u521D\u59CB\u5316");return}this.logger.info(`\u5F00\u59CB\u521D\u59CB\u5316 XiaozhiConnectionManager\uFF0C\u7AEF\u70B9\u6570\u91CF: ${e.length}`);try{this.validateInitializeParams(e,t),await this.cleanup();for(let r of e)await this.createConnection(r,t);this.isInitialized=!0,this.performanceMetrics.memoryUsage.initial=process.memoryUsage().heapUsed,this.performanceMetrics.memoryUsage.current=this.performanceMetrics.memoryUsage.initial,this.performanceMetrics.memoryUsage.peak=this.performanceMetrics.memoryUsage.initial,this.logger.info(`XiaozhiConnectionManager \u521D\u59CB\u5316\u5B8C\u6210\uFF0C\u7BA1\u7406 ${this.connections.size} \u4E2A\u8FDE\u63A5`)}catch(r){throw this.logger.error("XiaozhiConnectionManager \u521D\u59CB\u5316\u5931\u8D25:",r),await this.cleanup(),r}}async connect(){if(!this.isInitialized)throw new Error("XiaozhiConnectionManager \u672A\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u8C03\u7528 initialize()");if(this.isConnecting){this.logger.warn("\u8FDE\u63A5\u64CD\u4F5C\u6B63\u5728\u8FDB\u884C\u4E2D\uFF0C\u8BF7\u7B49\u5F85\u5B8C\u6210");return}this.isConnecting=!0,this.performanceMetrics.connectionStartTime=Date.now(),this.logger.info(`\u5F00\u59CB\u8FDE\u63A5\u6240\u6709\u7AEF\u70B9\uFF0C\u603B\u6570: ${this.connections.size}`);try{let e=[];for(let[s,c]of this.connections)e.push(this.connectSingleEndpoint(s,c));let t=await Promise.allSettled(e),r=t.filter(s=>s.status==="fulfilled").length,n=t.length-r,o=Date.now()-this.performanceMetrics.connectionStartTime;if(this.performanceMetrics.totalConnectionTime+=o,this.performanceMetrics.connectionCount++,this.performanceMetrics.averageConnectionTime=this.performanceMetrics.totalConnectionTime/this.performanceMetrics.connectionCount,this.logger.info(`\u8FDE\u63A5\u5B8C\u6210 - \u6210\u529F: ${r}, \u5931\u8D25: ${n}, \u8017\u65F6: ${o}ms`),r===0)throw new Error("\u6240\u6709\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5\u5931\u8D25");this.startHealthCheck()}finally{this.isConnecting=!1}}async disconnect(){this.logger.info("\u5F00\u59CB\u65AD\u5F00\u6240\u6709\u8FDE\u63A5"),this.stopHealthCheck(),this.clearAllReconnectTimers();let e=[];for(let[t,r]of this.connections)e.push(this.disconnectSingleEndpoint(t,r));await Promise.allSettled(e),this.logger.info("\u6240\u6709\u8FDE\u63A5\u5DF2\u65AD\u5F00")}async addEndpoint(e){if(!this.isInitialized)throw new Error("XiaozhiConnectionManager \u672A\u521D\u59CB\u5316");if(this.connections.has(e)){this.logger.warn(`\u7AEF\u70B9 ${e} \u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7\u6DFB\u52A0`);return}this.logger.info(`\u52A8\u6001\u6DFB\u52A0\u7AEF\u70B9: ${e}`);try{let t=this.getCurrentTools();if(await this.createConnection(e,t),this.isAnyConnected()){let r=this.connections.get(e);await this.connectSingleEndpoint(e,r)}this.logger.info(`\u7AEF\u70B9 ${e} \u6DFB\u52A0\u6210\u529F`)}catch(t){throw this.logger.error(`\u6DFB\u52A0\u7AEF\u70B9 ${e} \u5931\u8D25:`,t),this.connections.delete(e),this.connectionStates.delete(e),t}}async removeEndpoint(e){if(!this.connections.has(e)){this.logger.warn(`\u7AEF\u70B9 ${e} \u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u79FB\u9664`);return}this.logger.info(`\u52A8\u6001\u79FB\u9664\u7AEF\u70B9: ${e}`);try{let t=this.connections.get(e);await this.disconnectSingleEndpoint(e,t),this.connections.delete(e),this.connectionStates.delete(e);let r=this.reconnectTimers.get(e);r&&(clearTimeout(r),this.reconnectTimers.delete(e)),this.logger.info(`\u7AEF\u70B9 ${e} \u79FB\u9664\u6210\u529F`)}catch(t){throw this.logger.error(`\u79FB\u9664\u7AEF\u70B9 ${e} \u5931\u8D25:`,t),t}}getHealthyConnections(){let e=[];for(let[t,r]of this.connections){let n=this.connectionStates.get(t);n?.connected&&n.healthScore>50&&e.push(r)}return e}getConnectionStatus(){return Array.from(this.connectionStates.values())}isAnyConnected(){for(let e of this.connectionStates.values())if(e.connected)return!0;return!1}setServiceManager(e){this.mcpServiceManager=e,this.logger.info("\u5DF2\u8BBE\u7F6E MCPServiceManager"),this.connections.size>0&&this.syncToolsToAllConnections()}setHealthCheckEnabled(e,t){let r=this.connectionStates.get(e);r?(r.healthCheckEnabled=t,this.logger.info(`\u7AEF\u70B9 ${e} \u5065\u5EB7\u68C0\u67E5\u5DF2${t?"\u542F\u7528":"\u7981\u7528"}`)):this.logger.warn(`\u7AEF\u70B9 ${e} \u4E0D\u5B58\u5728\uFF0C\u65E0\u6CD5\u8BBE\u7F6E\u5065\u5EB7\u68C0\u67E5\u72B6\u6001`)}getHealthCheckStats(){let e={};for(let[t,r]of this.connectionStates){let n=r.totalRequests>0?r.successfulRequests/r.totalRequests*100:0;e[t]={endpoint:t,healthScore:r.healthScore,successRate:Math.round(n*100)/100,averageResponseTime:r.responseTime||0,consecutiveFailures:r.consecutiveFailures,lastHealthCheck:r.lastHealthCheck,lastSuccessTime:r.lastSuccessTime}}return e}async triggerHealthCheck(){this.logger.info("\u624B\u52A8\u89E6\u53D1\u5065\u5EB7\u68C0\u67E5"),await this.performHealthCheck()}async triggerReconnect(e){let t=this.connectionStates.get(e);if(!t)throw new Error(`\u7AEF\u70B9 ${e} \u4E0D\u5B58\u5728`);if(t.connected){this.logger.warn(`\u7AEF\u70B9 ${e} \u5DF2\u8FDE\u63A5\uFF0C\u65E0\u9700\u91CD\u8FDE`);return}this.logger.info(`\u624B\u52A8\u89E6\u53D1\u91CD\u8FDE: ${e}`);let r=this.reconnectTimers.get(e);r&&(clearTimeout(r),this.reconnectTimers.delete(e)),await this.performReconnect(e)}stopReconnect(e){let t=this.connectionStates.get(e);if(!t){this.logger.warn(`\u7AEF\u70B9 ${e} \u4E0D\u5B58\u5728`);return}let r=this.reconnectTimers.get(e);r&&(clearTimeout(r),this.reconnectTimers.delete(e),t.isReconnecting=!1,t.nextReconnectTime=void 0,this.logger.info(`\u5DF2\u505C\u6B62\u7AEF\u70B9 ${e} \u7684\u91CD\u8FDE`))}stopAllReconnects(){this.logger.info("\u505C\u6B62\u6240\u6709\u7AEF\u70B9\u7684\u91CD\u8FDE");for(let[e]of this.reconnectTimers)this.stopReconnect(e)}getReconnectStats(){let e={};for(let[t,r]of this.connectionStates)e[t]={endpoint:t,reconnectAttempts:r.reconnectAttempts,isReconnecting:r.isReconnecting,nextReconnectTime:r.nextReconnectTime,lastReconnectAttempt:r.lastReconnectAttempt,reconnectDelay:r.reconnectDelay,errorType:r.errorType,recentReconnectHistory:r.reconnectHistory.slice(-5)};return e}validateEndpoints(e){let t=[],r=[];for(let n of e){if(!n||typeof n!="string"){r.push(n);continue}if(!n.startsWith("ws://")&&!n.startsWith("wss://")){r.push(n);continue}try{new URL(n),t.push(n)}catch{r.push(n)}}return{valid:t,invalid:r}}validateOptions(e){let t=[];if(e.healthCheckInterval!==void 0&&(typeof e.healthCheckInterval!="number"||e.healthCheckInterval<1e3)&&t.push("healthCheckInterval \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1000 \u7684\u6570\u5B57"),e.reconnectInterval!==void 0&&(typeof e.reconnectInterval!="number"||e.reconnectInterval<100)&&t.push("reconnectInterval \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 100 \u7684\u6570\u5B57"),e.maxReconnectAttempts!==void 0&&(typeof e.maxReconnectAttempts!="number"||e.maxReconnectAttempts<0)&&t.push("maxReconnectAttempts \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 0 \u7684\u6570\u5B57"),e.connectionTimeout!==void 0&&(typeof e.connectionTimeout!="number"||e.connectionTimeout<1e3)&&t.push("connectionTimeout \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1000 \u7684\u6570\u5B57"),e.maxReconnectDelay!==void 0&&(typeof e.maxReconnectDelay!="number"||e.maxReconnectDelay<1e3)&&t.push("maxReconnectDelay \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1000 \u7684\u6570\u5B57"),e.reconnectBackoffMultiplier!==void 0&&(typeof e.reconnectBackoffMultiplier!="number"||e.reconnectBackoffMultiplier<1)&&t.push("reconnectBackoffMultiplier \u5FC5\u987B\u662F\u5927\u4E8E\u7B49\u4E8E 1 \u7684\u6570\u5B57"),e.loadBalanceStrategy!==void 0){let r=["round-robin","random","health-based"];r.includes(e.loadBalanceStrategy)||t.push(`loadBalanceStrategy \u5FC5\u987B\u662F\u4EE5\u4E0B\u503C\u4E4B\u4E00: ${r.join(", ")}`)}if(e.reconnectStrategy!==void 0){let r=Object.values(xr);r.includes(e.reconnectStrategy)||t.push(`reconnectStrategy \u5FC5\u987B\u662F\u4EE5\u4E0B\u503C\u4E4B\u4E00: ${r.join(", ")}`)}return{valid:t.length===0,errors:t}}async updateEndpoints(e,t=[]){if(!this.isInitialized)throw new Error("XiaozhiConnectionManager \u672A\u521D\u59CB\u5316");this.logger.info(`\u66F4\u65B0\u7AEF\u70B9\u914D\u7F6E\uFF0C\u65B0\u7AEF\u70B9\u6570\u91CF: ${e.length}`);let{valid:r,invalid:n}=this.validateEndpoints(e);if(n.length>0&&this.logger.warn(`\u53D1\u73B0\u65E0\u6548\u7AEF\u70B9: ${n.join(", ")}`),r.length===0)throw new Error("\u6CA1\u6709\u6709\u6548\u7684\u7AEF\u70B9");let o=Array.from(this.connections.keys()),s=r.filter(u=>!o.includes(u)),c=o.filter(u=>!r.includes(u)),l=o.filter(u=>r.includes(u));this.logger.info(`\u7AEF\u70B9\u53D8\u66F4 - \u6DFB\u52A0: ${s.length}, \u79FB\u9664: ${c.length}, \u4FDD\u6301: ${l.length}`);try{for(let W of c)await this.removeEndpoint(W);for(let W of s)await this.addEndpoint(W);let u={type:s.length>0&&c.length>0?"endpoints_updated":s.length>0?"endpoints_added":"endpoints_removed",data:{added:s.length>0?s:void 0,removed:c.length>0?c:void 0,updated:s.length>0&&c.length>0?r:void 0},timestamp:new Date};this.emit("configChange",u),this.logger.info("\u7AEF\u70B9\u914D\u7F6E\u66F4\u65B0\u5B8C\u6210")}catch(u){throw this.logger.error("\u7AEF\u70B9\u914D\u7F6E\u66F4\u65B0\u5931\u8D25:",u),u}}updateOptions(e){this.logger.info("\u66F4\u65B0\u8FDE\u63A5\u9009\u9879");let{valid:t,errors:r}=this.validateOptions(e);if(!t)throw new Error(`\u65E0\u6548\u7684\u8FDE\u63A5\u9009\u9879: ${r.join(", ")}`);let n={...this.options};this.options={...this.options,...e};let o={type:"options_updated",data:{oldOptions:n,newOptions:e},timestamp:new Date};this.emit("configChange",o),this.logger.info("\u8FDE\u63A5\u9009\u9879\u66F4\u65B0\u5B8C\u6210"),this.logger.debug("\u65B0\u7684\u914D\u7F6E\u9009\u9879:",this.options)}getCurrentConfig(){return{endpoints:Array.from(this.connections.keys()),options:{...this.options}}}async reloadConfig(e){this.logger.info("\u5F00\u59CB\u70ED\u91CD\u8F7D\u914D\u7F6E");try{e.options&&this.updateOptions(e.options),e.endpoints&&await this.updateEndpoints(e.endpoints,e.tools||[]),this.logger.info("\u914D\u7F6E\u70ED\u91CD\u8F7D\u5B8C\u6210")}catch(t){throw this.logger.error("\u914D\u7F6E\u70ED\u91CD\u8F7D\u5931\u8D25:",t),t}}selectBestConnection(e=[]){let t=this.getHealthyConnections();if(t.length===0)return this.logger.warn("\u6CA1\u6709\u5065\u5EB7\u7684\u8FDE\u63A5\u53EF\u7528"),null;let r=t.filter(o=>{let s=this.getEndpointByConnection(o);return s&&!e.includes(s)});if(r.length===0)return this.logger.warn("\u6CA1\u6709\u53EF\u7528\u7684\u8FDE\u63A5\uFF08\u6392\u9664\u6307\u5B9A\u7AEF\u70B9\u540E\uFF09"),null;let n;switch(this.options.loadBalanceStrategy){case"round-robin":n=this.selectRoundRobin(r);break;case"random":n=this.selectRandom(r);break;case"health-based":n=this.selectHealthBased(r);break;default:n=this.selectRoundRobin(r)}return this.lastSelectedEndpoint=this.getEndpointByConnection(n),n}selectRoundRobin(e){if(e.length===0)throw new Error("\u6CA1\u6709\u53EF\u7528\u7684\u8FDE\u63A5");let t=e[this.roundRobinIndex%e.length];return this.roundRobinIndex=(this.roundRobinIndex+1)%e.length,t}selectRandom(e){if(e.length===0)throw new Error("\u6CA1\u6709\u53EF\u7528\u7684\u8FDE\u63A5");let t=Math.floor(Math.random()*e.length);return e[t]}selectHealthBased(e){if(e.length===0)throw new Error("\u6CA1\u6709\u53EF\u7528\u7684\u8FDE\u63A5");let t=e.map(o=>{let s=this.getEndpointByConnection(o),c=s?this.connectionStates.get(s):null;return{connection:o,endpoint:s,healthScore:c?.healthScore||0,responseTime:c?.responseTime||Number.POSITIVE_INFINITY,successRate:c&&c.totalRequests>0?c.successfulRequests/c.totalRequests*100:0}});t.sort((o,s)=>o.healthScore!==s.healthScore?s.healthScore-o.healthScore:o.successRate!==s.successRate?s.successRate-o.successRate:o.responseTime-s.responseTime);let r=t.reduce((o,s)=>o+(s.healthScore+1),0),n=Math.random()*r;for(let o of t)if(n-=o.healthScore+1,n<=0)return o.connection;return t[0].connection}getEndpointByConnection(e){for(let[t,r]of this.connections)if(r===e)return t;return null}getLoadBalanceStats(){let e=this.getHealthyConnections(),t={};for(let[r,n]of this.connectionStates){let o=n.totalRequests>0?n.successfulRequests/n.totalRequests*100:0,s=n.healthScore+1;t[r]={healthScore:n.healthScore,responseTime:n.responseTime||0,successRate:Math.round(o*100)/100,weight:s}}return{strategy:this.options.loadBalanceStrategy,totalConnections:this.connections.size,healthyConnections:e.length,lastSelectedEndpoint:this.lastSelectedEndpoint,roundRobinIndex:this.roundRobinIndex,connectionWeights:t}}setLoadBalanceStrategy(e){let t=this.options.loadBalanceStrategy;this.options.loadBalanceStrategy=e,e==="round-robin"&&(this.roundRobinIndex=0),this.logger.info(`\u8D1F\u8F7D\u5747\u8861\u7B56\u7565\u5DF2\u4ECE ${t} \u5207\u6362\u5230 ${e}`);let r={type:"options_updated",data:{oldOptions:{loadBalanceStrategy:t},newOptions:{loadBalanceStrategy:e}},timestamp:new Date};this.emit("configChange",r)}async performFailover(e){this.logger.warn(`\u6267\u884C\u6545\u969C\u8F6C\u79FB\uFF0C\u5931\u8D25\u7AEF\u70B9: ${e}`);let t=this.selectBestConnection([e]);if(!t)return this.logger.error("\u6545\u969C\u8F6C\u79FB\u5931\u8D25\uFF1A\u6CA1\u6709\u53EF\u7528\u7684\u5907\u7528\u8FDE\u63A5"),null;let r=this.getEndpointByConnection(t);return this.logger.info(`\u6545\u969C\u8F6C\u79FB\u6210\u529F\uFF0C\u5207\u6362\u5230\u7AEF\u70B9: ${r}`),t}async prewarmConnections(e=[]){let t=e.length>0?e:Array.from(this.connections.keys());this.logger.info(`\u5F00\u59CB\u9884\u70ED\u8FDE\u63A5\uFF0C\u7AEF\u70B9\u6570\u91CF: ${t.length}`);let r=t.map(async n=>{if(this.performanceMetrics.prewarmedConnections.has(n)){this.logger.debug(`\u7AEF\u70B9 ${n} \u5DF2\u9884\u70ED\uFF0C\u8DF3\u8FC7`);return}try{this.connections.get(n)&&(await this.performHealthCheck(),this.performanceMetrics.prewarmedConnections.add(n),this.logger.debug(`\u7AEF\u70B9 ${n} \u9884\u70ED\u5B8C\u6210`))}catch(o){this.logger.warn(`\u7AEF\u70B9 ${n} \u9884\u70ED\u5931\u8D25:`,o)}});await Promise.all(r),this.logger.info(`\u8FDE\u63A5\u9884\u70ED\u5B8C\u6210\uFF0C\u6210\u529F\u9884\u70ED ${this.performanceMetrics.prewarmedConnections.size} \u4E2A\u7AEF\u70B9`)}updatePerformanceMetrics(){let e=process.memoryUsage().heapUsed;this.performanceMetrics.memoryUsage.current=e,e>this.performanceMetrics.memoryUsage.peak&&(this.performanceMetrics.memoryUsage.peak=e)}getPerformanceMetrics(){this.updatePerformanceMetrics();let e=this.performanceMetrics.memoryUsage.current-this.performanceMetrics.memoryUsage.initial,t=this.performanceMetrics.memoryUsage.initial>0?e/this.performanceMetrics.memoryUsage.initial*100:0;return{connectionTime:{total:this.performanceMetrics.totalConnectionTime,average:this.performanceMetrics.averageConnectionTime,count:this.performanceMetrics.connectionCount},memoryUsage:{initial:this.performanceMetrics.memoryUsage.initial,current:this.performanceMetrics.memoryUsage.current,peak:this.performanceMetrics.memoryUsage.peak,growth:e,growthPercentage:Math.round(t*100)/100},prewarmedConnections:this.performanceMetrics.prewarmedConnections.size,totalConnections:this.connections.size,healthyConnections:this.getHealthyConnections().length}}optimizeMemoryUsage(){this.logger.info("\u5F00\u59CB\u5185\u5B58\u4F18\u5316");for(let[,e]of this.connectionStates)if(e.reconnectHistory&&e.reconnectHistory.length>10&&(e.reconnectHistory=e.reconnectHistory.slice(-10)),e.totalRequests>1e4){let t=e.successfulRequests/e.totalRequests;e.totalRequests=1e3,e.successfulRequests=Math.round(t*1e3)}global.gc&&global.gc(),this.updatePerformanceMetrics(),this.logger.info("\u5185\u5B58\u4F18\u5316\u5B8C\u6210")}async cleanup(){this.logger.info("\u5F00\u59CB\u6E05\u7406 XiaozhiConnectionManager \u8D44\u6E90");try{await this.disconnect(),this.connections.clear(),this.connectionStates.clear(),this.isInitialized=!1,this.isConnecting=!1,this.roundRobinIndex=0,this.lastSelectedEndpoint=null,this.logger.info("XiaozhiConnectionManager \u8D44\u6E90\u6E05\u7406\u5B8C\u6210")}catch(e){throw this.logger.error("XiaozhiConnectionManager \u8D44\u6E90\u6E05\u7406\u5931\u8D25:",e),e}}validateInitializeParams(e,t){if(!Array.isArray(e)||e.length===0)throw new Error("\u7AEF\u70B9\u5217\u8868\u4E0D\u80FD\u4E3A\u7A7A");if(!Array.isArray(t))throw new Error("\u5DE5\u5177\u5217\u8868\u5FC5\u987B\u662F\u6570\u7EC4");for(let r of e){if(!r||typeof r!="string")throw new Error(`\u65E0\u6548\u7684\u7AEF\u70B9\u5730\u5740: ${r}`);if(!r.startsWith("ws://")&&!r.startsWith("wss://"))throw new Error(`\u7AEF\u70B9\u5730\u5740\u5FC5\u987B\u662F WebSocket URL: ${r}`)}}async createConnection(e,t){this.logger.debug(`\u521B\u5EFA\u8FDE\u63A5\u5B9E\u4F8B: ${e}`);try{let r=new re(e);this.mcpServiceManager&&r.setServiceManager(this.mcpServiceManager),this.connections.set(e,r),this.connectionStates.set(e,{endpoint:e,connected:!1,initialized:!1,reconnectAttempts:0,healthScore:100,consecutiveFailures:0,totalRequests:0,successfulRequests:0,healthCheckEnabled:!0,isReconnecting:!1,reconnectDelay:this.options.reconnectInterval,reconnectHistory:[]}),this.logger.debug(`\u8FDE\u63A5\u5B9E\u4F8B\u521B\u5EFA\u6210\u529F: ${e}`)}catch(r){throw this.logger.error(`\u521B\u5EFA\u8FDE\u63A5\u5B9E\u4F8B\u5931\u8D25 ${e}:`,r),r}}async connectSingleEndpoint(e,t){let r=this.connectionStates.get(e);if(!r)throw new Error(`\u7AEF\u70B9\u72B6\u6001\u4E0D\u5B58\u5728: ${e}`);this.logger.debug(`\u8FDE\u63A5\u7AEF\u70B9: ${e}`);try{r.connected=!1,r.initialized=!1,await t.connect(),r.connected=!0,r.initialized=!0,r.lastConnected=new Date,r.lastError=void 0,r.reconnectAttempts=0,r.healthScore=100,this.logger.info(`\u7AEF\u70B9\u8FDE\u63A5\u6210\u529F: ${e}`)}catch(n){throw r.connected=!1,r.initialized=!1,r.lastError=n instanceof Error?n.message:String(n),r.reconnectAttempts++,r.healthScore=Math.max(0,r.healthScore-20),this.logger.error(`\u7AEF\u70B9\u8FDE\u63A5\u5931\u8D25 ${e}:`,n),this.scheduleReconnect(e),n}}async disconnectSingleEndpoint(e,t){let r=this.connectionStates.get(e);if(r){this.logger.debug(`\u65AD\u5F00\u7AEF\u70B9: ${e}`);try{t.disconnect(),r.connected=!1,r.initialized=!1,this.logger.debug(`\u7AEF\u70B9\u65AD\u5F00\u6210\u529F: ${e}`)}catch(n){this.logger.error(`\u7AEF\u70B9\u65AD\u5F00\u5931\u8D25 ${e}:`,n),r.connected=!1,r.initialized=!1}}}getCurrentTools(){if(!this.mcpServiceManager)return[];try{return this.mcpServiceManager.getAllTools()}catch(e){return this.logger.error("\u83B7\u53D6\u5DE5\u5177\u5217\u8868\u5931\u8D25:",e),[]}}syncToolsToAllConnections(){if(this.mcpServiceManager){this.logger.debug("\u540C\u6B65\u5DE5\u5177\u5230\u6240\u6709\u8FDE\u63A5");for(let[e,t]of this.connections)try{t.setServiceManager(this.mcpServiceManager),this.logger.debug(`\u5DE5\u5177\u540C\u6B65\u6210\u529F: ${e}`)}catch(r){this.logger.error(`\u5DE5\u5177\u540C\u6B65\u5931\u8D25 ${e}:`,r)}}}startHealthCheck(){this.healthCheckInterval||(this.logger.debug(`\u542F\u52A8\u5065\u5EB7\u68C0\u67E5\uFF0C\u95F4\u9694: ${this.options.healthCheckInterval}ms`),this.healthCheckInterval=setInterval(()=>{this.performHealthCheck()},this.options.healthCheckInterval))}stopHealthCheck(){this.healthCheckInterval&&(clearInterval(this.healthCheckInterval),this.healthCheckInterval=null,this.logger.debug("\u5065\u5EB7\u68C0\u67E5\u5DF2\u505C\u6B62"))}async performHealthCheck(){this.logger.debug("\u6267\u884C\u5065\u5EB7\u68C0\u67E5");let e=[];for(let[t,r]of this.connectionStates)r.healthCheckEnabled&&e.push(this.performSingleHealthCheck(t,r));await Promise.allSettled(e)}async performSingleHealthCheck(e,t){let r=Date.now();t.lastHealthCheck=new Date,t.totalRequests++;try{let n=this.connections.get(e);if(!n)throw new Error(`\u8FDE\u63A5\u5B9E\u4F8B\u4E0D\u5B58\u5728: ${e}`);await this.checkConnectionHealth(n,e);let o=Date.now()-r;this.handleHealthCheckSuccess(t,o)}catch(n){let o=Date.now()-r;this.handleHealthCheckFailure(t,n,o)}}async checkConnectionHealth(e,t){if(!e)throw new Error("\u8FDE\u63A5\u5B9E\u4F8B\u4E0D\u5B58\u5728");if(await new Promise(n=>setTimeout(n,10)),!this.connectionStates.get(t)?.connected)throw new Error("\u8FDE\u63A5\u5DF2\u65AD\u5F00")}handleHealthCheckSuccess(e,t){e.successfulRequests++,e.consecutiveFailures=0,e.responseTime=t,e.lastSuccessTime=new Date,this.updateHealthScore(e,!0,t),this.logger.debug(`\u5065\u5EB7\u68C0\u67E5\u6210\u529F: ${e.endpoint}, \u54CD\u5E94\u65F6\u95F4: ${t}ms, \u5065\u5EB7\u5EA6: ${e.healthScore}`)}handleHealthCheckFailure(e,t,r){e.consecutiveFailures++,e.responseTime=r,e.lastError=t.message,this.updateHealthScore(e,!1,r),this.logger.warn(`\u5065\u5EB7\u68C0\u67E5\u5931\u8D25: ${e.endpoint}, \u9519\u8BEF: ${t.message}, \u8FDE\u7EED\u5931\u8D25: ${e.consecutiveFailures}, \u5065\u5EB7\u5EA6: ${e.healthScore}`),e.consecutiveFailures>=3&&e.connected&&this.logger.warn(`\u7AEF\u70B9 ${e.endpoint} \u8FDE\u7EED\u5931\u8D25 ${e.consecutiveFailures} \u6B21\uFF0C\u53EF\u80FD\u9700\u8981\u91CD\u8FDE`)}updateHealthScore(e,t,r){let n=e.healthScore;if(t){let o=5;r<100?o=10:r<500?o=7:r<1e3?o=5:o=2,e.healthScore=Math.min(100,n+o)}else{let o=15;e.consecutiveFailures>=5?o=30:e.consecutiveFailures>=3&&(o=20),e.healthScore=Math.max(0,n-o)}if(e.totalRequests>0){let o=e.successfulRequests/e.totalRequests;o<.5?e.healthScore=Math.max(0,e.healthScore-10):o>.9&&(e.healthScore=Math.min(100,e.healthScore+5))}}classifyConnectionError(e){let t=e.message.toLowerCase();return t.includes("timeout")||t.includes("timed out")?"timeout_error":t.includes("network")||t.includes("connection refused")||t.includes("econnrefused")||t.includes("enotfound")?"network_error":t.includes("auth")||t.includes("unauthorized")||t.includes("forbidden")||t.includes("401")||t.includes("403")?"authentication_error":t.includes("server")||t.includes("500")||t.includes("502")||t.includes("503")||t.includes("504")?"server_error":"unknown_error"}calculateReconnectDelay(e){let t=this.options.reconnectInterval,r=this.options.maxReconnectDelay,n=this.options.reconnectBackoffMultiplier,o=e.reconnectAttempts,s;switch(this.options.reconnectStrategy){case"fixed_interval":s=t;break;case"linear_backoff":s=t*(o+1);break;case"exponential_backoff":s=t*n**o;break;case"adaptive":s=this.calculateAdaptiveDelay(e,t,n,o);break;default:s=t*n**o}if(s=Math.min(s,r),this.options.jitterEnabled){let c=s*.1*Math.random();s+=c}return Math.round(s)}calculateAdaptiveDelay(e,t,r,n){let o=t;switch(e.errorType){case"network_error":o=t*r**n;break;case"authentication_error":o=t*r**n*2;break;case"server_error":o=t*(1+n);break;case"timeout_error":o=t*(1+n*.5);break;default:o=t*r**n}if(e.reconnectHistory.length>0){let s=e.reconnectHistory.slice(-5),c=s.filter(l=>l.success).length/s.length;c<.2?o*=1.5:c>.8&&(o*=.8)}return o}shouldReconnect(e){return e.reconnectAttempts>=this.options.maxReconnectAttempts?(this.logger.warn(`\u7AEF\u70B9 ${e.endpoint} \u5DF2\u8FBE\u5230\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${this.options.maxReconnectAttempts}`),!1):e.errorType==="authentication_error"&&e.reconnectAttempts>=3?(this.logger.warn(`\u7AEF\u70B9 ${e.endpoint} \u8BA4\u8BC1\u9519\u8BEF\uFF0C\u505C\u6B62\u91CD\u8FDE`),!1):e.consecutiveFailures>=10?(this.logger.warn(`\u7AEF\u70B9 ${e.endpoint} \u8FDE\u7EED\u5931\u8D25\u6B21\u6570\u8FC7\u591A\uFF0C\u505C\u6B62\u91CD\u8FDE`),!1):!0}scheduleReconnect(e){let t=this.connectionStates.get(e);if(!t)return;if(t.lastError&&(t.errorType=this.classifyConnectionError(new Error(t.lastError))),!this.shouldReconnect(t)){t.isReconnecting=!1;return}let r=this.reconnectTimers.get(e);r&&clearTimeout(r);let n=this.calculateReconnectDelay(t);t.reconnectDelay=n,t.isReconnecting=!0,t.nextReconnectTime=new Date(Date.now()+n),this.logger.info(`\u5B89\u6392\u91CD\u8FDE ${e}\uFF0C\u5EF6\u8FDF: ${n}ms\uFF0C\u5C1D\u8BD5\u6B21\u6570: ${t.reconnectAttempts+1}\uFF0C\u9519\u8BEF\u7C7B\u578B: ${t.errorType}`);let o=setTimeout(async()=>{this.reconnectTimers.delete(e),await this.performReconnect(e)},n);this.reconnectTimers.set(e,o)}async performReconnect(e){let t=this.connectionStates.get(e),r=this.connections.get(e);if(!(!t||!r)){t.lastReconnectAttempt=new Date,t.isReconnecting=!0,this.logger.info(`\u5F00\u59CB\u91CD\u8FDE ${e}\uFF0C\u7B2C ${t.reconnectAttempts+1} \u6B21\u5C1D\u8BD5`);try{await this.connectSingleEndpoint(e,r),t.isReconnecting=!1,t.reconnectHistory.push({timestamp:new Date,success:!0,delay:t.reconnectDelay}),this.logger.info(`\u91CD\u8FDE\u6210\u529F ${e}`)}catch(n){t.isReconnecting=!1,t.reconnectHistory.push({timestamp:new Date,success:!1,error:n instanceof Error?n.message:String(n),delay:t.reconnectDelay}),this.logger.error(`\u91CD\u8FDE\u5931\u8D25 ${e}:`,n),this.scheduleReconnect(e)}t.reconnectHistory.length>20&&(t.reconnectHistory=t.reconnectHistory.slice(-20))}}clearAllReconnectTimers(){for(let[,e]of this.reconnectTimers)clearTimeout(e);this.reconnectTimers.clear()}}});async function so(i){return console.log("\u{1F680} \u6B63\u5728\u521D\u59CB\u5316 XiaozhiConnectionManager \u5355\u4F8B..."),new Ze(i)}async function $r(i){if(Z&&D==="initialized")return Z;if(_&&D==="initializing")return _;D==="failed"&&At(),D="initializing",_=so(i);try{return Z=await _,D="initialized",ye=`xiaozhi-connection-manager-${Date.now()}-${Math.random().toString(36).substring(2,11)}`,De=null,console.log(`\u2705 XiaozhiConnectionManager \u5355\u4F8B\u521D\u59CB\u5316\u6210\u529F\uFF0C\u5B9E\u4F8BID: ${ye}`),Z}catch(e){throw D="failed",De=e,_=null,console.error("\u274C XiaozhiConnectionManager \u5355\u4F8B\u521D\u59CB\u5316\u5931\u8D25:",e.message),e}}async function Ar(){if(D==="cleanup"){console.log("\u26A0\uFE0F XiaozhiConnectionManager \u5355\u4F8B\u5DF2\u5728\u6E05\u7406\u4E2D\uFF0C\u8DF3\u8FC7\u91CD\u590D\u6E05\u7406");return}console.log("\u{1F9F9} \u6B63\u5728\u6E05\u7406 XiaozhiConnectionManager \u5355\u4F8B\u8D44\u6E90..."),D="cleanup";try{if(_){try{await(await _).cleanup()}catch(i){console.error("\u6E05\u7406\u521D\u59CB\u5316\u4E2D\u7684\u5B9E\u4F8B\u5931\u8D25:",i.message)}_=null}Z&&(await Z.cleanup(),Z=null),D="not_initialized",De=null,ye=null,console.log("\u2705 XiaozhiConnectionManager \u5355\u4F8B\u8D44\u6E90\u6E05\u7406\u5B8C\u6210")}catch(i){throw console.error("\u274C XiaozhiConnectionManager \u5355\u4F8B\u6E05\u7406\u5931\u8D25:",i.message),At(),i}}function At(){console.log("\u{1F504} \u91CD\u7F6E XiaozhiConnectionManager \u5355\u4F8B\u72B6\u6001..."),_&&(_=null),Z=null,D="not_initialized",De=null,ye=null,console.log("\u2705 XiaozhiConnectionManager \u5355\u4F8B\u72B6\u6001\u5DF2\u91CD\u7F6E")}function ao(){return D==="initialized"&&Z!==null}function co(){return{state:D,initializationTime:ye?new Date:void 0,lastError:De||void 0,instanceId:ye||void 0}}async function lo(i){return console.log("\u{1F504} \u5F3A\u5236\u91CD\u65B0\u521D\u59CB\u5316 XiaozhiConnectionManager \u5355\u4F8B..."),await Ar(),$r(i)}function go(){return Z}async function ho(){if(D==="initialized")return!0;if(D==="initializing"&&_)try{return await _,!0}catch{return!1}return!1}var Z,_,D,De,ye,Ee,Nr=g(()=>{"use strict";Or();Z=null,_=null,D="not_initialized",De=null,ye=null;a(so,"createInstance");a($r,"getInstance");a(Ar,"cleanup");a(At,"reset");a(ao,"isInitialized");a(co,"getStatus");a(lo,"forceReinitialize");a(go,"getCurrentInstance");a(ho,"waitForInitialization");Ee={getInstance:$r,cleanup:Ar,reset:At,isInitialized:ao,getStatus:co,forceReinitialize:lo,getCurrentInstance:go,waitForInitialization:ho};process.on("exit",()=>{Ee.isInitialized()&&(console.log("\u{1F504} \u8FDB\u7A0B\u9000\u51FA\uFF0C\u6B63\u5728\u6E05\u7406 XiaozhiConnectionManager \u5355\u4F8B..."),Ee.reset())});process.on("uncaughtException",async i=>{console.error("\u{1F4A5} \u672A\u6355\u83B7\u7684\u5F02\u5E38\uFF0C\u6E05\u7406 XiaozhiConnectionManager \u5355\u4F8B:",i);try{await Ee.cleanup()}catch(e){console.error("\u6E05\u7406\u8FC7\u7A0B\u4E2D\u53D1\u751F\u9519\u8BEF:",e)}});process.on("unhandledRejection",async i=>{console.error("\u{1F4A5} \u672A\u5904\u7406\u7684Promise\u62D2\u7EDD\uFF0C\u6E05\u7406 XiaozhiConnectionManager \u5355\u4F8B:",i);try{await Ee.cleanup()}catch(e){console.error("\u6E05\u7406\u8FC7\u7A0B\u4E2D\u53D1\u751F\u9519\u8BEF:",e)}})});import{EventEmitter as po}from"events";function U(){return be||(be=new Nt),be}function Dr(){be&&(be.destroy(),be=null)}var Nt,be,ae=g(()=>{"use strict";C();Nt=class extends po{static{a(this,"EventBus")}logger;eventStats=new Map;maxListeners=50;constructor(){super(),this.logger=h.withTag("EventBus"),this.setMaxListeners(this.maxListeners),this.setupErrorHandling()}setupErrorHandling(){this.on("error",e=>{this.logger.error("EventBus \u5185\u90E8\u9519\u8BEF:",e)}),this.on("newListener",e=>{let t=this.listenerCount(e);t>this.maxListeners*.8&&this.logger.warn(`\u4E8B\u4EF6 ${e} \u7684\u76D1\u542C\u5668\u6570\u91CF\u8FC7\u591A: ${t}`)})}emitEvent(e,t){try{return this.updateEventStats(e),this.logger.debug(`\u53D1\u5C04\u4E8B\u4EF6: ${e}`,t),this.emit(e,t)}catch(r){return this.logger.error(`\u53D1\u5C04\u4E8B\u4EF6\u5931\u8D25: ${e}`,r),!1}}onEvent(e,t){return this.logger.debug(`\u6DFB\u52A0\u4E8B\u4EF6\u76D1\u542C\u5668: ${e}`),this.on(e,t)}onceEvent(e,t){return this.logger.debug(`\u6DFB\u52A0\u4E00\u6B21\u6027\u4E8B\u4EF6\u76D1\u542C\u5668: ${e}`),this.once(e,t)}offEvent(e,t){return this.logger.debug(`\u79FB\u9664\u4E8B\u4EF6\u76D1\u542C\u5668: ${e}`),this.off(e,t)}updateEventStats(e){let t=this.eventStats.get(e)||{count:0,lastEmitted:new Date};t.count++,t.lastEmitted=new Date,this.eventStats.set(e,t)}getEventStats(){let e={};for(let[t,r]of this.eventStats)e[t]={...r};return e}getListenerStats(){let e={};for(let t of this.eventNames())e[t]=this.listenerCount(t);return e}clearEventStats(){this.eventStats.clear(),this.logger.info("\u4E8B\u4EF6\u7EDF\u8BA1\u5DF2\u6E05\u7406")}getStatus(){return{totalEvents:this.eventStats.size,totalListeners:Object.values(this.getListenerStats()).reduce((e,t)=>e+t,0),eventStats:this.getEventStats(),listenerStats:this.getListenerStats()}}destroy(){this.removeAllListeners(),this.eventStats.clear(),this.logger.info("EventBus \u5DF2\u9500\u6BC1")}},be=null;a(U,"getEventBus");a(Dr,"destroyEventBus")});var Q,ke=g(()=>{"use strict";C();ie();ae();Q=class{static{a(this,"ConfigService")}logger;eventBus;constructor(){this.logger=h.withTag("ConfigService"),this.eventBus=U()}async getConfig(){try{let e=d.getConfig();return this.logger.debug("\u83B7\u53D6\u914D\u7F6E\u6210\u529F"),e}catch(e){throw this.logger.error("\u83B7\u53D6\u914D\u7F6E\u5931\u8D25:",e),this.eventBus.emitEvent("config:error",{error:e instanceof Error?e:new Error(String(e)),operation:"getConfig"}),e}}async updateConfig(e,t="unknown"){try{this.logger.info(`\u5F00\u59CB\u66F4\u65B0\u914D\u7F6E\uFF0C\u6765\u6E90: ${t}`),this.validateConfig(e),e.mcpEndpoint!==d.getMcpEndpoint()&&d.updateMcpEndpoint(e.mcpEndpoint);let r=d.getMcpServers();for(let[n,o]of Object.entries(e.mcpServers))JSON.stringify(r[n])!==JSON.stringify(o)&&d.updateMcpServer(n,o);for(let n of Object.keys(r))n in e.mcpServers||(d.removeMcpServer(n),d.removeServerToolsConfig(n));if(e.connection&&d.updateConnectionConfig(e.connection),e.modelscope&&d.updateModelScopeConfig(e.modelscope),e.webUI&&d.updateWebUIConfig(e.webUI),e.mcpServerConfig)for(let[n,o]of Object.entries(e.mcpServerConfig))for(let[s,c]of Object.entries(o.tools))d.setToolEnabled(n,s,c.enable);this.logger.info("\u914D\u7F6E\u66F4\u65B0\u6210\u529F"),this.eventBus.emitEvent("config:updated",{config:e,source:t})}catch(r){throw this.logger.error("\u914D\u7F6E\u66F4\u65B0\u5931\u8D25:",r),this.eventBus.emitEvent("config:error",{error:r instanceof Error?r:new Error(String(r)),operation:"updateConfig"}),r}}getMcpEndpoint(){try{return d.getMcpEndpoint()}catch(e){throw this.logger.error("\u83B7\u53D6 MCP \u7AEF\u70B9\u5931\u8D25:",e),e}}getMcpEndpoints(){try{return d.getMcpEndpoints()}catch(e){throw this.logger.error("\u83B7\u53D6 MCP \u7AEF\u70B9\u5217\u8868\u5931\u8D25:",e),e}}getMcpServers(){try{return d.getMcpServers()}catch(e){throw this.logger.error("\u83B7\u53D6 MCP \u670D\u52A1\u914D\u7F6E\u5931\u8D25:",e),e}}getConnectionConfig(){try{return d.getConnectionConfig()}catch(e){throw this.logger.error("\u83B7\u53D6\u8FDE\u63A5\u914D\u7F6E\u5931\u8D25:",e),e}}getWebUIPort(){try{return d.getWebUIPort()||9999}catch(e){return this.logger.error("\u83B7\u53D6 Web UI \u7AEF\u53E3\u5931\u8D25:",e),9999}}validateConfig(e){if(!e||typeof e!="object")throw new Error("\u914D\u7F6E\u5FC5\u987B\u662F\u6709\u6548\u7684\u5BF9\u8C61");if(!e.mcpEndpoint)throw new Error("\u914D\u7F6E\u5FC5\u987B\u5305\u542B mcpEndpoint");if(!e.mcpServers||typeof e.mcpServers!="object")throw new Error("\u914D\u7F6E\u5FC5\u987B\u5305\u542B\u6709\u6548\u7684 mcpServers")}configExists(){return d.configExists()}async reloadConfig(){try{this.logger.info("\u91CD\u65B0\u52A0\u8F7D\u914D\u7F6E"),d.reloadConfig();let e=await this.getConfig();return this.eventBus.emitEvent("config:updated",{config:e,source:"reload"}),e}catch(e){throw this.logger.error("\u91CD\u65B0\u52A0\u8F7D\u914D\u7F6E\u5931\u8D25:",e),this.eventBus.emitEvent("config:error",{error:e instanceof Error?e:new Error(String(e)),operation:"reloadConfig"}),e}}getConfigPath(){return d.getConfigPath()}}});var Qe,kr=g(()=>{"use strict";C();ke();Qe=class{static{a(this,"ConfigApiHandler")}logger;configService;constructor(){this.logger=h.withTag("ConfigApiHandler"),this.configService=new Q}createErrorResponse(e,t,r){return{error:{code:e,message:t,details:r}}}createSuccessResponse(e,t){return{success:!0,data:e,message:t}}async getConfig(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u914D\u7F6E\u8BF7\u6C42");let t=await this.configService.getConfig();return this.logger.info("\u83B7\u53D6\u914D\u7F6E\u6210\u529F"),e.json(this.createSuccessResponse(t))}catch(t){this.logger.error("\u83B7\u53D6\u914D\u7F6E\u5931\u8D25:",t);let r=this.createErrorResponse("CONFIG_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u914D\u7F6E\u5931\u8D25");return e.json(r,500)}}async updateConfig(e){try{this.logger.debug("\u5904\u7406\u66F4\u65B0\u914D\u7F6E\u8BF7\u6C42");let t=await e.req.json();if(!t||typeof t!="object"){let r=this.createErrorResponse("INVALID_REQUEST_BODY","\u8BF7\u6C42\u4F53\u5FC5\u987B\u662F\u6709\u6548\u7684\u914D\u7F6E\u5BF9\u8C61");return e.json(r,400)}return await this.configService.updateConfig(t,"http-api"),this.logger.info("\u914D\u7F6E\u66F4\u65B0\u6210\u529F"),e.json(this.createSuccessResponse(null,"\u914D\u7F6E\u66F4\u65B0\u6210\u529F"))}catch(t){this.logger.error("\u914D\u7F6E\u66F4\u65B0\u5931\u8D25:",t);let r=this.createErrorResponse("CONFIG_UPDATE_ERROR",t instanceof Error?t.message:"\u914D\u7F6E\u66F4\u65B0\u5931\u8D25");return e.json(r,400)}}async getMcpEndpoint(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6 MCP \u7AEF\u70B9\u8BF7\u6C42");let t=this.configService.getMcpEndpoint();return this.logger.debug("\u83B7\u53D6 MCP \u7AEF\u70B9\u6210\u529F"),e.json(this.createSuccessResponse({endpoint:t}))}catch(t){this.logger.error("\u83B7\u53D6 MCP \u7AEF\u70B9\u5931\u8D25:",t);let r=this.createErrorResponse("MCP_ENDPOINT_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6 MCP \u7AEF\u70B9\u5931\u8D25");return e.json(r,500)}}async getMcpEndpoints(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6 MCP \u7AEF\u70B9\u5217\u8868\u8BF7\u6C42");let t=this.configService.getMcpEndpoints();return this.logger.debug("\u83B7\u53D6 MCP \u7AEF\u70B9\u5217\u8868\u6210\u529F"),e.json(this.createSuccessResponse({endpoints:t}))}catch(t){this.logger.error("\u83B7\u53D6 MCP \u7AEF\u70B9\u5217\u8868\u5931\u8D25:",t);let r=this.createErrorResponse("MCP_ENDPOINTS_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6 MCP \u7AEF\u70B9\u5217\u8868\u5931\u8D25");return e.json(r,500)}}async getMcpServers(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6 MCP \u670D\u52A1\u914D\u7F6E\u8BF7\u6C42");let t=this.configService.getMcpServers();return this.logger.debug("\u83B7\u53D6 MCP \u670D\u52A1\u914D\u7F6E\u6210\u529F"),e.json(this.createSuccessResponse({servers:t}))}catch(t){this.logger.error("\u83B7\u53D6 MCP \u670D\u52A1\u914D\u7F6E\u5931\u8D25:",t);let r=this.createErrorResponse("MCP_SERVERS_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6 MCP \u670D\u52A1\u914D\u7F6E\u5931\u8D25");return e.json(r,500)}}async getConnectionConfig(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u8FDE\u63A5\u914D\u7F6E\u8BF7\u6C42");let t=this.configService.getConnectionConfig();return this.logger.debug("\u83B7\u53D6\u8FDE\u63A5\u914D\u7F6E\u6210\u529F"),e.json(this.createSuccessResponse({connection:t}))}catch(t){this.logger.error("\u83B7\u53D6\u8FDE\u63A5\u914D\u7F6E\u5931\u8D25:",t);let r=this.createErrorResponse("CONNECTION_CONFIG_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u8FDE\u63A5\u914D\u7F6E\u5931\u8D25");return e.json(r,500)}}async reloadConfig(e){try{this.logger.info("\u5904\u7406\u91CD\u65B0\u52A0\u8F7D\u914D\u7F6E\u8BF7\u6C42");let t=await this.configService.reloadConfig();return this.logger.info("\u91CD\u65B0\u52A0\u8F7D\u914D\u7F6E\u6210\u529F"),e.json(this.createSuccessResponse(t,"\u914D\u7F6E\u91CD\u65B0\u52A0\u8F7D\u6210\u529F"))}catch(t){this.logger.error("\u91CD\u65B0\u52A0\u8F7D\u914D\u7F6E\u5931\u8D25:",t);let r=this.createErrorResponse("CONFIG_RELOAD_ERROR",t instanceof Error?t.message:"\u91CD\u65B0\u52A0\u8F7D\u914D\u7F6E\u5931\u8D25");return e.json(r,500)}}async getConfigPath(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84\u8BF7\u6C42");let t=this.configService.getConfigPath();return this.logger.debug("\u83B7\u53D6\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84\u6210\u529F"),e.json(this.createSuccessResponse({path:t}))}catch(t){this.logger.error("\u83B7\u53D6\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84\u5931\u8D25:",t);let r=this.createErrorResponse("CONFIG_PATH_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84\u5931\u8D25");return e.json(r,500)}}async checkConfigExists(e){try{this.logger.debug("\u5904\u7406\u68C0\u67E5\u914D\u7F6E\u662F\u5426\u5B58\u5728\u8BF7\u6C42");let t=this.configService.configExists();return this.logger.debug(`\u914D\u7F6E\u5B58\u5728\u68C0\u67E5\u7ED3\u679C: ${t}`),e.json(this.createSuccessResponse({exists:t}))}catch(t){this.logger.error("\u68C0\u67E5\u914D\u7F6E\u662F\u5426\u5B58\u5728\u5931\u8D25:",t);let r=this.createErrorResponse("CONFIG_EXISTS_CHECK_ERROR",t instanceof Error?t.message:"\u68C0\u67E5\u914D\u7F6E\u662F\u5426\u5B58\u5728\u5931\u8D25");return e.json(r,500)}}}});var Ye,Hr=g(()=>{"use strict";C();ke();Ye=class{static{a(this,"HeartbeatHandler")}logger;statusService;notificationService;configService;constructor(e,t){this.logger=h.withTag("HeartbeatHandler"),this.statusService=e,this.notificationService=t,this.configService=new Q}async handleClientStatus(e,t,r){try{this.logger.debug(`\u5904\u7406\u5BA2\u6237\u7AEF\u72B6\u6001\u66F4\u65B0: ${r}`,t.data);let n={...t.data,lastHeartbeat:Date.now()};this.statusService.updateClientInfo(n,`websocket-${r}`),await this.sendLatestConfig(e,r),this.logger.debug(`\u5BA2\u6237\u7AEF\u72B6\u6001\u66F4\u65B0\u6210\u529F: ${r}`)}catch(n){this.logger.error(`\u5904\u7406\u5BA2\u6237\u7AEF\u72B6\u6001\u66F4\u65B0\u5931\u8D25: ${r}`,n),this.sendError(e,"CLIENT_STATUS_ERROR",n instanceof Error?n.message:"\u5BA2\u6237\u7AEF\u72B6\u6001\u66F4\u65B0\u5931\u8D25")}}async sendLatestConfig(e,t){try{let n={type:"configUpdate",data:await this.configService.getConfig(),timestamp:Date.now()};e.send(JSON.stringify(n)),this.logger.debug(`\u6700\u65B0\u914D\u7F6E\u5DF2\u53D1\u9001\u7ED9\u5BA2\u6237\u7AEF: ${t}`)}catch(r){this.logger.error(`\u53D1\u9001\u6700\u65B0\u914D\u7F6E\u5931\u8D25: ${t}`,r)}}sendError(e,t,r){try{let n={type:"error",error:{code:t,message:r,timestamp:Date.now()}};e.send(JSON.stringify(n))}catch(n){this.logger.error("\u53D1\u9001\u9519\u8BEF\u6D88\u606F\u5931\u8D25:",n)}}checkHeartbeatTimeout(){let e=this.statusService.getLastHeartbeat(),t=Date.now();e&&t-e>35e3&&(this.logger.warn("\u5BA2\u6237\u7AEF\u5FC3\u8DF3\u8D85\u65F6\uFF0C\u6807\u8BB0\u4E3A\u65AD\u5F00\u8FDE\u63A5"),this.statusService.updateClientInfo({status:"disconnected"},"heartbeat-timeout"))}startHeartbeatMonitoring(){return this.logger.info("\u542F\u52A8\u5FC3\u8DF3\u76D1\u63A7"),setInterval(()=>{this.checkHeartbeatTimeout(),this.cleanupDisconnectedClients()},1e4)}cleanupDisconnectedClients(){try{this.notificationService.cleanupDisconnectedClients()}catch(e){this.logger.error("\u6E05\u7406\u65AD\u5F00\u8FDE\u63A5\u7684\u5BA2\u6237\u7AEF\u5931\u8D25:",e)}}stopHeartbeatMonitoring(e){this.logger.info("\u505C\u6B62\u5FC3\u8DF3\u76D1\u63A7"),clearInterval(e)}getHeartbeatStats(){return{lastHeartbeat:this.statusService.getLastHeartbeat(),isConnected:this.statusService.isClientConnected(),clientStats:this.notificationService.getClientStats()}}handleClientConnect(e){this.logger.info(`\u5BA2\u6237\u7AEF\u8FDE\u63A5\u5EFA\u7ACB: ${e}`),this.statusService.updateClientInfo({status:"connected",lastHeartbeat:Date.now()},`websocket-connect-${e}`)}handleClientDisconnect(e){this.logger.info(`\u5BA2\u6237\u7AEF\u8FDE\u63A5\u65AD\u5F00: ${e}`),this.statusService.updateClientInfo({status:"disconnected"},`websocket-disconnect-${e}`)}sendHeartbeatResponse(e,t){try{let r={type:"heartbeatResponse",data:{timestamp:Date.now(),status:"ok"}};e.send(JSON.stringify(r)),this.logger.debug(`\u5FC3\u8DF3\u54CD\u5E94\u5DF2\u53D1\u9001: ${t}`)}catch(r){this.logger.error(`\u53D1\u9001\u5FC3\u8DF3\u54CD\u5E94\u5931\u8D25: ${t}`,r)}}validateHeartbeatMessage(e){return e&&typeof e=="object"&&e.type==="clientStatus"&&e.data&&typeof e.data=="object"}}});var et,jr=g(()=>{"use strict";C();ke();ae();et=class{static{a(this,"RealtimeNotificationHandler")}logger;notificationService;configService;statusService;eventBus;constructor(e,t){this.logger=h.withTag("RealtimeNotificationHandler"),this.notificationService=e,this.configService=new Q,this.statusService=t,this.eventBus=U()}async handleMessage(e,t,r){try{switch(this.logger.debug(`\u5904\u7406 WebSocket \u6D88\u606F: ${t.type}`,{clientId:r}),this.eventBus.emitEvent("websocket:message:received",{type:t.type,data:t.data,clientId:r}),t.type){case"getConfig":await this.handleGetConfig(e,r);break;case"updateConfig":await this.handleUpdateConfig(e,t.data,r);break;case"getStatus":await this.handleGetStatus(e,r);break;case"restartService":await this.handleRestartService(e,r);break;default:this.logger.warn(`\u672A\u77E5\u7684 WebSocket \u6D88\u606F\u7C7B\u578B: ${t.type}`,{clientId:r}),this.sendError(e,"UNKNOWN_MESSAGE_TYPE",`\u672A\u77E5\u7684\u6D88\u606F\u7C7B\u578B: ${t.type}`)}}catch(n){this.logger.error(`\u5904\u7406 WebSocket \u6D88\u606F\u5931\u8D25: ${t.type}`,n),this.sendError(e,"MESSAGE_PROCESSING_ERROR",n instanceof Error?n.message:"\u6D88\u606F\u5904\u7406\u5931\u8D25")}}async handleGetConfig(e,t){this.logDeprecationWarning("WebSocket getConfig","GET /api/config");try{let r=await this.configService.getConfig();this.logger.debug("WebSocket: getConfig \u8BF7\u6C42\u5904\u7406\u6210\u529F",{clientId:t}),e.send(JSON.stringify({type:"config",data:r}))}catch(r){this.logger.error("WebSocket: getConfig \u8BF7\u6C42\u5904\u7406\u5931\u8D25",r),this.sendError(e,"CONFIG_READ_ERROR",r instanceof Error?r.message:"\u83B7\u53D6\u914D\u7F6E\u5931\u8D25")}}async handleUpdateConfig(e,t,r){this.logDeprecationWarning("WebSocket updateConfig","PUT /api/config");try{await this.configService.updateConfig(t,`websocket-${r}`),this.logger.debug("WebSocket: updateConfig \u8BF7\u6C42\u5904\u7406\u6210\u529F",{clientId:r})}catch(n){this.logger.error("WebSocket: updateConfig \u8BF7\u6C42\u5904\u7406\u5931\u8D25",n),this.sendError(e,"CONFIG_UPDATE_ERROR",n instanceof Error?n.message:"\u914D\u7F6E\u66F4\u65B0\u5931\u8D25")}}async handleGetStatus(e,t){this.logDeprecationWarning("WebSocket getStatus","GET /api/status");try{let r=this.statusService.getFullStatus();e.send(JSON.stringify({type:"status",data:r.client})),this.logger.debug("WebSocket: getStatus \u8BF7\u6C42\u5904\u7406\u6210\u529F",{clientId:t})}catch(r){this.logger.error("WebSocket: getStatus \u8BF7\u6C42\u5904\u7406\u5931\u8D25",r),this.sendError(e,"STATUS_READ_ERROR",r instanceof Error?r.message:"\u83B7\u53D6\u72B6\u6001\u5931\u8D25")}}async handleRestartService(e,t){this.logDeprecationWarning("WebSocket restartService","POST /api/services/restart");try{this.logger.info("WebSocket: \u6536\u5230\u670D\u52A1\u91CD\u542F\u8BF7\u6C42",{clientId:t}),this.eventBus.emitEvent("service:restart:requested",{source:`websocket-${t}`}),this.statusService.updateRestartStatus("restarting")}catch(r){this.logger.error("WebSocket: \u5904\u7406\u91CD\u542F\u8BF7\u6C42\u5931\u8D25",r),this.sendError(e,"RESTART_REQUEST_ERROR",r instanceof Error?r.message:"\u5904\u7406\u91CD\u542F\u8BF7\u6C42\u5931\u8D25")}}sendError(e,t,r){try{let n={type:"error",error:{code:t,message:r,timestamp:Date.now()}};e.send(JSON.stringify(n))}catch(n){this.logger.error("\u53D1\u9001\u9519\u8BEF\u6D88\u606F\u5931\u8D25:",n)}}logDeprecationWarning(e,t){this.logger.warn(`[DEPRECATED] ${e} \u529F\u80FD\u5DF2\u5E9F\u5F03\uFF0C\u8BF7\u4F7F\u7528 ${t} \u66FF\u4EE3`)}async sendInitialData(e,t){try{this.logger.debug("\u53D1\u9001\u521D\u59CB\u6570\u636E\u7ED9\u5BA2\u6237\u7AEF",{clientId:t});let r=await this.configService.getConfig();e.send(JSON.stringify({type:"configUpdate",data:r}));let n=this.statusService.getFullStatus();e.send(JSON.stringify({type:"statusUpdate",data:n.client})),n.restart&&e.send(JSON.stringify({type:"restartStatus",data:n.restart})),this.logger.debug("\u521D\u59CB\u6570\u636E\u53D1\u9001\u5B8C\u6210",{clientId:t})}catch(r){this.logger.error("\u53D1\u9001\u521D\u59CB\u6570\u636E\u5931\u8D25:",r),this.sendError(e,"INITIAL_DATA_ERROR",r instanceof Error?r.message:"\u53D1\u9001\u521D\u59CB\u6570\u636E\u5931\u8D25")}}handleClientDisconnect(e){this.logger.info(`\u5BA2\u6237\u7AEF\u65AD\u5F00\u8FDE\u63A5: ${e}`),this.notificationService.unregisterClient(e)}handleClientConnect(e,t){this.logger.info(`\u5BA2\u6237\u7AEF\u8FDE\u63A5: ${t}`),this.notificationService.registerClient(t,e)}}});import{spawn as tt}from"child_process";var rt,Fr=g(()=>{"use strict";C();Dt();ae();rt=class{static{a(this,"ServiceApiHandler")}logger;statusService;eventBus;constructor(e){this.logger=h.withTag("ServiceApiHandler"),this.statusService=e,this.eventBus=U()}createErrorResponse(e,t,r){return{error:{code:e,message:t,details:r}}}createSuccessResponse(e,t){return{success:!0,data:e,message:t}}async restartService(e){try{return this.logger.info("\u5904\u7406\u670D\u52A1\u91CD\u542F\u8BF7\u6C42"),this.eventBus.emitEvent("service:restart:requested",{source:"http-api"}),this.statusService.updateRestartStatus("restarting"),setTimeout(async()=>{try{await this.executeRestart(),setTimeout(()=>{this.statusService.updateRestartStatus("completed")},5e3)}catch(t){this.logger.error("\u670D\u52A1\u91CD\u542F\u5931\u8D25:",t),this.statusService.updateRestartStatus("failed",t instanceof Error?t.message:"\u672A\u77E5\u9519\u8BEF")}},500),e.json(this.createSuccessResponse(null,"\u91CD\u542F\u8BF7\u6C42\u5DF2\u63A5\u6536"))}catch(t){this.logger.error("\u5904\u7406\u91CD\u542F\u8BF7\u6C42\u5931\u8D25:",t);let r=this.createErrorResponse("RESTART_REQUEST_ERROR",t instanceof Error?t.message:"\u5904\u7406\u91CD\u542F\u8BF7\u6C42\u5931\u8D25");return e.json(r,500)}}async executeRestart(){this.logger.info("\u6B63\u5728\u91CD\u542F MCP \u670D\u52A1...");try{let r=await(await ce()).get("serviceManager").getStatus();if(!r.running){this.logger.warn("MCP \u670D\u52A1\u672A\u8FD0\u884C\uFF0C\u5C1D\u8BD5\u542F\u52A8\u670D\u52A1"),tt("xiaozhi",["start","--daemon"],{detached:!0,stdio:"ignore",env:{...process.env,XIAOZHI_CONFIG_DIR:process.env.XIAOZHI_CONFIG_DIR||process.cwd()}}).unref(),this.logger.info("MCP \u670D\u52A1\u542F\u52A8\u547D\u4EE4\u5DF2\u53D1\u9001");return}let n=r.mode==="daemon",o=["restart"];n&&o.push("--daemon"),tt("xiaozhi",o,{detached:!0,stdio:"ignore",env:{...process.env,XIAOZHI_CONFIG_DIR:process.env.XIAOZHI_CONFIG_DIR||process.cwd()}}).unref(),this.logger.info("MCP \u670D\u52A1\u91CD\u542F\u547D\u4EE4\u5DF2\u53D1\u9001")}catch(e){throw this.logger.error("\u91CD\u542F\u670D\u52A1\u5931\u8D25:",e),e}}async stopService(e){try{return this.logger.info("\u5904\u7406\u670D\u52A1\u505C\u6B62\u8BF7\u6C42"),tt("xiaozhi",["stop"],{detached:!0,stdio:"ignore",env:{...process.env,XIAOZHI_CONFIG_DIR:process.env.XIAOZHI_CONFIG_DIR||process.cwd()}}).unref(),this.logger.info("MCP \u670D\u52A1\u505C\u6B62\u547D\u4EE4\u5DF2\u53D1\u9001"),e.json(this.createSuccessResponse(null,"\u505C\u6B62\u8BF7\u6C42\u5DF2\u63A5\u6536"))}catch(t){this.logger.error("\u5904\u7406\u505C\u6B62\u8BF7\u6C42\u5931\u8D25:",t);let r=this.createErrorResponse("STOP_REQUEST_ERROR",t instanceof Error?t.message:"\u5904\u7406\u505C\u6B62\u8BF7\u6C42\u5931\u8D25");return e.json(r,500)}}async startService(e){try{return this.logger.info("\u5904\u7406\u670D\u52A1\u542F\u52A8\u8BF7\u6C42"),tt("xiaozhi",["start","--daemon"],{detached:!0,stdio:"ignore",env:{...process.env,XIAOZHI_CONFIG_DIR:process.env.XIAOZHI_CONFIG_DIR||process.cwd()}}).unref(),this.logger.info("MCP \u670D\u52A1\u542F\u52A8\u547D\u4EE4\u5DF2\u53D1\u9001"),e.json(this.createSuccessResponse(null,"\u542F\u52A8\u8BF7\u6C42\u5DF2\u63A5\u6536"))}catch(t){this.logger.error("\u5904\u7406\u542F\u52A8\u8BF7\u6C42\u5931\u8D25:",t);let r=this.createErrorResponse("START_REQUEST_ERROR",t instanceof Error?t.message:"\u5904\u7406\u542F\u52A8\u8BF7\u6C42\u5931\u8D25");return e.json(r,500)}}async getServiceStatus(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u670D\u52A1\u72B6\u6001\u8BF7\u6C42");let n=await(await ce()).get("serviceManager").getStatus();return this.logger.debug("\u83B7\u53D6\u670D\u52A1\u72B6\u6001\u6210\u529F"),e.json(this.createSuccessResponse(n))}catch(t){this.logger.error("\u83B7\u53D6\u670D\u52A1\u72B6\u6001\u5931\u8D25:",t);let r=this.createErrorResponse("SERVICE_STATUS_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u670D\u52A1\u72B6\u6001\u5931\u8D25");return e.json(r,500)}}async getServiceHealth(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u670D\u52A1\u5065\u5EB7\u72B6\u6001\u8BF7\u6C42");let t={status:"healthy",timestamp:Date.now(),uptime:process.uptime(),memory:process.memoryUsage(),version:process.version};return this.logger.debug("\u83B7\u53D6\u670D\u52A1\u5065\u5EB7\u72B6\u6001\u6210\u529F"),e.json(this.createSuccessResponse(t))}catch(t){this.logger.error("\u83B7\u53D6\u670D\u52A1\u5065\u5EB7\u72B6\u6001\u5931\u8D25:",t);let r=this.createErrorResponse("SERVICE_HEALTH_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u670D\u52A1\u5065\u5EB7\u72B6\u6001\u5931\u8D25");return e.json(r,500)}}}});import{existsSync as nt}from"fs";import{readFile as uo}from"fs/promises";import{dirname as mo,join as ne}from"path";import{fileURLToPath as fo}from"url";var ot,Lr=g(()=>{"use strict";C();ot=class{static{a(this,"StaticFileHandler")}logger;webPath=null;constructor(){this.logger=h.withTag("StaticFileHandler"),this.initializeWebPath()}initializeWebPath(){try{let e=mo(fo(import.meta.url));this.logger.debug(`\u5F53\u524D\u6587\u4EF6\u76EE\u5F55: ${e}`);let t=[ne(e,"..","..","web","dist"),ne(e,"..","web","dist"),ne(e,"..","..","web"),ne(e,"..","web"),ne(e,"..","..","..","web","dist"),ne(e,"..","..","..","web")];this.webPath=t.find(r=>{let n=nt(r);return this.logger.debug(`\u68C0\u67E5\u8DEF\u5F84 ${r}: ${n?"\u5B58\u5728":"\u4E0D\u5B58\u5728"}`),n})||null,this.webPath?this.logger.info(`\u9759\u6001\u6587\u4EF6\u670D\u52A1\u8DEF\u5F84: ${this.webPath}`):(this.logger.warn("\u672A\u627E\u5230\u9759\u6001\u6587\u4EF6\u76EE\u5F55"),this.logger.debug("\u5C1D\u8BD5\u7684\u8DEF\u5F84:",t))}catch(e){this.logger.error("\u521D\u59CB\u5316\u9759\u6001\u6587\u4EF6\u8DEF\u5F84\u5931\u8D25:",e)}}async handleStaticFile(e){let t=new URL(e.req.url).pathname;try{if(this.logger.debug(`\u5904\u7406\u9759\u6001\u6587\u4EF6\u8BF7\u6C42: ${t}`),!this.webPath)return this.createErrorPage(e,"\u627E\u4E0D\u5230\u524D\u7AEF\u8D44\u6E90\u6587\u4EF6");let r=t;if(r==="/"&&(r="/index.html"),r.includes(".."))return this.logger.warn(`\u8DEF\u5F84\u904D\u5386\u653B\u51FB\u5C1D\u8BD5: ${r}`),e.text("Forbidden",403);let n=ne(this.webPath,r);if(!nt(n)){let s=ne(this.webPath,"index.html");return nt(s)?(this.logger.debug(`SPA \u56DE\u9000\u5230 index.html: ${t}`),this.serveFile(e,s,"text/html")):(this.logger.debug(`\u6587\u4EF6\u4E0D\u5B58\u5728: ${n}`),e.text("Not Found",404))}let o=this.getContentType(n);return this.logger.debug(`\u670D\u52A1\u9759\u6001\u6587\u4EF6: ${n}, Content-Type: ${o}`),this.serveFile(e,n,o)}catch(r){return this.logger.error(`\u670D\u52A1\u9759\u6001\u6587\u4EF6\u9519\u8BEF (${t}):`,r),e.text("Internal Server Error",500)}}async serveFile(e,t,r){try{let n=await uo(t);return r.startsWith("text/")||r.includes("javascript")||r.includes("json")?e.text(n.toString(),200,{"Content-Type":r}):e.body(n,200,{"Content-Type":r})}catch(n){throw this.logger.error(`\u8BFB\u53D6\u6587\u4EF6\u5931\u8D25: ${t}`,n),n}}getContentType(e){let t=e.split(".").pop()?.toLowerCase();return{html:"text/html",htm:"text/html",js:"application/javascript",mjs:"application/javascript",css:"text/css",json:"application/json",png:"image/png",jpg:"image/jpeg",jpeg:"image/jpeg",gif:"image/gif",svg:"image/svg+xml",ico:"image/x-icon",woff:"font/woff",woff2:"font/woff2",ttf:"font/ttf",eot:"application/vnd.ms-fontobject",pdf:"application/pdf",txt:"text/plain",xml:"application/xml",zip:"application/zip",tar:"application/x-tar",gz:"application/gzip"}[t||""]||"application/octet-stream"}createErrorPage(e,t){let r=`
|
|
16
|
+
<!DOCTYPE html>
|
|
17
|
+
<html>
|
|
18
|
+
<head>
|
|
19
|
+
<title>\u5C0F\u667A\u914D\u7F6E\u7BA1\u7406</title>
|
|
20
|
+
<meta charset="utf-8">
|
|
21
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
22
|
+
<style>
|
|
23
|
+
body {
|
|
24
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
25
|
+
max-width: 800px;
|
|
26
|
+
margin: 50px auto;
|
|
27
|
+
padding: 20px;
|
|
28
|
+
line-height: 1.6;
|
|
29
|
+
color: #333;
|
|
30
|
+
}
|
|
31
|
+
.error {
|
|
32
|
+
color: #e53e3e;
|
|
33
|
+
background: #fed7d7;
|
|
34
|
+
padding: 20px;
|
|
35
|
+
border-radius: 8px;
|
|
36
|
+
border-left: 4px solid #e53e3e;
|
|
37
|
+
}
|
|
38
|
+
.info {
|
|
39
|
+
background: #e6f3ff;
|
|
40
|
+
padding: 20px;
|
|
41
|
+
border-radius: 8px;
|
|
42
|
+
border-left: 4px solid #0066cc;
|
|
43
|
+
margin-top: 20px;
|
|
44
|
+
}
|
|
45
|
+
pre {
|
|
46
|
+
background: #f5f5f5;
|
|
47
|
+
padding: 10px;
|
|
48
|
+
border-radius: 4px;
|
|
49
|
+
overflow-x: auto;
|
|
50
|
+
}
|
|
51
|
+
h1 {
|
|
52
|
+
color: #2d3748;
|
|
53
|
+
margin-bottom: 20px;
|
|
54
|
+
}
|
|
55
|
+
</style>
|
|
56
|
+
</head>
|
|
57
|
+
<body>
|
|
58
|
+
<h1>\u5C0F\u667A\u914D\u7F6E\u7BA1\u7406</h1>
|
|
59
|
+
<div class="error">
|
|
60
|
+
<p><strong>\u9519\u8BEF\uFF1A</strong>${t}</p>
|
|
61
|
+
</div>
|
|
62
|
+
<div class="info">
|
|
63
|
+
<p><strong>\u89E3\u51B3\u65B9\u6848\uFF1A</strong></p>
|
|
64
|
+
<p>\u8BF7\u5148\u6784\u5EFA\u524D\u7AEF\u9879\u76EE\uFF1A</p>
|
|
65
|
+
<pre>cd web && pnpm install && pnpm build</pre>
|
|
66
|
+
<p>\u7136\u540E\u91CD\u65B0\u542F\u52A8\u670D\u52A1\u5668\u3002</p>
|
|
67
|
+
</div>
|
|
68
|
+
</body>
|
|
69
|
+
</html>
|
|
70
|
+
`;return e.html(r)}isWebPathAvailable(){return this.webPath!==null&&nt(this.webPath)}getWebPath(){return this.webPath}reinitializeWebPath(){this.initializeWebPath()}}});var it,zr=g(()=>{"use strict";C();it=class{static{a(this,"StatusApiHandler")}logger;statusService;constructor(e){this.logger=h.withTag("StatusApiHandler"),this.statusService=e}createErrorResponse(e,t,r){return{error:{code:e,message:t,details:r}}}createSuccessResponse(e,t){return{success:!0,data:e,message:t}}async getStatus(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u72B6\u6001\u8BF7\u6C42");let t=this.statusService.getFullStatus();return this.logger.debug("\u83B7\u53D6\u72B6\u6001\u6210\u529F"),e.json(this.createSuccessResponse(t))}catch(t){this.logger.error("\u83B7\u53D6\u72B6\u6001\u5931\u8D25:",t);let r=this.createErrorResponse("STATUS_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u72B6\u6001\u5931\u8D25");return e.json(r,500)}}async getClientStatus(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u5BA2\u6237\u7AEF\u72B6\u6001\u8BF7\u6C42");let t=this.statusService.getClientStatus();return this.logger.debug("\u83B7\u53D6\u5BA2\u6237\u7AEF\u72B6\u6001\u6210\u529F"),e.json(this.createSuccessResponse(t))}catch(t){this.logger.error("\u83B7\u53D6\u5BA2\u6237\u7AEF\u72B6\u6001\u5931\u8D25:",t);let r=this.createErrorResponse("CLIENT_STATUS_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u5BA2\u6237\u7AEF\u72B6\u6001\u5931\u8D25");return e.json(r,500)}}async getRestartStatus(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u91CD\u542F\u72B6\u6001\u8BF7\u6C42");let t=this.statusService.getRestartStatus();return this.logger.debug("\u83B7\u53D6\u91CD\u542F\u72B6\u6001\u6210\u529F"),e.json(this.createSuccessResponse(t))}catch(t){this.logger.error("\u83B7\u53D6\u91CD\u542F\u72B6\u6001\u5931\u8D25:",t);let r=this.createErrorResponse("RESTART_STATUS_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u91CD\u542F\u72B6\u6001\u5931\u8D25");return e.json(r,500)}}async checkClientConnected(e){try{this.logger.debug("\u5904\u7406\u68C0\u67E5\u5BA2\u6237\u7AEF\u8FDE\u63A5\u8BF7\u6C42");let t=this.statusService.isClientConnected();return this.logger.debug(`\u5BA2\u6237\u7AEF\u8FDE\u63A5\u72B6\u6001: ${t}`),e.json(this.createSuccessResponse({connected:t}))}catch(t){this.logger.error("\u68C0\u67E5\u5BA2\u6237\u7AEF\u8FDE\u63A5\u5931\u8D25:",t);let r=this.createErrorResponse("CLIENT_CONNECTION_CHECK_ERROR",t instanceof Error?t.message:"\u68C0\u67E5\u5BA2\u6237\u7AEF\u8FDE\u63A5\u5931\u8D25");return e.json(r,500)}}async getLastHeartbeat(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u6700\u540E\u5FC3\u8DF3\u65F6\u95F4\u8BF7\u6C42");let t=this.statusService.getLastHeartbeat();return this.logger.debug("\u83B7\u53D6\u6700\u540E\u5FC3\u8DF3\u65F6\u95F4\u6210\u529F"),e.json(this.createSuccessResponse({lastHeartbeat:t}))}catch(t){this.logger.error("\u83B7\u53D6\u6700\u540E\u5FC3\u8DF3\u65F6\u95F4\u5931\u8D25:",t);let r=this.createErrorResponse("HEARTBEAT_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u6700\u540E\u5FC3\u8DF3\u65F6\u95F4\u5931\u8D25");return e.json(r,500)}}async getActiveMCPServers(e){try{this.logger.debug("\u5904\u7406\u83B7\u53D6\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u8BF7\u6C42");let t=this.statusService.getActiveMCPServers();return this.logger.debug("\u83B7\u53D6\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u6210\u529F"),e.json(this.createSuccessResponse({servers:t}))}catch(t){this.logger.error("\u83B7\u53D6\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u5931\u8D25:",t);let r=this.createErrorResponse("ACTIVE_MCP_SERVERS_READ_ERROR",t instanceof Error?t.message:"\u83B7\u53D6\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u5931\u8D25");return e.json(r,500)}}async updateClientStatus(e){try{this.logger.debug("\u5904\u7406\u66F4\u65B0\u5BA2\u6237\u7AEF\u72B6\u6001\u8BF7\u6C42");let t=await e.req.json();if(!t||typeof t!="object"){let r=this.createErrorResponse("INVALID_REQUEST_BODY","\u8BF7\u6C42\u4F53\u5FC5\u987B\u662F\u6709\u6548\u7684\u72B6\u6001\u5BF9\u8C61");return e.json(r,400)}return this.statusService.updateClientInfo(t,"http-api"),this.logger.info("\u5BA2\u6237\u7AEF\u72B6\u6001\u66F4\u65B0\u6210\u529F"),e.json(this.createSuccessResponse(null,"\u5BA2\u6237\u7AEF\u72B6\u6001\u66F4\u65B0\u6210\u529F"))}catch(t){this.logger.error("\u66F4\u65B0\u5BA2\u6237\u7AEF\u72B6\u6001\u5931\u8D25:",t);let r=this.createErrorResponse("CLIENT_STATUS_UPDATE_ERROR",t instanceof Error?t.message:"\u66F4\u65B0\u5BA2\u6237\u7AEF\u72B6\u6001\u5931\u8D25");return e.json(r,400)}}async setActiveMCPServers(e){try{this.logger.debug("\u5904\u7406\u8BBE\u7F6E\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u8BF7\u6C42");let{servers:t}=await e.req.json();if(!Array.isArray(t)){let r=this.createErrorResponse("INVALID_REQUEST_BODY","servers \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4");return e.json(r,400)}return this.statusService.setActiveMCPServers(t),this.logger.info("\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u8BBE\u7F6E\u6210\u529F"),e.json(this.createSuccessResponse(null,"\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u8BBE\u7F6E\u6210\u529F"))}catch(t){this.logger.error("\u8BBE\u7F6E\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u5931\u8D25:",t);let r=this.createErrorResponse("ACTIVE_MCP_SERVERS_UPDATE_ERROR",t instanceof Error?t.message:"\u8BBE\u7F6E\u6D3B\u8DC3 MCP \u670D\u52A1\u5668\u5931\u8D25");return e.json(r,400)}}async resetStatus(e){try{return this.logger.info("\u5904\u7406\u91CD\u7F6E\u72B6\u6001\u8BF7\u6C42"),this.statusService.reset(),this.logger.info("\u72B6\u6001\u91CD\u7F6E\u6210\u529F"),e.json(this.createSuccessResponse(null,"\u72B6\u6001\u91CD\u7F6E\u6210\u529F"))}catch(t){this.logger.error("\u91CD\u7F6E\u72B6\u6001\u5931\u8D25:",t);let r=this.createErrorResponse("STATUS_RESET_ERROR",t instanceof Error?t.message:"\u91CD\u7F6E\u72B6\u6001\u5931\u8D25");return e.json(r,500)}}}});var st,_r=g(()=>{"use strict";C();ae();st=class{static{a(this,"NotificationService")}logger;eventBus;clients=new Map;messageQueue=new Map;maxQueueSize=100;constructor(){this.logger=h.withTag("NotificationService"),this.eventBus=U(),this.setupEventListeners()}setupEventListeners(){this.eventBus.onEvent("config:updated",e=>{this.broadcastConfigUpdate(e.config)}),this.eventBus.onEvent("status:updated",e=>{this.broadcastStatusUpdate(e.status)}),this.eventBus.onEvent("service:restart:started",e=>{this.broadcastRestartStatus("restarting",void 0,e.timestamp)}),this.eventBus.onEvent("service:restart:completed",e=>{this.broadcastRestartStatus("completed",void 0,e.timestamp)}),this.eventBus.onEvent("service:restart:failed",e=>{this.broadcastRestartStatus("failed",e.error.message,e.timestamp)}),this.eventBus.onEvent("notification:broadcast",e=>{e.target?this.sendToClient(e.target,e.type,e.data):this.broadcast(e.type,e.data)})}registerClient(e,t){try{let r={id:e,ws:t,readyState:t.readyState,send:a(n=>{t.readyState===1&&t.send(n)},"send")};this.clients.set(e,r),this.logger.info(`WebSocket \u5BA2\u6237\u7AEF\u5DF2\u6CE8\u518C: ${e}`),this.logger.debug(`\u5F53\u524D\u5BA2\u6237\u7AEF\u6570\u91CF: ${this.clients.size}`),this.sendQueuedMessages(e),this.eventBus.emitEvent("websocket:client:connected",{clientId:e,timestamp:Date.now()})}catch(r){this.logger.error(`\u6CE8\u518C\u5BA2\u6237\u7AEF\u5931\u8D25: ${e}`,r),this.eventBus.emitEvent("notification:error",{error:r instanceof Error?r:new Error(String(r)),type:"client:register"})}}unregisterClient(e){try{this.clients.has(e)&&(this.clients.delete(e),this.messageQueue.delete(e),this.logger.info(`WebSocket \u5BA2\u6237\u7AEF\u5DF2\u6CE8\u9500: ${e}`),this.logger.debug(`\u5269\u4F59\u5BA2\u6237\u7AEF\u6570\u91CF: ${this.clients.size}`),this.eventBus.emitEvent("websocket:client:disconnected",{clientId:e,timestamp:Date.now()}))}catch(t){this.logger.error(`\u6CE8\u9500\u5BA2\u6237\u7AEF\u5931\u8D25: ${e}`,t)}}broadcast(e,t){let r={type:e,data:t,timestamp:Date.now()};this.logger.debug(`\u5E7F\u64AD\u6D88\u606F: ${e}`,{clientCount:this.clients.size});for(let[n,o]of this.clients)this.sendMessageToClient(o,r,n)}sendToClient(e,t,r){let n={type:t,data:r,timestamp:Date.now()},o=this.clients.get(e);o?this.sendMessageToClient(o,n,e):this.queueMessage(e,n)}sendMessageToClient(e,t,r){try{if(e.ws.readyState===1){let n=JSON.stringify(t);e.send(n),this.logger.debug(`\u6D88\u606F\u5DF2\u53D1\u9001\u7ED9\u5BA2\u6237\u7AEF ${r}: ${t.type}`)}else this.queueMessage(r,t),this.logger.warn(`\u5BA2\u6237\u7AEF ${r} \u8FDE\u63A5\u4E0D\u53EF\u7528\uFF0C\u6D88\u606F\u5DF2\u52A0\u5165\u961F\u5217`)}catch(n){this.logger.error(`\u53D1\u9001\u6D88\u606F\u7ED9\u5BA2\u6237\u7AEF ${r} \u5931\u8D25:`,n),this.queueMessage(r,t),this.eventBus.emitEvent("notification:error",{error:n instanceof Error?n:new Error(String(n)),type:"message:send"})}}queueMessage(e,t){this.messageQueue.has(e)||this.messageQueue.set(e,[]);let r=this.messageQueue.get(e);r.push(t),r.length>this.maxQueueSize&&(r.shift(),this.logger.warn(`\u5BA2\u6237\u7AEF ${e} \u6D88\u606F\u961F\u5217\u5DF2\u6EE1\uFF0C\u79FB\u9664\u6700\u65E7\u6D88\u606F`))}sendQueuedMessages(e){let t=this.messageQueue.get(e);if(!t||t.length===0)return;let r=this.clients.get(e);if(r){this.logger.info(`\u53D1\u9001 ${t.length} \u6761\u6392\u961F\u6D88\u606F\u7ED9\u5BA2\u6237\u7AEF ${e}`);for(let n of t)this.sendMessageToClient(r,n,e);this.messageQueue.delete(e)}}broadcastConfigUpdate(e){this.broadcast("configUpdate",e)}broadcastStatusUpdate(e){this.broadcast("statusUpdate",e)}broadcastRestartStatus(e,t,r){let n={status:e,error:t,timestamp:r||Date.now()};this.broadcast("restartStatus",n)}getClientStats(){let e=Array.from(this.clients.values()).filter(r=>r.ws.readyState===1).length,t=Array.from(this.messageQueue.values()).reduce((r,n)=>r+n.length,0);return{totalClients:this.clients.size,connectedClients:e,queuedMessages:t}}cleanupDisconnectedClients(){let e=[];for(let[t,r]of this.clients)r.ws.readyState!==1&&e.push(t);for(let t of e)this.unregisterClient(t);e.length>0&&this.logger.info(`\u6E05\u7406\u4E86 ${e.length} \u4E2A\u65AD\u5F00\u7684\u5BA2\u6237\u7AEF`)}destroy(){this.logger.info("\u9500\u6BC1\u901A\u77E5\u670D\u52A1"),this.clients.clear(),this.messageQueue.clear()}}});var at,Ur=g(()=>{"use strict";C();ae();at=class{static{a(this,"StatusService")}logger;eventBus;clientInfo={status:"disconnected",mcpEndpoint:"",activeMCPServers:[]};restartStatus;heartbeatTimeout;HEARTBEAT_TIMEOUT=35e3;constructor(){this.logger=h.withTag("StatusService"),this.eventBus=U()}getClientStatus(){return{...this.clientInfo}}updateClientInfo(e,t="unknown"){try{let r={...this.clientInfo};this.clientInfo={...this.clientInfo,...e},e.lastHeartbeat&&(this.clientInfo.lastHeartbeat=Date.now()),e.status==="connected"&&this.resetHeartbeatTimeout(),this.logger.debug(`\u5BA2\u6237\u7AEF\u72B6\u6001\u66F4\u65B0\uFF0C\u6765\u6E90: ${t}`,{old:r,new:this.clientInfo}),this.eventBus.emitEvent("status:updated",{status:this.clientInfo,source:t})}catch(r){this.logger.error("\u66F4\u65B0\u5BA2\u6237\u7AEF\u72B6\u6001\u5931\u8D25:",r),this.eventBus.emitEvent("status:error",{error:r instanceof Error?r:new Error(String(r)),operation:"updateClientInfo"})}}getRestartStatus(){return this.restartStatus?{...this.restartStatus}:void 0}updateRestartStatus(e,t){try{switch(this.restartStatus={status:e,error:t,timestamp:Date.now()},this.logger.info(`\u91CD\u542F\u72B6\u6001\u66F4\u65B0: ${e}`,{error:t}),e){case"restarting":this.eventBus.emitEvent("service:restart:started",{timestamp:this.restartStatus.timestamp});break;case"completed":this.eventBus.emitEvent("service:restart:completed",{timestamp:this.restartStatus.timestamp});break;case"failed":this.eventBus.emitEvent("service:restart:failed",{error:new Error(t||"\u91CD\u542F\u5931\u8D25"),timestamp:this.restartStatus.timestamp});break}}catch(r){this.logger.error("\u66F4\u65B0\u91CD\u542F\u72B6\u6001\u5931\u8D25:",r),this.eventBus.emitEvent("status:error",{error:r instanceof Error?r:new Error(String(r)),operation:"updateRestartStatus"})}}getFullStatus(){return{client:this.getClientStatus(),restart:this.getRestartStatus(),timestamp:Date.now()}}resetHeartbeatTimeout(){this.heartbeatTimeout&&clearTimeout(this.heartbeatTimeout),this.heartbeatTimeout=setTimeout(()=>{this.logger.warn("\u5BA2\u6237\u7AEF\u5FC3\u8DF3\u8D85\u65F6\uFF0C\u6807\u8BB0\u4E3A\u65AD\u5F00\u8FDE\u63A5"),this.updateClientInfo({status:"disconnected"},"heartbeat-timeout")},this.HEARTBEAT_TIMEOUT)}clearHeartbeatTimeout(){this.heartbeatTimeout&&(clearTimeout(this.heartbeatTimeout),this.heartbeatTimeout=void 0)}isClientConnected(){return this.clientInfo.status==="connected"}getLastHeartbeat(){return this.clientInfo.lastHeartbeat}getActiveMCPServers(){return[...this.clientInfo.activeMCPServers]}setActiveMCPServers(e){this.updateClientInfo({activeMCPServers:[...e]},"mcp-servers-update")}setMcpEndpoint(e){this.updateClientInfo({mcpEndpoint:e},"mcp-endpoint-update")}reset(){this.logger.info("\u91CD\u7F6E\u72B6\u6001\u670D\u52A1"),this.clearHeartbeatTimeout(),this.clientInfo={status:"disconnected",mcpEndpoint:"",activeMCPServers:[]},this.restartStatus=void 0}destroy(){this.logger.info("\u9500\u6BC1\u72B6\u6001\u670D\u52A1"),this.clearHeartbeatTimeout(),this.reset()}}});var Ht={};H(Ht,{WebServer:()=>kt});import{createServer as vo}from"http";import{serve as So}from"@hono/node-server";import{Hono as Co}from"hono";import{cors as yo}from"hono/cors";import{WebSocketServer as Eo}from"ws";var kt,jt=g(()=>{"use strict";C();Be();Pr();ie();Ir();Nr();kr();Hr();jr();Fr();Lr();zr();ke();ae();_r();Ur();kt=class{static{a(this,"WebServer")}app;httpServer=null;wss=null;logger;port;eventBus;configService;statusService;notificationService;configApiHandler;statusApiHandler;serviceApiHandler;staticFileHandler;realtimeNotificationHandler;heartbeatHandler;heartbeatMonitorInterval;proxyMCPServer;xiaozhiConnectionManager;mcpServiceManager;createErrorResponse(e,t,r){return{error:{code:e,message:t,details:r}}}createSuccessResponse(e,t){return{success:!0,data:e,message:t}}logDeprecationWarning(e,t){this.logger.warn(`[DEPRECATED] ${e} \u529F\u80FD\u5DF2\u5E9F\u5F03\uFF0C\u8BF7\u4F7F\u7528 ${t} \u66FF\u4EE3`)}constructor(e){try{this.port=e??d.getWebUIPort()??9999}catch{this.port=e??9999}this.logger=h.withTag("WebServer"),this.eventBus=U(),this.configService=new Q,this.statusService=new at,this.notificationService=new st,this.configApiHandler=new Qe,this.statusApiHandler=new it(this.statusService),this.serviceApiHandler=new rt(this.statusService),this.staticFileHandler=new ot,this.realtimeNotificationHandler=new et(this.notificationService,this.statusService),this.heartbeatHandler=new Ye(this.statusService,this.notificationService),this.app=new Co,this.setupMiddleware(),this.setupRoutes(),this.logger.info("WebServer \u67B6\u6784\u91CD\u6784\u5B8C\u6210 - \u7B2C\u4E8C\u9636\u6BB5\uFF1A\u6A21\u5757\u5316\u62C6\u5206")}async initializeConnections(){try{this.logger.info("\u5F00\u59CB\u521D\u59CB\u5316\u8FDE\u63A5...");let e=await this.loadConfiguration();this.mcpServiceManager=await Ce.getInstance(),await this.loadMCPServicesFromConfig(e.mcpServers);let t=this.mcpServiceManager.getAllTools();this.logger.info(`\u5DF2\u52A0\u8F7D ${t.length} \u4E2A\u5DE5\u5177`),await this.initializeXiaozhiConnection(e.mcpEndpoint,t),this.logger.info("\u6240\u6709\u8FDE\u63A5\u521D\u59CB\u5316\u5B8C\u6210")}catch(e){throw this.logger.error("\u8FDE\u63A5\u521D\u59CB\u5316\u5931\u8D25:",e),e}}async loadConfiguration(){if(!d.configExists())throw new Error("\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8BF7\u5148\u8FD0\u884C 'xiaozhi init' \u521D\u59CB\u5316\u914D\u7F6E");d.cleanupInvalidServerToolsConfig();let e=d.getConfig();return{mcpEndpoint:e.mcpEndpoint,mcpServers:e.mcpServers,webUIPort:e.webUI?.port??9999}}async loadMCPServicesFromConfig(e){if(!this.mcpServiceManager)throw new Error("MCPServiceManager \u672A\u521D\u59CB\u5316");for(let[t,r]of Object.entries(e)){this.logger.info(`\u6DFB\u52A0 MCP \u670D\u52A1\u914D\u7F6E: ${t}`);let n=Mr(t,r);this.mcpServiceManager.addServiceConfig(t,n)}await this.mcpServiceManager.startAllServices(),this.logger.info("\u6240\u6709 MCP \u670D\u52A1\u5DF2\u542F\u52A8")}async initializeXiaozhiConnection(e,t){let n=(Array.isArray(e)?e:[e]).filter(o=>o&&!o.includes("<\u8BF7\u586B\u5199"));if(n.length===0){this.logger.warn("\u672A\u914D\u7F6E\u6709\u6548\u7684\u5C0F\u667A\u63A5\u5165\u70B9\uFF0C\u8DF3\u8FC7\u8FDE\u63A5");return}this.logger.info(`\u521D\u59CB\u5316\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5\u7BA1\u7406\u5668\uFF0C\u7AEF\u70B9\u6570\u91CF: ${n.length}`),this.logger.debug("\u6709\u6548\u7AEF\u70B9\u5217\u8868:",n);try{this.xiaozhiConnectionManager=await Ee.getInstance({healthCheckInterval:3e4,reconnectInterval:5e3,maxReconnectAttempts:10,loadBalanceStrategy:"round-robin",connectionTimeout:1e4}),this.mcpServiceManager&&this.xiaozhiConnectionManager.setServiceManager(this.mcpServiceManager),await this.xiaozhiConnectionManager.initialize(n,t),await this.xiaozhiConnectionManager.connect(),this.xiaozhiConnectionManager.on("configChange",o=>{this.logger.info(`\u5C0F\u667A\u8FDE\u63A5\u914D\u7F6E\u53D8\u66F4: ${o.type}`,o.data)}),this.logger.info(`\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5\u7BA1\u7406\u5668\u521D\u59CB\u5316\u5B8C\u6210\uFF0C\u7BA1\u7406 ${n.length} \u4E2A\u7AEF\u70B9`)}catch(o){this.logger.error("\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5\u7BA1\u7406\u5668\u521D\u59CB\u5316\u5931\u8D25:",o),this.logger.warn("\u56DE\u9000\u5230\u5355\u8FDE\u63A5\u6A21\u5F0F");let s=n[0];this.logger.info(`\u521D\u59CB\u5316\u5355\u4E2A\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5: ${s}`),this.proxyMCPServer=new re(s),this.mcpServiceManager&&this.proxyMCPServer.setServiceManager(this.mcpServiceManager),await this.connectWithRetry(()=>this.proxyMCPServer.connect(),"\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5"),this.logger.info("\u5C0F\u667A\u63A5\u5165\u70B9\u8FDE\u63A5\u6210\u529F\uFF08\u5355\u8FDE\u63A5\u6A21\u5F0F\uFF09")}}getBestXiaozhiConnection(){return this.xiaozhiConnectionManager?this.xiaozhiConnectionManager.selectBestConnection():this.proxyMCPServer||null}getXiaozhiConnectionStatus(){return this.xiaozhiConnectionManager?{type:"multi-endpoint",manager:{healthyConnections:this.xiaozhiConnectionManager.getHealthyConnections().length,totalConnections:this.xiaozhiConnectionManager.getConnectionStatus().length,loadBalanceStats:this.xiaozhiConnectionManager.getLoadBalanceStats(),healthCheckStats:this.xiaozhiConnectionManager.getHealthCheckStats(),reconnectStats:this.xiaozhiConnectionManager.getReconnectStats()},connections:this.xiaozhiConnectionManager.getConnectionStatus()}:this.proxyMCPServer?{type:"single-endpoint",connected:!0,endpoint:"unknown"}:{type:"none",connected:!1}}async connectWithRetry(e,t,r=5,n=1e3,o=3e4,s=2){let c=null;for(let l=1;l<=r;l++)try{return this.logger.info(`${t} - \u5C1D\u8BD5\u8FDE\u63A5 (${l}/${r})`),await e()}catch(u){if(c=u,this.logger.warn(`${t} - \u8FDE\u63A5\u5931\u8D25:`,u),l<r){let W=Math.min(n*s**(l-1),o);this.logger.info(`${t} - ${W}ms \u540E\u91CD\u8BD5...`),await this.sleep(W)}}throw new Error(`${t} - \u8FDE\u63A5\u5931\u8D25\uFF0C\u5DF2\u8FBE\u5230\u6700\u5927\u91CD\u8BD5\u6B21\u6570: ${c?.message}`)}sleep(e){return new Promise(t=>setTimeout(t,e))}setupMiddleware(){this.app?.use("*",yo({origin:"*",allowMethods:["GET","POST","PUT","OPTIONS"],allowHeaders:["Content-Type"]})),this.app?.onError((e,t)=>{this.logger.error("HTTP request error:",e);let r=this.createErrorResponse("INTERNAL_SERVER_ERROR","\u670D\u52A1\u5668\u5185\u90E8\u9519\u8BEF",process.env.NODE_ENV==="development"?e.stack:void 0);return t.json(r,500)})}setupRoutes(){this.app?.get("/api/config",e=>this.configApiHandler.getConfig(e)),this.app?.put("/api/config",e=>this.configApiHandler.updateConfig(e)),this.app?.get("/api/config/mcp-endpoint",e=>this.configApiHandler.getMcpEndpoint(e)),this.app?.get("/api/config/mcp-endpoints",e=>this.configApiHandler.getMcpEndpoints(e)),this.app?.get("/api/config/mcp-servers",e=>this.configApiHandler.getMcpServers(e)),this.app?.get("/api/config/connection",e=>this.configApiHandler.getConnectionConfig(e)),this.app?.post("/api/config/reload",e=>this.configApiHandler.reloadConfig(e)),this.app?.get("/api/config/path",e=>this.configApiHandler.getConfigPath(e)),this.app?.get("/api/config/exists",e=>this.configApiHandler.checkConfigExists(e)),this.app?.get("/api/status",e=>this.statusApiHandler.getStatus(e)),this.app?.get("/api/status/client",e=>this.statusApiHandler.getClientStatus(e)),this.app?.get("/api/status/restart",e=>this.statusApiHandler.getRestartStatus(e)),this.app?.get("/api/status/connected",e=>this.statusApiHandler.checkClientConnected(e)),this.app?.get("/api/status/heartbeat",e=>this.statusApiHandler.getLastHeartbeat(e)),this.app?.get("/api/status/mcp-servers",e=>this.statusApiHandler.getActiveMCPServers(e)),this.app?.put("/api/status/client",e=>this.statusApiHandler.updateClientStatus(e)),this.app?.put("/api/status/mcp-servers",e=>this.statusApiHandler.setActiveMCPServers(e)),this.app?.post("/api/status/reset",e=>this.statusApiHandler.resetStatus(e)),this.app?.post("/api/services/restart",e=>this.serviceApiHandler.restartService(e)),this.app?.post("/api/services/stop",e=>this.serviceApiHandler.stopService(e)),this.app?.post("/api/services/start",e=>this.serviceApiHandler.startService(e)),this.app?.get("/api/services/status",e=>this.serviceApiHandler.getServiceStatus(e)),this.app?.get("/api/services/health",e=>this.serviceApiHandler.getServiceHealth(e)),this.app?.all("/api/*",async e=>{let t=this.createErrorResponse("API_NOT_FOUND",`API \u7AEF\u70B9\u4E0D\u5B58\u5728: ${e.req.path}`);return e.json(t,404)}),this.app.get("*",e=>this.staticFileHandler.handleStaticFile(e))}setupWebSocket(){this.wss&&this.wss.on("connection",e=>{let t=`client-${Date.now()}-${Math.random().toString(36).substr(2,9)}`;this.logger.info(`WebSocket \u5BA2\u6237\u7AEF\u5DF2\u8FDE\u63A5: ${t}`),this.logger.debug(`\u5F53\u524D WebSocket \u8FDE\u63A5\u6570: ${this.wss?.clients.size||0}`),this.realtimeNotificationHandler.handleClientConnect(e,t),this.heartbeatHandler.handleClientConnect(t),e.on("message",async r=>{try{let n=JSON.parse(r.toString());n.type==="clientStatus"?await this.heartbeatHandler.handleClientStatus(e,n,t):await this.realtimeNotificationHandler.handleMessage(e,n,t)}catch(n){this.logger.error("WebSocket message error:",n);let o={type:"error",error:{code:"MESSAGE_PARSE_ERROR",message:n instanceof Error?n.message:"\u6D88\u606F\u89E3\u6790\u5931\u8D25",timestamp:Date.now()}};e.send(JSON.stringify(o))}}),e.on("close",()=>{this.logger.info(`WebSocket \u5BA2\u6237\u7AEF\u5DF2\u65AD\u5F00\u8FDE\u63A5: ${t}`),this.logger.debug(`\u5269\u4F59 WebSocket \u8FDE\u63A5\u6570: ${this.wss?.clients.size||0}`),this.realtimeNotificationHandler.handleClientDisconnect(t),this.heartbeatHandler.handleClientDisconnect(t)}),e.on("error",r=>{this.logger.error(`WebSocket \u8FDE\u63A5\u9519\u8BEF (${t}):`,r)}),this.realtimeNotificationHandler.sendInitialData(e,t)})}async start(){if(this.httpServer){this.logger.warn("Web server is already running");return}let e=So({fetch:this.app.fetch,port:this.port,hostname:"0.0.0.0",createServer:vo});this.httpServer=e,this.wss=new Eo({server:this.httpServer}),this.setupWebSocket(),this.heartbeatMonitorInterval=this.heartbeatHandler.startHeartbeatMonitoring(),this.logger.info(`Web server listening on http://0.0.0.0:${this.port}`),this.logger.info(`Local access: http://localhost:${this.port}`),this.logger.info("=== \u901A\u4FE1\u67B6\u6784\u91CD\u6784\u4FE1\u606F - \u7B2C\u4E8C\u9636\u6BB5\u5B8C\u6210 ==="),this.logger.info("\u2705 \u6A21\u5757\u5316\u62C6\u5206: HTTP/WebSocket \u5904\u7406\u5668\u72EC\u7ACB"),this.logger.info("\u2705 \u670D\u52A1\u5C42\u62BD\u8C61: ConfigService, StatusService, NotificationService"),this.logger.info("\u2705 \u4E8B\u4EF6\u9A71\u52A8\u673A\u5236: EventBus \u5B9E\u73B0\u6A21\u5757\u95F4\u89E3\u8026\u901A\u4FE1"),this.logger.info("\u2705 HTTP API \u804C\u8D23: \u914D\u7F6E\u7BA1\u7406\u3001\u72B6\u6001\u67E5\u8BE2\u3001\u670D\u52A1\u63A7\u5236"),this.logger.info("\u2705 WebSocket \u804C\u8D23: \u5B9E\u65F6\u901A\u77E5\u3001\u5FC3\u8DF3\u68C0\u6D4B\u3001\u4E8B\u4EF6\u5E7F\u64AD"),this.logger.info("\u26A0\uFE0F \u5DF2\u5E9F\u5F03\u7684 WebSocket \u6D88\u606F: getConfig, updateConfig, getStatus, restartService"),this.logger.info("\u{1F4D6} \u63A8\u8350\u4F7F\u7528\u5BF9\u5E94\u7684 HTTP API \u66FF\u4EE3\u5E9F\u5F03\u7684 WebSocket \u6D88\u606F"),this.logger.info("================================================");try{await this.initializeConnections(),this.logger.info("\u6240\u6709\u8FDE\u63A5\u521D\u59CB\u5316\u5B8C\u6210")}catch(t){this.logger.error("\u8FDE\u63A5\u521D\u59CB\u5316\u5931\u8D25\uFF0C\u4F46 Web \u670D\u52A1\u5668\u7EE7\u7EED\u8FD0\u884C:",t)}}stop(){return new Promise(e=>{let t=!1,r=a(()=>{t||(t=!0,e())},"doResolve");if(this.proxyMCPServer?.disconnect(),this.heartbeatMonitorInterval&&(this.heartbeatHandler.stopHeartbeatMonitoring(this.heartbeatMonitorInterval),this.heartbeatMonitorInterval=void 0),this.wss){for(let n of this.wss.clients)n.terminate();this.wss.close(()=>{this.httpServer?this.httpServer.close(()=>{this.logger.info("Web server stopped"),r()}):(this.logger.info("Web server stopped"),r()),setTimeout(()=>{this.logger.info("Web server force stopped"),r()},2e3)})}else this.logger.info("Web server stopped"),r()})}destroy(){this.logger.info("\u9500\u6BC1 WebServer \u5B9E\u4F8B"),this.heartbeatMonitorInterval&&(this.heartbeatHandler.stopHeartbeatMonitoring(this.heartbeatMonitorInterval),this.heartbeatMonitorInterval=void 0),this.statusService.destroy(),this.notificationService.destroy(),Dr(),this.proxyMCPServer?.disconnect(),this.logger.info("WebServer \u5B9E\u4F8B\u5DF2\u9500\u6BC1")}}});var Wr={};H(Wr,{ServiceManagerImpl:()=>Ft});var Ft,Br=g(()=>{"use strict";G();fe();xe();ze();Ft=class{constructor(e,t,r){this.processManager=e;this.configManager=t;this.logger=r}static{a(this,"ServiceManagerImpl")}async start(e){try{this.validateStartOptions(e),this.processManager.cleanupContainerState();let t=this.getStatus();if(t.running){console.log(`\u68C0\u6D4B\u5230\u670D\u52A1\u5DF2\u5728\u8FD0\u884C (PID: ${t.pid})\uFF0C\u6B63\u5728\u81EA\u52A8\u91CD\u542F...`);try{await this.processManager.gracefulKillProcess(t.pid),this.processManager.cleanupPidFile(),await new Promise(r=>setTimeout(r,1e3)),console.log("\u73B0\u6709\u670D\u52A1\u5DF2\u505C\u6B62\uFF0C\u6B63\u5728\u542F\u52A8\u65B0\u670D\u52A1...")}catch(r){console.warn(`\u505C\u6B62\u73B0\u6709\u670D\u52A1\u65F6\u51FA\u73B0\u8B66\u544A: ${r instanceof Error?r.message:String(r)}`)}}switch(this.checkEnvironment(),e.mode){case"mcp-server":await this.startMcpServerMode(e);break;case"stdio":await this.startStdioMode(e);break;case"normal":await this.startNormalMode(e);break;default:await this.startNormalMode(e);break}}catch(t){throw t instanceof w?t:w.startFailed(t instanceof Error?t.message:String(t))}}async stop(){try{let e=this.getStatus();if(!e.running)throw w.notRunning();await this.processManager.gracefulKillProcess(e.pid),this.processManager.cleanupPidFile()}catch(e){throw e instanceof w?e:new w(`\u505C\u6B62\u670D\u52A1\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}}async restart(e){try{this.getStatus().running&&(await this.stop(),await new Promise(r=>setTimeout(r,1e3))),await this.start(e)}catch(t){throw new w(`\u91CD\u542F\u670D\u52A1\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`)}}getStatus(){return this.processManager.getServiceStatus()}validateStartOptions(e){if(e.port!==void 0&&z.validatePort(e.port),e.mode&&!["normal","mcp-server","stdio"].includes(e.mode))throw new w(`\u65E0\u6548\u7684\u8FD0\u884C\u6A21\u5F0F: ${e.mode}`)}checkEnvironment(){if(!this.configManager.configExists())throw se.configNotFound();try{if(!this.configManager.getConfig())throw new se("\u914D\u7F6E\u6587\u4EF6\u65E0\u6548")}catch(e){throw e instanceof se?e:new se(`\u914D\u7F6E\u6587\u4EF6\u9519\u8BEF: ${e instanceof Error?e.message:String(e)}`)}}async startNormalMode(e){let{spawn:t}=await import("child_process");e.daemon?await this.startWebServerInDaemon(e.ui||!1):await this.startWebServerInForeground(e.ui||!1)}async startMcpServerMode(e){let t=e.port||3e3,{spawn:r}=await import("child_process");if(e.daemon){let n=b.getExecutablePath("cli"),o=r("node",[n,"start","--server",t.toString()],{detached:!0,stdio:["ignore","ignore","ignore"],env:{...process.env,XIAOZHI_CONFIG_DIR:b.getConfigDir(),XIAOZHI_DAEMON:"true",MCP_SERVER_MODE:"true"}});this.processManager.savePidInfo(o.pid,"daemon"),o.unref(),console.log(`\u2705 MCP Server \u5DF2\u5728\u540E\u53F0\u542F\u52A8 (PID: ${o.pid}, Port: ${t})`),console.log("\u{1F4A1} \u4F7F\u7528 'xiaozhi status' \u67E5\u770B\u72B6\u6001"),process.exit(0)}else{let{MCPServer:n}=await Promise.resolve().then(()=>(wr(),br)),o=new n(t),s=a(async()=>{await o.stop(),process.exit(0)},"cleanup");process.on("SIGINT",s),process.on("SIGTERM",s),await o.start()}}async startStdioMode(e){let{spawn:t}=await import("child_process"),r=b.getMcpServerProxyPath(),n=t("node",[r],{stdio:"inherit",env:{...process.env,XIAOZHI_CONFIG_DIR:b.getConfigDir()}});this.processManager.savePidInfo(n.pid,"foreground")}async startWebServerInDaemon(e){let{spawn:t}=await import("child_process"),r=b.getWebServerStandalonePath();if(!(await import("fs")).default.existsSync(r))throw new w(`WebServer \u6587\u4EF6\u4E0D\u5B58\u5728: ${r}`);let o=[r];e&&o.push("--open-browser");let s=t("node",o,{detached:!0,stdio:["ignore","ignore","ignore"],env:{...process.env,XIAOZHI_CONFIG_DIR:b.getConfigDir(),XIAOZHI_DAEMON:"true"}});this.processManager.savePidInfo(s.pid,"daemon"),s.unref(),console.log(`\u2705 \u540E\u53F0\u670D\u52A1\u5DF2\u542F\u52A8 (PID: ${s.pid})`),console.log("\u{1F4A1} \u4F7F\u7528 'xiaozhi status' \u67E5\u770B\u72B6\u6001"),console.log("\u{1F4A1} \u4F7F\u7528 'xiaozhi attach' \u67E5\u770B\u65E5\u5FD7"),process.exit(0)}async startWebServerInForeground(e){let{WebServer:t}=await Promise.resolve().then(()=>(jt(),Ht)),r=new t,n=a(async()=>{await r.stop(),this.processManager.cleanupPidFile(),process.exit(0)},"cleanup");if(process.on("SIGINT",n),process.on("SIGTERM",n),this.processManager.savePidInfo(process.pid,"foreground"),await r.start(),e){let s=this.configManager.getConfig()?.webServer?.port||9999;await this.openBrowserUrl(`http://localhost:${s}`)}}async openBrowserUrl(e){try{let{spawn:t}=await import("child_process"),r=j.getCurrentPlatform(),n,o;r==="darwin"?(n="open",o=[e]):r==="win32"?(n="start",o=["",e]):(n="xdg-open",o=[e]),t(n,o,{detached:!0,stdio:"ignore"}),console.log(`\u{1F310} \u5DF2\u5C1D\u8BD5\u6253\u5F00\u6D4F\u89C8\u5668: ${e}`)}catch{console.log(`\u26A0\uFE0F \u81EA\u52A8\u6253\u5F00\u6D4F\u89C8\u5668\u5931\u8D25\uFF0C\u8BF7\u624B\u52A8\u8BBF\u95EE: ${e}`)}}}});var Vr={};H(Vr,{TemplateManagerImpl:()=>Lt});import bo from"fs";import le from"path";var Lt,Gr=g(()=>{"use strict";G();Ie();fe();ze();Lt=class{static{a(this,"TemplateManagerImpl")}templateCache=new Map;async getAvailableTemplates(){try{let e=b.findTemplatesDir();if(!e)return[];let t=[],r=bo.readdirSync(e,{withFileTypes:!0}).filter(n=>n.isDirectory()).map(n=>n.name);for(let n of r)try{let o=await this.getTemplateInfo(n);o&&t.push(o)}catch{console.warn(`\u8DF3\u8FC7\u65E0\u6548\u6A21\u677F: ${n}`)}return t}catch{throw new m("\u65E0\u6CD5\u8BFB\u53D6\u6A21\u677F\u76EE\u5F55",b.findTemplatesDir()||"")}}async getTemplateInfo(e){try{if(z.validateTemplateName(e),this.templateCache.has(e))return this.templateCache.get(e);let t=b.getTemplatePath(e);if(!t)return null;let r=le.join(t,"template.json"),n={};if(S.exists(r))try{let c=S.readFile(r);n=JSON.parse(c)}catch{console.warn(`\u6A21\u677F\u914D\u7F6E\u6587\u4EF6\u89E3\u6790\u5931\u8D25: ${e}`)}let o=this.getTemplateFiles(t),s={name:e,path:t,description:n.description||`${e} \u6A21\u677F`,version:n.version||"1.0.0",author:n.author,files:o};return this.templateCache.set(e,s),s}catch(t){throw t instanceof v?t:new m(`\u65E0\u6CD5\u83B7\u53D6\u6A21\u677F\u4FE1\u606F: ${e}`,"")}}async copyTemplate(e,t){await this.createProject({templateName:e,targetPath:t,projectName:le.basename(t)})}async createProject(e){try{this.validateCreateOptions(e);let t=e.templateName||"default",r=await this.getTemplateInfo(t);if(!r)throw new m(`\u6A21\u677F\u4E0D\u5B58\u5728: ${t}`,"");let n=le.resolve(e.targetPath);if(S.exists(n))throw m.alreadyExists(n);S.ensureDir(n),await this.copyTemplateFiles(r,n,e),await this.processTemplateVariables(n,e),console.log(`\u2705 \u9879\u76EE\u521B\u5EFA\u6210\u529F: ${n}`)}catch(t){throw t instanceof m||t instanceof v?t:new m(`\u521B\u5EFA\u9879\u76EE\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`,e.targetPath)}}async validateTemplate(e){try{let t=await this.getTemplateInfo(e);if(!t)return!1;let r=["package.json"];for(let n of r){let o=le.join(t.path,n);if(!S.exists(o))return console.warn(`\u6A21\u677F\u7F3A\u5C11\u5FC5\u8981\u6587\u4EF6: ${n}`),!1}return!0}catch{return!1}}clearCache(){this.templateCache.clear()}getTemplateFiles(e){try{return S.listDirectory(e,{recursive:!0,includeHidden:!1}).filter(r=>{let n=le.relative(e,r);return!n.startsWith(".")&&n!=="template.json"&&!n.includes("node_modules")})}catch{return[]}}validateCreateOptions(e){z.validateRequired(e.targetPath,"targetPath"),z.validateRequired(e.projectName,"projectName"),z.validateProjectName(e.projectName),e.templateName&&z.validateTemplateName(e.templateName)}async copyTemplateFiles(e,t,r){try{S.copyDirectory(e.path,t,{exclude:["template.json",".git","node_modules"],overwrite:!1,recursive:!0})}catch(n){throw new m(`\u590D\u5236\u6A21\u677F\u6587\u4EF6\u5931\u8D25: ${n instanceof Error?n.message:String(n)}`,e.path)}}async processTemplateVariables(e,t){try{let r={PROJECT_NAME:t.projectName,PROJECT_NAME_LOWER:t.projectName.toLowerCase(),PROJECT_NAME_UPPER:t.projectName.toUpperCase(),...t.variables},n=["package.json","README.md","src/**/*.ts","src/**/*.js","src/**/*.json"];for(let o of n){let s=this.findFilesByPattern(e,o);for(let c of s)await this.replaceVariablesInFile(c,r)}}catch(r){console.warn(`\u5904\u7406\u6A21\u677F\u53D8\u91CF\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`)}}findFilesByPattern(e,t){try{if(!t.includes("*")){let o=le.join(e,t);return S.exists(o)?[o]:[]}let r=S.listDirectory(e,{recursive:!0}),n=new RegExp(t.replace(/\*\*/g,".*").replace(/\*/g,"[^/]*"));return r.filter(o=>{let s=le.relative(e,o);return n.test(s)})}catch{return[]}}async replaceVariablesInFile(e,t){try{let r=S.readFile(e),n=!1;for(let[o,s]of Object.entries(t)){let c=new RegExp(`{{\\s*${o}\\s*}}`,"g");c.test(r)&&(r=r.replace(c,s),n=!0)}n&&S.writeFile(e,r,{overwrite:!0})}catch(r){console.warn(`\u66FF\u6362\u6587\u4EF6\u53D8\u91CF\u5931\u8D25 ${e}: ${r instanceof Error?r.message:String(r)}`)}}}});async function ce(){return zt.create()}var zt,Dt=g(()=>{"use strict";C();ie();yt();Ie();Et();fe();xe();ze();ir();zt=class i{static{a(this,"DIContainer")}instances=new Map;factories=new Map;asyncFactories=new Map;singletons=new Set;register(e,t,r=!1){this.factories.set(e,t),r&&this.singletons.add(e)}registerSingleton(e,t){this.register(e,t,!0)}registerInstance(e,t){this.instances.set(e,t),this.singletons.add(e)}get(e){if(this.singletons.has(e)&&this.instances.has(e))return this.instances.get(e);let t=this.factories.get(e);if(!t)throw new Error(`Service ${e} not registered`);let r=t();return this.singletons.has(e)&&this.instances.set(e,r),r}has(e){return this.factories.has(e)||this.instances.has(e)}clear(){this.instances.clear(),this.factories.clear(),this.singletons.clear()}getRegisteredKeys(){let e=Array.from(this.factories.keys()),t=Array.from(this.instances.keys());return[...new Set([...e,...t])]}static create(){let e=new i;return e.registerSingleton("versionUtils",()=>Ue),e.registerSingleton("platformUtils",()=>j),e.registerSingleton("formatUtils",()=>me),e.registerSingleton("fileUtils",()=>S),e.registerSingleton("pathUtils",()=>b),e.registerSingleton("validation",()=>z),e.registerSingleton("configManager",()=>d),e.registerSingleton("logger",()=>h),e.registerSingleton("errorHandler",()=>X),e.registerSingleton("processManager",()=>{let t=(ar(),V(sr));return new t.ProcessManagerImpl}),e.registerSingleton("daemonManager",()=>{let t=(gr(),V(lr)),r=e.get("processManager"),n=e.get("logger");return new t.DaemonManagerImpl(r,n)}),e.registerSingleton("serviceManager",()=>{let t=(Br(),V(Wr)),r=e.get("processManager"),n=e.get("configManager"),o=e.get("logger");return new t.ServiceManagerImpl(r,n,o)}),e.registerSingleton("templateManager",()=>{let t=(Gr(),V(Vr));return new t.TemplateManagerImpl}),e}};a(ce,"createContainer")});var k,ge=g(()=>{"use strict";k=class{constructor(e){this.container=e}static{a(this,"BaseCommandHandler")}options;subcommands;getService(e){return this.container.get(e)}handleError(e){this.getService("errorHandler").handle(e)}validateArgs(e,t){if(e.length<t)throw new Error(`\u547D\u4EE4\u9700\u8981\u81F3\u5C11 ${t} \u4E2A\u53C2\u6570\uFF0C\u4F46\u53EA\u63D0\u4F9B\u4E86 ${e.length} \u4E2A`)}}});var Xr={};H(Xr,{ServiceCommandHandler:()=>_t});var _t,Kr=g(()=>{"use strict";ge();_t=class extends k{static{a(this,"ServiceCommandHandler")}name="service";description="\u670D\u52A1\u7BA1\u7406\u547D\u4EE4";subcommands=[{name:"start",description:"\u542F\u52A8\u670D\u52A1",options:[{flags:"-d, --daemon",description:"\u5728\u540E\u53F0\u8FD0\u884C\u670D\u52A1"},{flags:"-u, --ui",description:"\u540C\u65F6\u542F\u52A8 Web UI \u670D\u52A1"},{flags:"-s, --server [port]",description:"\u4EE5 MCP Server \u6A21\u5F0F\u542F\u52A8 (\u53EF\u9009\u6307\u5B9A\u7AEF\u53E3\uFF0C\u9ED8\u8BA4 3000)"},{flags:"--stdio",description:"\u4EE5 stdio \u6A21\u5F0F\u8FD0\u884C MCP Server (\u7528\u4E8E Cursor \u7B49\u5BA2\u6237\u7AEF)"}],execute:a(async(e,t)=>{await this.handleStart(t)},"execute")},{name:"stop",description:"\u505C\u6B62\u670D\u52A1",execute:a(async(e,t)=>{await this.handleStop()},"execute")},{name:"status",description:"\u68C0\u67E5\u670D\u52A1\u72B6\u6001",execute:a(async(e,t)=>{await this.handleStatus()},"execute")},{name:"restart",description:"\u91CD\u542F\u670D\u52A1",options:[{flags:"-d, --daemon",description:"\u5728\u540E\u53F0\u8FD0\u884C\u670D\u52A1"},{flags:"-u, --ui",description:"\u540C\u65F6\u542F\u52A8 Web UI \u670D\u52A1"}],execute:a(async(e,t)=>{await this.handleRestart(t)},"execute")},{name:"attach",description:"\u8FDE\u63A5\u5230\u540E\u53F0\u670D\u52A1\u67E5\u770B\u65E5\u5FD7",execute:a(async(e,t)=>{await this.handleAttach()},"execute")}];constructor(e){super(e)}async execute(e,t){console.log("\u670D\u52A1\u7BA1\u7406\u547D\u4EE4\u3002\u4F7F\u7528 --help \u67E5\u770B\u53EF\u7528\u7684\u5B50\u547D\u4EE4\u3002")}async handleStart(e){try{let t=this.getService("serviceManager");if(e.stdio)await this.startStdioMode();else if(e.server){let r=typeof e.server=="string"?Number.parseInt(e.server):3e3;await this.startMCPServerMode(r,e.daemon)}else await t.start({daemon:e.daemon||!1,ui:e.ui||!1})}catch(t){this.handleError(t)}}async handleStop(){try{await this.getService("serviceManager").stop()}catch(e){this.handleError(e)}}async handleStatus(){try{let t=await this.getService("serviceManager").getStatus();t.running?(console.log(`\u2705 \u670D\u52A1\u6B63\u5728\u8FD0\u884C (PID: ${t.pid})`),t.uptime&&console.log(`\u23F1\uFE0F \u8FD0\u884C\u65F6\u95F4: ${t.uptime}`),t.mode&&console.log(`\u{1F527} \u8FD0\u884C\u6A21\u5F0F: ${t.mode}`)):console.log("\u274C \u670D\u52A1\u672A\u8FD0\u884C")}catch(e){this.handleError(e)}}async handleRestart(e){try{await this.getService("serviceManager").restart({daemon:e.daemon||!1,ui:e.ui||!1})}catch(t){this.handleError(t)}}async handleAttach(){try{await this.getService("daemonManager").attachToLogs()}catch(e){this.handleError(e)}}async startStdioMode(){let{spawn:e}=await import("child_process"),{fileURLToPath:t}=await import("url"),r=await import("path"),n=t(import.meta.url),o=r.dirname(n),s=r.join(o,"mcpServerProxy.js");e("node",[s],{stdio:"inherit",env:{...process.env,XIAOZHI_CONFIG_DIR:process.env.XIAOZHI_CONFIG_DIR||process.cwd()}})}async startMCPServerMode(e,t){throw new Error("MCP Server \u6A21\u5F0F\u542F\u52A8\u903B\u8F91\u9700\u8981\u5B9E\u73B0")}}});var qr={};H(qr,{ConfigCommandHandler:()=>Wt});import wo from"path";import y from"chalk";import Ut from"ora";var Wt,Jr=g(()=>{"use strict";ge();Wt=class extends k{static{a(this,"ConfigCommandHandler")}name="config";description="\u914D\u7F6E\u7BA1\u7406\u547D\u4EE4";subcommands=[{name:"init",description:"\u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6",options:[{flags:"-f, --format <format>",description:"\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F (json, json5, jsonc)",defaultValue:"json"}],execute:a(async(e,t)=>{await this.handleInit(t)},"execute")},{name:"get",description:"\u67E5\u770B\u914D\u7F6E\u503C",execute:a(async(e,t)=>{this.validateArgs(e,1),await this.handleGet(e[0])},"execute")},{name:"set",description:"\u8BBE\u7F6E\u914D\u7F6E\u503C",execute:a(async(e,t)=>{this.validateArgs(e,2),await this.handleSet(e[0],e[1])},"execute")}];constructor(e){super(e)}async execute(e,t){console.log("\u914D\u7F6E\u7BA1\u7406\u547D\u4EE4\u3002\u4F7F\u7528 --help \u67E5\u770B\u53EF\u7528\u7684\u5B50\u547D\u4EE4\u3002")}async handleInit(e){let t=Ut("\u521D\u59CB\u5316\u914D\u7F6E...").start();try{let r=e.format;if(r!=="json"&&r!=="json5"&&r!=="jsonc")throw new Error("\u683C\u5F0F\u5FC5\u987B\u662F json, json5 \u6216 jsonc");let n=this.getService("configManager");if(n.configExists()){t.warn("\u914D\u7F6E\u6587\u4EF6\u5DF2\u5B58\u5728"),console.log(y.yellow("\u5982\u9700\u91CD\u65B0\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u5220\u9664\u73B0\u6709\u7684\u914D\u7F6E\u6587\u4EF6"));return}n.initConfig(r),t.succeed("\u914D\u7F6E\u6587\u4EF6\u521D\u59CB\u5316\u6210\u529F");let o=process.env.XIAOZHI_CONFIG_DIR||process.cwd(),s=`xiaozhi.config.${r}`,c=wo.join(o,s);console.log(y.green(`\u2705 \u914D\u7F6E\u6587\u4EF6\u5DF2\u521B\u5EFA: ${s}`)),console.log(y.yellow("\u{1F4DD} \u8BF7\u7F16\u8F91\u914D\u7F6E\u6587\u4EF6\u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9:")),console.log(y.gray(` \u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84: ${c}`)),console.log(y.yellow("\u{1F4A1} \u6216\u8005\u4F7F\u7528\u547D\u4EE4\u8BBE\u7F6E:")),console.log(y.gray(" xiaozhi config set mcpEndpoint <your-endpoint-url>"))}catch(r){t.fail(`\u521D\u59CB\u5316\u914D\u7F6E\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`),this.handleError(r)}}async handleGet(e){let t=Ut("\u8BFB\u53D6\u914D\u7F6E...").start();try{let r=this.getService("configManager");if(!r.configExists()){t.fail("\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728"),console.log(y.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u5148\u8FD0\u884C "xiaozhi config init" \u521D\u59CB\u5316\u914D\u7F6E'));return}let n=r.getConfig();switch(e){case"mcpEndpoint":{t.succeed("\u914D\u7F6E\u4FE1\u606F");let o=r.getMcpEndpoints();o.length===0?console.log(y.yellow("\u672A\u914D\u7F6E\u4EFB\u4F55 MCP \u7AEF\u70B9")):o.length===1?console.log(y.green(`MCP \u7AEF\u70B9: ${o[0]}`)):(console.log(y.green(`MCP \u7AEF\u70B9 (${o.length} \u4E2A):`)),o.forEach((s,c)=>{console.log(y.gray(` ${c+1}. ${s}`))}));break}case"mcpServers":t.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(y.green("MCP \u670D\u52A1:"));for(let[o,s]of Object.entries(n.mcpServers)){let c=s;"type"in c&&c.type==="sse"?console.log(y.gray(` ${o}: [SSE] ${c.url}`)):console.log(y.gray(` ${o}: ${c.command} ${c.args.join(" ")}`))}break;case"connection":{t.succeed("\u914D\u7F6E\u4FE1\u606F");let o=r.getConnectionConfig();console.log(y.green("\u8FDE\u63A5\u914D\u7F6E:")),console.log(y.gray(` \u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694: ${o.heartbeatInterval}ms`)),console.log(y.gray(` \u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4: ${o.heartbeatTimeout}ms`)),console.log(y.gray(` \u91CD\u8FDE\u95F4\u9694: ${o.reconnectInterval}ms`));break}case"heartbeatInterval":t.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(y.green(`\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694: ${r.getHeartbeatInterval()}ms`));break;case"heartbeatTimeout":t.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(y.green(`\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4: ${r.getHeartbeatTimeout()}ms`));break;case"reconnectInterval":t.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(y.green(`\u91CD\u8FDE\u95F4\u9694: ${r.getReconnectInterval()}ms`));break;default:t.fail(`\u672A\u77E5\u7684\u914D\u7F6E\u9879: ${e}`),console.log(y.yellow("\u652F\u6301\u7684\u914D\u7F6E\u9879: mcpEndpoint, mcpServers, connection, heartbeatInterval, heartbeatTimeout, reconnectInterval"))}}catch(r){t.fail(`\u8BFB\u53D6\u914D\u7F6E\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`),this.handleError(r)}}async handleSet(e,t){let r=Ut("\u66F4\u65B0\u914D\u7F6E...").start();try{let n=this.getService("configManager");if(!n.configExists()){r.fail("\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728"),console.log(y.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u5148\u8FD0\u884C "xiaozhi config init" \u521D\u59CB\u5316\u914D\u7F6E'));return}switch(e){case"mcpEndpoint":n.updateMcpEndpoint(t),r.succeed(`MCP \u7AEF\u70B9\u5DF2\u8BBE\u7F6E\u4E3A: ${t}`);break;case"heartbeatInterval":{let o=Number.parseInt(t);if(Number.isNaN(o)||o<=0)throw new Error("\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5FC5\u987B\u662F\u6B63\u6574\u6570");n.updateHeartbeatInterval(o),r.succeed(`\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5DF2\u8BBE\u7F6E\u4E3A: ${o}ms`);break}case"heartbeatTimeout":{let o=Number.parseInt(t);if(Number.isNaN(o)||o<=0)throw new Error("\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5FC5\u987B\u662F\u6B63\u6574\u6570");n.updateHeartbeatTimeout(o),r.succeed(`\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5DF2\u8BBE\u7F6E\u4E3A: ${o}ms`);break}case"reconnectInterval":{let o=Number.parseInt(t);if(Number.isNaN(o)||o<=0)throw new Error("\u91CD\u8FDE\u95F4\u9694\u5FC5\u987B\u662F\u6B63\u6574\u6570");n.updateReconnectInterval(o),r.succeed(`\u91CD\u8FDE\u95F4\u9694\u5DF2\u8BBE\u7F6E\u4E3A: ${o}ms`);break}default:r.fail(`\u4E0D\u652F\u6301\u8BBE\u7F6E\u7684\u914D\u7F6E\u9879: ${e}`),console.log(y.yellow("\u652F\u6301\u8BBE\u7F6E\u7684\u914D\u7F6E\u9879: mcpEndpoint, heartbeatInterval, heartbeatTimeout, reconnectInterval"))}}catch(n){r.fail(`\u8BBE\u7F6E\u914D\u7F6E\u5931\u8D25: ${n instanceof Error?n.message:String(n)}`),this.handleError(n)}}}});var Zr={};H(Zr,{ProjectCommandHandler:()=>Bt});import Mo from"path";import M from"chalk";import Po from"ora";var Bt,Qr=g(()=>{"use strict";ge();Bt=class extends k{static{a(this,"ProjectCommandHandler")}name="create";description="\u521B\u5EFA\u9879\u76EE";options=[{flags:"-t, --template <templateName>",description:"\u4F7F\u7528\u6307\u5B9A\u6A21\u677F\u521B\u5EFA\u9879\u76EE"}];constructor(e){super(e)}async execute(e,t){this.validateArgs(e,1);let r=e[0];await this.handleCreate(r,t)}async handleCreate(e,t){let r=Po("\u521D\u59CB\u5316\u9879\u76EE...").start();try{let n=this.getService("templateManager"),o=this.getService("fileUtils"),s=Mo.join(process.cwd(),e);if(await o.exists(s)){r.fail(`\u76EE\u5F55 "${e}" \u5DF2\u5B58\u5728`),console.log(M.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u9009\u62E9\u4E0D\u540C\u7684\u9879\u76EE\u540D\u79F0\u6216\u5220\u9664\u73B0\u6709\u76EE\u5F55"));return}t.template?await this.createFromTemplate(e,t.template,s,r,n):await this.createBasicProject(e,s,r,n)}catch(n){r.fail(`\u521B\u5EFA\u9879\u76EE\u5931\u8D25: ${n instanceof Error?n.message:String(n)}`),this.handleError(n)}}async createFromTemplate(e,t,r,n,o){n.text="\u68C0\u67E5\u6A21\u677F...";let s=await o.getAvailableTemplates();if(s.length===0){n.fail("\u627E\u4E0D\u5230\u53EF\u7528\u6A21\u677F"),console.log(M.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u786E\u4FDD xiaozhi-client \u6B63\u786E\u5B89\u88C5"));return}let c=t;if(!await o.validateTemplate(c)){n.fail(`\u6A21\u677F "${c}" \u4E0D\u5B58\u5728`);let u=this.findSimilarTemplate(c,s);if(u)if(console.log(M.yellow(`\u{1F4A1} \u4F60\u662F\u60F3\u4F7F\u7528\u6A21\u677F "${u}" \u5417\uFF1F`)),await this.askUserConfirmation(M.cyan("\u786E\u8BA4\u4F7F\u7528\u6B64\u6A21\u677F\uFF1F(y/n): ")))c=u;else{this.showAvailableTemplates(s);return}else{this.showAvailableTemplates(s);return}}n.text=`\u4ECE\u6A21\u677F "${c}" \u521B\u5EFA\u9879\u76EE "${e}"...`,await o.createProject({templateName:c,targetPath:r,projectName:e}),n.succeed(`\u9879\u76EE "${e}" \u521B\u5EFA\u6210\u529F`),console.log(M.green("\u2705 \u9879\u76EE\u521B\u5EFA\u5B8C\u6210!")),console.log(M.yellow("\u{1F4DD} \u63A5\u4E0B\u6765\u7684\u6B65\u9AA4:")),console.log(M.gray(` cd ${e}`)),console.log(M.gray(" pnpm install # \u5B89\u88C5\u4F9D\u8D56")),console.log(M.gray(" # \u7F16\u8F91 xiaozhi.config.json \u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9")),console.log(M.gray(" xiaozhi start # \u542F\u52A8\u670D\u52A1"))}async createBasicProject(e,t,r,n){r.text=`\u521B\u5EFA\u57FA\u672C\u9879\u76EE "${e}"...`,await n.createProject({templateName:null,targetPath:t,projectName:e}),r.succeed(`\u9879\u76EE "${e}" \u521B\u5EFA\u6210\u529F`),console.log(M.green("\u2705 \u57FA\u672C\u9879\u76EE\u521B\u5EFA\u5B8C\u6210!")),console.log(M.yellow("\u{1F4DD} \u63A5\u4E0B\u6765\u7684\u6B65\u9AA4:")),console.log(M.gray(` cd ${e}`)),console.log(M.gray(" # \u7F16\u8F91 xiaozhi.config.json \u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9\u548C\u670D\u52A1")),console.log(M.gray(" xiaozhi start # \u542F\u52A8\u670D\u52A1")),console.log(M.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 --template \u9009\u9879\u53EF\u4EE5\u4ECE\u6A21\u677F\u521B\u5EFA\u9879\u76EE"))}showAvailableTemplates(e){console.log(M.yellow("\u53EF\u7528\u7684\u6A21\u677F:"));for(let t of e)console.log(M.gray(` - ${t}`))}findSimilarTemplate(e,t){let r=this.getService("formatUtils"),n=null,o=0;for(let s of t){let c=r.calculateSimilarity(e.toLowerCase(),s.toLowerCase());c>o&&c>.6&&(o=c,n=s)}return n}async askUserConfirmation(e){let r=(await import("readline")).createInterface({input:process.stdin,output:process.stdout});return new Promise(n=>{r.question(e,o=>{r.close(),n(o.toLowerCase().trim()==="y"||o.toLowerCase().trim()==="yes")})})}}});var ct={};H(ct,{getDisplayWidth:()=>Gt,listMcpServers:()=>Ro,listServerTools:()=>To,setToolEnabled:()=>Io,truncateToWidth:()=>Xt});import p from"chalk";import Yr from"cli-table3";import Vt from"ora";function Gt(i){let e=0;for(let t of i)/[\u4e00-\u9fff\u3400-\u4dbf\uff00-\uffef]/.test(t)?e+=2:e+=1;return e}function Xt(i,e){if(Gt(i)<=e)return i;if(e<=3)return"";let t="",r=0,n=!1;for(let o of i){let s=/[\u4e00-\u9fff\u3400-\u4dbf\uff00-\uffef]/.test(o)?2:1;if(r+s>e-3){if(!n)return"";t+="...";break}t+=o,r+=s,n=!0}return t}async function Ro(i={}){let e=Vt("\u83B7\u53D6 MCP \u670D\u52A1\u5217\u8868...").start();try{let t=d.getMcpServers(),r=Object.keys(t);if(r.length===0){e.warn("\u672A\u914D\u7F6E\u4EFB\u4F55 MCP \u670D\u52A1"),console.log(p.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi config' \u547D\u4EE4\u914D\u7F6E MCP \u670D\u52A1"));return}if(e.succeed(`\u627E\u5230 ${r.length} \u4E2A MCP \u670D\u52A1`),i.tools){console.log(),console.log(p.bold("MCP \u670D\u52A1\u5DE5\u5177\u5217\u8868:")),console.log();let n=8,o=[];for(let c of r){let l=d.getServerToolsConfig(c),u=Object.keys(l);o.push(...u)}for(let c of o){let l=Gt(c);l>n&&(n=l)}n=Math.max(10,Math.min(n+2,30));let s=new Yr({head:[p.bold("MCP"),p.bold("\u5DE5\u5177\u540D\u79F0"),p.bold("\u72B6\u6001"),p.bold("\u63CF\u8FF0")],colWidths:[15,n,8,40],wordWrap:!0,style:{head:[],border:[]}});for(let c of r){let l=d.getServerToolsConfig(c),u=Object.keys(l);if(u.length===0)s.push([p.gray(c),p.gray("-"),p.gray("-"),p.gray("\u6682\u672A\u8BC6\u522B\u5230\u76F8\u5173\u5DE5\u5177")]);else{s.length>0&&s.push([{colSpan:4,content:""}]);for(let W of u){let I=l[W],te=I.enable?p.green("\u542F\u7528"):p.red("\u7981\u7528"),pe=Xt(I.description||"",32);s.push([c,W,te,pe])}}}console.log(s.toString())}else{console.log(),console.log(p.bold("MCP \u670D\u52A1\u5217\u8868:")),console.log();for(let n of r){let o=t[n],s=d.getServerToolsConfig(n),c=Object.keys(s).length,l=Object.values(s).filter(u=>u.enable!==!1).length;console.log(`${p.cyan("\u2022")} ${p.bold(n)}`),"url"in o?("type"in o&&o.type==="sse"?console.log(` \u7C7B\u578B: ${p.gray("SSE")}`):console.log(` \u7C7B\u578B: ${p.gray("Streamable HTTP")}`),console.log(` URL: ${p.gray(o.url)}`)):console.log(` \u547D\u4EE4: ${p.gray(o.command)} ${p.gray(o.args.join(" "))}`),c>0?console.log(` \u5DE5\u5177: ${p.green(l)} \u542F\u7528 / ${p.yellow(c)} \u603B\u8BA1`):console.log(` \u5DE5\u5177: ${p.gray("\u672A\u626B\u63CF (\u8BF7\u5148\u542F\u52A8\u670D\u52A1)")}`),console.log()}}console.log(p.gray("\u{1F4A1} \u63D0\u793A:")),console.log(p.gray(" - \u4F7F\u7528 'xiaozhi mcp list --tools' \u67E5\u770B\u6240\u6709\u5DE5\u5177")),console.log(p.gray(" - \u4F7F\u7528 'xiaozhi mcp <\u670D\u52A1\u540D> list' \u67E5\u770B\u6307\u5B9A\u670D\u52A1\u7684\u5DE5\u5177")),console.log(p.gray(" - \u4F7F\u7528 'xiaozhi mcp <\u670D\u52A1\u540D> <\u5DE5\u5177\u540D> enable/disable' \u542F\u7528/\u7981\u7528\u5DE5\u5177"))}catch(t){e.fail("\u83B7\u53D6 MCP \u670D\u52A1\u5217\u8868\u5931\u8D25"),console.error(p.red(`\u9519\u8BEF: ${t instanceof Error?t.message:String(t)}`)),process.exit(1)}}async function To(i){let e=Vt(`\u83B7\u53D6 ${i} \u670D\u52A1\u7684\u5DE5\u5177\u5217\u8868...`).start();try{if(!d.getMcpServers()[i]){e.fail(`\u670D\u52A1 '${i}' \u4E0D\u5B58\u5728`),console.log(p.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp list' \u67E5\u770B\u6240\u6709\u53EF\u7528\u670D\u52A1"));return}let r=d.getServerToolsConfig(i),n=Object.keys(r);if(n.length===0){e.warn(`\u670D\u52A1 '${i}' \u6682\u65E0\u5DE5\u5177\u4FE1\u606F`),console.log(p.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u5148\u542F\u52A8\u670D\u52A1\u4EE5\u626B\u63CF\u5DE5\u5177\u5217\u8868"));return}e.succeed(`\u670D\u52A1 '${i}' \u5171\u6709 ${n.length} \u4E2A\u5DE5\u5177`),console.log(),console.log(p.bold(`${i} \u670D\u52A1\u5DE5\u5177\u5217\u8868:`)),console.log();let o=new Yr({head:[p.bold("\u5DE5\u5177\u540D\u79F0"),p.bold("\u72B6\u6001"),p.bold("\u63CF\u8FF0")],colWidths:[30,8,50],wordWrap:!0,style:{head:[],border:[]}});for(let s of n){let c=r[s],l=c.enable?p.green("\u542F\u7528"):p.red("\u7981\u7528"),u=Xt(c.description||"",40);o.push([s,l,u])}console.log(o.toString()),console.log(),console.log(p.gray("\u{1F4A1} \u63D0\u793A:")),console.log(p.gray(` - \u4F7F\u7528 'xiaozhi mcp ${i} <\u5DE5\u5177\u540D> enable' \u542F\u7528\u5DE5\u5177`)),console.log(p.gray(` - \u4F7F\u7528 'xiaozhi mcp ${i} <\u5DE5\u5177\u540D> disable' \u7981\u7528\u5DE5\u5177`))}catch(t){e.fail("\u83B7\u53D6\u5DE5\u5177\u5217\u8868\u5931\u8D25"),console.error(p.red(`\u9519\u8BEF: ${t instanceof Error?t.message:String(t)}`)),process.exit(1)}}async function Io(i,e,t){let r=t?"\u542F\u7528":"\u7981\u7528",n=Vt(`${r}\u5DE5\u5177 ${i}/${e}...`).start();try{if(!d.getMcpServers()[i]){n.fail(`\u670D\u52A1 '${i}' \u4E0D\u5B58\u5728`),console.log(p.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp list' \u67E5\u770B\u6240\u6709\u53EF\u7528\u670D\u52A1"));return}let s=d.getServerToolsConfig(i);if(!s[e]){n.fail(`\u5DE5\u5177 '${e}' \u5728\u670D\u52A1 '${i}' \u4E2D\u4E0D\u5B58\u5728`),console.log(p.yellow(`\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp ${i} list' \u67E5\u770B\u8BE5\u670D\u52A1\u7684\u6240\u6709\u5DE5\u5177`));return}d.setToolEnabled(i,e,t,s[e].description),n.succeed(`\u6210\u529F${r}\u5DE5\u5177 ${p.cyan(i)}/${p.cyan(e)}`),console.log(),console.log(p.gray("\u{1F4A1} \u63D0\u793A: \u5DE5\u5177\u72B6\u6001\u66F4\u6539\u5C06\u5728\u4E0B\u6B21\u542F\u52A8\u670D\u52A1\u65F6\u751F\u6548"))}catch(o){n.fail(`${r}\u5DE5\u5177\u5931\u8D25`),console.error(p.red(`\u9519\u8BEF: ${o instanceof Error?o.message:String(o)}`)),process.exit(1)}}var lt=g(()=>{"use strict";ie();a(Gt,"getDisplayWidth");a(Xt,"truncateToWidth");a(Ro,"listMcpServers");a(To,"listServerTools");a(Io,"setToolEnabled")});var en={};H(en,{McpCommandHandler:()=>Kt});import xo from"chalk";var Kt,tn=g(()=>{"use strict";ge();Kt=class extends k{static{a(this,"McpCommandHandler")}name="mcp";description="MCP \u670D\u52A1\u548C\u5DE5\u5177\u7BA1\u7406";subcommands=[{name:"list",description:"\u5217\u51FA MCP \u670D\u52A1",options:[{flags:"--tools",description:"\u663E\u793A\u6240\u6709\u670D\u52A1\u7684\u5DE5\u5177\u5217\u8868"}],execute:a(async(e,t)=>{await this.handleList(t)},"execute")},{name:"server",description:"\u7BA1\u7406\u6307\u5B9A\u7684 MCP \u670D\u52A1",execute:a(async(e,t)=>{this.validateArgs(e,1),await this.handleServer(e[0])},"execute")},{name:"tool",description:"\u542F\u7528\u6216\u7981\u7528\u6307\u5B9A\u670D\u52A1\u7684\u5DE5\u5177",execute:a(async(e,t)=>{this.validateArgs(e,3);let[r,n,o]=e;o!=="enable"&&o!=="disable"&&(console.error(xo.red("\u9519\u8BEF: \u64CD\u4F5C\u5FC5\u987B\u662F 'enable' \u6216 'disable'")),process.exit(1));let s=o==="enable";await this.handleTool(r,n,s)},"execute")}];constructor(e){super(e)}async execute(e,t){console.log("MCP \u670D\u52A1\u548C\u5DE5\u5177\u7BA1\u7406\u547D\u4EE4\u3002\u4F7F\u7528 --help \u67E5\u770B\u53EF\u7528\u7684\u5B50\u547D\u4EE4\u3002")}async handleList(e){try{let{listMcpServers:t}=await Promise.resolve().then(()=>(lt(),ct));await t(e)}catch(t){this.handleError(t)}}async handleServer(e){try{let{listServerTools:t}=await Promise.resolve().then(()=>(lt(),ct));await t(e)}catch(t){this.handleError(t)}}async handleTool(e,t,r){try{let{setToolEnabled:n}=await Promise.resolve().then(()=>(lt(),ct));await n(e,t,r)}catch(n){this.handleError(n)}}}});var rn={};H(rn,{EndpointCommandHandler:()=>qt});import we from"chalk";import gt from"ora";var qt,nn=g(()=>{"use strict";ge();qt=class extends k{static{a(this,"EndpointCommandHandler")}name="endpoint";description="\u7BA1\u7406 MCP \u7AEF\u70B9";subcommands=[{name:"list",description:"\u5217\u51FA\u6240\u6709 MCP \u7AEF\u70B9",execute:a(async(e,t)=>{await this.handleList()},"execute")},{name:"add",description:"\u6DFB\u52A0\u65B0\u7684 MCP \u7AEF\u70B9",execute:a(async(e,t)=>{this.validateArgs(e,1),await this.handleAdd(e[0])},"execute")},{name:"remove",description:"\u79FB\u9664\u6307\u5B9A\u7684 MCP \u7AEF\u70B9",execute:a(async(e,t)=>{this.validateArgs(e,1),await this.handleRemove(e[0])},"execute")},{name:"set",description:"\u8BBE\u7F6E MCP \u7AEF\u70B9\uFF08\u53EF\u4EE5\u662F\u5355\u4E2A\u6216\u591A\u4E2A\uFF09",execute:a(async(e,t)=>{this.validateArgs(e,1),await this.handleSet(e)},"execute")}];constructor(e){super(e)}async execute(e,t){console.log("MCP \u7AEF\u70B9\u7BA1\u7406\u547D\u4EE4\u3002\u4F7F\u7528 --help \u67E5\u770B\u53EF\u7528\u7684\u5B50\u547D\u4EE4\u3002")}async handleList(){let e=gt("\u8BFB\u53D6\u7AEF\u70B9\u914D\u7F6E...").start();try{let r=this.getService("configManager").getMcpEndpoints();e.succeed("\u7AEF\u70B9\u5217\u8868"),r.length===0?console.log(we.yellow("\u672A\u914D\u7F6E\u4EFB\u4F55 MCP \u7AEF\u70B9")):(console.log(we.green(`\u5171 ${r.length} \u4E2A\u7AEF\u70B9:`)),r.forEach((n,o)=>{console.log(we.gray(` ${o+1}. ${n}`))}))}catch(t){e.fail(`\u8BFB\u53D6\u7AEF\u70B9\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`),this.handleError(t)}}async handleAdd(e){let t=gt("\u6DFB\u52A0\u7AEF\u70B9...").start();try{let r=this.getService("configManager");r.addMcpEndpoint(e),t.succeed(`\u6210\u529F\u6DFB\u52A0\u7AEF\u70B9: ${e}`);let n=r.getMcpEndpoints();console.log(we.gray(`\u5F53\u524D\u5171 ${n.length} \u4E2A\u7AEF\u70B9`))}catch(r){t.fail(`\u6DFB\u52A0\u7AEF\u70B9\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`),this.handleError(r)}}async handleRemove(e){let t=gt("\u79FB\u9664\u7AEF\u70B9...").start();try{let r=this.getService("configManager");r.removeMcpEndpoint(e),t.succeed(`\u6210\u529F\u79FB\u9664\u7AEF\u70B9: ${e}`);let n=r.getMcpEndpoints();console.log(we.gray(`\u5F53\u524D\u5269\u4F59 ${n.length} \u4E2A\u7AEF\u70B9`))}catch(r){t.fail(`\u79FB\u9664\u7AEF\u70B9\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`),this.handleError(r)}}async handleSet(e){let t=gt("\u8BBE\u7F6E\u7AEF\u70B9...").start();try{let r=this.getService("configManager");if(e.length===1)r.updateMcpEndpoint(e[0]),t.succeed(`\u6210\u529F\u8BBE\u7F6E\u7AEF\u70B9: ${e[0]}`);else{r.updateMcpEndpoint(e),t.succeed(`\u6210\u529F\u8BBE\u7F6E ${e.length} \u4E2A\u7AEF\u70B9`);for(let[n,o]of e.entries())console.log(we.gray(` ${n+1}. ${o}`))}}catch(r){t.fail(`\u8BBE\u7F6E\u7AEF\u70B9\u5931\u8D25: ${r instanceof Error?r.message:String(r)}`),this.handleError(r)}}}});var on={};H(on,{UICommandHandler:()=>Jt});import he from"chalk";import Oo from"ora";var Jt,sn=g(()=>{"use strict";ge();Jt=class extends k{static{a(this,"UICommandHandler")}name="ui";description="\u542F\u52A8\u914D\u7F6E\u7BA1\u7406\u7F51\u9875";constructor(e){super(e)}async execute(e,t){await this.handleUI()}async handleUI(){let e=Oo("\u542F\u52A8 UI \u670D\u52A1...").start();try{let t=this.getService("configManager");if(!t.configExists()){e.fail("\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728"),console.log(he.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u5148\u8FD0\u884C "xiaozhi config init" \u521D\u59CB\u5316\u914D\u7F6E'));return}let{WebServer:r}=await Promise.resolve().then(()=>(jt(),Ht));await new r().start(),e.succeed("UI \u670D\u52A1\u5DF2\u542F\u52A8");let o=t.getWebUIPort();console.log(he.green("\u2705 \u914D\u7F6E\u7BA1\u7406\u7F51\u9875\u5DF2\u542F\u52A8\uFF0C\u53EF\u901A\u8FC7\u4EE5\u4E0B\u5730\u5740\u8BBF\u95EE:")),console.log(he.green(` \u672C\u5730\u8BBF\u95EE: http://localhost:${o}`)),console.log(he.green(` \u7F51\u7EDC\u8BBF\u95EE: http://<\u4F60\u7684IP\u5730\u5740>:${o}`)),console.log(he.yellow("\u{1F4A1} \u63D0\u793A: \u6309 Ctrl+C \u505C\u6B62\u670D\u52A1")),await this.openBrowser(`http://localhost:${o}`)}catch(t){e.fail(`\u542F\u52A8 UI \u670D\u52A1\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`),this.handleError(t)}}async openBrowser(e){try{let{spawn:t}=await import("child_process"),r;process.platform==="darwin"?r=t("open",[e],{detached:!0,stdio:"ignore"}):process.platform==="win32"?r=t("cmd",["/c","start",e],{detached:!0,stdio:"ignore"}):r=t("xdg-open",[e],{detached:!0,stdio:"ignore"}),r.on("error",()=>{console.log(he.gray(`\u{1F4A1} \u63D0\u793A: \u65E0\u6CD5\u81EA\u52A8\u6253\u5F00\u6D4F\u89C8\u5668\uFF0C\u8BF7\u624B\u52A8\u8BBF\u95EE: ${e}`))}),r.unref()}catch{console.log(he.gray(`\u{1F4A1} \u63D0\u793A: \u65E0\u6CD5\u81EA\u52A8\u6253\u5F00\u6D4F\u89C8\u5668\uFF0C\u8BF7\u624B\u52A8\u8BBF\u95EE: ${e}`))}}}});Dt();import ee from"chalk";import{Command as $o}from"commander";yt();var ht=class{constructor(e){this.container=e}static{a(this,"CommandHandlerFactory")}createHandlers(){return[this.createHandler("service"),this.createHandler("config"),this.createHandler("project"),this.createHandler("mcp"),this.createHandler("endpoint"),this.createHandler("ui")]}createHandler(e){switch(e){case"service":return this.createServiceCommandHandler();case"config":return this.createConfigCommandHandler();case"project":return this.createProjectCommandHandler();case"mcp":return this.createMcpCommandHandler();case"endpoint":return this.createEndpointCommandHandler();case"ui":return this.createUICommandHandler();default:throw new Error(`\u672A\u77E5\u7684\u547D\u4EE4\u5904\u7406\u5668\u7C7B\u578B: ${e}`)}}createServiceCommandHandler(){let{ServiceCommandHandler:e}=(Kr(),V(Xr));return new e(this.container)}createConfigCommandHandler(){let{ConfigCommandHandler:e}=(Jr(),V(qr));return new e(this.container)}createProjectCommandHandler(){let{ProjectCommandHandler:e}=(Qr(),V(Zr));return new e(this.container)}createMcpCommandHandler(){let{McpCommandHandler:e}=(tn(),V(en));return new e(this.container)}createEndpointCommandHandler(){let{EndpointCommandHandler:e}=(nn(),V(rn));return new e(this.container)}createUICommandHandler(){let{UICommandHandler:e}=(sn(),V(on));return new e(this.container)}};var pt=class{constructor(e){this.container=e;this.handlerFactory=new ht(e)}static{a(this,"CommandRegistry")}handlers=[];handlerFactory;async registerCommands(e){try{this.registerVersionCommand(e),this.registerHelpCommand(e);let t=this.handlerFactory.createHandlers();for(let r of t)this.registerHandler(r),this.registerCommand(e,r);this.registerLegacyServiceCommands(e,t)}catch(t){X.handle(t)}}registerHandler(e){this.handlers.push(e)}registerCommand(e,t){if(t.subcommands&&t.subcommands.length>0){let r=e.command(t.name).description(t.description);for(let n of t.subcommands){let o=n.name;n.name==="get"?o="get <key>":n.name==="set"&&(o="set <key> <value>");let s=r.command(o).description(n.description);if(n.options)for(let c of n.options)s.option(c.flags,c.description,c.defaultValue);s.action(async(...c)=>{try{let u=c[c.length-1].opts();await n.execute(c.slice(0,-1),u)}catch(l){X.handle(l)}})}r.action(async(...n)=>{try{let s=n[n.length-1].opts();await t.execute(n.slice(0,-1),s)}catch(o){X.handle(o)}})}else{let r=t.name;t.name==="create"&&(r="create <projectName>");let n=e.command(r).description(t.description);if(t.options)for(let o of t.options)n.option(o.flags,o.description,o.defaultValue);n.action(async(...o)=>{try{let c=o[o.length-1].opts();await t.execute(o.slice(0,-1),c)}catch(s){X.handle(s)}})}}registerVersionCommand(e){let t=this.container.get("versionUtils");e.version(t.getVersion(),"-v, --version","\u663E\u793A\u7248\u672C\u4FE1\u606F"),e.option("--info","\u663E\u793A\u8BE6\u7EC6\u4FE1\u606F"),e.option("--version-info","\u663E\u793A\u8BE6\u7EC6\u7248\u672C\u4FE1\u606F")}registerHelpCommand(e){e.helpOption("-h, --help","\u663E\u793A\u5E2E\u52A9\u4FE1\u606F").addHelpText("after",`
|
|
71
|
+
\u793A\u4F8B:
|
|
72
|
+
xiaozhi init # \u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6
|
|
73
|
+
xiaozhi start # \u542F\u52A8\u670D\u52A1
|
|
74
|
+
xiaozhi start -d # \u540E\u53F0\u542F\u52A8\u670D\u52A1
|
|
75
|
+
xiaozhi start -u # \u542F\u52A8\u670D\u52A1\u5E76\u6253\u5F00 Web UI
|
|
76
|
+
xiaozhi start -s 3000 # \u4EE5 MCP Server \u6A21\u5F0F\u542F\u52A8
|
|
77
|
+
xiaozhi stop # \u505C\u6B62\u670D\u52A1
|
|
78
|
+
xiaozhi status # \u68C0\u67E5\u670D\u52A1\u72B6\u6001
|
|
79
|
+
xiaozhi restart -d # \u91CD\u542F\u670D\u52A1\uFF08\u540E\u53F0\u6A21\u5F0F\uFF09
|
|
80
|
+
xiaozhi config mcpEndpoint <url> # \u8BBE\u7F6E MCP \u7AEF\u70B9
|
|
81
|
+
xiaozhi create my-project # \u521B\u5EFA\u9879\u76EE
|
|
82
|
+
xiaozhi mcp list # \u5217\u51FA MCP \u670D\u52A1
|
|
83
|
+
|
|
84
|
+
\u66F4\u591A\u4FE1\u606F\u8BF7\u8BBF\u95EE: https://github.com/your-org/xiaozhi-client
|
|
85
|
+
`)}registerLegacyServiceCommands(e,t){let r=t.find(n=>n.name==="service");if(!(!r||!r.subcommands))for(let n of r.subcommands){let o=e.command(n.name).description(n.description);if(n.options)for(let s of n.options)o.option(s.flags,s.description,s.defaultValue);o.action(async(...s)=>{try{let l=s[s.length-1].opts();await n.execute(s.slice(0,-1),l)}catch(c){X.handle(c)}})}}};var dt=new $o;function Ao(){console.log(ee.blue("\u{1F916} \u5C0F\u667A MCP \u5BA2\u6237\u7AEF")),console.log(),console.log("\u4E00\u4E2A\u5F3A\u5927\u7684 MCP (Model Context Protocol) \u5BA2\u6237\u7AEF\uFF0C\u652F\u6301\u591A\u79CD\u8FDE\u63A5\u65B9\u5F0F\u548C\u670D\u52A1\u7BA1\u7406\u3002"),console.log(),console.log(ee.yellow("\u4E3B\u8981\u529F\u80FD:")),console.log(" \u2022 \u652F\u6301 WebSocket \u548C HTTP \u8FDE\u63A5"),console.log(" \u2022 \u591A MCP \u670D\u52A1\u7BA1\u7406"),console.log(" \u2022 \u5DE5\u5177\u8C03\u7528\u548C\u8D44\u6E90\u8BBF\u95EE"),console.log(" \u2022 \u914D\u7F6E\u7BA1\u7406\u548C\u6A21\u677F\u521B\u5EFA"),console.log(" \u2022 \u540E\u53F0\u670D\u52A1\u548C Web UI"),console.log(),console.log(ee.yellow("\u5FEB\u901F\u5F00\u59CB:")),console.log(" xiaozhi config init # \u521D\u59CB\u5316\u914D\u7F6E"),console.log(" xiaozhi start # \u542F\u52A8\u670D\u52A1"),console.log(" xiaozhi ui # \u542F\u52A8 Web UI"),console.log(),console.log("\u4F7F\u7528 'xiaozhi --help' \u67E5\u770B\u6240\u6709\u53EF\u7528\u547D\u4EE4")}a(Ao,"showHelp");function No(i){let e=i.get("versionUtils"),t=i.get("platformUtils"),r=e.getVersionInfo(),n=t.getSystemInfo();console.log(ee.blue("\u{1F916} \u5C0F\u667A MCP \u5BA2\u6237\u7AEF - \u8BE6\u7EC6\u4FE1\u606F")),console.log(),console.log(ee.green("\u7248\u672C\u4FE1\u606F:")),console.log(` \u540D\u79F0: ${r.name||"xiaozhi"}`),console.log(` \u7248\u672C: ${r.version}`),r.description&&console.log(` \u63CF\u8FF0: ${r.description}`),console.log(),console.log(ee.green("\u7CFB\u7EDF\u4FE1\u606F:")),console.log(` Node.js: ${n.nodeVersion}`),console.log(` \u5E73\u53F0: ${n.platform} ${n.arch}`),n.isContainer&&console.log(" \u73AF\u5883: Container"),console.log(),console.log(ee.green("\u914D\u7F6E\u4FE1\u606F:"));let o=i.get("configManager");if(o.configExists()){let s=o.getConfigPath();console.log(` \u914D\u7F6E\u6587\u4EF6: ${s}`);try{let c=o.getMcpEndpoints();console.log(` MCP \u7AEF\u70B9: ${c.length} \u4E2A`)}catch{console.log(" MCP \u7AEF\u70B9: \u8BFB\u53D6\u5931\u8D25")}}else console.log(" \u914D\u7F6E\u6587\u4EF6: \u672A\u521D\u59CB\u5316")}a(No,"showDetailedInfo");async function Do(){try{if(process.argv.includes("--info")){let t=await ce();No(t),process.exit(0)}if(process.argv.includes("--version-info")){let t=await ce(),r=t.get("versionUtils"),n=t.get("platformUtils"),o=r.getVersionInfo(),s=n.getSystemInfo();console.log(`${o.name||"xiaozhi"} v${o.version}`),o.description&&console.log(o.description),console.log(`Node.js: ${s.nodeVersion}`),console.log(`Platform: ${s.platform} ${s.arch}`),s.isContainer&&console.log("Environment: Container"),process.exit(0)}let i=await ce(),e=new pt(i);dt.name("xiaozhi").description("\u5C0F\u667A MCP \u5BA2\u6237\u7AEF - \u5F3A\u5927\u7684 Model Context Protocol \u5BA2\u6237\u7AEF"),await e.registerCommands(dt),dt.helpOption("-h, --help","\u663E\u793A\u5E2E\u52A9\u4FE1\u606F").addHelpText("after",`
|
|
86
|
+
\u793A\u4F8B:
|
|
87
|
+
xiaozhi config init # \u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6
|
|
88
|
+
xiaozhi start # \u542F\u52A8\u670D\u52A1
|
|
89
|
+
xiaozhi start -d # \u540E\u53F0\u542F\u52A8\u670D\u52A1
|
|
90
|
+
xiaozhi start -u # \u542F\u52A8\u670D\u52A1\u5E76\u6253\u5F00 Web UI
|
|
91
|
+
xiaozhi start -s 3000 # \u4EE5 MCP Server \u6A21\u5F0F\u542F\u52A8
|
|
92
|
+
xiaozhi stop # \u505C\u6B62\u670D\u52A1
|
|
93
|
+
xiaozhi status # \u68C0\u67E5\u670D\u52A1\u72B6\u6001
|
|
94
|
+
xiaozhi restart -d # \u91CD\u542F\u670D\u52A1\uFF08\u540E\u53F0\u6A21\u5F0F\uFF09
|
|
95
|
+
xiaozhi config set mcpEndpoint <url> # \u8BBE\u7F6E MCP \u7AEF\u70B9
|
|
96
|
+
xiaozhi create my-project # \u521B\u5EFA\u9879\u76EE
|
|
97
|
+
xiaozhi mcp list # \u5217\u51FA MCP \u670D\u52A1
|
|
98
|
+
xiaozhi endpoint list # \u5217\u51FA MCP \u7AEF\u70B9
|
|
99
|
+
xiaozhi ui # \u542F\u52A8 Web UI
|
|
100
|
+
|
|
101
|
+
\u66F4\u591A\u4FE1\u606F\u8BF7\u8BBF\u95EE: https://github.com/your-org/xiaozhi-client
|
|
102
|
+
`),process.argv.length<=2&&(Ao(),process.exit(0)),await dt.parseAsync(process.argv)}catch(i){console.error(ee.red("\u7A0B\u5E8F\u542F\u52A8\u5931\u8D25:"),i instanceof Error?i.message:String(i)),process.exit(1)}}a(Do,"main");Do().catch(i=>{console.error(ee.red("\u7A0B\u5E8F\u6267\u884C\u5931\u8D25:"),i instanceof Error?i.message:String(i)),process.exit(1)});
|
|
103
|
+
//# sourceMappingURL=cli.js.map
|