@qqbrowser/qbot-claw-launcher 0.9.81 → 0.9.83
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build.js +1 -1
- package/dist/openai-proxy.js +11 -11
- package/package.json +3 -7
package/dist/build.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IS_DEBUG_BUILD=exports.LAUNCHER_VERSION=void 0,exports.LAUNCHER_VERSION="0.9.
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IS_DEBUG_BUILD=exports.LAUNCHER_VERSION=void 0,exports.LAUNCHER_VERSION="0.9.83",exports.IS_DEBUG_BUILD=!1;
|
package/dist/openai-proxy.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
"use strict";var
|
|
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: ${$}
|
|
2
2
|
|
|
3
|
-
`);const
|
|
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}
|
|
4
4
|
|
|
5
5
|
`),r.write(`data: [DONE]
|
|
6
6
|
|
|
7
|
-
`),r.end()}function
|
|
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}
|
|
8
8
|
|
|
9
9
|
`),r.write(`data: [DONE]
|
|
10
10
|
|
|
11
|
-
`),r.end())}function
|
|
12
|
-
${
|
|
13
|
-
${
|
|
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,`
|
|
14
14
|
|
|
15
|
-
[${
|
|
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,`
|
|
16
16
|
`).replace(/\r/g,`
|
|
17
|
-
`);const
|
|
18
|
-
`);p=
|
|
19
|
-
${
|
|
20
|
-
`);(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${
|
|
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}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qqbrowser/qbot-claw-launcher",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.83",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -10,15 +10,11 @@
|
|
|
10
10
|
"dist",
|
|
11
11
|
"node_modules"
|
|
12
12
|
],
|
|
13
|
-
"bundledDependencies":
|
|
14
|
-
"fs-xattr",
|
|
15
|
-
"@parcel/watcher",
|
|
16
|
-
"@tencent/qb-auth-send-h5"
|
|
17
|
-
],
|
|
13
|
+
"bundledDependencies": true,
|
|
18
14
|
"scripts": {
|
|
19
15
|
"build": "node scripts/inject-version.js && tsc && node scripts/obfuscate.js && chmod +x dist/index.js",
|
|
20
16
|
"prepare": "test -d prebuilds && mkdir -p fs-xattr/build/Release parcel-watcher/build/Release && cp prebuilds/xattr.node fs-xattr/build/Release/ && cp prebuilds/watcher.node parcel-watcher/build/Release/ && echo '✅ prebuilds copied' || true",
|
|
21
|
-
"prepack": "
|
|
17
|
+
"prepack": "bash scripts/prepack.sh",
|
|
22
18
|
"start": "node dist/index.js",
|
|
23
19
|
"dev": "ts-node src/index.ts",
|
|
24
20
|
"test": "echo \"Error: no test specified\" && exit 1"
|