@qqbrowser/qbot-claw-launcher 0.10.10 → 0.10.11
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/api/content-review.js +5 -2
- package/dist/api/content-review.js.map +1 -1
- package/dist/build.js +1 -1
- package/dist/filePicker.js +14 -12
- package/dist/openai-proxy.js +10 -12
- package/package.json +1 -1
|
@@ -34,10 +34,12 @@ class ContentReviewApi extends trpc_1.TrpcApi {
|
|
|
34
34
|
}
|
|
35
35
|
exports.contentReviewApi = new ContentReviewApi();
|
|
36
36
|
class ContentReviewFailedError extends Error {
|
|
37
|
-
constructor(fallbackMsg) {
|
|
37
|
+
constructor(fallbackMsg, is_show, phase_action) {
|
|
38
38
|
super('内容审核未通过');
|
|
39
39
|
this.name = 'ContentReviewFailedError';
|
|
40
40
|
this.fallbackMsg = fallbackMsg;
|
|
41
|
+
this.is_show = is_show;
|
|
42
|
+
this.phase_action = phase_action;
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
exports.ContentReviewFailedError = ContentReviewFailedError;
|
|
@@ -64,8 +66,9 @@ const reviewText = async (text, auditContentType, authOrLoginInfo) => {
|
|
|
64
66
|
else {
|
|
65
67
|
rsp = await exports.contentReviewApi.getReviewResult(body, authOrLoginInfo);
|
|
66
68
|
}
|
|
69
|
+
(0, reportLog_1.log)(`[content-review] rsp: ${JSON.stringify(rsp)}`);
|
|
67
70
|
if (!rsp.pass) {
|
|
68
|
-
throw new ContentReviewFailedError(rsp.fallback_msg);
|
|
71
|
+
throw new ContentReviewFailedError(rsp.fallback_msg, rsp.is_show, rsp.phase_action);
|
|
69
72
|
}
|
|
70
73
|
return rsp.session_id;
|
|
71
74
|
};
|
|
@@ -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,gB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AAC,CAAC;IAChK,CAAC;IAED,MAAM,YAAY,GAAG,MAAM,IAAA,uBAAe,EAAC,UAAU,CAAC,CAAC;IACvD,IAAA,eAAG,EAAC,kCAAkC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC;IAEtE,MAAM,IAAI,GAAuB;QAC/B,cAAc,EAAE,YAAY;QAC5B,UAAU,EAAE,SAAS;QACrB,IAAI;QACJ,kBAAkB,EAAE,gBAAgB;KACrC,CAAC;IAEF,IAAI,GAAuB,CAAC;IAE5B,IAAI,UAAU,EAAE,CAAC;QAEf,GAAG,GAAG,MAAM,UAAU,CAAC,YAAY,CACjC,CAAC,SAAS,EAAE,YAAY,EAAE,EAAE,CAAC,wBAAgB,CAAC,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE,YAAY,CAAC,CAC7F,CAAC;IACJ,CAAC;SAAM,CAAC;QAEN,GAAG,GAAG,MAAM,wBAAgB,CAAC,eAAe,CAAC,IAAI,EAAE,eAAwC,CAAC,CAAC;IAC/F,CAAC;IAED,IAAA,eAAG,EAAC,yBAAyB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACpD,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;QACd,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC,YAAY,EAAE,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC;IACtF,CAAC;IAED,OAAO,GAAG,CAAC,UAAU,CAAC;AACxB,CAAC,CAAC;AA3CW,QAAA,UAAU,cA2CrB"}
|
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.11",exports.IS_DEBUG_BUILD=!1;
|
package/dist/filePicker.js
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.isFilePickerBusy=P,exports.showFilePicker=O;const
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.isFilePickerBusy=P,exports.showFilePicker=O;const d=require("child_process"),w=3e5;let h=!1;function P(){return h}async function O(l={}){if(h)throw new Error("\u5DF2\u6709\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u6B63\u5728\u663E\u793A");h=!0;try{const o=process.platform;switch(o){case"darwin":return await S(l);case"win32":return await k(l);case"linux":return await E(l);default:throw new Error(`\u4E0D\u652F\u6301\u7684\u64CD\u4F5C\u7CFB\u7EDF\u5E73\u53F0: ${o}`)}}finally{h=!1}}function S(l){const{extensions:o=[],multiple:a=!1,title:u="\u9009\u62E9\u6587\u4EF6",allowDirectory:m=!1}=l;let e;if(m)e=`choose folder with prompt "${y(u)}"`,a&&(e+=" with multiple selections allowed");else if(e=`choose file with prompt "${y(u)}"`,a&&(e+=" with multiple selections allowed"),o.length>0){const i=o.map(t=>`"${t.replace(/^\./,"")}"`).join(", ");e+=` of type {${i}}`}const n=a?`set selectedItems to (${e})
|
|
2
2
|
set posixPaths to ""
|
|
3
3
|
repeat with anItem in selectedItems
|
|
4
4
|
set posixPaths to posixPaths & POSIX path of anItem & linefeed
|
|
5
5
|
end repeat
|
|
6
6
|
return posixPaths`:`set selectedItem to (${e})
|
|
7
|
-
return POSIX path of selectedItem`;return new Promise((
|
|
8
|
-
`).map(
|
|
7
|
+
return POSIX path of selectedItem`;return new Promise((i,t)=>{const c=(0,d.execFile)("osascript",["-e",n],{timeout:w},(s,p,r)=>{if(s){if(s.killed){t(new Error("\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u8D85\u65F6\uFF0860\u79D2\uFF09"));return}if(r&&r.includes("User canceled")){i(null);return}if(s.code===1){i(null);return}t(new Error(`macOS \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${r||s.message}`));return}const f=p.trim().split(`
|
|
8
|
+
`).map(g=>g.trim()).filter(Boolean);i(f.length>0?f:null)})})}function y(l){return l.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}function k(l){const{extensions:o=[],multiple:a=!1,title:u="\u9009\u62E9\u6587\u4EF6",allowDirectory:m=!1}=l;let e;const n="[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;";if(m)e=`
|
|
9
|
+
${n}
|
|
9
10
|
Add-Type -AssemblyName System.Windows.Forms
|
|
10
11
|
$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
|
|
11
|
-
$dialog.Description = '${
|
|
12
|
+
$dialog.Description = '${$(u)}'
|
|
12
13
|
$dialog.ShowNewFolderButton = $true
|
|
13
14
|
$result = $dialog.ShowDialog()
|
|
14
15
|
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
|
|
15
16
|
Write-Output $dialog.SelectedPath
|
|
16
17
|
} else {
|
|
17
18
|
Write-Output ''
|
|
18
|
-
}`;else{let i="\u6240\u6709\u6587\u4EF6 (*.*)|*.*";if(
|
|
19
|
+
}`;else{let i="\u6240\u6709\u6587\u4EF6 (*.*)|*.*";if(o.length>0){const t=o.map(s=>`*${s.startsWith(".")?s:"."+s}`).join(";");i=`\u5141\u8BB8\u7684\u6587\u4EF6\u7C7B\u578B (${o.map(s=>s.startsWith(".")?s:"."+s).join(", ")})|${t}|\u6240\u6709\u6587\u4EF6 (*.*)|*.*`}e=`
|
|
20
|
+
${n}
|
|
19
21
|
Add-Type -AssemblyName System.Windows.Forms
|
|
20
22
|
$dialog = New-Object System.Windows.Forms.OpenFileDialog
|
|
21
|
-
$dialog.Title = '${
|
|
22
|
-
$dialog.Filter = '${
|
|
23
|
+
$dialog.Title = '${$(u)}'
|
|
24
|
+
$dialog.Filter = '${$(i)}'
|
|
23
25
|
$dialog.Multiselect = $${a?"true":"false"}
|
|
24
26
|
$result = $dialog.ShowDialog()
|
|
25
27
|
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
|
|
26
28
|
$dialog.FileNames | ForEach-Object { Write-Output $_ }
|
|
27
29
|
} else {
|
|
28
30
|
Write-Output ''
|
|
29
|
-
}`}return new Promise((i,
|
|
30
|
-
`).map(
|
|
31
|
-
`),!
|
|
32
|
-
`).map(
|
|
33
|
-
`).map(
|
|
31
|
+
}`}return new Promise((i,t)=>{(0,d.execFile)("powershell.exe",["-NoProfile","-NonInteractive","-Command",e],{timeout:w,windowsHide:!0,encoding:"utf8"},(c,s,p)=>{if(c){if(c.killed){t(new Error("\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u8D85\u65F6\uFF0860\u79D2\uFF09"));return}t(new Error(`Windows \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${p||c.message}`));return}const r=s.trim();if(!r){i(null);return}const f=r.split(`
|
|
32
|
+
`).map(g=>g.trim()).filter(Boolean);i(f.length>0?f:null)})})}function $(l){return l.replace(/'/g,"''")}async function E(l){if(await F("zenity"))return W(l);if(await F("kdialog"))return D(l);throw new Error("Linux \u7CFB\u7EDF\u672A\u5B89\u88C5 zenity \u6216 kdialog\uFF0C\u65E0\u6CD5\u5F39\u51FA\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u3002\u8BF7\u5B89\u88C5\u5176\u4E2D\u4E4B\u4E00\uFF1Asudo apt install zenity \u6216 sudo apt install kdialog")}function F(l){return new Promise(o=>{(0,d.execFile)("which",[l],a=>{o(!a)})})}function W(l){const{extensions:o=[],multiple:a=!1,title:u="\u9009\u62E9\u6587\u4EF6",allowDirectory:m=!1}=l,e=["--file-selection",`--title=${u}`];if(m&&e.push("--directory"),a&&e.push("--multiple",`--separator=
|
|
33
|
+
`),!m&&o.length>0){const n=o.map(i=>`*${i.startsWith(".")?i:"."+i}`).join(" ");e.push(`--file-filter=\u5141\u8BB8\u7684\u6587\u4EF6\u7C7B\u578B | ${n}`),e.push("--file-filter=\u6240\u6709\u6587\u4EF6 | *")}return new Promise((n,i)=>{(0,d.execFile)("zenity",e,{timeout:w},(t,c,s)=>{if(t){if(t.killed){i(new Error("\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u8D85\u65F6\uFF0860\u79D2\uFF09"));return}if(t.code===1){n(null);return}i(new Error(`zenity \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${s||t.message}`));return}const p=c.trim();if(!p){n(null);return}const r=p.split(`
|
|
34
|
+
`).map(f=>f.trim()).filter(Boolean);n(r.length>0?r:null)})})}function D(l){const{extensions:o=[],multiple:a=!1,title:u="\u9009\u62E9\u6587\u4EF6",allowDirectory:m=!1}=l;let e;if(m)e=["--getexistingdirectory",process.env.HOME||"/"];else{let n="";o.length>0&&(n=`${o.map(t=>`*${t.startsWith(".")?t:"."+t}`).join(" ")} | \u5141\u8BB8\u7684\u6587\u4EF6\u7C7B\u578B`),a?e=["--getopenfilename",process.env.HOME||"/",n,"--multiple","--separate-output"]:e=["--getopenfilename",process.env.HOME||"/",n]}return e.push("--title",u),new Promise((n,i)=>{(0,d.execFile)("kdialog",e,{timeout:w},(t,c,s)=>{if(t){if(t.killed){i(new Error("\u6587\u4EF6\u9009\u62E9\u5BF9\u8BDD\u6846\u8D85\u65F6\uFF0860\u79D2\uFF09"));return}if(t.code===1){n(null);return}i(new Error(`kdialog \u6587\u4EF6\u9009\u62E9\u5931\u8D25: ${s||t.message}`));return}const p=c.trim();if(!p){n(null);return}const r=p.split(`
|
|
35
|
+
`).map(f=>f.trim()).filter(Boolean);n(r.length>0?r:null)})})}
|
package/dist/openai-proxy.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
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=Se;const G=J(require("crypto")),fe=J(require("http")),de=J(require("https")),te=require("url"),o=require("./reportLog"),oe=require("./api/llm-access-proxy"),ge=require("./api/auth-client"),w=require("./api/content-review");exports.proxyApiKey=G.default.randomBytes(32).toString("base64url");const he={enabled:!0,targetBaseUrl:"https://qbqaproxy.imtt.qq.com/openapi",allowedApiKeys:[exports.proxyApiKey],timeout:12e4},p={INVALID_REQUEST:"invalid_request_error",AUTH:"authentication_error",SERVER:"server_error",CONTENT_FILTER:"content_filter",INSUFFICIENT_QUOTA:"insufficient_quota",UPSTREAM:"upstream_error"},u={NOT_FOUND:"not_found",MISSING_API_KEY:"missing_api_key",INVALID_API_KEY:"invalid_api_key",BAD_REQUEST:"bad_request",AUTH_EXPIRED:"auth_expired",TOKEN_FETCH_ERROR:"token_fetch_error",PROXY_ERROR:"proxy_error",GATEWAY_TIMEOUT:"gateway_timeout",CONTENT_REVIEW_FAILED_INPUT:"content_review_failed_input",CONTENT_REVIEW_FAILED_OUTPUT:"content_review_failed_output",CONTENT_REVIEW_FAILED_OUTPUT_PARTIAL:"content_review_failed_output_partial",TOKEN_EXHAUSTED:"token_exhausted",UPSTREAM_UNAVAILABLE:"upstream_unavailable"};function ye(r){const e=r.headers.authorization;if(!e||typeof e!="string")return null;const i=e.match(/^Bearer\s+(.+)$/i);return i?i[1]:null}function _(r,e,i,l,t,h){const y=JSON.stringify({error:{message:i,type:l,code:t,...h||{}}});r.writeHead(e,{"Content-Type":"application/json","Content-Length":Buffer.byteLength(y)}),r.end(y)}function Ee(r,e,i="QBotClawRouter"){const l=Math.floor(Date.now()/1e3),t=`chatcmpl-${G.default.randomBytes(12).toString("hex")}`,h=JSON.stringify({id:t,object:"chat.completion",created:l,model:i,choices:[{index:0,message:{role:"assistant",content:e},finish_reason:"stop"}],usage:{prompt_tokens:0,completion_tokens:0,total_tokens:0}});r.writeHead(200,{"Content-Type":"application/json","Content-Length":Buffer.byteLength(h)}),r.end(h)}function _e(r,e,i="QBotClawRouter"){if(!r.writable)return;const l=Math.floor(Date.now()/1e3),t=`chatcmpl-${G.default.randomBytes(12).toString("hex")}`;r.write(`data: ${JSON.stringify({id:t,object:"chat.completion.chunk",created:l,model:i,choices:[{index:0,delta:{role:"assistant",content:e},finish_reason:null}]})}
|
|
2
2
|
|
|
3
|
-
`)
|
|
3
|
+
`),r.write(`data: ${JSON.stringify({id:t,object:"chat.completion.chunk",created:l,model:i,choices:[{index:0,delta:{},finish_reason:"stop"}]})}
|
|
4
4
|
|
|
5
5
|
`),r.write(`data: [DONE]
|
|
6
6
|
|
|
7
|
-
`),r.end()}function
|
|
7
|
+
`),r.end()}function Te(){return Math.random().toString(36).substring(2,10)}function ne(r){const e=r?.choices?.[0]?.delta;if(!e)return(0,o.log)(`[extractDeltaContent] delta \u4E3A\u7A7A, choices=${JSON.stringify(r?.choices)}`),{content:"",hasToolCalls:!1};const i=e.content,l=Array.isArray(e.tool_calls)&&e.tool_calls.length>0||e.function_call!=null;return{content:typeof i=="string"&&i.length>0?i:"",hasToolCalls:l}}function $e(r){try{const e=JSON.parse(r);return ne(e)}catch(e){return(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${e}`),{content:"",hasToolCalls:!1}}}function k(r,e,i,l,t){if(!r.writable)return;const h={message:e,type:i,code:l,...t||{}},y=JSON.stringify({error:h});r.write(`data: ${y}
|
|
8
8
|
|
|
9
9
|
`),r.write(`data: [DONE]
|
|
10
10
|
|
|
11
|
-
`),r.end())}function
|
|
12
|
-
${a}`),await(0,
|
|
13
|
-
${
|
|
14
|
-
|
|
15
|
-
[${W}]`,d?.model||"QBotClawRouter"),n.destroy();return}(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${h.message||JSON.stringify(h)}`)}};n.on("data",s=>{if(x){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA1\u6838\u5DF2\u4E2D\u65AD\uFF0C\u8DF3\u8FC7chunk (${s.length}\u5B57\u8282)`);return}if(!e.writable){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA2\u6237\u7AEF\u8FDE\u63A5\u5DF2\u5173\u95ED(res.writable=false)\uFF0C\u8DF3\u8FC7chunk (${s.length}\u5B57\u8282)`);return}const $=s.toString("utf-8");p+=$,p=p.replace(/\r\n/g,`
|
|
11
|
+
`),r.end()}function re(r,e){const{isStream:i,httpStatus:l,message:t,type:h,code:y,extra:O}=e;if(!i){_(r,l,t,h,y,O);return}if(!r.headersSent){_(r,l,t,h,y,O);return}k(r,t,h,y,O)}function me(r){return new Promise((e,i)=>{const l=[];r.on("data",t=>l.push(t)),r.on("end",()=>e(Buffer.concat(l))),r.on("error",i)})}async function Se(r,e,i){const l=r.url||"";if(!l.startsWith("/v1/"))return!1;const t=Te(),h=r.method||"GET",y=Date.now();(0,o.log)(`[openai-proxy][${t}] \u6536\u5230\u8BF7\u6C42: ${h} ${l}`);const O=he;if(!O.enabled)return(0,o.log)(`[openai-proxy][${t}] \u4EE3\u7406\u672A\u542F\u7528\uFF0C\u8FD4\u56DE 404`),_(e,404,"Not Found",p.INVALID_REQUEST,u.NOT_FOUND),!0;const W=ye(r);if(!W)return(0,o.log)(`[openai-proxy][${t}] \u7F3A\u5C11 API Key`),_(e,401,"You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth.",p.INVALID_REQUEST,u.MISSING_API_KEY),!0;if(!O.allowedApiKeys.includes(W))return(0,o.log)(`[openai-proxy][${t}] API Key \u6821\u9A8C\u5931\u8D25`),_(e,401,"Incorrect API key provided. You can check your API key in the proxy configuration.",p.INVALID_REQUEST,u.INVALID_API_KEY),!0;let D;try{D=await me(r)}catch(n){return(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u8BF7\u6C42\u4F53\u5931\u8D25: ${n.message}`),_(e,400,"Failed to read request body",p.INVALID_REQUEST,u.BAD_REQUEST),!0}let x=!1,A=null;if(l.includes("/chat/completions")&&D.length>0)try{A=JSON.parse(D.toString("utf-8")),x=A.stream===!0,(0,o.log)(`[openai-proxy][${t}] stream=${A.stream}`)}catch{}let q=!1;if(Array.isArray(A?.messages)){const n=A.messages;let a=-1;for(let s=n.length-1;s>=0;s--)if(n[s].role==="user"){a=s;break}a>=0&&(q=n.slice(a+1).some(s=>s.role==="tool"))}if(q&&(0,o.log)(`[openai-proxy][${t}] \u6700\u540E\u4E00\u6761\u7528\u6237\u6D88\u606F\u4E4B\u540E\u5B58\u5728 tool \u6D88\u606F\uFF0C\u8DF3\u8FC7\u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF08\u5DE5\u5177\u8C03\u7528\u540E\u7EED\u8BF7\u6C42\uFF09`),A&&l.includes("/chat/completions")&&Array.isArray(A.messages)&&!q){const n=[];for(const a of A.messages)if(a.role==="user"){if(typeof a.content=="string"&&a.content.length>0)n.push(a.content);else if(Array.isArray(a.content))for(const s of a.content)s.type==="text"&&typeof s.text=="string"&&s.text.length>0&&n.push(s.text)}if(n.length>0){const a=n[n.length-1];try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${a.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
|
|
12
|
+
${a}`),await(0,w.reviewText)(a,w.AuditContentType.INPUT,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}catch(s){if(s instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${s.fallbackMsg}`);const B=s.fallbackMsg||"\u8F93\u5165\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";return re(e,{isStream:x,httpStatus:400,message:B,type:p.CONTENT_FILTER,code:u.CONTENT_REVIEW_FAILED_INPUT,extra:{phase_action:s.phase_action,is_show:s.is_show}}),!0}(0,o.log)(`[openai-proxy][${t}] \u8F93\u5165\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${s.message||JSON.stringify(s)}`)}}}const Q=new te.URL(O.targetBaseUrl),ae=Q.pathname.replace(/\/+$/,""),se=l.replace(/^\/v1/,""),C=new te.URL(ae+se,Q.origin),X=C.protocol==="https:",ie=X?de.default:fe.default,le=X?443:80,L={};for(const[n,a]of Object.entries(r.headers)){if(!a)continue;const s=n.toLowerCase();["host","connection","keep-alive","transfer-encoding","upgrade"].includes(s)||(L[n]=a)}L.host=C.host;let H;try{H=await(0,oe.fetchLlmToken)(i),(0,o.log)(`[openai-proxy][${t}] \u6210\u529F\u83B7\u53D6 LLM Token ${H}`)}catch(n){if(n instanceof oe.TokenExhaustedError){(0,o.log)(`[openai-proxy][${t}] Token \u5DF2\u8017\u5C3D: ${n.message}`);const s=n.message||"\u5F53\u65E5Token\u5DF2\u8017\u5C3D\uFF0C\u8BF7\u5207\u6362\u6A21\u578B\u6216\u660E\u65E5\u518D\u8BD5";return re(e,{isStream:x,httpStatus:429,message:s,type:p.INSUFFICIENT_QUOTA,code:u.TOKEN_EXHAUSTED}),!0}if(n instanceof ge.AuthExpiredError)return(0,o.log)(`[openai-proxy][${t}] \u7968\u636E\u8FC7\u671F: ${n.message}`),_(e,401,`Auth token expired: ${n.message}`,p.AUTH,u.AUTH_EXPIRED),!0;const a=n instanceof Error?n.message:JSON.stringify(n);return(0,o.log)(`[openai-proxy][${t}] \u83B7\u53D6 LLM Token \u5931\u8D25: ${a}`),_(e,500,`Failed to obtain auth token: ${a}`,p.SERVER,u.TOKEN_FETCH_ERROR),!0}L.authorization=`Bearer ${H}`;const ce={hostname:C.hostname,port:C.port||le,path:C.pathname+C.search,method:h,headers:L,timeout:O.timeout||12e4};(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u5230: ${h} ${C.href}`);try{const n=D.toString("utf-8");(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u4F53: ${n.length>500?n.substring(0,500)+`...(truncated, total ${n.length} chars)`:n}`)}catch{}const v=ie.request(ce,n=>{const a=n.statusCode||502,s=n.headers["content-type"]||"",B=s.includes("text/event-stream")||x,pe=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u76EE\u6807\u54CD\u5E94: ${a} ${s} (${pe}ms)${B?" [SSE\u6D41\u5F0F]":""} (isStreamRequest=${x})`);const K={};for(const[T,E]of Object.entries(n.headers)){if(!E)continue;const f=T.toLowerCase();["connection","keep-alive","upgrade"].includes(f)||(K[T]=E)}const j=x&&!s.includes("text/event-stream")&&a>=400;if(j&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u975ESSE\u9519\u8BEF\u54CD\u5E94 (status=${a}, content-type=${s})\uFF0C\u964D\u7EA7\u4E3A\u975E\u6D41\u5F0F\u5904\u7406`),B&&!j){e.writeHead(a,K);let T="",E="",f="",$=!1,d=!1,M=!1;const g=100;let R=[],m=!1,N=!1,z=Promise.resolve();const Y=()=>{if(!(!e.writable||$)){for(const c of R)e.write(c);R.length>0&&(m=!0),R=[]}},Z=async(c,U)=>{if(!c||$)return;const b=w.AuditContentType.OUTPUT;try{(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838${U?"(\u6700\u7EC8)":""}\uFF0C\u6587\u672C\u957F\u5EA6: ${c.length}\uFF0C\u9001\u5BA1\u5185\u5BB9:
|
|
13
|
+
${c}`),await(0,w.reviewText)(c,b,i),(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7\uFF0Cflush ${R.length} \u4E2A\u7F13\u5B58chunk`),Y()}catch(S){if(S instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${S.fallbackMsg}`),$=!0,R=[];const I=S.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002";m?((0,o.log)(`[openai-proxy][${t}] [SSE] \u5DF2\u6709\u5185\u5BB9\u88ABflush\uFF0C\u8FFD\u52A0\u6807\u51C6 error`),k(e,I,p.CONTENT_FILTER,u.CONTENT_REVIEW_FAILED_OUTPUT_PARTIAL,{phase_action:S.phase_action,is_show:S.is_show})):((0,o.log)(`[openai-proxy][${t}] [SSE] \u5C1A\u672Aflush\u4EFB\u4F55\u5185\u5BB9\uFF0C\u8FD4\u56DE\u6807\u51C6 error`),k(e,I,p.CONTENT_FILTER,u.CONTENT_REVIEW_FAILED_OUTPUT,{phase_action:S.phase_action,is_show:S.is_show})),n.destroy();return}(0,o.log)(`[openai-proxy][${t}] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF0C\u76F4\u63A5flush\uFF09: ${S.message||JSON.stringify(S)}`),Y()}};n.on("data",c=>{if($){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA1\u6838\u5DF2\u4E2D\u65AD\uFF0C\u8DF3\u8FC7chunk (${c.length}\u5B57\u8282)`);return}if(!e.writable){(0,o.log)(`[openai-proxy][${t}] [SSE] \u5BA2\u6237\u7AEF\u8FDE\u63A5\u5DF2\u5173\u95ED(res.writable=false)\uFF0C\u8DF3\u8FC7chunk (${c.length}\u5B57\u8282)`);return}const U=c.toString("utf-8");f+=U,f=f.replace(/\r\n/g,`
|
|
16
14
|
`).replace(/\r/g,`
|
|
17
|
-
`);const
|
|
18
|
-
`);
|
|
19
|
-
${
|
|
20
|
-
`);(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${
|
|
15
|
+
`);const b=f.split(`
|
|
16
|
+
`);f=b.pop()||"";let S=!1;for(let I=0;I<b.length;I++){const F=b[I].trim();if(F){if(F==="data: [DONE]")M=!0;else if(F.startsWith("data: ")){const ee=F.slice(6);try{const P=JSON.parse(ee);if(P?.type==="upstream_error"||P?.error?.type==="upstream_error"){const ue=P.reqid||P.error?.reqid;(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${ue})\uFF0C\u539F\u59CB\u56DE\u5305: ${ee}\uFF0C\u8FD4\u56DE\u6807\u51C6 error`),$=!0,R=[],k(e,"\u5F53\u524D\u4F7F\u7528\u4EBA\u6570\u8F83\u591A\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",p.UPSTREAM,u.UPSTREAM_UNAVAILABLE),n.destroy(),S=!0;break}const V=ne(P);V.content&&(T+=V.content,E+=V.content),V.hasToolCalls&&(d=!0)}catch(P){(0,o.log)(`[extractDeltaContent] JSON\u89E3\u6790\u5931\u8D25: ${P}`)}}}}if(!S&&(M&&E.length===0&&!d?(0,o.log)(`[openai-proxy][${t}] [SSE] \u68C0\u6D4B\u5230 [DONE] \u4E14\u8F93\u51FA\u4E3A\u7A7A\uFF0C\u6682\u4E0D\u7F13\u5B58\uFF0C\u7B49\u5F85 end \u4E8B\u4EF6\u5904\u7406`):R.push(c),T.length>=g&&!N)){const I=T;T="",(0,o.log)(`[openai-proxy][${t}] [SSE] \u89E6\u53D1\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${I.length} ${I}`),N=!0,n.pause(),z=Z(I,!1).finally(()=>{N=!1,$||n.resume()})}}),n.on("end",async()=>{if(N&&((0,o.log)(`[openai-proxy][${t}] [SSE] end\u4E8B\u4EF6\u89E6\u53D1\uFF0C\u7B49\u5F85\u8FDB\u884C\u4E2D\u7684\u5BA1\u6838\u5B8C\u6210...`),await z),f.trim()){const c=f.trim();if(c.startsWith("data: ")&&c!=="data: [DONE]"){const U=$e(c.slice(6));U.content&&(T+=U.content,E+=U.content),U.hasToolCalls&&(d=!0)}}if(E.length===0?(0,o.log)(`[openai-proxy][${t}] [SSE] \u26A0\uFE0F \u6A21\u578B\u8F93\u51FA\u5185\u5BB9\u4E3A\u7A7A\uFF01\u8BF7\u68C0\u67E5\u4E0A\u65B9\u65E5\u5FD7\u4E2D\u7684\u539F\u59CBchunk\u6570\u636E`):(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u5185\u5BB9 (${E.length}\u5B57\u7B26):
|
|
17
|
+
${E}`),T.length>0&&!$&&await Z(T,!0),!$){if(Y(),E.length===0&&!d){(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4E14\u65E0tool_calls\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),_e(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",A?.model||"QBotClawRouter");return}E.length===0&&d&&(0,o.log)(`[openai-proxy][${t}] [SSE] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4F46\u5305\u542Btool_calls\uFF08\u51FD\u6570\u8C03\u7528\u4E2D\u95F4\u8F6E\uFF09\uFF0C\u6B63\u5E38\u7ED3\u675F`);const c=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${c}ms`),e.end()}}),n.on("error",c=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${c.message}`),e.headersSent?$||k(e,`Bad Gateway: ${c.message}`,p.SERVER,u.PROXY_ERROR):_(e,502,`Bad Gateway: ${c.message}`,p.SERVER,u.PROXY_ERROR)})}else{const T=l.includes("/chat/completions"),E=[];n.on("data",f=>{E.push(f)}),n.on("end",async()=>{const f=Buffer.concat(E),$=f.toString("utf-8");a>=400&&(0,o.log)(`[openai-proxy][${t}] \u26A0\uFE0F \u4E0A\u6E38\u8FD4\u56DE\u9519\u8BEF (status=${a}): ${$.substring(0,1e3)}`);let d=null;if(T)try{d=JSON.parse($)}catch{}if(d?.type==="upstream_error"||d?.error?.type==="upstream_error"){const g=d.reqid||d.error?.reqid;(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u68C0\u6D4B\u5230\u4E0A\u6E38 upstream_error (reqid=${g})\uFF0C\u539F\u59CB\u56DE\u5305: ${$.substring(0,2e3)}\uFF0C\u8FD4\u56DE\u6807\u51C6 error`),e.headersSent?e.end():_(e,503,"\u5F53\u524D\u4F7F\u7528\u4EBA\u6570\u8F83\u591A\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",p.UPSTREAM,u.UPSTREAM_UNAVAILABLE);return}if(T&&a>=200&&a<300&&d)try{const g=[];if(Array.isArray(d.choices))for(const m of d.choices){const N=m.message?.content;typeof N=="string"&&N.length>0&&g.push(N)}const R=Array.isArray(d.choices)&&d.choices.some(m=>Array.isArray(m.message?.tool_calls)&&m.message.tool_calls.length>0||m.message?.function_call!=null);if(g.length===0&&!R){if((0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u26A0\uFE0F \u6A21\u578B\u8F93\u51FA\u5185\u5BB9\u4E3A\u7A7A\u4E14\u65E0tool_calls\uFF0C\u8FD4\u56DE\u7EDF\u4E00\u63D0\u793A`),e.headersSent)e.end();else{const m=A?.model||d?.model||"QBotClawRouter";Ee(e,"\u62B1\u6B49\uFF0C\u6682\u65F6\u65E0\u6CD5\u56DE\u7B54\u8BE5\u95EE\u9898",m)}return}if(g.length===0&&R&&(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u6A21\u578B\u8F93\u51FA\u4E3A\u7A7A\u4F46\u5305\u542Btool_calls\uFF08\u51FD\u6570\u8C03\u7528\u4E2D\u95F4\u8F6E\uFF09\uFF0C\u6B63\u5E38\u900F\u4F20`),g.length>0){const m=g.join(`
|
|
18
|
+
`);(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\uFF0C\u6587\u672C\u957F\u5EA6: ${m.length}`),await(0,w.reviewText)(m,w.AuditContentType.OUTPUT,i),(0,o.log)(`[openai-proxy][${t}] [\u975E\u6D41\u5F0F] \u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u901A\u8FC7`)}}catch(g){if(g instanceof w.ContentReviewFailedError){(0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u672A\u901A\u8FC7: ${g.fallbackMsg}`),e.headersSent?e.end():_(e,400,g.fallbackMsg||"\u8F93\u51FA\u5185\u5BB9\u5305\u542B\u8FDD\u89C4\u4FE1\u606F\uFF0C\u8BF7\u4FEE\u6539\u540E\u91CD\u8BD5\u3002",p.CONTENT_FILTER,u.CONTENT_REVIEW_FAILED_OUTPUT,{phase_action:g.phase_action,is_show:g.is_show});return}(0,o.log)(`[openai-proxy][${t}] \u975E\u6D41\u5F0F\u8F93\u51FA\u5185\u5BB9\u5BA1\u6838\u5F02\u5E38\uFF08\u4E0D\u963B\u585E\uFF09: ${g.message||g}`)}e.headersSent||e.writeHead(a,K),e.end(f);const M=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u5B8C\u6210\uFF0C\u603B\u8017\u65F6: ${M}ms`)}),n.on("error",f=>{(0,o.log)(`[openai-proxy][${t}] \u8BFB\u53D6\u76EE\u6807\u54CD\u5E94\u5931\u8D25: ${f.message}`),e.headersSent?e.end():_(e,502,`Bad Gateway: ${f.message}`,p.SERVER,u.PROXY_ERROR)})}});return v.on("error",n=>{const a=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u8F6C\u53D1\u8BF7\u6C42\u5931\u8D25 (${a}ms): ${n.message}`),e.headersSent?x?k(e,`Bad Gateway: ${n.message}`,p.SERVER,u.PROXY_ERROR):e.end():_(e,502,`Bad Gateway: ${n.message}`,p.SERVER,u.PROXY_ERROR)}),v.on("timeout",()=>{const n=Date.now()-y;(0,o.log)(`[openai-proxy][${t}] \u8BF7\u6C42\u8D85\u65F6 (${n}ms)`),v.destroy(),e.headersSent?x?k(e,"Gateway Timeout",p.SERVER,u.GATEWAY_TIMEOUT):e.end():_(e,504,"Gateway Timeout",p.SERVER,u.GATEWAY_TIMEOUT)}),v.end(D),!0}
|