@qqbrowser/qbot-claw-launcher 0.10.16 → 0.10.17

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/build.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IS_DEBUG_BUILD=exports.LAUNCHER_VERSION=void 0,exports.LAUNCHER_VERSION="0.10.16",exports.IS_DEBUG_BUILD=!1;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IS_DEBUG_BUILD=exports.LAUNCHER_VERSION=void 0,exports.LAUNCHER_VERSION="0.10.17",exports.IS_DEBUG_BUILD=!1;
package/dist/server.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var R=exports&&exports.__importDefault||function(u){return u&&u.__esModule?u:{default:u}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.server=void 0,exports.setGlobalAuthClient=z,exports.getGlobalAuthClient=j,exports.watchParentProcess=W;const U=R(require("http")),y=R(require("fs")),I=R(require("path")),f=require("./config"),n=require("./utils"),o=require("./reportLog"),L=require("./installer"),G=require("./build"),P=require("./state"),$=require("child_process"),Q=require("./service"),J=require("./filePicker"),x=require("./fileSecurityCheck"),F=require("./authMiddleware"),q=require("./qbotclaw-routes"),H="\u9009\u62E9\u6587\u4EF6",b=10485760,B=10,M=!1,w={CANCELLED:"CANCELLED",INVALID_PARAMS:"INVALID_PARAMS",DIALOG_BUSY:"DIALOG_BUSY",DIALOG_TIMEOUT:"DIALOG_TIMEOUT",PLATFORM_NOT_SUPPORTED:"PLATFORM_NOT_SUPPORTED",INTERNAL_ERROR:"INTERNAL_ERROR"},V=require("./openai-proxy"),K=require("./api/auth-client");let k=new K.AuthClient({usePipe:!0});function z(u){k=u}function j(){return k}const E={"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET, POST, PUT, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type, Authorization, X-Requested-With","Access-Control-Max-Age":"86400"};function Y(u){for(const[t,g]of Object.entries(E))u.setHeader(t,g)}exports.server=U.default.createServer(async(u,t)=>{const g=u.url||"";if(Y(t),u.method==="OPTIONS"){t.writeHead(204,E),t.end();return}if(g.startsWith("/v1/")){await(0,V.handleOpenAIProxy)(u,t,k);return}if(g==="/api/version"){let a="unknown";try{a=(await(0,n.execCommand)(`"${f.NODE_BIN}" -v`)).trim(),(0,o.log)(`[version] node \u7248\u672C: ${a}`)}catch(i){(0,o.log)(`[version] \u83B7\u53D6 node \u7248\u672C\u5931\u8D25: ${i.message}`),a=`error: ${i.message}`}const r=await(0,n.getQBotClawVersion)()??"0.0.0";(0,o.log)(`[version] qbotclaw \u7248\u672C: ${r}`);let s="0.0.0";if(y.default.existsSync(f.PYTHON_BIN))try{s=(await(0,n.execCommand)(`"${f.PYTHON_BIN}" --version`)).trim(),(0,o.log)(`[version] python \u7248\u672C: ${s}`)}catch(i){(0,o.log)(`[version] \u83B7\u53D6 python \u7248\u672C\u5931\u8D25: ${i.message}`),s=`error: ${i.message}`}(0,n.sendJSON)(t,200,{status:"\u6210\u529F",launcher:G.LAUNCHER_VERSION,node:a,qbotclaw:r,python:s});return}if(g==="/api/port"){const a=exports.server.address(),r=a&&typeof a=="object"?a.port:null,s=(0,P.readState)();(0,n.sendJSON)(t,200,{status:"\u6210\u529F",launcher:r,qbotclaw:s.gatewayPort??2e3,mcpWs:s.mcpWsPort??8765}),(0,o.log)(`[port] launcher: ${r}, qbotclaw: ${s.gatewayPort}, mcpWs: ${s.mcpWsPort}`);return}if(g==="/api/status"){const a=(0,P.readState)(),r=[];if(a.gatewayPid)try{process.kill(a.gatewayPid,0)}catch{r.push(`gateway \u8FDB\u7A0B (pid: ${a.gatewayPid}) \u5DF2\u9000\u51FA`)}else r.push("gateway \u8FDB\u7A0B\u672A\u542F\u52A8\uFF08\u72B6\u6001\u4E2D\u65E0 gatewayPid\uFF09");const s=[{name:"gateway",port:a.gatewayPort},{name:"mcpWs",port:a.mcpWsPort}];for(const{name:i,port:p}of s){if(!p){r.push(`${i} \u7AEF\u53E3\u672A\u914D\u7F6E\uFF08\u72B6\u6001\u4E2D\u65E0\u5BF9\u5E94\u7AEF\u53E3\u53F7\uFF09`);continue}await(0,P.isPortAvailable)(p)&&r.push(`${i} \u7AEF\u53E3 (${p}) \u65E0\u670D\u52A1\u76D1\u542C`)}r.length===0?(0,n.sendJSON)(t,200,{ok:!0,message:"\u6240\u6709\u670D\u52A1\u8FD0\u884C\u6B63\u5E38"}):(0,n.sendJSON)(t,200,{ok:!1,message:r.join("\uFF1B")});return}if(g==="/api/restart"){try{const a=(0,P.readState)();if(a.gatewayPid)try{process.kill(a.gatewayPid,"SIGTERM"),(0,o.log)(`[restart] \u5DF2\u53D1\u9001 SIGTERM \u7ED9\u65E7 gateway \u8FDB\u7A0B pid: ${a.gatewayPid}`)}catch{(0,o.log)(`[restart] \u65E7 gateway \u8FDB\u7A0B ${a.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA\uFF0C\u5FFD\u7565`)}const{gatewayPid:r,gatewayPort:s,mcpWsPort:i,...p}=a;(0,P.writeState)(p),(0,o.log)("[restart] \u5DF2\u6E05\u9664 gatewayPid, gatewayPort, mcpWsPort"),await(0,L.configAndRunQBotClaw)(),(0,o.log)("[restart] QBotClaw gateway \u91CD\u542F\u5B8C\u6210");const d=(0,P.readState)(),l=exports.server.address(),c=l&&typeof l=="object"?l.port:null;(0,n.sendJSON)(t,200,{ok:!0,message:"QBotClaw gateway \u91CD\u542F\u6210\u529F",launcher:c,qbotclaw:d.gatewayPort??2e3,mcpWs:d.mcpWsPort??8765,gatewayToken:(0,Q.getOrCreateGatewayToken)()})}catch(a){(0,o.log)(`[restart] \u91CD\u542F\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,message:`\u91CD\u542F\u5931\u8D25: ${a.message}`})}return}if(g==="/api/wechat/history"){try{if(!y.default.existsSync(f.SESSIONS_FILE)){(0,n.sendJSON)(t,404,{ok:!1,sessionKey:"",message:"\u672A\u627E\u5230\u4F1A\u8BDD\u6587\u4EF6"});return}const a=JSON.parse(y.default.readFileSync(f.SESSIONS_FILE,"utf-8"));let r=null,s=0;for(const[d,l]of Object.entries(a))if(d.includes("openclaw-weixin")){const e=l.updatedAt??0;e>s&&(s=e,r=d)}if(!r){(0,n.sendJSON)(t,404,{ok:!1,sessionKey:"",message:"\u672A\u627E\u5230\u5FAE\u4FE1\u4F1A\u8BDD"});return}const p=r.split(":").slice(2).join(":");(0,n.sendJSON)(t,200,{ok:!0,sessionKey:p,message:"\u83B7\u53D6\u6210\u529F"})}catch(a){(0,o.log)(`[wechat/history] \u8BFB\u53D6\u5FAE\u4FE1\u4F1A\u8BDD\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,sessionKey:"",message:`\u8BFB\u53D6\u5FAE\u4FE1\u4F1A\u8BDD\u5931\u8D25: ${a.message}`})}return}if(g==="/api/chat/latest"){try{if(!y.default.existsSync(f.SESSIONS_FILE)){(0,n.sendJSON)(t,404,{ok:!1,sessionId:"",message:"\u672A\u627E\u5230\u4F1A\u8BDD\u6587\u4EF6"});return}const a=JSON.parse(y.default.readFileSync(f.SESSIONS_FILE,"utf-8"));let r=null,s=0;for(const[p,d]of Object.entries(a)){if(p.includes("openclaw-weixin"))continue;const c=d.updatedAt??0;c>s&&(s=c,r=p)}if(!r){(0,n.sendJSON)(t,404,{ok:!1,sessionId:"",message:"\u672A\u627E\u5230\u975E\u5FAE\u4FE1\u4F1A\u8BDD"});return}const i=a[r];(0,n.sendJSON)(t,200,{ok:!0,sessionId:i.sessionId??"",sessionKey:r,updatedAt:s,message:"\u83B7\u53D6\u6210\u529F"})}catch(a){(0,o.log)(`[chat/latest] \u8BFB\u53D6\u4F1A\u8BDD\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,sessionId:"",message:`\u8BFB\u53D6\u4F1A\u8BDD\u5931\u8D25: ${a.message}`})}return}if(g==="/api/installing"){(0,n.sendJSON)(t,200,{installing:L.isInstalling});return}if(g==="/api/install"||g.startsWith("/api/install?")){const a=new URL(g,"http://localhost"),r=a.searchParams.get("version")||void 0,s=a.searchParams.get("overrideConfig")==="true";if(!r){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 version\uFF0C\u7528\u6CD5: GET /api/install?version=x.x.x"});return}t.writeHead(200,{...E,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"}),(0,L.runInstall)(t,r,s);return}if(g.startsWith("/api/watch")){const r=new URL(g,"http://localhost").searchParams.get("ppid");if(!r||isNaN(Number(r))){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u6709\u6548\u7684 ppid \u53C2\u6570\uFF0C\u7528\u6CD5: GET /api/watch?ppid=12345"});return}const s=parseInt(r,10);try{process.kill(s,0)}catch{(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:`\u8FDB\u7A0B ${s} \u4E0D\u5B58\u5728`});return}const i=(0,P.readState)();(0,P.writeState)({...i,ppid:s}),W(s),(0,n.sendJSON)(t,200,{status:"\u6210\u529F",message:`\u5DF2\u5F00\u59CB\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${s}`});return}if(!(g.startsWith("/api/qbotclaw/")&&await(0,q.handleQBotClawRoutes)(u,t,g))){if(g.startsWith("/api/uninstall")){const r=new URL(g,"http://localhost").searchParams.get("keepWorkspace")==="true";t.writeHead(200,{...E,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"});const s=(i,p,d)=>{if(t.writable)try{t.write(JSON.stringify({status:i,file:p,msg:d})+`
2
- `)}catch{}};try{s("\u6210\u529F","","\u6B63\u5728\u505C\u6B62 QBotClaw...");const i=(0,P.readState)();if(i.gatewayPid)try{process.kill(i.gatewayPid,"SIGTERM"),(0,o.log)(`[uninstall] \u5DF2\u505C\u6B62 QBotClaw gateway \u8FDB\u7A0B pid: ${i.gatewayPid}`),s("\u6210\u529F","",`\u5DF2\u505C\u6B62 QBotClaw gateway \u8FDB\u7A0B pid: ${i.gatewayPid}`)}catch{(0,o.log)(`[uninstall] QBotClaw gateway \u8FDB\u7A0B ${i.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA`),s("\u6210\u529F","",`QBotClaw gateway \u8FDB\u7A0B ${i.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA`)}else s("\u6210\u529F","","QBotClaw \u672A\u5728\u8FD0\u884C");s("\u6210\u529F","",`\u6B63\u5728\u6267\u884C npm uninstall -g ${f.NPM_PACKAGE_NAME}...`);try{await new Promise((d,l)=>{const c=(0,$.spawn)(f.NPM_BIN,["--userconfig",f.NPM_RC,"uninstall","-g",f.NPM_PACKAGE_NAME],{env:{...process.env,PATH:`${f.NODE_BIN_DIR}${I.default.delimiter}${process.env.PATH}`},shell:process.platform==="win32",windowsHide:!0});c.stdout?.on("data",e=>{(0,o.log)(`[uninstall] npm: ${e.toString().trim()}`)}),c.stderr?.on("data",e=>{(0,o.log)(`[uninstall] npm stderr: ${e.toString().trim()}`)}),c.on("close",e=>{e===0?d():l(new Error(`npm uninstall \u9000\u51FA\u7801: ${e}`))}),c.on("error",e=>l(e))}),(0,o.log)(`[uninstall] npm uninstall -g ${f.NPM_PACKAGE_NAME} \u6210\u529F`),s("\u6210\u529F","",`npm uninstall -g ${f.NPM_PACKAGE_NAME} \u6210\u529F`)}catch(d){(0,o.log)(`[uninstall] npm uninstall \u5931\u8D25: ${d.message}`),s("\u5931\u8D25","",`npm uninstall \u5931\u8D25: ${d.message}`)}s("\u6210\u529F","",`\u6B63\u5728\u6E05\u7406\u76EE\u5F55 ${f.QBOT_CLAW_DIR}...`);const p=[];if(y.default.existsSync(f.QBOT_CLAW_DIR)){const d=y.default.readdirSync(f.QBOT_CLAW_DIR);for(const l of d){const c=I.default.join(f.QBOT_CLAW_DIR,l);if(r&&l==="workspace"){s("\u6210\u529F",c,"\u4FDD\u7559 workspace \u76EE\u5F55");continue}try{y.default.statSync(c).isDirectory()?y.default.rmSync(c,{recursive:!0,force:!0}):y.default.unlinkSync(c),s("\u6210\u529F",c,"\u5DF2\u5220\u9664")}catch(e){(0,o.log)(`[uninstall] \u5220\u9664\u5931\u8D25: ${c} - ${e.message}`),s("\u5931\u8D25",c,`\u5220\u9664\u5931\u8D25: ${e.message}`),p.push(c)}}if(p.length>0){s("\u6210\u529F","",`${p.length} \u4E2A\u9879\u76EE\u5220\u9664\u5931\u8D25\uFF0C\u4F7F\u7528 rm \u547D\u4EE4\u91CD\u8BD5...`);for(const l of p)try{process.platform==="win32"?(0,$.execSync)(`rmdir /s /q "${l}"`,{windowsHide:!0}):(0,$.execSync)(`rm -rf "${l}"`),(0,o.log)(`[uninstall] \u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u6210\u529F: ${l}`),s("\u6210\u529F",l,"\u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u5220\u9664\u6210\u529F")}catch(c){(0,o.log)(`[uninstall] \u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u4E5F\u5931\u8D25: ${l} - ${c.message}`),s("\u5931\u8D25",l,`\u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u4E5F\u5931\u8D25: ${c.message}`)}}if(r)s("\u6210\u529F",f.QBOT_CLAW_DIR,"\u4FDD\u7559\u6839\u76EE\u5F55\uFF08workspace \u6A21\u5F0F\uFF09");else try{y.default.rmSync(f.QBOT_CLAW_DIR,{recursive:!0,force:!0}),s("\u6210\u529F",f.QBOT_CLAW_DIR,"\u5DF2\u5220\u9664\u6839\u76EE\u5F55")}catch(l){(0,o.log)(`[uninstall] \u5220\u9664\u6839\u76EE\u5F55\u5931\u8D25: ${l.message}`);try{process.platform==="win32"?(0,$.execSync)(`rmdir /s /q "${f.QBOT_CLAW_DIR}"`,{windowsHide:!0}):(0,$.execSync)(`rm -rf "${f.QBOT_CLAW_DIR}"`),s("\u6210\u529F",f.QBOT_CLAW_DIR,"\u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u5220\u9664\u6839\u76EE\u5F55\u6210\u529F")}catch(c){s("\u5931\u8D25",f.QBOT_CLAW_DIR,`\u5220\u9664\u6839\u76EE\u5F55\u5931\u8D25: ${c.message}`)}}}else s("\u6210\u529F",f.QBOT_CLAW_DIR,"\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u65E0\u9700\u6E05\u7406");s("\u6210\u529F","","\u5378\u8F7D\u5B8C\u6210")}catch(i){(0,o.log)(`[uninstall] \u5378\u8F7D\u8FC7\u7A0B\u5F02\u5E38: ${i.message}`),s("\u5931\u8D25","",`\u5378\u8F7D\u8FC7\u7A0B\u5F02\u5E38: ${i.message}`)}t.end(),setTimeout(()=>{(0,o.log)("[uninstall] \u5378\u8F7D\u6D41\u7A0B\u7ED3\u675F\uFF0Clauncher \u8FDB\u7A0B\u9000\u51FA"),process.exit(0)},500);return}if(g==="/api/file-picker"&&u.method==="POST"){const a=(0,F.authenticateRequest)(u);if(!a.ok){(0,n.sendJSON)(t,a.statusCode,{ok:!1,message:a.message});return}if((0,J.isFilePickerBusy)()){(0,n.sendJSON)(t,409,{ok:!1,code:w.DIALOG_BUSY,files:[],message:"\u5DF2\u6709\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u6B63\u5728\u663E\u793A\uFF0C\u8BF7\u7B49\u5F85\u5F53\u524D\u5BF9\u8BDD\u6846\u5173\u95ED\u540E\u91CD\u8BD5"});return}let r="";u.on("data",s=>{r+=s.toString()}),u.on("end",async()=>{let s=[],i=!1,p=H,d=b,l=B,c=M;if(r){let e;try{e=JSON.parse(r)}catch{(0,n.sendJSON)(t,400,{ok:!1,code:w.INVALID_PARAMS,files:[],message:"\u8BF7\u6C42\u4F53\u5FC5\u987B\u662F\u5408\u6CD5\u7684 JSON"});return}if(e.extensions!==void 0){if(!Array.isArray(e.extensions)||!e.extensions.every(m=>typeof m=="string")){(0,n.sendJSON)(t,400,{ok:!1,code:w.INVALID_PARAMS,files:[],message:"\u53C2\u6570 extensions \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4"});return}s=e.extensions}if(e.multiple!==void 0){if(typeof e.multiple!="boolean"){(0,n.sendJSON)(t,400,{ok:!1,code:w.INVALID_PARAMS,files:[],message:"\u53C2\u6570 multiple \u5FC5\u987B\u662F\u5E03\u5C14\u503C"});return}i=e.multiple}if(e.title!==void 0){if(typeof e.title!="string"||e.title.length>200){(0,n.sendJSON)(t,400,{ok:!1,code:w.INVALID_PARAMS,files:[],message:"\u53C2\u6570 title \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u4E14\u957F\u5EA6\u4E0D\u8D85\u8FC7 200"});return}p=e.title}if(e.maxFileSize!==void 0){if(typeof e.maxFileSize!="number"||e.maxFileSize<=0||!Number.isFinite(e.maxFileSize)){(0,n.sendJSON)(t,400,{ok:!1,code:w.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileSize \u5FC5\u987B\u662F\u6B63\u6570"});return}d=Math.min(e.maxFileSize,b)}if(e.maxFileCount!==void 0){if(typeof e.maxFileCount!="number"||e.maxFileCount<=0||!Number.isInteger(e.maxFileCount)){(0,n.sendJSON)(t,400,{ok:!1,code:w.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileCount \u5FC5\u987B\u662F\u6B63\u6574\u6570"});return}l=Math.min(e.maxFileCount,B)}if(M&&e.allowDirectory!==void 0){if(typeof e.allowDirectory!="boolean"){(0,n.sendJSON)(t,400,{ok:!1,code:w.INVALID_PARAMS,files:[],message:"\u53C2\u6570 allowDirectory \u5FC5\u987B\u662F\u5E03\u5C14\u503C"});return}c=e.allowDirectory}}i||(l=1);try{const e=await(0,J.showFilePicker)({extensions:s,multiple:i,title:p,allowDirectory:c});if(!e||e.length===0){(0,n.sendJSON)(t,200,{ok:!1,code:w.CANCELLED,files:[],message:"\u7528\u6237\u53D6\u6D88\u4E86\u6587\u4EF6\u9009\u62E9"});return}if(c){const A=e.slice(0,l).map(D=>({name:I.default.basename(D),path:D,size:0,mimeType:"inode/directory"})),h=e.length>l;(0,n.sendJSON)(t,200,{ok:!0,files:A,message:h?`\u5DF2\u9009\u62E9 ${A.length} \u4E2A\u76EE\u5F55\uFF08\u5171\u9009\u62E9 ${e.length} \u4E2A\uFF0C\u8D85\u51FA\u4E0A\u9650 ${l} \u4E2A\u5DF2\u622A\u65AD\uFF09`:`\u5DF2\u9009\u62E9 ${A.length} \u4E2A\u76EE\u5F55`});return}let m=!1,N=e;e.length>l&&(N=e.slice(0,l),m=!0);const O=[],S=[];for(const A of N){const h=(0,x.readFileInfo)(A,d);h.ok?O.push({name:h.name,path:h.path,size:h.size,mimeType:h.mimeType}):S.push({path:h.path,reason:h.reason,code:h.code})}const _=[];if(O.length>0&&_.push(`\u6210\u529F\u8BFB\u53D6 ${O.length} \u4E2A\u6587\u4EF6`),S.length>0){const A=S.map(h=>`${I.default.basename(h.path)}: ${h.reason}`).join("\uFF1B");_.push(`${S.length} \u4E2A\u6587\u4EF6\u5931\u8D25\uFF08${A}\uFF09`)}m&&_.push(`\u5171\u9009\u62E9 ${e.length} \u4E2A\u6587\u4EF6\uFF0C\u8D85\u51FA\u4E0A\u9650 ${l} \u4E2A\u5DF2\u622A\u65AD`);const v=O.length===0&&S.length>0;(0,n.sendJSON)(t,200,{ok:!v,code:v?S[0].code:void 0,files:O,failedFiles:S.length>0?S:void 0,message:_.join("\uFF1B")||"\u6587\u4EF6\u9009\u62E9\u6210\u529F"})}catch(e){(0,o.log)(`[file-picker] \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${e.message}`);let m=w.INTERNAL_ERROR;e.message.includes("\u8D85\u65F6")?m=w.DIALOG_TIMEOUT:e.message.includes("\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0")&&(m=w.PLATFORM_NOT_SUPPORTED),(0,n.sendJSON)(t,500,{ok:!1,code:m,files:[],message:`\u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${e.message}`})}});return}if(g.startsWith("/api/reveal")){console.log(`[reveal] \u5B8C\u6574URL: ${g}`);const a=(0,F.authenticateRequest)(u);if(!a.ok){(0,n.sendJSON)(t,a.statusCode,{status:"\u5931\u8D25",message:a.message});return}const s=new URL(g,"http://localhost").searchParams.get("path")?.replace(/ /g,"+")??null;if(!s){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 path\uFF08\u9700 Base64 \u7F16\u7801\uFF09\uFF0C\u7528\u6CD5: GET /api/reveal?path=<base64\u7F16\u7801\u7684\u6587\u4EF6\u7EDD\u5BF9\u8DEF\u5F84>"});return}let i;try{i=Buffer.from(s,"base64").toString("utf-8")}catch{(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"path \u53C2\u6570 Base64 \u89E3\u7801\u5931\u8D25\uFF0C\u8BF7\u786E\u4FDD\u4F20\u5165\u5408\u6CD5\u7684 Base64 \u7F16\u7801\u5B57\u7B26\u4E32"});return}if(!i||i.trim()===""){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"Base64 \u89E3\u7801\u540E\u8DEF\u5F84\u4E3A\u7A7A"});return}const p=(0,x.validateFilePath)(i);if(!p.valid){const m=p.reason;m.includes("\u975E\u6CD5\u5B57\u7B26")?(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:m}):m.includes("\u4E0D\u5B58\u5728")?(0,n.sendJSON)(t,404,{status:"\u5931\u8D25",message:m}):m.includes("\u65E0\u6CD5\u89E3\u6790")?(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:m}):((0,o.log)(`[reveal] \u5B89\u5168\u68C0\u67E5\u88AB\u62D2\u7EDD: ${i}\uFF0C\u539F\u56E0: ${m}`),(0,n.sendJSON)(t,403,{status:"\u5931\u8D25",message:m}));return}const d=I.default.resolve(i),l=process.platform;let c,e;l==="darwin"?(c="open",e=["-R",d]):l==="win32"?(c="explorer",e=[`/select,${d}`]):(c="xdg-open",e=[I.default.dirname(d)]);try{await new Promise((m,N)=>{const O=(0,$.spawn)(c,e,{stdio:"ignore",windowsHide:!0}),S=setTimeout(()=>{O.kill(),N(new Error("\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u8D85\u65F6\uFF085\u79D2\uFF09"))},5e3);O.on("close",_=>{clearTimeout(S),_===0||_===1?m():N(new Error(`\u8FDB\u7A0B\u9000\u51FA\u7801: ${_}`))}),O.on("error",_=>{clearTimeout(S),N(_)})}),(0,o.log)(`[reveal] \u5DF2\u5728\u6587\u4EF6\u7BA1\u7406\u5668\u4E2D\u663E\u793A: ${d}`),(0,n.sendJSON)(t,200,{status:"\u6210\u529F",message:`\u5DF2\u5728\u6587\u4EF6\u7BA1\u7406\u5668\u4E2D\u663E\u793A: ${d}`})}catch(m){(0,o.log)(`[reveal] \u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u5931\u8D25: ${m.message}`),(0,n.sendJSON)(t,500,{status:"\u5931\u8D25",message:`\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u5931\u8D25: ${m.message}`})}return}(0,n.sendJSON)(t,404,{status:"\u5931\u8D25",message:"\u63A5\u53E3\u4E0D\u5B58\u5728"})}});let C=null,T=null;function W(u){if(!u)return;if(C&&T===u){(0,o.log)(`[watchParentProcess] \u5DF2\u5728\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${u}\uFF0C\u8DF3\u8FC7\u91CD\u590D\u76D1\u542C`);return}C?((0,o.log)(`[watchParentProcess] \u5DF2\u6709\u76D1\u63A7\uFF08ppid: ${T}\uFF09\uFF0C\u505C\u6B62\u65E7\u7684\u76D1\u63A7\uFF0C\u91CD\u65B0\u76D1\u63A7 ppid: ${u}`),clearInterval(C),C=null,T=null):(0,o.log)(`[watchParentProcess] \u5F00\u59CB\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${u}`);const t=setInterval(()=>{try{process.kill(u,0)}catch{(0,o.log)(`[watchParentProcess] \u7236\u8FDB\u7A0B ${u} \u5DF2\u9000\u51FA\uFF0C\u5F00\u59CB\u6E05\u7406\u5E76\u9000\u51FA`),clearInterval(t),C=null,T=null,(0,P.stopGateway)(),(0,P.clearState)(),(0,o.log)("[watchParentProcess] \u72B6\u6001\u5DF2\u6E05\u7406\uFF0C\u5173\u95ED server \u5E76\u9000\u51FA"),exports.server.close(()=>{(0,o.log)("[watchParentProcess] server \u5DF2\u5173\u95ED\uFF0C\u8FDB\u7A0B\u9000\u51FA"),process.exit(0)}),setTimeout(()=>{(0,o.log)("[watchParentProcess] server.close \u8D85\u65F6\uFF0C\u5F3A\u5236\u9000\u51FA"),process.exit(0)},3e3).unref()}},3e3);t.unref(),C=t,T=u}
1
+ "use strict";var L=exports&&exports.__importDefault||function(u){return u&&u.__esModule?u:{default:u}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.server=void 0,exports.setGlobalAuthClient=z,exports.getGlobalAuthClient=j,exports.watchParentProcess=W;const U=L(require("http")),P=L(require("fs")),$=L(require("path")),f=require("./config"),n=require("./utils"),o=require("./reportLog"),k=require("./installer"),G=require("./build"),_=require("./state"),C=require("child_process"),Q=require("./service"),J=require("./filePicker"),x=require("./fileSecurityCheck"),F=require("./authMiddleware"),q=require("./qbotclaw-routes"),H="\u9009\u62E9\u6587\u4EF6",b=10485760,B=10,M=!1,y={CANCELLED:"CANCELLED",INVALID_PARAMS:"INVALID_PARAMS",DIALOG_BUSY:"DIALOG_BUSY",DIALOG_TIMEOUT:"DIALOG_TIMEOUT",PLATFORM_NOT_SUPPORTED:"PLATFORM_NOT_SUPPORTED",INTERNAL_ERROR:"INTERNAL_ERROR"},V=require("./openai-proxy"),K=require("./api/auth-client");let v=new K.AuthClient({usePipe:!0});function z(u){v=u}function j(){return v}const R={"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET, POST, PUT, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type, Authorization, X-Requested-With","Access-Control-Max-Age":"86400"};function Y(u){for(const[t,g]of Object.entries(R))u.setHeader(t,g)}exports.server=U.default.createServer(async(u,t)=>{const g=u.url||"";if(Y(t),u.method==="OPTIONS"){t.writeHead(204,R),t.end();return}if(g.startsWith("/v1/")){await(0,V.handleOpenAIProxy)(u,t,v);return}if(g==="/api/version"){let a="unknown";try{a=(await(0,n.execCommand)(`"${f.NODE_BIN}" -v`)).trim(),(0,o.log)(`[version] node \u7248\u672C: ${a}`)}catch(i){(0,o.log)(`[version] \u83B7\u53D6 node \u7248\u672C\u5931\u8D25: ${i.message}`),a=`error: ${i.message}`}const r=await(0,n.getQBotClawVersion)()??"0.0.0";(0,o.log)(`[version] qbotclaw \u7248\u672C: ${r}`);let s="0.0.0";if(P.default.existsSync(f.PYTHON_BIN))try{s=(await(0,n.execCommand)(`"${f.PYTHON_BIN}" --version`)).trim(),(0,o.log)(`[version] python \u7248\u672C: ${s}`)}catch(i){(0,o.log)(`[version] \u83B7\u53D6 python \u7248\u672C\u5931\u8D25: ${i.message}`),s=`error: ${i.message}`}(0,n.sendJSON)(t,200,{status:"\u6210\u529F",launcher:G.LAUNCHER_VERSION,node:a,qbotclaw:r,python:s});return}if(g==="/api/port"){const a=exports.server.address(),r=a&&typeof a=="object"?a.port:null,s=(0,_.readState)();(0,n.sendJSON)(t,200,{status:"\u6210\u529F",launcher:r,qbotclaw:s.gatewayPort??2e3,mcpWs:s.mcpWsPort??8765}),(0,o.log)(`[port] launcher: ${r}, qbotclaw: ${s.gatewayPort}, mcpWs: ${s.mcpWsPort}`);return}if(g==="/api/status"){const a=(0,_.readState)(),r=[];if(a.gatewayPid)try{process.kill(a.gatewayPid,0)}catch{r.push(`gateway \u8FDB\u7A0B (pid: ${a.gatewayPid}) \u5DF2\u9000\u51FA`)}else r.push("gateway \u8FDB\u7A0B\u672A\u542F\u52A8\uFF08\u72B6\u6001\u4E2D\u65E0 gatewayPid\uFF09");const s=[{name:"gateway",port:a.gatewayPort},{name:"mcpWs",port:a.mcpWsPort}];for(const{name:i,port:p}of s){if(!p){r.push(`${i} \u7AEF\u53E3\u672A\u914D\u7F6E\uFF08\u72B6\u6001\u4E2D\u65E0\u5BF9\u5E94\u7AEF\u53E3\u53F7\uFF09`);continue}await(0,_.isPortAvailable)(p)&&r.push(`${i} \u7AEF\u53E3 (${p}) \u65E0\u670D\u52A1\u76D1\u542C`)}r.length===0?(0,n.sendJSON)(t,200,{ok:!0,message:"\u6240\u6709\u670D\u52A1\u8FD0\u884C\u6B63\u5E38"}):(0,n.sendJSON)(t,200,{ok:!1,message:r.join("\uFF1B")});return}if(g==="/api/restart"){try{const a=(0,_.readState)();if(a.gatewayPid)try{process.kill(a.gatewayPid,"SIGTERM"),(0,o.log)(`[restart] \u5DF2\u53D1\u9001 SIGTERM \u7ED9\u65E7 gateway \u8FDB\u7A0B pid: ${a.gatewayPid}`)}catch{(0,o.log)(`[restart] \u65E7 gateway \u8FDB\u7A0B ${a.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA\uFF0C\u5FFD\u7565`)}const{gatewayPid:r,gatewayPort:s,mcpWsPort:i,...p}=a;(0,_.writeState)(p),(0,o.log)("[restart] \u5DF2\u6E05\u9664 gatewayPid, gatewayPort, mcpWsPort"),await(0,k.configAndRunQBotClaw)(),(0,o.log)("[restart] QBotClaw gateway \u91CD\u542F\u5B8C\u6210");const d=(0,_.readState)(),l=exports.server.address(),c=l&&typeof l=="object"?l.port:null;(0,n.sendJSON)(t,200,{ok:!0,message:"QBotClaw gateway \u91CD\u542F\u6210\u529F",launcher:c,qbotclaw:d.gatewayPort??2e3,mcpWs:d.mcpWsPort??8765,gatewayToken:(0,Q.getOrCreateGatewayToken)()})}catch(a){(0,o.log)(`[restart] \u91CD\u542F\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,message:`\u91CD\u542F\u5931\u8D25: ${a.message}`})}return}if(g==="/api/wechat/history"){try{if(!P.default.existsSync(f.SESSIONS_FILE)){(0,n.sendJSON)(t,404,{ok:!1,sessionKey:"",message:"\u672A\u627E\u5230\u4F1A\u8BDD\u6587\u4EF6"});return}const a=JSON.parse(P.default.readFileSync(f.SESSIONS_FILE,"utf-8"));let r=null,s=0;for(const[d,l]of Object.entries(a))if(d.includes("openclaw-weixin")){const e=l.updatedAt??0;e>s&&(s=e,r=d)}if(!r){(0,n.sendJSON)(t,404,{ok:!1,sessionKey:"",message:"\u672A\u627E\u5230\u5FAE\u4FE1\u4F1A\u8BDD"});return}const p=r.split(":").slice(2).join(":");(0,n.sendJSON)(t,200,{ok:!0,sessionKey:p,message:"\u83B7\u53D6\u6210\u529F"})}catch(a){(0,o.log)(`[wechat/history] \u8BFB\u53D6\u5FAE\u4FE1\u4F1A\u8BDD\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,sessionKey:"",message:`\u8BFB\u53D6\u5FAE\u4FE1\u4F1A\u8BDD\u5931\u8D25: ${a.message}`})}return}if(g==="/api/chat/latest"){try{if(!P.default.existsSync(f.SESSIONS_FILE)){(0,n.sendJSON)(t,404,{ok:!1,sessionId:"",message:"\u672A\u627E\u5230\u4F1A\u8BDD\u6587\u4EF6"});return}const a=JSON.parse(P.default.readFileSync(f.SESSIONS_FILE,"utf-8"));let r=null,s=0;for(const[p,d]of Object.entries(a)){if(p.includes("openclaw-weixin"))continue;const c=d.updatedAt??0;c>s&&(s=c,r=p)}if(!r){(0,n.sendJSON)(t,404,{ok:!1,sessionId:"",message:"\u672A\u627E\u5230\u975E\u5FAE\u4FE1\u4F1A\u8BDD"});return}const i=a[r];(0,n.sendJSON)(t,200,{ok:!0,sessionId:i.sessionId??"",sessionKey:r,updatedAt:s,message:"\u83B7\u53D6\u6210\u529F"})}catch(a){(0,o.log)(`[chat/latest] \u8BFB\u53D6\u4F1A\u8BDD\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,sessionId:"",message:`\u8BFB\u53D6\u4F1A\u8BDD\u5931\u8D25: ${a.message}`})}return}if(g==="/api/installing"){(0,n.sendJSON)(t,200,{installing:k.isInstalling});return}if(g==="/api/install"||g.startsWith("/api/install?")){const a=new URL(g,"http://localhost"),r=a.searchParams.get("version")||void 0,s=a.searchParams.get("overrideConfig")==="true";if(!r){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 version\uFF0C\u7528\u6CD5: GET /api/install?version=x.x.x"});return}t.writeHead(200,{...R,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"}),(0,k.runInstall)(t,r,s);return}if(g.startsWith("/api/watch")){const r=new URL(g,"http://localhost").searchParams.get("ppid");if(!r||isNaN(Number(r))){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u6709\u6548\u7684 ppid \u53C2\u6570\uFF0C\u7528\u6CD5: GET /api/watch?ppid=12345"});return}const s=parseInt(r,10);try{process.kill(s,0)}catch{(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:`\u8FDB\u7A0B ${s} \u4E0D\u5B58\u5728`});return}const i=(0,_.readState)();(0,_.writeState)({...i,ppid:s}),W(s),(0,n.sendJSON)(t,200,{status:"\u6210\u529F",message:`\u5DF2\u5F00\u59CB\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${s}`});return}if(!(g.startsWith("/api/qbotclaw/")&&await(0,q.handleQBotClawRoutes)(u,t,g))){if(g.startsWith("/api/uninstall")){const r=new URL(g,"http://localhost").searchParams.get("keepWorkspace")==="true";t.writeHead(200,{...R,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"});const s=(i,p,d)=>{if(t.writable)try{t.write(JSON.stringify({status:i,file:p,msg:d})+`
2
+ `)}catch{}};try{s("\u6210\u529F","","\u6B63\u5728\u505C\u6B62 QBotClaw...");const i=(0,_.readState)();if(i.gatewayPid)try{process.kill(i.gatewayPid,"SIGTERM"),(0,o.log)(`[uninstall] \u5DF2\u505C\u6B62 QBotClaw gateway \u8FDB\u7A0B pid: ${i.gatewayPid}`),s("\u6210\u529F","",`\u5DF2\u505C\u6B62 QBotClaw gateway \u8FDB\u7A0B pid: ${i.gatewayPid}`)}catch{(0,o.log)(`[uninstall] QBotClaw gateway \u8FDB\u7A0B ${i.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA`),s("\u6210\u529F","",`QBotClaw gateway \u8FDB\u7A0B ${i.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA`)}else s("\u6210\u529F","","QBotClaw \u672A\u5728\u8FD0\u884C");s("\u6210\u529F","",`\u6B63\u5728\u6267\u884C npm uninstall -g ${f.NPM_PACKAGE_NAME}...`);try{await new Promise((d,l)=>{const c=(0,C.spawn)(f.NPM_BIN,["--userconfig",f.NPM_RC,"uninstall","-g",f.NPM_PACKAGE_NAME],{env:{...process.env,PATH:`${f.NODE_BIN_DIR}${$.default.delimiter}${process.env.PATH}`},shell:process.platform==="win32",windowsHide:!0});c.stdout?.on("data",e=>{(0,o.log)(`[uninstall] npm: ${e.toString().trim()}`)}),c.stderr?.on("data",e=>{(0,o.log)(`[uninstall] npm stderr: ${e.toString().trim()}`)}),c.on("close",e=>{e===0?d():l(new Error(`npm uninstall \u9000\u51FA\u7801: ${e}`))}),c.on("error",e=>l(e))}),(0,o.log)(`[uninstall] npm uninstall -g ${f.NPM_PACKAGE_NAME} \u6210\u529F`),s("\u6210\u529F","",`npm uninstall -g ${f.NPM_PACKAGE_NAME} \u6210\u529F`)}catch(d){(0,o.log)(`[uninstall] npm uninstall \u5931\u8D25: ${d.message}`),s("\u5931\u8D25","",`npm uninstall \u5931\u8D25: ${d.message}`)}s("\u6210\u529F","",`\u6B63\u5728\u6E05\u7406\u76EE\u5F55 ${f.QBOT_CLAW_DIR}...`);const p=[];if(P.default.existsSync(f.QBOT_CLAW_DIR)){const d=P.default.readdirSync(f.QBOT_CLAW_DIR);for(const l of d){const c=$.default.join(f.QBOT_CLAW_DIR,l);if(r&&l==="workspace"){s("\u6210\u529F",c,"\u4FDD\u7559 workspace \u76EE\u5F55");continue}try{P.default.statSync(c).isDirectory()?P.default.rmSync(c,{recursive:!0,force:!0}):P.default.unlinkSync(c),s("\u6210\u529F",c,"\u5DF2\u5220\u9664")}catch(e){(0,o.log)(`[uninstall] \u5220\u9664\u5931\u8D25: ${c} - ${e.message}`),s("\u5931\u8D25",c,`\u5220\u9664\u5931\u8D25: ${e.message}`),p.push(c)}}if(p.length>0){s("\u6210\u529F","",`${p.length} \u4E2A\u9879\u76EE\u5220\u9664\u5931\u8D25\uFF0C\u4F7F\u7528 rm \u547D\u4EE4\u91CD\u8BD5...`);for(const l of p)try{process.platform==="win32"?(0,C.execSync)(`rmdir /s /q "${l}"`,{windowsHide:!0}):(0,C.execSync)(`rm -rf "${l}"`),(0,o.log)(`[uninstall] \u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u6210\u529F: ${l}`),s("\u6210\u529F",l,"\u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u5220\u9664\u6210\u529F")}catch(c){(0,o.log)(`[uninstall] \u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u4E5F\u5931\u8D25: ${l} - ${c.message}`),s("\u5931\u8D25",l,`\u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u4E5F\u5931\u8D25: ${c.message}`)}}if(r)s("\u6210\u529F",f.QBOT_CLAW_DIR,"\u4FDD\u7559\u6839\u76EE\u5F55\uFF08workspace \u6A21\u5F0F\uFF09");else try{P.default.rmSync(f.QBOT_CLAW_DIR,{recursive:!0,force:!0}),s("\u6210\u529F",f.QBOT_CLAW_DIR,"\u5DF2\u5220\u9664\u6839\u76EE\u5F55")}catch(l){(0,o.log)(`[uninstall] \u5220\u9664\u6839\u76EE\u5F55\u5931\u8D25: ${l.message}`);try{process.platform==="win32"?(0,C.execSync)(`rmdir /s /q "${f.QBOT_CLAW_DIR}"`,{windowsHide:!0}):(0,C.execSync)(`rm -rf "${f.QBOT_CLAW_DIR}"`),s("\u6210\u529F",f.QBOT_CLAW_DIR,"\u7CFB\u7EDF\u547D\u4EE4\u91CD\u8BD5\u5220\u9664\u6839\u76EE\u5F55\u6210\u529F")}catch(c){s("\u5931\u8D25",f.QBOT_CLAW_DIR,`\u5220\u9664\u6839\u76EE\u5F55\u5931\u8D25: ${c.message}`)}}}else s("\u6210\u529F",f.QBOT_CLAW_DIR,"\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u65E0\u9700\u6E05\u7406");s("\u6210\u529F","","\u5378\u8F7D\u5B8C\u6210")}catch(i){(0,o.log)(`[uninstall] \u5378\u8F7D\u8FC7\u7A0B\u5F02\u5E38: ${i.message}`),s("\u5931\u8D25","",`\u5378\u8F7D\u8FC7\u7A0B\u5F02\u5E38: ${i.message}`)}t.end(),setTimeout(()=>{(0,o.log)("[uninstall] \u5378\u8F7D\u6D41\u7A0B\u7ED3\u675F\uFF0Clauncher \u8FDB\u7A0B\u9000\u51FA"),process.exit(0)},500);return}if(g==="/api/file-picker"&&u.method==="POST"){const a=(0,F.authenticateRequest)(u);if(!a.ok){(0,n.sendJSON)(t,a.statusCode,{ok:!1,message:a.message});return}if((0,J.isFilePickerBusy)()){(0,n.sendJSON)(t,409,{ok:!1,code:y.DIALOG_BUSY,files:[],message:"\u5DF2\u6709\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u6B63\u5728\u663E\u793A\uFF0C\u8BF7\u7B49\u5F85\u5F53\u524D\u5BF9\u8BDD\u6846\u5173\u95ED\u540E\u91CD\u8BD5"});return}let r="";u.on("data",s=>{r+=s.toString()}),u.on("end",async()=>{let s=[],i=!1,p=H,d=b,l=B,c=M;if(r){let e;try{e=JSON.parse(r)}catch{(0,n.sendJSON)(t,400,{ok:!1,code:y.INVALID_PARAMS,files:[],message:"\u8BF7\u6C42\u4F53\u5FC5\u987B\u662F\u5408\u6CD5\u7684 JSON"});return}if(e.extensions!==void 0){if(!Array.isArray(e.extensions)||!e.extensions.every(w=>typeof w=="string")){(0,n.sendJSON)(t,400,{ok:!1,code:y.INVALID_PARAMS,files:[],message:"\u53C2\u6570 extensions \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4"});return}s=e.extensions}if(e.multiple!==void 0){if(typeof e.multiple!="boolean"){(0,n.sendJSON)(t,400,{ok:!1,code:y.INVALID_PARAMS,files:[],message:"\u53C2\u6570 multiple \u5FC5\u987B\u662F\u5E03\u5C14\u503C"});return}i=e.multiple}if(e.title!==void 0){if(typeof e.title!="string"||e.title.length>200){(0,n.sendJSON)(t,400,{ok:!1,code:y.INVALID_PARAMS,files:[],message:"\u53C2\u6570 title \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u4E14\u957F\u5EA6\u4E0D\u8D85\u8FC7 200"});return}p=e.title}if(e.maxFileSize!==void 0){if(typeof e.maxFileSize!="number"||e.maxFileSize<=0||!Number.isFinite(e.maxFileSize)){(0,n.sendJSON)(t,400,{ok:!1,code:y.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileSize \u5FC5\u987B\u662F\u6B63\u6570"});return}d=Math.min(e.maxFileSize,b)}if(e.maxFileCount!==void 0){if(typeof e.maxFileCount!="number"||e.maxFileCount<=0||!Number.isInteger(e.maxFileCount)){(0,n.sendJSON)(t,400,{ok:!1,code:y.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileCount \u5FC5\u987B\u662F\u6B63\u6574\u6570"});return}l=Math.min(e.maxFileCount,B)}if(M&&e.allowDirectory!==void 0){if(typeof e.allowDirectory!="boolean"){(0,n.sendJSON)(t,400,{ok:!1,code:y.INVALID_PARAMS,files:[],message:"\u53C2\u6570 allowDirectory \u5FC5\u987B\u662F\u5E03\u5C14\u503C"});return}c=e.allowDirectory}}i||(l=1);try{const e=await(0,J.showFilePicker)({extensions:s,multiple:i,title:p,allowDirectory:c});if(!e||e.length===0){(0,n.sendJSON)(t,200,{ok:!1,code:y.CANCELLED,files:[],message:"\u7528\u6237\u53D6\u6D88\u4E86\u6587\u4EF6\u9009\u62E9"});return}if(c){const I=e.slice(0,l).map(D=>({name:$.default.basename(D),path:D,size:0,mimeType:"inode/directory"})),h=e.length>l;(0,n.sendJSON)(t,200,{ok:!0,files:I,message:h?`\u5DF2\u9009\u62E9 ${I.length} \u4E2A\u76EE\u5F55\uFF08\u5171\u9009\u62E9 ${e.length} \u4E2A\uFF0C\u8D85\u51FA\u4E0A\u9650 ${l} \u4E2A\u5DF2\u622A\u65AD\uFF09`:`\u5DF2\u9009\u62E9 ${I.length} \u4E2A\u76EE\u5F55`});return}let w=!1,m=e;e.length>l&&(m=e.slice(0,l),w=!0);const O=[],S=[];for(const I of m){const h=(0,x.readFileInfo)(I,d);h.ok?O.push({name:h.name,path:h.path,size:h.size,mimeType:h.mimeType}):S.push({path:h.path,reason:h.reason,code:h.code})}const N=[];if(O.length>0&&N.push(`\u6210\u529F\u8BFB\u53D6 ${O.length} \u4E2A\u6587\u4EF6`),S.length>0){const I=S.map(h=>`${$.default.basename(h.path)}: ${h.reason}`).join("\uFF1B");N.push(`${S.length} \u4E2A\u6587\u4EF6\u5931\u8D25\uFF08${I}\uFF09`)}w&&N.push(`\u5171\u9009\u62E9 ${e.length} \u4E2A\u6587\u4EF6\uFF0C\u8D85\u51FA\u4E0A\u9650 ${l} \u4E2A\u5DF2\u622A\u65AD`);const A=O.length===0&&S.length>0;(0,n.sendJSON)(t,200,{ok:!A,code:A?S[0].code:void 0,files:O,failedFiles:S.length>0?S:void 0,message:N.join("\uFF1B")||"\u6587\u4EF6\u9009\u62E9\u6210\u529F"})}catch(e){(0,o.log)(`[file-picker] \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${e.message}`);let w=y.INTERNAL_ERROR;e.message.includes("\u8D85\u65F6")?w=y.DIALOG_TIMEOUT:e.message.includes("\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0")&&(w=y.PLATFORM_NOT_SUPPORTED),(0,n.sendJSON)(t,500,{ok:!1,code:w,files:[],message:`\u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${e.message}`})}});return}if(g.startsWith("/api/reveal")){console.log(`[reveal] \u5B8C\u6574URL: ${g}`);const a=(0,F.authenticateRequest)(u);if(!a.ok){(0,n.sendJSON)(t,a.statusCode,{status:"\u5931\u8D25",message:a.message});return}const s=new URL(g,"http://localhost").searchParams.get("path")?.replace(/ /g,"+")??null;if(!s){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 path\uFF08\u9700 Base64 \u7F16\u7801\uFF09\uFF0C\u7528\u6CD5: GET /api/reveal?path=<base64\u7F16\u7801\u7684\u6587\u4EF6\u7EDD\u5BF9\u8DEF\u5F84>"});return}let i;try{i=Buffer.from(s,"base64").toString("utf-8")}catch{(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"path \u53C2\u6570 Base64 \u89E3\u7801\u5931\u8D25\uFF0C\u8BF7\u786E\u4FDD\u4F20\u5165\u5408\u6CD5\u7684 Base64 \u7F16\u7801\u5B57\u7B26\u4E32"});return}if(!i||i.trim()===""){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"Base64 \u89E3\u7801\u540E\u8DEF\u5F84\u4E3A\u7A7A"});return}const p=(0,x.validateFilePath)(i);if(!p.valid){const m=p.reason;m.includes("\u975E\u6CD5\u5B57\u7B26")?(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:m}):m.includes("\u4E0D\u5B58\u5728")?(0,n.sendJSON)(t,404,{status:"\u5931\u8D25",message:m}):m.includes("\u65E0\u6CD5\u89E3\u6790")?(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:m}):((0,o.log)(`[reveal] \u5B89\u5168\u68C0\u67E5\u88AB\u62D2\u7EDD: ${i}\uFF0C\u539F\u56E0: ${m}`),(0,n.sendJSON)(t,403,{status:"\u5931\u8D25",message:m}));return}const d=$.default.resolve(i),l=process.platform;let c,e,w={stdio:"ignore",windowsHide:!0};l==="darwin"?(c="open",e=["-R",d]):l==="win32"?(c="explorer.exe",e=[`/select,"${d}"`],w={...w,shell:!0}):(c="xdg-open",e=[$.default.dirname(d)]),(0,o.log)(`\u6267\u884C\u547D\u4EE4: ${c} ${e.join(" ")}`);try{await new Promise((m,O)=>{const S=(0,C.spawn)(c,e,w),N=setTimeout(()=>{S.kill(),O(new Error("\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u8D85\u65F6\uFF085\u79D2\uFF09"))},5e3);S.on("close",A=>{clearTimeout(N),l==="win32"||A===0||A===1?m():O(new Error(`\u8FDB\u7A0B\u9000\u51FA\u7801: ${A}`))}),S.on("error",A=>{clearTimeout(N),O(A)})}),(0,o.log)(`[reveal] \u5DF2\u5728\u6587\u4EF6\u7BA1\u7406\u5668\u4E2D\u663E\u793A: ${d}`),(0,n.sendJSON)(t,200,{status:"\u6210\u529F",message:`\u5DF2\u5728\u6587\u4EF6\u7BA1\u7406\u5668\u4E2D\u663E\u793A: ${d}`})}catch(m){(0,o.log)(`[reveal] \u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u5931\u8D25: ${m.message}`),(0,n.sendJSON)(t,500,{status:"\u5931\u8D25",message:`\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u5931\u8D25: ${m.message}`})}return}(0,n.sendJSON)(t,404,{status:"\u5931\u8D25",message:"\u63A5\u53E3\u4E0D\u5B58\u5728"})}});let T=null,E=null;function W(u){if(!u)return;if(T&&E===u){(0,o.log)(`[watchParentProcess] \u5DF2\u5728\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${u}\uFF0C\u8DF3\u8FC7\u91CD\u590D\u76D1\u542C`);return}T?((0,o.log)(`[watchParentProcess] \u5DF2\u6709\u76D1\u63A7\uFF08ppid: ${E}\uFF09\uFF0C\u505C\u6B62\u65E7\u7684\u76D1\u63A7\uFF0C\u91CD\u65B0\u76D1\u63A7 ppid: ${u}`),clearInterval(T),T=null,E=null):(0,o.log)(`[watchParentProcess] \u5F00\u59CB\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${u}`);const t=setInterval(()=>{try{process.kill(u,0)}catch{(0,o.log)(`[watchParentProcess] \u7236\u8FDB\u7A0B ${u} \u5DF2\u9000\u51FA\uFF0C\u5F00\u59CB\u6E05\u7406\u5E76\u9000\u51FA`),clearInterval(t),T=null,E=null,(0,_.stopGateway)(),(0,_.clearState)(),(0,o.log)("[watchParentProcess] \u72B6\u6001\u5DF2\u6E05\u7406\uFF0C\u5173\u95ED server \u5E76\u9000\u51FA"),exports.server.close(()=>{(0,o.log)("[watchParentProcess] server \u5DF2\u5173\u95ED\uFF0C\u8FDB\u7A0B\u9000\u51FA"),process.exit(0)}),setTimeout(()=>{(0,o.log)("[watchParentProcess] server.close \u8D85\u65F6\uFF0C\u5F3A\u5236\u9000\u51FA"),process.exit(0)},3e3).unref()}},3e3);t.unref(),T=t,E=u}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qqbrowser/qbot-claw-launcher",
3
- "version": "0.10.16",
3
+ "version": "0.10.17",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "bin": {