xiaozhi-client 1.4.0 → 1.5.0-beta.2

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/webServer.js CHANGED
@@ -1,5 +1,5 @@
1
- var M=Object.defineProperty;var g=(s,t)=>M(s,"name",{value:t,configurable:!0});import{existsSync as v}from"fs";import{readFile as I}from"fs/promises";import{createServer as N}from"http";import{dirname as $,join as p}from"path";import{parse as J}from"url";import{fileURLToPath as D}from"url";import{WebSocketServer as k}from"ws";import{copyFileSync as O,existsSync as w,readFileSync as x,writeFileSync as U}from"fs";import{dirname as F,resolve as y}from"path";import{fileURLToPath as R}from"url";var H=F(R(import.meta.url)),S={heartbeatInterval:3e4,heartbeatTimeout:1e4,reconnectInterval:5e3},b=class s{static{g(this,"ConfigManager")}static instance;defaultConfigPath;config=null;constructor(){this.defaultConfigPath=y(H,"xiaozhi.config.default.json")}getConfigFilePath(){let t=process.env.XIAOZHI_CONFIG_DIR||process.cwd();return y(t,"xiaozhi.config.json")}static getInstance(){return s.instance||(s.instance=new s),s.instance}configExists(){let t=this.getConfigFilePath();return w(t)}initConfig(){if(!w(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 t=this.getConfigFilePath();O(this.defaultConfigPath,t),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 t=this.getConfigFilePath(),e=x(t,"utf8"),o=JSON.parse(e);return this.validateConfig(o),o}catch(t){throw t instanceof SyntaxError?new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF: ${t.message}`):t}}validateConfig(t){if(!t||typeof t!="object")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1A\u6839\u5BF9\u8C61\u65E0\u6548");let e=t;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[o,i]of Object.entries(e.mcpServers)){if(!i||typeof i!="object")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${o} \u65E0\u6548`);let n=i;if(n.type==="sse"){if(!n.url||typeof n.url!="string")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${o}.url \u65E0\u6548`)}else{if(!n.command||typeof n.command!="string")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${o}.command \u65E0\u6548`);if(!Array.isArray(n.args))throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${o}.args \u5FC5\u987B\u662F\u6570\u7EC4`);if(n.env&&typeof n.env!="object")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${o}.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(t){return this.getMcpServerConfig()[t]?.tools||{}}isToolEnabled(t,e){return this.getServerToolsConfig(t)[e]?.enable!==!1}updateMcpEndpoint(t){if(!t||typeof t!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let o={...this.getConfig(),mcpEndpoint:t};this.saveConfig(o)}updateMcpServer(t,e){if(!t||typeof t!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if("type"in e&&e.type==="sse"){if(!e.url||typeof e.url!="string")throw new Error("SSE \u670D\u52A1\u914D\u7F6E\u7684 url \u5B57\u6BB5\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32")}else{let n=e;if(!n.command||typeof n.command!="string")throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 command \u5B57\u6BB5\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(!Array.isArray(n.args))throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 args \u5B57\u6BB5\u5FC5\u987B\u662F\u6570\u7EC4");if(n.env&&typeof n.env!="object")throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 env \u5B57\u6BB5\u5FC5\u987B\u662F\u5BF9\u8C61")}let o=this.getConfig(),i={...o,mcpServers:{...o.mcpServers,[t]:e}};this.saveConfig(i)}removeMcpServer(t){if(!t||typeof t!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let e=this.getConfig();if(!e.mcpServers[t])throw new Error(`\u670D\u52A1 ${t} \u4E0D\u5B58\u5728`);let o={...e.mcpServers};delete o[t];let i={...e,mcpServers:o};this.saveConfig(i)}updateServerToolsConfig(t,e){let i={...this.getConfig()};i.mcpServerConfig||(i.mcpServerConfig={}),i.mcpServerConfig[t]={tools:e},this.saveConfig(i)}setToolEnabled(t,e,o,i){let r={...this.getConfig()};r.mcpServerConfig||(r.mcpServerConfig={}),r.mcpServerConfig[t]||(r.mcpServerConfig[t]={tools:{}}),r.mcpServerConfig[t].tools[e]={enable:o,...i&&{description:i}},this.saveConfig(r)}saveConfig(t){try{this.validateConfig(t);let e=this.getConfigFilePath(),o=JSON.stringify(t,null,2);U(e,o,"utf8"),this.config=t}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??S.heartbeatInterval,heartbeatTimeout:e.heartbeatTimeout??S.heartbeatTimeout,reconnectInterval:e.reconnectInterval??S.reconnectInterval}}getHeartbeatInterval(){return this.getConnectionConfig().heartbeatInterval}getHeartbeatTimeout(){return this.getConnectionConfig().heartbeatTimeout}getReconnectInterval(){return this.getConnectionConfig().reconnectInterval}updateConnectionConfig(t){let e=this.getConfig(),i={...e.connection||{},...t},n={...e,connection:i};this.saveConfig(n)}setHeartbeatInterval(t){if(t<=0)throw new Error("\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatInterval:t})}setHeartbeatTimeout(t){if(t<=0)throw new Error("\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatTimeout:t})}setReconnectInterval(t){if(t<=0)throw new Error("\u91CD\u8FDE\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({reconnectInterval:t})}getModelScopeConfig(){return this.getConfig().modelscope||{}}getModelScopeApiKey(){return this.getModelScopeConfig().apiKey||process.env.MODELSCOPE_API_TOKEN}updateModelScopeConfig(t){let e=this.getConfig(),i={...e.modelscope||{},...t},n={...e,modelscope:i};this.saveConfig(n)}setModelScopeApiKey(t){if(!t||typeof t!="string")throw new Error("API Key \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");this.updateModelScopeConfig({apiKey:t})}getWebUIConfig(){return this.getConfig().webUI||{}}getWebUIPort(){return this.getWebUIConfig().port??9999}updateWebUIConfig(t){let e=this.getConfig(),i={...e.webUI||{},...t},n={...e,webUI:i};this.saveConfig(n)}setWebUIPort(t){if(!Number.isInteger(t)||t<=0||t>65535)throw new Error("\u7AEF\u53E3\u53F7\u5FC5\u987B\u662F 1-65535 \u4E4B\u95F4\u7684\u6574\u6570");this.updateWebUIConfig({port:t})}},a=b.getInstance();import u from"fs";import A from"path";import l from"chalk";import{createConsola as W}from"consola";function j(s){let t=s.getFullYear(),e=String(s.getMonth()+1).padStart(2,"0"),o=String(s.getDate()).padStart(2,"0"),i=String(s.getHours()).padStart(2,"0"),n=String(s.getMinutes()).padStart(2,"0"),r=String(s.getSeconds()).padStart(2,"0");return`${t}-${e}-${o} ${i}:${n}:${r}`}g(j,"formatDateTime");var d=class{static{g(this,"Logger")}logFilePath=null;writeStream=null;consolaInstance;isDaemonMode;constructor(){this.isDaemonMode=process.env.XIAOZHI_DAEMON==="true",this.consolaInstance=W({formatOptions:{date:!1,colors:!0,compact:!0},fancy:!1});let t=this.isDaemonMode;this.consolaInstance.setReporters([{log:g(e=>{let o={info:"INFO",success:"SUCCESS",warn:"WARN",error:"ERROR",debug:"DEBUG",log:"LOG"},i={info:l.blue,success:l.green,warn:l.yellow,error:l.red,debug:l.gray,log:g(f=>f,"log")},n=o[e.type]||e.type.toUpperCase(),r=i[e.type]||(f=>f),c=j(new Date),C=r(`[${n}]`),m=`[${c}] ${C} ${e.args.join(" ")}`;if(!t)try{console.error(m)}catch(f){if(f instanceof Error&&f.message?.includes("EPIPE"))return;throw f}},"log")}])}initLogFile(t){this.logFilePath=A.join(t,"xiaozhi.log"),u.existsSync(this.logFilePath)||u.writeFileSync(this.logFilePath,""),this.writeStream=u.createWriteStream(this.logFilePath,{flags:"a",encoding:"utf8"})}logToFile(t,e,...o){if(this.writeStream){let n=`[${new Date().toISOString()}] [${t.toUpperCase()}] ${e}`,r=o.length>0?`${n} ${o.map(c=>typeof c=="object"?JSON.stringify(c):String(c)).join(" ")}`:n;this.writeStream.write(`${r}
2
- `)}}enableFileLogging(t){t&&!this.writeStream&&this.logFilePath?this.writeStream=u.createWriteStream(this.logFilePath,{flags:"a",encoding:"utf8"}):!t&&this.writeStream&&(this.writeStream.end(),this.writeStream=null)}info(t,...e){this.consolaInstance.info(t,...e),this.logToFile("info",t,...e)}success(t,...e){this.consolaInstance.success(t,...e),this.logToFile("success",t,...e)}warn(t,...e){this.consolaInstance.warn(t,...e),this.logToFile("warn",t,...e)}error(t,...e){this.consolaInstance.error(t,...e),this.logToFile("error",t,...e)}debug(t,...e){this.consolaInstance.debug(t,...e),this.logToFile("debug",t,...e)}log(t,...e){this.consolaInstance.log(t,...e),this.logToFile("log",t,...e)}withTag(t){return this}close(){this.writeStream&&(this.writeStream.end(),this.writeStream=null)}},V=new d;var T=class{static{g(this,"WebServer")}httpServer;wss;logger;port;clientInfo={status:"disconnected",mcpEndpoint:"",activeMCPServers:[]};heartbeatTimeout;HEARTBEAT_TIMEOUT=35e3;constructor(t){if(t===void 0)try{this.port=a.getWebUIPort()}catch{this.port=9999}else this.port=t;this.logger=new d,this.httpServer=N((e,o)=>{this.handleHttpRequest(e,o)}),this.wss=new k({server:this.httpServer}),this.setupWebSocket()}async handleHttpRequest(t,e){let{pathname:o}=J(t.url||"",!0);if(e.setHeader("Access-Control-Allow-Origin","*"),e.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, OPTIONS"),e.setHeader("Access-Control-Allow-Headers","Content-Type"),t.method==="OPTIONS"){e.writeHead(200),e.end();return}try{if(t.method==="GET"&&!o?.startsWith("/api/")){await this.serveStaticFile(o||"/",e);return}if(o==="/api/config"&&t.method==="GET"){let i=a.getConfig();e.writeHead(200,{"Content-Type":"application/json"}),e.end(JSON.stringify(i))}else if(o==="/api/config"&&t.method==="PUT"){let i="";t.on("data",n=>{i+=n.toString()}),t.on("end",async()=>{try{let n=JSON.parse(i);this.updateConfig(n),e.writeHead(200,{"Content-Type":"application/json"}),e.end(JSON.stringify({success:!0})),this.broadcastConfigUpdate(n)}catch(n){e.writeHead(400,{"Content-Type":"application/json"}),e.end(JSON.stringify({error:n instanceof Error?n.message:String(n)}))}})}else o==="/api/status"&&t.method==="GET"?(e.writeHead(200,{"Content-Type":"application/json"}),e.end(JSON.stringify(this.clientInfo))):(e.writeHead(404),e.end("Not Found"))}catch(i){this.logger.error("HTTP request error:",i),e.writeHead(500,{"Content-Type":"application/json"}),e.end(JSON.stringify({error:"Internal Server Error"}))}}async serveStaticFile(t,e){try{let o=$(D(import.meta.url)),n=[p(o,"..","web","dist"),p(o,"..","web"),p(process.cwd(),"web","dist"),p(process.cwd(),"web")].find(h=>v(h));if(!n){e.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),e.end(`
1
+ var M=Object.defineProperty;var g=(s,t)=>M(s,"name",{value:t,configurable:!0});import{existsSync as b}from"fs";import{readFile as E}from"fs/promises";import{createServer as N}from"http";import{dirname as $,join as p}from"path";import{parse as J}from"url";import{fileURLToPath as D}from"url";import{WebSocketServer as k}from"ws";import{copyFileSync as O,existsSync as w,readFileSync as x,writeFileSync as U}from"fs";import{dirname as A,resolve as y}from"path";import{fileURLToPath as F}from"url";var R=A(F(import.meta.url)),S={heartbeatInterval:3e4,heartbeatTimeout:1e4,reconnectInterval:5e3},v=class s{static{g(this,"ConfigManager")}static instance;defaultConfigPath;config=null;constructor(){this.defaultConfigPath=y(R,"xiaozhi.config.default.json")}getConfigFilePath(){let t=process.env.XIAOZHI_CONFIG_DIR||process.cwd();return y(t,"xiaozhi.config.json")}static getInstance(){return s.instance||(s.instance=new s),s.instance}configExists(){let t=this.getConfigFilePath();return w(t)}initConfig(){if(!w(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 t=this.getConfigFilePath();O(this.defaultConfigPath,t),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 t=this.getConfigFilePath(),e=x(t,"utf8"),n=JSON.parse(e);return this.validateConfig(n),n}catch(t){throw t instanceof SyntaxError?new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF: ${t.message}`):t}}validateConfig(t){if(!t||typeof t!="object")throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1A\u6839\u5BF9\u8C61\u65E0\u6548");let e=t;if(e.mcpEndpoint===void 0||e.mcpEndpoint===null)throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpEndpoint \u5B57\u6BB5\u65E0\u6548");if(typeof e.mcpEndpoint!="string")if(Array.isArray(e.mcpEndpoint)){if(e.mcpEndpoint.length===0)throw new Error("\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpEndpoint \u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A");for(let n of e.mcpEndpoint)if(typeof n!="string"||n.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(!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 o=i;if(o.type==="sse"){if(!o.url||typeof o.url!="string")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${n}.url \u65E0\u6548`)}else{if(!o.command||typeof o.command!="string")throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${n}.command \u65E0\u6548`);if(!Array.isArray(o.args))throw new Error(`\u914D\u7F6E\u6587\u4EF6\u683C\u5F0F\u9519\u8BEF\uFF1AmcpServers.${n}.args \u5FC5\u987B\u662F\u6570\u7EC4`);if(o.env&&typeof o.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(){let t=this.getConfig();return Array.isArray(t.mcpEndpoint)?t.mcpEndpoint[0]||"":t.mcpEndpoint}getMcpEndpoints(){let t=this.getConfig();return Array.isArray(t.mcpEndpoint)?[...t.mcpEndpoint]:t.mcpEndpoint?[t.mcpEndpoint]:[]}getMcpServers(){return this.getConfig().mcpServers}getMcpServerConfig(){return this.getConfig().mcpServerConfig||{}}getServerToolsConfig(t){return this.getMcpServerConfig()[t]?.tools||{}}isToolEnabled(t,e){return this.getServerToolsConfig(t)[e]?.enable!==!1}updateMcpEndpoint(t){if(Array.isArray(t)){if(t.length===0)throw new Error("MCP \u7AEF\u70B9\u6570\u7EC4\u4E0D\u80FD\u4E3A\u7A7A");for(let i of t)if(!i||typeof i!="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(!t||typeof t!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let n={...this.getConfig(),mcpEndpoint:t};this.saveConfig(n)}addMcpEndpoint(t){if(!t||typeof t!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let e=this.getConfig(),n=this.getMcpEndpoints();if(n.includes(t))throw new Error(`MCP \u7AEF\u70B9 ${t} \u5DF2\u5B58\u5728`);let i=[...n,t],o={...e,mcpEndpoint:i};this.saveConfig(o)}removeMcpEndpoint(t){if(!t||typeof t!="string")throw new Error("MCP \u7AEF\u70B9\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let e=this.getConfig(),n=this.getMcpEndpoints();if(n.indexOf(t)===-1)throw new Error(`MCP \u7AEF\u70B9 ${t} \u4E0D\u5B58\u5728`);if(n.length===1)throw new Error("\u4E0D\u80FD\u5220\u9664\u6700\u540E\u4E00\u4E2A MCP \u7AEF\u70B9");let o=n.filter(a=>a!==t),r={...e,mcpEndpoint:o};this.saveConfig(r)}updateMcpServer(t,e){if(!t||typeof t!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if("type"in e&&e.type==="sse"){if(!e.url||typeof e.url!="string")throw new Error("SSE \u670D\u52A1\u914D\u7F6E\u7684 url \u5B57\u6BB5\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32")}else{let o=e;if(!o.command||typeof o.command!="string")throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 command \u5B57\u6BB5\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");if(!Array.isArray(o.args))throw new Error("\u670D\u52A1\u914D\u7F6E\u7684 args \u5B57\u6BB5\u5FC5\u987B\u662F\u6570\u7EC4");if(o.env&&typeof o.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,[t]:e}};this.saveConfig(i)}removeMcpServer(t){if(!t||typeof t!="string")throw new Error("\u670D\u52A1\u540D\u79F0\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");let e=this.getConfig();if(!e.mcpServers[t])throw new Error(`\u670D\u52A1 ${t} \u4E0D\u5B58\u5728`);let n={...e.mcpServers};delete n[t];let i={...e,mcpServers:n};this.saveConfig(i)}updateServerToolsConfig(t,e){let i={...this.getConfig()};i.mcpServerConfig||(i.mcpServerConfig={}),i.mcpServerConfig[t]={tools:e},this.saveConfig(i)}setToolEnabled(t,e,n,i){let r={...this.getConfig()};r.mcpServerConfig||(r.mcpServerConfig={}),r.mcpServerConfig[t]||(r.mcpServerConfig[t]={tools:{}}),r.mcpServerConfig[t].tools[e]={enable:n,...i&&{description:i}},this.saveConfig(r)}saveConfig(t){try{this.validateConfig(t);let e=this.getConfigFilePath(),n=JSON.stringify(t,null,2);U(e,n,"utf8"),this.config=t}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??S.heartbeatInterval,heartbeatTimeout:e.heartbeatTimeout??S.heartbeatTimeout,reconnectInterval:e.reconnectInterval??S.reconnectInterval}}getHeartbeatInterval(){return this.getConnectionConfig().heartbeatInterval}getHeartbeatTimeout(){return this.getConnectionConfig().heartbeatTimeout}getReconnectInterval(){return this.getConnectionConfig().reconnectInterval}updateConnectionConfig(t){let e=this.getConfig(),i={...e.connection||{},...t},o={...e,connection:i};this.saveConfig(o)}setHeartbeatInterval(t){if(t<=0)throw new Error("\u5FC3\u8DF3\u68C0\u6D4B\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatInterval:t})}setHeartbeatTimeout(t){if(t<=0)throw new Error("\u5FC3\u8DF3\u8D85\u65F6\u65F6\u95F4\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({heartbeatTimeout:t})}setReconnectInterval(t){if(t<=0)throw new Error("\u91CD\u8FDE\u95F4\u9694\u5FC5\u987B\u5927\u4E8E0");this.updateConnectionConfig({reconnectInterval:t})}getModelScopeConfig(){return this.getConfig().modelscope||{}}getModelScopeApiKey(){return this.getModelScopeConfig().apiKey||process.env.MODELSCOPE_API_TOKEN}updateModelScopeConfig(t){let e=this.getConfig(),i={...e.modelscope||{},...t},o={...e,modelscope:i};this.saveConfig(o)}setModelScopeApiKey(t){if(!t||typeof t!="string")throw new Error("API Key \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");this.updateModelScopeConfig({apiKey:t})}getWebUIConfig(){return this.getConfig().webUI||{}}getWebUIPort(){return this.getWebUIConfig().port??9999}updateWebUIConfig(t){let e=this.getConfig(),i={...e.webUI||{},...t},o={...e,webUI:i};this.saveConfig(o)}setWebUIPort(t){if(!Number.isInteger(t)||t<=0||t>65535)throw new Error("\u7AEF\u53E3\u53F7\u5FC5\u987B\u662F 1-65535 \u4E4B\u95F4\u7684\u6574\u6570");this.updateWebUIConfig({port:t})}},c=v.getInstance();import u from"fs";import H from"path";import l from"chalk";import{createConsola as W}from"consola";function j(s){let t=s.getFullYear(),e=String(s.getMonth()+1).padStart(2,"0"),n=String(s.getDate()).padStart(2,"0"),i=String(s.getHours()).padStart(2,"0"),o=String(s.getMinutes()).padStart(2,"0"),r=String(s.getSeconds()).padStart(2,"0");return`${t}-${e}-${n} ${i}:${o}:${r}`}g(j,"formatDateTime");var d=class{static{g(this,"Logger")}logFilePath=null;writeStream=null;consolaInstance;isDaemonMode;constructor(){this.isDaemonMode=process.env.XIAOZHI_DAEMON==="true",this.consolaInstance=W({formatOptions:{date:!1,colors:!0,compact:!0},fancy:!1});let t=this.isDaemonMode;this.consolaInstance.setReporters([{log:g(e=>{let n={info:"INFO",success:"SUCCESS",warn:"WARN",error:"ERROR",debug:"DEBUG",log:"LOG"},i={info:l.blue,success:l.green,warn:l.yellow,error:l.red,debug:l.gray,log:g(f=>f,"log")},o=n[e.type]||e.type.toUpperCase(),r=i[e.type]||(f=>f),a=j(new Date),C=r(`[${o}]`),m=`[${a}] ${C} ${e.args.join(" ")}`;if(!t)try{console.error(m)}catch(f){if(f instanceof Error&&f.message?.includes("EPIPE"))return;throw f}},"log")}])}initLogFile(t){this.logFilePath=H.join(t,"xiaozhi.log"),u.existsSync(this.logFilePath)||u.writeFileSync(this.logFilePath,""),this.writeStream=u.createWriteStream(this.logFilePath,{flags:"a",encoding:"utf8"})}logToFile(t,e,...n){if(this.writeStream){let o=`[${new Date().toISOString()}] [${t.toUpperCase()}] ${e}`,r=n.length>0?`${o} ${n.map(a=>typeof a=="object"?JSON.stringify(a):String(a)).join(" ")}`:o;this.writeStream.write(`${r}
2
+ `)}}enableFileLogging(t){t&&!this.writeStream&&this.logFilePath?this.writeStream=u.createWriteStream(this.logFilePath,{flags:"a",encoding:"utf8"}):!t&&this.writeStream&&(this.writeStream.end(),this.writeStream=null)}info(t,...e){this.consolaInstance.info(t,...e),this.logToFile("info",t,...e)}success(t,...e){this.consolaInstance.success(t,...e),this.logToFile("success",t,...e)}warn(t,...e){this.consolaInstance.warn(t,...e),this.logToFile("warn",t,...e)}error(t,...e){this.consolaInstance.error(t,...e),this.logToFile("error",t,...e)}debug(t,...e){this.consolaInstance.debug(t,...e),this.logToFile("debug",t,...e)}log(t,...e){this.consolaInstance.log(t,...e),this.logToFile("log",t,...e)}withTag(t){return this}close(){this.writeStream&&(this.writeStream.end(),this.writeStream=null)}},V=new d;var I=class{static{g(this,"WebServer")}httpServer;wss;logger;port;clientInfo={status:"disconnected",mcpEndpoint:"",activeMCPServers:[]};heartbeatTimeout;HEARTBEAT_TIMEOUT=35e3;constructor(t){if(t===void 0)try{this.port=c.getWebUIPort()}catch{this.port=9999}else this.port=t;this.logger=new d,this.httpServer=N((e,n)=>{this.handleHttpRequest(e,n)}),this.wss=new k({server:this.httpServer}),this.setupWebSocket()}async handleHttpRequest(t,e){let{pathname:n}=J(t.url||"",!0);if(e.setHeader("Access-Control-Allow-Origin","*"),e.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, OPTIONS"),e.setHeader("Access-Control-Allow-Headers","Content-Type"),t.method==="OPTIONS"){e.writeHead(200),e.end();return}try{if(t.method==="GET"&&!n?.startsWith("/api/")){await this.serveStaticFile(n||"/",e);return}if(n==="/api/config"&&t.method==="GET"){let i=c.getConfig();e.writeHead(200,{"Content-Type":"application/json"}),e.end(JSON.stringify(i))}else if(n==="/api/config"&&t.method==="PUT"){let i="";t.on("data",o=>{i+=o.toString()}),t.on("end",async()=>{try{let o=JSON.parse(i);this.updateConfig(o),e.writeHead(200,{"Content-Type":"application/json"}),e.end(JSON.stringify({success:!0})),this.broadcastConfigUpdate(o)}catch(o){e.writeHead(400,{"Content-Type":"application/json"}),e.end(JSON.stringify({error:o instanceof Error?o.message:String(o)}))}})}else n==="/api/status"&&t.method==="GET"?(e.writeHead(200,{"Content-Type":"application/json"}),e.end(JSON.stringify(this.clientInfo))):(e.writeHead(404),e.end("Not Found"))}catch(i){this.logger.error("HTTP request error:",i),e.writeHead(500,{"Content-Type":"application/json"}),e.end(JSON.stringify({error:"Internal Server Error"}))}}async serveStaticFile(t,e){try{let n=$(D(import.meta.url)),o=[p(n,"..","web","dist"),p(n,"..","web"),p(process.cwd(),"web","dist"),p(process.cwd(),"web")].find(h=>b(h));if(!o){e.writeHead(200,{"Content-Type":"text/html; charset=utf-8"}),e.end(`
3
3
  <!DOCTYPE html>
4
4
  <html>
5
5
  <head>
@@ -19,5 +19,5 @@ var M=Object.defineProperty;var g=(s,t)=>M(s,"name",{value:t,configurable:!0});i
19
19
  </div>
20
20
  </body>
21
21
  </html>
22
- `);return}let r=t;if(r==="/"&&(r="/index.html"),r.includes("..")){e.writeHead(403),e.end("Forbidden");return}let c=p(n,r);if(!v(c)){let h=p(n,"index.html");if(v(h)){let P=await I(h);e.writeHead(200,{"Content-Type":"text/html"}),e.end(P)}else e.writeHead(404),e.end("Not Found");return}let C=await I(c),m=c.split(".").pop()?.toLowerCase(),E={html:"text/html",js:"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"}[m||""]||"application/octet-stream";e.writeHead(200,{"Content-Type":E}),e.end(C)}catch(o){this.logger.error("Serve static file error:",o),e.writeHead(500),e.end("Internal Server Error")}}setupWebSocket(){this.wss.on("connection",t=>{this.logger.debug("WebSocket client connected"),t.on("message",async e=>{try{let o=JSON.parse(e.toString());await this.handleWebSocketMessage(t,o)}catch(o){this.logger.error("WebSocket message error:",o),t.send(JSON.stringify({type:"error",error:o instanceof Error?o.message:String(o)}))}}),t.on("close",()=>{this.logger.debug("WebSocket client disconnected")}),this.sendInitialData(t)})}async handleWebSocketMessage(t,e){switch(e.type){case"getConfig":{let o=a.getConfig();t.send(JSON.stringify({type:"config",data:o}));break}case"updateConfig":this.updateConfig(e.config),this.broadcastConfigUpdate(e.config);break;case"getStatus":t.send(JSON.stringify({type:"status",data:this.clientInfo}));break;case"clientStatus":this.updateClientInfo(e.data),this.broadcastStatusUpdate();break}}async sendInitialData(t){let e=a.getConfig();t.send(JSON.stringify({type:"config",data:e})),t.send(JSON.stringify({type:"status",data:this.clientInfo}))}broadcastConfigUpdate(t){let e=JSON.stringify({type:"configUpdate",data:t});for(let o of this.wss.clients)o.readyState===1&&o.send(e)}broadcastStatusUpdate(){let t=JSON.stringify({type:"statusUpdate",data:this.clientInfo});for(let e of this.wss.clients)e.readyState===1&&e.send(t)}updateClientInfo(t){this.clientInfo={...this.clientInfo,...t},t.lastHeartbeat&&(this.clientInfo.lastHeartbeat=Date.now()),t.status==="connected"&&this.resetHeartbeatTimeout()}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"}),this.broadcastStatusUpdate()},this.HEARTBEAT_TIMEOUT)}updateConfig(t){t.mcpEndpoint!==a.getMcpEndpoint()&&a.updateMcpEndpoint(t.mcpEndpoint);let e=a.getMcpServers();for(let[o,i]of Object.entries(t.mcpServers))JSON.stringify(e[o])!==JSON.stringify(i)&&a.updateMcpServer(o,i);for(let o of Object.keys(e))o in t.mcpServers||a.removeMcpServer(o);if(t.connection&&a.updateConnectionConfig(t.connection),t.modelscope&&a.updateModelScopeConfig(t.modelscope),t.webUI&&a.updateWebUIConfig(t.webUI),t.mcpServerConfig)for(let[o,i]of Object.entries(t.mcpServerConfig))for(let[n,r]of Object.entries(i.tools))a.setToolEnabled(o,n,r.enable)}updateStatus(t){this.updateClientInfo(t),this.broadcastStatusUpdate()}start(){return new Promise((t,e)=>{this.httpServer.listen(this.port,()=>{this.logger.info(`Web server listening on http://localhost:${this.port}`),t()}).on("error",e)})}stop(){return new Promise(t=>{this.heartbeatTimeout&&(clearTimeout(this.heartbeatTimeout),this.heartbeatTimeout=void 0);for(let e of this.wss.clients)e.terminate();this.wss.close(()=>{this.httpServer.close(()=>{this.logger.info("Web server stopped"),t()}),setTimeout(()=>{this.logger.info("Web server force stopped"),t()},2e3)})})}};export{T as WebServer};
22
+ `);return}let r=t;if(r==="/"&&(r="/index.html"),r.includes("..")){e.writeHead(403),e.end("Forbidden");return}let a=p(o,r);if(!b(a)){let h=p(o,"index.html");if(b(h)){let P=await E(h);e.writeHead(200,{"Content-Type":"text/html"}),e.end(P)}else e.writeHead(404),e.end("Not Found");return}let C=await E(a),m=a.split(".").pop()?.toLowerCase(),T={html:"text/html",js:"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"}[m||""]||"application/octet-stream";e.writeHead(200,{"Content-Type":T}),e.end(C)}catch(n){this.logger.error("Serve static file error:",n),e.writeHead(500),e.end("Internal Server Error")}}setupWebSocket(){this.wss.on("connection",t=>{this.logger.debug("WebSocket client connected"),t.on("message",async e=>{try{let n=JSON.parse(e.toString());await this.handleWebSocketMessage(t,n)}catch(n){this.logger.error("WebSocket message error:",n),t.send(JSON.stringify({type:"error",error:n instanceof Error?n.message:String(n)}))}}),t.on("close",()=>{this.logger.debug("WebSocket client disconnected")}),this.sendInitialData(t)})}async handleWebSocketMessage(t,e){switch(e.type){case"getConfig":{let n=c.getConfig();t.send(JSON.stringify({type:"config",data:n}));break}case"updateConfig":this.updateConfig(e.config),this.broadcastConfigUpdate(e.config);break;case"getStatus":t.send(JSON.stringify({type:"status",data:this.clientInfo}));break;case"clientStatus":this.updateClientInfo(e.data),this.broadcastStatusUpdate();break}}async sendInitialData(t){let e=c.getConfig();t.send(JSON.stringify({type:"config",data:e})),t.send(JSON.stringify({type:"status",data:this.clientInfo}))}broadcastConfigUpdate(t){let e=JSON.stringify({type:"configUpdate",data:t});for(let n of this.wss.clients)n.readyState===1&&n.send(e)}broadcastStatusUpdate(){let t=JSON.stringify({type:"statusUpdate",data:this.clientInfo});for(let e of this.wss.clients)e.readyState===1&&e.send(t)}updateClientInfo(t){this.clientInfo={...this.clientInfo,...t},t.lastHeartbeat&&(this.clientInfo.lastHeartbeat=Date.now()),t.status==="connected"&&this.resetHeartbeatTimeout()}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"}),this.broadcastStatusUpdate()},this.HEARTBEAT_TIMEOUT)}updateConfig(t){t.mcpEndpoint!==c.getMcpEndpoint()&&c.updateMcpEndpoint(t.mcpEndpoint);let e=c.getMcpServers();for(let[n,i]of Object.entries(t.mcpServers))JSON.stringify(e[n])!==JSON.stringify(i)&&c.updateMcpServer(n,i);for(let n of Object.keys(e))n in t.mcpServers||c.removeMcpServer(n);if(t.connection&&c.updateConnectionConfig(t.connection),t.modelscope&&c.updateModelScopeConfig(t.modelscope),t.webUI&&c.updateWebUIConfig(t.webUI),t.mcpServerConfig)for(let[n,i]of Object.entries(t.mcpServerConfig))for(let[o,r]of Object.entries(i.tools))c.setToolEnabled(n,o,r.enable)}updateStatus(t){this.updateClientInfo(t),this.broadcastStatusUpdate()}start(){return new Promise((t,e)=>{this.httpServer.listen(this.port,()=>{this.logger.info(`Web server listening on http://localhost:${this.port}`),t()}).on("error",e)})}stop(){return new Promise(t=>{this.heartbeatTimeout&&(clearTimeout(this.heartbeatTimeout),this.heartbeatTimeout=void 0);for(let e of this.wss.clients)e.terminate();this.wss.close(()=>{this.httpServer.close(()=>{this.logger.info("Web server stopped"),t()}),setTimeout(()=>{this.logger.info("Web server force stopped"),t()},2e3)})})}};export{I as WebServer};
23
23
  //# sourceMappingURL=webServer.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/webServer.ts","../src/configManager.ts","../src/logger.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { readFile } from \"node:fs/promises\";\nimport { createServer } from \"node:http\";\nimport { dirname, join } from \"node:path\";\nimport { parse } from \"node:url\";\nimport { fileURLToPath } from \"node:url\";\nimport { WebSocketServer } from \"ws\";\nimport { configManager } from \"./configManager.js\";\nimport type { AppConfig } from \"./configManager.js\";\nimport { Logger } from \"./logger.js\";\n\ninterface ClientInfo {\n status: \"connected\" | \"disconnected\";\n mcpEndpoint: string;\n activeMCPServers: string[];\n lastHeartbeat?: number;\n}\n\nexport class WebServer {\n private httpServer: ReturnType<typeof createServer>;\n private wss: WebSocketServer;\n private logger: Logger;\n private port: number;\n private clientInfo: ClientInfo = {\n status: \"disconnected\",\n mcpEndpoint: \"\",\n activeMCPServers: [],\n };\n private heartbeatTimeout?: NodeJS.Timeout;\n private readonly HEARTBEAT_TIMEOUT = 35000; // 35 seconds (slightly more than client's 30s interval)\n\n constructor(port?: number) {\n // 如果没有指定端口,从配置文件获取\n if (port === undefined) {\n try {\n this.port = configManager.getWebUIPort();\n } catch (error) {\n // 如果配置文件不存在或读取失败,使用默认端口\n this.port = 9999;\n }\n } else {\n this.port = port;\n }\n this.logger = new Logger();\n\n this.httpServer = createServer((req, res) => {\n this.handleHttpRequest(req, res);\n });\n\n this.wss = new WebSocketServer({ server: this.httpServer });\n this.setupWebSocket();\n }\n\n private async handleHttpRequest(req: any, res: any) {\n const { pathname } = parse(req.url || \"\", true);\n\n res.setHeader(\"Access-Control-Allow-Origin\", \"*\");\n res.setHeader(\"Access-Control-Allow-Methods\", \"GET, POST, PUT, OPTIONS\");\n res.setHeader(\"Access-Control-Allow-Headers\", \"Content-Type\");\n\n if (req.method === \"OPTIONS\") {\n res.writeHead(200);\n res.end();\n return;\n }\n\n try {\n // 提供静态文件\n if (req.method === \"GET\" && !pathname?.startsWith(\"/api/\")) {\n await this.serveStaticFile(pathname || \"/\", res);\n return;\n }\n\n if (pathname === \"/api/config\" && req.method === \"GET\") {\n const config = configManager.getConfig();\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify(config));\n } else if (pathname === \"/api/config\" && req.method === \"PUT\") {\n let body = \"\";\n req.on(\"data\", (chunk: any) => {\n body += chunk.toString();\n });\n req.on(\"end\", async () => {\n try {\n const newConfig: AppConfig = JSON.parse(body);\n this.updateConfig(newConfig);\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify({ success: true }));\n\n this.broadcastConfigUpdate(newConfig);\n } catch (error) {\n res.writeHead(400, { \"Content-Type\": \"application/json\" });\n res.end(\n JSON.stringify({\n error: error instanceof Error ? error.message : String(error),\n })\n );\n }\n });\n } else if (pathname === \"/api/status\" && req.method === \"GET\") {\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify(this.clientInfo));\n } else {\n res.writeHead(404);\n res.end(\"Not Found\");\n }\n } catch (error) {\n this.logger.error(\"HTTP request error:\", error);\n res.writeHead(500, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify({ error: \"Internal Server Error\" }));\n }\n }\n\n private async serveStaticFile(pathname: string, res: any) {\n try {\n // 获取当前文件所在目录\n const __dirname = dirname(fileURLToPath(import.meta.url));\n\n // 确定web目录路径\n const possibleWebPaths = [\n join(__dirname, \"..\", \"web\", \"dist\"), // 构建后的目录\n join(__dirname, \"..\", \"web\"), // 开发目录\n join(process.cwd(), \"web\", \"dist\"), // 当前工作目录\n join(process.cwd(), \"web\"),\n ];\n\n const webPath = possibleWebPaths.find((p) => existsSync(p));\n\n if (!webPath) {\n // 如果找不到 web 目录,返回简单的 HTML 页面\n res.writeHead(200, { \"Content-Type\": \"text/html; charset=utf-8\" });\n res.end(`\n <!DOCTYPE html>\n <html>\n <head>\n <title>小智配置管理</title>\n <meta charset=\"utf-8\">\n <style>\n body { font-family: sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }\n .error { color: #e53e3e; background: #fed7d7; padding: 20px; border-radius: 8px; }\n </style>\n </head>\n <body>\n <h1>小智配置管理</h1>\n <div class=\"error\">\n <p>错误:找不到前端资源文件。</p>\n <p>请先构建前端项目:</p>\n <pre>cd web && pnpm install && pnpm build</pre>\n </div>\n </body>\n </html>\n `);\n return;\n }\n\n // 处理路径\n let filePath = pathname;\n if (filePath === \"/\") {\n filePath = \"/index.html\";\n }\n\n // 安全性检查:防止路径遍历\n if (filePath.includes(\"..\")) {\n res.writeHead(403);\n res.end(\"Forbidden\");\n return;\n }\n\n const fullPath = join(webPath, filePath);\n\n // 检查文件是否存在\n if (!existsSync(fullPath)) {\n // 对于 SPA,返回 index.html\n const indexPath = join(webPath, \"index.html\");\n if (existsSync(indexPath)) {\n const content = await readFile(indexPath);\n res.writeHead(200, { \"Content-Type\": \"text/html\" });\n res.end(content);\n } else {\n res.writeHead(404);\n res.end(\"Not Found\");\n }\n return;\n }\n\n // 读取文件\n const content = await readFile(fullPath);\n\n // 设置正确的 Content-Type\n const ext = fullPath.split(\".\").pop()?.toLowerCase();\n const contentTypes: Record<string, string> = {\n html: \"text/html\",\n js: \"application/javascript\",\n css: \"text/css\",\n json: \"application/json\",\n png: \"image/png\",\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n gif: \"image/gif\",\n svg: \"image/svg+xml\",\n ico: \"image/x-icon\",\n };\n\n const contentType = contentTypes[ext || \"\"] || \"application/octet-stream\";\n res.writeHead(200, { \"Content-Type\": contentType });\n res.end(content);\n } catch (error) {\n this.logger.error(\"Serve static file error:\", error);\n res.writeHead(500);\n res.end(\"Internal Server Error\");\n }\n }\n\n private setupWebSocket() {\n this.wss.on(\"connection\", (ws) => {\n // 只在调试模式下输出连接日志\n this.logger.debug(\"WebSocket client connected\");\n\n ws.on(\"message\", async (message) => {\n try {\n const data = JSON.parse(message.toString());\n await this.handleWebSocketMessage(ws, data);\n } catch (error) {\n this.logger.error(\"WebSocket message error:\", error);\n ws.send(\n JSON.stringify({\n type: \"error\",\n error: error instanceof Error ? error.message : String(error),\n })\n );\n }\n });\n\n ws.on(\"close\", () => {\n // 只在调试模式下输出断开日志\n this.logger.debug(\"WebSocket client disconnected\");\n });\n\n this.sendInitialData(ws);\n });\n }\n\n private async handleWebSocketMessage(ws: any, data: any) {\n switch (data.type) {\n case \"getConfig\": {\n const config = configManager.getConfig();\n ws.send(JSON.stringify({ type: \"config\", data: config }));\n break;\n }\n\n case \"updateConfig\":\n this.updateConfig(data.config);\n this.broadcastConfigUpdate(data.config);\n break;\n\n case \"getStatus\":\n ws.send(JSON.stringify({ type: \"status\", data: this.clientInfo }));\n break;\n\n case \"clientStatus\":\n this.updateClientInfo(data.data);\n this.broadcastStatusUpdate();\n break;\n }\n }\n\n private async sendInitialData(ws: any) {\n const config = configManager.getConfig();\n ws.send(JSON.stringify({ type: \"config\", data: config }));\n ws.send(JSON.stringify({ type: \"status\", data: this.clientInfo }));\n }\n\n private broadcastConfigUpdate(config: AppConfig) {\n const message = JSON.stringify({ type: \"configUpdate\", data: config });\n for (const client of this.wss.clients) {\n if (client.readyState === 1) {\n client.send(message);\n }\n }\n }\n\n private broadcastStatusUpdate() {\n const message = JSON.stringify({\n type: \"statusUpdate\",\n data: this.clientInfo,\n });\n for (const client of this.wss.clients) {\n if (client.readyState === 1) {\n client.send(message);\n }\n }\n }\n\n private updateClientInfo(info: Partial<ClientInfo>) {\n this.clientInfo = { ...this.clientInfo, ...info };\n if (info.lastHeartbeat) {\n this.clientInfo.lastHeartbeat = Date.now();\n }\n\n // Reset heartbeat timeout when receiving client status\n if (info.status === \"connected\") {\n this.resetHeartbeatTimeout();\n }\n }\n\n private resetHeartbeatTimeout() {\n // Clear existing timeout\n if (this.heartbeatTimeout) {\n clearTimeout(this.heartbeatTimeout);\n }\n\n // Set new timeout\n this.heartbeatTimeout = setTimeout(() => {\n this.logger.warn(\"客户端心跳超时,标记为断开连接\");\n this.updateClientInfo({ status: \"disconnected\" });\n this.broadcastStatusUpdate();\n }, this.HEARTBEAT_TIMEOUT);\n }\n\n private updateConfig(newConfig: AppConfig) {\n // 更新 MCP 端点\n if (newConfig.mcpEndpoint !== configManager.getMcpEndpoint()) {\n configManager.updateMcpEndpoint(newConfig.mcpEndpoint);\n }\n\n // 更新 MCP 服务\n const currentServers = configManager.getMcpServers();\n for (const [name, config] of Object.entries(newConfig.mcpServers)) {\n if (JSON.stringify(currentServers[name]) !== JSON.stringify(config)) {\n configManager.updateMcpServer(name, config);\n }\n }\n\n // 删除不存在的服务\n for (const name of Object.keys(currentServers)) {\n if (!(name in newConfig.mcpServers)) {\n configManager.removeMcpServer(name);\n }\n }\n\n // 更新连接配置\n if (newConfig.connection) {\n configManager.updateConnectionConfig(newConfig.connection);\n }\n\n // 更新 ModelScope 配置\n if (newConfig.modelscope) {\n configManager.updateModelScopeConfig(newConfig.modelscope);\n }\n\n // 更新 Web UI 配置\n if (newConfig.webUI) {\n configManager.updateWebUIConfig(newConfig.webUI);\n }\n\n // 更新服务工具配置\n if (newConfig.mcpServerConfig) {\n for (const [serverName, toolsConfig] of Object.entries(\n newConfig.mcpServerConfig\n )) {\n for (const [toolName, toolConfig] of Object.entries(\n toolsConfig.tools\n )) {\n configManager.setToolEnabled(serverName, toolName, toolConfig.enable);\n // 注释:configManager 不支持直接设置工具描述,描述作为工具配置的一部分保存\n }\n }\n }\n }\n\n public updateStatus(info: Partial<ClientInfo>) {\n this.updateClientInfo(info);\n this.broadcastStatusUpdate();\n }\n\n public start(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.httpServer\n .listen(this.port, () => {\n this.logger.info(\n `Web server listening on http://localhost:${this.port}`\n );\n resolve();\n })\n .on(\"error\", reject);\n });\n }\n\n public stop(): Promise<void> {\n return new Promise((resolve) => {\n // Clear heartbeat timeout\n if (this.heartbeatTimeout) {\n clearTimeout(this.heartbeatTimeout);\n this.heartbeatTimeout = undefined;\n }\n\n // 强制断开所有 WebSocket 客户端连接\n for (const client of this.wss.clients) {\n client.terminate();\n }\n\n // 关闭 WebSocket 服务器\n this.wss.close(() => {\n // 强制关闭 HTTP 服务器,不等待现有连接\n this.httpServer.close(() => {\n this.logger.info(\"Web server stopped\");\n resolve();\n });\n\n // 设置超时,如果 2 秒内没有关闭则强制退出\n setTimeout(() => {\n this.logger.info(\"Web server force stopped\");\n resolve();\n }, 2000);\n });\n });\n }\n}\n","import { copyFileSync, existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\n// 在 ESM 中,需要从 import.meta.url 获取当前文件目录\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\n// 默认连接配置\nconst DEFAULT_CONNECTION_CONFIG: Required<ConnectionConfig> = {\n heartbeatInterval: 30000, // 30秒心跳间隔\n heartbeatTimeout: 10000, // 10秒心跳超时\n reconnectInterval: 5000, // 5秒重连间隔\n};\n\n// 配置文件接口定义\n// 本地 MCP 服务配置\nexport interface LocalMCPServerConfig {\n command: string;\n args: string[];\n env?: Record<string, string>;\n}\n\n// ModelScope SSE MCP 服务配置\nexport interface SSEMCPServerConfig {\n type: \"sse\";\n url: string;\n}\n\n// 统一的 MCP 服务配置\nexport type MCPServerConfig = LocalMCPServerConfig | SSEMCPServerConfig;\n\nexport interface MCPToolConfig {\n description?: string;\n enable: boolean;\n}\n\nexport interface MCPServerToolsConfig {\n tools: Record<string, MCPToolConfig>;\n}\n\nexport interface ConnectionConfig {\n heartbeatInterval?: number; // 心跳检测间隔(毫秒),默认30000\n heartbeatTimeout?: number; // 心跳超时时间(毫秒),默认10000\n reconnectInterval?: number; // 重连间隔(毫秒),默认5000\n}\n\nexport interface ModelScopeConfig {\n apiKey?: string; // ModelScope API 密钥\n}\n\nexport interface WebUIConfig {\n port?: number; // Web UI 端口号,默认 9999\n}\n\nexport interface AppConfig {\n mcpEndpoint: string;\n mcpServers: Record<string, MCPServerConfig>;\n mcpServerConfig?: Record<string, MCPServerToolsConfig>;\n connection?: ConnectionConfig; // 连接配置(可选,用于向后兼容)\n modelscope?: ModelScopeConfig; // ModelScope 配置(可选)\n webUI?: WebUIConfig; // Web UI 配置(可选)\n}\n\n/**\n * 配置管理类\n * 负责管理应用配置,提供只读访问和安全的配置更新功能\n */\nexport class ConfigManager {\n private static instance: ConfigManager;\n private defaultConfigPath: string;\n private config: AppConfig | null = null;\n\n private constructor() {\n this.defaultConfigPath = resolve(__dirname, \"xiaozhi.config.default.json\");\n }\n\n /**\n * 获取配置文件路径(动态计算)\n */\n private getConfigFilePath(): string {\n // 配置文件路径 - 优先使用环境变量指定的目录,否则使用当前工作目录\n const configDir = process.env.XIAOZHI_CONFIG_DIR || process.cwd();\n return resolve(configDir, \"xiaozhi.config.json\");\n }\n\n /**\n * 获取配置管理器单例实例\n */\n public static getInstance(): ConfigManager {\n if (!ConfigManager.instance) {\n ConfigManager.instance = new ConfigManager();\n }\n return ConfigManager.instance;\n }\n\n /**\n * 检查配置文件是否存在\n */\n public configExists(): boolean {\n const configPath = this.getConfigFilePath();\n return existsSync(configPath);\n }\n\n /**\n * 初始化配置文件\n * 从 config.default.json 复制到 config.json\n */\n public initConfig(): void {\n if (!existsSync(this.defaultConfigPath)) {\n throw new Error(\"默认配置文件 xiaozhi.config.default.json 不存在\");\n }\n\n if (this.configExists()) {\n throw new Error(\"配置文件 xiaozhi.config.json 已存在,无需重复初始化\");\n }\n\n const configPath = this.getConfigFilePath();\n copyFileSync(this.defaultConfigPath, configPath);\n this.config = null; // 重置缓存\n }\n\n /**\n * 加载配置文件\n */\n private loadConfig(): AppConfig {\n if (!this.configExists()) {\n throw new Error(\n \"配置文件 xiaozhi.config.json 不存在,请先运行 xiaozhi init 初始化配置\"\n );\n }\n\n try {\n const configPath = this.getConfigFilePath();\n const configData = readFileSync(configPath, \"utf8\");\n const config = JSON.parse(configData) as AppConfig;\n\n // 验证配置结构\n this.validateConfig(config);\n\n return config;\n } catch (error) {\n if (error instanceof SyntaxError) {\n throw new Error(`配置文件格式错误: ${error.message}`);\n }\n throw error;\n }\n }\n\n /**\n * 验证配置文件结构\n */\n private validateConfig(config: unknown): void {\n if (!config || typeof config !== \"object\") {\n throw new Error(\"配置文件格式错误:根对象无效\");\n }\n\n const configObj = config as Record<string, unknown>;\n\n if (!configObj.mcpEndpoint || typeof configObj.mcpEndpoint !== \"string\") {\n throw new Error(\"配置文件格式错误:mcpEndpoint 字段无效\");\n }\n\n if (!configObj.mcpServers || typeof configObj.mcpServers !== \"object\") {\n throw new Error(\"配置文件格式错误:mcpServers 字段无效\");\n }\n\n // 验证每个 MCP 服务配置\n for (const [serverName, serverConfig] of Object.entries(\n configObj.mcpServers as Record<string, unknown>\n )) {\n if (!serverConfig || typeof serverConfig !== \"object\") {\n throw new Error(`配置文件格式错误:mcpServers.${serverName} 无效`);\n }\n\n const sc = serverConfig as Record<string, unknown>;\n\n // 检查是否是 SSE 类型\n if (sc.type === \"sse\") {\n // SSE 类型的验证\n if (!sc.url || typeof sc.url !== \"string\") {\n throw new Error(\n `配置文件格式错误:mcpServers.${serverName}.url 无效`\n );\n }\n } else {\n // 本地类型的验证\n if (!sc.command || typeof sc.command !== \"string\") {\n throw new Error(\n `配置文件格式错误:mcpServers.${serverName}.command 无效`\n );\n }\n\n if (!Array.isArray(sc.args)) {\n throw new Error(\n `配置文件格式错误:mcpServers.${serverName}.args 必须是数组`\n );\n }\n\n if (sc.env && typeof sc.env !== \"object\") {\n throw new Error(\n `配置文件格式错误:mcpServers.${serverName}.env 必须是对象`\n );\n }\n }\n }\n }\n\n /**\n * 获取配置(只读)\n */\n public getConfig(): Readonly<AppConfig> {\n if (!this.config) {\n this.config = this.loadConfig();\n }\n\n // 返回深度只读副本\n return JSON.parse(JSON.stringify(this.config));\n }\n\n /**\n * 获取 MCP 端点\n */\n public getMcpEndpoint(): string {\n const config = this.getConfig();\n return config.mcpEndpoint;\n }\n\n /**\n * 获取 MCP 服务配置\n */\n public getMcpServers(): Readonly<Record<string, MCPServerConfig>> {\n const config = this.getConfig();\n return config.mcpServers;\n }\n\n /**\n * 获取 MCP 服务工具配置\n */\n public getMcpServerConfig(): Readonly<Record<string, MCPServerToolsConfig>> {\n const config = this.getConfig();\n return config.mcpServerConfig || {};\n }\n\n /**\n * 获取指定服务的工具配置\n */\n public getServerToolsConfig(\n serverName: string\n ): Readonly<Record<string, MCPToolConfig>> {\n const serverConfig = this.getMcpServerConfig();\n return serverConfig[serverName]?.tools || {};\n }\n\n /**\n * 检查工具是否启用\n */\n public isToolEnabled(serverName: string, toolName: string): boolean {\n const toolsConfig = this.getServerToolsConfig(serverName);\n const toolConfig = toolsConfig[toolName];\n return toolConfig?.enable !== false; // 默认启用\n }\n\n /**\n * 更新 MCP 端点\n */\n public updateMcpEndpoint(endpoint: string): void {\n if (!endpoint || typeof endpoint !== \"string\") {\n throw new Error(\"MCP 端点必须是非空字符串\");\n }\n\n const config = this.getConfig();\n const newConfig = { ...config, mcpEndpoint: endpoint };\n this.saveConfig(newConfig);\n }\n\n /**\n * 更新 MCP 服务配置\n */\n public updateMcpServer(\n serverName: string,\n serverConfig: MCPServerConfig\n ): void {\n if (!serverName || typeof serverName !== \"string\") {\n throw new Error(\"服务名称必须是非空字符串\");\n }\n\n // 验证服务配置\n if (\"type\" in serverConfig && serverConfig.type === \"sse\") {\n // SSE 类型的验证\n if (!serverConfig.url || typeof serverConfig.url !== \"string\") {\n throw new Error(\"SSE 服务配置的 url 字段必须是非空字符串\");\n }\n } else {\n // 本地类型的验证\n const localConfig = serverConfig as LocalMCPServerConfig;\n if (!localConfig.command || typeof localConfig.command !== \"string\") {\n throw new Error(\"服务配置的 command 字段必须是非空字符串\");\n }\n\n if (!Array.isArray(localConfig.args)) {\n throw new Error(\"服务配置的 args 字段必须是数组\");\n }\n\n if (localConfig.env && typeof localConfig.env !== \"object\") {\n throw new Error(\"服务配置的 env 字段必须是对象\");\n }\n }\n\n const config = this.getConfig();\n const newConfig = {\n ...config,\n mcpServers: {\n ...config.mcpServers,\n [serverName]: serverConfig,\n },\n };\n this.saveConfig(newConfig);\n }\n\n /**\n * 删除 MCP 服务配置\n */\n public removeMcpServer(serverName: string): void {\n if (!serverName || typeof serverName !== \"string\") {\n throw new Error(\"服务名称必须是非空字符串\");\n }\n\n const config = this.getConfig();\n if (!config.mcpServers[serverName]) {\n throw new Error(`服务 ${serverName} 不存在`);\n }\n\n const newMcpServers = { ...config.mcpServers };\n delete newMcpServers[serverName];\n\n const newConfig = {\n ...config,\n mcpServers: newMcpServers,\n };\n this.saveConfig(newConfig);\n }\n\n /**\n * 更新服务工具配置\n */\n public updateServerToolsConfig(\n serverName: string,\n toolsConfig: Record<string, MCPToolConfig>\n ): void {\n const config = this.getConfig();\n const newConfig = { ...config };\n\n // 确保 mcpServerConfig 存在\n if (!newConfig.mcpServerConfig) {\n newConfig.mcpServerConfig = {};\n }\n\n // 更新指定服务的工具配置\n newConfig.mcpServerConfig[serverName] = {\n tools: toolsConfig,\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 设置工具启用状态\n */\n public setToolEnabled(\n serverName: string,\n toolName: string,\n enabled: boolean,\n description?: string\n ): void {\n const config = this.getConfig();\n const newConfig = { ...config };\n\n // 确保 mcpServerConfig 存在\n if (!newConfig.mcpServerConfig) {\n newConfig.mcpServerConfig = {};\n }\n\n // 确保服务配置存在\n if (!newConfig.mcpServerConfig[serverName]) {\n newConfig.mcpServerConfig[serverName] = { tools: {} };\n }\n\n // 更新工具配置\n newConfig.mcpServerConfig[serverName].tools[toolName] = {\n enable: enabled,\n ...(description && { description }),\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 保存配置到文件\n */\n private saveConfig(config: AppConfig): void {\n try {\n // 验证配置\n this.validateConfig(config);\n\n // 格式化 JSON 并保存\n const configPath = this.getConfigFilePath();\n const configJson = JSON.stringify(config, null, 2);\n writeFileSync(configPath, configJson, \"utf8\");\n\n // 更新缓存\n this.config = config;\n } catch (error) {\n throw new Error(\n `保存配置失败: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n }\n\n /**\n * 重新加载配置(清除缓存)\n */\n public reloadConfig(): void {\n this.config = null;\n }\n\n /**\n * 获取配置文件路径\n */\n public getConfigPath(): string {\n return this.getConfigFilePath();\n }\n\n /**\n * 获取默认配置文件路径\n */\n public getDefaultConfigPath(): string {\n return this.defaultConfigPath;\n }\n\n /**\n * 获取连接配置(包含默认值)\n */\n public getConnectionConfig(): Required<ConnectionConfig> {\n const config = this.getConfig();\n const connectionConfig = config.connection || {};\n\n return {\n heartbeatInterval:\n connectionConfig.heartbeatInterval ??\n DEFAULT_CONNECTION_CONFIG.heartbeatInterval,\n heartbeatTimeout:\n connectionConfig.heartbeatTimeout ??\n DEFAULT_CONNECTION_CONFIG.heartbeatTimeout,\n reconnectInterval:\n connectionConfig.reconnectInterval ??\n DEFAULT_CONNECTION_CONFIG.reconnectInterval,\n };\n }\n\n /**\n * 获取心跳检测间隔(毫秒)\n */\n public getHeartbeatInterval(): number {\n return this.getConnectionConfig().heartbeatInterval;\n }\n\n /**\n * 获取心跳超时时间(毫秒)\n */\n public getHeartbeatTimeout(): number {\n return this.getConnectionConfig().heartbeatTimeout;\n }\n\n /**\n * 获取重连间隔(毫秒)\n */\n public getReconnectInterval(): number {\n return this.getConnectionConfig().reconnectInterval;\n }\n\n /**\n * 更新连接配置\n */\n public updateConnectionConfig(\n connectionConfig: Partial<ConnectionConfig>\n ): void {\n const config = this.getConfig();\n const currentConnectionConfig = config.connection || {};\n\n const newConnectionConfig = {\n ...currentConnectionConfig,\n ...connectionConfig,\n };\n\n const newConfig = {\n ...config,\n connection: newConnectionConfig,\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 设置心跳检测间隔\n */\n public setHeartbeatInterval(interval: number): void {\n if (interval <= 0) {\n throw new Error(\"心跳检测间隔必须大于0\");\n }\n this.updateConnectionConfig({ heartbeatInterval: interval });\n }\n\n /**\n * 设置心跳超时时间\n */\n public setHeartbeatTimeout(timeout: number): void {\n if (timeout <= 0) {\n throw new Error(\"心跳超时时间必须大于0\");\n }\n this.updateConnectionConfig({ heartbeatTimeout: timeout });\n }\n\n /**\n * 设置重连间隔\n */\n public setReconnectInterval(interval: number): void {\n if (interval <= 0) {\n throw new Error(\"重连间隔必须大于0\");\n }\n this.updateConnectionConfig({ reconnectInterval: interval });\n }\n\n /**\n * 获取 ModelScope 配置\n */\n public getModelScopeConfig(): Readonly<ModelScopeConfig> {\n const config = this.getConfig();\n return config.modelscope || {};\n }\n\n /**\n * 获取 ModelScope API Key\n * 优先从配置文件读取,其次从环境变量读取\n */\n public getModelScopeApiKey(): string | undefined {\n const modelScopeConfig = this.getModelScopeConfig();\n return modelScopeConfig.apiKey || process.env.MODELSCOPE_API_TOKEN;\n }\n\n /**\n * 更新 ModelScope 配置\n */\n public updateModelScopeConfig(\n modelScopeConfig: Partial<ModelScopeConfig>\n ): void {\n const config = this.getConfig();\n const currentModelScopeConfig = config.modelscope || {};\n\n const newModelScopeConfig = {\n ...currentModelScopeConfig,\n ...modelScopeConfig,\n };\n\n const newConfig = {\n ...config,\n modelscope: newModelScopeConfig,\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 设置 ModelScope API Key\n */\n public setModelScopeApiKey(apiKey: string): void {\n if (!apiKey || typeof apiKey !== \"string\") {\n throw new Error(\"API Key 必须是非空字符串\");\n }\n this.updateModelScopeConfig({ apiKey });\n }\n\n /**\n * 获取 Web UI 配置\n */\n public getWebUIConfig(): Readonly<WebUIConfig> {\n const config = this.getConfig();\n return config.webUI || {};\n }\n\n /**\n * 获取 Web UI 端口号\n */\n public getWebUIPort(): number {\n const webUIConfig = this.getWebUIConfig();\n return webUIConfig.port ?? 9999; // 默认端口 9999\n }\n\n /**\n * 更新 Web UI 配置\n */\n public updateWebUIConfig(webUIConfig: Partial<WebUIConfig>): void {\n const config = this.getConfig();\n const currentWebUIConfig = config.webUI || {};\n\n const newWebUIConfig = {\n ...currentWebUIConfig,\n ...webUIConfig,\n };\n\n const newConfig = {\n ...config,\n webUI: newWebUIConfig,\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 设置 Web UI 端口号\n */\n public setWebUIPort(port: number): void {\n if (!Number.isInteger(port) || port <= 0 || port > 65535) {\n throw new Error(\"端口号必须是 1-65535 之间的整数\");\n }\n this.updateWebUIConfig({ port });\n }\n}\n\n// 导出单例实例\nexport const configManager = ConfigManager.getInstance();\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport chalk from \"chalk\";\nimport { type consola, createConsola } from \"consola\";\n\nfunction formatDateTime(date: Date) {\n const year = date.getFullYear();\n const month = String(date.getMonth() + 1).padStart(2, \"0\");\n const day = String(date.getDate()).padStart(2, \"0\");\n const hours = String(date.getHours()).padStart(2, \"0\");\n const minutes = String(date.getMinutes()).padStart(2, \"0\");\n const seconds = String(date.getSeconds()).padStart(2, \"0\");\n\n return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;\n}\n\nexport class Logger {\n private logFilePath: string | null = null;\n private writeStream: fs.WriteStream | null = null;\n private consolaInstance: typeof consola;\n private isDaemonMode: boolean;\n\n constructor() {\n // 检查是否为守护进程模式\n this.isDaemonMode = process.env.XIAOZHI_DAEMON === \"true\";\n // 创建自定义的 consola 实例,禁用图标并自定义格式\n this.consolaInstance = createConsola({\n formatOptions: {\n date: false,\n colors: true,\n compact: true,\n },\n fancy: false,\n });\n\n // 保存对当前实例的引用,以便在闭包中访问\n const isDaemonMode = this.isDaemonMode;\n\n // 自定义格式化器\n this.consolaInstance.setReporters([\n {\n log: (logObj) => {\n const levelMap: Record<string, string> = {\n info: \"INFO\",\n success: \"SUCCESS\",\n warn: \"WARN\",\n error: \"ERROR\",\n debug: \"DEBUG\",\n log: \"LOG\",\n };\n\n const colorMap: Record<string, (text: string) => string> = {\n info: chalk.blue,\n success: chalk.green,\n warn: chalk.yellow,\n error: chalk.red,\n debug: chalk.gray,\n log: (text: string) => text,\n };\n\n const level = levelMap[logObj.type] || logObj.type.toUpperCase();\n const colorFn = colorMap[logObj.type] || ((text: string) => text);\n const timestamp = formatDateTime(new Date());\n\n // 为级别添加颜色\n const coloredLevel = colorFn(`[${level}]`);\n const message = `[${timestamp}] ${coloredLevel} ${logObj.args.join(\n \" \"\n )}`;\n\n // 守护进程模式下不输出到控制台,只写入文件\n if (!isDaemonMode) {\n // 输出到 stderr(与原来保持一致)\n try {\n console.error(message);\n } catch (error) {\n // 忽略 EPIPE 错误\n if (error instanceof Error && error.message?.includes(\"EPIPE\")) {\n return;\n }\n throw error;\n }\n }\n },\n },\n ]);\n }\n\n /**\n * 初始化日志文件\n * @param projectDir 项目目录\n */\n initLogFile(projectDir: string): void {\n this.logFilePath = path.join(projectDir, \"xiaozhi.log\");\n\n // 确保日志文件存在\n if (!fs.existsSync(this.logFilePath)) {\n fs.writeFileSync(this.logFilePath, \"\");\n }\n\n // 创建写入流,追加模式\n this.writeStream = fs.createWriteStream(this.logFilePath, {\n flags: \"a\",\n encoding: \"utf8\",\n });\n }\n\n /**\n * 记录日志到文件\n * @param level 日志级别\n * @param message 日志消息\n * @param args 额外参数\n */\n private logToFile(level: string, message: string, ...args: any[]): void {\n if (this.writeStream) {\n const timestamp = new Date().toISOString();\n const formattedMessage = `[${timestamp}] [${level.toUpperCase()}] ${message}`;\n const fullMessage =\n args.length > 0\n ? `${formattedMessage} ${args\n .map((arg) =>\n typeof arg === \"object\" ? JSON.stringify(arg) : String(arg)\n )\n .join(\" \")}`\n : formattedMessage;\n\n this.writeStream.write(`${fullMessage}\\n`);\n }\n }\n\n /**\n * 设置是否启用文件日志\n * @param enable 是否启用\n */\n enableFileLogging(enable: boolean): void {\n if (enable && !this.writeStream && this.logFilePath) {\n this.writeStream = fs.createWriteStream(this.logFilePath, {\n flags: \"a\",\n encoding: \"utf8\",\n });\n } else if (!enable && this.writeStream) {\n this.writeStream.end();\n this.writeStream = null;\n }\n }\n\n /**\n * 日志方法\n */\n info(message: string, ...args: any[]): void {\n this.consolaInstance.info(message, ...args);\n this.logToFile(\"info\", message, ...args);\n }\n\n success(message: string, ...args: any[]): void {\n this.consolaInstance.success(message, ...args);\n this.logToFile(\"success\", message, ...args);\n }\n\n warn(message: string, ...args: any[]): void {\n this.consolaInstance.warn(message, ...args);\n this.logToFile(\"warn\", message, ...args);\n }\n\n error(message: string, ...args: any[]): void {\n this.consolaInstance.error(message, ...args);\n this.logToFile(\"error\", message, ...args);\n }\n\n debug(message: string, ...args: any[]): void {\n this.consolaInstance.debug(message, ...args);\n this.logToFile(\"debug\", message, ...args);\n }\n\n log(message: string, ...args: any[]): void {\n this.consolaInstance.log(message, ...args);\n this.logToFile(\"log\", message, ...args);\n }\n\n /**\n * 创建一个带标签的日志实例(已废弃,直接返回原实例)\n * @param tag 标签(不再使用)\n * @deprecated 标签功能已移除\n */\n withTag(tag: string): Logger {\n // 不再添加标签,直接返回共享实例\n return this;\n }\n\n /**\n * 关闭日志文件流\n */\n close(): void {\n if (this.writeStream) {\n this.writeStream.end();\n this.writeStream = null;\n }\n }\n}\n\n// 导出单例实例\nexport const logger = new Logger();\n"],"mappings":"+EAAA,OAAS,cAAAA,MAAkB,KAC3B,OAAS,YAAAC,MAAgB,cACzB,OAAS,gBAAAC,MAAoB,OAC7B,OAAS,WAAAC,EAAS,QAAAC,MAAY,OAC9B,OAAS,SAAAC,MAAa,MACtB,OAAS,iBAAAC,MAAqB,MAC9B,OAAS,mBAAAC,MAAuB,KCNhC,OAAS,gBAAAC,EAAc,cAAAC,EAAY,gBAAAC,EAAc,iBAAAC,MAAqB,KACtE,OAAS,WAAAC,EAAS,WAAAC,MAAe,OACjC,OAAS,iBAAAC,MAAqB,MAG9B,IAAMC,EAAYC,EAAQC,EAAc,YAAY,GAAG,CAAC,EAGlDC,EAAwD,CAC5D,kBAAmB,IACnB,iBAAkB,IAClB,kBAAmB,GACrB,EAuDaC,EAAN,MAAMC,CAAc,CAnE3B,MAmE2B,CAAAC,EAAA,sBACzB,OAAe,SACP,kBACA,OAA2B,KAE3B,aAAc,CACpB,KAAK,kBAAoBC,EAAQP,EAAW,6BAA6B,CAC3E,CAKQ,mBAA4B,CAElC,IAAMQ,EAAY,QAAQ,IAAI,oBAAsB,QAAQ,IAAI,EAChE,OAAOD,EAAQC,EAAW,qBAAqB,CACjD,CAKA,OAAc,aAA6B,CACzC,OAAKH,EAAc,WACjBA,EAAc,SAAW,IAAIA,GAExBA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ACAD,EAAQ,CACV,CAAC,EACA,GAAG,QAASC,CAAM,CACvB,CAAC,CACH,CAEO,MAAsB,CAC3B,OAAO,IAAI,QAASD,GAAY,CAE1B,KAAK,mBACP,aAAa,KAAK,gBAAgB,EAClC,KAAK,iBAAmB,QAI1B,QAAWR,KAAU,KAAK,IAAI,QAC5BA,EAAO,UAAU,EAInB,KAAK,IAAI,MAAM,IAAM,CAEnB,KAAK,WAAW,MAAM,IAAM,CAC1B,KAAK,OAAO,KAAK,oBAAoB,EACrCQ,EAAQ,CACV,CAAC,EAGD,WAAW,IAAM,CACf,KAAK,OAAO,KAAK,0BAA0B,EAC3CA,EAAQ,CACV,EAAG,GAAI,CACT,CAAC,CACH,CAAC,CACH,CACF","names":["existsSync","readFile","createServer","dirname","join","parse","fileURLToPath","WebSocketServer","copyFileSync","existsSync","readFileSync","writeFileSync","dirname","resolve","fileURLToPath","__dirname","dirname","fileURLToPath","DEFAULT_CONNECTION_CONFIG","ConfigManager","_ConfigManager","__name","resolve","configDir","configPath","existsSync","copyFileSync","configData","readFileSync","config","error","configObj","serverName","serverConfig","sc","toolName","endpoint","newConfig","localConfig","newMcpServers","toolsConfig","enabled","description","configJson","writeFileSync","connectionConfig","newConnectionConfig","interval","timeout","modelScopeConfig","newModelScopeConfig","apiKey","webUIConfig","newWebUIConfig","port","configManager","fs","path","chalk","createConsola","formatDateTime","date","year","month","day","hours","minutes","seconds","__name","Logger","createConsola","isDaemonMode","logObj","levelMap","colorMap","chalk","text","level","colorFn","timestamp","coloredLevel","message","error","projectDir","path","fs","args","formattedMessage","fullMessage","arg","enable","tag","logger","WebServer","__name","port","configManager","Logger","createServer","req","res","WebSocketServer","pathname","parse","config","body","chunk","newConfig","error","__dirname","dirname","fileURLToPath","webPath","join","p","existsSync","filePath","fullPath","indexPath","content","readFile","ext","contentType","ws","message","data","client","info","currentServers","name","serverName","toolsConfig","toolName","toolConfig","resolve","reject"]}
1
+ {"version":3,"sources":["../src/webServer.ts","../src/configManager.ts","../src/logger.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { readFile } from \"node:fs/promises\";\nimport { createServer } from \"node:http\";\nimport { dirname, join } from \"node:path\";\nimport { parse } from \"node:url\";\nimport { fileURLToPath } from \"node:url\";\nimport { WebSocketServer } from \"ws\";\nimport { configManager } from \"./configManager.js\";\nimport type { AppConfig } from \"./configManager.js\";\nimport { Logger } from \"./logger.js\";\n\ninterface ClientInfo {\n status: \"connected\" | \"disconnected\";\n mcpEndpoint: string;\n activeMCPServers: string[];\n lastHeartbeat?: number;\n}\n\nexport class WebServer {\n private httpServer: ReturnType<typeof createServer>;\n private wss: WebSocketServer;\n private logger: Logger;\n private port: number;\n private clientInfo: ClientInfo = {\n status: \"disconnected\",\n mcpEndpoint: \"\",\n activeMCPServers: [],\n };\n private heartbeatTimeout?: NodeJS.Timeout;\n private readonly HEARTBEAT_TIMEOUT = 35000; // 35 seconds (slightly more than client's 30s interval)\n\n constructor(port?: number) {\n // 如果没有指定端口,从配置文件获取\n if (port === undefined) {\n try {\n this.port = configManager.getWebUIPort();\n } catch (error) {\n // 如果配置文件不存在或读取失败,使用默认端口\n this.port = 9999;\n }\n } else {\n this.port = port;\n }\n this.logger = new Logger();\n\n this.httpServer = createServer((req, res) => {\n this.handleHttpRequest(req, res);\n });\n\n this.wss = new WebSocketServer({ server: this.httpServer });\n this.setupWebSocket();\n }\n\n private async handleHttpRequest(req: any, res: any) {\n const { pathname } = parse(req.url || \"\", true);\n\n res.setHeader(\"Access-Control-Allow-Origin\", \"*\");\n res.setHeader(\"Access-Control-Allow-Methods\", \"GET, POST, PUT, OPTIONS\");\n res.setHeader(\"Access-Control-Allow-Headers\", \"Content-Type\");\n\n if (req.method === \"OPTIONS\") {\n res.writeHead(200);\n res.end();\n return;\n }\n\n try {\n // 提供静态文件\n if (req.method === \"GET\" && !pathname?.startsWith(\"/api/\")) {\n await this.serveStaticFile(pathname || \"/\", res);\n return;\n }\n\n if (pathname === \"/api/config\" && req.method === \"GET\") {\n const config = configManager.getConfig();\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify(config));\n } else if (pathname === \"/api/config\" && req.method === \"PUT\") {\n let body = \"\";\n req.on(\"data\", (chunk: any) => {\n body += chunk.toString();\n });\n req.on(\"end\", async () => {\n try {\n const newConfig: AppConfig = JSON.parse(body);\n this.updateConfig(newConfig);\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify({ success: true }));\n\n this.broadcastConfigUpdate(newConfig);\n } catch (error) {\n res.writeHead(400, { \"Content-Type\": \"application/json\" });\n res.end(\n JSON.stringify({\n error: error instanceof Error ? error.message : String(error),\n })\n );\n }\n });\n } else if (pathname === \"/api/status\" && req.method === \"GET\") {\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify(this.clientInfo));\n } else {\n res.writeHead(404);\n res.end(\"Not Found\");\n }\n } catch (error) {\n this.logger.error(\"HTTP request error:\", error);\n res.writeHead(500, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify({ error: \"Internal Server Error\" }));\n }\n }\n\n private async serveStaticFile(pathname: string, res: any) {\n try {\n // 获取当前文件所在目录\n const __dirname = dirname(fileURLToPath(import.meta.url));\n\n // 确定web目录路径\n const possibleWebPaths = [\n join(__dirname, \"..\", \"web\", \"dist\"), // 构建后的目录\n join(__dirname, \"..\", \"web\"), // 开发目录\n join(process.cwd(), \"web\", \"dist\"), // 当前工作目录\n join(process.cwd(), \"web\"),\n ];\n\n const webPath = possibleWebPaths.find((p) => existsSync(p));\n\n if (!webPath) {\n // 如果找不到 web 目录,返回简单的 HTML 页面\n res.writeHead(200, { \"Content-Type\": \"text/html; charset=utf-8\" });\n res.end(`\n <!DOCTYPE html>\n <html>\n <head>\n <title>小智配置管理</title>\n <meta charset=\"utf-8\">\n <style>\n body { font-family: sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }\n .error { color: #e53e3e; background: #fed7d7; padding: 20px; border-radius: 8px; }\n </style>\n </head>\n <body>\n <h1>小智配置管理</h1>\n <div class=\"error\">\n <p>错误:找不到前端资源文件。</p>\n <p>请先构建前端项目:</p>\n <pre>cd web && pnpm install && pnpm build</pre>\n </div>\n </body>\n </html>\n `);\n return;\n }\n\n // 处理路径\n let filePath = pathname;\n if (filePath === \"/\") {\n filePath = \"/index.html\";\n }\n\n // 安全性检查:防止路径遍历\n if (filePath.includes(\"..\")) {\n res.writeHead(403);\n res.end(\"Forbidden\");\n return;\n }\n\n const fullPath = join(webPath, filePath);\n\n // 检查文件是否存在\n if (!existsSync(fullPath)) {\n // 对于 SPA,返回 index.html\n const indexPath = join(webPath, \"index.html\");\n if (existsSync(indexPath)) {\n const content = await readFile(indexPath);\n res.writeHead(200, { \"Content-Type\": \"text/html\" });\n res.end(content);\n } else {\n res.writeHead(404);\n res.end(\"Not Found\");\n }\n return;\n }\n\n // 读取文件\n const content = await readFile(fullPath);\n\n // 设置正确的 Content-Type\n const ext = fullPath.split(\".\").pop()?.toLowerCase();\n const contentTypes: Record<string, string> = {\n html: \"text/html\",\n js: \"application/javascript\",\n css: \"text/css\",\n json: \"application/json\",\n png: \"image/png\",\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n gif: \"image/gif\",\n svg: \"image/svg+xml\",\n ico: \"image/x-icon\",\n };\n\n const contentType = contentTypes[ext || \"\"] || \"application/octet-stream\";\n res.writeHead(200, { \"Content-Type\": contentType });\n res.end(content);\n } catch (error) {\n this.logger.error(\"Serve static file error:\", error);\n res.writeHead(500);\n res.end(\"Internal Server Error\");\n }\n }\n\n private setupWebSocket() {\n this.wss.on(\"connection\", (ws) => {\n // 只在调试模式下输出连接日志\n this.logger.debug(\"WebSocket client connected\");\n\n ws.on(\"message\", async (message) => {\n try {\n const data = JSON.parse(message.toString());\n await this.handleWebSocketMessage(ws, data);\n } catch (error) {\n this.logger.error(\"WebSocket message error:\", error);\n ws.send(\n JSON.stringify({\n type: \"error\",\n error: error instanceof Error ? error.message : String(error),\n })\n );\n }\n });\n\n ws.on(\"close\", () => {\n // 只在调试模式下输出断开日志\n this.logger.debug(\"WebSocket client disconnected\");\n });\n\n this.sendInitialData(ws);\n });\n }\n\n private async handleWebSocketMessage(ws: any, data: any) {\n switch (data.type) {\n case \"getConfig\": {\n const config = configManager.getConfig();\n ws.send(JSON.stringify({ type: \"config\", data: config }));\n break;\n }\n\n case \"updateConfig\":\n this.updateConfig(data.config);\n this.broadcastConfigUpdate(data.config);\n break;\n\n case \"getStatus\":\n ws.send(JSON.stringify({ type: \"status\", data: this.clientInfo }));\n break;\n\n case \"clientStatus\":\n this.updateClientInfo(data.data);\n this.broadcastStatusUpdate();\n break;\n }\n }\n\n private async sendInitialData(ws: any) {\n const config = configManager.getConfig();\n ws.send(JSON.stringify({ type: \"config\", data: config }));\n ws.send(JSON.stringify({ type: \"status\", data: this.clientInfo }));\n }\n\n private broadcastConfigUpdate(config: AppConfig) {\n const message = JSON.stringify({ type: \"configUpdate\", data: config });\n for (const client of this.wss.clients) {\n if (client.readyState === 1) {\n client.send(message);\n }\n }\n }\n\n private broadcastStatusUpdate() {\n const message = JSON.stringify({\n type: \"statusUpdate\",\n data: this.clientInfo,\n });\n for (const client of this.wss.clients) {\n if (client.readyState === 1) {\n client.send(message);\n }\n }\n }\n\n private updateClientInfo(info: Partial<ClientInfo>) {\n this.clientInfo = { ...this.clientInfo, ...info };\n if (info.lastHeartbeat) {\n this.clientInfo.lastHeartbeat = Date.now();\n }\n\n // Reset heartbeat timeout when receiving client status\n if (info.status === \"connected\") {\n this.resetHeartbeatTimeout();\n }\n }\n\n private resetHeartbeatTimeout() {\n // Clear existing timeout\n if (this.heartbeatTimeout) {\n clearTimeout(this.heartbeatTimeout);\n }\n\n // Set new timeout\n this.heartbeatTimeout = setTimeout(() => {\n this.logger.warn(\"客户端心跳超时,标记为断开连接\");\n this.updateClientInfo({ status: \"disconnected\" });\n this.broadcastStatusUpdate();\n }, this.HEARTBEAT_TIMEOUT);\n }\n\n private updateConfig(newConfig: AppConfig) {\n // 更新 MCP 端点\n if (newConfig.mcpEndpoint !== configManager.getMcpEndpoint()) {\n configManager.updateMcpEndpoint(newConfig.mcpEndpoint);\n }\n\n // 更新 MCP 服务\n const currentServers = configManager.getMcpServers();\n for (const [name, config] of Object.entries(newConfig.mcpServers)) {\n if (JSON.stringify(currentServers[name]) !== JSON.stringify(config)) {\n configManager.updateMcpServer(name, config);\n }\n }\n\n // 删除不存在的服务\n for (const name of Object.keys(currentServers)) {\n if (!(name in newConfig.mcpServers)) {\n configManager.removeMcpServer(name);\n }\n }\n\n // 更新连接配置\n if (newConfig.connection) {\n configManager.updateConnectionConfig(newConfig.connection);\n }\n\n // 更新 ModelScope 配置\n if (newConfig.modelscope) {\n configManager.updateModelScopeConfig(newConfig.modelscope);\n }\n\n // 更新 Web UI 配置\n if (newConfig.webUI) {\n configManager.updateWebUIConfig(newConfig.webUI);\n }\n\n // 更新服务工具配置\n if (newConfig.mcpServerConfig) {\n for (const [serverName, toolsConfig] of Object.entries(\n newConfig.mcpServerConfig\n )) {\n for (const [toolName, toolConfig] of Object.entries(\n toolsConfig.tools\n )) {\n configManager.setToolEnabled(serverName, toolName, toolConfig.enable);\n // 注释:configManager 不支持直接设置工具描述,描述作为工具配置的一部分保存\n }\n }\n }\n }\n\n public updateStatus(info: Partial<ClientInfo>) {\n this.updateClientInfo(info);\n this.broadcastStatusUpdate();\n }\n\n public start(): Promise<void> {\n return new Promise((resolve, reject) => {\n this.httpServer\n .listen(this.port, () => {\n this.logger.info(\n `Web server listening on http://localhost:${this.port}`\n );\n resolve();\n })\n .on(\"error\", reject);\n });\n }\n\n public stop(): Promise<void> {\n return new Promise((resolve) => {\n // Clear heartbeat timeout\n if (this.heartbeatTimeout) {\n clearTimeout(this.heartbeatTimeout);\n this.heartbeatTimeout = undefined;\n }\n\n // 强制断开所有 WebSocket 客户端连接\n for (const client of this.wss.clients) {\n client.terminate();\n }\n\n // 关闭 WebSocket 服务器\n this.wss.close(() => {\n // 强制关闭 HTTP 服务器,不等待现有连接\n this.httpServer.close(() => {\n this.logger.info(\"Web server stopped\");\n resolve();\n });\n\n // 设置超时,如果 2 秒内没有关闭则强制退出\n setTimeout(() => {\n this.logger.info(\"Web server force stopped\");\n resolve();\n }, 2000);\n });\n });\n }\n}\n","import { copyFileSync, existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\n// 在 ESM 中,需要从 import.meta.url 获取当前文件目录\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\n// 默认连接配置\nconst DEFAULT_CONNECTION_CONFIG: Required<ConnectionConfig> = {\n heartbeatInterval: 30000, // 30秒心跳间隔\n heartbeatTimeout: 10000, // 10秒心跳超时\n reconnectInterval: 5000, // 5秒重连间隔\n};\n\n// 配置文件接口定义\n// 本地 MCP 服务配置\nexport interface LocalMCPServerConfig {\n command: string;\n args: string[];\n env?: Record<string, string>;\n}\n\n// ModelScope SSE MCP 服务配置\nexport interface SSEMCPServerConfig {\n type: \"sse\";\n url: string;\n}\n\n// 统一的 MCP 服务配置\nexport type MCPServerConfig = LocalMCPServerConfig | SSEMCPServerConfig;\n\nexport interface MCPToolConfig {\n description?: string;\n enable: boolean;\n}\n\nexport interface MCPServerToolsConfig {\n tools: Record<string, MCPToolConfig>;\n}\n\nexport interface ConnectionConfig {\n heartbeatInterval?: number; // 心跳检测间隔(毫秒),默认30000\n heartbeatTimeout?: number; // 心跳超时时间(毫秒),默认10000\n reconnectInterval?: number; // 重连间隔(毫秒),默认5000\n}\n\nexport interface ModelScopeConfig {\n apiKey?: string; // ModelScope API 密钥\n}\n\nexport interface WebUIConfig {\n port?: number; // Web UI 端口号,默认 9999\n}\n\nexport interface AppConfig {\n mcpEndpoint: string | string[];\n mcpServers: Record<string, MCPServerConfig>;\n mcpServerConfig?: Record<string, MCPServerToolsConfig>;\n connection?: ConnectionConfig; // 连接配置(可选,用于向后兼容)\n modelscope?: ModelScopeConfig; // ModelScope 配置(可选)\n webUI?: WebUIConfig; // Web UI 配置(可选)\n}\n\n/**\n * 配置管理类\n * 负责管理应用配置,提供只读访问和安全的配置更新功能\n */\nexport class ConfigManager {\n private static instance: ConfigManager;\n private defaultConfigPath: string;\n private config: AppConfig | null = null;\n\n private constructor() {\n this.defaultConfigPath = resolve(__dirname, \"xiaozhi.config.default.json\");\n }\n\n /**\n * 获取配置文件路径(动态计算)\n */\n private getConfigFilePath(): string {\n // 配置文件路径 - 优先使用环境变量指定的目录,否则使用当前工作目录\n const configDir = process.env.XIAOZHI_CONFIG_DIR || process.cwd();\n return resolve(configDir, \"xiaozhi.config.json\");\n }\n\n /**\n * 获取配置管理器单例实例\n */\n public static getInstance(): ConfigManager {\n if (!ConfigManager.instance) {\n ConfigManager.instance = new ConfigManager();\n }\n return ConfigManager.instance;\n }\n\n /**\n * 检查配置文件是否存在\n */\n public configExists(): boolean {\n const configPath = this.getConfigFilePath();\n return existsSync(configPath);\n }\n\n /**\n * 初始化配置文件\n * 从 config.default.json 复制到 config.json\n */\n public initConfig(): void {\n if (!existsSync(this.defaultConfigPath)) {\n throw new Error(\"默认配置文件 xiaozhi.config.default.json 不存在\");\n }\n\n if (this.configExists()) {\n throw new Error(\"配置文件 xiaozhi.config.json 已存在,无需重复初始化\");\n }\n\n const configPath = this.getConfigFilePath();\n copyFileSync(this.defaultConfigPath, configPath);\n this.config = null; // 重置缓存\n }\n\n /**\n * 加载配置文件\n */\n private loadConfig(): AppConfig {\n if (!this.configExists()) {\n throw new Error(\n \"配置文件 xiaozhi.config.json 不存在,请先运行 xiaozhi init 初始化配置\"\n );\n }\n\n try {\n const configPath = this.getConfigFilePath();\n const configData = readFileSync(configPath, \"utf8\");\n const config = JSON.parse(configData) as AppConfig;\n\n // 验证配置结构\n this.validateConfig(config);\n\n return config;\n } catch (error) {\n if (error instanceof SyntaxError) {\n throw new Error(`配置文件格式错误: ${error.message}`);\n }\n throw error;\n }\n }\n\n /**\n * 验证配置文件结构\n */\n private validateConfig(config: unknown): void {\n if (!config || typeof config !== \"object\") {\n throw new Error(\"配置文件格式错误:根对象无效\");\n }\n\n const configObj = config as Record<string, unknown>;\n\n if (configObj.mcpEndpoint === undefined || configObj.mcpEndpoint === null) {\n throw new Error(\"配置文件格式错误:mcpEndpoint 字段无效\");\n }\n\n // 验证 mcpEndpoint 类型(字符串或字符串数组)\n if (typeof configObj.mcpEndpoint === \"string\") {\n // 空字符串是允许的,getMcpEndpoints 会返回空数组\n } else if (Array.isArray(configObj.mcpEndpoint)) {\n if (configObj.mcpEndpoint.length === 0) {\n throw new Error(\"配置文件格式错误:mcpEndpoint 数组不能为空\");\n }\n for (const endpoint of configObj.mcpEndpoint) {\n if (typeof endpoint !== \"string\" || endpoint.trim() === \"\") {\n throw new Error(\n \"配置文件格式错误:mcpEndpoint 数组中的每个元素必须是非空字符串\"\n );\n }\n }\n } else {\n throw new Error(\"配置文件格式错误:mcpEndpoint 必须是字符串或字符串数组\");\n }\n\n if (!configObj.mcpServers || typeof configObj.mcpServers !== \"object\") {\n throw new Error(\"配置文件格式错误:mcpServers 字段无效\");\n }\n\n // 验证每个 MCP 服务配置\n for (const [serverName, serverConfig] of Object.entries(\n configObj.mcpServers as Record<string, unknown>\n )) {\n if (!serverConfig || typeof serverConfig !== \"object\") {\n throw new Error(`配置文件格式错误:mcpServers.${serverName} 无效`);\n }\n\n const sc = serverConfig as Record<string, unknown>;\n\n // 检查是否是 SSE 类型\n if (sc.type === \"sse\") {\n // SSE 类型的验证\n if (!sc.url || typeof sc.url !== \"string\") {\n throw new Error(\n `配置文件格式错误:mcpServers.${serverName}.url 无效`\n );\n }\n } else {\n // 本地类型的验证\n if (!sc.command || typeof sc.command !== \"string\") {\n throw new Error(\n `配置文件格式错误:mcpServers.${serverName}.command 无效`\n );\n }\n\n if (!Array.isArray(sc.args)) {\n throw new Error(\n `配置文件格式错误:mcpServers.${serverName}.args 必须是数组`\n );\n }\n\n if (sc.env && typeof sc.env !== \"object\") {\n throw new Error(\n `配置文件格式错误:mcpServers.${serverName}.env 必须是对象`\n );\n }\n }\n }\n }\n\n /**\n * 获取配置(只读)\n */\n public getConfig(): Readonly<AppConfig> {\n if (!this.config) {\n this.config = this.loadConfig();\n }\n\n // 返回深度只读副本\n return JSON.parse(JSON.stringify(this.config));\n }\n\n /**\n * 获取 MCP 端点(向后兼容)\n * @deprecated 使用 getMcpEndpoints() 获取所有端点\n */\n public getMcpEndpoint(): string {\n const config = this.getConfig();\n if (Array.isArray(config.mcpEndpoint)) {\n return config.mcpEndpoint[0] || \"\";\n }\n return config.mcpEndpoint;\n }\n\n /**\n * 获取所有 MCP 端点\n */\n public getMcpEndpoints(): string[] {\n const config = this.getConfig();\n if (Array.isArray(config.mcpEndpoint)) {\n return [...config.mcpEndpoint];\n }\n return config.mcpEndpoint ? [config.mcpEndpoint] : [];\n }\n\n /**\n * 获取 MCP 服务配置\n */\n public getMcpServers(): Readonly<Record<string, MCPServerConfig>> {\n const config = this.getConfig();\n return config.mcpServers;\n }\n\n /**\n * 获取 MCP 服务工具配置\n */\n public getMcpServerConfig(): Readonly<Record<string, MCPServerToolsConfig>> {\n const config = this.getConfig();\n return config.mcpServerConfig || {};\n }\n\n /**\n * 获取指定服务的工具配置\n */\n public getServerToolsConfig(\n serverName: string\n ): Readonly<Record<string, MCPToolConfig>> {\n const serverConfig = this.getMcpServerConfig();\n return serverConfig[serverName]?.tools || {};\n }\n\n /**\n * 检查工具是否启用\n */\n public isToolEnabled(serverName: string, toolName: string): boolean {\n const toolsConfig = this.getServerToolsConfig(serverName);\n const toolConfig = toolsConfig[toolName];\n return toolConfig?.enable !== false; // 默认启用\n }\n\n /**\n * 更新 MCP 端点(支持字符串或数组)\n */\n public updateMcpEndpoint(endpoint: string | string[]): void {\n if (Array.isArray(endpoint)) {\n if (endpoint.length === 0) {\n throw new Error(\"MCP 端点数组不能为空\");\n }\n for (const ep of endpoint) {\n if (!ep || typeof ep !== \"string\") {\n throw new Error(\"MCP 端点数组中的每个元素必须是非空字符串\");\n }\n }\n } else {\n if (!endpoint || typeof endpoint !== \"string\") {\n throw new Error(\"MCP 端点必须是非空字符串\");\n }\n }\n\n const config = this.getConfig();\n const newConfig = { ...config, mcpEndpoint: endpoint };\n this.saveConfig(newConfig);\n }\n\n /**\n * 添加 MCP 端点\n */\n public addMcpEndpoint(endpoint: string): void {\n if (!endpoint || typeof endpoint !== \"string\") {\n throw new Error(\"MCP 端点必须是非空字符串\");\n }\n\n const config = this.getConfig();\n const currentEndpoints = this.getMcpEndpoints();\n\n // 检查是否已存在\n if (currentEndpoints.includes(endpoint)) {\n throw new Error(`MCP 端点 ${endpoint} 已存在`);\n }\n\n const newEndpoints = [...currentEndpoints, endpoint];\n const newConfig = { ...config, mcpEndpoint: newEndpoints };\n this.saveConfig(newConfig);\n }\n\n /**\n * 移除 MCP 端点\n */\n public removeMcpEndpoint(endpoint: string): void {\n if (!endpoint || typeof endpoint !== \"string\") {\n throw new Error(\"MCP 端点必须是非空字符串\");\n }\n\n const config = this.getConfig();\n const currentEndpoints = this.getMcpEndpoints();\n\n // 检查是否存在\n const index = currentEndpoints.indexOf(endpoint);\n if (index === -1) {\n throw new Error(`MCP 端点 ${endpoint} 不存在`);\n }\n\n // 不允许删除最后一个端点\n if (currentEndpoints.length === 1) {\n throw new Error(\"不能删除最后一个 MCP 端点\");\n }\n\n const newEndpoints = currentEndpoints.filter((ep) => ep !== endpoint);\n const newConfig = { ...config, mcpEndpoint: newEndpoints };\n this.saveConfig(newConfig);\n }\n\n /**\n * 更新 MCP 服务配置\n */\n public updateMcpServer(\n serverName: string,\n serverConfig: MCPServerConfig\n ): void {\n if (!serverName || typeof serverName !== \"string\") {\n throw new Error(\"服务名称必须是非空字符串\");\n }\n\n // 验证服务配置\n if (\"type\" in serverConfig && serverConfig.type === \"sse\") {\n // SSE 类型的验证\n if (!serverConfig.url || typeof serverConfig.url !== \"string\") {\n throw new Error(\"SSE 服务配置的 url 字段必须是非空字符串\");\n }\n } else {\n // 本地类型的验证\n const localConfig = serverConfig as LocalMCPServerConfig;\n if (!localConfig.command || typeof localConfig.command !== \"string\") {\n throw new Error(\"服务配置的 command 字段必须是非空字符串\");\n }\n\n if (!Array.isArray(localConfig.args)) {\n throw new Error(\"服务配置的 args 字段必须是数组\");\n }\n\n if (localConfig.env && typeof localConfig.env !== \"object\") {\n throw new Error(\"服务配置的 env 字段必须是对象\");\n }\n }\n\n const config = this.getConfig();\n const newConfig = {\n ...config,\n mcpServers: {\n ...config.mcpServers,\n [serverName]: serverConfig,\n },\n };\n this.saveConfig(newConfig);\n }\n\n /**\n * 删除 MCP 服务配置\n */\n public removeMcpServer(serverName: string): void {\n if (!serverName || typeof serverName !== \"string\") {\n throw new Error(\"服务名称必须是非空字符串\");\n }\n\n const config = this.getConfig();\n if (!config.mcpServers[serverName]) {\n throw new Error(`服务 ${serverName} 不存在`);\n }\n\n const newMcpServers = { ...config.mcpServers };\n delete newMcpServers[serverName];\n\n const newConfig = {\n ...config,\n mcpServers: newMcpServers,\n };\n this.saveConfig(newConfig);\n }\n\n /**\n * 更新服务工具配置\n */\n public updateServerToolsConfig(\n serverName: string,\n toolsConfig: Record<string, MCPToolConfig>\n ): void {\n const config = this.getConfig();\n const newConfig = { ...config };\n\n // 确保 mcpServerConfig 存在\n if (!newConfig.mcpServerConfig) {\n newConfig.mcpServerConfig = {};\n }\n\n // 更新指定服务的工具配置\n newConfig.mcpServerConfig[serverName] = {\n tools: toolsConfig,\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 设置工具启用状态\n */\n public setToolEnabled(\n serverName: string,\n toolName: string,\n enabled: boolean,\n description?: string\n ): void {\n const config = this.getConfig();\n const newConfig = { ...config };\n\n // 确保 mcpServerConfig 存在\n if (!newConfig.mcpServerConfig) {\n newConfig.mcpServerConfig = {};\n }\n\n // 确保服务配置存在\n if (!newConfig.mcpServerConfig[serverName]) {\n newConfig.mcpServerConfig[serverName] = { tools: {} };\n }\n\n // 更新工具配置\n newConfig.mcpServerConfig[serverName].tools[toolName] = {\n enable: enabled,\n ...(description && { description }),\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 保存配置到文件\n */\n private saveConfig(config: AppConfig): void {\n try {\n // 验证配置\n this.validateConfig(config);\n\n // 格式化 JSON 并保存\n const configPath = this.getConfigFilePath();\n const configJson = JSON.stringify(config, null, 2);\n writeFileSync(configPath, configJson, \"utf8\");\n\n // 更新缓存\n this.config = config;\n } catch (error) {\n throw new Error(\n `保存配置失败: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n }\n\n /**\n * 重新加载配置(清除缓存)\n */\n public reloadConfig(): void {\n this.config = null;\n }\n\n /**\n * 获取配置文件路径\n */\n public getConfigPath(): string {\n return this.getConfigFilePath();\n }\n\n /**\n * 获取默认配置文件路径\n */\n public getDefaultConfigPath(): string {\n return this.defaultConfigPath;\n }\n\n /**\n * 获取连接配置(包含默认值)\n */\n public getConnectionConfig(): Required<ConnectionConfig> {\n const config = this.getConfig();\n const connectionConfig = config.connection || {};\n\n return {\n heartbeatInterval:\n connectionConfig.heartbeatInterval ??\n DEFAULT_CONNECTION_CONFIG.heartbeatInterval,\n heartbeatTimeout:\n connectionConfig.heartbeatTimeout ??\n DEFAULT_CONNECTION_CONFIG.heartbeatTimeout,\n reconnectInterval:\n connectionConfig.reconnectInterval ??\n DEFAULT_CONNECTION_CONFIG.reconnectInterval,\n };\n }\n\n /**\n * 获取心跳检测间隔(毫秒)\n */\n public getHeartbeatInterval(): number {\n return this.getConnectionConfig().heartbeatInterval;\n }\n\n /**\n * 获取心跳超时时间(毫秒)\n */\n public getHeartbeatTimeout(): number {\n return this.getConnectionConfig().heartbeatTimeout;\n }\n\n /**\n * 获取重连间隔(毫秒)\n */\n public getReconnectInterval(): number {\n return this.getConnectionConfig().reconnectInterval;\n }\n\n /**\n * 更新连接配置\n */\n public updateConnectionConfig(\n connectionConfig: Partial<ConnectionConfig>\n ): void {\n const config = this.getConfig();\n const currentConnectionConfig = config.connection || {};\n\n const newConnectionConfig = {\n ...currentConnectionConfig,\n ...connectionConfig,\n };\n\n const newConfig = {\n ...config,\n connection: newConnectionConfig,\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 设置心跳检测间隔\n */\n public setHeartbeatInterval(interval: number): void {\n if (interval <= 0) {\n throw new Error(\"心跳检测间隔必须大于0\");\n }\n this.updateConnectionConfig({ heartbeatInterval: interval });\n }\n\n /**\n * 设置心跳超时时间\n */\n public setHeartbeatTimeout(timeout: number): void {\n if (timeout <= 0) {\n throw new Error(\"心跳超时时间必须大于0\");\n }\n this.updateConnectionConfig({ heartbeatTimeout: timeout });\n }\n\n /**\n * 设置重连间隔\n */\n public setReconnectInterval(interval: number): void {\n if (interval <= 0) {\n throw new Error(\"重连间隔必须大于0\");\n }\n this.updateConnectionConfig({ reconnectInterval: interval });\n }\n\n /**\n * 获取 ModelScope 配置\n */\n public getModelScopeConfig(): Readonly<ModelScopeConfig> {\n const config = this.getConfig();\n return config.modelscope || {};\n }\n\n /**\n * 获取 ModelScope API Key\n * 优先从配置文件读取,其次从环境变量读取\n */\n public getModelScopeApiKey(): string | undefined {\n const modelScopeConfig = this.getModelScopeConfig();\n return modelScopeConfig.apiKey || process.env.MODELSCOPE_API_TOKEN;\n }\n\n /**\n * 更新 ModelScope 配置\n */\n public updateModelScopeConfig(\n modelScopeConfig: Partial<ModelScopeConfig>\n ): void {\n const config = this.getConfig();\n const currentModelScopeConfig = config.modelscope || {};\n\n const newModelScopeConfig = {\n ...currentModelScopeConfig,\n ...modelScopeConfig,\n };\n\n const newConfig = {\n ...config,\n modelscope: newModelScopeConfig,\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 设置 ModelScope API Key\n */\n public setModelScopeApiKey(apiKey: string): void {\n if (!apiKey || typeof apiKey !== \"string\") {\n throw new Error(\"API Key 必须是非空字符串\");\n }\n this.updateModelScopeConfig({ apiKey });\n }\n\n /**\n * 获取 Web UI 配置\n */\n public getWebUIConfig(): Readonly<WebUIConfig> {\n const config = this.getConfig();\n return config.webUI || {};\n }\n\n /**\n * 获取 Web UI 端口号\n */\n public getWebUIPort(): number {\n const webUIConfig = this.getWebUIConfig();\n return webUIConfig.port ?? 9999; // 默认端口 9999\n }\n\n /**\n * 更新 Web UI 配置\n */\n public updateWebUIConfig(webUIConfig: Partial<WebUIConfig>): void {\n const config = this.getConfig();\n const currentWebUIConfig = config.webUI || {};\n\n const newWebUIConfig = {\n ...currentWebUIConfig,\n ...webUIConfig,\n };\n\n const newConfig = {\n ...config,\n webUI: newWebUIConfig,\n };\n\n this.saveConfig(newConfig);\n }\n\n /**\n * 设置 Web UI 端口号\n */\n public setWebUIPort(port: number): void {\n if (!Number.isInteger(port) || port <= 0 || port > 65535) {\n throw new Error(\"端口号必须是 1-65535 之间的整数\");\n }\n this.updateWebUIConfig({ port });\n }\n}\n\n// 导出单例实例\nexport const configManager = ConfigManager.getInstance();\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport chalk from \"chalk\";\nimport { type consola, createConsola } from \"consola\";\n\nfunction formatDateTime(date: Date) {\n const year = date.getFullYear();\n const month = String(date.getMonth() + 1).padStart(2, \"0\");\n const day = String(date.getDate()).padStart(2, \"0\");\n const hours = String(date.getHours()).padStart(2, \"0\");\n const minutes = String(date.getMinutes()).padStart(2, \"0\");\n const seconds = String(date.getSeconds()).padStart(2, \"0\");\n\n return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;\n}\n\nexport class Logger {\n private logFilePath: string | null = null;\n private writeStream: fs.WriteStream | null = null;\n private consolaInstance: typeof consola;\n private isDaemonMode: boolean;\n\n constructor() {\n // 检查是否为守护进程模式\n this.isDaemonMode = process.env.XIAOZHI_DAEMON === \"true\";\n // 创建自定义的 consola 实例,禁用图标并自定义格式\n this.consolaInstance = createConsola({\n formatOptions: {\n date: false,\n colors: true,\n compact: true,\n },\n fancy: false,\n });\n\n // 保存对当前实例的引用,以便在闭包中访问\n const isDaemonMode = this.isDaemonMode;\n\n // 自定义格式化器\n this.consolaInstance.setReporters([\n {\n log: (logObj) => {\n const levelMap: Record<string, string> = {\n info: \"INFO\",\n success: \"SUCCESS\",\n warn: \"WARN\",\n error: \"ERROR\",\n debug: \"DEBUG\",\n log: \"LOG\",\n };\n\n const colorMap: Record<string, (text: string) => string> = {\n info: chalk.blue,\n success: chalk.green,\n warn: chalk.yellow,\n error: chalk.red,\n debug: chalk.gray,\n log: (text: string) => text,\n };\n\n const level = levelMap[logObj.type] || logObj.type.toUpperCase();\n const colorFn = colorMap[logObj.type] || ((text: string) => text);\n const timestamp = formatDateTime(new Date());\n\n // 为级别添加颜色\n const coloredLevel = colorFn(`[${level}]`);\n const message = `[${timestamp}] ${coloredLevel} ${logObj.args.join(\n \" \"\n )}`;\n\n // 守护进程模式下不输出到控制台,只写入文件\n if (!isDaemonMode) {\n // 输出到 stderr(与原来保持一致)\n try {\n console.error(message);\n } catch (error) {\n // 忽略 EPIPE 错误\n if (error instanceof Error && error.message?.includes(\"EPIPE\")) {\n return;\n }\n throw error;\n }\n }\n },\n },\n ]);\n }\n\n /**\n * 初始化日志文件\n * @param projectDir 项目目录\n */\n initLogFile(projectDir: string): void {\n this.logFilePath = path.join(projectDir, \"xiaozhi.log\");\n\n // 确保日志文件存在\n if (!fs.existsSync(this.logFilePath)) {\n fs.writeFileSync(this.logFilePath, \"\");\n }\n\n // 创建写入流,追加模式\n this.writeStream = fs.createWriteStream(this.logFilePath, {\n flags: \"a\",\n encoding: \"utf8\",\n });\n }\n\n /**\n * 记录日志到文件\n * @param level 日志级别\n * @param message 日志消息\n * @param args 额外参数\n */\n private logToFile(level: string, message: string, ...args: any[]): void {\n if (this.writeStream) {\n const timestamp = new Date().toISOString();\n const formattedMessage = `[${timestamp}] [${level.toUpperCase()}] ${message}`;\n const fullMessage =\n args.length > 0\n ? `${formattedMessage} ${args\n .map((arg) =>\n typeof arg === \"object\" ? JSON.stringify(arg) : String(arg)\n )\n .join(\" \")}`\n : formattedMessage;\n\n this.writeStream.write(`${fullMessage}\\n`);\n }\n }\n\n /**\n * 设置是否启用文件日志\n * @param enable 是否启用\n */\n enableFileLogging(enable: boolean): void {\n if (enable && !this.writeStream && this.logFilePath) {\n this.writeStream = fs.createWriteStream(this.logFilePath, {\n flags: \"a\",\n encoding: \"utf8\",\n });\n } else if (!enable && this.writeStream) {\n this.writeStream.end();\n this.writeStream = null;\n }\n }\n\n /**\n * 日志方法\n */\n info(message: string, ...args: any[]): void {\n this.consolaInstance.info(message, ...args);\n this.logToFile(\"info\", message, ...args);\n }\n\n success(message: string, ...args: any[]): void {\n this.consolaInstance.success(message, ...args);\n this.logToFile(\"success\", message, ...args);\n }\n\n warn(message: string, ...args: any[]): void {\n this.consolaInstance.warn(message, ...args);\n this.logToFile(\"warn\", message, ...args);\n }\n\n error(message: string, ...args: any[]): void {\n this.consolaInstance.error(message, ...args);\n this.logToFile(\"error\", message, ...args);\n }\n\n debug(message: string, ...args: any[]): void {\n this.consolaInstance.debug(message, ...args);\n this.logToFile(\"debug\", message, ...args);\n }\n\n log(message: string, ...args: any[]): void {\n this.consolaInstance.log(message, ...args);\n this.logToFile(\"log\", message, ...args);\n }\n\n /**\n * 创建一个带标签的日志实例(已废弃,直接返回原实例)\n * @param tag 标签(不再使用)\n * @deprecated 标签功能已移除\n */\n withTag(tag: string): Logger {\n // 不再添加标签,直接返回共享实例\n return this;\n }\n\n /**\n * 关闭日志文件流\n */\n close(): void {\n if (this.writeStream) {\n this.writeStream.end();\n this.writeStream = null;\n }\n }\n}\n\n// 导出单例实例\nexport const logger = new Logger();\n"],"mappings":"+EAAA,OAAS,cAAAA,MAAkB,KAC3B,OAAS,YAAAC,MAAgB,cACzB,OAAS,gBAAAC,MAAoB,OAC7B,OAAS,WAAAC,EAAS,QAAAC,MAAY,OAC9B,OAAS,SAAAC,MAAa,MACtB,OAAS,iBAAAC,MAAqB,MAC9B,OAAS,mBAAAC,MAAuB,KCNhC,OAAS,gBAAAC,EAAc,cAAAC,EAAY,gBAAAC,EAAc,iBAAAC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0B,EAC3CA,EAAQ,CACV,EAAG,GAAI,CACT,CAAC,CACH,CAAC,CACH,CACF","names":["existsSync","readFile","createServer","dirname","join","parse","fileURLToPath","WebSocketServer","copyFileSync","existsSync","readFileSync","writeFileSync","dirname","resolve","fileURLToPath","__dirname","dirname","fileURLToPath","DEFAULT_CONNECTION_CONFIG","ConfigManager","_ConfigManager","__name","resolve","configDir","configPath","existsSync","copyFileSync","configData","readFileSync","config","error","configObj","endpoint","serverName","serverConfig","sc","toolName","ep","newConfig","currentEndpoints","newEndpoints","localConfig","newMcpServers","toolsConfig","enabled","description","configJson","writeFileSync","connectionConfig","newConnectionConfig","interval","timeout","modelScopeConfig","newModelScopeConfig","apiKey","webUIConfig","newWebUIConfig","port","configManager","fs","path","chalk","createConsola","formatDateTime","date","year","month","day","hours","minutes","seconds","__name","Logger","createConsola","isDaemonMode","logObj","levelMap","colorMap","chalk","text","level","colorFn","timestamp","coloredLevel","message","error","projectDir","path","fs","args","formattedMessage","fullMessage","arg","enable","tag","logger","WebServer","__name","port","configManager","Logger","createServer","req","res","WebSocketServer","pathname","parse","config","body","chunk","newConfig","error","__dirname","dirname","fileURLToPath","webPath","join","p","existsSync","filePath","fullPath","indexPath","content","readFile","ext","contentType","ws","message","data","client","info","currentServers","name","serverName","toolsConfig","toolName","toolConfig","resolve","reject"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xiaozhi-client",
3
- "version": "1.4.0",
3
+ "version": "1.5.0-beta.2",
4
4
  "description": "小智 AI 客户端 命令行工具",
5
5
  "type": "module",
6
6
  "main": "dist/cli.js",
@@ -20,24 +20,6 @@
20
20
  "xiaozhi": "./dist/cli.js",
21
21
  "xiaozhi-client": "./dist/cli.js"
22
22
  },
23
- "scripts": {
24
- "build": "pnpm run build:web && tsup",
25
- "build:web": "cd web && pnpm install && pnpm run build",
26
- "dev": "tsup --watch",
27
- "test": "vitest run",
28
- "test:watch": "vitest",
29
- "test:coverage": "vitest run --coverage",
30
- "test:ui": "vitest --ui",
31
- "format": "biome format --write .",
32
- "lint": "biome lint --write .",
33
- "type:check": "tsc --noEmit",
34
- "check": "biome check .",
35
- "check:write": "biome check --write .",
36
- "check:all": "pnpm check && pnpm type:check && pnpm spell:check && pnpm duplicate:check",
37
- "spell:check": "cspell \"src/**/*.ts\" \"*.md\" \"*.json\"",
38
- "duplicate:check": "jscpd src/",
39
- "release": "semantic-release"
40
- },
41
23
  "keywords": [
42
24
  "xiaozhi",
43
25
  "mcp",
@@ -48,12 +30,14 @@
48
30
  "license": "MIT",
49
31
  "dependencies": {
50
32
  "@modelcontextprotocol/sdk": "^1.12.1",
33
+ "@types/express": "^5.0.3",
51
34
  "chalk": "^5.4.1",
52
35
  "cli-table3": "^0.6.5",
53
36
  "commander": "^14.0.0",
54
37
  "consola": "^3.4.2",
55
38
  "dotenv": "^16.3.1",
56
39
  "eventsource": "^4.0.0",
40
+ "express": "^5.1.0",
57
41
  "omelette": "^0.4.17",
58
42
  "ora": "^8.2.0",
59
43
  "ws": "^8.14.2",
@@ -83,5 +67,23 @@
83
67
  "tsup": "^8.5.0",
84
68
  "typescript": "^5.8.3",
85
69
  "vitest": "^3.2.3"
70
+ },
71
+ "scripts": {
72
+ "build": "pnpm run build:web && tsup",
73
+ "build:web": "cd web && pnpm install && pnpm run build",
74
+ "dev": "tsup --watch",
75
+ "test": "vitest run",
76
+ "test:watch": "vitest",
77
+ "test:coverage": "vitest run --coverage",
78
+ "test:ui": "vitest --ui",
79
+ "format": "biome format --write .",
80
+ "lint": "biome lint --write .",
81
+ "type:check": "tsc --noEmit",
82
+ "check": "biome check .",
83
+ "check:write": "biome check --write .",
84
+ "check:all": "pnpm check && pnpm type:check && pnpm spell:check && pnpm duplicate:check",
85
+ "spell:check": "cspell \"src/**/*.ts\" \"*.md\" \"*.json\"",
86
+ "duplicate:check": "jscpd src/",
87
+ "release": "semantic-release"
86
88
  }
87
- }
89
+ }