@wengine-ai/llms 2.3.2404 → 2.3.2405

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.
@@ -1,52 +1,52 @@
1
1
  import { createRequire as __createRequire } from "node:module"; import { fileURLToPath as __fileURLToPath } from "node:url"; import { dirname as __pathDirname } from "node:path"; const require = __createRequire(import.meta.url); const __filename = __fileURLToPath(import.meta.url); const __dirname = __pathDirname(__filename);
2
- var Un=Object.defineProperty;var ca=Object.getOwnPropertyDescriptor;var la=Object.getOwnPropertyNames;var ua=Object.prototype.hasOwnProperty;var Ve=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var w=(n,e)=>()=>(n&&(e=n(n=0)),e);var jn=(n,e)=>{for(var t in e)Un(n,t,{get:e[t],enumerable:!0})},da=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of la(e))!ua.call(n,r)&&r!==t&&Un(n,r,{get:()=>e[r],enumerable:!(o=ca(e,r))||o.enumerable});return n};var pa=n=>da(Un({},"__esModule",{value:!0}),n);import{ProxyAgent as fa}from"undici";function ga(n){if(n==null)return{ok:!0};let e=String(n).trim();if(!e)return{ok:!0};if(ma.test(e))return{ok:!0};try{let t=new URL(e);return t.protocol!=="http:"&&t.protocol!=="https:"?{ok:!1,error:`Unsupported proxy protocol: ${t.protocol}. Only http:// and https:// are allowed.`}:{ok:!0}}catch{return{ok:!1,error:`Invalid proxy URL: ${e}`}}}function kr(n){let e=[];for(let t of _r){let o=n[t];if(o==null)continue;let r=ga(o);r.ok||e.push({key:t,error:r.error})}return e}function vt(n){for(let e of _r){let t=n.get(e);if(t==null)continue;let o=String(t).trim();if(o)return o}}function br(n){if(n==null||n==="")return!0;if(typeof n=="string"){let e=n.trim().toLowerCase();return e==="false"||e==="0"?!1:e==="true"||e==="1"?!0:!!e}return n===!1||n===0?!1:n===!0||n===1?!0:!!n}function ce(n,e){let t=vt(n);if(t)return br(n.get("PROXY_GLOBAL_ENABLED"))||e?.proxyEnabled===!0||e?.proxy_enabled===!0?t:void 0}function G(n){let e=ha(n),t=Tt.get(e);if(t)return t;let o=new fa(e);return Tt.set(e,o),o}async function Hn(){let n=Array.from(Tt.values());Tt.clear(),await Promise.allSettled(n.map(e=>e.close()))}function ha(n){let e=n.trim();if(!e)throw new TypeError("Proxy URL must not be empty");let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")throw new TypeError(`Unsupported proxy protocol: ${t.protocol}`);return t.toString()}var _r,ma,Tt,ne=w(()=>{"use strict";_r=["HTTPS_PROXY","https_proxy","httpsProxy","PROXY_URL"],ma=/\$\{[^}]+\}|\$[A-Z_][A-Z0-9_]*/;Tt=new Map});import{readFileSync as ya,existsSync as Tr}from"fs";import{join as vr}from"path";import{config as _a}from"dotenv";import ka from"json5";var ve,zn=w(()=>{"use strict";ne();ve=class{config={};options;constructor(e={jsonPath:"./config.json"}){this.options={envPath:e.envPath||".env",jsonPath:e.jsonPath,useEnvFile:!1,useJsonFile:e.useJsonFile!==!1,useEnvironmentVariables:e.useEnvironmentVariables!==!1,...e},this.loadConfig()}loadConfig(){this.options.useJsonFile&&this.options.jsonPath&&this.loadJsonConfig(),this.options.initialConfig&&(this.config={...this.config,...this.options.initialConfig}),this.options.useEnvFile&&this.loadEnvConfig(),this.config.LOG_FILE&&(process.env.LOG_FILE=this.config.LOG_FILE),this.config.LOG&&(process.env.LOG=this.config.LOG)}loadJsonConfig(){if(!this.options.jsonPath)return;let e=this.isAbsolutePath(this.options.jsonPath)?this.options.jsonPath:vr(process.cwd(),this.options.jsonPath);if(Tr(e))try{let t=ya(e,"utf-8"),o=ka.parse(t);this.config={...this.config,...o},console.log(`Loaded JSON config from: ${e}`)}catch(t){console.warn(`Failed to load JSON config from ${e}:`,t)}else console.warn(`JSON config file not found: ${e}`)}loadEnvConfig(){let e=this.isAbsolutePath(this.options.envPath)?this.options.envPath:vr(process.cwd(),this.options.envPath);if(Tr(e))try{let t=_a({path:e});t.parsed&&(this.config={...this.config,...this.parseEnvConfig(t.parsed)})}catch(t){console.warn(`Failed to load .env config from ${e}:`,t)}}loadEnvironmentVariables(){let e=this.parseEnvConfig(process.env);this.config={...this.config,...e}}parseEnvConfig(e){let t={};return Object.assign(t,e),t}isAbsolutePath(e){return e.startsWith("/")||e.includes(":")}get(e,t){let o=this.config[e];return o!==void 0?o:t}getAll(){return{...this.config}}getHttpsProxy(){return vt(this)}has(e){return this.config[e]!==void 0}set(e,t){this.config[e]=t}reload(){this.config={},this.loadConfig()}getConfigSummary(){let e=[];return this.options.initialConfig&&e.push("Initial Config"),this.options.useJsonFile&&this.options.jsonPath&&e.push(`JSON: ${this.options.jsonPath}`),this.options.useEnvFile&&e.push(`ENV: ${this.options.envPath}`),this.options.useEnvironmentVariables&&e.push("Environment Variables"),`Config sources: ${e.join(", ")}`}}});function B(n,e=500,t="internal_error",o="api_error"){let r=new Error(n);return r.statusCode=e,r.code=t,r.type=o,r}async function Cr(n,e,t){e.log.error(n);let o=n.statusCode||500;if(n.rawBody)try{let s=JSON.parse(n.rawBody);return t.code(o).send(s)}catch{return t.code(o).send(n.rawBody)}let r={error:{message:n.message||"Internal Server Error",type:n.type||"api_error",code:n.code||"internal_error"}};return t.code(o).send(r)}var Ct=w(()=>{"use strict"});function Bn(n){Jn=n}function xt(){return Jn}function St(n){return Jn||n.get("DEBUG_LOG")===!0}function Rr(n){let e=n.get("DEBUG_LOG_OPTIONS");return!e||typeof e!="object"?Ce:{maxBodyLength:e.maxBodyLength??Ce.maxBodyLength,maxStreamChunks:e.maxStreamChunks??Ce.maxStreamChunks}}function Rt(n){let e={};for(let[t,o]of Object.entries(n))ba.has(t.toLowerCase())?e[t]="***MASKED***":e[t]=o;return e}function wt(n,e){return n.length<=e?n:n.slice(0,e)+`...[truncated, total ${n.length} bytes]`}function wr(n,e,t){let o=typeof t.body=="string"?t.body:JSON.stringify(t.body);n.info({debug_log:!0,reqId:e,phase:"provider_request",url:t.url,headers:Rt(t.headers),body:wt(o,Ce.maxBodyLength)})}function Pr(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_response",status:t.status,headers:Rt(t.headers),...t.body!==void 0?{body:wt(t.body,Ce.maxBodyLength)}:{}})}function xr(n,e,t,o){let r=Ta(o);n.info({debug_log:!0,reqId:e,phase:"provider_stream_chunk",chunkIndex:t,...r?{usage_monitor:r}:{},data:wt(o,Ce.maxBodyLength)})}function Ta(n){let e=[];for(let t of n.split(/\r?\n/)){if(!t.startsWith("data:"))continue;let o=t.slice(5).trim();if(!(!o||o==="[DONE]"))try{let r=JSON.parse(o),s=r?.usage||r?.response?.usage;s&&e.push({path:r?.usage?"usage":"response.usage",usage:s,choicesLength:Array.isArray(r?.choices)?r.choices.length:void 0,finishReason:r?.choices?.[0]?.finish_reason})}catch{e.push({parseError:!0,data:wt(o,512)})}}if(e.length)return e.length===1?e[0]:e}function Sr(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_stream_end",totalChunks:t})}function Er(n,e,t,o){(async()=>{let r=n.pipeThrough(new TextDecoderStream).getReader(),s=0;try{let i="";for(;;){let{done:c,value:l}=await r.read();if(c)break;i+=l;let a=i.split(`
2
+ var jn=Object.defineProperty;var da=Object.getOwnPropertyDescriptor;var pa=Object.getOwnPropertyNames;var fa=Object.prototype.hasOwnProperty;var Ve=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var w=(n,e)=>()=>(n&&(e=n(n=0)),e);var Hn=(n,e)=>{for(var t in e)jn(n,t,{get:e[t],enumerable:!0})},ma=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of pa(e))!fa.call(n,r)&&r!==t&&jn(n,r,{get:()=>e[r],enumerable:!(o=da(e,r))||o.enumerable});return n};var ga=n=>ma(jn({},"__esModule",{value:!0}),n);import{ProxyAgent as ha}from"undici";function _a(n){if(n==null)return{ok:!0};let e=String(n).trim();if(!e)return{ok:!0};if(ya.test(e))return{ok:!0};try{let t=new URL(e);return t.protocol!=="http:"&&t.protocol!=="https:"?{ok:!1,error:`Unsupported proxy protocol: ${t.protocol}. Only http:// and https:// are allowed.`}:{ok:!0}}catch{return{ok:!1,error:`Invalid proxy URL: ${e}`}}}function Tr(n){let e=[];for(let t of br){let o=n[t];if(o==null)continue;let r=_a(o);r.ok||e.push({key:t,error:r.error})}return e}function vt(n){for(let e of br){let t=n.get(e);if(t==null)continue;let o=String(t).trim();if(o)return o}}function vr(n){if(n==null||n==="")return!0;if(typeof n=="string"){let e=n.trim().toLowerCase();return e==="false"||e==="0"?!1:e==="true"||e==="1"?!0:!!e}return n===!1||n===0?!1:n===!0||n===1?!0:!!n}function ce(n,e){let t=vt(n);if(t)return vr(n.get("PROXY_GLOBAL_ENABLED"))||e?.proxyEnabled===!0||e?.proxy_enabled===!0?t:void 0}function G(n){let e=ka(n),t=Tt.get(e);if(t)return t;let o=new ha(e);return Tt.set(e,o),o}async function zn(){let n=Array.from(Tt.values());Tt.clear(),await Promise.allSettled(n.map(e=>e.close()))}function ka(n){let e=n.trim();if(!e)throw new TypeError("Proxy URL must not be empty");let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")throw new TypeError(`Unsupported proxy protocol: ${t.protocol}`);return t.toString()}var br,ya,Tt,oe=w(()=>{"use strict";br=["HTTPS_PROXY","https_proxy","httpsProxy","PROXY_URL"],ya=/\$\{[^}]+\}|\$[A-Z_][A-Z0-9_]*/;Tt=new Map});import{readFileSync as ba,existsSync as Cr}from"fs";import{join as xr}from"path";import{config as Ta}from"dotenv";import va from"json5";var Ce,Bn=w(()=>{"use strict";oe();Ce=class{config={};options;constructor(e={jsonPath:"./config.json"}){this.options={envPath:e.envPath||".env",jsonPath:e.jsonPath,useEnvFile:!1,useJsonFile:e.useJsonFile!==!1,useEnvironmentVariables:e.useEnvironmentVariables!==!1,...e},this.loadConfig()}loadConfig(){this.options.useJsonFile&&this.options.jsonPath&&this.loadJsonConfig(),this.options.initialConfig&&(this.config={...this.config,...this.options.initialConfig}),this.options.useEnvFile&&this.loadEnvConfig(),this.config.LOG_FILE&&(process.env.LOG_FILE=this.config.LOG_FILE),this.config.LOG&&(process.env.LOG=this.config.LOG)}loadJsonConfig(){if(!this.options.jsonPath)return;let e=this.isAbsolutePath(this.options.jsonPath)?this.options.jsonPath:xr(process.cwd(),this.options.jsonPath);if(Cr(e))try{let t=ba(e,"utf-8"),o=va.parse(t);this.config={...this.config,...o},console.log(`Loaded JSON config from: ${e}`)}catch(t){console.warn(`Failed to load JSON config from ${e}:`,t)}else console.warn(`JSON config file not found: ${e}`)}loadEnvConfig(){let e=this.isAbsolutePath(this.options.envPath)?this.options.envPath:xr(process.cwd(),this.options.envPath);if(Cr(e))try{let t=Ta({path:e});t.parsed&&(this.config={...this.config,...this.parseEnvConfig(t.parsed)})}catch(t){console.warn(`Failed to load .env config from ${e}:`,t)}}loadEnvironmentVariables(){let e=this.parseEnvConfig(process.env);this.config={...this.config,...e}}parseEnvConfig(e){let t={};return Object.assign(t,e),t}isAbsolutePath(e){return e.startsWith("/")||e.includes(":")}get(e,t){let o=this.config[e];return o!==void 0?o:t}getAll(){return{...this.config}}getHttpsProxy(){return vt(this)}has(e){return this.config[e]!==void 0}set(e,t){this.config[e]=t}reload(){this.config={},this.loadConfig()}getConfigSummary(){let e=[];return this.options.initialConfig&&e.push("Initial Config"),this.options.useJsonFile&&this.options.jsonPath&&e.push(`JSON: ${this.options.jsonPath}`),this.options.useEnvFile&&e.push(`ENV: ${this.options.envPath}`),this.options.useEnvironmentVariables&&e.push("Environment Variables"),`Config sources: ${e.join(", ")}`}}});function J(n,e=500,t="internal_error",o="api_error"){let r=new Error(n);return r.statusCode=e,r.code=t,r.type=o,r}async function Sr(n,e,t){e.log.error(n);let o=n.statusCode||500;if(n.rawBody)try{let s=JSON.parse(n.rawBody);return t.code(o).send(s)}catch{return t.code(o).send(n.rawBody)}let r={error:{message:n.message||"Internal Server Error",type:n.type||"api_error",code:n.code||"internal_error"}};return t.code(o).send(r)}var Ct=w(()=>{"use strict"});function Wn(n){Jn=n}function xt(){return Jn}function St(n){return Jn||n.get("DEBUG_LOG")===!0}function Pr(n){let e=n.get("DEBUG_LOG_OPTIONS");return!e||typeof e!="object"?xe:{maxBodyLength:e.maxBodyLength??xe.maxBodyLength,maxStreamChunks:e.maxStreamChunks??xe.maxStreamChunks}}function Rt(n){let e={};for(let[t,o]of Object.entries(n))Ca.has(t.toLowerCase())?e[t]="***MASKED***":e[t]=o;return e}function wt(n,e){return n.length<=e?n:n.slice(0,e)+`...[truncated, total ${n.length} bytes]`}function Er(n,e,t){let o=typeof t.body=="string"?t.body:JSON.stringify(t.body);n.info({debug_log:!0,reqId:e,phase:"provider_request",url:t.url,headers:Rt(t.headers),body:wt(o,xe.maxBodyLength)})}function Or(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_response",status:t.status,headers:Rt(t.headers),...t.body!==void 0?{body:wt(t.body,xe.maxBodyLength)}:{}})}function Rr(n,e,t,o){let r=xa(o);n.info({debug_log:!0,reqId:e,phase:"provider_stream_chunk",chunkIndex:t,...r?{usage_monitor:r}:{},data:wt(o,xe.maxBodyLength)})}function xa(n){let e=[];for(let t of n.split(/\r?\n/)){if(!t.startsWith("data:"))continue;let o=t.slice(5).trim();if(!(!o||o==="[DONE]"))try{let r=JSON.parse(o),s=r?.usage||r?.response?.usage;s&&e.push({path:r?.usage?"usage":"response.usage",usage:s,choicesLength:Array.isArray(r?.choices)?r.choices.length:void 0,finishReason:r?.choices?.[0]?.finish_reason})}catch{e.push({parseError:!0,data:wt(o,512)})}}if(e.length)return e.length===1?e[0]:e}function wr(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_stream_end",totalChunks:t})}function Ar(n,e,t,o){(async()=>{let r=n.pipeThrough(new TextDecoderStream).getReader(),s=0;try{let i="";for(;;){let{done:c,value:l}=await r.read();if(c)break;i+=l;let a=i.split(`
3
3
 
4
- `);i=a.pop()||"";for(let u of a)u.trim()&&(s<o.maxStreamChunks?xr(e,t,s,u):s===o.maxStreamChunks&&e.info({debug_log:!0,reqId:t,phase:"provider_stream_chunk_omitted",message:`[chunks after #${s} omitted, maxStreamChunks=${o.maxStreamChunks}]`}),s++)}i.trim()&&(s<o.maxStreamChunks&&xr(e,t,s,i.trim()),s++),Sr(e,t,s)}catch(i){console.error(`[debug_log] Error reading debug stream for reqId=${t}:`,i),Sr(e,t,s)}finally{r.releaseLock()}})()}var ba,Ce,Jn,Ye=w(()=>{"use strict";ba=new Set(["authorization","x-api-key","x-goog-api-key","cookie","set-cookie"]),Ce={maxBodyLength:4096,maxStreamChunks:100},Jn=!1});function Or(n,e,t,o,r){let s=new Headers({"Content-Type":"application/json"});t.headers&&Object.entries(t.headers).forEach(([a,u])=>{u&&s.set(a,u)});let i,c=AbortSignal.timeout(t.TIMEOUT??60*1e3*60);if(t.signal){let a=new AbortController,u=()=>a.abort();t.signal.addEventListener("abort",u),c.addEventListener("abort",u),i=a.signal}else i=c;let l={method:"POST",headers:s,body:JSON.stringify(e),signal:i};return t.httpsProxy&&(l.dispatcher=G(t.httpsProxy)),r?.debug({reqId:o.req.id,method:"POST",headers:Rt(Object.fromEntries(s.entries())),requestUrl:typeof n=="string"?n:n.toString(),useProxy:va(t.httpsProxy)},"final request"),fetch(typeof n=="string"?n:n.toString(),l)}function va(n){if(!(typeof n!="string"||!n.trim()))try{let e=new URL(n);return e.username||e.password?(e.username="",e.password="",`${e.toString()} (credentials redacted)`):e.toString()}catch{return"[invalid proxy URL]"}}var Ar=w(()=>{ne();Ye()});var Pt,Wn=w(()=>{Pt="2.3.2404"});var Ir={};jn(Ir,{getFallbackPromotionStore:()=>xe,resetFallbackPromotionStore:()=>Sa});function xe(n){return he||(he=new Gn,n&&he.configure(n)),he}function Sa(){he&&(he.clearAll(),he=null)}var xa,Gn,he,Et=w(()=>{"use strict";oe();xa=600*1e3,Gn=class{promotions=new Map;enabled=!0;ttlMs=xa;configure(e){e?.enabled!==void 0&&(this.enabled=e.enabled),e?.ttlMinutes!==void 0&&(this.ttlMs=e.ttlMinutes*60*1e3)}getKey(e,t,o){return`${e},${t}:${o}`}promote(e,t,o,r,s,i){if(!this.enabled)return;let c=this.getKey(e,t,o),l=`${r},${s}`;this.promotions.set(c,{promotedModel:l,timestamp:Date.now(),ttlMs:i??this.ttlMs})}getPromotion(e,t,o,r){if(!this.enabled)return null;let s=this.getKey(e,t,o),i=this.promotions.get(s);if(!i)return null;if(Date.now()-i.timestamp>=i.ttlMs)return this.promotions.delete(s),null;let[l,...a]=i.promotedModel.split(","),u=a.join(",");if(!U().isAvailable(l,u))return this.promotions.delete(s),null;let m=r.find(y=>y.name.toLowerCase()===l.toLowerCase()),f=m?.models?.find(y=>String(y).toLowerCase()===u.toLowerCase());return!m||!f||m.enabled===!1?(this.promotions.delete(s),null):`${m.name},${f}`}clear(e,t,o){let r=this.getKey(e,t,o);this.promotions.delete(r)}clearByPrimary(e,t){let o=`${e},${t}:`,r=0;for(let s of this.promotions.keys())s.startsWith(o)&&(this.promotions.delete(s),r++);return r}clearAll(){this.promotions.clear()}clearExpired(){let e=Date.now();for(let[t,o]of this.promotions)e-o.timestamp>=o.ttlMs&&this.promotions.delete(t)}getAllPromotions(){let e=Date.now(),t=[];for(let[o,r]of this.promotions){let s=r.ttlMs-(e-r.timestamp);s>0&&t.push({key:o,promotedModel:r.promotedModel,remainingMs:s})}return t}},he=null});import{existsSync as Nr,mkdirSync as Ra,readFileSync as wa,writeFileSync as Pa}from"fs";import{join as Lr}from"path";import{HOME_DIR as Ea}from"@wengine-ai/claude-code-router-shared";function Ma(n){try{if(!Nr(Vn))return;let e=JSON.parse(wa(Vn,"utf-8"));if(!Array.isArray(e))return;let t=Date.now();for(let o of e){if(!o||!o.provider||!o.model)continue;let r=o.updatedAt||o.lastFailureTime||0;r&&t-r>1440*60*1e3||n.restore({provider:o.provider,model:o.model,status:o.status,failureCount:o.failureCount??0,successCount:o.successCount??0,lastFailureTime:o.lastFailureTime??0,lastProbeTime:o.lastProbeTime??0,lastError:o.lastError,rateLimitUntil:o.rateLimitUntil})}}catch{}}function $r(n){try{let e=n.getAllStates();if(e.length===0)return;Nr(Qn)||Ra(Qn,{recursive:!0});let t=e.map(o=>({...o,updatedAt:Date.now()}));Pa(Vn,JSON.stringify(t,null,2),"utf-8")}catch{}}function Yn(){if(Mr)return;Mr=!0;let n=U();Ma(n),Ia=setInterval(()=>$r(n),6e4),process.on("exit",()=>$r(n))}function U(n){return Kn||(Kn=new Ot(n)),Kn}var Oa,Aa,Ot,Qn,Vn,Mr,Ia,Kn,oe=w(()=>{"use strict";Oa=120,Aa={enabled:!0,failureThreshold:3,probeIntervalMinutes:5,halfOpenSuccessThreshold:2},Ot=class{states=new Map;config;probeTimer;constructor(e){this.config={...Aa,...e}}getKey(e,t){return!e||!t?null:`${e},${t}`}recordSuccess(e,t){if(!this.config.enabled)return;let o=this.getKey(e,t);if(!o)return;let r=this.states.get(o);r&&(r.successCount++,!r.rateLimitUntil&&(r.status==="half-open"?r.successCount>=this.config.halfOpenSuccessThreshold&&(this.clearPromotionsForPrimary(e,t),this.states.delete(o)):r.status==="open"&&(r.status="half-open",r.successCount=1,r.failureCount=0)))}clearPromotionsForPrimary(e,t){try{let{getFallbackPromotionStore:o}=(Et(),pa(Ir));o().clearByPrimary(e,t)}catch{}}recordFailure(e,t,o){if(!this.config.enabled)return;let r=this.getKey(e,t);if(!r)return;let s=this.states.get(r);s||(s={provider:e,model:t,status:"closed",failureCount:0,successCount:0,lastFailureTime:0,lastProbeTime:0},this.states.set(r,s)),s.failureCount++,s.lastFailureTime=Date.now(),s.lastError=o,s.status==="half-open"?(s.status="open",s.successCount=0,s.lastProbeTime=0):s.status==="closed"&&s.failureCount>=this.config.failureThreshold&&(s.status="open",s.lastProbeTime=0)}getState(e,t){let o=this.getKey(e,t);if(o)return this.states.get(o)}isAvailable(e,t){if(!this.config.enabled)return!0;if(!this.getKey(e,t))return!1;let r=this.getState(e,t);return r?r.status==="open"&&r.rateLimitUntil?!(Date.now()<r.rateLimitUntil):r.status!=="open":!0}getPriority(e,t){if(!this.config.enabled)return 0;if(!this.getKey(e,t))return 2;let r=this.getState(e,t);if(!r)return 0;switch(r.status){case"closed":return 0;case"half-open":return 1;case"open":return 2}}getHealthyModels(){return[]}getFailPoolModels(){let e=[];for(let[t,o]of this.states)o.status==="open"&&e.push(t);return e}getHalfOpenModels(){let e=[];for(let[t,o]of this.states)o.status==="half-open"&&e.push(t);return e}forceOpen(e,t,o){if(!this.config.enabled)return;let r=this.getKey(e,t);if(!r)return;let s=this.states.get(r);s?(s.status="open",s.failureCount=Math.max(s.failureCount,this.config.failureThreshold),s.lastFailureTime=Date.now(),s.lastError=o,s.lastProbeTime=0):(s={provider:e,model:t,status:"open",failureCount:this.config.failureThreshold,successCount:0,lastFailureTime:Date.now(),lastProbeTime:0,lastError:o},this.states.set(r,s))}markRateLimited(e,t,o,r){if(!this.config.enabled)return;let s=this.getKey(e,t);if(!s)return;let i=this.states.get(s);i?(i.status="open",i.lastFailureTime=Date.now(),i.lastError=r||"rate-limited",i.lastProbeTime=0):(i={provider:e,model:t,status:"open",failureCount:0,successCount:0,lastFailureTime:Date.now(),lastProbeTime:0,lastError:r||"rate-limited"},this.states.set(s,i));let c=(o??Oa)*1e3;i.rateLimitUntil=Date.now()+c}getRateLimitedModels(){let e=[];for(let[t,o]of this.states)o.status==="open"&&o.rateLimitUntil&&e.push(t);return e}needsProbe(e){return Date.now()-e.lastProbeTime>=this.config.probeIntervalMinutes*60*1e3}markProbeAttempt(e,t){let o=this.getKey(e,t);if(!o)return;let r=this.states.get(o);r&&r.status==="open"&&(r.lastProbeTime=Date.now())}clear(){this.states.clear()}recover(e,t){let o=this.getKey(e,t);o&&this.states.delete(o)}getAllStates(){return Array.from(this.states.values())}restore(e){let t=this.getKey(e.provider,e.model);t&&this.states.set(t,{...e})}stopProbeTimer(){this.probeTimer&&(clearInterval(this.probeTimer),this.probeTimer=void 0)}},Qn=Lr(Ea,"runtime"),Vn=Lr(Qn,"provider-health.json"),Mr=!1,Ia=null;Kn=null});import{existsSync as Hr,mkdirSync as $a,readFileSync as Na,writeFileSync as La}from"fs";import{join as zr}from"path";import{HOME_DIR as Da}from"@wengine-ai/claude-code-router-shared";function Xe(n,e,t){let o=c=>{if(t instanceof Headers)return t.get(c);let l=Object.keys(t).find(a=>a.toLowerCase()===c);return l?t[l]:null},r=Xn(o,Fa),s=Xn(o,Ua),i=Xn(o,ja);(r||s||i)&&Se.set(n,{provider:n,remaining:Ur(r),limit:Ur(s),reset:za(i),capturedAt:Date.now()})}function Ha(n){return Se.get(n)}function eo(){return Array.from(Se.values())}function Xn(n,e){for(let t of e){let o=n(t);if(o)return o}return null}function Ur(n){if(!n)return null;let e=Number(n.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function za(n){if(!n)return null;let e=n.trim();if(!e)return null;let t=Number(e);if(Number.isFinite(t))return t>1e12?Math.floor(t/1e3):t>1e9?Math.floor(t):Math.floor(Date.now()/1e3+t);let o=Date.parse(e);if(Number.isFinite(o))return Math.floor(o/1e3);let r=Ja(e);return r===null?null:Math.floor((Date.now()+r)/1e3)}function Ja(n){let e=/(\d+(?:\.\d+)?)(ms|s|m|h|d)/gi,t=0,o=!1;for(let r of n.matchAll(e)){o=!0;let s=Number(r[1]),i=r[2].toLowerCase();if(Number.isFinite(s))switch(i){case"ms":t+=s;break;case"s":t+=s*1e3;break;case"m":t+=s*60*1e3;break;case"h":t+=s*60*60*1e3;break;case"d":t+=s*24*60*60*1e3;break}}return o?t:null}function Ba(){try{if(!Hr(Zn))return;let n=JSON.parse(Na(Zn,"utf-8"));if(!Array.isArray(n))return;let e=Date.now();for(let t of n)if(t&&t.provider){if(t.capturedAt&&e-t.capturedAt>3600*1e3)continue;Se.set(t.provider,t)}}catch{}}function jr(){try{if(Se.size===0)return;Hr(qn)||$a(qn,{recursive:!0}),La(Zn,JSON.stringify(Array.from(Se.values()),null,2),"utf-8")}catch{}}function qe(){Dr||(Dr=!0,Ba(),Fr=setInterval(jr,6e4),Fr.unref?.(),process.on("exit",jr))}var qn,Zn,Se,Dr,Fr,Fa,Ua,ja,Re=w(()=>{"use strict";qn=zr(Da,"runtime"),Zn=zr(qn,"rate-limit.json"),Se=new Map,Dr=!1,Fr=null,Fa=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],Ua=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],ja=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"]});async function F(n){let e=await n.text();return Wa(e,n.status)}function Wa(n,e){let t=n.replace(/^/,"").trim();try{return JSON.parse(t)}catch{let o=t.length>200?`${t.slice(0,200)}...`:t,r=e!==void 0?` (HTTP ${e})`:"";throw new Error(`Upstream returned a non-JSON response${r}: ${o||"(empty body)"}`)}}function Jr(n){return(n.headers.get("Content-Type")||"").includes("text/event-stream")}function Ze(n){return/^\s*(event:|data:|:\s*[A-Z])/m.test(n)||n.startsWith("data:")}async function we(n){if(!n.body||n.body.locked)return null;let[e,t]=n.body.tee(),o=e.getReader(),r=new TextDecoder,s="";try{for(let i=0;i<8;i++){let{value:c,done:l}=await o.read();if(l||(c&&(s+=r.decode(c,{stream:!0})),Ze(s))||/^\s*[\[{]/.test(s))break}s+=r.decode()}finally{o.releaseLock()}return e.cancel().catch(()=>{}),{body:t,isSSE:Ze(s)}}async function Pe(n){try{let e=await n.text();if(Ze(e)){let o=new TextEncoder,r=new ReadableStream({start(s){s.enqueue(o.encode(e)),s.close()}});return{response:new Response(r,{status:n.status,statusText:n.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}}),isSSE:!0}}return{response:new Response(e,{status:n.status,statusText:n.statusText,headers:n.headers}),isSSE:!1}}catch{return{response:n,isSSE:!1}}}var Q=w(()=>{"use strict"});var Ee,to=w(()=>{"use strict";Q();Ee=class{name="openai-responses";endPoint="/v1/responses";getCacheReadInputTokens(e){return e?.cache_read_input_tokens??e?.input_tokens_details?.cached_tokens??e?.prompt_tokens_details?.cached_tokens??0}getCacheCreationInputTokens(e){return e?.cache_creation_input_tokens??e?.input_tokens_details?.cache_creation_tokens??e?.input_tokens_details?.cache_write_tokens??e?.prompt_tokens_details?.cache_creation_tokens??e?.prompt_tokens_details?.cache_write_tokens??0}buildResponsesUsage(e){let t=e?.input_tokens??e?.prompt_tokens??0,o=e?.output_tokens??e?.completion_tokens??0,r=this.getCacheReadInputTokens(e),s=this.getCacheCreationInputTokens(e);return{input_tokens:t,output_tokens:o,total_tokens:e?.total_tokens??t+o,input_tokens_details:{cached_tokens:r,cache_creation_tokens:s},output_tokens_details:{reasoning_tokens:e?.output_tokens_details?.reasoning_tokens??0},cache_read_input_tokens:r,cache_creation_input_tokens:s}}async transformRequestIn(e){delete e.temperature,delete e.max_tokens,e.reasoning&&(e.reasoning={effort:e.reasoning.effort,summary:e.reasoning.summary||"auto"});let t=[],o=e.messages.filter(r=>r.role==="system");if(o.length>0){let r=o[0];Array.isArray(r.content)?r.content.forEach(s=>{let i="";typeof s=="string"?i=s:s&&typeof s=="object"&&"text"in s&&(i=s.text),t.push({role:"system",content:i})}):e.instructions=r.content}if(e.messages.forEach(r=>{if(r.role!=="system"){if(Array.isArray(r.content)){let s=r.content.map(i=>this.normalizeRequestContent(i,r.role)).filter(i=>i!==null);s.length>0?r.content=s:delete r.content}if(r.role==="tool"){let s={...r};s.type="function_call_output",s.call_id=r.tool_call_id,s.output=r.content,delete s.cache_control,delete s.role,delete s.tool_call_id,delete s.content,t.push(s);return}if(r.role==="assistant"&&Array.isArray(r.tool_calls)){r.tool_calls.forEach(s=>{t.push({type:"function_call",arguments:s.function.arguments,name:s.function.name,call_id:s.id})});return}t.push(r)}}),e.input=t,delete e.messages,Array.isArray(e.tools)){let r=e.tools.find(s=>s.function.name==="web_search");e.tools=e.tools.filter(s=>s.function.name!=="web_search").map(s=>(s.function.name==="WebSearch"&&delete s.function.parameters.properties.allowed_domains,s.function.name==="Edit"?{type:s.type,name:s.function.name,description:s.function.description,parameters:{...s.function.parameters,required:["file_path","old_string","new_string","replace_all"]},strict:!0}:{type:s.type,name:s.function.name,description:s.function.description,parameters:s.function.parameters})),r&&e.tools.push({type:"web_search"})}return e.parallel_tool_calls=!1,e}async transformResponseOut(e){let t=e.headers.get("Content-Type")||"";if(t.includes("application/json")){let o=await we(e);if(o?.isSSE&&o.body){let r=new Response(o.body,{status:e.status,statusText:e.statusText,headers:new Headers({...Object.fromEntries(e.headers.entries()),"content-type":"text/event-stream"})});return this.transformResponseOut(r)}if(o?.body)e=new Response(o.body,{status:e.status,statusText:e.statusText,headers:e.headers});else if(o===null){let r=await Pe(e);if(r.isSSE)return this.transformResponseOut(r.response);e=r.response}}if(t.includes("application/json")){let o=await F(e);if(o.object==="response"&&o.output){let r=this.convertResponseToChat(o);return new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers})}return o.type==="message"&&Array.isArray(o.content)?new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers}):new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(t.includes("text/event-stream")){if(!e.body)return e;let[o,r]=e.body.tee(),s=o.getReader(),i="";try{let{value:g,done:_}=await s.read();!_&&g&&(i=new TextDecoder().decode(g))}finally{s.releaseLock()}if(o.cancel().catch(()=>{}),i.includes("message_start")||i.includes("content_block_start")){let g=this.prependToStream(i,r);return new Response(g,{status:e.status,statusText:e.statusText,headers:e.headers})}if(i.includes("response.created")||i.includes("response.output_text.delta")||i.includes("response.in_progress")||i.includes("response.output_item.added")){let g=this.prependToStream(i,r);return new Response(g,{status:e.status,statusText:e.statusText,headers:e.headers})}let a=this.prependToStream(i,r),u=new TextDecoder,d=new TextEncoder,m="",f=!1,y=this,p=new ReadableStream({async start(g){let _=a.getReader(),k=-1,h="",b=T=>(T!==h&&(k++,h=T),k);try{for(;;){let{done:T,value:x}=await _.read();if(T){f||g.enqueue(d.encode(`data: [DONE]
4
+ `);i=a.pop()||"";for(let u of a)u.trim()&&(s<o.maxStreamChunks?Rr(e,t,s,u):s===o.maxStreamChunks&&e.info({debug_log:!0,reqId:t,phase:"provider_stream_chunk_omitted",message:`[chunks after #${s} omitted, maxStreamChunks=${o.maxStreamChunks}]`}),s++)}i.trim()&&(s<o.maxStreamChunks&&Rr(e,t,s,i.trim()),s++),wr(e,t,s)}catch(i){console.error(`[debug_log] Error reading debug stream for reqId=${t}:`,i),wr(e,t,s)}finally{r.releaseLock()}})()}var Ca,xe,Jn,Ye=w(()=>{"use strict";Ca=new Set(["authorization","x-api-key","x-goog-api-key","cookie","set-cookie"]),xe={maxBodyLength:4096,maxStreamChunks:100},Jn=!1});function Ir(n,e,t,o,r){let s=new Headers({"Content-Type":"application/json"});t.headers&&Object.entries(t.headers).forEach(([a,u])=>{u&&s.set(a,u)});let i,c=AbortSignal.timeout(t.TIMEOUT??60*1e3*60);if(t.signal){let a=new AbortController,u=()=>a.abort();t.signal.addEventListener("abort",u),c.addEventListener("abort",u),i=a.signal}else i=c;let l={method:"POST",headers:s,body:JSON.stringify(e),signal:i};return t.httpsProxy&&(l.dispatcher=G(t.httpsProxy)),r?.debug({reqId:o.req.id,method:"POST",headers:Rt(Object.fromEntries(s.entries())),requestUrl:typeof n=="string"?n:n.toString(),useProxy:Sa(t.httpsProxy)},"final request"),fetch(typeof n=="string"?n:n.toString(),l)}function Sa(n){if(!(typeof n!="string"||!n.trim()))try{let e=new URL(n);return e.username||e.password?(e.username="",e.password="",`${e.toString()} (credentials redacted)`):e.toString()}catch{return"[invalid proxy URL]"}}var Mr=w(()=>{oe();Ye()});var Pt,Gn=w(()=>{Pt="2.3.2405"});var $r={};Hn($r,{getFallbackPromotionStore:()=>Se,resetFallbackPromotionStore:()=>Pa});function Se(n){return ye||(ye=new Kn,n&&ye.configure(n)),ye}function Pa(){ye&&(ye.clearAll(),ye=null)}var wa,Kn,ye,Et=w(()=>{"use strict";re();wa=600*1e3,Kn=class{promotions=new Map;enabled=!0;ttlMs=wa;configure(e){e?.enabled!==void 0&&(this.enabled=e.enabled),e?.ttlMinutes!==void 0&&(this.ttlMs=e.ttlMinutes*60*1e3)}getKey(e,t,o){return`${e},${t}:${o}`}promote(e,t,o,r,s,i){if(!this.enabled)return;let c=this.getKey(e,t,o),l=`${r},${s}`;this.promotions.set(c,{promotedModel:l,timestamp:Date.now(),ttlMs:i??this.ttlMs})}getPromotion(e,t,o,r){if(!this.enabled)return null;let s=this.getKey(e,t,o),i=this.promotions.get(s);if(!i)return null;if(Date.now()-i.timestamp>=i.ttlMs)return this.promotions.delete(s),null;let[l,...a]=i.promotedModel.split(","),u=a.join(",");if(!U().isAvailable(l,u))return this.promotions.delete(s),null;let f=r.find(y=>y.name.toLowerCase()===l.toLowerCase()),g=f?.models?.find(y=>String(y).toLowerCase()===u.toLowerCase());return!f||!g||f.enabled===!1?(this.promotions.delete(s),null):`${f.name},${g}`}clear(e,t,o){let r=this.getKey(e,t,o);this.promotions.delete(r)}clearByPrimary(e,t){let o=`${e},${t}:`,r=0;for(let s of this.promotions.keys())s.startsWith(o)&&(this.promotions.delete(s),r++);return r}clearAll(){this.promotions.clear()}clearExpired(){let e=Date.now();for(let[t,o]of this.promotions)e-o.timestamp>=o.ttlMs&&this.promotions.delete(t)}getAllPromotions(){let e=Date.now(),t=[];for(let[o,r]of this.promotions){let s=r.ttlMs-(e-r.timestamp);s>0&&t.push({key:o,promotedModel:r.promotedModel,remainingMs:s})}return t}},ye=null});import{existsSync as Dr,mkdirSync as Ea,readFileSync as Oa,writeFileSync as Aa}from"fs";import{join as Fr}from"path";import{HOME_DIR as Ia}from"@wengine-ai/claude-code-router-shared";function La(n){try{if(!Dr(Yn))return;let e=JSON.parse(Oa(Yn,"utf-8"));if(!Array.isArray(e))return;let t=Date.now();for(let o of e){if(!o||!o.provider||!o.model)continue;let r=o.updatedAt||o.lastFailureTime||0;r&&t-r>1440*60*1e3||n.restore({provider:o.provider,model:o.model,status:o.status,failureCount:o.failureCount??0,successCount:o.successCount??0,lastFailureTime:o.lastFailureTime??0,lastProbeTime:o.lastProbeTime??0,lastError:o.lastError,rateLimitUntil:o.rateLimitUntil})}}catch{}}function Lr(n){try{let e=n.getAllStates();if(e.length===0)return;Dr(Vn)||Ea(Vn,{recursive:!0});let t=e.map(o=>({...o,updatedAt:Date.now()}));Aa(Yn,JSON.stringify(t,null,2),"utf-8")}catch{}}function Xn(){if(Nr)return;Nr=!0;let n=U();La(n),Na=setInterval(()=>Lr(n),6e4),process.on("exit",()=>Lr(n))}function U(n){return Qn||(Qn=new Ot(n)),Qn}var Ma,$a,Ot,Vn,Yn,Nr,Na,Qn,re=w(()=>{"use strict";Ma=120,$a={enabled:!0,failureThreshold:3,probeIntervalMinutes:5,halfOpenSuccessThreshold:2},Ot=class{states=new Map;config;probeTimer;constructor(e){this.config={...$a,...e}}getKey(e,t){return!e||!t?null:`${e},${t}`}recordSuccess(e,t){if(!this.config.enabled)return;let o=this.getKey(e,t);if(!o)return;let r=this.states.get(o);r&&(r.successCount++,!r.rateLimitUntil&&(r.status==="half-open"?r.successCount>=this.config.halfOpenSuccessThreshold&&(this.clearPromotionsForPrimary(e,t),this.states.delete(o)):r.status==="open"&&(r.status="half-open",r.successCount=1,r.failureCount=0)))}clearPromotionsForPrimary(e,t){try{let{getFallbackPromotionStore:o}=(Et(),ga($r));o().clearByPrimary(e,t)}catch{}}recordFailure(e,t,o){if(!this.config.enabled)return;let r=this.getKey(e,t);if(!r)return;let s=this.states.get(r);s||(s={provider:e,model:t,status:"closed",failureCount:0,successCount:0,lastFailureTime:0,lastProbeTime:0},this.states.set(r,s)),s.failureCount++,s.lastFailureTime=Date.now(),s.lastError=o,s.status==="half-open"?(s.status="open",s.successCount=0,s.lastProbeTime=0):s.status==="closed"&&s.failureCount>=this.config.failureThreshold&&(s.status="open",s.lastProbeTime=0)}getState(e,t){let o=this.getKey(e,t);if(o)return this.states.get(o)}isAvailable(e,t){if(!this.config.enabled)return!0;if(!this.getKey(e,t))return!1;let r=this.getState(e,t);return r?r.status==="open"&&r.rateLimitUntil?!(Date.now()<r.rateLimitUntil):r.status!=="open":!0}getPriority(e,t){if(!this.config.enabled)return 0;if(!this.getKey(e,t))return 2;let r=this.getState(e,t);if(!r)return 0;switch(r.status){case"closed":return 0;case"half-open":return 1;case"open":return 2}}getHealthyModels(){return[]}getFailPoolModels(){let e=[];for(let[t,o]of this.states)o.status==="open"&&e.push(t);return e}getHalfOpenModels(){let e=[];for(let[t,o]of this.states)o.status==="half-open"&&e.push(t);return e}forceOpen(e,t,o){if(!this.config.enabled)return;let r=this.getKey(e,t);if(!r)return;let s=this.states.get(r);s?(s.status="open",s.failureCount=Math.max(s.failureCount,this.config.failureThreshold),s.lastFailureTime=Date.now(),s.lastError=o,s.lastProbeTime=0):(s={provider:e,model:t,status:"open",failureCount:this.config.failureThreshold,successCount:0,lastFailureTime:Date.now(),lastProbeTime:0,lastError:o},this.states.set(r,s))}markRateLimited(e,t,o,r){if(!this.config.enabled)return;let s=this.getKey(e,t);if(!s)return;let i=this.states.get(s);i?(i.status="open",i.lastFailureTime=Date.now(),i.lastError=r||"rate-limited",i.lastProbeTime=0):(i={provider:e,model:t,status:"open",failureCount:0,successCount:0,lastFailureTime:Date.now(),lastProbeTime:0,lastError:r||"rate-limited"},this.states.set(s,i));let c=(o??Ma)*1e3;i.rateLimitUntil=Date.now()+c}getRateLimitedModels(){let e=[];for(let[t,o]of this.states)o.status==="open"&&o.rateLimitUntil&&e.push(t);return e}needsProbe(e){return Date.now()-e.lastProbeTime>=this.config.probeIntervalMinutes*60*1e3}markProbeAttempt(e,t){let o=this.getKey(e,t);if(!o)return;let r=this.states.get(o);r&&r.status==="open"&&(r.lastProbeTime=Date.now())}clear(){this.states.clear()}recover(e,t){let o=this.getKey(e,t);o&&this.states.delete(o)}getAllStates(){return Array.from(this.states.values())}restore(e){let t=this.getKey(e.provider,e.model);t&&this.states.set(t,{...e})}stopProbeTimer(){this.probeTimer&&(clearInterval(this.probeTimer),this.probeTimer=void 0)}},Vn=Fr(Ia,"runtime"),Yn=Fr(Vn,"provider-health.json"),Nr=!1,Na=null;Qn=null});import{existsSync as Br,mkdirSync as Da,readFileSync as Fa,writeFileSync as Ua}from"fs";import{join as Jr}from"path";import{HOME_DIR as ja}from"@wengine-ai/claude-code-router-shared";function Xe(n,e,t){let o=c=>{if(t instanceof Headers)return t.get(c);let l=Object.keys(t).find(a=>a.toLowerCase()===c);return l?t[l]:null},r=Zn(o,Ha),s=Zn(o,za),i=Zn(o,Ba);(r||s||i)&&Re.set(n,{provider:n,remaining:Hr(r),limit:Hr(s),reset:Wa(i),capturedAt:Date.now()})}function Ja(n){return Re.get(n)}function to(){return Array.from(Re.values())}function Zn(n,e){for(let t of e){let o=n(t);if(o)return o}return null}function Hr(n){if(!n)return null;let e=Number(n.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function Wa(n){if(!n)return null;let e=n.trim();if(!e)return null;let t=Number(e);if(Number.isFinite(t))return t>1e12?Math.floor(t/1e3):t>1e9?Math.floor(t):Math.floor(Date.now()/1e3+t);let o=Date.parse(e);if(Number.isFinite(o))return Math.floor(o/1e3);let r=Ga(e);return r===null?null:Math.floor((Date.now()+r)/1e3)}function Ga(n){let e=/(\d+(?:\.\d+)?)(ms|s|m|h|d)/gi,t=0,o=!1;for(let r of n.matchAll(e)){o=!0;let s=Number(r[1]),i=r[2].toLowerCase();if(Number.isFinite(s))switch(i){case"ms":t+=s;break;case"s":t+=s*1e3;break;case"m":t+=s*60*1e3;break;case"h":t+=s*60*60*1e3;break;case"d":t+=s*24*60*60*1e3;break}}return o?t:null}function Ka(){try{if(!Br(eo))return;let n=JSON.parse(Fa(eo,"utf-8"));if(!Array.isArray(n))return;let e=Date.now();for(let t of n)if(t&&t.provider){if(t.capturedAt&&e-t.capturedAt>3600*1e3)continue;Re.set(t.provider,t)}}catch{}}function zr(){try{if(Re.size===0)return;Br(qn)||Da(qn,{recursive:!0}),Ua(eo,JSON.stringify(Array.from(Re.values()),null,2),"utf-8")}catch{}}function Ze(){Ur||(Ur=!0,Ka(),jr=setInterval(zr,6e4),jr.unref?.(),process.on("exit",zr))}var qn,eo,Re,Ur,jr,Ha,za,Ba,we=w(()=>{"use strict";qn=Jr(ja,"runtime"),eo=Jr(qn,"rate-limit.json"),Re=new Map,Ur=!1,jr=null,Ha=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],za=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],Ba=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"]});async function F(n){let e=await n.text();return Qa(e,n.status)}function Qa(n,e){let t=n.replace(/^/,"").trim();try{return JSON.parse(t)}catch{let o=t.length>200?`${t.slice(0,200)}...`:t,r=e!==void 0?` (HTTP ${e})`:"";throw new Error(`Upstream returned a non-JSON response${r}: ${o||"(empty body)"}`)}}function Wr(n){return(n.headers.get("Content-Type")||"").includes("text/event-stream")}function qe(n){return/^\s*(event:|data:|:\s*[A-Z])/m.test(n)||n.startsWith("data:")}async function Pe(n){if(!n.body||n.body.locked)return null;let[e,t]=n.body.tee(),o=e.getReader(),r=new TextDecoder,s="";try{for(let i=0;i<8;i++){let{value:c,done:l}=await o.read();if(l||(c&&(s+=r.decode(c,{stream:!0})),qe(s))||/^\s*[\[{]/.test(s))break}s+=r.decode()}finally{o.releaseLock()}return e.cancel().catch(()=>{}),{body:t,isSSE:qe(s)}}async function Ee(n){try{let e=await n.text();if(qe(e)){let o=new TextEncoder,r=new ReadableStream({start(s){s.enqueue(o.encode(e)),s.close()}});return{response:new Response(r,{status:n.status,statusText:n.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}}),isSSE:!0}}return{response:new Response(e,{status:n.status,statusText:n.statusText,headers:n.headers}),isSSE:!1}}catch{return{response:n,isSSE:!1}}}var Q=w(()=>{"use strict"});var Oe,no=w(()=>{"use strict";Q();Oe=class{name="openai-responses";endPoint="/v1/responses";getCacheReadInputTokens(e){return e?.cache_read_input_tokens??e?.input_tokens_details?.cached_tokens??e?.prompt_tokens_details?.cached_tokens??0}getCacheCreationInputTokens(e){return e?.cache_creation_input_tokens??e?.input_tokens_details?.cache_creation_tokens??e?.input_tokens_details?.cache_write_tokens??e?.prompt_tokens_details?.cache_creation_tokens??e?.prompt_tokens_details?.cache_write_tokens??0}buildResponsesUsage(e){let t=e?.input_tokens??e?.prompt_tokens??0,o=e?.output_tokens??e?.completion_tokens??0,r=this.getCacheReadInputTokens(e),s=this.getCacheCreationInputTokens(e);return{input_tokens:t,output_tokens:o,total_tokens:e?.total_tokens??t+o,input_tokens_details:{cached_tokens:r,cache_creation_tokens:s},output_tokens_details:{reasoning_tokens:e?.output_tokens_details?.reasoning_tokens??0},cache_read_input_tokens:r,cache_creation_input_tokens:s}}async transformRequestIn(e){delete e.temperature,delete e.max_tokens,e.reasoning&&(e.reasoning={effort:e.reasoning.effort,summary:e.reasoning.summary||"auto"});let t=[],o=e.messages.filter(r=>r.role==="system");if(o.length>0){let r=o[0];Array.isArray(r.content)?r.content.forEach(s=>{let i="";typeof s=="string"?i=s:s&&typeof s=="object"&&"text"in s&&(i=s.text),t.push({role:"system",content:i})}):e.instructions=r.content}if(e.messages.forEach(r=>{if(r.role!=="system"){if(Array.isArray(r.content)){let s=r.content.map(i=>this.normalizeRequestContent(i,r.role)).filter(i=>i!==null);s.length>0?r.content=s:delete r.content}if(r.role==="tool"){let s={...r};s.type="function_call_output",s.call_id=r.tool_call_id,s.output=r.content,delete s.cache_control,delete s.role,delete s.tool_call_id,delete s.content,t.push(s);return}if(r.role==="assistant"&&Array.isArray(r.tool_calls)){r.tool_calls.forEach(s=>{t.push({type:"function_call",arguments:s.function.arguments,name:s.function.name,call_id:s.id})});return}t.push(r)}}),e.input=t,delete e.messages,Array.isArray(e.tools)){let r=e.tools.find(s=>s.function.name==="web_search");e.tools=e.tools.filter(s=>s.function.name!=="web_search").map(s=>(s.function.name==="WebSearch"&&delete s.function.parameters.properties.allowed_domains,s.function.name==="Edit"?{type:s.type,name:s.function.name,description:s.function.description,parameters:{...s.function.parameters,required:["file_path","old_string","new_string","replace_all"]},strict:!0}:{type:s.type,name:s.function.name,description:s.function.description,parameters:s.function.parameters})),r&&e.tools.push({type:"web_search"})}return e.parallel_tool_calls=!1,e}async transformResponseOut(e){let t=e.headers.get("Content-Type")||"";if(t.includes("application/json")){let o=await Pe(e);if(o?.isSSE&&o.body){let r=new Response(o.body,{status:e.status,statusText:e.statusText,headers:new Headers({...Object.fromEntries(e.headers.entries()),"content-type":"text/event-stream"})});return this.transformResponseOut(r)}if(o?.body)e=new Response(o.body,{status:e.status,statusText:e.statusText,headers:e.headers});else if(o===null){let r=await Ee(e);if(r.isSSE)return this.transformResponseOut(r.response);e=r.response}}if(t.includes("application/json")){let o=await F(e);if(o.object==="response"&&o.output){let r=this.convertResponseToChat(o);return new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers})}return o.type==="message"&&Array.isArray(o.content)?new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers}):new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(t.includes("text/event-stream")){if(!e.body)return e;let[o,r]=e.body.tee(),s=o.getReader(),i="";try{let{value:p,done:_}=await s.read();!_&&p&&(i=new TextDecoder().decode(p))}finally{s.releaseLock()}if(o.cancel().catch(()=>{}),i.includes("message_start")||i.includes("content_block_start")){let p=this.prependToStream(i,r);return new Response(p,{status:e.status,statusText:e.statusText,headers:e.headers})}if(i.includes("response.created")||i.includes("response.output_text.delta")||i.includes("response.in_progress")||i.includes("response.output_item.added")){let p=this.prependToStream(i,r);return new Response(p,{status:e.status,statusText:e.statusText,headers:e.headers})}let a=this.prependToStream(i,r),u=new TextDecoder,d=new TextEncoder,f="",g=!1,y=this,m=new ReadableStream({async start(p){let _=a.getReader(),k=-1,h="",b=T=>(T!==h&&(k++,h=T),k);try{for(;;){let{done:T,value:x}=await _.read();if(T){g||p.enqueue(d.encode(`data: [DONE]
5
5
 
6
- `));break}let C=u.decode(x,{stream:!0});m+=C;let S=m.split(/\r?\n/);m=S.pop()||"";for(let v of S)if(v.trim())try{if(v.startsWith("event: "))continue;if(v.startsWith("data: ")){let P=v.slice(5).trim();if(P==="[DONE]"){f=!0,g.enqueue(d.encode(`data: [DONE]
6
+ `));break}let C=u.decode(x,{stream:!0});f+=C;let S=f.split(/\r?\n/);f=S.pop()||"";for(let v of S)if(v.trim())try{if(v.startsWith("event: "))continue;if(v.startsWith("data: ")){let P=v.slice(5).trim();if(P==="[DONE]"){g=!0,p.enqueue(d.encode(`data: [DONE]
7
7
 
8
- `));continue}try{let R=JSON.parse(P);if(R.type==="response.output_text.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:b(R.type),delta:{content:R.delta||""},finish_reason:null}]};g.enqueue(d.encode(`data: ${JSON.stringify(O)}
8
+ `));continue}try{let R=JSON.parse(P);if(R.type==="response.output_text.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:b(R.type),delta:{content:R.delta||""},finish_reason:null}]};p.enqueue(d.encode(`data: ${JSON.stringify(O)}
9
9
 
10
- `))}else if(R.type==="response.output_item.added"&&R.item?.type==="function_call"){let O={id:R.item.call_id||R.item.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:b(R.type),delta:{role:"assistant",tool_calls:[{index:0,id:R.item.call_id||R.item.id,function:{name:R.item.name||"",arguments:""},type:"function"}]},finish_reason:null}]};g.enqueue(d.encode(`data: ${JSON.stringify(O)}
10
+ `))}else if(R.type==="response.output_item.added"&&R.item?.type==="function_call"){let O={id:R.item.call_id||R.item.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:b(R.type),delta:{role:"assistant",tool_calls:[{index:0,id:R.item.call_id||R.item.id,function:{name:R.item.name||"",arguments:""},type:"function"}]},finish_reason:null}]};p.enqueue(d.encode(`data: ${JSON.stringify(O)}
11
11
 
12
- `))}else if(R.type==="response.output_item.added"&&R.item?.type==="message"){let O=[];(R.item.content||[]).forEach(A=>{A.type==="output_text"&&O.push({type:"text",text:A.text||""})});let E={role:"assistant"};if(O.length===1&&O[0].type==="text"?E.content=O[0].text:O.length>0&&(E.content=O),E.content){let A={id:R.item.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:b(R.type),delta:E,finish_reason:null}]};g.enqueue(d.encode(`data: ${JSON.stringify(A)}
12
+ `))}else if(R.type==="response.output_item.added"&&R.item?.type==="message"){let O=[];(R.item.content||[]).forEach(A=>{A.type==="output_text"&&O.push({type:"text",text:A.text||""})});let E={role:"assistant"};if(O.length===1&&O[0].type==="text"?E.content=O[0].text:O.length>0&&(E.content=O),E.content){let A={id:R.item.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:b(R.type),delta:E,finish_reason:null}]};p.enqueue(d.encode(`data: ${JSON.stringify(A)}
13
13
 
14
- `))}}else if(R.type==="response.output_text.annotation.added"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex",choices:[{index:b(R.type),delta:{annotations:[{type:"url_citation",url_citation:{url:R.annotation?.url||"",title:R.annotation?.title||"",content:"",start_index:R.annotation?.start_index||0,end_index:R.annotation?.end_index||0}}]},finish_reason:null}]};g.enqueue(d.encode(`data: ${JSON.stringify(O)}
14
+ `))}}else if(R.type==="response.output_text.annotation.added"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex",choices:[{index:b(R.type),delta:{annotations:[{type:"url_citation",url_citation:{url:R.annotation?.url||"",title:R.annotation?.title||"",content:"",start_index:R.annotation?.start_index||0,end_index:R.annotation?.end_index||0}}]},finish_reason:null}]};p.enqueue(d.encode(`data: ${JSON.stringify(O)}
15
15
 
16
- `))}else if(R.type==="response.function_call_arguments.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:b(R.type),delta:{tool_calls:[{index:0,function:{arguments:R.delta||""}}]},finish_reason:null}]};g.enqueue(d.encode(`data: ${JSON.stringify(O)}
16
+ `))}else if(R.type==="response.function_call_arguments.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:b(R.type),delta:{tool_calls:[{index:0,function:{arguments:R.delta||""}}]},finish_reason:null}]};p.enqueue(d.encode(`data: ${JSON.stringify(O)}
17
17
 
18
- `))}else if(R.type==="response.completed"){let O=R.response?.output?.some(A=>A.type==="function_call")?"tool_calls":"stop",E={id:R.response?.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:0,delta:{},finish_reason:O}]};g.enqueue(d.encode(`data: ${JSON.stringify(E)}
18
+ `))}else if(R.type==="response.completed"){let O=R.response?.output?.some(A=>A.type==="function_call")?"tool_calls":"stop",E={id:R.response?.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:0,delta:{},finish_reason:O}]};p.enqueue(d.encode(`data: ${JSON.stringify(E)}
19
19
 
20
- `)),f=!0}else if(R.type==="response.reasoning_summary_text.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:b(R.type),delta:{thinking:{content:R.delta||""}},finish_reason:null}]};g.enqueue(d.encode(`data: ${JSON.stringify(O)}
20
+ `)),g=!0}else if(R.type==="response.reasoning_summary_text.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:b(R.type),delta:{thinking:{content:R.delta||""}},finish_reason:null}]};p.enqueue(d.encode(`data: ${JSON.stringify(O)}
21
21
 
22
- `))}else if(R.type==="response.reasoning_summary_part.done"&&R.part){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:k,delta:{thinking:{signature:R.item_id}},finish_reason:null}]};g.enqueue(d.encode(`data: ${JSON.stringify(O)}
22
+ `))}else if(R.type==="response.reasoning_summary_part.done"&&R.part){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:k,delta:{thinking:{signature:R.item_id}},finish_reason:null}]};p.enqueue(d.encode(`data: ${JSON.stringify(O)}
23
23
 
24
- `))}}catch{g.enqueue(d.encode(v+`
25
- `))}}else g.enqueue(d.encode(v+`
26
- `))}catch(P){console.error("Error processing line:",v,P),g.enqueue(d.encode(v+`
27
- `))}}m.trim()&&g.enqueue(d.encode(m+`
28
- `)),f||g.enqueue(d.encode(`data: [DONE]
24
+ `))}}catch{p.enqueue(d.encode(v+`
25
+ `))}}else p.enqueue(d.encode(v+`
26
+ `))}catch(P){console.error("Error processing line:",v,P),p.enqueue(d.encode(v+`
27
+ `))}}f.trim()&&p.enqueue(d.encode(f+`
28
+ `)),g||p.enqueue(d.encode(`data: [DONE]
29
29
 
30
- `))}catch(T){console.error("Stream error:",T),g.error(T)}finally{try{_.releaseLock()}catch(T){console.error("Error releasing reader lock:",T)}g.close()}}});return new Response(p,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive","Access-Control-Allow-Origin":"*"}})}return e}normalizeRequestContent(e,t){let o={...e};if(delete o.cache_control,e.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:e.text};if(e.type==="image_url"){console.log(e);let r={type:t==="assistant"?"output_image":"input_image"};return typeof e.image_url?.url=="string"&&(r.image_url=e.image_url.url),r}return null}async transformResponseIn(e,t){if(e.headers.get("Content-Type")?.includes("text/event-stream")&&e.body){let[r,s]=e.body.tee(),i=r.getReader(),c="";try{let{value:m,done:f}=await i.read();!f&&m&&(c=new TextDecoder().decode(m))}finally{i.releaseLock()}r.cancel().catch(()=>{});let l=c.includes("message_start")||c.includes("content_block_start"),a=c.includes("chat.completion.chunk");if(c.includes("response.created")||c.includes("response.output_text.delta")||c.includes("response.in_progress")||c.includes("response.output_item.added"))return new Response(s,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}});if(a&&!l){let m=this.convertOpenAIChatStreamToResponsesAPI(s,t);return new Response(m,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}let d=this.convertAnthropicStreamToResponsesAPI(s,t);return new Response(d,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}try{let r=await F(e);return new Response(JSON.stringify(this.wrapChatInResponses(r)),{headers:{"Content-Type":"application/json"}})}catch{return e}}prependToStream(e,t){let r=new TextEncoder().encode(e),s=t.getReader(),i=!1;return new ReadableStream({async pull(c){i||(i=!0,c.enqueue(r));try{let{done:l,value:a}=await s.read();l?c.close():c.enqueue(a)}catch(l){c.error(l)}},cancel(){s.cancel().catch(()=>{})}})}convertOpenAIChatStreamToResponsesAPI(e,t){let o=new TextDecoder,r=new TextEncoder,s=t?.provider?.models?.[0]||"gpt-5.4",i=`resp_${Date.now()}`,c=0,l=!1,a=!1,u="",d=-1,m=[],f=(p,g)=>r.encode(`event: ${p}
31
- data: ${JSON.stringify(g)}
30
+ `))}catch(T){console.error("Stream error:",T),p.error(T)}finally{try{_.releaseLock()}catch(T){console.error("Error releasing reader lock:",T)}p.close()}}});return new Response(m,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive","Access-Control-Allow-Origin":"*"}})}return e}normalizeRequestContent(e,t){let o={...e};if(delete o.cache_control,e.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:e.text};if(e.type==="image_url"){console.log(e);let r={type:t==="assistant"?"output_image":"input_image"};return typeof e.image_url?.url=="string"&&(r.image_url=e.image_url.url),r}return null}async transformResponseIn(e,t){if(e.headers.get("Content-Type")?.includes("text/event-stream")&&e.body){let[r,s]=e.body.tee(),i=r.getReader(),c="";try{let{value:f,done:g}=await i.read();!g&&f&&(c=new TextDecoder().decode(f))}finally{i.releaseLock()}r.cancel().catch(()=>{});let l=c.includes("message_start")||c.includes("content_block_start"),a=c.includes("chat.completion.chunk");if(c.includes("response.created")||c.includes("response.output_text.delta")||c.includes("response.in_progress")||c.includes("response.output_item.added"))return new Response(s,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}});if(a&&!l){let f=this.convertOpenAIChatStreamToResponsesAPI(s,t);return new Response(f,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}let d=this.convertAnthropicStreamToResponsesAPI(s,t);return new Response(d,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}try{let r=await F(e);return new Response(JSON.stringify(this.wrapChatInResponses(r)),{headers:{"Content-Type":"application/json"}})}catch{return e}}prependToStream(e,t){let r=new TextEncoder().encode(e),s=t.getReader(),i=!1;return new ReadableStream({async pull(c){i||(i=!0,c.enqueue(r));try{let{done:l,value:a}=await s.read();l?c.close():c.enqueue(a)}catch(l){c.error(l)}},cancel(){s.cancel().catch(()=>{})}})}convertOpenAIChatStreamToResponsesAPI(e,t){let o=new TextDecoder,r=new TextEncoder,s=t?.provider?.models?.[0]||"gpt-5.4",i=`resp_${Date.now()}`,c=0,l=!1,a=!1,u="",d=-1,f=[],g=(m,p)=>r.encode(`event: ${m}
31
+ data: ${JSON.stringify(p)}
32
32
 
33
- `),y=(p,g=[])=>({id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:p,model:s,output:g,usage:this.buildResponsesUsage()});return new ReadableStream({async start(p){try{let g=e.getReader(),_="",k=!1,h=b=>{if(!k){k=!0;for(let T of m)if(T.type==="function_call"){let x=m.indexOf(T);p.enqueue(f("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:T.call_id||T.id,output_index:x,arguments:T.arguments||""})),T.status="completed",p.enqueue(f("response.output_item.done",{type:"response.output_item.done",output_index:x,item:T}))}for(let T of m)if(T.type==="message"){let x=m.indexOf(T);T.status="completed",p.enqueue(f("response.output_item.done",{type:"response.output_item.done",output_index:x,item:T}))}p.enqueue(f("response.completed",{type:"response.completed",response:{id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:"completed",model:b?.model||s,output:m.slice(),usage:b?.usage?{...this.buildResponsesUsage(b.usage)}:this.buildResponsesUsage()}}))}};for(;;){let{done:b,value:T}=await g.read();if(b)break;_+=o.decode(T,{stream:!0});let x=_.split(`
34
- `);_=x.pop()||"";for(let C of x){let S=C.trim();if(!S||S.startsWith("event:")||!S.startsWith("data:"))continue;let v=S.slice(5).trim();if(v==="[DONE]"){h({}),p.enqueue(r.encode(`data: [DONE]
33
+ `),y=(m,p=[])=>({id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:m,model:s,output:p,usage:this.buildResponsesUsage()});return new ReadableStream({async start(m){try{let p=e.getReader(),_="",k=!1,h=b=>{if(!k){k=!0;for(let T of f)if(T.type==="function_call"){let x=f.indexOf(T);m.enqueue(g("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:T.call_id||T.id,output_index:x,arguments:T.arguments||""})),T.status="completed",m.enqueue(g("response.output_item.done",{type:"response.output_item.done",output_index:x,item:T}))}for(let T of f)if(T.type==="message"){let x=f.indexOf(T);T.status="completed",m.enqueue(g("response.output_item.done",{type:"response.output_item.done",output_index:x,item:T}))}m.enqueue(g("response.completed",{type:"response.completed",response:{id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:"completed",model:b?.model||s,output:f.slice(),usage:b?.usage?{...this.buildResponsesUsage(b.usage)}:this.buildResponsesUsage()}}))}};for(;;){let{done:b,value:T}=await p.read();if(b)break;_+=o.decode(T,{stream:!0});let x=_.split(`
34
+ `);_=x.pop()||"";for(let C of x){let S=C.trim();if(!S||S.startsWith("event:")||!S.startsWith("data:"))continue;let v=S.slice(5).trim();if(v==="[DONE]"){h({}),m.enqueue(r.encode(`data: [DONE]
35
35
 
36
- `));continue}let P;try{P=JSON.parse(v)}catch{continue}l||(l=!0,p.enqueue(f("response.created",{type:"response.created",response:y("in_progress")})),p.enqueue(f("response.in_progress",{type:"response.in_progress",response:y("in_progress")})));let R=P.choices?.[0];if(!R)continue;let O=R.delta;if(O?.content){if(!a){a=!0;let E=c++,A={id:`${i}_msg`,type:"message",status:"in_progress",role:"assistant",content:[]};m.push(A),p.enqueue(f("response.output_item.added",{type:"response.output_item.added",output_index:E,item:A}))}p.enqueue(f("response.output_text.delta",{type:"response.output_text.delta",item_id:`${i}_msg`,output_index:c-1,content_index:0,delta:O.content}))}if(Array.isArray(O?.tool_calls))for(let E of O.tool_calls){if(E.id){u=E.id,d=c++;let A={id:E.id,type:"function_call",status:"in_progress",call_id:E.id,name:E.function?.name||"",arguments:""};m.push(A),p.enqueue(f("response.output_item.added",{type:"response.output_item.added",output_index:d,item:A}))}if(E.function?.arguments){p.enqueue(f("response.function_call_arguments.delta",{type:"response.function_call_arguments.delta",item_id:u,output_index:d,delta:E.function.arguments}));let A=m.find(N=>N.call_id===u);A&&(A.arguments+=E.function.arguments)}}R.finish_reason&&h(P)}}k||h({}),p.close()}catch(g){p.error(g)}}})}convertAnthropicStreamToResponsesAPI(e,t){let o=new TextDecoder,r=new TextEncoder,s=t?.provider?.models?.[0]||"gpt-5.4",i=`resp_${Date.now()}`,c="",l="",a=-1,u="",d="",m="",f=-1,y="",p="",g=-1,_=0,k=!1,h=!1,b=!1,T=!1,x=null,C=[],S=(A,N)=>r.encode(`event: ${A}
36
+ `));continue}let P;try{P=JSON.parse(v)}catch{continue}l||(l=!0,m.enqueue(g("response.created",{type:"response.created",response:y("in_progress")})),m.enqueue(g("response.in_progress",{type:"response.in_progress",response:y("in_progress")})));let R=P.choices?.[0];if(!R)continue;let O=R.delta;if(O?.content){if(!a){a=!0;let E=c++,A={id:`${i}_msg`,type:"message",status:"in_progress",role:"assistant",content:[]};f.push(A),m.enqueue(g("response.output_item.added",{type:"response.output_item.added",output_index:E,item:A}))}m.enqueue(g("response.output_text.delta",{type:"response.output_text.delta",item_id:`${i}_msg`,output_index:c-1,content_index:0,delta:O.content}))}if(Array.isArray(O?.tool_calls))for(let E of O.tool_calls){if(E.id){u=E.id,d=c++;let A={id:E.id,type:"function_call",status:"in_progress",call_id:E.id,name:E.function?.name||"",arguments:""};f.push(A),m.enqueue(g("response.output_item.added",{type:"response.output_item.added",output_index:d,item:A}))}if(E.function?.arguments){m.enqueue(g("response.function_call_arguments.delta",{type:"response.function_call_arguments.delta",item_id:u,output_index:d,delta:E.function.arguments}));let A=f.find(N=>N.call_id===u);A&&(A.arguments+=E.function.arguments)}}R.finish_reason&&h(P)}}k||h({}),m.close()}catch(p){m.error(p)}}})}convertAnthropicStreamToResponsesAPI(e,t){let o=new TextDecoder,r=new TextEncoder,s=t?.provider?.models?.[0]||"gpt-5.4",i=`resp_${Date.now()}`,c="",l="",a=-1,u="",d="",f="",g=-1,y="",m="",p=-1,_=0,k=!1,h=!1,b=!1,T=!1,x=null,C=[],S=(A,N)=>r.encode(`event: ${A}
37
37
  data: ${JSON.stringify(N)}
38
38
 
39
39
  `),v=(A,N=[])=>({id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:A,model:s,output:N,usage:this.buildResponsesUsage()}),P=()=>{k||(k=!0,E.enqueue(S("response.created",{type:"response.created",response:v("in_progress")})),E.enqueue(S("response.in_progress",{type:"response.in_progress",response:v("in_progress")})))},R=(A,N)=>{E.enqueue(S("response.output_item.done",{type:"response.output_item.done",output_index:A,item:{...N,status:"completed"}}))},O=A=>{if(!T){if(T=!0,h){let N=C.find(D=>D.type==="message");N&&(N.content=[{type:"output_text",text:c}],N.status="completed")}for(let N of C)N.type==="function_call"&&(N.status="completed");return S("response.completed",{type:"response.completed",response:{id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:"completed",model:s,output:C,usage:A?{...this.buildResponsesUsage(A)}:this.buildResponsesUsage()}})}},E;return new ReadableStream({async start(A){E=A;try{let N=e.getReader(),D="";for(;;){let{done:$,value:L}=await N.read();if($)break;D+=o.decode(L,{stream:!0});let W=D.split(`
40
- `);D=W.pop()||"";for(let ge of W){let j=ge.trim();if(!j||j.startsWith("event:")||!j.startsWith("data:"))continue;let Y=j.slice(5).trim();if(Y==="[DONE]")continue;let z;try{z=JSON.parse(Y)}catch{continue}if(z.type==="message_start"&&(i=z.message?.id||i,P()),z.type==="content_block_start"){let I=z.content_block;if(x=I?.type||null,I?.type==="tool_use"){u=I.id||"",d=I.name||"",m="",f=_++;let J={id:u,type:"function_call",status:"in_progress",call_id:u,name:d,arguments:""};C.push(J),E.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:f,item:J}))}if(I?.type==="thinking"&&(y=I.thinking||"",!b)){b=!0,g=_++,p=`rs_${i}`;let J={id:p,type:"reasoning",status:"in_progress",summary:[]};C.push(J),E.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:g,item:J}))}if(I?.type==="text"&&(c=I?.text||"",!h)){h=!0,a=_++,l=`${i}_msg`;let J={id:l,type:"message",status:"in_progress",role:"assistant",content:[]};C.push(J),E.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:a,item:J}))}}if(z.type==="content_block_delta"){let I=z.delta;if(I?.type==="text_delta"){if(!h){h=!0,a=_++,l=`${i}_msg`;let J={id:l,type:"message",status:"in_progress",role:"assistant",content:[]};C.push(J),E.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:a,item:J}))}c+=I.text||"",E.enqueue(S("response.output_text.delta",{type:"response.output_text.delta",item_id:l,output_index:a,content_index:0,delta:I.text||""}))}I?.type==="thinking_delta"&&(y+=I.thinking||"",E.enqueue(S("response.reasoning_summary_text.delta",{type:"response.reasoning_summary_text.delta",item_id:p||`rs_${i}`,output_index:g>=0?g:0,summary_index:0,delta:I.thinking||""}))),I?.type==="input_json_delta"&&(m+=I.partial_json||"",E.enqueue(S("response.function_call_arguments.delta",{type:"response.function_call_arguments.delta",item_id:u||"call_0",output_index:f>=0?f:0,delta:I.partial_json||""})))}if(z.type==="content_block_stop"){if(x==="tool_use"&&u){E.enqueue(S("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:u,output_index:f,arguments:m}));let I=C.find(J=>J.call_id===u);I&&(I.arguments=m,I.status="completed"),I&&R(f,I)}if(x==="text"&&h){E.enqueue(S("response.output_text.done",{type:"response.output_text.done",item_id:l,output_index:a,content_index:0,text:c}));let I=C.find(J=>J.type==="message");I&&(I.content=[{type:"output_text",text:c}],R(a,I))}if(x==="thinking"&&b){let I=C.find(J=>J.type==="reasoning");I&&(I.summary=[{type:"summary_text",text:y}],R(g,I))}x=null}if(z.type==="message_delta"){let I=O(z.usage);I&&E.enqueue(I)}if(z.type==="message_stop"){let I=O();I&&E.enqueue(I),E.enqueue(r.encode(`data: [DONE]
40
+ `);D=W.pop()||"";for(let he of W){let j=he.trim();if(!j||j.startsWith("event:")||!j.startsWith("data:"))continue;let Y=j.slice(5).trim();if(Y==="[DONE]")continue;let z;try{z=JSON.parse(Y)}catch{continue}if(z.type==="message_start"&&(i=z.message?.id||i,P()),z.type==="content_block_start"){let I=z.content_block;if(x=I?.type||null,I?.type==="tool_use"){u=I.id||"",d=I.name||"",f="",g=_++;let B={id:u,type:"function_call",status:"in_progress",call_id:u,name:d,arguments:""};C.push(B),E.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:g,item:B}))}if(I?.type==="thinking"&&(y=I.thinking||"",!b)){b=!0,p=_++,m=`rs_${i}`;let B={id:m,type:"reasoning",status:"in_progress",summary:[]};C.push(B),E.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:p,item:B}))}if(I?.type==="text"&&(c=I?.text||"",!h)){h=!0,a=_++,l=`${i}_msg`;let B={id:l,type:"message",status:"in_progress",role:"assistant",content:[]};C.push(B),E.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:a,item:B}))}}if(z.type==="content_block_delta"){let I=z.delta;if(I?.type==="text_delta"){if(!h){h=!0,a=_++,l=`${i}_msg`;let B={id:l,type:"message",status:"in_progress",role:"assistant",content:[]};C.push(B),E.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:a,item:B}))}c+=I.text||"",E.enqueue(S("response.output_text.delta",{type:"response.output_text.delta",item_id:l,output_index:a,content_index:0,delta:I.text||""}))}I?.type==="thinking_delta"&&(y+=I.thinking||"",E.enqueue(S("response.reasoning_summary_text.delta",{type:"response.reasoning_summary_text.delta",item_id:m||`rs_${i}`,output_index:p>=0?p:0,summary_index:0,delta:I.thinking||""}))),I?.type==="input_json_delta"&&(f+=I.partial_json||"",E.enqueue(S("response.function_call_arguments.delta",{type:"response.function_call_arguments.delta",item_id:u||"call_0",output_index:g>=0?g:0,delta:I.partial_json||""})))}if(z.type==="content_block_stop"){if(x==="tool_use"&&u){E.enqueue(S("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:u,output_index:g,arguments:f}));let I=C.find(B=>B.call_id===u);I&&(I.arguments=f,I.status="completed"),I&&R(g,I)}if(x==="text"&&h){E.enqueue(S("response.output_text.done",{type:"response.output_text.done",item_id:l,output_index:a,content_index:0,text:c}));let I=C.find(B=>B.type==="message");I&&(I.content=[{type:"output_text",text:c}],R(a,I))}if(x==="thinking"&&b){let I=C.find(B=>B.type==="reasoning");I&&(I.summary=[{type:"summary_text",text:y}],R(p,I))}x=null}if(z.type==="message_delta"){let I=O(z.usage);I&&E.enqueue(I)}if(z.type==="message_stop"){let I=O();I&&E.enqueue(I),E.enqueue(r.encode(`data: [DONE]
41
41
 
42
- `))}}}P();let H=O();H&&E.enqueue(H),D="",E.close()}catch(N){E.error(N)}}})}wrapChatInResponses(e){let t=e?.choices?.[0],o=[],r=[];if(t?.message?.content&&r.push({type:"output_text",text:t.message.content}),o.push({type:"message",role:"assistant",content:r}),Array.isArray(t?.message?.tool_calls))for(let s of t.message.tool_calls)o.push({type:"function_call",id:s.id,call_id:s.id,name:s.function?.name||"",arguments:s.function?.arguments||"{}"});return{id:e.id||`resp_${Date.now()}`,object:"response",model:e.model,output:o,usage:e.usage?{...this.buildResponsesUsage(e.usage)}:void 0}}convertResponseToChat(e){let t=e.output?.find(a=>a.type==="message"),o=e.output?.find(a=>a.type==="function_call"),r;t?.content?.length&&t?.content[0].annotations&&(r=t.content[0].annotations.map(a=>({type:"url_citation",url_citation:{url:a.url||"",title:a.title||"",content:"",start_index:a.start_index||0,end_index:a.end_index||0}}))),this.logger.debug({data:r,type:"url_citation"});let s=null,i=null,c=null;if(t&&t.reasoning&&(c={content:t.reasoning}),t&&t.content){let a=[],u=[];if(t.content.forEach(d=>{if(d.type==="output_text")a.push(d.text||"");else if(d.type==="output_image"){let m=this.buildImageContent({url:d.image_url,mime_type:d.mime_type});m&&u.push(m)}else if(d.type==="output_image_base64"){let m=this.buildImageContent({b64_json:d.image_base64,mime_type:d.mime_type});m&&u.push(m)}}),u.length>0){let d=[];a.length>0&&d.push({type:"text",text:a.join("")}),d.push(...u),s=d}else s=a.join("")}return o&&(i=[{id:o.call_id||o.id,function:{name:o.name,arguments:o.arguments},type:"function"}]),{id:e.id||"chatcmpl-"+Date.now(),object:"chat.completion",created:e.created_at,model:e.model,choices:[{index:0,message:{role:"assistant",content:s||null,tool_calls:i,thinking:c,annotations:r},logprobs:null,finish_reason:i?"tool_calls":"stop"}],usage:e.usage?{prompt_tokens:e.usage.input_tokens||0,completion_tokens:e.usage.output_tokens||0,total_tokens:e.usage.total_tokens||0,prompt_tokens_details:{cached_tokens:this.getCacheReadInputTokens(e.usage),cache_creation_tokens:this.getCacheCreationInputTokens(e.usage)},cache_read_input_tokens:this.getCacheReadInputTokens(e.usage),cache_creation_input_tokens:this.getCacheCreationInputTokens(e.usage)}:null}}buildImageContent(e){return e&&(e.url||e.b64_json)?{type:"image_url",image_url:{url:e.url||"",b64_json:e.b64_json},media_type:e.mime_type}:null}}});var no,X,At=w(()=>{"use strict";no=class{capacity;cache;constructor(e){this.capacity=e,this.cache=new Map}get(e){if(!this.cache.has(e))return;let t=this.cache.get(e);return this.cache.delete(e),this.cache.set(e,t),t}put(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.capacity){let o=this.cache.keys().next().value;o!==void 0&&this.cache.delete(o)}this.cache.set(e,t)}values(){return Array.from(this.cache.values())}delete(e){return this.cache.delete(e)}},X=new no(100)});import{existsSync as Kr,mkdirSync as Ga,readFileSync as Ka,writeFileSync as Qa}from"fs";import{join as Qr}from"path";import{HOME_DIR as Va}from"@wengine-ai/claude-code-router-shared";function so(n,e){n&&Oe.set(n,{...e,provider:n,capturedAt:Date.now()})}function It(n){let e=Oe.get(n);return e?{...e}:void 0}function io(){return Array.from(Oe.values()).map(n=>({...n}))}function Ya(){try{if(!Kr(ro))return;let n=JSON.parse(Ka(ro,"utf-8"));if(!Array.isArray(n))return;let e=Date.now();for(let t of n)if(t&&t.provider){if(t.capturedAt&&e-t.capturedAt>1440*60*1e3)continue;Oe.set(t.provider,t)}}catch{}}function Gr(){try{if(Oe.size===0)return;Kr(oo)||Ga(oo,{recursive:!0}),Qa(ro,JSON.stringify(Array.from(Oe.values()),null,2),"utf-8")}catch{}}function et(){Br||(Br=!0,Ya(),Wr=setInterval(Gr,6e4),Wr.unref?.(),process.on("exit",Gr))}var oo,ro,Oe,Br,Wr,Ae=w(()=>{"use strict";oo=Qr(Va,"runtime"),ro=Qr(oo,"quota-store.json"),Oe=new Map,Br=!1,Wr=null});function Ie(n){if(typeof n!="string")return;let e=n.trim();if(!(!e||!Xa.test(e)))return e}function ye(n){if(!n)return;if(typeof n=="object")return Ie(n.session_id??n.sessionId);if(typeof n!="string")return;let e=n.trim();if(!e)return;try{let r=JSON.parse(e),s=Ie(r?.session_id??r?.sessionId);if(s)return s}catch{}let t="_session_",o=e.lastIndexOf(t);if(o!==-1)return Ie(e.slice(o+t.length))}var Xa,Me=w(()=>{"use strict";Xa=/^[A-Za-z0-9_-]+$/});import{randomUUID as qa}from"node:crypto";import{CCR_PROJECT_HEADER as ao}from"@wengine-ai/claude-code-router-shared";function ec(n){if(typeof n=="string")return n.slice(0,1e3);if(Array.isArray(n)){let e=n.find(t=>t&&typeof t.text=="string");return e?String(e.text).slice(0,1e3):""}return""}function tc(n){if(typeof n?.pathname=="string")return n.pathname;if(typeof n?.url!="string")return"";try{return new URL(n.url,"http://127.0.0.1").pathname}catch{return n.url}}function Vr(n,e){let t=n?.headers;if(!t||typeof t!="object")return{present:!1};let o=Object.keys(t).find(s=>s.toLowerCase()===e);if(!o)return{present:!1};let r=t[o];return typeof r=="string"?{present:!0,value:r.trim()}:Array.isArray(r)&&typeof r[0]=="string"?{present:!0,value:r[0].trim()}:{present:!0}}function nc(n,e){if(!(!n?.headers||typeof n.headers!="object"))for(let t of Object.keys(n.headers))t.toLowerCase()===e&&delete n.headers[t]}function tt(n,e=!0){return{clientType:n,usageScope:"request",supportsExplicitExtendedContext:e}}function Yr(n,e,t=!0){let o=ye(n?.body?.metadata?.user_id);return{clientType:e,usageScope:o?"session":"request",stableSessionId:o,supportsExplicitExtendedContext:t}}function qr(n){return typeof n=="string"&&Za.includes(n)}function $e(n){if(qr(n?.clientType))return n.clientType;let e=tc(n),t=n?.headers||{},o=typeof t["user-agent"]=="string"?t["user-agent"]:"",r=n?.body||{},s=ec(r.system);if(/\bYou are Qwen Code\b/i.test(s)||/Qwen Code, an interactive CLI agent/i.test(s))return"qwen-code";if(/operating inside pi\b/i.test(s)||/a coding agent harness/i.test(s))return"pi";if(/\bYou are opencode\b/i.test(s))return"opencode";if(Vr(n,ao).present)return"pi";let i=t["x-anthropic-billing-header"];if(typeof i=="string"&&i.includes("cc_version="))return"claude-code";if(o.includes("codex")||o.includes("Codex")||e.endsWith("/v1/responses"))return"codex";if(r.metadata&&typeof r.metadata=="object"&&typeof r.metadata.user_id=="string")return"claude-code";if(o.includes("opencode"))return"opencode";if(o.includes("pi-coding-agent"))return"pi";if(o.includes("QwenCode"))return"qwen-code";let c=n?.originalModel||r.model||"";return/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i.test(c)?o.includes("Anthropic/")?"pi":o.includes("claude-cli")||o.includes("Claude-CLI")?"qwen-code":typeof t["x-stainless-package-version"]=="string"?"pi":"claude-code":o.includes("claude-cli")||o.includes("Claude-CLI")?"claude-code":String(c).toLowerCase()==="ccr-codex"?"codex":e.endsWith("/v1/messages")?"api":"unknown"}function Zr(n){return Xr[n]}function uc(n){if(typeof n?.__ccrRequestScopeId=="string"&&n.__ccrRequestScopeId)return n.__ccrRequestScopeId;let e=typeof n?.id=="string"&&n.id||qa();return n.__ccrRequestScopeId=e,e}function _e(n,e={}){let t=$e(n),o=Zr(t).createContext(n,e),r=o.stableSessionId||uc(n),s=o.usageScope==="session"?"session":"request";return n.clientType=t,n.clientContext=o,o.stableSessionId?n.sessionId=o.stableSessionId:delete n.sessionId,o.projectHeaderPresent?n.projectId=o.projectId:delete n.projectId,nc(n,ao),n.usageSessionId=r,n.usageCacheKey=`${t}:${s}:${r}`,o}function dc(){}var Za,oc,rc,sc,ic,ac,cc,lc,Xr,Ne=w(()=>{"use strict";Me();Za=["claude-code","pi","qwen-code","opencode","codex","api","unknown"];oc={type:"claude-code",createContext(n){return Yr(n,"claude-code")}},rc={type:"pi",createContext(n){let e=Vr(n,ao);return{...tt("pi",!1),projectId:e.value,projectHeaderPresent:e.present}}},sc={type:"qwen-code",createContext(n){return Yr(n,"qwen-code")}},ic={type:"opencode",createContext(){return tt("opencode")}},ac={type:"codex",createContext(){return tt("codex")}},cc={type:"api",createContext(){return tt("api")}},lc={type:"unknown",createContext(){return tt("unknown")}},Xr={"claude-code":oc,pi:rc,"qwen-code":sc,opencode:ic,codex:ac,api:cc,unknown:lc}});import{get_encoding as pc}from"tiktoken";import{readFile as co}from"fs/promises";import{opendir as fc,stat as mc}from"fs/promises";import{join as lo}from"path";import{CLAUDE_PROJECTS_DIR as es,HOME_DIR as ts,getClaudeProjectId as gc}from"@wengine-ai/claude-code-router-shared";function q(n,e,t,o){let r=is(e,t);if(r.code==="model_unhealthy"){let c=K(e,t,!0);if(c)return n.log.warn(`Strict project target ${e} is in the health fail-pool; retrying it anyway (no fallback available). scenario=${o}`),c}let s=n.sessionId||n.clientContext?.stableSessionId,i=ss[r.code]||502;throw new Z(`Project routing error (${o}): ${r.reason}. Project-level Router target: "${e}". Session: "${s||"unknown"}".`,{sessionId:s,configuredTarget:e,code:r.code,statusCode:i})}function $t(n){let e=n||"";return e.includes(",")&&(e=e.split(",").pop()||e),e.includes("/")&&(e=e.split("/").pop()||e),e.includes(":")&&(e=e.split(":")[0]),e.trim().toLowerCase()}function ot(n){let e=$t(n),t=e.includes("[1m]")||e.endsWith("[1m"),o=e.replace(/\[1m\]|\[1m$/g,""),r=o.match(/^ccr-(opus|sonnet|haiku)$/i);if(r)return{family:r[1].toLowerCase(),extended:t,isCcrAlias:!0};let s=o.match(/claude-(?:\d+-\d+-|\d+-)?(sonnet|opus|haiku)(?:-|$)/i)||o.match(/claude-(sonnet|opus|haiku)(?:-|$)/i);return s?{family:s[1].toLowerCase(),extended:t,isCcrAlias:!1}:{family:null,extended:t,isCcrAlias:!1}}function yc(n,e){if(!e||!n)return null;let t=$t(n);if(e[n])return e[n];if(e[t])return e[t];let{family:o}=ot(n);if(o&&e[o])return e[o];for(let[r,s]of Object.entries(e)){let i=$t(r);if(i&&t.includes(i))return s}return null}function _c(n){return n?(n.input_tokens||0)+(n.cache_read_input_tokens||0)+(n.cache_creation_input_tokens||0):0}function Mt(n){if(!n?.includes(","))return null;let[e,...t]=n.split(","),o=e.trim(),r=t.join(",").trim();return!o||!r?null:{providerName:o,routeModel:r}}function nt(n,e,t){let o=n.find(s=>s.name.toLowerCase()===e.toLowerCase());if(!o||o.enabled===!1)return null;let r=o.models?.find(s=>String(s).toLowerCase()===t.toLowerCase());return r?{provider:o,model:String(r)}:null}function is(n,e){let t=Mt(n);if(!t)return{reason:`invalid model format "${n}" (expected "provider,model")`,code:"invalid_model_format"};let{providerName:o,routeModel:r}=t,s=e.find(a=>a.name?.toLowerCase()===o.toLowerCase());if(!s)return{reason:`provider "${o}" not found`,code:"provider_not_found"};if(s.enabled===!1)return{reason:`provider "${o}" is disabled`,code:"provider_disabled"};let i=s.models?.find(a=>String(a).toLowerCase()===r.toLowerCase());if(!i)return{reason:`model "${r}" not found in provider "${o}"`,code:"model_not_found"};if(!U().isAvailable(s.name,String(i)))return{reason:`model "${r}" in provider "${o}" is temporarily unavailable (health fail-pool)`,code:"model_unhealthy"};let l=It(s.name);if(l){let a=l.limitDaily!==void 0&&l.usedDailyBalance!==void 0&&l.usedDailyBalance>=l.limitDaily,u=l.totalBalance!==void 0&&l.usedBalance!==void 0&&l.usedBalance>=l.totalBalance;if(a||u)return{reason:`provider "${o}" quota is exhausted`,code:"quota_exhausted"}}return{reason:`unable to resolve model "${n}"`,code:"resolution_failed"}}function K(n,e,t,o,r,s,i){let c=Mt(n);if(!c)return i?null:n;let{providerName:l,routeModel:a}=c,u=nt(e,l,a);if(!u){if(r===!0&&o&&!t&&s!==!1){let m=xe(),f=m.getPromotion(l,a,o,e);if(f){let y=Mt(f);if(y){let p=nt(e,y.providerName,y.routeModel);if(p)return`${p.provider.name},${p.model}`}m.clear(l,a,o)}}return null}if(r===!0&&o&&!t&&s!==!1){let m=xe(),f=m.getPromotion(l,a,o,e);if(f){let y=Mt(f);if(y){let p=nt(e,y.providerName,y.routeModel);if(p)return`${p.provider.name},${p.model}`}m.clear(l,a,o)}}if(!t&&!U().isAvailable(u.provider.name,u.model))return null;let d=It(u.provider.name);if(d){let m=d.limitDaily!==void 0&&d.usedDailyBalance!==void 0&&d.usedDailyBalance>=d.limitDaily,f=d.totalBalance!==void 0&&d.usedBalance!==void 0&&d.usedBalance>=d.totalBalance;if(m||f)return null}return`${u.provider.name},${u.model}`}function V(n,e,t,o,r,s){let i=U(),c=[t?.[n],o?.[n]];for(let l of c)if(!(!Array.isArray(l)||l.length===0))for(let a of l){let u=K(a,e,!1,n,void 0,void 0,s);if(u)return u}return null}function kc(n){return n.body.messages?.some(e=>e.role==="user"&&Array.isArray(e.content)&&e.content.some(t=>t.type==="image"||t.type==="image_url"||Array.isArray(t?.content)&&t.content.some(o=>o.type==="image"||o.type==="image_url")))}function uo(n){let e=$t(n);return[/claude/i,/gemini/i,/gpt-4o/i,/gpt-4\.1/i,/gpt-4-vision/i,/qwen.*vl/i,/glm-4v/i,/grok.*vision/i,/pixtral/i,/llava/i].some(o=>o.test(e))}function as(n,e,t){return t?.usageScope!=="session"?n:Math.max(n,_c(e))}function cs(n,e,t){return n?.extendedContextThreshold??e?.extendedContextThreshold??t??2e5}function bc(n,e,t,o,r,s,i,c,l,a,u,d){let m=n.clientContext,f=m?.longContextThreshold??t.longContextThreshold??l??6e4,y=as(e,r,m),p=n.modelFamily,g=cs(m,t,a),_=m?.supportsExplicitExtendedContext!==!1&&s===!0;if((_||y>g)&&t.extendedContext){let b=K(t.extendedContext,o,!1,"extendedContext",c,d,u);if(b)return n.log.info(`Family: using extended context model (1M+), tokens: ${y}, estimated: ${e}, explicit: ${_}, threshold: ${g}`),{model:b,scenarioType:"extendedContext",isFallback:!1};let T=c?V("extendedContext",o,t.fallback,i,p,u):null;if(T)return n.log.info(`Family: using extended context fallback model (1M+), tokens: ${y}, estimated: ${e}, explicit: ${_}, threshold: ${g}`),{model:T,scenarioType:"extendedContext",isFallback:!0};if(u){let x=q(n,t.extendedContext,o,"extendedContext");if(x)return{model:x,scenarioType:"extendedContext",isFallback:!1}}n.log.warn("Family: extendedContext model unavailable (fail pool), skipping")}if(y>f&&(t.longContext||t.fallback?.longContext?.length||i?.longContext?.length)){let b=t.longContext?K(t.longContext,o,!1,"longContext",c,d,u):null;if(b)return n.log.info(`Family: using long context model, tokens: ${y}, estimated: ${e}`),{model:b,scenarioType:"longContext",isFallback:!1};let T=c?V("longContext",o,t.fallback,i,p,u):null;if(T)return n.log.info(`Family: using long context fallback model, tokens: ${y}, estimated: ${e}`),{model:T,scenarioType:"longContext",isFallback:!0};if(u&&t.longContext){let x=q(n,t.longContext,o,"longContext");if(x)return{model:x,scenarioType:"longContext",isFallback:!1}}n.log.warn("Family: no healthy longContext model available, falling through to other scenarios")}if(Array.isArray(n.body.tools)&&n.body.tools.some(b=>b.type?.startsWith("web_search"))&&t.webSearch){let b=K(t.webSearch,o,!1,"webSearch",c,d,u);if(b)return{model:b,scenarioType:"webSearch",isFallback:!1};let T=c?V("webSearch",o,t.fallback,i,p,u):null;if(T)return n.log.info("Family: using webSearch fallback model"),{model:T,scenarioType:"webSearch",isFallback:!0};if(u){let x=q(n,t.webSearch,o,"webSearch");if(x)return{model:x,scenarioType:"webSearch",isFallback:!1}}n.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(n.body.thinking?.type==="enabled"&&t.think){let b=K(t.think,o,!1,"think",c,d,u);if(b)return{model:b,scenarioType:"think",isFallback:!1};let T=c?V("think",o,t.fallback,i,p,u):null;if(T)return n.log.info("Family: using think fallback model"),{model:T,scenarioType:"think",isFallback:!0};if(u){let x=q(n,t.think,o,"think");if(x)return{model:x,scenarioType:"think",isFallback:!1}}n.log.warn("Family: think model unavailable (fail pool), skipping")}if(t.default){let b=K(t.default,o,!1,"default",c,d,u);if(b)return{model:b,scenarioType:"default",isFallback:!1};let T=c?V("default",o,t.fallback,i,p,u):null;if(T)return n.log.info("Family: using default fallback model"),{model:T,scenarioType:"default",isFallback:!0};if(u){let x=q(n,t.default,o,"default");if(x)return{model:x,scenarioType:"default",isFallback:!1}}n.log.warn("Family: default model unavailable (fail pool), skipping")}return null}function Sc(){if(ke.size<=ns)return;let n=[...ke.keys()].slice(0,ke.size-ns);for(let e of n)ke.delete(e)}function Rc(){if(Le.size<=os)return;let n=[...Le].slice(0,Le.size-os);for(let e of n)Le.delete(e)}var ss,Z,le,Nt,hc,Tc,Lt,ke,ns,Le,os,vc,Cc,xc,rs,ls,De=w(()=>{"use strict";At();oe();Ae();Et();Me();Ne();ss={provider_not_found:404,model_not_found:404,invalid_model_format:400,provider_disabled:503,model_unhealthy:503,quota_exhausted:503,resolution_failed:502,project_config_error:500,invalid_project_id:400,project_config_not_found:404},Z=class extends Error{statusCode;code;type;sessionId;configuredTarget;constructor(e,t){super(e),this.name="ProjectRoutingError",this.statusCode=t?.statusCode||ss[t?.code||""]||502,this.code=t?.code||"project_routing_error",this.type="api_error",this.sessionId=t?.sessionId,this.configuredTarget=t?.configuredTarget}};le=pc("cl100k_base"),Nt=(n,e,t)=>{let o=0;return Array.isArray(n)&&n.forEach(r=>{typeof r.content=="string"?o+=le.encode(r.content).length:Array.isArray(r.content)&&r.content.forEach(s=>{s.type==="text"?o+=le.encode(s.text).length:s.type==="tool_use"?o+=le.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(o+=le.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?o+=le.encode(e).length:Array.isArray(e)&&e.forEach(r=>{r.type==="text"&&(typeof r.text=="string"?o+=le.encode(r.text).length:Array.isArray(r.text)&&r.text.forEach(s=>{o+=le.encode(s||"").length}))}),t&&t.forEach(r=>{r.description&&(o+=le.encode(r.name+r.description).length),r.input_schema&&(o+=le.encode(JSON.stringify(r.input_schema)).length)}),o},hc=async(n,e)=>{let t=n.clientContext?.projectHeaderPresent===!0,o=n.clientContext?.projectId;if(t&&(typeof o!="string"||o.length===0||o.length>1024||o==="."||o===".."||/[/\\\0]/.test(o)))throw new Z(`Project routing error: invalid managed project id "${String(o)}".`,{configuredTarget:String(o),code:"invalid_project_id"});let r=t?o:n.sessionId?await ls(n.sessionId):null;if(r){n.projectId=r;let s=lo(ts,r,"config.json");if(!t&&n.sessionId){let i=lo(ts,r,`${n.sessionId}.json`);try{let c=JSON.parse(await co(i,"utf8"));if(c&&c.Router&&Object.keys(c.Router).length>0)return c.Router}catch{}}try{let i=JSON.parse(await co(s,"utf8"));if(t&&(typeof i?.projectPath!="string"||gc(i.projectPath)!==o))throw new Z(`Project routing error: managed project id "${o}" does not match its stored project path.`,{configuredTarget:s,code:"invalid_project_id"});if(i&&i.Router&&Object.keys(i.Router).length>0)return i.Router}catch(i){if(i instanceof Z)throw i;if(t&&i?.code==="ENOENT")throw new Z(`Project routing error: managed project "${o}" no longer has a project config.`,{configuredTarget:s,code:"project_config_not_found"});if(!i||i.code!=="ENOENT"){let c=n.sessionId||n.clientContext?.stableSessionId;throw new Z(`Project routing error: project config.json is malformed or unreadable in project "${r}". ${i.message}. Session: "${c||"unknown"}".`,{sessionId:c,configuredTarget:s,code:"project_config_error",statusCode:500})}}}};Tc=async(n,e,t,o,r,s)=>{let i=t.get("providers")||[],c=r||t.get("Router"),l=c?.enableFallback===!0,a=l?c?.fallback:void 0,u=l&&!s?a||t.get("fallback"):a,d=!s,m=null;if(n.body?.system?.length>1&&n.body?.system[1]?.text?.includes("<CCR-SUBAGENT-MODEL>")){let C=n.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);C&&(n.body.system[1].text=n.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${C[1]}</CCR-SUBAGENT-MODEL>`,""),m=C[1])}let{family:f,extended:y,isCcrAlias:p}=ot(n.body.model),g=c?.families?.[f||""];if(g&&c?.enableFamilyRouting){n.log.info(`Using model family routing for: ${f}${y?" (1M)":""}`),n.modelFamily=f,n.familyFallback=g.fallback;let C=bc(n,e,g,i,o,y,u,l,c?.longContextThreshold,c?.extendedContextThreshold,s,d);if(C)return{model:C.model,scenarioType:C.scenarioType}}if(n.body.model.includes(",")&&!s){let C=K(n.body.model,i,!1,"default",l,d,s);if(C)return{model:C,scenarioType:"default"};n.log.warn(`Explicit model ${n.body.model} unavailable (fail pool), trying fallback`);let S=l?V("default",i,void 0,u,void 0,s):null;if(S)return n.log.info(`Using fallback for explicit model: ${S}`),{model:S,scenarioType:"default"};n.log.warn(`No fallback available for explicit model ${n.body.model}, continuing through routing logic`)}let _=p&&!c?.enableFamilyRouting?null:yc(n.body.model,c?.models);if(_){let C=K(_,i,!1,"modelMapping",l,d,s);if(C)return n.log.info(`Using mapped model for ${n.body.model}: ${_}`),{model:C,scenarioType:"modelMapping"};let S=l?V("modelMapping",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"modelMapping"};if(s){let v=q(n,_,i,"modelMapping");if(v)return{model:v,scenarioType:"modelMapping"}}n.log.warn(`Mapped model ${_} unavailable (fail pool), skipping`)}let k=n.clientContext,h=as(e,o,k),b=cs(k,void 0,c?.extendedContextThreshold);if(h>b&&c?.extendedContext){n.log.info(`Using extended context (1M) model due to token count: ${h}, estimated: ${e}, threshold: ${b}`);let C=K(c.extendedContext,i,!1,"extendedContext",l,d,s);if(C)return{model:C,scenarioType:"extendedContext"};n.log.warn(`Extended context model ${c.extendedContext} unavailable (fail pool), trying fallback`);let S=l?V("extendedContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"extendedContext"};if(s){let v=q(n,c.extendedContext,i,"extendedContext");if(v)return{model:v,scenarioType:"extendedContext"}}}let T=k?.longContextThreshold??c?.longContextThreshold??6e4;if(h>T&&c?.longContext){n.log.info(`Using long context model due to token count: ${h}, estimated: ${e}, threshold: ${T}`);let C=K(c.longContext,i,!1,"longContext",l,d,s);if(C)return{model:C,scenarioType:"longContext"};n.log.warn(`Long context model ${c.longContext} unavailable (fail pool), trying fallback`);let S=l?V("longContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"longContext"};if(s){let v=q(n,c.longContext,i,"longContext");if(v)return{model:v,scenarioType:"longContext"}}}if(!s&&m)return{model:m,scenarioType:"default"};if(n.body.model?.includes("claude")&&n.body.model?.includes("haiku")&&c?.background){n.log.info(`Using background model for ${n.body.model}`);let C=K(c.background,i,!1,"background",l,d,s);if(C)return{model:C,scenarioType:"background"};let S=l?V("background",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"background"};if(s){let v=q(n,c.background,i,"background");if(v)return{model:v,scenarioType:"background"}}n.log.warn(`Background model ${c.background} unavailable (fail pool), falling through`)}if(Array.isArray(n.body.tools)&&n.body.tools.some(C=>C.type?.startsWith("web_search"))&&c?.webSearch){let C=K(c.webSearch,i,!1,"webSearch",l,d,s);if(C)return{model:C,scenarioType:"webSearch"};n.log.warn(`WebSearch model ${c.webSearch} unavailable (fail pool), trying fallback`);let S=l?V("webSearch",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"webSearch"};if(s){let v=q(n,c.webSearch,i,"webSearch");if(v)return{model:v,scenarioType:"webSearch"}}}if(n.body.thinking?.type==="enabled"&&c?.think){n.log.info(`Using think model for ${n.body.thinking}`);let C=K(c.think,i,!1,"think",l,d,s);if(C)return{model:C,scenarioType:"think"};n.log.warn(`Think model ${c.think} unavailable (fail pool), trying fallback`);let S=l?V("think",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"think"};if(s){let v=q(n,c.think,i,"think");if(v)return{model:v,scenarioType:"think"}}}if(c?.default){let C=K(c.default,i,!1,"default",l,d,s);if(C)return{model:C,scenarioType:"default"};n.log.warn(`Default model ${c.default} unavailable (fail pool), trying fallback`);let S=l?V("default",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"default"};let v=K(c.default,i,!0,"default",l,d,s);if(v)return n.log.warn(`No fallback available; retrying default model ${c.default} despite fail-pool state (strict=${s})`),{model:v,scenarioType:"default"}}if(s){let C=q(n,c?.default||n.body.model,i,"default");if(C)return{model:C,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},Lt=async(n,e,t)=>{let{configService:o,event:r}=t;!n.originalModel&&n.body.model&&(n.originalModel=n.body.model),(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&_e(n,o.getAll());let s=await hc(n,o),i=s||o.get("Router"),c=i?.enableFallback===!0,l=o.get("providers")||[],a=!!s;n.enableFallback=c,n.strictProjectRouting=a,n.fallbackConfig=a?i?.fallback:i?.fallback||o.get("fallback");let u=n.clientContext?.usageScope==="session"?n.previousUsage??X.get(n.usageCacheKey):void 0,{messages:d,system:m=[],tools:f}=n.body,y=o.get("REWRITE_SYSTEM_PROMPT");if(y&&m.length>1&&m[1]?.text?.includes("<env>")){let p=await co(y,"utf-8");m[1].text=`${p}<env>${m[1].text.split("<env>").pop()}`}try{let[p,g]=n.body.model.split(","),_=t.tokenizerService?.getTokenizerConfigForModel(p,g),k;t.tokenizerService?k=(await t.tokenizerService.countTokens({messages:d,system:m,tools:f},_,p)).tokenCount:k=Nt(d,m,f),n.tokenCount=k;let h,b=a?void 0:o.get("CUSTOM_ROUTER_PATH");if(b)try{h=await Ve(b)(n,o.getAll(),{event:r})}catch(S){n.log.error(`failed to load custom router: ${S.message}`)}if(h)n.scenarioType="default";else{let S=await Tc(n,k,o,u,s,!!s);h=S.model,n.scenarioType=S.scenarioType}let{family:T}=ot(n.originalModel||n.body.model),x=i?.enableFamilyRouting&&T?i.families?.[T]:void 0,C=[x?.image,i?.image].filter((S,v,P)=>!!S&&P.indexOf(S)===v);if(C.length>0&&kc(n)&&!uo(h))if(C.includes(h))n.scenarioType="image";else{let S=C.map(v=>K(v,l,!1,"image",c,!a,a)).find(v=>!!v);if(S)n.log.info(`Using image model fallback for ${h}`),h=S,n.scenarioType="image";else{let v=c?V("image",l,x?.fallback,n.fallbackConfig,T||void 0,a):null;if(v)n.log.info(`Using image fallback model: ${v}`),h=v,n.scenarioType="image";else if(a){let P=q(n,C[0],l,"image");P&&(h=P,n.scenarioType="image")}else n.log.warn(`Image models ${C.join(", ")} unavailable (fail pool), keeping ${h}`),n.scenarioType="image"}}typeof h=="string"&&h.trim()?n.body.model=h:(n.log.warn(`Router could not resolve a valid model for ${n.originalModel||n.body.model}; keeping original request model`),n.scenarioType=n.scenarioType||"default")}catch(p){if(p instanceof Z)throw p;if(n.log.error(`Error in router middleware: ${p.message}`),a){let g=n.sessionId||n.clientContext?.stableSessionId;throw new Z(`Project routing error: router failure in project mode. ${p.message}. Session: "${g||"unknown"}".`,{sessionId:g,configuredTarget:i?.default,code:"project_router_failure",statusCode:502})}n.body.model=i?.default,n.scenarioType="default"}},ke=new Map,ns=1e3,Le=new Set,os=1e3,vc=3,Cc=50,xc=n=>new Promise(e=>setTimeout(e,n));rs=async n=>{let e=await fc(es),t=[];for await(let s of e)s.isDirectory()&&t.push(s.name);let o=t.map(async s=>{let i=lo(es,s,`${n}.jsonl`);try{return(await mc(i)).isFile()?s:null}catch{return null}}),r=await Promise.all(o);for(let s of r)if(s)return s;return null},ls=async n=>{let e=Ie(n);if(!e)return null;if(ke.has(e))return ke.get(e)||null;try{let t=await rs(e);if(!t&&!Le.has(e)){Le.add(e),Rc();for(let o=0;o<vc&&!t;o++)await xc(Cc),t=await rs(e)}return t?(ke.set(e,t),Sc(),t):null}catch(t){return console.error("Error searching for project by session:",t),null}}});function rt(n){let e=[],t=Array.isArray(n.input)?n.input:[n.input];for(let o of t)if(o.type==="function_call")e.push({role:"assistant",tool_calls:[{id:o.call_id,type:"function",function:{name:o.name,arguments:o.arguments||"{}"}}]});else if(o.type==="function_call_output")e.push({role:"tool",tool_call_id:o.call_id,content:typeof o.output=="string"?o.output:JSON.stringify(o.output)});else if(o.type==="reasoning"){let r=Array.isArray(o.summary)?o.summary.filter(s=>s.type==="summary_text"&&s.text).map(s=>s.text).join(`
42
+ `))}}}P();let H=O();H&&E.enqueue(H),D="",E.close()}catch(N){E.error(N)}}})}wrapChatInResponses(e){let t=e?.choices?.[0],o=[],r=[];if(t?.message?.content&&r.push({type:"output_text",text:t.message.content}),o.push({type:"message",role:"assistant",content:r}),Array.isArray(t?.message?.tool_calls))for(let s of t.message.tool_calls)o.push({type:"function_call",id:s.id,call_id:s.id,name:s.function?.name||"",arguments:s.function?.arguments||"{}"});return{id:e.id||`resp_${Date.now()}`,object:"response",model:e.model,output:o,usage:e.usage?{...this.buildResponsesUsage(e.usage)}:void 0}}convertResponseToChat(e){let t=e.output?.find(a=>a.type==="message"),o=e.output?.find(a=>a.type==="function_call"),r;t?.content?.length&&t?.content[0].annotations&&(r=t.content[0].annotations.map(a=>({type:"url_citation",url_citation:{url:a.url||"",title:a.title||"",content:"",start_index:a.start_index||0,end_index:a.end_index||0}}))),this.logger.debug({data:r,type:"url_citation"});let s=null,i=null,c=null;if(t&&t.reasoning&&(c={content:t.reasoning}),t&&t.content){let a=[],u=[];if(t.content.forEach(d=>{if(d.type==="output_text")a.push(d.text||"");else if(d.type==="output_image"){let f=this.buildImageContent({url:d.image_url,mime_type:d.mime_type});f&&u.push(f)}else if(d.type==="output_image_base64"){let f=this.buildImageContent({b64_json:d.image_base64,mime_type:d.mime_type});f&&u.push(f)}}),u.length>0){let d=[];a.length>0&&d.push({type:"text",text:a.join("")}),d.push(...u),s=d}else s=a.join("")}return o&&(i=[{id:o.call_id||o.id,function:{name:o.name,arguments:o.arguments},type:"function"}]),{id:e.id||"chatcmpl-"+Date.now(),object:"chat.completion",created:e.created_at,model:e.model,choices:[{index:0,message:{role:"assistant",content:s||null,tool_calls:i,thinking:c,annotations:r},logprobs:null,finish_reason:i?"tool_calls":"stop"}],usage:e.usage?{prompt_tokens:e.usage.input_tokens||0,completion_tokens:e.usage.output_tokens||0,total_tokens:e.usage.total_tokens||0,prompt_tokens_details:{cached_tokens:this.getCacheReadInputTokens(e.usage),cache_creation_tokens:this.getCacheCreationInputTokens(e.usage)},cache_read_input_tokens:this.getCacheReadInputTokens(e.usage),cache_creation_input_tokens:this.getCacheCreationInputTokens(e.usage)}:null}}buildImageContent(e){return e&&(e.url||e.b64_json)?{type:"image_url",image_url:{url:e.url||"",b64_json:e.b64_json},media_type:e.mime_type}:null}}});var oo,X,At=w(()=>{"use strict";oo=class{capacity;cache;constructor(e){this.capacity=e,this.cache=new Map}get(e){if(!this.cache.has(e))return;let t=this.cache.get(e);return this.cache.delete(e),this.cache.set(e,t),t}put(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.capacity){let o=this.cache.keys().next().value;o!==void 0&&this.cache.delete(o)}this.cache.set(e,t)}values(){return Array.from(this.cache.values())}delete(e){return this.cache.delete(e)}},X=new oo(100)});import{existsSync as Vr,mkdirSync as Va,readFileSync as Ya,writeFileSync as Xa}from"fs";import{join as Yr}from"path";import{HOME_DIR as Za}from"@wengine-ai/claude-code-router-shared";function io(n,e){n&&Ae.set(n,{...e,provider:n,capturedAt:Date.now()})}function It(n){let e=Ae.get(n);return e?{...e}:void 0}function ao(){return Array.from(Ae.values()).map(n=>({...n}))}function qa(){try{if(!Vr(so))return;let n=JSON.parse(Ya(so,"utf-8"));if(!Array.isArray(n))return;let e=Date.now();for(let t of n)if(t&&t.provider){if(t.capturedAt&&e-t.capturedAt>1440*60*1e3)continue;Ae.set(t.provider,t)}}catch{}}function Qr(){try{if(Ae.size===0)return;Vr(ro)||Va(ro,{recursive:!0}),Xa(so,JSON.stringify(Array.from(Ae.values()),null,2),"utf-8")}catch{}}function et(){Gr||(Gr=!0,qa(),Kr=setInterval(Qr,6e4),Kr.unref?.(),process.on("exit",Qr))}var ro,so,Ae,Gr,Kr,Ie=w(()=>{"use strict";ro=Yr(Za,"runtime"),so=Yr(ro,"quota-store.json"),Ae=new Map,Gr=!1,Kr=null});function Me(n){if(typeof n!="string")return;let e=n.trim();if(!(!e||!ec.test(e)))return e}function _e(n){if(!n)return;if(typeof n=="object")return Me(n.session_id??n.sessionId);if(typeof n!="string")return;let e=n.trim();if(!e)return;try{let r=JSON.parse(e),s=Me(r?.session_id??r?.sessionId);if(s)return s}catch{}let t="_session_",o=e.lastIndexOf(t);if(o!==-1)return Me(e.slice(o+t.length))}var ec,$e=w(()=>{"use strict";ec=/^[A-Za-z0-9_-]+$/});import{randomUUID as tc}from"node:crypto";import{CCR_PROJECT_HEADER as co}from"@wengine-ai/claude-code-router-shared";function oc(n){if(typeof n=="string")return n.slice(0,1e3);if(Array.isArray(n)){let e=n.find(t=>t&&typeof t.text=="string");return e?String(e.text).slice(0,1e3):""}return""}function rc(n){if(typeof n?.pathname=="string")return n.pathname;if(typeof n?.url!="string")return"";try{return new URL(n.url,"http://127.0.0.1").pathname}catch{return n.url}}function Xr(n,e){let t=n?.headers;if(!t||typeof t!="object")return{present:!1};let o=Object.keys(t).find(s=>s.toLowerCase()===e);if(!o)return{present:!1};let r=t[o];return typeof r=="string"?{present:!0,value:r.trim()}:Array.isArray(r)&&typeof r[0]=="string"?{present:!0,value:r[0].trim()}:{present:!0}}function sc(n,e){if(!(!n?.headers||typeof n.headers!="object"))for(let t of Object.keys(n.headers))t.toLowerCase()===e&&delete n.headers[t]}function tt(n,e=!0){return{clientType:n,usageScope:"request",supportsExplicitExtendedContext:e}}function Zr(n,e,t=!0){let o=_e(n?.body?.metadata?.user_id);return{clientType:e,usageScope:o?"session":"request",stableSessionId:o,supportsExplicitExtendedContext:t}}function es(n){return typeof n=="string"&&nc.includes(n)}function Ne(n){if(es(n?.clientType))return n.clientType;let e=rc(n),t=n?.headers||{},o=typeof t["user-agent"]=="string"?t["user-agent"]:"",r=n?.body||{},s=oc(r.system);if(/\bYou are Qwen Code\b/i.test(s)||/Qwen Code, an interactive CLI agent/i.test(s))return"qwen-code";if(/operating inside pi\b/i.test(s)||/a coding agent harness/i.test(s))return"pi";if(/\bYou are opencode\b/i.test(s))return"opencode";if(Xr(n,co).present)return"pi";let i=t["x-anthropic-billing-header"];if(typeof i=="string"&&i.includes("cc_version="))return"claude-code";if(o.includes("codex")||o.includes("Codex")||e.endsWith("/v1/responses"))return"codex";if(r.metadata&&typeof r.metadata=="object"&&typeof r.metadata.user_id=="string")return"claude-code";if(o.includes("opencode"))return"opencode";if(o.includes("pi-coding-agent"))return"pi";if(o.includes("QwenCode"))return"qwen-code";let c=n?.originalModel||r.model||"";return/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i.test(c)?o.includes("Anthropic/")?"pi":o.includes("claude-cli")||o.includes("Claude-CLI")?"qwen-code":typeof t["x-stainless-package-version"]=="string"?"pi":"claude-code":o.includes("claude-cli")||o.includes("Claude-CLI")?"claude-code":String(c).toLowerCase()==="ccr-codex"?"codex":e.endsWith("/v1/messages")?"api":"unknown"}function ts(n){return qr[n]}function fc(n){if(typeof n?.__ccrRequestScopeId=="string"&&n.__ccrRequestScopeId)return n.__ccrRequestScopeId;let e=typeof n?.id=="string"&&n.id||tc();return n.__ccrRequestScopeId=e,e}function ke(n,e={}){let t=Ne(n),o=ts(t).createContext(n,e),r=o.stableSessionId||fc(n),s=o.usageScope==="session"?"session":"request";return n.clientType=t,n.clientContext=o,o.stableSessionId?n.sessionId=o.stableSessionId:delete n.sessionId,o.projectHeaderPresent?n.projectId=o.projectId:delete n.projectId,sc(n,co),n.usageSessionId=r,n.usageCacheKey=`${t}:${s}:${r}`,o}function mc(){}var nc,ic,ac,cc,lc,uc,dc,pc,qr,Le=w(()=>{"use strict";$e();nc=["claude-code","pi","qwen-code","opencode","codex","api","unknown"];ic={type:"claude-code",createContext(n){return Zr(n,"claude-code")}},ac={type:"pi",createContext(n){let e=Xr(n,co);return{...tt("pi",!1),projectId:e.value,projectHeaderPresent:e.present}}},cc={type:"qwen-code",createContext(n){return Zr(n,"qwen-code")}},lc={type:"opencode",createContext(){return tt("opencode")}},uc={type:"codex",createContext(){return tt("codex")}},dc={type:"api",createContext(){return tt("api")}},pc={type:"unknown",createContext(){return tt("unknown")}},qr={"claude-code":ic,pi:ac,"qwen-code":cc,opencode:lc,codex:uc,api:dc,unknown:pc}});import{get_encoding as gc}from"tiktoken";import{readFile as lo}from"fs/promises";import{opendir as hc,stat as yc}from"fs/promises";import{join as uo}from"path";import{CLAUDE_PROJECTS_DIR as ns,HOME_DIR as os,getClaudeProjectId as _c}from"@wengine-ai/claude-code-router-shared";function Z(n,e,t,o){let r=cs(e,t);if(r.code==="model_unhealthy"){let c=K(e,t,!0);if(c)return n.log.warn(`Strict project target ${e} is in the health fail-pool; retrying it anyway (no fallback available). scenario=${o}`),c}let s=n.sessionId||n.clientContext?.stableSessionId,i=as[r.code]||502;throw new q(`Project routing error (${o}): ${r.reason}. Project-level Router target: "${e}". Session: "${s||"unknown"}".`,{sessionId:s,configuredTarget:e,code:r.code,statusCode:i})}function $t(n){let e=n||"";return e.includes(",")&&(e=e.split(",").pop()||e),e.includes("/")&&(e=e.split("/").pop()||e),e.includes(":")&&(e=e.split(":")[0]),e.trim().toLowerCase()}function ot(n){let e=$t(n),t=e.includes("[1m]")||e.endsWith("[1m"),o=e.replace(/\[1m\]|\[1m$/g,""),r=o.match(/^ccr-(opus|sonnet|haiku)$/i);if(r)return{family:r[1].toLowerCase(),extended:t,isCcrAlias:!0};let s=o.match(/claude-(?:\d+-\d+-|\d+-)?(sonnet|opus|haiku)(?:-|$)/i)||o.match(/claude-(sonnet|opus|haiku)(?:-|$)/i);return s?{family:s[1].toLowerCase(),extended:t,isCcrAlias:!1}:{family:null,extended:t,isCcrAlias:!1}}function bc(n,e){if(!e||!n)return null;let t=$t(n);if(e[n])return e[n];if(e[t])return e[t];let{family:o}=ot(n);if(o&&e[o])return e[o];for(let[r,s]of Object.entries(e)){let i=$t(r);if(i&&t.includes(i))return s}return null}function Tc(n){return n?(n.input_tokens||0)+(n.cache_read_input_tokens||0)+(n.cache_creation_input_tokens||0):0}function Mt(n){if(!n?.includes(","))return null;let[e,...t]=n.split(","),o=e.trim(),r=t.join(",").trim();return!o||!r?null:{providerName:o,routeModel:r}}function nt(n,e,t){let o=n.find(s=>s.name.toLowerCase()===e.toLowerCase());if(!o||o.enabled===!1)return null;let r=o.models?.find(s=>String(s).toLowerCase()===t.toLowerCase());return r?{provider:o,model:String(r)}:null}function cs(n,e){let t=Mt(n);if(!t)return{reason:`invalid model format "${n}" (expected "provider,model")`,code:"invalid_model_format"};let{providerName:o,routeModel:r}=t,s=e.find(a=>a.name?.toLowerCase()===o.toLowerCase());if(!s)return{reason:`provider "${o}" not found`,code:"provider_not_found"};if(s.enabled===!1)return{reason:`provider "${o}" is disabled`,code:"provider_disabled"};let i=s.models?.find(a=>String(a).toLowerCase()===r.toLowerCase());if(!i)return{reason:`model "${r}" not found in provider "${o}"`,code:"model_not_found"};if(!U().isAvailable(s.name,String(i)))return{reason:`model "${r}" in provider "${o}" is temporarily unavailable (health fail-pool)`,code:"model_unhealthy"};let l=It(s.name);if(l){let a=l.limitDaily!==void 0&&l.usedDailyBalance!==void 0&&l.usedDailyBalance>=l.limitDaily,u=l.totalBalance!==void 0&&l.usedBalance!==void 0&&l.usedBalance>=l.totalBalance;if(a||u)return{reason:`provider "${o}" quota is exhausted`,code:"quota_exhausted"}}return{reason:`unable to resolve model "${n}"`,code:"resolution_failed"}}function K(n,e,t,o,r,s,i){let c=Mt(n);if(!c)return i?null:n;let{providerName:l,routeModel:a}=c,u=nt(e,l,a);if(!u){if(r===!0&&o&&!t&&s!==!1){let f=Se(),g=f.getPromotion(l,a,o,e);if(g){let y=Mt(g);if(y){let m=nt(e,y.providerName,y.routeModel);if(m)return`${m.provider.name},${m.model}`}f.clear(l,a,o)}}return null}if(r===!0&&o&&!t&&s!==!1){let f=Se(),g=f.getPromotion(l,a,o,e);if(g){let y=Mt(g);if(y){let m=nt(e,y.providerName,y.routeModel);if(m)return`${m.provider.name},${m.model}`}f.clear(l,a,o)}}if(!t&&!U().isAvailable(u.provider.name,u.model))return null;let d=It(u.provider.name);if(d){let f=d.limitDaily!==void 0&&d.usedDailyBalance!==void 0&&d.usedDailyBalance>=d.limitDaily,g=d.totalBalance!==void 0&&d.usedBalance!==void 0&&d.usedBalance>=d.totalBalance;if(f||g)return null}return`${u.provider.name},${u.model}`}function V(n,e,t,o,r,s){let i=U(),c=[t?.[n],o?.[n]];for(let l of c)if(!(!Array.isArray(l)||l.length===0))for(let a of l){let u=K(a,e,!1,n,void 0,void 0,s);if(u)return u}return null}function vc(n){return n.body.messages?.some(e=>e.role==="user"&&Array.isArray(e.content)&&e.content.some(t=>t.type==="image"||t.type==="image_url"||Array.isArray(t?.content)&&t.content.some(o=>o.type==="image"||o.type==="image_url")))}function po(n){let e=$t(n);return[/claude/i,/gemini/i,/gpt-4o/i,/gpt-4\.1/i,/gpt-4-vision/i,/qwen.*vl/i,/glm-4v/i,/grok.*vision/i,/pixtral/i,/llava/i].some(o=>o.test(e))}function ls(n,e,t){return t?.usageScope!=="session"?n:Math.max(n,Tc(e))}function us(n,e,t){return n?.extendedContextThreshold??e?.extendedContextThreshold??t??2e5}function Cc(n,e,t,o,r,s,i,c,l,a,u,d){let f=n.clientContext,g=f?.longContextThreshold??t.longContextThreshold??l??6e4,y=ls(e,r,f),m=n.modelFamily,p=us(f,t,a),_=f?.supportsExplicitExtendedContext!==!1&&s===!0;if((_||y>p)&&t.extendedContext){let b=K(t.extendedContext,o,!1,"extendedContext",c,d,u);if(b)return n.log.info(`Family: using extended context model (1M+), tokens: ${y}, estimated: ${e}, explicit: ${_}, threshold: ${p}`),{model:b,scenarioType:"extendedContext",isFallback:!1};let T=c?V("extendedContext",o,t.fallback,i,m,u):null;if(T)return n.log.info(`Family: using extended context fallback model (1M+), tokens: ${y}, estimated: ${e}, explicit: ${_}, threshold: ${p}`),{model:T,scenarioType:"extendedContext",isFallback:!0};if(u){let x=Z(n,t.extendedContext,o,"extendedContext");if(x)return{model:x,scenarioType:"extendedContext",isFallback:!1}}n.log.warn("Family: extendedContext model unavailable (fail pool), skipping")}if(y>g&&(t.longContext||t.fallback?.longContext?.length||i?.longContext?.length)){let b=t.longContext?K(t.longContext,o,!1,"longContext",c,d,u):null;if(b)return n.log.info(`Family: using long context model, tokens: ${y}, estimated: ${e}`),{model:b,scenarioType:"longContext",isFallback:!1};let T=c?V("longContext",o,t.fallback,i,m,u):null;if(T)return n.log.info(`Family: using long context fallback model, tokens: ${y}, estimated: ${e}`),{model:T,scenarioType:"longContext",isFallback:!0};if(u&&t.longContext){let x=Z(n,t.longContext,o,"longContext");if(x)return{model:x,scenarioType:"longContext",isFallback:!1}}n.log.warn("Family: no healthy longContext model available, falling through to other scenarios")}if(Array.isArray(n.body.tools)&&n.body.tools.some(b=>b.type?.startsWith("web_search"))&&t.webSearch){let b=K(t.webSearch,o,!1,"webSearch",c,d,u);if(b)return{model:b,scenarioType:"webSearch",isFallback:!1};let T=c?V("webSearch",o,t.fallback,i,m,u):null;if(T)return n.log.info("Family: using webSearch fallback model"),{model:T,scenarioType:"webSearch",isFallback:!0};if(u){let x=Z(n,t.webSearch,o,"webSearch");if(x)return{model:x,scenarioType:"webSearch",isFallback:!1}}n.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(n.body.thinking?.type==="enabled"&&t.think){let b=K(t.think,o,!1,"think",c,d,u);if(b)return{model:b,scenarioType:"think",isFallback:!1};let T=c?V("think",o,t.fallback,i,m,u):null;if(T)return n.log.info("Family: using think fallback model"),{model:T,scenarioType:"think",isFallback:!0};if(u){let x=Z(n,t.think,o,"think");if(x)return{model:x,scenarioType:"think",isFallback:!1}}n.log.warn("Family: think model unavailable (fail pool), skipping")}if(t.default){let b=K(t.default,o,!1,"default",c,d,u);if(b)return{model:b,scenarioType:"default",isFallback:!1};let T=c?V("default",o,t.fallback,i,m,u):null;if(T)return n.log.info("Family: using default fallback model"),{model:T,scenarioType:"default",isFallback:!0};if(u){let x=Z(n,t.default,o,"default");if(x)return{model:x,scenarioType:"default",isFallback:!1}}n.log.warn("Family: default model unavailable (fail pool), skipping")}return null}function Pc(){if(be.size<=rs)return;let n=[...be.keys()].slice(0,be.size-rs);for(let e of n)be.delete(e)}function Ec(){if(De.size<=ss)return;let n=[...De].slice(0,De.size-ss);for(let e of n)De.delete(e)}var as,q,le,Nt,kc,xc,Lt,be,rs,De,ss,Sc,Rc,wc,is,ds,Fe=w(()=>{"use strict";At();re();Ie();Et();$e();Le();as={provider_not_found:404,model_not_found:404,invalid_model_format:400,provider_disabled:503,model_unhealthy:503,quota_exhausted:503,resolution_failed:502,project_config_error:500,invalid_project_id:400,project_config_not_found:404},q=class extends Error{statusCode;code;type;sessionId;configuredTarget;constructor(e,t){super(e),this.name="ProjectRoutingError",this.statusCode=t?.statusCode||as[t?.code||""]||502,this.code=t?.code||"project_routing_error",this.type="api_error",this.sessionId=t?.sessionId,this.configuredTarget=t?.configuredTarget}};le=gc("cl100k_base"),Nt=(n,e,t)=>{let o=0;return Array.isArray(n)&&n.forEach(r=>{typeof r.content=="string"?o+=le.encode(r.content).length:Array.isArray(r.content)&&r.content.forEach(s=>{s.type==="text"?o+=le.encode(s.text).length:s.type==="tool_use"?o+=le.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(o+=le.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?o+=le.encode(e).length:Array.isArray(e)&&e.forEach(r=>{r.type==="text"&&(typeof r.text=="string"?o+=le.encode(r.text).length:Array.isArray(r.text)&&r.text.forEach(s=>{o+=le.encode(s||"").length}))}),t&&t.forEach(r=>{r.description&&(o+=le.encode(r.name+r.description).length),r.input_schema&&(o+=le.encode(JSON.stringify(r.input_schema)).length)}),o},kc=async(n,e)=>{let t=n.clientContext?.projectHeaderPresent===!0,o=n.clientContext?.projectId;if(t&&(typeof o!="string"||o.length===0||o.length>1024||o==="."||o===".."||/[/\\\0]/.test(o)))throw new q(`Project routing error: invalid managed project id "${String(o)}".`,{configuredTarget:String(o),code:"invalid_project_id"});let r=t?o:n.sessionId?await ds(n.sessionId):null;if(r){n.projectId=r;let s=uo(os,r,"config.json");if(!t&&n.sessionId){let i=uo(os,r,`${n.sessionId}.json`);try{let c=JSON.parse(await lo(i,"utf8"));if(c&&c.Router&&Object.keys(c.Router).length>0)return c.Router}catch{}}try{let i=JSON.parse(await lo(s,"utf8"));if(t&&(typeof i?.projectPath!="string"||_c(i.projectPath)!==o))throw new q(`Project routing error: managed project id "${o}" does not match its stored project path.`,{configuredTarget:s,code:"invalid_project_id"});if(i&&i.Router&&Object.keys(i.Router).length>0)return i.Router}catch(i){if(i instanceof q)throw i;if(t&&i?.code==="ENOENT")throw new q(`Project routing error: managed project "${o}" no longer has a project config.`,{configuredTarget:s,code:"project_config_not_found"});if(!i||i.code!=="ENOENT"){let c=n.sessionId||n.clientContext?.stableSessionId;throw new q(`Project routing error: project config.json is malformed or unreadable in project "${r}". ${i.message}. Session: "${c||"unknown"}".`,{sessionId:c,configuredTarget:s,code:"project_config_error",statusCode:500})}}}};xc=async(n,e,t,o,r,s)=>{let i=t.get("providers")||[],c=r||t.get("Router"),l=c?.enableFallback===!0,a=l?c?.fallback:void 0,u=l&&!s?a||t.get("fallback"):a,d=!s,f=null;if(n.body?.system?.length>1&&n.body?.system[1]?.text?.includes("<CCR-SUBAGENT-MODEL>")){let C=n.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);C&&(n.body.system[1].text=n.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${C[1]}</CCR-SUBAGENT-MODEL>`,""),f=C[1])}let{family:g,extended:y,isCcrAlias:m}=ot(n.body.model),p=c?.families?.[g||""];if(p&&c?.enableFamilyRouting){n.log.info(`Using model family routing for: ${g}${y?" (1M)":""}`),n.modelFamily=g,n.familyFallback=p.fallback;let C=Cc(n,e,p,i,o,y,u,l,c?.longContextThreshold,c?.extendedContextThreshold,s,d);if(C)return{model:C.model,scenarioType:C.scenarioType}}if(n.body.model.includes(",")&&!s){let C=K(n.body.model,i,!1,"default",l,d,s);if(C)return{model:C,scenarioType:"default"};n.log.warn(`Explicit model ${n.body.model} unavailable (fail pool), trying fallback`);let S=l?V("default",i,void 0,u,void 0,s):null;if(S)return n.log.info(`Using fallback for explicit model: ${S}`),{model:S,scenarioType:"default"};n.log.warn(`No fallback available for explicit model ${n.body.model}, continuing through routing logic`)}let _=m&&!c?.enableFamilyRouting?null:bc(n.body.model,c?.models);if(_){let C=K(_,i,!1,"modelMapping",l,d,s);if(C)return n.log.info(`Using mapped model for ${n.body.model}: ${_}`),{model:C,scenarioType:"modelMapping"};let S=l?V("modelMapping",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"modelMapping"};if(s){let v=Z(n,_,i,"modelMapping");if(v)return{model:v,scenarioType:"modelMapping"}}n.log.warn(`Mapped model ${_} unavailable (fail pool), skipping`)}let k=n.clientContext,h=ls(e,o,k),b=us(k,void 0,c?.extendedContextThreshold);if(h>b&&c?.extendedContext){n.log.info(`Using extended context (1M) model due to token count: ${h}, estimated: ${e}, threshold: ${b}`);let C=K(c.extendedContext,i,!1,"extendedContext",l,d,s);if(C)return{model:C,scenarioType:"extendedContext"};n.log.warn(`Extended context model ${c.extendedContext} unavailable (fail pool), trying fallback`);let S=l?V("extendedContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"extendedContext"};if(s){let v=Z(n,c.extendedContext,i,"extendedContext");if(v)return{model:v,scenarioType:"extendedContext"}}}let T=k?.longContextThreshold??c?.longContextThreshold??6e4;if(h>T&&c?.longContext){n.log.info(`Using long context model due to token count: ${h}, estimated: ${e}, threshold: ${T}`);let C=K(c.longContext,i,!1,"longContext",l,d,s);if(C)return{model:C,scenarioType:"longContext"};n.log.warn(`Long context model ${c.longContext} unavailable (fail pool), trying fallback`);let S=l?V("longContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"longContext"};if(s){let v=Z(n,c.longContext,i,"longContext");if(v)return{model:v,scenarioType:"longContext"}}}if(!s&&f)return{model:f,scenarioType:"default"};if(n.body.model?.includes("claude")&&n.body.model?.includes("haiku")&&c?.background){n.log.info(`Using background model for ${n.body.model}`);let C=K(c.background,i,!1,"background",l,d,s);if(C)return{model:C,scenarioType:"background"};let S=l?V("background",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"background"};if(s){let v=Z(n,c.background,i,"background");if(v)return{model:v,scenarioType:"background"}}n.log.warn(`Background model ${c.background} unavailable (fail pool), falling through`)}if(Array.isArray(n.body.tools)&&n.body.tools.some(C=>C.type?.startsWith("web_search"))&&c?.webSearch){let C=K(c.webSearch,i,!1,"webSearch",l,d,s);if(C)return{model:C,scenarioType:"webSearch"};n.log.warn(`WebSearch model ${c.webSearch} unavailable (fail pool), trying fallback`);let S=l?V("webSearch",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"webSearch"};if(s){let v=Z(n,c.webSearch,i,"webSearch");if(v)return{model:v,scenarioType:"webSearch"}}}if(n.body.thinking?.type==="enabled"&&c?.think){n.log.info(`Using think model for ${n.body.thinking}`);let C=K(c.think,i,!1,"think",l,d,s);if(C)return{model:C,scenarioType:"think"};n.log.warn(`Think model ${c.think} unavailable (fail pool), trying fallback`);let S=l?V("think",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"think"};if(s){let v=Z(n,c.think,i,"think");if(v)return{model:v,scenarioType:"think"}}}if(c?.default){let C=K(c.default,i,!1,"default",l,d,s);if(C)return{model:C,scenarioType:"default"};n.log.warn(`Default model ${c.default} unavailable (fail pool), trying fallback`);let S=l?V("default",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"default"};let v=K(c.default,i,!0,"default",l,d,s);if(v)return n.log.warn(`No fallback available; retrying default model ${c.default} despite fail-pool state (strict=${s})`),{model:v,scenarioType:"default"}}if(s){let C=Z(n,c?.default||n.body.model,i,"default");if(C)return{model:C,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},Lt=async(n,e,t)=>{let{configService:o,event:r}=t;!n.originalModel&&n.body.model&&(n.originalModel=n.body.model),(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&ke(n,o.getAll());let s=await kc(n,o),i=s||o.get("Router"),c=i?.enableFallback===!0,l=o.get("providers")||[],a=!!s;n.enableFallback=c,n.strictProjectRouting=a,n.fallbackConfig=a?i?.fallback:i?.fallback||o.get("fallback");let u=n.clientContext?.usageScope==="session"?n.previousUsage??X.get(n.usageCacheKey):void 0,{messages:d,system:f=[],tools:g}=n.body,y=o.get("REWRITE_SYSTEM_PROMPT");if(y&&f.length>1&&f[1]?.text?.includes("<env>")){let m=await lo(y,"utf-8");f[1].text=`${m}<env>${f[1].text.split("<env>").pop()}`}try{let[m,p]=n.body.model.split(","),_=t.tokenizerService?.getTokenizerConfigForModel(m,p),k;t.tokenizerService?k=(await t.tokenizerService.countTokens({messages:d,system:f,tools:g},_,m)).tokenCount:k=Nt(d,f,g),n.tokenCount=k;let h,b=a?void 0:o.get("CUSTOM_ROUTER_PATH");if(b)try{h=await Ve(b)(n,o.getAll(),{event:r})}catch(S){n.log.error(`failed to load custom router: ${S.message}`)}if(h)n.scenarioType="default";else{let S=await xc(n,k,o,u,s,!!s);h=S.model,n.scenarioType=S.scenarioType}let{family:T}=ot(n.originalModel||n.body.model),x=i?.enableFamilyRouting&&T?i.families?.[T]:void 0,C=[x?.image,i?.image].filter((S,v,P)=>!!S&&P.indexOf(S)===v);if(C.length>0&&vc(n)&&!po(h))if(C.includes(h))n.scenarioType="image";else{let S=C.map(v=>K(v,l,!1,"image",c,!a,a)).find(v=>!!v);if(S)n.log.info(`Using image model fallback for ${h}`),h=S,n.scenarioType="image";else{let v=c?V("image",l,x?.fallback,n.fallbackConfig,T||void 0,a):null;if(v)n.log.info(`Using image fallback model: ${v}`),h=v,n.scenarioType="image";else if(a){let P=Z(n,C[0],l,"image");P&&(h=P,n.scenarioType="image")}else n.log.warn(`Image models ${C.join(", ")} unavailable (fail pool), keeping ${h}`),n.scenarioType="image"}}typeof h=="string"&&h.trim()?n.body.model=h:(n.log.warn(`Router could not resolve a valid model for ${n.originalModel||n.body.model}; keeping original request model`),n.scenarioType=n.scenarioType||"default")}catch(m){if(m instanceof q)throw m;if(n.log.error(`Error in router middleware: ${m.message}`),a){let p=n.sessionId||n.clientContext?.stableSessionId;throw new q(`Project routing error: router failure in project mode. ${m.message}. Session: "${p||"unknown"}".`,{sessionId:p,configuredTarget:i?.default,code:"project_router_failure",statusCode:502})}n.body.model=i?.default,n.scenarioType="default"}},be=new Map,rs=1e3,De=new Set,ss=1e3,Sc=3,Rc=50,wc=n=>new Promise(e=>setTimeout(e,n));is=async n=>{let e=await hc(ns),t=[];for await(let s of e)s.isDirectory()&&t.push(s.name);let o=t.map(async s=>{let i=uo(ns,s,`${n}.jsonl`);try{return(await yc(i)).isFile()?s:null}catch{return null}}),r=await Promise.all(o);for(let s of r)if(s)return s;return null},ds=async n=>{let e=Me(n);if(!e)return null;if(be.has(e))return be.get(e)||null;try{let t=await is(e);if(!t&&!De.has(e)){De.add(e),Ec();for(let o=0;o<Sc&&!t;o++)await wc(Rc),t=await is(e)}return t?(be.set(e,t),Pc(),t):null}catch(t){return console.error("Error searching for project by session:",t),null}}});function rt(n){let e=[],t=Array.isArray(n.input)?n.input:[n.input];for(let o of t)if(o.type==="function_call")e.push({role:"assistant",tool_calls:[{id:o.call_id,type:"function",function:{name:o.name,arguments:o.arguments||"{}"}}]});else if(o.type==="function_call_output")e.push({role:"tool",tool_call_id:o.call_id,content:typeof o.output=="string"?o.output:JSON.stringify(o.output)});else if(o.type==="reasoning"){let r=Array.isArray(o.summary)?o.summary.filter(s=>s.type==="summary_text"&&s.text).map(s=>s.text).join(`
43
43
  `):"";r&&e.push({role:"assistant",content:[{type:"text",text:`[reasoning]
44
- ${r}`}],_reasoningItem:!0})}else if(o.role){let r=o.content;Array.isArray(r)&&(r=r.map(s=>s.type==="input_text"?{type:"text",text:s.text}:s.type==="output_text"?{type:"text",text:s.text}:s.type==="input_image"?{type:"image_url",image_url:{url:s.image_url},media_type:s.mime_type}:s)),e.push({role:o.role,content:r})}n.messages=e,delete n.input,n.instructions&&typeof n.instructions=="string"&&(n.system=n.instructions,delete n.instructions),Array.isArray(n.tools)&&(n.tools=n.tools.filter(o=>o&&typeof o=="object").map(o=>o.type==="function"&&o.name&&!o.function?{type:"function",function:{name:o.name,description:o.description||"",parameters:o.parameters||{type:"object",properties:{}}}}:o.function?.name?o:o.type==="web_search"?{type:"function",function:{name:"web_search",description:"Search the web",parameters:{type:"object",properties:{query:{type:"string"}}}}}:null).filter(Boolean)),n.reasoning&&typeof n.reasoning=="object"&&(n.reasoning={enabled:!0,effort:n.reasoning.effort}),delete n.store,delete n.previous_response_id,delete n.include}async function us(n,e,t,o){let r=n.body;(n.ccrHookOrder||=[]).push("handler"),o.endPoint==="/v1/responses"&&r?.input&&!r?.messages&&rt(r),o.endPoint==="/v1/responses"&&!n.provider&&typeof r?.model=="string"&&wc.test(r.model)&&await Lt(n,e,{configService:t.configService,tokenizerService:t.tokenizerService});let s=n.provider;if(!s&&r?.model){let a=r.model.split(",");a.length>1&&(s=a[0],r.model=a.slice(1).join(","))}let i=s?t.providerService.getProvider(s):void 0;if(!i&&r?.model){let a=t.providerService.resolveModelRoute(r.model);a&&(i=a.provider,r.model=a.targetModel)}if(!i&&r?.model){let u=t.configService.get("Router")?.default;if(u&&u.includes(",")){let[d,...m]=u.split(","),f=t.providerService.getProvider(d);f&&(i=f,r.model=m.join(","))}}if(n.originalModel||(n.originalModel=r?.model),!i)throw B(`Provider '${s}' not found`,404,"provider_not_found");n.provider=i.name||s;let c=Oc(n),l=o.endPoint==="/v1/messages"&&c;c&&e.header("X-Codex-Detected","true"),l&&e.header("X-Codex-On-Messages","true"),n.isCodexOnMessagesEndpoint=l,n.codexDetected=c;try{o.endPoint==="/v1/responses"&&r.input&&!r.messages&&rt(r);let{requestBody:a,config:u,bypass:d}=await ps(r,i,o,n.headers,{req:n});if(a?.messages&&!d&&!n.isTargetAnthropic){let p=a.messages,g=new Set,_=[],k=[];for(let h of p){if(h?.role!=="system"){k.push(h);continue}let b=JSON.stringify(h.content??"");g.has(b)||(g.add(b),_.push(h))}_.length>0&&k.length>0&&(a.messages=[..._,...k])}let m=await fs(a,u,i,t,d,o,{req:n});try{i?.baseUrl&&m?.headers&&Xe(s,i.baseUrl,m.headers)}catch{}let f=await ms(a,m,i,o,d,{req:n,isCodexOnMessagesEndpoint:l});f=await ds(r,f,i.name||s);let y=gs(f,e,r,t);try{let p=r.model||n.originalModel;s&&p&&U().recordSuccess(s,p)}catch{}return y}catch(a){if(a instanceof Z)throw a;if(!n.__retryAttempted){n.__retryAttempted=!0;try{return await us(n,e,t,o)}catch(d){if(n.__fallbackExhausted)throw d;a=d}}let u=await Pc(n,e,t,o,a);if(u)return u;throw n.__fallbackExhausted=!0,a}}async function ds(n,e,t){if(!n?.stream||!e?.body)return e;let o=e.body.getReader(),r=new TextDecoder,s=[],i="";for(let a=0;a<8;a++){let u=await o.read();if(u.done||(u.value&&(s.push(u.value),i+=r.decode(u.value,{stream:!0})),i.split(`
45
- `).some(d=>d.startsWith("data:"))))break}if(i+=r.decode(),s.length===0)throw o.releaseLock(),B(`Provider returned empty streaming response from ${t}`,400,"provider_response_error");if(i.includes("event: error")){o.releaseLock();let a=i.length<500?i:i.slice(0,500);throw B(`Provider ${t} returned error in SSE stream: ${a}`,400,"provider_response_error")}if(i.split(`
46
- `).filter(a=>a.startsWith("data:")&&a.trim().length>5).length===0){o.releaseLock();let a=i.length<500?i:i.slice(0,500);throw B(`Provider ${t} returned streaming response with no SSE data lines: ${a}`,400,"provider_response_error")}let l=new ReadableStream({start:a=>{for(let d of s)a.enqueue(d);let u=()=>{o.read().then(({done:d,value:m})=>{d?a.close():(a.enqueue(m),u())}).catch(d=>a.error(d))};u()},cancel:()=>{o.cancel()}});return new Response(l,{headers:e.headers,status:e.status,statusText:e.statusText})}async function Pc(n,e,t,o,r){let s=n.scenarioType||"default",i=n.familyFallback,c=n.modelFamily,l=n.strictProjectRouting?n.fallbackConfig:n.fallbackConfig??t.configService.get("fallback"),a=U(),u=n.provider||"",d=n.body.model||"",m=new Set,f=r?.statusCode===429||r?.isRateLimit===!0||r?.rawBody&&Dt(r.rawBody)||r?.message&&Dt(r.message);if(u&&d&&(f?(a.markRateLimited(u,d,120,r?.message),n.log.warn(`Primary model ${u},${d} rate-limited, will try fallbacks`)):a.recordFailure(u,d,r?.message),m.add(`${u},${d}`),t.recordUsage?.({provider:u,model:d,originalModel:n.originalModel||d,scenarioType:s,modelFamily:n.modelFamily,errorMessage:r?.message||String(r),sessionId:n.usageSessionId||n.id,stream:n.body.stream,inputTokens:n.tokenCount||0,req:n})),!(n.enableFallback??t.configService.get("Router")?.enableFallback===!0))return n.log.info("Fallback disabled by configuration, skipping fallback attempts"),null;let p=k=>{let[h,...b]=k.split(","),T=h?.trim(),x=b.join(",").trim();return!T||!x?null:{provider:T,model:x,key:`${T},${x}`}},g=[];if(c){let k=i?.[s];if(Array.isArray(k)&&k.length>0)g.push({name:`${c}/${s}`,models:k});else{let h=n.strictProjectRouting;n.log.warn(`No ${c} fallback configured for ${s}; ${h?"no project fallback available":"will try global fallback"}`)}}if(Array.isArray(l?.[s])&&l[s].length>0&&g.push({name:`global/${s}`,models:l[s]}),g.length===0&&s!=="image"&&n.body?.messages?.some(b=>b.role==="user"&&Array.isArray(b.content)&&b.content.some(T=>T.type==="image"||T.type==="image_url"||Array.isArray(T?.content)&&T.content.some(x=>x.type==="image"||x.type==="image_url")))&&Array.isArray(l?.image)&&l.image.length>0&&(n.log.info(`No fallback for scenario '${s}', but request has images \u2014 trying fallback.image`),g.push({name:"global/image (auto-detected)",models:l.image})),g.length===0)return null;let _=g.reduce((k,h)=>k+h.models.length,0);n.log.warn(`Request failed for ${s}, trying ${_} fallback models across ${g.length} fallback stage(s)`);for(let k of g){n.log.info(`Trying ${k.name} fallback stage with ${k.models.length} models`);for(let h of k.models){let b=p(h);if(!b){n.log.warn(`Fallback model '${h}' is invalid, skipping`);continue}if(m.has(b.key))continue;m.add(b.key);let T=t.providerService.getProviders(),x=nt(T,b.provider,b.model);if(!x){n.log.warn(`Fallback model ${h} is not configured or provider is disabled, skipping`);continue}let C=x.provider.name,S=x.model;if(!a.isAvailable(C,S)){n.log.warn(`Fallback model ${h} unavailable (fail pool), skipping`);continue}let v;for(let P=1;P<=2;P++)try{n.log.info(`Trying fallback model: ${C},${S}${P>1?" (retry)":""}`);let R={...n.body};R.model=S;let O={...n,provider:C,body:R},E=x.provider,{requestBody:A,config:N,bypass:D}=await ps(R,E,o,n.headers,{req:O}),H=await fs(A,N,E,t,D,o,{req:O});try{E?.baseUrl&&H?.headers&&Xe(C,E.baseUrl,H.headers)}catch{}let $=await ms(A,H,E,o,D,{req:O,isCodexOnMessagesEndpoint:n.isCodexOnMessagesEndpoint||!1});return $=await ds(R,$,C),n.log.info(`Fallback model ${C},${S} succeeded`),a.recordSuccess(C,S),u&&d&&!n.strictProjectRouting&&(xe().promote(u,d,s,C,S),n.log.info(`Promoted fallback model ${C},${S} for ${u},${d}:${s}`),f?n.log.info(`Rate-limited original model ${u},${d} already marked unavailable with cooldown`):(a.forceOpen(u,d,r?.message),n.log.info(`Marked original model ${u},${d} as unavailable`))),n.provider=C,n.body=R,gs($,e,R,t)}catch(R){if(v=R,P===1){n.log.warn(`Fallback model ${C},${S} failed, retrying once: ${R.message}`);continue}}a.recordFailure(C,S,v?.message),n.log.warn(`Fallback model ${C},${S} failed after retry: ${v?.message}`),t.recordUsage?.({provider:C,model:S,originalModel:n.originalModel||d||n.body.model,scenarioType:s,modelFamily:n.modelFamily,errorMessage:v?.message||String(v),sessionId:n.usageSessionId||n.id,stream:n.body.stream,inputTokens:n.tokenCount||0,req:n})}}return n.log.error(`All fallback models failed for ${s}`),null}async function ps(n,e,t,o,r){let s=JSON.parse(JSON.stringify(n)),i=s?.system;if(typeof i=="string"&&i.includes("cch="))s.system=i.replace(/cch=[^;]+;?/g,"cch=ccr-stable;");else if(Array.isArray(i))for(let a=0;a<i.length;a++){let u=i[a],d=typeof u=="string"?u:u?.text??"";if(d.includes("cch=")){let m=d.replace(/cch=[^;]+;?/g,"cch=ccr-stable;");typeof u=="string"?i[a]=m:typeof u?.text=="string"&&(u.text=m)}}let c={},l=!1;if(l=Ec(e,t,n),l&&(o instanceof Headers?o.delete("content-length"):delete o["content-length"],c.headers=o),!l&&typeof t.transformRequestOut=="function"){let a=await t.transformRequestOut(s);a.body?(s=a.body,c=a.config||{}):s=a}if(!l&&e.transformer?.use?.length)for(let a of e.transformer.use){if(!a||typeof a.transformRequestIn!="function")continue;let u=await a.transformRequestIn(s,e,r);u.body?(s=u.body,c={...c,...u.config}):s=u}if(!l&&e.transformer?.[n.model]?.use?.length)for(let a of e.transformer[n.model].use)!a||typeof a.transformRequestIn!="function"||(s=await a.transformRequestIn(s,e,r));return{requestBody:s,config:c,bypass:l}}function Ec(n,e,t){return e.endPoint==="/v1/chat/completions"&&Array.isArray(t?.system)?!1:n.transformer?.use?.length===1&&n.transformer.use[0].name===e.name&&(!n.transformer?.[t.model]?.use.length||n.transformer?.[t.model]?.use.length===1&&n.transformer?.[t.model]?.use[0].name===e.name)}function Oc(n){if(n.clientType)return n.clientType==="codex";let e=n.headers||{},t=e["x-anthropic-billing-header"]||"";if(typeof t=="string"&&t.includes("cc_version="))return!1;let o=e["user-agent"]||"";if(typeof o=="string"&&/codex/i.test(o))return!0;let r=n.pathname||n.url||"";if(typeof r=="string"&&r.split("?",1)[0].endsWith("/v1/responses"))return!0;let s=n.originalModel||n.body?.model||"";return typeof s=="string"&&s.toLowerCase()==="ccr-codex"}async function fs(n,e,t,o,r,s,i){let c=e.url||new URL(t.baseUrl);if(!e.TIMEOUT){let u=o.configService.get("API_TIMEOUT_MS");u&&(e.TIMEOUT=typeof u=="string"?parseInt(u,10):u)}if(r&&typeof s.auth=="function"){let u=await s.auth(n,t);if(u.body){n=u.body;let d=e.headers||{};u.config?.headers&&(d={...d,...u.config.headers},delete d.host,delete u.config.headers),e={...e,...u.config,headers:d}}else n=u}let l={Authorization:`Bearer ${t.apiKey}`,...e?.headers||{}};for(let u in l)(l[u]==="undefined"||["authorization","Authorization"].includes(u)&&l[u]?.includes("undefined"))&&delete l[u];St(o.configService)&&wr(o.log,i.req.id,{url:String(c),headers:l,body:n});let a=await Or(c,n,{...e,headers:JSON.parse(JSON.stringify(l)),httpsProxy:ce(o.configService,t)},i,o.log);if(!a.ok){let u=await a.text();o.log.error(`[provider_response_error] Error from provider(${t.name},${n.model}: ${a.status}): ${u}`);let d=B(`Error from provider(${t.name},${n.model}: ${a.status}): ${u}`,a.status,"provider_response_error");throw d.isRateLimit=a.status===429||Dt(u),d.rawBody=u,d}if(a.ok){let u=a.headers.get("content-type")||"",d=n.stream===!0;if(o.log.info(`[hidden-error-check] provider=${t.name} model=${n.model} stream=${n.stream} isStreamRequest=${d} contentType=${u} ok=${a.ok} status=${a.status} hasBody=${!!a.body}`),!d||u.includes("application/json")){let m=a.clone(),f="";try{if(f=await m.text(),!f||f.trim()==="")throw new SyntaxError("Empty JSON body");if(Ze(f))return o.log.info(`[hidden-error-check] body is SSE mislabeled as ${u}, skipping JSON validation`),a;let y=JSON.parse(f);if(y&&typeof y=="object"&&y.error&&(typeof y.error=="string"||typeof y.error=="object"&&Object.keys(y.error).length>0)){o.log.error(`[provider_response_error] Hidden error from provider(${t.name},${n.model}: ${a.status}): ${f}`);let g=Dt(f),_=B(`Error from provider(${t.name},${n.model}: ${a.status}): ${f}`,400,"provider_response_error");throw _.isRateLimit=g,_.rawBody=f,_}}catch(y){if(y instanceof SyntaxError){o.log.warn(`[provider_response_error] Empty or malformed JSON response from provider(${t.name},${n.model}: 200): "${f}"`);let p=B(`API returned an empty or malformed response (HTTP 200) from ${t.name} \u2014 check for a proxy or gateway intercepting the request`,400,"provider_response_error");throw p.rawBody=f,p}throw y}}}if(!n.stream&&St(o.configService)){let d=await a.clone().text();Pr(o.log,i.req.id,{status:a.status,headers:Object.fromEntries(a.headers.entries()),body:d})}return a}async function ms(n,e,t,o,r,s){let i=e;if(!r&&t.transformer?.use?.length)for(let c of Array.from(t.transformer.use).reverse())!c||typeof c.transformResponseOut!="function"||(i=await c.transformResponseOut(i,s));if(!r&&t.transformer?.[n.model]?.use?.length)for(let c of Array.from(t.transformer[n.model].use).reverse())!c||typeof c.transformResponseOut!="function"||(i=await c.transformResponseOut(i,s));if(!r&&o.transformResponseIn&&(s.provider||(s.provider=t),i=await o.transformResponseIn(i,s)),s?.isCodexOnMessagesEndpoint)try{i=await new Ee().transformResponseIn(i,{...s,provider:t})}catch(c){console.error("Failed to apply Responses API conversion for codex client:",c)}return i}async function gs(n,e,t,o){n.ok||e.code(n.status);let r=Jr(n);if(!r){let s=await we(n);if(s)s.isSSE&&(r=!0),n=new Response(s.body,{status:n.status,statusText:n.statusText,headers:n.headers});else{let i=await Pe(n);n=i.response,i.isSSE&&(r=!0)}}if(r){if(e.header("Content-Type","text/event-stream"),e.header("Cache-Control","no-cache"),e.header("Connection","keep-alive"),o&&St(o.configService)){let[s,i]=n.body.tee(),c=Rr(o.configService);return Er(s,o.log,e.request.id,c),e.send(i)}return e.send(n.body)}else return F(n)}function Dt(n){let e=n.toLowerCase();return e.includes("rate limit")||e.includes("rate_limit")||e.includes("rate_limit_error")||e.includes("too many requests")||e.includes("too many")||e.includes("quota exhausted")||e.includes("qps_limit")||e.includes("token_limit")||e.includes("\u9650\u6D41")||e.includes("\u9891\u7387\u9650\u5236")}function Ac(n){try{return new URL(n),!0}catch{return!1}}var wc,hs,Ft=w(()=>{"use strict";Ar();Ct();Ye();Wn();oe();Re();Et();to();Q();De();ne();wc=/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i;hs=async n=>{n.get("/",async()=>({message:"LLMs API",version:Pt})),n.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),n.get("/providers/health",async()=>({states:U().getAllStates().map(r=>({provider:r.provider,model:r.model,status:r.status,failureCount:r.failureCount,successCount:r.successCount,lastFailureTime:r.lastFailureTime,lastError:r.lastError,rateLimitUntil:r.rateLimitUntil??null})),timestamp:new Date().toISOString()}));let e=n.transformerService.getTransformersWithEndpoint();for(let{transformer:t}of e)t.endPoint&&n.post(t.endPoint,async(o,r)=>us(o,r,n,t));n.post("/providers",{schema:{body:{type:"object",properties:{id:{type:"string"},name:{type:"string"},type:{type:"string",enum:["openai","anthropic"]},baseUrl:{type:"string"},apiKey:{type:"string"},models:{type:"array",items:{type:"string"}},proxyEnabled:{type:"boolean"}},required:["id","name","type","baseUrl","apiKey","models"]}}},async(t,o)=>{let{name:r,baseUrl:s,apiKey:i,models:c}=t.body;if(!r?.trim())throw B("Provider name is required",400,"invalid_request");if(!s||!Ac(s))throw B("Valid base URL is required",400,"invalid_request");if(!i?.trim())throw B("API key is required",400,"invalid_request");if(!c||!Array.isArray(c)||c.length===0)throw B("At least one model is required",400,"invalid_request");if(n.providerService.getProvider(t.body.name))throw B(`Provider with name '${t.body.name}' already exists`,400,"provider_exists");return n.providerService.registerProvider(t.body)}),n.get("/providers",async()=>n.providerService.getProviders()),n.get("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async t=>{let o=n.providerService.getProvider(t.params.id);if(!o)throw B("Provider not found",404,"provider_not_found");return o}),n.put("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{name:{type:"string"},type:{type:"string",enum:["openai","anthropic"]},baseUrl:{type:"string"},apiKey:{type:"string"},models:{type:"array",items:{type:"string"}},proxyEnabled:{type:"boolean"},enabled:{type:"boolean"}}}}},async(t,o)=>{let r=n.providerService.updateProvider(t.params.id,t.body);if(!r)throw B("Provider not found",404,"provider_not_found");return r}),n.delete("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async t=>{if(!n.providerService.deleteProvider(t.params.id))throw B("Provider not found",404,"provider_not_found");return{message:"Provider deleted successfully"}}),n.patch("/providers/:id/toggle",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{enabled:{type:"boolean"}},required:["enabled"]}}},async(t,o)=>{if(!n.providerService.toggleProvider(t.params.id,t.body.enabled))throw B("Provider not found",404,"provider_not_found");return{message:`Provider ${t.body.enabled?"enabled":"disabled"} successfully`}})}});var ue,Ut=w(()=>{"use strict";ue=class{constructor(e,t,o){this.configService=e;this.transformerService=t;this.logger=o;this.initializeCustomProviders()}providers=new Map;modelRoutes=new Map;initializeCustomProviders(){let e=this.configService.get("providers");if(e&&Array.isArray(e)){this.initializeFromProvidersArray(e);return}}initializeFromProvidersArray(e){e.forEach(t=>{try{if(!t.name||!t.api_base_url||!t.api_key)return;let o={};t.transformer&&Object.keys(t.transformer).forEach(r=>{r==="use"?Array.isArray(t.transformer.use)&&(o.use=t.transformer.use.map(s=>{if(Array.isArray(s)&&typeof s[0]=="string"){let i=this.transformerService.getTransformer(s[0]);if(i)return new i(s[1])}if(typeof s=="string"){let i=this.transformerService.getTransformer(s);return typeof i=="function"?new i:i}}).filter(s=>typeof s<"u")):Array.isArray(t.transformer[r]?.use)&&(o[r]={use:t.transformer[r].use.map(s=>{if(Array.isArray(s)&&typeof s[0]=="string"){let i=this.transformerService.getTransformer(s[0]);if(i)return new i(s[1])}if(typeof s=="string"){let i=this.transformerService.getTransformer(s);return typeof i=="function"?new i:i}}).filter(s=>typeof s<"u")})}),this.registerProvider({name:t.name,baseUrl:t.api_base_url,apiKey:t.api_key,models:t.models||[],quotaToken:t.quota_token,quotaSecToken:t.quota_sec_token,transformer:t.transformer?o:void 0,cacheMode:t.cache_mode||"exclusive",enabled:t.enabled!==!1,proxyEnabled:t.proxy_enabled===!0,wakeupEnabled:t.wakeup_enabled===!0,wakeupTime:t.wakeup_time||"06:00",wakeupModel:t.wakeup_model||""}),this.logger.info(`${t.name} provider registered`)}catch(o){this.logger.error(`${t.name} provider registered error: ${o}`)}})}registerProvider(e){let t={...e,enabled:e.enabled!==!1,proxyEnabled:e.proxyEnabled===!0,wakeupEnabled:e.wakeupEnabled===!0,wakeupTime:e.wakeupTime||"06:00",wakeupModel:e.wakeupModel||""};return this.providers.set(t.name,t),e.models.forEach(o=>{let r=`${t.name},${o}`,s={provider:t.name,model:o,fullModel:r};this.modelRoutes.set(r,s),this.modelRoutes.has(o)||this.modelRoutes.set(o,s)}),t}getProviders(){return Array.from(this.providers.values())}getProvider(e){return this.providers.get(e)}updateProvider(e,t){let o=this.providers.get(e);if(!o)return null;let r={...o,...t,updatedAt:new Date};return this.providers.set(e,r),t.models&&(o.models.forEach(s=>{let i=`${o.name},${s}`;this.modelRoutes.delete(i),this.modelRoutes.delete(s)}),t.models.forEach(s=>{let i=`${o.name},${s}`,c={provider:o.name,model:s,fullModel:i};this.modelRoutes.set(i,c),this.modelRoutes.has(s)||this.modelRoutes.set(s,c)})),r}deleteProvider(e){let t=this.providers.get(e);return t?(t.models.forEach(o=>{let r=`${t.name},${o}`;this.modelRoutes.delete(r),this.modelRoutes.delete(o)}),this.providers.delete(e),!0):!1}toggleProvider(e,t){let o=this.providers.get(e);if(!o)return!1;o.enabled=t;let r=this.configService.get("providers");if(r&&Array.isArray(r)){let s=r.find(i=>i.name===e);s&&(s.enabled=t)}return!0}resolveModelRoute(e){let t=this.modelRoutes.get(e);if(!t)return null;let o=this.providers.get(t.provider);return o?{provider:o,originalModel:e,targetModel:t.model}:null}getAvailableModelNames(){let e=[];return this.providers.forEach(t=>{t.enabled!==!1&&t.models.forEach(o=>{e.push(o),e.push(`${t.name},${o}`)})}),e}getModelRoutes(){return Array.from(this.modelRoutes.values())}parseTransformerConfig(e){return e?Array.isArray(e)?e.reduce((t,o)=>{if(Array.isArray(o)){let[r,s={}]=o;t[r]=s}else t[o]={};return t},{}):e:{}}async getAvailableModels(){let e=[];return this.providers.forEach(t=>{t.enabled!==!1&&t.models.forEach(o=>{e.push({id:o,object:"model",owned_by:t.name,provider:t.name}),e.push({id:`${t.name},${o}`,object:"model",owned_by:t.name,provider:t.name})})}),{object:"list",data:e}}}});var ys,_s=w(()=>{ys=n=>n<=0?"none":n<=1024?"low":n<=8192?"medium":"high"});var ks,bs=w(()=>{ks=(n,e)=>(n.includes("base64")&&(n=n.split("base64").pop(),n.startsWith(",")&&(n=n.slice(1))),`data:${e};base64,${n}`)});function Ic(n){return n.filter(e=>!!e?.function?.name).map(e=>({name:e.function.name,description:e.function.description,input_schema:e.function.parameters}))}function Ts(n){let e=[],t=[],o=m=>{if(typeof m=="string"){t.push({type:"text",text:m});return}Array.isArray(m)&&m.forEach(f=>{if(typeof f=="string"){t.push({type:"text",text:f});return}f&&typeof f=="object"&&f.type==="text"&&f.text&&t.push({...f})})},r=()=>{let m=new Map;for(let f=0;f<t.length;f++){let y=t[f],p=y?.text??JSON.stringify(y),g=m.get(p);if(g===void 0){m.set(p,f);continue}!t[g]?.cache_control&&y?.cache_control&&(t[g]=y),t.splice(f,1),f--}};n.messages.forEach(m=>{m.role==="system"?o(m.content):e.push(m)}),r();let s=[];for(let m=0;m<e.length;m++){let f=e[m];if(f.role==="tool"){let y=f.content;try{typeof f.content=="string"&&(y=JSON.parse(f.content))}catch{}y!=null&&typeof y!="object"&&typeof y!="string"&&(y=String(y));let p={type:"tool_result",tool_use_id:f.tool_call_id,content:y};f.cache_control&&(p.cache_control=f.cache_control);let g=s[s.length-1];g&&g.role==="user"&&Array.isArray(g.content)?g.content.push(p):s.push({role:"user",content:[p]})}else if(f.role==="user"){let y=[];typeof f.content=="string"?y.push({type:"text",text:f.content}):Array.isArray(f.content)&&f.content.forEach(g=>{g&&typeof g=="object"&&g.type==="image_url"&&g.image_url?.url?y.push({type:"image",source:{type:"base64",media_type:g.media_type||"image/jpeg",data:g.image_url.url.split(",")[1]||g.image_url.url}}):y.push(g)});let p=s[s.length-1];p&&p.role==="user"&&Array.isArray(p.content)?p.content.push(...y):s.push({role:"user",content:y.length>0?y:[{type:"text",text:" "}]})}else if(f.role==="assistant"){let y=[];f.content&&(typeof f.content=="string"?y.push({type:"text",text:f.content}):Array.isArray(f.content)&&y.push(...f.content)),f.tool_calls&&f.tool_calls.length>0&&f.tool_calls.forEach(g=>{let _={};try{_=JSON.parse(g.function.arguments)}catch{_=g.function.arguments}y.push({type:"tool_use",id:g.id,name:g.function.name,input:_})}),f.thinking&&y.unshift({type:"thinking",thinking:f.thinking.content,signature:f.thinking.signature});let p=s[s.length-1];p&&p.role==="assistant"&&Array.isArray(p.content)?p.content.push(...y):s.push({role:"assistant",content:y.length>0?y:[{type:"text",text:" "}]})}}let i=()=>{let m=t.filter(f=>f?.cache_control).length;for(let f of s)Array.isArray(f.content)&&(m+=f.content.filter(y=>y?.cache_control).length);return m},c=m=>m&&typeof m=="object"?{...m}:{type:"ephemeral"},l=()=>{for(let m=s.length-1;m>=0;m--){let f=s[m]?.content;if(!Array.isArray(f))continue;let y=f.find(p=>p?.cache_control);if(y?.cache_control)return y.cache_control}for(let m=t.length-1;m>=0;m--)if(t[m]?.cache_control)return t[m].cache_control;return{type:"ephemeral"}},a=(m,f)=>{if(!Array.isArray(m?.content)||m.content.some(p=>p?.cache_control))return!1;let y=m.content.find(p=>p&&typeof p=="object"&&!p.cache_control);return y?(y.cache_control=c(f),!0):!1};(()=>{let m=4-i();if(m<=0||s.length===0)return;let f=s,y=l(),p=[];for(let g=f.length-1;g>=0;g--)f[g]?.role!=="user"||!Array.isArray(f[g].content)||p.push(g);for(let g of p){if(m<=0)break;a(f[g],y)&&m--}})();let d={messages:s,model:n.model,max_tokens:n.max_tokens||4096,temperature:n.temperature,stream:n.stream};if(t.length>0){let m=t.some(f=>f?.cache_control);d.system=m?t:t.map(f=>f.text||"").join(`
44
+ ${r}`}],_reasoningItem:!0})}else if(o.role){let r=o.content;Array.isArray(r)&&(r=r.map(s=>s.type==="input_text"?{type:"text",text:s.text}:s.type==="output_text"?{type:"text",text:s.text}:s.type==="input_image"?{type:"image_url",image_url:{url:s.image_url},media_type:s.mime_type}:s)),e.push({role:o.role,content:r})}n.messages=e,delete n.input,n.instructions&&typeof n.instructions=="string"&&(n.system=n.instructions,delete n.instructions),Array.isArray(n.tools)&&(n.tools=n.tools.filter(o=>o&&typeof o=="object").map(o=>o.type==="function"&&o.name&&!o.function?{type:"function",function:{name:o.name,description:o.description||"",parameters:o.parameters||{type:"object",properties:{}}}}:o.function?.name?o:o.type==="web_search"?{type:"function",function:{name:"web_search",description:"Search the web",parameters:{type:"object",properties:{query:{type:"string"}}}}}:null).filter(Boolean)),n.reasoning&&typeof n.reasoning=="object"&&(n.reasoning={enabled:!0,effort:n.reasoning.effort}),delete n.store,delete n.previous_response_id,delete n.include}async function ps(n,e,t,o){let r=n.body;(n.ccrHookOrder||=[]).push("handler"),o.endPoint==="/v1/responses"&&r?.input&&!r?.messages&&rt(r),o.endPoint==="/v1/responses"&&!n.provider&&typeof r?.model=="string"&&Oc.test(r.model)&&await Lt(n,e,{configService:t.configService,tokenizerService:t.tokenizerService});let s=n.provider;if(!s&&r?.model){let a=r.model.split(",");a.length>1&&(s=a[0],r.model=a.slice(1).join(","))}let i=s?t.providerService.getProvider(s):void 0;if(!i&&r?.model){let a=t.providerService.resolveModelRoute(r.model);a&&(i=a.provider,r.model=a.targetModel)}if(!i&&r?.model){let u=t.configService.get("Router")?.default;if(u&&u.includes(",")){let[d,...f]=u.split(","),g=t.providerService.getProvider(d);g&&(i=g,r.model=f.join(","))}}if(n.originalModel||(n.originalModel=r?.model),!i)throw J(`Provider '${s}' not found`,404,"provider_not_found");n.provider=i.name||s;let c=Mc(n),l=o.endPoint==="/v1/messages"&&c;c&&e.header("X-Codex-Detected","true"),l&&e.header("X-Codex-On-Messages","true"),n.isCodexOnMessagesEndpoint=l,n.codexDetected=c;try{o.endPoint==="/v1/responses"&&r.input&&!r.messages&&rt(r);let{requestBody:a,config:u,bypass:d}=await ms(r,i,o,n.headers,{req:n});if(a?.messages&&!d&&!n.isTargetAnthropic){let m=a.messages,p=new Set,_=[],k=[];for(let h of m){if(h?.role!=="system"){k.push(h);continue}let b=JSON.stringify(h.content??"");p.has(b)||(p.add(b),_.push(h))}_.length>0&&k.length>0&&(a.messages=[..._,...k])}let f=await gs(a,u,i,t,d,o,{req:n});try{i?.baseUrl&&f?.headers&&Xe(s,i.baseUrl,f.headers)}catch{}let g=await hs(a,f,i,o,d,{req:n,isCodexOnMessagesEndpoint:l});g=await fs(r,g,i.name||s);let y=ys(g,e,r,t);try{let m=r.model||n.originalModel;s&&m&&U().recordSuccess(s,m)}catch{}return y}catch(a){if(a instanceof q)throw a;if(!n.__retryAttempted){n.__retryAttempted=!0;try{return await ps(n,e,t,o)}catch(d){if(n.__fallbackExhausted)throw d;a=d}}let u=await Ac(n,e,t,o,a);if(u)return u;throw n.__fallbackExhausted=!0,a}}async function fs(n,e,t){if(!n?.stream||!e?.body)return e;let o=e.body.getReader(),r=new TextDecoder,s=[],i="";for(let a=0;a<8;a++){let u=await o.read();if(u.done||(u.value&&(s.push(u.value),i+=r.decode(u.value,{stream:!0})),i.split(`
45
+ `).some(d=>d.startsWith("data:"))))break}if(i+=r.decode(),s.length===0)throw o.releaseLock(),J(`Provider returned empty streaming response from ${t}`,400,"provider_response_error");if(i.includes("event: error")){o.releaseLock();let a=i.length<500?i:i.slice(0,500);throw J(`Provider ${t} returned error in SSE stream: ${a}`,400,"provider_response_error")}if(i.split(`
46
+ `).filter(a=>a.startsWith("data:")&&a.trim().length>5).length===0){o.releaseLock();let a=i.length<500?i:i.slice(0,500);throw J(`Provider ${t} returned streaming response with no SSE data lines: ${a}`,400,"provider_response_error")}let l=new ReadableStream({start:a=>{for(let d of s)a.enqueue(d);let u=()=>{o.read().then(({done:d,value:f})=>{d?a.close():(a.enqueue(f),u())}).catch(d=>a.error(d))};u()},cancel:()=>{o.cancel()}});return new Response(l,{headers:e.headers,status:e.status,statusText:e.statusText})}async function Ac(n,e,t,o,r){let s=n.scenarioType||"default",i=n.familyFallback,c=n.modelFamily,l=n.strictProjectRouting?n.fallbackConfig:n.fallbackConfig??t.configService.get("fallback"),a=U(),u=n.provider||"",d=n.body.model||"",f=new Set,g=r?.statusCode===429||r?.isRateLimit===!0||r?.rawBody&&Dt(r.rawBody)||r?.message&&Dt(r.message);if(u&&d&&(g?(a.markRateLimited(u,d,120,r?.message),n.log.warn(`Primary model ${u},${d} rate-limited, will try fallbacks`)):a.recordFailure(u,d,r?.message),f.add(`${u},${d}`),t.recordUsage?.({provider:u,model:d,originalModel:n.originalModel||d,scenarioType:s,modelFamily:n.modelFamily,errorMessage:r?.message||String(r),sessionId:n.usageSessionId||n.id,stream:n.body.stream,inputTokens:n.tokenCount||0,req:n})),!(n.enableFallback??t.configService.get("Router")?.enableFallback===!0))return n.log.info("Fallback disabled by configuration, skipping fallback attempts"),null;let m=k=>{let[h,...b]=k.split(","),T=h?.trim(),x=b.join(",").trim();return!T||!x?null:{provider:T,model:x,key:`${T},${x}`}},p=[];if(c){let k=i?.[s];if(Array.isArray(k)&&k.length>0)p.push({name:`${c}/${s}`,models:k});else{let h=n.strictProjectRouting;n.log.warn(`No ${c} fallback configured for ${s}; ${h?"no project fallback available":"will try global fallback"}`)}}if(Array.isArray(l?.[s])&&l[s].length>0&&p.push({name:`global/${s}`,models:l[s]}),p.length===0&&s!=="image"&&n.body?.messages?.some(b=>b.role==="user"&&Array.isArray(b.content)&&b.content.some(T=>T.type==="image"||T.type==="image_url"||Array.isArray(T?.content)&&T.content.some(x=>x.type==="image"||x.type==="image_url")))&&Array.isArray(l?.image)&&l.image.length>0&&(n.log.info(`No fallback for scenario '${s}', but request has images \u2014 trying fallback.image`),p.push({name:"global/image (auto-detected)",models:l.image})),p.length===0)return null;let _=p.reduce((k,h)=>k+h.models.length,0);n.log.warn(`Request failed for ${s}, trying ${_} fallback models across ${p.length} fallback stage(s)`);for(let k of p){n.log.info(`Trying ${k.name} fallback stage with ${k.models.length} models`);for(let h of k.models){let b=m(h);if(!b){n.log.warn(`Fallback model '${h}' is invalid, skipping`);continue}if(f.has(b.key))continue;f.add(b.key);let T=t.providerService.getProviders(),x=nt(T,b.provider,b.model);if(!x){n.log.warn(`Fallback model ${h} is not configured or provider is disabled, skipping`);continue}let C=x.provider.name,S=x.model;if(!a.isAvailable(C,S)){n.log.warn(`Fallback model ${h} unavailable (fail pool), skipping`);continue}let v;for(let P=1;P<=2;P++)try{n.log.info(`Trying fallback model: ${C},${S}${P>1?" (retry)":""}`);let R={...n.body};R.model=S;let O={...n,provider:C,body:R},E=x.provider,{requestBody:A,config:N,bypass:D}=await ms(R,E,o,n.headers,{req:O}),H=await gs(A,N,E,t,D,o,{req:O});try{E?.baseUrl&&H?.headers&&Xe(C,E.baseUrl,H.headers)}catch{}let $=await hs(A,H,E,o,D,{req:O,isCodexOnMessagesEndpoint:n.isCodexOnMessagesEndpoint||!1});return $=await fs(R,$,C),n.log.info(`Fallback model ${C},${S} succeeded`),a.recordSuccess(C,S),u&&d&&!n.strictProjectRouting&&(Se().promote(u,d,s,C,S),n.log.info(`Promoted fallback model ${C},${S} for ${u},${d}:${s}`),g?n.log.info(`Rate-limited original model ${u},${d} already marked unavailable with cooldown`):(a.forceOpen(u,d,r?.message),n.log.info(`Marked original model ${u},${d} as unavailable`))),n.provider=C,n.body=R,ys($,e,R,t)}catch(R){if(v=R,P===1){n.log.warn(`Fallback model ${C},${S} failed, retrying once: ${R.message}`);continue}}a.recordFailure(C,S,v?.message),n.log.warn(`Fallback model ${C},${S} failed after retry: ${v?.message}`),t.recordUsage?.({provider:C,model:S,originalModel:n.originalModel||d||n.body.model,scenarioType:s,modelFamily:n.modelFamily,errorMessage:v?.message||String(v),sessionId:n.usageSessionId||n.id,stream:n.body.stream,inputTokens:n.tokenCount||0,req:n})}}return n.log.error(`All fallback models failed for ${s}`),null}async function ms(n,e,t,o,r){let s=JSON.parse(JSON.stringify(n)),i=s?.system;if(typeof i=="string"&&i.includes("cch="))s.system=i.replace(/cch=[^;]+;?/g,"cch=ccr-stable;");else if(Array.isArray(i))for(let a=0;a<i.length;a++){let u=i[a],d=typeof u=="string"?u:u?.text??"";if(d.includes("cch=")){let f=d.replace(/cch=[^;]+;?/g,"cch=ccr-stable;");typeof u=="string"?i[a]=f:typeof u?.text=="string"&&(u.text=f)}}let c={},l=!1;if(l=Ic(e,t,n),l&&(o instanceof Headers?o.delete("content-length"):delete o["content-length"],c.headers=o),!l&&typeof t.transformRequestOut=="function"){let a=await t.transformRequestOut(s);a.body?(s=a.body,c=a.config||{}):s=a}if(!l&&e.transformer?.use?.length)for(let a of e.transformer.use){if(!a||typeof a.transformRequestIn!="function")continue;let u=await a.transformRequestIn(s,e,r);u.body?(s=u.body,c={...c,...u.config}):s=u}if(!l&&e.transformer?.[n.model]?.use?.length)for(let a of e.transformer[n.model].use)!a||typeof a.transformRequestIn!="function"||(s=await a.transformRequestIn(s,e,r));return{requestBody:s,config:c,bypass:l}}function Ic(n,e,t){return e.endPoint==="/v1/chat/completions"&&Array.isArray(t?.system)?!1:n.transformer?.use?.length===1&&n.transformer.use[0].name===e.name&&(!n.transformer?.[t.model]?.use.length||n.transformer?.[t.model]?.use.length===1&&n.transformer?.[t.model]?.use[0].name===e.name)}function Mc(n){if(n.clientType)return n.clientType==="codex";let e=n.headers||{},t=e["x-anthropic-billing-header"]||"";if(typeof t=="string"&&t.includes("cc_version="))return!1;let o=e["user-agent"]||"";if(typeof o=="string"&&/codex/i.test(o))return!0;let r=n.pathname||n.url||"";if(typeof r=="string"&&r.split("?",1)[0].endsWith("/v1/responses"))return!0;let s=n.originalModel||n.body?.model||"";return typeof s=="string"&&s.toLowerCase()==="ccr-codex"}async function gs(n,e,t,o,r,s,i){let c=e.url||new URL(t.baseUrl);if(!e.TIMEOUT){let u=o.configService.get("API_TIMEOUT_MS");u&&(e.TIMEOUT=typeof u=="string"?parseInt(u,10):u)}if(r&&typeof s.auth=="function"){let u=await s.auth(n,t);if(u.body){n=u.body;let d=e.headers||{};u.config?.headers&&(d={...d,...u.config.headers},delete d.host,delete u.config.headers),e={...e,...u.config,headers:d}}else n=u}let l={Authorization:`Bearer ${t.apiKey}`,...e?.headers||{}};for(let u in l)(l[u]==="undefined"||["authorization","Authorization"].includes(u)&&l[u]?.includes("undefined"))&&delete l[u];St(o.configService)&&Er(o.log,i.req.id,{url:String(c),headers:l,body:n});let a=await Ir(c,n,{...e,headers:JSON.parse(JSON.stringify(l)),httpsProxy:ce(o.configService,t)},i,o.log);if(!a.ok){let u=await a.text();o.log.error(`[provider_response_error] Error from provider(${t.name},${n.model}: ${a.status}): ${u}`);let d=J(`Error from provider(${t.name},${n.model}: ${a.status}): ${u}`,a.status,"provider_response_error");throw d.isRateLimit=a.status===429||Dt(u),d.rawBody=u,d}if(a.ok){let u=a.headers.get("content-type")||"",d=n.stream===!0;if(o.log.info(`[hidden-error-check] provider=${t.name} model=${n.model} stream=${n.stream} isStreamRequest=${d} contentType=${u} ok=${a.ok} status=${a.status} hasBody=${!!a.body}`),!d||u.includes("application/json")){let f=a.clone(),g="";try{if(g=await f.text(),!g||g.trim()==="")throw new SyntaxError("Empty JSON body");if(qe(g))return o.log.info(`[hidden-error-check] body is SSE mislabeled as ${u}, skipping JSON validation`),a;let y=JSON.parse(g);if(y&&typeof y=="object"&&y.error&&(typeof y.error=="string"||typeof y.error=="object"&&Object.keys(y.error).length>0)){o.log.error(`[provider_response_error] Hidden error from provider(${t.name},${n.model}: ${a.status}): ${g}`);let p=Dt(g),_=J(`Error from provider(${t.name},${n.model}: ${a.status}): ${g}`,400,"provider_response_error");throw _.isRateLimit=p,_.rawBody=g,_}}catch(y){if(y instanceof SyntaxError){o.log.warn(`[provider_response_error] Empty or malformed JSON response from provider(${t.name},${n.model}: 200): "${g}"`);let m=J(`API returned an empty or malformed response (HTTP 200) from ${t.name} \u2014 check for a proxy or gateway intercepting the request`,400,"provider_response_error");throw m.rawBody=g,m}throw y}}}if(!n.stream&&St(o.configService)){let d=await a.clone().text();Or(o.log,i.req.id,{status:a.status,headers:Object.fromEntries(a.headers.entries()),body:d})}return a}async function hs(n,e,t,o,r,s){let i=e;if(!r&&t.transformer?.use?.length)for(let c of Array.from(t.transformer.use).reverse())!c||typeof c.transformResponseOut!="function"||(i=await c.transformResponseOut(i,s));if(!r&&t.transformer?.[n.model]?.use?.length)for(let c of Array.from(t.transformer[n.model].use).reverse())!c||typeof c.transformResponseOut!="function"||(i=await c.transformResponseOut(i,s));if(!r&&o.transformResponseIn&&(s.provider||(s.provider=t),i=await o.transformResponseIn(i,s)),s?.isCodexOnMessagesEndpoint)try{i=await new Oe().transformResponseIn(i,{...s,provider:t})}catch(c){console.error("Failed to apply Responses API conversion for codex client:",c)}return i}async function ys(n,e,t,o){n.ok||e.code(n.status);let r=Wr(n);if(!r){let s=await Pe(n);if(s)s.isSSE&&(r=!0),n=new Response(s.body,{status:n.status,statusText:n.statusText,headers:n.headers});else{let i=await Ee(n);n=i.response,i.isSSE&&(r=!0)}}if(r){if(e.header("Content-Type","text/event-stream"),e.header("Cache-Control","no-cache"),e.header("Connection","keep-alive"),o&&St(o.configService)){let[s,i]=n.body.tee(),c=Pr(o.configService);return Ar(s,o.log,e.request.id,c),e.send(i)}return e.send(n.body)}else return F(n)}function Dt(n){let e=n.toLowerCase();return e.includes("rate limit")||e.includes("rate_limit")||e.includes("rate_limit_error")||e.includes("too many requests")||e.includes("too many")||e.includes("quota exhausted")||e.includes("qps_limit")||e.includes("token_limit")||e.includes("\u9650\u6D41")||e.includes("\u9891\u7387\u9650\u5236")}function $c(n){try{return new URL(n),!0}catch{return!1}}var Oc,_s,Ft=w(()=>{"use strict";Mr();Ct();Ye();Gn();re();we();Et();no();Q();Fe();oe();Oc=/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i;_s=async n=>{n.get("/",async()=>({message:"LLMs API",version:Pt})),n.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),n.get("/providers/health",async()=>({states:U().getAllStates().map(r=>({provider:r.provider,model:r.model,status:r.status,failureCount:r.failureCount,successCount:r.successCount,lastFailureTime:r.lastFailureTime,lastError:r.lastError,rateLimitUntil:r.rateLimitUntil??null})),timestamp:new Date().toISOString()}));let e=n.transformerService.getTransformersWithEndpoint();for(let{transformer:t}of e)t.endPoint&&n.post(t.endPoint,async(o,r)=>ps(o,r,n,t));n.post("/providers",{schema:{body:{type:"object",properties:{id:{type:"string"},name:{type:"string"},type:{type:"string",enum:["openai","anthropic"]},baseUrl:{type:"string"},apiKey:{type:"string"},models:{type:"array",items:{type:"string"}},proxyEnabled:{type:"boolean"}},required:["id","name","type","baseUrl","apiKey","models"]}}},async(t,o)=>{let{name:r,baseUrl:s,apiKey:i,models:c}=t.body;if(!r?.trim())throw J("Provider name is required",400,"invalid_request");if(!s||!$c(s))throw J("Valid base URL is required",400,"invalid_request");if(!i?.trim())throw J("API key is required",400,"invalid_request");if(!c||!Array.isArray(c)||c.length===0)throw J("At least one model is required",400,"invalid_request");if(n.providerService.getProvider(t.body.name))throw J(`Provider with name '${t.body.name}' already exists`,400,"provider_exists");return n.providerService.registerProvider(t.body)}),n.get("/providers",async()=>n.providerService.getProviders()),n.get("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async t=>{let o=n.providerService.getProvider(t.params.id);if(!o)throw J("Provider not found",404,"provider_not_found");return o}),n.put("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{name:{type:"string"},type:{type:"string",enum:["openai","anthropic"]},baseUrl:{type:"string"},apiKey:{type:"string"},models:{type:"array",items:{type:"string"}},proxyEnabled:{type:"boolean"},enabled:{type:"boolean"}}}}},async(t,o)=>{let r=n.providerService.updateProvider(t.params.id,t.body);if(!r)throw J("Provider not found",404,"provider_not_found");return r}),n.delete("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async t=>{if(!n.providerService.deleteProvider(t.params.id))throw J("Provider not found",404,"provider_not_found");return{message:"Provider deleted successfully"}}),n.patch("/providers/:id/toggle",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{enabled:{type:"boolean"}},required:["enabled"]}}},async(t,o)=>{if(!n.providerService.toggleProvider(t.params.id,t.body.enabled))throw J("Provider not found",404,"provider_not_found");return{message:`Provider ${t.body.enabled?"enabled":"disabled"} successfully`}})}});var ue,Ut=w(()=>{"use strict";ue=class{constructor(e,t,o){this.configService=e;this.transformerService=t;this.logger=o;this.initializeCustomProviders()}providers=new Map;modelRoutes=new Map;initializeCustomProviders(){let e=this.configService.get("providers");if(e&&Array.isArray(e)){this.initializeFromProvidersArray(e);return}}initializeFromProvidersArray(e){e.forEach(t=>{try{if(!t.name||!t.api_base_url||!t.api_key)return;let o={};t.transformer&&Object.keys(t.transformer).forEach(s=>{s==="use"?Array.isArray(t.transformer.use)&&(o.use=t.transformer.use.map(i=>{if(Array.isArray(i)&&typeof i[0]=="string"){let c=this.transformerService.getTransformer(i[0]);if(c)return new c(i[1])}if(typeof i=="string"){let c=this.transformerService.getTransformer(i);return typeof c=="function"?new c:c}}).filter(i=>typeof i<"u")):Array.isArray(t.transformer[s]?.use)&&(o[s]={use:t.transformer[s].use.map(i=>{if(Array.isArray(i)&&typeof i[0]=="string"){let c=this.transformerService.getTransformer(i[0]);if(c)return new c(i[1])}if(typeof i=="string"){let c=this.transformerService.getTransformer(i);return typeof c=="function"?new c:c}}).filter(i=>typeof i<"u")})});let r=t.default_thinking_level;if(r){let s=this.transformerService.getTransformer("defaultthinking");if(s){let i=new s({level:r});o.use=[i,...o.use||[]]}else this.logger.warn(`default_thinking_level set on ${t.name} but defaultthinking transformer is not registered`)}this.registerProvider({name:t.name,baseUrl:t.api_base_url,apiKey:t.api_key,models:t.models||[],quotaToken:t.quota_token,quotaSecToken:t.quota_sec_token,transformer:t.transformer?o:void 0,cacheMode:t.cache_mode||"exclusive",enabled:t.enabled!==!1,proxyEnabled:t.proxy_enabled===!0,wakeupEnabled:t.wakeup_enabled===!0,wakeupTime:t.wakeup_time||"06:00",wakeupModel:t.wakeup_model||""}),this.logger.info(`${t.name} provider registered`)}catch(o){this.logger.error(`${t.name} provider registered error: ${o}`)}})}registerProvider(e){let t={...e,enabled:e.enabled!==!1,proxyEnabled:e.proxyEnabled===!0,wakeupEnabled:e.wakeupEnabled===!0,wakeupTime:e.wakeupTime||"06:00",wakeupModel:e.wakeupModel||""};return this.providers.set(t.name,t),e.models.forEach(o=>{let r=`${t.name},${o}`,s={provider:t.name,model:o,fullModel:r};this.modelRoutes.set(r,s),this.modelRoutes.has(o)||this.modelRoutes.set(o,s)}),t}getProviders(){return Array.from(this.providers.values())}getProvider(e){return this.providers.get(e)}updateProvider(e,t){let o=this.providers.get(e);if(!o)return null;let r={...o,...t,updatedAt:new Date};return this.providers.set(e,r),t.models&&(o.models.forEach(s=>{let i=`${o.name},${s}`;this.modelRoutes.delete(i),this.modelRoutes.delete(s)}),t.models.forEach(s=>{let i=`${o.name},${s}`,c={provider:o.name,model:s,fullModel:i};this.modelRoutes.set(i,c),this.modelRoutes.has(s)||this.modelRoutes.set(s,c)})),r}deleteProvider(e){let t=this.providers.get(e);return t?(t.models.forEach(o=>{let r=`${t.name},${o}`;this.modelRoutes.delete(r),this.modelRoutes.delete(o)}),this.providers.delete(e),!0):!1}toggleProvider(e,t){let o=this.providers.get(e);if(!o)return!1;o.enabled=t;let r=this.configService.get("providers");if(r&&Array.isArray(r)){let s=r.find(i=>i.name===e);s&&(s.enabled=t)}return!0}resolveModelRoute(e){let t=this.modelRoutes.get(e);if(!t)return null;let o=this.providers.get(t.provider);return o?{provider:o,originalModel:e,targetModel:t.model}:null}getAvailableModelNames(){let e=[];return this.providers.forEach(t=>{t.enabled!==!1&&t.models.forEach(o=>{e.push(o),e.push(`${t.name},${o}`)})}),e}getModelRoutes(){return Array.from(this.modelRoutes.values())}parseTransformerConfig(e){return e?Array.isArray(e)?e.reduce((t,o)=>{if(Array.isArray(o)){let[r,s={}]=o;t[r]=s}else t[o]={};return t},{}):e:{}}async getAvailableModels(){let e=[];return this.providers.forEach(t=>{t.enabled!==!1&&t.models.forEach(o=>{e.push({id:o,object:"model",owned_by:t.name,provider:t.name}),e.push({id:`${t.name},${o}`,object:"model",owned_by:t.name,provider:t.name})})}),{object:"list",data:e}}}});var ks,bs,fo=w(()=>{ks=n=>n<=0?"none":n<=1024?"low":n<=8192?"medium":"high",bs=n=>n==="none"?0:n==="low"?1024:n==="medium"?8192:16384});var Ts,vs=w(()=>{Ts=(n,e)=>(n.includes("base64")&&(n=n.split("base64").pop(),n.startsWith(",")&&(n=n.slice(1))),`data:${e};base64,${n}`)});function Nc(n){return n.filter(e=>!!e?.function?.name).map(e=>({name:e.function.name,description:e.function.description,input_schema:e.function.parameters}))}function Cs(n){let e=[],t=[],o=f=>{if(typeof f=="string"){t.push({type:"text",text:f});return}Array.isArray(f)&&f.forEach(g=>{if(typeof g=="string"){t.push({type:"text",text:g});return}g&&typeof g=="object"&&g.type==="text"&&g.text&&t.push({...g})})},r=()=>{let f=new Map;for(let g=0;g<t.length;g++){let y=t[g],m=y?.text??JSON.stringify(y),p=f.get(m);if(p===void 0){f.set(m,g);continue}!t[p]?.cache_control&&y?.cache_control&&(t[p]=y),t.splice(g,1),g--}};n.messages.forEach(f=>{f.role==="system"?o(f.content):e.push(f)}),r();let s=[];for(let f=0;f<e.length;f++){let g=e[f];if(g.role==="tool"){let y=g.content;try{typeof g.content=="string"&&(y=JSON.parse(g.content))}catch{}y!=null&&typeof y!="object"&&typeof y!="string"&&(y=String(y));let m={type:"tool_result",tool_use_id:g.tool_call_id,content:y};g.cache_control&&(m.cache_control=g.cache_control);let p=s[s.length-1];p&&p.role==="user"&&Array.isArray(p.content)?p.content.push(m):s.push({role:"user",content:[m]})}else if(g.role==="user"){let y=[];typeof g.content=="string"?y.push({type:"text",text:g.content}):Array.isArray(g.content)&&g.content.forEach(p=>{p&&typeof p=="object"&&p.type==="image_url"&&p.image_url?.url?y.push({type:"image",source:{type:"base64",media_type:p.media_type||"image/jpeg",data:p.image_url.url.split(",")[1]||p.image_url.url}}):y.push(p)});let m=s[s.length-1];m&&m.role==="user"&&Array.isArray(m.content)?m.content.push(...y):s.push({role:"user",content:y.length>0?y:[{type:"text",text:" "}]})}else if(g.role==="assistant"){let y=[];g.content&&(typeof g.content=="string"?y.push({type:"text",text:g.content}):Array.isArray(g.content)&&y.push(...g.content)),g.tool_calls&&g.tool_calls.length>0&&g.tool_calls.forEach(p=>{let _={};try{_=JSON.parse(p.function.arguments)}catch{_=p.function.arguments}y.push({type:"tool_use",id:p.id,name:p.function.name,input:_})}),g.thinking&&y.unshift({type:"thinking",thinking:g.thinking.content,signature:g.thinking.signature});let m=s[s.length-1];m&&m.role==="assistant"&&Array.isArray(m.content)?m.content.push(...y):s.push({role:"assistant",content:y.length>0?y:[{type:"text",text:" "}]})}}let i=()=>{let f=t.filter(g=>g?.cache_control).length;for(let g of s)Array.isArray(g.content)&&(f+=g.content.filter(y=>y?.cache_control).length);return f},c=f=>f&&typeof f=="object"?{...f}:{type:"ephemeral"},l=()=>{for(let f=s.length-1;f>=0;f--){let g=s[f]?.content;if(!Array.isArray(g))continue;let y=g.find(m=>m?.cache_control);if(y?.cache_control)return y.cache_control}for(let f=t.length-1;f>=0;f--)if(t[f]?.cache_control)return t[f].cache_control;return{type:"ephemeral"}},a=(f,g)=>{if(!Array.isArray(f?.content)||f.content.some(m=>m?.cache_control))return!1;let y=f.content.find(m=>m&&typeof m=="object"&&!m.cache_control);return y?(y.cache_control=c(g),!0):!1};(()=>{let f=4-i();if(f<=0||s.length===0)return;let g=s,y=l(),m=[];for(let p=g.length-1;p>=0;p--)g[p]?.role!=="user"||!Array.isArray(g[p].content)||m.push(p);for(let p of m){if(f<=0)break;a(g[p],y)&&f--}})();let d={messages:s,model:n.model,max_tokens:n.max_tokens||4096,temperature:n.temperature,stream:n.stream};if(n.reasoning?.enabled){let f=n.reasoning.max_tokens||bs(n.reasoning.effort||"medium"),g=Math.min(f,d.max_tokens-1024);g>=1024&&(d.thinking={type:"enabled",budget_tokens:g})}if(t.length>0){let f=t.some(g=>g?.cache_control);d.system=f?t:t.map(g=>g.text||"").join(`
47
47
 
48
- `)}return n.tools&&n.tools.length>0&&(d.tools=Ic(n.tools),n.tool_choice&&(n.tool_choice==="auto"?d.tool_choice={type:"auto"}:n.tool_choice==="any"||n.tool_choice==="required"?d.tool_choice={type:"any"}:typeof n.tool_choice=="string"?d.tool_choice={type:"tool",name:n.tool_choice}:typeof n.tool_choice=="object"&&n.tool_choice.type==="function"&&(d.tool_choice={type:"tool",name:n.tool_choice.function.name}))),d}var vs=w(()=>{});import{v4 as po}from"uuid";var jt,Cs=w(()=>{"use strict";_s();Ct();Q();bs();vs();jt=class{constructor(e){this.options=e;this.useBearer=this.options?.UseBearer??!1}name="Anthropic";endPoint="/v1/messages";useBearer;logger;async auth(e,t){let o={};return this.useBearer?(o.authorization=`Bearer ${t.apiKey}`,o["x-api-key"]=void 0):(o["x-api-key"]=t.apiKey,o.authorization=void 0),{body:e,config:{headers:o}}}async transformRequestOut(e){let t=[];if(e.system){if(typeof e.system=="string")t.push({role:"system",content:e.system});else if(Array.isArray(e.system)&&e.system.length){let s=e.system.filter(i=>i.type==="text"&&i.text).map(i=>({type:"text",text:i.text,cache_control:i.cache_control}));t.push({role:"system",content:s})}}let o=JSON.parse(JSON.stringify(e.messages||[]));o?.forEach(s=>{s.role==="system"&&t.push({role:"system",content:s.content})}),o?.forEach(s=>{if(s.role!=="system"&&(s.role==="user"||s.role==="assistant")){if(typeof s.content=="string"){t.push({role:s.role,content:s.content});return}if(Array.isArray(s.content)){if(s.role==="user"){let i=s.content.filter(l=>l.type==="tool_result"&&l.tool_use_id);i.length&&i.forEach(l=>{let a={role:"tool",content:typeof l.content=="string"?l.content:JSON.stringify(l.content),tool_call_id:l.tool_use_id,cache_control:l.cache_control};t.push(a)});let c=s.content.filter(l=>l.type==="text"&&l.text||l.type==="image"&&l.source);c.length&&t.push({role:"user",content:c.map(l=>l?.type==="image"?{type:"image_url",image_url:{url:l.source?.type==="base64"?ks(l.source.data,l.source.media_type):l.source.url},media_type:l.source.media_type}:l)})}else if(s.role==="assistant"){let i={role:"assistant",content:""},c=s.content.filter(u=>u.type==="text"&&u.text);c.length&&(i.content=c.map(u=>u.text).join(`
49
- `));let l=s.content.filter(u=>u.type==="tool_use"&&u.id);l.length&&(i.tool_calls=l.map(u=>({id:u.id,type:"function",function:{name:u.name,arguments:JSON.stringify(u.input||{})}})));let a=s.content.find(u=>u.type==="thinking");a&&(i.thinking={content:a.thinking,signature:a.signature}),t.push(i)}return}}});let r={messages:t,model:e.model,max_tokens:e.max_tokens,temperature:e.temperature,stream:e.stream,tools:e.tools?.length?this.convertAnthropicToolsToUnified(e.tools):void 0,tool_choice:e.tool_choice};return e.thinking&&(r.reasoning={effort:ys(e.thinking.budget_tokens),enabled:e.thinking.type==="enabled"}),e.tool_choice&&(e.tool_choice.type==="tool"?r.tool_choice={type:"function",function:{name:e.tool_choice.name}}:r.tool_choice=e.tool_choice.type),r}async transformRequestIn(e,t,o){if(!this.isAnthropicMessagesEndpoint(t.baseUrl))return e;o&&o.req&&(o.req.isTargetAnthropic=!0);let r=Ts(e),s={};return this.useBearer?(s.authorization=`Bearer ${t.apiKey}`,s["x-api-key"]=void 0):(s["x-api-key"]=t.apiKey,s.authorization=void 0),s["anthropic-version"]="2023-06-01",s["content-type"]="application/json",{body:r,config:{headers:s}}}isAnthropicMessagesEndpoint(e){try{return new URL(e.trim()).pathname.replace(/\/+$/,"").endsWith("/messages")}catch{return!1}}async transformResponseIn(e,t){if(t?.req?.isTargetAnthropic)return e;if(e.headers.get("Content-Type")?.includes("text/event-stream")){if(!e.body)throw new Error("Stream response body is null");let r=await this.convertOpenAIStreamToAnthropic(e.body,t);return new Response(r,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}else{let r=await we(e);if((r?.isSSE??!1)&&r?.body){let a=await this.convertOpenAIStreamToAnthropic(r.body,t);return new Response(a,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}let i;if(r?.body)i=new Response(r.body,{status:e.status,statusText:e.statusText,headers:e.headers});else{let a=await Pe(e);if(a.isSSE){let u=await this.convertOpenAIStreamToAnthropic(a.response.body,t);return new Response(u,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}i=a.response}let c=await F(i),l=this.convertOpenAIResponseToAnthropic(c,t);return new Response(JSON.stringify(l),{headers:{"Content-Type":"application/json"}})}}convertAnthropicToolsToUnified(e){return e.map(t=>({type:"function",function:{name:t.name,description:t.description||"",parameters:t.input_schema}}))}async convertOpenAIStreamToAnthropic(e,t){return new ReadableStream({start:async r=>{let s=new TextEncoder,i=`msg_${Date.now()}`,c=null,l="unknown",a=!1,u=!1,d=!1,m=new Map,f=new Map,y=0,p=0,g=0,_=!1,k=!1,h=0,b=-1,T=()=>{let v=h;return h++,v},x=v=>{if(!_)try{r.enqueue(v);let P=new TextDecoder().decode(v);this.logger.debug({reqId:t.req.id,data:P,type:"send data"})}catch(P){if(P instanceof TypeError&&P.message.includes("Controller is already closed"))_=!0;else throw this.logger.debug({reqId:t.req.id,error:P instanceof Error?P.message:String(P),type:"send data error"}),P}},C=()=>{if(!_)try{if(!a){this.logger?.warn("[empty_stream] Upstream produced no data, closing silently"),r.close(),_=!0;return}if(b>=0){let P={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
48
+ `)}return n.tools&&n.tools.length>0&&(d.tools=Nc(n.tools),n.tool_choice&&(n.tool_choice==="auto"?d.tool_choice={type:"auto"}:n.tool_choice==="any"||n.tool_choice==="required"?d.tool_choice={type:"any"}:typeof n.tool_choice=="string"?d.tool_choice={type:"tool",name:n.tool_choice}:typeof n.tool_choice=="object"&&n.tool_choice.type==="function"&&(d.tool_choice={type:"tool",name:n.tool_choice.function.name}))),d}var xs=w(()=>{fo()});import{v4 as mo}from"uuid";var jt,Ss=w(()=>{"use strict";fo();Ct();Q();vs();xs();jt=class{constructor(e){this.options=e;this.useBearer=this.options?.UseBearer??!1}name="Anthropic";endPoint="/v1/messages";useBearer;logger;async auth(e,t){let o={};return this.useBearer?(o.authorization=`Bearer ${t.apiKey}`,o["x-api-key"]=void 0):(o["x-api-key"]=t.apiKey,o.authorization=void 0),{body:e,config:{headers:o}}}async transformRequestOut(e){let t=[];if(e.system){if(typeof e.system=="string")t.push({role:"system",content:e.system});else if(Array.isArray(e.system)&&e.system.length){let s=e.system.filter(i=>i.type==="text"&&i.text).map(i=>({type:"text",text:i.text,cache_control:i.cache_control}));t.push({role:"system",content:s})}}let o=JSON.parse(JSON.stringify(e.messages||[]));o?.forEach(s=>{s.role==="system"&&t.push({role:"system",content:s.content})}),o?.forEach(s=>{if(s.role!=="system"&&(s.role==="user"||s.role==="assistant")){if(typeof s.content=="string"){t.push({role:s.role,content:s.content});return}if(Array.isArray(s.content)){if(s.role==="user"){let i=s.content.filter(l=>l.type==="tool_result"&&l.tool_use_id);i.length&&i.forEach(l=>{let a={role:"tool",content:typeof l.content=="string"?l.content:JSON.stringify(l.content),tool_call_id:l.tool_use_id,cache_control:l.cache_control};t.push(a)});let c=s.content.filter(l=>l.type==="text"&&l.text||l.type==="image"&&l.source);c.length&&t.push({role:"user",content:c.map(l=>l?.type==="image"?{type:"image_url",image_url:{url:l.source?.type==="base64"?Ts(l.source.data,l.source.media_type):l.source.url},media_type:l.source.media_type}:l)})}else if(s.role==="assistant"){let i={role:"assistant",content:""},c=s.content.filter(u=>u.type==="text"&&u.text);c.length&&(i.content=c.map(u=>u.text).join(`
49
+ `));let l=s.content.filter(u=>u.type==="tool_use"&&u.id);l.length&&(i.tool_calls=l.map(u=>({id:u.id,type:"function",function:{name:u.name,arguments:JSON.stringify(u.input||{})}})));let a=s.content.find(u=>u.type==="thinking");a&&(i.thinking={content:a.thinking,signature:a.signature}),t.push(i)}return}}});let r={messages:t,model:e.model,max_tokens:e.max_tokens,temperature:e.temperature,stream:e.stream,tools:e.tools?.length?this.convertAnthropicToolsToUnified(e.tools):void 0,tool_choice:e.tool_choice};return e.thinking&&(r.reasoning={effort:ks(e.thinking.budget_tokens),enabled:e.thinking.type==="enabled"}),e.tool_choice&&(e.tool_choice.type==="tool"?r.tool_choice={type:"function",function:{name:e.tool_choice.name}}:r.tool_choice=e.tool_choice.type),r}async transformRequestIn(e,t,o){if(!this.isAnthropicMessagesEndpoint(t.baseUrl))return e;o&&o.req&&(o.req.isTargetAnthropic=!0);let r=Cs(e),s={};return this.useBearer?(s.authorization=`Bearer ${t.apiKey}`,s["x-api-key"]=void 0):(s["x-api-key"]=t.apiKey,s.authorization=void 0),s["anthropic-version"]="2023-06-01",s["content-type"]="application/json",{body:r,config:{headers:s}}}isAnthropicMessagesEndpoint(e){try{return new URL(e.trim()).pathname.replace(/\/+$/,"").endsWith("/messages")}catch{return!1}}async transformResponseIn(e,t){if(t?.req?.isTargetAnthropic)return e;if(e.headers.get("Content-Type")?.includes("text/event-stream")){if(!e.body)throw new Error("Stream response body is null");let r=await this.convertOpenAIStreamToAnthropic(e.body,t);return new Response(r,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}else{let r=await Pe(e);if((r?.isSSE??!1)&&r?.body){let a=await this.convertOpenAIStreamToAnthropic(r.body,t);return new Response(a,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}let i;if(r?.body)i=new Response(r.body,{status:e.status,statusText:e.statusText,headers:e.headers});else{let a=await Ee(e);if(a.isSSE){let u=await this.convertOpenAIStreamToAnthropic(a.response.body,t);return new Response(u,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}i=a.response}let c=await F(i),l=this.convertOpenAIResponseToAnthropic(c,t);return new Response(JSON.stringify(l),{headers:{"Content-Type":"application/json"}})}}convertAnthropicToolsToUnified(e){return e.map(t=>({type:"function",function:{name:t.name,description:t.description||"",parameters:t.input_schema}}))}async convertOpenAIStreamToAnthropic(e,t){return new ReadableStream({start:async r=>{let s=new TextEncoder,i=`msg_${Date.now()}`,c=null,l="unknown",a=!1,u=!1,d=!1,f=new Map,g=new Map,y=0,m=0,p=0,_=!1,k=!1,h=0,b=-1,T=()=>{let v=h;return h++,v},x=v=>{if(!_)try{r.enqueue(v);let P=new TextDecoder().decode(v);this.logger.debug({reqId:t.req.id,data:P,type:"send data"})}catch(P){if(P instanceof TypeError&&P.message.includes("Controller is already closed"))_=!0;else throw this.logger.debug({reqId:t.req.id,error:P instanceof Error?P.message:String(P),type:"send data error"}),P}},C=()=>{if(!_)try{if(!a){this.logger?.warn("[empty_stream] Upstream produced no data, closing silently"),r.close(),_=!0;return}if(b>=0){let P={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
50
50
  data: ${JSON.stringify(P)}
51
51
 
52
52
  `)),b=-1}if(c)x(s.encode(`event: message_delta
@@ -65,7 +65,7 @@ data: ${JSON.stringify($)}
65
65
  `));continue}if(l=D.model||l,!a&&!_&&!d){a=!0;let $=t?.req?.tokenCount||0,L={type:"message_start",message:{id:i,type:"message",role:"assistant",content:[],model:l,stop_reason:null,stop_sequence:null,usage:{input_tokens:$,output_tokens:0}}};x(s.encode(`event: message_start
66
66
  data: ${JSON.stringify(L)}
67
67
 
68
- `))}let H=D.choices?.[0];if(D.usage){let $=D.usage?.prompt_tokens_details?.cached_tokens??D.usage?.prompt_cache_hit_tokens??0,W=(D.usage?.prompt_tokens||0)-$,ge=D.usage?.completion_tokens||0;this.logger?.info({debug_log:!0,reqId:t.req.id,phase:"anthropic_transformer_usage",providerUsage:D.usage,mappedUsage:{input_tokens:W,output_tokens:ge,cache_read_input_tokens:$},choicesLength:Array.isArray(D.choices)?D.choices.length:void 0,finishReason:H?.finish_reason});let j={input_tokens:W,output_tokens:ge,cache_read_input_tokens:$};if(!c)c={type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{...j}};else{let Y=c.usage||{};c.usage={input_tokens:j.input_tokens||Y.input_tokens||0,output_tokens:j.output_tokens||Y.output_tokens||0,cache_read_input_tokens:j.cache_read_input_tokens||Y.cache_read_input_tokens||0}}}if(!H)continue;if(H?.delta?.thinking&&!_&&!d&&!u){if(!k){let $=T(),L={type:"content_block_start",index:$,content_block:{type:"thinking",thinking:""}};x(s.encode(`event: content_block_start
68
+ `))}let H=D.choices?.[0];if(D.usage){let $=D.usage?.prompt_tokens_details?.cached_tokens??D.usage?.prompt_cache_hit_tokens??0,W=(D.usage?.prompt_tokens||0)-$,he=D.usage?.completion_tokens||0;this.logger?.info({debug_log:!0,reqId:t.req.id,phase:"anthropic_transformer_usage",providerUsage:D.usage,mappedUsage:{input_tokens:W,output_tokens:he,cache_read_input_tokens:$},choicesLength:Array.isArray(D.choices)?D.choices.length:void 0,finishReason:H?.finish_reason});let j={input_tokens:W,output_tokens:he,cache_read_input_tokens:$};if(!c)c={type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{...j}};else{let Y=c.usage||{};c.usage={input_tokens:j.input_tokens||Y.input_tokens||0,output_tokens:j.output_tokens||Y.output_tokens||0,cache_read_input_tokens:j.cache_read_input_tokens||Y.cache_read_input_tokens||0}}}if(!H)continue;if(H?.delta?.thinking&&!_&&!d&&!u){if(!k){let $=T(),L={type:"content_block_start",index:$,content_block:{type:"thinking",thinking:""}};x(s.encode(`event: content_block_start
69
69
  data: ${JSON.stringify(L)}
70
70
 
71
71
  `)),b=$,k=!0}if(H.delta.thinking.signature){let $={type:"content_block_delta",index:b,delta:{type:"signature_delta",signature:H.delta.thinking.signature}};x(s.encode(`event: content_block_delta
@@ -77,7 +77,7 @@ data: ${JSON.stringify(L)}
77
77
  `)),b=-1}else if(H.delta.thinking.content){let $={type:"content_block_delta",index:b,delta:{type:"thinking_delta",thinking:H.delta.thinking.content||""}};x(s.encode(`event: content_block_delta
78
78
  data: ${JSON.stringify($)}
79
79
 
80
- `))}}if(H?.delta?.content&&!_&&!d){if(p++,b>=0&&!u){let L={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
80
+ `))}}if(H?.delta?.content&&!_&&!d){if(m++,b>=0&&!u){let L={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
81
81
  data: ${JSON.stringify(L)}
82
82
 
83
83
  `)),b=-1}if(!u&&!d){u=!0;let $=T(),L={type:"content_block_start",index:$,content_block:{type:"text",text:""}};x(s.encode(`event: content_block_start
@@ -89,29 +89,29 @@ data: ${JSON.stringify($)}
89
89
  `))}}if(H?.delta?.annotations?.length&&!_&&!d){if(b>=0&&u){let $={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
90
90
  data: ${JSON.stringify($)}
91
91
 
92
- `)),b=-1,u=!1}H?.delta?.annotations.forEach($=>{let L=T(),W={type:"content_block_start",index:L,content_block:{type:"web_search_tool_result",tool_use_id:`srvtoolu_${po()}`,content:[{type:"web_search_result",title:$.url_citation.title,url:$.url_citation.url}]}};x(s.encode(`event: content_block_start
92
+ `)),b=-1,u=!1}H?.delta?.annotations.forEach($=>{let L=T(),W={type:"content_block_start",index:L,content_block:{type:"web_search_tool_result",tool_use_id:`srvtoolu_${mo()}`,content:[{type:"web_search_result",title:$.url_citation.title,url:$.url_citation.url}]}};x(s.encode(`event: content_block_start
93
93
  data: ${JSON.stringify(W)}
94
94
 
95
- `));let ge={type:"content_block_stop",index:L};x(s.encode(`event: content_block_stop
96
- data: ${JSON.stringify(ge)}
95
+ `));let he={type:"content_block_stop",index:L};x(s.encode(`event: content_block_stop
96
+ data: ${JSON.stringify(he)}
97
97
 
98
- `)),b=-1})}if(H?.delta?.tool_calls&&!_&&!d){g++;let $=new Set;for(let L of H.delta.tool_calls){if(_)break;let W=L.index??0;if($.has(W))continue;if($.add(W),!f.has(W)){if(b>=0){let aa={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
99
- data: ${JSON.stringify(aa)}
98
+ `)),b=-1})}if(H?.delta?.tool_calls&&!_&&!d){p++;let $=new Set;for(let L of H.delta.tool_calls){if(_)break;let W=L.index??0;if($.has(W))continue;if($.add(W),!g.has(W)){if(b>=0){let ua={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
99
+ data: ${JSON.stringify(ua)}
100
100
 
101
- `)),b=-1}let j=T();f.set(W,j);let Y=L.id||`call_ccr_${W}`,z=L.function?.name||`tool_${W}`,I={type:"content_block_start",index:j,content_block:{type:"tool_use",id:Y,name:z,input:{}}};x(s.encode(`event: content_block_start
101
+ `)),b=-1}let j=T();g.set(W,j);let Y=L.id||`call_ccr_${W}`,z=L.function?.name||`tool_${W}`,I={type:"content_block_start",index:j,content_block:{type:"tool_use",id:Y,name:z,input:{}}};x(s.encode(`event: content_block_start
102
102
  data: ${JSON.stringify(I)}
103
103
 
104
- `)),b=j;let J={id:Y,name:z,arguments:"",contentBlockIndex:j};m.set(W,J)}else if(L.id&&L.function?.name){let j=m.get(W);j.id.startsWith("call_")&&j.name.startsWith("tool_")&&(j.id=L.id,j.name=L.function.name)}if(L.function?.arguments&&!_&&!d){let j=f.get(W);if(j===void 0)continue;let Y=m.get(W);Y&&(Y.arguments+=L.function.arguments);try{let z={type:"content_block_delta",index:j,delta:{type:"input_json_delta",partial_json:L.function.arguments}};x(s.encode(`event: content_block_delta
104
+ `)),b=j;let B={id:Y,name:z,arguments:"",contentBlockIndex:j};f.set(W,B)}else if(L.id&&L.function?.name){let j=f.get(W);j.id.startsWith("call_")&&j.name.startsWith("tool_")&&(j.id=L.id,j.name=L.function.name)}if(L.function?.arguments&&!_&&!d){let j=g.get(W);if(j===void 0)continue;let Y=f.get(W);Y&&(Y.arguments+=L.function.arguments);try{let z={type:"content_block_delta",index:j,delta:{type:"input_json_delta",partial_json:L.function.arguments}};x(s.encode(`event: content_block_delta
105
105
  data: ${JSON.stringify(z)}
106
106
 
107
107
  `))}catch{try{let z=L.function.arguments.replace(/[\x00-\x1F\x7F-\x9F]/g,"").replace(/\\/g,"\\\\").replace(/"/g,'\\"'),I={type:"content_block_delta",index:j,delta:{type:"input_json_delta",partial_json:z}};x(s.encode(`event: content_block_delta
108
108
  data: ${JSON.stringify(I)}
109
109
 
110
- `))}catch(z){console.error(z)}}}}}if(H?.finish_reason&&!_&&!d){if(p===0&&g===0&&console.error("Warning: No content in the stream response!"),b>=0){let $={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
110
+ `))}catch(z){console.error(z)}}}}}if(H?.finish_reason&&!_&&!d){if(m===0&&p===0&&console.error("Warning: No content in the stream response!"),b>=0){let $={type:"content_block_stop",index:b};x(s.encode(`event: content_block_stop
111
111
  data: ${JSON.stringify($)}
112
112
 
113
- `)),b=-1}if(!_){let L={stop:"end_turn",length:"max_tokens",tool_calls:"tool_use",content_filter:"stop_sequence"}[H.finish_reason]||"end_turn";c?c.delta={...c.delta,stop_reason:L,stop_sequence:null}:c={type:"message_delta",delta:{stop_reason:L,stop_sequence:null},usage:{input_tokens:0,output_tokens:0,cache_read_input_tokens:0}}}d=!0}}catch(D){this.logger?.error(`parseError: ${D.name} message: ${D.message} stack: ${D.stack} data: ${N}`)}}}C()}catch(v){if(!_)try{r.error(v)}catch(P){console.error(P)}}finally{if(S)try{S.releaseLock()}catch(v){console.error(v)}}},cancel:r=>{this.logger.debug({reqId:t.req.id},`cancle stream: ${r}`)}})}convertOpenAIResponseToAnthropic(e,t){this.logger.debug({reqId:t.req.id,response:e},"Original OpenAI response");try{let o=e.choices[0];if(!o)throw new Error("No choices found in OpenAI response");let r=[];if(o.message.annotations){let l=`srvtoolu_${po()}`;r.push({type:"server_tool_use",id:l,name:"web_search",input:{query:""}}),r.push({type:"web_search_tool_result",tool_use_id:l,content:o.message.annotations.map(a=>({type:"web_search_result",url:a.url_citation.url,title:a.url_citation.title}))})}o.message.content&&r.push({type:"text",text:o.message.content}),o.message.tool_calls&&o.message.tool_calls.length>0&&o.message.tool_calls.forEach(l=>{let a={};try{let u=l.function.arguments||"{}";typeof u=="object"?a=u:typeof u=="string"&&(a=JSON.parse(u))}catch{a={text:l.function.arguments||""}}r.push({type:"tool_use",id:l.id,name:l.function.name,input:a})}),o.message?.thinking?.content&&r.push({type:"thinking",thinking:o.message.thinking.content,signature:o.message.thinking.signature});let s=e.usage?.prompt_tokens_details?.cached_tokens??e.usage?.prompt_cache_hit_tokens??0,i=(e.usage?.prompt_tokens||0)-s,c={id:e.id?.startsWith("msg_")?e.id:`msg_${po().replace(/-/g,"")}`,type:"message",role:"assistant",model:e.model,content:r,stop_reason:o.finish_reason==="stop"?"end_turn":o.finish_reason==="length"?"max_tokens":o.finish_reason==="tool_calls"?"tool_use":o.finish_reason==="content_filter"?"stop_sequence":"end_turn",stop_sequence:null,usage:{input_tokens:i,cache_creation_input_tokens:0,cache_read_input_tokens:s,output_tokens:e.usage?.completion_tokens||0,server_tool_use:{web_search_requests:0,web_fetch_requests:0},service_tier:"standard",cache_creation:{ephemeral_1h_input_tokens:0,ephemeral_5m_input_tokens:0},inference_geo:"",iterations:[],speed:"standard"},stop_details:null,context_management:null};return this.logger.debug({reqId:t.req.id,result:c},"Conversion complete, final Anthropic response"),c}catch{throw B(`Provider error: ${JSON.stringify(e)}`,500,"provider_error")}}}});function Mc(n,e){n.includes("null")&&(e.nullable=!0);let t=n.filter(o=>o!=="null");if(t.length===1){let o=t[0].toUpperCase();e.type=Object.values(Fe).includes(o)?o:Fe.TYPE_UNSPECIFIED}else{e.anyOf=[];for(let o of t){let r=o.toUpperCase();e.anyOf.push({type:Object.values(Fe).includes(r)?r:Fe.TYPE_UNSPECIFIED})}}}function st(n){let e={},t=["items"],o=["anyOf"],r=["properties"];if(n.type&&n.anyOf)throw new Error("type and anyOf cannot be both populated.");let s=n.anyOf;s!=null&&Array.isArray(s)&&s.length==2&&(s[0]&&s[0].type==="null"?(e.nullable=!0,n=s[1]):s[1]&&s[1].type==="null"&&(e.nullable=!0,n=s[0])),n.type&&Array.isArray(n.type)&&Mc(n.type,e);for(let[i,c]of Object.entries(n))if(c!=null)if(i=="type"){if(c==="null")throw new Error("type: null can not be the only possible type for the field.");if(Array.isArray(c))continue;let l=c.toUpperCase();e.type=Object.values(Fe).includes(l)?l:Fe.TYPE_UNSPECIFIED}else if(t.includes(i))e[i]=st(c);else if(o.includes(i)){let l=[];for(let a of c){if(a.type=="null"){e.nullable=!0;continue}l.push(st(a))}e[i]=l}else if(r.includes(i)){let l={};for(let[a,u]of Object.entries(c))l[a]=st(u);e[i]=l}else{if(i==="additionalProperties")continue;e[i]=c}return e}function $c(n){if(n.functionDeclarations)for(let e of n.functionDeclarations)e.parameters&&(Object.keys(e.parameters).includes("$schema")?e.parametersJsonSchema||(e.parametersJsonSchema=e.parameters,delete e.parameters):e.parameters=st(e.parameters)),e.response&&(Object.keys(e.response).includes("$schema")?e.responseJsonSchema||(e.responseJsonSchema=e.response,delete e.response):e.response=st(e.response));return n}function Ht(n){let e=[],t=n.tools?.filter(l=>l.function.name!=="web_search")?.map(l=>({name:l.function.name,description:l.function.description,parametersJsonSchema:l.function.parameters}));t?.length&&e.push($c({functionDeclarations:t})),n.tools?.find(l=>l.function.name==="web_search")&&e.push({googleSearch:{}});let r=[],s=n.messages.filter(l=>l.role==="tool");n.messages.filter(l=>l.role!=="tool").forEach(l=>{let a;l.role==="assistant"?a="model":(["user","system"].includes(l.role),a="user");let u=[];if(typeof l.content=="string"){let d={text:l.content};l.thinking?.signature&&(d.thoughtSignature=l.thinking.signature),u.push(d)}else Array.isArray(l.content)?u.push(...l.content.map(d=>{if(d.type==="text")return{text:d.text||""};if(d.type==="image_url")return d.image_url.url.startsWith("http")?{file_data:{mime_type:d.media_type,file_uri:d.image_url.url}}:{inlineData:{mime_type:d.media_type,data:d.image_url.url?.split(",")?.pop()||d.image_url.url}}})):l.content&&typeof l.content=="object"&&(l.content.text?u.push({text:l.content.text}):u.push({text:JSON.stringify(l.content)}));if(Array.isArray(l.tool_calls)&&u.push(...l.tool_calls.map(d=>{let m={functionCall:{id:d.id||`tool_${Math.random().toString(36).substring(2,15)}`,name:d.function.name,args:JSON.parse(d.function.arguments||"{}")}};return l.thinking?.signature&&(m.thoughtSignature=l.thinking.signature),m})),u.length===0&&u.push({text:""}),r.push({role:a,parts:u}),a==="model"&&l.tool_calls){let d=l.tool_calls.map(m=>{let f=s.find(y=>y.tool_call_id===m.id);return{functionResponse:{name:m?.function?.name,response:{result:f?.content}}}});r.push({role:"user",parts:d})}});let i={};if(n.reasoning&&n.reasoning.effort&&n.reasoning.effort!=="none")if(i.thinkingConfig={includeThoughts:!0},n.model.includes("gemini-3"))i.thinkingConfig.thinkingLevel=n.reasoning.effort;else{let l=n.model.includes("pro")?[128,32768]:[0,24576],a,u=n.reasoning.max_tokens;typeof u<"u"&&(u>=l[0]&&u<=l[1]?a=u:u<l[0]?a=l[0]:u>l[1]&&(a=l[1]),i.thinkingConfig.thinkingBudget=a)}let c={contents:r,tools:e.length?e:void 0,generationConfig:i};if(n.tool_choice){let l={functionCallingConfig:{}};n.tool_choice==="auto"?l.functionCallingConfig.mode="auto":n.tool_choice==="none"?l.functionCallingConfig.mode="none":n.tool_choice==="required"?l.functionCallingConfig.mode="any":n.tool_choice?.function?.name&&(l.functionCallingConfig.mode="any",l.functionCallingConfig.allowedFunctionNames=[n.tool_choice?.function?.name]),c.toolConfig=l}return c}function zt(n){let e=n.contents,t=n.tools,o=n.model,r=n.max_tokens,s=n.temperature,i=n.stream,c=n.tool_choice,l={messages:[],model:o,max_tokens:r,temperature:s,stream:i,tool_choice:c};return Array.isArray(e)&&e.forEach(a=>{typeof a=="string"?l.messages.push({role:"user",content:a}):typeof a.text=="string"?l.messages.push({role:"user",content:a.text||null}):a.role==="user"?l.messages.push({role:"user",content:a?.parts?.map(u=>({type:"text",text:u.text||""}))||[]}):a.role==="model"&&l.messages.push({role:"assistant",content:a?.parts?.map(u=>({type:"text",text:u.text||""}))||[]})}),Array.isArray(t)&&(l.tools=[],t.forEach(a=>{Array.isArray(a.functionDeclarations)&&a.functionDeclarations.forEach(u=>{l.tools.push({type:"function",function:{name:u.name,description:u.description,parameters:u.parameters}})})})),l}async function Jt(n,e,t){if(n.headers.get("Content-Type")?.includes("application/json")){let o=await n.json();t?.debug({response:o},`${e} response:`);let r="",s="",i=o.candidates[0]?.content?.parts||[],c=[];for(let d of i)d.text&&d.thought===!0?r+=d.text:c.push(d);s=i.find(d=>d.thoughtSignature)?.thoughtSignature;let l=c?.filter(d=>d.functionCall)?.map(d=>({id:d.functionCall?.id||`tool_${Math.random().toString(36).substring(2,15)}`,type:"function",function:{name:d.functionCall?.name,arguments:JSON.stringify(d.functionCall?.args||{})}}))||[],a=c?.filter(d=>d.text)?.map(d=>d.text)?.join(`
114
- `)||"",u={id:o.responseId,choices:[{finish_reason:o.candidates[0].finishReason?.toLowerCase()||null,index:0,message:{content:a,role:"assistant",tool_calls:l.length>0?l:void 0,...s&&{thinking:{content:r||"(no content)",signature:s}}}}],created:parseInt(new Date().getTime()/1e3+"",10),model:o.modelVersion,object:"chat.completion",usage:{completion_tokens:o.usageMetadata?.candidatesTokenCount||0,prompt_tokens:o.usageMetadata?.promptTokenCount||0,prompt_tokens_details:{cached_tokens:o.usageMetadata?.cachedContentTokenCount||0},total_tokens:o.usageMetadata?.totalTokenCount||0,output_tokens_details:{reasoning_tokens:o.usageMetadata?.thoughtsTokenCount||0}}};return new Response(JSON.stringify(u),{status:n.status,statusText:n.statusText,headers:n.headers})}else if(n.headers.get("Content-Type")?.includes("stream")){if(!n.body)return n;let o=new TextDecoder,r=new TextEncoder,s=!1,i=!1,c=!1,l="",a=0,u=-1,d=new ReadableStream({async start(m){let f=async(g,_)=>{if(g.startsWith("data: ")){let k=g.slice(6).trim();if(k){t?.debug({chunkStr:k},`${e} chunk:`);try{let h=JSON.parse(k);if(!h.candidates||!h.candidates[0]){t?.debug({chunkStr:k},"Invalid chunk structure");return}let b=h.candidates[0],T=b.content?.parts||[];T.filter(v=>v.text&&v.thought===!0).forEach(v=>{c||(c=!0);let P={choices:[{delta:{role:"assistant",content:null,thinking:{content:v.text}},finish_reason:null,index:a,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:h.responseId||"",model:h.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};_.enqueue(r.encode(`data: ${JSON.stringify(P)}
113
+ `)),b=-1}if(!_){let L={stop:"end_turn",length:"max_tokens",tool_calls:"tool_use",content_filter:"stop_sequence"}[H.finish_reason]||"end_turn";c?c.delta={...c.delta,stop_reason:L,stop_sequence:null}:c={type:"message_delta",delta:{stop_reason:L,stop_sequence:null},usage:{input_tokens:0,output_tokens:0,cache_read_input_tokens:0}}}d=!0}}catch(D){this.logger?.error(`parseError: ${D.name} message: ${D.message} stack: ${D.stack} data: ${N}`)}}}C()}catch(v){if(!_)try{r.error(v)}catch(P){console.error(P)}}finally{if(S)try{S.releaseLock()}catch(v){console.error(v)}}},cancel:r=>{this.logger.debug({reqId:t.req.id},`cancle stream: ${r}`)}})}convertOpenAIResponseToAnthropic(e,t){this.logger.debug({reqId:t.req.id,response:e},"Original OpenAI response");try{let o=e.choices[0];if(!o)throw new Error("No choices found in OpenAI response");let r=[];if(o.message.annotations){let l=`srvtoolu_${mo()}`;r.push({type:"server_tool_use",id:l,name:"web_search",input:{query:""}}),r.push({type:"web_search_tool_result",tool_use_id:l,content:o.message.annotations.map(a=>({type:"web_search_result",url:a.url_citation.url,title:a.url_citation.title}))})}o.message.content&&r.push({type:"text",text:o.message.content}),o.message.tool_calls&&o.message.tool_calls.length>0&&o.message.tool_calls.forEach(l=>{let a={};try{let u=l.function.arguments||"{}";typeof u=="object"?a=u:typeof u=="string"&&(a=JSON.parse(u))}catch{a={text:l.function.arguments||""}}r.push({type:"tool_use",id:l.id,name:l.function.name,input:a})}),o.message?.thinking?.content&&r.push({type:"thinking",thinking:o.message.thinking.content,signature:o.message.thinking.signature});let s=e.usage?.prompt_tokens_details?.cached_tokens??e.usage?.prompt_cache_hit_tokens??0,i=(e.usage?.prompt_tokens||0)-s,c={id:e.id?.startsWith("msg_")?e.id:`msg_${mo().replace(/-/g,"")}`,type:"message",role:"assistant",model:e.model,content:r,stop_reason:o.finish_reason==="stop"?"end_turn":o.finish_reason==="length"?"max_tokens":o.finish_reason==="tool_calls"?"tool_use":o.finish_reason==="content_filter"?"stop_sequence":"end_turn",stop_sequence:null,usage:{input_tokens:i,cache_creation_input_tokens:0,cache_read_input_tokens:s,output_tokens:e.usage?.completion_tokens||0,server_tool_use:{web_search_requests:0,web_fetch_requests:0},service_tier:"standard",cache_creation:{ephemeral_1h_input_tokens:0,ephemeral_5m_input_tokens:0},inference_geo:"",iterations:[],speed:"standard"},stop_details:null,context_management:null};return this.logger.debug({reqId:t.req.id,result:c},"Conversion complete, final Anthropic response"),c}catch{throw J(`Provider error: ${JSON.stringify(e)}`,500,"provider_error")}}}});function Lc(n,e){n.includes("null")&&(e.nullable=!0);let t=n.filter(o=>o!=="null");if(t.length===1){let o=t[0].toUpperCase();e.type=Object.values(Ue).includes(o)?o:Ue.TYPE_UNSPECIFIED}else{e.anyOf=[];for(let o of t){let r=o.toUpperCase();e.anyOf.push({type:Object.values(Ue).includes(r)?r:Ue.TYPE_UNSPECIFIED})}}}function st(n){let e={},t=["items"],o=["anyOf"],r=["properties"];if(n.type&&n.anyOf)throw new Error("type and anyOf cannot be both populated.");let s=n.anyOf;s!=null&&Array.isArray(s)&&s.length==2&&(s[0]&&s[0].type==="null"?(e.nullable=!0,n=s[1]):s[1]&&s[1].type==="null"&&(e.nullable=!0,n=s[0])),n.type&&Array.isArray(n.type)&&Lc(n.type,e);for(let[i,c]of Object.entries(n))if(c!=null)if(i=="type"){if(c==="null")throw new Error("type: null can not be the only possible type for the field.");if(Array.isArray(c))continue;let l=c.toUpperCase();e.type=Object.values(Ue).includes(l)?l:Ue.TYPE_UNSPECIFIED}else if(t.includes(i))e[i]=st(c);else if(o.includes(i)){let l=[];for(let a of c){if(a.type=="null"){e.nullable=!0;continue}l.push(st(a))}e[i]=l}else if(r.includes(i)){let l={};for(let[a,u]of Object.entries(c))l[a]=st(u);e[i]=l}else{if(i==="additionalProperties")continue;e[i]=c}return e}function Dc(n){if(n.functionDeclarations)for(let e of n.functionDeclarations)e.parameters&&(Object.keys(e.parameters).includes("$schema")?e.parametersJsonSchema||(e.parametersJsonSchema=e.parameters,delete e.parameters):e.parameters=st(e.parameters)),e.response&&(Object.keys(e.response).includes("$schema")?e.responseJsonSchema||(e.responseJsonSchema=e.response,delete e.response):e.response=st(e.response));return n}function Ht(n){let e=[],t=n.tools?.filter(l=>l.function.name!=="web_search")?.map(l=>({name:l.function.name,description:l.function.description,parametersJsonSchema:l.function.parameters}));t?.length&&e.push(Dc({functionDeclarations:t})),n.tools?.find(l=>l.function.name==="web_search")&&e.push({googleSearch:{}});let r=[],s=n.messages.filter(l=>l.role==="tool");n.messages.filter(l=>l.role!=="tool").forEach(l=>{let a;l.role==="assistant"?a="model":(["user","system"].includes(l.role),a="user");let u=[];if(typeof l.content=="string"){let d={text:l.content};l.thinking?.signature&&(d.thoughtSignature=l.thinking.signature),u.push(d)}else Array.isArray(l.content)?u.push(...l.content.map(d=>{if(d.type==="text")return{text:d.text||""};if(d.type==="image_url")return d.image_url.url.startsWith("http")?{file_data:{mime_type:d.media_type,file_uri:d.image_url.url}}:{inlineData:{mime_type:d.media_type,data:d.image_url.url?.split(",")?.pop()||d.image_url.url}}})):l.content&&typeof l.content=="object"&&(l.content.text?u.push({text:l.content.text}):u.push({text:JSON.stringify(l.content)}));if(Array.isArray(l.tool_calls)&&u.push(...l.tool_calls.map(d=>{let f={functionCall:{id:d.id||`tool_${Math.random().toString(36).substring(2,15)}`,name:d.function.name,args:JSON.parse(d.function.arguments||"{}")}};return l.thinking?.signature&&(f.thoughtSignature=l.thinking.signature),f})),u.length===0&&u.push({text:""}),r.push({role:a,parts:u}),a==="model"&&l.tool_calls){let d=l.tool_calls.map(f=>{let g=s.find(y=>y.tool_call_id===f.id);return{functionResponse:{name:f?.function?.name,response:{result:g?.content}}}});r.push({role:"user",parts:d})}});let i={};if(n.reasoning&&n.reasoning.effort&&n.reasoning.effort!=="none")if(i.thinkingConfig={includeThoughts:!0},n.model.includes("gemini-3"))i.thinkingConfig.thinkingLevel=n.reasoning.effort;else{let l=n.model.includes("pro")?[128,32768]:[0,24576],a,u=n.reasoning.max_tokens;typeof u<"u"&&(u>=l[0]&&u<=l[1]?a=u:u<l[0]?a=l[0]:u>l[1]&&(a=l[1]),i.thinkingConfig.thinkingBudget=a)}let c={contents:r,tools:e.length?e:void 0,generationConfig:i};if(n.tool_choice){let l={functionCallingConfig:{}};n.tool_choice==="auto"?l.functionCallingConfig.mode="auto":n.tool_choice==="none"?l.functionCallingConfig.mode="none":n.tool_choice==="required"?l.functionCallingConfig.mode="any":n.tool_choice?.function?.name&&(l.functionCallingConfig.mode="any",l.functionCallingConfig.allowedFunctionNames=[n.tool_choice?.function?.name]),c.toolConfig=l}return c}function zt(n){let e=n.contents,t=n.tools,o=n.model,r=n.max_tokens,s=n.temperature,i=n.stream,c=n.tool_choice,l={messages:[],model:o,max_tokens:r,temperature:s,stream:i,tool_choice:c};return Array.isArray(e)&&e.forEach(a=>{typeof a=="string"?l.messages.push({role:"user",content:a}):typeof a.text=="string"?l.messages.push({role:"user",content:a.text||null}):a.role==="user"?l.messages.push({role:"user",content:a?.parts?.map(u=>({type:"text",text:u.text||""}))||[]}):a.role==="model"&&l.messages.push({role:"assistant",content:a?.parts?.map(u=>({type:"text",text:u.text||""}))||[]})}),Array.isArray(t)&&(l.tools=[],t.forEach(a=>{Array.isArray(a.functionDeclarations)&&a.functionDeclarations.forEach(u=>{l.tools.push({type:"function",function:{name:u.name,description:u.description,parameters:u.parameters}})})})),l}async function Bt(n,e,t){if(n.headers.get("Content-Type")?.includes("application/json")){let o=await n.json();t?.debug({response:o},`${e} response:`);let r="",s="",i=o.candidates[0]?.content?.parts||[],c=[];for(let d of i)d.text&&d.thought===!0?r+=d.text:c.push(d);s=i.find(d=>d.thoughtSignature)?.thoughtSignature;let l=c?.filter(d=>d.functionCall)?.map(d=>({id:d.functionCall?.id||`tool_${Math.random().toString(36).substring(2,15)}`,type:"function",function:{name:d.functionCall?.name,arguments:JSON.stringify(d.functionCall?.args||{})}}))||[],a=c?.filter(d=>d.text)?.map(d=>d.text)?.join(`
114
+ `)||"",u={id:o.responseId,choices:[{finish_reason:o.candidates[0].finishReason?.toLowerCase()||null,index:0,message:{content:a,role:"assistant",tool_calls:l.length>0?l:void 0,...s&&{thinking:{content:r||"(no content)",signature:s}}}}],created:parseInt(new Date().getTime()/1e3+"",10),model:o.modelVersion,object:"chat.completion",usage:{completion_tokens:o.usageMetadata?.candidatesTokenCount||0,prompt_tokens:o.usageMetadata?.promptTokenCount||0,prompt_tokens_details:{cached_tokens:o.usageMetadata?.cachedContentTokenCount||0},total_tokens:o.usageMetadata?.totalTokenCount||0,output_tokens_details:{reasoning_tokens:o.usageMetadata?.thoughtsTokenCount||0}}};return new Response(JSON.stringify(u),{status:n.status,statusText:n.statusText,headers:n.headers})}else if(n.headers.get("Content-Type")?.includes("stream")){if(!n.body)return n;let o=new TextDecoder,r=new TextEncoder,s=!1,i=!1,c=!1,l="",a=0,u=-1,d=new ReadableStream({async start(f){let g=async(p,_)=>{if(p.startsWith("data: ")){let k=p.slice(6).trim();if(k){t?.debug({chunkStr:k},`${e} chunk:`);try{let h=JSON.parse(k);if(!h.candidates||!h.candidates[0]){t?.debug({chunkStr:k},"Invalid chunk structure");return}let b=h.candidates[0],T=b.content?.parts||[];T.filter(v=>v.text&&v.thought===!0).forEach(v=>{c||(c=!0);let P={choices:[{delta:{role:"assistant",content:null,thinking:{content:v.text}},finish_reason:null,index:a,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:h.responseId||"",model:h.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};_.enqueue(r.encode(`data: ${JSON.stringify(P)}
115
115
 
116
116
  `))});let x=T.find(v=>v.thoughtSignature)?.thoughtSignature;if(x&&!s){if(!c){let P={choices:[{delta:{role:"assistant",content:null,thinking:{content:"(no content)"}},finish_reason:null,index:a,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:h.responseId||"",model:h.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};_.enqueue(r.encode(`data: ${JSON.stringify(P)}
117
117
 
@@ -128,93 +128,93 @@ data: ${JSON.stringify($)}
128
128
 
129
129
  `)),!i&&S&&(i=!0)}C.length>0&&(C.forEach(v=>{a++,u++;let P={choices:[{delta:{role:"assistant",tool_calls:[{...v,index:u}]},finish_reason:b.finishReason?.toLowerCase()||null,index:a,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:h.responseId||"",model:h.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:h.usageMetadata?.candidatesTokenCount||0,prompt_tokens:h.usageMetadata?.promptTokenCount||0,prompt_tokens_details:{cached_tokens:h.usageMetadata?.cachedContentTokenCount||0},total_tokens:h.usageMetadata?.totalTokenCount||0,output_tokens_details:{reasoning_tokens:h.usageMetadata?.thoughtsTokenCount||0}}};b?.groundingMetadata?.groundingChunks?.length&&(P.choices[0].delta.annotations=b.groundingMetadata.groundingChunks.map((R,O)=>{let E=b?.groundingMetadata?.groundingSupports?.filter(A=>A.groundingChunkIndices?.includes(O));return{type:"url_citation",url_citation:{url:R?.web?.uri||"",title:R?.web?.title||"",content:E?.[0]?.segment?.text||"",start_index:E?.[0]?.segment?.startIndex||0,end_index:E?.[0]?.segment?.endIndex||0}}})),_.enqueue(r.encode(`data: ${JSON.stringify(P)}
130
130
 
131
- `))}),!i&&S&&(i=!0))}catch(h){t?.error(`Error parsing ${e} stream chunk`,k,h.message)}}}},y=n.body.getReader(),p="";try{for(;;){let{done:g,value:_}=await y.read();if(g){p&&await f(p,m);break}p+=o.decode(_,{stream:!0});let k=p.split(`
132
- `);p=k.pop()||"";for(let h of k)await f(h,m)}}catch(g){m.error(g)}finally{m.close()}}});return new Response(d,{status:n.status,statusText:n.statusText,headers:n.headers})}}var Fe,fo=w(()=>{"use strict";Fe={TYPE_UNSPECIFIED:"TYPE_UNSPECIFIED",STRING:"STRING",NUMBER:"NUMBER",INTEGER:"INTEGER",BOOLEAN:"BOOLEAN",ARRAY:"ARRAY",OBJECT:"OBJECT",NULL:"NULL"}});var Bt,xs=w(()=>{"use strict";fo();Bt=class{name="gemini";endPoint="/v1beta/models/:modelAndAction";async transformRequestIn(e,t){return{body:Ht(e),config:{url:new URL(`./${e.model}:${e.stream?"streamGenerateContent?alt=sse":"generateContent"}`,t.baseUrl),headers:{"x-goog-api-key":t.apiKey,Authorization:void 0}}}}transformRequestOut=zt;async transformResponseOut(e){return Jt(e,this.name,this.logger)}}});async function Nc(){try{let{GoogleAuth:n}=await import("google-auth-library");return(await(await new n({scopes:["https://www.googleapis.com/auth/cloud-platform"]}).getClient()).getAccessToken()).token||""}catch(n){throw console.error("Error getting access token:",n),new Error(`Failed to get access token for Vertex AI. Please ensure you have set up authentication using one of these methods:
131
+ `))}),!i&&S&&(i=!0))}catch(h){t?.error(`Error parsing ${e} stream chunk`,k,h.message)}}}},y=n.body.getReader(),m="";try{for(;;){let{done:p,value:_}=await y.read();if(p){m&&await g(m,f);break}m+=o.decode(_,{stream:!0});let k=m.split(`
132
+ `);m=k.pop()||"";for(let h of k)await g(h,f)}}catch(p){f.error(p)}finally{f.close()}}});return new Response(d,{status:n.status,statusText:n.statusText,headers:n.headers})}}var Ue,go=w(()=>{"use strict";Ue={TYPE_UNSPECIFIED:"TYPE_UNSPECIFIED",STRING:"STRING",NUMBER:"NUMBER",INTEGER:"INTEGER",BOOLEAN:"BOOLEAN",ARRAY:"ARRAY",OBJECT:"OBJECT",NULL:"NULL"}});var Jt,Rs=w(()=>{"use strict";go();Jt=class{name="gemini";endPoint="/v1beta/models/:modelAndAction";async transformRequestIn(e,t){return{body:Ht(e),config:{url:new URL(`./${e.model}:${e.stream?"streamGenerateContent?alt=sse":"generateContent"}`,t.baseUrl),headers:{"x-goog-api-key":t.apiKey,Authorization:void 0}}}}transformRequestOut=zt;async transformResponseOut(e){return Bt(e,this.name,this.logger)}}});async function Fc(){try{let{GoogleAuth:n}=await import("google-auth-library");return(await(await new n({scopes:["https://www.googleapis.com/auth/cloud-platform"]}).getClient()).getAccessToken()).token||""}catch(n){throw console.error("Error getting access token:",n),new Error(`Failed to get access token for Vertex AI. Please ensure you have set up authentication using one of these methods:
133
133
  1. Set GOOGLE_APPLICATION_CREDENTIALS to point to service account key file
134
134
  2. Run "gcloud auth application-default login"
135
- 3. Use Google Cloud environment with default service account`)}}var Wt,Ss=w(()=>{"use strict";fo();Wt=class{name="vertex-gemini";async transformRequestIn(e,t){let o=process.env.GOOGLE_CLOUD_PROJECT,r=process.env.GOOGLE_CLOUD_LOCATION||"us-central1";if(!o&&process.env.GOOGLE_APPLICATION_CREDENTIALS)try{let c=(await import("fs")).readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS,"utf8"),l=JSON.parse(c);l&&l.project_id&&(o=l.project_id)}catch(i){console.error("Error extracting project_id from GOOGLE_APPLICATION_CREDENTIALS:",i)}if(!o)throw new Error("Project ID is required for Vertex AI. Set GOOGLE_CLOUD_PROJECT environment variable or ensure project_id is in GOOGLE_APPLICATION_CREDENTIALS file.");let s=await Nc();return{body:Ht(e),config:{url:new URL(`./v1beta1/projects/${o}/locations/${r}/publishers/google/models/${e.model}:${e.stream?"streamGenerateContent":"generateContent"}`,t.baseUrl.endsWith("/")?t.baseUrl:t.baseUrl+"/"||`https://${r}-aiplatform.googleapis.com`),headers:{Authorization:`Bearer ${s}`,"x-goog-api-key":void 0}}}}async transformRequestOut(e){return zt(e)}async transformResponseOut(e){return Jt(e,this.name)}}});var Gt,Rs=w(()=>{"use strict";Q();Gt=class{name="deepseek";async transformRequestIn(e){e.max_tokens&&e.max_tokens>8192&&(e.max_tokens=8192);let t=e.model?.includes("reasoner")||e.model?.includes("v4")||e.model?.includes("pro")||e.model?.includes("think"),o=e.thinking||e.reasoning?.enabled||e.messages.some(r=>r.thinking?.content)||t;return o&&e.tool_choice&&delete e.tool_choice,e.messages.forEach(r=>{if(r.role==="assistant"){let s=r.thinking?.content,i=r.thinking?.signature,c="";if(typeof r.content=="string"){let a=/<thinking>([\s\S]*?)<\/thinking>/g.exec(r.content);a&&(c=a[1].trim(),r.content=r.content.replace(a[0],"").trim())}s&&typeof s=="string"&&s.trim()?(r.reasoning_content=s,i&&(r.reasoning_content_signature=i)):c&&(r.reasoning_content=c),o&&!r.reasoning_content&&(r.reasoning_content=" "),r.thinking&&delete r.thinking}}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r="",s=!1,i="",c=new ReadableStream({async start(l){let a=e.body.getReader(),u=(m,f,y)=>{let p=m.split(`
136
- `);for(let g of p)g.trim()&&f.enqueue(y.encode(g+`
137
- `))},d=(m,f)=>{let{controller:y,encoder:p}=f;if(m.startsWith("data: ")&&m.trim()!=="data: [DONE]")try{let g=JSON.parse(m.slice(6));if(g.choices?.[0]?.delta?.reasoning_content){f.appendReasoningContent(g.choices[0].delta.reasoning_content);let _={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,thinking:{content:g.choices[0].delta.reasoning_content}}}]};delete _.choices[0].delta.reasoning_content;let k=`data: ${JSON.stringify(_)}
135
+ 3. Use Google Cloud environment with default service account`)}}var Wt,ws=w(()=>{"use strict";go();Wt=class{name="vertex-gemini";async transformRequestIn(e,t){let o=process.env.GOOGLE_CLOUD_PROJECT,r=process.env.GOOGLE_CLOUD_LOCATION||"us-central1";if(!o&&process.env.GOOGLE_APPLICATION_CREDENTIALS)try{let c=(await import("fs")).readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS,"utf8"),l=JSON.parse(c);l&&l.project_id&&(o=l.project_id)}catch(i){console.error("Error extracting project_id from GOOGLE_APPLICATION_CREDENTIALS:",i)}if(!o)throw new Error("Project ID is required for Vertex AI. Set GOOGLE_CLOUD_PROJECT environment variable or ensure project_id is in GOOGLE_APPLICATION_CREDENTIALS file.");let s=await Fc();return{body:Ht(e),config:{url:new URL(`./v1beta1/projects/${o}/locations/${r}/publishers/google/models/${e.model}:${e.stream?"streamGenerateContent":"generateContent"}`,t.baseUrl.endsWith("/")?t.baseUrl:t.baseUrl+"/"||`https://${r}-aiplatform.googleapis.com`),headers:{Authorization:`Bearer ${s}`,"x-goog-api-key":void 0}}}}async transformRequestOut(e){return zt(e)}async transformResponseOut(e){return Bt(e,this.name)}}});var Gt,Ps=w(()=>{"use strict";Q();Gt=class{name="deepseek";async transformRequestIn(e){e.max_tokens&&e.max_tokens>8192&&(e.max_tokens=8192);let t=e.model?.includes("reasoner")||e.model?.includes("v4")||e.model?.includes("pro")||e.model?.includes("think"),o=e.thinking||e.reasoning?.enabled||e.messages.some(r=>r.thinking?.content)||t;return o&&e.tool_choice&&delete e.tool_choice,e.messages.forEach(r=>{if(r.role==="assistant"){let s=r.thinking?.content,i=r.thinking?.signature,c="";if(typeof r.content=="string"){let a=/<thinking>([\s\S]*?)<\/thinking>/g.exec(r.content);a&&(c=a[1].trim(),r.content=r.content.replace(a[0],"").trim())}s&&typeof s=="string"&&s.trim()?(r.reasoning_content=s,i&&(r.reasoning_content_signature=i)):c&&(r.reasoning_content=c),o&&!r.reasoning_content&&(r.reasoning_content=" "),r.thinking&&delete r.thinking}}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r="",s=!1,i="",c=new ReadableStream({async start(l){let a=e.body.getReader(),u=(f,g,y)=>{let m=f.split(`
136
+ `);for(let p of m)p.trim()&&g.enqueue(y.encode(p+`
137
+ `))},d=(f,g)=>{let{controller:y,encoder:m}=g;if(f.startsWith("data: ")&&f.trim()!=="data: [DONE]")try{let p=JSON.parse(f.slice(6));if(p.choices?.[0]?.delta?.reasoning_content){g.appendReasoningContent(p.choices[0].delta.reasoning_content);let _={...p,choices:[{...p.choices[0],delta:{...p.choices[0].delta,thinking:{content:p.choices[0].delta.reasoning_content}}}]};delete _.choices[0].delta.reasoning_content;let k=`data: ${JSON.stringify(_)}
138
138
 
139
- `;y.enqueue(p.encode(k));return}if(g.choices?.[0]?.delta?.content&&f.reasoningContent()&&!f.isReasoningComplete()){f.setReasoningComplete(!0);let _="ccr_think_signature",k=g.choices[0].delta.content,h={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,content:null,thinking:{content:f.reasoningContent(),signature:_}}}]};delete h.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(h)}
139
+ `;y.enqueue(m.encode(k));return}if(p.choices?.[0]?.delta?.content&&g.reasoningContent()&&!g.isReasoningComplete()){g.setReasoningComplete(!0);let _="ccr_think_signature",k=p.choices[0].delta.content,h={...p,choices:[{...p.choices[0],delta:{...p.choices[0].delta,content:null,thinking:{content:g.reasoningContent(),signature:_}}}]};delete h.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(h)}
140
140
 
141
- `;y.enqueue(p.encode(b));let T={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,content:k}}]};delete T.choices[0].delta.reasoning_content;let x=`data: ${JSON.stringify(T)}
141
+ `;y.enqueue(m.encode(b));let T={...p,choices:[{...p.choices[0],delta:{...p.choices[0].delta,content:k}}]};delete T.choices[0].delta.reasoning_content;let x=`data: ${JSON.stringify(T)}
142
142
 
143
- `;y.enqueue(p.encode(x));return}if(g.choices[0]?.delta?.reasoning_content&&delete g.choices[0].delta.reasoning_content,g.choices?.[0]?.delta&&Object.keys(g.choices[0].delta).length>0){let _=`data: ${JSON.stringify(g)}
143
+ `;y.enqueue(m.encode(x));return}if(p.choices[0]?.delta?.reasoning_content&&delete p.choices[0].delta.reasoning_content,p.choices?.[0]?.delta&&Object.keys(p.choices[0].delta).length>0){let _=`data: ${JSON.stringify(p)}
144
144
 
145
- `;y.enqueue(p.encode(_))}}catch{y.enqueue(p.encode(m+`
146
- `))}else y.enqueue(p.encode(m+`
147
- `))};try{for(;;){let{done:m,value:f}=await a.read();if(m){i.trim()&&u(i,l,o);break}let y=t.decode(f,{stream:!0});i+=y;let p=i.split(`
148
- `);i=p.pop()||"";for(let g of p)if(g.trim())try{d(g,{controller:l,encoder:o,reasoningContent:()=>r,appendReasoningContent:_=>r+=_,isReasoningComplete:()=>s,setReasoningComplete:_=>s=_})}catch(_){console.error("Error processing line:",g,_),l.enqueue(o.encode(g+`
149
- `))}}}catch(m){console.error("Stream error:",m),l.error(m)}finally{try{a.releaseLock()}catch(m){console.error("Error releasing reader lock:",m)}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":e.headers.get("Content-Type")||"text/plain","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var Kt,ws=w(()=>{"use strict";Q();Kt=class{name="tooluse";transformRequestIn(e){return e.messages.push({role:"system",content:"<system-reminder>Tool mode is active. The user expects you to proactively execute the most suitable tool to help complete the task. \nBefore invoking a tool, you must carefully evaluate whether it matches the current task. If no available tool is appropriate for the task, you MUST call the `ExitTool` to exit tool mode \u2014 this is the only valid way to terminate tool mode.\nAlways prioritize completing the user's task effectively and efficiently by using tools whenever appropriate.</system-reminder>"}),e.tools?.length&&(e.tool_choice="required",e.tools.push({type:"function",function:{name:"ExitTool",description:`Use this tool when you are in tool mode and have completed the task. This is the only valid way to exit tool mode.
145
+ `;y.enqueue(m.encode(_))}}catch{y.enqueue(m.encode(f+`
146
+ `))}else y.enqueue(m.encode(f+`
147
+ `))};try{for(;;){let{done:f,value:g}=await a.read();if(f){i.trim()&&u(i,l,o);break}let y=t.decode(g,{stream:!0});i+=y;let m=i.split(`
148
+ `);i=m.pop()||"";for(let p of m)if(p.trim())try{d(p,{controller:l,encoder:o,reasoningContent:()=>r,appendReasoningContent:_=>r+=_,isReasoningComplete:()=>s,setReasoningComplete:_=>s=_})}catch(_){console.error("Error processing line:",p,_),l.enqueue(o.encode(p+`
149
+ `))}}}catch(f){console.error("Stream error:",f),l.error(f)}finally{try{a.releaseLock()}catch(f){console.error("Error releasing reader lock:",f)}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":e.headers.get("Content-Type")||"text/plain","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var Kt,Es=w(()=>{"use strict";Q();Kt=class{name="tooluse";transformRequestIn(e){return e.messages.push({role:"system",content:"<system-reminder>Tool mode is active. The user expects you to proactively execute the most suitable tool to help complete the task. \nBefore invoking a tool, you must carefully evaluate whether it matches the current task. If no available tool is appropriate for the task, you MUST call the `ExitTool` to exit tool mode \u2014 this is the only valid way to terminate tool mode.\nAlways prioritize completing the user's task effectively and efficiently by using tools whenever appropriate.</system-reminder>"}),e.tools?.length&&(e.tool_choice="required",e.tools.push({type:"function",function:{name:"ExitTool",description:`Use this tool when you are in tool mode and have completed the task. This is the only valid way to exit tool mode.
150
150
  IMPORTANT: Before using this tool, ensure that none of the available tools are applicable to the current task. You must evaluate all available options \u2014 only if no suitable tool can help you complete the task should you use ExitTool to terminate tool mode.
151
151
  Examples:
152
152
  1. Task: "Use a tool to summarize this document" \u2014 Do not use ExitTool if a summarization tool is available.
153
- 2. Task: "What\u2019s the weather today?" \u2014 If no tool is available to answer, use ExitTool after reasoning that none can fulfill the task.`,parameters:{type:"object",properties:{response:{type:"string",description:"Your response will be forwarded to the user exactly as returned \u2014 the tool will not modify or post-process it in any way."}},required:["response"]}}})),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);if(t?.choices?.[0]?.message.tool_calls?.length&&t?.choices?.[0]?.message.tool_calls[0]?.function?.name==="ExitTool"){let o=t?.choices[0]?.message.tool_calls[0],r=JSON.parse(o.function.arguments||"{}");t.choices[0].message.content=r.response||"",delete t.choices[0].message.tool_calls}return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=-1,s="",i="",c=new ReadableStream({async start(l){let a=e.body.getReader(),u=(m,f,y)=>{let p=m.split(`
154
- `);for(let g of p)g.trim()&&f.enqueue(y.encode(g+`
155
- `))},d=(m,f)=>{let{controller:y,encoder:p,exitToolIndex:g,setExitToolIndex:_,appendExitToolResponse:k}=f;if(m.startsWith("data: ")&&m.trim()!=="data: [DONE]")try{let h=JSON.parse(m.slice(6));if(h.choices[0]?.delta?.tool_calls?.length){let b=h.choices[0].delta.tool_calls[0];if(b.function?.name==="ExitTool"){_(b.index);return}else if(g()>-1&&b.index===g()&&b.function.arguments){k(b.function.arguments);try{let T=JSON.parse(f.exitToolResponse());h.choices=[{delta:{role:"assistant",content:T.response||""}}];let x=`data: ${JSON.stringify(h)}
153
+ 2. Task: "What\u2019s the weather today?" \u2014 If no tool is available to answer, use ExitTool after reasoning that none can fulfill the task.`,parameters:{type:"object",properties:{response:{type:"string",description:"Your response will be forwarded to the user exactly as returned \u2014 the tool will not modify or post-process it in any way."}},required:["response"]}}})),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);if(t?.choices?.[0]?.message.tool_calls?.length&&t?.choices?.[0]?.message.tool_calls[0]?.function?.name==="ExitTool"){let o=t?.choices[0]?.message.tool_calls[0],r=JSON.parse(o.function.arguments||"{}");t.choices[0].message.content=r.response||"",delete t.choices[0].message.tool_calls}return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=-1,s="",i="",c=new ReadableStream({async start(l){let a=e.body.getReader(),u=(f,g,y)=>{let m=f.split(`
154
+ `);for(let p of m)p.trim()&&g.enqueue(y.encode(p+`
155
+ `))},d=(f,g)=>{let{controller:y,encoder:m,exitToolIndex:p,setExitToolIndex:_,appendExitToolResponse:k}=g;if(f.startsWith("data: ")&&f.trim()!=="data: [DONE]")try{let h=JSON.parse(f.slice(6));if(h.choices[0]?.delta?.tool_calls?.length){let b=h.choices[0].delta.tool_calls[0];if(b.function?.name==="ExitTool"){_(b.index);return}else if(p()>-1&&b.index===p()&&b.function.arguments){k(b.function.arguments);try{let T=JSON.parse(g.exitToolResponse());h.choices=[{delta:{role:"assistant",content:T.response||""}}];let x=`data: ${JSON.stringify(h)}
156
156
 
157
- `;y.enqueue(p.encode(x))}catch{}return}}if(h.choices?.[0]?.delta&&Object.keys(h.choices[0].delta).length>0){let b=`data: ${JSON.stringify(h)}
157
+ `;y.enqueue(m.encode(x))}catch{}return}}if(h.choices?.[0]?.delta&&Object.keys(h.choices[0].delta).length>0){let b=`data: ${JSON.stringify(h)}
158
158
 
159
- `;y.enqueue(p.encode(b))}}catch{y.enqueue(p.encode(m+`
160
- `))}else y.enqueue(p.encode(m+`
161
- `))};try{for(;;){let{done:m,value:f}=await a.read();if(m){i.trim()&&u(i,l,o);break}let y=t.decode(f,{stream:!0});i+=y;let p=i.split(`
162
- `);i=p.pop()||"";for(let g of p)if(g.trim())try{d(g,{controller:l,encoder:o,exitToolIndex:()=>r,setExitToolIndex:_=>r=_,exitToolResponse:()=>s,appendExitToolResponse:_=>s+=_})}catch(_){console.error("Error processing line:",g,_),l.enqueue(o.encode(g+`
163
- `))}}}catch(m){console.error("Stream error:",m),l.error(m)}finally{try{a.releaseLock()}catch(m){console.error("Error releasing reader lock:",m)}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});import{v4 as Lc}from"uuid";var Qt,Ps=w(()=>{"use strict";Q();Qt=class{constructor(e){this.options=e}static TransformerName="openrouter";async transformRequestIn(e){return e.model.includes("claude")?e.messages.forEach(t=>{Array.isArray(t.content)&&t.content.forEach(o=>{o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)})}):e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control,o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`${o.image_url.url}`),delete o.media_type)}):t.cache_control&&delete t.cache_control}),Object.assign(e,this.options||{}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,c=!1,l="",a=new ReadableStream({async start(u){let d=e.body.getReader(),m=(y,p,g)=>{let _=y.split(`
164
- `);for(let k of _)k.trim()&&p.enqueue(g.encode(k+`
165
- `))},f=(y,p)=>{let{controller:g,encoder:_}=p;if(y.startsWith("data: ")&&y.trim()!=="data: [DONE]"){let k=y.slice(6);try{let h=JSON.parse(k);if(h.usage&&(this.logger?.debug({usage:h.usage,hasToolCall:c},"usage"),h.choices[0].finish_reason=c?"tool_calls":"stop"),h.choices?.[0]?.finish_reason==="error"){g.enqueue(_.encode(`data: ${JSON.stringify({error:h.choices?.[0].error})}
159
+ `;y.enqueue(m.encode(b))}}catch{y.enqueue(m.encode(f+`
160
+ `))}else y.enqueue(m.encode(f+`
161
+ `))};try{for(;;){let{done:f,value:g}=await a.read();if(f){i.trim()&&u(i,l,o);break}let y=t.decode(g,{stream:!0});i+=y;let m=i.split(`
162
+ `);i=m.pop()||"";for(let p of m)if(p.trim())try{d(p,{controller:l,encoder:o,exitToolIndex:()=>r,setExitToolIndex:_=>r=_,exitToolResponse:()=>s,appendExitToolResponse:_=>s+=_})}catch(_){console.error("Error processing line:",p,_),l.enqueue(o.encode(p+`
163
+ `))}}}catch(f){console.error("Stream error:",f),l.error(f)}finally{try{a.releaseLock()}catch(f){console.error("Error releasing reader lock:",f)}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});import{v4 as Uc}from"uuid";var Qt,Os=w(()=>{"use strict";Q();Qt=class{constructor(e){this.options=e}static TransformerName="openrouter";async transformRequestIn(e){return e.model.includes("claude")?e.messages.forEach(t=>{Array.isArray(t.content)&&t.content.forEach(o=>{o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)})}):e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control,o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`${o.image_url.url}`),delete o.media_type)}):t.cache_control&&delete t.cache_control}),Object.assign(e,this.options||{}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,c=!1,l="",a=new ReadableStream({async start(u){let d=e.body.getReader(),f=(y,m,p)=>{let _=y.split(`
164
+ `);for(let k of _)k.trim()&&m.enqueue(p.encode(k+`
165
+ `))},g=(y,m)=>{let{controller:p,encoder:_}=m;if(y.startsWith("data: ")&&y.trim()!=="data: [DONE]"){let k=y.slice(6);try{let h=JSON.parse(k);if(h.usage&&(this.logger?.debug({usage:h.usage,hasToolCall:c},"usage"),h.choices[0].finish_reason=c?"tool_calls":"stop"),h.choices?.[0]?.finish_reason==="error"){p.enqueue(_.encode(`data: ${JSON.stringify({error:h.choices?.[0].error})}
166
166
 
167
- `));return}h.choices?.[0]?.delta?.content&&!p.hasTextContent()&&p.setHasTextContent(!0);let b=h.choices?.[0]?.delta?.reasoning||h.choices?.[0]?.delta?.reasoning_content;if(b){p.appendReasoningContent(b);let v={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,thinking:{content:b}}}]};v.choices?.[0]?.delta&&(delete v.choices[0].delta.reasoning,delete v.choices[0].delta.reasoning_content);let P=`data: ${JSON.stringify(v)}
167
+ `));return}h.choices?.[0]?.delta?.content&&!m.hasTextContent()&&m.setHasTextContent(!0);let b=h.choices?.[0]?.delta?.reasoning||h.choices?.[0]?.delta?.reasoning_content;if(b){m.appendReasoningContent(b);let v={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,thinking:{content:b}}}]};v.choices?.[0]?.delta&&(delete v.choices[0].delta.reasoning,delete v.choices[0].delta.reasoning_content);let P=`data: ${JSON.stringify(v)}
168
168
 
169
- `;g.enqueue(_.encode(P));return}if(h.choices?.[0]?.delta?.content&&p.reasoningContent()&&!p.isReasoningComplete()){p.setReasoningComplete(!0);let v="ccr_think_signature",P=h.choices[0].delta.content,R={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:null,thinking:{content:p.reasoningContent(),signature:v}}}]};R.choices?.[0]?.delta&&delete R.choices[0].delta.reasoning;let O=`data: ${JSON.stringify(R)}
169
+ `;p.enqueue(_.encode(P));return}if(h.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let v="ccr_think_signature",P=h.choices[0].delta.content,R={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:v}}}]};R.choices?.[0]?.delta&&delete R.choices[0].delta.reasoning;let O=`data: ${JSON.stringify(R)}
170
170
 
171
- `;g.enqueue(_.encode(O));let E={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:P}}]};E.choices?.[0]?.delta&&(delete E.choices[0].delta.reasoning,delete E.choices[0].delta.reasoning_content);let A=`data: ${JSON.stringify(E)}
171
+ `;p.enqueue(_.encode(O));let E={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:P}}]};E.choices?.[0]?.delta&&(delete E.choices[0].delta.reasoning,delete E.choices[0].delta.reasoning_content);let A=`data: ${JSON.stringify(E)}
172
172
 
173
- `;g.enqueue(_.encode(A));return}h.choices?.[0]?.delta?.reasoning&&delete h.choices[0].delta.reasoning,h.choices?.[0]?.delta?.reasoning_content&&delete h.choices[0].delta.reasoning_content,h.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(h.choices?.[0]?.delta?.tool_calls[0].id,10))&&h.choices?.[0]?.delta?.tool_calls.forEach(v=>{v.id=`call_${Lc()}`}),h.choices?.[0]?.delta?.tool_calls?.length&&!c&&(c=!0);let T=h.choices?.[0]?.delta,x=T&&Object.keys(T).length>0,C=h.choices?.[0]?.finish_reason,S=h.usage;if(x||C||S){let v=`data: ${JSON.stringify(h)}
173
+ `;p.enqueue(_.encode(A));return}h.choices?.[0]?.delta?.reasoning&&delete h.choices[0].delta.reasoning,h.choices?.[0]?.delta?.reasoning_content&&delete h.choices[0].delta.reasoning_content,h.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(h.choices?.[0]?.delta?.tool_calls[0].id,10))&&h.choices?.[0]?.delta?.tool_calls.forEach(v=>{v.id=`call_${Uc()}`}),h.choices?.[0]?.delta?.tool_calls?.length&&!c&&(c=!0);let T=h.choices?.[0]?.delta,x=T&&Object.keys(T).length>0,C=h.choices?.[0]?.finish_reason,S=h.usage;if(x||C||S){let v=`data: ${JSON.stringify(h)}
174
174
 
175
- `;g.enqueue(_.encode(v))}}catch{g.enqueue(_.encode(y+`
176
- `))}}else g.enqueue(_.encode(y+`
177
- `))};try{for(;;){let{done:y,value:p}=await d.read();if(y){l.trim()&&m(l,u,o);break}if(!p||p.length===0)continue;let g;try{g=t.decode(p,{stream:!0})}catch(k){console.warn("Failed to decode chunk",k);continue}if(g.length===0)continue;if(l+=g,l.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let k=l.split(`
178
- `);l=k.pop()||"";for(let h of k)if(h.trim())try{f(h,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:b=>r=b,reasoningContent:()=>s,appendReasoningContent:b=>s+=b,isReasoningComplete:()=>i,setReasoningComplete:b=>i=b})}catch(b){console.error("Error processing line:",h,b),u.enqueue(o.encode(h+`
175
+ `;p.enqueue(_.encode(v))}}catch{p.enqueue(_.encode(y+`
176
+ `))}}else p.enqueue(_.encode(y+`
177
+ `))};try{for(;;){let{done:y,value:m}=await d.read();if(y){l.trim()&&f(l,u,o);break}if(!m||m.length===0)continue;let p;try{p=t.decode(m,{stream:!0})}catch(k){console.warn("Failed to decode chunk",k);continue}if(p.length===0)continue;if(l+=p,l.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let k=l.split(`
178
+ `);l=k.pop()||"";for(let h of k)if(h.trim())try{g(h,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:b=>r=b,reasoningContent:()=>s,appendReasoningContent:b=>s+=b,isReasoningComplete:()=>i,setReasoningComplete:b=>i=b})}catch(b){console.error("Error processing line:",h,b),u.enqueue(o.encode(h+`
179
179
  `))}continue}let _=l.split(`
180
- `);l=_.pop()||"";for(let k of _)if(k.trim())try{f(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:h=>r=h,reasoningContent:()=>s,appendReasoningContent:h=>s+=h,isReasoningComplete:()=>i,setReasoningComplete:h=>i=h})}catch(h){console.error("Error processing line:",k,h),u.enqueue(o.encode(k+`
181
- `))}}}catch(y){console.error("Stream error:",y),u.error(y)}finally{try{d.releaseLock()}catch(y){console.error("Error releasing reader lock:",y)}u.close()}}});return new Response(a,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var Vt,Es=w(()=>{"use strict";Vt=class{constructor(e){this.options=e;this.max_tokens=this.options?.max_tokens}static TransformerName="maxtoken";max_tokens;async transformRequestIn(e){return e.max_tokens&&e.max_tokens>this.max_tokens&&(e.max_tokens=this.max_tokens),e}}});import{v4 as Dc}from"uuid";var Yt,Os=w(()=>{"use strict";Q();Yt=class{name="groq";async transformRequestIn(e){return e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control}):t.cache_control&&delete t.cache_control}),Array.isArray(e.tools)&&e.tools.forEach(t=>{delete t.function.parameters.$schema}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,c="",l=new ReadableStream({async start(a){let u=e.body.getReader(),d=(f,y,p)=>{let g=f.split(`
182
- `);for(let _ of g)_.trim()&&y.enqueue(p.encode(_+`
183
- `))},m=(f,y)=>{let{controller:p,encoder:g}=y;if(f.startsWith("data: ")&&f.trim()!=="data: [DONE]"){let _=f.slice(6);try{let k=JSON.parse(_);if(k.error)throw new Error(JSON.stringify(k));k.choices?.[0]?.delta?.content&&!y.hasTextContent()&&y.setHasTextContent(!0),k.choices?.[0]?.delta?.tool_calls?.length&&k.choices?.[0]?.delta?.tool_calls.forEach(b=>{b.id=`call_${Dc()}`}),k.choices?.[0]?.delta?.tool_calls?.length&&y.hasTextContent()&&(typeof k.choices[0].index=="number"?k.choices[0].index+=1:k.choices[0].index=1);let h=`data: ${JSON.stringify(k)}
184
-
185
- `;p.enqueue(g.encode(h))}catch{p.enqueue(g.encode(f+`
186
- `))}}else p.enqueue(g.encode(f+`
187
- `))};try{for(;;){let{done:f,value:y}=await u.read();if(f){c.trim()&&d(c,a,o);break}if(!y||y.length===0)continue;let p;try{p=t.decode(y,{stream:!0})}catch(_){console.warn("Failed to decode chunk",_);continue}if(p.length===0)continue;if(c+=p,c.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let _=c.split(`
188
- `);c=_.pop()||"";for(let k of _)if(k.trim())try{m(k,{controller:a,encoder:o,hasTextContent:()=>r,setHasTextContent:h=>r=h,reasoningContent:()=>s,appendReasoningContent:h=>s+=h,isReasoningComplete:()=>i,setReasoningComplete:h=>i=h})}catch(h){console.error("Error processing line:",k,h),a.enqueue(o.encode(k+`
189
- `))}continue}let g=c.split(`
190
- `);c=g.pop()||"";for(let _ of g)if(_.trim())try{m(_,{controller:a,encoder:o,hasTextContent:()=>r,setHasTextContent:k=>r=k,reasoningContent:()=>s,appendReasoningContent:k=>s+=k,isReasoningComplete:()=>i,setReasoningComplete:k=>i=k})}catch(k){console.error("Error processing line:",_,k),a.enqueue(o.encode(_+`
191
- `))}}}catch(f){console.error("Stream error:",f),a.error(f)}finally{try{u.releaseLock()}catch(f){console.error("Error releasing reader lock:",f)}a.close()}}});return new Response(l,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var Xt,As=w(()=>{"use strict";Xt=class{name="cleancache";async transformRequestIn(e){return Array.isArray(e.messages)&&e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control}):t.cache_control&&delete t.cache_control}),e}}});import Fc from"json5";import{jsonrepair as Uc}from"jsonrepair";function mo(n,e){if(!n||n.trim()===""||n==="{}")return"{}";try{return JSON.parse(n),e?.debug("\u5DE5\u5177\u8C03\u7528\u53C2\u6570\u6807\u51C6JSON\u89E3\u6790\u6210\u529F / Tool arguments standard JSON parsing successful"),n}catch(t){try{let o=Fc.parse(n);return e?.debug("Tool arguments JSON5 parsing successful"),JSON.stringify(o)}catch(o){try{let r=Uc(n);return e?.debug("Tool arguments safely repaired"),r}catch(r){return e?.error(`JSON parsing failed: ${t.message}. JSON5 parsing failed: ${o.message}. JSON repair failed: ${r.message}. Input data: ${JSON.stringify(n)}`),e?.debug("Returning safe empty object as fallback"),"{}"}}}}var Is=w(()=>{});var qt,Ms=w(()=>{"use strict";Is();Q();qt=class{name="enhancetool";async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);if(t?.choices?.[0]?.message?.tool_calls?.length)for(let o of t.choices[0].message.tool_calls)o.function?.arguments&&(o.function.arguments=mo(o.function.arguments,this.logger));return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r={},s=!1,i="",c=!1,l=!1,a="",u=new ReadableStream({async start(d){let m=e.body.getReader(),f=(g,_,k)=>{let h=g.split(`
180
+ `);l=_.pop()||"";for(let k of _)if(k.trim())try{g(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:h=>r=h,reasoningContent:()=>s,appendReasoningContent:h=>s+=h,isReasoningComplete:()=>i,setReasoningComplete:h=>i=h})}catch(h){console.error("Error processing line:",k,h),u.enqueue(o.encode(k+`
181
+ `))}}}catch(y){console.error("Stream error:",y),u.error(y)}finally{try{d.releaseLock()}catch(y){console.error("Error releasing reader lock:",y)}u.close()}}});return new Response(a,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var Vt,As=w(()=>{"use strict";Vt=class{constructor(e){this.options=e;this.max_tokens=this.options?.max_tokens}static TransformerName="maxtoken";max_tokens;async transformRequestIn(e){return e.max_tokens&&e.max_tokens>this.max_tokens&&(e.max_tokens=this.max_tokens),e}}});import{v4 as jc}from"uuid";var Yt,Is=w(()=>{"use strict";Q();Yt=class{name="groq";async transformRequestIn(e){return e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control}):t.cache_control&&delete t.cache_control}),Array.isArray(e.tools)&&e.tools.forEach(t=>{delete t.function.parameters.$schema}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,c="",l=new ReadableStream({async start(a){let u=e.body.getReader(),d=(g,y,m)=>{let p=g.split(`
182
+ `);for(let _ of p)_.trim()&&y.enqueue(m.encode(_+`
183
+ `))},f=(g,y)=>{let{controller:m,encoder:p}=y;if(g.startsWith("data: ")&&g.trim()!=="data: [DONE]"){let _=g.slice(6);try{let k=JSON.parse(_);if(k.error)throw new Error(JSON.stringify(k));k.choices?.[0]?.delta?.content&&!y.hasTextContent()&&y.setHasTextContent(!0),k.choices?.[0]?.delta?.tool_calls?.length&&k.choices?.[0]?.delta?.tool_calls.forEach(b=>{b.id=`call_${jc()}`}),k.choices?.[0]?.delta?.tool_calls?.length&&y.hasTextContent()&&(typeof k.choices[0].index=="number"?k.choices[0].index+=1:k.choices[0].index=1);let h=`data: ${JSON.stringify(k)}
184
+
185
+ `;m.enqueue(p.encode(h))}catch{m.enqueue(p.encode(g+`
186
+ `))}}else m.enqueue(p.encode(g+`
187
+ `))};try{for(;;){let{done:g,value:y}=await u.read();if(g){c.trim()&&d(c,a,o);break}if(!y||y.length===0)continue;let m;try{m=t.decode(y,{stream:!0})}catch(_){console.warn("Failed to decode chunk",_);continue}if(m.length===0)continue;if(c+=m,c.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let _=c.split(`
188
+ `);c=_.pop()||"";for(let k of _)if(k.trim())try{f(k,{controller:a,encoder:o,hasTextContent:()=>r,setHasTextContent:h=>r=h,reasoningContent:()=>s,appendReasoningContent:h=>s+=h,isReasoningComplete:()=>i,setReasoningComplete:h=>i=h})}catch(h){console.error("Error processing line:",k,h),a.enqueue(o.encode(k+`
189
+ `))}continue}let p=c.split(`
190
+ `);c=p.pop()||"";for(let _ of p)if(_.trim())try{f(_,{controller:a,encoder:o,hasTextContent:()=>r,setHasTextContent:k=>r=k,reasoningContent:()=>s,appendReasoningContent:k=>s+=k,isReasoningComplete:()=>i,setReasoningComplete:k=>i=k})}catch(k){console.error("Error processing line:",_,k),a.enqueue(o.encode(_+`
191
+ `))}}}catch(g){console.error("Stream error:",g),a.error(g)}finally{try{u.releaseLock()}catch(g){console.error("Error releasing reader lock:",g)}a.close()}}});return new Response(l,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var Xt,Ms=w(()=>{"use strict";Xt=class{name="cleancache";async transformRequestIn(e){return Array.isArray(e.messages)&&e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control}):t.cache_control&&delete t.cache_control}),e}}});import Hc from"json5";import{jsonrepair as zc}from"jsonrepair";function ho(n,e){if(!n||n.trim()===""||n==="{}")return"{}";try{return JSON.parse(n),e?.debug("\u5DE5\u5177\u8C03\u7528\u53C2\u6570\u6807\u51C6JSON\u89E3\u6790\u6210\u529F / Tool arguments standard JSON parsing successful"),n}catch(t){try{let o=Hc.parse(n);return e?.debug("Tool arguments JSON5 parsing successful"),JSON.stringify(o)}catch(o){try{let r=zc(n);return e?.debug("Tool arguments safely repaired"),r}catch(r){return e?.error(`JSON parsing failed: ${t.message}. JSON5 parsing failed: ${o.message}. JSON repair failed: ${r.message}. Input data: ${JSON.stringify(n)}`),e?.debug("Returning safe empty object as fallback"),"{}"}}}}var $s=w(()=>{});var Zt,Ns=w(()=>{"use strict";$s();Q();Zt=class{name="enhancetool";async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);if(t?.choices?.[0]?.message?.tool_calls?.length)for(let o of t.choices[0].message.tool_calls)o.function?.arguments&&(o.function.arguments=ho(o.function.arguments,this.logger));return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r={},s=!1,i="",c=!1,l=!1,a="",u=new ReadableStream({async start(d){let f=e.body.getReader(),g=(p,_,k)=>{let h=p.split(`
192
192
  `);for(let b of h)b.trim()&&_.enqueue(k.encode(b+`
193
- `))},y=(g,_,k)=>{let h="";try{h=mo(r.arguments||"",this.logger)}catch(C){console.error(`${C.message} ${C.stack} \u5DE5\u5177\u8C03\u7528\u53C2\u6570\u89E3\u6790\u5931\u8D25: ${JSON.stringify(r)}`),h=r.arguments||""}let b={role:"assistant",tool_calls:[{function:{name:r.name,arguments:h},id:r.id,index:r.index,type:"function"}]},T={...g,choices:[{...g.choices[0],delta:b}]};T.choices[0].delta.content!==void 0&&delete T.choices[0].delta.content;let x=`data: ${JSON.stringify(T)}
193
+ `))},y=(p,_,k)=>{let h="";try{h=ho(r.arguments||"",this.logger)}catch(C){console.error(`${C.message} ${C.stack} \u5DE5\u5177\u8C03\u7528\u53C2\u6570\u89E3\u6790\u5931\u8D25: ${JSON.stringify(r)}`),h=r.arguments||""}let b={role:"assistant",tool_calls:[{function:{name:r.name,arguments:h},id:r.id,index:r.index,type:"function"}]},T={...p,choices:[{...p.choices[0],delta:b}]};T.choices[0].delta.content!==void 0&&delete T.choices[0].delta.content;let x=`data: ${JSON.stringify(T)}
194
194
 
195
- `;_.enqueue(k.encode(x))},p=(g,_)=>{let{controller:k,encoder:h}=_;if(g.startsWith("data: ")&&g.trim()!=="data: [DONE]"){let b=g.slice(6);try{let T=JSON.parse(b);if(T.choices?.[0]?.delta?.tool_calls?.length){let C=T.choices[0].delta.tool_calls[0];if(typeof r.index>"u"){r={index:C.index,name:C.function?.name||"",id:C.id||"",arguments:C.function?.arguments||""},C.function?.arguments&&(C.function.arguments="");let S=`data: ${JSON.stringify(T)}
195
+ `;_.enqueue(k.encode(x))},m=(p,_)=>{let{controller:k,encoder:h}=_;if(p.startsWith("data: ")&&p.trim()!=="data: [DONE]"){let b=p.slice(6);try{let T=JSON.parse(b);if(T.choices?.[0]?.delta?.tool_calls?.length){let C=T.choices[0].delta.tool_calls[0];if(typeof r.index>"u"){r={index:C.index,name:C.function?.name||"",id:C.id||"",arguments:C.function?.arguments||""},C.function?.arguments&&(C.function.arguments="");let S=`data: ${JSON.stringify(T)}
196
196
 
197
197
  `;k.enqueue(h.encode(S));return}else if(r.index===C.index){C.function?.arguments&&(r.arguments+=C.function.arguments);return}else{y(T,k,h),r={index:C.index,name:C.function?.name||"",id:C.id||"",arguments:C.function?.arguments||""};return}}if(T.choices?.[0]?.finish_reason==="tool_calls"&&r.index!==void 0){y(T,k,h),r={};return}T.choices?.[0]?.delta?.tool_calls?.length&&_.hasTextContent()&&(typeof T.choices[0].index=="number"?T.choices[0].index+=1:T.choices[0].index=1);let x=`data: ${JSON.stringify(T)}
198
198
 
199
- `;k.enqueue(h.encode(x))}catch{k.enqueue(h.encode(g+`
200
- `))}}else k.enqueue(h.encode(g+`
201
- `))};try{for(;;){let{done:g,value:_}=await m.read();if(g){a.trim()&&f(a,d,o);break}if(!_||_.length===0)continue;let k;try{k=t.decode(_,{stream:!0})}catch(b){console.warn("Failed to decode chunk",b);continue}if(k.length===0)continue;if(a+=k,a.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let b=a.split(`
202
- `);a=b.pop()||"";for(let T of b)if(T.trim())try{p(T,{controller:d,encoder:o,hasTextContent:()=>s,setHasTextContent:x=>s=x,reasoningContent:()=>i,appendReasoningContent:x=>i+=x,isReasoningComplete:()=>c,setReasoningComplete:x=>c=x})}catch(x){console.error("Error processing line:",T,x),d.enqueue(o.encode(T+`
199
+ `;k.enqueue(h.encode(x))}catch{k.enqueue(h.encode(p+`
200
+ `))}}else k.enqueue(h.encode(p+`
201
+ `))};try{for(;;){let{done:p,value:_}=await f.read();if(p){a.trim()&&g(a,d,o);break}if(!_||_.length===0)continue;let k;try{k=t.decode(_,{stream:!0})}catch(b){console.warn("Failed to decode chunk",b);continue}if(k.length===0)continue;if(a+=k,a.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let b=a.split(`
202
+ `);a=b.pop()||"";for(let T of b)if(T.trim())try{m(T,{controller:d,encoder:o,hasTextContent:()=>s,setHasTextContent:x=>s=x,reasoningContent:()=>i,appendReasoningContent:x=>i+=x,isReasoningComplete:()=>c,setReasoningComplete:x=>c=x})}catch(x){console.error("Error processing line:",T,x),d.enqueue(o.encode(T+`
203
203
  `))}continue}let h=a.split(`
204
- `);a=h.pop()||"";for(let b of h)if(b.trim())try{p(b,{controller:d,encoder:o,hasTextContent:()=>s,setHasTextContent:T=>s=T,reasoningContent:()=>i,appendReasoningContent:T=>i+=T,isReasoningComplete:()=>c,setReasoningComplete:T=>c=T})}catch(T){console.error("Error processing line:",b,T),d.enqueue(o.encode(b+`
205
- `))}}}catch(g){console.error("Stream error:",g),d.error(g)}finally{try{m.releaseLock()}catch(g){console.error("Error releasing reader lock:",g)}d.close()}}});return new Response(u,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var Zt,$s=w(()=>{"use strict";Q();Zt=class{constructor(e){this.options=e;this.enable=this.options?.enable??!0}static TransformerName="reasoning";enable;async transformRequestIn(e){return this.enable?(e.reasoning&&(e.thinking={type:"enabled",budget_tokens:e.reasoning.max_tokens},e.enable_thinking=!0),e):(e.thinking={type:"disabled",budget_tokens:-1},e.enable_thinking=!1,e)}async transformResponseOut(e){if(!this.enable)return e;if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return t.choices[0]?.message.reasoning_content&&(t.thinking={content:t.choices[0]?.message.reasoning_content}),new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r="",s=!1,i="",c=new ReadableStream({async start(l){let a=e.body.getReader(),u=(m,f,y)=>{let p=m.split(`
206
- `);for(let g of p)g.trim()&&f.enqueue(y.encode(g+`
207
- `))},d=(m,f)=>{let{controller:y,encoder:p}=f;if(this.logger?.debug({line:m},"Processing reason line"),m.startsWith("data: ")&&m.trim()!=="data: [DONE]")try{let g=JSON.parse(m.slice(6));if(console.log(JSON.stringify(g)),g.choices?.[0]?.delta?.reasoning_content){f.appendReasoningContent(g.choices[0].delta.reasoning_content);let _={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,thinking:{content:g.choices[0].delta.reasoning_content}}}]};delete _.choices[0].delta.reasoning_content;let k=`data: ${JSON.stringify(_)}
204
+ `);a=h.pop()||"";for(let b of h)if(b.trim())try{m(b,{controller:d,encoder:o,hasTextContent:()=>s,setHasTextContent:T=>s=T,reasoningContent:()=>i,appendReasoningContent:T=>i+=T,isReasoningComplete:()=>c,setReasoningComplete:T=>c=T})}catch(T){console.error("Error processing line:",b,T),d.enqueue(o.encode(b+`
205
+ `))}}}catch(p){console.error("Stream error:",p),d.error(p)}finally{try{f.releaseLock()}catch(p){console.error("Error releasing reader lock:",p)}d.close()}}});return new Response(u,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var qt,Ls=w(()=>{"use strict";Q();qt=class{constructor(e){this.options=e;this.enable=this.options?.enable??!0}static TransformerName="reasoning";enable;async transformRequestIn(e){return this.enable?(e.reasoning&&(e.thinking={type:"enabled",budget_tokens:e.reasoning.max_tokens},e.enable_thinking=!0),e):(e.thinking={type:"disabled",budget_tokens:-1},e.enable_thinking=!1,e)}async transformResponseOut(e){if(!this.enable)return e;if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return t.choices[0]?.message.reasoning_content&&(t.thinking={content:t.choices[0]?.message.reasoning_content}),new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r="",s=!1,i="",c=new ReadableStream({async start(l){let a=e.body.getReader(),u=(f,g,y)=>{let m=f.split(`
206
+ `);for(let p of m)p.trim()&&g.enqueue(y.encode(p+`
207
+ `))},d=(f,g)=>{let{controller:y,encoder:m}=g;if(this.logger?.debug({line:f},"Processing reason line"),f.startsWith("data: ")&&f.trim()!=="data: [DONE]")try{let p=JSON.parse(f.slice(6));if(console.log(JSON.stringify(p)),p.choices?.[0]?.delta?.reasoning_content){g.appendReasoningContent(p.choices[0].delta.reasoning_content);let _={...p,choices:[{...p.choices[0],delta:{...p.choices[0].delta,thinking:{content:p.choices[0].delta.reasoning_content}}}]};delete _.choices[0].delta.reasoning_content;let k=`data: ${JSON.stringify(_)}
208
208
 
209
- `;y.enqueue(p.encode(k));return}if(g.choices?.[0]?.delta?.content&&f.reasoningContent()&&!f.isReasoningComplete()){f.setReasoningComplete(!0);let _="ccr_think_signature",k=g.choices[0].delta.content,h={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,content:null,thinking:{content:f.reasoningContent(),signature:_}}}]};delete h.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(h)}
209
+ `;y.enqueue(m.encode(k));return}if(p.choices?.[0]?.delta?.content&&g.reasoningContent()&&!g.isReasoningComplete()){g.setReasoningComplete(!0);let _="ccr_think_signature",k=p.choices[0].delta.content,h={...p,choices:[{...p.choices[0],delta:{...p.choices[0].delta,content:null,thinking:{content:g.reasoningContent(),signature:_}}}]};delete h.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(h)}
210
210
 
211
- `;y.enqueue(p.encode(b))}if(g.choices?.[0]?.delta?.reasoning_content&&delete g.choices[0].delta.reasoning_content,g.choices?.[0]?.delta&&Object.keys(g.choices[0].delta).length>0){f.isReasoningComplete()&&g.choices[0].index++;let _=`data: ${JSON.stringify(g)}
211
+ `;y.enqueue(m.encode(b))}if(p.choices?.[0]?.delta?.reasoning_content&&delete p.choices[0].delta.reasoning_content,p.choices?.[0]?.delta&&Object.keys(p.choices[0].delta).length>0){g.isReasoningComplete()&&p.choices[0].index++;let _=`data: ${JSON.stringify(p)}
212
212
 
213
- `;y.enqueue(p.encode(_))}}catch{y.enqueue(p.encode(m+`
214
- `))}else y.enqueue(p.encode(m+`
215
- `))};try{for(;;){let{done:m,value:f}=await a.read();if(m){i.trim()&&u(i,l,o);break}let y=t.decode(f,{stream:!0});i+=y;let p=i.split(`
216
- `);i=p.pop()||"";for(let g of p)if(g.trim())try{d(g,{controller:l,encoder:o,reasoningContent:()=>r,appendReasoningContent:_=>r+=_,isReasoningComplete:()=>s,setReasoningComplete:_=>s=_})}catch(_){console.error("Error processing line:",g,_),l.enqueue(o.encode(g+`
217
- `))}}}catch(m){console.error("Stream error:",m),l.error(m)}finally{try{a.releaseLock()}catch(m){console.error("Error releasing reader lock:",m)}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var en,Ns=w(()=>{"use strict";en=class{constructor(e){this.options=e;this.max_tokens=this.options?.max_tokens,this.temperature=this.options?.temperature,this.top_p=this.options?.top_p,this.top_k=this.options?.top_k,this.repetition_penalty=this.options?.repetition_penalty}static TransformerName="sampling";max_tokens;temperature;top_p;top_k;repetition_penalty;async transformRequestIn(e){return e.max_tokens&&e.max_tokens>this.max_tokens&&(e.max_tokens=this.max_tokens),typeof this.temperature<"u"&&(e.temperature=this.temperature),typeof this.top_p<"u"&&(e.top_p=this.top_p),typeof this.top_k<"u"&&(e.top_k=this.top_k),typeof this.repetition_penalty<"u"&&(e.repetition_penalty=this.repetition_penalty),e}}});var tn,Ls=w(()=>{"use strict";tn=class{static TransformerName="maxcompletiontokens";async transformRequestIn(e){return e.max_tokens&&(e.max_completion_tokens=e.max_tokens,delete e.max_tokens),e}}});function Ds(n){let e=[];for(let o=0;o<n.messages.length;o++){let r=n.messages[o],s=o===n.messages.length-1,i=r.role==="assistant",c=[];typeof r.content=="string"?c.push({type:"text",text:r.content}):Array.isArray(r.content)&&r.content.forEach(l=>{l.type==="text"?c.push({type:"text",text:l.text||""}):l.type==="image_url"&&c.push({type:"image",source:{type:"base64",media_type:l.media_type||"image/jpeg",data:l.image_url.url}})}),!(!s&&c.length===0&&!r.tool_calls&&!r.content)&&(s&&i&&c.length===0&&r.tool_calls&&c.push({type:"text",text:""}),e.push({role:r.role==="assistant"?"assistant":"user",content:c}))}let t={anthropic_version:"vertex-2023-10-16",messages:e,max_tokens:n.max_tokens||1e3,stream:n.stream||!1,...n.temperature&&{temperature:n.temperature}};return n.tools&&n.tools.length>0&&(t.tools=n.tools.map(o=>({name:o.function.name,description:o.function.description,input_schema:o.function.parameters}))),n.tool_choice&&(n.tool_choice==="auto"||n.tool_choice==="none"?t.tool_choice=n.tool_choice:typeof n.tool_choice=="string"&&(t.tool_choice={type:"tool",name:n.tool_choice})),t}function Fs(n){let e=n,o={messages:e.messages.map(r=>{let s=r.content.map(i=>i.type==="text"?{type:"text",text:i.text||""}:i.type==="image"&&i.source?{type:"image_url",image_url:{url:i.source.data},media_type:i.source.media_type}:{type:"text",text:""});return{role:r.role,content:s}}),model:n.model||"claude-sonnet-4@20250514",max_tokens:e.max_tokens,temperature:e.temperature,stream:e.stream};return e.tools&&e.tools.length>0&&(o.tools=e.tools.map(r=>({type:"function",function:{name:r.name,description:r.description,parameters:{type:"object",properties:r.input_schema.properties,required:r.input_schema.required,additionalProperties:r.input_schema.additionalProperties,$schema:r.input_schema.$schema}}}))),e.tool_choice&&(typeof e.tool_choice=="string"?o.tool_choice=e.tool_choice:e.tool_choice.type==="tool"&&(o.tool_choice=e.tool_choice.name)),o}async function Us(n,e,t){if(n.headers.get("Content-Type")?.includes("application/json")){let o=await n.json(),r;o.tool_use&&o.tool_use.length>0&&(r=o.tool_use.map(i=>({id:i.id,type:"function",function:{name:i.name,arguments:JSON.stringify(i.input)}})));let s={id:o.id,choices:[{finish_reason:o.stop_reason||null,index:0,message:{content:o.content[0]?.text||"",role:"assistant",...r&&{tool_calls:r}}}],created:parseInt(new Date().getTime()/1e3+"",10),model:o.model,object:"chat.completion",usage:{completion_tokens:o.usage.output_tokens,prompt_tokens:o.usage.input_tokens,total_tokens:o.usage.input_tokens+o.usage.output_tokens}};return new Response(JSON.stringify(s),{status:n.status,statusText:n.statusText,headers:n.headers})}else if(n.headers.get("Content-Type")?.includes("stream")){if(!n.body)return n;let o=new TextDecoder,r=new TextEncoder,s=(c,l)=>{if(c.startsWith("data: ")){let a=c.slice(6).trim();if(a){t?.debug({chunkStr:a},`${e} chunk:`);try{let u=JSON.parse(a);if(u.type==="content_block_delta"&&u.delta?.type==="text_delta"){let d={choices:[{delta:{role:"assistant",content:u.delta.text||""},finish_reason:null,index:0,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:u.id||"",model:u.model||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:u.usage?.output_tokens||0,prompt_tokens:u.usage?.input_tokens||0,total_tokens:(u.usage?.input_tokens||0)+(u.usage?.output_tokens||0)}};l.enqueue(r.encode(`data: ${JSON.stringify(d)}
213
+ `;y.enqueue(m.encode(_))}}catch{y.enqueue(m.encode(f+`
214
+ `))}else y.enqueue(m.encode(f+`
215
+ `))};try{for(;;){let{done:f,value:g}=await a.read();if(f){i.trim()&&u(i,l,o);break}let y=t.decode(g,{stream:!0});i+=y;let m=i.split(`
216
+ `);i=m.pop()||"";for(let p of m)if(p.trim())try{d(p,{controller:l,encoder:o,reasoningContent:()=>r,appendReasoningContent:_=>r+=_,isReasoningComplete:()=>s,setReasoningComplete:_=>s=_})}catch(_){console.error("Error processing line:",p,_),l.enqueue(o.encode(p+`
217
+ `))}}}catch(f){console.error("Stream error:",f),l.error(f)}finally{try{a.releaseLock()}catch(f){console.error("Error releasing reader lock:",f)}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var en,Ds=w(()=>{"use strict";en=class{constructor(e){this.options=e;this.max_tokens=this.options?.max_tokens,this.temperature=this.options?.temperature,this.top_p=this.options?.top_p,this.top_k=this.options?.top_k,this.repetition_penalty=this.options?.repetition_penalty}static TransformerName="sampling";max_tokens;temperature;top_p;top_k;repetition_penalty;async transformRequestIn(e){return e.max_tokens&&e.max_tokens>this.max_tokens&&(e.max_tokens=this.max_tokens),typeof this.temperature<"u"&&(e.temperature=this.temperature),typeof this.top_p<"u"&&(e.top_p=this.top_p),typeof this.top_k<"u"&&(e.top_k=this.top_k),typeof this.repetition_penalty<"u"&&(e.repetition_penalty=this.repetition_penalty),e}}});var tn,Fs=w(()=>{"use strict";tn=class{static TransformerName="maxcompletiontokens";async transformRequestIn(e){return e.max_tokens&&(e.max_completion_tokens=e.max_tokens,delete e.max_tokens),e}}});function Us(n){let e=[];for(let o=0;o<n.messages.length;o++){let r=n.messages[o],s=o===n.messages.length-1,i=r.role==="assistant",c=[];typeof r.content=="string"?c.push({type:"text",text:r.content}):Array.isArray(r.content)&&r.content.forEach(l=>{l.type==="text"?c.push({type:"text",text:l.text||""}):l.type==="image_url"&&c.push({type:"image",source:{type:"base64",media_type:l.media_type||"image/jpeg",data:l.image_url.url}})}),!(!s&&c.length===0&&!r.tool_calls&&!r.content)&&(s&&i&&c.length===0&&r.tool_calls&&c.push({type:"text",text:""}),e.push({role:r.role==="assistant"?"assistant":"user",content:c}))}let t={anthropic_version:"vertex-2023-10-16",messages:e,max_tokens:n.max_tokens||1e3,stream:n.stream||!1,...n.temperature&&{temperature:n.temperature}};return n.tools&&n.tools.length>0&&(t.tools=n.tools.map(o=>({name:o.function.name,description:o.function.description,input_schema:o.function.parameters}))),n.tool_choice&&(n.tool_choice==="auto"||n.tool_choice==="none"?t.tool_choice=n.tool_choice:typeof n.tool_choice=="string"&&(t.tool_choice={type:"tool",name:n.tool_choice})),t}function js(n){let e=n,o={messages:e.messages.map(r=>{let s=r.content.map(i=>i.type==="text"?{type:"text",text:i.text||""}:i.type==="image"&&i.source?{type:"image_url",image_url:{url:i.source.data},media_type:i.source.media_type}:{type:"text",text:""});return{role:r.role,content:s}}),model:n.model||"claude-sonnet-4@20250514",max_tokens:e.max_tokens,temperature:e.temperature,stream:e.stream};return e.tools&&e.tools.length>0&&(o.tools=e.tools.map(r=>({type:"function",function:{name:r.name,description:r.description,parameters:{type:"object",properties:r.input_schema.properties,required:r.input_schema.required,additionalProperties:r.input_schema.additionalProperties,$schema:r.input_schema.$schema}}}))),e.tool_choice&&(typeof e.tool_choice=="string"?o.tool_choice=e.tool_choice:e.tool_choice.type==="tool"&&(o.tool_choice=e.tool_choice.name)),o}async function Hs(n,e,t){if(n.headers.get("Content-Type")?.includes("application/json")){let o=await n.json(),r;o.tool_use&&o.tool_use.length>0&&(r=o.tool_use.map(i=>({id:i.id,type:"function",function:{name:i.name,arguments:JSON.stringify(i.input)}})));let s={id:o.id,choices:[{finish_reason:o.stop_reason||null,index:0,message:{content:o.content[0]?.text||"",role:"assistant",...r&&{tool_calls:r}}}],created:parseInt(new Date().getTime()/1e3+"",10),model:o.model,object:"chat.completion",usage:{completion_tokens:o.usage.output_tokens,prompt_tokens:o.usage.input_tokens,total_tokens:o.usage.input_tokens+o.usage.output_tokens}};return new Response(JSON.stringify(s),{status:n.status,statusText:n.statusText,headers:n.headers})}else if(n.headers.get("Content-Type")?.includes("stream")){if(!n.body)return n;let o=new TextDecoder,r=new TextEncoder,s=(c,l)=>{if(c.startsWith("data: ")){let a=c.slice(6).trim();if(a){t?.debug({chunkStr:a},`${e} chunk:`);try{let u=JSON.parse(a);if(u.type==="content_block_delta"&&u.delta?.type==="text_delta"){let d={choices:[{delta:{role:"assistant",content:u.delta.text||""},finish_reason:null,index:0,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:u.id||"",model:u.model||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:u.usage?.output_tokens||0,prompt_tokens:u.usage?.input_tokens||0,total_tokens:(u.usage?.input_tokens||0)+(u.usage?.output_tokens||0)}};l.enqueue(r.encode(`data: ${JSON.stringify(d)}
218
218
 
219
219
  `))}else if(u.type==="content_block_delta"&&u.delta?.type==="input_json_delta"){let d={choices:[{delta:{tool_calls:[{index:u.index||0,function:{arguments:u.delta.partial_json||""}}]},finish_reason:null,index:0,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:u.id||"",model:u.model||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:u.usage?.output_tokens||0,prompt_tokens:u.usage?.input_tokens||0,total_tokens:(u.usage?.input_tokens||0)+(u.usage?.output_tokens||0)}};l.enqueue(r.encode(`data: ${JSON.stringify(d)}
220
220
 
@@ -226,94 +226,94 @@ Examples:
226
226
 
227
227
  `));else{let d={choices:[{delta:{role:"assistant",content:u.content?.[0]?.text||""},finish_reason:u.stop_reason?.toLowerCase()||null,index:0,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:u.id||"",model:u.model||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:u.usage?.output_tokens||0,prompt_tokens:u.usage?.input_tokens||0,total_tokens:(u.usage?.input_tokens||0)+(u.usage?.output_tokens||0)}};l.enqueue(r.encode(`data: ${JSON.stringify(d)}
228
228
 
229
- `))}}catch(u){t?.error(`Error parsing ${e} stream chunk`,a,u.message)}}}},i=new ReadableStream({async start(c){let l=n.body.getReader(),a="";try{for(;;){let{done:u,value:d}=await l.read();if(u){a&&s(a,c);break}a+=o.decode(d,{stream:!0});let m=a.split(`
230
- `);a=m.pop()||"";for(let f of m)s(f,c)}}catch(u){c.error(u)}finally{c.close()}}});return new Response(i,{status:n.status,statusText:n.statusText,headers:n.headers})}return n}var js=w(()=>{"use strict"});async function jc(){try{let{GoogleAuth:n}=await import("google-auth-library");return(await(await new n({scopes:["https://www.googleapis.com/auth/cloud-platform"]}).getClient()).getAccessToken()).token||""}catch(n){throw console.error("Error getting access token:",n),new Error(`Failed to get access token for Vertex AI. Please ensure you have set up authentication using one of these methods:
229
+ `))}}catch(u){t?.error(`Error parsing ${e} stream chunk`,a,u.message)}}}},i=new ReadableStream({async start(c){let l=n.body.getReader(),a="";try{for(;;){let{done:u,value:d}=await l.read();if(u){a&&s(a,c);break}a+=o.decode(d,{stream:!0});let f=a.split(`
230
+ `);a=f.pop()||"";for(let g of f)s(g,c)}}catch(u){c.error(u)}finally{c.close()}}});return new Response(i,{status:n.status,statusText:n.statusText,headers:n.headers})}return n}var zs=w(()=>{"use strict"});async function Bc(){try{let{GoogleAuth:n}=await import("google-auth-library");return(await(await new n({scopes:["https://www.googleapis.com/auth/cloud-platform"]}).getClient()).getAccessToken()).token||""}catch(n){throw console.error("Error getting access token:",n),new Error(`Failed to get access token for Vertex AI. Please ensure you have set up authentication using one of these methods:
231
231
  1. Set GOOGLE_APPLICATION_CREDENTIALS to point to service account key file
232
232
  2. Run "gcloud auth application-default login"
233
- 3. Use Google Cloud environment with default service account`)}}var nn,Hs=w(()=>{"use strict";js();nn=class{name="vertex-claude";async transformRequestIn(e,t){let o=process.env.GOOGLE_CLOUD_PROJECT,r=process.env.GOOGLE_CLOUD_LOCATION||"us-east5";if(!o&&process.env.GOOGLE_APPLICATION_CREDENTIALS)try{let c=(await import("fs")).readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS,"utf8"),l=JSON.parse(c);l&&l.project_id&&(o=l.project_id)}catch(i){console.error("Error extracting project_id from GOOGLE_APPLICATION_CREDENTIALS:",i)}if(!o)throw new Error("Project ID is required for Vertex AI. Set GOOGLE_CLOUD_PROJECT environment variable or ensure project_id is in GOOGLE_APPLICATION_CREDENTIALS file.");let s=await jc();return{body:Ds(e),config:{url:new URL(`/v1/projects/${o}/locations/${r}/publishers/anthropic/models/${e.model}:${e.stream?"streamRawPredict":"rawPredict"}`,`https://${r}-aiplatform.googleapis.com`).toString(),headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json"}}}}async transformRequestOut(e){return Fs(e)}async transformResponseOut(e){return Us(e,this.name,this.logger)}}});var on,zs=w(()=>{"use strict";on=class{name="cerebras";async transformRequestIn(e,t){let o=JSON.parse(JSON.stringify(e));return o.reasoning?delete o.reasoning:o.disable_reasoning=!1,{body:o,config:{headers:{Authorization:`Bearer ${t.apiKey}`,"Content-Type":"application/json"}}}}async transformResponseOut(e){return e}}});var rn,Js=w(()=>{"use strict";rn=class{name="streamoptions";async transformRequestIn(e){return e.stream&&(e.stream_options={include_usage:!0}),e}}});var sn,Bs=w(()=>{"use strict";sn=class{name="OpenAI";endPoint="/v1/chat/completions"}});var an,Ws=w(()=>{"use strict";an=class{static TransformerName="customparams";options;constructor(e={}){this.options=e}async transformRequestIn(e){let t={...e},o=Object.entries(this.options);for(let[r,s]of o)if(r in t)if(typeof t[r]=="object"&&typeof s=="object"&&!Array.isArray(t[r])&&!Array.isArray(s)&&t[r]!==null&&s!==null)t[r]=this.deepMergeObjects(t[r],s);else continue;else t[r]=this.cloneValue(s);return t}async transformResponseOut(e){return e}deepMergeObjects(e,t){let o={...e};for(let[r,s]of Object.entries(t))r in o&&typeof o[r]=="object"&&typeof s=="object"&&!Array.isArray(o[r])&&!Array.isArray(s)&&o[r]!==null&&s!==null?o[r]=this.deepMergeObjects(o[r],s):o[r]=this.cloneValue(s);return o}cloneValue(e){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return e.map(o=>this.cloneValue(o));let t={};for(let[o,r]of Object.entries(e))t[o]=this.cloneValue(r);return t}}});import{v4 as Hc}from"uuid";var cn,Gs=w(()=>{"use strict";Q();cn=class{constructor(e){this.options=e}static TransformerName="vercel";endPoint="/v1/chat/completions";async transformRequestIn(e){return e.model.includes("claude")?e.messages.forEach(t=>{Array.isArray(t.content)&&t.content.forEach(o=>{o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)})}):e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control,o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)}):t.cache_control&&delete t.cache_control}),Object.assign(e,this.options||{}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,c=!1,l="",a=new ReadableStream({async start(u){let d=e.body.getReader(),m=(y,p,g)=>{let _=y.split(`
234
- `);for(let k of _)k.trim()&&p.enqueue(g.encode(k+`
235
- `))},f=(y,p)=>{let{controller:g,encoder:_}=p;if(y.startsWith("data: ")&&y.trim()!=="data: [DONE]"){let k=y.slice(6);try{let h=JSON.parse(k);if(h.usage&&(this.logger?.debug({usage:h.usage,hasToolCall:c},"usage"),h.choices[0].finish_reason=c?"tool_calls":"stop"),h.choices?.[0]?.finish_reason==="error"&&g.enqueue(_.encode(`data: ${JSON.stringify({error:h.choices?.[0].error})}
233
+ 3. Use Google Cloud environment with default service account`)}}var nn,Bs=w(()=>{"use strict";zs();nn=class{name="vertex-claude";async transformRequestIn(e,t){let o=process.env.GOOGLE_CLOUD_PROJECT,r=process.env.GOOGLE_CLOUD_LOCATION||"us-east5";if(!o&&process.env.GOOGLE_APPLICATION_CREDENTIALS)try{let c=(await import("fs")).readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS,"utf8"),l=JSON.parse(c);l&&l.project_id&&(o=l.project_id)}catch(i){console.error("Error extracting project_id from GOOGLE_APPLICATION_CREDENTIALS:",i)}if(!o)throw new Error("Project ID is required for Vertex AI. Set GOOGLE_CLOUD_PROJECT environment variable or ensure project_id is in GOOGLE_APPLICATION_CREDENTIALS file.");let s=await Bc();return{body:Us(e),config:{url:new URL(`/v1/projects/${o}/locations/${r}/publishers/anthropic/models/${e.model}:${e.stream?"streamRawPredict":"rawPredict"}`,`https://${r}-aiplatform.googleapis.com`).toString(),headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json"}}}}async transformRequestOut(e){return js(e)}async transformResponseOut(e){return Hs(e,this.name,this.logger)}}});var on,Js=w(()=>{"use strict";on=class{name="cerebras";async transformRequestIn(e,t){let o=JSON.parse(JSON.stringify(e));return o.reasoning?delete o.reasoning:o.disable_reasoning=!1,{body:o,config:{headers:{Authorization:`Bearer ${t.apiKey}`,"Content-Type":"application/json"}}}}async transformResponseOut(e){return e}}});var rn,Ws=w(()=>{"use strict";rn=class{name="streamoptions";async transformRequestIn(e){return e.stream&&(e.stream_options={include_usage:!0}),e}}});var sn,Gs=w(()=>{"use strict";sn=class{name="OpenAI";endPoint="/v1/chat/completions"}});var an,Ks=w(()=>{"use strict";an=class{static TransformerName="customparams";options;constructor(e={}){this.options=e}async transformRequestIn(e){let t={...e},o=Object.entries(this.options);for(let[r,s]of o)if(r in t)if(typeof t[r]=="object"&&typeof s=="object"&&!Array.isArray(t[r])&&!Array.isArray(s)&&t[r]!==null&&s!==null)t[r]=this.deepMergeObjects(t[r],s);else continue;else t[r]=this.cloneValue(s);return t}async transformResponseOut(e){return e}deepMergeObjects(e,t){let o={...e};for(let[r,s]of Object.entries(t))r in o&&typeof o[r]=="object"&&typeof s=="object"&&!Array.isArray(o[r])&&!Array.isArray(s)&&o[r]!==null&&s!==null?o[r]=this.deepMergeObjects(o[r],s):o[r]=this.cloneValue(s);return o}cloneValue(e){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return e.map(o=>this.cloneValue(o));let t={};for(let[o,r]of Object.entries(e))t[o]=this.cloneValue(r);return t}}});import{v4 as Jc}from"uuid";var cn,Qs=w(()=>{"use strict";Q();cn=class{constructor(e){this.options=e}static TransformerName="vercel";endPoint="/v1/chat/completions";async transformRequestIn(e){return e.model.includes("claude")?e.messages.forEach(t=>{Array.isArray(t.content)&&t.content.forEach(o=>{o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)})}):e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control,o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)}):t.cache_control&&delete t.cache_control}),Object.assign(e,this.options||{}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,c=!1,l="",a=new ReadableStream({async start(u){let d=e.body.getReader(),f=(y,m,p)=>{let _=y.split(`
234
+ `);for(let k of _)k.trim()&&m.enqueue(p.encode(k+`
235
+ `))},g=(y,m)=>{let{controller:p,encoder:_}=m;if(y.startsWith("data: ")&&y.trim()!=="data: [DONE]"){let k=y.slice(6);try{let h=JSON.parse(k);if(h.usage&&(this.logger?.debug({usage:h.usage,hasToolCall:c},"usage"),h.choices[0].finish_reason=c?"tool_calls":"stop"),h.choices?.[0]?.finish_reason==="error"&&p.enqueue(_.encode(`data: ${JSON.stringify({error:h.choices?.[0].error})}
236
236
 
237
- `)),h.choices?.[0]?.delta?.content&&!p.hasTextContent()&&p.setHasTextContent(!0),h.choices?.[0]?.delta?.reasoning){p.appendReasoningContent(h.choices[0].delta.reasoning);let T={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,thinking:{content:h.choices[0].delta.reasoning}}}]};T.choices?.[0]?.delta&&delete T.choices[0].delta.reasoning;let x=`data: ${JSON.stringify(T)}
237
+ `)),h.choices?.[0]?.delta?.content&&!m.hasTextContent()&&m.setHasTextContent(!0),h.choices?.[0]?.delta?.reasoning){m.appendReasoningContent(h.choices[0].delta.reasoning);let T={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,thinking:{content:h.choices[0].delta.reasoning}}}]};T.choices?.[0]?.delta&&delete T.choices[0].delta.reasoning;let x=`data: ${JSON.stringify(T)}
238
238
 
239
- `;g.enqueue(_.encode(x));return}if(h.choices?.[0]?.delta?.content&&p.reasoningContent()&&!p.isReasoningComplete()){p.setReasoningComplete(!0);let x={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:null,thinking:{content:p.reasoningContent(),signature:"ccr_think_signature"}}}]};x.choices?.[0]?.delta&&delete x.choices[0].delta.reasoning;let C=`data: ${JSON.stringify(x)}
239
+ `;p.enqueue(_.encode(x));return}if(h.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let x={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:"ccr_think_signature"}}}]};x.choices?.[0]?.delta&&delete x.choices[0].delta.reasoning;let C=`data: ${JSON.stringify(x)}
240
240
 
241
- `;g.enqueue(_.encode(C))}h.choices?.[0]?.delta?.reasoning&&delete h.choices[0].delta.reasoning,h.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(h.choices?.[0]?.delta?.tool_calls[0].id,10))&&h.choices?.[0]?.delta?.tool_calls.forEach(T=>{T.id=`call_${Hc()}`}),h.choices?.[0]?.delta?.tool_calls?.length&&!c&&(c=!0),h.choices?.[0]?.delta?.tool_calls?.length&&p.hasTextContent()&&(typeof h.choices[0].index=="number"?h.choices[0].index+=1:h.choices[0].index=1);let b=`data: ${JSON.stringify(h)}
241
+ `;p.enqueue(_.encode(C))}h.choices?.[0]?.delta?.reasoning&&delete h.choices[0].delta.reasoning,h.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(h.choices?.[0]?.delta?.tool_calls[0].id,10))&&h.choices?.[0]?.delta?.tool_calls.forEach(T=>{T.id=`call_${Jc()}`}),h.choices?.[0]?.delta?.tool_calls?.length&&!c&&(c=!0),h.choices?.[0]?.delta?.tool_calls?.length&&m.hasTextContent()&&(typeof h.choices[0].index=="number"?h.choices[0].index+=1:h.choices[0].index=1);let b=`data: ${JSON.stringify(h)}
242
242
 
243
- `;g.enqueue(_.encode(b))}catch{g.enqueue(_.encode(y+`
244
- `))}}else g.enqueue(_.encode(y+`
245
- `))};try{for(;;){let{done:y,value:p}=await d.read();if(y){l.trim()&&m(l,u,o);break}if(!p||p.length===0)continue;let g;try{g=t.decode(p,{stream:!0})}catch(k){console.warn("Failed to decode chunk",k);continue}if(g.length===0)continue;if(l+=g,l.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let k=l.split(`
246
- `);l=k.pop()||"";for(let h of k)if(h.trim())try{f(h,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:b=>r=b,reasoningContent:()=>s,appendReasoningContent:b=>s+=b,isReasoningComplete:()=>i,setReasoningComplete:b=>i=b})}catch(b){console.error("Error processing line:",h,b),u.enqueue(o.encode(h+`
243
+ `;p.enqueue(_.encode(b))}catch{p.enqueue(_.encode(y+`
244
+ `))}}else p.enqueue(_.encode(y+`
245
+ `))};try{for(;;){let{done:y,value:m}=await d.read();if(y){l.trim()&&f(l,u,o);break}if(!m||m.length===0)continue;let p;try{p=t.decode(m,{stream:!0})}catch(k){console.warn("Failed to decode chunk",k);continue}if(p.length===0)continue;if(l+=p,l.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let k=l.split(`
246
+ `);l=k.pop()||"";for(let h of k)if(h.trim())try{g(h,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:b=>r=b,reasoningContent:()=>s,appendReasoningContent:b=>s+=b,isReasoningComplete:()=>i,setReasoningComplete:b=>i=b})}catch(b){console.error("Error processing line:",h,b),u.enqueue(o.encode(h+`
247
247
  `))}continue}let _=l.split(`
248
- `);l=_.pop()||"";for(let k of _)if(k.trim())try{f(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:h=>r=h,reasoningContent:()=>s,appendReasoningContent:h=>s+=h,isReasoningComplete:()=>i,setReasoningComplete:h=>i=h})}catch(h){console.error("Error processing line:",k,h),u.enqueue(o.encode(k+`
249
- `))}}}catch(y){console.error("Stream error:",y),u.error(y)}finally{try{d.releaseLock()}catch(y){console.error("Error releasing reader lock:",y)}u.close()}}});return new Response(a,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var go,zc,ln,Ks=w(()=>{"use strict";Q();go=`Always think before answering. Even if the problem seems simple, always write down your reasoning process explicitly.
248
+ `);l=_.pop()||"";for(let k of _)if(k.trim())try{g(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:h=>r=h,reasoningContent:()=>s,appendReasoningContent:h=>s+=h,isReasoningComplete:()=>i,setReasoningComplete:h=>i=h})}catch(h){console.error("Error processing line:",k,h),u.enqueue(o.encode(k+`
249
+ `))}}}catch(y){console.error("Stream error:",y),u.error(y)}finally{try{d.releaseLock()}catch(y){console.error("Error releasing reader lock:",y)}u.close()}}});return new Response(a,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var yo,Wc,ln,Vs=w(()=>{"use strict";Q();yo=`Always think before answering. Even if the problem seems simple, always write down your reasoning process explicitly.
250
250
 
251
251
  Output format:
252
252
  <reasoning_content>
253
253
  Your detailed thinking process goes here
254
254
  </reasoning_content>
255
- Your final answer must follow after the closing tag above.`,zc=10,ln=class{name="forcereasoning";async transformRequestIn(e){let t=0;e.messages.filter(r=>r.role==="assistant").reverse().forEach(r=>{r.thinking&&(r.thinking.content&&(!r.content||t<zc)&&(t++,r.content=`<reasoning_content>${r.thinking.content}</reasoning_content>
256
- ${r.content}`),delete r.thinking)});let o=e.messages[e.messages.length-1];return o.role==="user"&&(Array.isArray(o.content)?o.content.push({type:"text",text:go}):o.content=[{type:"text",text:go},{type:"text",text:o.content||""}]),o.role==="tool"&&e.messages.push({role:"user",content:[{type:"text",text:go}]}),e}async transformResponseOut(e){let t="<reasoning_content>",o="</reasoning_content>";if(e.headers.get("Content-Type")?.includes("application/json")){let r=await F(e);if(r.choices[0]?.message.content){let s=/<reasoning_content>(.*?)<\/reasoning_content>/s,i=r.choices[0]?.message.content.match(s);i&&i[1]&&(r.thinking={content:i[1]})}return new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let r=0,s=new TextDecoder,i=new TextEncoder,c=new ReadableStream({async start(l){let a=e.body.getReader(),u="",d="SEARCHING",m="",f="",y=(p,g)=>{if(typeof g!="string"){p.choices?.[0]?.delta&&Object.keys(p.choices[0].delta).length>0&&!p.choices[0].delta.content&&(p.choices[0].index=r,l.enqueue(i.encode(`data: ${JSON.stringify(p)}
255
+ Your final answer must follow after the closing tag above.`,Wc=10,ln=class{name="forcereasoning";async transformRequestIn(e){let t=0;e.messages.filter(r=>r.role==="assistant").reverse().forEach(r=>{r.thinking&&(r.thinking.content&&(!r.content||t<Wc)&&(t++,r.content=`<reasoning_content>${r.thinking.content}</reasoning_content>
256
+ ${r.content}`),delete r.thinking)});let o=e.messages[e.messages.length-1];return o.role==="user"&&(Array.isArray(o.content)?o.content.push({type:"text",text:yo}):o.content=[{type:"text",text:yo},{type:"text",text:o.content||""}]),o.role==="tool"&&e.messages.push({role:"user",content:[{type:"text",text:yo}]}),e}async transformResponseOut(e){let t="<reasoning_content>",o="</reasoning_content>";if(e.headers.get("Content-Type")?.includes("application/json")){let r=await F(e);if(r.choices[0]?.message.content){let s=/<reasoning_content>(.*?)<\/reasoning_content>/s,i=r.choices[0]?.message.content.match(s);i&&i[1]&&(r.thinking={content:i[1]})}return new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let r=0,s=new TextDecoder,i=new TextEncoder,c=new ReadableStream({async start(l){let a=e.body.getReader(),u="",d="SEARCHING",f="",g="",y=(m,p)=>{if(typeof p!="string"){m.choices?.[0]?.delta&&Object.keys(m.choices[0].delta).length>0&&!m.choices[0].delta.content&&(m.choices[0].index=r,l.enqueue(i.encode(`data: ${JSON.stringify(m)}
257
257
 
258
- `)));return}let _=m+g;for(m="";_.length>0;)if(d==="SEARCHING"){let k=_.indexOf(t);if(k!==-1)_=_.substring(k+t.length),d="REASONING";else{for(let h=t.length-1;h>0;h--)if(_.endsWith(t.substring(0,h))){m=_.substring(_.length-h);break}_=""}}else if(d==="REASONING"){let k=_.indexOf(o);if(k!==-1){let h=_.substring(0,k);if(h.length>0){let x={...p.choices[0].delta,thinking:{content:h}};delete x.content;let C={...p,choices:[{...p.choices[0],delta:x,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(C)}
258
+ `)));return}let _=f+p;for(f="";_.length>0;)if(d==="SEARCHING"){let k=_.indexOf(t);if(k!==-1)_=_.substring(k+t.length),d="REASONING";else{for(let h=t.length-1;h>0;h--)if(_.endsWith(t.substring(0,h))){f=_.substring(_.length-h);break}_=""}}else if(d==="REASONING"){let k=_.indexOf(o);if(k!==-1){let h=_.substring(0,k);if(h.length>0){let x={...m.choices[0].delta,thinking:{content:h}};delete x.content;let C={...m,choices:[{...m.choices[0],delta:x,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(C)}
259
259
 
260
- `))}let b={...p.choices[0].delta,thinking:{signature:"ccr_think_signature"}};delete b.content;let T={...p,choices:[{...p.choices[0],delta:b,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
260
+ `))}let b={...m.choices[0].delta,thinking:{signature:"ccr_think_signature"}};delete b.content;let T={...m,choices:[{...m.choices[0],delta:b,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
261
261
 
262
- `)),r++,_=_.substring(k+o.length),d="FINAL"}else{let h=_;for(let b=o.length-1;b>0;b--)if(_.endsWith(o.substring(0,b))){m=_.substring(_.length-b),h=_.substring(0,_.length-b);break}if(h.length>0){let b={...p.choices[0].delta,thinking:{content:h}};delete b.content;let T={...p,choices:[{...p.choices[0],delta:b,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
262
+ `)),r++,_=_.substring(k+o.length),d="FINAL"}else{let h=_;for(let b=o.length-1;b>0;b--)if(_.endsWith(o.substring(0,b))){f=_.substring(_.length-b),h=_.substring(0,_.length-b);break}if(h.length>0){let b={...m.choices[0].delta,thinking:{content:h}};delete b.content;let T={...m,choices:[{...m.choices[0],delta:b,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
263
263
 
264
- `))}_=""}}else if(d==="FINAL"){if(_.length>0)if(/^\s*$/.test(_))f+=_;else{let h=f+_,b={...p.choices[0].delta,content:h};b.thinking&&delete b.thinking;let T={...p,choices:[{...p.choices[0],delta:b}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
264
+ `))}_=""}}else if(d==="FINAL"){if(_.length>0)if(/^\s*$/.test(_))g+=_;else{let h=g+_,b={...m.choices[0].delta,content:h};b.thinking&&delete b.thinking;let T={...m,choices:[{...m.choices[0],delta:b}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
265
265
 
266
- `)),f=""}r++,_=""}};try{for(;;){let{done:p,value:g}=await a.read();if(p)break;let _=s.decode(g,{stream:!0});u+=_;let k=u.split(`
266
+ `)),g=""}r++,_=""}};try{for(;;){let{done:m,value:p}=await a.read();if(m)break;let _=s.decode(p,{stream:!0});u+=_;let k=u.split(`
267
267
  `);u=k.pop()||"";for(let h of k)if(h.trim()){if(h.trim()==="data: [DONE]"){l.enqueue(i.encode(h+`
268
268
 
269
269
  `));break}if(h.startsWith("data:"))try{let b=JSON.parse(h.slice(5));y(b,b.choices?.[0]?.delta?.content)}catch{l.enqueue(i.encode(h+`
270
270
  `))}else l.enqueue(i.encode(h+`
271
- `))}}}catch(p){console.error("Stream error:",p),l.error(p)}finally{try{a.releaseLock()}catch(p){console.error("Error releasing reader lock:",p)}if(d==="REASONING"){let g={choices:[{delta:{thinking:{signature:"ccr_think_signature"}}}]};l.enqueue(i.encode(`data: ${JSON.stringify(g)}
271
+ `))}}}catch(m){console.error("Stream error:",m),l.error(m)}finally{try{a.releaseLock()}catch(m){console.error("Error releasing reader lock:",m)}if(d==="REASONING"){let p={choices:[{delta:{thinking:{signature:"ccr_think_signature"}}}]};l.enqueue(i.encode(`data: ${JSON.stringify(p)}
272
272
 
273
- `))}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":e.headers.get("Content-Type")||"text/plain","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var un,Qs=w(()=>{"use strict";Q();un=class{name="kimi";async transformRequestIn(e){return(e.thinking||e.reasoning?.enabled||e.messages.some(o=>o.thinking?.content))&&e.messages.forEach(o=>{if(o.role==="assistant"){let r=o.thinking?.content,s=o.thinking?.signature;r&&typeof r=="string"&&r.trim()&&(o.reasoning_content=r,s&&(o.reasoning_content_signature=s)),o.tool_calls&&o.tool_calls.length>0&&(o.reasoning_content||(o.reasoning_content="")),o.thinking&&delete o.thinking}}),e}async transformResponseOut(e){let t=e.headers.get("Content-Type");if(t?.includes("stream")){if(!e.body)return e;let o=new TextDecoder,r=new TextEncoder,s="",i="",c=new ReadableStream({async start(l){let a=e.body.getReader();try{for(;;){let{done:u,value:d}=await a.read();if(u){i.trim()&&l.enqueue(r.encode(i+`
274
- `));break}let m=o.decode(d,{stream:!0});i+=m;let f=i.split(`
275
- `);i=f.pop()||"";for(let y of f)if(y.trim())if(y.startsWith("data: ")&&y.trim()!=="data: [DONE]")try{let p=JSON.parse(y.slice(6));if(p.choices?.[0]?.delta?.reasoning_content){s+=p.choices[0].delta.reasoning_content;let g={...p,choices:[{...p.choices[0],delta:{thinking:{content:p.choices[0].delta.reasoning_content}}}]};delete g.choices[0].delta.reasoning_content,l.enqueue(r.encode(`data: ${JSON.stringify(g)}
273
+ `))}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":e.headers.get("Content-Type")||"text/plain","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var un,Ys=w(()=>{"use strict";Q();un=class{name="kimi";async transformRequestIn(e){return(e.thinking||e.reasoning?.enabled||e.messages.some(o=>o.thinking?.content))&&e.messages.forEach(o=>{if(o.role==="assistant"){let r=o.thinking?.content,s=o.thinking?.signature;r&&typeof r=="string"&&r.trim()&&(o.reasoning_content=r,s&&(o.reasoning_content_signature=s)),o.tool_calls&&o.tool_calls.length>0&&(o.reasoning_content||(o.reasoning_content="")),o.thinking&&delete o.thinking}}),e}async transformResponseOut(e){let t=e.headers.get("Content-Type");if(t?.includes("stream")){if(!e.body)return e;let o=new TextDecoder,r=new TextEncoder,s="",i="",c=new ReadableStream({async start(l){let a=e.body.getReader();try{for(;;){let{done:u,value:d}=await a.read();if(u){i.trim()&&l.enqueue(r.encode(i+`
274
+ `));break}let f=o.decode(d,{stream:!0});i+=f;let g=i.split(`
275
+ `);i=g.pop()||"";for(let y of g)if(y.trim())if(y.startsWith("data: ")&&y.trim()!=="data: [DONE]")try{let m=JSON.parse(y.slice(6));if(m.choices?.[0]?.delta?.reasoning_content){s+=m.choices[0].delta.reasoning_content;let p={...m,choices:[{...m.choices[0],delta:{thinking:{content:m.choices[0].delta.reasoning_content}}}]};delete p.choices[0].delta.reasoning_content,l.enqueue(r.encode(`data: ${JSON.stringify(p)}
276
276
 
277
- `));continue}if(p.choices?.[0]?.delta?.content&&s&&!p.choices[0]?.delta?.reasoning_content){let g=Date.now().toString(),_={...p,choices:[{...p.choices[0],delta:{content:null,thinking:{content:s,signature:g}}}]};l.enqueue(r.encode(`data: ${JSON.stringify(_)}
277
+ `));continue}if(m.choices?.[0]?.delta?.content&&s&&!m.choices[0]?.delta?.reasoning_content){let p=Date.now().toString(),_={...m,choices:[{...m.choices[0],delta:{content:null,thinking:{content:s,signature:p}}}]};l.enqueue(r.encode(`data: ${JSON.stringify(_)}
278
278
 
279
- `)),s=""}p.choices?.[0]?.delta?.reasoning_content&&delete p.choices[0].delta.reasoning_content,p.choices?.[0]?.delta&&Object.keys(p.choices[0].delta).length>0&&l.enqueue(r.encode(`data: ${JSON.stringify(p)}
279
+ `)),s=""}m.choices?.[0]?.delta?.reasoning_content&&delete m.choices[0].delta.reasoning_content,m.choices?.[0]?.delta&&Object.keys(m.choices[0].delta).length>0&&l.enqueue(r.encode(`data: ${JSON.stringify(m)}
280
280
 
281
281
  `))}catch{l.enqueue(r.encode(y+`
282
282
  `))}else l.enqueue(r.encode(y+`
283
- `))}}catch(u){l.error(u)}finally{try{a.releaseLock()}catch{}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":t||"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}if(t?.includes("application/json")){let o=await F(e);return o.choices?.[0]?.message?.reasoning_content&&(o.choices[0].message.thinking={content:o.choices[0].message.reasoning_content},delete o.choices[0].message.reasoning_content),new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}return e}}});import{v4 as Vs}from"uuid";var dn,Ys=w(()=>{"use strict";Q();Me();dn=class{constructor(e){this.options=e}static TransformerName="opencode";endPoint="/v1/chat/completions";resolveSessionId(e,t){let o=t?.req,r=o?.headers||{},s=i=>{let c=Object.keys(r).find(a=>a.toLowerCase()===i),l=c?r[c]:void 0;return typeof l=="string"&&l.trim()?l.trim():void 0};return s("x-opencode-session")||o?.clientContext?.stableSessionId||o?.sessionId||ye(e.metadata?.user_id)||s("session_id")||Vs()}async transformRequestIn(e,t,o){let r=e.reasoning?.enabled||e.messages.some(s=>s.thinking?.content);return e.messages.forEach(s=>{if(Array.isArray(s.content)&&s.content.forEach(i=>{i.type==="image_url"&&(i.image_url.url.startsWith("http")||(i.image_url.url=`data:${i.media_type};base64,${i.image_url.url}`),delete i.media_type)}),s.role==="assistant"){let i=s.thinking?.content,c=s.thinking?.signature;i&&typeof i=="string"&&i.trim()?(s.reasoning_content=i,c&&(s.reasoning_content_signature=c)):r&&s.tool_calls?.length&&!s.reasoning_content&&(s.reasoning_content=" "),s.thinking&&delete s.thinking}}),Object.assign(e,this.options||{}),{body:e,config:{headers:{"x-opencode-session":this.resolveSessionId(e,o)}}}}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return t.choices?.[0]?.message?.reasoning_content&&(t.choices[0].message.thinking={content:t.choices[0].message.reasoning_content},delete t.choices[0].message.reasoning_content),new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,c=!1,l="",a=new ReadableStream({async start(u){let d=e.body.getReader(),m=(y,p,g)=>{let _=y.split(`
284
- `);for(let k of _)k.trim()&&p.enqueue(g.encode(k+`
285
- `))},f=(y,p)=>{let{controller:g,encoder:_}=p;if(y.startsWith("data: ")&&y.trim()!=="data: [DONE]")try{let k=JSON.parse(y.slice(6));if(k.usage&&(k.choices[0].finish_reason=c?"tool_calls":"stop"),k.choices?.[0]?.finish_reason==="error"){g.enqueue(_.encode(`data: ${JSON.stringify({error:k.choices?.[0]?.error})}
283
+ `))}}catch(u){l.error(u)}finally{try{a.releaseLock()}catch{}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":t||"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}if(t?.includes("application/json")){let o=await F(e);return o.choices?.[0]?.message?.reasoning_content&&(o.choices[0].message.thinking={content:o.choices[0].message.reasoning_content},delete o.choices[0].message.reasoning_content),new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}return e}}});import{v4 as Xs}from"uuid";var dn,Zs=w(()=>{"use strict";Q();$e();dn=class{constructor(e){this.options=e}static TransformerName="opencode";endPoint="/v1/chat/completions";resolveSessionId(e,t){let o=t?.req,r=o?.headers||{},s=i=>{let c=Object.keys(r).find(a=>a.toLowerCase()===i),l=c?r[c]:void 0;return typeof l=="string"&&l.trim()?l.trim():void 0};return s("x-opencode-session")||o?.clientContext?.stableSessionId||o?.sessionId||_e(e.metadata?.user_id)||s("session_id")||Xs()}async transformRequestIn(e,t,o){let r=e.reasoning?.enabled||e.messages.some(s=>s.thinking?.content);return e.messages.forEach(s=>{if(Array.isArray(s.content)&&s.content.forEach(i=>{i.type==="image_url"&&(i.image_url.url.startsWith("http")||(i.image_url.url=`data:${i.media_type};base64,${i.image_url.url}`),delete i.media_type)}),s.role==="assistant"){let i=s.thinking?.content,c=s.thinking?.signature;i&&typeof i=="string"&&i.trim()?(s.reasoning_content=i,c&&(s.reasoning_content_signature=c)):r&&s.tool_calls?.length&&!s.reasoning_content&&(s.reasoning_content=" "),s.thinking&&delete s.thinking}}),Object.assign(e,this.options||{}),{body:e,config:{headers:{"x-opencode-session":this.resolveSessionId(e,o)}}}}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return t.choices?.[0]?.message?.reasoning_content&&(t.choices[0].message.thinking={content:t.choices[0].message.reasoning_content},delete t.choices[0].message.reasoning_content),new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,c=!1,l="",a=new ReadableStream({async start(u){let d=e.body.getReader(),f=(y,m,p)=>{let _=y.split(`
284
+ `);for(let k of _)k.trim()&&m.enqueue(p.encode(k+`
285
+ `))},g=(y,m)=>{let{controller:p,encoder:_}=m;if(y.startsWith("data: ")&&y.trim()!=="data: [DONE]")try{let k=JSON.parse(y.slice(6));if(k.usage&&(k.choices[0].finish_reason=c?"tool_calls":"stop"),k.choices?.[0]?.finish_reason==="error"){p.enqueue(_.encode(`data: ${JSON.stringify({error:k.choices?.[0]?.error})}
286
286
 
287
- `));return}k.choices?.[0]?.delta?.content&&!p.hasTextContent()&&p.setHasTextContent(!0);let h=k.choices?.[0]?.delta?.reasoning||k.choices?.[0]?.delta?.reasoning_content;if(h){p.appendReasoningContent(h);let S={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,thinking:{content:h}}}]};S.choices?.[0]?.delta&&(delete S.choices[0].delta.reasoning,delete S.choices[0].delta.reasoning_content),g.enqueue(_.encode(`data: ${JSON.stringify(S)}
287
+ `));return}k.choices?.[0]?.delta?.content&&!m.hasTextContent()&&m.setHasTextContent(!0);let h=k.choices?.[0]?.delta?.reasoning||k.choices?.[0]?.delta?.reasoning_content;if(h){m.appendReasoningContent(h);let S={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,thinking:{content:h}}}]};S.choices?.[0]?.delta&&(delete S.choices[0].delta.reasoning,delete S.choices[0].delta.reasoning_content),p.enqueue(_.encode(`data: ${JSON.stringify(S)}
288
288
 
289
- `));return}if(k.choices?.[0]?.delta?.content&&p.reasoningContent()&&!p.isReasoningComplete()){p.setReasoningComplete(!0);let S="ccr_think_signature",v=k.choices[0].delta.content,P={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:null,thinking:{content:p.reasoningContent(),signature:S}}}]};P.choices?.[0]?.delta&&(delete P.choices[0].delta.reasoning,delete P.choices[0].delta.reasoning_content),g.enqueue(_.encode(`data: ${JSON.stringify(P)}
289
+ `));return}if(k.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let S="ccr_think_signature",v=k.choices[0].delta.content,P={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:S}}}]};P.choices?.[0]?.delta&&(delete P.choices[0].delta.reasoning,delete P.choices[0].delta.reasoning_content),p.enqueue(_.encode(`data: ${JSON.stringify(P)}
290
290
 
291
- `));let R={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:v}}]};R.choices?.[0]?.delta&&(delete R.choices[0].delta.reasoning,delete R.choices[0].delta.reasoning_content),g.enqueue(_.encode(`data: ${JSON.stringify(R)}
291
+ `));let R={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:v}}]};R.choices?.[0]?.delta&&(delete R.choices[0].delta.reasoning,delete R.choices[0].delta.reasoning_content),p.enqueue(_.encode(`data: ${JSON.stringify(R)}
292
292
 
293
- `));return}k.choices?.[0]?.delta?.reasoning&&delete k.choices[0].delta.reasoning,k.choices?.[0]?.delta?.reasoning_content&&delete k.choices[0].delta.reasoning_content,k.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(k.choices?.[0]?.delta?.tool_calls[0].id,10))&&k.choices?.[0]?.delta?.tool_calls.forEach(S=>{S.id=`call_${Vs()}`}),k.choices?.[0]?.delta?.tool_calls?.length&&!c&&(c=!0);let b=k.choices?.[0]?.delta,T=b&&Object.keys(b).length>0,x=k.choices?.[0]?.finish_reason,C=k.usage;(T||x||C)&&g.enqueue(_.encode(`data: ${JSON.stringify(k)}
293
+ `));return}k.choices?.[0]?.delta?.reasoning&&delete k.choices[0].delta.reasoning,k.choices?.[0]?.delta?.reasoning_content&&delete k.choices[0].delta.reasoning_content,k.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(k.choices?.[0]?.delta?.tool_calls[0].id,10))&&k.choices?.[0]?.delta?.tool_calls.forEach(S=>{S.id=`call_${Xs()}`}),k.choices?.[0]?.delta?.tool_calls?.length&&!c&&(c=!0);let b=k.choices?.[0]?.delta,T=b&&Object.keys(b).length>0,x=k.choices?.[0]?.finish_reason,C=k.usage;(T||x||C)&&p.enqueue(_.encode(`data: ${JSON.stringify(k)}
294
294
 
295
- `))}catch{g.enqueue(_.encode(y+`
296
- `))}else g.enqueue(_.encode(y+`
297
- `))};try{for(;;){let{done:y,value:p}=await d.read();if(y){l.trim()&&m(l,u,o);break}if(!p||p.length===0)continue;let g;try{g=t.decode(p,{stream:!0})}catch(k){console.warn("Failed to decode chunk",k);continue}if(g.length===0)continue;if(l+=g,l.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let k=l.split(`
298
- `);l=k.pop()||"";for(let h of k)if(h.trim())try{f(h,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:b=>r=b,reasoningContent:()=>s,appendReasoningContent:b=>s+=b,isReasoningComplete:()=>i,setReasoningComplete:b=>i=b})}catch(b){console.error("Error processing line:",h,b),u.enqueue(o.encode(h+`
295
+ `))}catch{p.enqueue(_.encode(y+`
296
+ `))}else p.enqueue(_.encode(y+`
297
+ `))};try{for(;;){let{done:y,value:m}=await d.read();if(y){l.trim()&&f(l,u,o);break}if(!m||m.length===0)continue;let p;try{p=t.decode(m,{stream:!0})}catch(k){console.warn("Failed to decode chunk",k);continue}if(p.length===0)continue;if(l+=p,l.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let k=l.split(`
298
+ `);l=k.pop()||"";for(let h of k)if(h.trim())try{g(h,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:b=>r=b,reasoningContent:()=>s,appendReasoningContent:b=>s+=b,isReasoningComplete:()=>i,setReasoningComplete:b=>i=b})}catch(b){console.error("Error processing line:",h,b),u.enqueue(o.encode(h+`
299
299
  `))}continue}let _=l.split(`
300
- `);l=_.pop()||"";for(let k of _)if(k.trim())try{f(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:h=>r=h,reasoningContent:()=>s,appendReasoningContent:h=>s+=h,isReasoningComplete:()=>i,setReasoningComplete:h=>i=h})}catch(h){console.error("Error processing line:",k,h),u.enqueue(o.encode(k+`
301
- `))}}}catch(y){console.error("Stream error:",y),u.error(y)}finally{try{d.releaseLock()}catch(y){console.error("Error releasing reader lock:",y)}u.close()}}});return new Response(a,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var Xs,qs=w(()=>{Cs();xs();Ss();Rs();ws();Ps();Es();Os();As();Ms();$s();Ns();Ls();Hs();zs();Js();Bs();Ws();Gs();to();Ks();Qs();Ys();Xs={AnthropicTransformer:jt,GeminiTransformer:Bt,VertexGeminiTransformer:Wt,VertexClaudeTransformer:nn,DeepseekTransformer:Gt,TooluseTransformer:Kt,OpenrouterTransformer:Qt,OpenAITransformer:sn,MaxTokenTransformer:Vt,GroqTransformer:Yt,CleancacheTransformer:Xt,EnhanceToolTransformer:qt,ReasoningTransformer:Zt,SamplingTransformer:en,MaxCompletionTokens:tn,CerebrasTransformer:on,StreamOptionsTransformer:rn,CustomParamsTransformer:an,VercelTransformer:cn,OpenAIResponsesTransformer:Ee,ForceReasoningTransformer:ln,KimiTransformer:un,OpenCodeTransformer:dn}});var Ue,ho=w(()=>{"use strict";qs();Ue=class{constructor(e,t){this.configService=e;this.logger=t}transformers=new Map;registerTransformer(e,t){this.transformers.set(e,t),this.logger.info(`register transformer: ${e}${t.endPoint?` (endpoint: ${t.endPoint})`:" (no endpoint)"}`)}getTransformer(e){return this.transformers.get(e)}getAllTransformers(){return new Map(this.transformers)}getTransformersWithEndpoint(){let e=[];return this.transformers.forEach((t,o)=>{typeof t=="object"&&t.endPoint&&e.push({name:o,transformer:t})}),e}getTransformersWithoutEndpoint(){let e=[];return this.transformers.forEach((t,o)=>{typeof t=="object"&&!t.endPoint&&e.push({name:o,transformer:t})}),e}removeTransformer(e){return this.transformers.delete(e)}hasTransformer(e){return this.transformers.has(e)}async registerTransformerFromConfig(e){try{if(e.path){let t=Ve(Ve.resolve(e.path));if(t){let o=new t(e.options);if(o&&typeof o=="object"&&(o.logger=this.logger),!o.name)throw new Error(`Transformer instance from ${e.path} does not have a name property.`);return this.registerTransformer(o.name,o),!0}}return!1}catch(t){return this.logger.error(`load transformer (${e.path})
300
+ `);l=_.pop()||"";for(let k of _)if(k.trim())try{g(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:h=>r=h,reasoningContent:()=>s,appendReasoningContent:h=>s+=h,isReasoningComplete:()=>i,setReasoningComplete:h=>i=h})}catch(h){console.error("Error processing line:",k,h),u.enqueue(o.encode(k+`
301
+ `))}}}catch(y){console.error("Stream error:",y),u.error(y)}finally{try{d.releaseLock()}catch(y){console.error("Error releasing reader lock:",y)}u.close()}}});return new Response(a,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});function Gc(n){let e="";try{e=new URL(String(n||"")).pathname.replace(/\/+$/,"")}catch{return"chat"}return e.endsWith("/messages")?"anthropic":e.endsWith("/responses")?"responses":"chat"}var pn,qs=w(()=>{"use strict";pn=class{constructor(e){this.options=e}static TransformerName="defaultthinking";async transformRequestIn(e,t){let o=this.options?.level??this.options?.value;return!o||o==="none"||e.reasoning||(Gc(t?.baseUrl)==="chat"?e.reasoning_effort=o:e.reasoning={enabled:!0,effort:o}),e}}});var ei,ti=w(()=>{Ss();Rs();ws();Ps();Es();Os();As();Is();Ms();Ns();Ls();Ds();Fs();Bs();Js();Ws();Gs();Ks();Qs();no();Vs();Ys();Zs();qs();ei={AnthropicTransformer:jt,GeminiTransformer:Jt,VertexGeminiTransformer:Wt,VertexClaudeTransformer:nn,DeepseekTransformer:Gt,TooluseTransformer:Kt,OpenrouterTransformer:Qt,OpenAITransformer:sn,MaxTokenTransformer:Vt,GroqTransformer:Yt,CleancacheTransformer:Xt,EnhanceToolTransformer:Zt,ReasoningTransformer:qt,SamplingTransformer:en,MaxCompletionTokens:tn,CerebrasTransformer:on,StreamOptionsTransformer:rn,CustomParamsTransformer:an,VercelTransformer:cn,OpenAIResponsesTransformer:Oe,ForceReasoningTransformer:ln,KimiTransformer:un,OpenCodeTransformer:dn,DefaultThinkingTransformer:pn}});var je,_o=w(()=>{"use strict";ti();je=class{constructor(e,t){this.configService=e;this.logger=t}transformers=new Map;registerTransformer(e,t){this.transformers.set(e,t),this.logger.info(`register transformer: ${e}${t.endPoint?` (endpoint: ${t.endPoint})`:" (no endpoint)"}`)}getTransformer(e){return this.transformers.get(e)}getAllTransformers(){return new Map(this.transformers)}getTransformersWithEndpoint(){let e=[];return this.transformers.forEach((t,o)=>{typeof t=="object"&&t.endPoint&&e.push({name:o,transformer:t})}),e}getTransformersWithoutEndpoint(){let e=[];return this.transformers.forEach((t,o)=>{typeof t=="object"&&!t.endPoint&&e.push({name:o,transformer:t})}),e}removeTransformer(e){return this.transformers.delete(e)}hasTransformer(e){return this.transformers.has(e)}async registerTransformerFromConfig(e){try{if(e.path){let t=Ve(Ve.resolve(e.path));if(t){let o=new t(e.options);if(o&&typeof o=="object"&&(o.logger=this.logger),!o.name)throw new Error(`Transformer instance from ${e.path} does not have a name property.`);return this.registerTransformer(o.name,o),!0}}return!1}catch(t){return this.logger.error(`load transformer (${e.path})
302
302
  error: ${t.message}
303
303
  stack: ${t.stack}`),!1}}async initialize(){try{await this.registerDefaultTransformersInternal(),await this.loadFromConfig()}catch(e){this.logger.error(`TransformerService init error: ${e.message}
304
- Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(Xs).forEach(e=>{if("TransformerName"in e&&typeof e.TransformerName=="string")this.registerTransformer(e.TransformerName,e);else{let t=new e;t&&typeof t=="object"&&(t.logger=this.logger),this.registerTransformer(t.name,t)}})}catch(e){this.logger.error({error:e},"transformer regist error:")}}async loadFromConfig(){let e=this.configService.get("transformers",[]);for(let t of e)await this.registerTransformerFromConfig(t)}}});import{get_encoding as Jc}from"tiktoken";var it,Zs=w(()=>{"use strict";it=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=Jc(e)}catch{throw new Error(`Failed to initialize tiktoken encoding: ${e}`)}}async initialize(){if(!this.encoding)throw new Error("Tiktoken encoding not initialized")}async countTokens(e){let t=this.encoding;if(!t)throw new Error("Encoding not initialized");let o=0,{messages:r,system:s,tools:i}=e;return Array.isArray(r)&&r.forEach(c=>{typeof c.content=="string"?o+=t.encode(c.content).length:Array.isArray(c.content)&&c.content.forEach(l=>{if(l.type==="text")o+=t.encode(l.text).length;else if(l.type==="tool_use")o+=t.encode(JSON.stringify(l.input)).length;else if(l.type==="tool_result"){let a=typeof l.content=="string"?l.content:JSON.stringify(l.content);o+=t.encode(a).length}})}),typeof s=="string"?o+=t.encode(s).length:Array.isArray(s)&&s.forEach(c=>{c.type==="text"&&(typeof c.text=="string"?o+=t.encode(c.text).length:Array.isArray(c.text)&&c.text.forEach(l=>{o+=t.encode(l||"").length}))}),i&&i.forEach(c=>{c.description&&(o+=t.encode(c.name+c.description).length),c.input_schema&&(o+=t.encode(JSON.stringify(c.input_schema)).length)}),o}isInitialized(){return this.encoding!==void 0}encodeText(e){let t=this.encoding;if(!t)throw new Error("Encoding not initialized");return Array.from(t.encode(e))}dispose(){this.encoding&&(this.encoding.free(),this.encoding=void 0)}}});import{join as pn}from"path";import{homedir as Bc}from"os";import{existsSync as yo,mkdirSync as Wc}from"fs";import{promises as fn}from"fs";import{Tokenizer as Gc}from"@huggingface/tokenizers";var mn,ei=w(()=>{"use strict";mn=class{type="huggingface";name;modelId;logger;options;tokenizer=null;cacheDir;safeModelName;constructor(e,t,o={}){this.modelId=e,this.logger=t,this.options=o,this.cacheDir=o.cacheDir||pn(Bc(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=pn(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:pn(e,"tokenizer.json"),tokenizerConfig:pn(e,"tokenizer_config.json")}}ensureDir(e){yo(e)||Wc(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!yo(e.tokenizerJson)||!yo(e.tokenizerConfig))return null;let[t,o]=await Promise.all([fn.readFile(e.tokenizerJson,"utf-8"),fn.readFile(e.tokenizerConfig,"utf-8")]);return{tokenizerJson:JSON.parse(t),tokenizerConfig:JSON.parse(o)}}catch(e){return this.logger?.warn(`Failed to load from cache: ${e.message}`),null}}async downloadAndCache(){let e=this.getCachePaths(),t={json:`https://huggingface.co/${this.modelId}/resolve/main/tokenizer.json`,config:`https://huggingface.co/${this.modelId}/resolve/main/tokenizer_config.json`};this.logger?.info(`Downloading tokenizer files for ${this.modelId}`);let o=new AbortController,r=setTimeout(()=>o.abort(),this.options.timeout||3e4);try{let[s,i]=await Promise.all([fetch(t.json,{signal:o.signal}),fetch(t.config,{signal:o.signal})]);if(!s.ok)throw new Error(`Failed to fetch tokenizer.json: ${s.statusText}`);let[c,l]=await Promise.all([s.json(),i.ok?i.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([fn.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),fn.writeFile(e.tokenizerConfig,JSON.stringify(l,null,2))]),{tokenizerJson:c,tokenizerConfig:l}}finally{clearTimeout(r)}}async initialize(){try{this.logger?.info(`Initializing HuggingFace tokenizer: ${this.modelId}`);let e=this.getCachePaths();this.ensureDir(this.cacheDir);let t=await this.loadFromCache()||await this.downloadAndCache();this.tokenizer=new Gc(t.tokenizerJson,t.tokenizerConfig),this.logger?.info(`Tokenizer initialized: ${this.name}`)}catch(e){throw this.logger?.error(`Failed to initialize tokenizer: ${e.message}`),new Error(`Failed to initialize HuggingFace tokenizer for ${this.modelId}: ${e.message}`)}}async countTokens(e){if(!this.tokenizer)throw new Error("Tokenizer not initialized");try{let t=this.extractTextFromRequest(e);return this.tokenizer.encode(t).ids.length}catch(t){throw this.logger?.error(`Error counting tokens: ${t.message}`),t}}isInitialized(){return this.tokenizer!==null}encodeText(e){if(!this.tokenizer)throw new Error("Tokenizer not initialized");return this.tokenizer.encode(e).ids}dispose(){this.tokenizer=null}extractTextFromRequest(e){let t=[],{messages:o,system:r,tools:s}=e;if(Array.isArray(o)){for(let i of o)if(typeof i.content=="string")t.push(i.content);else if(Array.isArray(i.content))for(let c of i.content)c.type==="text"&&c.text?t.push(c.text):c.type==="tool_use"&&c.input?t.push(JSON.stringify(c.input)):c.type==="tool_result"&&t.push(typeof c.content=="string"?c.content:JSON.stringify(c.content))}if(typeof r=="string")t.push(r);else if(Array.isArray(r)){for(let i of r)if(i.type==="text"){if(typeof i.text=="string")t.push(i.text);else if(Array.isArray(i.text))for(let c of i.text)c&&t.push(c)}}if(s)for(let i of s)i.name&&t.push(i.name),i.description&&t.push(i.description),i.input_schema&&t.push(JSON.stringify(i.input_schema));return t.join(" ")}}});var gn,ti=w(()=>{"use strict";ne();gn=class{type="api";name;config;logger;options;constructor(e,t,o={}){if(!e.url||!e.apiKey)throw new Error("API tokenizer requires url and apiKey");this.config={url:e.url,apiKey:e.apiKey,requestFormat:e.requestFormat||"standard",responseField:e.responseField||"token_count",headers:e.headers||{}},this.logger=t,this.options=o;try{let r=new URL(e.url);this.name=`api-${r.hostname}`}catch{this.name=`api-${e.url}`}}async initialize(){try{new URL(this.config.url)}catch{throw new Error(`Invalid API URL: ${this.config.url}`)}}async countTokens(e){try{let t=this.formatRequestBody(e),o={"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`,...this.config.headers},r=new AbortController,s=setTimeout(()=>r.abort(),this.options.timeout||3e4),i={method:"POST",headers:o,body:JSON.stringify(t),signal:r.signal},c=this.options.resolveProxyUrl?.();c&&(i.dispatcher=G(c));let l=await fetch(this.config.url,i);if(clearTimeout(s),!l.ok)throw new Error(`API tokenizer request failed: ${l.status} ${l.statusText}`);let a=await l.json();return this.extractTokenCount(a)}catch(t){throw t.name==="AbortError"?new Error("API tokenizer request timed out"):t}}isInitialized(){return!0}dispose(){}formatRequestBody(e){switch(this.config.requestFormat){case"standard":return e;case"openai":return{model:"gpt-3.5-turbo",messages:this.extractMessagesAsOpenAIFormat(e)};case"anthropic":return{messages:e.messages||[],system:e.system,tools:e.tools};case"custom":return{text:this.extractConcatenatedText(e)};default:return e}}extractMessagesAsOpenAIFormat(e){return e.messages?e.messages.map(t=>({role:t.role,content:this.extractTextFromMessage(t)})):[]}extractTextFromMessage(e){return typeof e.content=="string"?e.content:Array.isArray(e.content)?e.content.map(t=>t.type==="text"&&t.text?t.text:t.type==="tool_use"&&t.input?JSON.stringify(t.input):t.type==="tool_result"?typeof t.content=="string"?t.content:JSON.stringify(t.content):"").join(" "):""}extractConcatenatedText(e){let t=[];return e.messages&&e.messages.forEach(o=>{t.push(this.extractTextFromMessage(o))}),typeof e.system=="string"?t.push(e.system):Array.isArray(e.system)&&e.system.forEach(o=>{o.type==="text"&&(typeof o.text=="string"?t.push(o.text):Array.isArray(o.text)&&o.text.forEach(r=>{r&&t.push(r)}))}),e.tools&&e.tools.forEach(o=>{o.name&&t.push(o.name),o.description&&t.push(o.description),o.input_schema&&t.push(JSON.stringify(o.input_schema))}),t.join(" ")}extractTokenCount(e){try{let t=this.config.responseField,o=t.split("."),r=e;for(let s of o){if(r==null)throw new Error(`Field path '${t}' not found in response`);r=r[s]}if(typeof r!="number")throw new Error(`Expected number at field path '${t}', got ${typeof r}`);return r}catch(t){throw this.logger?.error(`Failed to extract token count from API response: ${t.message}. Response: ${JSON.stringify(e)}`),new Error(`Invalid response from API tokenizer: ${t.message}`)}}}});var je,_o=w(()=>{"use strict";Zs();ei();ti();ne();je=class{tokenizers=new Map;configService;logger;options;fallbackTokenizer;constructor(e,t,o={}){this.configService=e,this.logger=t,this.options={timeout:o.timeout??3e4,...o}}async initialize(){try{this.fallbackTokenizer=new it("cl100k_base"),await this.fallbackTokenizer.initialize(),this.tokenizers.set("fallback",this.fallbackTokenizer),this.logger?.info("TokenizerService initialized successfully")}catch(e){throw this.logger?.error(`TokenizerService initialization error: ${e.message}`),e}}async getTokenizer(e,t){let o=this.getCacheKey(e,t);if(this.tokenizers.has(o))return this.tokenizers.get(o);let r;try{switch(e.type){case"tiktoken":r=new it(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),r=new mn(e.model,this.logger,{timeout:this.options.timeout});break;case"api":r=new gn(e,this.logger,{timeout:this.options.timeout,resolveProxyUrl:()=>{let s=t?this.getRawProvider(t):void 0;return ce(this.configService,s)}});break;default:throw new Error(`Unknown tokenizer type: ${e.type}`)}return this.logger?.info(`Calling initialize() on ${e.type} tokenizer...`),await r.initialize(),this.tokenizers.set(o,r),this.logger?.info(`Tokenizer initialized successfully: ${e.type} (${o})`),r}catch(s){return this.logger?.error(`Failed to initialize ${e.type} tokenizer: ${s.message}`),this.logger?.error(`Error stack: ${s.stack}`),this.fallbackTokenizer||await this.initialize(),this.fallbackTokenizer}}async countTokens(e,t,o){let r=t?await this.getTokenizer(t,o):this.fallbackTokenizer;return{tokenCount:await r.countTokens(e),tokenizerUsed:r.name,cached:!1}}getTokenizerConfigForModel(e,t){let r=(this.configService.get("providers")||[]).find(s=>s.name===e);if(r?.tokenizer)return r.tokenizer.models?.[t]?r.tokenizer.models[t]:r.tokenizer.default}dispose(){this.tokenizers.forEach(e=>{try{e.dispose()}catch(t){this.logger?.error(`Error disposing tokenizer: ${t}`)}}),this.tokenizers.clear()}getCacheKey(e,t){switch(e.type){case"tiktoken":return`tiktoken:${e.encoding||"cl100k_base"}`;case"huggingface":return`hf:${e.model}`;case"api":return`api:${t||"unscoped"}:${e.url}`;default:return`unknown:${JSON.stringify(e)}`}}getRawProvider(e){return(this.configService.get("providers")||[]).find(o=>o?.name===e)}}});function ni(n){if(!n||typeof n!="object"||Zc(n))return null;for(let t of qc(n)){let o=Xc(t);if(o)return o}return null}function Xc(n){if(!n||typeof n!="object")return null;let e={},t=hn(n,Kc);if(t!==void 0){let i=oi(t);e.usedDailyBalance=i,e.limitDaily=100}let o=hn(n,Qc);if(o!==void 0){let i=oi(o);e.usedBalance=i,e.totalBalance=100}let r=hn(n,Vc);if(r!==void 0){let i=ri(r);i&&(e.resetTime=i)}let s=hn(n,Yc);if(s!==void 0){let i=ri(s);i&&(e.resetTime7d=i,e.resetTime||(e.resetTime=i))}return e.usedDailyBalance===void 0&&e.limitDaily===void 0&&e.usedBalance===void 0&&e.totalBalance===void 0&&e.resetTime===void 0?null:e}function oi(n){return!Number.isFinite(n)||n<=0?0:Math.round(n*1e4)/100}function hn(n,e){for(let t of e){let o=M(n?.[t]);if(o!==void 0)return o}}function qc(n){let e=[],t=new WeakSet,o=["data","Data","DataV2"],r=s=>{if(!(!s||typeof s!="object")&&!t.has(s)){t.add(s),e.push(s);for(let i of o)s[i]&&typeof s[i]=="object"&&r(s[i])}};return r(n),e}function Zc(n){let e=n?.data;return e&&typeof e=="object"&&(e.success===!1||e.errorCode||e.errorMsg)?String(e.errorCode||e.errorMsg||"gateway-error"):null}function ri(n){if(typeof n!="string"&&typeof n!="number")return;let e=n;if(typeof n=="number"&&n<1e12&&(e=n*1e3),typeof n=="string"){let o=Number(n.trim());Number.isFinite(o)&&o<1e12&&(e=o*1e3)}let t=new Date(e);return isNaN(t.getTime())?void 0:t.toISOString()}function Oo(n){let e=Ao(n);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?al:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?cl:e==="deepseek.com"||e.endsWith(".deepseek.com")?el:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?tl:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?nl:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?ol:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?rl:e==="maas.aliyuncs.com"||e.endsWith(".maas.aliyuncs.com")?sl:e==="xf-yun.com"||e.endsWith(".xf-yun.com")||e==="xfyun.cn"||e.endsWith(".xfyun.cn")||e==="xfyun.com"||e.endsWith(".xfyun.com")?il:null:null}function ll(n){let e=Ao(n);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function ul(n,e){let t=Po(n);if(t.length===0)return null;let o=String(e||"").split(":").map(s=>s.trim()).filter(Boolean);return t.find(s=>{let i=s?.codingPlanAppCredentialDTO||s?.appCredential||s,c=String(i?.apiKey||i?.api_key||"").trim();return c&&o.some(l=>l.includes(c)||c.includes(l))})||t[0]}function Po(n){return!n||typeof n!="object"?[]:!Array.isArray(n)&&n.codingPlanUsageDTO?[n]:Array.isArray(n)?n.flatMap(e=>Po(e)):Object.values(n).flatMap(e=>Po(e))}function Eo(n,e){let t=n.match(new RegExp(`(?:^|;\\s*)${e}=([^;]+)`));return t?decodeURIComponent(t[1]):void 0}function Ao(n){try{return new URL(n).hostname.toLowerCase()}catch{return null}}function M(n){if(typeof n=="number")return Number.isFinite(n)?n:void 0;if(typeof n!="string")return;let e=n.trim().replace(/,/g,"");if(!e)return;let t=Number(e);return Number.isFinite(t)?t:void 0}var re,ko,bo,To,vo,Co,Kc,Qc,Vc,Yc,xo,So,Ro,wo,el,tl,nl,ol,rl,sl,il,al,cl,Io=w(()=>{"use strict";ne();re=class{async fetchJson(e,t,o,r,s){let i=s||t.apiKey;if(!i)return null;let c={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${i}`},signal:AbortSignal.timeout(o)};try{r&&(c.dispatcher=G(r));let l=await fetch(e,c);return l.ok?await l.json():null}catch{return null}}hasQuotaData(e){return e.totalBalance!==void 0||e.usedBalance!==void 0||e.remainingBalance!==void 0||e.usedDailyBalance!==void 0||e.limitDaily!==void 0||e.resetTime!==void 0}},ko=class extends re{async queryQuota(e,t,o){let r=await this.fetchJson("https://api.deepseek.com/user/balance",e,t,o),i=(Array.isArray(r?.balance_infos)?r.balance_infos:[]).find(l=>M(l?.total_balance)!==void 0);if(!i)return null;let c=M(i.total_balance);return c===void 0?null:{totalBalance:c,currency:typeof i.currency=="string"?i.currency:void 0}}},bo=class extends re{async queryQuota(e,t,o){let s=(await this.fetchJson("https://openrouter.ai/api/v1/key",e,t,o))?.data;if(!s||typeof s!="object")return null;let i={},c=M(s.limit),l=M(s.limit_remaining),a=M(s.usage),u=M(s.usage_daily);return c!==void 0&&(i.totalBalance=c),l!==void 0&&(i.remainingBalance=l),a!==void 0&&(i.usedBalance=a),u!==void 0&&(i.usedDailyBalance=u),this.hasQuotaData(i)?i:null}},To=class extends re{async queryQuota(e,t,o){let r=ll(e.baseUrl),i=(await this.fetchJson(r,e,t,o))?.data;if(!i||typeof i!="object")return null;let c={},l=M(i.totalBalance),a=M(i.balance);return l!==void 0&&(c.totalBalance=l),a!==void 0&&(c.remainingBalance=a),l!==void 0&&a!==void 0&&(c.usedBalance=Math.max(0,l-a)),this.hasQuotaData(c)?c:null}},vo=class extends re{async queryQuota(e,t,o){if(!e.apiKey)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:e.apiKey.trim()},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://api.z.ai/api/monitor/usage/quota/limit",s);if(!i.ok)return null;let c=await i.json(),l=Array.isArray(c?.data?.limits)?c.data.limits:[],a=null,u=null,d=null;for(let f of l)f.type==="TOKENS_LIMIT"?f.unit===3?a=f:f.unit===6?u=f:a||(a=f):f.type==="TIME_LIMIT"&&(d=f);let m={};if(a){let f=M(a.currentValue),y=M(a.usage),p=M(a.remaining),g=M(a.percentage);f!==void 0?m.usedDailyBalance=f:g!==void 0&&(m.usedDailyBalance=g,m.limitDaily=100),y!==void 0?m.limitDaily=y:f!==void 0&&p!==void 0&&(m.limitDaily=f+p),a.nextResetTime&&(m.resetTime=new Date(a.nextResetTime).toISOString())}if(u){let f=M(u.currentValue),y=M(u.usage),p=M(u.remaining),g=M(u.percentage);f!==void 0?m.usedBalance=f:g!==void 0&&(m.usedBalance=g,m.totalBalance=100),y!==void 0?m.totalBalance=y:f!==void 0&&p!==void 0&&(m.totalBalance=f+p),u.nextResetTime&&!m.resetTime&&(m.resetTime=new Date(u.nextResetTime).toISOString())}if(d){if(m.usedDailyBalance===void 0){let f=M(d.currentValue);f!==void 0&&(m.usedDailyBalance=f)}if(m.limitDaily===void 0){let f=M(d.usage),y=M(d.currentValue),p=M(d.remaining);f!==void 0?m.limitDaily=f:y!==void 0&&p!==void 0&&(m.limitDaily=y+p)}d.nextResetTime&&!m.resetTime&&(m.resetTime=new Date(d.nextResetTime).toISOString())}return this.hasQuotaData(m)?m:null}catch{return null}}},Co=class extends re{async queryQuota(e,t,o){if(!e.quotaToken)return null;let r={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://bailian.console.aliyun.com",Referer:"https://bailian.console.aliyun.com/cn-beijing",Cookie:e.quotaToken},signal:AbortSignal.timeout(t)},s=JSON.stringify({Api:"zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",V:"1.0",Data:{queryCodingPlanInstanceInfoRequest:{commodityCode:"sfm_codingplan_public_cn",onlyLatestOne:!0},cornerstoneParam:{feTraceId:`ccr-${Date.now()}`,feURL:"https://bailian.console.aliyun.com/cn-beijing",protocol:"V2",console:"ONE_CONSOLE",productCode:"p_efm",switchAgent:10736808,switchUserType:3,domain:"bailian.console.aliyun.com",consoleSite:"BAILIAN_ALIYUN",userNickName:"",userPrincipalName:"",xsp_lang:"zh-CN"}}}),i=new URLSearchParams({params:s,region:"cn-beijing"}).toString();try{o&&(r.dispatcher=G(o));let c=await fetch("https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",{...r,body:i});if(!c.ok)return null;let a=(await c.json())?.data?.DataV2?.data?.data?.codingPlanInstanceInfos;if(!Array.isArray(a)||a.length===0)return null;let u=a[0]?.codingPlanQuotaInfo;if(!u)return null;let d={},m=M(u.per5HourUsedQuota),f=M(u.per5HourTotalQuota);m!==void 0&&(d.usedDailyBalance=m),f!==void 0&&(d.limitDaily=f);let y=M(u.perWeekUsedQuota),p=M(u.perWeekTotalQuota);return y!==void 0&&(d.usedBalance=y),p!==void 0&&(d.totalBalance=p),this.hasQuotaData(d)?d:null}catch{return null}}};Kc=["per5HourPercentage","per5HourUsedPercentage","fiveHourPercentage"],Qc=["per1WeekPercentage","perWeekPercentage","per1WeekUsedPercentage"],Vc=["per5HourResetTime","per5HourNextResetTime","fiveHourResetTime"],Yc=["per1WeekResetTime","per1WeekNextResetTime","perWeekResetTime"];xo=class extends re{async queryQuota(e,t,o){let r=e.quotaToken?.trim();if(!r)return null;let s=e.quotaSecToken?.trim();if(s){try{let i=await this.queryOfficialEndpoint(r,s,t,o);if(i)return i}catch{}return this.queryLegacyEndpoint(r,t,o)}return this.queryLegacyEndpoint(r,t,o)}async queryOfficialEndpoint(e,t,o,r){let s="zeldaHttp.apikeyMgr./tokenplan/personal/api/v2/usage",i=Eo(e,"cna")||"",c=JSON.stringify({Api:s,V:"1.0",Data:{cornerstoneParam:{feTraceId:`ccr-${Date.now()}`,feURL:"https://bailian.console.aliyun.com/cn-beijing/?tab=plan#/efm/subscription/token-plan/personal",protocol:"V2",console:"ONE_CONSOLE",productCode:"p_efm",switchAgent:10736808,switchUserType:3,domain:"bailian.console.aliyun.com",consoleSite:"BAILIAN_ALIYUN",userNickName:"",userPrincipalName:"",xsp_lang:"zh-CN","X-Anonymous-Id":i}}}),l=new URLSearchParams({params:c,region:"cn-beijing",sec_token:t}).toString(),a={method:"POST",headers:{Accept:"application/json","Content-Type":"application/x-www-form-urlencoded",Cookie:e,"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://bailian.console.aliyun.com",Referer:"https://bailian.console.aliyun.com/cn-beijing/?tab=plan"},body:l,signal:AbortSignal.timeout(o)};try{r&&(a.dispatcher=G(r));let u=await fetch(`https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=${encodeURIComponent(s)}&_v=undefined`,a);if(!u.ok)return null;let d=await u.json();return ni(d)}catch{return null}}async queryLegacyEndpoint(e,t,o){let r="zeldaHttp.apikeyMgr./tokenplan/personal/api/v2/usage",s=JSON.stringify({Api:r,V:"1.0",Data:{cornerstoneParam:{feTraceId:`ccr-${Date.now()}`,feURL:"https://bailian.console.aliyun.com/",protocol:"V2",console:"ONE_CONSOLE",productCode:"p_efm",domain:"bailian.console.aliyun.com",consoleSite:"BAILIAN_ALIYUN",xsp_lang:"zh-CN"}}}),i=new URLSearchParams({params:s,region:"cn-beijing"}).toString(),c={method:"POST",headers:{Accept:"application/json","Content-Type":"application/x-www-form-urlencoded",Cookie:e,"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://bailian.console.aliyun.com",Referer:"https://bailian.console.aliyun.com/"},body:i,signal:AbortSignal.timeout(t)};try{o&&(c.dispatcher=G(o));let l=await fetch(`https://cs-data.qianwenai.com/data/api.json?product=sfm_bailian&action=BroadScopeAspnGateway&api=${encodeURIComponent(r)}`,c);if(!l.ok)return null;let a=await l.json();return ni(a)}catch{return null}}},So=class extends re{async queryQuota(e,t,o){if(!e.quotaToken)return null;let s={method:"GET",headers:{Accept:"application/json","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://maas.xfyun.cn",Referer:"https://maas.xfyun.cn/packageSubscription",Authorization:(Eo(e.quotaToken,"atp-auth-token")||Eo(e.quotaToken,"tenantToken")||e.quotaToken).trim(),Cookie:e.quotaToken},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://maas.xfyun.cn/api/v1/gpt-finetune/coding-plan/list?page=1&size=10",s);if(!i.ok)return null;let c=await i.json();if(c?.succeed===!1||c?.failed===!0)return null;let l=ul(c,e.apiKey),a=l?.codingPlanUsageDTO||l?.usage||l;if(!a||typeof a!="object")return null;let u={},d=M(a.rp5hUsage),m=M(a.rp5hLimit);d!==void 0&&(u.usedDailyBalance=d),m!==void 0&&(u.limitDaily=m);let f=M(a.rpwUsage),y=M(a.rpwLimit);return f!==void 0&&(u.usedBalance=f),y!==void 0&&(u.totalBalance=y),u.remainingBalance===void 0&&y!==void 0&&f!==void 0&&(u.remainingBalance=Math.max(0,y-f)),this.hasQuotaData(u)?u:null}catch{return null}}},Ro=class extends re{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${r.trim()}`},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://api.kimi.com/coding/v1/usages",s);if(!i.ok)return null;let c=await i.json(),l={},a=Array.isArray(c?.limits)?c.limits:[];if(a.length>0&&a[0]?.detail){let d=a[0].detail,m=M(d.limit),f=M(d.remaining),y=d.resetTime;m!==void 0&&f!==void 0&&(l.usedDailyBalance=Math.max(0,m-f),l.limitDaily=m),y&&(l.resetTime=new Date(y).toISOString())}let u=c?.usage;if(u){let d=M(u.limit),m=M(u.remaining),f=M(u.used),y=u.resetTime;d!==void 0&&(l.totalBalance=d,f!==void 0?l.usedBalance=f:m!==void 0&&(l.usedBalance=Math.max(0,d-m),l.remainingBalance=m)),y&&(l.resetTime7d=new Date(y).toISOString(),l.resetTime||(l.resetTime=l.resetTime7d))}return this.hasQuotaData(l)?l:null}catch{return null}}},wo=class extends re{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s=Ao(e.baseUrl)||"api.minimaxi.com",i=s.endsWith(".minimax.io")||s==="minimax.io"?"api.minimax.io":"api.minimaxi.com";try{let c=[`https://${i}/v1/token_plan/remains`,`https://${i}/v1/api/openplatform/coding_plan/remains`],l=null;for(let b of c)if(l=await this.fetchJson(b,e,t,o,r.trim()),l)break;if(!l)return null;if(l?.base_resp){let b=l.base_resp.status_code;if(b!==void 0&&b!==0)return null}let a=Array.isArray(l?.model_remains)?l.model_remains:[];if(a.length===0)return null;let u=a[0],d={},m=M(u.current_interval_total_count),f=M(u.current_interval_usage_count),y=M(u.current_interval_remaining_percent),p=M(u.end_time);m!==void 0&&f!==void 0&&m>0?(d.usedDailyBalance=Math.max(0,m-f),d.limitDaily=m):y!==void 0&&(d.limitDaily=100,d.usedDailyBalance=Math.max(0,100-y)),p&&(d.resetTime=new Date(p).toISOString());let g=M(u.current_weekly_total_count),_=M(u.current_weekly_usage_count),k=M(u.current_weekly_remaining_percent),h=M(u.weekly_end_time);return g!==void 0&&_!==void 0&&g>0?(d.usedBalance=Math.max(0,g-_),d.remainingBalance=_,d.totalBalance=g):k!==void 0&&(d.totalBalance=100,d.usedBalance=Math.max(0,100-k),d.remainingBalance=k),h&&!d.resetTime&&(d.resetTime=new Date(h).toISOString()),this.hasQuotaData(d)?d:null}catch{return null}}},el=new ko,tl=new bo,nl=new To,ol=new vo,rl=new Co,sl=new xo,il=new So,al=new Ro,cl=new wo});function si(n){return{[dl]:n,[pl]:Pt}}function ml(n){return n===!0||n==="true"||n===1||n==="1"}function gl(n){let e=n.getFullYear(),t=String(n.getMonth()+1).padStart(2,"0"),o=String(n.getDate()).padStart(2,"0");return`${e}-${t}-${o}`}function hl(n){try{let e=new URL(n),t=e.pathname;return t.endsWith("/v1/messages")?t=t.slice(0,-12):t.endsWith("/messages")?t=t.slice(0,-9):t.endsWith("/v1/chat/completions")?t=t.slice(0,-20):t.endsWith("/chat/completions")&&(t=t.slice(0,-17)),t.endsWith("/v1")||(t=t.endsWith("/")?`${t}v1`:`${t}/v1`),e.pathname=`${t}/models`,e.toString()}catch{return null}}function yl(n){try{let e=new URL(n).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function Mo(n,e,t){let o=performance.now(),r=()=>Math.max(0,Math.round(performance.now()-o)),s=hl(n.baseUrl);if(!s)return{success:!1,error:"Cannot derive models endpoint from baseUrl",errorKind:"network",latencyMs:r()};try{let i={method:"GET",headers:{Authorization:`Bearer ${n.apiKey}`},signal:AbortSignal.timeout(e)};t&&(i.dispatcher=G(t));let c=await fetch(s,i);if(c.headers&&Xe(n.name,n.baseUrl,c.headers),c.ok)return{success:!0,headers:c.headers,latencyMs:r()};if(c.status===429)return{success:!1,error:`HTTP 429: ${(await c.text().catch(()=>"")).slice(0,100)||"Rate limited"}`,headers:c.headers,latencyMs:r(),errorKind:"http"};if(c.status>=400&&c.status<500)return{success:!0,headers:c.headers,latencyMs:r()};let l=await c.text().catch(()=>"");return{success:!1,error:`HTTP ${c.status}: ${l.slice(0,100)}`,headers:c.headers,latencyMs:r(),errorKind:"http"}}catch(i){let c=i?.message||i?.toString()||"Unknown probe error",l=i?.name==="TimeoutError"||i?.name==="AbortError"||i?.code===20||i?.code==="ABORT_ERR";return{success:!1,error:c,latencyMs:r(),errorKind:l?"timeout":"network"}}}async function _l(n,e,t,o){let r=n.wakeupModel||(Array.isArray(n.models)?n.models[0]:void 0);if(!r)return{success:!1,error:"No models configured for provider"};let s=n.baseUrl,i=n.baseUrl.includes("/messages")||r.includes("claude");try{let c={"Content-Type":"application/json",...si("scheduled-wakeup")};i?(c["x-api-key"]=n.apiKey,c["anthropic-version"]="2023-06-01"):c.Authorization=`Bearer ${n.apiKey}`;let l={method:"POST",headers:c,body:JSON.stringify({model:r,messages:[{role:"user",content:"Wake up and acknowledge with a short greeting."}],max_tokens:10}),signal:AbortSignal.timeout(e)};t&&(l.dispatcher=G(t)),o?.info(`Sending scheduled wake-up call to provider ${n.name} using model ${r}...`);let a=await fetch(s,l);if(a.ok)return{success:!0};let u=await a.text().catch(()=>"");return{success:!1,error:`HTTP ${a.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(c){return{success:!1,error:c?.message||c?.toString()||"Unknown wake-up error"}}}async function kl(n,e,t,o,r){let s=n.baseUrl,i=n.baseUrl.includes("/messages")||e.includes("claude");try{let c={"Content-Type":"application/json",...si("rate-limit-probe")};i?(c["x-api-key"]=n.apiKey,c["anthropic-version"]="2023-06-01"):c.Authorization=`Bearer ${n.apiKey}`;let l={method:"POST",headers:c,body:JSON.stringify({model:e,messages:[{role:"user",content:"Hello, please reply briefly."}],max_tokens:10}),signal:AbortSignal.timeout(t)};o&&(l.dispatcher=G(o)),r?.debug?.(`Sending independent ping to rate-limited model ${n.name} (${e})...`);let a=await fetch(s,l);if(a.ok)return{success:!0};let u=await a.text().catch(()=>"");return{success:!1,error:`HTTP ${a.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(c){return{success:!1,error:c?.message||c?.toString()||"Unknown ping error"}}}function ii(n,e,t,o,r){return pe||(pe=new yn(n,e,t,o,r)),pe}function He(n,e,t,o,r){let s=ii(n,e,t,o,r);return s.start(),s}function $o(){pe&&pe.stop()}function _n(){pe&&(pe.stop(),pe=null)}var dl,pl,fl,yn,pe,kn=w(()=>{"use strict";oe();Re();Io();Ae();Wn();ne();dl="x-claude-code-router-source",pl="x-claude-code-router-version";fl={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,slowThresholdMs:3e3,initialDelayMs:5e3,excludeProviders:[]};yn=class{config;quotaProbeTimer;healthProbeTimer;wakeupTimer;rateLimitProbeTimer;lastWakeupDate=new Map;getProviders;resolveProxyUrl;logger;getConfig;running=!1;probeTelemetry=new Map;constructor(e,t,o,r,s){this.config={...fl,...t},this.getProviders=e,this.resolveProxyUrl=o,this.logger=r,this.getConfig=s}recordProbe(e,t,o){let r=Date.now(),s=t.success&&t.latencyMs>this.config.slowThresholdMs,i;t.success?i=s?"slow":"healthy":i=t.errorKind==="timeout"?"timeout":"error";let c=this.probeTelemetry.get(e),l={provider:e,latencyMs:t.latencyMs,status:i,isSlow:s,errorKind:t.errorKind,errorMessage:t.error,lastProbeAt:r,lastSuccessAt:t.success?r:c?.lastSuccessAt,source:o};return this.probeTelemetry.set(e,l),s&&this.logger?.warn?.(`Slow network detected for provider ${e}: probe took ${t.latencyMs} ms (threshold ${this.config.slowThresholdMs} ms)`),l}getProbeTelemetry(){return Array.from(this.probeTelemetry.values()).map(e=>({...e}))}getProviderProbeTelemetry(e){let t=this.probeTelemetry.get(e);return t?{...t}:void 0}start(){if(this.running)return;let e=this.isWakeupGloballyEnabled();!this.config.enabled&&!e||(this.running=!0,setTimeout(()=>{this.running&&(this.config.enabled&&(this.runQuotaProbe(),this.runHealthProbe(),this.runRateLimitProbe()),this.runScheduledWakeup())},this.config.initialDelayMs),this.config.enabled&&(this.quotaProbeTimer=setInterval(()=>this.runQuotaProbe(),this.config.quotaProbeIntervalMinutes*60*1e3),this.healthProbeTimer=setInterval(()=>this.runHealthProbe(),300*1e3),this.rateLimitProbeTimer=setInterval(()=>this.runRateLimitProbe(),120*1e3)),this.wakeupTimer=setInterval(()=>this.runScheduledWakeup(),60*1e3),this.logger?.info("Active probe service started"))}stop(){this.running=!1,this.quotaProbeTimer&&(clearInterval(this.quotaProbeTimer),this.quotaProbeTimer=void 0),this.healthProbeTimer&&(clearInterval(this.healthProbeTimer),this.healthProbeTimer=void 0),this.rateLimitProbeTimer&&(clearInterval(this.rateLimitProbeTimer),this.rateLimitProbeTimer=void 0),this.wakeupTimer&&(clearInterval(this.wakeupTimer),this.wakeupTimer=void 0),this.logger?.info("Active probe service stopped")}async runQuotaProbe(){let e=this.getProviders().filter(s=>s?.name&&s.enabled!==!1&&!this.config.excludeProviders.includes(s.name)),t=U();if(e.length===0)return;let o=[];for(let s of e){let i=Oo(s.baseUrl),c=Array.isArray(s.models)?s.models:[],l=this.resolveProxyUrl?.(s);if(!i){let a;try{a=new URL(s.baseUrl).hostname}catch{a=void 0}this.logger?.debug?.(`No quota adapter for provider ${s.name}`+(a?` (host: ${a})`:"")+" \u2014 skipping quota probe")}i&&o.push({provider:s.name,models:c,type:"quota-adapter",promise:i.queryQuota(s,this.config.probeTimeoutMs,l).then(a=>{if(a){so(s.name,a),this.logger?.debug?.(`Stored quota probe result for ${s.name}`);let u=a.limitDaily!==void 0&&a.usedDailyBalance!==void 0&&a.usedDailyBalance>=a.limitDaily,d=a.totalBalance!==void 0&&a.usedBalance!==void 0&&a.usedBalance>=a.totalBalance;if(u||d){let m=u?`Quota exhausted: 5h limit reached (${a.usedDailyBalance}/${a.limitDaily})`:`Quota exhausted: 7d balance depleted (${a.usedBalance}/${a.totalBalance})`;for(let f of c){if(t.getState(s.name,f)?.rateLimitUntil){this.logger?.debug?.(`Skip forceOpen for ${s.name} (${f}): already rate-limited with active cooldown`);continue}t.forceOpen(s.name,f,m)}this.logger?.warn?.(`${m} for ${s.name}, marked as unhealthy`)}else for(let m of c){let f=t.getState(s.name,m);f&&f.status==="open"&&f.lastError?.includes("Quota exhausted")&&(t.recover(s.name,m),this.logger?.info?.(`Quota recovered for ${s.name} (${m}), marked as healthy`))}}})}),yl(s.baseUrl)&&o.push({provider:s.name,models:c,type:"rate-limit-headers",promise:Mo(s,this.config.probeTimeoutMs,l).then(a=>{this.recordProbe(s.name,a,"rate-limit-headers")})})}if(o.length===0)return;this.logger?.debug?.(`Running quota probe with ${o.length} tasks for ${e.length} providers`);let r=await Promise.allSettled(o.map(s=>s.promise));for(let s=0;s<o.length;s++){let i=r[s];if(i.status==="rejected"){let c=o[s];this.logger?.warn?.(`Quota probe failed for ${c.provider} (${c.type}): ${i.reason}`)}}}async runHealthProbe(){let e=U(),t=this.getProviders().filter(r=>r?.name&&r.enabled!==!1&&!this.config.excludeProviders.includes(r.name));if(t.length===0)return;this.logger?.debug?.(`Running health probe for ${t.length} providers`);let o=await Promise.allSettled(t.map(r=>Mo(r,this.config.probeTimeoutMs,this.resolveProxyUrl?.(r))));for(let r=0;r<t.length;r++){let s=t[r],i=o[r],c=Array.isArray(s.models)?s.models:[];if(i.status==="fulfilled"){let l=i.value;if(this.recordProbe(s.name,l,"health"),l.success){let a=0;for(let u of c){let d=e.getState(s.name,u);if(d&&d.status==="open"){let m=d.lastError?.includes("Quota exhausted"),f=d.rateLimitUntil||d.lastError&&(d.lastError.includes("429")||d.lastError.toLowerCase().includes("rate limit")||d.lastError.toLowerCase().includes("rate_limit")||d.lastError.toLowerCase().includes("too many requests")||d.lastError.toLowerCase().includes("\u9650\u6D41"));if(m){this.logger?.debug?.(`Skipping health probe recovery for ${s.name} (${u}) because quota is exhausted`);continue}if(f||d.rateLimitUntil){this.logger?.debug?.(`Skipping health probe recovery for rate-limited ${s.name} (${u})`);continue}}e.recordSuccess(s.name,u),a++}a>0&&this.logger?.info?.(`Health probe succeeded for ${s.name}`)}else{for(let a of c)e.recordFailure(s.name,a,l.error);this.logger?.warn?.(`Health probe failed for ${s.name}: ${l.error}`)}}else{this.recordProbe(s.name,{success:!1,error:i.reason?.message||String(i.reason||"Probe error"),latencyMs:0,errorKind:"network"},"health");for(let l of c)e.recordFailure(s.name,l,i.reason?.message||"Probe error");this.logger?.warn?.(`Health probe error for ${s.name}: ${i.reason}`)}}}async runRateLimitProbe(){let e=U(),t=e.getRateLimitedModels();if(t.length===0)return;let o=this.getProviders(),r=this.config.probeTimeoutMs;this.logger?.debug?.(`Running rate-limit recovery probe for ${t.length} model(s)`);for(let s of t){let[i,...c]=s.split(","),l=c.join(",");if(!i||!l)continue;let a=e.getState(i,l);if(!a||!a.rateLimitUntil)continue;if(Date.now()>=a.rateLimitUntil){e.recover(i,l),this.logger?.info?.(`Rate-limit cooldown expired, auto-recovered ${s}`);continue}let u=o.find(d=>d?.name===i&&d.enabled!==!1);if(!u){this.logger?.warn?.(`Provider ${i} not found or disabled for rate-limit probe`);continue}try{let d=await kl(u,l,r,this.resolveProxyUrl?.(u),this.logger);if(d.success)this.logger?.info?.(`Rate-limit probe succeeded for ${s}, recovering model`),e.recover(i,l);else if(d.error&&(d.error.includes("429")||d.error.toLowerCase().includes("rate limit")||d.error.toLowerCase().includes("rate_limit")||d.error.toLowerCase().includes("too many requests")||d.error.toLowerCase().includes("\u9650\u6D41"))){let m=Date.now();a.rateLimitUntil&&a.rateLimitUntil>m?a.rateLimitUntil+=120*1e3:a.rateLimitUntil=m+120*1e3,this.logger?.debug?.(`Rate-limit probe for ${s} confirmed still limited, extending cooldown`)}else this.logger?.debug?.(`Rate-limit probe for ${s} returned non-rate-limit error: ${d.error?.slice(0,100)}`)}catch(d){this.logger?.warn?.(`Rate-limit probe exception for ${s}: ${d?.message||d}`)}}}async runScheduledWakeup(){if(!this.isWakeupGloballyEnabled())return;let e=this.getProviders().filter(c=>c?.name&&c.enabled!==!1&&c.wakeupEnabled===!0);if(e.length===0)return;let t=new Date,o=t.getHours(),r=t.getMinutes(),s=gl(t),i=this.getConfig&&this.getConfig("WAKEUP_TIME")||"06:00";for(let c of e){let l=c.wakeupTime||i,[a,u]=l.split(":"),d=parseInt(a,10),m=parseInt(u,10);if(isNaN(d)||isNaN(m))continue;let f=o>d||o===d&&r>=m,y=(d*60+m)*6e4-(o*60+r)*6e4,p=y>0&&y<=300*1e3;(f||p)&&!this.hasWakeupFiredToday(c.name,s)&&(this.logger?.info?.(`Scheduled wake-up triggered for provider ${c.name} at ${l}`),_l(c,3e4,this.resolveProxyUrl?.(c),this.logger).then(g=>{g.success?this.logger?.info?.(`Successfully woke up provider ${c.name}`):this.logger?.error?.(`Failed to wake up provider ${c.name}: ${g.error}`)}).catch(g=>{this.logger?.error?.(`Error in wake-up task for ${c.name}: ${g}`)}))}}isWakeupGloballyEnabled(){return this.getConfig?ml(this.getConfig("WAKEUP_ENABLED")):!1}hasWakeupFiredToday(e,t){return this.lastWakeupDate.get(e)===t?!0:(this.lastWakeupDate.set(e,t),!1)}async probeProviderManually(e){let o=this.getProviders().find(c=>c.name===e);if(!o)return this.logger?.warn?.(`Provider ${e} not found for manual probe`),{success:!1,latencyMs:0,status:"error",isSlow:!1,error:`Provider ${e} not found`};if(o.enabled===!1)return this.logger?.warn?.(`Provider ${e} is disabled, skipping manual probe`),{success:!1,latencyMs:0,status:"error",isSlow:!1,error:`Provider ${e} is disabled`};let r=await Mo(o,this.config.probeTimeoutMs,this.resolveProxyUrl?.(o)),s=this.recordProbe(o.name,r,"manual"),i=Array.isArray(o.models)?o.models:[];if(r.success)for(let c of i)U().recordSuccess(o.name,c);else for(let c of i)U().recordFailure(o.name,c,r.error);return{success:r.success,latencyMs:r.latencyMs,status:s.status,isSlow:s.isSlow,errorKind:r.errorKind,error:r.error}}},pe=null});var ai=w(()=>{"use strict"});var No,at,ci=w(()=>{"use strict";No=class{plugins=new Map;pluginInstances=new Map;registerPlugin(e,t={}){this.pluginInstances.set(e.name,e),this.plugins.set(e.name,{name:e.name,enabled:t.enabled!==!1,options:t})}async enablePlugin(e,t){let o=this.plugins.get(e),r=this.pluginInstances.get(e);if(!o||!r)throw new Error(`Plugin ${e} not found`);o.enabled&&await t.register(r.register,o.options)}async enablePlugins(e){for(let[t,o]of this.plugins)if(o.enabled)try{await this.enablePlugin(t,e)}catch(r){let s=r instanceof Error?r.message:String(r);e.log?.error(`Failed to enable plugin ${t}: ${s}`)}}getPlugins(){return Array.from(this.plugins.values())}getPlugin(e){return this.pluginInstances.get(e)}hasPlugin(e){return this.pluginInstances.has(e)}isPluginEnabled(e){return this.plugins.get(e)?.enabled||!1}setPluginEnabled(e,t){let o=this.plugins.get(e);o&&(o.enabled=t)}removePlugin(e){this.plugins.delete(e),this.pluginInstances.delete(e)}clear(){this.plugins.clear(),this.pluginInstances.clear()}},at=new No});var ae,Lo=w(()=>{"use strict";ae=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,t)=>{this.buffer+=e;let o=this.buffer.split(`
305
- `);this.buffer=o.pop()||"";for(let r of o){let s=this.processLine(r);s&&t.enqueue(s)}},flush:e=>{if(this.buffer.trim()){let t=[];this.processLine(this.buffer.trim(),t),t.forEach(o=>e.enqueue(o))}Object.keys(this.currentEvent).length>0&&e.enqueue(this.currentEvent)}})}processLine(e,t){if(!e.trim()){if(Object.keys(this.currentEvent).length>0){let o={...this.currentEvent};return this.currentEvent={},t?(t.push(o),null):o}return null}if(e.startsWith("event:"))this.currentEvent.event=e.slice(6).trim();else if(e.startsWith("data:")){let o=e.slice(5).trim();if(o==="[DONE]")this.currentEvent.data={type:"done"};else try{this.currentEvent.data=JSON.parse(o)}catch{this.currentEvent.data={raw:o,error:"JSON parse failed"}}}else e.startsWith("id:")?this.currentEvent.id=e.slice(3).trim():e.startsWith("retry:")&&(this.currentEvent.retry=parseInt(e.slice(6).trim()));return null}}});var ze,Do=w(()=>{"use strict";ze=class extends TransformStream{constructor(){super({transform:(e,t)=>{let o="";e.event&&(o+=`event: ${e.event}
304
+ Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(ei).forEach(e=>{if("TransformerName"in e&&typeof e.TransformerName=="string")this.registerTransformer(e.TransformerName,e);else{let t=new e;t&&typeof t=="object"&&(t.logger=this.logger),this.registerTransformer(t.name,t)}})}catch(e){this.logger.error({error:e},"transformer regist error:")}}async loadFromConfig(){let e=this.configService.get("transformers",[]);for(let t of e)await this.registerTransformerFromConfig(t)}}});import{get_encoding as Kc}from"tiktoken";var it,ni=w(()=>{"use strict";it=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=Kc(e)}catch{throw new Error(`Failed to initialize tiktoken encoding: ${e}`)}}async initialize(){if(!this.encoding)throw new Error("Tiktoken encoding not initialized")}async countTokens(e){let t=this.encoding;if(!t)throw new Error("Encoding not initialized");let o=0,{messages:r,system:s,tools:i}=e;return Array.isArray(r)&&r.forEach(c=>{typeof c.content=="string"?o+=t.encode(c.content).length:Array.isArray(c.content)&&c.content.forEach(l=>{if(l.type==="text")o+=t.encode(l.text).length;else if(l.type==="tool_use")o+=t.encode(JSON.stringify(l.input)).length;else if(l.type==="tool_result"){let a=typeof l.content=="string"?l.content:JSON.stringify(l.content);o+=t.encode(a).length}})}),typeof s=="string"?o+=t.encode(s).length:Array.isArray(s)&&s.forEach(c=>{c.type==="text"&&(typeof c.text=="string"?o+=t.encode(c.text).length:Array.isArray(c.text)&&c.text.forEach(l=>{o+=t.encode(l||"").length}))}),i&&i.forEach(c=>{c.description&&(o+=t.encode(c.name+c.description).length),c.input_schema&&(o+=t.encode(JSON.stringify(c.input_schema)).length)}),o}isInitialized(){return this.encoding!==void 0}encodeText(e){let t=this.encoding;if(!t)throw new Error("Encoding not initialized");return Array.from(t.encode(e))}dispose(){this.encoding&&(this.encoding.free(),this.encoding=void 0)}}});import{join as fn}from"path";import{homedir as Qc}from"os";import{existsSync as ko,mkdirSync as Vc}from"fs";import{promises as mn}from"fs";import{Tokenizer as Yc}from"@huggingface/tokenizers";var gn,oi=w(()=>{"use strict";gn=class{type="huggingface";name;modelId;logger;options;tokenizer=null;cacheDir;safeModelName;constructor(e,t,o={}){this.modelId=e,this.logger=t,this.options=o,this.cacheDir=o.cacheDir||fn(Qc(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=fn(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:fn(e,"tokenizer.json"),tokenizerConfig:fn(e,"tokenizer_config.json")}}ensureDir(e){ko(e)||Vc(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!ko(e.tokenizerJson)||!ko(e.tokenizerConfig))return null;let[t,o]=await Promise.all([mn.readFile(e.tokenizerJson,"utf-8"),mn.readFile(e.tokenizerConfig,"utf-8")]);return{tokenizerJson:JSON.parse(t),tokenizerConfig:JSON.parse(o)}}catch(e){return this.logger?.warn(`Failed to load from cache: ${e.message}`),null}}async downloadAndCache(){let e=this.getCachePaths(),t={json:`https://huggingface.co/${this.modelId}/resolve/main/tokenizer.json`,config:`https://huggingface.co/${this.modelId}/resolve/main/tokenizer_config.json`};this.logger?.info(`Downloading tokenizer files for ${this.modelId}`);let o=new AbortController,r=setTimeout(()=>o.abort(),this.options.timeout||3e4);try{let[s,i]=await Promise.all([fetch(t.json,{signal:o.signal}),fetch(t.config,{signal:o.signal})]);if(!s.ok)throw new Error(`Failed to fetch tokenizer.json: ${s.statusText}`);let[c,l]=await Promise.all([s.json(),i.ok?i.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([mn.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),mn.writeFile(e.tokenizerConfig,JSON.stringify(l,null,2))]),{tokenizerJson:c,tokenizerConfig:l}}finally{clearTimeout(r)}}async initialize(){try{this.logger?.info(`Initializing HuggingFace tokenizer: ${this.modelId}`);let e=this.getCachePaths();this.ensureDir(this.cacheDir);let t=await this.loadFromCache()||await this.downloadAndCache();this.tokenizer=new Yc(t.tokenizerJson,t.tokenizerConfig),this.logger?.info(`Tokenizer initialized: ${this.name}`)}catch(e){throw this.logger?.error(`Failed to initialize tokenizer: ${e.message}`),new Error(`Failed to initialize HuggingFace tokenizer for ${this.modelId}: ${e.message}`)}}async countTokens(e){if(!this.tokenizer)throw new Error("Tokenizer not initialized");try{let t=this.extractTextFromRequest(e);return this.tokenizer.encode(t).ids.length}catch(t){throw this.logger?.error(`Error counting tokens: ${t.message}`),t}}isInitialized(){return this.tokenizer!==null}encodeText(e){if(!this.tokenizer)throw new Error("Tokenizer not initialized");return this.tokenizer.encode(e).ids}dispose(){this.tokenizer=null}extractTextFromRequest(e){let t=[],{messages:o,system:r,tools:s}=e;if(Array.isArray(o)){for(let i of o)if(typeof i.content=="string")t.push(i.content);else if(Array.isArray(i.content))for(let c of i.content)c.type==="text"&&c.text?t.push(c.text):c.type==="tool_use"&&c.input?t.push(JSON.stringify(c.input)):c.type==="tool_result"&&t.push(typeof c.content=="string"?c.content:JSON.stringify(c.content))}if(typeof r=="string")t.push(r);else if(Array.isArray(r)){for(let i of r)if(i.type==="text"){if(typeof i.text=="string")t.push(i.text);else if(Array.isArray(i.text))for(let c of i.text)c&&t.push(c)}}if(s)for(let i of s)i.name&&t.push(i.name),i.description&&t.push(i.description),i.input_schema&&t.push(JSON.stringify(i.input_schema));return t.join(" ")}}});var hn,ri=w(()=>{"use strict";oe();hn=class{type="api";name;config;logger;options;constructor(e,t,o={}){if(!e.url||!e.apiKey)throw new Error("API tokenizer requires url and apiKey");this.config={url:e.url,apiKey:e.apiKey,requestFormat:e.requestFormat||"standard",responseField:e.responseField||"token_count",headers:e.headers||{}},this.logger=t,this.options=o;try{let r=new URL(e.url);this.name=`api-${r.hostname}`}catch{this.name=`api-${e.url}`}}async initialize(){try{new URL(this.config.url)}catch{throw new Error(`Invalid API URL: ${this.config.url}`)}}async countTokens(e){try{let t=this.formatRequestBody(e),o={"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`,...this.config.headers},r=new AbortController,s=setTimeout(()=>r.abort(),this.options.timeout||3e4),i={method:"POST",headers:o,body:JSON.stringify(t),signal:r.signal},c=this.options.resolveProxyUrl?.();c&&(i.dispatcher=G(c));let l=await fetch(this.config.url,i);if(clearTimeout(s),!l.ok)throw new Error(`API tokenizer request failed: ${l.status} ${l.statusText}`);let a=await l.json();return this.extractTokenCount(a)}catch(t){throw t.name==="AbortError"?new Error("API tokenizer request timed out"):t}}isInitialized(){return!0}dispose(){}formatRequestBody(e){switch(this.config.requestFormat){case"standard":return e;case"openai":return{model:"gpt-3.5-turbo",messages:this.extractMessagesAsOpenAIFormat(e)};case"anthropic":return{messages:e.messages||[],system:e.system,tools:e.tools};case"custom":return{text:this.extractConcatenatedText(e)};default:return e}}extractMessagesAsOpenAIFormat(e){return e.messages?e.messages.map(t=>({role:t.role,content:this.extractTextFromMessage(t)})):[]}extractTextFromMessage(e){return typeof e.content=="string"?e.content:Array.isArray(e.content)?e.content.map(t=>t.type==="text"&&t.text?t.text:t.type==="tool_use"&&t.input?JSON.stringify(t.input):t.type==="tool_result"?typeof t.content=="string"?t.content:JSON.stringify(t.content):"").join(" "):""}extractConcatenatedText(e){let t=[];return e.messages&&e.messages.forEach(o=>{t.push(this.extractTextFromMessage(o))}),typeof e.system=="string"?t.push(e.system):Array.isArray(e.system)&&e.system.forEach(o=>{o.type==="text"&&(typeof o.text=="string"?t.push(o.text):Array.isArray(o.text)&&o.text.forEach(r=>{r&&t.push(r)}))}),e.tools&&e.tools.forEach(o=>{o.name&&t.push(o.name),o.description&&t.push(o.description),o.input_schema&&t.push(JSON.stringify(o.input_schema))}),t.join(" ")}extractTokenCount(e){try{let t=this.config.responseField,o=t.split("."),r=e;for(let s of o){if(r==null)throw new Error(`Field path '${t}' not found in response`);r=r[s]}if(typeof r!="number")throw new Error(`Expected number at field path '${t}', got ${typeof r}`);return r}catch(t){throw this.logger?.error(`Failed to extract token count from API response: ${t.message}. Response: ${JSON.stringify(e)}`),new Error(`Invalid response from API tokenizer: ${t.message}`)}}}});var He,bo=w(()=>{"use strict";ni();oi();ri();oe();He=class{tokenizers=new Map;configService;logger;options;fallbackTokenizer;constructor(e,t,o={}){this.configService=e,this.logger=t,this.options={timeout:o.timeout??3e4,...o}}async initialize(){try{this.fallbackTokenizer=new it("cl100k_base"),await this.fallbackTokenizer.initialize(),this.tokenizers.set("fallback",this.fallbackTokenizer),this.logger?.info("TokenizerService initialized successfully")}catch(e){throw this.logger?.error(`TokenizerService initialization error: ${e.message}`),e}}async getTokenizer(e,t){let o=this.getCacheKey(e,t);if(this.tokenizers.has(o))return this.tokenizers.get(o);let r;try{switch(e.type){case"tiktoken":r=new it(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),r=new gn(e.model,this.logger,{timeout:this.options.timeout});break;case"api":r=new hn(e,this.logger,{timeout:this.options.timeout,resolveProxyUrl:()=>{let s=t?this.getRawProvider(t):void 0;return ce(this.configService,s)}});break;default:throw new Error(`Unknown tokenizer type: ${e.type}`)}return this.logger?.info(`Calling initialize() on ${e.type} tokenizer...`),await r.initialize(),this.tokenizers.set(o,r),this.logger?.info(`Tokenizer initialized successfully: ${e.type} (${o})`),r}catch(s){return this.logger?.error(`Failed to initialize ${e.type} tokenizer: ${s.message}`),this.logger?.error(`Error stack: ${s.stack}`),this.fallbackTokenizer||await this.initialize(),this.fallbackTokenizer}}async countTokens(e,t,o){let r=t?await this.getTokenizer(t,o):this.fallbackTokenizer;return{tokenCount:await r.countTokens(e),tokenizerUsed:r.name,cached:!1}}getTokenizerConfigForModel(e,t){let r=(this.configService.get("providers")||[]).find(s=>s.name===e);if(r?.tokenizer)return r.tokenizer.models?.[t]?r.tokenizer.models[t]:r.tokenizer.default}dispose(){this.tokenizers.forEach(e=>{try{e.dispose()}catch(t){this.logger?.error(`Error disposing tokenizer: ${t}`)}}),this.tokenizers.clear()}getCacheKey(e,t){switch(e.type){case"tiktoken":return`tiktoken:${e.encoding||"cl100k_base"}`;case"huggingface":return`hf:${e.model}`;case"api":return`api:${t||"unscoped"}:${e.url}`;default:return`unknown:${JSON.stringify(e)}`}}getRawProvider(e){return(this.configService.get("providers")||[]).find(o=>o?.name===e)}}});function si(n){if(!n||typeof n!="object"||ol(n))return null;for(let t of nl(n)){let o=tl(t);if(o)return o}return null}function tl(n){if(!n||typeof n!="object")return null;let e={},t=yn(n,Xc);if(t!==void 0){let i=ii(t);e.usedDailyBalance=i,e.limitDaily=100}let o=yn(n,Zc);if(o!==void 0){let i=ii(o);e.usedBalance=i,e.totalBalance=100}let r=yn(n,qc);if(r!==void 0){let i=ai(r);i&&(e.resetTime=i)}let s=yn(n,el);if(s!==void 0){let i=ai(s);i&&(e.resetTime7d=i,e.resetTime||(e.resetTime=i))}return e.usedDailyBalance===void 0&&e.limitDaily===void 0&&e.usedBalance===void 0&&e.totalBalance===void 0&&e.resetTime===void 0?null:e}function ii(n){return!Number.isFinite(n)||n<=0?0:Math.round(n*1e4)/100}function yn(n,e){for(let t of e){let o=M(n?.[t]);if(o!==void 0)return o}}function nl(n){let e=[],t=new WeakSet,o=["data","Data","DataV2"],r=s=>{if(!(!s||typeof s!="object")&&!t.has(s)){t.add(s),e.push(s);for(let i of o)s[i]&&typeof s[i]=="object"&&r(s[i])}};return r(n),e}function ol(n){let e=n?.data;return e&&typeof e=="object"&&(e.success===!1||e.errorCode||e.errorMsg)?String(e.errorCode||e.errorMsg||"gateway-error"):null}function ai(n){if(typeof n!="string"&&typeof n!="number")return;let e=n;if(typeof n=="number"&&n<1e12&&(e=n*1e3),typeof n=="string"){let o=Number(n.trim());Number.isFinite(o)&&o<1e12&&(e=o*1e3)}let t=new Date(e);return isNaN(t.getTime())?void 0:t.toISOString()}function Io(n){let e=Mo(n);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?dl:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?pl:e==="deepseek.com"||e.endsWith(".deepseek.com")?rl:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?sl:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?il:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?al:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?cl:e==="maas.aliyuncs.com"||e.endsWith(".maas.aliyuncs.com")?ll:e==="xf-yun.com"||e.endsWith(".xf-yun.com")||e==="xfyun.cn"||e.endsWith(".xfyun.cn")||e==="xfyun.com"||e.endsWith(".xfyun.com")?ul:null:null}function fl(n){let e=Mo(n);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function ml(n,e){let t=Oo(n);if(t.length===0)return null;let o=String(e||"").split(":").map(s=>s.trim()).filter(Boolean);return t.find(s=>{let i=s?.codingPlanAppCredentialDTO||s?.appCredential||s,c=String(i?.apiKey||i?.api_key||"").trim();return c&&o.some(l=>l.includes(c)||c.includes(l))})||t[0]}function Oo(n){return!n||typeof n!="object"?[]:!Array.isArray(n)&&n.codingPlanUsageDTO?[n]:Array.isArray(n)?n.flatMap(e=>Oo(e)):Object.values(n).flatMap(e=>Oo(e))}function Ao(n,e){let t=n.match(new RegExp(`(?:^|;\\s*)${e}=([^;]+)`));return t?decodeURIComponent(t[1]):void 0}function Mo(n){try{return new URL(n).hostname.toLowerCase()}catch{return null}}function M(n){if(typeof n=="number")return Number.isFinite(n)?n:void 0;if(typeof n!="string")return;let e=n.trim().replace(/,/g,"");if(!e)return;let t=Number(e);return Number.isFinite(t)?t:void 0}var se,To,vo,Co,xo,So,Xc,Zc,qc,el,Ro,wo,Po,Eo,rl,sl,il,al,cl,ll,ul,dl,pl,$o=w(()=>{"use strict";oe();se=class{async fetchJson(e,t,o,r,s){let i=s||t.apiKey;if(!i)return null;let c={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${i}`},signal:AbortSignal.timeout(o)};try{r&&(c.dispatcher=G(r));let l=await fetch(e,c);return l.ok?await l.json():null}catch{return null}}hasQuotaData(e){return e.totalBalance!==void 0||e.usedBalance!==void 0||e.remainingBalance!==void 0||e.usedDailyBalance!==void 0||e.limitDaily!==void 0||e.resetTime!==void 0}},To=class extends se{async queryQuota(e,t,o){let r=await this.fetchJson("https://api.deepseek.com/user/balance",e,t,o),i=(Array.isArray(r?.balance_infos)?r.balance_infos:[]).find(l=>M(l?.total_balance)!==void 0);if(!i)return null;let c=M(i.total_balance);return c===void 0?null:{totalBalance:c,currency:typeof i.currency=="string"?i.currency:void 0}}},vo=class extends se{async queryQuota(e,t,o){let s=(await this.fetchJson("https://openrouter.ai/api/v1/key",e,t,o))?.data;if(!s||typeof s!="object")return null;let i={},c=M(s.limit),l=M(s.limit_remaining),a=M(s.usage),u=M(s.usage_daily);return c!==void 0&&(i.totalBalance=c),l!==void 0&&(i.remainingBalance=l),a!==void 0&&(i.usedBalance=a),u!==void 0&&(i.usedDailyBalance=u),this.hasQuotaData(i)?i:null}},Co=class extends se{async queryQuota(e,t,o){let r=fl(e.baseUrl),i=(await this.fetchJson(r,e,t,o))?.data;if(!i||typeof i!="object")return null;let c={},l=M(i.totalBalance),a=M(i.balance);return l!==void 0&&(c.totalBalance=l),a!==void 0&&(c.remainingBalance=a),l!==void 0&&a!==void 0&&(c.usedBalance=Math.max(0,l-a)),this.hasQuotaData(c)?c:null}},xo=class extends se{async queryQuota(e,t,o){if(!e.apiKey)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:e.apiKey.trim()},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://api.z.ai/api/monitor/usage/quota/limit",s);if(!i.ok)return null;let c=await i.json(),l=Array.isArray(c?.data?.limits)?c.data.limits:[],a=null,u=null,d=null;for(let g of l)g.type==="TOKENS_LIMIT"?g.unit===3?a=g:g.unit===6?u=g:a||(a=g):g.type==="TIME_LIMIT"&&(d=g);let f={};if(a){let g=M(a.currentValue),y=M(a.usage),m=M(a.remaining),p=M(a.percentage);g!==void 0?f.usedDailyBalance=g:p!==void 0&&(f.usedDailyBalance=p,f.limitDaily=100),y!==void 0?f.limitDaily=y:g!==void 0&&m!==void 0&&(f.limitDaily=g+m),a.nextResetTime&&(f.resetTime=new Date(a.nextResetTime).toISOString())}if(u){let g=M(u.currentValue),y=M(u.usage),m=M(u.remaining),p=M(u.percentage);g!==void 0?f.usedBalance=g:p!==void 0&&(f.usedBalance=p,f.totalBalance=100),y!==void 0?f.totalBalance=y:g!==void 0&&m!==void 0&&(f.totalBalance=g+m),u.nextResetTime&&!f.resetTime&&(f.resetTime=new Date(u.nextResetTime).toISOString())}if(d){if(f.usedDailyBalance===void 0){let g=M(d.currentValue);g!==void 0&&(f.usedDailyBalance=g)}if(f.limitDaily===void 0){let g=M(d.usage),y=M(d.currentValue),m=M(d.remaining);g!==void 0?f.limitDaily=g:y!==void 0&&m!==void 0&&(f.limitDaily=y+m)}d.nextResetTime&&!f.resetTime&&(f.resetTime=new Date(d.nextResetTime).toISOString())}return this.hasQuotaData(f)?f:null}catch{return null}}},So=class extends se{async queryQuota(e,t,o){if(!e.quotaToken)return null;let r={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://bailian.console.aliyun.com",Referer:"https://bailian.console.aliyun.com/cn-beijing",Cookie:e.quotaToken},signal:AbortSignal.timeout(t)},s=JSON.stringify({Api:"zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",V:"1.0",Data:{queryCodingPlanInstanceInfoRequest:{commodityCode:"sfm_codingplan_public_cn",onlyLatestOne:!0},cornerstoneParam:{feTraceId:`ccr-${Date.now()}`,feURL:"https://bailian.console.aliyun.com/cn-beijing",protocol:"V2",console:"ONE_CONSOLE",productCode:"p_efm",switchAgent:10736808,switchUserType:3,domain:"bailian.console.aliyun.com",consoleSite:"BAILIAN_ALIYUN",userNickName:"",userPrincipalName:"",xsp_lang:"zh-CN"}}}),i=new URLSearchParams({params:s,region:"cn-beijing"}).toString();try{o&&(r.dispatcher=G(o));let c=await fetch("https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",{...r,body:i});if(!c.ok)return null;let a=(await c.json())?.data?.DataV2?.data?.data?.codingPlanInstanceInfos;if(!Array.isArray(a)||a.length===0)return null;let u=a[0]?.codingPlanQuotaInfo;if(!u)return null;let d={},f=M(u.per5HourUsedQuota),g=M(u.per5HourTotalQuota);f!==void 0&&(d.usedDailyBalance=f),g!==void 0&&(d.limitDaily=g);let y=M(u.perWeekUsedQuota),m=M(u.perWeekTotalQuota);return y!==void 0&&(d.usedBalance=y),m!==void 0&&(d.totalBalance=m),this.hasQuotaData(d)?d:null}catch{return null}}};Xc=["per5HourPercentage","per5HourUsedPercentage","fiveHourPercentage"],Zc=["per1WeekPercentage","perWeekPercentage","per1WeekUsedPercentage"],qc=["per5HourResetTime","per5HourNextResetTime","fiveHourResetTime"],el=["per1WeekResetTime","per1WeekNextResetTime","perWeekResetTime"];Ro=class extends se{async queryQuota(e,t,o){let r=e.quotaToken?.trim();if(!r)return null;let s=e.quotaSecToken?.trim();if(s){try{let i=await this.queryOfficialEndpoint(r,s,t,o);if(i)return i}catch{}return this.queryLegacyEndpoint(r,t,o)}return this.queryLegacyEndpoint(r,t,o)}async queryOfficialEndpoint(e,t,o,r){let s="zeldaHttp.apikeyMgr./tokenplan/personal/api/v2/usage",i=Ao(e,"cna")||"",c=JSON.stringify({Api:s,V:"1.0",Data:{cornerstoneParam:{feTraceId:`ccr-${Date.now()}`,feURL:"https://bailian.console.aliyun.com/cn-beijing/?tab=plan#/efm/subscription/token-plan/personal",protocol:"V2",console:"ONE_CONSOLE",productCode:"p_efm",switchAgent:10736808,switchUserType:3,domain:"bailian.console.aliyun.com",consoleSite:"BAILIAN_ALIYUN",userNickName:"",userPrincipalName:"",xsp_lang:"zh-CN","X-Anonymous-Id":i}}}),l=new URLSearchParams({params:c,region:"cn-beijing",sec_token:t}).toString(),a={method:"POST",headers:{Accept:"application/json","Content-Type":"application/x-www-form-urlencoded",Cookie:e,"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://bailian.console.aliyun.com",Referer:"https://bailian.console.aliyun.com/cn-beijing/?tab=plan"},body:l,signal:AbortSignal.timeout(o)};try{r&&(a.dispatcher=G(r));let u=await fetch(`https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=${encodeURIComponent(s)}&_v=undefined`,a);if(!u.ok)return null;let d=await u.json();return si(d)}catch{return null}}async queryLegacyEndpoint(e,t,o){let r="zeldaHttp.apikeyMgr./tokenplan/personal/api/v2/usage",s=JSON.stringify({Api:r,V:"1.0",Data:{cornerstoneParam:{feTraceId:`ccr-${Date.now()}`,feURL:"https://bailian.console.aliyun.com/",protocol:"V2",console:"ONE_CONSOLE",productCode:"p_efm",domain:"bailian.console.aliyun.com",consoleSite:"BAILIAN_ALIYUN",xsp_lang:"zh-CN"}}}),i=new URLSearchParams({params:s,region:"cn-beijing"}).toString(),c={method:"POST",headers:{Accept:"application/json","Content-Type":"application/x-www-form-urlencoded",Cookie:e,"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://bailian.console.aliyun.com",Referer:"https://bailian.console.aliyun.com/"},body:i,signal:AbortSignal.timeout(t)};try{o&&(c.dispatcher=G(o));let l=await fetch(`https://cs-data.qianwenai.com/data/api.json?product=sfm_bailian&action=BroadScopeAspnGateway&api=${encodeURIComponent(r)}`,c);if(!l.ok)return null;let a=await l.json();return si(a)}catch{return null}}},wo=class extends se{async queryQuota(e,t,o){if(!e.quotaToken)return null;let s={method:"GET",headers:{Accept:"application/json","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://maas.xfyun.cn",Referer:"https://maas.xfyun.cn/packageSubscription",Authorization:(Ao(e.quotaToken,"atp-auth-token")||Ao(e.quotaToken,"tenantToken")||e.quotaToken).trim(),Cookie:e.quotaToken},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://maas.xfyun.cn/api/v1/gpt-finetune/coding-plan/list?page=1&size=10",s);if(!i.ok)return null;let c=await i.json();if(c?.succeed===!1||c?.failed===!0)return null;let l=ml(c,e.apiKey),a=l?.codingPlanUsageDTO||l?.usage||l;if(!a||typeof a!="object")return null;let u={},d=M(a.rp5hUsage),f=M(a.rp5hLimit);d!==void 0&&(u.usedDailyBalance=d),f!==void 0&&(u.limitDaily=f);let g=M(a.rpwUsage),y=M(a.rpwLimit);return g!==void 0&&(u.usedBalance=g),y!==void 0&&(u.totalBalance=y),u.remainingBalance===void 0&&y!==void 0&&g!==void 0&&(u.remainingBalance=Math.max(0,y-g)),this.hasQuotaData(u)?u:null}catch{return null}}},Po=class extends se{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${r.trim()}`},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://api.kimi.com/coding/v1/usages",s);if(!i.ok)return null;let c=await i.json(),l={},a=Array.isArray(c?.limits)?c.limits:[];if(a.length>0&&a[0]?.detail){let d=a[0].detail,f=M(d.limit),g=M(d.remaining),y=d.resetTime;f!==void 0&&g!==void 0&&(l.usedDailyBalance=Math.max(0,f-g),l.limitDaily=f),y&&(l.resetTime=new Date(y).toISOString())}let u=c?.usage;if(u){let d=M(u.limit),f=M(u.remaining),g=M(u.used),y=u.resetTime;d!==void 0&&(l.totalBalance=d,g!==void 0?l.usedBalance=g:f!==void 0&&(l.usedBalance=Math.max(0,d-f),l.remainingBalance=f)),y&&(l.resetTime7d=new Date(y).toISOString(),l.resetTime||(l.resetTime=l.resetTime7d))}return this.hasQuotaData(l)?l:null}catch{return null}}},Eo=class extends se{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s=Mo(e.baseUrl)||"api.minimaxi.com",i=s.endsWith(".minimax.io")||s==="minimax.io"?"api.minimax.io":"api.minimaxi.com";try{let c=[`https://${i}/v1/token_plan/remains`,`https://${i}/v1/api/openplatform/coding_plan/remains`],l=null;for(let b of c)if(l=await this.fetchJson(b,e,t,o,r.trim()),l)break;if(!l)return null;if(l?.base_resp){let b=l.base_resp.status_code;if(b!==void 0&&b!==0)return null}let a=Array.isArray(l?.model_remains)?l.model_remains:[];if(a.length===0)return null;let u=a[0],d={},f=M(u.current_interval_total_count),g=M(u.current_interval_usage_count),y=M(u.current_interval_remaining_percent),m=M(u.end_time);f!==void 0&&g!==void 0&&f>0?(d.usedDailyBalance=Math.max(0,f-g),d.limitDaily=f):y!==void 0&&(d.limitDaily=100,d.usedDailyBalance=Math.max(0,100-y)),m&&(d.resetTime=new Date(m).toISOString());let p=M(u.current_weekly_total_count),_=M(u.current_weekly_usage_count),k=M(u.current_weekly_remaining_percent),h=M(u.weekly_end_time);return p!==void 0&&_!==void 0&&p>0?(d.usedBalance=Math.max(0,p-_),d.remainingBalance=_,d.totalBalance=p):k!==void 0&&(d.totalBalance=100,d.usedBalance=Math.max(0,100-k),d.remainingBalance=k),h&&!d.resetTime&&(d.resetTime=new Date(h).toISOString()),this.hasQuotaData(d)?d:null}catch{return null}}},rl=new To,sl=new vo,il=new Co,al=new xo,cl=new So,ll=new Ro,ul=new wo,dl=new Po,pl=new Eo});function ci(n){return{[gl]:n,[hl]:Pt}}function _l(n){return n===!0||n==="true"||n===1||n==="1"}function kl(n){let e=n.getFullYear(),t=String(n.getMonth()+1).padStart(2,"0"),o=String(n.getDate()).padStart(2,"0");return`${e}-${t}-${o}`}function bl(n){try{let e=new URL(n),t=e.pathname;return t.endsWith("/v1/messages")?t=t.slice(0,-12):t.endsWith("/messages")?t=t.slice(0,-9):t.endsWith("/v1/chat/completions")?t=t.slice(0,-20):t.endsWith("/chat/completions")&&(t=t.slice(0,-17)),t.endsWith("/v1")||(t=t.endsWith("/")?`${t}v1`:`${t}/v1`),e.pathname=`${t}/models`,e.toString()}catch{return null}}function Tl(n){try{let e=new URL(n).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function No(n,e,t){let o=performance.now(),r=()=>Math.max(0,Math.round(performance.now()-o)),s=bl(n.baseUrl);if(!s)return{success:!1,error:"Cannot derive models endpoint from baseUrl",errorKind:"network",latencyMs:r()};try{let i={method:"GET",headers:{Authorization:`Bearer ${n.apiKey}`},signal:AbortSignal.timeout(e)};t&&(i.dispatcher=G(t));let c=await fetch(s,i);if(c.headers&&Xe(n.name,n.baseUrl,c.headers),c.ok)return{success:!0,headers:c.headers,latencyMs:r()};if(c.status===429)return{success:!1,error:`HTTP 429: ${(await c.text().catch(()=>"")).slice(0,100)||"Rate limited"}`,headers:c.headers,latencyMs:r(),errorKind:"http"};if(c.status>=400&&c.status<500)return{success:!0,headers:c.headers,latencyMs:r()};let l=await c.text().catch(()=>"");return{success:!1,error:`HTTP ${c.status}: ${l.slice(0,100)}`,headers:c.headers,latencyMs:r(),errorKind:"http"}}catch(i){let c=i?.message||i?.toString()||"Unknown probe error",l=i?.name==="TimeoutError"||i?.name==="AbortError"||i?.code===20||i?.code==="ABORT_ERR";return{success:!1,error:c,latencyMs:r(),errorKind:l?"timeout":"network"}}}async function vl(n,e,t,o){let r=n.wakeupModel||(Array.isArray(n.models)?n.models[0]:void 0);if(!r)return{success:!1,error:"No models configured for provider"};let s=n.baseUrl,i=n.baseUrl.includes("/messages")||r.includes("claude");try{let c={"Content-Type":"application/json",...ci("scheduled-wakeup")};i?(c["x-api-key"]=n.apiKey,c["anthropic-version"]="2023-06-01"):c.Authorization=`Bearer ${n.apiKey}`;let l={method:"POST",headers:c,body:JSON.stringify({model:r,messages:[{role:"user",content:"Wake up and acknowledge with a short greeting."}],max_tokens:10}),signal:AbortSignal.timeout(e)};t&&(l.dispatcher=G(t)),o?.info(`Sending scheduled wake-up call to provider ${n.name} using model ${r}...`);let a=await fetch(s,l);if(a.ok)return{success:!0};let u=await a.text().catch(()=>"");return{success:!1,error:`HTTP ${a.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(c){return{success:!1,error:c?.message||c?.toString()||"Unknown wake-up error"}}}async function Cl(n,e,t,o,r){let s=n.baseUrl,i=n.baseUrl.includes("/messages")||e.includes("claude");try{let c={"Content-Type":"application/json",...ci("rate-limit-probe")};i?(c["x-api-key"]=n.apiKey,c["anthropic-version"]="2023-06-01"):c.Authorization=`Bearer ${n.apiKey}`;let l={method:"POST",headers:c,body:JSON.stringify({model:e,messages:[{role:"user",content:"Hello, please reply briefly."}],max_tokens:10}),signal:AbortSignal.timeout(t)};o&&(l.dispatcher=G(o)),r?.debug?.(`Sending independent ping to rate-limited model ${n.name} (${e})...`);let a=await fetch(s,l);if(a.ok)return{success:!0};let u=await a.text().catch(()=>"");return{success:!1,error:`HTTP ${a.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(c){return{success:!1,error:c?.message||c?.toString()||"Unknown ping error"}}}function li(n,e,t,o,r){return pe||(pe=new _n(n,e,t,o,r)),pe}function ze(n,e,t,o,r){let s=li(n,e,t,o,r);return s.start(),s}function Lo(){pe&&pe.stop()}function kn(){pe&&(pe.stop(),pe=null)}var gl,hl,yl,_n,pe,bn=w(()=>{"use strict";re();we();$o();Ie();Gn();oe();gl="x-claude-code-router-source",hl="x-claude-code-router-version";yl={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,slowThresholdMs:3e3,initialDelayMs:5e3,excludeProviders:[]};_n=class{config;quotaProbeTimer;healthProbeTimer;wakeupTimer;rateLimitProbeTimer;lastWakeupDate=new Map;getProviders;resolveProxyUrl;logger;getConfig;running=!1;probeTelemetry=new Map;constructor(e,t,o,r,s){this.config={...yl,...t},this.getProviders=e,this.resolveProxyUrl=o,this.logger=r,this.getConfig=s}recordProbe(e,t,o){let r=Date.now(),s=t.success&&t.latencyMs>this.config.slowThresholdMs,i;t.success?i=s?"slow":"healthy":i=t.errorKind==="timeout"?"timeout":"error";let c=this.probeTelemetry.get(e),l={provider:e,latencyMs:t.latencyMs,status:i,isSlow:s,errorKind:t.errorKind,errorMessage:t.error,lastProbeAt:r,lastSuccessAt:t.success?r:c?.lastSuccessAt,source:o};return this.probeTelemetry.set(e,l),s&&this.logger?.warn?.(`Slow network detected for provider ${e}: probe took ${t.latencyMs} ms (threshold ${this.config.slowThresholdMs} ms)`),l}getProbeTelemetry(){return Array.from(this.probeTelemetry.values()).map(e=>({...e}))}getProviderProbeTelemetry(e){let t=this.probeTelemetry.get(e);return t?{...t}:void 0}start(){if(this.running)return;let e=this.isWakeupGloballyEnabled();!this.config.enabled&&!e||(this.running=!0,setTimeout(()=>{this.running&&(this.config.enabled&&(this.runQuotaProbe(),this.runHealthProbe(),this.runRateLimitProbe()),this.runScheduledWakeup())},this.config.initialDelayMs),this.config.enabled&&(this.quotaProbeTimer=setInterval(()=>this.runQuotaProbe(),this.config.quotaProbeIntervalMinutes*60*1e3),this.healthProbeTimer=setInterval(()=>this.runHealthProbe(),300*1e3),this.rateLimitProbeTimer=setInterval(()=>this.runRateLimitProbe(),120*1e3)),this.wakeupTimer=setInterval(()=>this.runScheduledWakeup(),60*1e3),this.logger?.info("Active probe service started"))}stop(){this.running=!1,this.quotaProbeTimer&&(clearInterval(this.quotaProbeTimer),this.quotaProbeTimer=void 0),this.healthProbeTimer&&(clearInterval(this.healthProbeTimer),this.healthProbeTimer=void 0),this.rateLimitProbeTimer&&(clearInterval(this.rateLimitProbeTimer),this.rateLimitProbeTimer=void 0),this.wakeupTimer&&(clearInterval(this.wakeupTimer),this.wakeupTimer=void 0),this.logger?.info("Active probe service stopped")}async runQuotaProbe(){let e=this.getProviders().filter(s=>s?.name&&s.enabled!==!1&&!this.config.excludeProviders.includes(s.name)),t=U();if(e.length===0)return;let o=[];for(let s of e){let i=Io(s.baseUrl),c=Array.isArray(s.models)?s.models:[],l=this.resolveProxyUrl?.(s);if(!i){let a;try{a=new URL(s.baseUrl).hostname}catch{a=void 0}this.logger?.debug?.(`No quota adapter for provider ${s.name}`+(a?` (host: ${a})`:"")+" \u2014 skipping quota probe")}i&&o.push({provider:s.name,models:c,type:"quota-adapter",promise:i.queryQuota(s,this.config.probeTimeoutMs,l).then(a=>{if(a){io(s.name,a),this.logger?.debug?.(`Stored quota probe result for ${s.name}`);let u=a.limitDaily!==void 0&&a.usedDailyBalance!==void 0&&a.usedDailyBalance>=a.limitDaily,d=a.totalBalance!==void 0&&a.usedBalance!==void 0&&a.usedBalance>=a.totalBalance;if(u||d){let f=u?`Quota exhausted: 5h limit reached (${a.usedDailyBalance}/${a.limitDaily})`:`Quota exhausted: 7d balance depleted (${a.usedBalance}/${a.totalBalance})`;for(let g of c){if(t.getState(s.name,g)?.rateLimitUntil){this.logger?.debug?.(`Skip forceOpen for ${s.name} (${g}): already rate-limited with active cooldown`);continue}t.forceOpen(s.name,g,f)}this.logger?.warn?.(`${f} for ${s.name}, marked as unhealthy`)}else for(let f of c){let g=t.getState(s.name,f);g&&g.status==="open"&&g.lastError?.includes("Quota exhausted")&&(t.recover(s.name,f),this.logger?.info?.(`Quota recovered for ${s.name} (${f}), marked as healthy`))}}})}),Tl(s.baseUrl)&&o.push({provider:s.name,models:c,type:"rate-limit-headers",promise:No(s,this.config.probeTimeoutMs,l).then(a=>{this.recordProbe(s.name,a,"rate-limit-headers")})})}if(o.length===0)return;this.logger?.debug?.(`Running quota probe with ${o.length} tasks for ${e.length} providers`);let r=await Promise.allSettled(o.map(s=>s.promise));for(let s=0;s<o.length;s++){let i=r[s];if(i.status==="rejected"){let c=o[s];this.logger?.warn?.(`Quota probe failed for ${c.provider} (${c.type}): ${i.reason}`)}}}async runHealthProbe(){let e=U(),t=this.getProviders().filter(r=>r?.name&&r.enabled!==!1&&!this.config.excludeProviders.includes(r.name));if(t.length===0)return;this.logger?.debug?.(`Running health probe for ${t.length} providers`);let o=await Promise.allSettled(t.map(r=>No(r,this.config.probeTimeoutMs,this.resolveProxyUrl?.(r))));for(let r=0;r<t.length;r++){let s=t[r],i=o[r],c=Array.isArray(s.models)?s.models:[];if(i.status==="fulfilled"){let l=i.value;if(this.recordProbe(s.name,l,"health"),l.success){let a=0;for(let u of c){let d=e.getState(s.name,u);if(d&&d.status==="open"){let f=d.lastError?.includes("Quota exhausted"),g=d.rateLimitUntil||d.lastError&&(d.lastError.includes("429")||d.lastError.toLowerCase().includes("rate limit")||d.lastError.toLowerCase().includes("rate_limit")||d.lastError.toLowerCase().includes("too many requests")||d.lastError.toLowerCase().includes("\u9650\u6D41"));if(f){this.logger?.debug?.(`Skipping health probe recovery for ${s.name} (${u}) because quota is exhausted`);continue}if(g||d.rateLimitUntil){this.logger?.debug?.(`Skipping health probe recovery for rate-limited ${s.name} (${u})`);continue}}e.recordSuccess(s.name,u),a++}a>0&&this.logger?.info?.(`Health probe succeeded for ${s.name}`)}else{for(let a of c)e.recordFailure(s.name,a,l.error);this.logger?.warn?.(`Health probe failed for ${s.name}: ${l.error}`)}}else{this.recordProbe(s.name,{success:!1,error:i.reason?.message||String(i.reason||"Probe error"),latencyMs:0,errorKind:"network"},"health");for(let l of c)e.recordFailure(s.name,l,i.reason?.message||"Probe error");this.logger?.warn?.(`Health probe error for ${s.name}: ${i.reason}`)}}}async runRateLimitProbe(){let e=U(),t=e.getRateLimitedModels();if(t.length===0)return;let o=this.getProviders(),r=this.config.probeTimeoutMs;this.logger?.debug?.(`Running rate-limit recovery probe for ${t.length} model(s)`);for(let s of t){let[i,...c]=s.split(","),l=c.join(",");if(!i||!l)continue;let a=e.getState(i,l);if(!a||!a.rateLimitUntil)continue;if(Date.now()>=a.rateLimitUntil){e.recover(i,l),this.logger?.info?.(`Rate-limit cooldown expired, auto-recovered ${s}`);continue}let u=o.find(d=>d?.name===i&&d.enabled!==!1);if(!u){this.logger?.warn?.(`Provider ${i} not found or disabled for rate-limit probe`);continue}try{let d=await Cl(u,l,r,this.resolveProxyUrl?.(u),this.logger);if(d.success)this.logger?.info?.(`Rate-limit probe succeeded for ${s}, recovering model`),e.recover(i,l);else if(d.error&&(d.error.includes("429")||d.error.toLowerCase().includes("rate limit")||d.error.toLowerCase().includes("rate_limit")||d.error.toLowerCase().includes("too many requests")||d.error.toLowerCase().includes("\u9650\u6D41"))){let f=Date.now();a.rateLimitUntil&&a.rateLimitUntil>f?a.rateLimitUntil+=120*1e3:a.rateLimitUntil=f+120*1e3,this.logger?.debug?.(`Rate-limit probe for ${s} confirmed still limited, extending cooldown`)}else this.logger?.debug?.(`Rate-limit probe for ${s} returned non-rate-limit error: ${d.error?.slice(0,100)}`)}catch(d){this.logger?.warn?.(`Rate-limit probe exception for ${s}: ${d?.message||d}`)}}}async runScheduledWakeup(){if(!this.isWakeupGloballyEnabled())return;let e=this.getProviders().filter(c=>c?.name&&c.enabled!==!1&&c.wakeupEnabled===!0);if(e.length===0)return;let t=new Date,o=t.getHours(),r=t.getMinutes(),s=kl(t),i=this.getConfig&&this.getConfig("WAKEUP_TIME")||"06:00";for(let c of e){let l=c.wakeupTime||i,[a,u]=l.split(":"),d=parseInt(a,10),f=parseInt(u,10);if(isNaN(d)||isNaN(f))continue;let g=o>d||o===d&&r>=f,y=(d*60+f)*6e4-(o*60+r)*6e4,m=y>0&&y<=300*1e3;(g||m)&&!this.hasWakeupFiredToday(c.name,s)&&(this.logger?.info?.(`Scheduled wake-up triggered for provider ${c.name} at ${l}`),vl(c,3e4,this.resolveProxyUrl?.(c),this.logger).then(p=>{p.success?this.logger?.info?.(`Successfully woke up provider ${c.name}`):this.logger?.error?.(`Failed to wake up provider ${c.name}: ${p.error}`)}).catch(p=>{this.logger?.error?.(`Error in wake-up task for ${c.name}: ${p}`)}))}}isWakeupGloballyEnabled(){return this.getConfig?_l(this.getConfig("WAKEUP_ENABLED")):!1}hasWakeupFiredToday(e,t){return this.lastWakeupDate.get(e)===t?!0:(this.lastWakeupDate.set(e,t),!1)}async probeProviderManually(e){let o=this.getProviders().find(c=>c.name===e);if(!o)return this.logger?.warn?.(`Provider ${e} not found for manual probe`),{success:!1,latencyMs:0,status:"error",isSlow:!1,error:`Provider ${e} not found`};if(o.enabled===!1)return this.logger?.warn?.(`Provider ${e} is disabled, skipping manual probe`),{success:!1,latencyMs:0,status:"error",isSlow:!1,error:`Provider ${e} is disabled`};let r=await No(o,this.config.probeTimeoutMs,this.resolveProxyUrl?.(o)),s=this.recordProbe(o.name,r,"manual"),i=Array.isArray(o.models)?o.models:[];if(r.success)for(let c of i)U().recordSuccess(o.name,c);else for(let c of i)U().recordFailure(o.name,c,r.error);return{success:r.success,latencyMs:r.latencyMs,status:s.status,isSlow:s.isSlow,errorKind:r.errorKind,error:r.error}}},pe=null});var ui=w(()=>{"use strict"});var Do,at,di=w(()=>{"use strict";Do=class{plugins=new Map;pluginInstances=new Map;registerPlugin(e,t={}){this.pluginInstances.set(e.name,e),this.plugins.set(e.name,{name:e.name,enabled:t.enabled!==!1,options:t})}async enablePlugin(e,t){let o=this.plugins.get(e),r=this.pluginInstances.get(e);if(!o||!r)throw new Error(`Plugin ${e} not found`);o.enabled&&await t.register(r.register,o.options)}async enablePlugins(e){for(let[t,o]of this.plugins)if(o.enabled)try{await this.enablePlugin(t,e)}catch(r){let s=r instanceof Error?r.message:String(r);e.log?.error(`Failed to enable plugin ${t}: ${s}`)}}getPlugins(){return Array.from(this.plugins.values())}getPlugin(e){return this.pluginInstances.get(e)}hasPlugin(e){return this.pluginInstances.has(e)}isPluginEnabled(e){return this.plugins.get(e)?.enabled||!1}setPluginEnabled(e,t){let o=this.plugins.get(e);o&&(o.enabled=t)}removePlugin(e){this.plugins.delete(e),this.pluginInstances.delete(e)}clear(){this.plugins.clear(),this.pluginInstances.clear()}},at=new Do});var ae,Fo=w(()=>{"use strict";ae=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,t)=>{this.buffer+=e;let o=this.buffer.split(`
305
+ `);this.buffer=o.pop()||"";for(let r of o){let s=this.processLine(r);s&&t.enqueue(s)}},flush:e=>{if(this.buffer.trim()){let t=[];this.processLine(this.buffer.trim(),t),t.forEach(o=>e.enqueue(o))}Object.keys(this.currentEvent).length>0&&e.enqueue(this.currentEvent)}})}processLine(e,t){if(!e.trim()){if(Object.keys(this.currentEvent).length>0){let o={...this.currentEvent};return this.currentEvent={},t?(t.push(o),null):o}return null}if(e.startsWith("event:"))this.currentEvent.event=e.slice(6).trim();else if(e.startsWith("data:")){let o=e.slice(5).trim();if(o==="[DONE]")this.currentEvent.data={type:"done"};else try{this.currentEvent.data=JSON.parse(o)}catch{this.currentEvent.data={raw:o,error:"JSON parse failed"}}}else e.startsWith("id:")?this.currentEvent.id=e.slice(3).trim():e.startsWith("retry:")&&(this.currentEvent.retry=parseInt(e.slice(6).trim()));return null}}});var Be,Uo=w(()=>{"use strict";Be=class extends TransformStream{constructor(){super({transform:(e,t)=>{let o="";e.event&&(o+=`event: ${e.event}
306
306
  `),e.id&&(o+=`id: ${e.id}
307
307
  `),e.retry&&(o+=`retry: ${e.retry}
308
308
  `),e.data&&(e.data.type==="done"?o+=`data: [DONE]
309
309
  `:o+=`data: ${JSON.stringify(e.data)}
310
310
  `),o+=`
311
- `,t.enqueue(o)}})}}});var bn,Fo=w(()=>{"use strict";bn=(n,e)=>{let t=n.getReader();return new ReadableStream({async start(o){try{for(;;){let{done:r,value:s}=await t.read();if(r){o.close();break}let i=await e(s,o);i!==void 0&&o.enqueue(i)}}catch(r){o.error(r)}finally{t.releaseLock()}}})}});var Uo=w(()=>{"use strict";Lo();Do();Fo()});var li=w(()=>{"use strict"});var ct,jo=w(()=>{"use strict";ct=class{type="console";config;colors={reset:"\x1B[0m",bright:"\x1B[1m",dim:"\x1B[2m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",cyan:"\x1B[36m",white:"\x1B[37m"};constructor(e={}){this.config={colors:!0,level:"log",...e}}formatData(e,t){let{format:o="text",timestamp:r=!0,prefix:s,metadata:i}=t||{},c="";if(r){let l=new Date().toISOString();c+=this.config.colors?`${this.colors.cyan}[${l}]${this.colors.reset} `:`[${l}] `}switch(s&&(c+=this.config.colors?`${this.colors.bright}${s}${this.colors.reset} `:`${s} `),o){case"json":c+=JSON.stringify(e,null,2);break;case"markdown":typeof e=="object"?c+=this.toMarkdown(e):c+=String(e);break;case"text":default:typeof e=="object"?c+=JSON.stringify(e,null,2):c+=String(e);break}return i&&Object.keys(i).length>0&&(c+=`
311
+ `,t.enqueue(o)}})}}});var Tn,jo=w(()=>{"use strict";Tn=(n,e)=>{let t=n.getReader();return new ReadableStream({async start(o){try{for(;;){let{done:r,value:s}=await t.read();if(r){o.close();break}let i=await e(s,o);i!==void 0&&o.enqueue(i)}}catch(r){o.error(r)}finally{t.releaseLock()}}})}});var Ho=w(()=>{"use strict";Fo();Uo();jo()});var pi=w(()=>{"use strict"});var ct,zo=w(()=>{"use strict";ct=class{type="console";config;colors={reset:"\x1B[0m",bright:"\x1B[1m",dim:"\x1B[2m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",magenta:"\x1B[35m",cyan:"\x1B[36m",white:"\x1B[37m"};constructor(e={}){this.config={colors:!0,level:"log",...e}}formatData(e,t){let{format:o="text",timestamp:r=!0,prefix:s,metadata:i}=t||{},c="";if(r){let l=new Date().toISOString();c+=this.config.colors?`${this.colors.cyan}[${l}]${this.colors.reset} `:`[${l}] `}switch(s&&(c+=this.config.colors?`${this.colors.bright}${s}${this.colors.reset} `:`${s} `),o){case"json":c+=JSON.stringify(e,null,2);break;case"markdown":typeof e=="object"?c+=this.toMarkdown(e):c+=String(e);break;case"text":default:typeof e=="object"?c+=JSON.stringify(e,null,2):c+=String(e);break}return i&&Object.keys(i).length>0&&(c+=`
312
312
  `+(this.config.colors?`${this.colors.dim}`:""),c+="Metadata: "+JSON.stringify(i,null,2),this.config.colors&&(c+=this.colors.reset)),c}toMarkdown(e,t=0){let o=" ".repeat(t);return Array.isArray(e)?e.map(r=>typeof r=="object"?`${o}-
313
313
  ${this.toMarkdown(r,t+1)}`:`${o}- ${r}`).join(`
314
314
  `):typeof e=="object"&&e!==null?Object.entries(e).map(([r,s])=>typeof s=="object"&&s!==null?`${o}${r}:
315
315
  ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
316
- `):`${o}${e}`}async output(e,t={}){try{let o=this.formatData(e,t);switch(this.config.level||"log"){case"info":console.info(o);break;case"warn":console.warn(o);break;case"error":console.error(o);break;case"debug":console.debug(o);break;case"log":default:console.log(o);break}return!0}catch(o){return console.error("[ConsoleOutputHandler] Output failed:",o),!1}}}});var lt,Ho=w(()=>{"use strict";lt=class{type="webhook";config;defaultTimeout=3e4;constructor(e){if(!e.url)throw new Error("Webhook URL is required");this.config={method:"POST",retry:{maxAttempts:3,backoffMs:1e3},silent:!1,...e}}buildHeaders(){let e={"Content-Type":"application/json",...this.config.headers||{}};if(this.config.auth)switch(this.config.auth.type){case"bearer":this.config.auth.token&&(e.Authorization=`Bearer ${this.config.auth.token}`);break;case"basic":if(this.config.auth.username&&this.config.auth.password){let t=Buffer.from(`${this.config.auth.username}:${this.config.auth.password}`).toString("base64");e.Authorization=`Basic ${t}`}break;case"custom":this.config.auth.custom&&(e[this.config.auth.custom.header]=this.config.auth.custom.value);break}return e}buildBody(e,t){let{format:o="json",timestamp:r=!0,prefix:s,metadata:i}=t||{},c={data:e};return r&&(c.timestamp=new Date().toISOString()),s&&(c.prefix=s),i&&Object.keys(i).length>0&&(c.metadata=i),c}async sendRequest(e,t,o,r,s){let i=new AbortController,c=setTimeout(()=>i.abort(),s);try{let l=await fetch(e,{method:t,headers:o,body:JSON.stringify(r),signal:i.signal});if(clearTimeout(c),!l.ok)throw new Error(`HTTP ${l.status}: ${l.statusText}`);return l}catch(l){throw clearTimeout(c),l}}delay(e){return new Promise(t=>setTimeout(t,e))}async sendWithRetry(e,t,o,r,s,i){let c=null;for(let l=1;l<=i.maxAttempts;l++)try{return await this.sendRequest(e,t,o,r,s)}catch(a){if(c=a,l===i.maxAttempts)break;let u=i.backoffMs*Math.pow(2,l-1);console.warn(`[WebhookOutputHandler] Request failed (attempt ${l}/${i.maxAttempts}), retrying in ${u}ms...`,a.message),await this.delay(u)}throw c}async output(e,t={}){let o=t.timeout||this.defaultTimeout;try{let r=this.buildHeaders(),s=this.buildBody(e,t),i=await this.sendWithRetry(this.config.url,this.config.method,r,s,o,this.config.retry);return!0}catch(r){let s=r instanceof Error?r.message:String(r);if(this.config.silent)return console.error(`[WebhookOutputHandler] Failed to send data: ${s}`),!1;throw new Error(`Webhook output failed: ${s}`)}}}});import{writeFileSync as bl,existsSync as Tl,mkdirSync as vl}from"fs";import{join as ui}from"path";import{tmpdir as Cl}from"os";var ut,zo=w(()=>{"use strict";ut=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let t=Cl();this.baseDir=ui(t,this.config.subdirectory),this.ensureDir()}ensureDir(){try{Tl(this.baseDir)||vl(this.baseDir,{recursive:!0})}catch{}}getFilePath(e){let t=this.config.prefix||"session",o=this.config.extension?`.${this.config.extension}`:"",r;if(this.config.includeTimestamp){let s=Date.now();r=`${t}-${e}-${s}${o}`}else r=`${t}-${e}${o}`;return ui(this.baseDir,r)}async output(e,t={}){try{let o=t.metadata?.sessionId;if(!o)return!1;let r={...e,timestamp:Date.now(),sessionId:o},s=this.getFilePath(o);return bl(s,JSON.stringify(r,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Jo,Je,di=w(()=>{"use strict";jo();Ho();zo();Jo=class{handlers=new Map;defaultOptions={};registerHandler(e,t){this.handlers.set(e,t)}registerHandlers(e){for(let t of e)if(t.enabled!==!1)try{let o=this.createHandler(t),r=t.type+"_"+Date.now();this.registerHandler(r,o)}catch(o){console.error(`[OutputManager] Failed to register ${t.type} handler:`,o)}}createHandler(e){switch(e.type){case"console":return new ct(e.config);case"webhook":return new lt(e.config);case"temp-file":return new ut(e.config);default:throw new Error(`Unknown output handler type: ${e.type}`)}}unregisterHandler(e){return this.handlers.delete(e)}getHandler(e){return this.handlers.get(e)}getAllHandlers(){return new Map(this.handlers)}clearHandlers(){this.handlers.clear()}setDefaultOptions(e){this.defaultOptions={...this.defaultOptions,...e}}getDefaultOptions(){return{...this.defaultOptions}}async output(e,t){let o={...this.defaultOptions,...t},r={success:[],failed:[]},s=Array.from(this.handlers.entries()).map(async([i,c])=>{try{await c.output(e,o)?r.success.push(i):r.failed.push(i)}catch(l){console.error(`[OutputManager] Handler ${i} failed:`,l),r.failed.push(i)}});return await Promise.all(s),r}async outputTo(e,t,o){let r={...this.defaultOptions,...o},s={success:[],failed:[]},i=e.map(async c=>{let l=this.handlers.get(c);if(!l){console.warn(`[OutputManager] Handler ${c} not found`),s.failed.push(c);return}try{await l.output(t,r)?s.success.push(c):s.failed.push(c)}catch(a){console.error(`[OutputManager] Handler ${c} failed:`,a),s.failed.push(c)}});return await Promise.all(i),s}async outputToType(e,t,o){let r=Array.from(this.handlers.entries()).filter(([s,i])=>i.type===e).map(([s])=>s);return this.outputTo(r,t,o)}},Je=new Jo});var Bo=w(()=>{"use strict";li();jo();Ho();zo();di()});import xl from"fastify-plugin";function pi(n,e,t=0){if(n<=0)return 0;let o=Math.max((e-t)/1e3,Sl);return Math.round(n/o)}function pt(n){let e=(n.match(/[\u4e00-\u9fa5]/g)||[]).length,t=n.length-e;return Math.ceil(e/1.5+t/4)}async function fi(n,e,t,o=!1){let r=o?"[Token Speed Final]":"[Token Speed]",s={requestId:n.requestId.substring(0,8),sessionId:n.sessionId,stream:n.stream,tokenCount:n.tokenCount,tokensPerSecond:n.tokensPerSecond,timeToFirstToken:n.timeToFirstToken?`${n.timeToFirstToken}ms`:"N/A",duration:`${((n.lastTokenTime-n.startTime)/1e3).toFixed(2)}s`,timestamp:Date.now()},i={prefix:r,metadata:{sessionId:n.sessionId},...t};for(let c of e)try{await Je.outputToType(c,s,i)}catch(l){console.error(`[TokenSpeedPlugin] Failed to output to ${c}:`,l)}}var dt,Wo,Sl,Tn,mi=w(()=>{"use strict";Uo();Bo();Me();dt=new Map,Wo=new Map,Sl=1,Tn={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:xl(async(n,e)=>{let t={reporter:["console","temp-file"],...e},o=Array.isArray(t.reporter)?t.reporter:[t.reporter];if(t.outputHandlers&&t.outputHandlers.length>0)Je.registerHandlers(t.outputHandlers);else{let i=[];for(let c of o)c==="console"?i.push({type:"console",enabled:!0,config:{colors:!0,level:"log"}}):c==="temp-file"?i.push({type:"temp-file",enabled:!0,config:{subdirectory:"claude-code-router",extension:"json",includeTimestamp:!0,prefix:"session"}}):c==="webhook"&&console.warn("[TokenSpeedPlugin] Webhook reporter requires explicit configuration in outputHandlers");i.length>0&&Je.registerHandlers(i)}t.outputOptions&&Je.setDefaultOptions(t.outputOptions);let r=async i=>{let c=n.tokenizerService;if(!c)return n.log?.warn("TokenizerService not available"),null;if(!i.provider||!i.model)return null;let l=i.provider,a=i.model,u=`${l}:${a}`;if(Wo.has(u))return Wo.get(u);let d=c.getTokenizerConfigForModel(l,a);if(!d)return n.log?.debug(`No tokenizer config for ${l}:${a}, using fallback`),null;try{let m=await c.getTokenizer(d,l);return Wo.set(u,m),n.log?.info(`Created tokenizer for ${l}:${a} - ${m.name}`),m}catch(m){return n.log?.warn(`Failed to create tokenizer for ${l}:${a}: ${m.message}`),null}},s=i=>{let c=new URL(`http://127.0.0.1${i}`).pathname;return c.endsWith("/v1/messages")||c.endsWith("/v1/responses")};n.addHook("onRequest",async i=>{s(i.url)&&(i.requestStartTime=performance.now())}),n.addHook("onSend",async(i,c,l)=>{let a=i.requestStartTime;if(!a)return;let u=i.id||Date.now().toString(),d=i.usageSessionId;if(!d)try{d=ye(i.body?.metadata?.user_id)}catch{}if(!d){let g=i.headers?.["x-request-id"],_=Array.isArray(g)?g[0]:g;d=(typeof _=="string"?_:void 0)||u}let m=await r(i);if(l instanceof ReadableStream){dt.set(u,{requestId:u,sessionId:d,startTime:a,lastTokenTime:a,tokenCount:0,tokensPerSecond:0,stream:!0});let[g,_]=l.tee(),k=async()=>{let h=null,b=async T=>{let x=dt.get(u);if(!x)return;let C=x.firstTokenTime?x.firstTokenTime-x.startTime:0,v=(T?x.lastTokenTime:performance.now())-x.startTime;x.tokensPerSecond=pi(x.tokenCount,v,C),await fi(x,o,t.outputOptions,T).catch(P=>{n.log?.warn(`Failed to output streaming stats: ${P.message}`)})};try{let x=_.pipeThrough(new TextDecoderStream).pipeThrough(new ae).getReader();for(h=setInterval(async()=>{dt.get(u)&&await b(!1)},1e3);;){let{done:C,value:S}=await x.read();if(C)break;let v=S,P=dt.get(u);if(!P)continue;let R=performance.now(),O=v.event||v.data?.type,E=v.event==="content_block_start"||v.event==="content_block_delta"||v.event==="text_block"||v.event==="content_block"||O==="response.output_text.delta"||O==="response.function_call_arguments.delta"||O==="response.reasoning_summary_text.delta";!P.firstTokenTime&&E&&(P.firstTokenTime=R,P.timeToFirstToken=Math.round(R-P.startTime));let A="";if(v.event==="content_block_delta"&&v.data?.delta){let N=v.data.delta.type;N==="text_delta"?A=v.data.delta.text||"":N==="input_json_delta"?A=v.data.delta.partial_json||"":N==="thinking_delta"&&(A=v.data.delta.thinking||"")}if((O==="response.output_text.delta"||O==="response.function_call_arguments.delta"||O==="response.reasoning_summary_text.delta")&&(A=typeof v.data?.delta=="string"?v.data.delta:""),A){let N=m&&m.encodeText?m.encodeText(A).length:pt(A);P.tokenCount+=N,P.lastTokenTime=R}(v.event==="message_stop"||O==="response.completed"||O==="response.failed")&&(h&&(clearInterval(h),h=null),await b(!0),dt.delete(u))}}catch(T){h&&clearInterval(h),T.name!=="AbortError"&&T.code!=="ERR_STREAM_PREMATURE_CLOSE"&&n.log?.warn(`Error processing token stats: ${T.message}`)}};return i.tokenSpeedCapturePromise=k().catch(h=>{console.log(h),n.log?.warn(`Background stats processing failed: ${h.message}`)}),g}let f=performance.now(),y=0,p=!1;if(l&&typeof l=="string")try{let g=JSON.parse(l);if(typeof g.usage?.output_tokens=="number")y=g.usage.output_tokens,p=!0;else{let _=g.content??g.message?.content;if(_!==void 0)if(p=!0,m)Array.isArray(_)?y=_.reduce((k,h)=>{if(h.type==="text"){let b=h.text||"";return k+(m.encodeText?m.encodeText(b).length:pt(b))}return k},0):typeof _=="string"&&(y=m.encodeText?m.encodeText(_).length:pt(_));else{let k=Array.isArray(_)?_.map(h=>h.text).join(""):typeof _=="string"?_:"";y=pt(k)}}}catch{}if(p){let g=f-a,_={requestId:u,sessionId:d,startTime:a,lastTokenTime:f,tokenCount:y,tokensPerSecond:pi(y,g),stream:!1};await fi(_,o,t.outputOptions,!0)}return l})})}});var Go=w(()=>{"use strict";ai();ci();mi();Bo()});import ee from"node:fs/promises";import gi from"json5";import be from"node:path";import{CONFIG_FILE as te,HOME_DIR as vn,PLUGINS_DIR as Rl}from"@wengine-ai/claude-code-router-shared";function hi(){if(process.env.VITEST||process.env.CI)throw new Error("Config write attempted during test/CI. Set CCR_CONFIG_DIR to a temp dir to isolate.")}var Ko,ft,Cn,Qo,se,xn,Be,wl,Vo,Sn=w(()=>{"use strict";Ko=n=>{if(typeof n=="string")return n.replace(/\$\{([^}]+)\}|\$([A-Z_][A-Z0-9_]*)/g,(e,t,o)=>{let r=t||o;return process.env[r]||e});if(Array.isArray(n))return n.map(Ko);if(n!==null&&typeof n=="object"){let e={};for(let[t,o]of Object.entries(n))e[t]=Ko(o);return e}return n},ft=async n=>{try{await ee.access(n)}catch{await ee.mkdir(n,{recursive:!0})}},Cn=async()=>{await ft(vn),await ft(Rl),await ft(be.join(vn,"logs"))},Qo=async()=>{let n=await ee.readFile(te,"utf-8");return gi.parse(n)},se=async()=>{try{let n=await ee.readFile(te,"utf-8");try{let e=gi.parse(n);return Ko(e)}catch(e){console.error(`Failed to parse config file at ${te}`),console.error("Error details:",e.message),console.error("Please check your config file syntax."),process.exit(1)}}catch(n){if(n.code==="ENOENT")try{await Cn();let e=await xn();e&&console.log(`Backed up existing configuration file to ${e}`);let t={PORT:3456,Providers:[],Router:{}};return await Be(t),console.log("Created minimal default configuration file at ~/.claude-code-router/config.json"),console.log("Please edit this file with your actual configuration."),t}catch(e){console.error("Failed to create default configuration:",e.message),process.exit(1)}else console.error(`Failed to read config file at ${te}`),console.error("Error details:",n.message),process.exit(1)}};xn=async()=>{hi();try{if(await ee.access(te).then(()=>!0).catch(()=>!1)){let n=new Date().toISOString().replace(/[:.]/g,"-"),e=`${te}.${n}.bak`;await ee.copyFile(te,e);try{let t=be.dirname(te),o=be.basename(te),s=(await ee.readdir(t)).filter(i=>i.startsWith(o)&&i.endsWith(".bak")).sort().reverse();if(s.length>3)for(let i=3;i<s.length;i++){let c=be.join(t,s[i]);await ee.unlink(c)}}catch(t){console.warn("Failed to clean up old backups:",t)}return e}}catch(n){console.error("Failed to backup config file:",n)}return null},Be=async n=>{hi(),await ft(vn),await wl();let e=`${JSON.stringify(n,null,2)}`;await ee.writeFile(te,e)},wl=async()=>{if(!await ee.access(te).then(()=>!0).catch(()=>!1))return;let e=be.join(vn,"config-history");await ft(e);let t=new Date().toISOString().replace(/[:.]/g,"-"),o=be.join(e,`config-${t}.json`);await ee.copyFile(te,o);try{let s=(await ee.readdir(e)).filter(i=>i.startsWith("config-")&&i.endsWith(".json")).sort().reverse();if(s.length>50)for(let i of s.slice(50))await ee.unlink(be.join(e,i))}catch(r){console.warn("Failed to prune config-history:",r)}},Vo=async()=>{let n=await se();return Object.assign(process.env,n),n}});function Pl(n){return n?.cache_read_input_tokens??n?.input_tokens_details?.cached_tokens??n?.prompt_tokens_details?.cached_tokens??0}function El(n){return n?.cache_creation_input_tokens??n?.input_tokens_details?.cache_creation_tokens??n?.input_tokens_details?.cache_write_tokens??n?.prompt_tokens_details?.cache_creation_tokens??n?.prompt_tokens_details?.cache_write_tokens??0}function We(n){if(!(!n||typeof n!="object"))return{...n,input_tokens:n.input_tokens??n.prompt_tokens??0,output_tokens:n.output_tokens??n.completion_tokens??0,cache_read_input_tokens:Pl(n),cache_creation_input_tokens:El(n)}}function Rn(n,e,t){let o=t?{}:n||{},r=e||{};return{...o,input_tokens:r.input_tokens||o.input_tokens||0,output_tokens:r.output_tokens||o.output_tokens||0,cache_read_input_tokens:r.cache_read_input_tokens||o.cache_read_input_tokens||0,cache_creation_input_tokens:r.cache_creation_input_tokens||o.cache_creation_input_tokens||0}}var Yo=w(()=>{"use strict"});function wn(n,e,t){if(n){if(typeof n=="string"){let o=n.indexOf(",");if(o>0){let r=n.slice(0,o).trim(),s=n.slice(o+1).trim();r&&s&&e.has(r)&&t.add(Xo(r,s))}return}if(Array.isArray(n)){for(let o of n)wn(o,e,t);return}if(typeof n=="object")for(let o of Object.values(n))wn(o,e,t)}}function yi(n){let e=n?.Providers||n?.providers||[],t=new Set,o=new Set;for(let r of e){let s=r?.name;if(!s)continue;t.add(s);let i=Array.isArray(r?.models)?r.models:[];for(let c of i)c&&o.add(Xo(s,c))}return wn(n?.Router,t,o),wn(n?.fallback,t,o),o}function mt(n,e){try{let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=yi(n),r=0;for(let s of t.getAllStates()||[]){let i=s?.provider,c=s?.model;!i||!c||o.has(Xo(i,c))||(t.recover(i,c),r++)}return r>0&&e?.info?.(`[health-reconcile] Pruned ${r} orphaned provider-health entr${r===1?"y":"ies"}`),r}catch(t){return e?.warn?.(`[health-reconcile] Failed to reconcile health store: ${t?.message||t}`),0}}function Pn(n,e){try{if(!n)return 0;let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=0;for(let r of t.getAllStates()||[])r?.provider===n&&r?.model&&(t.recover(n,r.model),o++);return o>0&&e?.info?.(`[health-reconcile] Cleared ${o} health entr${o===1?"y":"ies"} for probed provider ${n}`),o}catch(t){return e?.warn?.(`[health-reconcile] Failed to clear provider health for ${n}: ${t?.message||t}`),0}}var Ol,Xo,En=w(()=>{"use strict";oe();Ol="\0",Xo=(n,e)=>`${n}${Ol}${e}`});function Al(n){return/\[1m\]/i.test(n)?1e6:2e5}function Il(n){let e=n.toLowerCase();return e.includes("haiku")||e.includes("mini")?8192:32e3}function Ml(n,e=n){let t=Al(n),o=Il(n);return{type:"model",id:n,display_name:e,created_at:"2024-01-01T00:00:00Z",context_window:t,contextWindow:t,max_input_tokens:t,maxInputTokens:t,max_tokens:o,maxTokens:o,max_output_tokens:o,maxOutputTokens:o,capabilities:{context_window:t,contextWindow:t,max_input_tokens:t,maxInputTokens:t,max_tokens:o,maxTokens:o,max_output_tokens:o,maxOutputTokens:o}}}function On(n){let e=new Map,t=(o,r=o)=>{!o||e.has(o)||e.set(o,Ml(o,r))};t("ccr-opus","CCR Opus"),t("ccr-sonnet","CCR Sonnet"),t("ccr-haiku","CCR Haiku"),t("ccr-opus[1m]","CCR Opus 1M"),t("ccr-sonnet[1m]","CCR Sonnet 1M"),t("ccr-haiku[1m]","CCR Haiku 1M");for(let o of n.Providers||n.providers||[])if(!(!o?.name||!Array.isArray(o.models)))for(let r of o.models)typeof r!="string"||!r||(t(r,r),t(`${o.name},${r}`,`${o.name}/${r}`));return Array.from(e.values())}var qo=w(()=>{"use strict"});function $l(n){let e=n.ip||n.connection?.remoteAddress||"";return e==="127.0.0.1"||e==="::1"||e==="::ffff:127.0.0.1"}var An,Zo=w(()=>{"use strict";An=n=>async(e,t,o)=>{if(["/","/health"].includes(e.url)||e.url.startsWith("/ui")||$l(e))return o();let s=n.Providers||n.providers||[];if(!s||s.length===0)return o();let i=n.APIKEY;if(!i){let u=[`http://127.0.0.1:${n.PORT||3456}`,`http://localhost:${n.PORT||3456}`];if(e.headers.origin&&!u.includes(e.headers.origin)){t.status(403).send("CORS not allowed for this origin");return}else t.header("Access-Control-Allow-Origin",`http://127.0.0.1:${n.PORT||3456}`),t.header("Access-Control-Allow-Origin",`http://localhost:${n.PORT||3456}`);return o()}let c=e.headers.authorization||e.headers["x-api-key"],l=Array.isArray(c)?c[0]:c||"";if(!l){t.log.warn({url:e.url,headers:Object.keys(e.headers)},"[AUTH] No auth header"),t.status(401).send("APIKEY is missing");return}let a="";if(l.startsWith("Bearer")?a=l.split(" ")[1]:a=l,a!==i){t.log.warn({url:e.url,gotPrefix:a.slice(0,10),gotSuffix:a.slice(-6),gotLen:a.length,expectedPrefix:i.slice(0,6),expectedSuffix:i.slice(-6),expectedLen:i.length},"[AUTH] Key mismatch"),t.status(401).send("Invalid API key");return}o()}});var Si={};jn(Si,{append:()=>_t,clear:()=>sr,close:()=>tu,getOldestRecordTimestamp:()=>nu,query:()=>rr,querySummary:()=>kt,readTokenSpeedStats:()=>ir});import{existsSync as nr,mkdirSync as Nl,readFileSync as tr,readdirSync as Ll}from"fs";import{join as Ke}from"path";import{homedir as Dl,tmpdir as Fl}from"os";import Ul from"better-sqlite3";function Bl(){nr(Mn)||Nl(Mn,{recursive:!0})}function yt(){if(me)return me;Bl();let n=new Ul(jl);n.pragma("journal_mode = WAL"),n.pragma("synchronous = NORMAL"),n.pragma("busy_timeout = 5000");let e=n.pragma("user_version",{simple:!0})||0;return e<1&&(Wl(n),n.pragma("user_version = 1")),e<2&&(n.prepare("PRAGMA table_info(usage_records)").all().some(o=>o.name==="upstream_model")||n.exec("ALTER TABLE usage_records ADD COLUMN upstream_model TEXT"),n.pragma("user_version = 2")),Kl(n),vi(n,!0),or=n.prepare(`
316
+ `):`${o}${e}`}async output(e,t={}){try{let o=this.formatData(e,t);switch(this.config.level||"log"){case"info":console.info(o);break;case"warn":console.warn(o);break;case"error":console.error(o);break;case"debug":console.debug(o);break;case"log":default:console.log(o);break}return!0}catch(o){return console.error("[ConsoleOutputHandler] Output failed:",o),!1}}}});var lt,Bo=w(()=>{"use strict";lt=class{type="webhook";config;defaultTimeout=3e4;constructor(e){if(!e.url)throw new Error("Webhook URL is required");this.config={method:"POST",retry:{maxAttempts:3,backoffMs:1e3},silent:!1,...e}}buildHeaders(){let e={"Content-Type":"application/json",...this.config.headers||{}};if(this.config.auth)switch(this.config.auth.type){case"bearer":this.config.auth.token&&(e.Authorization=`Bearer ${this.config.auth.token}`);break;case"basic":if(this.config.auth.username&&this.config.auth.password){let t=Buffer.from(`${this.config.auth.username}:${this.config.auth.password}`).toString("base64");e.Authorization=`Basic ${t}`}break;case"custom":this.config.auth.custom&&(e[this.config.auth.custom.header]=this.config.auth.custom.value);break}return e}buildBody(e,t){let{format:o="json",timestamp:r=!0,prefix:s,metadata:i}=t||{},c={data:e};return r&&(c.timestamp=new Date().toISOString()),s&&(c.prefix=s),i&&Object.keys(i).length>0&&(c.metadata=i),c}async sendRequest(e,t,o,r,s){let i=new AbortController,c=setTimeout(()=>i.abort(),s);try{let l=await fetch(e,{method:t,headers:o,body:JSON.stringify(r),signal:i.signal});if(clearTimeout(c),!l.ok)throw new Error(`HTTP ${l.status}: ${l.statusText}`);return l}catch(l){throw clearTimeout(c),l}}delay(e){return new Promise(t=>setTimeout(t,e))}async sendWithRetry(e,t,o,r,s,i){let c=null;for(let l=1;l<=i.maxAttempts;l++)try{return await this.sendRequest(e,t,o,r,s)}catch(a){if(c=a,l===i.maxAttempts)break;let u=i.backoffMs*Math.pow(2,l-1);console.warn(`[WebhookOutputHandler] Request failed (attempt ${l}/${i.maxAttempts}), retrying in ${u}ms...`,a.message),await this.delay(u)}throw c}async output(e,t={}){let o=t.timeout||this.defaultTimeout;try{let r=this.buildHeaders(),s=this.buildBody(e,t),i=await this.sendWithRetry(this.config.url,this.config.method,r,s,o,this.config.retry);return!0}catch(r){let s=r instanceof Error?r.message:String(r);if(this.config.silent)return console.error(`[WebhookOutputHandler] Failed to send data: ${s}`),!1;throw new Error(`Webhook output failed: ${s}`)}}}});import{writeFileSync as xl,existsSync as Sl,mkdirSync as Rl}from"fs";import{join as fi}from"path";import{tmpdir as wl}from"os";var ut,Jo=w(()=>{"use strict";ut=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let t=wl();this.baseDir=fi(t,this.config.subdirectory),this.ensureDir()}ensureDir(){try{Sl(this.baseDir)||Rl(this.baseDir,{recursive:!0})}catch{}}getFilePath(e){let t=this.config.prefix||"session",o=this.config.extension?`.${this.config.extension}`:"",r;if(this.config.includeTimestamp){let s=Date.now();r=`${t}-${e}-${s}${o}`}else r=`${t}-${e}${o}`;return fi(this.baseDir,r)}async output(e,t={}){try{let o=t.metadata?.sessionId;if(!o)return!1;let r={...e,timestamp:Date.now(),sessionId:o},s=this.getFilePath(o);return xl(s,JSON.stringify(r,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Wo,Je,mi=w(()=>{"use strict";zo();Bo();Jo();Wo=class{handlers=new Map;defaultOptions={};registerHandler(e,t){this.handlers.set(e,t)}registerHandlers(e){for(let t of e)if(t.enabled!==!1)try{let o=this.createHandler(t),r=t.type+"_"+Date.now();this.registerHandler(r,o)}catch(o){console.error(`[OutputManager] Failed to register ${t.type} handler:`,o)}}createHandler(e){switch(e.type){case"console":return new ct(e.config);case"webhook":return new lt(e.config);case"temp-file":return new ut(e.config);default:throw new Error(`Unknown output handler type: ${e.type}`)}}unregisterHandler(e){return this.handlers.delete(e)}getHandler(e){return this.handlers.get(e)}getAllHandlers(){return new Map(this.handlers)}clearHandlers(){this.handlers.clear()}setDefaultOptions(e){this.defaultOptions={...this.defaultOptions,...e}}getDefaultOptions(){return{...this.defaultOptions}}async output(e,t){let o={...this.defaultOptions,...t},r={success:[],failed:[]},s=Array.from(this.handlers.entries()).map(async([i,c])=>{try{await c.output(e,o)?r.success.push(i):r.failed.push(i)}catch(l){console.error(`[OutputManager] Handler ${i} failed:`,l),r.failed.push(i)}});return await Promise.all(s),r}async outputTo(e,t,o){let r={...this.defaultOptions,...o},s={success:[],failed:[]},i=e.map(async c=>{let l=this.handlers.get(c);if(!l){console.warn(`[OutputManager] Handler ${c} not found`),s.failed.push(c);return}try{await l.output(t,r)?s.success.push(c):s.failed.push(c)}catch(a){console.error(`[OutputManager] Handler ${c} failed:`,a),s.failed.push(c)}});return await Promise.all(i),s}async outputToType(e,t,o){let r=Array.from(this.handlers.entries()).filter(([s,i])=>i.type===e).map(([s])=>s);return this.outputTo(r,t,o)}},Je=new Wo});var Go=w(()=>{"use strict";pi();zo();Bo();Jo();mi()});import Pl from"fastify-plugin";function gi(n,e,t=0){if(n<=0)return 0;let o=Math.max((e-t)/1e3,El);return Math.round(n/o)}function pt(n){let e=(n.match(/[\u4e00-\u9fa5]/g)||[]).length,t=n.length-e;return Math.ceil(e/1.5+t/4)}async function hi(n,e,t,o=!1){let r=o?"[Token Speed Final]":"[Token Speed]",s={requestId:n.requestId.substring(0,8),sessionId:n.sessionId,stream:n.stream,tokenCount:n.tokenCount,tokensPerSecond:n.tokensPerSecond,timeToFirstToken:n.timeToFirstToken?`${n.timeToFirstToken}ms`:"N/A",duration:`${((n.lastTokenTime-n.startTime)/1e3).toFixed(2)}s`,timestamp:Date.now()},i={prefix:r,metadata:{sessionId:n.sessionId},...t};for(let c of e)try{await Je.outputToType(c,s,i)}catch(l){console.error(`[TokenSpeedPlugin] Failed to output to ${c}:`,l)}}var dt,Ko,El,vn,yi=w(()=>{"use strict";Ho();Go();$e();dt=new Map,Ko=new Map,El=1,vn={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:Pl(async(n,e)=>{let t={reporter:["console","temp-file"],...e},o=Array.isArray(t.reporter)?t.reporter:[t.reporter];if(t.outputHandlers&&t.outputHandlers.length>0)Je.registerHandlers(t.outputHandlers);else{let i=[];for(let c of o)c==="console"?i.push({type:"console",enabled:!0,config:{colors:!0,level:"log"}}):c==="temp-file"?i.push({type:"temp-file",enabled:!0,config:{subdirectory:"claude-code-router",extension:"json",includeTimestamp:!0,prefix:"session"}}):c==="webhook"&&console.warn("[TokenSpeedPlugin] Webhook reporter requires explicit configuration in outputHandlers");i.length>0&&Je.registerHandlers(i)}t.outputOptions&&Je.setDefaultOptions(t.outputOptions);let r=async i=>{let c=n.tokenizerService;if(!c)return n.log?.warn("TokenizerService not available"),null;if(!i.provider||!i.model)return null;let l=i.provider,a=i.model,u=`${l}:${a}`;if(Ko.has(u))return Ko.get(u);let d=c.getTokenizerConfigForModel(l,a);if(!d)return n.log?.debug(`No tokenizer config for ${l}:${a}, using fallback`),null;try{let f=await c.getTokenizer(d,l);return Ko.set(u,f),n.log?.info(`Created tokenizer for ${l}:${a} - ${f.name}`),f}catch(f){return n.log?.warn(`Failed to create tokenizer for ${l}:${a}: ${f.message}`),null}},s=i=>{let c=new URL(`http://127.0.0.1${i}`).pathname;return c.endsWith("/v1/messages")||c.endsWith("/v1/responses")};n.addHook("onRequest",async i=>{s(i.url)&&(i.requestStartTime=performance.now())}),n.addHook("onSend",async(i,c,l)=>{let a=i.requestStartTime;if(!a)return;let u=i.id||Date.now().toString(),d=i.usageSessionId;if(!d)try{d=_e(i.body?.metadata?.user_id)}catch{}if(!d){let p=i.headers?.["x-request-id"],_=Array.isArray(p)?p[0]:p;d=(typeof _=="string"?_:void 0)||u}let f=await r(i);if(l instanceof ReadableStream){dt.set(u,{requestId:u,sessionId:d,startTime:a,lastTokenTime:a,tokenCount:0,tokensPerSecond:0,stream:!0});let[p,_]=l.tee(),k=async()=>{let h=null,b=async T=>{let x=dt.get(u);if(!x)return;let C=x.firstTokenTime?x.firstTokenTime-x.startTime:0,v=(T?x.lastTokenTime:performance.now())-x.startTime;x.tokensPerSecond=gi(x.tokenCount,v,C),await hi(x,o,t.outputOptions,T).catch(P=>{n.log?.warn(`Failed to output streaming stats: ${P.message}`)})};try{let x=_.pipeThrough(new TextDecoderStream).pipeThrough(new ae).getReader();for(h=setInterval(async()=>{dt.get(u)&&await b(!1)},1e3);;){let{done:C,value:S}=await x.read();if(C)break;let v=S,P=dt.get(u);if(!P)continue;let R=performance.now(),O=v.event||v.data?.type,E=v.event==="content_block_start"||v.event==="content_block_delta"||v.event==="text_block"||v.event==="content_block"||O==="response.output_text.delta"||O==="response.function_call_arguments.delta"||O==="response.reasoning_summary_text.delta";!P.firstTokenTime&&E&&(P.firstTokenTime=R,P.timeToFirstToken=Math.round(R-P.startTime));let A="";if(v.event==="content_block_delta"&&v.data?.delta){let N=v.data.delta.type;N==="text_delta"?A=v.data.delta.text||"":N==="input_json_delta"?A=v.data.delta.partial_json||"":N==="thinking_delta"&&(A=v.data.delta.thinking||"")}if((O==="response.output_text.delta"||O==="response.function_call_arguments.delta"||O==="response.reasoning_summary_text.delta")&&(A=typeof v.data?.delta=="string"?v.data.delta:""),A){let N=f&&f.encodeText?f.encodeText(A).length:pt(A);P.tokenCount+=N,P.lastTokenTime=R}(v.event==="message_stop"||O==="response.completed"||O==="response.failed")&&(h&&(clearInterval(h),h=null),await b(!0),dt.delete(u))}}catch(T){h&&clearInterval(h),T.name!=="AbortError"&&T.code!=="ERR_STREAM_PREMATURE_CLOSE"&&n.log?.warn(`Error processing token stats: ${T.message}`)}};return i.tokenSpeedCapturePromise=k().catch(h=>{console.log(h),n.log?.warn(`Background stats processing failed: ${h.message}`)}),p}let g=performance.now(),y=0,m=!1;if(l&&typeof l=="string")try{let p=JSON.parse(l);if(typeof p.usage?.output_tokens=="number")y=p.usage.output_tokens,m=!0;else{let _=p.content??p.message?.content;if(_!==void 0)if(m=!0,f)Array.isArray(_)?y=_.reduce((k,h)=>{if(h.type==="text"){let b=h.text||"";return k+(f.encodeText?f.encodeText(b).length:pt(b))}return k},0):typeof _=="string"&&(y=f.encodeText?f.encodeText(_).length:pt(_));else{let k=Array.isArray(_)?_.map(h=>h.text).join(""):typeof _=="string"?_:"";y=pt(k)}}}catch{}if(m){let p=g-a,_={requestId:u,sessionId:d,startTime:a,lastTokenTime:g,tokenCount:y,tokensPerSecond:gi(y,p),stream:!1};await hi(_,o,t.outputOptions,!0)}return l})})}});var Qo=w(()=>{"use strict";ui();di();yi();Go()});import ee from"node:fs/promises";import _i from"json5";import Te from"node:path";import{CONFIG_FILE as te,HOME_DIR as Cn,PLUGINS_DIR as Ol}from"@wengine-ai/claude-code-router-shared";function ki(){if(process.env.VITEST||process.env.CI)throw new Error("Config write attempted during test/CI. Set CCR_CONFIG_DIR to a temp dir to isolate.")}var Vo,ft,xn,Yo,ne,Sn,fe,Al,Xo,Rn=w(()=>{"use strict";Vo=n=>{if(typeof n=="string")return n.replace(/\$\{([^}]+)\}|\$([A-Z_][A-Z0-9_]*)/g,(e,t,o)=>{let r=t||o;return process.env[r]||e});if(Array.isArray(n))return n.map(Vo);if(n!==null&&typeof n=="object"){let e={};for(let[t,o]of Object.entries(n))e[t]=Vo(o);return e}return n},ft=async n=>{try{await ee.access(n)}catch{await ee.mkdir(n,{recursive:!0})}},xn=async()=>{await ft(Cn),await ft(Ol),await ft(Te.join(Cn,"logs"))},Yo=async()=>{let n=await ee.readFile(te,"utf-8");return _i.parse(n)},ne=async()=>{try{let n=await ee.readFile(te,"utf-8");try{let e=_i.parse(n);return Vo(e)}catch(e){console.error(`Failed to parse config file at ${te}`),console.error("Error details:",e.message),console.error("Please check your config file syntax."),process.exit(1)}}catch(n){if(n.code==="ENOENT")try{await xn();let e=await Sn();e&&console.log(`Backed up existing configuration file to ${e}`);let t={PORT:3456,Providers:[],Router:{}};return await fe(t),console.log("Created minimal default configuration file at ~/.claude-code-router/config.json"),console.log("Please edit this file with your actual configuration."),t}catch(e){console.error("Failed to create default configuration:",e.message),process.exit(1)}else console.error(`Failed to read config file at ${te}`),console.error("Error details:",n.message),process.exit(1)}};Sn=async()=>{ki();try{if(await ee.access(te).then(()=>!0).catch(()=>!1)){let n=new Date().toISOString().replace(/[:.]/g,"-"),e=`${te}.${n}.bak`;await ee.copyFile(te,e);try{let t=Te.dirname(te),o=Te.basename(te),s=(await ee.readdir(t)).filter(i=>i.startsWith(o)&&i.endsWith(".bak")).sort().reverse();if(s.length>3)for(let i=3;i<s.length;i++){let c=Te.join(t,s[i]);await ee.unlink(c)}}catch(t){console.warn("Failed to clean up old backups:",t)}return e}}catch(n){console.error("Failed to backup config file:",n)}return null},fe=async n=>{ki(),await ft(Cn),await Al();let e=`${JSON.stringify(n,null,2)}`;await ee.writeFile(te,e)},Al=async()=>{if(!await ee.access(te).then(()=>!0).catch(()=>!1))return;let e=Te.join(Cn,"config-history");await ft(e);let t=new Date().toISOString().replace(/[:.]/g,"-"),o=Te.join(e,`config-${t}.json`);await ee.copyFile(te,o);try{let s=(await ee.readdir(e)).filter(i=>i.startsWith("config-")&&i.endsWith(".json")).sort().reverse();if(s.length>50)for(let i of s.slice(50))await ee.unlink(Te.join(e,i))}catch(r){console.warn("Failed to prune config-history:",r)}},Xo=async()=>{let n=await ne();return Object.assign(process.env,n),n}});function Il(n){return n?.cache_read_input_tokens??n?.input_tokens_details?.cached_tokens??n?.prompt_tokens_details?.cached_tokens??0}function Ml(n){return n?.cache_creation_input_tokens??n?.input_tokens_details?.cache_creation_tokens??n?.input_tokens_details?.cache_write_tokens??n?.prompt_tokens_details?.cache_creation_tokens??n?.prompt_tokens_details?.cache_write_tokens??0}function We(n){if(!(!n||typeof n!="object"))return{...n,input_tokens:n.input_tokens??n.prompt_tokens??0,output_tokens:n.output_tokens??n.completion_tokens??0,cache_read_input_tokens:Il(n),cache_creation_input_tokens:Ml(n)}}function wn(n,e,t){let o=t?{}:n||{},r=e||{};return{...o,input_tokens:r.input_tokens||o.input_tokens||0,output_tokens:r.output_tokens||o.output_tokens||0,cache_read_input_tokens:r.cache_read_input_tokens||o.cache_read_input_tokens||0,cache_creation_input_tokens:r.cache_creation_input_tokens||o.cache_creation_input_tokens||0}}var Zo=w(()=>{"use strict"});function Pn(n,e,t){if(n){if(typeof n=="string"){let o=n.indexOf(",");if(o>0){let r=n.slice(0,o).trim(),s=n.slice(o+1).trim();r&&s&&e.has(r)&&t.add(qo(r,s))}return}if(Array.isArray(n)){for(let o of n)Pn(o,e,t);return}if(typeof n=="object")for(let o of Object.values(n))Pn(o,e,t)}}function bi(n){let e=n?.Providers||n?.providers||[],t=new Set,o=new Set;for(let r of e){let s=r?.name;if(!s)continue;t.add(s);let i=Array.isArray(r?.models)?r.models:[];for(let c of i)c&&o.add(qo(s,c))}return Pn(n?.Router,t,o),Pn(n?.fallback,t,o),o}function mt(n,e){try{let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=bi(n),r=0;for(let s of t.getAllStates()||[]){let i=s?.provider,c=s?.model;!i||!c||o.has(qo(i,c))||(t.recover(i,c),r++)}return r>0&&e?.info?.(`[health-reconcile] Pruned ${r} orphaned provider-health entr${r===1?"y":"ies"}`),r}catch(t){return e?.warn?.(`[health-reconcile] Failed to reconcile health store: ${t?.message||t}`),0}}function En(n,e){try{if(!n)return 0;let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=0;for(let r of t.getAllStates()||[])r?.provider===n&&r?.model&&(t.recover(n,r.model),o++);return o>0&&e?.info?.(`[health-reconcile] Cleared ${o} health entr${o===1?"y":"ies"} for probed provider ${n}`),o}catch(t){return e?.warn?.(`[health-reconcile] Failed to clear provider health for ${n}: ${t?.message||t}`),0}}var $l,qo,On=w(()=>{"use strict";re();$l="\0",qo=(n,e)=>`${n}${$l}${e}`});function Nl(n){return/\[1m\]/i.test(n)?1e6:2e5}function Ll(n){let e=n.toLowerCase();return e.includes("haiku")||e.includes("mini")?8192:32e3}function Dl(n,e=n){let t=Nl(n),o=Ll(n);return{type:"model",id:n,display_name:e,created_at:"2024-01-01T00:00:00Z",context_window:t,contextWindow:t,max_input_tokens:t,maxInputTokens:t,max_tokens:o,maxTokens:o,max_output_tokens:o,maxOutputTokens:o,capabilities:{context_window:t,contextWindow:t,max_input_tokens:t,maxInputTokens:t,max_tokens:o,maxTokens:o,max_output_tokens:o,maxOutputTokens:o}}}function An(n){let e=new Map,t=(o,r=o)=>{!o||e.has(o)||e.set(o,Dl(o,r))};t("ccr-opus","CCR Opus"),t("ccr-sonnet","CCR Sonnet"),t("ccr-haiku","CCR Haiku"),t("ccr-opus[1m]","CCR Opus 1M"),t("ccr-sonnet[1m]","CCR Sonnet 1M"),t("ccr-haiku[1m]","CCR Haiku 1M");for(let o of n.Providers||n.providers||[])if(!(!o?.name||!Array.isArray(o.models)))for(let r of o.models)typeof r!="string"||!r||(t(r,r),t(`${o.name},${r}`,`${o.name}/${r}`));return Array.from(e.values())}var er=w(()=>{"use strict"});function Fl(n){let e=n.ip||n.connection?.remoteAddress||"";return e==="127.0.0.1"||e==="::1"||e==="::ffff:127.0.0.1"}var In,tr=w(()=>{"use strict";In=n=>async(e,t,o)=>{if(["/","/health"].includes(e.url)||e.url.startsWith("/ui")||Fl(e))return o();let s=n.Providers||n.providers||[];if(!s||s.length===0)return o();let i=n.APIKEY;if(!i){let u=[`http://127.0.0.1:${n.PORT||3456}`,`http://localhost:${n.PORT||3456}`];if(e.headers.origin&&!u.includes(e.headers.origin)){t.status(403).send("CORS not allowed for this origin");return}else t.header("Access-Control-Allow-Origin",`http://127.0.0.1:${n.PORT||3456}`),t.header("Access-Control-Allow-Origin",`http://localhost:${n.PORT||3456}`);return o()}let c=e.headers.authorization||e.headers["x-api-key"],l=Array.isArray(c)?c[0]:c||"";if(!l){t.log.warn({url:e.url,headers:Object.keys(e.headers)},"[AUTH] No auth header"),t.status(401).send("APIKEY is missing");return}let a="";if(l.startsWith("Bearer")?a=l.split(" ")[1]:a=l,a!==i){t.log.warn({url:e.url,gotPrefix:a.slice(0,10),gotSuffix:a.slice(-6),gotLen:a.length,expectedPrefix:i.slice(0,6),expectedSuffix:i.slice(-6),expectedLen:i.length},"[AUTH] Key mismatch"),t.status(401).send("Invalid API key");return}o()}});var Pi={};Hn(Pi,{append:()=>_t,clear:()=>ar,close:()=>su,getOldestRecordTimestamp:()=>iu,query:()=>ir,querySummary:()=>kt,readTokenSpeedStats:()=>cr});import{existsSync as rr,mkdirSync as Ul,readFileSync as or,readdirSync as jl}from"fs";import{join as Ke}from"path";import{homedir as Hl,tmpdir as zl}from"os";import Bl from"better-sqlite3";function Ql(){rr($n)||Ul($n,{recursive:!0})}function yt(){if(ge)return ge;Ql();let n=new Bl(Jl);n.pragma("journal_mode = WAL"),n.pragma("synchronous = NORMAL"),n.pragma("busy_timeout = 5000");let e=n.pragma("user_version",{simple:!0})||0;return e<1&&(Vl(n),n.pragma("user_version = 1")),e<2&&(n.prepare("PRAGMA table_info(usage_records)").all().some(o=>o.name==="upstream_model")||n.exec("ALTER TABLE usage_records ADD COLUMN upstream_model TEXT"),n.pragma("user_version = 2")),Xl(n),Si(n,!0),sr=n.prepare(`
317
317
  INSERT INTO usage_records (
318
318
  id, timestamp, session_id, provider, original_model, model, upstream_model, model_family,
319
319
  scenario_type, client_type,
@@ -327,7 +327,7 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
327
327
  @cache_read_input_tokens, @cache_creation_input_tokens, @ttft,
328
328
  @tokens_per_second, @duration_ms, @status, @error_message, @response_body
329
329
  )
330
- `),me=n,me}function Wl(n){n.exec(`
330
+ `),ge=n,ge}function Vl(n){n.exec(`
331
331
  CREATE TABLE IF NOT EXISTS usage_records (
332
332
  id TEXT PRIMARY KEY,
333
333
  timestamp TEXT NOT NULL,
@@ -364,12 +364,12 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
364
364
  CREATE INDEX IF NOT EXISTS idx_usage_records_scenario_timestamp ON usage_records(scenario_type, timestamp);
365
365
  CREATE INDEX IF NOT EXISTS idx_usage_records_client_timestamp ON usage_records(client_type, timestamp);
366
366
  CREATE INDEX IF NOT EXISTS idx_usage_records_session_timestamp ON usage_records(session_id, timestamp);
367
- `)}function Gl(n,e){return n.prepare("SELECT value FROM usage_meta WHERE key = ?").get(e)?.value}function gt(n,e,t){n.prepare(`
367
+ `)}function Yl(n,e){return n.prepare("SELECT value FROM usage_meta WHERE key = ?").get(e)?.value}function gt(n,e,t){n.prepare(`
368
368
  INSERT INTO usage_meta (key, value)
369
369
  VALUES (?, ?)
370
370
  ON CONFLICT(key) DO UPDATE SET value = excluded.value
371
- `).run(e,t)}function Kl(n){if(Gl(n,_i)||!nr(er))return;let e=Vl(n,"INSERT OR IGNORE"),t=tr(er,"utf-8"),o=0,r=0,s=0;n.transaction(()=>{for(let c of t.split(`
372
- `)){let l=c.trim();if(l)try{let a=JSON.parse(l);if(!Ql(a)){r++;continue}e.run(Ti($n(a))).changes>0?o++:s++}catch{r++}}gt(n,_i,new Date().toISOString()),gt(n,"legacy_usage_jsonl_migrated_path",er),gt(n,"legacy_usage_jsonl_imported_count",String(o)),gt(n,"legacy_usage_jsonl_skipped_count",String(r)),gt(n,"legacy_usage_jsonl_duplicate_count",String(s))})()}function Ql(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.id=="string"&&e.id.length>0&&typeof e.timestamp=="string"&&e.timestamp.length>0}function Vl(n,e="INSERT"){return n.prepare(`
371
+ `).run(e,t)}function Xl(n){if(Yl(n,Ti)||!rr(nr))return;let e=ql(n,"INSERT OR IGNORE"),t=or(nr,"utf-8"),o=0,r=0,s=0;n.transaction(()=>{for(let c of t.split(`
372
+ `)){let l=c.trim();if(l)try{let a=JSON.parse(l);if(!Zl(a)){r++;continue}e.run(xi(Nn(a))).changes>0?o++:s++}catch{r++}}gt(n,Ti,new Date().toISOString()),gt(n,"legacy_usage_jsonl_migrated_path",nr),gt(n,"legacy_usage_jsonl_imported_count",String(o)),gt(n,"legacy_usage_jsonl_skipped_count",String(r)),gt(n,"legacy_usage_jsonl_duplicate_count",String(s))})()}function Zl(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.id=="string"&&e.id.length>0&&typeof e.timestamp=="string"&&e.timestamp.length>0}function ql(n,e="INSERT"){return n.prepare(`
373
373
  ${e} INTO usage_records (
374
374
  id, timestamp, session_id, provider, original_model, model, upstream_model, model_family,
375
375
  scenario_type, client_type,
@@ -383,11 +383,11 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
383
383
  @cache_read_input_tokens, @cache_creation_input_tokens, @ttft,
384
384
  @tokens_per_second, @duration_ms, @status, @error_message, @response_body
385
385
  )
386
- `)}function Yl(n,e,t){if(n<=0||e==null||!Number.isFinite(e))return null;let o=t!=null&&Number.isFinite(t)?t:0,r=Math.max((e-o)/1e3,Hl);return Math.round(n/r)}function $n(n){let e=Number(n.outputTokens)||0,t=Number(n.durationMs),o=Number.isFinite(t)?t:null,r=n.stream?Te(n.ttft):null;return{...n,ttft:r,tokensPerSecond:Yl(e,o,r)}}function fe(n){return typeof n=="string"?n:n==null?"":String(n)}function In(n){if(n==null)return;let e=String(n);return e.length>0?e:void 0}function ht(n){let e=Number(n);return Number.isFinite(e)?e:0}function bi(n){return n==="success"?"success":"error"}function Ti(n){let e=$n(n);return{id:fe(e.id),timestamp:fe(e.timestamp),session_id:fe(e.sessionId),provider:fe(e.provider),original_model:fe(e.originalModel),model:fe(e.model),upstream_model:In(e.upstreamModel)??null,model_family:fe(e.modelFamily),scenario_type:fe(e.scenarioType),client_type:In(e.clientType)??null,stream:e.stream?1:0,input_tokens:Math.trunc(ht(e.inputTokens)),output_tokens:Math.trunc(ht(e.outputTokens)),cache_read_input_tokens:Math.trunc(ht(e.cacheReadInputTokens)),cache_creation_input_tokens:Math.trunc(ht(e.cacheCreationInputTokens)),ttft:Te(e.ttft),tokens_per_second:Te(e.tokensPerSecond),duration_ms:Math.trunc(ht(e.durationMs)),status:bi(e.status),error_message:In(e.errorMessage)??null,response_body:In(e.responseBody)??null}}function Xl(n){return $n({id:n.id,timestamp:n.timestamp,sessionId:n.session_id,provider:n.provider,originalModel:n.original_model,model:n.model,upstreamModel:n.upstream_model??void 0,modelFamily:n.model_family,scenarioType:n.scenario_type,clientType:n.client_type??void 0,stream:n.stream===1,inputTokens:n.input_tokens,outputTokens:n.output_tokens,cacheReadInputTokens:n.cache_read_input_tokens,cacheCreationInputTokens:n.cache_creation_input_tokens,ttft:n.ttft,tokensPerSecond:n.tokens_per_second,durationMs:n.duration_ms,status:bi(n.status),errorMessage:n.error_message??void 0,responseBody:n.response_body??void 0})}function ql(){return new Date(Date.now()-zl*24*60*60*1e3).toISOString()}function vi(n,e=!1){let t=Date.now();!e&&t-ki<Jl||(n.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(ql()),ki=t)}function Ci(n){let e=[],t=[];return n.startTime&&(e.push("timestamp >= ?"),t.push(n.startTime)),n.endTime&&(e.push("timestamp <= ?"),t.push(n.endTime)),n.model&&(e.push("model = ?"),t.push(n.model)),n.provider&&(e.push("provider = ?"),t.push(n.provider)),n.scenario&&(e.push("scenario_type = ?"),t.push(n.scenario)),n.clientType&&(n.clientType==="unknown"?(e.push("(client_type IS NULL OR client_type = '' OR client_type = ?)"),t.push(n.clientType)):(e.push("client_type = ?"),t.push(n.clientType))),n.sessionId&&(e.push("session_id = ?"),t.push(n.sessionId)),n.status&&(e.push("status = ?"),t.push(n.status)),{sql:e.length>0?`WHERE ${e.join(" AND ")}`:"",params:t}}function Ge(n,e,t,o=""){let r=o?e.sql?`${e.sql} AND ${o}`:`WHERE ${o}`:e.sql,s=n.prepare(`
387
- SELECT ${t} AS key, ${Zl}
386
+ `)}function eu(n,e,t){if(n<=0||e==null||!Number.isFinite(e))return null;let o=t!=null&&Number.isFinite(t)?t:0,r=Math.max((e-o)/1e3,Wl);return Math.round(n/r)}function Nn(n){let e=Number(n.outputTokens)||0,t=Number(n.durationMs),o=Number.isFinite(t)?t:null,r=n.stream?ve(n.ttft):null;return{...n,ttft:r,tokensPerSecond:eu(e,o,r)}}function me(n){return typeof n=="string"?n:n==null?"":String(n)}function Mn(n){if(n==null)return;let e=String(n);return e.length>0?e:void 0}function ht(n){let e=Number(n);return Number.isFinite(e)?e:0}function Ci(n){return n==="success"?"success":"error"}function xi(n){let e=Nn(n);return{id:me(e.id),timestamp:me(e.timestamp),session_id:me(e.sessionId),provider:me(e.provider),original_model:me(e.originalModel),model:me(e.model),upstream_model:Mn(e.upstreamModel)??null,model_family:me(e.modelFamily),scenario_type:me(e.scenarioType),client_type:Mn(e.clientType)??null,stream:e.stream?1:0,input_tokens:Math.trunc(ht(e.inputTokens)),output_tokens:Math.trunc(ht(e.outputTokens)),cache_read_input_tokens:Math.trunc(ht(e.cacheReadInputTokens)),cache_creation_input_tokens:Math.trunc(ht(e.cacheCreationInputTokens)),ttft:ve(e.ttft),tokens_per_second:ve(e.tokensPerSecond),duration_ms:Math.trunc(ht(e.durationMs)),status:Ci(e.status),error_message:Mn(e.errorMessage)??null,response_body:Mn(e.responseBody)??null}}function tu(n){return Nn({id:n.id,timestamp:n.timestamp,sessionId:n.session_id,provider:n.provider,originalModel:n.original_model,model:n.model,upstreamModel:n.upstream_model??void 0,modelFamily:n.model_family,scenarioType:n.scenario_type,clientType:n.client_type??void 0,stream:n.stream===1,inputTokens:n.input_tokens,outputTokens:n.output_tokens,cacheReadInputTokens:n.cache_read_input_tokens,cacheCreationInputTokens:n.cache_creation_input_tokens,ttft:n.ttft,tokensPerSecond:n.tokens_per_second,durationMs:n.duration_ms,status:Ci(n.status),errorMessage:n.error_message??void 0,responseBody:n.response_body??void 0})}function nu(){return new Date(Date.now()-Gl*24*60*60*1e3).toISOString()}function Si(n,e=!1){let t=Date.now();!e&&t-vi<Kl||(n.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(nu()),vi=t)}function Ri(n){let e=[],t=[];return n.startTime&&(e.push("timestamp >= ?"),t.push(n.startTime)),n.endTime&&(e.push("timestamp <= ?"),t.push(n.endTime)),n.model&&(e.push("model = ?"),t.push(n.model)),n.provider&&(e.push("provider = ?"),t.push(n.provider)),n.scenario&&(e.push("scenario_type = ?"),t.push(n.scenario)),n.clientType&&(n.clientType==="unknown"?(e.push("(client_type IS NULL OR client_type = '' OR client_type = ?)"),t.push(n.clientType)):(e.push("client_type = ?"),t.push(n.clientType))),n.sessionId&&(e.push("session_id = ?"),t.push(n.sessionId)),n.status&&(e.push("status = ?"),t.push(n.status)),{sql:e.length>0?`WHERE ${e.join(" AND ")}`:"",params:t}}function Ge(n,e,t,o=""){let r=o?e.sql?`${e.sql} AND ${o}`:`WHERE ${o}`:e.sql,s=n.prepare(`
387
+ SELECT ${t} AS key, ${ou}
388
388
  FROM usage_records ${r}
389
389
  GROUP BY key
390
- `).all(...e.params),i={};for(let c of s)c.key!=null&&(i[String(c.key)]={count:c.count,inputTokens:c.inputTokens,outputTokens:c.outputTokens,cacheReadInputTokens:c.cacheReadInputTokens,cacheCreationInputTokens:c.cacheCreationInputTokens});return i}function xi(n,e){let t=Ci(e),o=n.prepare(`
390
+ `).all(...e.params),i={};for(let c of s)c.key!=null&&(i[String(c.key)]={count:c.count,inputTokens:c.inputTokens,outputTokens:c.outputTokens,cacheReadInputTokens:c.cacheReadInputTokens,cacheCreationInputTokens:c.cacheCreationInputTokens});return i}function wi(n,e){let t=Ri(e),o=n.prepare(`
391
391
  SELECT
392
392
  COUNT(*) AS totalRequests,
393
393
  COALESCE(SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END), 0) AS successCount,
@@ -398,14 +398,14 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
398
398
  AVG(CASE WHEN status = 'success' AND ttft IS NOT NULL THEN ttft END) AS avgTtft,
399
399
  AVG(CASE WHEN status = 'success' AND tokens_per_second IS NOT NULL THEN tokens_per_second END) AS avgTokensPerSecond
400
400
  FROM usage_records ${t.sql}
401
- `).get(...t.params);return{totalRequests:o.totalRequests,successCount:o.successCount,errorCount:o.totalRequests-o.successCount,totalInputTokens:o.totalInputTokens,totalOutputTokens:o.totalOutputTokens,totalCacheReadInputTokens:o.totalCacheReadInputTokens,totalCacheCreationInputTokens:o.totalCacheCreationInputTokens,avgTtft:o.avgTtft!=null?Math.round(o.avgTtft):null,avgTokensPerSecond:o.avgTokensPerSecond!=null?Math.round(o.avgTokensPerSecond):null,byModel:Ge(n,t,"model"),byProvider:Ge(n,t,"provider"),byScenario:Ge(n,t,"scenario_type"),byFamily:Ge(n,t,"model_family || '/' || scenario_type","model_family IS NOT NULL AND model_family != ''"),byDay:Ge(n,t,"date(timestamp, 'localtime')"),byClient:Ge(n,t,"CASE WHEN client_type IS NULL OR client_type = '' THEN 'unknown' ELSE client_type END")}}function _t(n){let e=yt(),t=$n(n);or.run(Ti(t)),vi(e)}function rr(n){let e=yt(),t=Ci(n),o=Number.isFinite(n.page)&&n.page>0?Math.floor(n.page):1,r=Number.isFinite(n.pageSize)&&n.pageSize>0?Math.min(Math.floor(n.pageSize),eu):50,s=(o-1)*r,i=e.prepare(`SELECT COUNT(*) AS count FROM usage_records ${t.sql}`).get(...t.params),c=e.prepare(`SELECT * FROM usage_records ${t.sql} ORDER BY timestamp DESC LIMIT ? OFFSET ?`).all(...t.params,r,s),l=xi(e,n);return{records:c.map(Xl),summary:l,total:i.count,page:o,pageSize:r}}function kt(n,e,t){let o=yt();return xi(o,{startTime:n,endTime:e,status:t})}function sr(n){let e=yt();if(!n){e.prepare("DELETE FROM usage_records").run();return}e.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(n)}function tu(){me&&(me.pragma("wal_checkpoint(TRUNCATE)"),me.close(),me=null,or=null)}function Te(n){if(n==null)return null;if(typeof n=="number")return n;let e=parseFloat(String(n));return isNaN(e)?null:e}function ir(n){let e=Ke(Fl(),"claude-code-router"),t=Ke(e,`session-${n}.json`);if(nr(t))try{let o=tr(t,"utf-8"),r=JSON.parse(o);return{ttft:Te(r.timeToFirstToken),tokensPerSecond:Te(r.tokensPerSecond)}}catch{}try{let o=Ll(e),r=new RegExp(`^session-${n}-\\d+\\.json$`),s=o.filter(i=>r.test(i));if(s.length>0){let i=s.sort().reverse(),c=Ke(e,i[0]);try{let l=tr(c,"utf-8"),a=JSON.parse(l);return{ttft:Te(a.timeToFirstToken),tokensPerSecond:Te(a.tokensPerSecond)}}catch{return{ttft:null,tokensPerSecond:null}}}}catch{}return{ttft:null,tokensPerSecond:null}}function nu(n,e,t){return yt().prepare("SELECT MIN(timestamp) AS timestamp FROM usage_records WHERE provider = ? AND timestamp >= ? AND timestamp <= ?").get(n,e,t)?.timestamp??void 0}var Mn,jl,er,Hl,zl,Jl,_i,me,or,ki,Zl,eu,bt=w(()=>{"use strict";Mn=Ke(Dl(),".claude-code-router","data"),jl=Ke(Mn,"usage.sqlite"),er=Ke(Mn,"usage.jsonl"),Hl=1,zl=180,Jl=3600*1e3,_i="legacy_usage_jsonl_migrated_at",me=null,or=null,ki=0;Zl=`
401
+ `).get(...t.params);return{totalRequests:o.totalRequests,successCount:o.successCount,errorCount:o.totalRequests-o.successCount,totalInputTokens:o.totalInputTokens,totalOutputTokens:o.totalOutputTokens,totalCacheReadInputTokens:o.totalCacheReadInputTokens,totalCacheCreationInputTokens:o.totalCacheCreationInputTokens,avgTtft:o.avgTtft!=null?Math.round(o.avgTtft):null,avgTokensPerSecond:o.avgTokensPerSecond!=null?Math.round(o.avgTokensPerSecond):null,byModel:Ge(n,t,"model"),byProvider:Ge(n,t,"provider"),byScenario:Ge(n,t,"scenario_type"),byFamily:Ge(n,t,"model_family || '/' || scenario_type","model_family IS NOT NULL AND model_family != ''"),byDay:Ge(n,t,"date(timestamp, 'localtime')"),byClient:Ge(n,t,"CASE WHEN client_type IS NULL OR client_type = '' THEN 'unknown' ELSE client_type END")}}function _t(n){let e=yt(),t=Nn(n);sr.run(xi(t)),Si(e)}function ir(n){let e=yt(),t=Ri(n),o=Number.isFinite(n.page)&&n.page>0?Math.floor(n.page):1,r=Number.isFinite(n.pageSize)&&n.pageSize>0?Math.min(Math.floor(n.pageSize),ru):50,s=(o-1)*r,i=e.prepare(`SELECT COUNT(*) AS count FROM usage_records ${t.sql}`).get(...t.params),c=e.prepare(`SELECT * FROM usage_records ${t.sql} ORDER BY timestamp DESC LIMIT ? OFFSET ?`).all(...t.params,r,s),l=wi(e,n);return{records:c.map(tu),summary:l,total:i.count,page:o,pageSize:r}}function kt(n,e,t){let o=yt();return wi(o,{startTime:n,endTime:e,status:t})}function ar(n){let e=yt();if(!n){e.prepare("DELETE FROM usage_records").run();return}e.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(n)}function su(){ge&&(ge.pragma("wal_checkpoint(TRUNCATE)"),ge.close(),ge=null,sr=null)}function ve(n){if(n==null)return null;if(typeof n=="number")return n;let e=parseFloat(String(n));return isNaN(e)?null:e}function cr(n){let e=Ke(zl(),"claude-code-router"),t=Ke(e,`session-${n}.json`);if(rr(t))try{let o=or(t,"utf-8"),r=JSON.parse(o);return{ttft:ve(r.timeToFirstToken),tokensPerSecond:ve(r.tokensPerSecond)}}catch{}try{let o=jl(e),r=new RegExp(`^session-${n}-\\d+\\.json$`),s=o.filter(i=>r.test(i));if(s.length>0){let i=s.sort().reverse(),c=Ke(e,i[0]);try{let l=or(c,"utf-8"),a=JSON.parse(l);return{ttft:ve(a.timeToFirstToken),tokensPerSecond:ve(a.tokensPerSecond)}}catch{return{ttft:null,tokensPerSecond:null}}}}catch{}return{ttft:null,tokensPerSecond:null}}function iu(n,e,t){return yt().prepare("SELECT MIN(timestamp) AS timestamp FROM usage_records WHERE provider = ? AND timestamp >= ? AND timestamp <= ?").get(n,e,t)?.timestamp??void 0}var $n,Jl,nr,Wl,Gl,Kl,Ti,ge,sr,vi,ou,ru,bt=w(()=>{"use strict";$n=Ke(Hl(),".claude-code-router","data"),Jl=Ke($n,"usage.sqlite"),nr=Ke($n,"usage.jsonl"),Wl=1,Gl=180,Kl=3600*1e3,Ti="legacy_usage_jsonl_migrated_at",ge=null,sr=null,vi=0;ou=`
402
402
  COUNT(*) AS count,
403
403
  COALESCE(SUM(CASE WHEN status = 'success' THEN input_tokens ELSE 0 END), 0) AS inputTokens,
404
404
  COALESCE(SUM(CASE WHEN status = 'success' THEN output_tokens ELSE 0 END), 0) AS outputTokens,
405
405
  COALESCE(SUM(CASE WHEN status = 'success' THEN cache_read_input_tokens ELSE 0 END), 0) AS cacheReadInputTokens,
406
406
  COALESCE(SUM(CASE WHEN status = 'success' THEN cache_creation_input_tokens ELSE 0 END), 0) AS cacheCreationInputTokens
407
- `;eu=200});import{existsSync as ou}from"fs";import{homedir as ru}from"os";import{join as su}from"path";import{writeFile as iu}from"fs/promises";async function Ri(){let n=ru(),e=su(n,".claude.json");if(!ou(e)){let o={numStartups:184,autoUpdaterStatus:"enabled",userID:Array.from({length:64},()=>Math.random().toString(16)[2]).join(""),hasCompletedOnboarding:!0,lastOnboardingVersion:"1.0.17",projects:{}};await iu(e,JSON.stringify(o,null,2))}}var wi=w(()=>{"use strict"});import{existsSync as au,readdirSync as cu,statSync as lu,unlinkSync as uu}from"fs";import{join as Pi}from"path";import{HOME_DIR as du}from"@wengine-ai/claude-code-router-shared";function Oi(n=Pi(du,"logs")){try{if(!au(n))return;let e=Date.now()-fu;for(let t of cu(n)){if(!/^ccr-.*\.log$/.test(t))continue;let o=Pi(n,t),r=lu(o);r.isFile()&&r.mtime.getTime()<e&&uu(o)}}catch(e){console.warn("Failed to prune old server log files:",e)}}function Ai(){Ei||(Ei=!0,Oi(),setInterval(Oi,mu).unref())}var pu,fu,mu,Ei,Ii=w(()=>{"use strict";pu=7,fu=pu*24*60*60*1e3,mu=1440*60*1e3,Ei=!1});async function Mi(n,e){let t=e.plugins||e.Plugins||[];for(let o of t){let{name:r,enabled:s=!1,options:i={}}=o;switch(r){case"token-speed":at.registerPlugin(Tn,{enabled:s,outputHandlers:[{type:"temp-file",enabled:!0,config:{subdirectory:"claude-code-router",extension:"json",includeTimestamp:!0,prefix:"session"}}],...i});break;default:console.warn(`Unknown plugin: ${r}`);break}}await at.enablePlugins(n)}var $i=w(()=>{"use strict";Go()});import{join as ie,isAbsolute as gu,normalize as hu,dirname as yu}from"path";import{fileURLToPath as _u}from"url";import ku from"@fastify/static";import{readdirSync as Ni,statSync as bu,readFileSync as Li,writeFileSync as Tu,existsSync as de,unlinkSync as Di,rmSync as vu}from"fs";import{homedir as ar}from"os";import{getPresetDir as Nn,readManifestFromDir as cr,manifestToPresetFile as Fi,saveManifest as Ui,isPresetInstalled as ji,extractPreset as Cu,HOME_DIR as xu,loadConfigFromManifest as Su,downloadPresetToTemp as Ru,findMarketPresetByName as wu,getMarketPresets as Pu,applyClientSelection as Eu,disableClient as Ou,enableClient as Au,isClientId as Iu,listClientStatuses as Hi,restoreClient as Mu,listProjectConfigs as $u,readProjectConfigById as lr,readProjectConfig as Nu,writeProjectConfig as zi,deleteProjectConfig as Lu,getClaudeProjectId as Du,getProjectConfigPath as Fu,refreshCcrProjectTakeover as Uu,refreshPiProjectTakeover as ju,refreshProjectTakeovers as Hu,syncGlobalProjectTakeovers as ur,getProjectTakeoverClients as dr,setProjectTakeover as pr,isProjectTakeoverClient as zu}from"@wengine-ai/claude-code-router-shared";import Ju from"@fastify/multipart";import Bu from"adm-zip";function Ji(n,e,t){return e.failed.length===0?!0:(n.log?.warn?.({source:t,projectTakeoverSync:e},"Client configuration changed but some project takeovers failed to sync"),!1)}function Wu(n){let e=new Date().toISOString(),t=new Date(Date.now()-300*60*1e3).toISOString(),o=new Date(Date.now()-10080*60*1e3).toISOString(),r=kt(t,e),s=kt(o,e),i=[],c=new Set;for(let l of n||[])l&&l.name&&c.add(l.name);for(let l of Object.keys(r.byProvider||{}))c.add(l);for(let l of Object.keys(s.byProvider||{}))c.add(l);for(let l of c){let a=r.byProvider[l]||{inputTokens:0,outputTokens:0},u=s.byProvider[l]||{inputTokens:0,outputTokens:0},d=(a.inputTokens||0)+(a.outputTokens||0),m=(u.inputTokens||0)+(u.outputTokens||0),f,y,p=(n||[]).find(g=>g&&g.name===l);p&&p.quota&&(f=p.quota.limit5h,y=p.quota.limit7d),i.push({provider:l,used5h:d,used7d:m,limit5h:f,limit7d:y})}return i}async function Bi(n,e){let t=n.app;qe(),et(),t.register(Ju,{limits:{fileSize:50*1024*1024}}),t.get("/v1/models",async()=>{let c=On(e);return{object:"list",data:c,first_id:c[0]?.id||null,last_id:c[c.length-1]?.id||null,has_more:!1}}),t.get("/v1/models/:modelId",async(c,l)=>{let a=decodeURIComponent(c.params.modelId),u=On(e).find(d=>d.id===a);return u||(l.status(404),{type:"error",error:{type:"not_found_error",message:`Model not found: ${a}`}})}),t.post("/v1/messages/count_tokens",async(c,l)=>{let{messages:a,tools:u,system:d,model:m}=c.body,f=t._server.tokenizerService;if(m&&m.includes(",")&&f)try{let[p,g]=m.split(",");c.log?.info(`Looking up tokenizer for provider: ${p}, model: ${g}`);let _=f.getTokenizerConfigForModel(p,g);_?c.log?.info(`Using tokenizer config: ${JSON.stringify(_)}`):c.log?.warn(`No tokenizer config found for ${p},${g}, using default tiktoken`);let k=await f.countTokens({messages:a,system:d,tools:u},_,p);return{input_tokens:k.tokenCount,tokenizer:k.tokenizerUsed}}catch(p){c.log?.error(`Error using configured tokenizer: ${p.message}`),c.log?.error(p.stack)}else m?m.includes(",")?f||c.log?.warn("TokenizerService not available, using default tiktoken"):c.log?.info(`Model "${m}" does not contain comma, using default tiktoken`):c.log?.info("No model specified, using default tiktoken");return{input_tokens:Nt(a,d,u)}}),t.get("/api/config",async(c,l)=>await Qo()),t.get("/api/transformers",async(c,l)=>{let a=t._server.transformerService.getAllTransformers();return{transformers:Array.from(a.entries()).map(([d,m])=>({name:d,endpoint:m.endPoint||null}))}}),t.post("/api/config",async(c,l)=>{let a=c.body,u=kr(a||{});if(u.length>0)return l.status(400),{success:!1,error:"Invalid proxy URL",message:u.map(f=>`${f.key}: ${f.error}`).join("; "),proxyErrors:u};let d=await xn();d&&console.log(`Backed up existing configuration file to ${d}`),await Be(a);let m;try{m=await ur(a),m.failed.length>0&&t.log?.warn?.({projectTakeoverSync:m},"Config saved but some global project takeovers failed to sync")}catch(f){t.log?.warn?.(`Config saved but global project takeover sync failed: ${f?.message||f}`)}try{let f=t._server;if(f?.configService){f.configService.reload();let y=a?.Providers||a?.providers;if(Array.isArray(y)&&f.configService.set("providers",y),ue&&f.transformerService&&(f.providerService=new ue(f.configService,f.transformerService,t.log)),mt(a,t.log),He&&_n){try{_n()}catch{}let p={enabled:f.configService.get("ACTIVE_PROBE_ENABLED")??!0,quotaProbeIntervalMinutes:f.configService.get("QUOTA_PROBE_INTERVAL_MINUTES")??10,probeTimeoutMs:f.configService.get("PROBE_TIMEOUT_MS")??15e3,slowThresholdMs:Number(f.configService.get("PROBE_SLOW_THRESHOLD_MS"))||3e3,initialDelayMs:f.configService.get("PROBE_INITIAL_DELAY_MS")??5e3,excludeProviders:f.configService.get("EXCLUDE_PROBE_PROVIDERS")??[]};f.activeProbeService=He(()=>f.providerService.getProviders(),p,g=>ce(f.configService,g),t.log,g=>f.configService.get(g))}}}catch(f){t.log?.warn?.(`Config saved but runtime reload failed: ${f?.message||f}`)}return{success:!0,message:"Config saved successfully",projectTakeoverSync:m}}),t.get("/api/clients",async(c,l)=>{try{let a=await se();return{clients:Hi(a)}}catch(a){console.error("Failed to get client integrations:",a),l.status(500).send({error:a.message||"Failed to get client integrations"})}}),t.post("/api/clients/apply",async(c,l)=>{try{let a=c.body,u=Array.isArray(a?.enabled)?a.enabled:[],d=await se(),m=Eu(d,u);await Be(m.config);let f=await ur(m.config),y=Ji(t,f,"clients/apply");return{...m,success:m.success&&y,projectTakeoverSync:f}}catch(a){console.error("Failed to apply client integrations:",a),l.status(500).send({error:a.message||"Failed to apply client integrations"})}});async function o(c,l,a){try{let{id:u}=c.params;if(!Iu(u)){l.status(404).send({error:`Unknown client: ${u}`});return}let d=await se(),m=a==="enable"?Au(d,u):a==="restore"?Mu(d,u):Ou(d,u);await Be(d);let f=await ur(d),y=Ji(t,f,`clients/${u}/${a}`);return{success:m.success&&y,result:m,results:[m],clients:Hi(d),config:d,projectTakeoverSync:f}}catch(u){console.error(`Failed to ${a} client integration:`,u),l.status(500).send({error:u.message||`Failed to ${a} client integration`})}}t.post("/api/clients/:id/enable",async(c,l)=>o(c,l,"enable")),t.post("/api/clients/:id/disable",async(c,l)=>o(c,l,"disable")),t.post("/api/clients/:id/restore",async(c,l)=>o(c,l,"restore")),t.get("/api/projects",async(c,l)=>{try{let a=await $u();return{projects:await Promise.all(a.map(async d=>{let m=await dr(d.path);return{...d,ccrTakeoverClients:m,ccrTakeover:m.length>0}}))}}catch(a){console.error("Failed to list project configs:",a),l.status(500).send({error:a.message||"Failed to list project configs"})}}),t.post("/api/projects",async(c,l)=>{try{let{path:a}=c.body||{};if(!a||typeof a!="string"||!gu(a)){l.status(400).send({error:"A valid absolute project path is required"});return}let u=hu(a).replace(/[\\/]+$/,"")||a;if(await Nu(u)){l.status(409).send({error:"Project is already configured"});return}await zi(u,{Router:{}});try{let f=await se();await pr(u,["claudeCode"],f)}catch(f){console.error("Failed to auto-enable ccr takeover for new project:",f)}let m=await dr(u);return{id:Du(u),path:u,configPath:Fu(u),Router:{},ccrTakeoverClients:m,ccrTakeover:m.length>0}}catch(a){console.error("Failed to add project config:",a),l.status(500).send({error:a.message||"Failed to add project config"})}}),t.put("/api/projects/:id",async(c,l)=>{try{let{id:a}=c.params,{Router:u}=c.body||{};if(!u||typeof u!="object"){l.status(400).send({error:"Router must be an object"});return}let d=await lr(a);if(!d){l.status(404).send({error:"Project config not found"});return}await zi(d.path,{Router:u});let m=await se();Object.keys(u).length===0?await Hu(d.path,m):(await Uu(d.path,m,u),await ju(d.path,m,u));let f=await dr(d.path);return{id:a,path:d.path,configPath:d.configPath,Router:u,ccrTakeoverClients:f,ccrTakeover:f.length>0}}catch(a){console.error("Failed to update project config:",a),l.status(500).send({error:a.message||"Failed to update project config"})}}),t.put("/api/projects/:id/takeover",async(c,l)=>{try{let{id:a}=c.params,u=c.body||{},d;if(Array.isArray(u.clients))d=u.clients.filter(zu);else if(typeof u.enabled=="boolean")d=u.enabled?["claudeCode"]:[];else{l.status(400).send({error:"clients (array) or enabled (boolean) is required"});return}let m=await lr(a);if(!m){l.status(404).send({error:"Project config not found"});return}let f=await se(),y=await pr(m.path,d,f);return{id:a,path:m.path,ccrTakeoverClients:y,ccrTakeover:y.length>0}}catch(a){console.error("Failed to update ccr takeover status:",a),l.status(500).send({error:a.message||"Failed to update ccr takeover status"})}}),t.delete("/api/projects/:id",async(c,l)=>{try{let{id:a}=c.params,u=await lr(a);if(!u){l.status(404).send({error:"Project config not found"});return}try{let d=await se();await pr(u.path,[],d)}catch(d){console.error("Failed to remove ccr takeover while deleting project:",d)}return await Lu(u.path),{success:!0}}catch(a){console.error("Failed to delete project config:",a),l.status(500).send({error:a.message||"Failed to delete project config"})}});let r=typeof __dirname=="string"?__dirname:yu(_u(import.meta.url)),s=de(ie(r,"index.html"))?r:de(ie(r,"..","index.html"))?ie(r,".."):ie(r,"..","dist");t.register(ku,{root:s,prefix:"/ui/",maxAge:"1h"}),t.get("/ui",async(c,l)=>l.redirect("/ui/")),t.get("/api/logs/files",async(c,l)=>{try{let a=ie(ar(),".claude-code-router","logs"),u=[];if(de(a)){let d=Ni(a);for(let m of d)if(m.endsWith(".log")){let f=ie(a,m),y=bu(f);u.push({name:m,path:f,size:y.size,lastModified:y.mtime.toISOString()})}u.sort((m,f)=>new Date(f.lastModified).getTime()-new Date(m.lastModified).getTime())}return u}catch(a){console.error("Failed to get log files:",a),l.status(500).send({error:"Failed to get log files"})}}),t.get("/api/logs",async(c,l)=>{try{let a=c.query.file,u;return a?u=a:u=ie(ar(),".claude-code-router","logs","app.log"),de(u)?Li(u,"utf8").split(`
408
- `).filter(f=>f.trim()):[]}catch(a){console.error("Failed to get logs:",a),l.status(500).send({error:"Failed to get logs"})}}),t.delete("/api/logs",async(c,l)=>{try{let a=c.query.file,u;return a?u=a:u=ie(ar(),".claude-code-router","logs","app.log"),de(u)&&Tu(u,"","utf8"),{success:!0,message:"Logs cleared successfully"}}catch(a){console.error("Failed to clear logs:",a),l.status(500).send({error:"Failed to clear logs"})}}),t.get("/api/debug-log",async()=>({enabled:xt()})),t.put("/api/debug-log",async c=>{let{enabled:l}=c.body;return Bn(l),{enabled:xt()}}),t.get("/api/usage",async(c,l)=>{try{let a=c.query;return rr({startTime:a.startDate,endTime:a.endDate,model:a.model,provider:a.provider,scenario:a.scenario,clientType:a.clientType,sessionId:a.sessionId,status:a.status,page:a.page?parseInt(a.page,10):void 0,pageSize:a.pageSize?parseInt(a.pageSize,10):void 0})}catch(a){console.error("Failed to query usage:",a),l.status(500).send({error:"Failed to query usage"})}}),t.get("/api/usage/summary",async(c,l)=>{try{let a=c.query;return kt(a.startDate,a.endDate,a.status)}catch(a){console.error("Failed to query usage summary:",a),l.status(500).send({error:"Failed to query usage summary"})}}),t.delete("/api/usage",async(c,l)=>{try{let a=c.query;return sr(a.beforeDate),{success:!0,message:a.beforeDate?"Usage data cleared before "+a.beforeDate:"All usage data cleared"}}catch(a){console.error("Failed to clear usage:",a),l.status(500).send({error:"Failed to clear usage data"})}}),t.get("/api/providers/health",async(c,l)=>{try{let u=U().getAllStates(),m=t._server?.activeProbeService?.getProbeTelemetry?.()||[];return{states:u.map(f=>({provider:f.provider,model:f.model,status:f.status,failureCount:f.failureCount,successCount:f.successCount,lastFailureTime:f.lastFailureTime,lastError:f.lastError,rateLimitUntil:f.rateLimitUntil??null})),probes:m,timestamp:new Date().toISOString()}}catch(a){console.error("Failed to get provider health:",a),l.status(500).send({error:"Failed to get provider health"})}}),t.post("/api/providers/probe",async(c,l)=>{try{let{providerName:a}=c.body;if(!a)return l.status(400),{error:"providerName is required"};let d=t._server?.activeProbeService;if(!d?.probeProviderManually)return l.status(503),{error:"Active probe service is not available"};let m=await d.probeProviderManually(a);return m.success&&Pn(a,c.log),{provider:a,success:m.success,latencyMs:m.latencyMs,status:m.status,isSlow:m.isSlow,errorKind:m.errorKind??null,error:m.error,timestamp:new Date().toISOString()}}catch(a){console.error("Failed to probe provider:",a),l.status(500).send({error:a.message||"Failed to probe provider"})}}),t.post("/api/providers/probe-all",async(c,l)=>{try{let a=t._server,u=a?.activeProbeService;if(!u?.probeProviderManually)return l.status(503),{error:"Active probe service is not available"};let d=(a?.providerService?.getProviders?.()||[]).filter(f=>f?.name&&f.enabled!==!1),m=await Promise.all(d.map(async f=>{let y=await u.probeProviderManually(f.name);return y.success&&Pn(f.name,c?.log),{provider:f.name,success:y.success,latencyMs:y.latencyMs,status:y.status,isSlow:y.isSlow,errorKind:y.errorKind??null,error:y.error}}));return{results:m,successCount:m.filter(f=>f.success).length,total:m.length,timestamp:new Date().toISOString()}}catch(a){console.error("Failed to probe all providers:",a),l.status(500).send({error:a.message||"Failed to probe all providers"})}}),t.get("/api/providers/quota",async(c,l)=>{try{let u=(await se()).Providers||[],d=Wu(u),m=new Map(d.map(p=>[p.provider,p])),f=eo();for(let p of d){let g=f.find(_=>_.provider===p.provider);g&&g.limit!=null&&(p.limit5h=g.limit,p.used5h=g.limit-(g.remaining??0),g.reset&&(p.reset5h=new Date(g.reset*1e3).toISOString()))}let y=io();for(let p of y){let g=m.get(p.provider);g||(g={provider:p.provider,used5h:0,used7d:0},d.push(g),m.set(p.provider,g)),typeof p.totalBalance=="number"?(g.limit7d=p.totalBalance,g.type7d=p.currency?"balance":"rateLimit",p.currency&&(g.currency=p.currency),typeof p.usedBalance=="number"?g.used7d=p.usedBalance:typeof p.remainingBalance=="number"?g.used7d=Math.max(0,p.totalBalance-p.remainingBalance):(g.used7d=0,g.limit7d=p.totalBalance)):typeof p.usedBalance=="number"&&(g.used7d=p.usedBalance),typeof p.usedDailyBalance=="number"&&(g.used5h=p.usedDailyBalance,g.type5h="rateLimit"),typeof p.limitDaily=="number"&&(g.limit5h=p.limitDaily,g.type5h="rateLimit"),typeof p.resetTime=="string"&&(p.limitDaily!==void 0?g.reset5h=p.resetTime:g.reset7d=p.resetTime),typeof p.resetTime7d=="string"&&(g.reset7d=p.resetTime7d)}return{quotas:d,timestamp:new Date().toISOString()}}catch(a){console.error("Failed to compute provider quota:",a),l.status(500).send({error:"Failed to compute provider quota"})}}),t.get("/api/presets",async(c,l)=>{try{let a=ie(xu,"presets");if(!de(a))return{presets:[]};let d=Ni(a,{withFileTypes:!0}).filter(f=>f.isDirectory()&&!f.name.startsWith(".")).map(f=>f.name),m=[];for(let f of d){let y=ie(a,f);try{let p=ie(y,"manifest.json"),g=Li(p,"utf-8"),_=JSON.parse(g),{Providers:k,Router:h,PORT:b,HOST:T,API_TIMEOUT_MS:x,PROXY_URL:C,PROXY_GLOBAL_ENABLED:S,LOG:v,LOG_LEVEL:P,StatusLine:R,NON_INTERACTIVE_MODE:O,...E}=_;m.push({id:f,name:E.name||f,version:E.version||"1.0.0",description:E.description,author:E.author,homepage:E.homepage,repository:E.repository,license:E.license,keywords:E.keywords,ccrVersion:E.ccrVersion,source:E.source,sourceType:E.sourceType,checksum:E.checksum,installed:!0})}catch(p){console.error(`Failed to read preset ${f}:`,p)}}return{presets:m}}catch(a){console.error("Failed to get presets:",a),l.status(500).send({error:"Failed to get presets"})}}),t.get("/api/presets/:name",async(c,l)=>{try{let{name:a}=c.params,u=Nn(a);if(!de(u)){l.status(404).send({error:"Preset not found"});return}let d=await cr(u);return{...Fi(d),config:Su(d,u),userValues:d.userValues||{}}}catch(a){console.error("Failed to get preset:",a),l.status(500).send({error:a.message||"Failed to get preset"})}}),t.post("/api/presets/:name/apply",async(c,l)=>{try{let{name:a}=c.params,{secrets:u}=c.body,d=Nn(a);if(!de(d)){l.status(404).send({error:"Preset not found"});return}let f={...await cr(d)};return u&&Object.keys(u).length>0&&(f.userValues={...f.userValues,...u}),await Ui(a,f),{success:!0,message:"Preset applied successfully"}}catch(a){console.error("Failed to apply preset:",a),l.status(500).send({error:a.message||"Failed to apply preset"})}}),t.delete("/api/presets/:name",async(c,l)=>{try{let{name:a}=c.params,u=Nn(a);if(!de(u)){l.status(404).send({error:"Preset not found"});return}return vu(u,{recursive:!0,force:!0}),{success:!0,message:"Preset deleted successfully"}}catch(a){console.error("Failed to delete preset:",a),l.status(500).send({error:a.message||"Failed to delete preset"})}}),t.get("/api/presets/market",async(c,l)=>{try{return{presets:await Pu()}}catch(a){console.error("Failed to get market presets:",a),l.status(500).send({error:a.message||"Failed to get market presets"})}}),t.post("/api/presets/install/github",async(c,l)=>{try{let{presetName:a}=c.body;if(!a){l.status(400).send({error:"Preset name is required"});return}let u=await wu(a);if(!u){l.status(400).send({error:"Preset not found in marketplace",message:`Preset '${a}' is not available in the official marketplace. Please check the available presets.`});return}if(!u.repo){l.status(400).send({error:"Invalid preset data",message:`Preset '${a}' does not have repository information`});return}let d=u.repo.match(/(?:github\.com[:/]|^)([^/]+)\/([^/\s#]+?)(?:\.git)?$/);if(!d){l.status(400).send({error:"Invalid GitHub repository URL"});return}let[,m,f]=d,y=u.name||a;if(await ji(y)){l.status(409).send({error:"Preset already installed",message:`Preset '${y}' is already installed. To update or reconfigure, please delete it first using the delete button.`,presetName:y});return}let p=`https://github.com/${m}/${f}/archive/refs/heads/main.zip`,g=await Ru(p),_=await i(g);if(await ji(y)){Di(g),l.status(409).send({error:"Preset already installed",message:`Preset '${y}' was installed while downloading. Please try again.`,presetName:y});return}let k=Nn(y);await Cu(g,k);let h=await cr(k);return h.repository=u.repo,u.url&&(h.source=u.url),await Ui(y,h),Di(g),{success:!0,presetName:y,preset:{..._.metadata,installed:!0}}}catch(a){console.error("Failed to install preset from GitHub:",a),l.status(500).send({error:a.message||"Failed to install preset from GitHub"})}});async function i(c){let l=new Bu(c),a=l.getEntry("manifest.json");if(a||(a=l.getEntries().find(m=>m.entryName.includes("manifest.json"))||null),!a)throw new Error("Invalid preset file: manifest.json not found");let u=JSON.parse(a.getData().toString("utf-8"));return Fi(u)}return n}var Wi=w(()=>{"use strict";De();Re();Ae();Ut();kn();ne();Ye();oe();En();Sn();bt();qo()});function Gu(n,e){let t=n?.Router,{family:o}=ot(e||"");if(t?.enableFamilyRouting&&o){let r=t.families?.[o]?.image;if(r)return r}return t?.image}function Gi(n){return n?.Router?.image}function Ku(n){return n.some(e=>e.role==="user"&&Array.isArray(e.content)&&e.content.some(t=>t.type==="image"||t.type==="image_url"||Array.isArray(t?.content)&&t.content.some(o=>o.type==="image"||o.type==="image_url")))}function Qu(n){let e=n[n.length-1];if(e?.role!=="user"||!Array.isArray(e.content))return;let t=[];e.content.filter(o=>o.type==="tool_result").forEach(o=>{if(!Array.isArray(o.content))return;let r=o.content.filter(s=>s.type==="image"?(t.push(s),!1):!0);r.length!==o.content.length&&(o.content=r.length>0?r:"read image successfully")}),e.content.push(...t)}var fr,Ln,mr,Ki,Qi=w(()=>{"use strict";De();fr=class{cache=new Map;maxSize;ttl;constructor(e=100,t=300*1e3){this.maxSize=e,this.ttl=t}evictExpired(){let e=Date.now();for(let[t,o]of this.cache)e-o.timestamp>this.ttl&&this.cache.delete(t)}trim(){if(this.cache.size<=this.maxSize)return;let e=[...this.cache.keys()].slice(0,this.cache.size-this.maxSize);for(let t of e)this.cache.delete(t)}storeImage(e,t){this.hasImage(e)||(this.evictExpired(),this.cache.set(e,{source:t,timestamp:Date.now()}),this.trim())}getImage(e){let t=this.cache.get(e);return t?Date.now()-t.timestamp>this.ttl?(this.cache.delete(e),null):t.source:null}hasImage(e){let t=this.cache.get(e);return t?Date.now()-t.timestamp>this.ttl?(this.cache.delete(e),!1):!0:!1}clear(){this.cache.clear()}size(){return this.evictExpired(),this.cache.size}},Ln=new fr;mr=class{name="image";tools;constructor(){this.tools=new Map,this.appendTools()}shouldHandle(e,t){let o=Gu(t,e.originalModel||e.body.model);if(!o||e.body.model===o||!Ku(e.body.messages))return!1;let s=t.forceUseImageAgent===!0;return!s&&uo(e.body.model)?!1:(Qu(e.body.messages),s&&!!Gi(t))}appendTools(){this.tools.set("analyzeImage",{name:"analyzeImage",description:"Analyse image or images by ID and extract information such as OCR text, objects, layout, colors, or safety signals.",input_schema:{type:"object",properties:{imageId:{type:"array",description:"an array of IDs to analyse",items:{type:"string"}},task:{type:"string",description:"Details of task to perform on the image.The more detailed, the better"},regions:{type:"array",description:"Optional regions of interest within the image",items:{type:"object",properties:{name:{type:"string",description:"Optional label for the region"},x:{type:"number",description:"X coordinate"},y:{type:"number",description:"Y coordinate"},w:{type:"number",description:"Width of the region"},h:{type:"number",description:"Height of the region"},units:{type:"string",enum:["px","pct"],description:"Units for coordinates and size"}},required:["x","y","w","h","units"]}}},required:["imageId","task"]},handler:async(e,t)=>{let o=[],r;if(e.imageId){if(Array.isArray(e.imageId))e.imageId.forEach(c=>{let l=Ln.getImage(`${t.req.id}_Image#${c}`);l&&o.push({type:"image",source:l})});else{let c=Ln.getImage(`${t.req.id}_Image#${e.imageId}`);c&&o.push({type:"image",source:c})}r=e.imageId,delete e.imageId}let s=t.req.body.messages[t.req.body.messages.length-1];if(s.role==="user"&&Array.isArray(s.content)){let c=s.content.filter(l=>l.type==="text"&&!l.text.includes("This is an image, if you need to view or analyze it, you need to extract the imageId"));o.push(...c)}Object.keys(e).length>0&&o.push({type:"text",text:JSON.stringify(e)});let i=await fetch(`http://127.0.0.1:${t.config.PORT||3456}/v1/messages`,{method:"POST",headers:{"x-api-key":t.config.APIKEY,"content-type":"application/json"},body:JSON.stringify({model:Gi(t.config),system:[{type:"text",text:`You must interpret and analyze images strictly according to the assigned task.
407
+ `;ru=200});import{existsSync as au}from"fs";import{homedir as cu}from"os";import{join as lu}from"path";import{writeFile as uu}from"fs/promises";async function Ei(){let n=cu(),e=lu(n,".claude.json");if(!au(e)){let o={numStartups:184,autoUpdaterStatus:"enabled",userID:Array.from({length:64},()=>Math.random().toString(16)[2]).join(""),hasCompletedOnboarding:!0,lastOnboardingVersion:"1.0.17",projects:{}};await uu(e,JSON.stringify(o,null,2))}}var Oi=w(()=>{"use strict"});import{existsSync as du,readdirSync as pu,statSync as fu,unlinkSync as mu}from"fs";import{join as Ai}from"path";import{HOME_DIR as gu}from"@wengine-ai/claude-code-router-shared";function Mi(n=Ai(gu,"logs")){try{if(!du(n))return;let e=Date.now()-yu;for(let t of pu(n)){if(!/^ccr-.*\.log$/.test(t))continue;let o=Ai(n,t),r=fu(o);r.isFile()&&r.mtime.getTime()<e&&mu(o)}}catch(e){console.warn("Failed to prune old server log files:",e)}}function $i(){Ii||(Ii=!0,Mi(),setInterval(Mi,_u).unref())}var hu,yu,_u,Ii,Ni=w(()=>{"use strict";hu=7,yu=hu*24*60*60*1e3,_u=1440*60*1e3,Ii=!1});async function Li(n,e){let t=e.plugins||e.Plugins||[];for(let o of t){let{name:r,enabled:s=!1,options:i={}}=o;switch(r){case"token-speed":at.registerPlugin(vn,{enabled:s,outputHandlers:[{type:"temp-file",enabled:!0,config:{subdirectory:"claude-code-router",extension:"json",includeTimestamp:!0,prefix:"session"}}],...i});break;default:console.warn(`Unknown plugin: ${r}`);break}}await at.enablePlugins(n)}var Di=w(()=>{"use strict";Qo()});import{ensureCcrSelfProvider as ku}from"@wengine-ai/claude-code-router-shared";import{join as ie,isAbsolute as bu,normalize as Tu,dirname as vu}from"path";import{fileURLToPath as Cu}from"url";import xu from"@fastify/static";import{readdirSync as Fi,statSync as Su,readFileSync as Ui,writeFileSync as Ru,existsSync as de,unlinkSync as ji,rmSync as wu}from"fs";import{homedir as lr}from"os";import{getPresetDir as Ln,readManifestFromDir as ur,manifestToPresetFile as Hi,saveManifest as zi,isPresetInstalled as Bi,extractPreset as Pu,HOME_DIR as Eu,loadConfigFromManifest as Ou,downloadPresetToTemp as Au,findMarketPresetByName as Iu,getMarketPresets as Mu,applyClientSelection as $u,disableClient as Nu,enableClient as Lu,isClientId as Du,listClientStatuses as Ji,restoreClient as Fu,listProjectConfigs as Uu,readProjectConfigById as dr,readProjectConfig as ju,writeProjectConfig as Wi,deleteProjectConfig as Hu,getClaudeProjectId as zu,getProjectConfigPath as Bu,refreshCcrProjectTakeover as Ju,refreshPiProjectTakeover as Wu,refreshProjectTakeovers as Gu,syncGlobalProjectTakeovers as pr,getProjectTakeoverClients as fr,setProjectTakeover as mr,isProjectTakeoverClient as Ku}from"@wengine-ai/claude-code-router-shared";import Qu from"@fastify/multipart";import Vu from"adm-zip";function Gi(n,e,t){return e.failed.length===0?!0:(n.log?.warn?.({source:t,projectTakeoverSync:e},"Client configuration changed but some project takeovers failed to sync"),!1)}function Yu(n){let e=new Date().toISOString(),t=new Date(Date.now()-300*60*1e3).toISOString(),o=new Date(Date.now()-10080*60*1e3).toISOString(),r=kt(t,e),s=kt(o,e),i=[],c=new Set;for(let l of n||[])l&&l.name&&c.add(l.name);for(let l of Object.keys(r.byProvider||{}))c.add(l);for(let l of Object.keys(s.byProvider||{}))c.add(l);for(let l of c){let a=r.byProvider[l]||{inputTokens:0,outputTokens:0},u=s.byProvider[l]||{inputTokens:0,outputTokens:0},d=(a.inputTokens||0)+(a.outputTokens||0),f=(u.inputTokens||0)+(u.outputTokens||0),g,y,m=(n||[]).find(p=>p&&p.name===l);m&&m.quota&&(g=m.quota.limit5h,y=m.quota.limit7d),i.push({provider:l,used5h:d,used7d:f,limit5h:g,limit7d:y})}return i}async function Ki(n,e){let t=n.app;Ze(),et(),t.register(Qu,{limits:{fileSize:50*1024*1024}}),t.get("/v1/models",async()=>{let c=An(e);return{object:"list",data:c,first_id:c[0]?.id||null,last_id:c[c.length-1]?.id||null,has_more:!1}}),t.get("/v1/models/:modelId",async(c,l)=>{let a=decodeURIComponent(c.params.modelId),u=An(e).find(d=>d.id===a);return u||(l.status(404),{type:"error",error:{type:"not_found_error",message:`Model not found: ${a}`}})}),t.post("/v1/messages/count_tokens",async(c,l)=>{let{messages:a,tools:u,system:d,model:f}=c.body,g=t._server.tokenizerService;if(f&&f.includes(",")&&g)try{let[m,p]=f.split(",");c.log?.info(`Looking up tokenizer for provider: ${m}, model: ${p}`);let _=g.getTokenizerConfigForModel(m,p);_?c.log?.info(`Using tokenizer config: ${JSON.stringify(_)}`):c.log?.warn(`No tokenizer config found for ${m},${p}, using default tiktoken`);let k=await g.countTokens({messages:a,system:d,tools:u},_,m);return{input_tokens:k.tokenCount,tokenizer:k.tokenizerUsed}}catch(m){c.log?.error(`Error using configured tokenizer: ${m.message}`),c.log?.error(m.stack)}else f?f.includes(",")?g||c.log?.warn("TokenizerService not available, using default tiktoken"):c.log?.info(`Model "${f}" does not contain comma, using default tiktoken`):c.log?.info("No model specified, using default tiktoken");return{input_tokens:Nt(a,d,u)}}),t.get("/api/config",async(c,l)=>await Yo()),t.get("/api/transformers",async(c,l)=>{let a=t._server.transformerService.getAllTransformers();return{transformers:Array.from(a.entries()).map(([d,f])=>({name:d,endpoint:f.endPoint||null}))}}),t.post("/api/config",async(c,l)=>{let a=c.body,u=Tr(a||{});if(u.length>0)return l.status(400),{success:!1,error:"Invalid proxy URL",message:u.map(m=>`${m.key}: ${m.error}`).join("; "),proxyErrors:u};let d=await Sn();d&&console.log(`Backed up existing configuration file to ${d}`);let f=null;try{f=await ne()}catch{}let g=ku(a,f);await fe(a);let y;try{y=await pr(a),y.failed.length>0&&t.log?.warn?.({projectTakeoverSync:y},"Config saved but some global project takeovers failed to sync")}catch(m){t.log?.warn?.(`Config saved but global project takeover sync failed: ${m?.message||m}`)}try{let m=t._server;if(m?.configService){m.configService.reload();let p=a?.Providers||a?.providers;if(Array.isArray(p)&&m.configService.set("providers",p),ue&&m.transformerService&&(m.providerService=new ue(m.configService,m.transformerService,t.log)),mt(a,t.log),ze&&kn){try{kn()}catch{}let _={enabled:m.configService.get("ACTIVE_PROBE_ENABLED")??!0,quotaProbeIntervalMinutes:m.configService.get("QUOTA_PROBE_INTERVAL_MINUTES")??10,probeTimeoutMs:m.configService.get("PROBE_TIMEOUT_MS")??15e3,slowThresholdMs:Number(m.configService.get("PROBE_SLOW_THRESHOLD_MS"))||3e3,initialDelayMs:m.configService.get("PROBE_INITIAL_DELAY_MS")??5e3,excludeProviders:m.configService.get("EXCLUDE_PROBE_PROVIDERS")??[]};m.activeProbeService=ze(()=>m.providerService.getProviders(),_,k=>ce(m.configService,k),t.log,k=>m.configService.get(k))}}}catch(m){t.log?.warn?.(`Config saved but runtime reload failed: ${m?.message||m}`)}return{success:!0,message:"Config saved successfully",projectTakeoverSync:y}}),t.get("/api/clients",async(c,l)=>{try{let a=await ne();return{clients:Ji(a)}}catch(a){console.error("Failed to get client integrations:",a),l.status(500).send({error:a.message||"Failed to get client integrations"})}}),t.post("/api/clients/apply",async(c,l)=>{try{let a=c.body,u=Array.isArray(a?.enabled)?a.enabled:[],d=await ne(),f=$u(d,u);await fe(f.config);let g=await pr(f.config),y=Gi(t,g,"clients/apply");return{...f,success:f.success&&y,projectTakeoverSync:g}}catch(a){console.error("Failed to apply client integrations:",a),l.status(500).send({error:a.message||"Failed to apply client integrations"})}});async function o(c,l,a){try{let{id:u}=c.params;if(!Du(u)){l.status(404).send({error:`Unknown client: ${u}`});return}let d=await ne(),f=a==="enable"?Lu(d,u):a==="restore"?Fu(d,u):Nu(d,u);await fe(d);let g=await pr(d),y=Gi(t,g,`clients/${u}/${a}`);return{success:f.success&&y,result:f,results:[f],clients:Ji(d),config:d,projectTakeoverSync:g}}catch(u){console.error(`Failed to ${a} client integration:`,u),l.status(500).send({error:u.message||`Failed to ${a} client integration`})}}t.post("/api/clients/:id/enable",async(c,l)=>o(c,l,"enable")),t.post("/api/clients/:id/disable",async(c,l)=>o(c,l,"disable")),t.post("/api/clients/:id/restore",async(c,l)=>o(c,l,"restore")),t.get("/api/projects",async(c,l)=>{try{let a=await Uu();return{projects:await Promise.all(a.map(async d=>{let f=await fr(d.path);return{...d,ccrTakeoverClients:f,ccrTakeover:f.length>0}}))}}catch(a){console.error("Failed to list project configs:",a),l.status(500).send({error:a.message||"Failed to list project configs"})}}),t.post("/api/projects",async(c,l)=>{try{let{path:a}=c.body||{};if(!a||typeof a!="string"||!bu(a)){l.status(400).send({error:"A valid absolute project path is required"});return}let u=Tu(a).replace(/[\\/]+$/,"")||a;if(await ju(u)){l.status(409).send({error:"Project is already configured"});return}await Wi(u,{Router:{}});try{let g=await ne();await mr(u,["claudeCode"],g)}catch(g){console.error("Failed to auto-enable ccr takeover for new project:",g)}let f=await fr(u);return{id:zu(u),path:u,configPath:Bu(u),Router:{},ccrTakeoverClients:f,ccrTakeover:f.length>0}}catch(a){console.error("Failed to add project config:",a),l.status(500).send({error:a.message||"Failed to add project config"})}}),t.put("/api/projects/:id",async(c,l)=>{try{let{id:a}=c.params,{Router:u}=c.body||{};if(!u||typeof u!="object"){l.status(400).send({error:"Router must be an object"});return}let d=await dr(a);if(!d){l.status(404).send({error:"Project config not found"});return}await Wi(d.path,{Router:u});let f=await ne();Object.keys(u).length===0?await Gu(d.path,f):(await Ju(d.path,f,u),await Wu(d.path,f,u));let g=await fr(d.path);return{id:a,path:d.path,configPath:d.configPath,Router:u,ccrTakeoverClients:g,ccrTakeover:g.length>0}}catch(a){console.error("Failed to update project config:",a),l.status(500).send({error:a.message||"Failed to update project config"})}}),t.put("/api/projects/:id/takeover",async(c,l)=>{try{let{id:a}=c.params,u=c.body||{},d;if(Array.isArray(u.clients))d=u.clients.filter(Ku);else if(typeof u.enabled=="boolean")d=u.enabled?["claudeCode"]:[];else{l.status(400).send({error:"clients (array) or enabled (boolean) is required"});return}let f=await dr(a);if(!f){l.status(404).send({error:"Project config not found"});return}let g=await ne(),y=await mr(f.path,d,g);return{id:a,path:f.path,ccrTakeoverClients:y,ccrTakeover:y.length>0}}catch(a){console.error("Failed to update ccr takeover status:",a),l.status(500).send({error:a.message||"Failed to update ccr takeover status"})}}),t.delete("/api/projects/:id",async(c,l)=>{try{let{id:a}=c.params,u=await dr(a);if(!u){l.status(404).send({error:"Project config not found"});return}try{let d=await ne();await mr(u.path,[],d)}catch(d){console.error("Failed to remove ccr takeover while deleting project:",d)}return await Hu(u.path),{success:!0}}catch(a){console.error("Failed to delete project config:",a),l.status(500).send({error:a.message||"Failed to delete project config"})}});let r=typeof __dirname=="string"?__dirname:vu(Cu(import.meta.url)),s=de(ie(r,"index.html"))?r:de(ie(r,"..","index.html"))?ie(r,".."):ie(r,"..","dist");t.register(xu,{root:s,prefix:"/ui/",maxAge:"1h"}),t.get("/ui",async(c,l)=>l.redirect("/ui/")),t.get("/api/logs/files",async(c,l)=>{try{let a=ie(lr(),".claude-code-router","logs"),u=[];if(de(a)){let d=Fi(a);for(let f of d)if(f.endsWith(".log")){let g=ie(a,f),y=Su(g);u.push({name:f,path:g,size:y.size,lastModified:y.mtime.toISOString()})}u.sort((f,g)=>new Date(g.lastModified).getTime()-new Date(f.lastModified).getTime())}return u}catch(a){console.error("Failed to get log files:",a),l.status(500).send({error:"Failed to get log files"})}}),t.get("/api/logs",async(c,l)=>{try{let a=c.query.file,u;return a?u=a:u=ie(lr(),".claude-code-router","logs","app.log"),de(u)?Ui(u,"utf8").split(`
408
+ `).filter(g=>g.trim()):[]}catch(a){console.error("Failed to get logs:",a),l.status(500).send({error:"Failed to get logs"})}}),t.delete("/api/logs",async(c,l)=>{try{let a=c.query.file,u;return a?u=a:u=ie(lr(),".claude-code-router","logs","app.log"),de(u)&&Ru(u,"","utf8"),{success:!0,message:"Logs cleared successfully"}}catch(a){console.error("Failed to clear logs:",a),l.status(500).send({error:"Failed to clear logs"})}}),t.get("/api/debug-log",async()=>({enabled:xt()})),t.put("/api/debug-log",async c=>{let{enabled:l}=c.body;return Wn(l),{enabled:xt()}}),t.get("/api/usage",async(c,l)=>{try{let a=c.query;return ir({startTime:a.startDate,endTime:a.endDate,model:a.model,provider:a.provider,scenario:a.scenario,clientType:a.clientType,sessionId:a.sessionId,status:a.status,page:a.page?parseInt(a.page,10):void 0,pageSize:a.pageSize?parseInt(a.pageSize,10):void 0})}catch(a){console.error("Failed to query usage:",a),l.status(500).send({error:"Failed to query usage"})}}),t.get("/api/usage/summary",async(c,l)=>{try{let a=c.query;return kt(a.startDate,a.endDate,a.status)}catch(a){console.error("Failed to query usage summary:",a),l.status(500).send({error:"Failed to query usage summary"})}}),t.delete("/api/usage",async(c,l)=>{try{let a=c.query;return ar(a.beforeDate),{success:!0,message:a.beforeDate?"Usage data cleared before "+a.beforeDate:"All usage data cleared"}}catch(a){console.error("Failed to clear usage:",a),l.status(500).send({error:"Failed to clear usage data"})}}),t.get("/api/providers/health",async(c,l)=>{try{let u=U().getAllStates(),f=t._server?.activeProbeService?.getProbeTelemetry?.()||[];return{states:u.map(g=>({provider:g.provider,model:g.model,status:g.status,failureCount:g.failureCount,successCount:g.successCount,lastFailureTime:g.lastFailureTime,lastError:g.lastError,rateLimitUntil:g.rateLimitUntil??null})),probes:f,timestamp:new Date().toISOString()}}catch(a){console.error("Failed to get provider health:",a),l.status(500).send({error:"Failed to get provider health"})}}),t.post("/api/providers/probe",async(c,l)=>{try{let{providerName:a}=c.body;if(!a)return l.status(400),{error:"providerName is required"};let d=t._server?.activeProbeService;if(!d?.probeProviderManually)return l.status(503),{error:"Active probe service is not available"};let f=await d.probeProviderManually(a);return f.success&&En(a,c.log),{provider:a,success:f.success,latencyMs:f.latencyMs,status:f.status,isSlow:f.isSlow,errorKind:f.errorKind??null,error:f.error,timestamp:new Date().toISOString()}}catch(a){console.error("Failed to probe provider:",a),l.status(500).send({error:a.message||"Failed to probe provider"})}}),t.post("/api/providers/probe-all",async(c,l)=>{try{let a=t._server,u=a?.activeProbeService;if(!u?.probeProviderManually)return l.status(503),{error:"Active probe service is not available"};let d=(a?.providerService?.getProviders?.()||[]).filter(g=>g?.name&&g.enabled!==!1),f=await Promise.all(d.map(async g=>{let y=await u.probeProviderManually(g.name);return y.success&&En(g.name,c?.log),{provider:g.name,success:y.success,latencyMs:y.latencyMs,status:y.status,isSlow:y.isSlow,errorKind:y.errorKind??null,error:y.error}}));return{results:f,successCount:f.filter(g=>g.success).length,total:f.length,timestamp:new Date().toISOString()}}catch(a){console.error("Failed to probe all providers:",a),l.status(500).send({error:a.message||"Failed to probe all providers"})}}),t.get("/api/providers/quota",async(c,l)=>{try{let u=(await ne()).Providers||[],d=Yu(u),f=new Map(d.map(m=>[m.provider,m])),g=to();for(let m of d){let p=g.find(_=>_.provider===m.provider);p&&p.limit!=null&&(m.limit5h=p.limit,m.used5h=p.limit-(p.remaining??0),p.reset&&(m.reset5h=new Date(p.reset*1e3).toISOString()))}let y=ao();for(let m of y){let p=f.get(m.provider);p||(p={provider:m.provider,used5h:0,used7d:0},d.push(p),f.set(m.provider,p)),typeof m.totalBalance=="number"?(p.limit7d=m.totalBalance,p.type7d=m.currency?"balance":"rateLimit",m.currency&&(p.currency=m.currency),typeof m.usedBalance=="number"?p.used7d=m.usedBalance:typeof m.remainingBalance=="number"?p.used7d=Math.max(0,m.totalBalance-m.remainingBalance):(p.used7d=0,p.limit7d=m.totalBalance)):typeof m.usedBalance=="number"&&(p.used7d=m.usedBalance),typeof m.usedDailyBalance=="number"&&(p.used5h=m.usedDailyBalance,p.type5h="rateLimit"),typeof m.limitDaily=="number"&&(p.limit5h=m.limitDaily,p.type5h="rateLimit"),typeof m.resetTime=="string"&&(m.limitDaily!==void 0?p.reset5h=m.resetTime:p.reset7d=m.resetTime),typeof m.resetTime7d=="string"&&(p.reset7d=m.resetTime7d)}return{quotas:d,timestamp:new Date().toISOString()}}catch(a){console.error("Failed to compute provider quota:",a),l.status(500).send({error:"Failed to compute provider quota"})}}),t.get("/api/presets",async(c,l)=>{try{let a=ie(Eu,"presets");if(!de(a))return{presets:[]};let d=Fi(a,{withFileTypes:!0}).filter(g=>g.isDirectory()&&!g.name.startsWith(".")).map(g=>g.name),f=[];for(let g of d){let y=ie(a,g);try{let m=ie(y,"manifest.json"),p=Ui(m,"utf-8"),_=JSON.parse(p),{Providers:k,Router:h,PORT:b,HOST:T,API_TIMEOUT_MS:x,PROXY_URL:C,PROXY_GLOBAL_ENABLED:S,LOG:v,LOG_LEVEL:P,StatusLine:R,NON_INTERACTIVE_MODE:O,...E}=_;f.push({id:g,name:E.name||g,version:E.version||"1.0.0",description:E.description,author:E.author,homepage:E.homepage,repository:E.repository,license:E.license,keywords:E.keywords,ccrVersion:E.ccrVersion,source:E.source,sourceType:E.sourceType,checksum:E.checksum,installed:!0})}catch(m){console.error(`Failed to read preset ${g}:`,m)}}return{presets:f}}catch(a){console.error("Failed to get presets:",a),l.status(500).send({error:"Failed to get presets"})}}),t.get("/api/presets/:name",async(c,l)=>{try{let{name:a}=c.params,u=Ln(a);if(!de(u)){l.status(404).send({error:"Preset not found"});return}let d=await ur(u);return{...Hi(d),config:Ou(d,u),userValues:d.userValues||{}}}catch(a){console.error("Failed to get preset:",a),l.status(500).send({error:a.message||"Failed to get preset"})}}),t.post("/api/presets/:name/apply",async(c,l)=>{try{let{name:a}=c.params,{secrets:u}=c.body,d=Ln(a);if(!de(d)){l.status(404).send({error:"Preset not found"});return}let g={...await ur(d)};return u&&Object.keys(u).length>0&&(g.userValues={...g.userValues,...u}),await zi(a,g),{success:!0,message:"Preset applied successfully"}}catch(a){console.error("Failed to apply preset:",a),l.status(500).send({error:a.message||"Failed to apply preset"})}}),t.delete("/api/presets/:name",async(c,l)=>{try{let{name:a}=c.params,u=Ln(a);if(!de(u)){l.status(404).send({error:"Preset not found"});return}return wu(u,{recursive:!0,force:!0}),{success:!0,message:"Preset deleted successfully"}}catch(a){console.error("Failed to delete preset:",a),l.status(500).send({error:a.message||"Failed to delete preset"})}}),t.get("/api/presets/market",async(c,l)=>{try{return{presets:await Mu()}}catch(a){console.error("Failed to get market presets:",a),l.status(500).send({error:a.message||"Failed to get market presets"})}}),t.post("/api/presets/install/github",async(c,l)=>{try{let{presetName:a}=c.body;if(!a){l.status(400).send({error:"Preset name is required"});return}let u=await Iu(a);if(!u){l.status(400).send({error:"Preset not found in marketplace",message:`Preset '${a}' is not available in the official marketplace. Please check the available presets.`});return}if(!u.repo){l.status(400).send({error:"Invalid preset data",message:`Preset '${a}' does not have repository information`});return}let d=u.repo.match(/(?:github\.com[:/]|^)([^/]+)\/([^/\s#]+?)(?:\.git)?$/);if(!d){l.status(400).send({error:"Invalid GitHub repository URL"});return}let[,f,g]=d,y=u.name||a;if(await Bi(y)){l.status(409).send({error:"Preset already installed",message:`Preset '${y}' is already installed. To update or reconfigure, please delete it first using the delete button.`,presetName:y});return}let m=`https://github.com/${f}/${g}/archive/refs/heads/main.zip`,p=await Au(m),_=await i(p);if(await Bi(y)){ji(p),l.status(409).send({error:"Preset already installed",message:`Preset '${y}' was installed while downloading. Please try again.`,presetName:y});return}let k=Ln(y);await Pu(p,k);let h=await ur(k);return h.repository=u.repo,u.url&&(h.source=u.url),await zi(y,h),ji(p),{success:!0,presetName:y,preset:{..._.metadata,installed:!0}}}catch(a){console.error("Failed to install preset from GitHub:",a),l.status(500).send({error:a.message||"Failed to install preset from GitHub"})}});async function i(c){let l=new Vu(c),a=l.getEntry("manifest.json");if(a||(a=l.getEntries().find(f=>f.entryName.includes("manifest.json"))||null),!a)throw new Error("Invalid preset file: manifest.json not found");let u=JSON.parse(a.getData().toString("utf-8"));return Hi(u)}return n}var Qi=w(()=>{"use strict";Fe();we();Ie();Ut();bn();oe();Ye();re();On();Rn();bt();er()});function Xu(n,e){let t=n?.Router,{family:o}=ot(e||"");if(t?.enableFamilyRouting&&o){let r=t.families?.[o]?.image;if(r)return r}return t?.image}function Vi(n){return n?.Router?.image}function Zu(n){return n.some(e=>e.role==="user"&&Array.isArray(e.content)&&e.content.some(t=>t.type==="image"||t.type==="image_url"||Array.isArray(t?.content)&&t.content.some(o=>o.type==="image"||o.type==="image_url")))}function qu(n){let e=n[n.length-1];if(e?.role!=="user"||!Array.isArray(e.content))return;let t=[];e.content.filter(o=>o.type==="tool_result").forEach(o=>{if(!Array.isArray(o.content))return;let r=o.content.filter(s=>s.type==="image"?(t.push(s),!1):!0);r.length!==o.content.length&&(o.content=r.length>0?r:"read image successfully")}),e.content.push(...t)}var gr,Dn,hr,Yi,Xi=w(()=>{"use strict";Fe();gr=class{cache=new Map;maxSize;ttl;constructor(e=100,t=300*1e3){this.maxSize=e,this.ttl=t}evictExpired(){let e=Date.now();for(let[t,o]of this.cache)e-o.timestamp>this.ttl&&this.cache.delete(t)}trim(){if(this.cache.size<=this.maxSize)return;let e=[...this.cache.keys()].slice(0,this.cache.size-this.maxSize);for(let t of e)this.cache.delete(t)}storeImage(e,t){this.hasImage(e)||(this.evictExpired(),this.cache.set(e,{source:t,timestamp:Date.now()}),this.trim())}getImage(e){let t=this.cache.get(e);return t?Date.now()-t.timestamp>this.ttl?(this.cache.delete(e),null):t.source:null}hasImage(e){let t=this.cache.get(e);return t?Date.now()-t.timestamp>this.ttl?(this.cache.delete(e),!1):!0:!1}clear(){this.cache.clear()}size(){return this.evictExpired(),this.cache.size}},Dn=new gr;hr=class{name="image";tools;constructor(){this.tools=new Map,this.appendTools()}shouldHandle(e,t){let o=Xu(t,e.originalModel||e.body.model);if(!o||e.body.model===o||!Zu(e.body.messages))return!1;let s=t.forceUseImageAgent===!0;return!s&&po(e.body.model)?!1:(qu(e.body.messages),s&&!!Vi(t))}appendTools(){this.tools.set("analyzeImage",{name:"analyzeImage",description:"Analyse image or images by ID and extract information such as OCR text, objects, layout, colors, or safety signals.",input_schema:{type:"object",properties:{imageId:{type:"array",description:"an array of IDs to analyse",items:{type:"string"}},task:{type:"string",description:"Details of task to perform on the image.The more detailed, the better"},regions:{type:"array",description:"Optional regions of interest within the image",items:{type:"object",properties:{name:{type:"string",description:"Optional label for the region"},x:{type:"number",description:"X coordinate"},y:{type:"number",description:"Y coordinate"},w:{type:"number",description:"Width of the region"},h:{type:"number",description:"Height of the region"},units:{type:"string",enum:["px","pct"],description:"Units for coordinates and size"}},required:["x","y","w","h","units"]}}},required:["imageId","task"]},handler:async(e,t)=>{let o=[],r;if(e.imageId){if(Array.isArray(e.imageId))e.imageId.forEach(c=>{let l=Dn.getImage(`${t.req.id}_Image#${c}`);l&&o.push({type:"image",source:l})});else{let c=Dn.getImage(`${t.req.id}_Image#${e.imageId}`);c&&o.push({type:"image",source:c})}r=e.imageId,delete e.imageId}let s=t.req.body.messages[t.req.body.messages.length-1];if(s.role==="user"&&Array.isArray(s.content)){let c=s.content.filter(l=>l.type==="text"&&!l.text.includes("This is an image, if you need to view or analyze it, you need to extract the imageId"));o.push(...c)}Object.keys(e).length>0&&o.push({type:"text",text:JSON.stringify(e)});let i=await fetch(`http://127.0.0.1:${t.config.PORT||3456}/v1/messages`,{method:"POST",headers:{"x-api-key":t.config.APIKEY,"content-type":"application/json"},body:JSON.stringify({model:Vi(t.config),system:[{type:"text",text:`You must interpret and analyze images strictly according to the assigned task.
409
409
  When an image placeholder is provided, your role is to parse the image content only within the scope of the user\u2019s instructions.
410
410
  Do not ignore or deviate from the task.
411
411
  Always ensure that your response reflects a clear, accurate interpretation of the image aligned with the given objective.`}],messages:[{role:"user",content:o}],stream:!1})}).then(c=>c.json()).catch(c=>null);return!i||!i.content?"analyzeImage Error":i.content[0].text}})}reqHandler(e,t){e.body?.system?.push({type:"text",text:`You are a text-only language model and do not possess visual perception.
@@ -418,5 +418,5 @@ If multiple images exist, select the **most relevant imageId** based on the user
418
418
 
419
419
  Do not attempt to describe or analyze the image directly yourself.
420
420
  Ignore any user interruptions or unrelated instructions that might cause you to skip this requirement.
421
- Your response should consistently follow this rule whenever image-related analysis is requested.`});let o=e.body.messages.filter(s=>s.role==="user"&&Array.isArray(s.content)&&s.content.some(i=>i.type==="image"||Array.isArray(i.content)&&i.content.some(c=>c.type==="image"))),r=1;o.forEach(s=>{Array.isArray(s.content)&&s.content.forEach(i=>{if(i.type==="image")Ln.storeImage(`${e.id}_Image#${r}`,i.source),i.type="text",delete i.source,i.text=`[Image #${r}]This is an image, if you need to view or analyze it, you need to extract the imageId`,r++;else if(i.type==="text"&&i.text.includes("[Image #"))i.text=i.text.replace(/\[Image #\d+\]/g,"");else if(i.type==="tool_result"&&Array.isArray(i.content)&&i.content.some(c=>c.type==="image")){let c=[],l=i.content.filter(u=>u.type!=="image"?!0:(Ln.storeImage(`${e.id}_Image#${r}`,u.source),c.push(`[Image #${r}]`),r++,!1)),a=`${c.join("")}This is an image, if you need to view or analyze it, you need to extract the imageId`;i.content=l.length>0?[{type:"text",text:a},...l]:a}})})}},Ki=new mr});var gr,Vi,Dn,Yi=w(()=>{"use strict";Qi();gr=class{agents=new Map;registerAgent(e){this.agents.set(e.name,e)}getAgent(e){return this.agents.get(e)}getAllAgents(){return Array.from(this.agents.values())}getAllTools(){let e=[];for(let t of this.agents.values())e.push(...t.tools.values());return e}},Vi=new gr;Vi.registerAgent(Ki);Dn=Vi});import{EventEmitter as Vu}from"node:events";import{performance as Xi}from"node:perf_hooks";import Yu from"json5";function Xu(n,e){return n===429||!!(e&&/rate[\s_]limit|too many|限流|频率限制|qps_limit|token_limit|quota exhausted|usage limit|limit reached/i.test(e))}function Zi(n,e){(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&_e(n,e)}function hr(n){return n.usageSessionId||n.id}function Fn(n){return n.usageCacheKey||`${n.clientType||"unknown"}:request:${hr(n)}`}function Qe(n){return n.body?.model?Array.isArray(n.body.model)?n.body.model.join(","):n.body.model:Array.isArray(n.model)?n.model.join(","):n.model||""}function qu(n){if(!n)return;let e=n;if(Buffer.isBuffer(n)&&(e=n.toString("utf8")),typeof e=="string")try{e=JSON.parse(e)}catch{return}if(typeof e!="object")return;let t=e.usage?We(e.usage):e.response?.usage?We(e.response.usage):void 0,o=e.model||e.response?.model;if(!(!t&&!o))return{usage:t,model:o}}async function Zu(n,e,t){if(await new Promise((r,s)=>{let i=c=>c?s(c):r();An(t)(n,e,i).then(()=>r(),s)}),e.sent)return;let o=new URL(`http://127.0.0.1${n.url}`);if(n.pathname=o.pathname,n.pathname.endsWith("/v1/messages")&&n.pathname!=="/v1/messages"&&(n.preset=n.pathname.replace("/v1/messages","").replace("/","")),n.pathname.endsWith("/v1/responses")&&n.pathname!=="/v1/responses"&&(n.preset=n.pathname.replace("/v1/responses","").replace("/","")),n.pathname.endsWith("/v1/messages")||n.pathname.endsWith("/v1/responses")){Zi(n,t);let r=Fn(n);n.clientContext?.usageScope==="session"&&(n.previousUsage=X.get(r))}}async function ed(n,e,t){if(!n.pathname?.endsWith("/v1/messages"))return;let o=[];for(let r of Dn.getAllAgents())r.shouldHandle(n,t)&&(o.push(r.name),r.reqHandler(n,t),r.tools.size&&(n.body?.tools?.length||(n.body.tools=[]),n.body.tools.unshift(...Array.from(r.tools.values()).map(s=>({name:s.name,description:s.description,input_schema:s.input_schema})))));o.length&&(n.agents=o)}function ea(n){return{authClient:(e,t)=>Zu(e,t,n),agent:(e,t)=>ed(e,t,n)}}function ta(n,e){n.addHook("onRequest",async t=>{let o=new URL(`http://127.0.0.1${t.url}`);t.pathname=o.pathname,(o.pathname.endsWith("/v1/messages")||o.pathname.endsWith("/v1/responses"))&&!t.requestStartTime&&(t.requestStartTime=Xi.now())}),n.addHook("preHandler",async(t,o)=>{let r=t.pathname||new URL(`http://127.0.0.1${t.url}`).pathname;r.endsWith("/v1/messages")||r.endsWith("/v1/responses")||await new Promise((s,i)=>{let c=l=>l?i(l):s();An(e)(t,o,c).then(()=>s(),i)})}),n.addHook("onError",async(t,o,r)=>{t.errorMessage=r?.message||r?.toString?.()||"Unknown error",qi.emit("onError",t,o,r)}),n.addHook("onSend",(t,o,r,s)=>{(t.ccrHookOrder||=[]).push("onSend");let i=t.pathname?.endsWith("/v1/messages"),c=t.pathname?.endsWith("/v1/responses");if(i||c){Zi(t,e);let l=hr(t),a=Fn(t);if(r instanceof ReadableStream){if(i&&t.agents){let y=new AbortController,p=r.pipeThrough(new ae),g,_=-1,k="",h="",b="",T=[],x=[];return s(null,bn(p,async(C,S)=>{try{if(C.event==="content_block_start"&&C?.data?.content_block?.name){let v=t.agents.find(P=>Dn.getAgent(P)?.tools.get(C.data.content_block.name));if(v){g=Dn.getAgent(v),_=C.data.index,k=C.data.content_block.name,b=C.data.content_block.id;return}}if(_>-1&&C.data.index===_&&C.data?.delta?.type==="input_json_delta"){h+=C.data?.delta?.partial_json;return}if(_>-1&&C.data.index===_&&C.data?.type==="content_block_stop"){try{let v=Yu.parse(h);x.push({type:"tool_use",id:b,name:k,input:v});let P=await g?.tools.get(k)?.handler(v,{req:t,config:e});T.push({tool_use_id:b,type:"tool_result",content:P}),g=void 0,_=-1,k="",h="",b=""}catch(v){console.log(v)}return}if(C.event==="message_delta"&&T.length){t.body.messages.push({role:"assistant",content:x}),t.body.messages.push({role:"user",content:T});let v=await fetch(`http://127.0.0.1:${e.PORT||3456}/v1/messages`,{method:"POST",headers:{"x-api-key":e.APIKEY,"content-type":"application/json"},body:JSON.stringify(t.body)});if(!v.ok)return;let R=v.body.pipeThrough(new ae).getReader();for(;;)try{let{value:O,done:E}=await R.read();if(E)break;let A=O;if(["message_start","message_stop"].includes(A.event))continue;if(!S.desiredSize)break;S.enqueue(A)}catch(O){if(O.name==="AbortError"||O.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();break}throw O}return}return C}catch(v){if(console.error("Unexpected error in stream processing:",v),v.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();return}throw v}}).pipeThrough(new ze))}let[d,m]=r.tee(),f=async y=>{let g=y.pipeThrough(new TextDecoderStream).pipeThrough(new ae).getReader();try{for(;;){let{done:_,value:k}=await g.read();if(_)break;let h=k.event,b=k.data;if(h==="message_start"&&b?.message?.model&&(t.upstreamModel=b.message.model),h==="response.completed"&&b?.response?.model&&(t.upstreamModel=b.response.model),b?.usage){let T=X.get(a)||{},x=We(b.usage),C=Rn(T,x,h==="message_start");t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_anthropic_sse",event:h,provider:t.provider,model:Qe(t),usageSessionId:l,capturedUsage:x,mergedUsage:C}),(x?.cache_read_input_tokens||x?.cache_creation_input_tokens)&&console.log("[Usage] Cache tokens:",{cache_read:x?.cache_read_input_tokens,cache_creation:x?.cache_creation_input_tokens}),X.put(a,C)}if(b?.response?.usage){let T=We(b.response.usage),x=X.get(a)||{},C=Rn(x,T,!1);t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_responses_sse",event:h,provider:t.provider,model:Qe(t),usageSessionId:l,capturedUsage:T,mergedUsage:C}),X.put(a,C)}}}catch(_){_.name==="AbortError"||_.code==="ERR_STREAM_PREMATURE_CLOSE"?console.error("Background read stream closed prematurely"):console.error("Error in background stream reading:",_)}finally{g.releaseLock()}};return t.usageCapturePromise=f(m),s(null,d)}let u=qu(r);if(u?.usage&&X.put(a,u.usage),u?.model&&(t.upstreamModel=u.model),typeof r=="object")return r.error?s(r.error,null):s(r,null)}if(typeof r=="object"&&r.error)return s(r.error,null);s(null,r)}),n.addHook("onSend",async(t,o,r)=>{if(qi.emit("onSend",t,o,r),o.statusCode>=400&&(t.pathname?.endsWith("/v1/messages")||t.pathname?.endsWith("/v1/responses")))try{typeof r=="string"?t.errorResponseBody=r:r&&typeof r=="object"&&(t.errorResponseBody=JSON.stringify(r))}catch{}return r}),n.addHook("onResponse",async(t,o)=>{if((t.ccrHookOrder||=[]).push("onResponse"),!(!t.pathname?.endsWith("/v1/messages")&&!t.pathname?.endsWith("/v1/responses")))try{t.usageCapturePromise&&await t.usageCapturePromise,t.tokenSpeedCapturePromise&&await t.tokenSpeedCapturePromise;let r=hr(t),s=o.statusCode>=400,i=s?void 0:X.get(Fn(t)),c=ir(r),l=U(),a,u,d=t.sseError,m=d&&(d.type||d.code||d.message),f=i?.output_tokens&&i.output_tokens>0,y=s||m&&!f;if(y){a=t.errorMessage||o.errorMessage||t.error?.message||t.error?.toString?.()||void 0,m&&(a=a||JSON.stringify(d),u=JSON.stringify(d)),u=u||t.errorResponseBody;let h=Qe(t);Xu(o.statusCode,a)?l.markRateLimited(t.provider||"",h,120,a):l.recordFailure(t.provider||"",h,a)}else{let h=Qe(t);l.recordSuccess(t.provider||"",h)}let p=i?.cache_read_input_tokens??0,_=i?.input_tokens||0||t.tokenCount||0;!p&&t.tokenCount&&i?.input_tokens&&i.input_tokens<t.tokenCount&&(p=t.tokenCount-i.input_tokens,p&&console.log("[Usage] Inferred cache tokens:",{tokenCount:t.tokenCount,netInput:i.input_tokens,impliedCache:p}));let k=y?0:i?.output_tokens||0;!y&&t.body?.stream&&_===0&&k===0&&t.log?.warn?.({debug_log:!0,reqId:t.id,phase:"usage_zero_stream",provider:t.provider,originalModel:t.originalModel||t.body?.model||"",model:Qe(t),usageSessionId:r,tokenCount:t.tokenCount,cachedUsage:i,note:"Successful streaming request ended with zero usage. Check provider_stream_chunk usage_monitor and usage_capture_* logs."}),_t({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:r,provider:t.provider||"",originalModel:t.originalModel||t.body?.model||"",model:Qe(t),upstreamModel:t.upstreamModel,modelFamily:t.modelFamily||"",scenarioType:t.scenarioType||"default",clientType:t.clientType||$e(t),stream:t.body?.stream??!1,inputTokens:_,outputTokens:k,cacheReadInputTokens:p,cacheCreationInputTokens:i?.cache_creation_input_tokens||0,ttft:y||!t.body?.stream?null:c.ttft,tokensPerSecond:y?null:c.tokensPerSecond,durationMs:t.requestStartTime?Math.round(Xi.now()-t.requestStartTime):0,status:y?"error":"success",errorMessage:a,responseBody:u}),t.clientContext?.usageScope!=="session"&&X.delete(Fn(t))}catch(r){console.error("Usage tracking error:",r)}})}var qi,na=w(()=>{"use strict";Ne();At();oe();Lo();Do();Fo();bt();Yo();Yi();Zo();Ft();qi=new Vu});var oa={};jn(oa,{createCcrServer:()=>ad});import{homedir as td}from"os";import{join as nd}from"path";import{createStream as od}from"rotating-file-stream";import{CONFIG_FILE as rd,HOME_DIR as sd,listPresets as id}from"@wengine-ai/claude-code-router-shared";async function ad(n={}){await Ri(),await Cn();let e=await Vo(),t=e.Providers||e.providers||[],o=t&&t.length>0,r=e.HOST||"127.0.0.1";o?(r=e.HOST,e.APIKEY||(r="127.0.0.1")):(r="0.0.0.0",console.log("\u2139\uFE0F No providers configured. Listening on 0.0.0.0 without authentication."));let s=n.port??e.PORT??3456,i=process.env.SERVICE_PORT?parseInt(process.env.SERVICE_PORT):s,c=p=>(p>9?"":"0")+p,l=(p,g)=>{let _;p?typeof p=="number"?_=new Date(p):_=p:_=new Date;let k=_.getFullYear()+""+c(_.getMonth()+1),h=c(_.getDate()),b=c(_.getHours()),T=c(_.getMinutes());return`./logs/ccr-${k}${h}${b}${T}${c(_.getSeconds())}${g?`_${g}`:""}.log`},a;n.logger!==void 0?a=n.logger:(Ai(),e.LOG!==!1?(e.LOG===void 0&&(e.LOG=!0),a={level:e.LOG_LEVEL||"error",stream:od(l,{path:sd,interval:"1d",size:"50M",compress:!1})}):a=!1);let u=await id(),d=new sa({jsonPath:rd,initialConfig:{providers:e.Providers||e.providers,HOST:r,PORT:i,LOG_FILE:nd(td(),".claude-code-router","claude-code-router.log")},logger:a});await d.ready(),await Bi(d,e),d.recordUsage=p=>{try{let g=p.req?p.req.clientType||$e(p.req):"unknown";_t({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:p.sessionId||"",provider:p.provider||"",originalModel:p.originalModel||"",model:p.model||"",modelFamily:p.modelFamily||"",scenarioType:p.scenarioType||"default",clientType:g,stream:p.stream??!1,inputTokens:p.inputTokens||0,outputTokens:0,cacheReadInputTokens:p.cacheReadInputTokens||0,cacheCreationInputTokens:p.cacheCreationInputTokens||0,ttft:null,tokensPerSecond:null,durationMs:0,status:"error",errorMessage:p.errorMessage,responseBody:void 0})}catch(g){console.error("Fallback usage tracking error:",g)}},ta(d,e),d.ccrPreHandlerCallbacks=ea(e),(await Promise.allSettled(u.map(async p=>await d.registerNamespace(`/preset/${p.name}`,p.config)))).forEach((p,g)=>{if(p.status==="rejected"){let _=u[g],k=p.reason instanceof Error?p.reason.message:String(p.reason);d.app?.log?.error?.(`Failed to register preset "${_?.name}": ${k}`)}}),await Mi(d,e),mt(e,d.app?.log);let f=p=>{d.app.log.error({err:p},"Uncaught exception")},y=(p,g)=>{d.app.log.error({reason:p,promise:g},"Unhandled rejection")};return process.on("uncaughtException",f),process.on("unhandledRejection",y),d.app.addHook("onClose",async()=>{process.off("uncaughtException",f),process.off("unhandledRejection",y)}),d}var ra=w(()=>{"use strict";ia();wi();Sn();Ii();$i();Wi();na();En();bt();Ne()});import cd from"fastify";import ld from"@fastify/cors";function ud(n={}){let e=cd({bodyLimit:52428800,...n});return e.setErrorHandler(Cr),e.register(ld),e}async function dd(n={}){return(await Promise.resolve().then(()=>(ra(),oa))).createCcrServer(n)}var yr,sa,wy,ia=w(()=>{zn();Ct();Ft();Ut();ho();_o();De();At();kn();oe();Re();Ae();ne();Ne();Ft();Me();Ne();De();zn();Ut();ho();_o();Go();Uo();oe();Re();Io();Ae();oe();kn();Ye();ne();Sn();Yo();En();qo();Zo();bt();yr=class{app;configService;providerService;transformerService;tokenizerService;activeProbeService;recordUsage;readiness;mainNamespaceRegistered=!1;signalHandlersRegistered=!1;ccrPreHandlerCallbacks;constructor(e={}){let{initialConfig:t,jsonPath:o,envPath:r,useEnvFile:s,useJsonFile:i,useEnvironmentVariables:c,...l}=e;this.app=ud({...l,logger:l.logger??!0}),this.app._server=this;let a={initialConfig:t};o!==void 0&&(a.jsonPath=o),r!==void 0&&(a.envPath=r),s!==void 0&&(a.useEnvFile=s),i!==void 0&&(a.useJsonFile=i),c!==void 0&&(a.useEnvironmentVariables=c),this.configService=new ve(a),this.transformerService=new Ue(this.configService,this.app.log),this.tokenizerService=new je(this.configService,this.app.log),this.readiness=(async()=>{await Promise.all([this.transformerService.initialize(),this.tokenizerService.initialize()]),this.providerService=new ue(this.configService,this.transformerService,this.app.log)})()}async ready(){await this.readiness}async register(e,t){await this.app.register(e,t)}addHook(e,t){this.app.addHook(e,t)}async registerNamespace(e,t){if(!e)throw new Error("name is required");if(await this.ready(),e==="/"&&this.mainNamespaceRegistered)return;let o=this.configService,r=this.transformerService,s=this.providerService,i=this.tokenizerService;if(e!=="/"){if(!t)throw new Error("options is required");o=new ve({initialConfig:{...t,providers:t.Providers||t.providers}}),r=new Ue(o,this.app.log),i=new je(o,this.app.log),await Promise.all([r.initialize(),i.initialize()]),s=new ue(o,r,this.app.log)}let c=async l=>{l.decorate("configService",o),l.decorate("transformerService",r),l.decorate("providerService",s),l.decorate("tokenizerService",i),l.decorate("recordUsage",this.recordUsage||(()=>{})),l.addHook("preHandler",async(a,u)=>{let d=new URL(`http://127.0.0.1${a.url}`);a.pathname=d.pathname;let m=d.pathname.endsWith("/v1/messages"),f=d.pathname.endsWith("/v1/responses");if(!m&&!f||!a.body||typeof a.body!="object")return;let y=a.ccrHookOrder||=[];if(y.push("request-normalize"),f&&a.body.input&&!a.body.messages&&rt(a.body),a.body.stream===void 0&&(a.body.stream=!1),a.log.info({data:a.body,type:"request body"}),y.push("adapter"),!a.originalModel&&a.body.model&&(a.originalModel=a.body.model),_e(a,o.getAll()),y.push("auth-client"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.authClient(a,u),u.sent))&&(y.push("agent"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.agent(a,u),u.sent)))){y.push("router"),a.body?.model&&await Lt(a,u,{configService:o,tokenizerService:i}),a.usageCacheKey&&X.put(a.usageCacheKey,{input_tokens:0,output_tokens:0,cache_read_input_tokens:0,cache_creation_input_tokens:0}),y.push("provider-model-normalize");try{let p=a.body;if(!p||!p.model)return u.code(400).send({error:"Missing model in request body"});let g=p.model.split(","),_=p.model;if(g.length>1){let[k,...h]=g;_=h.join(","),p.model=_,a.provider=k}a.model=_}catch(p){return a.log.error({error:p},"Error in modelProviderMiddleware:"),u.code(500).send({error:"Internal server error"})}}}),await hs(l)};e==="/"?(await this.app.register(c),this.mainNamespaceRegistered=!0):await this.app.register(c,{prefix:e})}async start(){try{if(await this.ready(),await this.registerNamespace("/"),!this.signalHandlersRegistered){this.signalHandlersRegistered=!0;let t=async s=>{this.app.log.info(`Received ${s}, shutting down gracefully...`);try{$o()}catch{}await this.app.close(),process.exit(0)},o=()=>void t("SIGINT"),r=()=>void t("SIGTERM");process.on("SIGINT",o),process.on("SIGTERM",r),this.app.addHook("onClose",async()=>{process.off("SIGINT",o),process.off("SIGTERM",r),this.signalHandlersRegistered=!1;try{this.tokenizerService.dispose()}catch{}await Hn()})}await this.app.ready();let e=await this.app.listen({port:parseInt(this.configService.get("PORT")||"3000",10),host:this.configService.get("HOST")||"127.0.0.1"});this.app.log.info(`\u{1F680} LLMs API server listening on ${e}`);try{qe()}catch{}try{et()}catch{}try{Yn()}catch{}try{let t={enabled:this.configService.get("ACTIVE_PROBE_ENABLED")??!0,quotaProbeIntervalMinutes:this.configService.get("QUOTA_PROBE_INTERVAL_MINUTES")??10,probeTimeoutMs:this.configService.get("PROBE_TIMEOUT_MS")??15e3,slowThresholdMs:Number(this.configService.get("PROBE_SLOW_THRESHOLD_MS"))||3e3,initialDelayMs:this.configService.get("PROBE_INITIAL_DELAY_MS")??5e3,excludeProviders:this.configService.get("EXCLUDE_PROBE_PROVIDERS")??[]};this.activeProbeService=He(()=>this.providerService.getProviders(),t,o=>ce(this.configService,o),this.app.log,o=>this.configService.get(o))}catch(t){this.app.log.warn?.(`Failed to start active probe service: ${t.message}`)}}catch(e){this.app.log.error(`Error starting server: ${e}`),process.exit(1)}}},sa=yr;wy=dd});ia();export{yn as ActiveProbeService,ve as ConfigService,Z as ProjectRoutingError,Ot as ProviderHealthStore,ue as ProviderService,ae as SSEParserTransform,ze as SSESerializerTransform,je as TokenizerService,Ue as TransformerService,An as apiKeyAuth,_e as applyClientAdapter,xn as backupConfigFile,Xr as builtinClientAdapterRegistry,Nt as calculateTokenCount,dc as clearClientAdapterCaches,Pn as clearProviderHealth,Hn as closeProxyDispatchers,yi as collectReachableModelKeys,dd as createCcrServer,sa as default,$e as detectClientType,is as diagnoseResolutionFailure,ye as extractSessionIdFromUserId,ii as getActiveProbeService,io as getAllQuotaResults,eo as getAllRateLimitInfo,Zr as getClientAdapter,vt as getConfiguredProxyUrl,U as getHealthStore,G as getProxyDispatcher,Oo as getQuotaAdapter,It as getQuotaResult,Ha as getRateLimitInfo,xt as getRuntimeDebugLog,wy as getServer,Vo as initConfig,Cn as initDir,Yn as initProviderHealthPersistence,et as initQuotaStorePersistence,qe as initRateLimitPersistence,qr as isClientType,br as isGlobalProxyEnabled,On as listAnthropicCompatibleModels,Rn as mergeUsageCapture,Ie as normalizeSessionId,We as normalizeUsagePayload,at as pluginManager,se as readConfigFile,Qo as readConfigFileRaw,mt as reconcileHealthStore,_n as resetActiveProbeService,ce as resolveProviderProxyUrl,bn as rewriteStream,Lt as router,ls as searchProjectBySession,X as sessionUsageCache,Bn as setRuntimeDebugLog,He as startActiveProbe,$o as stopActiveProbe,so as storeQuotaResult,Tn as tokenSpeedPlugin,Si as usageStore,Be as writeConfigFile};
421
+ Your response should consistently follow this rule whenever image-related analysis is requested.`});let o=e.body.messages.filter(s=>s.role==="user"&&Array.isArray(s.content)&&s.content.some(i=>i.type==="image"||Array.isArray(i.content)&&i.content.some(c=>c.type==="image"))),r=1;o.forEach(s=>{Array.isArray(s.content)&&s.content.forEach(i=>{if(i.type==="image")Dn.storeImage(`${e.id}_Image#${r}`,i.source),i.type="text",delete i.source,i.text=`[Image #${r}]This is an image, if you need to view or analyze it, you need to extract the imageId`,r++;else if(i.type==="text"&&i.text.includes("[Image #"))i.text=i.text.replace(/\[Image #\d+\]/g,"");else if(i.type==="tool_result"&&Array.isArray(i.content)&&i.content.some(c=>c.type==="image")){let c=[],l=i.content.filter(u=>u.type!=="image"?!0:(Dn.storeImage(`${e.id}_Image#${r}`,u.source),c.push(`[Image #${r}]`),r++,!1)),a=`${c.join("")}This is an image, if you need to view or analyze it, you need to extract the imageId`;i.content=l.length>0?[{type:"text",text:a},...l]:a}})})}},Yi=new hr});var yr,Zi,Fn,qi=w(()=>{"use strict";Xi();yr=class{agents=new Map;registerAgent(e){this.agents.set(e.name,e)}getAgent(e){return this.agents.get(e)}getAllAgents(){return Array.from(this.agents.values())}getAllTools(){let e=[];for(let t of this.agents.values())e.push(...t.tools.values());return e}},Zi=new yr;Zi.registerAgent(Yi);Fn=Zi});import{EventEmitter as ed}from"node:events";import{performance as ea}from"node:perf_hooks";import td from"json5";function nd(n,e){return n===429||!!(e&&/rate[\s_]limit|too many|限流|频率限制|qps_limit|token_limit|quota exhausted|usage limit|limit reached/i.test(e))}function na(n,e){(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&ke(n,e)}function _r(n){return n.usageSessionId||n.id}function Un(n){return n.usageCacheKey||`${n.clientType||"unknown"}:request:${_r(n)}`}function Qe(n){return n.body?.model?Array.isArray(n.body.model)?n.body.model.join(","):n.body.model:Array.isArray(n.model)?n.model.join(","):n.model||""}function od(n){if(!n)return;let e=n;if(Buffer.isBuffer(n)&&(e=n.toString("utf8")),typeof e=="string")try{e=JSON.parse(e)}catch{return}if(typeof e!="object")return;let t=e.usage?We(e.usage):e.response?.usage?We(e.response.usage):void 0,o=e.model||e.response?.model;if(!(!t&&!o))return{usage:t,model:o}}async function rd(n,e,t){if(await new Promise((r,s)=>{let i=c=>c?s(c):r();In(t)(n,e,i).then(()=>r(),s)}),e.sent)return;let o=new URL(`http://127.0.0.1${n.url}`);if(n.pathname=o.pathname,n.pathname.endsWith("/v1/messages")&&n.pathname!=="/v1/messages"&&(n.preset=n.pathname.replace("/v1/messages","").replace("/","")),n.pathname.endsWith("/v1/responses")&&n.pathname!=="/v1/responses"&&(n.preset=n.pathname.replace("/v1/responses","").replace("/","")),n.pathname.endsWith("/v1/messages")||n.pathname.endsWith("/v1/responses")){na(n,t);let r=Un(n);n.clientContext?.usageScope==="session"&&(n.previousUsage=X.get(r))}}async function sd(n,e,t){if(!n.pathname?.endsWith("/v1/messages"))return;let o=[];for(let r of Fn.getAllAgents())r.shouldHandle(n,t)&&(o.push(r.name),r.reqHandler(n,t),r.tools.size&&(n.body?.tools?.length||(n.body.tools=[]),n.body.tools.unshift(...Array.from(r.tools.values()).map(s=>({name:s.name,description:s.description,input_schema:s.input_schema})))));o.length&&(n.agents=o)}function oa(n){return{authClient:(e,t)=>rd(e,t,n),agent:(e,t)=>sd(e,t,n)}}function ra(n,e){n.addHook("onRequest",async t=>{let o=new URL(`http://127.0.0.1${t.url}`);t.pathname=o.pathname,(o.pathname.endsWith("/v1/messages")||o.pathname.endsWith("/v1/responses"))&&!t.requestStartTime&&(t.requestStartTime=ea.now())}),n.addHook("preHandler",async(t,o)=>{let r=t.pathname||new URL(`http://127.0.0.1${t.url}`).pathname;r.endsWith("/v1/messages")||r.endsWith("/v1/responses")||await new Promise((s,i)=>{let c=l=>l?i(l):s();In(e)(t,o,c).then(()=>s(),i)})}),n.addHook("onError",async(t,o,r)=>{t.errorMessage=r?.message||r?.toString?.()||"Unknown error",ta.emit("onError",t,o,r)}),n.addHook("onSend",(t,o,r,s)=>{(t.ccrHookOrder||=[]).push("onSend");let i=t.pathname?.endsWith("/v1/messages"),c=t.pathname?.endsWith("/v1/responses");if(i||c){na(t,e);let l=_r(t),a=Un(t);if(r instanceof ReadableStream){if(i&&t.agents){let y=new AbortController,m=r.pipeThrough(new ae),p,_=-1,k="",h="",b="",T=[],x=[];return s(null,Tn(m,async(C,S)=>{try{if(C.event==="content_block_start"&&C?.data?.content_block?.name){let v=t.agents.find(P=>Fn.getAgent(P)?.tools.get(C.data.content_block.name));if(v){p=Fn.getAgent(v),_=C.data.index,k=C.data.content_block.name,b=C.data.content_block.id;return}}if(_>-1&&C.data.index===_&&C.data?.delta?.type==="input_json_delta"){h+=C.data?.delta?.partial_json;return}if(_>-1&&C.data.index===_&&C.data?.type==="content_block_stop"){try{let v=td.parse(h);x.push({type:"tool_use",id:b,name:k,input:v});let P=await p?.tools.get(k)?.handler(v,{req:t,config:e});T.push({tool_use_id:b,type:"tool_result",content:P}),p=void 0,_=-1,k="",h="",b=""}catch(v){console.log(v)}return}if(C.event==="message_delta"&&T.length){t.body.messages.push({role:"assistant",content:x}),t.body.messages.push({role:"user",content:T});let v=await fetch(`http://127.0.0.1:${e.PORT||3456}/v1/messages`,{method:"POST",headers:{"x-api-key":e.APIKEY,"content-type":"application/json"},body:JSON.stringify(t.body)});if(!v.ok)return;let R=v.body.pipeThrough(new ae).getReader();for(;;)try{let{value:O,done:E}=await R.read();if(E)break;let A=O;if(["message_start","message_stop"].includes(A.event))continue;if(!S.desiredSize)break;S.enqueue(A)}catch(O){if(O.name==="AbortError"||O.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();break}throw O}return}return C}catch(v){if(console.error("Unexpected error in stream processing:",v),v.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();return}throw v}}).pipeThrough(new Be))}let[d,f]=r.tee(),g=async y=>{let p=y.pipeThrough(new TextDecoderStream).pipeThrough(new ae).getReader();try{for(;;){let{done:_,value:k}=await p.read();if(_)break;let h=k.event,b=k.data;if(h==="message_start"&&b?.message?.model&&(t.upstreamModel=b.message.model),h==="response.completed"&&b?.response?.model&&(t.upstreamModel=b.response.model),b?.usage){let T=X.get(a)||{},x=We(b.usage),C=wn(T,x,h==="message_start");t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_anthropic_sse",event:h,provider:t.provider,model:Qe(t),usageSessionId:l,capturedUsage:x,mergedUsage:C}),(x?.cache_read_input_tokens||x?.cache_creation_input_tokens)&&console.log("[Usage] Cache tokens:",{cache_read:x?.cache_read_input_tokens,cache_creation:x?.cache_creation_input_tokens}),X.put(a,C)}if(b?.response?.usage){let T=We(b.response.usage),x=X.get(a)||{},C=wn(x,T,!1);t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_responses_sse",event:h,provider:t.provider,model:Qe(t),usageSessionId:l,capturedUsage:T,mergedUsage:C}),X.put(a,C)}}}catch(_){_.name==="AbortError"||_.code==="ERR_STREAM_PREMATURE_CLOSE"?console.error("Background read stream closed prematurely"):console.error("Error in background stream reading:",_)}finally{p.releaseLock()}};return t.usageCapturePromise=g(f),s(null,d)}let u=od(r);if(u?.usage&&X.put(a,u.usage),u?.model&&(t.upstreamModel=u.model),typeof r=="object")return r.error?s(r.error,null):s(r,null)}if(typeof r=="object"&&r.error)return s(r.error,null);s(null,r)}),n.addHook("onSend",async(t,o,r)=>{if(ta.emit("onSend",t,o,r),o.statusCode>=400&&(t.pathname?.endsWith("/v1/messages")||t.pathname?.endsWith("/v1/responses")))try{typeof r=="string"?t.errorResponseBody=r:r&&typeof r=="object"&&(t.errorResponseBody=JSON.stringify(r))}catch{}return r}),n.addHook("onResponse",async(t,o)=>{if((t.ccrHookOrder||=[]).push("onResponse"),!(!t.pathname?.endsWith("/v1/messages")&&!t.pathname?.endsWith("/v1/responses")))try{t.usageCapturePromise&&await t.usageCapturePromise,t.tokenSpeedCapturePromise&&await t.tokenSpeedCapturePromise;let r=_r(t),s=o.statusCode>=400,i=s?void 0:X.get(Un(t)),c=cr(r),l=U(),a,u,d=t.sseError,f=d&&(d.type||d.code||d.message),g=i?.output_tokens&&i.output_tokens>0,y=s||f&&!g;if(y){a=t.errorMessage||o.errorMessage||t.error?.message||t.error?.toString?.()||void 0,f&&(a=a||JSON.stringify(d),u=JSON.stringify(d)),u=u||t.errorResponseBody;let h=Qe(t);nd(o.statusCode,a)?l.markRateLimited(t.provider||"",h,120,a):l.recordFailure(t.provider||"",h,a)}else{let h=Qe(t);l.recordSuccess(t.provider||"",h)}let m=i?.cache_read_input_tokens??0,_=i?.input_tokens||0||t.tokenCount||0;!m&&t.tokenCount&&i?.input_tokens&&i.input_tokens<t.tokenCount&&(m=t.tokenCount-i.input_tokens,m&&console.log("[Usage] Inferred cache tokens:",{tokenCount:t.tokenCount,netInput:i.input_tokens,impliedCache:m}));let k=y?0:i?.output_tokens||0;!y&&t.body?.stream&&_===0&&k===0&&t.log?.warn?.({debug_log:!0,reqId:t.id,phase:"usage_zero_stream",provider:t.provider,originalModel:t.originalModel||t.body?.model||"",model:Qe(t),usageSessionId:r,tokenCount:t.tokenCount,cachedUsage:i,note:"Successful streaming request ended with zero usage. Check provider_stream_chunk usage_monitor and usage_capture_* logs."}),_t({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:r,provider:t.provider||"",originalModel:t.originalModel||t.body?.model||"",model:Qe(t),upstreamModel:t.upstreamModel,modelFamily:t.modelFamily||"",scenarioType:t.scenarioType||"default",clientType:t.clientType||Ne(t),stream:t.body?.stream??!1,inputTokens:_,outputTokens:k,cacheReadInputTokens:m,cacheCreationInputTokens:i?.cache_creation_input_tokens||0,ttft:y||!t.body?.stream?null:c.ttft,tokensPerSecond:y?null:c.tokensPerSecond,durationMs:t.requestStartTime?Math.round(ea.now()-t.requestStartTime):0,status:y?"error":"success",errorMessage:a,responseBody:u}),t.clientContext?.usageScope!=="session"&&X.delete(Un(t))}catch(r){console.error("Usage tracking error:",r)}})}var ta,sa=w(()=>{"use strict";Le();At();re();Fo();Uo();jo();bt();Zo();qi();tr();Ft();ta=new ed});var ia={};Hn(ia,{createCcrServer:()=>fd});import{homedir as id}from"os";import{join as ad}from"path";import{createStream as cd}from"rotating-file-stream";import{CONFIG_FILE as ld,HOME_DIR as ud,listPresets as dd,ensureCcrSelfProvider as pd}from"@wengine-ai/claude-code-router-shared";async function fd(n={}){await Ei(),await xn();let e=await Xo();pd(e).changed&&(await fe(e),console.log('\u2139\uFE0F Added the preset "ccr" provider pointing at this server (delete it in the UI to remove it for good).'));let o=e.Providers||e.providers||[],r=o&&o.length>0,s=e.HOST||"127.0.0.1";r?(s=e.HOST,e.APIKEY||(s="127.0.0.1")):(s="0.0.0.0",console.log("\u2139\uFE0F No providers configured. Listening on 0.0.0.0 without authentication."));let i=n.port??e.PORT??3456,c=process.env.SERVICE_PORT?parseInt(process.env.SERVICE_PORT):i,l=p=>(p>9?"":"0")+p,a=(p,_)=>{let k;p?typeof p=="number"?k=new Date(p):k=p:k=new Date;let h=k.getFullYear()+""+l(k.getMonth()+1),b=l(k.getDate()),T=l(k.getHours()),x=l(k.getMinutes());return`./logs/ccr-${h}${b}${T}${x}${l(k.getSeconds())}${_?`_${_}`:""}.log`},u;n.logger!==void 0?u=n.logger:($i(),e.LOG!==!1?(e.LOG===void 0&&(e.LOG=!0),u={level:e.LOG_LEVEL||"error",stream:cd(a,{path:ud,interval:"1d",size:"50M",compress:!1})}):u=!1);let d=await dd(),f=new ca({jsonPath:ld,initialConfig:{providers:e.Providers||e.providers,HOST:s,PORT:c,LOG_FILE:ad(id(),".claude-code-router","claude-code-router.log")},logger:u});await f.ready(),await Ki(f,e),f.recordUsage=p=>{try{let _=p.req?p.req.clientType||Ne(p.req):"unknown";_t({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:p.sessionId||"",provider:p.provider||"",originalModel:p.originalModel||"",model:p.model||"",modelFamily:p.modelFamily||"",scenarioType:p.scenarioType||"default",clientType:_,stream:p.stream??!1,inputTokens:p.inputTokens||0,outputTokens:0,cacheReadInputTokens:p.cacheReadInputTokens||0,cacheCreationInputTokens:p.cacheCreationInputTokens||0,ttft:null,tokensPerSecond:null,durationMs:0,status:"error",errorMessage:p.errorMessage,responseBody:void 0})}catch(_){console.error("Fallback usage tracking error:",_)}},ra(f,e),f.ccrPreHandlerCallbacks=oa(e),(await Promise.allSettled(d.map(async p=>await f.registerNamespace(`/preset/${p.name}`,p.config)))).forEach((p,_)=>{if(p.status==="rejected"){let k=d[_],h=p.reason instanceof Error?p.reason.message:String(p.reason);f.app?.log?.error?.(`Failed to register preset "${k?.name}": ${h}`)}}),await Li(f,e),mt(e,f.app?.log);let y=p=>{f.app.log.error({err:p},"Uncaught exception")},m=(p,_)=>{f.app.log.error({reason:p,promise:_},"Unhandled rejection")};return process.on("uncaughtException",y),process.on("unhandledRejection",m),f.app.addHook("onClose",async()=>{process.off("uncaughtException",y),process.off("unhandledRejection",m)}),f}var aa=w(()=>{"use strict";la();Oi();Rn();Ni();Di();Qi();sa();On();bt();Le()});import md from"fastify";import gd from"@fastify/cors";function hd(n={}){let e=md({bodyLimit:52428800,...n});return e.setErrorHandler(Sr),e.register(gd),e}async function yd(n={}){return(await Promise.resolve().then(()=>(aa(),ia))).createCcrServer(n)}var kr,ca,Dy,la=w(()=>{Bn();Ct();Ft();Ut();_o();bo();Fe();At();bn();re();we();Ie();oe();Le();Ft();$e();Le();Fe();Bn();Ut();_o();bo();Qo();Ho();re();we();$o();Ie();re();bn();Ye();oe();Rn();Zo();On();er();tr();bt();kr=class{app;configService;providerService;transformerService;tokenizerService;activeProbeService;recordUsage;readiness;mainNamespaceRegistered=!1;signalHandlersRegistered=!1;ccrPreHandlerCallbacks;constructor(e={}){let{initialConfig:t,jsonPath:o,envPath:r,useEnvFile:s,useJsonFile:i,useEnvironmentVariables:c,...l}=e;this.app=hd({...l,logger:l.logger??!0}),this.app._server=this;let a={initialConfig:t};o!==void 0&&(a.jsonPath=o),r!==void 0&&(a.envPath=r),s!==void 0&&(a.useEnvFile=s),i!==void 0&&(a.useJsonFile=i),c!==void 0&&(a.useEnvironmentVariables=c),this.configService=new Ce(a),this.transformerService=new je(this.configService,this.app.log),this.tokenizerService=new He(this.configService,this.app.log),this.readiness=(async()=>{await Promise.all([this.transformerService.initialize(),this.tokenizerService.initialize()]),this.providerService=new ue(this.configService,this.transformerService,this.app.log)})()}async ready(){await this.readiness}async register(e,t){await this.app.register(e,t)}addHook(e,t){this.app.addHook(e,t)}async registerNamespace(e,t){if(!e)throw new Error("name is required");if(await this.ready(),e==="/"&&this.mainNamespaceRegistered)return;let o=this.configService,r=this.transformerService,s=this.providerService,i=this.tokenizerService;if(e!=="/"){if(!t)throw new Error("options is required");o=new Ce({initialConfig:{...t,providers:t.Providers||t.providers}}),r=new je(o,this.app.log),i=new He(o,this.app.log),await Promise.all([r.initialize(),i.initialize()]),s=new ue(o,r,this.app.log)}let c=async l=>{l.decorate("configService",o),l.decorate("transformerService",r),l.decorate("providerService",s),l.decorate("tokenizerService",i),l.decorate("recordUsage",this.recordUsage||(()=>{})),l.addHook("preHandler",async(a,u)=>{let d=new URL(`http://127.0.0.1${a.url}`);a.pathname=d.pathname;let f=d.pathname.endsWith("/v1/messages"),g=d.pathname.endsWith("/v1/responses");if(!f&&!g||!a.body||typeof a.body!="object")return;let y=a.ccrHookOrder||=[];if(y.push("request-normalize"),g&&a.body.input&&!a.body.messages&&rt(a.body),a.body.stream===void 0&&(a.body.stream=!1),a.log.info({data:a.body,type:"request body"}),y.push("adapter"),!a.originalModel&&a.body.model&&(a.originalModel=a.body.model),ke(a,o.getAll()),y.push("auth-client"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.authClient(a,u),u.sent))&&(y.push("agent"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.agent(a,u),u.sent)))){y.push("router"),a.body?.model&&await Lt(a,u,{configService:o,tokenizerService:i}),a.usageCacheKey&&X.put(a.usageCacheKey,{input_tokens:0,output_tokens:0,cache_read_input_tokens:0,cache_creation_input_tokens:0}),y.push("provider-model-normalize");try{let m=a.body;if(!m||!m.model)return u.code(400).send({error:"Missing model in request body"});let p=m.model.split(","),_=m.model;if(p.length>1){let[k,...h]=p;_=h.join(","),m.model=_,a.provider=k}a.model=_}catch(m){return a.log.error({error:m},"Error in modelProviderMiddleware:"),u.code(500).send({error:"Internal server error"})}}}),await _s(l)};e==="/"?(await this.app.register(c),this.mainNamespaceRegistered=!0):await this.app.register(c,{prefix:e})}async start(){try{if(await this.ready(),await this.registerNamespace("/"),!this.signalHandlersRegistered){this.signalHandlersRegistered=!0;let t=async s=>{this.app.log.info(`Received ${s}, shutting down gracefully...`);try{Lo()}catch{}await this.app.close(),process.exit(0)},o=()=>void t("SIGINT"),r=()=>void t("SIGTERM");process.on("SIGINT",o),process.on("SIGTERM",r),this.app.addHook("onClose",async()=>{process.off("SIGINT",o),process.off("SIGTERM",r),this.signalHandlersRegistered=!1;try{this.tokenizerService.dispose()}catch{}await zn()})}await this.app.ready();let e=await this.app.listen({port:parseInt(this.configService.get("PORT")||"3000",10),host:this.configService.get("HOST")||"127.0.0.1"});this.app.log.info(`\u{1F680} LLMs API server listening on ${e}`);try{Ze()}catch{}try{et()}catch{}try{Xn()}catch{}try{let t={enabled:this.configService.get("ACTIVE_PROBE_ENABLED")??!0,quotaProbeIntervalMinutes:this.configService.get("QUOTA_PROBE_INTERVAL_MINUTES")??10,probeTimeoutMs:this.configService.get("PROBE_TIMEOUT_MS")??15e3,slowThresholdMs:Number(this.configService.get("PROBE_SLOW_THRESHOLD_MS"))||3e3,initialDelayMs:this.configService.get("PROBE_INITIAL_DELAY_MS")??5e3,excludeProviders:this.configService.get("EXCLUDE_PROBE_PROVIDERS")??[]};this.activeProbeService=ze(()=>this.providerService.getProviders(),t,o=>ce(this.configService,o),this.app.log,o=>this.configService.get(o))}catch(t){this.app.log.warn?.(`Failed to start active probe service: ${t.message}`)}}catch(e){this.app.log.error(`Error starting server: ${e}`),process.exit(1)}}},ca=kr;Dy=yd});la();export{_n as ActiveProbeService,Ce as ConfigService,q as ProjectRoutingError,Ot as ProviderHealthStore,ue as ProviderService,ae as SSEParserTransform,Be as SSESerializerTransform,He as TokenizerService,je as TransformerService,In as apiKeyAuth,ke as applyClientAdapter,Sn as backupConfigFile,qr as builtinClientAdapterRegistry,Nt as calculateTokenCount,mc as clearClientAdapterCaches,En as clearProviderHealth,zn as closeProxyDispatchers,bi as collectReachableModelKeys,yd as createCcrServer,ca as default,Ne as detectClientType,cs as diagnoseResolutionFailure,_e as extractSessionIdFromUserId,li as getActiveProbeService,ao as getAllQuotaResults,to as getAllRateLimitInfo,ts as getClientAdapter,vt as getConfiguredProxyUrl,U as getHealthStore,G as getProxyDispatcher,Io as getQuotaAdapter,It as getQuotaResult,Ja as getRateLimitInfo,xt as getRuntimeDebugLog,Dy as getServer,Xo as initConfig,xn as initDir,Xn as initProviderHealthPersistence,et as initQuotaStorePersistence,Ze as initRateLimitPersistence,es as isClientType,vr as isGlobalProxyEnabled,An as listAnthropicCompatibleModels,wn as mergeUsageCapture,Me as normalizeSessionId,We as normalizeUsagePayload,at as pluginManager,ne as readConfigFile,Yo as readConfigFileRaw,mt as reconcileHealthStore,kn as resetActiveProbeService,ce as resolveProviderProxyUrl,Tn as rewriteStream,Lt as router,ds as searchProjectBySession,X as sessionUsageCache,Wn as setRuntimeDebugLog,ze as startActiveProbe,Lo as stopActiveProbe,io as storeQuotaResult,vn as tokenSpeedPlugin,Pi as usageStore,fe as writeConfigFile};
422
422
  //# sourceMappingURL=server.mjs.map