@qqbrowser/qbot-claw-launcher 0.10.11 → 0.10.12
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 +1 -1
- package/package.json +1 -1
package/dist/build.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IS_DEBUG_BUILD=exports.LAUNCHER_VERSION=void 0,exports.LAUNCHER_VERSION="0.10.
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IS_DEBUG_BUILD=exports.LAUNCHER_VERSION=void 0,exports.LAUNCHER_VERSION="0.10.12",exports.IS_DEBUG_BUILD=!1;
|
package/dist/openai-proxy.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
`),r.end()}function re(r,e){const{isStream:i,httpStatus:l,message:t,type:h,code:y,extra:O}=e;if(!i){_(r,l,t,h,y,O);return}if(!r.headersSent){_(r,l,t,h,y,O);return}k(r,t,h,y,O)}function me(r){return new Promise((e,i)=>{const l=[];r.on("data",t=>l.push(t)),r.on("end",()=>e(Buffer.concat(l))),r.on("error",i)})}async function Se(r,e,i){const l=r.url||"";if(!l.startsWith("/v1/"))return!1;const t=Te(),h=r.method||"GET",y=Date.now();(0,o.log)(`[openai-proxy][${t}] \u6536\u5230\u8BF7\u6C42: ${h} ${l}`);const O=he;if(!O.enabled)return(0,o.log)(`[openai-proxy][${t}] \u4EE3\u7406\u672A\u542F\u7528\uFF0C\u8FD4\u56DE 404`),_(e,404,"Not Found",p.INVALID_REQUEST,u.NOT_FOUND),!0;const W=ye(r);if(!W)return(0,o.log)(`[openai-proxy][${t}] \u7F3A\u5C11 API Key`),_(e,401,"You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth.",p.INVALID_REQUEST,u.MISSING_API_KEY),!0;if(!O.allowedApiKeys.includes(W))return(0,o.log)(`[openai-proxy][${t}] API Key \u6821\u9A8C\u5931\u8D25`),_(e,401,"Incorrect API key provided. You can check your API key in the proxy configuration.",p.INVALID_REQUEST,u.INVALID_API_KEY),!0;let D;try{D=await me(r)}catch(n){return(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u8BF7\u6C42\u4F53\u5931\u8D25: ${n.message}`),_(e,400,"Failed to read request body",p.INVALID_REQUEST,u.BAD_REQUEST),!0}let x=!1,A=null;if(l.includes("/chat/completions")&&D.length>0)try{A=JSON.parse(D.toString("utf-8")),x=A.stream===!0,(0,o.log)(`[openai-proxy][${t}] stream=${A.stream}`)}catch{}let q=!1;if(Array.isArray(A?.messages)){const n=A.messages;let a=-1;for(let s=n.length-1;s>=0;s--)if(n[s].role==="user"){a=s;break}a>=0&&(q=n.slice(a+1).some(s=>s.role==="tool"))}if(q&&(0,o.log)(`[openai-proxy][${t}] \u6700\u540E\u4E00\u6761\u7528\u6237\u6D88\u606F\u4E4B\u540E\u5B58\u5728 tool \u6D88\u606F\uFF0C\u8DF3\u8FC7\u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF08\u5DE5\u5177\u8C03\u7528\u540E\u7EED\u8BF7\u6C42\uFF09`),A&&l.includes("/chat/completions")&&Array.isArray(A.messages)&&!q){const n=[];for(const a of A.messages)if(a.role==="user"){if(typeof a.content=="string"&&a.content.length>0)n.push(a.content);else if(Array.isArray(a.content))for(const s of a.content)s.type==="text"&&typeof s.text=="string"&&s.text.length>0&&n.push(s.text)}if(n.length>0){const a=n[n.length-1];try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
|
|
12
12
|
${a}`),await(0,w.reviewText)(a,w.AuditContentType.INPUT,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(s){if(s instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${s.fallbackMsg}`);const B=s.fallbackMsg||"\u8F93\u5165\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";return re(e,{isStream:x,httpStatus:400,message:B,type:p.CONTENT_FILTER,code:u.CONTENT_REVIEW_FAILED_INPUT,extra:{phase_action:s.phase_action,is_show:s.is_show}}),!0}(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${s.message||JSON.stringify(s)}`)}}}const Q=new te.URL(O.targetBaseUrl),ae=Q.pathname.replace(/\/+$/,""),se=l.replace(/^\/v1/,""),C=new te.URL(ae+se,Q.origin),X=C.protocol==="https:",ie=X?de.default:fe.default,le=X?443:80,L={};for(const[n,a]of Object.entries(r.headers)){if(!a)continue;const s=n.toLowerCase();["host","connection","keep-alive","transfer-encoding","upgrade"].includes(s)||(L[n]=a)}L.host=C.host;let H;try{H=await(0,oe.fetchLlmToken)(i),(0,o.log)(`[openai-proxy][${t}] \u6210\u529F\u83B7\u53D6 LLM Token ${H}`)}catch(n){if(n instanceof oe.TokenExhaustedError){(0,o.log)(`[openai-proxy][${t}] Token \u5DF2\u8017\u5C3D: ${n.message}`);const s=n.message||"\u5F53\u65E5Token\u5DF2\u8017\u5C3D\uFF0C\u8BF7\u5207\u6362\u6A21\u578B\u6216\u660E\u65E5\u518D\u8BD5";return re(e,{isStream:x,httpStatus:429,message:s,type:p.INSUFFICIENT_QUOTA,code:u.TOKEN_EXHAUSTED}),!0}if(n instanceof ge.AuthExpiredError)return(0,o.log)(`[openai-proxy][${t}] \u7968\u636E\u8FC7\u671F: ${n.message}`),_(e,401,`Auth token expired: ${n.message}`,p.AUTH,u.AUTH_EXPIRED),!0;const a=n instanceof Error?n.message:JSON.stringify(n);return(0,o.log)(`[openai-proxy][${t}] \u83B7\u53D6 LLM Token \u5931\u8D25: ${a}`),_(e,500,`Failed to obtain auth token: ${a}`,p.SERVER,u.TOKEN_FETCH_ERROR),!0}L.authorization=`Bearer ${H}`;const ce={hostname:C.hostname,port:C.port||le,path:C.pathname+C.search,method:h,headers:L,timeout:O.timeout||12e4};(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u5230: ${h} ${C.href}`);try{const n=D.toString("utf-8");(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u4F53: ${n.length>500?n.substring(0,500)+`...(truncated, total ${n.length} chars)`:n}`)}catch{}const v=ie.request(ce,n=>{const a=n.statusCode||502,s=n.headers["content-type"]||"",B=s.includes("text/event-stream")||x,pe=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u76EE\u6807\u54CD\u5E94: ${a} ${s} (${pe}ms)${B?" [SSE\u6D41\u5F0F]":""} (isStreamRequest=${x})`);const K={};for(const[T,E]of Object.entries(n.headers)){if(!E)continue;const f=T.toLowerCase();["connection","keep-alive","upgrade"].includes(f)||(K[T]=E)}const j=x&&!s.includes("text/event-stream")&&a>=400;if(j&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u975ESSE\u9519\u8BEF\u54CD\u5E94 (status=${a}, content-type=${s})\uFF0C\u964D\u7EA7\u4E3A\u975E\u6D41\u5F0F\u5904\u7406`),B&&!j){e.writeHead(a,K);let T="",E="",f="",$=!1,d=!1,M=!1;const g=100;let R=[],m=!1,N=!1,z=Promise.resolve();const Y=()=>{if(!(!e.writable||$)){for(const c of R)e.write(c);R.length>0&&(m=!0),R=[]}},Z=async(c,U)=>{if(!c||$)return;const b=w.AuditContentType.OUTPUT;try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838${U?"(\u6700\u7EC8)":""}\uFF0C\u6587\u672C\u957F\u5EA6: ${c.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
|
|
13
|
-
${c}`),await(0,w.reviewText)(c,b,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7\uFF0Cflush ${R.length} \u4E2A\u7F13\u5B58chunk`),Y()}catch(S){if(S instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${S.fallbackMsg}`),$=!0,R=[];const I
|
|
13
|
+
${c}`),await(0,w.reviewText)(c,b,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7\uFF0Cflush ${R.length} \u4E2A\u7F13\u5B58chunk`),Y()}catch(S){if(S instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${S.fallbackMsg}`),$=!0,R=[];const I=`400 ${S.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002"}`;m?((0,o.log)(`[openai-proxy][${t}] [SSE] \u5DF2\u6709\u5185\u5BB9\u88ABflush\uFF0C\u8FFD\u52A0\u6807\u51C6 error`),k(e,I,p.CONTENT_FILTER,u.CONTENT_REVIEW_FAILED_OUTPUT_PARTIAL,{phase_action:S.phase_action,is_show:S.is_show})):((0,o.log)(`[openai-proxy][${t}] [SSE] \u5C1A\u672Aflush\u4EFB\u4F55\u5185\u5BB9\uFF0C\u8FD4\u56DE\u6807\u51C6 error`),k(e,I,p.CONTENT_FILTER,u.CONTENT_REVIEW_FAILED_OUTPUT,{phase_action:S.phase_action,is_show:S.is_show})),n.destroy();return}(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF0C\u76F4\u63A5flush\uFF09: ${S.message||JSON.stringify(S)}`),Y()}};n.on("data",c=>{if($){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA1\u6838\u5DF2\u4E2D\u65AD\uFF0C\u8DF3\u8FC7chunk (${c.length}\u5B57\u8282)`);return}if(!e.writable){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA2\u6237\u7AEF\u8FDE\u63A5\u5DF2\u5173\u95ED(res.writable=false)\uFF0C\u8DF3\u8FC7chunk (${c.length}\u5B57\u8282)`);return}const U=c.toString("utf-8");f+=U,f=f.replace(/\r\n/g,`
|
|
14
14
|
`).replace(/\r/g,`
|
|
15
15
|
`);const b=f.split(`
|
|
16
16
|
`);f=b.pop()||"";let S=!1;for(let I=0;I<b.length;I++){const F=b[I].trim();if(F){if(F==="data: [DONE]")M=!0;else if(F.startsWith("data: ")){const ee=F.slice(6);try{const P=JSON.parse(ee);if(P?.type==="upstream_error"||P?.error?.type==="upstream_error"){const ue=P.reqid||P.error?.reqid;(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${ue})\uFF0C\u539F\u59CB\u56DE\u5305: ${ee}\uFF0C\u8FD4\u56DE\u6807\u51C6 error`),$=!0,R=[],k(e,"\u5F53\u524D\u4F7F\u7528\u4EBA\u6570\u8F83\u591A\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",p.UPSTREAM,u.UPSTREAM_UNAVAILABLE),n.destroy(),S=!0;break}const V=ne(P);V.content&&(T+=V.content,E+=V.content),V.hasToolCalls&&(d=!0)}catch(P){(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${P}`)}}}}if(!S&&(M&&E.length===0&&!d?(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230 [DONE] \u4E14\u8F93\u51FA\u4E3A\u7A7A\uFF0C\u6682\u4E0D\u7F13\u5B58\uFF0C\u7B49\u5F85 end \u4E8B\u4EF6\u5904\u7406`):R.push(c),T.length>=g&&!N)){const I=T;T="",(0,o.log)(`[openai-proxy][${t}] [SSE] \u89E6\u53D1\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${I.length} ${I}`),N=!0,n.pause(),z=Z(I,!1).finally(()=>{N=!1,$||n.resume()})}}),n.on("end",async()=>{if(N&&((0,o.log)(`[openai-proxy][${t}] [SSE] end\u4E8B\u4EF6\u89E6\u53D1\uFF0C\u7B49\u5F85\u8FDB\u884C\u4E2D\u7684\u5BA1\u6838\u5B8C\u6210...`),await z),f.trim()){const c=f.trim();if(c.startsWith("data: ")&&c!=="data: [DONE]"){const U=$e(c.slice(6));U.content&&(T+=U.content,E+=U.content),U.hasToolCalls&&(d=!0)}}if(E.length===0?(0,o.log)(`[openai-proxy][${t}] [SSE] \u26A0\uFE0F \u6A21\u578B\u8F93\u51FA\u5185\u5BB9\u4E3A\u7A7A\uFF01\u8BF7\u68C0\u67E5\u4E0A\u65B9\u65E5\u5FD7\u4E2D\u7684\u539F\u59CBchunk\u6570\u636E`):(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u5185\u5BB9 (${E.length}\u5B57\u7B26):
|