You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
25 KiB
JavaScript

9 months ago
(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[919],{2067:e=>{"use strict";e.exports=require("node:async_hooks")},6195:e=>{"use strict";e.exports=require("node:buffer")},5437:(e,t,o)=>{"use strict";o.r(t),o.d(t,{ComponentMod:()=>eg,default:()=>eb});var r={};o.r(r),o.d(r,{GET:()=>es,POST:()=>en,preferredRegion:()=>ei,runtime:()=>el});var a={};o.r(a),o.d(a,{headerHooks:()=>ep,originalPathname:()=>em,patchFetch:()=>ef,requestAsyncStorage:()=>ec,routeModule:()=>eu,serverHooks:()=>eh,staticGenerationAsyncStorage:()=>ed,staticGenerationBailout:()=>ey});var s=o(7875),n=o(8381),l=o(2251),i=o(9845),u=o(7736),c=o(9579),d=o(1480),h=o(572),p=o(5577);function y(e){let t=e.split("/");if("gateway.ai.cloudflare.com"==t[2]){if("azure-openai"==t[6])return t.slice(0,8).concat(t.slice(-3)).join("/");if("openai"==t[6]||"anthropic"==t[6])return t.slice(0,7).concat(t.slice(-2)).join("/")}return e}var m=o(413);let f=(0,c.g)();async function g(e){let t=new AbortController,o=e.nextUrl.pathname.includes("azure/deployments");var r,a="";o?(r=e.headers.get("Authorization")?.trim().replaceAll("Bearer ","").trim()??"",a="api-key"):(r=e.headers.get("Authorization")??"",a="Authorization");let s=`${e.nextUrl.pathname}`.replaceAll("/api/openai/",""),n=(o?f.azureUrl:f.baseUrl)||u.Bi;n.startsWith("http")||(n=`https://${n}`),n.endsWith("/")&&(n=n.slice(0,-1)),console.log("[Proxy] ",s),console.log("[Base Url]",n);let l=setTimeout(()=>{t.abort()},6e5);if(o){let t=e?.nextUrl?.searchParams?.get("api-version")||f.azureApiVersion;if(n=n.split("/deployments").shift(),s=`${e.nextUrl.pathname.replaceAll("/api/azure/","")}?api-version=${t}`,f.customModels&&f.azureUrl){let e=s.split("/")[1],t="";f.customModels.split(",").filter(t=>!!t&&!t.startsWith("-")&&t.includes(e)).forEach(e=>{let[o,r]=e.split("="),[a,s]=(0,m.En)(o);if("azure"===s&&!r){let[e,o]=(f?.azureUrl??"").split("deployments/");o&&(t=o)}}),t&&(console.log("[Replace with DeployId",t),s=s.replaceAll(e,t))}}let i=y(`${n}/${s}`);console.log("fetchUrl",i);let c={headers:{"Content-Type":"application/json","Cache-Control":"no-store",[a]:r,...f.openaiOrgId&&{"OpenAI-Organization":f.openaiOrgId}},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(f.customModels&&e.body)try{let t=await e.text();c.body=t;let o=JSON.parse(t);if((0,m.f8)(f.customModels,o?.model,[u.UT.OpenAI,u.UT.Azure,o?.model]))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[OpenAI] gpt4 filter",e)}try{let e=await fetch(i,c),t=e.headers.get("OpenAI-Organization");f.openaiOrgId&&""!==f.openaiOrgId.trim()?console.log("[Org ID]",t):console.log("[Org ID] is not set up.");let o=new Headers(e.headers);return o.delete("www-authenticate"),o.set("X-Accel-Buffering","no"),f.openaiOrgId&&""!==f.openaiOrgId.trim()||o.delete("OpenAI-Organization"),o.delete("content-encoding"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:o})}finally{clearTimeout(l)}}let b=new Set(Object.values(u.mX));async function w(e,{params:t}){if(console.log("[OpenAI Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=t.path.join("/");if(!b.has(o))return console.log("[OpenAI Route] forbidden path ",o),h.xk.json({error:!0,msg:"you are not allowed to request "+o},{status:403});let r=(0,p.I)(e,u.k8.GPT);if(r.error)return h.xk.json(r,{status:401});try{let t=await g(e);if(o===u.mX.ListModelPath&&200===t.status){var a;let e=(a=await t.json(),(0,c.g)().disableGPT4&&(a.data=a.data.filter(e=>!(e.id.startsWith("gpt-4")||e.id.startsWith("chatgpt-4o")||e.id.startsWith("o1")||e.id.startsWith("o3"))||e.id.startsWith("gpt-4o-mini"))),a);return h.xk.json(e,{status:t.status})}return t}catch(e){return console.error("[OpenAI] ",e),h.xk.json((0,d.B)(e))}}async function x(e,{params:t}){if(console.log("[Azure Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});t.path.join("/");let o=(0,p.I)(e,u.k8.GPT);if(o.error)return h.xk.json(o,{status:401});try{return await g(e)}catch(e){return console.error("[Azure] ",e),h.xk.j
//# sourceMappingURL=route.js.map