@qqbrowser/qbot-claw-launcher 0.10.11 → 0.11.10

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.
@@ -34,12 +34,10 @@ class ContentReviewApi extends trpc_1.TrpcApi {
34
34
  }
35
35
  exports.contentReviewApi = new ContentReviewApi();
36
36
  class ContentReviewFailedError extends Error {
37
- constructor(fallbackMsg, is_show, phase_action) {
37
+ constructor(fallbackMsg) {
38
38
  super('内容审核未通过');
39
39
  this.name = 'ContentReviewFailedError';
40
40
  this.fallbackMsg = fallbackMsg;
41
- this.is_show = is_show;
42
- this.phase_action = phase_action;
43
41
  }
44
42
  }
45
43
  exports.ContentReviewFailedError = ContentReviewFailedError;
@@ -66,9 +64,8 @@ const reviewText = async (text, auditContentType, authOrLoginInfo) => {
66
64
  else {
67
65
  rsp = await exports.contentReviewApi.getReviewResult(body, authOrLoginInfo);
68
66
  }
69
- (0, reportLog_1.log)(`[content-review] rsp: ${JSON.stringify(rsp)}`);
70
67
  if (!rsp.pass) {
71
- throw new ContentReviewFailedError(rsp.fallback_msg, rsp.is_show, rsp.phase_action);
68
+ throw new ContentReviewFailedError(rsp.fallback_msg);
72
69
  }
73
70
  return rsp.session_id;
74
71
  };
@@ -1 +1 @@
1
- {"version":3,"file":"content-review.js","sourceRoot":"","sources":["../../src/api/content-review.ts"],"names":[],"mappings":"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"}
1
+ {"version":3,"file":"content-review.js","sourceRoot":"","sources":["../../src/api/content-review.ts"],"names":[],"mappings":"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"}
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.11",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.11.10",exports.IS_DEBUG_BUILD=!1;
package/dist/installer.js CHANGED
@@ -1,12 +1,12 @@
1
- "use strict";var y=exports&&exports.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.PYTHON_DIR=exports.isInstalling=exports.installState=void 0,exports.runQBotClaw=T,exports.configAndRunQBotClaw=C,exports.runInstall=b;const c=y(require("fs")),g=y(require("path")),A=require("child_process"),a=require("./config");Object.defineProperty(exports,"PYTHON_DIR",{enumerable:!0,get:function(){return a.PYTHON_DIR}});const E=require("./utils"),w=require("./state"),s=require("./reportLog"),O=require("./service"),L=require("./openai-proxy"),f=require("./report"),B=require("./qqbrowser-skill-installer");let m="qqbrowser-skill-control";const I=[];function N(t,e){if(t&&t.writable)try{t.write(e)}catch{}}function d(t,e,i,l){const o=e>0?Math.round(i/e*1e4)/100:0,n=JSON.stringify({status:"downloading",progress:o,totalSteps:e,currentStep:i,message:l})+`
2
- `;N(t,n);for(const r of I)r!==t&&N(r,n)}function S(t){const e=t+`
3
- `;for(const i of I)if(i&&i.writable)try{i.end(e)}catch{}I.length=0}exports.installState={status:"idle",message:"\u672A\u5F00\u59CB"},exports.isInstalling=!1;function T(t,e){return(0,s.log)(`[QBotClaw] \u6267\u884C: QBotClaw ${t.join(" ")}`),new Promise(i=>{const l=(0,w.readState)(),o=(0,A.spawn)(a.QBOTCLAW_BIN,t,{stdio:["ignore","pipe","pipe"],shell:process.platform==="win32",windowsHide:!0,env:{...process.platform==="win32"?process.env:{},PATH:`${a.NODE_BIN_DIR}${g.default.delimiter}${process.env.PATH||""}`,HOME:process.env.HOME||process.env.USERPROFILE,USERPROFILE:process.env.USERPROFILE,OPENCLAW_TELEMETRY_GALIELO_GUID:process.env.QB_GUID||"",OPENCLAW_TELEMETRY_GALIELO_QIEMI36:process.env.QB_QIMEI36||"",QBOTCLAW_LLM_BASE_URL:`http://127.0.0.1:${l.port}/v1`,QBOTCLAW_LLM_API_KEY:L.proxyApiKey}});(0,s.log)(`[QBotClaw] \u6267\u884C: QBotClaw ${t.join(" ")}`),o.stdout.on("data",n=>{const r=n.toString();if(e&&e.writable)try{e.write(r)}catch{}}),o.stderr.on("data",n=>{const r=n.toString();if(e&&e.writable)try{e.write(r)}catch{}}),o.on("close",n=>{(0,s.log)(`[QBotClaw] \u6267\u884C\u5B8C\u6210\uFF0C\u9000\u51FA\u7801: ${n}`),i()}),o.on("error",n=>{(0,s.log)(`[QBotClaw] \u542F\u52A8\u5931\u8D25: ${n.message}`),i()})})}function Q(){c.default.existsSync(a.QBOT_CLAW_DIR)||c.default.mkdirSync(a.QBOT_CLAW_DIR,{recursive:!0})}function h(){const t=process.platform==="win32"?"pip.ini":"pip.conf",e=g.default.join(a.PYTHON_DIR,t);if(c.default.existsSync(e)){(0,s.log)(`${t} \u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7`);return}const i=["[global]","index-url = https://mirrors.tencent.com/pypi/simple/",`cache-dir = ${g.default.join(a.PYTHON_DIR,".pip-cache")}`,"","[install]",`prefix = ${a.PYTHON_DIR}`,""].join(`
4
- `);c.default.writeFileSync(e,i,"utf-8"),(0,s.log)(`\u5DF2\u5199\u5165 ${t}: ${e}`)}function $(t,e){return new Promise((i,l)=>{const o=(0,A.spawn)(a.NPM_BIN,["--userconfig",a.NPM_RC,...t],{stdio:["ignore","pipe","pipe"],shell:process.platform==="win32",windowsHide:!0,env:{...process.env,PATH:`${g.default.dirname(a.NPM_BIN)}${g.default.delimiter}${process.env.PATH}`}});o.stdout.on("data",n=>{const r=`[npm] ${n.toString().trim()}`;(0,s.log)(r)}),o.stderr.on("data",n=>{const r=`[npm] ${n.toString().trim()}`;(0,s.log)(r)}),o.on("close",n=>{n===0?((0,s.log)(`${e} \u6210\u529F`),i()):l(new Error(`${e} \u5931\u8D25\uFF0C\u9000\u51FA\u7801: ${n}`))}),o.on("error",l)})}function R(){const t=g.default.join(a.QBOT_CLAW_DIR,"node",".npm-cache","_logs");try{if(!c.default.existsSync(t)){(0,s.log)(`[dumpNpmLogs] npm \u65E5\u5FD7\u76EE\u5F55\u4E0D\u5B58\u5728: ${t}`);return}const e=c.default.readdirSync(t).filter(i=>c.default.statSync(g.default.join(t,i)).isFile()).sort((i,l)=>{const o=c.default.statSync(g.default.join(t,i));return c.default.statSync(g.default.join(t,l)).mtimeMs-o.mtimeMs}).slice(0,3);if(e.length===0){(0,s.log)(`[dumpNpmLogs] npm \u65E5\u5FD7\u76EE\u5F55\u4E3A\u7A7A: ${t}`);return}(0,s.log)(`[dumpNpmLogs] \u8BFB\u53D6\u6700\u65B0\u7684 ${e.length} \u4E2A npm \u65E5\u5FD7\u6587\u4EF6:`);for(const i of e){const l=g.default.join(t,i);try{const o=c.default.readFileSync(l,"utf-8");(0,s.log)(`[npm-log] ===== ${i} =====
5
- ${o}`)}catch(o){(0,s.log)(`[npm-log] \u8BFB\u53D6\u65E5\u5FD7\u6587\u4EF6\u5931\u8D25 ${i}: ${o.message}`)}}}catch(e){(0,s.log)(`[dumpNpmLogs] \u8BFB\u53D6 npm \u65E5\u5FD7\u76EE\u5F55\u5931\u8D25: ${e.message}`)}}async function v(t){const e=`${a.NPM_PACKAGE_NAME}@${t}`,i=`${e} \u4E0B\u8F7D\u89E3\u538B`;(0,s.log)(`\u5F00\u59CB\u5B89\u88C5 ${e}\uFF08--ignore-scripts\uFF0C\u4EC5\u4E0B\u8F7D\u89E3\u538B\uFF09...`);let l;for(let o=0;o<a.NPM_REGISTRIES.length;o++){const n=a.NPM_REGISTRIES[o];try{(0,s.log)(`[${i}] \u5C1D\u8BD5\u4F7F\u7528\u6E90 (${o+1}/${a.NPM_REGISTRIES.length}): ${n}`),await $(["install","-g","--ignore-scripts","--registry",n,e],i);return}catch(r){l=r,(0,s.log)(`[${i}] \u6E90 ${n} \u5931\u8D25: ${r.message}`),o<a.NPM_REGISTRIES.length-1&&(0,s.log)(`[${i}] \u5C06\u5C1D\u8BD5\u4E0B\u4E00\u4E2A\u6E90...`)}}throw(0,s.log)(`\u5B89\u88C5 ${e} \u6240\u6709\u6E90\u5747\u5931\u8D25`),R(),l}async function D(t){const e=`${a.NPM_PACKAGE_NAME}@${t}`;(0,s.log)("\u5F00\u59CB npm rebuild\uFF08\u6267\u884C postinstall \u811A\u672C\uFF09..."),await $(["rebuild","-g",a.NPM_PACKAGE_NAME],`${e} rebuild`),(0,s.log)(`${e} \u5B89\u88C5\u6210\u529F`)}async function M(t,e,i,l){try{const r=process.platform==="win32"?`"${a.QBOTCLAW_BIN}" config validate --json`:`"${a.NODE_BIN}" "${a.QBOTCLAW_BIN}" config validate --json`,p=await(0,E.execCommand)(r);(0,s.log)(`[config validate] \u8F93\u51FA: ${p}`);const u=JSON.parse(p);u.valid!==!0?((0,s.log)(`[config validate] \u914D\u7F6E\u6821\u9A8C\u672A\u901A\u8FC7\uFF08valid=${u.valid}\uFF09\uFF0C\u5F3A\u5236\u8986\u76D6\u914D\u7F6E`),l=!0):(0,s.log)("[config validate] \u914D\u7F6E\u6821\u9A8C\u901A\u8FC7")}catch(r){(0,s.log)(`[config validate] \u6267\u884C\u5931\u8D25: ${r.message}\uFF0C\u5F3A\u5236\u8986\u76D6\u914D\u7F6E`),l=!0}let o;if((0,s.log)(`\u8986\u76D6\u7528\u6237\u5DF2\u6709\u914D\u7F6E: ${l}`),!l&&c.default.existsSync(a.QBOT_CLAW_CONFIG_PATH))o=a.QBOT_CLAW_CONFIG_PATH,(0,s.log)(`\u4F7F\u7528\u5DF2\u6709\u914D\u7F6E\u6587\u4EF6: ${o}`);else{const r=g.default.join(a.NPM_GLOBAL_MODULES,a.NPM_PACKAGE_NAME,"preset-config.json");if(!c.default.existsSync(r)){(0,s.log)(`npm \u5168\u5C40\u5B89\u88C5\u76EE\u5F55\u4E2D\u7684\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u914D\u7F6E\u5199\u5165: ${r}`);return}o=r,(0,s.log)(`\u4F7F\u7528 npm \u5168\u5C40\u5B89\u88C5\u76EE\u5F55\u4E2D\u7684\u914D\u7F6E: ${o}`)}let n;try{n=JSON.parse(c.default.readFileSync(o,"utf-8")),(0,s.log)(`\u5DF2\u8BFB\u53D6\u914D\u7F6E: ${o}`)}catch(r){(0,s.log)(`\u8BFB\u53D6\u914D\u7F6E\u5931\u8D25: ${o}\uFF0C${r.message}\uFF0C\u8DF3\u8FC7\u914D\u7F6E\u5199\u5165`);return}n.gateway={...n.gateway??{},port:t},n.gateway.auth={...n.gateway.auth??{},token:e},(0,s.log)(`\u5DF2\u5199\u5165 gateway.port=${t}, gateway.token=${e}`),n.plugins=n.plugins??{},n.plugins.entries=n.plugins.entries??{},n.plugins.entries[m]=n.plugins.entries[m]??{},n.plugins.entries[m].config={...n.plugins.entries[m].config??{},wsPort:i},(0,s.log)(`\u5DF2\u5C06 ${m} wsPort=${i} \u5199\u5165\u914D\u7F6E`),c.default.writeFileSync(a.QBOT_CLAW_CONFIG_PATH,JSON.stringify(n,null,2),"utf-8"),(0,s.log)(`\u914D\u7F6E\u6587\u4EF6\u5DF2\u5199\u5165: ${a.QBOT_CLAW_CONFIG_PATH}`)}function W(){try{if(!c.default.existsSync(a.GALILEO_CONFIG_PATH)){(0,s.log)(`[galileo] \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7: ${a.GALILEO_CONFIG_PATH}`);return}const t=c.default.readFileSync(a.GALILEO_CONFIG_PATH,"utf-8"),e=JSON.parse(t),i=e.galileo;if(!i){(0,s.log)("[galileo] \u914D\u7F6E\u6587\u4EF6\u4E2D\u7F3A\u5C11 galileo \u5B57\u6BB5\uFF0C\u8DF3\u8FC7");return}const l=process.env.QB_QIMEI36||"";i.userId==="userid"&&l?(i.userId=l,i.customAttributes={qimei36:l,guid:process.env.QB_GUID||""},c.default.writeFileSync(a.GALILEO_CONFIG_PATH,JSON.stringify(e,null,2),"utf-8"),(0,s.log)(`[galileo] \u5DF2\u5C06 userId \u66F4\u65B0\u4E3A ${l}\uFF0CcustomAttributes \u5DF2\u5199\u5165`)):(0,s.log)(`[galileo] \u65E0\u9700\u66F4\u65B0\uFF08userId=${i.userId}, QB_QIMEI36=${l||"\u7A7A"})`)}catch(t){(0,s.log)(`[galileo] \u8BFB\u53D6/\u5199\u5165\u914D\u7F6E\u5931\u8D25: ${t.message}`)}}async function C(t){const e=Date.now(),i=await(0,E.getQBotClawVersion)()??"unknown";try{W();const l=(0,w.readState)();let o;l.gatewayPort&&!await(0,w.isPortAvailable)(l.gatewayPort)?(o=l.gatewayPort,(0,s.log)(`\u68C0\u6D4B\u5230 QBotClaw gateway \u5DF2\u5728\u7AEF\u53E3 ${o} \u8FD0\u884C\uFF0C\u590D\u7528\u539F\u7AEF\u53E3`)):o=await(0,E.findAvailablePort)(),(0,s.log)(`\u540E\u53F0\u542F\u52A8 QBotClaw gateway run --port ${o} --force`);const n=await(0,E.findAvailablePort)(),r=(0,O.getOrCreateGatewayToken)();await M(o,r,n,t),G(r);const p=(0,A.spawn)(a.QBOTCLAW_BIN,["gateway","run","--port",String(o),"--force"],{stdio:"ignore",shell:process.platform==="win32",windowsHide:!0,env:{...process.platform==="win32"?process.env:{PATH:a.NODE_BIN_DIR},HOME:process.env.HOME||process.env.USERPROFILE,USERPROFILE:process.env.USERPROFILE,OPENCLAW_NO_RESPAWN:"true",VENUS_API_KEY:"E9isWWfAr4VIFuIa5RbJaPxD@2040",OPENCLAW_TELEMETRY_GALIELO_GUID:process.env.QB_GUID||"",OPENCLAW_TELEMETRY_GALIELO_QIEMI36:process.env.QB_QIMEI36||"",QBOTCLAW_LLM_BASE_URL:`http://127.0.0.1:${l.port}/v1`,QBOTCLAW_LLM_API_KEY:L.proxyApiKey}});p.unref();const u=p.pid;(0,s.log)(`QBotClaw gateway \u5DF2\u5728\u540E\u53F0\u542F\u52A8\uFF0CPID: ${u}\uFF0C\u7AEF\u53E3: ${o}, token ${(0,O.getOrCreateGatewayToken)()}`),(0,w.writeState)({...(0,w.readState)(),gatewayPid:u,gatewayPort:o,mcpWsPort:n}),(0,f.report)({event_name:"qbot_claw_started",version:i,result:"1",cost_ms:String(Date.now()-e)})}catch(l){throw(0,f.report)({event_name:"qbot_claw_started",version:i,result:"0",cost_ms:String(Date.now()-e),error:l.message}),l}}function G(t){const e=g.default.join(a.QBOT_CLAW_DIR,".env"),i=`OPENCLAW_GATEWAY_TOKEN=${t}
6
- `;try{if(c.default.existsSync(e)){const n=c.default.readFileSync(e,"utf-8").split(`
1
+ "use strict";var y=exports&&exports.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.PYTHON_DIR=exports.isInstalling=exports.sandboxState=exports.installState=void 0,exports.runQBotClaw=B,exports.configAndRunQBotClaw=C,exports.checkSandboxInitialization=W,exports.runInstall=x;const c=y(require("fs")),g=y(require("path")),E=require("child_process"),s=require("./config");Object.defineProperty(exports,"PYTHON_DIR",{enumerable:!0,get:function(){return s.PYTHON_DIR}});const S=require("./utils"),w=require("./state"),o=require("./reportLog"),A=require("./service"),N=require("./openai-proxy"),p=require("./report"),T=require("./qqbrowser-skill-installer");let m="qqbrowser-skill-control";const I=[];function O(t,n){if(t&&t.writable)try{t.write(n)}catch{}}function f(t,n,i,l){const a=n>0?Math.round(i/n*1e4)/100:0,e=JSON.stringify({status:"downloading",progress:a,totalSteps:n,currentStep:i,message:l})+`
2
+ `;O(t,e);for(const r of I)r!==t&&O(r,e)}function $(t){const n=t+`
3
+ `;for(const i of I)if(i&&i.writable)try{i.end(n)}catch{}I.length=0}exports.installState={status:"idle",message:"\u672A\u5F00\u59CB"},exports.sandboxState={initializing:!1,message:"",elapsedSeconds:0},exports.isInstalling=!1;function B(t,n){return(0,o.log)(`[QBotClaw] \u6267\u884C: QBotClaw ${t.join(" ")}`),new Promise(i=>{const l=(0,w.readState)(),a=(0,E.spawn)(s.QBOTCLAW_BIN,t,{stdio:["ignore","pipe","pipe"],shell:process.platform==="win32",windowsHide:!0,env:{...process.platform==="win32"?process.env:{},PATH:`${s.NODE_BIN_DIR}${g.default.delimiter}${process.env.PATH||""}`,HOME:process.env.HOME||process.env.USERPROFILE,USERPROFILE:process.env.USERPROFILE,OPENCLAW_TELEMETRY_GALIELO_GUID:process.env.QB_GUID||"",OPENCLAW_TELEMETRY_GALIELO_QIEMI36:process.env.QB_QIMEI36||"",QBOTCLAW_LLM_BASE_URL:`http://127.0.0.1:${l.port}/v1`,QBOTCLAW_LLM_API_KEY:N.proxyApiKey}});(0,o.log)(`[QBotClaw] \u6267\u884C: QBotClaw ${t.join(" ")}`),a.stdout.on("data",e=>{const r=e.toString();if(n&&n.writable)try{n.write(r)}catch{}}),a.stderr.on("data",e=>{const r=e.toString();if(n&&n.writable)try{n.write(r)}catch{}}),a.on("close",e=>{(0,o.log)(`[QBotClaw] \u6267\u884C\u5B8C\u6210\uFF0C\u9000\u51FA\u7801: ${e}`),i()}),a.on("error",e=>{(0,o.log)(`[QBotClaw] \u542F\u52A8\u5931\u8D25: ${e.message}`),i()})})}function h(){c.default.existsSync(s.QBOT_CLAW_DIR)||c.default.mkdirSync(s.QBOT_CLAW_DIR,{recursive:!0})}function Q(){const t=process.platform==="win32"?"pip.ini":"pip.conf",n=g.default.join(s.PYTHON_DIR,t);if(c.default.existsSync(n)){(0,o.log)(`${t} \u5DF2\u5B58\u5728\uFF0C\u8DF3\u8FC7`);return}const i=["[global]","index-url = https://mirrors.tencent.com/pypi/simple/",`cache-dir = ${g.default.join(s.PYTHON_DIR,".pip-cache")}`,"","[install]",`prefix = ${s.PYTHON_DIR}`,""].join(`
4
+ `);c.default.writeFileSync(n,i,"utf-8"),(0,o.log)(`\u5DF2\u5199\u5165 ${t}: ${n}`)}function L(t,n){return new Promise((i,l)=>{const a=(0,E.spawn)(s.NPM_BIN,["--userconfig",s.NPM_RC,...t],{stdio:["ignore","pipe","pipe"],shell:process.platform==="win32",windowsHide:!0,env:{...process.env,PATH:`${g.default.dirname(s.NPM_BIN)}${g.default.delimiter}${process.env.PATH}`}});a.stdout.on("data",e=>{const r=`[npm] ${e.toString().trim()}`;(0,o.log)(r)}),a.stderr.on("data",e=>{const r=`[npm] ${e.toString().trim()}`;(0,o.log)(r)}),a.on("close",e=>{e===0?((0,o.log)(`${n} \u6210\u529F`),i()):l(new Error(`${n} \u5931\u8D25\uFF0C\u9000\u51FA\u7801: ${e}`))}),a.on("error",l)})}function R(){const t=g.default.join(s.QBOT_CLAW_DIR,"node",".npm-cache","_logs");try{if(!c.default.existsSync(t)){(0,o.log)(`[dumpNpmLogs] npm \u65E5\u5FD7\u76EE\u5F55\u4E0D\u5B58\u5728: ${t}`);return}const n=c.default.readdirSync(t).filter(i=>c.default.statSync(g.default.join(t,i)).isFile()).sort((i,l)=>{const a=c.default.statSync(g.default.join(t,i));return c.default.statSync(g.default.join(t,l)).mtimeMs-a.mtimeMs}).slice(0,3);if(n.length===0){(0,o.log)(`[dumpNpmLogs] npm \u65E5\u5FD7\u76EE\u5F55\u4E3A\u7A7A: ${t}`);return}(0,o.log)(`[dumpNpmLogs] \u8BFB\u53D6\u6700\u65B0\u7684 ${n.length} \u4E2A npm \u65E5\u5FD7\u6587\u4EF6:`);for(const i of n){const l=g.default.join(t,i);try{const a=c.default.readFileSync(l,"utf-8");(0,o.log)(`[npm-log] ===== ${i} =====
5
+ ${a}`)}catch(a){(0,o.log)(`[npm-log] \u8BFB\u53D6\u65E5\u5FD7\u6587\u4EF6\u5931\u8D25 ${i}: ${a.message}`)}}}catch(n){(0,o.log)(`[dumpNpmLogs] \u8BFB\u53D6 npm \u65E5\u5FD7\u76EE\u5F55\u5931\u8D25: ${n.message}`)}}async function v(t){const n=`${s.NPM_PACKAGE_NAME}@${t}`,i=`${n} \u4E0B\u8F7D\u89E3\u538B`;(0,o.log)(`\u5F00\u59CB\u5B89\u88C5 ${n}\uFF08--ignore-scripts\uFF0C\u4EC5\u4E0B\u8F7D\u89E3\u538B\uFF09...`);let l;for(let a=0;a<s.NPM_REGISTRIES.length;a++){const e=s.NPM_REGISTRIES[a];try{(0,o.log)(`[${i}] \u5C1D\u8BD5\u4F7F\u7528\u6E90 (${a+1}/${s.NPM_REGISTRIES.length}): ${e}`),await L(["install","-g","--ignore-scripts","--registry",e,n],i);return}catch(r){l=r,(0,o.log)(`[${i}] \u6E90 ${e} \u5931\u8D25: ${r.message}`),a<s.NPM_REGISTRIES.length-1&&(0,o.log)(`[${i}] \u5C06\u5C1D\u8BD5\u4E0B\u4E00\u4E2A\u6E90...`)}}throw(0,o.log)(`\u5B89\u88C5 ${n} \u6240\u6709\u6E90\u5747\u5931\u8D25`),R(),l}async function D(t){const n=`${s.NPM_PACKAGE_NAME}@${t}`;(0,o.log)("\u5F00\u59CB npm rebuild\uFF08\u6267\u884C postinstall \u811A\u672C\uFF09..."),await L(["rebuild","-g",s.NPM_PACKAGE_NAME],`${n} rebuild`),(0,o.log)(`${n} \u5B89\u88C5\u6210\u529F`)}async function M(t,n,i,l){try{const r=process.platform==="win32"?`"${s.QBOTCLAW_BIN}" config validate --json`:`"${s.NODE_BIN}" "${s.QBOTCLAW_BIN}" config validate --json`,u=await(0,S.execCommand)(r);(0,o.log)(`[config validate] \u8F93\u51FA: ${u}`);const _=JSON.parse(u);_.valid!==!0?((0,o.log)(`[config validate] \u914D\u7F6E\u6821\u9A8C\u672A\u901A\u8FC7\uFF08valid=${_.valid}\uFF09\uFF0C\u5F3A\u5236\u8986\u76D6\u914D\u7F6E`),l=!0):(0,o.log)("[config validate] \u914D\u7F6E\u6821\u9A8C\u901A\u8FC7")}catch(r){(0,o.log)(`[config validate] \u6267\u884C\u5931\u8D25: ${r.message}\uFF0C\u5F3A\u5236\u8986\u76D6\u914D\u7F6E`),l=!0}let a;if((0,o.log)(`\u8986\u76D6\u7528\u6237\u5DF2\u6709\u914D\u7F6E: ${l}`),!l&&c.default.existsSync(s.QBOT_CLAW_CONFIG_PATH))a=s.QBOT_CLAW_CONFIG_PATH,(0,o.log)(`\u4F7F\u7528\u5DF2\u6709\u914D\u7F6E\u6587\u4EF6: ${a}`);else{const r=g.default.join(s.NPM_GLOBAL_MODULES,s.NPM_PACKAGE_NAME,"preset-config.json");if(!c.default.existsSync(r)){(0,o.log)(`npm \u5168\u5C40\u5B89\u88C5\u76EE\u5F55\u4E2D\u7684\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u914D\u7F6E\u5199\u5165: ${r}`);return}a=r,(0,o.log)(`\u4F7F\u7528 npm \u5168\u5C40\u5B89\u88C5\u76EE\u5F55\u4E2D\u7684\u914D\u7F6E: ${a}`)}let e;try{e=JSON.parse(c.default.readFileSync(a,"utf-8")),(0,o.log)(`\u5DF2\u8BFB\u53D6\u914D\u7F6E: ${a}`)}catch(r){(0,o.log)(`\u8BFB\u53D6\u914D\u7F6E\u5931\u8D25: ${a}\uFF0C${r.message}\uFF0C\u8DF3\u8FC7\u914D\u7F6E\u5199\u5165`);return}e.gateway={...e.gateway??{},port:t},e.gateway.auth={...e.gateway.auth??{},token:n},(0,o.log)(`\u5DF2\u5199\u5165 gateway.port=${t}, gateway.token=${n}`),e.plugins=e.plugins??{},e.plugins.entries=e.plugins.entries??{},e.plugins.entries[m]=e.plugins.entries[m]??{},e.plugins.entries[m].config={...e.plugins.entries[m].config??{},wsPort:i},(0,o.log)(`\u5DF2\u5C06 ${m} wsPort=${i} \u5199\u5165\u914D\u7F6E`),c.default.writeFileSync(s.QBOT_CLAW_CONFIG_PATH,JSON.stringify(e,null,2),"utf-8"),(0,o.log)(`\u914D\u7F6E\u6587\u4EF6\u5DF2\u5199\u5165: ${s.QBOT_CLAW_CONFIG_PATH}`)}function b(){try{if(!c.default.existsSync(s.GALILEO_CONFIG_PATH)){(0,o.log)(`[galileo] \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7: ${s.GALILEO_CONFIG_PATH}`);return}const t=c.default.readFileSync(s.GALILEO_CONFIG_PATH,"utf-8"),n=JSON.parse(t),i=n.galileo;if(!i){(0,o.log)("[galileo] \u914D\u7F6E\u6587\u4EF6\u4E2D\u7F3A\u5C11 galileo \u5B57\u6BB5\uFF0C\u8DF3\u8FC7");return}const l=process.env.QB_QIMEI36||"";i.userId==="userid"&&l?(i.userId=l,i.customAttributes={qimei36:l,guid:process.env.QB_GUID||""},c.default.writeFileSync(s.GALILEO_CONFIG_PATH,JSON.stringify(n,null,2),"utf-8"),(0,o.log)(`[galileo] \u5DF2\u5C06 userId \u66F4\u65B0\u4E3A ${l}\uFF0CcustomAttributes \u5DF2\u5199\u5165`)):(0,o.log)(`[galileo] \u65E0\u9700\u66F4\u65B0\uFF08userId=${i.userId}, QB_QIMEI36=${l||"\u7A7A"})`)}catch(t){(0,o.log)(`[galileo] \u8BFB\u53D6/\u5199\u5165\u914D\u7F6E\u5931\u8D25: ${t.message}`)}}async function C(t){const n=Date.now(),i=await(0,S.getQBotClawVersion)()??"unknown";try{b();const l=(0,w.readState)();let a;l.gatewayPort&&!await(0,w.isPortAvailable)(l.gatewayPort)?(a=l.gatewayPort,(0,o.log)(`\u68C0\u6D4B\u5230 QBotClaw gateway \u5DF2\u5728\u7AEF\u53E3 ${a} \u8FD0\u884C\uFF0C\u590D\u7528\u539F\u7AEF\u53E3`)):a=await(0,S.findAvailablePort)(),(0,o.log)(`\u540E\u53F0\u542F\u52A8 QBotClaw gateway run --port ${a} --force`);const e=await(0,S.findAvailablePort)(),r=(0,A.getOrCreateGatewayToken)();await M(a,r,e,t),G(r);const u=(0,E.spawn)(s.QBOTCLAW_BIN,["gateway","run","--port",String(a),"--force"],{stdio:"ignore",shell:process.platform==="win32",windowsHide:!0,env:{...process.platform==="win32"?process.env:{PATH:s.NODE_BIN_DIR},HOME:process.env.HOME||process.env.USERPROFILE,USERPROFILE:process.env.USERPROFILE,OPENCLAW_NO_RESPAWN:"true",VENUS_API_KEY:"E9isWWfAr4VIFuIa5RbJaPxD@2040",OPENCLAW_TELEMETRY_GALIELO_GUID:process.env.QB_GUID||"",OPENCLAW_TELEMETRY_GALIELO_QIEMI36:process.env.QB_QIMEI36||"",QBOTCLAW_LLM_BASE_URL:`http://127.0.0.1:${l.port}/v1`,QBOTCLAW_LLM_API_KEY:N.proxyApiKey}});u.unref();const _=u.pid;(0,o.log)(`QBotClaw gateway \u5DF2\u5728\u540E\u53F0\u542F\u52A8\uFF0CPID: ${_}\uFF0C\u7AEF\u53E3: ${a}, token ${(0,A.getOrCreateGatewayToken)()}`),(0,w.writeState)({...(0,w.readState)(),gatewayPid:_,gatewayPort:a,mcpWsPort:e}),(0,p.report)({event_name:"qbot_claw_started",version:i,result:"1",cost_ms:String(Date.now()-n)})}catch(l){throw(0,p.report)({event_name:"qbot_claw_started",version:i,result:"0",cost_ms:String(Date.now()-n),error:l.message}),l}}async function W(){const t=g.default.join(s.QBOT_CLAW_DIR,".sandbox-acl-ready");if(c.default.existsSync(t))return;(0,o.log)("\u7B2C\u4E00\u6B21\u542F\u52A8\uFF0C\u6B63\u5728\u521D\u59CB\u5316\u6C99\u7BB1\u73AF\u5883\uFF08\u540E\u53F0\u68C0\u6D4B\uFF09..."),exports.sandboxState={initializing:!0,message:"\u7F51\u5173\u5DF2\u542F\u52A8\uFF0C\u6B63\u5728\u521D\u59CB\u5316\u6C99\u7BB1\u73AF\u5883...",elapsedSeconds:0};const n=300*1e3,i=1e3,l=Date.now();for(;!c.default.existsSync(t);){await new Promise(u=>setTimeout(u,i));const e=Date.now()-l,r=Math.floor(e/1e3);if((r%10===0||r<=3)&&(0,o.log)(`\u6C99\u7BB1\u73AF\u5883\u521D\u59CB\u5316\u4E2D... \u5DF2\u7B49\u5F85 ${r} \u79D2`),exports.sandboxState={initializing:!0,message:`\u6C99\u7BB1\u73AF\u5883\u521D\u59CB\u5316\u4E2D... \u5DF2\u7B49\u5F85 ${r} \u79D2`,elapsedSeconds:r},e>=n){(0,o.log)("\u6C99\u7BB1\u521D\u59CB\u5316\u8D85\u65F6\uFF085\u5206\u949F\uFF09"),exports.sandboxState={initializing:!1,message:"\u6C99\u7BB1\u521D\u59CB\u5316\u8D85\u65F6",elapsedSeconds:r};return}}const a=Math.floor((Date.now()-l)/1e3);exports.sandboxState={initializing:!1,message:"\u6C99\u7BB1\u73AF\u5883\u521D\u59CB\u5316\u5B8C\u6210",elapsedSeconds:a},(0,o.log)(`\u6C99\u7BB1\u73AF\u5883\u521D\u59CB\u5316\u5B8C\u6210\uFF0C\u8017\u65F6 ${a} \u79D2`)}function G(t){const n=g.default.join(s.QBOT_CLAW_DIR,".env"),i=`OPENCLAW_GATEWAY_TOKEN=${t}
6
+ `;try{if(c.default.existsSync(n)){const e=c.default.readFileSync(n,"utf-8").split(`
7
7
  `).filter(r=>!r.startsWith("OPENCLAW_GATEWAY_TOKEN=")).filter(r=>r!=="").join(`
8
8
  `)+`
9
- `+i;c.default.writeFileSync(e,n,"utf-8")}else c.default.writeFileSync(e,i,"utf-8");(0,s.log)(`\u5DF2\u5C06 OPENCLAW_GATEWAY_TOKEN \u5199\u5165 ${e}`)}catch(l){(0,s.log)(`\u5199\u5165 .env \u6587\u4EF6\u5931\u8D25: ${l.message}`)}}function b(t,e,i){if(exports.isInstalling){(0,s.log)("[runInstall] \u5B89\u88C5\u6B63\u5728\u8FDB\u884C\u4E2D\uFF0C\u5C06\u65B0\u8BF7\u6C42\u52A0\u5165\u8FDB\u5EA6\u76D1\u542C"),I.push(t),N(t,JSON.stringify({status:"downloading",progress:0,message:"\u5B89\u88C5\u6B63\u5728\u8FDB\u884C\u4E2D\uFF0C\u5DF2\u52A0\u5165\u8FDB\u5EA6\u76D1\u542C..."})+`
10
- `);return}exports.isInstalling=!0,I.push(t),exports.installState={status:"downloading",message:"\u6B63\u5728\u5B89\u88C5...",startTime:new Date().toISOString()},(0,s.log)(`\u5F00\u59CB\u5B89\u88C5 ${a.NPM_PACKAGE_NAME}@${e}`),(0,f.report)({event_name:"qbot_claw_install_start"});const l=Date.now();(async()=>{try{(0,w.stopGateway)(),Q(),h();const o=4;let n=0;d(t,o,n,"\u5F00\u59CB\u5B89\u88C5 qqbrowser-skill..."),await(0,B.installQQBrowserSkill)(_=>d(t,o,n,_)),n++,d(t,o,n,"\u5F00\u59CB\u4E0B\u8F7D\u89E3\u538B QBotClaw...");const r=Date.now();try{await v(e),n++,d(t,o,n,"npmDownloadQBotClaw \u5B8C\u6210"),d(t,o,n,"\u5F00\u59CB npm rebuild..."),await D(e),n++,d(t,o,n,"npmRebuildQBotClaw \u5B8C\u6210");const _=Date.now()-r;(0,f.report)({event_name:"qbot_claw_npm_install",result:"1",cost_ms:String(_)})}catch(_){const P=Date.now()-r;throw(0,f.report)({event_name:"qbot_claw_npm_install",result:"0",cost_ms:String(P),error:_.message}),_}try{c.default.existsSync(a.OPENCLAW_BIN)||(process.platform==="win32"?(c.default.copyFileSync(a.QBOTCLAW_BIN,a.OPENCLAW_BIN),(0,s.log)(`\u5DF2\u590D\u5236\u6587\u4EF6: ${a.QBOTCLAW_BIN} -> ${a.OPENCLAW_BIN}`)):(c.default.symlinkSync(a.QBOTCLAW_BIN,a.OPENCLAW_BIN),(0,s.log)(`\u5DF2\u521B\u5EFA\u8F6F\u94FE\u63A5: ${a.OPENCLAW_BIN} -> ${a.QBOTCLAW_BIN}`)))}catch(_){(0,s.log)(`\u521B\u5EFA openclaw \u94FE\u63A5/\u590D\u5236\u5931\u8D25: ${_.message}`)}exports.installState={status:"success",message:"\u5B89\u88C5\u6210\u529F",endTime:new Date().toISOString()},(0,s.log)("\u5B89\u88C5\u6210\u529F"),(0,f.report)({event_name:"qbot_claw_install_done",version:e,result:"1",cost_ms:String(Date.now()-l)}),(0,s.refreshClawVersion)(e),d(t,o,n,"\u5F00\u59CB\u914D\u7F6E\u5E76\u542F\u52A8 QBotClaw gateway..."),await C(i),n++;const p=(0,O.getOrCreateGatewayToken)(),u=JSON.stringify({status:"done",progress:100,message:"\u5B89\u88C5\u6210\u529F",gatewayToken:p});if(t.writable)try{t.end(u+`
11
- `)}catch{}S(u)}catch(o){exports.installState={status:"failed",message:o.message,endTime:new Date().toISOString()},(0,s.log)(`\u5B89\u88C5\u5931\u8D25: ${o.message}`),(0,f.report)({event_name:"qbot_claw_install_done",version:e,result:"0",cost_ms:String(Date.now()-l),error:o.message});const n=JSON.stringify({status:"failed",progress:0,message:o.message});if(t&&t.writable)try{t.end(n+`
12
- `)}catch{}S(n)}finally{exports.isInstalling=!1}})()}
9
+ `+i;c.default.writeFileSync(n,e,"utf-8")}else c.default.writeFileSync(n,i,"utf-8");(0,o.log)(`\u5DF2\u5C06 OPENCLAW_GATEWAY_TOKEN \u5199\u5165 ${n}`)}catch(l){(0,o.log)(`\u5199\u5165 .env \u6587\u4EF6\u5931\u8D25: ${l.message}`)}}function x(t,n,i){if(exports.isInstalling){(0,o.log)("[runInstall] \u5B89\u88C5\u6B63\u5728\u8FDB\u884C\u4E2D\uFF0C\u5C06\u65B0\u8BF7\u6C42\u52A0\u5165\u8FDB\u5EA6\u76D1\u542C"),I.push(t),O(t,JSON.stringify({status:"downloading",progress:0,message:"\u5B89\u88C5\u6B63\u5728\u8FDB\u884C\u4E2D\uFF0C\u5DF2\u52A0\u5165\u8FDB\u5EA6\u76D1\u542C..."})+`
10
+ `);return}exports.isInstalling=!0,I.push(t),exports.installState={status:"downloading",message:"\u6B63\u5728\u5B89\u88C5...",startTime:new Date().toISOString()},(0,o.log)(`\u5F00\u59CB\u5B89\u88C5 ${s.NPM_PACKAGE_NAME}@${n}`),(0,p.report)({event_name:"qbot_claw_install_start"});const l=Date.now();(async()=>{try{(0,w.stopGateway)(),h(),Q();const a=4;let e=0;f(t,a,e,"\u5F00\u59CB\u5B89\u88C5 qqbrowser-skill..."),await(0,T.installQQBrowserSkill)(d=>f(t,a,e,d)),e++,f(t,a,e,"\u5F00\u59CB\u4E0B\u8F7D\u89E3\u538B QBotClaw...");const r=Date.now();try{await v(n),e++,f(t,a,e,"npmDownloadQBotClaw \u5B8C\u6210"),f(t,a,e,"\u5F00\u59CB npm rebuild..."),await D(n),e++,f(t,a,e,"npmRebuildQBotClaw \u5B8C\u6210");const d=Date.now()-r;(0,p.report)({event_name:"qbot_claw_npm_install",result:"1",cost_ms:String(d)})}catch(d){const P=Date.now()-r;throw(0,p.report)({event_name:"qbot_claw_npm_install",result:"0",cost_ms:String(P),error:d.message}),d}try{c.default.existsSync(s.OPENCLAW_BIN)||(process.platform==="win32"?(c.default.copyFileSync(s.QBOTCLAW_BIN,s.OPENCLAW_BIN),(0,o.log)(`\u5DF2\u590D\u5236\u6587\u4EF6: ${s.QBOTCLAW_BIN} -> ${s.OPENCLAW_BIN}`)):(c.default.symlinkSync(s.QBOTCLAW_BIN,s.OPENCLAW_BIN),(0,o.log)(`\u5DF2\u521B\u5EFA\u8F6F\u94FE\u63A5: ${s.OPENCLAW_BIN} -> ${s.QBOTCLAW_BIN}`)))}catch(d){(0,o.log)(`\u521B\u5EFA openclaw \u94FE\u63A5/\u590D\u5236\u5931\u8D25: ${d.message}`)}exports.installState={status:"success",message:"\u5B89\u88C5\u6210\u529F",endTime:new Date().toISOString()},(0,o.log)("\u5B89\u88C5\u6210\u529F"),(0,p.report)({event_name:"qbot_claw_install_done",version:n,result:"1",cost_ms:String(Date.now()-l)}),(0,o.refreshClawVersion)(n),f(t,a,e,"\u5F00\u59CB\u914D\u7F6E\u5E76\u542F\u52A8 QBotClaw gateway..."),await C(i),e++;const u=(0,A.getOrCreateGatewayToken)(),_=JSON.stringify({status:"done",progress:100,message:"\u5B89\u88C5\u6210\u529F",gatewayToken:u});if(t.writable)try{t.end(_+`
11
+ `)}catch{}$(_)}catch(a){exports.installState={status:"failed",message:a.message,endTime:new Date().toISOString()},(0,o.log)(`\u5B89\u88C5\u5931\u8D25: ${a.message}`),(0,p.report)({event_name:"qbot_claw_install_done",version:n,result:"0",cost_ms:String(Date.now()-l),error:a.message});const e=JSON.stringify({status:"failed",progress:0,message:a.message});if(t&&t.writable)try{t.end(e+`
12
+ `)}catch{}$(e)}finally{exports.isInstalling=!1}})()}
@@ -1,18 +1,20 @@
1
- "use strict";var J=exports&&exports.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.proxyApiKey=void 0,exports.handleOpenAIProxy=Se;const G=J(require("crypto")),fe=J(require("http")),de=J(require("https")),te=require("url"),o=require("./reportLog"),oe=require("./api/llm-access-proxy"),ge=require("./api/auth-client"),w=require("./api/content-review");exports.proxyApiKey=G.default.randomBytes(32).toString("base64url");const he={enabled:!0,targetBaseUrl:"https://qbqaproxy.imtt.qq.com/openapi",allowedApiKeys:[exports.proxyApiKey],timeout:12e4},p={INVALID_REQUEST:"invalid_request_error",AUTH:"authentication_error",SERVER:"server_error",CONTENT_FILTER:"content_filter",INSUFFICIENT_QUOTA:"insufficient_quota",UPSTREAM:"upstream_error"},u={NOT_FOUND:"not_found",MISSING_API_KEY:"missing_api_key",INVALID_API_KEY:"invalid_api_key",BAD_REQUEST:"bad_request",AUTH_EXPIRED:"auth_expired",TOKEN_FETCH_ERROR:"token_fetch_error",PROXY_ERROR:"proxy_error",GATEWAY_TIMEOUT:"gateway_timeout",CONTENT_REVIEW_FAILED_INPUT:"content_review_failed_input",CONTENT_REVIEW_FAILED_OUTPUT:"content_review_failed_output",CONTENT_REVIEW_FAILED_OUTPUT_PARTIAL:"content_review_failed_output_partial",TOKEN_EXHAUSTED:"token_exhausted",UPSTREAM_UNAVAILABLE:"upstream_unavailable"};function ye(r){const e=r.headers.authorization;if(!e||typeof e!="string")return null;const i=e.match(/^Bearer\s+(.+)$/i);return i?i[1]:null}function _(r,e,i,l,t,h){const y=JSON.stringify({error:{message:i,type:l,code:t,...h||{}}});r.writeHead(e,{"Content-Type":"application/json","Content-Length":Buffer.byteLength(y)}),r.end(y)}function Ee(r,e,i="QBotClawRouter"){const l=Math.floor(Date.now()/1e3),t=`chatcmpl-${G.default.randomBytes(12).toString("hex")}`,h=JSON.stringify({id:t,object:"chat.completion",created:l,model:i,choices:[{index:0,message:{role:"assistant",content:e},finish_reason:"stop"}],usage:{prompt_tokens:0,completion_tokens:0,total_tokens:0}});r.writeHead(200,{"Content-Type":"application/json","Content-Length":Buffer.byteLength(h)}),r.end(h)}function _e(r,e,i="QBotClawRouter"){if(!r.writable)return;const l=Math.floor(Date.now()/1e3),t=`chatcmpl-${G.default.randomBytes(12).toString("hex")}`;r.write(`data: ${JSON.stringify({id:t,object:"chat.completion.chunk",created:l,model:i,choices:[{index:0,delta:{role:"assistant",content:e},finish_reason:null}]})}
1
+ "use strict";var H=exports&&exports.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.proxyApiKey=void 0,exports.handleOpenAIProxy=fe;const K=H(require("crypto")),ae=H(require("http")),se=H(require("https")),z=require("url"),o=require("./reportLog"),V=require("./api/llm-access-proxy"),ie=require("./api/auth-client"),S=require("./api/content-review");exports.proxyApiKey=K.default.randomBytes(32).toString("base64url");const le={enabled:!0,targetBaseUrl:"https://qbqaproxy.imtt.qq.com/openapi",allowedApiKeys:[exports.proxyApiKey],timeout:12e4};function ce(r){const e=r.headers.authorization;if(!e||typeof e!="string")return null;const l=e.match(/^Bearer\s+(.+)$/i);return l?l[1]:null}function _(r,e,l,c,t){const m=JSON.stringify({error:{message:l,type:c,code:t}});r.writeHead(e,{"Content-Type":"application/json","Content-Length":Buffer.byteLength(m)}),r.end(m)}function O(r,e,l="QBotClawRouter"){const c=Math.floor(Date.now()/1e3),t=`chatcmpl-${K.default.randomBytes(12).toString("hex")}`,m=JSON.stringify({id:t,object:"chat.completion",created:c,model:l,choices:[{index:0,message:{role:"assistant",content:e},finish_reason:"stop"}],usage:{prompt_tokens:0,completion_tokens:0,total_tokens:0}});r.writeHead(200,{"Content-Type":"application/json","Content-Length":Buffer.byteLength(m)}),r.end(m)}function B(r,e,l="QBotClawRouter"){if(!r.writable)return;const c=Math.floor(Date.now()/1e3),t=`chatcmpl-${K.default.randomBytes(12).toString("hex")}`,m=JSON.stringify({id:t,object:"chat.completion.chunk",created:c,model:l,choices:[{index:0,delta:{role:"assistant",content:e},finish_reason:null}]});r.write(`data: ${m}
2
2
 
3
- `),r.write(`data: ${JSON.stringify({id:t,object:"chat.completion.chunk",created:l,model:i,choices:[{index:0,delta:{},finish_reason:"stop"}]})}
3
+ `);const k=JSON.stringify({id:t,object:"chat.completion.chunk",created:c,model:l,choices:[{index:0,delta:{},finish_reason:"stop"}]});r.write(`data: ${k}
4
4
 
5
5
  `),r.write(`data: [DONE]
6
6
 
7
- `),r.end()}function Te(){return Math.random().toString(36).substring(2,10)}function ne(r){const e=r?.choices?.[0]?.delta;if(!e)return(0,o.log)(`[extractDeltaContent] delta \u4E3A\u7A7A, choices=${JSON.stringify(r?.choices)}`),{content:"",hasToolCalls:!1};const i=e.content,l=Array.isArray(e.tool_calls)&&e.tool_calls.length>0||e.function_call!=null;return{content:typeof i=="string"&&i.length>0?i:"",hasToolCalls:l}}function $e(r){try{const e=JSON.parse(r);return ne(e)}catch(e){return(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${e}`),{content:"",hasToolCalls:!1}}}function k(r,e,i,l,t){if(!r.writable)return;const h={message:e,type:i,code:l,...t||{}},y=JSON.stringify({error:h});r.write(`data: ${y}
7
+ `),r.end()}function pe(){return Math.random().toString(36).substring(2,10)}function X(r){const e=r?.choices?.[0]?.delta;if(!e)return(0,o.log)(`[extractDeltaContent] delta \u4E3A\u7A7A, choices=${JSON.stringify(r?.choices)}`),{content:"",hasToolCalls:!1};const l=e.content,c=Array.isArray(e.tool_calls)&&e.tool_calls.length>0||e.function_call!=null;return{content:typeof l=="string"&&l.length>0?l:"",hasToolCalls:c}}function ue(r){try{const e=JSON.parse(r);return X(e)}catch(e){return(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${e}`),{content:"",hasToolCalls:!1}}}function ge(r,e){const l=JSON.stringify({error:{message:e,type:"content_filter_error",code:"content_review_failed"}});r.writable&&(r.write(`data: ${l}
8
8
 
9
9
  `),r.write(`data: [DONE]
10
10
 
11
- `),r.end()}function re(r,e){const{isStream:i,httpStatus:l,message:t,type:h,code:y,extra:O}=e;if(!i){_(r,l,t,h,y,O);return}if(!r.headersSent){_(r,l,t,h,y,O);return}k(r,t,h,y,O)}function me(r){return new Promise((e,i)=>{const l=[];r.on("data",t=>l.push(t)),r.on("end",()=>e(Buffer.concat(l))),r.on("error",i)})}async function Se(r,e,i){const l=r.url||"";if(!l.startsWith("/v1/"))return!1;const t=Te(),h=r.method||"GET",y=Date.now();(0,o.log)(`[openai-proxy][${t}] \u6536\u5230\u8BF7\u6C42: ${h} ${l}`);const O=he;if(!O.enabled)return(0,o.log)(`[openai-proxy][${t}] \u4EE3\u7406\u672A\u542F\u7528\uFF0C\u8FD4\u56DE 404`),_(e,404,"Not Found",p.INVALID_REQUEST,u.NOT_FOUND),!0;const W=ye(r);if(!W)return(0,o.log)(`[openai-proxy][${t}] \u7F3A\u5C11 API Key`),_(e,401,"You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth.",p.INVALID_REQUEST,u.MISSING_API_KEY),!0;if(!O.allowedApiKeys.includes(W))return(0,o.log)(`[openai-proxy][${t}] API Key \u6821\u9A8C\u5931\u8D25`),_(e,401,"Incorrect API key provided. You can check your API key in the proxy configuration.",p.INVALID_REQUEST,u.INVALID_API_KEY),!0;let D;try{D=await me(r)}catch(n){return(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u8BF7\u6C42\u4F53\u5931\u8D25: ${n.message}`),_(e,400,"Failed to read request body",p.INVALID_REQUEST,u.BAD_REQUEST),!0}let x=!1,A=null;if(l.includes("/chat/completions")&&D.length>0)try{A=JSON.parse(D.toString("utf-8")),x=A.stream===!0,(0,o.log)(`[openai-proxy][${t}] stream=${A.stream}`)}catch{}let q=!1;if(Array.isArray(A?.messages)){const n=A.messages;let a=-1;for(let s=n.length-1;s>=0;s--)if(n[s].role==="user"){a=s;break}a>=0&&(q=n.slice(a+1).some(s=>s.role==="tool"))}if(q&&(0,o.log)(`[openai-proxy][${t}] \u6700\u540E\u4E00\u6761\u7528\u6237\u6D88\u606F\u4E4B\u540E\u5B58\u5728 tool \u6D88\u606F\uFF0C\u8DF3\u8FC7\u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF08\u5DE5\u5177\u8C03\u7528\u540E\u7EED\u8BF7\u6C42\uFF09`),A&&l.includes("/chat/completions")&&Array.isArray(A.messages)&&!q){const n=[];for(const a of A.messages)if(a.role==="user"){if(typeof a.content=="string"&&a.content.length>0)n.push(a.content);else if(Array.isArray(a.content))for(const s of a.content)s.type==="text"&&typeof s.text=="string"&&s.text.length>0&&n.push(s.text)}if(n.length>0){const a=n[n.length-1];try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
12
- ${a}`),await(0,w.reviewText)(a,w.AuditContentType.INPUT,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(s){if(s instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${s.fallbackMsg}`);const B=s.fallbackMsg||"\u8F93\u5165\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";return re(e,{isStream:x,httpStatus:400,message:B,type:p.CONTENT_FILTER,code:u.CONTENT_REVIEW_FAILED_INPUT,extra:{phase_action:s.phase_action,is_show:s.is_show}}),!0}(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${s.message||JSON.stringify(s)}`)}}}const Q=new te.URL(O.targetBaseUrl),ae=Q.pathname.replace(/\/+$/,""),se=l.replace(/^\/v1/,""),C=new te.URL(ae+se,Q.origin),X=C.protocol==="https:",ie=X?de.default:fe.default,le=X?443:80,L={};for(const[n,a]of Object.entries(r.headers)){if(!a)continue;const s=n.toLowerCase();["host","connection","keep-alive","transfer-encoding","upgrade"].includes(s)||(L[n]=a)}L.host=C.host;let H;try{H=await(0,oe.fetchLlmToken)(i),(0,o.log)(`[openai-proxy][${t}] \u6210\u529F\u83B7\u53D6 LLM Token ${H}`)}catch(n){if(n instanceof oe.TokenExhaustedError){(0,o.log)(`[openai-proxy][${t}] Token \u5DF2\u8017\u5C3D: ${n.message}`);const s=n.message||"\u5F53\u65E5Token\u5DF2\u8017\u5C3D\uFF0C\u8BF7\u5207\u6362\u6A21\u578B\u6216\u660E\u65E5\u518D\u8BD5";return re(e,{isStream:x,httpStatus:429,message:s,type:p.INSUFFICIENT_QUOTA,code:u.TOKEN_EXHAUSTED}),!0}if(n instanceof ge.AuthExpiredError)return(0,o.log)(`[openai-proxy][${t}] \u7968\u636E\u8FC7\u671F: ${n.message}`),_(e,401,`Auth token expired: ${n.message}`,p.AUTH,u.AUTH_EXPIRED),!0;const a=n instanceof Error?n.message:JSON.stringify(n);return(0,o.log)(`[openai-proxy][${t}] \u83B7\u53D6 LLM Token \u5931\u8D25: ${a}`),_(e,500,`Failed to obtain auth token: ${a}`,p.SERVER,u.TOKEN_FETCH_ERROR),!0}L.authorization=`Bearer ${H}`;const ce={hostname:C.hostname,port:C.port||le,path:C.pathname+C.search,method:h,headers:L,timeout:O.timeout||12e4};(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u5230: ${h} ${C.href}`);try{const n=D.toString("utf-8");(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u4F53: ${n.length>500?n.substring(0,500)+`...(truncated, total ${n.length} chars)`:n}`)}catch{}const v=ie.request(ce,n=>{const a=n.statusCode||502,s=n.headers["content-type"]||"",B=s.includes("text/event-stream")||x,pe=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u76EE\u6807\u54CD\u5E94: ${a} ${s} (${pe}ms)${B?" [SSE\u6D41\u5F0F]":""} (isStreamRequest=${x})`);const K={};for(const[T,E]of Object.entries(n.headers)){if(!E)continue;const f=T.toLowerCase();["connection","keep-alive","upgrade"].includes(f)||(K[T]=E)}const j=x&&!s.includes("text/event-stream")&&a>=400;if(j&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u975ESSE\u9519\u8BEF\u54CD\u5E94 (status=${a}, content-type=${s})\uFF0C\u964D\u7EA7\u4E3A\u975E\u6D41\u5F0F\u5904\u7406`),B&&!j){e.writeHead(a,K);let T="",E="",f="",$=!1,d=!1,M=!1;const g=100;let R=[],m=!1,N=!1,z=Promise.resolve();const Y=()=>{if(!(!e.writable||$)){for(const c of R)e.write(c);R.length>0&&(m=!0),R=[]}},Z=async(c,U)=>{if(!c||$)return;const b=w.AuditContentType.OUTPUT;try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838${U?"(\u6700\u7EC8)":""}\uFF0C\u6587\u672C\u957F\u5EA6: ${c.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
13
- ${c}`),await(0,w.reviewText)(c,b,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7\uFF0Cflush ${R.length} \u4E2A\u7F13\u5B58chunk`),Y()}catch(S){if(S instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${S.fallbackMsg}`),$=!0,R=[];const I=S.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";m?((0,o.log)(`[openai-proxy][${t}] [SSE] \u5DF2\u6709\u5185\u5BB9\u88ABflush\uFF0C\u8FFD\u52A0\u6807\u51C6 error`),k(e,I,p.CONTENT_FILTER,u.CONTENT_REVIEW_FAILED_OUTPUT_PARTIAL,{phase_action:S.phase_action,is_show:S.is_show})):((0,o.log)(`[openai-proxy][${t}] [SSE] \u5C1A\u672Aflush\u4EFB\u4F55\u5185\u5BB9\uFF0C\u8FD4\u56DE\u6807\u51C6 error`),k(e,I,p.CONTENT_FILTER,u.CONTENT_REVIEW_FAILED_OUTPUT,{phase_action:S.phase_action,is_show:S.is_show})),n.destroy();return}(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF0C\u76F4\u63A5flush\uFF09: ${S.message||JSON.stringify(S)}`),Y()}};n.on("data",c=>{if($){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA1\u6838\u5DF2\u4E2D\u65AD\uFF0C\u8DF3\u8FC7chunk (${c.length}\u5B57\u8282)`);return}if(!e.writable){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA2\u6237\u7AEF\u8FDE\u63A5\u5DF2\u5173\u95ED(res.writable=false)\uFF0C\u8DF3\u8FC7chunk (${c.length}\u5B57\u8282)`);return}const U=c.toString("utf-8");f+=U,f=f.replace(/\r\n/g,`
11
+ `),r.end())}function de(r){return new Promise((e,l)=>{const c=[];r.on("data",t=>c.push(t)),r.on("end",()=>e(Buffer.concat(c))),r.on("error",l)})}async function fe(r,e,l){const c=r.url||"";if(!c.startsWith("/v1/"))return!1;const t=pe(),m=r.method||"GET",k=Date.now();(0,o.log)(`[openai-proxy][${t}] \u6536\u5230\u8BF7\u6C42: ${m} ${c}`);const q=le;if(!q.enabled)return(0,o.log)(`[openai-proxy][${t}] \u4EE3\u7406\u672A\u542F\u7528\uFF0C\u8FD4\u56DE 404`),_(e,404,"Not Found","invalid_request_error","not_found"),!0;const Q=ce(r);if(!Q)return(0,o.log)(`[openai-proxy][${t}] \u7F3A\u5C11 API Key`),_(e,401,"You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth.","invalid_request_error","missing_api_key"),!0;if(!q.allowedApiKeys.includes(Q))return(0,o.log)(`[openai-proxy][${t}] API Key \u6821\u9A8C\u5931\u8D25`),_(e,401,"Incorrect API key provided. You can check your API key in the proxy configuration.","invalid_request_error","invalid_api_key"),!0;let A;try{A=await de(r)}catch(n){return(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u8BF7\u6C42\u4F53\u5931\u8D25: ${n.message}`),_(e,400,"Failed to read request body","invalid_request_error","bad_request"),!0}let v=!1,d=null;if(c.includes("/chat/completions")&&A.length>0)try{d=JSON.parse(A.toString("utf-8")),v=d.stream===!0,(0,o.log)(`[openai-proxy][${t}] stream=${d.stream}`)}catch{}let L=!1;if(Array.isArray(d?.messages)){const n=d.messages;let a=-1;for(let i=n.length-1;i>=0;i--)if(n[i].role==="user"){a=i;break}a>=0&&(L=n.slice(a+1).some(i=>i.role==="tool"))}if(L&&(0,o.log)(`[openai-proxy][${t}] \u6700\u540E\u4E00\u6761\u7528\u6237\u6D88\u606F\u4E4B\u540E\u5B58\u5728 tool \u6D88\u606F\uFF0C\u8DF3\u8FC7\u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF08\u5DE5\u5177\u8C03\u7528\u540E\u7EED\u8BF7\u6C42\uFF09`),d&&c.includes("/chat/completions")&&Array.isArray(d.messages)&&!L){const n=[];for(const a of d.messages)if(a.role==="user"){if(typeof a.content=="string"&&a.content.length>0)n.push(a.content);else if(Array.isArray(a.content))for(const i of a.content)i.type==="text"&&typeof i.text=="string"&&i.text.length>0&&n.push(i.text)}if(n.length>0){const a=n[n.length-1];try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
12
+ ${a}`),await(0,S.reviewText)(a,S.AuditContentType.INPUT,l),(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(i){if(i instanceof S.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${i.fallbackMsg}`);const C=d?.model||"QBotClawRouter",I=i.fallbackMsg||"\u8F93\u5165\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";return v?(e.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),B(e,I,C)):O(e,I,C),!0}(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${i.message||JSON.stringify(i)}`)}}}const j=new z.URL(q.targetBaseUrl),Z=j.pathname.replace(/\/+$/,""),ee=c.replace(/^\/v1/,""),T=new z.URL(Z+ee,j.origin),F=T.protocol==="https:",te=F?se.default:ae.default,oe=F?443:80,N={};for(const[n,a]of Object.entries(r.headers)){if(!a)continue;const i=n.toLowerCase();["host","connection","keep-alive","transfer-encoding","upgrade"].includes(i)||(N[n]=a)}N.host=T.host;let R;try{R=await(0,V.fetchLlmToken)(l),(0,o.log)(`[openai-proxy][${t}] \u6210\u529F\u83B7\u53D6 LLM Token ${R}`)}catch(n){if(n instanceof V.TokenExhaustedError){(0,o.log)(`[openai-proxy][${t}] Token \u5DF2\u8017\u5C3D: ${n.message}`);const i=d?.model||"QBotClawRouter",C=n.message||"\u5F53\u65E5Token\u5DF2\u8017\u5C3D\uFF0C\u8BF7\u5207\u6362\u6A21\u578B\u6216\u660E\u65E5\u518D\u8BD5";return v?(e.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),B(e,C,i)):O(e,C,i),!0}if(n instanceof ie.AuthExpiredError)return(0,o.log)(`[openai-proxy][${t}] \u7968\u636E\u8FC7\u671F: ${n.message}`),_(e,401,`Auth token expired: ${n.message}`,"authentication_error","auth_expired"),!0;const a=n instanceof Error?n.message:JSON.stringify(n);return(0,o.log)(`[openai-proxy][${t}] \u83B7\u53D6 LLM Token \u5931\u8D25: ${a}`),_(e,500,`Failed to obtain auth token: ${a}`,"server_error","token_fetch_error"),!0}N.authorization=`Bearer ${R}`;const ne={hostname:T.hostname,port:T.port||oe,path:T.pathname+T.search,method:m,headers:N,timeout:q.timeout||12e4};(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u5230: ${m} ${T.href}`);try{const n=A.toString("utf-8");(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u4F53: ${n.length>500?n.substring(0,500)+`...(truncated, total ${n.length} chars)`:n}`)}catch{}const D=te.request(ne,n=>{const a=n.statusCode||502,i=n.headers["content-type"]||"",C=i.includes("text/event-stream")||v,I=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u76EE\u6807\u54CD\u5E94: ${a} ${i} (${I}ms)${C?" [SSE\u6D41\u5F0F]":""} (isStreamRequest=${v})`);const U={};for(const[y,g]of Object.entries(n.headers)){if(!g)continue;const p=y.toLowerCase();["connection","keep-alive","upgrade"].includes(p)||(U[y]=g)}const G=v&&!i.includes("text/event-stream")&&a>=400;if(G&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u975ESSE\u9519\u8BEF\u54CD\u5E94 (status=${a}, content-type=${i})\uFF0C\u964D\u7EA7\u4E3A\u975E\u6D41\u5F0F\u5904\u7406`),C&&!G){e.writeHead(a,U);let y="",g="",p="",x=!1,u=!1,M=!1;const f=100,w=async(s,$)=>{if(!s||x)return;const E=S.AuditContentType.OUTPUT;try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838${$?"(\u6700\u7EC8)":""}\uFF0C\u6587\u672C\u957F\u5EA6: ${s.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
13
+ ${s}`),await(0,S.reviewText)(s,E,l),(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(h){if(h instanceof S.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${h.fallbackMsg}`),x=!0;const W=h.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";B(e,`
14
+
15
+ [${W}]`,d?.model||"QBotClawRouter"),n.destroy();return}(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${h.message||JSON.stringify(h)}`)}};n.on("data",s=>{if(x){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA1\u6838\u5DF2\u4E2D\u65AD\uFF0C\u8DF3\u8FC7chunk (${s.length}\u5B57\u8282)`);return}if(!e.writable){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA2\u6237\u7AEF\u8FDE\u63A5\u5DF2\u5173\u95ED(res.writable=false)\uFF0C\u8DF3\u8FC7chunk (${s.length}\u5B57\u8282)`);return}const $=s.toString("utf-8");p+=$,p=p.replace(/\r\n/g,`
14
16
  `).replace(/\r/g,`
15
- `);const b=f.split(`
16
- `);f=b.pop()||"";let S=!1;for(let I=0;I<b.length;I++){const F=b[I].trim();if(F){if(F==="data: [DONE]")M=!0;else if(F.startsWith("data: ")){const ee=F.slice(6);try{const P=JSON.parse(ee);if(P?.type==="upstream_error"||P?.error?.type==="upstream_error"){const ue=P.reqid||P.error?.reqid;(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${ue})\uFF0C\u539F\u59CB\u56DE\u5305: ${ee}\uFF0C\u8FD4\u56DE\u6807\u51C6 error`),$=!0,R=[],k(e,"\u5F53\u524D\u4F7F\u7528\u4EBA\u6570\u8F83\u591A\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",p.UPSTREAM,u.UPSTREAM_UNAVAILABLE),n.destroy(),S=!0;break}const V=ne(P);V.content&&(T+=V.content,E+=V.content),V.hasToolCalls&&(d=!0)}catch(P){(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${P}`)}}}}if(!S&&(M&&E.length===0&&!d?(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230 [DONE] \u4E14\u8F93\u51FA\u4E3A\u7A7A\uFF0C\u6682\u4E0D\u7F13\u5B58\uFF0C\u7B49\u5F85 end \u4E8B\u4EF6\u5904\u7406`):R.push(c),T.length>=g&&!N)){const I=T;T="",(0,o.log)(`[openai-proxy][${t}] [SSE] \u89E6\u53D1\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${I.length} ${I}`),N=!0,n.pause(),z=Z(I,!1).finally(()=>{N=!1,$||n.resume()})}}),n.on("end",async()=>{if(N&&((0,o.log)(`[openai-proxy][${t}] [SSE] end\u4E8B\u4EF6\u89E6\u53D1\uFF0C\u7B49\u5F85\u8FDB\u884C\u4E2D\u7684\u5BA1\u6838\u5B8C\u6210...`),await z),f.trim()){const c=f.trim();if(c.startsWith("data: ")&&c!=="data: [DONE]"){const U=$e(c.slice(6));U.content&&(T+=U.content,E+=U.content),U.hasToolCalls&&(d=!0)}}if(E.length===0?(0,o.log)(`[openai-proxy][${t}] [SSE] \u26A0\uFE0F \u6A21\u578B\u8F93\u51FA\u5185\u5BB9\u4E3A\u7A7A\uFF01\u8BF7\u68C0\u67E5\u4E0A\u65B9\u65E5\u5FD7\u4E2D\u7684\u539F\u59CBchunk\u6570\u636E`):(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u5185\u5BB9 (${E.length}\u5B57\u7B26):
17
- ${E}`),T.length>0&&!$&&await Z(T,!0),!$){if(Y(),E.length===0&&!d){(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4E14\u65E0tool_calls\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),_e(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",A?.model||"QBotClawRouter");return}E.length===0&&d&&(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4F46\u5305\u542Btool_calls\uFF08\u51FD\u6570\u8C03\u7528\u4E2D\u95F4\u8F6E\uFF09\uFF0C\u6B63\u5E38\u7ED3\u675F`);const c=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${c}ms`),e.end()}}),n.on("error",c=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${c.message}`),e.headersSent?$||k(e,`Bad Gateway: ${c.message}`,p.SERVER,u.PROXY_ERROR):_(e,502,`Bad Gateway: ${c.message}`,p.SERVER,u.PROXY_ERROR)})}else{const T=l.includes("/chat/completions"),E=[];n.on("data",f=>{E.push(f)}),n.on("end",async()=>{const f=Buffer.concat(E),$=f.toString("utf-8");a>=400&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u9519\u8BEF (status=${a}): ${$.substring(0,1e3)}`);let d=null;if(T)try{d=JSON.parse($)}catch{}if(d?.type==="upstream_error"||d?.error?.type==="upstream_error"){const g=d.reqid||d.error?.reqid;(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${g})\uFF0C\u539F\u59CB\u56DE\u5305: ${$.substring(0,2e3)}\uFF0C\u8FD4\u56DE\u6807\u51C6 error`),e.headersSent?e.end():_(e,503,"\u5F53\u524D\u4F7F\u7528\u4EBA\u6570\u8F83\u591A\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",p.UPSTREAM,u.UPSTREAM_UNAVAILABLE);return}if(T&&a>=200&&a<300&&d)try{const g=[];if(Array.isArray(d.choices))for(const m of d.choices){const N=m.message?.content;typeof N=="string"&&N.length>0&&g.push(N)}const R=Array.isArray(d.choices)&&d.choices.some(m=>Array.isArray(m.message?.tool_calls)&&m.message.tool_calls.length>0||m.message?.function_call!=null);if(g.length===0&&!R){if((0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u26A0\uFE0F \u6A21\u578B\u8F93\u51FA\u5185\u5BB9\u4E3A\u7A7A\u4E14\u65E0tool_calls\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),e.headersSent)e.end();else{const m=A?.model||d?.model||"QBotClawRouter";Ee(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",m)}return}if(g.length===0&&R&&(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4F46\u5305\u542Btool_calls\uFF08\u51FD\u6570\u8C03\u7528\u4E2D\u95F4\u8F6E\uFF09\uFF0C\u6B63\u5E38\u900F\u4F20`),g.length>0){const m=g.join(`
18
- `);(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${m.length}`),await(0,w.reviewText)(m,w.AuditContentType.OUTPUT,i),(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}}catch(g){if(g instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${g.fallbackMsg}`),e.headersSent?e.end():_(e,400,g.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002",p.CONTENT_FILTER,u.CONTENT_REVIEW_FAILED_OUTPUT,{phase_action:g.phase_action,is_show:g.is_show});return}(0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${g.message||g}`)}e.headersSent||e.writeHead(a,K),e.end(f);const M=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${M}ms`)}),n.on("error",f=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${f.message}`),e.headersSent?e.end():_(e,502,`Bad Gateway: ${f.message}`,p.SERVER,u.PROXY_ERROR)})}});return v.on("error",n=>{const a=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u5931\u8D25 (${a}ms): ${n.message}`),e.headersSent?x?k(e,`Bad Gateway: ${n.message}`,p.SERVER,u.PROXY_ERROR):e.end():_(e,502,`Bad Gateway: ${n.message}`,p.SERVER,u.PROXY_ERROR)}),v.on("timeout",()=>{const n=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u8D85\u65F6 (${n}ms)`),v.destroy(),e.headersSent?x?k(e,"Gateway Timeout",p.SERVER,u.GATEWAY_TIMEOUT):e.end():_(e,504,"Gateway Timeout",p.SERVER,u.GATEWAY_TIMEOUT)}),v.end(D),!0}
17
+ `);const E=p.split(`
18
+ `);p=E.pop()||"";for(let h=0;h<E.length;h++){const P=E[h].trim();if(P){if(P==="data: [DONE]")M=!0;else if(P.startsWith("data: ")){const Y=P.slice(6);try{const b=JSON.parse(Y);if(b?.type==="upstream_error"||b?.error?.type==="upstream_error"){const re=b.reqid||b.error?.reqid;(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${re})\uFF0C\u539F\u59CB\u56DE\u5305: ${Y}\uFF0C\u8FD4\u56DE\u53CB\u597D\u63D0\u793A`),x=!0,B(e,"\u5F53\u524D\u4F7F\u7528\u4EBA\u6570\u8F83\u591A\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",d?.model||"QBotClawRouter"),n.destroy();return}const J=X(b);J.content&&(y+=J.content,g+=J.content),J.hasToolCalls&&(u=!0)}catch(b){(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${b}`)}}}}if(M&&g.length===0&&!u?(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230 [DONE] \u4E14\u8F93\u51FA\u4E3A\u7A7A\uFF0C\u6682\u4E0D\u8F6C\u53D1\uFF0C\u7B49\u5F85 end \u4E8B\u4EF6\u5904\u7406`):e.write(s),y.length>=f){const h=y;y="",(0,o.log)(`[openai-proxy][${t}] [SSE] \u89E6\u53D1\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${h.length} ${h}`),w(h,!1)}}),n.on("end",async()=>{if(p.trim()){const s=p.trim();if(s.startsWith("data: ")&&s!=="data: [DONE]"){const $=ue(s.slice(6));$.content&&(y+=$.content,g+=$.content),$.hasToolCalls&&(u=!0)}}if(g.length===0?(0,o.log)(`[openai-proxy][${t}] [SSE] \u26A0\uFE0F \u6A21\u578B\u8F93\u51FA\u5185\u5BB9\u4E3A\u7A7A\uFF01\u8BF7\u68C0\u67E5\u4E0A\u65B9\u65E5\u5FD7\u4E2D\u7684\u539F\u59CBchunk\u6570\u636E`):(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u5185\u5BB9 (${g.length}\u5B57\u7B26):
19
+ ${g}`),y.length>0&&!x&&await w(y,!0),!x){if(g.length===0&&!u){(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4E14\u65E0tool_calls\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),B(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",d?.model||"QBotClawRouter");return}g.length===0&&u&&(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4F46\u5305\u542Btool_calls\uFF08\u51FD\u6570\u8C03\u7528\u4E2D\u95F4\u8F6E\uFF09\uFF0C\u6B63\u5E38\u7ED3\u675F`);const s=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${s}ms`),e.end()}}),n.on("error",s=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${s.message}`),e.headersSent?x||e.end():_(e,502,`Bad Gateway: ${s.message}`,"server_error","proxy_error")})}else{const y=c.includes("/chat/completions"),g=[];n.on("data",p=>{g.push(p)}),n.on("end",async()=>{const p=Buffer.concat(g),x=p.toString("utf-8");a>=400&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u9519\u8BEF (status=${a}): ${x.substring(0,1e3)}`);let u=null;if(y)try{u=JSON.parse(x)}catch{}if(u?.type==="upstream_error"||u?.error?.type==="upstream_error"){const f=u.reqid||u.error?.reqid;if((0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${f})\uFF0C\u539F\u59CB\u56DE\u5305: ${x.substring(0,2e3)}\uFF0C\u8FD4\u56DE\u53CB\u597D\u63D0\u793A`),e.headersSent)e.end();else{const w=d?.model||"QBotClawRouter";O(e,"\u5F53\u524D\u4F7F\u7528\u4EBA\u6570\u8F83\u591A\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",w)}return}if(y&&a>=200&&a<300&&u)try{const f=[];if(Array.isArray(u.choices))for(const s of u.choices){const $=s.message?.content;typeof $=="string"&&$.length>0&&f.push($)}const w=Array.isArray(u.choices)&&u.choices.some(s=>Array.isArray(s.message?.tool_calls)&&s.message.tool_calls.length>0||s.message?.function_call!=null);if(f.length===0&&!w){if((0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u26A0\uFE0F \u6A21\u578B\u8F93\u51FA\u5185\u5BB9\u4E3A\u7A7A\u4E14\u65E0tool_calls\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),e.headersSent)e.end();else{const s=d?.model||u?.model||"QBotClawRouter";O(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",s)}return}if(f.length===0&&w&&(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4F46\u5305\u542Btool_calls\uFF08\u51FD\u6570\u8C03\u7528\u4E2D\u95F4\u8F6E\uFF09\uFF0C\u6B63\u5E38\u900F\u4F20`),f.length>0){const s=f.join(`
20
+ `);(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${s.length}`),await(0,S.reviewText)(s,S.AuditContentType.OUTPUT,l),(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}}catch(f){if(f instanceof S.ContentReviewFailedError){if((0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${f.fallbackMsg}`),e.headersSent)e.end();else{const w=d?.model||"QBotClawRouter";O(e,f.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002",w)}return}(0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${f.message||f}`)}e.headersSent||e.writeHead(a,U),e.end(p);const M=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${M}ms`)}),n.on("error",p=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${p.message}`),e.headersSent?e.end():_(e,502,`Bad Gateway: ${p.message}`,"server_error","proxy_error")})}});return D.on("error",n=>{const a=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u5931\u8D25 (${a}ms): ${n.message}`),e.headersSent?e.end():_(e,502,`Bad Gateway: ${n.message}`,"server_error","proxy_error")}),D.on("timeout",()=>{const n=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u8D85\u65F6 (${n}ms)`),D.destroy(),e.headersSent?e.end():_(e,504,"Gateway Timeout","server_error","gateway_timeout")}),D.end(A),!0}
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")),y=L(require("fs")),I=L(require("path")),f=require("./config"),n=require("./utils"),o=require("./reportLog"),T=require("./installer"),G=require("./build"),P=require("./state"),$=require("child_process"),Q=require("./service"),J=require("./filePicker"),x=require("./fileSecurityCheck"),b=require("./authMiddleware"),q=require("./qbotclaw-routes"),H="\u9009\u62E9\u6587\u4EF6",F=10485760,B=10,M=!1,S={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 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,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,T.configAndRunQBotClaw)(),(0,o.log)("[restart] QBotClaw gateway \u91CD\u542F\u5B8C\u6210"),(0,T.checkSandboxInitialization)().catch(e=>{(0,o.log)(`[restart] \u6C99\u7BB1\u73AF\u5883\u68C0\u6D4B\u5F02\u5E38\uFF08\u975E\u81F4\u547D\uFF09: ${e.message}`)});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:T.isInstalling});return}if(g==="/api/sandbox"){(0,n.sendJSON)(t,200,T.sandboxState);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,T.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,{...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,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,b.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:S.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=F,l=B,c=M;if(r){let e;try{e=JSON.parse(r)}catch{(0,n.sendJSON)(t,400,{ok:!1,code:S.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:S.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:S.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:S.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:S.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileSize \u5FC5\u987B\u662F\u6B63\u6570"});return}d=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:S.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:S.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:S.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=[],w=[];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}):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 A=w.map(h=>`${I.default.basename(h.path)}: ${h.reason}`).join("\uFF1B");_.push(`${w.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&&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,o.log)(`[file-picker] \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${e.message}`);let m=S.INTERNAL_ERROR;e.message.includes("\u8D85\u65F6")?m=S.DIALOG_TIMEOUT:e.message.includes("\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0")&&(m=S.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,b.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}),w=setTimeout(()=>{O.kill(),N(new Error("\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u8D85\u65F6\uFF085\u79D2\uFF09"))},5e3);O.on("close",_=>{clearTimeout(w),_===0||_===1?m():N(new Error(`\u8FDB\u7A0B\u9000\u51FA\u7801: ${_}`))}),O.on("error",_=>{clearTimeout(w),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,E=null;function W(u){if(!u)return;if(C&&E===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: ${E}\uFF09\uFF0C\u505C\u6B62\u65E7\u7684\u76D1\u63A7\uFF0C\u91CD\u65B0\u76D1\u63A7 ppid: ${u}`),clearInterval(C),C=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),C=null,E=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,E=u}
package/dist/service.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";var S=exports&&exports.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.readCurrentGatewayToken=w,exports.getOrCreateGatewayToken=v,exports.startService=b,exports.stopService=O;const f=S(require("fs")),k=require("crypto"),y=require("./server"),t=require("./state"),p=require("./config"),_=require("./build"),q=require("./installer"),T=require("./utils"),r=require("./reportLog"),o=require("./report");let d,s;const g="0526bec85002fe65ef10d33df0a8dfbe76917cd125f5a4ad";function w(){try{if(!f.default.existsSync(p.QBOT_CLAW_CONFIG_PATH))return(0,r.log)(`[service] \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728: ${p.QBOT_CLAW_CONFIG_PATH}`),g;const e=f.default.readFileSync(p.QBOT_CLAW_CONFIG_PATH,"utf-8");return JSON.parse(e)?.gateway?.auth?.token||g}catch(e){return(0,r.log)(`[service] \u8BFB\u53D6\u914D\u7F6E\u6587\u4EF6 gateway.auth.token \u5931\u8D25: ${e.message}`),g}}function v(){return s||(s=require("crypto").randomBytes(24).toString("hex"),(0,r.log)(`[service] \u751F\u6210\u65B0\u7684 gateway token: ${s}`),s||w())}async function $(){const e=(0,t.readState)();if(!e.pid||!e.port)return null;try{process.kill(e.pid,0)}catch{return(0,t.clearState)(),null}if(await(0,t.isPortAvailable)(e.port))return(0,r.log)("\u7AEF\u53E3\u672A\u88AB\u5360\u7528\uFF0C\u8BF4\u660E\u72B6\u6001\u6587\u4EF6\u8FC7\u671F"),(0,t.clearState)(),null;const i=e.launcherVersion??null;if((0,r.log)(`[service] \u68C0\u6D4B\u5230\u6B63\u5728\u8FD0\u884C\u7684\u670D\u52A1\u7248\u672C: ${i??"unknown"}\uFF0C\u5F53\u524D\u7248\u672C: ${_.LAUNCHER_VERSION}`),i===_.LAUNCHER_VERSION)return(0,r.log)(`[service] \u7248\u672C\u4E00\u81F4\uFF0C\u590D\u7528\u5DF2\u6709\u670D\u52A1 (pid: ${e.pid}, port: ${e.port})`),!1;(0,r.log)(`[service] \u7248\u672C\u4E0D\u4E00\u81F4 (\u8FD0\u884C\u4E2D: ${i??"unknown"}, \u5F53\u524D: ${_.LAUNCHER_VERSION})\uFF0C\u6740\u6389\u65E7\u8FDB\u7A0B pid: ${e.pid}\uFF0C\u91CD\u65B0\u542F\u52A8`);try{process.kill(e.pid,"SIGTERM")}catch{}return(0,t.clearState)(),!0}async function b(e,n){if(await $()===!1){const a=(0,t.readState)();process.stdout.write(JSON.stringify({state:"success",pid:a.pid,port:a.port,gatewayToken:w()})+`
2
- `),process.stdout.uncork(),(0,r.log)(`[service] \u7248\u672C\u4E00\u81F4\uFF0C\u590D\u7528\u5DF2\u6709\u670D\u52A1 (pid: ${a.pid}, port: ${a.port})`);return}const c=await(0,t.findAvailablePort)();y.server.listen(c,"127.0.0.1",async()=>{d=(0,k.randomUUID)(),(0,t.writeState)({pid:process.pid,port:c,token:d,launcherVersion:_.LAUNCHER_VERSION,...e?{ppid:e}:{}}),(0,y.watchParentProcess)(e),(0,o.report)({event_name:"qbot_claw_try_start"});let a;if(f.default.existsSync(p.QBOTCLAW_BIN))try{const l=await(0,T.getQBotClawVersion)(),u=(0,t.readState)();l!==n?((0,r.log)(`[service] qbotclaw \u672C\u5730\u7248\u672C (${l??"unknown"}) \u4E0E\u671F\u671B\u7248\u672C (${n}) \u4E0D\u4E00\u81F4\uFF0C\u505C\u6B62\u5F53\u524D qbotclaw \u8FDB\u7A0B`),(0,o.report)({event_name:"qbot_claw_need_update"}),(0,t.stopGateway)()):u.gatewayPid&&u.gatewayPort?((0,r.log)(`[service] qbotclaw \u5DF2\u5728\u8FD0\u884C\uFF0Cpid: ${u.gatewayPid}, port: ${u.gatewayPort}`),a=w(),(0,o.report)({event_name:"qbot_claw_version_match"}),(0,o.report)({event_name:"qbot_claw_reuse"})):((0,r.log)("[service] \u68C0\u6D4B\u5230 qbotclaw\uFF0C\u7248\u672C\u5339\u914D\uFF0C\u81EA\u52A8\u542F\u52A8 gateway..."),(0,o.report)({event_name:"qbot_claw_version_match"}),await(0,q.configAndRunQBotClaw)(),a=v())}catch(l){(0,r.log)(`[service] \u81EA\u52A8\u542F\u52A8 gateway \u5931\u8D25: ${l.message}`)}else(0,r.log)("[service] qbotclaw \u672A\u5B89\u88C5\uFF0C\u4E0D\u81EA\u52A8\u542F\u52A8"),(0,o.report)({event_name:"qbot_claw_need_install"});(0,r.log)(`[service] \u670D\u52A1\u542F\u52A8\u6210\u529F\uFF0Cpid: ${process.pid}, port: ${c}, token: ${d}`),process.stdout.write(JSON.stringify({state:"success",pid:process.pid,port:c,token:d,gatewayToken:a})+`
3
- `),process.stdout.uncork()}),process.on("SIGTERM",()=>{(0,t.stopGateway)(),(0,t.clearState)(),(0,r.flushLogs)().finally(()=>{process.exit(0)})})}function O(){(0,t.stopGateway)();const e=(0,t.readState)();if(!e.pid){(0,t.clearState)();return}try{process.kill(e.pid,"SIGTERM")}catch{}(0,t.clearState)()}
1
+ "use strict";var k=exports&&exports.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.readCurrentGatewayToken=w,exports.getOrCreateGatewayToken=S,exports.startService=T,exports.stopService=h;const f=k(require("fs")),q=require("crypto"),y=require("./server"),t=require("./state"),p=require("./config"),_=require("./build"),v=require("./installer"),$=require("./utils"),r=require("./reportLog"),n=require("./report");let d,i;const g="0526bec85002fe65ef10d33df0a8dfbe76917cd125f5a4ad";function w(){try{if(!f.default.existsSync(p.QBOT_CLAW_CONFIG_PATH))return(0,r.log)(`[service] \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728: ${p.QBOT_CLAW_CONFIG_PATH}`),g;const e=f.default.readFileSync(p.QBOT_CLAW_CONFIG_PATH,"utf-8");return JSON.parse(e)?.gateway?.auth?.token||g}catch(e){return(0,r.log)(`[service] \u8BFB\u53D6\u914D\u7F6E\u6587\u4EF6 gateway.auth.token \u5931\u8D25: ${e.message}`),g}}function S(){return i||(i=require("crypto").randomBytes(24).toString("hex"),(0,r.log)(`[service] \u751F\u6210\u65B0\u7684 gateway token: ${i}`),i||w())}async function b(){const e=(0,t.readState)();if(!e.pid||!e.port)return null;try{process.kill(e.pid,0)}catch{return(0,t.clearState)(),null}if(await(0,t.isPortAvailable)(e.port))return(0,r.log)("\u7AEF\u53E3\u672A\u88AB\u5360\u7528\uFF0C\u8BF4\u660E\u72B6\u6001\u6587\u4EF6\u8FC7\u671F"),(0,t.clearState)(),null;const c=e.launcherVersion??null;if((0,r.log)(`[service] \u68C0\u6D4B\u5230\u6B63\u5728\u8FD0\u884C\u7684\u670D\u52A1\u7248\u672C: ${c??"unknown"}\uFF0C\u5F53\u524D\u7248\u672C: ${_.LAUNCHER_VERSION}`),c===_.LAUNCHER_VERSION)return(0,r.log)(`[service] \u7248\u672C\u4E00\u81F4\uFF0C\u590D\u7528\u5DF2\u6709\u670D\u52A1 (pid: ${e.pid}, port: ${e.port})`),!1;(0,r.log)(`[service] \u7248\u672C\u4E0D\u4E00\u81F4 (\u8FD0\u884C\u4E2D: ${c??"unknown"}, \u5F53\u524D: ${_.LAUNCHER_VERSION})\uFF0C\u6740\u6389\u65E7\u8FDB\u7A0B pid: ${e.pid}\uFF0C\u91CD\u65B0\u542F\u52A8`);try{process.kill(e.pid,"SIGTERM")}catch{}return(0,t.clearState)(),!0}async function T(e,s){if(await b()===!1){const a=(0,t.readState)();process.stdout.write(JSON.stringify({state:"success",pid:a.pid,port:a.port,gatewayToken:w()})+`
2
+ `),process.stdout.uncork(),(0,r.log)(`[service] \u7248\u672C\u4E00\u81F4\uFF0C\u590D\u7528\u5DF2\u6709\u670D\u52A1 (pid: ${a.pid}, port: ${a.port})`);return}const l=await(0,t.findAvailablePort)();y.server.listen(l,"127.0.0.1",async()=>{d=(0,q.randomUUID)(),(0,t.writeState)({pid:process.pid,port:l,token:d,launcherVersion:_.LAUNCHER_VERSION,...e?{ppid:e}:{}}),(0,y.watchParentProcess)(e),(0,n.report)({event_name:"qbot_claw_try_start"});let a;if(f.default.existsSync(p.QBOTCLAW_BIN))try{const o=await(0,$.getQBotClawVersion)(),u=(0,t.readState)();o!==s?((0,r.log)(`[service] qbotclaw \u672C\u5730\u7248\u672C (${o??"unknown"}) \u4E0E\u671F\u671B\u7248\u672C (${s}) \u4E0D\u4E00\u81F4\uFF0C\u505C\u6B62\u5F53\u524D qbotclaw \u8FDB\u7A0B`),(0,n.report)({event_name:"qbot_claw_need_update"}),(0,t.stopGateway)()):u.gatewayPid&&u.gatewayPort?((0,r.log)(`[service] qbotclaw \u5DF2\u5728\u8FD0\u884C\uFF0Cpid: ${u.gatewayPid}, port: ${u.gatewayPort}`),a=w(),(0,n.report)({event_name:"qbot_claw_version_match"}),(0,n.report)({event_name:"qbot_claw_reuse"})):((0,r.log)("[service] \u68C0\u6D4B\u5230 qbotclaw\uFF0C\u7248\u672C\u5339\u914D\uFF0C\u81EA\u52A8\u542F\u52A8 gateway..."),(0,n.report)({event_name:"qbot_claw_version_match"}),await(0,v.configAndRunQBotClaw)(),a=S())}catch(o){(0,r.log)(`[service] \u81EA\u52A8\u542F\u52A8 gateway \u5931\u8D25: ${o.message}`)}else(0,r.log)("[service] qbotclaw \u672A\u5B89\u88C5\uFF0C\u4E0D\u81EA\u52A8\u542F\u52A8"),(0,n.report)({event_name:"qbot_claw_need_install"});(0,r.log)(`[service] \u670D\u52A1\u542F\u52A8\u6210\u529F\uFF0Cpid: ${process.pid}, port: ${l}, token: ${d}`),(0,v.checkSandboxInitialization)().catch(o=>{(0,r.log)(`[service] \u6C99\u7BB1\u73AF\u5883\u68C0\u6D4B\u5F02\u5E38\uFF08\u975E\u81F4\u547D\uFF09: ${o.message}`)}),process.stdout.write(JSON.stringify({state:"success",pid:process.pid,port:l,token:d,gatewayToken:a})+`
3
+ `),process.stdout.uncork()}),process.on("SIGTERM",()=>{(0,t.stopGateway)(),(0,t.clearState)(),(0,r.flushLogs)().finally(()=>{process.exit(0)})})}function h(){(0,t.stopGateway)();const e=(0,t.readState)();if(!e.pid){(0,t.clearState)();return}try{process.kill(e.pid,"SIGTERM")}catch{}(0,t.clearState)()}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qqbrowser/qbot-claw-launcher",
3
- "version": "0.10.11",
3
+ "version": "0.11.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "bin": {