(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.json((0,d.B)(e))}}let A=(0,c.g)();async function k(e,{params:t}){if(console.log("[Google Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.GeminiPro);if(o.error)return h.xk.json(o,{status:401});let r=(e.headers.get("x-goog-api-key")||e.headers.get("Authorization")||"").trim().replaceAll("Bearer ","").trim()||A.googleApiKey;if(!r)return h.xk.json({error:!0,message:"missing GOOGLE_API_KEY in server env vars"},{status:401});try{return await T(e,r)}catch(e){return console.error("[Google] ",e),h.xk.json((0,d.B)(e))}}async function T(e,t){let o=new AbortController,r=A.googleUrl||u.Hm,a=`${e.nextUrl.pathname}`.replaceAll(u.L.Google,"");r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",a),console.log("[Base Url]",r);let s=setTimeout(()=>{o.abort()},6e5),n=`${r}${a}${e?.nextUrl?.searchParams?.get("alt")==="sse"?"?alt=sse":""}`;console.log("[Fetch Url] ",n);let l={headers:{"Content-Type":"application/json","Cache-Control":"no-store","x-goog-api-key":e.headers.get("x-goog-api-key")||(e.headers.get("Authorization")??"").replace("Bearer ","")},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:o.signal};try{let e=await fetch(n,l),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(s)}}let O=new Set([u.YU.ChatPath,u.YU.ChatPath1]);async function j(e,{params:t}){if(console.log("[Anthropic Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=t.path.join("/");if(!O.has(o))return console.log("[Anthropic 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.Claude);if(r.error)return h.xk.json(r,{status:401});try{return await I(e)}catch(e){return console.error("[Anthropic] ",e),h.xk.json((0,d.B)(e))}}let $=(0,c.g)();async function I(e){let t=new AbortController,o="x-api-key",r=e.headers.get(o)||e.headers.get("Authorization")?.replaceAll("Bearer ","").trim()||$.anthropicApiKey||"",a=`${e.nextUrl.pathname}`.replaceAll(u.L.Anthropic,""),s=$.anthropicUrl||$.baseUrl||u.y3;s.startsWith("http")||(s=`https://${s}`),s.endsWith("/")&&(s=s.slice(0,-1)),console.log("[Proxy] ",a),console.log("[Base Url]",s);let n=setTimeout(()=>{t.abort()},6e5),l=y(`${s}${a}`),i={headers:{"Content-Type":"application/json","Cache-Control":"no-store","anthropic-dangerous-direct-browser-access":"true",[o]:r,"anthropic-version":e.headers.get("anthropic-version")||$.anthropicApiVersion||u.YU.Vision},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if($.customModels&&e.body)try{let t=await e.text();i.body=t;let o=JSON.parse(t);if((0,m.f8)($.customModels,o?.model,u.UT.Anthropic))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[Anthropic] filter",e)}try{let e=await fetch(l,i),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(n)}}async function U(e,t){let o=await fetch(`${u.FR}?grant_type=client_credentials&client_id=${e}&client_secret=${t}`,{method:"POST",mode:"cors"});return await o.json()}let S=(0,c.g)();async function B(e,{params:t}){if(console.log("[Baidu Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.Ernie);if(o.error)return h.xk.json(o,{status:401});if(!S.baiduApiKey||!S.baiduSecretKey)return h.xk.json({error:!0,message:"missing BAIDU_API_KEY or BAIDU_SECRET_KEY in server env vars"},{status:401});try{return await P(e)}catch(e){return console.error("[Baidu] ",e),h.xk.json((0,d.B)(e))}}async function P(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.Baidu,""),r=S.baiduUrl||u.n9;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),{access_token:s}=await U(S.baiduApiKey,S.baiduSecretKey),n=`${r}${o}?access_token=${s}`,l={headers:{"Content-Type":"application/json"},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(S.customModels&&e.body)try{let t=await e.text();l.body=t;let o=JSON.parse(t);if((0,m.f8)(S.customModels,o?.model,u.UT.Baidu))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[Baidu] filter",e)}try{let e=await fetch(n,l),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}let C=(0,c.g)();async function z(e,{params:t}){if(console.log("[ByteDance Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.Doubao);if(o.error)return h.xk.json(o,{status:401});try{return await K(e)}catch(e){return console.error("[ByteDance] ",e),h.xk.json((0,d.B)(e))}}async function K(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.ByteDance,""),r=C.bytedanceUrl||u.ik;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),s=`${r}${o}`,n={headers:{"Content-Type":"application/json",Authorization:e.headers.get("Authorization")??""},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(C.customModels&&e.body)try{let t=await e.text();n.body=t;let o=JSON.parse(t);if((0,m.f8)(C.customModels,o?.model,u.UT.ByteDance))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[ByteDance] filter",e)}try{let e=await fetch(s,n),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}let M=(0,c.g)();async function R(e,{params:t}){if(console.log("[Alibaba Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.Qwen);if(o.error)return h.xk.json(o,{status:401});try{return await L(e)}catch(e){return console.error("[Alibaba] ",e),h.xk.json((0,d.B)(e))}}async function L(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.Alibaba,""),r=M.alibabaUrl||u.x5;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),s=`${r}${o}`,n={headers:{"Content-Type":"application/json",Authorization:e.headers.get("Authorization")??"","X-DashScope-SSE":e.headers.get("X-DashScope-SSE")??"disable"},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(M.customModels&&e.body)try{let t=await e.text();n.body=t;let o=JSON.parse(t);if((0,m.f8)(M.customModels,o?.model,u.UT.Alibaba))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[Alibaba] filter",e)}try{let e=await fetch(s,n),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}let W=(0,c.g)();async function v(e,{params:t}){if(console.log("[Moonshot Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.Moonshot);if(o.error)return h.xk.json(o,{status:401});try{return await N(e)}catch(e){return console.error("[Moonshot] ",e),h.xk.json((0,d.B)(e))}}async function N(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.Moonshot,""),r=W.moonshotUrl||u.bP;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),s=`${r}${o}`,n={headers:{"Content-Type":"application/json",Authorization:e.headers.get("Authorization")??""},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(W.customModels&&e.body)try{let t=await e.text();n.body=t;let o=JSON.parse(t);if((0,m.f8)(W.customModels,o?.model,u.UT.Moonshot))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[Moonshot] filter",e)}try{let e=await fetch(s,n),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}async function X(e,{params:t}){if(console.log("[Stability] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=new AbortController,r=(0,c.g)(),a=r.stabilityUrl||u.rT;a.startsWith("http")||(a=`https://${a}`),a.endsWith("/")&&(a=a.slice(0,-1));let s=`${e.nextUrl.pathname}`.replaceAll("/api/stability/","");console.log("[Stability Proxy] ",s),console.log("[Stability Base Url]",a);let n=setTimeout(()=>{o.abort()},6e5),l=(0,p.I)(e,u.k8.Stability);if(l.error)return h.xk.json(l,{status:401});let i=(e.headers.get("Authorization")??"").trim().replaceAll("Bearer ","").trim()||r.stabilityApiKey;if(!i)return h.xk.json({error:!0,message:"missing STABILITY_API_KEY in server env vars"},{status:401});let d=`${a}/${s}`;console.log("[Stability Url] ",d);let y={headers:{"Content-Type":e.headers.get("Content-Type")||"multipart/form-data",Accept:e.headers.get("Accept")||"application/json",Authorization:`Bearer ${i}`},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:o.signal};try{let e=await fetch(d,y),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(n)}}let D=(0,c.g)();async function E(e,{params:t}){if(console.log("[Iflytek Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.Iflytek);if(o.error)return h.xk.json(o,{status:401});try{return await G(e)}catch(e){return console.error("[Iflytek] ",e),h.xk.json((0,d.B)(e))}}async function G(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.Iflytek,""),r=D.iflytekUrl||u.pG;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),s=`${r}${o}`,n={headers:{"Content-Type":"application/json",Authorization:e.headers.get("Authorization")??""},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(D.customModels&&e.body)try{let t=await e.text();n.body=t;let o=JSON.parse(t);if((0,m.f8)(D.customModels,o?.model,u.UT.Iflytek))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[Iflytek] filter",e)}try{let e=await fetch(s,n),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}let _=(0,c.g)();async function H(e,{params:t}){if(console.log("[DeepSeek Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.DeepSeek);if(o.error)return h.xk.json(o,{status:401});try{return await F(e)}catch(e){return console.error("[DeepSeek] ",e),h.xk.json((0,d.B)(e))}}async function F(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.DeepSeek,""),r=_.deepseekUrl||u.Z_;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),s=`${r}${o}`,n={headers:{"Content-Type":"application/json",Authorization:e.headers.get("Authorization")??""},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(_.customModels&&e.body)try{let t=await e.text();n.body=t;let o=JSON.parse(t);if((0,m.f8)(_.customModels,o?.model,u.UT.DeepSeek))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[DeepSeek] filter",e)}try{let e=await fetch(s,n),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}let J=(0,c.g)();async function Y(e,{params:t}){if(console.log("[SiliconFlow Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.SiliconFlow);if(o.error)return h.xk.json(o,{status:401});try{return await q(e)}catch(e){return console.error("[SiliconFlow] ",e),h.xk.json((0,d.B)(e))}}async function q(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.SiliconFlow,""),r=J.siliconFlowUrl||u.jT;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),s=`${r}${o}`,n={headers:{"Content-Type":"application/json",Authorization:e.headers.get("Authorization")??""},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(J.customModels&&e.body)try{let t=await e.text();n.body=t;let o=JSON.parse(t);if((0,m.f8)(J.customModels,o?.model,u.UT.SiliconFlow))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[SiliconFlow] filter",e)}try{let e=await fetch(s,n),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}let V=(0,c.g)();async function Q(e,{params:t}){if(console.log("[XAI Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.XAI);if(o.error)return h.xk.json(o,{status:401});try{return await Z(e)}catch(e){return console.error("[XAI] ",e),h.xk.json((0,d.B)(e))}}async function Z(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.XAI,""),r=V.xaiUrl||u.eE;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),s=`${r}${o}`,n={headers:{"Content-Type":"application/json",Authorization:e.headers.get("Authorization")??""},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(V.customModels&&e.body)try{let t=await e.text();n.body=t;let o=JSON.parse(t);if((0,m.f8)(V.customModels,o?.model,u.UT.XAI))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[XAI] filter",e)}try{let e=await fetch(s,n),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}let ee=(0,c.g)();async function et(e,{params:t}){if(console.log("[GLM Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,p.I)(e,u.k8.ChatGLM);if(o.error)return h.xk.json(o,{status:401});try{return await eo(e)}catch(e){return console.error("[GLM] ",e),h.xk.json((0,d.B)(e))}}async function eo(e){let t=new AbortController,o=`${e.nextUrl.pathname}`.replaceAll(u.L.ChatGLM,""),r=ee.chatglmUrl||u.BJ;r.startsWith("http")||(r=`https://${r}`),r.endsWith("/")&&(r=r.slice(0,-1)),console.log("[Proxy] ",o),console.log("[Base Url]",r);let a=setTimeout(()=>{t.abort()},6e5),s=`${r}${o}`;console.log("[Fetch Url] ",s);let n={headers:{"Content-Type":"application/json",Authorization:e.headers.get("Authorization")??""},method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:t.signal};if(ee.customModels&&e.body)try{let t=await e.text();n.body=t;let o=JSON.parse(t);if((0,m.f8)(ee.customModels,o?.model,u.UT.ChatGLM))return h.xk.json({error:!0,message:`you are not allowed to use ${o?.model} model`},{status:403})}catch(e){console.error("[GLM] filter",e)}try{let e=await fetch(s,n),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(a)}}async function er(e,{params:t}){if(console.log("[Proxy Route] params ",t),"OPTIONS"===e.method)return h.xk.json({body:"OK"},{status:200});let o=(0,c.g)();e.nextUrl.searchParams.delete("path"),e.nextUrl.searchParams.delete("provider");let r=t.path.join("/"),a=`${e.headers.get("x-base-url")}/${r}?${e.nextUrl.searchParams.toString()}`,s=["connection","host","origin","referer","cookie"],n=new Headers(Array.from(e.headers.entries()).filter(e=>!(e[0].indexOf("x-")>-1||e[0].indexOf("sec-")>-1||s.includes(e[0])))),l=e.headers.get("x-base-url");if(l?.includes("api.openai.com")){if(!o.apiKey)return h.xk.json({error:"OpenAI API key not configured"},{status:500});n.set("Authorization",`Bearer ${o.apiKey}`)}let i=new AbortController,u={headers:n,method:e.method,body:e.body,redirect:"manual",duplex:"half",signal:i.signal},d=setTimeout(()=>{i.abort()},6e5);try{let e=await fetch(a,u),t=new Headers(e.headers);return t.delete("www-authenticate"),t.set("X-Accel-Buffering","no"),t.delete("content-encoding"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:t})}finally{clearTimeout(d)}}async function ea(e,{params:t}){let o=`/api/${t.provider}`;switch(console.log(`[${t.provider} Route] params `,t),o){case u.L.Azure:return x(e,{params:t});case u.L.Google:return k(e,{params:t});case u.L.Anthropic:return j(e,{params:t});case u.L.Baidu:return B(e,{params:t});case u.L.ByteDance:return z(e,{params:t});case u.L.Alibaba:return R(e,{params:t});case u.L.Moonshot:return v(e,{params:t});case u.L.Stability:return X(e,{params:t});case u.L.Iflytek:return E(e,{params:t});case u.L.DeepSeek:return H(e,{params:t});case u.L.XAI:return Q(e,{params:t});case u.L.ChatGLM:return et(e,{params:t});case u.L.SiliconFlow:return Y(e,{params:t});case u.L.OpenAI:return w(e,{params:t});default:return er(e,{params:t})}}let es=ea,en=ea,el="edge",ei=["arn1","bom1","cdg1","cle1","cpt1","dub1","fra1","gru1","hnd1","iad1","icn1","kix1","lhr1","pdx1","sfo1","sin1","syd1"],eu=new n.AppRouteRouteModule({definition:{kind:l.x.APP_ROUTE,page:"/api/[provider]/[...path]/route",pathname:"/api/[provider]/[...path]",filename:"route",bundlePath:"app/api/[provider]/[...path]/route"},resolvedPagePath:"D:\\NextWeb\\app\\api\\[provider]\\[...path]\\route.ts",nextConfigOutput:"standalone",userland:r}),{requestAsyncStorage:ec,staticGenerationAsyncStorage:ed,serverHooks:eh,headerHooks:ep,staticGenerationBailout:ey}=eu,em="/api/[provider]/[...path]/route";function ef(){return(0,i.XH)({serverHooks:eh,staticGenerationAsyncStorage:ed})}let eg=a,eb=s.a.wrap(eu)},5577:(e,t,o)=>{"use strict";o.d(t,{I:()=>l});var r=o(9579),a=o(5045),s=o.n(a),n=o(7736);function l(e,t){let{accessCode:o,apiKey:a}=function(e){let t=e.trim().replaceAll("Bearer ","").trim(),o=!t.startsWith(n.TW);return{accessCode:o?"":t.slice(n.TW.length),apiKey:o?t:""}}(e.headers.get("Authorization")??""),l=s().hash(o??"").trim(),i=(0,r.g)();if(console.log("[Auth] allowed hashed codes: ",[...i.codes]),console.log("[Auth] got access code:",o),console.log("[Auth] hashed access code:",l),console.log("[User IP] ",function(e){let t=e.ip??e.headers.get("x-real-ip"),o=e.headers.get("x-forwarded-for");return!t&&o&&(t=o.split(",").at(0)??""),t}(e)),console.log("[Time] ",new Date().toLocaleString()),i.needCode&&!i.codes.has(l)&&!a)return{error:!0,msg:o?"wrong access code":"empty access code"};if(i.hideUserApiKey&&a)return{error:!0,msg:"you are not allowed to access with your own api key"};if(a)console.log("[Auth] use user api key");else{let o;let a=(0,r.g)();switch(t){case n.k8.Stability:o=a.stabilityApiKey;break;case n.k8.GeminiPro:o=a.googleApiKey;break;case n.k8.Claude:o=a.anthropicApiKey;break;case n.k8.Doubao:o=a.bytedanceApiKey;break;case n.k8.Ernie:o=a.baiduApiKey;break;case n.k8.Qwen:o=a.alibabaApiKey;break;case n.k8.Moonshot:o=a.moonshotApiKey;break;case n.k8.Iflytek:o=a.iflytekApiKey+":"+a.iflytekApiSecret;break;case n.k8.DeepSeek:o=a.deepseekApiKey;break;case n.k8.XAI:o=a.xaiApiKey;break;case n.k8.ChatGLM:o=a.chatglmApiKey;break;case n.k8.SiliconFlow:o=a.siliconFlowApiKey;break;case n.k8.GPT:default:o=e.nextUrl.pathname.includes("azure/deployments")?a.azureApiKey:a.apiKey}o?(console.log("[Auth] use system api key"),e.headers.set("Authorization",`Bearer ${o}`)):console.log("[Auth] admin did not provide an api key")}return{error:!1}}},1480:(e,t,o)=>{"use strict";function r(e){let t=e;return("string"!=typeof e&&(e=JSON.stringify(e,null," ")),"{}"===e)?t.toString():e.startsWith("```json")?e:["```json",e,"```"].join("\n")}o.d(t,{B:()=>r})}},e=>{var t=t=>e(e.s=t);e.O(0,[297,45,579],()=>t(5437));var o=e.O();(_ENTRIES="undefined"==typeof _ENTRIES?{}:_ENTRIES)["middleware_app/api/[provider]/[...path]/route"]=o}]); //# sourceMappingURL=route.js.map