@qqbrowser/qbot-claw-launcher 0.9.89 → 0.9.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/build.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IS_DEBUG_BUILD=exports.LAUNCHER_VERSION=void 0,exports.LAUNCHER_VERSION="0.9.89",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.9.91",exports.IS_DEBUG_BUILD=!1;
@@ -1,20 +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=pe;const U=J(require("crypto")),ne=J(require("http")),re=J(require("https")),W=require("url"),o=require("./reportLog"),Y=require("./api/llm-access-proxy"),ae=require("./api/auth-client"),_=require("./api/content-review");exports.proxyApiKey=U.default.randomBytes(32).toString("base64url");const se={enabled:!0,targetBaseUrl:"https://qbqaproxy.imtt.qq.com/openapi",allowedApiKeys:[exports.proxyApiKey],timeout:12e4};function ie(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 m(r,e,i,l,t){const $=JSON.stringify({error:{message:i,type:l,code:t}});r.writeHead(e,{"Content-Type":"application/json","Content-Length":Buffer.byteLength($)}),r.end($)}function M(r,e,i="QBotClawRouter"){const l=Math.floor(Date.now()/1e3),t=`chatcmpl-${U.default.randomBytes(12).toString("hex")}`,$=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($)}),r.end($)}function P(r,e,i="QBotClawRouter"){if(!r.writable)return;const l=Math.floor(Date.now()/1e3),t=`chatcmpl-${U.default.randomBytes(12).toString("hex")}`,$=JSON.stringify({id:t,object:"chat.completion.chunk",created:l,model:i,choices:[{index:0,delta:{role:"assistant",content:e},finish_reason:null}]});r.write(`data: ${$}
1
+ "use strict";var U=exports&&exports.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.proxyApiKey=void 0,exports.handleOpenAIProxy=de;const H=U(require("crypto")),re=U(require("http")),ae=U(require("https")),z=require("url"),o=require("./reportLog"),V=require("./api/llm-access-proxy"),se=require("./api/auth-client"),w=require("./api/content-review");exports.proxyApiKey=H.default.randomBytes(32).toString("base64url");const ie={enabled:!0,targetBaseUrl:"https://qbqaproxy.imtt.qq.com/openapi",allowedApiKeys:[exports.proxyApiKey],timeout:12e4};function le(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,c,t){const m=JSON.stringify({error:{message:i,type:c,code:t}});r.writeHead(e,{"Content-Type":"application/json","Content-Length":Buffer.byteLength(m)}),r.end(m)}function O(r,e,i="QBotClawRouter"){const c=Math.floor(Date.now()/1e3),t=`chatcmpl-${H.default.randomBytes(12).toString("hex")}`,m=JSON.stringify({id:t,object:"chat.completion",created:c,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(m)}),r.end(m)}function B(r,e,i="QBotClawRouter"){if(!r.writable)return;const c=Math.floor(Date.now()/1e3),t=`chatcmpl-${H.default.randomBytes(12).toString("hex")}`,m=JSON.stringify({id:t,object:"chat.completion.chunk",created:c,model:i,choices:[{index:0,delta:{role:"assistant",content:e},finish_reason:null}]});r.write(`data: ${m}
2
2
 
3
- `);const C=JSON.stringify({id:t,object:"chat.completion.chunk",created:l,model:i,choices:[{index:0,delta:{},finish_reason:"stop"}]});r.write(`data: ${C}
3
+ `);const C=JSON.stringify({id:t,object:"chat.completion.chunk",created:c,model:i,choices:[{index:0,delta:{},finish_reason:"stop"}]});r.write(`data: ${C}
4
4
 
5
5
  `),r.write(`data: [DONE]
6
6
 
7
- `),r.end()}function le(){return Math.random().toString(36).substring(2,10)}function z(r){try{const e=JSON.parse(r),i=e?.choices?.[0]?.delta;if(!i)return(0,o.log)(`[extractDeltaContent] delta \u4E3A\u7A7A, choices=${JSON.stringify(e?.choices)}`),{content:"",hasToolCalls:!1};const l=i.content,t=Array.isArray(i.tool_calls)&&i.tool_calls.length>0||i.function_call!=null;return{content:typeof l=="string"&&l.length>0?l:"",hasToolCalls:t}}catch(e){return(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${e}`),{content:"",hasToolCalls:!1}}}function ue(r,e){const i=JSON.stringify({error:{message:e,type:"content_filter_error",code:"content_review_failed"}});r.writable&&(r.write(`data: ${i}
7
+ `),r.end()}function ce(){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 i=e.content,c=Array.isArray(e.tool_calls)&&e.tool_calls.length>0||e.function_call!=null;return{content:typeof i=="string"&&i.length>0?i:"",hasToolCalls:c}}function pe(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 fe(r,e){const i=JSON.stringify({error:{message:e,type:"content_filter_error",code:"content_review_failed"}});r.writable&&(r.write(`data: ${i}
8
8
 
9
9
  `),r.write(`data: [DONE]
10
10
 
11
- `),r.end())}function ce(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 pe(r,e,i){const l=r.url||"";if(!l.startsWith("/v1/"))return!1;const t=le(),$=r.method||"GET",C=Date.now();(0,o.log)(`[openai-proxy][${t}] \u6536\u5230\u8BF7\u6C42: ${$} ${l}`);const O=se;if(!O.enabled)return(0,o.log)(`[openai-proxy][${t}] \u4EE3\u7406\u672A\u542F\u7528\uFF0C\u8FD4\u56DE 404`),m(e,404,"Not Found","invalid_request_error","not_found"),!0;const H=ie(r);if(!H)return(0,o.log)(`[openai-proxy][${t}] \u7F3A\u5C11 API Key`),m(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(!O.allowedApiKeys.includes(H))return(0,o.log)(`[openai-proxy][${t}] API Key \u6821\u9A8C\u5931\u8D25`),m(e,401,"Incorrect API key provided. You can check your API key in the proxy configuration.","invalid_request_error","invalid_api_key"),!0;let b;try{b=await ce(r)}catch(n){return(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u8BF7\u6C42\u4F53\u5931\u8D25: ${n.message}`),m(e,400,"Failed to read request body","invalid_request_error","bad_request"),!0}let v=!1,f=null;if(l.includes("/chat/completions")&&b.length>0)try{f=JSON.parse(b.toString("utf-8")),v=f.stream===!0,(0,o.log)(`[openai-proxy][${t}] stream=${f.stream}`)}catch{}const j=f?.messages?.some(n=>n.role==="tool");if(j&&(0,o.log)(`[openai-proxy][${t}] \u68C0\u6D4B\u5230 tool \u6D88\u606F\uFF0C\u8DF3\u8FC7\u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF08\u5DE5\u5177\u8C03\u7528\u540E\u7EED\u8BF7\u6C42\uFF09`),f&&l.includes("/chat/completions")&&Array.isArray(f.messages)&&!j){const n=[];for(const s of f.messages)if(s.role==="user"){if(typeof s.content=="string"&&s.content.length>0)n.push(s.content);else if(Array.isArray(s.content))for(const c of s.content)c.type==="text"&&typeof c.text=="string"&&c.text.length>0&&n.push(c.text)}if(n.length>0){const s=n[n.length-1];try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${s.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
12
- ${s}`),await(0,_.reviewText)(s,_.AuditContentType.INPUT,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(c){if(c instanceof _.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${c.fallbackMsg}`);const w=f?.model||"QBotClawRouter",D=c.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"}),P(e,D,w)):M(e,D,w),!0}(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${c.message||JSON.stringify(c)}`)}}}const K=new W.URL(O.targetBaseUrl),V=K.pathname.replace(/\/+$/,""),X=l.replace(/^\/v1/,""),k=new W.URL(V+X,K.origin),Q=k.protocol==="https:",Z=Q?re.default:ne.default,ee=Q?443:80,B={};for(const[n,s]of Object.entries(r.headers)){if(!s)continue;const c=n.toLowerCase();["host","connection","keep-alive","transfer-encoding","upgrade"].includes(c)||(B[n]=s)}B.host=k.host;let L;try{L=await(0,Y.fetchLlmToken)(i),(0,o.log)(`[openai-proxy][${t}] \u6210\u529F\u83B7\u53D6 LLM Token ${L}`)}catch(n){if(n instanceof Y.TokenExhaustedError){(0,o.log)(`[openai-proxy][${t}] Token \u5DF2\u8017\u5C3D: ${n.message}`);const c=f?.model||"QBotClawRouter",w=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"}),P(e,w,c)):M(e,w,c),!0}if(n instanceof ae.AuthExpiredError)return(0,o.log)(`[openai-proxy][${t}] \u7968\u636E\u8FC7\u671F: ${n.message}`),m(e,401,`Auth token expired: ${n.message}`,"authentication_error","auth_expired"),!0;const s=n instanceof Error?n.message:JSON.stringify(n);return(0,o.log)(`[openai-proxy][${t}] \u83B7\u53D6 LLM Token \u5931\u8D25: ${s}`),m(e,500,`Failed to obtain auth token: ${s}`,"server_error","token_fetch_error"),!0}B.authorization=`Bearer ${L}`;const te={hostname:k.hostname,port:k.port||ee,path:k.pathname+k.search,method:$,headers:B,timeout:O.timeout||12e4};(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u5230: ${$} ${k.href}`);try{const n=b.toString("utf-8");(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u4F53: ${n}`)}catch{}const N=Z.request(te,n=>{const s=n.statusCode||502,c=n.headers["content-type"]||"",w=c.includes("text/event-stream")||v,D=Date.now()-C;(0,o.log)(`[openai-proxy][${t}] \u76EE\u6807\u54CD\u5E94: ${s} ${c} (${D}ms)${w?" [SSE\u6D41\u5F0F]":""} (isStreamRequest=${v})`);const R={};for(const[h,u]of Object.entries(n.headers)){if(!u)continue;const p=h.toLowerCase();["connection","keep-alive","upgrade"].includes(p)||(R[h]=u)}const F=v&&!c.includes("text/event-stream")&&s>=400;if(F&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u975ESSE\u9519\u8BEF\u54CD\u5E94 (status=${s}, content-type=${c})\uFF0C\u964D\u7EA7\u4E3A\u975E\u6D41\u5F0F\u5904\u7406`),w&&!F){e.writeHead(s,R);let h="",u="",p="",S=!1,T=!1,d=!1;const x=100,E=async(a,y)=>{if(!a||S)return;const A=_.AuditContentType.OUTPUT;try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838${y?"(\u6700\u7EC8)":""}\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
13
- ${a}`),await(0,_.reviewText)(a,A,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(g){if(g instanceof _.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${g.fallbackMsg}`),S=!0;const G=g.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";P(e,`
11
+ `),r.end())}function ue(r){return new Promise((e,i)=>{const c=[];r.on("data",t=>c.push(t)),r.on("end",()=>e(Buffer.concat(c))),r.on("error",i)})}async function de(r,e,i){const c=r.url||"";if(!c.startsWith("/v1/"))return!1;const t=ce(),m=r.method||"GET",C=Date.now();(0,o.log)(`[openai-proxy][${t}] \u6536\u5230\u8BF7\u6C42: ${m} ${c}`);const q=ie;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 K=le(r);if(!K)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(K))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 b;try{b=await ue(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,u=null;if(c.includes("/chat/completions")&&b.length>0)try{u=JSON.parse(b.toString("utf-8")),v=u.stream===!0,(0,o.log)(`[openai-proxy][${t}] stream=${u.stream}`)}catch{}const Q=u?.messages?.some(n=>n.role==="tool");if(Q&&(0,o.log)(`[openai-proxy][${t}] \u68C0\u6D4B\u5230 tool \u6D88\u606F\uFF0C\u8DF3\u8FC7\u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF08\u5DE5\u5177\u8C03\u7528\u540E\u7EED\u8BF7\u6C42\uFF09`),u&&c.includes("/chat/completions")&&Array.isArray(u.messages)&&!Q){const n=[];for(const s of u.messages)if(s.role==="user"){if(typeof s.content=="string"&&s.content.length>0)n.push(s.content);else if(Array.isArray(s.content))for(const l of s.content)l.type==="text"&&typeof l.text=="string"&&l.text.length>0&&n.push(l.text)}if(n.length>0){const s=n[n.length-1];try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${s.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
12
+ ${s}`),await(0,w.reviewText)(s,w.AuditContentType.INPUT,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(l){if(l instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${l.fallbackMsg}`);const S=u?.model||"QBotClawRouter",I=l.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,S)):O(e,I,S),!0}(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${l.message||JSON.stringify(l)}`)}}}const j=new z.URL(q.targetBaseUrl),Z=j.pathname.replace(/\/+$/,""),ee=c.replace(/^\/v1/,""),k=new z.URL(Z+ee,j.origin),F=k.protocol==="https:",te=F?ae.default:re.default,oe=F?443:80,N={};for(const[n,s]of Object.entries(r.headers)){if(!s)continue;const l=n.toLowerCase();["host","connection","keep-alive","transfer-encoding","upgrade"].includes(l)||(N[n]=s)}N.host=k.host;let R;try{R=await(0,V.fetchLlmToken)(i),(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 l=u?.model||"QBotClawRouter",S=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,S,l)):O(e,S,l),!0}if(n instanceof se.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 s=n instanceof Error?n.message:JSON.stringify(n);return(0,o.log)(`[openai-proxy][${t}] \u83B7\u53D6 LLM Token \u5931\u8D25: ${s}`),_(e,500,`Failed to obtain auth token: ${s}`,"server_error","token_fetch_error"),!0}N.authorization=`Bearer ${R}`;const ne={hostname:k.hostname,port:k.port||oe,path:k.pathname+k.search,method:m,headers:N,timeout:q.timeout||12e4};(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u5230: ${m} ${k.href}`);try{const n=b.toString("utf-8");(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u4F53: ${n}`)}catch{}const D=te.request(ne,n=>{const s=n.statusCode||502,l=n.headers["content-type"]||"",S=l.includes("text/event-stream")||v,I=Date.now()-C;(0,o.log)(`[openai-proxy][${t}] \u76EE\u6807\u54CD\u5E94: ${s} ${l} (${I}ms)${S?" [SSE\u6D41\u5F0F]":""} (isStreamRequest=${v})`);const L={};for(const[y,g]of Object.entries(n.headers)){if(!g)continue;const p=y.toLowerCase();["connection","keep-alive","upgrade"].includes(p)||(L[y]=g)}const G=v&&!l.includes("text/event-stream")&&s>=400;if(G&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u975ESSE\u9519\u8BEF\u54CD\u5E94 (status=${s}, content-type=${l})\uFF0C\u964D\u7EA7\u4E3A\u975E\u6D41\u5F0F\u5904\u7406`),S&&!G){e.writeHead(s,L);let y="",g="",p="",x=!1,d=!1,M=!1;const f=100,T=async(a,$)=>{if(!a||x)return;const E=w.AuditContentType.OUTPUT;try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838${$?"(\u6700\u7EC8)":""}\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
13
+ ${a}`),await(0,w.reviewText)(a,E,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(h){if(h instanceof w.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
14
 
15
- [${G}]`,f?.model||"QBotClawRouter"),n.destroy();return}(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${g.message||JSON.stringify(g)}`)}};n.on("data",a=>{if(S){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA1\u6838\u5DF2\u4E2D\u65AD\uFF0C\u8DF3\u8FC7chunk (${a.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 (${a.length}\u5B57\u8282)`);return}const y=a.toString("utf-8");p+=y,p=p.replace(/\r\n/g,`
15
+ [${W}]`,u?.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",a=>{if(x){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA1\u6838\u5DF2\u4E2D\u65AD\uFF0C\u8DF3\u8FC7chunk (${a.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 (${a.length}\u5B57\u8282)`);return}const $=a.toString("utf-8");p+=$,p=p.replace(/\r\n/g,`
16
16
  `).replace(/\r/g,`
17
- `);const A=p.split(`
18
- `);p=A.pop()||"";for(let g=0;g<A.length;g++){const q=A[g].trim();if(q){if(q==="data: [DONE]")d=!0;else if(q.startsWith("data: ")){const oe=q.slice(6),I=z(oe);I.content&&(h+=I.content,u+=I.content),I.hasToolCalls&&(T=!0)}}}if(d&&u.length===0&&!T?(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(a),h.length>=x){const g=h;h="",(0,o.log)(`[openai-proxy][${t}] [SSE] \u89E6\u53D1\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${g.length} ${g}`),E(g,!1)}}),n.on("end",async()=>{if(p.trim()){const a=p.trim();if(a.startsWith("data: ")&&a!=="data: [DONE]"){const y=z(a.slice(6));y.content&&(h+=y.content,u+=y.content),y.hasToolCalls&&(T=!0)}}if(u.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 (${u.length}\u5B57\u7B26):
19
- ${u}`),h.length>0&&!S&&await E(h,!0),!S){if(u.length===0&&!T){(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4E14\u65E0tool_calls\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),P(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",f?.model||"QBotClawRouter");return}u.length===0&&T&&(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 a=Date.now()-C;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${a}ms`),e.end()}}),n.on("error",a=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${a.message}`),e.headersSent?S||e.end():m(e,502,`Bad Gateway: ${a.message}`,"server_error","proxy_error")})}else{const h=l.includes("/chat/completions"),u=[];n.on("data",p=>{u.push(p)}),n.on("end",async()=>{const p=Buffer.concat(u),S=p.toString("utf-8");if(s>=400&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u9519\u8BEF (status=${s}): ${S.substring(0,1e3)}`),h&&s>=200&&s<300)try{const d=JSON.parse(p.toString("utf-8")),x=[];if(Array.isArray(d.choices))for(const a of d.choices){const y=a.message?.content;typeof y=="string"&&y.length>0&&x.push(y)}const E=Array.isArray(d.choices)&&d.choices.some(a=>Array.isArray(a.message?.tool_calls)&&a.message.tool_calls.length>0||a.message?.function_call!=null);if(x.length===0&&!E){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 a=f?.model||d?.model||"QBotClawRouter";M(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",a)}return}if(x.length===0&&E&&(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`),x.length>0){const a=x.join(`
20
- `);(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}`),await(0,_.reviewText)(a,_.AuditContentType.OUTPUT,i),(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}}catch(d){if(d instanceof _.ContentReviewFailedError){if((0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${d.fallbackMsg}`),e.headersSent)e.end();else{const x=f?.model||"QBotClawRouter";M(e,d.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002",x)}return}(0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${d.message||d}`)}e.headersSent||e.writeHead(s,R),e.end(p);const T=Date.now()-C;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${T}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():m(e,502,`Bad Gateway: ${p.message}`,"server_error","proxy_error")})}});return N.on("error",n=>{const s=Date.now()-C;(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u5931\u8D25 (${s}ms): ${n.message}`),e.headersSent?e.end():m(e,502,`Bad Gateway: ${n.message}`,"server_error","proxy_error")}),N.on("timeout",()=>{const n=Date.now()-C;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u8D85\u65F6 (${n}ms)`),N.destroy(),e.headersSent?e.end():m(e,504,"Gateway Timeout","server_error","gateway_timeout")}),N.end(b),!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 A=JSON.parse(Y);if(A?.type==="upstream_error"){(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${A.reqid})\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",u?.model||"QBotClawRouter"),n.destroy();return}const J=X(A);J.content&&(y+=J.content,g+=J.content),J.hasToolCalls&&(d=!0)}catch(A){(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${A}`)}}}}if(M&&g.length===0&&!d?(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(a),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}`),T(h,!1)}}),n.on("end",async()=>{if(p.trim()){const a=p.trim();if(a.startsWith("data: ")&&a!=="data: [DONE]"){const $=pe(a.slice(6));$.content&&(y+=$.content,g+=$.content),$.hasToolCalls&&(d=!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 T(y,!0),!x){if(g.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`),B(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",u?.model||"QBotClawRouter");return}g.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 a=Date.now()-C;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${a}ms`),e.end()}}),n.on("error",a=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${a.message}`),e.headersSent?x||e.end():_(e,502,`Bad Gateway: ${a.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");s>=400&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u9519\u8BEF (status=${s}): ${x.substring(0,1e3)}`);let d=null;if(y)try{d=JSON.parse(x)}catch{}if(d?.type==="upstream_error"){if((0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${d.reqid})\uFF0C\u539F\u59CB\u56DE\u5305: ${x.substring(0,2e3)}\uFF0C\u8FD4\u56DE\u53CB\u597D\u63D0\u793A`),e.headersSent)e.end();else{const f=u?.model||"QBotClawRouter";O(e,"\u5F53\u524D\u4F7F\u7528\u4EBA\u6570\u8F83\u591A\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",f)}return}if(y&&s>=200&&s<300&&d)try{const f=[];if(Array.isArray(d.choices))for(const a of d.choices){const $=a.message?.content;typeof $=="string"&&$.length>0&&f.push($)}const T=Array.isArray(d.choices)&&d.choices.some(a=>Array.isArray(a.message?.tool_calls)&&a.message.tool_calls.length>0||a.message?.function_call!=null);if(f.length===0&&!T){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 a=u?.model||d?.model||"QBotClawRouter";O(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",a)}return}if(f.length===0&&T&&(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 a=f.join(`
20
+ `);(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}`),await(0,w.reviewText)(a,w.AuditContentType.OUTPUT,i),(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}}catch(f){if(f instanceof w.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 T=u?.model||"QBotClawRouter";O(e,f.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002",T)}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(s,L),e.end(p);const M=Date.now()-C;(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 s=Date.now()-C;(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u5931\u8D25 (${s}ms): ${n.message}`),e.headersSent?e.end():_(e,502,`Bad Gateway: ${n.message}`,"server_error","proxy_error")}),D.on("timeout",()=>{const n=Date.now()-C;(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(b),!0}
@@ -0,0 +1 @@
1
+ "use strict";var m=exports&&exports.__importDefault||function(s){return s&&s.__esModule?s:{default:s}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.handleQBotClawRoutes=_;const f=m(require("fs")),g=require("./config"),o=require("./utils"),w=require("./installer"),u=require("./reportLog"),O=["models.providers","agents.defaults.model"];function p(s){return O.some(e=>s===e||s.startsWith(e+"."))}const S=["models.providers."];function y(s){return S.some(e=>s.startsWith(e))}const h=["channels.logout"];function N(s){return h.includes(s)}function i(s){return new Promise((e,n)=>{let a="";s.on("data",t=>{a+=t.toString()}),s.on("end",()=>e(a)),s.on("error",n)})}function c(s){return new Promise(e=>{let n="";const a={writable:!0,write(t){return n+=t,!0}};(0,w.runQBotClaw)(s,a).then(()=>{e(n)})})}function J(s){const e=s.match(/\{[\s\S]*\}/);if(!e)throw new Error(`\u54CD\u5E94\u4E2D\u672A\u5305\u542B\u6709\u6548\u7684 JSON: ${s.slice(0,200)}`);return JSON.parse(e[0])}const b={"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET, POST, PUT, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type, Authorization, X-Requested-With","Access-Control-Max-Age":"86400"};async function _(s,e,n){if(!n.startsWith("/api/qbotclaw/"))return!1;if(!f.default.existsSync(g.QBOTCLAW_BIN))return(0,o.sendJSON)(e,404,{status:"\u5931\u8D25",message:`qbotclaw \u53EF\u6267\u884C\u6587\u4EF6\u4E0D\u5B58\u5728: ${g.QBOTCLAW_BIN}`}),!0;if(n.startsWith("/api/qbotclaw/config/get")&&s.method==="GET"){const t=new URL(n,"http://localhost").searchParams.get("key");if(!t)return(0,o.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 key"}),!0;if(!p(t))return(0,u.log)(`[qbotclaw/config/get] \u62D2\u7EDD\u8BBF\u95EE\u4E0D\u5141\u8BB8\u7684\u914D\u7F6E\u9879: ${t}`),(0,o.sendJSON)(e,403,{status:"\u5931\u8D25",message:`\u4E0D\u5141\u8BB8\u8BBF\u95EE\u8BE5\u914D\u7F6E\u9879: ${t}`}),!0;try{(0,u.log)(`[qbotclaw/config/get] \u83B7\u53D6\u914D\u7F6E: ${t}`);const r=await c(["config","get",t]),l=J(r);(0,o.sendJSON)(e,200,l)}catch(r){(0,u.log)(`[qbotclaw/config/get] \u6267\u884C\u5931\u8D25: ${r.message}`),(0,o.sendJSON)(e,500,{status:"\u5931\u8D25",message:`\u83B7\u53D6\u914D\u7F6E\u5931\u8D25: ${r.message}`})}return!0}if(n==="/api/qbotclaw/config/set"&&s.method==="POST"){try{const a=await i(s),t=JSON.parse(a);if(!t.key||typeof t.key!="string")return(0,o.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u53C2\u6570 key \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32"}),!0;if(t.value===void 0||t.value===null)return(0,o.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u53C2\u6570 value \u4E0D\u80FD\u4E3A\u7A7A"}),!0;if(!y(t.key))return(0,u.log)(`[qbotclaw/config/set] \u62D2\u7EDD\u8BBE\u7F6E\u4E0D\u5141\u8BB8\u7684\u914D\u7F6E\u9879: ${t.key}`),(0,o.sendJSON)(e,403,{status:"\u5931\u8D25",message:`\u4E0D\u5141\u8BB8\u8BBE\u7F6E\u8BE5\u914D\u7F6E\u9879: ${t.key}`}),!0;const r=typeof t.value=="string"?t.value:JSON.stringify(t.value);(0,u.log)(`[qbotclaw/config/set] \u8BBE\u7F6E\u914D\u7F6E: ${t.key}`);const l=await c(["config","set",t.key,r]);(0,o.sendJSON)(e,200,{status:"\u6210\u529F",message:"\u914D\u7F6E\u5DF2\u66F4\u65B0",output:l.trim()})}catch(a){(0,u.log)(`[qbotclaw/config/set] \u6267\u884C\u5931\u8D25: ${a.message}`),(0,o.sendJSON)(e,500,{status:"\u5931\u8D25",message:`\u8BBE\u7F6E\u914D\u7F6E\u5931\u8D25: ${a.message}`})}return!0}if(n==="/api/qbotclaw/models/set"&&s.method==="POST"){try{const a=await i(s),t=JSON.parse(a);if(!t.modelName||typeof t.modelName!="string")return(0,o.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u53C2\u6570 modelName \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32"}),!0;(0,u.log)(`[qbotclaw/models/set] \u8BBE\u7F6E\u6D3B\u8DC3\u6A21\u578B: ${t.modelName}`);const r=await c(["models","set",t.modelName]);(0,o.sendJSON)(e,200,{status:"\u6210\u529F",message:"\u6A21\u578B\u5DF2\u8BBE\u7F6E",output:r.trim()})}catch(a){(0,u.log)(`[qbotclaw/models/set] \u6267\u884C\u5931\u8D25: ${a.message}`),(0,o.sendJSON)(e,500,{status:"\u5931\u8D25",message:`\u8BBE\u7F6E\u6A21\u578B\u5931\u8D25: ${a.message}`})}return!0}if(n==="/api/qbotclaw/gateway/call"&&s.method==="POST"){try{const a=await i(s),t=JSON.parse(a);if(!t.method||typeof t.method!="string")return(0,o.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u53C2\u6570 method \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32"}),!0;if(!N(t.method))return(0,u.log)(`[qbotclaw/gateway/call] \u62D2\u7EDD\u8C03\u7528\u4E0D\u5141\u8BB8\u7684\u65B9\u6CD5: ${t.method}`),(0,o.sendJSON)(e,403,{status:"\u5931\u8D25",message:`\u4E0D\u5141\u8BB8\u8C03\u7528\u8BE5\u65B9\u6CD5: ${t.method}`}),!0;const r=["gateway","call",t.method];if(t.params){const d=typeof t.params=="string"?t.params:JSON.stringify(t.params);r.push("--params",d)}(0,u.log)(`[qbotclaw/gateway/call] \u8C03\u7528: ${t.method}`);const l=await c(r);(0,o.sendJSON)(e,200,{status:"\u6210\u529F",message:"\u8C03\u7528\u5B8C\u6210",output:l.trim()})}catch(a){(0,u.log)(`[qbotclaw/gateway/call] \u6267\u884C\u5931\u8D25: ${a.message}`),(0,o.sendJSON)(e,500,{status:"\u5931\u8D25",message:`\u8C03\u7528\u5931\u8D25: ${a.message}`})}return!0}return(0,o.sendJSON)(e,404,{status:"\u5931\u8D25",message:"\u63A5\u53E3\u4E0D\u5B58\u5728"}),!0}
package/dist/server.js CHANGED
@@ -1,6 +1,2 @@
1
- "use strict";var L=exports&&exports.__importDefault||function(g){return g&&g.__esModule?g:{default:g}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.server=void 0,exports.setGlobalAuthClient=K,exports.getGlobalAuthClient=z,exports.watchParentProcess=W;const U=L(require("http")),S=L(require("fs")),$=L(require("path")),u=require("./config"),n=require("./utils"),o=require("./reportLog"),T=require("./installer"),H=require("./build"),w=require("./state"),R=require("child_process"),G=require("./service"),J=require("./filePicker"),x=require("./fileSecurityCheck"),B=require("./authMiddleware"),Q="\u9009\u62E9\u6587\u4EF6",M=10485760,F=10,b=!1,P={CANCELLED:"CANCELLED",INVALID_PARAMS:"INVALID_PARAMS",DIALOG_BUSY:"DIALOG_BUSY",DIALOG_TIMEOUT:"DIALOG_TIMEOUT",PLATFORM_NOT_SUPPORTED:"PLATFORM_NOT_SUPPORTED",INTERNAL_ERROR:"INTERNAL_ERROR"},q=require("./openai-proxy"),V=require("./api/auth-client");let k=new V.AuthClient({usePipe:!0});function K(g){k=g}function z(){return k}const C={"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 j(g){for(const[e,f]of Object.entries(C))g.setHeader(e,f)}exports.server=U.default.createServer(async(g,e)=>{const f=g.url||"";if(j(e),g.method==="OPTIONS"){e.writeHead(204,C),e.end();return}if(f.startsWith("/v1/")){await(0,q.handleOpenAIProxy)(g,e,k);return}if(f==="/api/version"){let a="unknown";try{a=(await(0,n.execCommand)(`"${u.NODE_BIN}" -v`)).trim(),(0,o.log)(`[version] node \u7248\u672C: ${a}`)}catch(d){(0,o.log)(`[version] \u83B7\u53D6 node \u7248\u672C\u5931\u8D25: ${d.message}`),a=`error: ${d.message}`}const r=await(0,n.getQBotClawVersion)()??"0.0.0";(0,o.log)(`[version] qbotclaw \u7248\u672C: ${r}`);let t="0.0.0";if(S.default.existsSync(u.MCPORTER_BIN))try{t=(await(0,n.execCommand)(`"${u.NODE_BIN}" "${u.MCPORTER_BIN}" --version`)).trim(),(0,o.log)(`[version] mcporter \u7248\u672C: ${t}`)}catch(d){(0,o.log)(`[version] \u83B7\u53D6 mcporter \u7248\u672C\u5931\u8D25: ${d.message}`),t=`error: ${d.message}`}let i="0.0.0";if(S.default.existsSync(u.PYTHON_BIN))try{i=(await(0,n.execCommand)(`"${u.PYTHON_BIN}" --version`)).trim(),(0,o.log)(`[version] python \u7248\u672C: ${i}`)}catch(d){(0,o.log)(`[version] \u83B7\u53D6 python \u7248\u672C\u5931\u8D25: ${d.message}`),i=`error: ${d.message}`}(0,n.sendJSON)(e,200,{status:"\u6210\u529F",launcher:H.LAUNCHER_VERSION,node:a,qbotclaw:r,mcporter:t,python:i});return}if(f==="/api/port"){const a=exports.server.address(),r=a&&typeof a=="object"?a.port:null,t=(0,w.readState)();(0,n.sendJSON)(e,200,{status:"\u6210\u529F",launcher:r,qbotclaw:t.gatewayPort??2e3,mcpHttp:t.mcpHttpPort??u.DEFAULT_MCP_HTTP_PORT,mcpWs:t.mcpWsPort??8765}),(0,o.log)(`[port] launcher: ${r}, qbotclaw: ${t.gatewayPort}, mcpHttp: ${t.mcpHttpPort}, mcpWs: ${t.mcpWsPort}`);return}if(f==="/api/status"){const a=(0,w.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 t=[{name:"gateway",port:a.gatewayPort},{name:"mcpHttp",port:a.mcpHttpPort},{name:"mcpWs",port:a.mcpWsPort}];for(const{name:i,port:d}of t){if(!d){r.push(`${i} \u7AEF\u53E3\u672A\u914D\u7F6E\uFF08\u72B6\u6001\u4E2D\u65E0\u5BF9\u5E94\u7AEF\u53E3\u53F7\uFF09`);continue}await(0,w.isPortAvailable)(d)&&r.push(`${i} \u7AEF\u53E3 (${d}) \u65E0\u670D\u52A1\u76D1\u542C`)}r.length===0?(0,n.sendJSON)(e,200,{ok:!0,message:"\u6240\u6709\u670D\u52A1\u8FD0\u884C\u6B63\u5E38"}):(0,n.sendJSON)(e,200,{ok:!1,message:r.join("\uFF1B")});return}if(f==="/api/restart"){try{const a=(0,w.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:t,mcpHttpPort:i,mcpWsPort:d,...m}=a;(0,w.writeState)(m),(0,o.log)("[restart] \u5DF2\u6E05\u9664 gatewayPid, gatewayPort, mcpHttpPort, mcpWsPort"),await(0,T.configAndRunQBotClaw)(),(0,o.log)("[restart] QBotClaw gateway \u91CD\u542F\u5B8C\u6210");const c=(0,w.readState)(),l=exports.server.address(),s=l&&typeof l=="object"?l.port:null;(0,n.sendJSON)(e,200,{ok:!0,message:"QBotClaw gateway \u91CD\u542F\u6210\u529F",launcher:s,qbotclaw:c.gatewayPort??2e3,mcpHttp:c.mcpHttpPort??u.DEFAULT_MCP_HTTP_PORT,mcpWs:c.mcpWsPort??8765,gatewayToken:(0,G.getOrCreateGatewayToken)()})}catch(a){(0,o.log)(`[restart] \u91CD\u542F\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(e,500,{ok:!1,message:`\u91CD\u542F\u5931\u8D25: ${a.message}`})}return}if(f==="/api/wechat/history"){try{if(!S.default.existsSync(u.SESSIONS_FILE)){(0,n.sendJSON)(e,404,{ok:!1,sessionKey:"",message:"\u672A\u627E\u5230\u4F1A\u8BDD\u6587\u4EF6"});return}const a=JSON.parse(S.default.readFileSync(u.SESSIONS_FILE,"utf-8"));let r=null,t=0;for(const[m,c]of Object.entries(a))if(m.includes("openclaw-weixin")){const s=c.updatedAt??0;s>t&&(t=s,r=m)}if(!r){(0,n.sendJSON)(e,404,{ok:!1,sessionKey:"",message:"\u672A\u627E\u5230\u5FAE\u4FE1\u4F1A\u8BDD"});return}const d=r.split(":").slice(2).join(":");(0,n.sendJSON)(e,200,{ok:!0,sessionKey:d,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)(e,500,{ok:!1,sessionKey:"",message:`\u8BFB\u53D6\u5FAE\u4FE1\u4F1A\u8BDD\u5931\u8D25: ${a.message}`})}return}if(f==="/api/chat/latest"){try{if(!S.default.existsSync(u.SESSIONS_FILE)){(0,n.sendJSON)(e,404,{ok:!1,sessionId:"",message:"\u672A\u627E\u5230\u4F1A\u8BDD\u6587\u4EF6"});return}const a=JSON.parse(S.default.readFileSync(u.SESSIONS_FILE,"utf-8"));let r=null,t=0;for(const[d,m]of Object.entries(a)){if(d.includes("openclaw-weixin"))continue;const l=m.updatedAt??0;l>t&&(t=l,r=d)}if(!r){(0,n.sendJSON)(e,404,{ok:!1,sessionId:"",message:"\u672A\u627E\u5230\u975E\u5FAE\u4FE1\u4F1A\u8BDD"});return}const i=a[r];(0,n.sendJSON)(e,200,{ok:!0,sessionId:i.sessionId??"",sessionKey:r,updatedAt:t,message:"\u83B7\u53D6\u6210\u529F"})}catch(a){(0,o.log)(`[chat/latest] \u8BFB\u53D6\u4F1A\u8BDD\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(e,500,{ok:!1,sessionId:"",message:`\u8BFB\u53D6\u4F1A\u8BDD\u5931\u8D25: ${a.message}`})}return}if(f==="/api/installing"){(0,n.sendJSON)(e,200,{installing:T.isInstalling});return}if(f==="/api/install"||f.startsWith("/api/install?")){if(T.installState.status==="downloading"){(0,n.sendJSON)(e,200,{ok:"downloading",message:"\u6B63\u5728\u5B89\u88C5\u4E2D..."});return}const a=new URL(f,"http://localhost"),r=a.searchParams.get("version")||void 0,t=a.searchParams.get("overrideConfig")==="true";if(!r){(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 version\uFF0C\u7528\u6CD5: GET /api/install?version=x.x.x"});return}e.writeHead(200,{...C,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"}),(0,T.runInstall)(e,r,t);return}if(f.startsWith("/api/watch")){const r=new URL(f,"http://localhost").searchParams.get("ppid");if(!r||isNaN(Number(r))){(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u6709\u6548\u7684 ppid \u53C2\u6570\uFF0C\u7528\u6CD5: GET /api/watch?ppid=12345"});return}const t=parseInt(r,10);try{process.kill(t,0)}catch{(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:`\u8FDB\u7A0B ${t} \u4E0D\u5B58\u5728`});return}const i=(0,w.readState)();(0,w.writeState)({...i,ppid:t}),W(t),(0,n.sendJSON)(e,200,{status:"\u6210\u529F",message:`\u5DF2\u5F00\u59CB\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${t}`});return}if(f==="/api/qbotclaw"&&g.method==="POST"){let a="";g.on("data",r=>{a+=r.toString()}),g.on("end",()=>{let r;try{const t=JSON.parse(a);if(!Array.isArray(t.args)||t.args.length===0){(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u53C2\u6570 args \u5FC5\u987B\u662F\u975E\u7A7A\u6570\u7EC4"});return}r=t.args.map(i=>String(i))}catch{(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u8BF7\u6C42\u4F53\u5FC5\u987B\u662F\u5408\u6CD5\u7684 JSON"});return}if(!S.default.existsSync(u.QBOTCLAW_BIN)){(0,n.sendJSON)(e,404,{status:"\u5931\u8D25",message:`qbotclaw \u53EF\u6267\u884C\u6587\u4EF6\u4E0D\u5B58\u5728: ${u.QBOTCLAW_BIN}`});return}e.writeHead(200,{...C,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"}),(0,T.runQBotClaw)(r,e).then(()=>{e.end(`
2
- [\u6267\u884C\u6210\u529F]
3
- `)}).catch(t=>{e.end(`
4
- [\u542F\u52A8\u5931\u8D25: ${t.message}]
5
- `)})});return}if(f.startsWith("/api/uninstall")){const r=new URL(f,"http://localhost").searchParams.get("keepWorkspace")==="true";e.writeHead(200,{...C,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"});const t=(i,d,m)=>{if(e.writable)try{e.write(JSON.stringify({status:i,file:d,msg:m})+`
6
- `)}catch{}};try{t("\u6210\u529F","","\u6B63\u5728\u505C\u6B62 QBotClaw...");const i=(0,w.readState)();if(i.gatewayPid)try{process.kill(i.gatewayPid,"SIGTERM"),(0,o.log)(`[uninstall] \u5DF2\u505C\u6B62 QBotClaw gateway \u8FDB\u7A0B pid: ${i.gatewayPid}`),t("\u6210\u529F","",`\u5DF2\u505C\u6B62 QBotClaw gateway \u8FDB\u7A0B pid: ${i.gatewayPid}`)}catch{(0,o.log)(`[uninstall] QBotClaw gateway \u8FDB\u7A0B ${i.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA`),t("\u6210\u529F","",`QBotClaw gateway \u8FDB\u7A0B ${i.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA`)}else t("\u6210\u529F","","QBotClaw \u672A\u5728\u8FD0\u884C");t("\u6210\u529F","",`\u6B63\u5728\u6267\u884C npm uninstall -g ${u.NPM_PACKAGE_NAME}...`);try{await new Promise((m,c)=>{const l=(0,R.spawn)(u.NPM_BIN,["--userconfig",u.NPM_RC,"uninstall","-g",u.NPM_PACKAGE_NAME],{env:{...process.env,PATH:`${u.NODE_BIN_DIR}:${process.env.PATH}`}});l.stdout?.on("data",s=>{(0,o.log)(`[uninstall] npm: ${s.toString().trim()}`)}),l.stderr?.on("data",s=>{(0,o.log)(`[uninstall] npm stderr: ${s.toString().trim()}`)}),l.on("close",s=>{s===0?m():c(new Error(`npm uninstall \u9000\u51FA\u7801: ${s}`))}),l.on("error",s=>c(s))}),(0,o.log)(`[uninstall] npm uninstall -g ${u.NPM_PACKAGE_NAME} \u6210\u529F`),t("\u6210\u529F","",`npm uninstall -g ${u.NPM_PACKAGE_NAME} \u6210\u529F`)}catch(m){(0,o.log)(`[uninstall] npm uninstall \u5931\u8D25: ${m.message}`),t("\u5931\u8D25","",`npm uninstall \u5931\u8D25: ${m.message}`)}t("\u6210\u529F","",`\u6B63\u5728\u6E05\u7406\u76EE\u5F55 ${u.QBOT_CLAW_DIR}...`);const d=[];if(S.default.existsSync(u.QBOT_CLAW_DIR)){const m=S.default.readdirSync(u.QBOT_CLAW_DIR);for(const c of m){const l=$.default.join(u.QBOT_CLAW_DIR,c);if(r&&c==="workspace"){t("\u6210\u529F",l,"\u4FDD\u7559 workspace \u76EE\u5F55");continue}try{S.default.statSync(l).isDirectory()?S.default.rmSync(l,{recursive:!0,force:!0}):S.default.unlinkSync(l),t("\u6210\u529F",l,"\u5DF2\u5220\u9664")}catch(s){(0,o.log)(`[uninstall] \u5220\u9664\u5931\u8D25: ${l} - ${s.message}`),t("\u5931\u8D25",l,`\u5220\u9664\u5931\u8D25: ${s.message}`),d.push(l)}}if(d.length>0){t("\u6210\u529F","",`${d.length} \u4E2A\u9879\u76EE\u5220\u9664\u5931\u8D25\uFF0C\u4F7F\u7528 rm \u547D\u4EE4\u91CD\u8BD5...`);for(const c of d)try{(0,R.execSync)(`rm -rf "${c}"`),(0,o.log)(`[uninstall] rm -rf \u91CD\u8BD5\u6210\u529F: ${c}`),t("\u6210\u529F",c,"rm \u91CD\u8BD5\u5220\u9664\u6210\u529F")}catch(l){(0,o.log)(`[uninstall] rm -rf \u91CD\u8BD5\u4E5F\u5931\u8D25: ${c} - ${l.message}`),t("\u5931\u8D25",c,`rm \u91CD\u8BD5\u4E5F\u5931\u8D25: ${l.message}`)}}if(r)t("\u6210\u529F",u.QBOT_CLAW_DIR,"\u4FDD\u7559\u6839\u76EE\u5F55\uFF08workspace \u6A21\u5F0F\uFF09");else try{S.default.rmSync(u.QBOT_CLAW_DIR,{recursive:!0,force:!0}),t("\u6210\u529F",u.QBOT_CLAW_DIR,"\u5DF2\u5220\u9664\u6839\u76EE\u5F55")}catch(c){(0,o.log)(`[uninstall] \u5220\u9664\u6839\u76EE\u5F55\u5931\u8D25: ${c.message}`);try{(0,R.execSync)(`rm -rf "${u.QBOT_CLAW_DIR}"`),t("\u6210\u529F",u.QBOT_CLAW_DIR,"rm \u91CD\u8BD5\u5220\u9664\u6839\u76EE\u5F55\u6210\u529F")}catch(l){t("\u5931\u8D25",u.QBOT_CLAW_DIR,`\u5220\u9664\u6839\u76EE\u5F55\u5931\u8D25: ${l.message}`)}}}else t("\u6210\u529F",u.QBOT_CLAW_DIR,"\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u65E0\u9700\u6E05\u7406");t("\u6210\u529F","","\u5378\u8F7D\u5B8C\u6210")}catch(i){(0,o.log)(`[uninstall] \u5378\u8F7D\u8FC7\u7A0B\u5F02\u5E38: ${i.message}`),t("\u5931\u8D25","",`\u5378\u8F7D\u8FC7\u7A0B\u5F02\u5E38: ${i.message}`)}e.end(),setTimeout(()=>{(0,o.log)("[uninstall] \u5378\u8F7D\u6D41\u7A0B\u7ED3\u675F\uFF0Clauncher \u8FDB\u7A0B\u9000\u51FA"),process.exit(0)},500);return}if(f==="/api/file-picker"&&g.method==="POST"){const a=(0,B.authenticateRequest)(g);if(!a.ok){(0,n.sendJSON)(e,a.statusCode,{ok:!1,message:a.message});return}if((0,J.isFilePickerBusy)()){(0,n.sendJSON)(e,409,{ok:!1,code:P.DIALOG_BUSY,files:[],message:"\u5DF2\u6709\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u6B63\u5728\u663E\u793A\uFF0C\u8BF7\u7B49\u5F85\u5F53\u524D\u5BF9\u8BDD\u6846\u5173\u95ED\u540E\u91CD\u8BD5"});return}let r="";g.on("data",t=>{r+=t.toString()}),g.on("end",async()=>{let t=[],i=!1,d=Q,m=M,c=F,l=b;if(r){let s;try{s=JSON.parse(r)}catch{(0,n.sendJSON)(e,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u8BF7\u6C42\u4F53\u5FC5\u987B\u662F\u5408\u6CD5\u7684 JSON"});return}if(s.extensions!==void 0){if(!Array.isArray(s.extensions)||!s.extensions.every(p=>typeof p=="string")){(0,n.sendJSON)(e,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 extensions \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4"});return}t=s.extensions}if(s.multiple!==void 0){if(typeof s.multiple!="boolean"){(0,n.sendJSON)(e,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 multiple \u5FC5\u987B\u662F\u5E03\u5C14\u503C"});return}i=s.multiple}if(s.title!==void 0){if(typeof s.title!="string"||s.title.length>200){(0,n.sendJSON)(e,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 title \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u4E14\u957F\u5EA6\u4E0D\u8D85\u8FC7 200"});return}d=s.title}if(s.maxFileSize!==void 0){if(typeof s.maxFileSize!="number"||s.maxFileSize<=0||!Number.isFinite(s.maxFileSize)){(0,n.sendJSON)(e,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileSize \u5FC5\u987B\u662F\u6B63\u6570"});return}m=Math.min(s.maxFileSize,M)}if(s.maxFileCount!==void 0){if(typeof s.maxFileCount!="number"||s.maxFileCount<=0||!Number.isInteger(s.maxFileCount)){(0,n.sendJSON)(e,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileCount \u5FC5\u987B\u662F\u6B63\u6574\u6570"});return}c=Math.min(s.maxFileCount,F)}if(b&&s.allowDirectory!==void 0){if(typeof s.allowDirectory!="boolean"){(0,n.sendJSON)(e,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 allowDirectory \u5FC5\u987B\u662F\u5E03\u5C14\u503C"});return}l=s.allowDirectory}}i||(c=1);try{const s=await(0,J.showFilePicker)({extensions:t,multiple:i,title:d,allowDirectory:l});if(!s||s.length===0){(0,n.sendJSON)(e,200,{ok:!1,code:P.CANCELLED,files:[],message:"\u7528\u6237\u53D6\u6D88\u4E86\u6587\u4EF6\u9009\u62E9"});return}if(l){const N=s.slice(0,c).map(D=>({name:$.default.basename(D),path:D,size:0,mimeType:"inode/directory"})),h=s.length>c;(0,n.sendJSON)(e,200,{ok:!0,files:N,message:h?`\u5DF2\u9009\u62E9 ${N.length} \u4E2A\u76EE\u5F55\uFF08\u5171\u9009\u62E9 ${s.length} \u4E2A\uFF0C\u8D85\u51FA\u4E0A\u9650 ${c} \u4E2A\u5DF2\u622A\u65AD\uFF09`:`\u5DF2\u9009\u62E9 ${N.length} \u4E2A\u76EE\u5F55`});return}let p=!1,A=s;s.length>c&&(A=s.slice(0,c),p=!0);const _=[],y=[];for(const N of A){const h=(0,x.readFileInfo)(N,m);h.ok?_.push({name:h.name,path:h.path,size:h.size,mimeType:h.mimeType}):y.push({path:h.path,reason:h.reason,code:h.code})}const O=[];if(_.length>0&&O.push(`\u6210\u529F\u8BFB\u53D6 ${_.length} \u4E2A\u6587\u4EF6`),y.length>0){const N=y.map(h=>`${$.default.basename(h.path)}: ${h.reason}`).join("\uFF1B");O.push(`${y.length} \u4E2A\u6587\u4EF6\u5931\u8D25\uFF08${N}\uFF09`)}p&&O.push(`\u5171\u9009\u62E9 ${s.length} \u4E2A\u6587\u4EF6\uFF0C\u8D85\u51FA\u4E0A\u9650 ${c} \u4E2A\u5DF2\u622A\u65AD`);const v=_.length===0&&y.length>0;(0,n.sendJSON)(e,200,{ok:!v,code:v?y[0].code:void 0,files:_,failedFiles:y.length>0?y:void 0,message:O.join("\uFF1B")||"\u6587\u4EF6\u9009\u62E9\u6210\u529F"})}catch(s){(0,o.log)(`[file-picker] \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${s.message}`);let p=P.INTERNAL_ERROR;s.message.includes("\u8D85\u65F6")?p=P.DIALOG_TIMEOUT:s.message.includes("\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0")&&(p=P.PLATFORM_NOT_SUPPORTED),(0,n.sendJSON)(e,500,{ok:!1,code:p,files:[],message:`\u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${s.message}`})}});return}if(f.startsWith("/api/reveal")){console.log(`[reveal] \u5B8C\u6574URL: ${f}`);const a=(0,B.authenticateRequest)(g);if(!a.ok){(0,n.sendJSON)(e,a.statusCode,{status:"\u5931\u8D25",message:a.message});return}const t=new URL(f,"http://localhost").searchParams.get("path")?.replace(/ /g,"+")??null;if(!t){(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 path\uFF08\u9700 Base64 \u7F16\u7801\uFF09\uFF0C\u7528\u6CD5: GET /api/reveal?path=<base64\u7F16\u7801\u7684\u6587\u4EF6\u7EDD\u5BF9\u8DEF\u5F84>"});return}let i;try{i=Buffer.from(t,"base64").toString("utf-8")}catch{(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:"path \u53C2\u6570 Base64 \u89E3\u7801\u5931\u8D25\uFF0C\u8BF7\u786E\u4FDD\u4F20\u5165\u5408\u6CD5\u7684 Base64 \u7F16\u7801\u5B57\u7B26\u4E32"});return}if(!i||i.trim()===""){(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:"Base64 \u89E3\u7801\u540E\u8DEF\u5F84\u4E3A\u7A7A"});return}const d=(0,x.validateFilePath)(i);if(!d.valid){const p=d.reason;p.includes("\u975E\u6CD5\u5B57\u7B26")?(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:p}):p.includes("\u4E0D\u5B58\u5728")?(0,n.sendJSON)(e,404,{status:"\u5931\u8D25",message:p}):p.includes("\u65E0\u6CD5\u89E3\u6790")?(0,n.sendJSON)(e,400,{status:"\u5931\u8D25",message:p}):((0,o.log)(`[reveal] \u5B89\u5168\u68C0\u67E5\u88AB\u62D2\u7EDD: ${i}\uFF0C\u539F\u56E0: ${p}`),(0,n.sendJSON)(e,403,{status:"\u5931\u8D25",message:p}));return}const m=$.default.resolve(i),c=process.platform;let l,s;c==="darwin"?(l="open",s=["-R",m]):c==="win32"?(l="explorer",s=[`/select,${m}`]):(l="xdg-open",s=[$.default.dirname(m)]);try{await new Promise((p,A)=>{const _=(0,R.spawn)(l,s,{stdio:"ignore"}),y=setTimeout(()=>{_.kill(),A(new Error("\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u8D85\u65F6\uFF085\u79D2\uFF09"))},5e3);_.on("close",O=>{clearTimeout(y),O===0||O===1?p():A(new Error(`\u8FDB\u7A0B\u9000\u51FA\u7801: ${O}`))}),_.on("error",O=>{clearTimeout(y),A(O)})}),(0,o.log)(`[reveal] \u5DF2\u5728\u6587\u4EF6\u7BA1\u7406\u5668\u4E2D\u663E\u793A: ${m}`),(0,n.sendJSON)(e,200,{status:"\u6210\u529F",message:`\u5DF2\u5728\u6587\u4EF6\u7BA1\u7406\u5668\u4E2D\u663E\u793A: ${m}`})}catch(p){(0,o.log)(`[reveal] \u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u5931\u8D25: ${p.message}`),(0,n.sendJSON)(e,500,{status:"\u5931\u8D25",message:`\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u5931\u8D25: ${p.message}`})}return}(0,n.sendJSON)(e,404,{status:"\u5931\u8D25",message:"\u63A5\u53E3\u4E0D\u5B58\u5728"})});let I=null,E=null;function W(g){if(!g)return;if(I&&E===g){(0,o.log)(`[watchParentProcess] \u5DF2\u5728\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${g}\uFF0C\u8DF3\u8FC7\u91CD\u590D\u76D1\u542C`);return}I?((0,o.log)(`[watchParentProcess] \u5DF2\u6709\u76D1\u63A7\uFF08ppid: ${E}\uFF09\uFF0C\u505C\u6B62\u65E7\u7684\u76D1\u63A7\uFF0C\u91CD\u65B0\u76D1\u63A7 ppid: ${g}`),clearInterval(I),I=null,E=null):(0,o.log)(`[watchParentProcess] \u5F00\u59CB\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${g}`);const e=setInterval(()=>{try{process.kill(g,0)}catch{(0,o.log)(`[watchParentProcess] \u7236\u8FDB\u7A0B ${g} \u5DF2\u9000\u51FA\uFF0C\u5F00\u59CB\u6E05\u7406\u5E76\u9000\u51FA`),clearInterval(e),I=null,E=null;const f=(0,w.readState)();if(f.gatewayPid)try{process.kill(f.gatewayPid,"SIGTERM"),(0,o.log)(`[watchParentProcess] \u5DF2\u53D1\u9001 SIGTERM \u7ED9 qbotclaw \u8FDB\u7A0B pid: ${f.gatewayPid}`)}catch{(0,o.log)(`[watchParentProcess] qbotclaw \u8FDB\u7A0B ${f.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA\uFF0C\u5FFD\u7565`)}(0,w.clearState)(),(0,o.log)("[watchParentProcess] \u72B6\u6001\u5DF2\u6E05\u7406\uFF0C\u5173\u95ED server \u5E76\u9000\u51FA"),exports.server.close(()=>{(0,o.log)("[watchParentProcess] server \u5DF2\u5173\u95ED\uFF0C\u8FDB\u7A0B\u9000\u51FA"),process.exit(0)}),setTimeout(()=>{(0,o.log)("[watchParentProcess] server.close \u8D85\u65F6\uFF0C\u5F3A\u5236\u9000\u51FA"),process.exit(0)},3e3).unref()}},3e3);e.unref(),I=e,E=g}
1
+ "use strict";var L=exports&&exports.__importDefault||function(g){return g&&g.__esModule?g:{default:g}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.server=void 0,exports.setGlobalAuthClient=z,exports.getGlobalAuthClient=j,exports.watchParentProcess=W;const U=L(require("http")),S=L(require("fs")),$=L(require("path")),d=require("./config"),n=require("./utils"),r=require("./reportLog"),C=require("./installer"),G=require("./build"),y=require("./state"),E=require("child_process"),H=require("./service"),J=require("./filePicker"),x=require("./fileSecurityCheck"),M=require("./authMiddleware"),q=require("./qbotclaw-routes"),Q="\u9009\u62E9\u6587\u4EF6",F=10485760,b=10,B=!1,P={CANCELLED:"CANCELLED",INVALID_PARAMS:"INVALID_PARAMS",DIALOG_BUSY:"DIALOG_BUSY",DIALOG_TIMEOUT:"DIALOG_TIMEOUT",PLATFORM_NOT_SUPPORTED:"PLATFORM_NOT_SUPPORTED",INTERNAL_ERROR:"INTERNAL_ERROR"},V=require("./openai-proxy"),K=require("./api/auth-client");let k=new K.AuthClient({usePipe:!0});function z(g){k=g}function j(){return k}const R={"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET, POST, PUT, DELETE, OPTIONS","Access-Control-Allow-Headers":"Content-Type, Authorization, X-Requested-With","Access-Control-Max-Age":"86400"};function Y(g){for(const[t,f]of Object.entries(R))g.setHeader(t,f)}exports.server=U.default.createServer(async(g,t)=>{const f=g.url||"";if(Y(t),g.method==="OPTIONS"){t.writeHead(204,R),t.end();return}if(f.startsWith("/v1/")){await(0,V.handleOpenAIProxy)(g,t,k);return}if(f==="/api/version"){let a="unknown";try{a=(await(0,n.execCommand)(`"${d.NODE_BIN}" -v`)).trim(),(0,r.log)(`[version] node \u7248\u672C: ${a}`)}catch(u){(0,r.log)(`[version] \u83B7\u53D6 node \u7248\u672C\u5931\u8D25: ${u.message}`),a=`error: ${u.message}`}const o=await(0,n.getQBotClawVersion)()??"0.0.0";(0,r.log)(`[version] qbotclaw \u7248\u672C: ${o}`);let s="0.0.0";if(S.default.existsSync(d.MCPORTER_BIN))try{s=(await(0,n.execCommand)(`"${d.NODE_BIN}" "${d.MCPORTER_BIN}" --version`)).trim(),(0,r.log)(`[version] mcporter \u7248\u672C: ${s}`)}catch(u){(0,r.log)(`[version] \u83B7\u53D6 mcporter \u7248\u672C\u5931\u8D25: ${u.message}`),s=`error: ${u.message}`}let i="0.0.0";if(S.default.existsSync(d.PYTHON_BIN))try{i=(await(0,n.execCommand)(`"${d.PYTHON_BIN}" --version`)).trim(),(0,r.log)(`[version] python \u7248\u672C: ${i}`)}catch(u){(0,r.log)(`[version] \u83B7\u53D6 python \u7248\u672C\u5931\u8D25: ${u.message}`),i=`error: ${u.message}`}(0,n.sendJSON)(t,200,{status:"\u6210\u529F",launcher:G.LAUNCHER_VERSION,node:a,qbotclaw:o,mcporter:s,python:i});return}if(f==="/api/port"){const a=exports.server.address(),o=a&&typeof a=="object"?a.port:null,s=(0,y.readState)();(0,n.sendJSON)(t,200,{status:"\u6210\u529F",launcher:o,qbotclaw:s.gatewayPort??2e3,mcpHttp:s.mcpHttpPort??d.DEFAULT_MCP_HTTP_PORT,mcpWs:s.mcpWsPort??8765}),(0,r.log)(`[port] launcher: ${o}, qbotclaw: ${s.gatewayPort}, mcpHttp: ${s.mcpHttpPort}, mcpWs: ${s.mcpWsPort}`);return}if(f==="/api/status"){const a=(0,y.readState)(),o=[];if(a.gatewayPid)try{process.kill(a.gatewayPid,0)}catch{o.push(`gateway \u8FDB\u7A0B (pid: ${a.gatewayPid}) \u5DF2\u9000\u51FA`)}else o.push("gateway \u8FDB\u7A0B\u672A\u542F\u52A8\uFF08\u72B6\u6001\u4E2D\u65E0 gatewayPid\uFF09");const s=[{name:"gateway",port:a.gatewayPort},{name:"mcpHttp",port:a.mcpHttpPort},{name:"mcpWs",port:a.mcpWsPort}];for(const{name:i,port:u}of s){if(!u){o.push(`${i} \u7AEF\u53E3\u672A\u914D\u7F6E\uFF08\u72B6\u6001\u4E2D\u65E0\u5BF9\u5E94\u7AEF\u53E3\u53F7\uFF09`);continue}await(0,y.isPortAvailable)(u)&&o.push(`${i} \u7AEF\u53E3 (${u}) \u65E0\u670D\u52A1\u76D1\u542C`)}o.length===0?(0,n.sendJSON)(t,200,{ok:!0,message:"\u6240\u6709\u670D\u52A1\u8FD0\u884C\u6B63\u5E38"}):(0,n.sendJSON)(t,200,{ok:!1,message:o.join("\uFF1B")});return}if(f==="/api/restart"){try{const a=(0,y.readState)();if(a.gatewayPid)try{process.kill(a.gatewayPid,"SIGTERM"),(0,r.log)(`[restart] \u5DF2\u53D1\u9001 SIGTERM \u7ED9\u65E7 gateway \u8FDB\u7A0B pid: ${a.gatewayPid}`)}catch{(0,r.log)(`[restart] \u65E7 gateway \u8FDB\u7A0B ${a.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA\uFF0C\u5FFD\u7565`)}const{gatewayPid:o,gatewayPort:s,mcpHttpPort:i,mcpWsPort:u,...p}=a;(0,y.writeState)(p),(0,r.log)("[restart] \u5DF2\u6E05\u9664 gatewayPid, gatewayPort, mcpHttpPort, mcpWsPort"),await(0,C.configAndRunQBotClaw)(),(0,r.log)("[restart] QBotClaw gateway \u91CD\u542F\u5B8C\u6210");const c=(0,y.readState)(),l=exports.server.address(),e=l&&typeof l=="object"?l.port:null;(0,n.sendJSON)(t,200,{ok:!0,message:"QBotClaw gateway \u91CD\u542F\u6210\u529F",launcher:e,qbotclaw:c.gatewayPort??2e3,mcpHttp:c.mcpHttpPort??d.DEFAULT_MCP_HTTP_PORT,mcpWs:c.mcpWsPort??8765,gatewayToken:(0,H.getOrCreateGatewayToken)()})}catch(a){(0,r.log)(`[restart] \u91CD\u542F\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,message:`\u91CD\u542F\u5931\u8D25: ${a.message}`})}return}if(f==="/api/wechat/history"){try{if(!S.default.existsSync(d.SESSIONS_FILE)){(0,n.sendJSON)(t,404,{ok:!1,sessionKey:"",message:"\u672A\u627E\u5230\u4F1A\u8BDD\u6587\u4EF6"});return}const a=JSON.parse(S.default.readFileSync(d.SESSIONS_FILE,"utf-8"));let o=null,s=0;for(const[p,c]of Object.entries(a))if(p.includes("openclaw-weixin")){const e=c.updatedAt??0;e>s&&(s=e,o=p)}if(!o){(0,n.sendJSON)(t,404,{ok:!1,sessionKey:"",message:"\u672A\u627E\u5230\u5FAE\u4FE1\u4F1A\u8BDD"});return}const u=o.split(":").slice(2).join(":");(0,n.sendJSON)(t,200,{ok:!0,sessionKey:u,message:"\u83B7\u53D6\u6210\u529F"})}catch(a){(0,r.log)(`[wechat/history] \u8BFB\u53D6\u5FAE\u4FE1\u4F1A\u8BDD\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,sessionKey:"",message:`\u8BFB\u53D6\u5FAE\u4FE1\u4F1A\u8BDD\u5931\u8D25: ${a.message}`})}return}if(f==="/api/chat/latest"){try{if(!S.default.existsSync(d.SESSIONS_FILE)){(0,n.sendJSON)(t,404,{ok:!1,sessionId:"",message:"\u672A\u627E\u5230\u4F1A\u8BDD\u6587\u4EF6"});return}const a=JSON.parse(S.default.readFileSync(d.SESSIONS_FILE,"utf-8"));let o=null,s=0;for(const[u,p]of Object.entries(a)){if(u.includes("openclaw-weixin"))continue;const l=p.updatedAt??0;l>s&&(s=l,o=u)}if(!o){(0,n.sendJSON)(t,404,{ok:!1,sessionId:"",message:"\u672A\u627E\u5230\u975E\u5FAE\u4FE1\u4F1A\u8BDD"});return}const i=a[o];(0,n.sendJSON)(t,200,{ok:!0,sessionId:i.sessionId??"",sessionKey:o,updatedAt:s,message:"\u83B7\u53D6\u6210\u529F"})}catch(a){(0,r.log)(`[chat/latest] \u8BFB\u53D6\u4F1A\u8BDD\u5931\u8D25: ${a.message}`),(0,n.sendJSON)(t,500,{ok:!1,sessionId:"",message:`\u8BFB\u53D6\u4F1A\u8BDD\u5931\u8D25: ${a.message}`})}return}if(f==="/api/installing"){(0,n.sendJSON)(t,200,{installing:C.isInstalling});return}if(f==="/api/install"||f.startsWith("/api/install?")){if(C.installState.status==="downloading"){(0,n.sendJSON)(t,200,{ok:"downloading",message:"\u6B63\u5728\u5B89\u88C5\u4E2D..."});return}const a=new URL(f,"http://localhost"),o=a.searchParams.get("version")||void 0,s=a.searchParams.get("overrideConfig")==="true";if(!o){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 version\uFF0C\u7528\u6CD5: GET /api/install?version=x.x.x"});return}t.writeHead(200,{...R,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"}),(0,C.runInstall)(t,o,s);return}if(f.startsWith("/api/watch")){const o=new URL(f,"http://localhost").searchParams.get("ppid");if(!o||isNaN(Number(o))){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u6709\u6548\u7684 ppid \u53C2\u6570\uFF0C\u7528\u6CD5: GET /api/watch?ppid=12345"});return}const s=parseInt(o,10);try{process.kill(s,0)}catch{(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:`\u8FDB\u7A0B ${s} \u4E0D\u5B58\u5728`});return}const i=(0,y.readState)();(0,y.writeState)({...i,ppid:s}),W(s),(0,n.sendJSON)(t,200,{status:"\u6210\u529F",message:`\u5DF2\u5F00\u59CB\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${s}`});return}if(!(f.startsWith("/api/qbotclaw/")&&await(0,q.handleQBotClawRoutes)(g,t,f))){if(f.startsWith("/api/uninstall")){const o=new URL(f,"http://localhost").searchParams.get("keepWorkspace")==="true";t.writeHead(200,{...R,"Content-Type":"text/plain; charset=utf-8","Transfer-Encoding":"chunked","Cache-Control":"no-cache"});const s=(i,u,p)=>{if(t.writable)try{t.write(JSON.stringify({status:i,file:u,msg:p})+`
2
+ `)}catch{}};try{s("\u6210\u529F","","\u6B63\u5728\u505C\u6B62 QBotClaw...");const i=(0,y.readState)();if(i.gatewayPid)try{process.kill(i.gatewayPid,"SIGTERM"),(0,r.log)(`[uninstall] \u5DF2\u505C\u6B62 QBotClaw gateway \u8FDB\u7A0B pid: ${i.gatewayPid}`),s("\u6210\u529F","",`\u5DF2\u505C\u6B62 QBotClaw gateway \u8FDB\u7A0B pid: ${i.gatewayPid}`)}catch{(0,r.log)(`[uninstall] QBotClaw gateway \u8FDB\u7A0B ${i.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA`),s("\u6210\u529F","",`QBotClaw gateway \u8FDB\u7A0B ${i.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA`)}else s("\u6210\u529F","","QBotClaw \u672A\u5728\u8FD0\u884C");s("\u6210\u529F","",`\u6B63\u5728\u6267\u884C npm uninstall -g ${d.NPM_PACKAGE_NAME}...`);try{await new Promise((p,c)=>{const l=(0,E.spawn)(d.NPM_BIN,["--userconfig",d.NPM_RC,"uninstall","-g",d.NPM_PACKAGE_NAME],{env:{...process.env,PATH:`${d.NODE_BIN_DIR}:${process.env.PATH}`}});l.stdout?.on("data",e=>{(0,r.log)(`[uninstall] npm: ${e.toString().trim()}`)}),l.stderr?.on("data",e=>{(0,r.log)(`[uninstall] npm stderr: ${e.toString().trim()}`)}),l.on("close",e=>{e===0?p():c(new Error(`npm uninstall \u9000\u51FA\u7801: ${e}`))}),l.on("error",e=>c(e))}),(0,r.log)(`[uninstall] npm uninstall -g ${d.NPM_PACKAGE_NAME} \u6210\u529F`),s("\u6210\u529F","",`npm uninstall -g ${d.NPM_PACKAGE_NAME} \u6210\u529F`)}catch(p){(0,r.log)(`[uninstall] npm uninstall \u5931\u8D25: ${p.message}`),s("\u5931\u8D25","",`npm uninstall \u5931\u8D25: ${p.message}`)}s("\u6210\u529F","",`\u6B63\u5728\u6E05\u7406\u76EE\u5F55 ${d.QBOT_CLAW_DIR}...`);const u=[];if(S.default.existsSync(d.QBOT_CLAW_DIR)){const p=S.default.readdirSync(d.QBOT_CLAW_DIR);for(const c of p){const l=$.default.join(d.QBOT_CLAW_DIR,c);if(o&&c==="workspace"){s("\u6210\u529F",l,"\u4FDD\u7559 workspace \u76EE\u5F55");continue}try{S.default.statSync(l).isDirectory()?S.default.rmSync(l,{recursive:!0,force:!0}):S.default.unlinkSync(l),s("\u6210\u529F",l,"\u5DF2\u5220\u9664")}catch(e){(0,r.log)(`[uninstall] \u5220\u9664\u5931\u8D25: ${l} - ${e.message}`),s("\u5931\u8D25",l,`\u5220\u9664\u5931\u8D25: ${e.message}`),u.push(l)}}if(u.length>0){s("\u6210\u529F","",`${u.length} \u4E2A\u9879\u76EE\u5220\u9664\u5931\u8D25\uFF0C\u4F7F\u7528 rm \u547D\u4EE4\u91CD\u8BD5...`);for(const c of u)try{(0,E.execSync)(`rm -rf "${c}"`),(0,r.log)(`[uninstall] rm -rf \u91CD\u8BD5\u6210\u529F: ${c}`),s("\u6210\u529F",c,"rm \u91CD\u8BD5\u5220\u9664\u6210\u529F")}catch(l){(0,r.log)(`[uninstall] rm -rf \u91CD\u8BD5\u4E5F\u5931\u8D25: ${c} - ${l.message}`),s("\u5931\u8D25",c,`rm \u91CD\u8BD5\u4E5F\u5931\u8D25: ${l.message}`)}}if(o)s("\u6210\u529F",d.QBOT_CLAW_DIR,"\u4FDD\u7559\u6839\u76EE\u5F55\uFF08workspace \u6A21\u5F0F\uFF09");else try{S.default.rmSync(d.QBOT_CLAW_DIR,{recursive:!0,force:!0}),s("\u6210\u529F",d.QBOT_CLAW_DIR,"\u5DF2\u5220\u9664\u6839\u76EE\u5F55")}catch(c){(0,r.log)(`[uninstall] \u5220\u9664\u6839\u76EE\u5F55\u5931\u8D25: ${c.message}`);try{(0,E.execSync)(`rm -rf "${d.QBOT_CLAW_DIR}"`),s("\u6210\u529F",d.QBOT_CLAW_DIR,"rm \u91CD\u8BD5\u5220\u9664\u6839\u76EE\u5F55\u6210\u529F")}catch(l){s("\u5931\u8D25",d.QBOT_CLAW_DIR,`\u5220\u9664\u6839\u76EE\u5F55\u5931\u8D25: ${l.message}`)}}}else s("\u6210\u529F",d.QBOT_CLAW_DIR,"\u76EE\u5F55\u4E0D\u5B58\u5728\uFF0C\u65E0\u9700\u6E05\u7406");s("\u6210\u529F","","\u5378\u8F7D\u5B8C\u6210")}catch(i){(0,r.log)(`[uninstall] \u5378\u8F7D\u8FC7\u7A0B\u5F02\u5E38: ${i.message}`),s("\u5931\u8D25","",`\u5378\u8F7D\u8FC7\u7A0B\u5F02\u5E38: ${i.message}`)}t.end(),setTimeout(()=>{(0,r.log)("[uninstall] \u5378\u8F7D\u6D41\u7A0B\u7ED3\u675F\uFF0Clauncher \u8FDB\u7A0B\u9000\u51FA"),process.exit(0)},500);return}if(f==="/api/file-picker"&&g.method==="POST"){const a=(0,M.authenticateRequest)(g);if(!a.ok){(0,n.sendJSON)(t,a.statusCode,{ok:!1,message:a.message});return}if((0,J.isFilePickerBusy)()){(0,n.sendJSON)(t,409,{ok:!1,code:P.DIALOG_BUSY,files:[],message:"\u5DF2\u6709\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u6B63\u5728\u663E\u793A\uFF0C\u8BF7\u7B49\u5F85\u5F53\u524D\u5BF9\u8BDD\u6846\u5173\u95ED\u540E\u91CD\u8BD5"});return}let o="";g.on("data",s=>{o+=s.toString()}),g.on("end",async()=>{let s=[],i=!1,u=Q,p=F,c=b,l=B;if(o){let e;try{e=JSON.parse(o)}catch{(0,n.sendJSON)(t,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u8BF7\u6C42\u4F53\u5FC5\u987B\u662F\u5408\u6CD5\u7684 JSON"});return}if(e.extensions!==void 0){if(!Array.isArray(e.extensions)||!e.extensions.every(m=>typeof m=="string")){(0,n.sendJSON)(t,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 extensions \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u6570\u7EC4"});return}s=e.extensions}if(e.multiple!==void 0){if(typeof e.multiple!="boolean"){(0,n.sendJSON)(t,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 multiple \u5FC5\u987B\u662F\u5E03\u5C14\u503C"});return}i=e.multiple}if(e.title!==void 0){if(typeof e.title!="string"||e.title.length>200){(0,n.sendJSON)(t,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 title \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u4E14\u957F\u5EA6\u4E0D\u8D85\u8FC7 200"});return}u=e.title}if(e.maxFileSize!==void 0){if(typeof e.maxFileSize!="number"||e.maxFileSize<=0||!Number.isFinite(e.maxFileSize)){(0,n.sendJSON)(t,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileSize \u5FC5\u987B\u662F\u6B63\u6570"});return}p=Math.min(e.maxFileSize,F)}if(e.maxFileCount!==void 0){if(typeof e.maxFileCount!="number"||e.maxFileCount<=0||!Number.isInteger(e.maxFileCount)){(0,n.sendJSON)(t,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 maxFileCount \u5FC5\u987B\u662F\u6B63\u6574\u6570"});return}c=Math.min(e.maxFileCount,b)}if(B&&e.allowDirectory!==void 0){if(typeof e.allowDirectory!="boolean"){(0,n.sendJSON)(t,400,{ok:!1,code:P.INVALID_PARAMS,files:[],message:"\u53C2\u6570 allowDirectory \u5FC5\u987B\u662F\u5E03\u5C14\u503C"});return}l=e.allowDirectory}}i||(c=1);try{const e=await(0,J.showFilePicker)({extensions:s,multiple:i,title:u,allowDirectory:l});if(!e||e.length===0){(0,n.sendJSON)(t,200,{ok:!1,code:P.CANCELLED,files:[],message:"\u7528\u6237\u53D6\u6D88\u4E86\u6587\u4EF6\u9009\u62E9"});return}if(l){const N=e.slice(0,c).map(D=>({name:$.default.basename(D),path:D,size:0,mimeType:"inode/directory"})),h=e.length>c;(0,n.sendJSON)(t,200,{ok:!0,files:N,message:h?`\u5DF2\u9009\u62E9 ${N.length} \u4E2A\u76EE\u5F55\uFF08\u5171\u9009\u62E9 ${e.length} \u4E2A\uFF0C\u8D85\u51FA\u4E0A\u9650 ${c} \u4E2A\u5DF2\u622A\u65AD\uFF09`:`\u5DF2\u9009\u62E9 ${N.length} \u4E2A\u76EE\u5F55`});return}let m=!1,A=e;e.length>c&&(A=e.slice(0,c),m=!0);const O=[],w=[];for(const N of A){const h=(0,x.readFileInfo)(N,p);h.ok?O.push({name:h.name,path:h.path,size:h.size,mimeType:h.mimeType}):w.push({path:h.path,reason:h.reason,code:h.code})}const _=[];if(O.length>0&&_.push(`\u6210\u529F\u8BFB\u53D6 ${O.length} \u4E2A\u6587\u4EF6`),w.length>0){const N=w.map(h=>`${$.default.basename(h.path)}: ${h.reason}`).join("\uFF1B");_.push(`${w.length} \u4E2A\u6587\u4EF6\u5931\u8D25\uFF08${N}\uFF09`)}m&&_.push(`\u5171\u9009\u62E9 ${e.length} \u4E2A\u6587\u4EF6\uFF0C\u8D85\u51FA\u4E0A\u9650 ${c} \u4E2A\u5DF2\u622A\u65AD`);const v=O.length===0&&w.length>0;(0,n.sendJSON)(t,200,{ok:!v,code:v?w[0].code:void 0,files:O,failedFiles:w.length>0?w:void 0,message:_.join("\uFF1B")||"\u6587\u4EF6\u9009\u62E9\u6210\u529F"})}catch(e){(0,r.log)(`[file-picker] \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${e.message}`);let m=P.INTERNAL_ERROR;e.message.includes("\u8D85\u65F6")?m=P.DIALOG_TIMEOUT:e.message.includes("\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0")&&(m=P.PLATFORM_NOT_SUPPORTED),(0,n.sendJSON)(t,500,{ok:!1,code:m,files:[],message:`\u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${e.message}`})}});return}if(f.startsWith("/api/reveal")){console.log(`[reveal] \u5B8C\u6574URL: ${f}`);const a=(0,M.authenticateRequest)(g);if(!a.ok){(0,n.sendJSON)(t,a.statusCode,{status:"\u5931\u8D25",message:a.message});return}const s=new URL(f,"http://localhost").searchParams.get("path")?.replace(/ /g,"+")??null;if(!s){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570 path\uFF08\u9700 Base64 \u7F16\u7801\uFF09\uFF0C\u7528\u6CD5: GET /api/reveal?path=<base64\u7F16\u7801\u7684\u6587\u4EF6\u7EDD\u5BF9\u8DEF\u5F84>"});return}let i;try{i=Buffer.from(s,"base64").toString("utf-8")}catch{(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"path \u53C2\u6570 Base64 \u89E3\u7801\u5931\u8D25\uFF0C\u8BF7\u786E\u4FDD\u4F20\u5165\u5408\u6CD5\u7684 Base64 \u7F16\u7801\u5B57\u7B26\u4E32"});return}if(!i||i.trim()===""){(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:"Base64 \u89E3\u7801\u540E\u8DEF\u5F84\u4E3A\u7A7A"});return}const u=(0,x.validateFilePath)(i);if(!u.valid){const m=u.reason;m.includes("\u975E\u6CD5\u5B57\u7B26")?(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:m}):m.includes("\u4E0D\u5B58\u5728")?(0,n.sendJSON)(t,404,{status:"\u5931\u8D25",message:m}):m.includes("\u65E0\u6CD5\u89E3\u6790")?(0,n.sendJSON)(t,400,{status:"\u5931\u8D25",message:m}):((0,r.log)(`[reveal] \u5B89\u5168\u68C0\u67E5\u88AB\u62D2\u7EDD: ${i}\uFF0C\u539F\u56E0: ${m}`),(0,n.sendJSON)(t,403,{status:"\u5931\u8D25",message:m}));return}const p=$.default.resolve(i),c=process.platform;let l,e;c==="darwin"?(l="open",e=["-R",p]):c==="win32"?(l="explorer",e=[`/select,${p}`]):(l="xdg-open",e=[$.default.dirname(p)]);try{await new Promise((m,A)=>{const O=(0,E.spawn)(l,e,{stdio:"ignore"}),w=setTimeout(()=>{O.kill(),A(new Error("\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u8D85\u65F6\uFF085\u79D2\uFF09"))},5e3);O.on("close",_=>{clearTimeout(w),_===0||_===1?m():A(new Error(`\u8FDB\u7A0B\u9000\u51FA\u7801: ${_}`))}),O.on("error",_=>{clearTimeout(w),A(_)})}),(0,r.log)(`[reveal] \u5DF2\u5728\u6587\u4EF6\u7BA1\u7406\u5668\u4E2D\u663E\u793A: ${p}`),(0,n.sendJSON)(t,200,{status:"\u6210\u529F",message:`\u5DF2\u5728\u6587\u4EF6\u7BA1\u7406\u5668\u4E2D\u663E\u793A: ${p}`})}catch(m){(0,r.log)(`[reveal] \u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u5931\u8D25: ${m.message}`),(0,n.sendJSON)(t,500,{status:"\u5931\u8D25",message:`\u6253\u5F00\u6587\u4EF6\u7BA1\u7406\u5668\u5931\u8D25: ${m.message}`})}return}(0,n.sendJSON)(t,404,{status:"\u5931\u8D25",message:"\u63A5\u53E3\u4E0D\u5B58\u5728"})}});let I=null,T=null;function W(g){if(!g)return;if(I&&T===g){(0,r.log)(`[watchParentProcess] \u5DF2\u5728\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${g}\uFF0C\u8DF3\u8FC7\u91CD\u590D\u76D1\u542C`);return}I?((0,r.log)(`[watchParentProcess] \u5DF2\u6709\u76D1\u63A7\uFF08ppid: ${T}\uFF09\uFF0C\u505C\u6B62\u65E7\u7684\u76D1\u63A7\uFF0C\u91CD\u65B0\u76D1\u63A7 ppid: ${g}`),clearInterval(I),I=null,T=null):(0,r.log)(`[watchParentProcess] \u5F00\u59CB\u76D1\u63A7\u7236\u8FDB\u7A0B ppid: ${g}`);const t=setInterval(()=>{try{process.kill(g,0)}catch{(0,r.log)(`[watchParentProcess] \u7236\u8FDB\u7A0B ${g} \u5DF2\u9000\u51FA\uFF0C\u5F00\u59CB\u6E05\u7406\u5E76\u9000\u51FA`),clearInterval(t),I=null,T=null;const f=(0,y.readState)();if(f.gatewayPid)try{process.kill(f.gatewayPid,"SIGTERM"),(0,r.log)(`[watchParentProcess] \u5DF2\u53D1\u9001 SIGTERM \u7ED9 qbotclaw \u8FDB\u7A0B pid: ${f.gatewayPid}`)}catch{(0,r.log)(`[watchParentProcess] qbotclaw \u8FDB\u7A0B ${f.gatewayPid} \u53EF\u80FD\u5DF2\u9000\u51FA\uFF0C\u5FFD\u7565`)}(0,y.clearState)(),(0,r.log)("[watchParentProcess] \u72B6\u6001\u5DF2\u6E05\u7406\uFF0C\u5173\u95ED server \u5E76\u9000\u51FA"),exports.server.close(()=>{(0,r.log)("[watchParentProcess] server \u5DF2\u5173\u95ED\uFF0C\u8FDB\u7A0B\u9000\u51FA"),process.exit(0)}),setTimeout(()=>{(0,r.log)("[watchParentProcess] server.close \u8D85\u65F6\uFF0C\u5F3A\u5236\u9000\u51FA"),process.exit(0)},3e3).unref()}},3e3);t.unref(),I=t,T=g}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qqbrowser/qbot-claw-launcher",
3
- "version": "0.9.89",
3
+ "version": "0.9.91",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "bin": {