@qqbrowser/qbot-claw-launcher 0.9.79 → 0.9.81
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.
|
@@ -41,7 +41,8 @@ class ContentReviewFailedError extends Error {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
exports.ContentReviewFailedError = ContentReviewFailedError;
|
|
44
|
-
const reviewText = async (text, auditContentType,
|
|
44
|
+
const reviewText = async (text, auditContentType, authOrLoginInfo) => {
|
|
45
|
+
const sessionId = process.env.QB_QIMEI36 || '';
|
|
45
46
|
const isAuthClient = authOrLoginInfo && 'callWithAuth' in authOrLoginInfo;
|
|
46
47
|
const authClient = isAuthClient ? authOrLoginInfo : undefined;
|
|
47
48
|
if (authClient) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-review.js","sourceRoot":"","sources":["../../src/api/content-review.ts"],"names":[],"mappings":";;;AACA,iCAAwC;AAExC,4CAAmC;AAenC,IAAY,gBAQX;AARD,WAAY,gBAAgB;IAC1B,qEAAe,CAAA;IAEf,yDAAS,CAAA;IAET,2DAAU,CAAA;IAEV,+DAAY,CAAA;AACd,CAAC,EARW,gBAAgB,gCAAhB,gBAAgB,QAQ3B;AAgCM,MAAM,eAAe,GAAG,KAAK,EAAE,UAAuB,EAAyB,EAAE;IACtF,MAAM,UAAU,GAAG,UAAU,EAAE,mBAAmB,EAAE,CAAC;IACrD,MAAM,SAAS,GAAG,UAAU,EAAE,YAAY,EAAE,CAAC;IAC7C,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,EAAE,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;IAClE,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,EAAE,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC;IAC5E,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,EAAE,IAAI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CAC7F,CAAC;IACJ,CAAC;SAAM,CAAC;QAEN,GAAG,GAAG,MAAM,wBAAgB,CAAC,eAAe,CAAC,IAAI,EAAE,eAAwC,CAAC,CAAC;IAC/F,CAAC;IAED,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;QACd,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;IACvD,CAAC;IAED,OAAO,GAAG,CAAC,UAAU,CAAC;AACxB,CAAC,CAAC;AA1CW,QAAA,UAAU,cA0CrB"}
|
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.81",exports.IS_DEBUG_BUILD=!1;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var c=exports&&exports.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.BLOCKED_DIRS=exports.FILE_ERROR_CODES=void 0,exports.getMimeType=l,exports.validateFilePath=p,exports.readFileInfo=m,exports.readFileAsBase64=u;const s=c(require("fs")),n=c(require("path")),f=require("./config");exports.FILE_ERROR_CODES={FILE_TOO_LARGE:"FILE_TOO_LARGE",FILE_NOT_FOUND:"FILE_NOT_FOUND",SECURITY_DENIED:"SECURITY_DENIED",NOT_A_FILE:"NOT_A_FILE",READ_ERROR:"READ_ERROR"},exports.BLOCKED_DIRS=process.platform==="win32"?["C:\\Windows","C:\\Program Files","C:\\Program Files (x86)","C:\\ProgramData"]:["/etc","/var","/usr","/bin","/sbin","/boot","/root","/sys","/proc","/dev","/lib","/lib64"];const _=n.default.basename(f.QBOT_CLAW_DIR),R={".pdf":"application/pdf",".doc":"application/msword",".docx":"application/vnd.openxmlformats-officedocument.wordprocessingml.document",".xls":"application/vnd.ms-excel",".xlsx":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",".ppt":"application/vnd.ms-powerpoint",".pptx":"application/vnd.openxmlformats-officedocument.presentationml.presentation",".txt":"text/plain",".csv":"text/csv",".rtf":"application/rtf",".md":"text/markdown",".jpg":"image/jpeg",".jpeg":"image/jpeg",".png":"image/png",".gif":"image/gif",".bmp":"image/bmp",".svg":"image/svg+xml",".webp":"image/webp",".ico":"image/x-icon",".tiff":"image/tiff",".tif":"image/tiff",".mp3":"audio/mpeg",".wav":"audio/wav",".ogg":"audio/ogg",".flac":"audio/flac",".aac":"audio/aac",".mp4":"video/mp4",".avi":"video/x-msvideo",".mov":"video/quicktime",".wmv":"video/x-ms-wmv",".webm":"video/webm",".mkv":"video/x-matroska",".zip":"application/zip",".rar":"application/vnd.rar",".7z":"application/x-7z-compressed",".tar":"application/x-tar",".gz":"application/gzip",".bz2":"application/x-bzip2",".json":"application/json",".xml":"application/xml",".html":"text/html",".htm":"text/html",".css":"text/css",".js":"application/javascript",".ts":"application/typescript",".py":"text/x-python",".java":"text/x-java-source",".c":"text/x-c",".cpp":"text/x-c++src",".h":"text/x-c",".sh":"application/x-sh",".yaml":"text/yaml",".yml":"text/yaml",".toml":"application/toml",".ini":"text/plain",".log":"text/plain",".wasm":"application/wasm",".exe":"application/x-msdownload",".dmg":"application/x-apple-diskimage",".iso":"application/x-iso9660-image"};function l(e){const a=n.default.extname(e).toLowerCase();return R[a]||"application/octet-stream"}function p(e){if(e.includes("\0"))return{valid:!1,reason:"\u8DEF\u5F84\u5305\u542B\u975E\u6CD5\u5B57\u7B26",code:exports.FILE_ERROR_CODES.SECURITY_DENIED};const a=n.default.resolve(e),t=d(a);if(!t.valid)return t;const i=E(a);if(!i.valid)return i;if(!s.default.existsSync(a))return{valid:!1,reason:`\u6587\u4EF6\u6216\u76EE\u5F55\u4E0D\u5B58\u5728: ${a}`,code:exports.FILE_ERROR_CODES.FILE_NOT_FOUND};let o;try{o=s.default.realpathSync(a)}catch{return{valid:!1,reason:"\u65E0\u6CD5\u89E3\u6790\u6587\u4EF6\u771F\u5B9E\u8DEF\u5F84",code:exports.FILE_ERROR_CODES.READ_ERROR}}if(o!==a){if(!d(o).valid)return{valid:!1,reason:"\u7981\u6B62\u8BBF\u95EE\u7CFB\u7EDF\u654F\u611F\u76EE\u5F55",code:exports.FILE_ERROR_CODES.SECURITY_DENIED};if(!E(o).valid)return{valid:!1,reason:"\u7981\u6B62\u8BBF\u95EE\u9690\u85CF\u6587\u4EF6\u6216\u76EE\u5F55",code:exports.FILE_ERROR_CODES.SECURITY_DENIED}}return{valid:!0}}function d(e){const a=e.toLowerCase();for(const t of exports.BLOCKED_DIRS)if(a===t.toLowerCase()||a.startsWith(t.toLowerCase()+n.default.sep))return{valid:!1,reason:"\u7981\u6B62\u8BBF\u95EE\u7CFB\u7EDF\u654F\u611F\u76EE\u5F55",code:exports.FILE_ERROR_CODES.SECURITY_DENIED};return{valid:!0}}function E(e){const a=e.split(n.default.sep);for(const t of a)if(t.startsWith(".")&&t!=="."&&t!==".."&&t!==_)return{valid:!1,reason:"\u7981\u6B62\u8BBF\u95EE\u9690\u85CF\u6587\u4EF6\u6216\u76EE\u5F55",code:exports.FILE_ERROR_CODES.SECURITY_DENIED};return{valid:!0}}function m(e,a){const t=p(e);if(!t.valid)return{ok:!1,path:e,reason:t.reason,code:t.code};const i=n.default.resolve(e);let o;try{o=s.default.statSync(i)}catch{return{ok:!1,path:e,reason:"\u65E0\u6CD5\u8BFB\u53D6\u6587\u4EF6\u4FE1\u606F",code:exports.FILE_ERROR_CODES.READ_ERROR}}if(o.isDirectory())return{ok:!1,path:e,reason:"\u8DEF\u5F84\u6307\u5411\u4E00\u4E2A\u76EE\u5F55\uFF0C\u4E0D\u662F\u6587\u4EF6",code:exports.FILE_ERROR_CODES.NOT_A_FILE};const r=l(i);return{ok:!0,name:n.default.basename(i),path:i,size:o.size,mimeType:r}}function u(e,a){const t=m(e,a);if(!t.ok)return t;try{const i=s.default.readFileSync(t.path);return{...t,content:i.toString("base64")}}catch(i){return{ok:!1,path:e,reason:`\u6587\u4EF6\u8BFB\u53D6\u5931\u8D25: ${i.message}`,code:exports.FILE_ERROR_CODES.READ_ERROR}}}
|
package/dist/openai-proxy.js
CHANGED
|
@@ -1,7 +1,20 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var L=exports&&exports.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.proxyApiKey=void 0,exports.handleOpenAIProxy=se;const R=L(require("crypto")),ee=L(require("http")),te=L(require("https")),Q=require("url"),o=require("./reportLog"),F=require("./api/llm-access-proxy"),oe=require("./api/auth-client"),w=require("./api/content-review");exports.proxyApiKey=R.default.randomBytes(32).toString("base64url");const ne={enabled:!0,targetBaseUrl:"https://qbqaproxy.imtt.qq.com/openapi",allowedApiKeys:[exports.proxyApiKey],timeout:12e4};function re(r){const e=r.headers.authorization;if(!e||typeof e!="string")return null;const s=e.match(/^Bearer\s+(.+)$/i);return s?s[1]:null}function m(r,e,s,c,t){const $=JSON.stringify({error:{message:s,type:c,code:t}});r.writeHead(e,{"Content-Type":"application/json","Content-Length":Buffer.byteLength($)}),r.end($)}function q(r,e,s="QBotClawRouter"){const c=Math.floor(Date.now()/1e3),t=`chatcmpl-${R.default.randomBytes(12).toString("hex")}`,$=JSON.stringify({id:t,object:"chat.completion",created:c,model:s,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 I(r,e,s="QBotClawRouter"){if(!r.writable)return;const c=Math.floor(Date.now()/1e3),t=`chatcmpl-${R.default.randomBytes(12).toString("hex")}`,$=JSON.stringify({id:t,object:"chat.completion.chunk",created:c,model:s,choices:[{index:0,delta:{role:"assistant",content:e},finish_reason:null}]});r.write(`data: ${$}
|
|
2
|
+
|
|
3
|
+
`);const k=JSON.stringify({id:t,object:"chat.completion.chunk",created:c,model:s,choices:[{index:0,delta:{},finish_reason:"stop"}]});r.write(`data: ${k}
|
|
2
4
|
|
|
3
5
|
`),r.write(`data: [DONE]
|
|
4
6
|
|
|
5
|
-
`),r.end()
|
|
6
|
-
|
|
7
|
-
`)
|
|
7
|
+
`),r.end()}function ae(){return Math.random().toString(36).substring(2,10)}function G(r){try{const e=JSON.parse(r),s=e?.choices?.[0]?.delta;if(!s)return(0,o.log)(`[extractDeltaContent] delta \u4E3A\u7A7A, choices=${JSON.stringify(e?.choices)}`),"";const c=s.content;return typeof c=="string"&&c.length>0?c:""}catch(e){return(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${e}`),""}}function ce(r,e){const s=JSON.stringify({error:{message:e,type:"content_filter_error",code:"content_review_failed"}});r.writable&&(r.write(`data: ${s}
|
|
8
|
+
|
|
9
|
+
`),r.write(`data: [DONE]
|
|
10
|
+
|
|
11
|
+
`),r.end())}function ie(r){return new Promise((e,s)=>{const c=[];r.on("data",t=>c.push(t)),r.on("end",()=>e(Buffer.concat(c))),r.on("error",s)})}async function se(r,e,s){const c=r.url||"";if(!c.startsWith("/v1/"))return!1;const t=ae(),$=r.method||"GET",k=Date.now();(0,o.log)(`[openai-proxy][${t}] \u6536\u5230\u8BF7\u6C42: ${$} ${c}`);const T=ne;if(!T.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 J=re(r);if(!J)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(!T.allowedApiKeys.includes(J))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 C;try{C=await ie(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 b=!1,f=null;if(c.includes("/chat/completions")&&C.length>0)try{f=JSON.parse(C.toString("utf-8")),b=f.stream===!0,(0,o.log)(`[openai-proxy][${t}] stream=${f.stream}`)}catch{}if(f&&c.includes("/chat/completions")&&Array.isArray(f.messages)){const n=[];for(const a of f.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 l of a.content)l.type==="text"&&typeof l.text=="string"&&l.text.length>0&&n.push(l.text)}if(n.length>0){const a=n[n.length-1];try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
|
|
12
|
+
${a}`),await(0,w.reviewText)(a,w.AuditContentType.INPUT,s),(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 _=f?.model||"QBotClawRouter",B=l.fallbackMsg||"\u8F93\u5165\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";return b?(e.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),I(e,B,_)):q(e,B,_),!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 U=new Q.URL(T.targetBaseUrl),W=U.pathname.replace(/\/+$/,""),Y=c.replace(/^\/v1/,""),v=new Q.URL(W+Y,U.origin),H=v.protocol==="https:",z=H?te.default:ee.default,V=H?443:80,E={};for(const[n,a]of Object.entries(r.headers)){if(!a)continue;const l=n.toLowerCase();["host","connection","keep-alive","transfer-encoding","upgrade"].includes(l)||(E[n]=a)}E.host=v.host;let N;try{N=await(0,F.fetchLlmToken)(s),(0,o.log)(`[openai-proxy][${t}] \u6210\u529F\u83B7\u53D6 LLM Token ${N}`)}catch(n){if(n instanceof F.TokenExhaustedError){(0,o.log)(`[openai-proxy][${t}] Token \u5DF2\u8017\u5C3D: ${n.message}`);const l=f?.model||"QBotClawRouter",_=n.message||"\u5F53\u65E5Token\u5DF2\u8017\u5C3D\uFF0C\u8BF7\u5207\u6362\u6A21\u578B\u6216\u660E\u65E5\u518D\u8BD5";return b?(e.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),I(e,_,l)):q(e,_,l),!0}if(n instanceof oe.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 a=n instanceof Error?n.message:JSON.stringify(n);return(0,o.log)(`[openai-proxy][${t}] \u83B7\u53D6 LLM Token \u5931\u8D25: ${a}`),m(e,500,`Failed to obtain auth token: ${a}`,"server_error","token_fetch_error"),!0}E.authorization=`Bearer ${N}`;const X={hostname:v.hostname,port:v.port||V,path:v.pathname+v.search,method:$,headers:E,timeout:T.timeout||12e4};(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u5230: ${$} ${v.href}`);try{const n=C.toString("utf-8");(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u4F53: ${n}`)}catch{}const O=z.request(X,n=>{const a=n.statusCode||502,l=n.headers["content-type"]||"",_=l.includes("text/event-stream")||b,B=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u76EE\u6807\u54CD\u5E94: ${a} ${l} (${B}ms)${_?" [SSE\u6D41\u5F0F]":""} (isStreamRequest=${b})`);const D={};for(const[g,y]of Object.entries(n.headers)){if(!y)continue;const p=g.toLowerCase();["connection","keep-alive","upgrade"].includes(p)||(D[g]=y)}const j=b&&!l.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=${l})\uFF0C\u964D\u7EA7\u4E3A\u975E\u6D41\u5F0F\u5904\u7406`),_&&!j){e.writeHead(a,D);let g="",y="",p="",S=!1;const M=100,h=async(i,u)=>{if(!i||S)return;const x=w.AuditContentType.OUTPUT;try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838${u?"(\u6700\u7EC8)":""}\uFF0C\u6587\u672C\u957F\u5EA6: ${i.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
|
|
13
|
+
${i}`),await(0,w.reviewText)(i,x,s),(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(d){if(d instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${d.fallbackMsg}`),S=!0;const K=d.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";I(e,`
|
|
14
|
+
|
|
15
|
+
[${K}]`,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: ${d.message||JSON.stringify(d)}`)}};n.on("data",i=>{if(S){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA1\u6838\u5DF2\u4E2D\u65AD\uFF0C\u8DF3\u8FC7chunk (${i.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 (${i.length}\u5B57\u8282)`);return}const u=i.toString("utf-8");p+=u,p=p.replace(/\r\n/g,`
|
|
16
|
+
`).replace(/\r/g,`
|
|
17
|
+
`);const x=p.split(`
|
|
18
|
+
`);p=x.pop()||"";for(let d=0;d<x.length;d++){const A=x[d].trim();if(A&&A.startsWith("data: ")&&A!=="data: [DONE]"){const Z=A.slice(6),P=G(Z);P&&(g+=P,y+=P)}}if(e.write(i),g.length>=M){const d=g;g="",(0,o.log)(`[openai-proxy][${t}] [SSE] \u89E6\u53D1\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${d.length} ${d}`),h(d,!1)}}),n.on("end",async()=>{if(p.trim()){const i=p.trim();if(i.startsWith("data: ")&&i!=="data: [DONE]"){const u=G(i.slice(6));u&&(g+=u,y+=u)}}if(y.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 (${y.length}\u5B57\u7B26):
|
|
19
|
+
${y}`),g.length>0&&!S&&await h(g,!0),!S){if(y.length===0){(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),I(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",f?.model||"QBotClawRouter");return}const i=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${i}ms`),e.end()}}),n.on("error",i=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${i.message}`),e.headersSent?S||e.end():m(e,502,`Bad Gateway: ${i.message}`,"server_error","proxy_error")})}else{const g=c.includes("/chat/completions"),y=[];n.on("data",p=>{y.push(p)}),n.on("end",async()=>{const p=Buffer.concat(y),S=p.toString("utf-8");if(a>=400&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u9519\u8BEF (status=${a}): ${S.substring(0,1e3)}`),g&&a>=200&&a<300)try{const h=JSON.parse(p.toString("utf-8")),i=[];if(Array.isArray(h.choices))for(const u of h.choices){const x=u.message?.content;typeof x=="string"&&x.length>0&&i.push(x)}if(i.length===0){if((0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u26A0\uFE0F \u6A21\u578B\u8F93\u51FA\u5185\u5BB9\u4E3A\u7A7A\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),e.headersSent)e.end();else{const u=f?.model||h?.model||"QBotClawRouter";q(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",u)}return}if(i.length>0){const u=i.join(`
|
|
20
|
+
`);(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${u.length}`),await(0,w.reviewText)(u,w.AuditContentType.OUTPUT,s),(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}}catch(h){if(h instanceof w.ContentReviewFailedError){if((0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${h.fallbackMsg}`),e.headersSent)e.end();else{const i=f?.model||"QBotClawRouter";q(e,h.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002",i)}return}(0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${h.message||h}`)}e.headersSent||e.writeHead(a,D),e.end(p);const M=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${M}ms`)}),n.on("error",p=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${p.message}`),e.headersSent?e.end():m(e,502,`Bad Gateway: ${p.message}`,"server_error","proxy_error")})}});return O.on("error",n=>{const a=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u5931\u8D25 (${a}ms): ${n.message}`),e.headersSent?e.end():m(e,502,`Bad Gateway: ${n.message}`,"server_error","proxy_error")}),O.on("timeout",()=>{const n=Date.now()-k;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u8D85\u65F6 (${n}ms)`),O.destroy(),e.headersSent?e.end():m(e,504,"Gateway Timeout","server_error","gateway_timeout")}),O.end(C),!0}
|