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