xiaozhi-client 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cli.js +7 -10
- package/dist/cli.js.map +1 -1
- package/dist/logger.d.ts +45 -0
- package/dist/logger.js +3 -0
- package/dist/logger.js.map +1 -0
- package/dist/mcpPipe.d.ts +1 -9
- package/dist/mcpPipe.js +7 -6
- package/dist/mcpPipe.js.map +1 -1
- package/dist/mcpServerProxy.js +7 -6
- package/dist/mcpServerProxy.js.map +1 -1
- package/dist/package.json +2 -1
- package/package.json +2 -1
package/README.md
CHANGED
package/dist/cli.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var q=Object.defineProperty;var g=(n,o)=>q(n,"name",{value:o,configurable:!0});import{spawn as _}from"child_process";import d from"fs";import J from"os";import m from"path";import{fileURLToPath as $}from"url";import i from"chalk";import{Command as so}from"commander";import S from"ora";import to from"omelette";import{copyFileSync as K,existsSync as j,readFileSync as Y,writeFileSync as Q}from"fs";import{dirname as oo,resolve as k}from"path";import{fileURLToPath as eo}from"url";var no=oo(eo(import.meta.url)),E={heartbeatInterval:3e4,heartbeatTimeout:1e4,reconnectInterval:5e3},T=class n{static{g(this,"ConfigManager")}static instance;defaultConfigPath;config=null;constructor(){this.defaultConfigPath=k(no,"xiaozhi.config.default.json")}getConfigFilePath(){let o=process.env.XIAOZHI_CONFIG_DIR||process.cwd();return k(o,"xiaozhi.config.json")}static getInstance(){return n.instance||(n.instance=new n),n.instance}configExists(){let o=this.getConfigFilePath();return j(o)}initConfig(){if(!j(this.defaultConfigPath))throw new Error("\u9ED8\u8BA4\u914D\u7F6E\u6587\u4EF6 xiaozhi.config.default.json \u4E0D\u5B58\u5728");if(this.configExists())throw new Error("\u914D\u7F6E\u6587\u4EF6 xiaozhi.config.json \u5DF2\u5B58\u5728\uFF0C\u65E0\u9700\u91CD\u590D\u521D\u59CB\u5316");let o=this.getConfigFilePath();K(this.defaultConfigPath,o),this.config=null}loadConfig(){if(!this.configExists())throw new Error("\u914D\u7F6E\u6587\u4EF6 xiaozhi.config.json \u4E0D\u5B58\u5728\uFF0C\u8BF7\u5148\u8FD0\u884C xiaozhi init \u521D\u59CB\u5316\u914D\u7F6E");try{let o=this.getConfigFilePath(),e=Y(o,"utf8"),t=JSON.parse(e);return this.validateConfig(t),t}catch(o){throw o instanceof SyntaxError?new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF: ${o.message}`):o}}validateConfig(o){if(!o||typeof o!="object")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1A\u6839\u5BF9\u8C61\u65E0\u6548");let e=o;if(!e.mcpEndpoint||typeof e.mcpEndpoint!="string")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpEndpoint \u5B57\u6BB5\u65E0\u6548");if(!e.mcpServers||typeof e.mcpServers!="object")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers \u5B57\u6BB5\u65E0\u6548");for(let[t,r]of Object.entries(e.mcpServers)){if(!r||typeof r!="object")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${t} \u65E0\u6548`);let c=r;if(!c.command||typeof c.command!="string")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${t}.command \u65E0\u6548`);if(!Array.isArray(c.args))throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${t}.args \u5FC5\u987B\u662F\u6570\u7EC4`);if(c.env&&typeof c.env!="object")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${t}.env \u5FC5\u987B\u662F\u5BF9\u8C61`)}}getConfig(){return this.config||(this.config=this.loadConfig()),JSON.parse(JSON.stringify(this.config))}getMcpEndpoint(){return this.getConfig().mcpEndpoint}getMcpServers(){return this.getConfig().mcpServers}getMcpServerConfig(){return this.getConfig().mcpServerConfig||{}}getServerToolsConfig(o){return this.getMcpServerConfig()[o]?.tools||{}}isToolEnabled(o,e){return this.getServerToolsConfig(o)[e]?.enable!==!1}updateMcpEndpoint(o){if(!o||typeof o!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let t={...this.getConfig(),mcpEndpoint:o};this.saveConfig(t)}updateMcpServer(o,e){if(!o||typeof o!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(!e.command||typeof e.command!="string")throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 command \u5B57\u6BB5\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(!Array.isArray(e.args))throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 args \u5B57\u6BB5\u5FC5\u987B\u662F\u6570\u7EC4");if(e.env&&typeof e.env!="object")throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 env \u5B57\u6BB5\u5FC5\u987B\u662F\u5BF9\u8C61");let t=this.getConfig(),r={...t,mcpServers:{...t.mcpServers,[o]:e}};this.saveConfig(r)}removeMcpServer(o){if(!o||typeof o!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let e=this.getConfig();if(!e.mcpServers[o])throw new Error(`\u670D\u52A1 ${o} \u4E0D\u5B58\u5728`);let t={...e.mcpServers};delete t[o];let r={...e,mcpServers:t};this.saveConfig(r)}updateServerToolsConfig(o,e){let r={...this.getConfig()};r.mcpServerConfig||(r.mcpServerConfig={}),r.mcpServerConfig[o]={tools:e},this.saveConfig(r)}setToolEnabled(o,e,t,r){let s={...this.getConfig()};s.mcpServerConfig||(s.mcpServerConfig={}),s.mcpServerConfig[o]||(s.mcpServerConfig[o]={tools:{}}),s.mcpServerConfig[o].tools[e]={enable:t,...r&&{description:r}},this.saveConfig(s)}saveConfig(o){try{this.validateConfig(o);let e=this.getConfigFilePath(),t=JSON.stringify(o,null,2);Q(e,t,"utf8"),this.config=o}catch(e){throw new Error(`\u4FDD\u5B58\u914D\u7F6E\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}}reloadConfig(){this.config=null}getConfigPath(){return this.getConfigFilePath()}getDefaultConfigPath(){return this.defaultConfigPath}getConnectionConfig(){let e=this.getConfig().connection||{};return{heartbeatInterval:e.heartbeatInterval??E.heartbeatInterval,heartbeatTimeout:e.heartbeatTimeout??E.heartbeatTimeout,reconnectInterval:e.reconnectInterval??E.reconnectInterval}}getHeartbeatInterval(){return this.getConnectionConfig().heartbeatInterval}getHeartbeatTimeout(){return this.getConnectionConfig().heartbeatTimeout}getReconnectInterval(){return this.getConnectionConfig().reconnectInterval}updateConnectionConfig(o){let e=this.getConfig(),r={...e.connection||{},...o},c={...e,connection:r};this.saveConfig(c)}setHeartbeatInterval(o){if(o<=0)throw new Error("\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatInterval:o})}setHeartbeatTimeout(o){if(o<=0)throw new Error("\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatTimeout:o})}setReconnectInterval(o){if(o<=0)throw new Error("\u91CD\u8FDE\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({reconnectInterval:o})}},f=T.getInstance();function ro(){try{if(!f.configExists())return[];let n=f.getMcpServers();return Object.keys(n)}catch{return[]}}g(ro,"getMcpServerNames");function io(n){try{if(!f.configExists())return[];let o=f.getServerToolsConfig(n);return Object.keys(o)}catch{return[]}}g(io,"getServerToolNames");function O(){let n=to("xiaozhi <command>");if(n.on("command",({reply:o})=>{o(["create","init","config","start","stop","status","attach","restart","mcp","completion"])}),n.on("complete",(o,{line:e,before:t,reply:r})=>{process.env.XIAOZHI_DEBUG_COMPLETION&&console.error(`Debug completion - line: "${e}", before: "${t}", fragment: "${o}"`);let c=e.trim().split(/\s+/),a=e!==e.trim()?c.length:c.length-1;if(c[1]==="mcp"){let p=c[2];if(a===2){let u=["list","server","tool"],h=c[2]||"",C=u.filter(b=>b.startsWith(h));r(C);return}if(a===3){switch(p){case"list":{let u=["--tools"],h=c[3]||"",C=u.filter(b=>b.startsWith(h));r(C);break}case"server":case"tool":{let u=ro(),h=c[3]||"",C=u.filter(b=>b.startsWith(h));r(C);break}default:r([])}return}if(a===4&&p==="tool"){let u=c[3],h=io(u),C=c[4]||"",b=h.filter(I=>I.startsWith(C));r(b);return}if(a===5&&p==="tool"){let u=["enable","disable"],h=c[5]||"",C=u.filter(b=>b.startsWith(h));r(C);return}}if(a===2){switch(c[1]){case"create":r(["--template","-t"]);break;case"start":case"restart":r(["--daemon","-d"]);break;case"completion":r(["install","uninstall"]);break;default:r([])}return}r([])}),process.argv.includes("--completion")){try{console.log(n.setupShellInitFile())}catch(o){console.error("\u751F\u6210\u81EA\u52A8\u8865\u5168\u811A\u672C\u65F6\u51FA\u9519:",o)}process.exit(0)}process.argv.includes("--completion-fish")&&(console.log(n.setupShellInitFile("fish")),process.exit(0)),process.argv.includes("--compzsh")||process.argv.includes("--compbash"),n.init()}g(O,"setupAutoCompletion");function D(){console.log("\u{1F680} xiaozhi \u81EA\u52A8\u8865\u5168\u8BBE\u7F6E"),console.log(),console.log("\u8981\u542F\u7528\u81EA\u52A8\u8865\u5168\uFF0C\u8BF7\u6839\u636E\u4F60\u7684shell\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\uFF1A"),console.log(),console.log("\u{1F4DD} Zsh (\u63A8\u8350):"),console.log(" xiaozhi --completion >> ~/.xiaozhi-completion.zsh"),console.log(" echo 'source ~/.xiaozhi-completion.zsh' >> ~/.zshrc"),console.log(" source ~/.zshrc"),console.log(),console.log("\u{1F4DD} Bash:"),console.log(" xiaozhi --completion >> ~/.xiaozhi-completion.bash"),console.log(" echo 'source ~/.xiaozhi-completion.bash' >> ~/.bash_profile"),console.log(" source ~/.bash_profile"),console.log(),console.log("\u{1F4DD} Fish:"),console.log(" xiaozhi --completion-fish >> ~/.config/fish/completions/xiaozhi.fish"),console.log(),console.log("\u2728 \u8BBE\u7F6E\u5B8C\u6210\u540E\uFF0C\u4F60\u5C31\u53EF\u4EE5\u4F7F\u7528 Tab \u952E\u8FDB\u884C\u81EA\u52A8\u8865\u5168\u4E86\uFF01"),console.log(),console.log("\u{1F4A1} \u793A\u4F8B:"),console.log(" xiaozhi m<Tab> # \u2192 mcp"),console.log(" xiaozhi mcp l<Tab> # \u2192 list"),console.log(" xiaozhi mcp tool <Tab> # \u2192 \u663E\u793A\u6240\u6709\u670D\u52A1\u5668\u540D\u79F0")}g(D,"showCompletionHelp");import l from"chalk";import R from"cli-table3";import M from"ora";function N(n){let o=0;for(let e of n)/[\u4e00-\u9fff\u3400-\u4dbf\uff00-\uffef]/.test(e)?o+=2:o+=1;return o}g(N,"getDisplayWidth");function F(n,o){if(N(n)<=o)return n;if(o<=3)return"";let e="",t=0,r=!1;for(let c of n){let s=/[\u4e00-\u9fff\u3400-\u4dbf\uff00-\uffef]/.test(c)?2:1;if(t+s>o-3){if(!r)return"";e+="...";break}e+=c,t+=s,r=!0}return e}g(F,"truncateToWidth");async function A(n={}){let o=M("\u83B7\u53D6 MCP \u670D\u52A1\u5217\u8868...").start();try{let e=f.getMcpServers(),t=Object.keys(e);if(t.length===0){o.warn("\u672A\u914D\u7F6E\u4EFB\u4F55 MCP \u670D\u52A1"),console.log(l.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi config' \u547D\u4EE4\u914D\u7F6E MCP \u670D\u52A1"));return}if(o.succeed(`\u627E\u5230 ${t.length} \u4E2A MCP \u670D\u52A1`),n.tools){console.log(),console.log(l.bold("MCP \u670D\u52A1\u5DE5\u5177\u5217\u8868:")),console.log();let r=8,c=[];for(let a of t){let p=f.getServerToolsConfig(a),u=Object.keys(p);c.push(...u)}for(let a of c){let p=N(a);p>r&&(r=p)}r=Math.max(10,Math.min(r+2,30));let s=new R({head:[l.bold("MCP"),l.bold("\u5DE5\u5177\u540D\u79F0"),l.bold("\u72B6\u6001"),l.bold("\u63CF\u8FF0")],colWidths:[15,r,8,40],wordWrap:!0,style:{head:[],border:[]}});for(let a of t){let p=f.getServerToolsConfig(a),u=Object.keys(p);if(u.length===0)s.push([l.gray(a),l.gray("(\u65E0\u5DE5\u5177)"),l.gray("-"),l.gray("\u8BF7\u5148\u542F\u52A8\u670D\u52A1\u626B\u63CF\u5DE5\u5177")]);else{s.length>0&&s.push([{colSpan:4,content:""}]);for(let h of u){let C=p[h],b=C.enable?l.green("\u542F\u7528"):l.red("\u7981\u7528"),I=F(C.description||"",32);s.push([a,h,b,I])}}}console.log(s.toString())}else{console.log(),console.log(l.bold("MCP \u670D\u52A1\u5217\u8868:")),console.log();for(let r of t){let c=e[r],s=f.getServerToolsConfig(r),a=Object.keys(s).length,p=Object.values(s).filter(u=>u.enable!==!1).length;console.log(`${l.cyan("\u2022")} ${l.bold(r)}`),console.log(` \u547D\u4EE4: ${l.gray(c.command)} ${l.gray(c.args.join(" "))}`),a>0?console.log(` \u5DE5\u5177: ${l.green(p)} \u542F\u7528 / ${l.yellow(a)} \u603B\u8BA1`):console.log(` \u5DE5\u5177: ${l.gray("\u672A\u626B\u63CF (\u8BF7\u5148\u542F\u52A8\u670D\u52A1)")}`),console.log()}}console.log(l.gray("\u{1F4A1} \u63D0\u793A:")),console.log(l.gray(" - \u4F7F\u7528 'xiaozhi mcp list --tools' \u67E5\u770B\u6240\u6709\u5DE5\u5177")),console.log(l.gray(" - \u4F7F\u7528 'xiaozhi mcp <\u670D\u52A1\u540D> list' \u67E5\u770B\u6307\u5B9A\u670D\u52A1\u7684\u5DE5\u5177")),console.log(l.gray(" - \u4F7F\u7528 'xiaozhi mcp <\u670D\u52A1\u540D> <\u5DE5\u5177\u540D> enable/disable' \u542F\u7528/\u7981\u7528\u5DE5\u5177"))}catch(e){o.fail("\u83B7\u53D6 MCP \u670D\u52A1\u5217\u8868\u5931\u8D25"),console.error(l.red(`\u9519\u8BEF: ${e instanceof Error?e.message:String(e)}`)),process.exit(1)}}g(A,"listMcpServers");async function L(n){let o=M(`\u83B7\u53D6 ${n} \u670D\u52A1\u7684\u5DE5\u5177\u5217\u8868...`).start();try{if(!f.getMcpServers()[n]){o.fail(`\u670D\u52A1 '${n}' \u4E0D\u5B58\u5728`),console.log(l.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp list' \u67E5\u770B\u6240\u6709\u53EF\u7528\u670D\u52A1"));return}let t=f.getServerToolsConfig(n),r=Object.keys(t);if(r.length===0){o.warn(`\u670D\u52A1 '${n}' \u6682\u65E0\u5DE5\u5177\u4FE1\u606F`),console.log(l.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u5148\u542F\u52A8\u670D\u52A1\u4EE5\u626B\u63CF\u5DE5\u5177\u5217\u8868"));return}o.succeed(`\u670D\u52A1 '${n}' \u5171\u6709 ${r.length} \u4E2A\u5DE5\u5177`),console.log(),console.log(l.bold(`${n} \u670D\u52A1\u5DE5\u5177\u5217\u8868:`)),console.log();let c=new R({head:[l.bold("\u5DE5\u5177\u540D\u79F0"),l.bold("\u72B6\u6001"),l.bold("\u63CF\u8FF0")],colWidths:[30,8,50],wordWrap:!0,style:{head:[],border:[]}});for(let s of r){let a=t[s],p=a.enable?l.green("\u542F\u7528"):l.red("\u7981\u7528"),u=F(a.description||"",40);c.push([s,p,u])}console.log(c.toString()),console.log(),console.log(l.gray("\u{1F4A1} \u63D0\u793A:")),console.log(l.gray(` - \u4F7F\u7528 'xiaozhi mcp ${n} <\u5DE5\u5177\u540D> enable' \u542F\u7528\u5DE5\u5177`)),console.log(l.gray(` - \u4F7F\u7528 'xiaozhi mcp ${n} <\u5DE5\u5177\u540D> disable' \u7981\u7528\u5DE5\u5177`))}catch(e){o.fail("\u83B7\u53D6\u5DE5\u5177\u5217\u8868\u5931\u8D25"),console.error(l.red(`\u9519\u8BEF: ${e instanceof Error?e.message:String(e)}`)),process.exit(1)}}g(L,"listServerTools");async function W(n,o,e){let t=e?"\u542F\u7528":"\u7981\u7528",r=M(`${t}\u5DE5\u5177 ${n}/${o}...`).start();try{if(!f.getMcpServers()[n]){r.fail(`\u670D\u52A1 '${n}' \u4E0D\u5B58\u5728`),console.log(l.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp list' \u67E5\u770B\u6240\u6709\u53EF\u7528\u670D\u52A1"));return}let s=f.getServerToolsConfig(n);if(!s[o]){r.fail(`\u5DE5\u5177 '${o}' \u5728\u670D\u52A1 '${n}' \u4E2D\u4E0D\u5B58\u5728`),console.log(l.yellow(`\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp ${n} list' \u67E5\u770B\u8BE5\u670D\u52A1\u7684\u6240\u6709\u5DE5\u5177`));return}f.setToolEnabled(n,o,e,s[o].description),r.succeed(`\u6210\u529F${t}\u5DE5\u5177 ${l.cyan(n)}/${l.cyan(o)}`),console.log(),console.log(l.gray("\u{1F4A1} \u63D0\u793A: \u5DE5\u5177\u72B6\u6001\u66F4\u6539\u5C06\u5728\u4E0B\u6B21\u542F\u52A8\u670D\u52A1\u65F6\u751F\u6548"))}catch(c){r.fail(`${t}\u5DE5\u5177\u5931\u8D25`),console.error(l.red(`\u9519\u8BEF: ${c instanceof Error?c.message:String(c)}`)),process.exit(1)}}g(W,"setToolEnabled");var y=new so,U="xiaozhi-mcp-service";function Z(){try{let n=$(import.meta.url),o=m.dirname(n),e=[m.join(o,"..","package.json"),m.join(o,"..","package.json"),m.join(o,"..","..","package.json"),m.join(o,"package.json")];for(let t of e)if(d.existsSync(t)){let r=JSON.parse(d.readFileSync(t,"utf8"));if(r.version)return r.version}return"unknown"}catch(n){return console.warn("Warning: Could not read version from package.json:",n),"unknown"}}g(Z,"getVersion");var w=m.join(J.tmpdir(),`${U}.pid`),v=m.join(J.tmpdir(),`${U}.log`);function P(){try{if(!d.existsSync(w))return{running:!1};let n=d.readFileSync(w,"utf8").trim(),[o,e,t]=n.split("|"),r=Number.parseInt(o);if(Number.isNaN(r))return d.unlinkSync(w),{running:!1};try{process.kill(r,0);let c=Number.parseInt(e),s=co(Date.now()-c);return{running:!0,pid:r,uptime:s,mode:t||"foreground"}}catch{return d.unlinkSync(w),{running:!1}}}catch{return{running:!1}}}g(P,"getServiceStatus");function co(n){let o=Math.floor(n/1e3),e=Math.floor(o/60),t=Math.floor(e/60),r=Math.floor(t/24);return r>0?`${r}\u5929 ${t%24}\u5C0F\u65F6 ${e%60}\u5206\u949F`:t>0?`${t}\u5C0F\u65F6 ${e%60}\u5206\u949F`:e>0?`${e}\u5206\u949F ${o%60}\u79D2`:`${o}\u79D2`}g(co,"formatUptime");function G(n,o){let e=`${n}|${Date.now()}|${o}`;d.writeFileSync(w,e)}g(G,"savePidInfo");function x(){try{d.existsSync(w)&&d.unlinkSync(w)}catch{}}g(x,"cleanupPidFile");function lo(){if(!f.configExists())return console.error(i.red("\u274C \u9519\u8BEF: \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728")),console.log(i.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u8FD0\u884C "xiaozhi init" \u521D\u59CB\u5316\u914D\u7F6E')),!1;try{let n=f.getMcpEndpoint();return!n||n.includes("<\u8BF7\u586B\u5199")?(console.error(i.red("\u274C \u9519\u8BEF: MCP \u7AEF\u70B9\u672A\u914D\u7F6E")),console.log(i.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u8FD0\u884C "xiaozhi config mcpEndpoint <your-endpoint-url>" \u8BBE\u7F6E\u7AEF\u70B9')),!1):!0}catch(n){return console.error(i.red(`\u274C \u9519\u8BEF: \u914D\u7F6E\u6587\u4EF6\u65E0\u6548 - ${n instanceof Error?n.message:String(n)}`)),console.log(i.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u8FD0\u884C "xiaozhi init" \u91CD\u65B0\u521D\u59CB\u5316\u914D\u7F6E')),!1}}g(lo,"checkEnvironment");function ao(){let n=m.dirname($(import.meta.url)),o;return n.includes("js-demo/dist")?o=n:o=[m.join(n,"..","js-demo","dist"),m.join(n,"..","..","js-demo","dist"),m.join(n,"..","..","..","js-demo","dist"),m.join(process.cwd(),"js-demo","dist"),m.join(process.cwd(),"dist")].find(t=>d.existsSync(m.join(t,"mcpPipe.js"))&&d.existsSync(m.join(t,"mcpServerProxy.js")))||n,{command:"node",args:["mcpPipe.js","mcpServerProxy.js"],cwd:o}}g(ao,"getServiceCommand");async function V(n=!1){let o=S("\u68C0\u67E5\u670D\u52A1\u72B6\u6001...").start();try{let e=P();if(e.running){o.fail(`\u670D\u52A1\u5DF2\u7ECF\u5728\u8FD0\u884C (PID: ${e.pid})`);return}if(o.text="\u68C0\u67E5\u73AF\u5883\u914D\u7F6E...",!lo()){o.fail("\u73AF\u5883\u914D\u7F6E\u68C0\u67E5\u5931\u8D25");return}let{command:t,args:r,cwd:c}=ao();if(o.text=`\u542F\u52A8\u670D\u52A1 (${n?"\u540E\u53F0\u6A21\u5F0F":"\u524D\u53F0\u6A21\u5F0F"})...`,n){let s=_(t,r,{cwd:c,detached:!0,stdio:["ignore","pipe","pipe"],env:{...process.env,XIAOZHI_CONFIG_DIR:process.cwd(),XIAOZHI_DAEMON:"true"}});G(s.pid,"daemon");let a=d.createWriteStream(v,{flags:"a"});s.stdout?.pipe(a),s.stderr?.pipe(a),s.on("exit",(p,u)=>{if(p!==0&&p!==null){let h=`
|
|
3
|
-
[${new Date().toISOString()}] \u540E\u53F0\u670D\u52A1\u5F02\u5E38\u9000\u51FA (\u4EE3\u7801: ${p}, \u4FE1\u53F7: ${u})
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
\u670D\u52A1\
|
|
8
|
-
\u670D\u52A1\u5DF2\u505C\u6B62`))}),process.on("SIGINT",()=>{console.log(i.yellow(`
|
|
9
|
-
\u6B63\u5728\u505C\u6B62\u670D\u52A1...`)),s.kill("SIGTERM")}),process.on("SIGTERM",()=>{s.kill("SIGTERM")})}}catch(e){o.fail(`\u542F\u52A8\u670D\u52A1\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}}g(V,"startService");async function X(){let n=S("\u68C0\u67E5\u670D\u52A1\u72B6\u6001...").start();try{let o=P();if(!o.running){n.warn("\u670D\u52A1\u672A\u5728\u8FD0\u884C");return}n.text=`\u505C\u6B62\u670D\u52A1 (PID: ${o.pid})...`;try{process.kill(o.pid,"SIGTERM");let e=0,t=30;for(;e<t;){await new Promise(r=>setTimeout(r,100));try{process.kill(o.pid,0),e++}catch{break}}try{process.kill(o.pid,0),n.text="\u5F3A\u5236\u505C\u6B62\u670D\u52A1...",process.kill(o.pid,"SIGKILL"),await new Promise(r=>setTimeout(r,500))}catch{}x(),n.succeed("\u670D\u52A1\u5DF2\u505C\u6B62")}catch(e){x(),n.fail(`\u505C\u6B62\u670D\u52A1\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}}catch(o){n.fail(`\u505C\u6B62\u670D\u52A1\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`)}}g(X,"stopService");async function go(){let n=S("\u68C0\u67E5\u670D\u52A1\u72B6\u6001...").start();try{let o=P();o.running?(n.succeed("\u670D\u52A1\u72B6\u6001"),console.log(i.green("\u2705 \u670D\u52A1\u6B63\u5728\u8FD0\u884C")),console.log(i.gray(` PID: ${o.pid}`)),console.log(i.gray(` \u8FD0\u884C\u65F6\u95F4: ${o.uptime}`)),console.log(i.gray(` \u8FD0\u884C\u6A21\u5F0F: ${o.mode==="daemon"?"\u540E\u53F0\u6A21\u5F0F":"\u524D\u53F0\u6A21\u5F0F"}`)),o.mode==="daemon"&&console.log(i.gray(` \u65E5\u5FD7\u6587\u4EF6: ${v}`))):(n.succeed("\u670D\u52A1\u72B6\u6001"),console.log(i.red("\u274C \u670D\u52A1\u672A\u8FD0\u884C")))}catch(o){n.fail(`\u68C0\u67E5\u72B6\u6001\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`)}}g(go,"checkStatus");async function fo(){let n=S("\u68C0\u67E5\u670D\u52A1\u72B6\u6001...").start();try{let o=P();if(!o.running){n.fail("\u670D\u52A1\u672A\u5728\u8FD0\u884C");return}if(o.mode!=="daemon"){n.fail("\u670D\u52A1\u4E0D\u662F\u5728\u540E\u53F0\u6A21\u5F0F\u8FD0\u884C");return}if(n.succeed("\u8FDE\u63A5\u5230\u540E\u53F0\u670D\u52A1..."),console.log(i.green(`\u5DF2\u8FDE\u63A5\u5230\u670D\u52A1 (PID: ${o.pid})`)),console.log(i.gray("\u6309 Ctrl+C \u53EF\u4EE5\u65AD\u5F00\u8FDE\u63A5\uFF08\u4E0D\u4F1A\u505C\u6B62\u670D\u52A1\uFF09")),console.log(i.gray("=".repeat(50))),d.existsSync(v))if(process.platform==="win32"){let{spawn:e}=await import("child_process"),t=e("powershell",["-Command",`Get-Content -Path "${v}" -Wait`],{stdio:"inherit"});process.on("SIGINT",()=>{console.log(i.yellow(`
|
|
10
|
-
\u65AD\u5F00\u8FDE\u63A5\uFF0C\u670D\u52A1\u7EE7\u7EED\u5728\u540E\u53F0\u8FD0\u884C`)),t.kill(),process.exit(0)}),t.on("exit",()=>{process.exit(0)})}else{let{spawn:e}=await import("child_process"),t=e("tail",["-f",v],{stdio:"inherit"});process.on("SIGINT",()=>{console.log(i.yellow(`
|
|
11
|
-
\u65AD\u5F00\u8FDE\u63A5\uFF0C\u670D\u52A1\u7EE7\u7EED\u5728\u540E\u53F0\u8FD0\u884C`)),t.kill(),process.exit(0)}),t.on("exit",()=>{process.exit(0)})}else console.log(i.yellow("\u65E5\u5FD7\u6587\u4EF6\u4E0D\u5B58\u5728"))}catch(o){n.fail(`\u8FDE\u63A5\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`)}}g(fo,"attachService");async function po(n=!1){console.log(i.blue("\u{1F504} \u91CD\u542F\u670D\u52A1...")),await X(),await new Promise(o=>setTimeout(o,1e3)),await V(n)}g(po,"restartService");function mo(){let n=Z();console.log(i.blue(`xiaozhi v${n}`)),console.log(i.gray("MCP Calculator Service CLI Tool")),console.log(i.gray("Built with Node.js and TypeScript")),console.log(i.gray(`Node.js: ${process.version}`)),console.log(i.gray(`Platform: ${process.platform} ${process.arch}`))}g(mo,"showDetailedInfo");async function uo(){let n=S("\u521D\u59CB\u5316\u914D\u7F6E...").start();try{if(f.configExists()){n.warn("\u914D\u7F6E\u6587\u4EF6\u5DF2\u5B58\u5728"),console.log(i.yellow("\u5982\u9700\u91CD\u65B0\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u5220\u9664\u73B0\u6709\u7684 xiaozhi.config.json \u6587\u4EF6"));return}f.initConfig(),n.succeed("\u914D\u7F6E\u6587\u4EF6\u521D\u59CB\u5316\u6210\u529F"),console.log(i.green("\u2705 \u914D\u7F6E\u6587\u4EF6\u5DF2\u521B\u5EFA: xiaozhi.config.json")),console.log(i.yellow("\u{1F4DD} \u8BF7\u7F16\u8F91\u914D\u7F6E\u6587\u4EF6\u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9:")),console.log(i.gray(` \u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84: ${f.getConfigPath()}`)),console.log(i.yellow("\u{1F4A1} \u6216\u8005\u4F7F\u7528\u547D\u4EE4\u8BBE\u7F6E:")),console.log(i.gray(" xiaozhi config mcpEndpoint <your-endpoint-url>"))}catch(o){n.fail(`\u521D\u59CB\u5316\u914D\u7F6E\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`)}}g(uo,"initConfig");function ho(){let n=m.dirname($(import.meta.url)),e=[m.join(n,"..","templates"),m.join(n,"templates"),m.join(n,"..","..","templates")].find(t=>d.existsSync(t));return e?d.readdirSync(e).filter(t=>{let r=m.join(e,t);return d.statSync(r).isDirectory()}):[]}g(ho,"getAvailableTemplates");function H(n,o){let e=n.length,t=o.length,r=Array(e+1).fill(null).map(()=>Array(t+1).fill(0));for(let s=0;s<=e;s++)r[s][0]=s;for(let s=0;s<=t;s++)r[0][s]=s;for(let s=1;s<=e;s++)for(let a=1;a<=t;a++)n[s-1]===o[a-1]?r[s][a]=r[s-1][a-1]:r[s][a]=Math.min(r[s-1][a]+1,r[s][a-1]+1,r[s-1][a-1]+1);let c=Math.max(e,t);return c===0?1:(c-r[e][t])/c}g(H,"calculateSimilarity");function Co(n,o){if(o.length===0)return null;let e=o[0],t=H(n.toLowerCase(),e.toLowerCase());for(let r of o.slice(1)){let c=H(n.toLowerCase(),r.toLowerCase());c>t&&(t=c,e=r)}return t>.5?e:null}g(Co,"findSimilarTemplate");async function yo(n){if(!process.stdin.isTTY)return console.log("n (\u975E\u4EA4\u4E92\u5F0F\u73AF\u5883)"),!1;let o=await import("readline");return new Promise(e=>{process.stdout.write(n);let t=o.createInterface({input:process.stdin,output:process.stdout}),r=g(c=>{let s=c.trim().toLowerCase();s==="y"||s==="yes"?(t.close(),e(!0)):s==="n"||s==="no"||s===""?(t.close(),e(!1)):process.stdout.write("\u8BF7\u8F93\u5165 y \u6216 n: ")},"handleInput");t.on("line",r),t.on("SIGINT",()=>{t.close(),e(!1)})})}g(yo,"askUserConfirmation");function bo(n){let o={mcpEndpoint:"<\u8BF7\u586B\u5199\u4F60\u7684\u63A5\u5165\u70B9\u5730\u5740\uFF08\u83B7\u53D6\u5730\u5740\u5728 xiaozhi.me\uFF09>",mcpServers:{}},e=m.join(n,"xiaozhi.config.json");d.writeFileSync(e,JSON.stringify(o,null,2),"utf8")}g(bo,"createBasicConfig");async function vo(n,o){let e=S("\u521D\u59CB\u5316\u9879\u76EE...").start();try{let t=m.join(process.cwd(),n);if(d.existsSync(t)){e.fail(`\u76EE\u5F55 "${n}" \u5DF2\u5B58\u5728`),console.log(i.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u9009\u62E9\u4E0D\u540C\u7684\u9879\u76EE\u540D\u79F0\u6216\u5220\u9664\u73B0\u6709\u76EE\u5F55"));return}if(o.template){e.text="\u68C0\u67E5\u6A21\u677F...";let r=ho();if(r.length===0){e.fail("\u627E\u4E0D\u5230 templates \u76EE\u5F55"),console.log(i.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u786E\u4FDD xiaozhi-client \u6B63\u786E\u5B89\u88C5"));return}if(!r.includes(o.template)){e.fail(`\u6A21\u677F "${o.template}" \u4E0D\u5B58\u5728`);let u=Co(o.template,r);if(u)if(console.log(i.yellow(`\u{1F4A1} \u4F60\u662F\u60F3\u4F7F\u7528\u6A21\u677F "${u}" \u5417\uFF1F`)),await yo(i.cyan("\u786E\u8BA4\u4F7F\u7528\u6B64\u6A21\u677F\uFF1F(y/n): ")))o.template=u;else{console.log(i.yellow("\u53EF\u7528\u7684\u6A21\u677F:"));for(let C of r)console.log(i.gray(` - ${C}`));return}else{console.log(i.yellow("\u53EF\u7528\u7684\u6A21\u677F:"));for(let h of r)console.log(i.gray(` - ${h}`));return}}let c=m.dirname($(import.meta.url)),a=[m.join(c,"..","templates"),m.join(c,"templates"),m.join(c,"..","..","templates")].find(u=>d.existsSync(u)),p=m.join(a,o.template);e.text=`\u4ECE\u6A21\u677F "${o.template}" \u521B\u5EFA\u9879\u76EE "${n}"...`,B(p,t,["node_modules",".pnpm-debug.log","pnpm-lock.yaml"]),e.succeed(`\u9879\u76EE "${n}" \u521B\u5EFA\u6210\u529F`),console.log(i.green("\u2705 \u9879\u76EE\u521B\u5EFA\u5B8C\u6210!")),console.log(i.yellow("\u{1F4DD} \u63A5\u4E0B\u6765\u7684\u6B65\u9AA4:")),console.log(i.gray(` cd ${n}`)),console.log(i.gray(" pnpm install # \u5B89\u88C5\u4F9D\u8D56")),console.log(i.gray(" # \u7F16\u8F91 xiaozhi.config.json \u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9")),console.log(i.gray(" xiaozhi start # \u542F\u52A8\u670D\u52A1"))}else e.text=`\u521B\u5EFA\u57FA\u672C\u9879\u76EE "${n}"...`,d.mkdirSync(t,{recursive:!0}),bo(t),e.succeed(`\u9879\u76EE "${n}" \u521B\u5EFA\u6210\u529F`),console.log(i.green("\u2705 \u57FA\u672C\u9879\u76EE\u521B\u5EFA\u5B8C\u6210!")),console.log(i.yellow("\u{1F4DD} \u63A5\u4E0B\u6765\u7684\u6B65\u9AA4:")),console.log(i.gray(` cd ${n}`)),console.log(i.gray(" # \u7F16\u8F91 xiaozhi.config.json \u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9\u548C\u670D\u52A1")),console.log(i.gray(" xiaozhi start # \u542F\u52A8\u670D\u52A1")),console.log(i.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 --template \u9009\u9879\u53EF\u4EE5\u4ECE\u6A21\u677F\u521B\u5EFA\u9879\u76EE"))}catch(t){e.fail(`\u521B\u5EFA\u9879\u76EE\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`)}}g(vo,"createProject");function B(n,o,e=[]){d.existsSync(o)||d.mkdirSync(o,{recursive:!0});let t=d.readdirSync(n);for(let r of t){if(e.some(p=>r.includes(p)))continue;let c=m.join(n,r),s=m.join(o,r);d.statSync(c).isDirectory()?B(c,s,e):d.copyFileSync(c,s)}}g(B,"copyDirectory");async function wo(n,o){let e=S("\u66F4\u65B0\u914D\u7F6E...").start();try{if(!f.configExists()){e.fail("\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728"),console.log(i.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u5148\u8FD0\u884C "xiaozhi init" \u521D\u59CB\u5316\u914D\u7F6E'));return}if(o)switch(n){case"mcpEndpoint":f.updateMcpEndpoint(o),e.succeed(`MCP \u7AEF\u70B9\u5DF2\u66F4\u65B0\u4E3A: ${o}`);break;case"heartbeatInterval":{let t=Number.parseInt(o,10);if(Number.isNaN(t)||t<=0){e.fail("\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5FC5\u987B\u662F\u5927\u4E8E0\u7684\u6570\u5B57\uFF08\u6BEB\u79D2\uFF09");return}f.setHeartbeatInterval(t),e.succeed(`\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5DF2\u66F4\u65B0\u4E3A: ${t}ms`);break}case"heartbeatTimeout":{let t=Number.parseInt(o,10);if(Number.isNaN(t)||t<=0){e.fail("\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5FC5\u987B\u662F\u5927\u4E8E0\u7684\u6570\u5B57\uFF08\u6BEB\u79D2\uFF09");return}f.setHeartbeatTimeout(t),e.succeed(`\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5DF2\u66F4\u65B0\u4E3A: ${t}ms`);break}case"reconnectInterval":{let t=Number.parseInt(o,10);if(Number.isNaN(t)||t<=0){e.fail("\u91CD\u8FDE\u95F4\u9694\u5FC5\u987B\u662F\u5927\u4E8E0\u7684\u6570\u5B57\uFF08\u6BEB\u79D2\uFF09");return}f.setReconnectInterval(t),e.succeed(`\u91CD\u8FDE\u95F4\u9694\u5DF2\u66F4\u65B0\u4E3A: ${t}ms`);break}default:e.fail(`\u914D\u7F6E\u9879 ${n} \u4E0D\u652F\u6301\u901A\u8FC7\u547D\u4EE4\u884C\u8BBE\u7F6E`),console.log(i.yellow("\u652F\u6301\u8BBE\u7F6E\u7684\u914D\u7F6E\u9879: mcpEndpoint, heartbeatInterval, heartbeatTimeout, reconnectInterval"));return}else{e.text="\u8BFB\u53D6\u914D\u7F6E...";let t=f.getConfig();switch(n){case"mcpEndpoint":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(i.green(`MCP \u7AEF\u70B9: ${t.mcpEndpoint}`));break;case"mcpServers":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(i.green("MCP \u670D\u52A1:"));for(let[r,c]of Object.entries(t.mcpServers))console.log(i.gray(` ${r}: ${c.command} ${c.args.join(" ")}`));break;case"connection":{e.succeed("\u914D\u7F6E\u4FE1\u606F");let r=f.getConnectionConfig();console.log(i.green("\u8FDE\u63A5\u914D\u7F6E:")),console.log(i.gray(` \u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694: ${r.heartbeatInterval}ms`)),console.log(i.gray(` \u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4: ${r.heartbeatTimeout}ms`)),console.log(i.gray(` \u91CD\u8FDE\u95F4\u9694: ${r.reconnectInterval}ms`));break}case"heartbeatInterval":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(i.green(`\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694: ${f.getHeartbeatInterval()}ms`));break;case"heartbeatTimeout":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(i.green(`\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4: ${f.getHeartbeatTimeout()}ms`));break;case"reconnectInterval":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(i.green(`\u91CD\u8FDE\u95F4\u9694: ${f.getReconnectInterval()}ms`));break;default:e.fail(`\u672A\u77E5\u7684\u914D\u7F6E\u9879: ${n}`),console.log(i.yellow("\u652F\u6301\u7684\u914D\u7F6E\u9879: mcpEndpoint, mcpServers, connection, heartbeatInterval, heartbeatTimeout, reconnectInterval"));return}}}catch(t){e.fail(`\u914D\u7F6E\u64CD\u4F5C\u5931\u8D25: ${t instanceof Error?t.message:String(t)}`)}}g(wo,"configCommand");function So(){console.log(i.blue.bold("xiaozhi - MCP Calculator Service CLI")),console.log(),console.log(i.yellow("\u4F7F\u7528\u65B9\u6CD5:")),console.log(" xiaozhi <command> [options]"),console.log(),console.log(i.yellow("\u547D\u4EE4:")),console.log(" create <projectName> \u521B\u5EFA\u9879\u76EE"),console.log(" init \u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6"),console.log(" config <key> [value] \u67E5\u770B\u6216\u8BBE\u7F6E\u914D\u7F6E"),console.log(" start [--daemon] \u542F\u52A8\u670D\u52A1 (--daemon \u540E\u53F0\u8FD0\u884C)"),console.log(" stop \u505C\u6B62\u670D\u52A1"),console.log(" status \u68C0\u67E5\u670D\u52A1\u72B6\u6001"),console.log(" attach \u8FDE\u63A5\u5230\u540E\u53F0\u670D\u52A1\u67E5\u770B\u65E5\u5FD7"),console.log(" restart [--daemon] \u91CD\u542F\u670D\u52A1 (--daemon \u540E\u53F0\u8FD0\u884C)"),console.log(" completion \u663E\u793A\u81EA\u52A8\u8865\u5168\u8BBE\u7F6E\u8BF4\u660E"),console.log(),console.log(i.yellow("\u9009\u9879:")),console.log(" -v, --version \u663E\u793A\u7248\u672C\u4FE1\u606F"),console.log(" -V \u663E\u793A\u8BE6\u7EC6\u4FE1\u606F"),console.log(" -h, --help \u663E\u793A\u5E2E\u52A9\u4FE1\u606F"),console.log(" -t, --template <name> \u6307\u5B9A\u6A21\u677F\u540D\u79F0\uFF08\u7528\u4E8E create \u547D\u4EE4\uFF09"),console.log(),console.log(i.yellow("\u9879\u76EE\u793A\u4F8B:")),console.log(" xiaozhi create my-app # \u521B\u5EFA\u57FA\u672C\u9879\u76EE"),console.log(" xiaozhi create my-app -t hello-world # \u4F7F\u7528 hello-world \u6A21\u677F"),console.log(" xiaozhi create my-app --template hello-world # \u540C\u4E0A\uFF0C\u5B8C\u6574\u9009\u9879\u540D"),console.log(),console.log(i.yellow("\u914D\u7F6E\u793A\u4F8B:")),console.log(" xiaozhi init # \u521D\u59CB\u5316\u914D\u7F6E"),console.log(" xiaozhi config mcpEndpoint # \u67E5\u770B MCP \u7AEF\u70B9"),console.log(" xiaozhi config mcpEndpoint wss://... # \u8BBE\u7F6E MCP \u7AEF\u70B9"),console.log(),console.log(i.yellow("\u670D\u52A1\u793A\u4F8B:")),console.log(" xiaozhi start # \u524D\u53F0\u542F\u52A8\u670D\u52A1"),console.log(" xiaozhi start --daemon # \u540E\u53F0\u542F\u52A8\u670D\u52A1"),console.log(" xiaozhi status # \u68C0\u67E5\u670D\u52A1\u72B6\u6001"),console.log(" xiaozhi attach # \u67E5\u770B\u540E\u53F0\u670D\u52A1\u65E5\u5FD7"),console.log(" xiaozhi stop # \u505C\u6B62\u670D\u52A1"),console.log(),console.log(i.yellow("MCP \u7BA1\u7406\u793A\u4F8B:")),console.log(" xiaozhi mcp list # \u5217\u51FA\u6240\u6709 MCP \u670D\u52A1"),console.log(" xiaozhi mcp list --tools # \u5217\u51FA\u6240\u6709\u670D\u52A1\u7684\u5DE5\u5177"),console.log(" xiaozhi mcp server <name> # \u5217\u51FA\u6307\u5B9A\u670D\u52A1\u7684\u5DE5\u5177"),console.log(" xiaozhi mcp tool <server> <tool> enable # \u542F\u7528\u5DE5\u5177"),console.log(" xiaozhi mcp tool <server> <tool> disable # \u7981\u7528\u5DE5\u5177"),console.log(),console.log(i.yellow("\u81EA\u52A8\u8865\u5168:")),console.log(" xiaozhi completion # \u663E\u793A\u81EA\u52A8\u8865\u5168\u8BBE\u7F6E\u8BF4\u660E"),console.log(" # \u8BBE\u7F6E\u540E\u53EF\u4F7F\u7528 Tab \u952E\u8FDB\u884C\u547D\u4EE4\u3001\u53C2\u6570\u81EA\u52A8\u8865\u5168")}g(So,"showHelp");y.name("xiaozhi").description("MCP Calculator Service CLI Tool").version(Z(),"-v, --version","\u663E\u793A\u7248\u672C\u4FE1\u606F").helpOption("-h, --help","\u663E\u793A\u5E2E\u52A9\u4FE1\u606F");y.command("create <projectName>").description("\u521B\u5EFA\u9879\u76EE").option("-t, --template <templateName>","\u4F7F\u7528\u6307\u5B9A\u6A21\u677F\u521B\u5EFA\u9879\u76EE").action(async(n,o)=>{await vo(n,o)});y.command("init").description("\u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6").action(async()=>{await uo()});y.command("config <key> [value]").description("\u67E5\u770B\u6216\u8BBE\u7F6E\u914D\u7F6E").action(async(n,o)=>{await wo(n,o)});y.command("start").description("\u542F\u52A8\u670D\u52A1").option("-d, --daemon","\u5728\u540E\u53F0\u8FD0\u884C\u670D\u52A1").action(async n=>{await V(n.daemon)});y.command("stop").description("\u505C\u6B62\u670D\u52A1").action(async()=>{await X()});y.command("status").description("\u68C0\u67E5\u670D\u52A1\u72B6\u6001").action(async()=>{await go()});y.command("attach").description("\u8FDE\u63A5\u5230\u540E\u53F0\u670D\u52A1\u67E5\u770B\u65E5\u5FD7").action(async()=>{await fo()});y.command("restart").description("\u91CD\u542F\u670D\u52A1").option("-d, --daemon","\u5728\u540E\u53F0\u8FD0\u884C\u670D\u52A1").action(async n=>{await po(n.daemon)});var z=y.command("mcp").description("MCP \u670D\u52A1\u548C\u5DE5\u5177\u7BA1\u7406");z.command("list").description("\u5217\u51FA MCP \u670D\u52A1").option("--tools","\u663E\u793A\u6240\u6709\u670D\u52A1\u7684\u5DE5\u5177\u5217\u8868").action(async n=>{await A(n)});z.command("server <serverName>").description("\u7BA1\u7406\u6307\u5B9A\u7684 MCP \u670D\u52A1").action(async n=>{await L(n)});z.command("tool <serverName> <toolName> <action>").description("\u542F\u7528\u6216\u7981\u7528\u6307\u5B9A\u670D\u52A1\u7684\u5DE5\u5177").action(async(n,o,e)=>{e!=="enable"&&e!=="disable"&&(console.error(i.red("\u9519\u8BEF: \u64CD\u4F5C\u5FC5\u987B\u662F 'enable' \u6216 'disable'")),process.exit(1)),await W(n,o,e==="enable")});y.command("completion").description("\u663E\u793A\u81EA\u52A8\u8865\u5168\u8BBE\u7F6E\u8BF4\u660E").action(async()=>{D()});y.option("-V","\u663E\u793A\u8BE6\u7EC6\u4FE1\u606F").action(n=>{n.V&&(mo(),process.exit(0))});O();process.argv.length<=2&&(So(),process.exit(0));y.parse(process.argv);export{H as calculateSimilarity,lo as checkEnvironment,co as formatUptime,P as getServiceStatus,Z as getVersion};
|
|
2
|
+
var Y=Object.defineProperty;var a=(t,o)=>Y(t,"name",{value:o,configurable:!0});import{spawn as U}from"child_process";import u from"fs";import go from"os";import f from"path";import{fileURLToPath as I}from"url";import r from"chalk";import{Command as fo}from"commander";import w from"ora";import io from"omelette";import{copyFileSync as K,existsSync as R,readFileSync as Q,writeFileSync as oo}from"fs";import{dirname as eo,resolve as D}from"path";import{fileURLToPath as to}from"url";var no=eo(to(import.meta.url)),M={heartbeatInterval:3e4,heartbeatTimeout:1e4,reconnectInterval:5e3},z=class t{static{a(this,"ConfigManager")}static instance;defaultConfigPath;config=null;constructor(){this.defaultConfigPath=D(no,"xiaozhi.config.default.json")}getConfigFilePath(){let o=process.env.XIAOZHI_CONFIG_DIR||process.cwd();return D(o,"xiaozhi.config.json")}static getInstance(){return t.instance||(t.instance=new t),t.instance}configExists(){let o=this.getConfigFilePath();return R(o)}initConfig(){if(!R(this.defaultConfigPath))throw new Error("\u9ED8\u8BA4\u914D\u7F6E\u6587\u4EF6 xiaozhi.config.default.json \u4E0D\u5B58\u5728");if(this.configExists())throw new Error("\u914D\u7F6E\u6587\u4EF6 xiaozhi.config.json \u5DF2\u5B58\u5728\uFF0C\u65E0\u9700\u91CD\u590D\u521D\u59CB\u5316");let o=this.getConfigFilePath();K(this.defaultConfigPath,o),this.config=null}loadConfig(){if(!this.configExists())throw new Error("\u914D\u7F6E\u6587\u4EF6 xiaozhi.config.json \u4E0D\u5B58\u5728\uFF0C\u8BF7\u5148\u8FD0\u884C xiaozhi init \u521D\u59CB\u5316\u914D\u7F6E");try{let o=this.getConfigFilePath(),e=Q(o,"utf8"),n=JSON.parse(e);return this.validateConfig(n),n}catch(o){throw o instanceof SyntaxError?new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF: ${o.message}`):o}}validateConfig(o){if(!o||typeof o!="object")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1A\u6839\u5BF9\u8C61\u65E0\u6548");let e=o;if(!e.mcpEndpoint||typeof e.mcpEndpoint!="string")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpEndpoint \u5B57\u6BB5\u65E0\u6548");if(!e.mcpServers||typeof e.mcpServers!="object")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers \u5B57\u6BB5\u65E0\u6548");for(let[n,i]of Object.entries(e.mcpServers)){if(!i||typeof i!="object")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${n} \u65E0\u6548`);let c=i;if(!c.command||typeof c.command!="string")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${n}.command \u65E0\u6548`);if(!Array.isArray(c.args))throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${n}.args \u5FC5\u987B\u662F\u6570\u7EC4`);if(c.env&&typeof c.env!="object")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${n}.env \u5FC5\u987B\u662F\u5BF9\u8C61`)}}getConfig(){return this.config||(this.config=this.loadConfig()),JSON.parse(JSON.stringify(this.config))}getMcpEndpoint(){return this.getConfig().mcpEndpoint}getMcpServers(){return this.getConfig().mcpServers}getMcpServerConfig(){return this.getConfig().mcpServerConfig||{}}getServerToolsConfig(o){return this.getMcpServerConfig()[o]?.tools||{}}isToolEnabled(o,e){return this.getServerToolsConfig(o)[e]?.enable!==!1}updateMcpEndpoint(o){if(!o||typeof o!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let n={...this.getConfig(),mcpEndpoint:o};this.saveConfig(n)}updateMcpServer(o,e){if(!o||typeof o!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(!e.command||typeof e.command!="string")throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 command \u5B57\u6BB5\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(!Array.isArray(e.args))throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 args \u5B57\u6BB5\u5FC5\u987B\u662F\u6570\u7EC4");if(e.env&&typeof e.env!="object")throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 env \u5B57\u6BB5\u5FC5\u987B\u662F\u5BF9\u8C61");let n=this.getConfig(),i={...n,mcpServers:{...n.mcpServers,[o]:e}};this.saveConfig(i)}removeMcpServer(o){if(!o||typeof o!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let e=this.getConfig();if(!e.mcpServers[o])throw new Error(`\u670D\u52A1 ${o} \u4E0D\u5B58\u5728`);let n={...e.mcpServers};delete n[o];let i={...e,mcpServers:n};this.saveConfig(i)}updateServerToolsConfig(o,e){let i={...this.getConfig()};i.mcpServerConfig||(i.mcpServerConfig={}),i.mcpServerConfig[o]={tools:e},this.saveConfig(i)}setToolEnabled(o,e,n,i){let s={...this.getConfig()};s.mcpServerConfig||(s.mcpServerConfig={}),s.mcpServerConfig[o]||(s.mcpServerConfig[o]={tools:{}}),s.mcpServerConfig[o].tools[e]={enable:n,...i&&{description:i}},this.saveConfig(s)}saveConfig(o){try{this.validateConfig(o);let e=this.getConfigFilePath(),n=JSON.stringify(o,null,2);oo(e,n,"utf8"),this.config=o}catch(e){throw new Error(`\u4FDD\u5B58\u914D\u7F6E\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}}reloadConfig(){this.config=null}getConfigPath(){return this.getConfigFilePath()}getDefaultConfigPath(){return this.defaultConfigPath}getConnectionConfig(){let e=this.getConfig().connection||{};return{heartbeatInterval:e.heartbeatInterval??M.heartbeatInterval,heartbeatTimeout:e.heartbeatTimeout??M.heartbeatTimeout,reconnectInterval:e.reconnectInterval??M.reconnectInterval}}getHeartbeatInterval(){return this.getConnectionConfig().heartbeatInterval}getHeartbeatTimeout(){return this.getConnectionConfig().heartbeatTimeout}getReconnectInterval(){return this.getConnectionConfig().reconnectInterval}updateConnectionConfig(o){let e=this.getConfig(),i={...e.connection||{},...o},c={...e,connection:i};this.saveConfig(c)}setHeartbeatInterval(o){if(o<=0)throw new Error("\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatInterval:o})}setHeartbeatTimeout(o){if(o<=0)throw new Error("\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatTimeout:o})}setReconnectInterval(o){if(o<=0)throw new Error("\u91CD\u8FDE\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({reconnectInterval:o})}},p=z.getInstance();function ro(){try{if(!p.configExists())return[];let t=p.getMcpServers();return Object.keys(t)}catch{return[]}}a(ro,"getMcpServerNames");function so(t){try{if(!p.configExists())return[];let o=p.getServerToolsConfig(t);return Object.keys(o)}catch{return[]}}a(so,"getServerToolNames");function O(){let t=io("xiaozhi <command>");if(t.on("command",({reply:o})=>{o(["create","init","config","start","stop","status","attach","restart","mcp","completion"])}),t.on("complete",(o,{line:e,before:n,reply:i})=>{process.env.XIAOZHI_DEBUG_COMPLETION&&console.error(`Debug completion - line: "${e}", before: "${n}", fragment: "${o}"`);let c=e.trim().split(/\s+/),l=e!==e.trim()?c.length:c.length-1;if(c[1]==="mcp"){let h=c[2];if(l===2){let m=["list","server","tool"],d=c[2]||"",y=m.filter(C=>C.startsWith(d));i(y);return}if(l===3){switch(h){case"list":{let m=["--tools"],d=c[3]||"",y=m.filter(C=>C.startsWith(d));i(y);break}case"server":case"tool":{let m=ro(),d=c[3]||"",y=m.filter(C=>C.startsWith(d));i(y);break}default:i([])}return}if(l===4&&h==="tool"){let m=c[3],d=so(m),y=c[4]||"",C=d.filter(T=>T.startsWith(y));i(C);return}if(l===5&&h==="tool"){let m=["enable","disable"],d=c[5]||"",y=m.filter(C=>C.startsWith(d));i(y);return}}if(l===2){switch(c[1]){case"create":i(["--template","-t"]);break;case"start":case"restart":i(["--daemon","-d"]);break;case"completion":i(["install","uninstall"]);break;default:i([])}return}i([])}),process.argv.includes("--completion")){try{console.log(t.setupShellInitFile())}catch(o){console.error("\u751F\u6210\u81EA\u52A8\u8865\u5168\u811A\u672C\u65F6\u51FA\u9519:",o)}process.exit(0)}process.argv.includes("--completion-fish")&&(console.log(t.setupShellInitFile("fish")),process.exit(0)),process.argv.includes("--compzsh")||process.argv.includes("--compbash"),t.init()}a(O,"setupAutoCompletion");function N(){console.log("\u{1F680} xiaozhi \u81EA\u52A8\u8865\u5168\u8BBE\u7F6E"),console.log(),console.log("\u8981\u542F\u7528\u81EA\u52A8\u8865\u5168\uFF0C\u8BF7\u6839\u636E\u4F60\u7684shell\u6267\u884C\u4EE5\u4E0B\u547D\u4EE4\uFF1A"),console.log(),console.log("\u{1F4DD} Zsh (\u63A8\u8350):"),console.log(" xiaozhi --completion >> ~/.xiaozhi-completion.zsh"),console.log(" echo 'source ~/.xiaozhi-completion.zsh' >> ~/.zshrc"),console.log(" source ~/.zshrc"),console.log(),console.log("\u{1F4DD} Bash:"),console.log(" xiaozhi --completion >> ~/.xiaozhi-completion.bash"),console.log(" echo 'source ~/.xiaozhi-completion.bash' >> ~/.bash_profile"),console.log(" source ~/.bash_profile"),console.log(),console.log("\u{1F4DD} Fish:"),console.log(" xiaozhi --completion-fish >> ~/.config/fish/completions/xiaozhi.fish"),console.log(),console.log("\u2728 \u8BBE\u7F6E\u5B8C\u6210\u540E\uFF0C\u4F60\u5C31\u53EF\u4EE5\u4F7F\u7528 Tab \u952E\u8FDB\u884C\u81EA\u52A8\u8865\u5168\u4E86\uFF01"),console.log(),console.log("\u{1F4A1} \u793A\u4F8B:"),console.log(" xiaozhi m<Tab> # \u2192 mcp"),console.log(" xiaozhi mcp l<Tab> # \u2192 list"),console.log(" xiaozhi mcp tool <Tab> # \u2192 \u663E\u793A\u6240\u6709\u670D\u52A1\u5668\u540D\u79F0")}a(N,"showCompletionHelp");import P from"fs";import co from"path";import S from"chalk";import{createConsola as lo}from"consola";function ao(t){let o=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),i=String(t.getHours()).padStart(2,"0"),c=String(t.getMinutes()).padStart(2,"0"),s=String(t.getSeconds()).padStart(2,"0");return`${o}-${e}-${n} ${i}:${c}:${s}`}a(ao,"formatDateTime");var j=class{static{a(this,"Logger")}logFilePath=null;writeStream=null;consolaInstance;constructor(){this.consolaInstance=lo({formatOptions:{date:!1,colors:!0,compact:!0},fancy:!1}),this.consolaInstance.setReporters([{log:a(o=>{let e={info:"INFO",success:"SUCCESS",warn:"WARN",error:"ERROR",debug:"DEBUG",log:"LOG"},n={info:S.blue,success:S.green,warn:S.yellow,error:S.red,debug:S.gray,log:a(m=>m,"log")},i=e[o.type]||o.type.toUpperCase(),c=n[o.type]||(m=>m),s=ao(new Date),l=c(`[${i}]`),h=`[${s}] ${l} ${o.args.join(" ")}`;console.error(h)},"log")}])}initLogFile(o){this.logFilePath=co.join(o,"xiaozhi.log"),P.existsSync(this.logFilePath)||P.writeFileSync(this.logFilePath,""),this.writeStream=P.createWriteStream(this.logFilePath,{flags:"a",encoding:"utf8"})}logToFile(o,e,...n){if(this.writeStream){let c=`[${new Date().toISOString()}] [${o.toUpperCase()}] ${e}`,s=n.length>0?`${c} ${n.map(l=>typeof l=="object"?JSON.stringify(l):String(l)).join(" ")}`:c;this.writeStream.write(`${s}
|
|
3
|
+
`)}}enableFileLogging(o){o&&!this.writeStream&&this.logFilePath?this.writeStream=P.createWriteStream(this.logFilePath,{flags:"a",encoding:"utf8"}):!o&&this.writeStream&&(this.writeStream.end(),this.writeStream=null)}info(o,...e){this.consolaInstance.info(o,...e),this.logToFile("info",o,...e)}success(o,...e){this.consolaInstance.success(o,...e),this.logToFile("success",o,...e)}warn(o,...e){this.consolaInstance.warn(o,...e),this.logToFile("warn",o,...e)}error(o,...e){this.consolaInstance.error(o,...e),this.logToFile("error",o,...e)}debug(o,...e){this.consolaInstance.debug(o,...e),this.logToFile("debug",o,...e)}log(o,...e){this.consolaInstance.log(o,...e),this.logToFile("log",o,...e)}withTag(o){return this}close(){this.writeStream&&(this.writeStream.end(),this.writeStream=null)}},x=new j;import g from"chalk";import A from"cli-table3";import k from"ora";function L(t){let o=0;for(let e of t)/[\u4e00-\u9fff\u3400-\u4dbf\uff00-\uffef]/.test(e)?o+=2:o+=1;return o}a(L,"getDisplayWidth");function W(t,o){if(L(t)<=o)return t;if(o<=3)return"";let e="",n=0,i=!1;for(let c of t){let s=/[\u4e00-\u9fff\u3400-\u4dbf\uff00-\uffef]/.test(c)?2:1;if(n+s>o-3){if(!i)return"";e+="...";break}e+=c,n+=s,i=!0}return e}a(W,"truncateToWidth");async function G(t={}){let o=k("\u83B7\u53D6 MCP \u670D\u52A1\u5217\u8868...").start();try{let e=p.getMcpServers(),n=Object.keys(e);if(n.length===0){o.warn("\u672A\u914D\u7F6E\u4EFB\u4F55 MCP \u670D\u52A1"),console.log(g.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi config' \u547D\u4EE4\u914D\u7F6E MCP \u670D\u52A1"));return}if(o.succeed(`\u627E\u5230 ${n.length} \u4E2A MCP \u670D\u52A1`),t.tools){console.log(),console.log(g.bold("MCP \u670D\u52A1\u5DE5\u5177\u5217\u8868:")),console.log();let i=8,c=[];for(let l of n){let h=p.getServerToolsConfig(l),m=Object.keys(h);c.push(...m)}for(let l of c){let h=L(l);h>i&&(i=h)}i=Math.max(10,Math.min(i+2,30));let s=new A({head:[g.bold("MCP"),g.bold("\u5DE5\u5177\u540D\u79F0"),g.bold("\u72B6\u6001"),g.bold("\u63CF\u8FF0")],colWidths:[15,i,8,40],wordWrap:!0,style:{head:[],border:[]}});for(let l of n){let h=p.getServerToolsConfig(l),m=Object.keys(h);if(m.length===0)s.push([g.gray(l),g.gray("(\u65E0\u5DE5\u5177)"),g.gray("-"),g.gray("\u8BF7\u5148\u542F\u52A8\u670D\u52A1\u626B\u63CF\u5DE5\u5177")]);else{s.length>0&&s.push([{colSpan:4,content:""}]);for(let d of m){let y=h[d],C=y.enable?g.green("\u542F\u7528"):g.red("\u7981\u7528"),T=W(y.description||"",32);s.push([l,d,C,T])}}}console.log(s.toString())}else{console.log(),console.log(g.bold("MCP \u670D\u52A1\u5217\u8868:")),console.log();for(let i of n){let c=e[i],s=p.getServerToolsConfig(i),l=Object.keys(s).length,h=Object.values(s).filter(m=>m.enable!==!1).length;console.log(`${g.cyan("\u2022")} ${g.bold(i)}`),console.log(` \u547D\u4EE4: ${g.gray(c.command)} ${g.gray(c.args.join(" "))}`),l>0?console.log(` \u5DE5\u5177: ${g.green(h)} \u542F\u7528 / ${g.yellow(l)} \u603B\u8BA1`):console.log(` \u5DE5\u5177: ${g.gray("\u672A\u626B\u63CF (\u8BF7\u5148\u542F\u52A8\u670D\u52A1)")}`),console.log()}}console.log(g.gray("\u{1F4A1} \u63D0\u793A:")),console.log(g.gray(" - \u4F7F\u7528 'xiaozhi mcp list --tools' \u67E5\u770B\u6240\u6709\u5DE5\u5177")),console.log(g.gray(" - \u4F7F\u7528 'xiaozhi mcp <\u670D\u52A1\u540D> list' \u67E5\u770B\u6307\u5B9A\u670D\u52A1\u7684\u5DE5\u5177")),console.log(g.gray(" - \u4F7F\u7528 'xiaozhi mcp <\u670D\u52A1\u540D> <\u5DE5\u5177\u540D> enable/disable' \u542F\u7528/\u7981\u7528\u5DE5\u5177"))}catch(e){o.fail("\u83B7\u53D6 MCP \u670D\u52A1\u5217\u8868\u5931\u8D25"),console.error(g.red(`\u9519\u8BEF: ${e instanceof Error?e.message:String(e)}`)),process.exit(1)}}a(G,"listMcpServers");async function H(t){let o=k(`\u83B7\u53D6 ${t} \u670D\u52A1\u7684\u5DE5\u5177\u5217\u8868...`).start();try{if(!p.getMcpServers()[t]){o.fail(`\u670D\u52A1 '${t}' \u4E0D\u5B58\u5728`),console.log(g.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp list' \u67E5\u770B\u6240\u6709\u53EF\u7528\u670D\u52A1"));return}let n=p.getServerToolsConfig(t),i=Object.keys(n);if(i.length===0){o.warn(`\u670D\u52A1 '${t}' \u6682\u65E0\u5DE5\u5177\u4FE1\u606F`),console.log(g.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u5148\u542F\u52A8\u670D\u52A1\u4EE5\u626B\u63CF\u5DE5\u5177\u5217\u8868"));return}o.succeed(`\u670D\u52A1 '${t}' \u5171\u6709 ${i.length} \u4E2A\u5DE5\u5177`),console.log(),console.log(g.bold(`${t} \u670D\u52A1\u5DE5\u5177\u5217\u8868:`)),console.log();let c=new A({head:[g.bold("\u5DE5\u5177\u540D\u79F0"),g.bold("\u72B6\u6001"),g.bold("\u63CF\u8FF0")],colWidths:[30,8,50],wordWrap:!0,style:{head:[],border:[]}});for(let s of i){let l=n[s],h=l.enable?g.green("\u542F\u7528"):g.red("\u7981\u7528"),m=W(l.description||"",40);c.push([s,h,m])}console.log(c.toString()),console.log(),console.log(g.gray("\u{1F4A1} \u63D0\u793A:")),console.log(g.gray(` - \u4F7F\u7528 'xiaozhi mcp ${t} <\u5DE5\u5177\u540D> enable' \u542F\u7528\u5DE5\u5177`)),console.log(g.gray(` - \u4F7F\u7528 'xiaozhi mcp ${t} <\u5DE5\u5177\u540D> disable' \u7981\u7528\u5DE5\u5177`))}catch(e){o.fail("\u83B7\u53D6\u5DE5\u5177\u5217\u8868\u5931\u8D25"),console.error(g.red(`\u9519\u8BEF: ${e instanceof Error?e.message:String(e)}`)),process.exit(1)}}a(H,"listServerTools");async function _(t,o,e){let n=e?"\u542F\u7528":"\u7981\u7528",i=k(`${n}\u5DE5\u5177 ${t}/${o}...`).start();try{if(!p.getMcpServers()[t]){i.fail(`\u670D\u52A1 '${t}' \u4E0D\u5B58\u5728`),console.log(g.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp list' \u67E5\u770B\u6240\u6709\u53EF\u7528\u670D\u52A1"));return}let s=p.getServerToolsConfig(t);if(!s[o]){i.fail(`\u5DE5\u5177 '${o}' \u5728\u670D\u52A1 '${t}' \u4E2D\u4E0D\u5B58\u5728`),console.log(g.yellow(`\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 'xiaozhi mcp ${t} list' \u67E5\u770B\u8BE5\u670D\u52A1\u7684\u6240\u6709\u5DE5\u5177`));return}p.setToolEnabled(t,o,e,s[o].description),i.succeed(`\u6210\u529F${n}\u5DE5\u5177 ${g.cyan(t)}/${g.cyan(o)}`),console.log(),console.log(g.gray("\u{1F4A1} \u63D0\u793A: \u5DE5\u5177\u72B6\u6001\u66F4\u6539\u5C06\u5728\u4E0B\u6B21\u542F\u52A8\u670D\u52A1\u65F6\u751F\u6548"))}catch(c){i.fail(`${n}\u5DE5\u5177\u5931\u8D25`),console.error(g.red(`\u9519\u8BEF: ${c instanceof Error?c.message:String(c)}`)),process.exit(1)}}a(_,"setToolEnabled");var b=new fo,po="xiaozhi-mcp-service";function B(){try{let t=I(import.meta.url),o=f.dirname(t),e=[f.join(o,"..","package.json"),f.join(o,"..","package.json"),f.join(o,"..","..","package.json"),f.join(o,"package.json")];for(let n of e)if(u.existsSync(n)){let i=JSON.parse(u.readFileSync(n,"utf8"));if(i.version)return i.version}return"unknown"}catch(t){return console.warn("\u65E0\u6CD5\u4ECE package.json \u8BFB\u53D6\u7248\u672C\u4FE1\u606F:",t),"unknown"}}a(B,"getVersion");var v=f.join(go.tmpdir(),`${po}.pid`);function E(){try{if(!u.existsSync(v))return{running:!1};let t=u.readFileSync(v,"utf8").trim(),[o,e,n]=t.split("|"),i=Number.parseInt(o);if(Number.isNaN(i))return u.unlinkSync(v),{running:!1};try{process.kill(i,0);let c=Number.parseInt(e),s=mo(Date.now()-c);return{running:!0,pid:i,uptime:s,mode:n||"foreground"}}catch{return u.unlinkSync(v),{running:!1}}}catch{return{running:!1}}}a(E,"getServiceStatus");function mo(t){let o=Math.floor(t/1e3),e=Math.floor(o/60),n=Math.floor(e/60),i=Math.floor(n/24);return i>0?`${i}\u5929 ${n%24}\u5C0F\u65F6 ${e%60}\u5206\u949F`:n>0?`${n}\u5C0F\u65F6 ${e%60}\u5206\u949F`:e>0?`${e}\u5206\u949F ${o%60}\u79D2`:`${o}\u79D2`}a(mo,"formatUptime");function J(t,o){let e=`${t}|${Date.now()}|${o}`;u.writeFileSync(v,e)}a(J,"savePidInfo");function $(){try{u.existsSync(v)&&u.unlinkSync(v)}catch{}}a($,"cleanupPidFile");function uo(){if(!p.configExists())return console.error(r.red("\u274C \u9519\u8BEF: \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728")),console.log(r.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u8FD0\u884C "xiaozhi init" \u521D\u59CB\u5316\u914D\u7F6E')),!1;try{let t=p.getMcpEndpoint();return!t||t.includes("<\u8BF7\u586B\u5199")?(console.error(r.red("\u274C \u9519\u8BEF: MCP \u7AEF\u70B9\u672A\u914D\u7F6E")),console.log(r.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u8FD0\u884C "xiaozhi config mcpEndpoint <your-endpoint-url>" \u8BBE\u7F6E\u7AEF\u70B9')),!1):!0}catch(t){return console.error(r.red(`\u274C \u9519\u8BEF: \u914D\u7F6E\u6587\u4EF6\u65E0\u6548 - ${t instanceof Error?t.message:String(t)}`)),console.log(r.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u8FD0\u884C "xiaozhi init" \u91CD\u65B0\u521D\u59CB\u5316\u914D\u7F6E')),!1}}a(uo,"checkEnvironment");function ho(){let t=f.dirname(I(import.meta.url)),o;return t.includes("js-demo/dist")?o=t:o=[f.join(t,"..","js-demo","dist"),f.join(t,"..","..","js-demo","dist"),f.join(t,"..","..","..","js-demo","dist"),f.join(process.cwd(),"js-demo","dist"),f.join(process.cwd(),"dist")].find(n=>u.existsSync(f.join(n,"mcpPipe.js"))&&u.existsSync(f.join(n,"mcpServerProxy.js")))||t,{command:"node",args:["mcpPipe.js","mcpServerProxy.js"],cwd:o}}a(ho,"getServiceCommand");async function V(t=!1){let o=w("\u68C0\u67E5\u670D\u52A1\u72B6\u6001...").start();try{let e=E();if(e.running){o.fail(`\u670D\u52A1\u5DF2\u7ECF\u5728\u8FD0\u884C (PID: ${e.pid})`);return}if(o.text="\u68C0\u67E5\u73AF\u5883\u914D\u7F6E...",!uo()){o.fail("\u73AF\u5883\u914D\u7F6E\u68C0\u67E5\u5931\u8D25");return}let{command:n,args:i,cwd:c}=ho();if(o.text=`\u542F\u52A8\u670D\u52A1 (${t?"\u540E\u53F0\u6A21\u5F0F":"\u524D\u53F0\u6A21\u5F0F"})...`,t){let s=U(n,i,{cwd:c,detached:!0,stdio:["ignore","pipe","pipe"],env:{...process.env,XIAOZHI_CONFIG_DIR:process.cwd(),XIAOZHI_DAEMON:"true"}});J(s.pid,"daemon");let l=process.cwd();x.initLogFile(l),x.enableFileLogging(!0);let h=f.join(l,"xiaozhi.log"),m=u.createWriteStream(h,{flags:"a"});s.stdout?.pipe(m),s.stderr?.pipe(m),s.on("exit",(d,y)=>{d!==0&&d!==null&&x.error(`\u540E\u53F0\u670D\u52A1\u5F02\u5E38\u9000\u51FA (\u4EE3\u7801: ${d}, \u4FE1\u53F7: ${y})`),$()}),s.on("error",d=>{x.error(`\u540E\u53F0\u670D\u52A1\u542F\u52A8\u9519\u8BEF: ${d.message}`),$(),o.fail(`\u540E\u53F0\u670D\u52A1\u542F\u52A8\u5931\u8D25: ${d.message}`)}),s.unref(),o.succeed(`\u670D\u52A1\u5DF2\u5728\u540E\u53F0\u542F\u52A8 (PID: ${s.pid})`),console.log(r.gray(`\u65E5\u5FD7\u6587\u4EF6: ${h}`)),console.log(r.gray("\u4F7F\u7528 'xiaozhi attach' \u53EF\u4EE5\u67E5\u770B\u5B9E\u65F6\u65E5\u5FD7"))}else{o.succeed("\u670D\u52A1\u542F\u52A8\u4E2D...");let s=U(n,i,{cwd:c,stdio:"inherit",env:{...process.env,XIAOZHI_CONFIG_DIR:process.cwd()}});J(s.pid,"foreground"),s.on("exit",(l,h)=>{$(),console.log(l!==0?r.red(`
|
|
4
|
+
\u670D\u52A1\u5F02\u5E38\u9000\u51FA (\u4EE3\u7801: ${l}, \u4FE1\u53F7: ${h})`):r.green(`
|
|
5
|
+
\u670D\u52A1\u5DF2\u505C\u6B62`))}),process.on("SIGINT",()=>{console.log(r.yellow(`
|
|
6
|
+
\u6B63\u5728\u505C\u6B62\u670D\u52A1...`)),s.kill("SIGTERM")}),process.on("SIGTERM",()=>{s.kill("SIGTERM")})}}catch(e){o.fail(`\u542F\u52A8\u670D\u52A1\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}}a(V,"startService");async function X(){let t=w("\u68C0\u67E5\u670D\u52A1\u72B6\u6001...").start();try{let o=E();if(!o.running){t.warn("\u670D\u52A1\u672A\u5728\u8FD0\u884C");return}t.text=`\u505C\u6B62\u670D\u52A1 (PID: ${o.pid})...`;try{process.kill(o.pid,"SIGTERM");let e=0,n=30;for(;e<n;){await new Promise(i=>setTimeout(i,100));try{process.kill(o.pid,0),e++}catch{break}}try{process.kill(o.pid,0),t.text="\u5F3A\u5236\u505C\u6B62\u670D\u52A1...",process.kill(o.pid,"SIGKILL"),await new Promise(i=>setTimeout(i,500))}catch{}$(),t.succeed("\u670D\u52A1\u5DF2\u505C\u6B62")}catch(e){$(),t.fail(`\u505C\u6B62\u670D\u52A1\u5931\u8D25: ${e instanceof Error?e.message:String(e)}`)}}catch(o){t.fail(`\u505C\u6B62\u670D\u52A1\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`)}}a(X,"stopService");async function yo(){let t=w("\u68C0\u67E5\u670D\u52A1\u72B6\u6001...").start();try{let o=E();if(o.running){if(t.succeed("\u670D\u52A1\u72B6\u6001"),console.log(r.green("\u2705 \u670D\u52A1\u6B63\u5728\u8FD0\u884C")),console.log(r.gray(` PID: ${o.pid}`)),console.log(r.gray(` \u8FD0\u884C\u65F6\u95F4: ${o.uptime}`)),console.log(r.gray(` \u8FD0\u884C\u6A21\u5F0F: ${o.mode==="daemon"?"\u540E\u53F0\u6A21\u5F0F":"\u524D\u53F0\u6A21\u5F0F"}`)),o.mode==="daemon"){let e=f.join(process.cwd(),"xiaozhi.log");console.log(r.gray(` \u65E5\u5FD7\u6587\u4EF6: ${e}`))}}else t.succeed("\u670D\u52A1\u72B6\u6001"),console.log(r.red("\u274C \u670D\u52A1\u672A\u8FD0\u884C"))}catch(o){t.fail(`\u68C0\u67E5\u72B6\u6001\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`)}}a(yo,"checkStatus");async function Co(){let t=w("\u68C0\u67E5\u670D\u52A1\u72B6\u6001...").start();try{let o=E();if(!o.running){t.fail("\u670D\u52A1\u672A\u5728\u8FD0\u884C");return}if(o.mode!=="daemon"){t.fail("\u670D\u52A1\u4E0D\u662F\u5728\u540E\u53F0\u6A21\u5F0F\u8FD0\u884C");return}t.succeed("\u8FDE\u63A5\u5230\u540E\u53F0\u670D\u52A1..."),console.log(r.green(`\u5DF2\u8FDE\u63A5\u5230\u670D\u52A1 (PID: ${o.pid})`)),console.log(r.gray("\u6309 Ctrl+C \u53EF\u4EE5\u65AD\u5F00\u8FDE\u63A5\uFF08\u4E0D\u4F1A\u505C\u6B62\u670D\u52A1\uFF09")),console.log(r.gray("=".repeat(50)));let e=f.join(process.cwd(),"xiaozhi.log");if(u.existsSync(e))if(process.platform==="win32"){let{spawn:n}=await import("child_process"),i=n("powershell",["-Command",`Get-Content -Path "${e}" -Wait`],{stdio:"inherit"});process.on("SIGINT",()=>{console.log(r.yellow(`
|
|
7
|
+
\u65AD\u5F00\u8FDE\u63A5\uFF0C\u670D\u52A1\u7EE7\u7EED\u5728\u540E\u53F0\u8FD0\u884C`)),i.kill(),process.exit(0)}),i.on("exit",()=>{process.exit(0)})}else{let{spawn:n}=await import("child_process"),i=n("tail",["-f",e],{stdio:"inherit"});process.on("SIGINT",()=>{console.log(r.yellow(`
|
|
8
|
+
\u65AD\u5F00\u8FDE\u63A5\uFF0C\u670D\u52A1\u7EE7\u7EED\u5728\u540E\u53F0\u8FD0\u884C`)),i.kill(),process.exit(0)}),i.on("exit",()=>{process.exit(0)})}else console.log(r.yellow("\u65E5\u5FD7\u6587\u4EF6\u4E0D\u5B58\u5728"))}catch(o){t.fail(`\u8FDE\u63A5\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`)}}a(Co,"attachService");async function bo(t=!1){console.log(r.blue("\u{1F504} \u91CD\u542F\u670D\u52A1...")),await X(),await new Promise(o=>setTimeout(o,1e3)),await V(t)}a(bo,"restartService");function vo(){let t=B();console.log(r.blue(`xiaozhi v${t}`)),console.log(r.gray("MCP Calculator Service CLI Tool")),console.log(r.gray("Built with Node.js and TypeScript")),console.log(r.gray(`Node.js: ${process.version}`)),console.log(r.gray(`Platform: ${process.platform} ${process.arch}`))}a(vo,"showDetailedInfo");async function wo(){let t=w("\u521D\u59CB\u5316\u914D\u7F6E...").start();try{if(p.configExists()){t.warn("\u914D\u7F6E\u6587\u4EF6\u5DF2\u5B58\u5728"),console.log(r.yellow("\u5982\u9700\u91CD\u65B0\u521D\u59CB\u5316\uFF0C\u8BF7\u5148\u5220\u9664\u73B0\u6709\u7684 xiaozhi.config.json \u6587\u4EF6"));return}p.initConfig(),t.succeed("\u914D\u7F6E\u6587\u4EF6\u521D\u59CB\u5316\u6210\u529F"),console.log(r.green("\u2705 \u914D\u7F6E\u6587\u4EF6\u5DF2\u521B\u5EFA: xiaozhi.config.json")),console.log(r.yellow("\u{1F4DD} \u8BF7\u7F16\u8F91\u914D\u7F6E\u6587\u4EF6\u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9:")),console.log(r.gray(` \u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84: ${p.getConfigPath()}`)),console.log(r.yellow("\u{1F4A1} \u6216\u8005\u4F7F\u7528\u547D\u4EE4\u8BBE\u7F6E:")),console.log(r.gray(" xiaozhi config mcpEndpoint <your-endpoint-url>"))}catch(o){t.fail(`\u521D\u59CB\u5316\u914D\u7F6E\u5931\u8D25: ${o instanceof Error?o.message:String(o)}`)}}a(wo,"initConfig");function So(){let t=f.dirname(I(import.meta.url)),e=[f.join(t,"..","templates"),f.join(t,"templates"),f.join(t,"..","..","templates")].find(n=>u.existsSync(n));return e?u.readdirSync(e).filter(n=>{let i=f.join(e,n);return u.statSync(i).isDirectory()}):[]}a(So,"getAvailableTemplates");function Z(t,o){let e=t.length,n=o.length,i=Array(e+1).fill(null).map(()=>Array(n+1).fill(0));for(let s=0;s<=e;s++)i[s][0]=s;for(let s=0;s<=n;s++)i[0][s]=s;for(let s=1;s<=e;s++)for(let l=1;l<=n;l++)t[s-1]===o[l-1]?i[s][l]=i[s-1][l-1]:i[s][l]=Math.min(i[s-1][l]+1,i[s][l-1]+1,i[s-1][l-1]+1);let c=Math.max(e,n);return c===0?1:(c-i[e][n])/c}a(Z,"calculateSimilarity");function xo(t,o){if(o.length===0)return null;let e=o[0],n=Z(t.toLowerCase(),e.toLowerCase());for(let i of o.slice(1)){let c=Z(t.toLowerCase(),i.toLowerCase());c>n&&(n=c,e=i)}return n>.5?e:null}a(xo,"findSimilarTemplate");async function $o(t){if(!process.stdin.isTTY)return console.log("n (\u975E\u4EA4\u4E92\u5F0F\u73AF\u5883)"),!1;let o=await import("readline");return new Promise(e=>{process.stdout.write(t);let n=o.createInterface({input:process.stdin,output:process.stdout}),i=a(c=>{let s=c.trim().toLowerCase();s==="y"||s==="yes"?(n.close(),e(!0)):s==="n"||s==="no"||s===""?(n.close(),e(!1)):process.stdout.write("\u8BF7\u8F93\u5165 y \u6216 n: ")},"handleInput");n.on("line",i),n.on("SIGINT",()=>{n.close(),e(!1)})})}a($o,"askUserConfirmation");function Po(t){let o={mcpEndpoint:"<\u8BF7\u586B\u5199\u4F60\u7684\u63A5\u5165\u70B9\u5730\u5740\uFF08\u83B7\u53D6\u5730\u5740\u5728 xiaozhi.me\uFF09>",mcpServers:{}},e=f.join(t,"xiaozhi.config.json");u.writeFileSync(e,JSON.stringify(o,null,2),"utf8")}a(Po,"createBasicConfig");async function Io(t,o){let e=w("\u521D\u59CB\u5316\u9879\u76EE...").start();try{let n=f.join(process.cwd(),t);if(u.existsSync(n)){e.fail(`\u76EE\u5F55 "${t}" \u5DF2\u5B58\u5728`),console.log(r.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u9009\u62E9\u4E0D\u540C\u7684\u9879\u76EE\u540D\u79F0\u6216\u5220\u9664\u73B0\u6709\u76EE\u5F55"));return}if(o.template){e.text="\u68C0\u67E5\u6A21\u677F...";let i=So();if(i.length===0){e.fail("\u627E\u4E0D\u5230 templates \u76EE\u5F55"),console.log(r.yellow("\u{1F4A1} \u63D0\u793A: \u8BF7\u786E\u4FDD xiaozhi-client \u6B63\u786E\u5B89\u88C5"));return}if(!i.includes(o.template)){e.fail(`\u6A21\u677F "${o.template}" \u4E0D\u5B58\u5728`);let d=xo(o.template,i);if(d)if(console.log(r.yellow(`\u{1F4A1} \u4F60\u662F\u60F3\u4F7F\u7528\u6A21\u677F "${d}" \u5417\uFF1F`)),await $o(r.cyan("\u786E\u8BA4\u4F7F\u7528\u6B64\u6A21\u677F\uFF1F(y/n): ")))o.template=d;else{console.log(r.yellow("\u53EF\u7528\u7684\u6A21\u677F:"));for(let C of i)console.log(r.gray(` - ${C}`));return}else{console.log(r.yellow("\u53EF\u7528\u7684\u6A21\u677F:"));for(let y of i)console.log(r.gray(` - ${y}`));return}}let c=f.dirname(I(import.meta.url)),l=[f.join(c,"..","templates"),f.join(c,"templates"),f.join(c,"..","..","templates")].find(d=>u.existsSync(d)),h=f.join(l,o.template);e.text=`\u4ECE\u6A21\u677F "${o.template}" \u521B\u5EFA\u9879\u76EE "${t}"...`,q(h,n,["node_modules",".pnpm-debug.log","pnpm-lock.yaml"]);let m=f.join(n,"xiaozhi.log");u.existsSync(m)||u.writeFileSync(m,"","utf8"),e.succeed(`\u9879\u76EE "${t}" \u521B\u5EFA\u6210\u529F`),console.log(r.green("\u2705 \u9879\u76EE\u521B\u5EFA\u5B8C\u6210!")),console.log(r.yellow("\u{1F4DD} \u63A5\u4E0B\u6765\u7684\u6B65\u9AA4:")),console.log(r.gray(` cd ${t}`)),console.log(r.gray(" pnpm install # \u5B89\u88C5\u4F9D\u8D56")),console.log(r.gray(" # \u7F16\u8F91 xiaozhi.config.json \u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9")),console.log(r.gray(" xiaozhi start # \u542F\u52A8\u670D\u52A1"))}else{e.text=`\u521B\u5EFA\u57FA\u672C\u9879\u76EE "${t}"...`,u.mkdirSync(n,{recursive:!0}),Po(n);let i=f.join(n,"xiaozhi.log");u.writeFileSync(i,"","utf8"),e.succeed(`\u9879\u76EE "${t}" \u521B\u5EFA\u6210\u529F`),console.log(r.green("\u2705 \u57FA\u672C\u9879\u76EE\u521B\u5EFA\u5B8C\u6210!")),console.log(r.yellow("\u{1F4DD} \u63A5\u4E0B\u6765\u7684\u6B65\u9AA4:")),console.log(r.gray(` cd ${t}`)),console.log(r.gray(" # \u7F16\u8F91 xiaozhi.config.json \u8BBE\u7F6E\u4F60\u7684 MCP \u7AEF\u70B9\u548C\u670D\u52A1")),console.log(r.gray(" xiaozhi start # \u542F\u52A8\u670D\u52A1")),console.log(r.yellow("\u{1F4A1} \u63D0\u793A: \u4F7F\u7528 --template \u9009\u9879\u53EF\u4EE5\u4ECE\u6A21\u677F\u521B\u5EFA\u9879\u76EE"))}}catch(n){e.fail(`\u521B\u5EFA\u9879\u76EE\u5931\u8D25: ${n instanceof Error?n.message:String(n)}`)}}a(Io,"createProject");function q(t,o,e=[]){u.existsSync(o)||u.mkdirSync(o,{recursive:!0});let n=u.readdirSync(t);for(let i of n){if(e.some(h=>i.includes(h)))continue;let c=f.join(t,i),s=f.join(o,i);u.statSync(c).isDirectory()?q(c,s,e):u.copyFileSync(c,s)}}a(q,"copyDirectory");async function Eo(t,o){let e=w("\u66F4\u65B0\u914D\u7F6E...").start();try{if(!p.configExists()){e.fail("\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728"),console.log(r.yellow('\u{1F4A1} \u63D0\u793A: \u8BF7\u5148\u8FD0\u884C "xiaozhi init" \u521D\u59CB\u5316\u914D\u7F6E'));return}if(o)switch(t){case"mcpEndpoint":p.updateMcpEndpoint(o),e.succeed(`MCP \u7AEF\u70B9\u5DF2\u66F4\u65B0\u4E3A: ${o}`);break;case"heartbeatInterval":{let n=Number.parseInt(o,10);if(Number.isNaN(n)||n<=0){e.fail("\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5FC5\u987B\u662F\u5927\u4E8E0\u7684\u6570\u5B57\uFF08\u6BEB\u79D2\uFF09");return}p.setHeartbeatInterval(n),e.succeed(`\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5DF2\u66F4\u65B0\u4E3A: ${n}ms`);break}case"heartbeatTimeout":{let n=Number.parseInt(o,10);if(Number.isNaN(n)||n<=0){e.fail("\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5FC5\u987B\u662F\u5927\u4E8E0\u7684\u6570\u5B57\uFF08\u6BEB\u79D2\uFF09");return}p.setHeartbeatTimeout(n),e.succeed(`\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5DF2\u66F4\u65B0\u4E3A: ${n}ms`);break}case"reconnectInterval":{let n=Number.parseInt(o,10);if(Number.isNaN(n)||n<=0){e.fail("\u91CD\u8FDE\u95F4\u9694\u5FC5\u987B\u662F\u5927\u4E8E0\u7684\u6570\u5B57\uFF08\u6BEB\u79D2\uFF09");return}p.setReconnectInterval(n),e.succeed(`\u91CD\u8FDE\u95F4\u9694\u5DF2\u66F4\u65B0\u4E3A: ${n}ms`);break}default:e.fail(`\u914D\u7F6E\u9879 ${t} \u4E0D\u652F\u6301\u901A\u8FC7\u547D\u4EE4\u884C\u8BBE\u7F6E`),console.log(r.yellow("\u652F\u6301\u8BBE\u7F6E\u7684\u914D\u7F6E\u9879: mcpEndpoint, heartbeatInterval, heartbeatTimeout, reconnectInterval"));return}else{e.text="\u8BFB\u53D6\u914D\u7F6E...";let n=p.getConfig();switch(t){case"mcpEndpoint":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(r.green(`MCP \u7AEF\u70B9: ${n.mcpEndpoint}`));break;case"mcpServers":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(r.green("MCP \u670D\u52A1:"));for(let[i,c]of Object.entries(n.mcpServers))console.log(r.gray(` ${i}: ${c.command} ${c.args.join(" ")}`));break;case"connection":{e.succeed("\u914D\u7F6E\u4FE1\u606F");let i=p.getConnectionConfig();console.log(r.green("\u8FDE\u63A5\u914D\u7F6E:")),console.log(r.gray(` \u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694: ${i.heartbeatInterval}ms`)),console.log(r.gray(` \u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4: ${i.heartbeatTimeout}ms`)),console.log(r.gray(` \u91CD\u8FDE\u95F4\u9694: ${i.reconnectInterval}ms`));break}case"heartbeatInterval":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(r.green(`\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694: ${p.getHeartbeatInterval()}ms`));break;case"heartbeatTimeout":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(r.green(`\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4: ${p.getHeartbeatTimeout()}ms`));break;case"reconnectInterval":e.succeed("\u914D\u7F6E\u4FE1\u606F"),console.log(r.green(`\u91CD\u8FDE\u95F4\u9694: ${p.getReconnectInterval()}ms`));break;default:e.fail(`\u672A\u77E5\u7684\u914D\u7F6E\u9879: ${t}`),console.log(r.yellow("\u652F\u6301\u7684\u914D\u7F6E\u9879: mcpEndpoint, mcpServers, connection, heartbeatInterval, heartbeatTimeout, reconnectInterval"));return}}}catch(n){e.fail(`\u914D\u7F6E\u64CD\u4F5C\u5931\u8D25: ${n instanceof Error?n.message:String(n)}`)}}a(Eo,"configCommand");function To(){console.log(r.blue.bold("xiaozhi - MCP Calculator Service CLI")),console.log(),console.log(r.yellow("\u4F7F\u7528\u65B9\u6CD5:")),console.log(" xiaozhi <command> [options]"),console.log(),console.log(r.yellow("\u547D\u4EE4:")),console.log(" create <projectName> \u521B\u5EFA\u9879\u76EE"),console.log(" init \u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6"),console.log(" config <key> [value] \u67E5\u770B\u6216\u8BBE\u7F6E\u914D\u7F6E"),console.log(" start [--daemon] \u542F\u52A8\u670D\u52A1 (--daemon \u540E\u53F0\u8FD0\u884C)"),console.log(" stop \u505C\u6B62\u670D\u52A1"),console.log(" status \u68C0\u67E5\u670D\u52A1\u72B6\u6001"),console.log(" attach \u8FDE\u63A5\u5230\u540E\u53F0\u670D\u52A1\u67E5\u770B\u65E5\u5FD7"),console.log(" restart [--daemon] \u91CD\u542F\u670D\u52A1 (--daemon \u540E\u53F0\u8FD0\u884C)"),console.log(" completion \u663E\u793A\u81EA\u52A8\u8865\u5168\u8BBE\u7F6E\u8BF4\u660E"),console.log(),console.log(r.yellow("\u9009\u9879:")),console.log(" -v, --version \u663E\u793A\u7248\u672C\u4FE1\u606F"),console.log(" -V \u663E\u793A\u8BE6\u7EC6\u4FE1\u606F"),console.log(" -h, --help \u663E\u793A\u5E2E\u52A9\u4FE1\u606F"),console.log(" -t, --template <name> \u6307\u5B9A\u6A21\u677F\u540D\u79F0\uFF08\u7528\u4E8E create \u547D\u4EE4\uFF09"),console.log(),console.log(r.yellow("\u9879\u76EE\u793A\u4F8B:")),console.log(" xiaozhi create my-app # \u521B\u5EFA\u57FA\u672C\u9879\u76EE"),console.log(" xiaozhi create my-app -t hello-world # \u4F7F\u7528 hello-world \u6A21\u677F"),console.log(" xiaozhi create my-app --template hello-world # \u540C\u4E0A\uFF0C\u5B8C\u6574\u9009\u9879\u540D"),console.log(),console.log(r.yellow("\u914D\u7F6E\u793A\u4F8B:")),console.log(" xiaozhi init # \u521D\u59CB\u5316\u914D\u7F6E"),console.log(" xiaozhi config mcpEndpoint # \u67E5\u770B MCP \u7AEF\u70B9"),console.log(" xiaozhi config mcpEndpoint wss://... # \u8BBE\u7F6E MCP \u7AEF\u70B9"),console.log(),console.log(r.yellow("\u670D\u52A1\u793A\u4F8B:")),console.log(" xiaozhi start # \u524D\u53F0\u542F\u52A8\u670D\u52A1"),console.log(" xiaozhi start --daemon # \u540E\u53F0\u542F\u52A8\u670D\u52A1"),console.log(" xiaozhi status # \u68C0\u67E5\u670D\u52A1\u72B6\u6001"),console.log(" xiaozhi attach # \u67E5\u770B\u540E\u53F0\u670D\u52A1\u65E5\u5FD7"),console.log(" xiaozhi stop # \u505C\u6B62\u670D\u52A1"),console.log(),console.log(r.yellow("MCP \u7BA1\u7406\u793A\u4F8B:")),console.log(" xiaozhi mcp list # \u5217\u51FA\u6240\u6709 MCP \u670D\u52A1"),console.log(" xiaozhi mcp list --tools # \u5217\u51FA\u6240\u6709\u670D\u52A1\u7684\u5DE5\u5177"),console.log(" xiaozhi mcp server <name> # \u5217\u51FA\u6307\u5B9A\u670D\u52A1\u7684\u5DE5\u5177"),console.log(" xiaozhi mcp tool <server> <tool> enable # \u542F\u7528\u5DE5\u5177"),console.log(" xiaozhi mcp tool <server> <tool> disable # \u7981\u7528\u5DE5\u5177"),console.log(),console.log(r.yellow("\u81EA\u52A8\u8865\u5168:")),console.log(" xiaozhi completion # \u663E\u793A\u81EA\u52A8\u8865\u5168\u8BBE\u7F6E\u8BF4\u660E"),console.log(" # \u8BBE\u7F6E\u540E\u53EF\u4F7F\u7528 Tab \u952E\u8FDB\u884C\u547D\u4EE4\u3001\u53C2\u6570\u81EA\u52A8\u8865\u5168")}a(To,"showHelp");b.name("xiaozhi").description("MCP Calculator Service CLI Tool").version(B(),"-v, --version","\u663E\u793A\u7248\u672C\u4FE1\u606F").helpOption("-h, --help","\u663E\u793A\u5E2E\u52A9\u4FE1\u606F");b.command("create <projectName>").description("\u521B\u5EFA\u9879\u76EE").option("-t, --template <templateName>","\u4F7F\u7528\u6307\u5B9A\u6A21\u677F\u521B\u5EFA\u9879\u76EE").action(async(t,o)=>{await Io(t,o)});b.command("init").description("\u521D\u59CB\u5316\u914D\u7F6E\u6587\u4EF6").action(async()=>{await wo()});b.command("config <key> [value]").description("\u67E5\u770B\u6216\u8BBE\u7F6E\u914D\u7F6E").action(async(t,o)=>{await Eo(t,o)});b.command("start").description("\u542F\u52A8\u670D\u52A1").option("-d, --daemon","\u5728\u540E\u53F0\u8FD0\u884C\u670D\u52A1").action(async t=>{await V(t.daemon)});b.command("stop").description("\u505C\u6B62\u670D\u52A1").action(async()=>{await X()});b.command("status").description("\u68C0\u67E5\u670D\u52A1\u72B6\u6001").action(async()=>{await yo()});b.command("attach").description("\u8FDE\u63A5\u5230\u540E\u53F0\u670D\u52A1\u67E5\u770B\u65E5\u5FD7").action(async()=>{await Co()});b.command("restart").description("\u91CD\u542F\u670D\u52A1").option("-d, --daemon","\u5728\u540E\u53F0\u8FD0\u884C\u670D\u52A1").action(async t=>{await bo(t.daemon)});var F=b.command("mcp").description("MCP \u670D\u52A1\u548C\u5DE5\u5177\u7BA1\u7406");F.command("list").description("\u5217\u51FA MCP \u670D\u52A1").option("--tools","\u663E\u793A\u6240\u6709\u670D\u52A1\u7684\u5DE5\u5177\u5217\u8868").action(async t=>{await G(t)});F.command("server <serverName>").description("\u7BA1\u7406\u6307\u5B9A\u7684 MCP \u670D\u52A1").action(async t=>{await H(t)});F.command("tool <serverName> <toolName> <action>").description("\u542F\u7528\u6216\u7981\u7528\u6307\u5B9A\u670D\u52A1\u7684\u5DE5\u5177").action(async(t,o,e)=>{e!=="enable"&&e!=="disable"&&(console.error(r.red("\u9519\u8BEF: \u64CD\u4F5C\u5FC5\u987B\u662F 'enable' \u6216 'disable'")),process.exit(1)),await _(t,o,e==="enable")});b.command("completion").description("\u663E\u793A\u81EA\u52A8\u8865\u5168\u8BBE\u7F6E\u8BF4\u660E").action(async()=>{N()});b.option("-V","\u663E\u793A\u8BE6\u7EC6\u4FE1\u606F").action(t=>{t.V&&(vo(),process.exit(0))});O();process.argv.length<=2&&(To(),process.exit(0));b.parse(process.argv);export{Z as calculateSimilarity,uo as checkEnvironment,mo as formatUptime,E as getServiceStatus,B as getVersion};
|
|
12
9
|
//# sourceMappingURL=cli.js.map
|