@wengine-ai/llms 2.3.2407 → 2.3.2409
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/cjs/server.cjs +88 -88
- package/dist/cjs/server.cjs.map +4 -4
- package/dist/esm/server.mjs +95 -95
- package/dist/esm/server.mjs.map +4 -4
- package/dist/index.html +11 -11
- package/dist/server.d.ts +2 -1
- package/dist/utils/concurrency-router.d.ts +91 -0
- package/package.json +15 -3
package/dist/cjs/server.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var mc=Object.create;var tn=Object.defineProperty;var gc=Object.getOwnPropertyDescriptor;var hc=Object.getOwnPropertyNames;var yc=Object.getPrototypeOf,_c=Object.prototype.hasOwnProperty;var w=(n,e)=>()=>(n&&(e=n(n=0)),e);var nn=(n,e)=>{for(var t in e)tn(n,t,{get:e[t],enumerable:!0})},rs=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of hc(e))!_c.call(n,r)&&r!==t&&tn(n,r,{get:()=>e[r],enumerable:!(o=gc(e,r))||o.enumerable});return n};var V=(n,e,t)=>(t=n!=null?mc(yc(n)):{},rs(e||!n||!n.__esModule?tn(t,"default",{value:n,enumerable:!0}):t,n)),ss=n=>rs(tn({},"__esModule",{value:!0}),n);function bc(n){if(n==null)return{ok:!0};let e=String(n).trim();if(!e)return{ok:!0};if(kc.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 cs(n){let e=[];for(let t of as){let o=n[t];if(o==null)continue;let r=bc(o);r.ok||e.push({key:t,error:r.error})}return e}function mt(n){for(let e of as){let t=n.get(e);if(t==null)continue;let o=String(t).trim();if(o)return o}}function Co(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 fe(n,e){let t=mt(n);if(t)return Co(n.get("PROXY_GLOBAL_ENABLED"))||e?.proxyEnabled===!0||e?.proxy_enabled===!0?t:void 0}function Y(n){let e=Tc(n),t=on.get(e);if(t)return t;let o=new is.ProxyAgent(e);return on.set(e,o),o}async function rn(){let n=Array.from(on.values());on.clear(),await Promise.allSettled(n.map(e=>e.close()))}function Tc(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 is,as,kc,on,ce=w(()=>{"use strict";is=require("undici"),as=["HTTPS_PROXY","https_proxy","httpsProxy","PROXY_URL"],kc=/\$\{[^}]+\}|\$[A-Z_][A-Z0-9_]*/;on=new Map});var gt,So,ls,us,Oe,Ro=w(()=>{"use strict";gt=require("fs"),So=require("path"),ls=require("dotenv"),us=V(require("json5"),1);ce();Oe=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:(0,So.join)(process.cwd(),this.options.jsonPath);if((0,gt.existsSync)(e))try{let t=(0,gt.readFileSync)(e,"utf-8"),o=us.default.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:(0,So.join)(process.cwd(),this.options.envPath);if((0,gt.existsSync)(e))try{let t=(0,ls.config)({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 mt(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 K(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 ds(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 sn=w(()=>{"use strict"});function an(n){wo=n}function ht(){return wo}function cn(n){return wo||n.get("DEBUG_LOG")===!0}function ms(n){let e=n.get("DEBUG_LOG_OPTIONS");return!e||typeof e!="object"?ze:{maxBodyLength:e.maxBodyLength??ze.maxBodyLength,maxStreamChunks:e.maxStreamChunks??ze.maxStreamChunks}}function ln(n){let e={};for(let[t,o]of Object.entries(n))vc.has(t.toLowerCase())?e[t]="***MASKED***":e[t]=o;return e}function un(n,e){return n.length<=e?n:n.slice(0,e)+`...[truncated, total ${n.length} bytes]`}function gs(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:ln(t.headers),body:un(o,ze.maxBodyLength)})}function hs(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_response",status:t.status,headers:ln(t.headers),...t.body!==void 0?{body:un(t.body,ze.maxBodyLength)}:{}})}function ps(n,e,t,o){let r=xc(o);n.info({debug_log:!0,reqId:e,phase:"provider_stream_chunk",chunkIndex:t,...r?{usage_monitor:r}:{},data:un(o,ze.maxBodyLength)})}function xc(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:un(o,512)})}}if(e.length)return e.length===1?e[0]:e}function fs(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_stream_end",totalChunks:t})}function ys(n,e,t,o){(async()=>{let r=n.pipeThrough(new TextDecoderStream).getReader(),s=0;try{let i="";for(;;){let{done:a,value:l}=await r.read();if(a)break;i+=l;let c=i.split(`
|
|
2
2
|
|
|
3
|
-
`);i=c.pop()||"";for(let u of c)u.trim()&&(s<o.maxStreamChunks?rs(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&&rs(e,t,s,i.trim()),s++),ss(e,t,s)}catch(i){console.error(`[debug_log] Error reading debug stream for reqId=${t}:`,i),ss(e,t,s)}finally{r.releaseLock()}})()}var cc,ze,So,yt=w(()=>{"use strict";cc=new Set(["authorization","x-api-key","x-goog-api-key","cookie","set-cookie"]),ze={maxBodyLength:4096,maxStreamChunks:100},So=!1});function us(n,e,t,o,r){let s=new Headers({"Content-Type":"application/json"});t.headers&&Object.entries(t.headers).forEach(([c,u])=>{u&&s.set(c,u)});let i,a=AbortSignal.timeout(t.TIMEOUT??60*1e3*60);if(t.signal){let c=new AbortController,u=()=>c.abort();t.signal.addEventListener("abort",u),a.addEventListener("abort",u),i=c.signal}else i=a;let l={method:"POST",headers:s,body:JSON.stringify(e),signal:i};return t.httpsProxy&&(l.dispatcher=Y(t.httpsProxy)),r?.debug({reqId:o.req.id,method:"POST",headers:ln(Object.fromEntries(s.entries())),requestUrl:typeof n=="string"?n:n.toString(),useProxy:uc(t.httpsProxy)},"final request"),fetch(typeof n=="string"?n:n.toString(),l)}function uc(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 ds=w(()=>{ce();yt()});var dn,Ro=w(()=>{dn="2.3.2407"});var ps={};nn(ps,{getFallbackPromotionStore:()=>Be,resetFallbackPromotionStore:()=>fc});function Be(n){return Ae||(Ae=new wo,n&&Ae.configure(n)),Ae}function fc(){Ae&&(Ae.clearAll(),Ae=null)}var pc,wo,Ae,pn=w(()=>{"use strict";le();pc=600*1e3,wo=class{promotions=new Map;enabled=!0;ttlMs=pc;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 a=this.getKey(e,t,o),l=`${r},${s}`;this.promotions.set(a,{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,...c]=i.promotedModel.split(","),u=c.join(",");if(!j().isAvailable(l,u))return this.promotions.delete(s),null;let p=r.find(y=>y.name.toLowerCase()===l.toLowerCase()),g=p?.models?.find(y=>String(y).toLowerCase()===u.toLowerCase());return!p||!g||p.enabled===!1?(this.promotions.delete(s),null):`${p.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}},Ae=null});function yc(n){try{if(!(0,he.existsSync)(Oo))return;let e=JSON.parse((0,he.readFileSync)(Oo,"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 ms(n){try{let e=n.getAllStates();if(e.length===0)return;(0,he.existsSync)(Eo)||(0,he.mkdirSync)(Eo,{recursive:!0});let t=e.map(o=>({...o,updatedAt:Date.now()}));(0,he.writeFileSync)(Oo,JSON.stringify(t,null,2),"utf-8")}catch{}}function fn(){if(fs)return;fs=!0;let n=j();yc(n),hc=setInterval(()=>ms(n),6e4),process.on("exit",()=>ms(n))}function j(n){return Po||(Po=new _t(n)),Po}var he,Ao,gs,mc,gc,_t,Eo,Oo,fs,hc,Po,le=w(()=>{"use strict";he=require("fs"),Ao=require("path"),gs=require("@wengine-ai/claude-code-router-shared"),mc=120,gc={enabled:!0,failureThreshold:3,probeIntervalMinutes:5,halfOpenSuccessThreshold:2},_t=class{states=new Map;config;probeTimer;constructor(e){this.config={...gc,...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}=(pn(),Xr(ps));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 a=(o??mc)*1e3;i.rateLimitUntil=Date.now()+a}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)}},Eo=(0,Ao.join)(gs.HOME_DIR,"runtime"),Oo=(0,Ao.join)(Eo,"provider-health.json"),fs=!1,hc=null;Po=null});function kt(n,e,t){let o=a=>{if(t instanceof Headers)return t.get(a);let l=Object.keys(t).find(c=>c.toLowerCase()===a);return l?t[l]:null},r=Io(o,_c),s=Io(o,kc),i=Io(o,bc);(r||s||i)&&Je.set(n,{provider:n,remaining:_s(r),limit:_s(s),reset:Tc(i),capturedAt:Date.now()})}function Ts(n){return Je.get(n)}function mn(){return Array.from(Je.values())}function Io(n,e){for(let t of e){let o=n(t);if(o)return o}return null}function _s(n){if(!n)return null;let e=Number(n.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function Tc(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=vc(e);return r===null?null:Math.floor((Date.now()+r)/1e3)}function vc(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 xc(){try{if(!(0,ye.existsSync)($o))return;let n=JSON.parse((0,ye.readFileSync)($o,"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;Je.set(t.provider,t)}}catch{}}function ks(){try{if(Je.size===0)return;(0,ye.existsSync)(Mo)||(0,ye.mkdirSync)(Mo,{recursive:!0}),(0,ye.writeFileSync)($o,JSON.stringify(Array.from(Je.values()),null,2),"utf-8")}catch{}}function We(){hs||(hs=!0,xc(),ys=setInterval(ks,6e4),ys.unref?.(),process.on("exit",ks))}var ye,No,bs,Mo,$o,Je,hs,ys,_c,kc,bc,Ge=w(()=>{"use strict";ye=require("fs"),No=require("path"),bs=require("@wengine-ai/claude-code-router-shared"),Mo=(0,No.join)(bs.HOME_DIR,"runtime"),$o=(0,No.join)(Mo,"rate-limit.json"),Je=new Map,hs=!1,ys=null,_c=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],kc=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],bc=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"]});async function U(n){let e=await n.text();return Cc(e,n.status)}function Cc(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 vs(n){return(n.headers.get("Content-Type")||"").includes("text/event-stream")}function bt(n){return/^\s*(event:|data:|:\s*[A-Z])/m.test(n)||n.startsWith("data:")}async function Ke(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:a,done:l}=await o.read();if(l||(a&&(s+=r.decode(a,{stream:!0})),bt(s))||/^\s*[\[{]/.test(s))break}s+=r.decode()}finally{o.releaseLock()}return e.cancel().catch(()=>{}),{body:t,isSSE:bt(s)}}async function Qe(n){try{let e=await n.text();if(bt(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 Ve,Lo=w(()=>{"use strict";q();Ve=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 Ke(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 Qe(e);if(r.isSSE)return this.transformResponseOut(r.response);e=r.response}}if(t.includes("application/json")){let o=await U(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:f,done:_}=await s.read();!_&&f&&(i=new TextDecoder().decode(f))}finally{s.releaseLock()}if(o.cancel().catch(()=>{}),i.includes("message_start")||i.includes("content_block_start")){let f=this.prependToStream(i,r);return new Response(f,{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 f=this.prependToStream(i,r);return new Response(f,{status:e.status,statusText:e.statusText,headers:e.headers})}let c=this.prependToStream(i,r),u=new TextDecoder,d=new TextEncoder,p="",g=!1,y=this,m=new ReadableStream({async start(f){let _=c.getReader(),k=-1,h="",b=T=>(T!==h&&(k++,h=T),k);try{for(;;){let{done:T,value:C}=await _.read();if(T){g||f.enqueue(d.encode(`data: [DONE]
|
|
3
|
+
`);i=c.pop()||"";for(let u of c)u.trim()&&(s<o.maxStreamChunks?ps(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&&ps(e,t,s,i.trim()),s++),fs(e,t,s)}catch(i){console.error(`[debug_log] Error reading debug stream for reqId=${t}:`,i),fs(e,t,s)}finally{r.releaseLock()}})()}var vc,ze,wo,yt=w(()=>{"use strict";vc=new Set(["authorization","x-api-key","x-goog-api-key","cookie","set-cookie"]),ze={maxBodyLength:4096,maxStreamChunks:100},wo=!1});function _s(n,e,t,o,r){let s=new Headers({"Content-Type":"application/json"});t.headers&&Object.entries(t.headers).forEach(([c,u])=>{u&&s.set(c,u)});let i,a=AbortSignal.timeout(t.TIMEOUT??60*1e3*60);if(t.signal){let c=new AbortController,u=()=>c.abort();t.signal.addEventListener("abort",u),a.addEventListener("abort",u),i=c.signal}else i=a;let l={method:"POST",headers:s,body:JSON.stringify(e),signal:i};return t.httpsProxy&&(l.dispatcher=Y(t.httpsProxy)),r?.debug({reqId:o.req.id,method:"POST",headers:ln(Object.fromEntries(s.entries())),requestUrl:typeof n=="string"?n:n.toString(),useProxy:Cc(t.httpsProxy)},"final request"),fetch(typeof n=="string"?n:n.toString(),l)}function Cc(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 ks=w(()=>{ce();yt()});var dn,Eo=w(()=>{dn="2.3.2409"});var bs={};nn(bs,{getFallbackPromotionStore:()=>Be,resetFallbackPromotionStore:()=>wc});function Be(n){return Ae||(Ae=new Po,n&&Ae.configure(n)),Ae}function wc(){Ae&&(Ae.clearAll(),Ae=null)}var Rc,Po,Ae,pn=w(()=>{"use strict";le();Rc=600*1e3,Po=class{promotions=new Map;enabled=!0;ttlMs=Rc;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 a=this.getKey(e,t,o),l=`${r},${s}`;this.promotions.set(a,{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,...c]=i.promotedModel.split(","),u=c.join(",");if(!j().isAvailable(l,u))return this.promotions.delete(s),null;let p=r.find(y=>y.name.toLowerCase()===l.toLowerCase()),g=p?.models?.find(y=>String(y).toLowerCase()===u.toLowerCase());return!p||!g||p.enabled===!1?(this.promotions.delete(s),null):`${p.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}},Ae=null});function Ac(n){try{if(!(0,he.existsSync)(Io))return;let e=JSON.parse((0,he.readFileSync)(Io,"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 vs(n){try{let e=n.getAllStates();if(e.length===0)return;(0,he.existsSync)(Ao)||(0,he.mkdirSync)(Ao,{recursive:!0});let t=e.map(o=>({...o,updatedAt:Date.now()}));(0,he.writeFileSync)(Io,JSON.stringify(t,null,2),"utf-8")}catch{}}function fn(){if(Ts)return;Ts=!0;let n=j();Ac(n),Oc=setInterval(()=>vs(n),6e4),process.on("exit",()=>vs(n))}function j(n){return Oo||(Oo=new _t(n)),Oo}var he,Mo,xs,Ec,Pc,_t,Ao,Io,Ts,Oc,Oo,le=w(()=>{"use strict";he=require("fs"),Mo=require("path"),xs=require("@wengine-ai/claude-code-router-shared"),Ec=120,Pc={enabled:!0,failureThreshold:3,probeIntervalMinutes:5,halfOpenSuccessThreshold:2},_t=class{states=new Map;config;probeTimer;constructor(e){this.config={...Pc,...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}=(pn(),ss(bs));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 a=(o??Ec)*1e3;i.rateLimitUntil=Date.now()+a}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)}},Ao=(0,Mo.join)(xs.HOME_DIR,"runtime"),Io=(0,Mo.join)(Ao,"provider-health.json"),Ts=!1,Oc=null;Oo=null});function kt(n,e,t){let o=a=>{if(t instanceof Headers)return t.get(a);let l=Object.keys(t).find(c=>c.toLowerCase()===a);return l?t[l]:null},r=$o(o,Ic),s=$o(o,Mc),i=$o(o,$c);(r||s||i)&&Je.set(n,{provider:n,remaining:Rs(r),limit:Rs(s),reset:Nc(i),capturedAt:Date.now()})}function Ps(n){return Je.get(n)}function mn(){return Array.from(Je.values())}function $o(n,e){for(let t of e){let o=n(t);if(o)return o}return null}function Rs(n){if(!n)return null;let e=Number(n.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function Nc(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=Lc(e);return r===null?null:Math.floor((Date.now()+r)/1e3)}function Lc(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 Dc(){try{if(!(0,ye.existsSync)(Lo))return;let n=JSON.parse((0,ye.readFileSync)(Lo,"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;Je.set(t.provider,t)}}catch{}}function ws(){try{if(Je.size===0)return;(0,ye.existsSync)(No)||(0,ye.mkdirSync)(No,{recursive:!0}),(0,ye.writeFileSync)(Lo,JSON.stringify(Array.from(Je.values()),null,2),"utf-8")}catch{}}function We(){Cs||(Cs=!0,Dc(),Ss=setInterval(ws,6e4),Ss.unref?.(),process.on("exit",ws))}var ye,Do,Es,No,Lo,Je,Cs,Ss,Ic,Mc,$c,Ge=w(()=>{"use strict";ye=require("fs"),Do=require("path"),Es=require("@wengine-ai/claude-code-router-shared"),No=(0,Do.join)(Es.HOME_DIR,"runtime"),Lo=(0,Do.join)(No,"rate-limit.json"),Je=new Map,Cs=!1,Ss=null,Ic=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],Mc=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],$c=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"]});function hn(){return Fo||(Fo=new jo),Fo}function zo(){return Uo||(Uo=new Ho),Uo}function Fc(n){return n?.sessionId||n?.clientContext?.stableSessionId||void 0}function Os(n){let e=Fc(n);if(!e)return"";let t=typeof n?.modelFamily=="string"?n.modelFamily:"",o=typeof n?.scenarioType=="string"&&n.scenarioType?n.scenarioType:"default";return`${e}:${t}:${o}`}function As(n,e){let t=hn(),o=n?.[gn];if(o&&!o.released){if(o.key===e)return;t.release(o)}n[gn]=t.acquire(e)}function Bo(n){let e=n?.[gn];e&&hn().release(e)}function Is(n,e){let t=Os(n),o=n?.[gn],r=t?zo().get(t)!==void 0:!1;!o&&!r||(t&&zo().bind(t,e),As(n,e))}function Uc(n,e,t){let o=hn(),r=null,s=1/0;for(let i of e){if(!i||typeof i!="string")continue;let a=t(i);if(!a||a===n)continue;let l=o.count(a);l<s&&(r=a,s=l)}return r}function Ms(n,e,t){let o=Os(n),r=zo(),s=hn(),i=Number.isFinite(t.threshold)&&t.threshold>0?Math.floor(t.threshold):3,a;if(o){let l=r.get(o);if(l&&l!==e){let c=t.resolve(l);c?(a=c,t.log?.(`Concurrency priority: session slot ${o} sticking to bound model ${c}`)):r.unbind(o)}else l&&(a=e)}if(!a){let l=s.count(e);if(l>=i){let c=Uc(e,t.fallbackModels,t.resolve);c&&(a=c,t.log?.(`Concurrency priority: model ${e} has ${l} in-flight request(s) (>= ${i}); assigning ${o?`session slot ${o} `:"request "}to overflow model ${c}`))}a=a||e,o&&r.bind(o,a)}return As(n,a),a}var jo,Ho,Fo,Uo,gn,yn=w(()=>{"use strict";jo=class{leases=new Map;nextId=1;acquire(e){let t={id:this.nextId++,key:e,createdAt:Date.now(),released:!1};return this.leases.set(t.id,t),t}release(e){!e||e.released||(e.released=!0,this.leases.delete(e.id))}prune(){let e=Date.now();for(let[t,o]of this.leases)e-o.createdAt>18e5&&this.leases.delete(t)}count(e){this.prune();let t=0;for(let o of this.leases.values())o.key===e&&t++;return t}clear(){this.leases.clear()}},Ho=class{bindings=new Map;get(e){let t=this.bindings.get(e);if(t){if(Date.now()-t.updatedAt>864e5){this.bindings.delete(e);return}return t.updatedAt=Date.now(),this.bindings.delete(e),this.bindings.set(e,t),t.modelKey}}bind(e,t){for(this.bindings.delete(e),this.bindings.set(e,{modelKey:t,updatedAt:Date.now()});this.bindings.size>2e3;){let o=this.bindings.keys().next().value;if(o===void 0)break;this.bindings.delete(o)}}unbind(e){this.bindings.delete(e)}clear(){this.bindings.clear()}},Fo=null,Uo=null;gn="__concurrencyLease"});async function U(n){let e=await n.text();return jc(e,n.status)}function jc(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 $s(n){return(n.headers.get("Content-Type")||"").includes("text/event-stream")}function bt(n){return/^\s*(event:|data:|:\s*[A-Z])/m.test(n)||n.startsWith("data:")}async function Ke(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:a,done:l}=await o.read();if(l||(a&&(s+=r.decode(a,{stream:!0})),bt(s))||/^\s*[\[{]/.test(s))break}s+=r.decode()}finally{o.releaseLock()}return e.cancel().catch(()=>{}),{body:t,isSSE:bt(s)}}async function Qe(n){try{let e=await n.text();if(bt(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 Ve,Jo=w(()=>{"use strict";q();Ve=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 Ke(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 Qe(e);if(r.isSSE)return this.transformResponseOut(r.response);e=r.response}}if(t.includes("application/json")){let o=await U(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:f,done:_}=await s.read();!_&&f&&(i=new TextDecoder().decode(f))}finally{s.releaseLock()}if(o.cancel().catch(()=>{}),i.includes("message_start")||i.includes("content_block_start")){let f=this.prependToStream(i,r);return new Response(f,{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 f=this.prependToStream(i,r);return new Response(f,{status:e.status,statusText:e.statusText,headers:e.headers})}let c=this.prependToStream(i,r),u=new TextDecoder,d=new TextEncoder,p="",g=!1,y=this,m=new ReadableStream({async start(f){let _=c.getReader(),k=-1,h="",b=v=>(v!==h&&(k++,h=v),k);try{for(;;){let{done:v,value:C}=await _.read();if(v){g||f.enqueue(d.encode(`data: [DONE]
|
|
4
4
|
|
|
5
|
-
`));break}let x=u.decode(C,{stream:!0});p+=x;let S=p.split(/\r?\n/);p=S.pop()||"";for(let
|
|
5
|
+
`));break}let x=u.decode(C,{stream:!0});p+=x;let S=p.split(/\r?\n/);p=S.pop()||"";for(let T of S)if(T.trim())try{if(T.startsWith("event: "))continue;if(T.startsWith("data: ")){let E=T.slice(5).trim();if(E==="[DONE]"){g=!0,f.enqueue(d.encode(`data: [DONE]
|
|
6
6
|
|
|
7
7
|
`));continue}try{let R=JSON.parse(E);if(R.type==="response.output_text.delta"){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:{content:R.delta||""},finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(A)}
|
|
8
8
|
|
|
@@ -20,32 +20,32 @@
|
|
|
20
20
|
|
|
21
21
|
`))}else if(R.type==="response.reasoning_summary_part.done"&&R.part){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:k,delta:{thinking:{signature:R.item_id}},finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(A)}
|
|
22
22
|
|
|
23
|
-
`))}}catch{f.enqueue(d.encode(
|
|
24
|
-
`))}}else f.enqueue(d.encode(
|
|
25
|
-
`))}catch(E){console.error("Error processing line:",
|
|
23
|
+
`))}}catch{f.enqueue(d.encode(T+`
|
|
24
|
+
`))}}else f.enqueue(d.encode(T+`
|
|
25
|
+
`))}catch(E){console.error("Error processing line:",T,E),f.enqueue(d.encode(T+`
|
|
26
26
|
`))}}p.trim()&&f.enqueue(d.encode(p+`
|
|
27
27
|
`)),g||f.enqueue(d.encode(`data: [DONE]
|
|
28
28
|
|
|
29
|
-
`))}catch(
|
|
29
|
+
`))}catch(v){console.error("Stream error:",v),f.error(v)}finally{try{_.releaseLock()}catch(v){console.error("Error releasing reader lock:",v)}f.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(),a="";try{let{value:p,done:g}=await i.read();!g&&p&&(a=new TextDecoder().decode(p))}finally{i.releaseLock()}r.cancel().catch(()=>{});let l=a.includes("message_start")||a.includes("content_block_start"),c=a.includes("chat.completion.chunk");if(a.includes("response.created")||a.includes("response.output_text.delta")||a.includes("response.in_progress")||a.includes("response.output_item.added"))return new Response(s,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}});if(c&&!l){let p=this.convertOpenAIChatStreamToResponsesAPI(s,t);return new Response(p,{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 U(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(a){i||(i=!0,a.enqueue(r));try{let{done:l,value:c}=await s.read();l?a.close():a.enqueue(c)}catch(l){a.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()}`,a=0,l=!1,c=!1,u="",d=-1,p=[],g=(m,f)=>r.encode(`event: ${m}
|
|
30
30
|
data: ${JSON.stringify(f)}
|
|
31
31
|
|
|
32
|
-
`),y=(m,f=[])=>({id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:m,model:s,output:f,usage:this.buildResponsesUsage()});return new ReadableStream({async start(m){try{let f=e.getReader(),_="",k=!1,h=b=>{if(!k){k=!0;for(let
|
|
33
|
-
`);_=C.pop()||"";for(let x of C){let S=x.trim();if(!S||S.startsWith("event:")||!S.startsWith("data:"))continue;let
|
|
32
|
+
`),y=(m,f=[])=>({id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:m,model:s,output:f,usage:this.buildResponsesUsage()});return new ReadableStream({async start(m){try{let f=e.getReader(),_="",k=!1,h=b=>{if(!k){k=!0;for(let v of p)if(v.type==="function_call"){let C=p.indexOf(v);m.enqueue(g("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:v.call_id||v.id,output_index:C,arguments:v.arguments||""})),v.status="completed",m.enqueue(g("response.output_item.done",{type:"response.output_item.done",output_index:C,item:v}))}for(let v of p)if(v.type==="message"){let C=p.indexOf(v);v.status="completed",m.enqueue(g("response.output_item.done",{type:"response.output_item.done",output_index:C,item:v}))}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:p.slice(),usage:b?.usage?{...this.buildResponsesUsage(b.usage)}:this.buildResponsesUsage()}}))}};for(;;){let{done:b,value:v}=await f.read();if(b)break;_+=o.decode(v,{stream:!0});let C=_.split(`
|
|
33
|
+
`);_=C.pop()||"";for(let x of C){let S=x.trim();if(!S||S.startsWith("event:")||!S.startsWith("data:"))continue;let T=S.slice(5).trim();if(T==="[DONE]"){h({}),m.enqueue(r.encode(`data: [DONE]
|
|
34
34
|
|
|
35
|
-
`));continue}let E;try{E=JSON.parse(
|
|
35
|
+
`));continue}let E;try{E=JSON.parse(T)}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=E.choices?.[0];if(!R)continue;let A=R.delta;if(A?.content){if(!c){c=!0;let O=a++,I={id:`${i}_msg`,type:"message",status:"in_progress",role:"assistant",content:[]};p.push(I),m.enqueue(g("response.output_item.added",{type:"response.output_item.added",output_index:O,item:I}))}m.enqueue(g("response.output_text.delta",{type:"response.output_text.delta",item_id:`${i}_msg`,output_index:a-1,content_index:0,delta:A.content}))}if(Array.isArray(A?.tool_calls))for(let O of A.tool_calls){if(O.id){u=O.id,d=a++;let I={id:O.id,type:"function_call",status:"in_progress",call_id:O.id,name:O.function?.name||"",arguments:""};p.push(I),m.enqueue(g("response.output_item.added",{type:"response.output_item.added",output_index:d,item:I}))}if(O.function?.arguments){m.enqueue(g("response.function_call_arguments.delta",{type:"response.function_call_arguments.delta",item_id:u,output_index:d,delta:O.function.arguments}));let I=p.find(L=>L.call_id===u);I&&(I.arguments+=O.function.arguments)}}R.finish_reason&&h(E)}}k||h({}),m.close()}catch(f){m.error(f)}}})}convertAnthropicStreamToResponsesAPI(e,t){let o=new TextDecoder,r=new TextEncoder,s=t?.provider?.models?.[0]||"gpt-5.4",i=`resp_${Date.now()}`,a="",l="",c=-1,u="",d="",p="",g=-1,y="",m="",f=-1,_=0,k=!1,h=!1,b=!1,v=!1,C=null,x=[],S=(I,L)=>r.encode(`event: ${I}
|
|
36
36
|
data: ${JSON.stringify(L)}
|
|
37
37
|
|
|
38
|
-
`),
|
|
39
|
-
`);F=Q.pop()||"";for(let
|
|
38
|
+
`),T=(I,L=[])=>({id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:I,model:s,output:L,usage:this.buildResponsesUsage()}),E=()=>{k||(k=!0,O.enqueue(S("response.created",{type:"response.created",response:T("in_progress")})),O.enqueue(S("response.in_progress",{type:"response.in_progress",response:T("in_progress")})))},R=(I,L)=>{O.enqueue(S("response.output_item.done",{type:"response.output_item.done",output_index:I,item:{...L,status:"completed"}}))},A=I=>{if(!v){if(v=!0,h){let L=x.find(F=>F.type==="message");L&&(L.content=[{type:"output_text",text:a}],L.status="completed")}for(let L of x)L.type==="function_call"&&(L.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:x,usage:I?{...this.buildResponsesUsage(I)}:this.buildResponsesUsage()}})}},O;return new ReadableStream({async start(I){O=I;try{let L=e.getReader(),F="";for(;;){let{done:N,value:D}=await L.read();if(N)break;F+=o.decode(D,{stream:!0});let Q=F.split(`
|
|
39
|
+
`);F=Q.pop()||"";for(let Pe of Q){let z=Pe.trim();if(!z||z.startsWith("event:")||!z.startsWith("data:"))continue;let re=z.slice(5).trim();if(re==="[DONE]")continue;let J;try{J=JSON.parse(re)}catch{continue}if(J.type==="message_start"&&(i=J.message?.id||i,E()),J.type==="content_block_start"){let M=J.content_block;if(C=M?.type||null,M?.type==="tool_use"){u=M.id||"",d=M.name||"",p="",g=_++;let G={id:u,type:"function_call",status:"in_progress",call_id:u,name:d,arguments:""};x.push(G),O.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:g,item:G}))}if(M?.type==="thinking"&&(y=M.thinking||"",!b)){b=!0,f=_++,m=`rs_${i}`;let G={id:m,type:"reasoning",status:"in_progress",summary:[]};x.push(G),O.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:f,item:G}))}if(M?.type==="text"&&(a=M?.text||"",!h)){h=!0,c=_++,l=`${i}_msg`;let G={id:l,type:"message",status:"in_progress",role:"assistant",content:[]};x.push(G),O.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:c,item:G}))}}if(J.type==="content_block_delta"){let M=J.delta;if(M?.type==="text_delta"){if(!h){h=!0,c=_++,l=`${i}_msg`;let G={id:l,type:"message",status:"in_progress",role:"assistant",content:[]};x.push(G),O.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:c,item:G}))}a+=M.text||"",O.enqueue(S("response.output_text.delta",{type:"response.output_text.delta",item_id:l,output_index:c,content_index:0,delta:M.text||""}))}M?.type==="thinking_delta"&&(y+=M.thinking||"",O.enqueue(S("response.reasoning_summary_text.delta",{type:"response.reasoning_summary_text.delta",item_id:m||`rs_${i}`,output_index:f>=0?f:0,summary_index:0,delta:M.thinking||""}))),M?.type==="input_json_delta"&&(p+=M.partial_json||"",O.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:M.partial_json||""})))}if(J.type==="content_block_stop"){if(C==="tool_use"&&u){O.enqueue(S("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:u,output_index:g,arguments:p}));let M=x.find(G=>G.call_id===u);M&&(M.arguments=p,M.status="completed"),M&&R(g,M)}if(C==="text"&&h){O.enqueue(S("response.output_text.done",{type:"response.output_text.done",item_id:l,output_index:c,content_index:0,text:a}));let M=x.find(G=>G.type==="message");M&&(M.content=[{type:"output_text",text:a}],R(c,M))}if(C==="thinking"&&b){let M=x.find(G=>G.type==="reasoning");M&&(M.summary=[{type:"summary_text",text:y}],R(f,M))}C=null}if(J.type==="message_delta"){let M=A(J.usage);M&&O.enqueue(M)}if(J.type==="message_stop"){let M=A();M&&O.enqueue(M),O.enqueue(r.encode(`data: [DONE]
|
|
40
40
|
|
|
41
|
-
`))}}}E();let B=A();B&&O.enqueue(B),F="",O.close()}catch(L){O.error(L)}}})}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(c=>c.type==="message"),o=e.output?.find(c=>c.type==="function_call"),r;t?.content?.length&&t?.content[0].annotations&&(r=t.content[0].annotations.map(c=>({type:"url_citation",url_citation:{url:c.url||"",title:c.title||"",content:"",start_index:c.start_index||0,end_index:c.end_index||0}}))),this.logger.debug({data:r,type:"url_citation"});let s=null,i=null,a=null;if(t&&t.reasoning&&(a={content:t.reasoning}),t&&t.content){let c=[],u=[];if(t.content.forEach(d=>{if(d.type==="output_text")c.push(d.text||"");else if(d.type==="output_image"){let p=this.buildImageContent({url:d.image_url,mime_type:d.mime_type});p&&u.push(p)}else if(d.type==="output_image_base64"){let p=this.buildImageContent({b64_json:d.image_base64,mime_type:d.mime_type});p&&u.push(p)}}),u.length>0){let d=[];c.length>0&&d.push({type:"text",text:c.join("")}),d.push(...u),s=d}else s=c.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:a,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 Do,ee,gn=w(()=>{"use strict";Do=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)}},ee=new Do(100)});function hn(n,e){n&&Ye.set(n,{...e,provider:n,capturedAt:Date.now()})}function Tt(n){let e=Ye.get(n);return e?{...e}:void 0}function yn(){return Array.from(Ye.values()).map(n=>({...n}))}function Sc(){try{if(!(0,_e.existsSync)(Uo))return;let n=JSON.parse((0,_e.readFileSync)(Uo,"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;Ye.set(t.provider,t)}}catch{}}function Ss(){try{if(Ye.size===0)return;(0,_e.existsSync)(Fo)||(0,_e.mkdirSync)(Fo,{recursive:!0}),(0,_e.writeFileSync)(Uo,JSON.stringify(Array.from(Ye.values()),null,2),"utf-8")}catch{}}function Xe(){xs||(xs=!0,Sc(),Cs=setInterval(Ss,6e4),Cs.unref?.(),process.on("exit",Ss))}var _e,jo,Rs,Fo,Uo,Ye,xs,Cs,Ze=w(()=>{"use strict";_e=require("fs"),jo=require("path"),Rs=require("@wengine-ai/claude-code-router-shared"),Fo=(0,jo.join)(Rs.HOME_DIR,"runtime"),Uo=(0,jo.join)(Fo,"quota-store.json"),Ye=new Map,xs=!1,Cs=null});function Ie(n){if(typeof n!="string")return;let e=n.trim();if(!(!e||!Rc.test(e)))return e}function Te(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 Rc,qe=w(()=>{"use strict";Rc=/^[A-Za-z0-9_-]+$/});function $c(){if(et.size<=ws)return;let n=[...et.keys()].slice(0,et.size-ws);for(let e of n)et.delete(e)}function Nc(){if(tt.size<=Ps)return;let n=[...tt].slice(0,tt.size-Ps);for(let e of n)tt.delete(e)}function Lc(n){let e=n.trim();if(!e||e==="."||e===".."||/[/\\\0]/.test(e))return[];let t=e.replace(/^sess_/,"");return[...new Set([e,t,`sess_${t}`])].filter(Boolean)}function Dc(n,e){let t;try{t=new Os.default(n,{readonly:!0,fileMustExist:!0});let o=t.prepare("SELECT workspace_path FROM tasks WHERE task_id = ? OR acp_session_id = ? ORDER BY updated_at DESC LIMIT 1");for(let r of e){let i=o.get(r,r)?.workspace_path;if(typeof i=="string"&&i.trim())return i.trim()}return null}catch{return null}finally{try{t?.close()}catch{}}}async function Fc(n,e){let t;try{t=await(0,_n.readdir)(n)}catch{return null}for(let o of t)for(let r of e){let s=(0,me.join)(n,o,`${r}.json`);if((0,Ho.existsSync)(s))try{let i=JSON.parse(await(0,_n.readFile)(s,"utf8")),a=i?.meta?.workspacePath??i?.workspacePath;if(typeof a=="string"&&a.trim())return a.trim()}catch{}}return null}async function As(n){let e=typeof n=="string"?n.trim():"";if(!e)return null;let t=et.get(e);if(t)return t;let o=async()=>{let r=Lc(e);if(r.length===0)return null;let s=(0,me.join)(wc(),Pc),i=(0,me.join)(s,Ec);if((0,Ho.existsSync)(i)){let a=Dc(i,r);if(a)return a}return Fc((0,me.join)(s,Oc),r)};try{let r=await o();if(!r&&!tt.has(e)){tt.add(e),Nc();for(let i=0;i<Ac&&!r;i++)await Mc(Ic),r=await o()}if(!r||!(0,me.isAbsolute)(r))return null;let s=(0,me.normalize)(r);return et.set(e,s),$c(),s}catch{return null}}var Ho,_n,Es,me,Os,wc,Pc,Ec,Oc,ws,Ps,Ac,Ic,et,tt,Mc,Is=w(()=>{"use strict";Ho=require("fs"),_n=require("fs/promises"),Es=require("os"),me=require("path"),Os=V(require("better-sqlite3"),1),wc=()=>process.env.CCR_ZCODE_DIR||(0,me.join)((0,Es.homedir)(),".zcode"),Pc="v2",Ec="tasks-index.sqlite",Oc="sessions",ws=500,Ps=500,Ac=3,Ic=50,et=new Map,tt=new Set,Mc=n=>new Promise(e=>setTimeout(e,n))});function jc(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 Hc(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 $s(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 zc(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 vt(n,e=!0){return{clientType:n,usageScope:"request",supportsExplicitExtendedContext:e}}function zo(n,e,t=!0){let o=Te(n?.body?.metadata?.user_id);return{clientType:e,usageScope:o?"session":"request",stableSessionId:o,supportsExplicitExtendedContext:t}}function Jo(n){return typeof n=="string"&&Uc.includes(n)}function Xc(n,e){return/zcode/i.test(e)?!0:Object.keys(n).some(t=>t.toLowerCase().startsWith("x-zcode"))}function Me(n){if(Jo(n?.clientType))return n.clientType;let e=Hc(n),t=n?.headers||{},o=typeof t["user-agent"]=="string"?t["user-agent"]:"",r=n?.body||{},s=jc(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($s(n,kn.CCR_PROJECT_HEADER).present)return"pi";if(Xc(t,o))return"zcode";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 a=n?.originalModel||r.model||"";return/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i.test(a)?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(a).toLowerCase()==="ccr-codex"?"codex":e.endsWith("/v1/messages")?"api":"unknown"}function Wo(n){return Bo[n]}function Zc(n){if(typeof n?.__ccrRequestScopeId=="string"&&n.__ccrRequestScopeId)return n.__ccrRequestScopeId;let e=typeof n?.id=="string"&&n.id||(0,Ms.randomUUID)();return n.__ccrRequestScopeId=e,e}function ve(n,e={}){let t=Me(n),o=Wo(t).createContext(n,e),r=o.stableSessionId||Zc(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,zc(n,kn.CCR_PROJECT_HEADER),n.usageSessionId=r,n.usageCacheKey=`${t}:${s}:${r}`,o}function Ns(){}var Ms,kn,Uc,Bc,Jc,Wc,Gc,Kc,Qc,Vc,Yc,Bo,nt=w(()=>{"use strict";Ms=require("node:crypto"),kn=require("@wengine-ai/claude-code-router-shared");qe();Uc=["claude-code","zcode","pi","qwen-code","opencode","codex","api","unknown"];Bc={type:"claude-code",createContext(n){return zo(n,"claude-code")}},Jc={type:"zcode",createContext(n){return zo(n,"zcode")}},Wc={type:"pi",createContext(n){let e=$s(n,kn.CCR_PROJECT_HEADER);return{...vt("pi",!1),projectId:e.value,projectHeaderPresent:e.present}}},Gc={type:"qwen-code",createContext(n){return zo(n,"qwen-code")}},Kc={type:"opencode",createContext(){return vt("opencode")}},Qc={type:"codex",createContext(){return vt("codex")}},Vc={type:"api",createContext(){return vt("api")}},Yc={type:"unknown",createContext(){return vt("unknown")}},Bo={"claude-code":Bc,zcode:Jc,pi:Wc,"qwen-code":Gc,opencode:Kc,codex:Qc,api:Vc,unknown:Yc}});function se(n,e,t,o){let r=Go(e,t);if(r.code==="model_unhealthy"){let a=Z(e,t,!0);if(a)return n.log.warn(`Strict project target ${e} is in the health fail-pool; retrying it anyway (no fallback available). scenario=${o}`),a}let s=n.sessionId||n.clientContext?.stableSessionId,i=js[r.code]||502;throw new ne(`Project routing error (${o}): ${r.reason}. Project-level Router target: "${e}". Session: "${s||"unknown"}".`,{sessionId:s,configuredTarget:e,code:r.code,statusCode:i})}function xn(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 St(n){let e=xn(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 el(n,e){if(!e||!n)return null;let t=xn(n);if(e[n])return e[n];if(e[t])return e[t];let{family:o}=St(n);if(o&&e[o])return e[o];for(let[r,s]of Object.entries(e)){let i=xn(r);if(i&&t.includes(i))return s}return null}function tl(n){return n?(n.input_tokens||0)+(n.cache_read_input_tokens||0)+(n.cache_creation_input_tokens||0):0}function bn(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 xt(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 Go(n,e){let t=bn(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(c=>c.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(c=>String(c).toLowerCase()===r.toLowerCase());if(!i)return{reason:`model "${r}" not found in provider "${o}"`,code:"model_not_found"};if(!j().isAvailable(s.name,String(i)))return{reason:`model "${r}" in provider "${o}" is temporarily unavailable (health fail-pool)`,code:"model_unhealthy"};let l=Tt(s.name);if(l){let c=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(c||u)return{reason:`provider "${o}" quota is exhausted`,code:"quota_exhausted"}}return{reason:`unable to resolve model "${n}"`,code:"resolution_failed"}}function Z(n,e,t,o,r,s,i){let a=bn(n);if(!a)return i?null:n;let{providerName:l,routeModel:c}=a,u=xt(e,l,c);if(!u){if(r===!0&&o&&!t&&s!==!1){let p=Be(),g=p.getPromotion(l,c,o,e);if(g){let y=bn(g);if(y){let m=xt(e,y.providerName,y.routeModel);if(m)return`${m.provider.name},${m.model}`}p.clear(l,c,o)}}return null}if(r===!0&&o&&!t&&s!==!1){let p=Be(),g=p.getPromotion(l,c,o,e);if(g){let y=bn(g);if(y){let m=xt(e,y.providerName,y.routeModel);if(m)return`${m.provider.name},${m.model}`}p.clear(l,c,o)}}if(!t&&!j().isAvailable(u.provider.name,u.model))return null;let d=Tt(u.provider.name);if(d){let p=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(p||g)return null}return`${u.provider.name},${u.model}`}function te(n,e,t,o,r,s){let i=j(),a=[t?.[n],o?.[n]];for(let l of a)if(!(!Array.isArray(l)||l.length===0))for(let c of l){let u=Z(c,e,!1,n,void 0,void 0,s);if(u)return u}return null}function nl(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 Ko(n){let e=xn(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 Hs(n,e,t){return t?.usageScope!=="session"?n:Math.max(n,tl(e))}function zs(n,e,t){return n?.extendedContextThreshold??e?.extendedContextThreshold??t??2e5}function ol(n,e,t,o,r,s,i,a,l,c,u,d){let p=n.clientContext,g=p?.longContextThreshold??t.longContextThreshold??l??6e4,y=Hs(e,r,p),m=n.modelFamily,f=zs(p,t,c),_=p?.supportsExplicitExtendedContext!==!1&&s===!0;if((_||y>f)&&t.extendedContext){let b=Z(t.extendedContext,o,!1,"extendedContext",a,d,u);if(b)return n.log.info(`Family: using extended context model (1M+), tokens: ${y}, estimated: ${e}, explicit: ${_}, threshold: ${f}`),{model:b,scenarioType:"extendedContext",isFallback:!1};let T=a?te("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: ${f}`),{model:T,scenarioType:"extendedContext",isFallback:!0};if(u){let C=se(n,t.extendedContext,o,"extendedContext");if(C)return{model:C,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?Z(t.longContext,o,!1,"longContext",a,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=a?te("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 C=se(n,t.longContext,o,"longContext");if(C)return{model:C,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=Z(t.webSearch,o,!1,"webSearch",a,d,u);if(b)return{model:b,scenarioType:"webSearch",isFallback:!1};let T=a?te("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 C=se(n,t.webSearch,o,"webSearch");if(C)return{model:C,scenarioType:"webSearch",isFallback:!1}}n.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(n.body.thinking?.type==="enabled"&&t.think){let b=Z(t.think,o,!1,"think",a,d,u);if(b)return{model:b,scenarioType:"think",isFallback:!1};let T=a?te("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 C=se(n,t.think,o,"think");if(C)return{model:C,scenarioType:"think",isFallback:!1}}n.log.warn("Family: think model unavailable (fail pool), skipping")}if(t.default){let b=Z(t.default,o,!1,"default",a,d,u);if(b)return{model:b,scenarioType:"default",isFallback:!1};let T=a?te("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 C=se(n,t.default,o,"default");if(C)return{model:C,scenarioType:"default",isFallback:!1}}n.log.warn("Family: default model unavailable (fail pool), skipping")}return null}function cl(){if($e.size<=Ls)return;let n=[...$e.keys()].slice(0,$e.size-Ls);for(let e of n)$e.delete(e)}function ll(){if(ot.size<=Ds)return;let n=[...ot].slice(0,ot.size-Ds);for(let e of n)ot.delete(e)}var Us,Tn,Cn,vn,ue,js,ne,ke,Ct,qc,rl,Rt,$e,Ls,ot,Ds,sl,il,al,Fs,Qo,rt=w(()=>{"use strict";Us=require("tiktoken");gn();Tn=require("fs/promises"),Cn=require("fs/promises"),vn=require("path"),ue=require("@wengine-ai/claude-code-router-shared");le();Ze();pn();qe();Is();nt();js={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},ne=class extends Error{statusCode;code;type;sessionId;configuredTarget;constructor(e,t){super(e),this.name="ProjectRoutingError",this.statusCode=t?.statusCode||js[t?.code||""]||502,this.code=t?.code||"project_routing_error",this.type="api_error",this.sessionId=t?.sessionId,this.configuredTarget=t?.configuredTarget}};ke=(0,Us.get_encoding)("cl100k_base"),Ct=(n,e,t)=>{let o=0;return Array.isArray(n)&&n.forEach(r=>{typeof r.content=="string"?o+=ke.encode(r.content).length:Array.isArray(r.content)&&r.content.forEach(s=>{s.type==="text"?o+=ke.encode(s.text).length:s.type==="tool_use"?o+=ke.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(o+=ke.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?o+=ke.encode(e).length:Array.isArray(e)&&e.forEach(r=>{r.type==="text"&&(typeof r.text=="string"?o+=ke.encode(r.text).length:Array.isArray(r.text)&&r.text.forEach(s=>{o+=ke.encode(s||"").length}))}),t&&t.forEach(r=>{r.description&&(o+=ke.encode(r.name+r.description).length),r.input_schema&&(o+=ke.encode(JSON.stringify(r.input_schema)).length)}),o},qc=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 ne(`Project routing error: invalid managed project id "${String(o)}".`,{configuredTarget:String(o),code:"invalid_project_id"});let r=t?o:n.sessionId?await Qo(n.sessionId):null;if(!r&&!t&&n.clientType==="zcode"&&n.sessionId){let s=await As(n.sessionId);s&&(0,ue.isZcodeProjectTakeoverActive)(s)&&(r=(0,ue.getClaudeProjectId)(s))}if(r){n.projectId=r;let s=(0,vn.join)(ue.HOME_DIR,r,"config.json");if(!t&&n.sessionId){let i=(0,vn.join)(ue.HOME_DIR,r,`${n.sessionId}.json`);try{let a=JSON.parse(await(0,Tn.readFile)(i,"utf8"));if(a&&a.Router&&Object.keys(a.Router).length>0)return a.Router}catch{}}try{let i=JSON.parse(await(0,Tn.readFile)(s,"utf8"));if(t&&(typeof i?.projectPath!="string"||(0,ue.getClaudeProjectId)(i.projectPath)!==o))throw new ne(`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 ne)throw i;if(t&&i?.code==="ENOENT")throw new ne(`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 a=n.sessionId||n.clientContext?.stableSessionId;throw new ne(`Project routing error: project config.json is malformed or unreadable in project "${r}". ${i.message}. Session: "${a||"unknown"}".`,{sessionId:a,configuredTarget:s,code:"project_config_error",statusCode:500})}}}};rl=async(n,e,t,o,r,s)=>{let i=t.get("providers")||[],a=r||t.get("Router"),l=a?.enableFallback===!0,c=l?a?.fallback:void 0,u=l&&!s?c||t.get("fallback"):c,d=!s,p=null;if(n.body?.system?.length>1&&n.body?.system[1]?.text?.includes("<CCR-SUBAGENT-MODEL>")){let x=n.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);x&&(n.body.system[1].text=n.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${x[1]}</CCR-SUBAGENT-MODEL>`,""),p=x[1])}let{family:g,extended:y,isCcrAlias:m}=St(n.body.model),f=a?.families?.[g||""];if(f&&a?.enableFamilyRouting){n.log.info(`Using model family routing for: ${g}${y?" (1M)":""}`),n.modelFamily=g,n.familyFallback=f.fallback;let x=ol(n,e,f,i,o,y,u,l,a?.longContextThreshold,a?.extendedContextThreshold,s,d);if(x)return{model:x.model,scenarioType:x.scenarioType}}if(n.body.model.includes(",")&&!s){let x=Z(n.body.model,i,!1,"default",l,d,s);if(x)return{model:x,scenarioType:"default"};n.log.warn(`Explicit model ${n.body.model} unavailable (fail pool), trying fallback`);let S=l?te("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&&!a?.enableFamilyRouting?null:el(n.body.model,a?.models);if(_){let x=Z(_,i,!1,"modelMapping",l,d,s);if(x)return n.log.info(`Using mapped model for ${n.body.model}: ${_}`),{model:x,scenarioType:"modelMapping"};let S=l?te("modelMapping",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"modelMapping"};if(s){let v=se(n,_,i,"modelMapping");if(v)return{model:v,scenarioType:"modelMapping"}}n.log.warn(`Mapped model ${_} unavailable (fail pool), skipping`)}let k=n.clientContext,h=Hs(e,o,k),b=zs(k,void 0,a?.extendedContextThreshold);if(h>b&&a?.extendedContext){n.log.info(`Using extended context (1M) model due to token count: ${h}, estimated: ${e}, threshold: ${b}`);let x=Z(a.extendedContext,i,!1,"extendedContext",l,d,s);if(x)return{model:x,scenarioType:"extendedContext"};n.log.warn(`Extended context model ${a.extendedContext} unavailable (fail pool), trying fallback`);let S=l?te("extendedContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"extendedContext"};if(s){let v=se(n,a.extendedContext,i,"extendedContext");if(v)return{model:v,scenarioType:"extendedContext"}}}let T=k?.longContextThreshold??a?.longContextThreshold??6e4;if(h>T&&a?.longContext){n.log.info(`Using long context model due to token count: ${h}, estimated: ${e}, threshold: ${T}`);let x=Z(a.longContext,i,!1,"longContext",l,d,s);if(x)return{model:x,scenarioType:"longContext"};n.log.warn(`Long context model ${a.longContext} unavailable (fail pool), trying fallback`);let S=l?te("longContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"longContext"};if(s){let v=se(n,a.longContext,i,"longContext");if(v)return{model:v,scenarioType:"longContext"}}}if(!s&&p)return{model:p,scenarioType:"default"};if(n.body.model?.includes("claude")&&n.body.model?.includes("haiku")&&a?.background){n.log.info(`Using background model for ${n.body.model}`);let x=Z(a.background,i,!1,"background",l,d,s);if(x)return{model:x,scenarioType:"background"};let S=l?te("background",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"background"};if(s){let v=se(n,a.background,i,"background");if(v)return{model:v,scenarioType:"background"}}n.log.warn(`Background model ${a.background} unavailable (fail pool), falling through`)}if(Array.isArray(n.body.tools)&&n.body.tools.some(x=>x.type?.startsWith("web_search"))&&a?.webSearch){let x=Z(a.webSearch,i,!1,"webSearch",l,d,s);if(x)return{model:x,scenarioType:"webSearch"};n.log.warn(`WebSearch model ${a.webSearch} unavailable (fail pool), trying fallback`);let S=l?te("webSearch",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"webSearch"};if(s){let v=se(n,a.webSearch,i,"webSearch");if(v)return{model:v,scenarioType:"webSearch"}}}if(n.body.thinking?.type==="enabled"&&a?.think){n.log.info(`Using think model for ${n.body.thinking}`);let x=Z(a.think,i,!1,"think",l,d,s);if(x)return{model:x,scenarioType:"think"};n.log.warn(`Think model ${a.think} unavailable (fail pool), trying fallback`);let S=l?te("think",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"think"};if(s){let v=se(n,a.think,i,"think");if(v)return{model:v,scenarioType:"think"}}}if(a?.default){let x=Z(a.default,i,!1,"default",l,d,s);if(x)return{model:x,scenarioType:"default"};n.log.warn(`Default model ${a.default} unavailable (fail pool), trying fallback`);let S=l?te("default",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"default"};let v=Z(a.default,i,!0,"default",l,d,s);if(v)return n.log.warn(`No fallback available; retrying default model ${a.default} despite fail-pool state (strict=${s})`),{model:v,scenarioType:"default"}}if(s){let x=se(n,a?.default||n.body.model,i,"default");if(x)return{model:x,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},Rt=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)&&ve(n,o.getAll());let s=await qc(n,o),i=s||o.get("Router"),a=i?.enableFallback===!0,l=o.get("providers")||[],c=!!s;n.enableFallback=a,n.strictProjectRouting=c,n.fallbackConfig=c?i?.fallback:i?.fallback||o.get("fallback");let u=n.clientContext?.usageScope==="session"?n.previousUsage??ee.get(n.usageCacheKey):void 0,{messages:d,system:p=[],tools:g}=n.body,y=o.get("REWRITE_SYSTEM_PROMPT");if(y&&p.length>1&&p[1]?.text?.includes("<env>")){let m=await(0,Tn.readFile)(y,"utf-8");p[1].text=`${m}<env>${p[1].text.split("<env>").pop()}`}try{let[m,f]=n.body.model.split(","),_=t.tokenizerService?.getTokenizerConfigForModel(m,f),k;t.tokenizerService?k=(await t.tokenizerService.countTokens({messages:d,system:p,tools:g},_,m)).tokenCount:k=Ct(d,p,g),n.tokenCount=k;let h,b=c?void 0:o.get("CUSTOM_ROUTER_PATH");if(b)try{h=await require(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 rl(n,k,o,u,s,!!s);h=S.model,n.scenarioType=S.scenarioType}let{family:T}=St(n.originalModel||n.body.model),C=i?.enableFamilyRouting&&T?i.families?.[T]:void 0,x=[C?.image,i?.image].filter((S,v,E)=>!!S&&E.indexOf(S)===v);if(x.length>0&&nl(n)&&!Ko(h))if(x.includes(h))n.scenarioType="image";else{let S=x.map(v=>Z(v,l,!1,"image",a,!c,c)).find(v=>!!v);if(S)n.log.info(`Using image model fallback for ${h}`),h=S,n.scenarioType="image";else{let v=a?te("image",l,C?.fallback,n.fallbackConfig,T||void 0,c):null;if(v)n.log.info(`Using image fallback model: ${v}`),h=v,n.scenarioType="image";else if(c){let E=se(n,x[0],l,"image");E&&(h=E,n.scenarioType="image")}else n.log.warn(`Image models ${x.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 ne)throw m;if(n.log.error(`Error in router middleware: ${m.message}`),c){let f=n.sessionId||n.clientContext?.stableSessionId;throw new ne(`Project routing error: router failure in project mode. ${m.message}. Session: "${f||"unknown"}".`,{sessionId:f,configuredTarget:i?.default,code:"project_router_failure",statusCode:502})}n.body.model=i?.default,n.scenarioType="default"}},$e=new Map,Ls=1e3,ot=new Set,Ds=1e3,sl=3,il=50,al=n=>new Promise(e=>setTimeout(e,n));Fs=async n=>{let e=await(0,Cn.opendir)(ue.CLAUDE_PROJECTS_DIR),t=[];for await(let s of e)s.isDirectory()&&t.push(s.name);let o=t.map(async s=>{let i=(0,vn.join)(ue.CLAUDE_PROJECTS_DIR,s,`${n}.jsonl`);try{return(await(0,Cn.stat)(i)).isFile()?s:null}catch{return null}}),r=await Promise.all(o);for(let s of r)if(s)return s;return null},Qo=async n=>{let e=Ie(n);if(!e)return null;if($e.has(e))return $e.get(e)||null;try{let t=await Fs(e);if(!t&&!ot.has(e)){ot.add(e),ll();for(let o=0;o<sl&&!t;o++)await al(il),t=await Fs(e)}return t?($e.set(e,t),cl(),t):null}catch(t){return console.error("Error searching for project by session:",t),null}}});function wt(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(`
|
|
41
|
+
`))}}}E();let B=A();B&&O.enqueue(B),F="",O.close()}catch(L){O.error(L)}}})}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(c=>c.type==="message"),o=e.output?.find(c=>c.type==="function_call"),r;t?.content?.length&&t?.content[0].annotations&&(r=t.content[0].annotations.map(c=>({type:"url_citation",url_citation:{url:c.url||"",title:c.title||"",content:"",start_index:c.start_index||0,end_index:c.end_index||0}}))),this.logger.debug({data:r,type:"url_citation"});let s=null,i=null,a=null;if(t&&t.reasoning&&(a={content:t.reasoning}),t&&t.content){let c=[],u=[];if(t.content.forEach(d=>{if(d.type==="output_text")c.push(d.text||"");else if(d.type==="output_image"){let p=this.buildImageContent({url:d.image_url,mime_type:d.mime_type});p&&u.push(p)}else if(d.type==="output_image_base64"){let p=this.buildImageContent({b64_json:d.image_base64,mime_type:d.mime_type});p&&u.push(p)}}),u.length>0){let d=[];c.length>0&&d.push({type:"text",text:c.join("")}),d.push(...u),s=d}else s=c.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:a,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 Wo,ee,_n=w(()=>{"use strict";Wo=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)}},ee=new Wo(100)});function kn(n,e){n&&Ye.set(n,{...e,provider:n,capturedAt:Date.now()})}function Tt(n){let e=Ye.get(n);return e?{...e}:void 0}function bn(){return Array.from(Ye.values()).map(n=>({...n}))}function Hc(){try{if(!(0,_e.existsSync)(Ko))return;let n=JSON.parse((0,_e.readFileSync)(Ko,"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;Ye.set(t.provider,t)}}catch{}}function Ds(){try{if(Ye.size===0)return;(0,_e.existsSync)(Go)||(0,_e.mkdirSync)(Go,{recursive:!0}),(0,_e.writeFileSync)(Ko,JSON.stringify(Array.from(Ye.values()),null,2),"utf-8")}catch{}}function Xe(){Ns||(Ns=!0,Hc(),Ls=setInterval(Ds,6e4),Ls.unref?.(),process.on("exit",Ds))}var _e,Qo,Fs,Go,Ko,Ye,Ns,Ls,Ze=w(()=>{"use strict";_e=require("fs"),Qo=require("path"),Fs=require("@wengine-ai/claude-code-router-shared"),Go=(0,Qo.join)(Fs.HOME_DIR,"runtime"),Ko=(0,Qo.join)(Go,"quota-store.json"),Ye=new Map,Ns=!1,Ls=null});function Ie(n){if(typeof n!="string")return;let e=n.trim();if(!(!e||!zc.test(e)))return e}function Te(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 zc,qe=w(()=>{"use strict";zc=/^[A-Za-z0-9_-]+$/});function Yc(){if(et.size<=Us)return;let n=[...et.keys()].slice(0,et.size-Us);for(let e of n)et.delete(e)}function Xc(){if(tt.size<=js)return;let n=[...tt].slice(0,tt.size-js);for(let e of n)tt.delete(e)}function Zc(n){let e=n.trim();if(!e||e==="."||e===".."||/[/\\\0]/.test(e))return[];let t=e.replace(/^sess_/,"");return[...new Set([e,t,`sess_${t}`])].filter(Boolean)}function qc(n,e){let t;try{t=new zs.default(n,{readonly:!0,fileMustExist:!0});let o=t.prepare("SELECT workspace_path FROM tasks WHERE task_id = ? OR acp_session_id = ? ORDER BY updated_at DESC LIMIT 1");for(let r of e){let i=o.get(r,r)?.workspace_path;if(typeof i=="string"&&i.trim())return i.trim()}return null}catch{return null}finally{try{t?.close()}catch{}}}async function el(n,e){let t;try{t=await(0,Tn.readdir)(n)}catch{return null}for(let o of t)for(let r of e){let s=(0,me.join)(n,o,`${r}.json`);if((0,Vo.existsSync)(s))try{let i=JSON.parse(await(0,Tn.readFile)(s,"utf8")),a=i?.meta?.workspacePath??i?.workspacePath;if(typeof a=="string"&&a.trim())return a.trim()}catch{}}return null}async function Bs(n){let e=typeof n=="string"?n.trim():"";if(!e)return null;let t=et.get(e);if(t)return t;let o=async()=>{let r=Zc(e);if(r.length===0)return null;let s=(0,me.join)(Bc(),Jc),i=(0,me.join)(s,Wc);if((0,Vo.existsSync)(i)){let a=qc(i,r);if(a)return a}return el((0,me.join)(s,Gc),r)};try{let r=await o();if(!r&&!tt.has(e)){tt.add(e),Xc();for(let i=0;i<Kc&&!r;i++)await Vc(Qc),r=await o()}if(!r||!(0,me.isAbsolute)(r))return null;let s=(0,me.normalize)(r);return et.set(e,s),Yc(),s}catch{return null}}var Vo,Tn,Hs,me,zs,Bc,Jc,Wc,Gc,Us,js,Kc,Qc,et,tt,Vc,Js=w(()=>{"use strict";Vo=require("fs"),Tn=require("fs/promises"),Hs=require("os"),me=require("path"),zs=V(require("better-sqlite3"),1),Bc=()=>process.env.CCR_ZCODE_DIR||(0,me.join)((0,Hs.homedir)(),".zcode"),Jc="v2",Wc="tasks-index.sqlite",Gc="sessions",Us=500,js=500,Kc=3,Qc=50,et=new Map,tt=new Set,Vc=n=>new Promise(e=>setTimeout(e,n))});function nl(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 ol(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 Gs(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 rl(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 vt(n,e=!0){return{clientType:n,usageScope:"request",supportsExplicitExtendedContext:e}}function Yo(n,e,t=!0){let o=Te(n?.body?.metadata?.user_id);return{clientType:e,usageScope:o?"session":"request",stableSessionId:o,supportsExplicitExtendedContext:t}}function Zo(n){return typeof n=="string"&&tl.includes(n)}function fl(n,e){return/zcode/i.test(e)?!0:Object.keys(n).some(t=>t.toLowerCase().startsWith("x-zcode"))}function Me(n){if(Zo(n?.clientType))return n.clientType;let e=ol(n),t=n?.headers||{},o=typeof t["user-agent"]=="string"?t["user-agent"]:"",r=n?.body||{},s=nl(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(Gs(n,vn.CCR_PROJECT_HEADER).present)return"pi";if(fl(t,o))return"zcode";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 a=n?.originalModel||r.model||"";return/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i.test(a)?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(a).toLowerCase()==="ccr-codex"?"codex":e.endsWith("/v1/messages")?"api":"unknown"}function qo(n){return Xo[n]}function ml(n){if(typeof n?.__ccrRequestScopeId=="string"&&n.__ccrRequestScopeId)return n.__ccrRequestScopeId;let e=typeof n?.id=="string"&&n.id||(0,Ws.randomUUID)();return n.__ccrRequestScopeId=e,e}function ve(n,e={}){let t=Me(n),o=qo(t).createContext(n,e),r=o.stableSessionId||ml(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,rl(n,vn.CCR_PROJECT_HEADER),n.usageSessionId=r,n.usageCacheKey=`${t}:${s}:${r}`,o}function Ks(){}var Ws,vn,tl,sl,il,al,cl,ll,ul,dl,pl,Xo,nt=w(()=>{"use strict";Ws=require("node:crypto"),vn=require("@wengine-ai/claude-code-router-shared");qe();tl=["claude-code","zcode","pi","qwen-code","opencode","codex","api","unknown"];sl={type:"claude-code",createContext(n){return Yo(n,"claude-code")}},il={type:"zcode",createContext(n){return Yo(n,"zcode")}},al={type:"pi",createContext(n){let e=Gs(n,vn.CCR_PROJECT_HEADER);return{...vt("pi",!1),projectId:e.value,projectHeaderPresent:e.present}}},cl={type:"qwen-code",createContext(n){return Yo(n,"qwen-code")}},ll={type:"opencode",createContext(){return vt("opencode")}},ul={type:"codex",createContext(){return vt("codex")}},dl={type:"api",createContext(){return vt("api")}},pl={type:"unknown",createContext(){return vt("unknown")}},Xo={"claude-code":sl,zcode:il,pi:al,"qwen-code":cl,opencode:ll,codex:ul,api:dl,unknown:pl}});function se(n,e,t,o){let r=er(e,t);if(r.code==="model_unhealthy"){let a=X(e,t,!0);if(a)return n.log.warn(`Strict project target ${e} is in the health fail-pool; retrying it anyway (no fallback available). scenario=${o}`),a}let s=n.sessionId||n.clientContext?.stableSessionId,i=Zs[r.code]||502;throw new ne(`Project routing error (${o}): ${r.reason}. Project-level Router target: "${e}". Session: "${s||"unknown"}".`,{sessionId:s,configuredTarget:e,code:r.code,statusCode:i})}function Rn(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 St(n){let e=Rn(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 yl(n,e){if(!e||!n)return null;let t=Rn(n);if(e[n])return e[n];if(e[t])return e[t];let{family:o}=St(n);if(o&&e[o])return e[o];for(let[r,s]of Object.entries(e)){let i=Rn(r);if(i&&t.includes(i))return s}return null}function _l(n){return n?(n.input_tokens||0)+(n.cache_read_input_tokens||0)+(n.cache_creation_input_tokens||0):0}function xn(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 xt(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 er(n,e){let t=xn(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(c=>c.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(c=>String(c).toLowerCase()===r.toLowerCase());if(!i)return{reason:`model "${r}" not found in provider "${o}"`,code:"model_not_found"};if(!j().isAvailable(s.name,String(i)))return{reason:`model "${r}" in provider "${o}" is temporarily unavailable (health fail-pool)`,code:"model_unhealthy"};let l=Tt(s.name);if(l){let c=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(c||u)return{reason:`provider "${o}" quota is exhausted`,code:"quota_exhausted"}}return{reason:`unable to resolve model "${n}"`,code:"resolution_failed"}}function X(n,e,t,o,r,s,i){let a=xn(n);if(!a)return i?null:n;let{providerName:l,routeModel:c}=a,u=xt(e,l,c);if(!u){if(r===!0&&o&&!t&&s!==!1){let p=Be(),g=p.getPromotion(l,c,o,e);if(g){let y=xn(g);if(y){let m=xt(e,y.providerName,y.routeModel);if(m)return`${m.provider.name},${m.model}`}p.clear(l,c,o)}}return null}if(r===!0&&o&&!t&&s!==!1){let p=Be(),g=p.getPromotion(l,c,o,e);if(g){let y=xn(g);if(y){let m=xt(e,y.providerName,y.routeModel);if(m)return`${m.provider.name},${m.model}`}p.clear(l,c,o)}}if(!t&&!j().isAvailable(u.provider.name,u.model))return null;let d=Tt(u.provider.name);if(d){let p=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(p||g)return null}return`${u.provider.name},${u.model}`}function te(n,e,t,o,r,s){let i=j(),a=[t?.[n],o?.[n]];for(let l of a)if(!(!Array.isArray(l)||l.length===0))for(let c of l){let u=X(c,e,!1,n,void 0,void 0,s);if(u)return u}return null}function kl(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 tr(n){let e=Rn(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 qs(n,e,t){return t?.usageScope!=="session"?n:Math.max(n,_l(e))}function ei(n,e,t){return n?.extendedContextThreshold??e?.extendedContextThreshold??t??2e5}function bl(n,e,t,o,r,s,i,a,l,c,u,d){let p=n.clientContext,g=p?.longContextThreshold??t.longContextThreshold??l??6e4,y=qs(e,r,p),m=n.modelFamily,f=ei(p,t,c),_=p?.supportsExplicitExtendedContext!==!1&&s===!0;if((_||y>f)&&t.extendedContext){let b=X(t.extendedContext,o,!1,"extendedContext",a,d,u);if(b)return n.log.info(`Family: using extended context model (1M+), tokens: ${y}, estimated: ${e}, explicit: ${_}, threshold: ${f}`),{model:b,scenarioType:"extendedContext",isFallback:!1};let v=a?te("extendedContext",o,t.fallback,i,m,u):null;if(v)return n.log.info(`Family: using extended context fallback model (1M+), tokens: ${y}, estimated: ${e}, explicit: ${_}, threshold: ${f}`),{model:v,scenarioType:"extendedContext",isFallback:!0};if(u){let C=se(n,t.extendedContext,o,"extendedContext");if(C)return{model:C,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?X(t.longContext,o,!1,"longContext",a,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 v=a?te("longContext",o,t.fallback,i,m,u):null;if(v)return n.log.info(`Family: using long context fallback model, tokens: ${y}, estimated: ${e}`),{model:v,scenarioType:"longContext",isFallback:!0};if(u&&t.longContext){let C=se(n,t.longContext,o,"longContext");if(C)return{model:C,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=X(t.webSearch,o,!1,"webSearch",a,d,u);if(b)return{model:b,scenarioType:"webSearch",isFallback:!1};let v=a?te("webSearch",o,t.fallback,i,m,u):null;if(v)return n.log.info("Family: using webSearch fallback model"),{model:v,scenarioType:"webSearch",isFallback:!0};if(u){let C=se(n,t.webSearch,o,"webSearch");if(C)return{model:C,scenarioType:"webSearch",isFallback:!1}}n.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(n.body.thinking?.type==="enabled"&&t.think){let b=X(t.think,o,!1,"think",a,d,u);if(b)return{model:b,scenarioType:"think",isFallback:!1};let v=a?te("think",o,t.fallback,i,m,u):null;if(v)return n.log.info("Family: using think fallback model"),{model:v,scenarioType:"think",isFallback:!0};if(u){let C=se(n,t.think,o,"think");if(C)return{model:C,scenarioType:"think",isFallback:!1}}n.log.warn("Family: think model unavailable (fail pool), skipping")}if(t.default){let b=X(t.default,o,!1,"default",a,d,u);if(b)return{model:b,scenarioType:"default",isFallback:!1};let v=a?te("default",o,t.fallback,i,m,u):null;if(v)return n.log.info("Family: using default fallback model"),{model:v,scenarioType:"default",isFallback:!0};if(u){let C=se(n,t.default,o,"default");if(C)return{model:C,scenarioType:"default",isFallback:!1}}n.log.warn("Family: default model unavailable (fail pool), skipping")}return null}function Sl(){if($e.size<=Qs)return;let n=[...$e.keys()].slice(0,$e.size-Qs);for(let e of n)$e.delete(e)}function Rl(){if(ot.size<=Vs)return;let n=[...ot].slice(0,ot.size-Vs);for(let e of n)ot.delete(e)}var Xs,Cn,wn,Sn,ue,Zs,ne,ke,Ct,hl,Tl,Rt,$e,Qs,ot,Vs,vl,xl,Cl,Ys,nr,rt=w(()=>{"use strict";Xs=require("tiktoken");_n();Cn=require("fs/promises"),wn=require("fs/promises"),Sn=require("path"),ue=require("@wengine-ai/claude-code-router-shared");le();Ze();pn();yn();qe();Js();nt();Zs={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},ne=class extends Error{statusCode;code;type;sessionId;configuredTarget;constructor(e,t){super(e),this.name="ProjectRoutingError",this.statusCode=t?.statusCode||Zs[t?.code||""]||502,this.code=t?.code||"project_routing_error",this.type="api_error",this.sessionId=t?.sessionId,this.configuredTarget=t?.configuredTarget}};ke=(0,Xs.get_encoding)("cl100k_base"),Ct=(n,e,t)=>{let o=0;return Array.isArray(n)&&n.forEach(r=>{typeof r.content=="string"?o+=ke.encode(r.content).length:Array.isArray(r.content)&&r.content.forEach(s=>{s.type==="text"?o+=ke.encode(s.text).length:s.type==="tool_use"?o+=ke.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(o+=ke.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?o+=ke.encode(e).length:Array.isArray(e)&&e.forEach(r=>{r.type==="text"&&(typeof r.text=="string"?o+=ke.encode(r.text).length:Array.isArray(r.text)&&r.text.forEach(s=>{o+=ke.encode(s||"").length}))}),t&&t.forEach(r=>{r.description&&(o+=ke.encode(r.name+r.description).length),r.input_schema&&(o+=ke.encode(JSON.stringify(r.input_schema)).length)}),o},hl=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 ne(`Project routing error: invalid managed project id "${String(o)}".`,{configuredTarget:String(o),code:"invalid_project_id"});let r=t?o:n.sessionId?await nr(n.sessionId):null;if(!r&&!t&&n.clientType==="zcode"&&n.sessionId){let s=await Bs(n.sessionId);s&&(0,ue.isZcodeProjectTakeoverActive)(s)&&(r=(0,ue.getClaudeProjectId)(s))}if(r){n.projectId=r;let s=(0,Sn.join)(ue.HOME_DIR,r,"config.json");if(!t&&n.sessionId){let i=(0,Sn.join)(ue.HOME_DIR,r,`${n.sessionId}.json`);try{let a=JSON.parse(await(0,Cn.readFile)(i,"utf8"));if(a&&a.Router&&Object.keys(a.Router).length>0)return a.Router}catch{}}try{let i=JSON.parse(await(0,Cn.readFile)(s,"utf8"));if(t&&(typeof i?.projectPath!="string"||(0,ue.getClaudeProjectId)(i.projectPath)!==o))throw new ne(`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 ne)throw i;if(t&&i?.code==="ENOENT")throw new ne(`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 a=n.sessionId||n.clientContext?.stableSessionId;throw new ne(`Project routing error: project config.json is malformed or unreadable in project "${r}". ${i.message}. Session: "${a||"unknown"}".`,{sessionId:a,configuredTarget:s,code:"project_config_error",statusCode:500})}}}};Tl=async(n,e,t,o,r,s)=>{let i=t.get("providers")||[],a=r||t.get("Router"),l=a?.enableFallback===!0,c=l?a?.fallback:void 0,u=l&&!s?c||t.get("fallback"):c,d=!s,p=null;if(n.body?.system?.length>1&&n.body?.system[1]?.text?.includes("<CCR-SUBAGENT-MODEL>")){let x=n.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);x&&(n.body.system[1].text=n.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${x[1]}</CCR-SUBAGENT-MODEL>`,""),p=x[1])}let{family:g,extended:y,isCcrAlias:m}=St(n.body.model),f=a?.families?.[g||""];if(f&&a?.enableFamilyRouting){n.log.info(`Using model family routing for: ${g}${y?" (1M)":""}`),n.modelFamily=g,n.familyFallback=f.fallback;let x=bl(n,e,f,i,o,y,u,l,a?.longContextThreshold,a?.extendedContextThreshold,s,d);if(x)return{model:x.model,scenarioType:x.scenarioType}}if(n.body.model.includes(",")&&!s){let x=X(n.body.model,i,!1,"default",l,d,s);if(x)return{model:x,scenarioType:"default"};n.log.warn(`Explicit model ${n.body.model} unavailable (fail pool), trying fallback`);let S=l?te("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&&!a?.enableFamilyRouting?null:yl(n.body.model,a?.models);if(_){let x=X(_,i,!1,"modelMapping",l,d,s);if(x)return n.log.info(`Using mapped model for ${n.body.model}: ${_}`),{model:x,scenarioType:"modelMapping"};let S=l?te("modelMapping",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"modelMapping"};if(s){let T=se(n,_,i,"modelMapping");if(T)return{model:T,scenarioType:"modelMapping"}}n.log.warn(`Mapped model ${_} unavailable (fail pool), skipping`)}let k=n.clientContext,h=qs(e,o,k),b=ei(k,void 0,a?.extendedContextThreshold);if(h>b&&a?.extendedContext){n.log.info(`Using extended context (1M) model due to token count: ${h}, estimated: ${e}, threshold: ${b}`);let x=X(a.extendedContext,i,!1,"extendedContext",l,d,s);if(x)return{model:x,scenarioType:"extendedContext"};n.log.warn(`Extended context model ${a.extendedContext} unavailable (fail pool), trying fallback`);let S=l?te("extendedContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"extendedContext"};if(s){let T=se(n,a.extendedContext,i,"extendedContext");if(T)return{model:T,scenarioType:"extendedContext"}}}let v=k?.longContextThreshold??a?.longContextThreshold??6e4;if(h>v&&a?.longContext){n.log.info(`Using long context model due to token count: ${h}, estimated: ${e}, threshold: ${v}`);let x=X(a.longContext,i,!1,"longContext",l,d,s);if(x)return{model:x,scenarioType:"longContext"};n.log.warn(`Long context model ${a.longContext} unavailable (fail pool), trying fallback`);let S=l?te("longContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"longContext"};if(s){let T=se(n,a.longContext,i,"longContext");if(T)return{model:T,scenarioType:"longContext"}}}if(!s&&p)return n.subagentOverrideModel=p,{model:p,scenarioType:"default"};if(n.body.model?.includes("claude")&&n.body.model?.includes("haiku")&&a?.background){n.log.info(`Using background model for ${n.body.model}`);let x=X(a.background,i,!1,"background",l,d,s);if(x)return{model:x,scenarioType:"background"};let S=l?te("background",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"background"};if(s){let T=se(n,a.background,i,"background");if(T)return{model:T,scenarioType:"background"}}n.log.warn(`Background model ${a.background} unavailable (fail pool), falling through`)}if(Array.isArray(n.body.tools)&&n.body.tools.some(x=>x.type?.startsWith("web_search"))&&a?.webSearch){let x=X(a.webSearch,i,!1,"webSearch",l,d,s);if(x)return{model:x,scenarioType:"webSearch"};n.log.warn(`WebSearch model ${a.webSearch} unavailable (fail pool), trying fallback`);let S=l?te("webSearch",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"webSearch"};if(s){let T=se(n,a.webSearch,i,"webSearch");if(T)return{model:T,scenarioType:"webSearch"}}}if(n.body.thinking?.type==="enabled"&&a?.think){n.log.info(`Using think model for ${n.body.thinking}`);let x=X(a.think,i,!1,"think",l,d,s);if(x)return{model:x,scenarioType:"think"};n.log.warn(`Think model ${a.think} unavailable (fail pool), trying fallback`);let S=l?te("think",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"think"};if(s){let T=se(n,a.think,i,"think");if(T)return{model:T,scenarioType:"think"}}}if(a?.default){let x=X(a.default,i,!1,"default",l,d,s);if(x)return{model:x,scenarioType:"default"};n.log.warn(`Default model ${a.default} unavailable (fail pool), trying fallback`);let S=l?te("default",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"default"};let T=X(a.default,i,!0,"default",l,d,s);if(T)return n.log.warn(`No fallback available; retrying default model ${a.default} despite fail-pool state (strict=${s})`),{model:T,scenarioType:"default"}}if(s){let x=se(n,a?.default||n.body.model,i,"default");if(x)return{model:x,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},Rt=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)&&ve(n,o.getAll());let s=await hl(n,o),i=s||o.get("Router"),a=i?.enableFallback===!0,l=o.get("providers")||[],c=!!s;n.enableFallback=a,n.strictProjectRouting=c,n.fallbackConfig=c?i?.fallback:i?.fallback||o.get("fallback");let u=n.clientContext?.usageScope==="session"?n.previousUsage??ee.get(n.usageCacheKey):void 0,{messages:d,system:p=[],tools:g}=n.body,y=o.get("REWRITE_SYSTEM_PROMPT");if(y&&p.length>1&&p[1]?.text?.includes("<env>")){let m=await(0,Cn.readFile)(y,"utf-8");p[1].text=`${m}<env>${p[1].text.split("<env>").pop()}`}try{let[m,f]=n.body.model.split(","),_=t.tokenizerService?.getTokenizerConfigForModel(m,f),k;t.tokenizerService?k=(await t.tokenizerService.countTokens({messages:d,system:p,tools:g},_,m)).tokenCount:k=Ct(d,p,g),n.tokenCount=k;let h,b=c?void 0:o.get("CUSTOM_ROUTER_PATH");if(b)try{h=await require(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 Tl(n,k,o,u,s,!!s);h=S.model,n.scenarioType=S.scenarioType}let{family:v}=St(n.originalModel||n.body.model),C=i?.enableFamilyRouting&&v?i.families?.[v]:void 0,x=[C?.image,i?.image].filter((S,T,E)=>!!S&&E.indexOf(S)===T);if(x.length>0&&kl(n)&&!tr(h))if(x.includes(h))n.scenarioType="image";else{let S=x.map(T=>X(T,l,!1,"image",a,!c,c)).find(T=>!!T);if(S)n.log.info(`Using image model fallback for ${h}`),h=S,n.scenarioType="image";else{let T=a?te("image",l,C?.fallback,n.fallbackConfig,v||void 0,c):null;if(T)n.log.info(`Using image fallback model: ${T}`),h=T,n.scenarioType="image";else if(c){let E=se(n,x[0],l,"image");E&&(h=E,n.scenarioType="image")}else n.log.warn(`Image models ${x.join(", ")} unavailable (fail pool), keeping ${h}`),n.scenarioType="image"}}if(i?.enableConcurrencyPriority===!0&&!(n.originalModel||"").includes(",")&&!n.subagentOverrideModel&&typeof h=="string"&&h.trim()){let S=n.scenarioType||"default",T=[...(n.familyFallback||{})[S]||[],...(n.fallbackConfig||{})[S]||[]];h=Ms(n,h,{threshold:Number(i?.concurrencyThreshold)||3,fallbackModels:T,resolve:E=>X(E,l,!1,S,a,!c,c),log:E=>n.log.info(E)})}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 ne)throw m;if(n.log.error(`Error in router middleware: ${m.message}`),c){let f=n.sessionId||n.clientContext?.stableSessionId;throw new ne(`Project routing error: router failure in project mode. ${m.message}. Session: "${f||"unknown"}".`,{sessionId:f,configuredTarget:i?.default,code:"project_router_failure",statusCode:502})}n.body.model=i?.default,n.scenarioType="default"}},$e=new Map,Qs=1e3,ot=new Set,Vs=1e3,vl=3,xl=50,Cl=n=>new Promise(e=>setTimeout(e,n));Ys=async n=>{let e=await(0,wn.opendir)(ue.CLAUDE_PROJECTS_DIR),t=[];for await(let s of e)s.isDirectory()&&t.push(s.name);let o=t.map(async s=>{let i=(0,Sn.join)(ue.CLAUDE_PROJECTS_DIR,s,`${n}.jsonl`);try{return(await(0,wn.stat)(i)).isFile()?s:null}catch{return null}}),r=await Promise.all(o);for(let s of r)if(s)return s;return null},nr=async n=>{let e=Ie(n);if(!e)return null;if($e.has(e))return $e.get(e)||null;try{let t=await Ys(e);if(!t&&!ot.has(e)){ot.add(e),Rl();for(let o=0;o<vl&&!t;o++)await Cl(xl),t=await Ys(e)}return t?($e.set(e,t),Sl(),t):null}catch(t){return console.error("Error searching for project by session:",t),null}}});function wt(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
42
|
`):"";r&&e.push({role:"assistant",content:[{type:"text",text:`[reasoning]
|
|
43
|
-
${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
|
|
43
|
+
${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 ti(n,e,t,o){let r=n.body;(n.ccrHookOrder||=[]).push("handler"),o.endPoint==="/v1/responses"&&r?.input&&!r?.messages&&wt(r),o.endPoint==="/v1/responses"&&!n.provider&&typeof r?.model=="string"&&wl.test(r.model)&&await Rt(n,e,{configService:t.configService,tokenizerService:t.tokenizerService});let s=n.provider;if(!s&&r?.model){let c=r.model.split(",");c.length>1&&(s=c[0],r.model=c.slice(1).join(","))}let i=s?t.providerService.getProvider(s):void 0;if(!i&&r?.model){let c=t.providerService.resolveModelRoute(r.model);c&&(i=c.provider,r.model=c.targetModel)}if(!i&&r?.model){let u=t.configService.get("Router")?.default;if(u&&u.includes(",")){let[d,...p]=u.split(","),g=t.providerService.getProvider(d);g&&(i=g,r.model=p.join(","))}}if(n.originalModel||(n.originalModel=r?.model),!i)throw K(`Provider '${s}' not found`,404,"provider_not_found");n.provider=i.name||s;let a=Ol(n),l=o.endPoint==="/v1/messages"&&a;a&&e.header("X-Codex-Detected","true"),l&&e.header("X-Codex-On-Messages","true"),n.isCodexOnMessagesEndpoint=l,n.codexDetected=a;try{o.endPoint==="/v1/responses"&&r.input&&!r.messages&&wt(r);let{requestBody:c,config:u,bypass:d}=await oi(r,i,o,n.headers,{req:n});if(c?.messages&&!d&&!n.isTargetAnthropic){let m=c.messages,f=new Set,_=[],k=[];for(let h of m){if(h?.role!=="system"){k.push(h);continue}let b=JSON.stringify(h.content??"");f.has(b)||(f.add(b),_.push(h))}_.length>0&&k.length>0&&(c.messages=[..._,...k])}let p=await ri(c,u,i,t,d,o,{req:n});try{i?.baseUrl&&p?.headers&&kt(s,i.baseUrl,p.headers)}catch{}let g=await si(c,p,i,o,d,{req:n,isCodexOnMessagesEndpoint:l});g=await ni(r,g,i.name||s);let y=ii(g,e,r,t);try{let m=r.model||n.originalModel;s&&m&&j().recordSuccess(s,m)}catch{}return y}catch(c){if(c instanceof ne)throw c;if(!n.__retryAttempted){n.__retryAttempted=!0;try{return await ti(n,e,t,o)}catch(d){if(n.__fallbackExhausted)throw d;c=d}}let u=await El(n,e,t,o,c);if(u)return u;throw n.__fallbackExhausted=!0,c}}async function ni(n,e,t){if(!n?.stream||!e?.body)return e;let o=e.body.getReader(),r=new TextDecoder,s=[],i="";for(let c=0;c<8;c++){let u=await o.read();if(u.done||(u.value&&(s.push(u.value),i+=r.decode(u.value,{stream:!0})),i.split(`
|
|
44
44
|
`).some(d=>d.startsWith("data:"))))break}if(i+=r.decode(),s.length===0)throw o.releaseLock(),K(`Provider returned empty streaming response from ${t}`,400,"provider_response_error");if(i.includes("event: error")){o.releaseLock();let c=i.length<500?i:i.slice(0,500);throw K(`Provider ${t} returned error in SSE stream: ${c}`,400,"provider_response_error")}if(i.split(`
|
|
45
|
-
`).filter(c=>c.startsWith("data:")&&c.trim().length>5).length===0){o.releaseLock();let c=i.length<500?i:i.slice(0,500);throw K(`Provider ${t} returned streaming response with no SSE data lines: ${c}`,400,"provider_response_error")}let l=new ReadableStream({start:c=>{for(let d of s)c.enqueue(d);let u=()=>{o.read().then(({done:d,value:p})=>{d?c.close():(c.enqueue(p),u())}).catch(d=>c.error(d))};u()},cancel:()=>{o.cancel()}});return new Response(l,{headers:e.headers,status:e.status,statusText:e.statusText})}async function dl(n,e,t,o,r){let s=n.scenarioType||"default",i=n.familyFallback,a=n.modelFamily,l=n.strictProjectRouting?n.fallbackConfig:n.fallbackConfig??t.configService.get("fallback"),c=j(),u=n.provider||"",d=n.body.model||"",p=new Set,g=r?.statusCode===429||r?.isRateLimit===!0||r?.rawBody&&Sn(r.rawBody)||r?.message&&Sn(r.message);if(u&&d&&(g?(c.markRateLimited(u,d,120,r?.message),n.log.warn(`Primary model ${u},${d} rate-limited, will try fallbacks`)):c.recordFailure(u,d,r?.message),p.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(),C=b.join(",").trim();return!T||!C?null:{provider:T,model:C,key:`${T},${C}`}},f=[];if(a){let k=i?.[s];if(Array.isArray(k)&&k.length>0)f.push({name:`${a}/${s}`,models:k});else{let h=n.strictProjectRouting;n.log.warn(`No ${a} fallback configured for ${s}; ${h?"no project fallback available":"will try global fallback"}`)}}if(Array.isArray(l?.[s])&&l[s].length>0&&f.push({name:`global/${s}`,models:l[s]}),f.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(C=>C.type==="image"||C.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`),f.push({name:"global/image (auto-detected)",models:l.image})),f.length===0)return null;let _=f.reduce((k,h)=>k+h.models.length,0);n.log.warn(`Request failed for ${s}, trying ${_} fallback models across ${f.length} fallback stage(s)`);for(let k of f){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(p.has(b.key))continue;p.add(b.key);let T=t.providerService.getProviders(),C=xt(T,b.provider,b.model);if(!C){n.log.warn(`Fallback model ${h} is not configured or provider is disabled, skipping`);continue}let x=C.provider.name,S=C.model;if(!c.isAvailable(x,S)){n.log.warn(`Fallback model ${h} unavailable (fail pool), skipping`);continue}let v;for(let E=1;E<=2;E++)try{n.log.info(`Trying fallback model: ${x},${S}${E>1?" (retry)":""}`);let R={...n.body};R.model=S;let A={...n,provider:x,body:R},O=C.provider,{requestBody:I,config:L,bypass:F}=await Ws(R,O,o,n.headers,{req:A}),B=await Gs(I,L,O,t,F,o,{req:A});try{O?.baseUrl&&B?.headers&&kt(x,O.baseUrl,B.headers)}catch{}let N=await Ks(I,B,O,o,F,{req:A,isCodexOnMessagesEndpoint:n.isCodexOnMessagesEndpoint||!1});return N=await Js(R,N,x),n.log.info(`Fallback model ${x},${S} succeeded`),c.recordSuccess(x,S),u&&d&&!n.strictProjectRouting&&(Be().promote(u,d,s,x,S),n.log.info(`Promoted fallback model ${x},${S} for ${u},${d}:${s}`),g?n.log.info(`Rate-limited original model ${u},${d} already marked unavailable with cooldown`):(c.forceOpen(u,d,r?.message),n.log.info(`Marked original model ${u},${d} as unavailable`))),n.provider=x,n.body=R,Qs(N,e,R,t)}catch(R){if(v=R,E===1){n.log.warn(`Fallback model ${x},${S} failed, retrying once: ${R.message}`);continue}}c.recordFailure(x,S,v?.message),n.log.warn(`Fallback model ${x},${S} failed after retry: ${v?.message}`),t.recordUsage?.({provider:x,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 Ws(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 c=0;c<i.length;c++){let u=i[c],d=typeof u=="string"?u:u?.text??"";if(d.includes("cch=")){let p=d.replace(/cch=[^;]+;?/g,"cch=ccr-stable;");typeof u=="string"?i[c]=p:typeof u?.text=="string"&&(u.text=p)}}let a={},l=!1;if(l=pl(e,t,n),l&&(o instanceof Headers?o.delete("content-length"):delete o["content-length"],a.headers=o),!l&&typeof t.transformRequestOut=="function"){let c=await t.transformRequestOut(s);c.body?(s=c.body,a=c.config||{}):s=c}if(!l&&e.transformer?.use?.length)for(let c of e.transformer.use){if(!c||typeof c.transformRequestIn!="function")continue;let u=await c.transformRequestIn(s,e,r);u.body?(s=u.body,a={...a,...u.config}):s=u}if(!l&&e.transformer?.[n.model]?.use?.length)for(let c of e.transformer[n.model].use)!c||typeof c.transformRequestIn!="function"||(s=await c.transformRequestIn(s,e,r));return{requestBody:s,config:a,bypass:l}}function pl(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 fl(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 a=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];cn(o.configService)&&as(o.log,i.req.id,{url:String(a),headers:l,body:n});let c=await us(a,n,{...e,headers:JSON.parse(JSON.stringify(l)),httpsProxy:fe(o.configService,t)},i,o.log);if(!c.ok){let u=await c.text();o.log.error(`[provider_response_error] Error from provider(${t.name},${n.model}: ${c.status}): ${u}`);let d=K(`Error from provider(${t.name},${n.model}: ${c.status}): ${u}`,c.status,"provider_response_error");throw d.isRateLimit=c.status===429||Sn(u),d.rawBody=u,d}if(c.ok){let u=c.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=${c.ok} status=${c.status} hasBody=${!!c.body}`),!d||u.includes("application/json")){let p=c.clone(),g="";try{if(g=await p.text(),!g||g.trim()==="")throw new SyntaxError("Empty JSON body");if(bt(g))return o.log.info(`[hidden-error-check] body is SSE mislabeled as ${u}, skipping JSON validation`),c;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}: ${c.status}): ${g}`);let f=Sn(g),_=K(`Error from provider(${t.name},${n.model}: ${c.status}): ${g}`,400,"provider_response_error");throw _.isRateLimit=f,_.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=K(`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&&cn(o.configService)){let d=await c.clone().text();cs(o.log,i.req.id,{status:c.status,headers:Object.fromEntries(c.headers.entries()),body:d})}return c}async function Ks(n,e,t,o,r,s){let i=e;if(!r&&t.transformer?.use?.length)for(let a of Array.from(t.transformer.use).reverse())!a||typeof a.transformResponseOut!="function"||(i=await a.transformResponseOut(i,s));if(!r&&t.transformer?.[n.model]?.use?.length)for(let a of Array.from(t.transformer[n.model].use).reverse())!a||typeof a.transformResponseOut!="function"||(i=await a.transformResponseOut(i,s));if(!r&&o.transformResponseIn&&(s.provider||(s.provider=t),i=await o.transformResponseIn(i,s)),s?.isCodexOnMessagesEndpoint)try{i=await new Ve().transformResponseIn(i,{...s,provider:t})}catch(a){console.error("Failed to apply Responses API conversion for codex client:",a)}return i}async function Qs(n,e,t,o){n.ok||e.code(n.status);let r=vs(n);if(!r){let s=await Ke(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 Qe(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&&cn(o.configService)){let[s,i]=n.body.tee(),a=is(o.configService);return ls(s,o.log,e.request.id,a),e.send(i)}return e.send(n.body)}else return U(n)}function Sn(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 ml(n){try{return new URL(n),!0}catch{return!1}}var ul,Vs,Rn=w(()=>{"use strict";ds();sn();yt();Ro();le();Ge();pn();Lo();q();rt();ce();ul=/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i;Vs=async n=>{n.get("/",async()=>({message:"LLMs API",version:dn})),n.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),n.get("/providers/health",async()=>({states:j().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)=>Bs(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:a}=t.body;if(!r?.trim())throw K("Provider name is required",400,"invalid_request");if(!s||!ml(s))throw K("Valid base URL is required",400,"invalid_request");if(!i?.trim())throw K("API key is required",400,"invalid_request");if(!a||!Array.isArray(a)||a.length===0)throw K("At least one model is required",400,"invalid_request");if(n.providerService.getProvider(t.body.name))throw K(`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 K("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 K("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 K("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 K("Provider not found",404,"provider_not_found");return{message:`Provider ${t.body.enabled?"enabled":"disabled"} successfully`}})}});var ge,wn=w(()=>{"use strict";ge=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 a=this.transformerService.getTransformer(i[0]);if(a)return new a(i[1])}if(typeof i=="string"){let a=this.transformerService.getTransformer(i);return typeof a=="function"?new a:a}}).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 a=this.transformerService.getTransformer(i[0]);if(a)return new a(i[1])}if(typeof i=="string"){let a=this.transformerService.getTransformer(i);return typeof a=="function"?new a:a}}).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}`,a={provider:o.name,model:s,fullModel:i};this.modelRoutes.set(i,a),this.modelRoutes.has(s)||this.modelRoutes.set(s,a)})),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 st,Pn,Ys,gl,Xs,Pt=w(()=>{"use strict";st=n=>n>0?n<=1024?"low":n<=8192?"medium":n<=16384?"high":"max":"none",Pn=n=>n==="none"?0:n==="low"?1024:n==="medium"?8192:n==="high"?16384:32768,Ys=["low","medium","high"],gl={none:"none",off:"none",disabled:"none",min:"low",minimal:"low",light:"low",low:"low",medium:"medium",mid:"medium",high:"high",xhigh:"max",max:"max",maximum:"max",ultra:"max",ultracode:"max"},Xs=n=>{if(typeof n=="string")return gl[n.trim().toLowerCase()]}});var Zs,qs=w(()=>{Zs=(n,e)=>(n.includes("base64")&&(n=n.split("base64").pop(),n.startsWith(",")&&(n=n.slice(1))),`data:${e};base64,${n}`)});function hl(n){return n.filter(e=>!!e?.function?.name).map(e=>({name:e.function.name,description:e.function.description,input_schema:e.function.parameters}))}function ei(n){let e=[],t=[],o=p=>{if(typeof p=="string"){t.push({type:"text",text:p});return}Array.isArray(p)&&p.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 p=new Map;for(let g=0;g<t.length;g++){let y=t[g],m=y?.text??JSON.stringify(y),f=p.get(m);if(f===void 0){p.set(m,g);continue}!t[f]?.cache_control&&y?.cache_control&&(t[f]=y),t.splice(g,1),g--}};n.messages.forEach(p=>{p.role==="system"?o(p.content):e.push(p)}),r();let s=[];for(let p=0;p<e.length;p++){let g=e[p];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 f=s[s.length-1];f&&f.role==="user"&&Array.isArray(f.content)?f.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(f=>{f&&typeof f=="object"&&f.type==="image_url"&&f.image_url?.url?y.push({type:"image",source:{type:"base64",media_type:f.media_type||"image/jpeg",data:f.image_url.url.split(",")[1]||f.image_url.url}}):y.push(f)});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(f=>{let _={};try{_=JSON.parse(f.function.arguments)}catch{_=f.function.arguments}y.push({type:"tool_use",id:f.id,name:f.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 p=t.filter(g=>g?.cache_control).length;for(let g of s)Array.isArray(g.content)&&(p+=g.content.filter(y=>y?.cache_control).length);return p},a=p=>p&&typeof p=="object"?{...p}:{type:"ephemeral"},l=()=>{for(let p=s.length-1;p>=0;p--){let g=s[p]?.content;if(!Array.isArray(g))continue;let y=g.find(m=>m?.cache_control);if(y?.cache_control)return y.cache_control}for(let p=t.length-1;p>=0;p--)if(t[p]?.cache_control)return t[p].cache_control;return{type:"ephemeral"}},c=(p,g)=>{if(!Array.isArray(p?.content)||p.content.some(m=>m?.cache_control))return!1;let y=p.content.find(m=>m&&typeof m=="object"&&!m.cache_control);return y?(y.cache_control=a(g),!0):!1};(()=>{let p=4-i();if(p<=0||s.length===0)return;let g=s,y=l(),m=[];for(let f=g.length-1;f>=0;f--)g[f]?.role!=="user"||!Array.isArray(g[f].content)||m.push(f);for(let f of m){if(p<=0)break;c(g[f],y)&&p--}})();let d={messages:s,model:n.model,max_tokens:n.max_tokens||4096,temperature:n.temperature,stream:n.stream};if(n.reasoning?.enabled){let p=n.reasoning.max_tokens||Pn(n.reasoning.effort||"medium"),g=Math.min(p,d.max_tokens-1024);g>=1024&&(d.thinking={type:"enabled",budget_tokens:g});let y=n.reasoning.effort&&n.reasoning.effort!=="none"?n.reasoning.effort:st(p);Ys.includes(y)&&(d.output_config={...d.output_config||{},effort:y})}if(t.length>0){let p=t.some(g=>g?.cache_control);d.system=p?t:t.map(g=>g.text||"").join(`
|
|
45
|
+
`).filter(c=>c.startsWith("data:")&&c.trim().length>5).length===0){o.releaseLock();let c=i.length<500?i:i.slice(0,500);throw K(`Provider ${t} returned streaming response with no SSE data lines: ${c}`,400,"provider_response_error")}let l=new ReadableStream({start:c=>{for(let d of s)c.enqueue(d);let u=()=>{o.read().then(({done:d,value:p})=>{d?c.close():(c.enqueue(p),u())}).catch(d=>c.error(d))};u()},cancel:()=>{o.cancel()}});return new Response(l,{headers:e.headers,status:e.status,statusText:e.statusText})}async function El(n,e,t,o,r){let s=n.scenarioType||"default",i=n.familyFallback,a=n.modelFamily,l=n.strictProjectRouting?n.fallbackConfig:n.fallbackConfig??t.configService.get("fallback"),c=j(),u=n.provider||"",d=n.body.model||"",p=new Set,g=r?.statusCode===429||r?.isRateLimit===!0||r?.rawBody&&En(r.rawBody)||r?.message&&En(r.message);if(u&&d&&(g?(c.markRateLimited(u,d,120,r?.message),n.log.warn(`Primary model ${u},${d} rate-limited, will try fallbacks`)):c.recordFailure(u,d,r?.message),p.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(","),v=h?.trim(),C=b.join(",").trim();return!v||!C?null:{provider:v,model:C,key:`${v},${C}`}},f=[];if(a){let k=i?.[s];if(Array.isArray(k)&&k.length>0)f.push({name:`${a}/${s}`,models:k});else{let h=n.strictProjectRouting;n.log.warn(`No ${a} fallback configured for ${s}; ${h?"no project fallback available":"will try global fallback"}`)}}if(Array.isArray(l?.[s])&&l[s].length>0&&f.push({name:`global/${s}`,models:l[s]}),f.length===0&&s!=="image"&&n.body?.messages?.some(b=>b.role==="user"&&Array.isArray(b.content)&&b.content.some(v=>v.type==="image"||v.type==="image_url"||Array.isArray(v?.content)&&v.content.some(C=>C.type==="image"||C.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`),f.push({name:"global/image (auto-detected)",models:l.image})),f.length===0)return null;let _=f.reduce((k,h)=>k+h.models.length,0);n.log.warn(`Request failed for ${s}, trying ${_} fallback models across ${f.length} fallback stage(s)`);for(let k of f){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(p.has(b.key))continue;p.add(b.key);let v=t.providerService.getProviders(),C=xt(v,b.provider,b.model);if(!C){n.log.warn(`Fallback model ${h} is not configured or provider is disabled, skipping`);continue}let x=C.provider.name,S=C.model;if(!c.isAvailable(x,S)){n.log.warn(`Fallback model ${h} unavailable (fail pool), skipping`);continue}let T;for(let E=1;E<=2;E++)try{n.log.info(`Trying fallback model: ${x},${S}${E>1?" (retry)":""}`);let R={...n.body};R.model=S;let A={...n,provider:x,body:R},O=C.provider,{requestBody:I,config:L,bypass:F}=await oi(R,O,o,n.headers,{req:A}),B=await ri(I,L,O,t,F,o,{req:A});try{O?.baseUrl&&B?.headers&&kt(x,O.baseUrl,B.headers)}catch{}let N=await si(I,B,O,o,F,{req:A,isCodexOnMessagesEndpoint:n.isCodexOnMessagesEndpoint||!1});return N=await ni(R,N,x),n.log.info(`Fallback model ${x},${S} succeeded`),c.recordSuccess(x,S),u&&d&&!n.strictProjectRouting&&(Be().promote(u,d,s,x,S),n.log.info(`Promoted fallback model ${x},${S} for ${u},${d}:${s}`),g?n.log.info(`Rate-limited original model ${u},${d} already marked unavailable with cooldown`):(c.forceOpen(u,d,r?.message),n.log.info(`Marked original model ${u},${d} as unavailable`))),n.provider=x,n.body=R,Is(n,`${x},${S}`),ii(N,e,R,t)}catch(R){if(T=R,E===1){n.log.warn(`Fallback model ${x},${S} failed, retrying once: ${R.message}`);continue}}c.recordFailure(x,S,T?.message),n.log.warn(`Fallback model ${x},${S} failed after retry: ${T?.message}`),t.recordUsage?.({provider:x,model:S,originalModel:n.originalModel||d||n.body.model,scenarioType:s,modelFamily:n.modelFamily,errorMessage:T?.message||String(T),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 oi(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 c=0;c<i.length;c++){let u=i[c],d=typeof u=="string"?u:u?.text??"";if(d.includes("cch=")){let p=d.replace(/cch=[^;]+;?/g,"cch=ccr-stable;");typeof u=="string"?i[c]=p:typeof u?.text=="string"&&(u.text=p)}}let a={},l=!1;if(l=Pl(e,t,n),l&&(o instanceof Headers?o.delete("content-length"):delete o["content-length"],a.headers=o),!l&&typeof t.transformRequestOut=="function"){let c=await t.transformRequestOut(s);c.body?(s=c.body,a=c.config||{}):s=c}if(!l&&e.transformer?.use?.length)for(let c of e.transformer.use){if(!c||typeof c.transformRequestIn!="function")continue;let u=await c.transformRequestIn(s,e,r);u.body?(s=u.body,a={...a,...u.config}):s=u}if(!l&&e.transformer?.[n.model]?.use?.length)for(let c of e.transformer[n.model].use)!c||typeof c.transformRequestIn!="function"||(s=await c.transformRequestIn(s,e,r));return{requestBody:s,config:a,bypass:l}}function Pl(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 Ol(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 ri(n,e,t,o,r,s,i){let a=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];cn(o.configService)&&gs(o.log,i.req.id,{url:String(a),headers:l,body:n});let c=await _s(a,n,{...e,headers:JSON.parse(JSON.stringify(l)),httpsProxy:fe(o.configService,t)},i,o.log);if(!c.ok){let u=await c.text();o.log.error(`[provider_response_error] Error from provider(${t.name},${n.model}: ${c.status}): ${u}`);let d=K(`Error from provider(${t.name},${n.model}: ${c.status}): ${u}`,c.status,"provider_response_error");throw d.isRateLimit=c.status===429||En(u),d.rawBody=u,d}if(c.ok){let u=c.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=${c.ok} status=${c.status} hasBody=${!!c.body}`),!d||u.includes("application/json")){let p=c.clone(),g="";try{if(g=await p.text(),!g||g.trim()==="")throw new SyntaxError("Empty JSON body");if(bt(g))return o.log.info(`[hidden-error-check] body is SSE mislabeled as ${u}, skipping JSON validation`),c;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}: ${c.status}): ${g}`);let f=En(g),_=K(`Error from provider(${t.name},${n.model}: ${c.status}): ${g}`,400,"provider_response_error");throw _.isRateLimit=f,_.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=K(`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&&cn(o.configService)){let d=await c.clone().text();hs(o.log,i.req.id,{status:c.status,headers:Object.fromEntries(c.headers.entries()),body:d})}return c}async function si(n,e,t,o,r,s){let i=e;if(!r&&t.transformer?.use?.length)for(let a of Array.from(t.transformer.use).reverse())!a||typeof a.transformResponseOut!="function"||(i=await a.transformResponseOut(i,s));if(!r&&t.transformer?.[n.model]?.use?.length)for(let a of Array.from(t.transformer[n.model].use).reverse())!a||typeof a.transformResponseOut!="function"||(i=await a.transformResponseOut(i,s));if(!r&&o.transformResponseIn&&(s.provider||(s.provider=t),i=await o.transformResponseIn(i,s)),s?.isCodexOnMessagesEndpoint)try{i=await new Ve().transformResponseIn(i,{...s,provider:t})}catch(a){console.error("Failed to apply Responses API conversion for codex client:",a)}return i}async function ii(n,e,t,o){n.ok||e.code(n.status);let r=$s(n);if(!r){let s=await Ke(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 Qe(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&&cn(o.configService)){let[s,i]=n.body.tee(),a=ms(o.configService);return ys(s,o.log,e.request.id,a),e.send(i)}return e.send(n.body)}else return U(n)}function En(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 Al(n){try{return new URL(n),!0}catch{return!1}}var wl,ai,Pn=w(()=>{"use strict";ks();sn();yt();Eo();le();Ge();pn();yn();Jo();q();rt();ce();wl=/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i;ai=async n=>{n.get("/",async()=>({message:"LLMs API",version:dn})),n.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),n.get("/providers/health",async()=>({states:j().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)=>ti(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:a}=t.body;if(!r?.trim())throw K("Provider name is required",400,"invalid_request");if(!s||!Al(s))throw K("Valid base URL is required",400,"invalid_request");if(!i?.trim())throw K("API key is required",400,"invalid_request");if(!a||!Array.isArray(a)||a.length===0)throw K("At least one model is required",400,"invalid_request");if(n.providerService.getProvider(t.body.name))throw K(`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 K("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 K("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 K("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 K("Provider not found",404,"provider_not_found");return{message:`Provider ${t.body.enabled?"enabled":"disabled"} successfully`}})}});var ge,On=w(()=>{"use strict";ge=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 a=this.transformerService.getTransformer(i[0]);if(a)return new a(i[1])}if(typeof i=="string"){let a=this.transformerService.getTransformer(i);return typeof a=="function"?new a:a}}).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 a=this.transformerService.getTransformer(i[0]);if(a)return new a(i[1])}if(typeof i=="string"){let a=this.transformerService.getTransformer(i);return typeof a=="function"?new a:a}}).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}`,a={provider:o.name,model:s,fullModel:i};this.modelRoutes.set(i,a),this.modelRoutes.has(s)||this.modelRoutes.set(s,a)})),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 st,An,ci,Il,li,Et=w(()=>{"use strict";st=n=>n>0?n<=1024?"low":n<=8192?"medium":n<=16384?"high":"max":"none",An=n=>n==="none"?0:n==="low"?1024:n==="medium"?8192:n==="high"?16384:32768,ci=["low","medium","high"],Il={none:"none",off:"none",disabled:"none",min:"low",minimal:"low",light:"low",low:"low",medium:"medium",mid:"medium",high:"high",xhigh:"max",max:"max",maximum:"max",ultra:"max",ultracode:"max"},li=n=>{if(typeof n=="string")return Il[n.trim().toLowerCase()]}});var ui,di=w(()=>{ui=(n,e)=>(n.includes("base64")&&(n=n.split("base64").pop(),n.startsWith(",")&&(n=n.slice(1))),`data:${e};base64,${n}`)});function Ml(n){return n.filter(e=>!!e?.function?.name).map(e=>({name:e.function.name,description:e.function.description,input_schema:e.function.parameters}))}function pi(n){let e=[],t=[],o=p=>{if(typeof p=="string"){t.push({type:"text",text:p});return}Array.isArray(p)&&p.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 p=new Map;for(let g=0;g<t.length;g++){let y=t[g],m=y?.text??JSON.stringify(y),f=p.get(m);if(f===void 0){p.set(m,g);continue}!t[f]?.cache_control&&y?.cache_control&&(t[f]=y),t.splice(g,1),g--}};n.messages.forEach(p=>{p.role==="system"?o(p.content):e.push(p)}),r();let s=[];for(let p=0;p<e.length;p++){let g=e[p];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 f=s[s.length-1];f&&f.role==="user"&&Array.isArray(f.content)?f.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(f=>{f&&typeof f=="object"&&f.type==="image_url"&&f.image_url?.url?y.push({type:"image",source:{type:"base64",media_type:f.media_type||"image/jpeg",data:f.image_url.url.split(",")[1]||f.image_url.url}}):y.push(f)});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(f=>{let _={};try{_=JSON.parse(f.function.arguments)}catch{_=f.function.arguments}y.push({type:"tool_use",id:f.id,name:f.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 p=t.filter(g=>g?.cache_control).length;for(let g of s)Array.isArray(g.content)&&(p+=g.content.filter(y=>y?.cache_control).length);return p},a=p=>p&&typeof p=="object"?{...p}:{type:"ephemeral"},l=()=>{for(let p=s.length-1;p>=0;p--){let g=s[p]?.content;if(!Array.isArray(g))continue;let y=g.find(m=>m?.cache_control);if(y?.cache_control)return y.cache_control}for(let p=t.length-1;p>=0;p--)if(t[p]?.cache_control)return t[p].cache_control;return{type:"ephemeral"}},c=(p,g)=>{if(!Array.isArray(p?.content)||p.content.some(m=>m?.cache_control))return!1;let y=p.content.find(m=>m&&typeof m=="object"&&!m.cache_control);return y?(y.cache_control=a(g),!0):!1};(()=>{let p=4-i();if(p<=0||s.length===0)return;let g=s,y=l(),m=[];for(let f=g.length-1;f>=0;f--)g[f]?.role!=="user"||!Array.isArray(g[f].content)||m.push(f);for(let f of m){if(p<=0)break;c(g[f],y)&&p--}})();let d={messages:s,model:n.model,max_tokens:n.max_tokens||4096,temperature:n.temperature,stream:n.stream};if(n.reasoning?.enabled){let p=n.reasoning.max_tokens||An(n.reasoning.effort||"medium"),g=Math.min(p,d.max_tokens-1024);g>=1024&&(d.thinking={type:"enabled",budget_tokens:g});let y=n.reasoning.effort&&n.reasoning.effort!=="none"?n.reasoning.effort:st(p);ci.includes(y)&&(d.output_config={...d.output_config||{},effort:y})}if(t.length>0){let p=t.some(g=>g?.cache_control);d.system=p?t:t.map(g=>g.text||"").join(`
|
|
46
46
|
|
|
47
|
-
`)}return n.tools&&n.tools.length>0&&(d.tools=
|
|
48
|
-
`));let u=a.content.filter(p=>p.type==="tool_use"&&p.id);u.length&&(l.tool_calls=u.map(p=>({id:p.id,type:"function",function:{name:p.name,arguments:JSON.stringify(p.input||{})}})));let d=a.content.find(p=>p.type==="thinking");d&&(l.thinking={content:d.thinking,signature:d.signature}),t.push(l)}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},s=
|
|
47
|
+
`)}return n.tools&&n.tools.length>0&&(d.tools=Ml(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 fi=w(()=>{Et()});function $l(n){if(n===!0)return!0;if(n===!1)return!1;if(typeof n!="string")return;let e=n.trim().toLowerCase();if(e==="enabled"||e==="adaptive"||e==="true")return!0;if(e==="disabled"||e==="off"||e==="none"||e==="false")return!1}var In,Mn,mi=w(()=>{"use strict";In=require("uuid");Et();sn();q();di();fi();Mn=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 a=e.system.filter(l=>l.type==="text"&&l.text).map(l=>({type:"text",text:l.text,cache_control:l.cache_control}));t.push({role:"system",content:a})}}let o=JSON.parse(JSON.stringify(e.messages||[]));o?.forEach(a=>{a.role==="system"&&t.push({role:"system",content:a.content})}),o?.forEach(a=>{if(a.role!=="system"&&(a.role==="user"||a.role==="assistant")){if(typeof a.content=="string"){t.push({role:a.role,content:a.content});return}if(Array.isArray(a.content)){if(a.role==="user"){let l=a.content.filter(u=>u.type==="tool_result"&&u.tool_use_id);l.length&&l.forEach(u=>{let d={role:"tool",content:typeof u.content=="string"?u.content:JSON.stringify(u.content),tool_call_id:u.tool_use_id,cache_control:u.cache_control};t.push(d)});let c=a.content.filter(u=>u.type==="text"&&u.text||u.type==="image"&&u.source);c.length&&t.push({role:"user",content:c.map(u=>u?.type==="image"?{type:"image_url",image_url:{url:u.source?.type==="base64"?ui(u.source.data,u.source.media_type):u.source.url},media_type:u.source.media_type}:u)})}else if(a.role==="assistant"){let l={role:"assistant",content:""},c=a.content.filter(p=>p.type==="text"&&p.text);c.length&&(l.content=c.map(p=>p.text).join(`
|
|
48
|
+
`));let u=a.content.filter(p=>p.type==="tool_use"&&p.id);u.length&&(l.tool_calls=u.map(p=>({id:p.id,type:"function",function:{name:p.name,arguments:JSON.stringify(p.input||{})}})));let d=a.content.find(p=>p.type==="thinking");d&&(l.thinking={content:d.thinking,signature:d.signature}),t.push(l)}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},s=li(e.output_config?.effort),i=typeof e.thinking?.budget_tokens=="number"?e.thinking.budget_tokens:void 0;if(e.thinking!==void 0||s!==void 0){let a={},c=$l(e.thinking?.type)??(s!==void 0||(i??0)>0);c!==void 0&&(a.enabled=c),s!==void 0?a.effort=s:i!==void 0&&(a.effort=st(i)),i!==void 0&&i>0&&(a.max_tokens=i),Object.keys(a).length>0&&(r.reasoning=a)}return 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=pi(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 Ke(e);if((r?.isSSE??!1)&&r?.body){let c=await this.convertOpenAIStreamToAnthropic(r.body,t);return new Response(c,{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 c=await Qe(e);if(c.isSSE){let u=await this.convertOpenAIStreamToAnthropic(c.response.body,t);return new Response(u,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}i=c.response}let a=await U(i),l=this.convertOpenAIResponseToAnthropic(a,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()}`,a=null,l="unknown",c=!1,u=!1,d=!1,p=new Map,g=new Map,y=0,m=0,f=0,_=!1,k=!1,h=0,b=-1,v=()=>{let T=h;return h++,T},C=T=>{if(!_)try{r.enqueue(T);let E=new TextDecoder().decode(T);this.logger.debug({reqId:t.req.id,data:E,type:"send data"})}catch(E){if(E instanceof TypeError&&E.message.includes("Controller is already closed"))_=!0;else throw this.logger.debug({reqId:t.req.id,error:E instanceof Error?E.message:String(E),type:"send data error"}),E}},x=()=>{if(!_)try{if(!c){this.logger?.warn("[empty_stream] Upstream produced no data, closing silently"),r.close(),_=!0;return}if(b>=0){let E={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
49
49
|
data: ${JSON.stringify(E)}
|
|
50
50
|
|
|
51
51
|
`)),b=-1}if(a)C(s.encode(`event: message_delta
|
|
@@ -54,17 +54,17 @@ data: ${JSON.stringify(a)}
|
|
|
54
54
|
`)),a=null;else{let E=t?.req?.tokenCount||0;C(s.encode(`event: message_delta
|
|
55
55
|
data: ${JSON.stringify({type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{input_tokens:E,output_tokens:0,cache_read_input_tokens:0}})}
|
|
56
56
|
|
|
57
|
-
`))}let
|
|
58
|
-
data: ${JSON.stringify(
|
|
57
|
+
`))}let T={type:"message_stop"};C(s.encode(`event: message_stop
|
|
58
|
+
data: ${JSON.stringify(T)}
|
|
59
59
|
|
|
60
|
-
`)),r.close(),_=!0}catch(
|
|
60
|
+
`)),r.close(),_=!0}catch(T){if(T instanceof TypeError&&T.message.includes("Controller is already closed"))_=!0;else throw T}},S=null;try{S=e.getReader();let T=new TextDecoder,E="";for(;!_;){let{done:R,value:A}=await S.read();if(R)break;E+=T.decode(A,{stream:!0});let O=E.split(`
|
|
61
61
|
`);E=O.pop()||"";for(let I of O){if(_)break;if(!I.startsWith("data:"))continue;let L=I.slice(5).trim();if(this.logger.debug({reqId:t.req.id,type:"recieved data",data:L}),L!=="[DONE]")try{let F=JSON.parse(L);if(y++,this.logger.debug({reqId:t.req.id,response:F,tppe:"Original Response"}),F.error){t?.req&&(t.req.sseError=F.error);let N={type:"error",message:{type:"api_error",message:JSON.stringify(F.error)}};C(s.encode(`event: error
|
|
62
62
|
data: ${JSON.stringify(N)}
|
|
63
63
|
|
|
64
64
|
`));continue}if(l=F.model||l,!c&&!_&&!d){c=!0;let N=t?.req?.tokenCount||0,D={type:"message_start",message:{id:i,type:"message",role:"assistant",content:[],model:l,stop_reason:null,stop_sequence:null,usage:{input_tokens:N,output_tokens:0}}};C(s.encode(`event: message_start
|
|
65
65
|
data: ${JSON.stringify(D)}
|
|
66
66
|
|
|
67
|
-
`))}let B=F.choices?.[0];if(F.usage){let N=F.usage?.prompt_tokens_details?.cached_tokens??F.usage?.prompt_cache_hit_tokens??0,Q=(F.usage?.prompt_tokens||0)-N,
|
|
67
|
+
`))}let B=F.choices?.[0];if(F.usage){let N=F.usage?.prompt_tokens_details?.cached_tokens??F.usage?.prompt_cache_hit_tokens??0,Q=(F.usage?.prompt_tokens||0)-N,Pe=F.usage?.completion_tokens||0;this.logger?.info({debug_log:!0,reqId:t.req.id,phase:"anthropic_transformer_usage",providerUsage:F.usage,mappedUsage:{input_tokens:Q,output_tokens:Pe,cache_read_input_tokens:N},choicesLength:Array.isArray(F.choices)?F.choices.length:void 0,finishReason:B?.finish_reason});let z={input_tokens:Q,output_tokens:Pe,cache_read_input_tokens:N};if(!a)a={type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{...z}};else{let re=a.usage||{};a.usage={input_tokens:z.input_tokens||re.input_tokens||0,output_tokens:z.output_tokens||re.output_tokens||0,cache_read_input_tokens:z.cache_read_input_tokens||re.cache_read_input_tokens||0}}}if(!B)continue;if(B?.delta?.thinking&&!_&&!d&&!u){if(!k){let N=v(),D={type:"content_block_start",index:N,content_block:{type:"thinking",thinking:""}};C(s.encode(`event: content_block_start
|
|
68
68
|
data: ${JSON.stringify(D)}
|
|
69
69
|
|
|
70
70
|
`)),b=N,k=!0}if(B.delta.thinking.signature){let N={type:"content_block_delta",index:b,delta:{type:"signature_delta",signature:B.delta.thinking.signature}};C(s.encode(`event: content_block_delta
|
|
@@ -79,7 +79,7 @@ data: ${JSON.stringify(N)}
|
|
|
79
79
|
`))}}if(B?.delta?.content&&!_&&!d){if(m++,b>=0&&!u){let D={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
80
80
|
data: ${JSON.stringify(D)}
|
|
81
81
|
|
|
82
|
-
`)),b=-1}if(!u&&!d){u=!0;let N=
|
|
82
|
+
`)),b=-1}if(!u&&!d){u=!0;let N=v(),D={type:"content_block_start",index:N,content_block:{type:"text",text:""}};C(s.encode(`event: content_block_start
|
|
83
83
|
data: ${JSON.stringify(D)}
|
|
84
84
|
|
|
85
85
|
`)),b=N}if(!_&&!d){let N={type:"content_block_delta",index:b,delta:{type:"text_delta",text:B.delta.content}};C(s.encode(`event: content_block_delta
|
|
@@ -88,16 +88,16 @@ data: ${JSON.stringify(N)}
|
|
|
88
88
|
`))}}if(B?.delta?.annotations?.length&&!_&&!d){if(b>=0&&u){let N={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
89
89
|
data: ${JSON.stringify(N)}
|
|
90
90
|
|
|
91
|
-
`)),b=-1,u=!1}B?.delta?.annotations.forEach(N=>{let D=
|
|
91
|
+
`)),b=-1,u=!1}B?.delta?.annotations.forEach(N=>{let D=v(),Q={type:"content_block_start",index:D,content_block:{type:"web_search_tool_result",tool_use_id:`srvtoolu_${(0,In.v4)()}`,content:[{type:"web_search_result",title:N.url_citation.title,url:N.url_citation.url}]}};C(s.encode(`event: content_block_start
|
|
92
92
|
data: ${JSON.stringify(Q)}
|
|
93
93
|
|
|
94
|
-
`));let
|
|
95
|
-
data: ${JSON.stringify(
|
|
94
|
+
`));let Pe={type:"content_block_stop",index:D};C(s.encode(`event: content_block_stop
|
|
95
|
+
data: ${JSON.stringify(Pe)}
|
|
96
96
|
|
|
97
|
-
`)),b=-1})}if(B?.delta?.tool_calls&&!_&&!d){f++;let N=new Set;for(let D of B.delta.tool_calls){if(_)break;let Q=D.index??0;if(N.has(Q))continue;if(N.add(Q),!g.has(Q)){if(b>=0){let
|
|
98
|
-
data: ${JSON.stringify(
|
|
97
|
+
`)),b=-1})}if(B?.delta?.tool_calls&&!_&&!d){f++;let N=new Set;for(let D of B.delta.tool_calls){if(_)break;let Q=D.index??0;if(N.has(Q))continue;if(N.add(Q),!g.has(Q)){if(b>=0){let fc={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
98
|
+
data: ${JSON.stringify(fc)}
|
|
99
99
|
|
|
100
|
-
`)),b=-1}let z=
|
|
100
|
+
`)),b=-1}let z=v();g.set(Q,z);let re=D.id||`call_ccr_${Q}`,J=D.function?.name||`tool_${Q}`,M={type:"content_block_start",index:z,content_block:{type:"tool_use",id:re,name:J,input:{}}};C(s.encode(`event: content_block_start
|
|
101
101
|
data: ${JSON.stringify(M)}
|
|
102
102
|
|
|
103
103
|
`)),b=z;let G={id:re,name:J,arguments:"",contentBlockIndex:z};p.set(Q,G)}else if(D.id&&D.function?.name){let z=p.get(Q);z.id.startsWith("call_")&&z.name.startsWith("tool_")&&(z.id=D.id,z.name=D.function.name)}if(D.function?.arguments&&!_&&!d){let z=g.get(Q);if(z===void 0)continue;let re=p.get(Q);re&&(re.arguments+=D.function.arguments);try{let J={type:"content_block_delta",index:z,delta:{type:"input_json_delta",partial_json:D.function.arguments}};C(s.encode(`event: content_block_delta
|
|
@@ -109,35 +109,35 @@ data: ${JSON.stringify(M)}
|
|
|
109
109
|
`))}catch(J){console.error(J)}}}}}if(B?.finish_reason&&!_&&!d){if(m===0&&f===0&&console.error("Warning: No content in the stream response!"),b>=0){let N={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
110
110
|
data: ${JSON.stringify(N)}
|
|
111
111
|
|
|
112
|
-
`)),b=-1}if(!_){let D={stop:"end_turn",length:"max_tokens",tool_calls:"tool_use",content_filter:"stop_sequence"}[B.finish_reason]||"end_turn";a?a.delta={...a.delta,stop_reason:D,stop_sequence:null}:a={type:"message_delta",delta:{stop_reason:D,stop_sequence:null},usage:{input_tokens:0,output_tokens:0,cache_read_input_tokens:0}}}d=!0}}catch(F){this.logger?.error(`parseError: ${F.name} message: ${F.message} stack: ${F.stack} data: ${L}`)}}}x()}catch(
|
|
113
|
-
`)||"",u={id:o.responseId,choices:[{finish_reason:o.candidates[0].finishReason?.toLowerCase()||null,index:0,message:{content:c,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,a=!1,l="",c=0,u=-1,d=new ReadableStream({async start(p){let g=async(f,_)=>{if(f.startsWith("data: ")){let k=f.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],
|
|
112
|
+
`)),b=-1}if(!_){let D={stop:"end_turn",length:"max_tokens",tool_calls:"tool_use",content_filter:"stop_sequence"}[B.finish_reason]||"end_turn";a?a.delta={...a.delta,stop_reason:D,stop_sequence:null}:a={type:"message_delta",delta:{stop_reason:D,stop_sequence:null},usage:{input_tokens:0,output_tokens:0,cache_read_input_tokens:0}}}d=!0}}catch(F){this.logger?.error(`parseError: ${F.name} message: ${F.message} stack: ${F.stack} data: ${L}`)}}}x()}catch(T){if(!_)try{r.error(T)}catch(E){console.error(E)}}finally{if(S)try{S.releaseLock()}catch(T){console.error(T)}}},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_${(0,In.v4)()}`;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(c=>({type:"web_search_result",url:c.url_citation.url,title:c.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 c={};try{let u=l.function.arguments||"{}";typeof u=="object"?c=u:typeof u=="string"&&(c=JSON.parse(u))}catch{c={text:l.function.arguments||""}}r.push({type:"tool_use",id:l.id,name:l.function.name,input:c})}),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,a={id:e.id?.startsWith("msg_")?e.id:`msg_${(0,In.v4)().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:a},"Conversion complete, final Anthropic response"),a}catch{throw K(`Provider error: ${JSON.stringify(e)}`,500,"provider_error")}}}});function Nl(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(it).includes(o)?o:it.TYPE_UNSPECIFIED}else{e.anyOf=[];for(let o of t){let r=o.toUpperCase();e.anyOf.push({type:Object.values(it).includes(r)?r:it.TYPE_UNSPECIFIED})}}}function Pt(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)&&Nl(n.type,e);for(let[i,a]of Object.entries(n))if(a!=null)if(i=="type"){if(a==="null")throw new Error("type: null can not be the only possible type for the field.");if(Array.isArray(a))continue;let l=a.toUpperCase();e.type=Object.values(it).includes(l)?l:it.TYPE_UNSPECIFIED}else if(t.includes(i))e[i]=Pt(a);else if(o.includes(i)){let l=[];for(let c of a){if(c.type=="null"){e.nullable=!0;continue}l.push(Pt(c))}e[i]=l}else if(r.includes(i)){let l={};for(let[c,u]of Object.entries(a))l[c]=Pt(u);e[i]=l}else{if(i==="additionalProperties")continue;e[i]=a}return e}function Ll(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=Pt(e.parameters)),e.response&&(Object.keys(e.response).includes("$schema")?e.responseJsonSchema||(e.responseJsonSchema=e.response,delete e.response):e.response=Pt(e.response));return n}function $n(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(Ll({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 c;l.role==="assistant"?c="model":(["user","system"].includes(l.role),c="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 p={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&&(p.thoughtSignature=l.thinking.signature),p})),u.length===0&&u.push({text:""}),r.push({role:c,parts:u}),c==="model"&&l.tool_calls){let d=l.tool_calls.map(p=>{let g=s.find(y=>y.tool_call_id===p.id);return{functionResponse:{name:p?.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],c,u=n.reasoning.max_tokens;typeof u<"u"&&(u>=l[0]&&u<=l[1]?c=u:u<l[0]?c=l[0]:u>l[1]&&(c=l[1]),i.thinkingConfig.thinkingBudget=c)}let a={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]),a.toolConfig=l}return a}function Nn(n){let e=n.contents,t=n.tools,o=n.model,r=n.max_tokens,s=n.temperature,i=n.stream,a=n.tool_choice,l={messages:[],model:o,max_tokens:r,temperature:s,stream:i,tool_choice:a};return Array.isArray(e)&&e.forEach(c=>{typeof c=="string"?l.messages.push({role:"user",content:c}):typeof c.text=="string"?l.messages.push({role:"user",content:c.text||null}):c.role==="user"?l.messages.push({role:"user",content:c?.parts?.map(u=>({type:"text",text:u.text||""}))||[]}):c.role==="model"&&l.messages.push({role:"assistant",content:c?.parts?.map(u=>({type:"text",text:u.text||""}))||[]})}),Array.isArray(t)&&(l.tools=[],t.forEach(c=>{Array.isArray(c.functionDeclarations)&&c.functionDeclarations.forEach(u=>{l.tools.push({type:"function",function:{name:u.name,description:u.description,parameters:u.parameters}})})})),l}async function Ln(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||[],a=[];for(let d of i)d.text&&d.thought===!0?r+=d.text:a.push(d);s=i.find(d=>d.thoughtSignature)?.thoughtSignature;let l=a?.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||{})}}))||[],c=a?.filter(d=>d.text)?.map(d=>d.text)?.join(`
|
|
113
|
+
`)||"",u={id:o.responseId,choices:[{finish_reason:o.candidates[0].finishReason?.toLowerCase()||null,index:0,message:{content:c,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,a=!1,l="",c=0,u=-1,d=new ReadableStream({async start(p){let g=async(f,_)=>{if(f.startsWith("data: ")){let k=f.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],v=b.content?.parts||[];v.filter(T=>T.text&&T.thought===!0).forEach(T=>{a||(a=!0);let E={choices:[{delta:{role:"assistant",content:null,thinking:{content:T.text}},finish_reason:null,index:c,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(E)}
|
|
114
114
|
|
|
115
|
-
`))});let C=
|
|
115
|
+
`))});let C=v.find(T=>T.thoughtSignature)?.thoughtSignature;if(C&&!s){if(!a){let E={choices:[{delta:{role:"assistant",content:null,thinking:{content:"(no content)"}},finish_reason:null,index:c,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(E)}
|
|
116
116
|
|
|
117
|
-
`))}let
|
|
117
|
+
`))}let T={choices:[{delta:{role:"assistant",content:null,thinking:{signature:C}},finish_reason:null,index:c,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:h.responseId||"",model:h.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};if(_.enqueue(r.encode(`data: ${JSON.stringify(T)}
|
|
118
118
|
|
|
119
119
|
`)),s=!0,c++,l){let E={choices:[{delta:{role:"assistant",content:l},finish_reason:null,index:c,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(E)}
|
|
120
120
|
|
|
121
|
-
`)),l="",i||(i=!0)}}let x=
|
|
122
|
-
`);if(!S&&s&&!i){let
|
|
121
|
+
`)),l="",i||(i=!0)}}let x=v.filter(T=>T.functionCall).map(T=>({id:T.functionCall?.id||`ccr_tool_${Math.random().toString(36).substring(2,15)}`,type:"function",function:{name:T.functionCall?.name,arguments:JSON.stringify(T.functionCall?.args||{})}})),S=v.filter(T=>T.text&&T.thought!==!0).map(T=>T.text).join(`
|
|
122
|
+
`);if(!S&&s&&!i){let T={choices:[{delta:{role:"assistant",content:"(no content)"},index:c,finish_reason:null,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(T)}
|
|
123
123
|
|
|
124
|
-
`)),i||(i=!0)}if(a&&S&&!s)if(h.modelVersion.includes("3")){l+=S;return}else{let
|
|
124
|
+
`)),i||(i=!0)}if(a&&S&&!s)if(h.modelVersion.includes("3")){l+=S;return}else{let T={choices:[{delta:{role:"assistant",content:null,thinking:{signature:`ccr_${+new Date}`}},finish_reason:null,index:c,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(T)}
|
|
125
125
|
|
|
126
|
-
`)),s=!0}if(S){l||c++;let
|
|
126
|
+
`)),s=!0}if(S){l||c++;let T={choices:[{delta:{role:"assistant",content:S},finish_reason:b.finishReason?.toLowerCase()||null,index:c,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&&(T.choices[0].delta.annotations=b.groundingMetadata.groundingChunks.map((E,R)=>{let A=b?.groundingMetadata?.groundingSupports?.filter(O=>O.groundingChunkIndices?.includes(R));return{type:"url_citation",url_citation:{url:E?.web?.uri||"",title:E?.web?.title||"",content:A?.[0]?.segment?.text||"",start_index:A?.[0]?.segment?.startIndex||0,end_index:A?.[0]?.segment?.endIndex||0}}})),_.enqueue(r.encode(`data: ${JSON.stringify(T)}
|
|
127
127
|
|
|
128
|
-
`)),!i&&S&&(i=!0)}x.length>0&&(x.forEach(
|
|
128
|
+
`)),!i&&S&&(i=!0)}x.length>0&&(x.forEach(T=>{c++,u++;let E={choices:[{delta:{role:"assistant",tool_calls:[{...T,index:u}]},finish_reason:b.finishReason?.toLowerCase()||null,index:c,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&&(E.choices[0].delta.annotations=b.groundingMetadata.groundingChunks.map((R,A)=>{let O=b?.groundingMetadata?.groundingSupports?.filter(I=>I.groundingChunkIndices?.includes(A));return{type:"url_citation",url_citation:{url:R?.web?.uri||"",title:R?.web?.title||"",content:O?.[0]?.segment?.text||"",start_index:O?.[0]?.segment?.startIndex||0,end_index:O?.[0]?.segment?.endIndex||0}}})),_.enqueue(r.encode(`data: ${JSON.stringify(E)}
|
|
129
129
|
|
|
130
130
|
`))}),!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:f,value:_}=await y.read();if(f){m&&await g(m,p);break}m+=o.decode(_,{stream:!0});let k=m.split(`
|
|
131
|
-
`);m=k.pop()||"";for(let h of k)await g(h,p)}}catch(f){p.error(f)}finally{p.close()}}});return new Response(d,{status:n.status,statusText:n.statusText,headers:n.headers})}}var it,
|
|
131
|
+
`);m=k.pop()||"";for(let h of k)await g(h,p)}}catch(f){p.error(f)}finally{p.close()}}});return new Response(d,{status:n.status,statusText:n.statusText,headers:n.headers})}}var it,or=w(()=>{"use strict";it={TYPE_UNSPECIFIED:"TYPE_UNSPECIFIED",STRING:"STRING",NUMBER:"NUMBER",INTEGER:"INTEGER",BOOLEAN:"BOOLEAN",ARRAY:"ARRAY",OBJECT:"OBJECT",NULL:"NULL"}});var Dn,gi=w(()=>{"use strict";or();Dn=class{name="gemini";endPoint="/v1beta/models/:modelAndAction";async transformRequestIn(e,t){return{body:$n(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=Nn;async transformResponseOut(e){return Ln(e,this.name,this.logger)}}});async function Dl(){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:
|
|
132
132
|
1. Set GOOGLE_APPLICATION_CREDENTIALS to point to service account key file
|
|
133
133
|
2. Run "gcloud auth application-default login"
|
|
134
|
-
3. Use Google Cloud environment with default service account`)}}var
|
|
134
|
+
3. Use Google Cloud environment with default service account`)}}var Fn,hi=w(()=>{"use strict";or();Fn=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 a=(await import("fs")).readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS,"utf8"),l=JSON.parse(a);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 Dl();return{body:$n(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 Nn(e)}async transformResponseOut(e){return Ln(e,this.name)}}});var Un,yi=w(()=>{"use strict";q();Un=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,a="";if(typeof r.content=="string"){let c=/<thinking>([\s\S]*?)<\/thinking>/g.exec(r.content);c&&(a=c[1].trim(),r.content=r.content.replace(c[0],"").trim())}s&&typeof s=="string"&&s.trim()?(r.reasoning_content=s,i&&(r.reasoning_content_signature=i)):a&&(r.reasoning_content=a),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 U(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="",a=new ReadableStream({async start(l){let c=e.body.getReader(),u=(p,g,y)=>{let m=p.split(`
|
|
135
135
|
`);for(let f of m)f.trim()&&g.enqueue(y.encode(f+`
|
|
136
136
|
`))},d=(p,g)=>{let{controller:y,encoder:m}=g;if(p.startsWith("data: ")&&p.trim()!=="data: [DONE]")try{let f=JSON.parse(p.slice(6));if(f.choices?.[0]?.delta?.reasoning_content){g.appendReasoningContent(f.choices[0].delta.reasoning_content);let _={...f,choices:[{...f.choices[0],delta:{...f.choices[0].delta,thinking:{content:f.choices[0].delta.reasoning_content}}}]};delete _.choices[0].delta.reasoning_content;let k=`data: ${JSON.stringify(_)}
|
|
137
137
|
|
|
138
138
|
`;y.enqueue(m.encode(k));return}if(f.choices?.[0]?.delta?.content&&g.reasoningContent()&&!g.isReasoningComplete()){g.setReasoningComplete(!0);let _="ccr_think_signature",k=f.choices[0].delta.content,h={...f,choices:[{...f.choices[0],delta:{...f.choices[0].delta,content:null,thinking:{content:g.reasoningContent(),signature:_}}}]};delete h.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(h)}
|
|
139
139
|
|
|
140
|
-
`;y.enqueue(m.encode(b));let
|
|
140
|
+
`;y.enqueue(m.encode(b));let v={...f,choices:[{...f.choices[0],delta:{...f.choices[0].delta,content:k}}]};delete v.choices[0].delta.reasoning_content;let C=`data: ${JSON.stringify(v)}
|
|
141
141
|
|
|
142
142
|
`;y.enqueue(m.encode(C));return}if(f.choices[0]?.delta?.reasoning_content&&delete f.choices[0].delta.reasoning_content,f.choices?.[0]?.delta&&Object.keys(f.choices[0].delta).length>0){let _=`data: ${JSON.stringify(f)}
|
|
143
143
|
|
|
@@ -145,13 +145,13 @@ data: ${JSON.stringify(N)}
|
|
|
145
145
|
`))}else y.enqueue(m.encode(p+`
|
|
146
146
|
`))};try{for(;;){let{done:p,value:g}=await c.read();if(p){i.trim()&&u(i,l,o);break}let y=t.decode(g,{stream:!0});i+=y;let m=i.split(`
|
|
147
147
|
`);i=m.pop()||"";for(let f of m)if(f.trim())try{d(f,{controller:l,encoder:o,reasoningContent:()=>r,appendReasoningContent:_=>r+=_,isReasoningComplete:()=>s,setReasoningComplete:_=>s=_})}catch(_){console.error("Error processing line:",f,_),l.enqueue(o.encode(f+`
|
|
148
|
-
`))}}}catch(p){console.error("Stream error:",p),l.error(p)}finally{try{c.releaseLock()}catch(p){console.error("Error releasing reader lock:",p)}l.close()}}});return new Response(a,{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
|
|
148
|
+
`))}}}catch(p){console.error("Stream error:",p),l.error(p)}finally{try{c.releaseLock()}catch(p){console.error("Error releasing reader lock:",p)}l.close()}}});return new Response(a,{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 jn,_i=w(()=>{"use strict";q();jn=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.
|
|
149
149
|
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.
|
|
150
150
|
Examples:
|
|
151
151
|
1. Task: "Use a tool to summarize this document" \u2014 Do not use ExitTool if a summarization tool is available.
|
|
152
152
|
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 U(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="",a=new ReadableStream({async start(l){let c=e.body.getReader(),u=(p,g,y)=>{let m=p.split(`
|
|
153
153
|
`);for(let f of m)f.trim()&&g.enqueue(y.encode(f+`
|
|
154
|
-
`))},d=(p,g)=>{let{controller:y,encoder:m,exitToolIndex:f,setExitToolIndex:_,appendExitToolResponse:k}=g;if(p.startsWith("data: ")&&p.trim()!=="data: [DONE]")try{let h=JSON.parse(p.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(f()>-1&&b.index===f()&&b.function.arguments){k(b.function.arguments);try{let
|
|
154
|
+
`))},d=(p,g)=>{let{controller:y,encoder:m,exitToolIndex:f,setExitToolIndex:_,appendExitToolResponse:k}=g;if(p.startsWith("data: ")&&p.trim()!=="data: [DONE]")try{let h=JSON.parse(p.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(f()>-1&&b.index===f()&&b.function.arguments){k(b.function.arguments);try{let v=JSON.parse(g.exitToolResponse());h.choices=[{delta:{role:"assistant",content:v.response||""}}];let C=`data: ${JSON.stringify(h)}
|
|
155
155
|
|
|
156
156
|
`;y.enqueue(m.encode(C))}catch{}return}}if(h.choices?.[0]?.delta&&Object.keys(h.choices[0].delta).length>0){let b=`data: ${JSON.stringify(h)}
|
|
157
157
|
|
|
@@ -159,27 +159,27 @@ Examples:
|
|
|
159
159
|
`))}else y.enqueue(m.encode(p+`
|
|
160
160
|
`))};try{for(;;){let{done:p,value:g}=await c.read();if(p){i.trim()&&u(i,l,o);break}let y=t.decode(g,{stream:!0});i+=y;let m=i.split(`
|
|
161
161
|
`);i=m.pop()||"";for(let f of m)if(f.trim())try{d(f,{controller:l,encoder:o,exitToolIndex:()=>r,setExitToolIndex:_=>r=_,exitToolResponse:()=>s,appendExitToolResponse:_=>s+=_})}catch(_){console.error("Error processing line:",f,_),l.enqueue(o.encode(f+`
|
|
162
|
-
`))}}}catch(p){console.error("Stream error:",p),l.error(p)}finally{try{c.releaseLock()}catch(p){console.error("Error releasing reader lock:",p)}l.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
|
|
162
|
+
`))}}}catch(p){console.error("Stream error:",p),l.error(p)}finally{try{c.releaseLock()}catch(p){console.error("Error releasing reader lock:",p)}l.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 ki,Hn,bi=w(()=>{"use strict";q();ki=require("uuid"),Hn=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 U(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,a=!1,l="",c=new ReadableStream({async start(u){let d=e.body.getReader(),p=(y,m,f)=>{let _=y.split(`
|
|
163
163
|
`);for(let k of _)k.trim()&&m.enqueue(f.encode(k+`
|
|
164
164
|
`))},g=(y,m)=>{let{controller:f,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:a},"usage"),h.choices[0].finish_reason=a?"tool_calls":"stop"),h.choices?.[0]?.finish_reason==="error"){f.enqueue(_.encode(`data: ${JSON.stringify({error:h.choices?.[0].error})}
|
|
165
165
|
|
|
166
|
-
`));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
|
|
166
|
+
`));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 T={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,thinking:{content:b}}}]};T.choices?.[0]?.delta&&(delete T.choices[0].delta.reasoning,delete T.choices[0].delta.reasoning_content);let E=`data: ${JSON.stringify(T)}
|
|
167
167
|
|
|
168
|
-
`;f.enqueue(_.encode(E));return}if(h.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let
|
|
168
|
+
`;f.enqueue(_.encode(E));return}if(h.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let T="ccr_think_signature",E=h.choices[0].delta.content,R={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:T}}}]};R.choices?.[0]?.delta&&delete R.choices[0].delta.reasoning;let A=`data: ${JSON.stringify(R)}
|
|
169
169
|
|
|
170
170
|
`;f.enqueue(_.encode(A));let O={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:E}}]};O.choices?.[0]?.delta&&(delete O.choices[0].delta.reasoning,delete O.choices[0].delta.reasoning_content);let I=`data: ${JSON.stringify(O)}
|
|
171
171
|
|
|
172
|
-
`;f.enqueue(_.encode(I));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(
|
|
172
|
+
`;f.enqueue(_.encode(I));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(T=>{T.id=`call_${(0,ki.v4)()}`}),h.choices?.[0]?.delta?.tool_calls?.length&&!a&&(a=!0);let v=h.choices?.[0]?.delta,C=v&&Object.keys(v).length>0,x=h.choices?.[0]?.finish_reason,S=h.usage;if(C||x||S){let T=`data: ${JSON.stringify(h)}
|
|
173
173
|
|
|
174
|
-
`;f.enqueue(_.encode(
|
|
174
|
+
`;f.enqueue(_.encode(T))}}catch{f.enqueue(_.encode(y+`
|
|
175
175
|
`))}}else f.enqueue(_.encode(y+`
|
|
176
176
|
`))};try{for(;;){let{done:y,value:m}=await d.read();if(y){l.trim()&&p(l,u,o);break}if(!m||m.length===0)continue;let f;try{f=t.decode(m,{stream:!0})}catch(k){console.warn("Failed to decode chunk",k);continue}if(f.length===0)continue;if(l+=f,l.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let k=l.split(`
|
|
177
177
|
`);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+`
|
|
178
178
|
`))}continue}let _=l.split(`
|
|
179
179
|
`);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+`
|
|
180
|
-
`))}}}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(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var
|
|
180
|
+
`))}}}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(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var zn,Ti=w(()=>{"use strict";zn=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}}});var vi,Bn,xi=w(()=>{"use strict";q();vi=require("uuid"),Bn=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 U(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,a="",l=new ReadableStream({async start(c){let u=e.body.getReader(),d=(g,y,m)=>{let f=g.split(`
|
|
181
181
|
`);for(let _ of f)_.trim()&&y.enqueue(m.encode(_+`
|
|
182
|
-
`))},p=(g,y)=>{let{controller:m,encoder:f}=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_${(0,
|
|
182
|
+
`))},p=(g,y)=>{let{controller:m,encoder:f}=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_${(0,vi.v4)()}`}),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)}
|
|
183
183
|
|
|
184
184
|
`;m.enqueue(f.encode(h))}catch{m.enqueue(f.encode(g+`
|
|
185
185
|
`))}}else m.enqueue(f.encode(g+`
|
|
@@ -187,21 +187,21 @@ Examples:
|
|
|
187
187
|
`);a=_.pop()||"";for(let k of _)if(k.trim())try{p(k,{controller:c,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),c.enqueue(o.encode(k+`
|
|
188
188
|
`))}continue}let f=a.split(`
|
|
189
189
|
`);a=f.pop()||"";for(let _ of f)if(_.trim())try{p(_,{controller:c,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),c.enqueue(o.encode(_+`
|
|
190
|
-
`))}}}catch(g){console.error("Stream error:",g),c.error(g)}finally{try{u.releaseLock()}catch(g){console.error("Error releasing reader lock:",g)}c.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
|
|
190
|
+
`))}}}catch(g){console.error("Stream error:",g),c.error(g)}finally{try{u.releaseLock()}catch(g){console.error("Error releasing reader lock:",g)}c.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 Jn,Ci=w(()=>{"use strict";Jn=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}}});function rr(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=Si.default.parse(n);return e?.debug("Tool arguments JSON5 parsing successful"),JSON.stringify(o)}catch(o){try{let r=(0,Ri.jsonrepair)(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 Si,Ri,wi=w(()=>{Si=V(require("json5")),Ri=require("jsonrepair")});var Wn,Ei=w(()=>{"use strict";wi();q();Wn=class{name="enhancetool";async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await U(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=rr(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="",a=!1,l=!1,c="",u=new ReadableStream({async start(d){let p=e.body.getReader(),g=(f,_,k)=>{let h=f.split(`
|
|
191
191
|
`);for(let b of h)b.trim()&&_.enqueue(k.encode(b+`
|
|
192
|
-
`))},y=(f,_,k)=>{let h="";try{h=
|
|
192
|
+
`))},y=(f,_,k)=>{let h="";try{h=rr(r.arguments||"",this.logger)}catch(x){console.error(`${x.message} ${x.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"}]},v={...f,choices:[{...f.choices[0],delta:b}]};v.choices[0].delta.content!==void 0&&delete v.choices[0].delta.content;let C=`data: ${JSON.stringify(v)}
|
|
193
193
|
|
|
194
|
-
`;_.enqueue(k.encode(C))},m=(f,_)=>{let{controller:k,encoder:h}=_;if(f.startsWith("data: ")&&f.trim()!=="data: [DONE]"){let b=f.slice(6);try{let
|
|
194
|
+
`;_.enqueue(k.encode(C))},m=(f,_)=>{let{controller:k,encoder:h}=_;if(f.startsWith("data: ")&&f.trim()!=="data: [DONE]"){let b=f.slice(6);try{let v=JSON.parse(b);if(v.choices?.[0]?.delta?.tool_calls?.length){let x=v.choices[0].delta.tool_calls[0];if(typeof r.index>"u"){r={index:x.index,name:x.function?.name||"",id:x.id||"",arguments:x.function?.arguments||""},x.function?.arguments&&(x.function.arguments="");let S=`data: ${JSON.stringify(v)}
|
|
195
195
|
|
|
196
|
-
`;k.enqueue(h.encode(S));return}else if(r.index===x.index){x.function?.arguments&&(r.arguments+=x.function.arguments);return}else{y(
|
|
196
|
+
`;k.enqueue(h.encode(S));return}else if(r.index===x.index){x.function?.arguments&&(r.arguments+=x.function.arguments);return}else{y(v,k,h),r={index:x.index,name:x.function?.name||"",id:x.id||"",arguments:x.function?.arguments||""};return}}if(v.choices?.[0]?.finish_reason==="tool_calls"&&r.index!==void 0){y(v,k,h),r={};return}v.choices?.[0]?.delta?.tool_calls?.length&&_.hasTextContent()&&(typeof v.choices[0].index=="number"?v.choices[0].index+=1:v.choices[0].index=1);let C=`data: ${JSON.stringify(v)}
|
|
197
197
|
|
|
198
198
|
`;k.enqueue(h.encode(C))}catch{k.enqueue(h.encode(f+`
|
|
199
199
|
`))}}else k.enqueue(h.encode(f+`
|
|
200
200
|
`))};try{for(;;){let{done:f,value:_}=await p.read();if(f){c.trim()&&g(c,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(c+=k,c.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let b=c.split(`
|
|
201
|
-
`);c=b.pop()||"";for(let
|
|
201
|
+
`);c=b.pop()||"";for(let v of b)if(v.trim())try{m(v,{controller:d,encoder:o,hasTextContent:()=>s,setHasTextContent:C=>s=C,reasoningContent:()=>i,appendReasoningContent:C=>i+=C,isReasoningComplete:()=>a,setReasoningComplete:C=>a=C})}catch(C){console.error("Error processing line:",v,C),d.enqueue(o.encode(v+`
|
|
202
202
|
`))}continue}let h=c.split(`
|
|
203
|
-
`);c=h.pop()||"";for(let b of h)if(b.trim())try{m(b,{controller:d,encoder:o,hasTextContent:()=>s,setHasTextContent:
|
|
204
|
-
`))}}}catch(f){console.error("Stream error:",f),d.error(f)}finally{try{p.releaseLock()}catch(f){console.error("Error releasing reader lock:",f)}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
|
|
203
|
+
`);c=h.pop()||"";for(let b of h)if(b.trim())try{m(b,{controller:d,encoder:o,hasTextContent:()=>s,setHasTextContent:v=>s=v,reasoningContent:()=>i,appendReasoningContent:v=>i+=v,isReasoningComplete:()=>a,setReasoningComplete:v=>a=v})}catch(v){console.error("Error processing line:",b,v),d.enqueue(o.encode(b+`
|
|
204
|
+
`))}}}catch(f){console.error("Stream error:",f),d.error(f)}finally{try{p.releaseLock()}catch(f){console.error("Error releasing reader lock:",f)}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 Gn,Pi=w(()=>{"use strict";q();Et();Gn=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??An(e.reasoning.effort||"medium")},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 U(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="",a=new ReadableStream({async start(l){let c=e.body.getReader(),u=(p,g,y)=>{let m=p.split(`
|
|
205
205
|
`);for(let f of m)f.trim()&&g.enqueue(y.encode(f+`
|
|
206
206
|
`))},d=(p,g)=>{let{controller:y,encoder:m}=g;if(this.logger?.debug({line:p},"Processing reason line"),p.startsWith("data: ")&&p.trim()!=="data: [DONE]")try{let f=JSON.parse(p.slice(6));if(console.log(JSON.stringify(f)),f.choices?.[0]?.delta?.reasoning_content){g.appendReasoningContent(f.choices[0].delta.reasoning_content);let _={...f,choices:[{...f.choices[0],delta:{...f.choices[0].delta,thinking:{content:f.choices[0].delta.reasoning_content}}}]};delete _.choices[0].delta.reasoning_content;let k=`data: ${JSON.stringify(_)}
|
|
207
207
|
|
|
@@ -213,7 +213,7 @@ Examples:
|
|
|
213
213
|
`))}else y.enqueue(m.encode(p+`
|
|
214
214
|
`))};try{for(;;){let{done:p,value:g}=await c.read();if(p){i.trim()&&u(i,l,o);break}let y=t.decode(g,{stream:!0});i+=y;let m=i.split(`
|
|
215
215
|
`);i=m.pop()||"";for(let f of m)if(f.trim())try{d(f,{controller:l,encoder:o,reasoningContent:()=>r,appendReasoningContent:_=>r+=_,isReasoningComplete:()=>s,setReasoningComplete:_=>s=_})}catch(_){console.error("Error processing line:",f,_),l.enqueue(o.encode(f+`
|
|
216
|
-
`))}}}catch(p){console.error("Stream error:",p),l.error(p)}finally{try{c.releaseLock()}catch(p){console.error("Error releasing reader lock:",p)}l.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
|
|
216
|
+
`))}}}catch(p){console.error("Stream error:",p),l.error(p)}finally{try{c.releaseLock()}catch(p){console.error("Error releasing reader lock:",p)}l.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 Kn,Oi=w(()=>{"use strict";Kn=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 Qn,Ai=w(()=>{"use strict";Qn=class{static TransformerName="maxcompletiontokens";async transformRequestIn(e){return e.max_tokens&&(e.max_completion_tokens=e.max_tokens,delete e.max_tokens),e}}});function Ii(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",a=[];typeof r.content=="string"?a.push({type:"text",text:r.content}):Array.isArray(r.content)&&r.content.forEach(l=>{l.type==="text"?a.push({type:"text",text:l.text||""}):l.type==="image_url"&&a.push({type:"image",source:{type:"base64",media_type:l.media_type||"image/jpeg",data:l.image_url.url}})}),!(!s&&a.length===0&&!r.tool_calls&&!r.content)&&(s&&i&&a.length===0&&r.tool_calls&&a.push({type:"text",text:""}),e.push({role:r.role==="assistant"?"assistant":"user",content:a}))}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 Mi(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 $i(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=(a,l)=>{if(a.startsWith("data: ")){let c=a.slice(6).trim();if(c){t?.debug({chunkStr:c},`${e} chunk:`);try{let u=JSON.parse(c);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)}
|
|
217
217
|
|
|
218
218
|
`))}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)}
|
|
219
219
|
|
|
@@ -226,18 +226,18 @@ Examples:
|
|
|
226
226
|
`));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)}
|
|
227
227
|
|
|
228
228
|
`))}}catch(u){t?.error(`Error parsing ${e} stream chunk`,c,u.message)}}}},i=new ReadableStream({async start(a){let l=n.body.getReader(),c="";try{for(;;){let{done:u,value:d}=await l.read();if(u){c&&s(c,a);break}c+=o.decode(d,{stream:!0});let p=c.split(`
|
|
229
|
-
`);c=p.pop()||"";for(let g of p)s(g,a)}}catch(u){a.error(u)}finally{a.close()}}});return new Response(i,{status:n.status,statusText:n.statusText,headers:n.headers})}return n}var
|
|
229
|
+
`);c=p.pop()||"";for(let g of p)s(g,a)}}catch(u){a.error(u)}finally{a.close()}}});return new Response(i,{status:n.status,statusText:n.statusText,headers:n.headers})}return n}var Ni=w(()=>{"use strict"});async function Fl(){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:
|
|
230
230
|
1. Set GOOGLE_APPLICATION_CREDENTIALS to point to service account key file
|
|
231
231
|
2. Run "gcloud auth application-default login"
|
|
232
|
-
3. Use Google Cloud environment with default service account`)}}var
|
|
232
|
+
3. Use Google Cloud environment with default service account`)}}var Vn,Li=w(()=>{"use strict";Ni();Vn=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 a=(await import("fs")).readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS,"utf8"),l=JSON.parse(a);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 Fl();return{body:Ii(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 Mi(e)}async transformResponseOut(e){return $i(e,this.name,this.logger)}}});var Yn,Di=w(()=>{"use strict";Yn=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 Xn,Fi=w(()=>{"use strict";Xn=class{name="streamoptions";async transformRequestIn(e){return e.stream&&(e.stream_options={include_usage:!0}),e}}});var Zn,Ui=w(()=>{"use strict";Zn=class{name="OpenAI";endPoint="/v1/chat/completions"}});var qn,ji=w(()=>{"use strict";qn=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}}});var Hi,eo,zi=w(()=>{"use strict";q();Hi=require("uuid"),eo=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 U(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,a=!1,l="",c=new ReadableStream({async start(u){let d=e.body.getReader(),p=(y,m,f)=>{let _=y.split(`
|
|
233
233
|
`);for(let k of _)k.trim()&&m.enqueue(f.encode(k+`
|
|
234
234
|
`))},g=(y,m)=>{let{controller:f,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:a},"usage"),h.choices[0].finish_reason=a?"tool_calls":"stop"),h.choices?.[0]?.finish_reason==="error"&&f.enqueue(_.encode(`data: ${JSON.stringify({error:h.choices?.[0].error})}
|
|
235
235
|
|
|
236
|
-
`)),h.choices?.[0]?.delta?.content&&!m.hasTextContent()&&m.setHasTextContent(!0),h.choices?.[0]?.delta?.reasoning){m.appendReasoningContent(h.choices[0].delta.reasoning);let
|
|
236
|
+
`)),h.choices?.[0]?.delta?.content&&!m.hasTextContent()&&m.setHasTextContent(!0),h.choices?.[0]?.delta?.reasoning){m.appendReasoningContent(h.choices[0].delta.reasoning);let v={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,thinking:{content:h.choices[0].delta.reasoning}}}]};v.choices?.[0]?.delta&&delete v.choices[0].delta.reasoning;let C=`data: ${JSON.stringify(v)}
|
|
237
237
|
|
|
238
238
|
`;f.enqueue(_.encode(C));return}if(h.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let C={...h,choices:[{...h.choices?.[0],delta:{...h.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:"ccr_think_signature"}}}]};C.choices?.[0]?.delta&&delete C.choices[0].delta.reasoning;let x=`data: ${JSON.stringify(C)}
|
|
239
239
|
|
|
240
|
-
`;f.enqueue(_.encode(x))}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(
|
|
240
|
+
`;f.enqueue(_.encode(x))}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(v=>{v.id=`call_${(0,Hi.v4)()}`}),h.choices?.[0]?.delta?.tool_calls?.length&&!a&&(a=!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)}
|
|
241
241
|
|
|
242
242
|
`;f.enqueue(_.encode(b))}catch{f.enqueue(_.encode(y+`
|
|
243
243
|
`))}}else f.enqueue(_.encode(y+`
|
|
@@ -245,22 +245,22 @@ Examples:
|
|
|
245
245
|
`);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+`
|
|
246
246
|
`))}continue}let _=l.split(`
|
|
247
247
|
`);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+`
|
|
248
|
-
`))}}}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(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var
|
|
248
|
+
`))}}}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(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var sr,Ul,to,Bi=w(()=>{"use strict";q();sr=`Always think before answering. Even if the problem seems simple, always write down your reasoning process explicitly.
|
|
249
249
|
|
|
250
250
|
Output format:
|
|
251
251
|
<reasoning_content>
|
|
252
252
|
Your detailed thinking process goes here
|
|
253
253
|
</reasoning_content>
|
|
254
|
-
Your final answer must follow after the closing tag above.`,
|
|
255
|
-
${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:
|
|
254
|
+
Your final answer must follow after the closing tag above.`,Ul=10,to=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<Ul)&&(t++,r.content=`<reasoning_content>${r.thinking.content}</reasoning_content>
|
|
255
|
+
${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:sr}):o.content=[{type:"text",text:sr},{type:"text",text:o.content||""}]),o.role==="tool"&&e.messages.push({role:"user",content:[{type:"text",text:sr}]}),e}async transformResponseOut(e){let t="<reasoning_content>",o="</reasoning_content>";if(e.headers.get("Content-Type")?.includes("application/json")){let r=await U(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,a=new ReadableStream({async start(l){let c=e.body.getReader(),u="",d="SEARCHING",p="",g="",y=(m,f)=>{if(typeof f!="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)}
|
|
256
256
|
|
|
257
257
|
`)));return}let _=p+f;for(p="";_.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))){p=_.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 C={...m.choices[0].delta,thinking:{content:h}};delete C.content;let x={...m,choices:[{...m.choices[0],delta:C,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(x)}
|
|
258
258
|
|
|
259
|
-
`))}let b={...m.choices[0].delta,thinking:{signature:"ccr_think_signature"}};delete b.content;let
|
|
259
|
+
`))}let b={...m.choices[0].delta,thinking:{signature:"ccr_think_signature"}};delete b.content;let v={...m,choices:[{...m.choices[0],delta:b,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(v)}
|
|
260
260
|
|
|
261
|
-
`)),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))){p=_.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
|
|
261
|
+
`)),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))){p=_.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 v={...m,choices:[{...m.choices[0],delta:b,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(v)}
|
|
262
262
|
|
|
263
|
-
`))}_=""}}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
|
|
263
|
+
`))}_=""}}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 v={...m,choices:[{...m.choices[0],delta:b}]};l.enqueue(i.encode(`data: ${JSON.stringify(v)}
|
|
264
264
|
|
|
265
265
|
`)),g=""}r++,_=""}};try{for(;;){let{done:m,value:f}=await c.read();if(m)break;let _=s.decode(f,{stream:!0});u+=_;let k=u.split(`
|
|
266
266
|
`);u=k.pop()||"";for(let h of k)if(h.trim()){if(h.trim()==="data: [DONE]"){l.enqueue(i.encode(h+`
|
|
@@ -269,7 +269,7 @@ ${r.content}`),delete r.thinking)});let o=e.messages[e.messages.length-1];return
|
|
|
269
269
|
`))}else l.enqueue(i.encode(h+`
|
|
270
270
|
`))}}}catch(m){console.error("Stream error:",m),l.error(m)}finally{try{c.releaseLock()}catch(m){console.error("Error releasing reader lock:",m)}if(d==="REASONING"){let f={choices:[{delta:{thinking:{signature:"ccr_think_signature"}}}]};l.enqueue(i.encode(`data: ${JSON.stringify(f)}
|
|
271
271
|
|
|
272
|
-
`))}l.close()}}});return new Response(a,{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
|
|
272
|
+
`))}l.close()}}});return new Response(a,{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 no,Ji=w(()=>{"use strict";q();no=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="",a=new ReadableStream({async start(l){let c=e.body.getReader();try{for(;;){let{done:u,value:d}=await c.read();if(u){i.trim()&&l.enqueue(r.encode(i+`
|
|
273
273
|
`));break}let p=o.decode(d,{stream:!0});i+=p;let g=i.split(`
|
|
274
274
|
`);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 f={...m,choices:[{...m.choices[0],delta:{thinking:{content:m.choices[0].delta.reasoning_content}}}]};delete f.choices[0].delta.reasoning_content,l.enqueue(r.encode(`data: ${JSON.stringify(f)}
|
|
275
275
|
|
|
@@ -279,17 +279,17 @@ ${r.content}`),delete r.thinking)});let o=e.messages[e.messages.length-1];return
|
|
|
279
279
|
|
|
280
280
|
`))}catch{l.enqueue(r.encode(y+`
|
|
281
281
|
`))}else l.enqueue(r.encode(y+`
|
|
282
|
-
`))}}catch(u){l.error(u)}finally{try{c.releaseLock()}catch{}l.close()}}});return new Response(a,{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 U(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}}});var
|
|
282
|
+
`))}}catch(u){l.error(u)}finally{try{c.releaseLock()}catch{}l.close()}}});return new Response(a,{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 U(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}}});var ir,oo,Wi=w(()=>{"use strict";q();qe();ir=require("uuid"),oo=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 a=Object.keys(r).find(c=>c.toLowerCase()===i),l=a?r[a]:void 0;return typeof l=="string"&&l.trim()?l.trim():void 0};return s("x-opencode-session")||o?.clientContext?.stableSessionId||o?.sessionId||Te(e.metadata?.user_id)||s("session_id")||(0,ir.v4)()}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,a=s.thinking?.signature;i&&typeof i=="string"&&i.trim()?(s.reasoning_content=i,a&&(s.reasoning_content_signature=a)):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 U(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,a=!1,l="",c=new ReadableStream({async start(u){let d=e.body.getReader(),p=(y,m,f)=>{let _=y.split(`
|
|
283
283
|
`);for(let k of _)k.trim()&&m.enqueue(f.encode(k+`
|
|
284
284
|
`))},g=(y,m)=>{let{controller:f,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=a?"tool_calls":"stop"),k.choices?.[0]?.finish_reason==="error"){f.enqueue(_.encode(`data: ${JSON.stringify({error:k.choices?.[0]?.error})}
|
|
285
285
|
|
|
286
286
|
`));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),f.enqueue(_.encode(`data: ${JSON.stringify(S)}
|
|
287
287
|
|
|
288
|
-
`));return}if(k.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let S="ccr_think_signature",
|
|
288
|
+
`));return}if(k.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let S="ccr_think_signature",T=k.choices[0].delta.content,E={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:S}}}]};E.choices?.[0]?.delta&&(delete E.choices[0].delta.reasoning,delete E.choices[0].delta.reasoning_content),f.enqueue(_.encode(`data: ${JSON.stringify(E)}
|
|
289
289
|
|
|
290
|
-
`));let R={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:
|
|
290
|
+
`));let R={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:T}}]};R.choices?.[0]?.delta&&(delete R.choices[0].delta.reasoning,delete R.choices[0].delta.reasoning_content),f.enqueue(_.encode(`data: ${JSON.stringify(R)}
|
|
291
291
|
|
|
292
|
-
`));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_${(0,
|
|
292
|
+
`));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_${(0,ir.v4)()}`}),k.choices?.[0]?.delta?.tool_calls?.length&&!a&&(a=!0);let b=k.choices?.[0]?.delta,v=b&&Object.keys(b).length>0,C=k.choices?.[0]?.finish_reason,x=k.usage;(v||C||x)&&f.enqueue(_.encode(`data: ${JSON.stringify(k)}
|
|
293
293
|
|
|
294
294
|
`))}catch{f.enqueue(_.encode(y+`
|
|
295
295
|
`))}else f.enqueue(_.encode(y+`
|
|
@@ -297,22 +297,22 @@ ${r.content}`),delete r.thinking)});let o=e.messages[e.messages.length-1];return
|
|
|
297
297
|
`);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+`
|
|
298
298
|
`))}continue}let _=l.split(`
|
|
299
299
|
`);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+`
|
|
300
|
-
`))}}}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(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});function
|
|
300
|
+
`))}}}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(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});function Hl(n){if(typeof n=="number")return Number.isFinite(n)?n:void 0;if(typeof n!="string")return;let e=n.trim();if(!e)return;let t=e.toLowerCase();return["none","low","medium","high","max"].includes(t)?t:/^\d+$/.test(e)?parseInt(e,10):e}function zl(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 jl,ro,Gi=w(()=>{"use strict";Et();jl={min:"low",minimal:"low",light:"low",xhigh:"max",maximum:"max",ultra:"max",ultracode:"max"};ro=class{constructor(e){this.options=e}static TransformerName="defaultthinking";async transformRequestIn(e,t){let o=Hl(this.options?.level??this.options?.value);if(o===void 0||o==="none"||e.reasoning)return e;let r=zl(t?.baseUrl);if(typeof o=="number"){if(o<=0)return e;if(r==="anthropic")e.reasoning={enabled:!0,max_tokens:Math.max(1024,Math.floor(o))};else{let i=st(o);r==="chat"?e.reasoning_effort=i:e.reasoning={enabled:!0,effort:i}}return e}if(o==="low"||o==="medium"||o==="high"||o==="max")return r==="chat"?e.reasoning_effort=o:e.reasoning={enabled:!0,effort:o},e;let s=jl[o.toLowerCase()];return s?(r==="anthropic"?e.reasoning={enabled:!0,effort:s}:r==="chat"?e.reasoning_effort=o:e.reasoning={enabled:!0,effort:o},e):(r==="chat"?e.reasoning_effort=o:r==="responses"&&(e.reasoning={enabled:!0,effort:o}),e)}}});var Ki,Qi=w(()=>{mi();gi();hi();yi();_i();bi();Ti();xi();Ci();Ei();Pi();Oi();Ai();Li();Di();Fi();Ui();ji();zi();Jo();Bi();Ji();Wi();Gi();Ki={AnthropicTransformer:Mn,GeminiTransformer:Dn,VertexGeminiTransformer:Fn,VertexClaudeTransformer:Vn,DeepseekTransformer:Un,TooluseTransformer:jn,OpenrouterTransformer:Hn,OpenAITransformer:Zn,MaxTokenTransformer:zn,GroqTransformer:Bn,CleancacheTransformer:Jn,EnhanceToolTransformer:Wn,ReasoningTransformer:Gn,SamplingTransformer:Kn,MaxCompletionTokens:Qn,CerebrasTransformer:Yn,StreamOptionsTransformer:Xn,CustomParamsTransformer:qn,VercelTransformer:eo,OpenAIResponsesTransformer:Ve,ForceReasoningTransformer:to,KimiTransformer:no,OpenCodeTransformer:oo,DefaultThinkingTransformer:ro}});var Ne,ar=w(()=>{"use strict";Qi();Ne=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=require(require.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})
|
|
301
301
|
error: ${t.message}
|
|
302
302
|
stack: ${t.stack}`),!1}}async initialize(){try{await this.registerDefaultTransformersInternal(),await this.loadFromConfig()}catch(e){this.logger.error(`TransformerService init error: ${e.message}
|
|
303
|
-
Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(Ni).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)}}});var Di,Ot,Fi=w(()=>{"use strict";Di=require("tiktoken"),Ot=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=(0,Di.get_encoding)(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(a=>{typeof a.content=="string"?o+=t.encode(a.content).length:Array.isArray(a.content)&&a.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 c=typeof l.content=="string"?l.content:JSON.stringify(l.content);o+=t.encode(c).length}})}),typeof s=="string"?o+=t.encode(s).length:Array.isArray(s)&&s.forEach(a=>{a.type==="text"&&(typeof a.text=="string"?o+=t.encode(a.text).length:Array.isArray(a.text)&&a.text.forEach(l=>{o+=t.encode(l||"").length}))}),i&&i.forEach(a=>{a.description&&(o+=t.encode(a.name+a.description).length),a.input_schema&&(o+=t.encode(JSON.stringify(a.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)}}});var At,Ui,at,It,ji,no,Hi=w(()=>{"use strict";At=require("path"),Ui=require("os"),at=require("fs"),It=require("fs"),ji=require("@huggingface/tokenizers"),no=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||(0,At.join)((0,Ui.homedir)(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=(0,At.join)(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:(0,At.join)(e,"tokenizer.json"),tokenizerConfig:(0,At.join)(e,"tokenizer_config.json")}}ensureDir(e){(0,at.existsSync)(e)||(0,at.mkdirSync)(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!(0,at.existsSync)(e.tokenizerJson)||!(0,at.existsSync)(e.tokenizerConfig))return null;let[t,o]=await Promise.all([It.promises.readFile(e.tokenizerJson,"utf-8"),It.promises.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[a,l]=await Promise.all([s.json(),i.ok?i.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([It.promises.writeFile(e.tokenizerJson,JSON.stringify(a,null,2)),It.promises.writeFile(e.tokenizerConfig,JSON.stringify(l,null,2))]),{tokenizerJson:a,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 ji.Tokenizer(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 a of i.content)a.type==="text"&&a.text?t.push(a.text):a.type==="tool_use"&&a.input?t.push(JSON.stringify(a.input)):a.type==="tool_result"&&t.push(typeof a.content=="string"?a.content:JSON.stringify(a.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 a of i.text)a&&t.push(a)}}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 oo,zi=w(()=>{"use strict";ce();oo=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},a=this.options.resolveProxyUrl?.();a&&(i.dispatcher=Y(a));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 c=await l.json();return this.extractTokenCount(c)}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 Le,er=w(()=>{"use strict";Fi();Hi();zi();ce();Le=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 Ot("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 Ot(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),r=new no(e.model,this.logger,{timeout:this.options.timeout});break;case"api":r=new oo(e,this.logger,{timeout:this.options.timeout,resolveProxyUrl:()=>{let s=t?this.getRawProvider(t):void 0;return fe(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 Bi(n){if(!n||typeof n!="object"||Il(n))return null;for(let t of Al(n)){let o=Ol(t);if(o)return o}return null}function Ol(n){if(!n||typeof n!="object")return null;let e={},t=ro(n,Rl);if(t!==void 0){let i=Ji(t);e.usedDailyBalance=i,e.limitDaily=100}let o=ro(n,wl);if(o!==void 0){let i=Ji(o);e.usedBalance=i,e.totalBalance=100}let r=ro(n,Pl);if(r!==void 0){let i=Wi(r);i&&(e.resetTime=i)}let s=ro(n,El);if(s!==void 0){let i=Wi(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 Ji(n){return!Number.isFinite(n)||n<=0?0:Math.round(n*1e4)/100}function ro(n,e){for(let t of e){let o=$(n?.[t]);if(o!==void 0)return o}}function Al(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 Il(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 Wi(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 so(n){let e=pr(n);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?jl:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?Hl:e==="deepseek.com"||e.endsWith(".deepseek.com")?Ml:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?$l:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?Nl:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?Ll:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?Dl:e==="maas.aliyuncs.com"||e.endsWith(".maas.aliyuncs.com")?Fl: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 zl(n){let e=pr(n);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function Bl(n,e){let t=ur(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,a=String(i?.apiKey||i?.api_key||"").trim();return a&&o.some(l=>l.includes(a)||a.includes(l))})||t[0]}function ur(n){return!n||typeof n!="object"?[]:!Array.isArray(n)&&n.codingPlanUsageDTO?[n]:Array.isArray(n)?n.flatMap(e=>ur(e)):Object.values(n).flatMap(e=>ur(e))}function dr(n,e){let t=n.match(new RegExp(`(?:^|;\\s*)${e}=([^;]+)`));return t?decodeURIComponent(t[1]):void 0}function pr(n){try{return new URL(n).hostname.toLowerCase()}catch{return null}}function $(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 de,tr,nr,or,rr,sr,Rl,wl,Pl,El,ir,ar,cr,lr,Ml,$l,Nl,Ll,Dl,Fl,Ul,jl,Hl,fr=w(()=>{"use strict";ce();de=class{async fetchJson(e,t,o,r,s){let i=s||t.apiKey;if(!i)return null;let a={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${i}`},signal:AbortSignal.timeout(o)};try{r&&(a.dispatcher=Y(r));let l=await fetch(e,a);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}},tr=class extends de{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=>$(l?.total_balance)!==void 0);if(!i)return null;let a=$(i.total_balance);return a===void 0?null:{totalBalance:a,currency:typeof i.currency=="string"?i.currency:void 0}}},nr=class extends de{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={},a=$(s.limit),l=$(s.limit_remaining),c=$(s.usage),u=$(s.usage_daily);return a!==void 0&&(i.totalBalance=a),l!==void 0&&(i.remainingBalance=l),c!==void 0&&(i.usedBalance=c),u!==void 0&&(i.usedDailyBalance=u),this.hasQuotaData(i)?i:null}},or=class extends de{async queryQuota(e,t,o){let r=zl(e.baseUrl),i=(await this.fetchJson(r,e,t,o))?.data;if(!i||typeof i!="object")return null;let a={},l=$(i.totalBalance),c=$(i.balance);return l!==void 0&&(a.totalBalance=l),c!==void 0&&(a.remainingBalance=c),l!==void 0&&c!==void 0&&(a.usedBalance=Math.max(0,l-c)),this.hasQuotaData(a)?a:null}},rr=class extends de{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=Y(o));let i=await fetch("https://api.z.ai/api/monitor/usage/quota/limit",s);if(!i.ok)return null;let a=await i.json(),l=Array.isArray(a?.data?.limits)?a.data.limits:[],c=null,u=null,d=null;for(let g of l)g.type==="TOKENS_LIMIT"?g.unit===3?c=g:g.unit===6?u=g:c||(c=g):g.type==="TIME_LIMIT"&&(d=g);let p={};if(c){let g=$(c.currentValue),y=$(c.usage),m=$(c.remaining),f=$(c.percentage);g!==void 0?p.usedDailyBalance=g:f!==void 0&&(p.usedDailyBalance=f,p.limitDaily=100),y!==void 0?p.limitDaily=y:g!==void 0&&m!==void 0&&(p.limitDaily=g+m),c.nextResetTime&&(p.resetTime=new Date(c.nextResetTime).toISOString())}if(u){let g=$(u.currentValue),y=$(u.usage),m=$(u.remaining),f=$(u.percentage);g!==void 0?p.usedBalance=g:f!==void 0&&(p.usedBalance=f,p.totalBalance=100),y!==void 0?p.totalBalance=y:g!==void 0&&m!==void 0&&(p.totalBalance=g+m),u.nextResetTime&&!p.resetTime&&(p.resetTime=new Date(u.nextResetTime).toISOString())}if(d){if(p.usedDailyBalance===void 0){let g=$(d.currentValue);g!==void 0&&(p.usedDailyBalance=g)}if(p.limitDaily===void 0){let g=$(d.usage),y=$(d.currentValue),m=$(d.remaining);g!==void 0?p.limitDaily=g:y!==void 0&&m!==void 0&&(p.limitDaily=y+m)}d.nextResetTime&&!p.resetTime&&(p.resetTime=new Date(d.nextResetTime).toISOString())}return this.hasQuotaData(p)?p:null}catch{return null}}},sr=class extends de{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=Y(o));let a=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(!a.ok)return null;let c=(await a.json())?.data?.DataV2?.data?.data?.codingPlanInstanceInfos;if(!Array.isArray(c)||c.length===0)return null;let u=c[0]?.codingPlanQuotaInfo;if(!u)return null;let d={},p=$(u.per5HourUsedQuota),g=$(u.per5HourTotalQuota);p!==void 0&&(d.usedDailyBalance=p),g!==void 0&&(d.limitDaily=g);let y=$(u.perWeekUsedQuota),m=$(u.perWeekTotalQuota);return y!==void 0&&(d.usedBalance=y),m!==void 0&&(d.totalBalance=m),this.hasQuotaData(d)?d:null}catch{return null}}};Rl=["per5HourPercentage","per5HourUsedPercentage","fiveHourPercentage"],wl=["per1WeekPercentage","perWeekPercentage","per1WeekUsedPercentage"],Pl=["per5HourResetTime","per5HourNextResetTime","fiveHourResetTime"],El=["per1WeekResetTime","per1WeekNextResetTime","perWeekResetTime"];ir=class extends de{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=dr(e,"cna")||"",a=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:a,region:"cn-beijing",sec_token:t}).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/cn-beijing/?tab=plan"},body:l,signal:AbortSignal.timeout(o)};try{r&&(c.dispatcher=Y(r));let u=await fetch(`https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=${encodeURIComponent(s)}&_v=undefined`,c);if(!u.ok)return null;let d=await u.json();return Bi(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(),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/"},body:i,signal:AbortSignal.timeout(t)};try{o&&(a.dispatcher=Y(o));let l=await fetch(`https://cs-data.qianwenai.com/data/api.json?product=sfm_bailian&action=BroadScopeAspnGateway&api=${encodeURIComponent(r)}`,a);if(!l.ok)return null;let c=await l.json();return Bi(c)}catch{return null}}},ar=class extends de{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:(dr(e.quotaToken,"atp-auth-token")||dr(e.quotaToken,"tenantToken")||e.quotaToken).trim(),Cookie:e.quotaToken},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=Y(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 a=await i.json();if(a?.succeed===!1||a?.failed===!0)return null;let l=Bl(a,e.apiKey),c=l?.codingPlanUsageDTO||l?.usage||l;if(!c||typeof c!="object")return null;let u={},d=$(c.rp5hUsage),p=$(c.rp5hLimit);d!==void 0&&(u.usedDailyBalance=d),p!==void 0&&(u.limitDaily=p);let g=$(c.rpwUsage),y=$(c.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}}},cr=class extends de{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=Y(o));let i=await fetch("https://api.kimi.com/coding/v1/usages",s);if(!i.ok)return null;let a=await i.json(),l={},c=Array.isArray(a?.limits)?a.limits:[];if(c.length>0&&c[0]?.detail){let d=c[0].detail,p=$(d.limit),g=$(d.remaining),y=d.resetTime;p!==void 0&&g!==void 0&&(l.usedDailyBalance=Math.max(0,p-g),l.limitDaily=p),y&&(l.resetTime=new Date(y).toISOString())}let u=a?.usage;if(u){let d=$(u.limit),p=$(u.remaining),g=$(u.used),y=u.resetTime;d!==void 0&&(l.totalBalance=d,g!==void 0?l.usedBalance=g:p!==void 0&&(l.usedBalance=Math.max(0,d-p),l.remainingBalance=p)),y&&(l.resetTime7d=new Date(y).toISOString(),l.resetTime||(l.resetTime=l.resetTime7d))}return this.hasQuotaData(l)?l:null}catch{return null}}},lr=class extends de{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s=pr(e.baseUrl)||"api.minimaxi.com",i=s.endsWith(".minimax.io")||s==="minimax.io"?"api.minimax.io":"api.minimaxi.com";try{let a=[`https://${i}/v1/token_plan/remains`,`https://${i}/v1/api/openplatform/coding_plan/remains`],l=null;for(let b of a)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 c=Array.isArray(l?.model_remains)?l.model_remains:[];if(c.length===0)return null;let u=c[0],d={},p=$(u.current_interval_total_count),g=$(u.current_interval_usage_count),y=$(u.current_interval_remaining_percent),m=$(u.end_time);p!==void 0&&g!==void 0&&p>0?(d.usedDailyBalance=Math.max(0,p-g),d.limitDaily=p):y!==void 0&&(d.limitDaily=100,d.usedDailyBalance=Math.max(0,100-y)),m&&(d.resetTime=new Date(m).toISOString());let f=$(u.current_weekly_total_count),_=$(u.current_weekly_usage_count),k=$(u.current_weekly_remaining_percent),h=$(u.weekly_end_time);return f!==void 0&&_!==void 0&&f>0?(d.usedBalance=Math.max(0,f-_),d.remainingBalance=_,d.totalBalance=f):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}}},Ml=new tr,$l=new nr,Nl=new or,Ll=new rr,Dl=new sr,Fl=new ir,Ul=new ar,jl=new cr,Hl=new lr});function Gi(n){return{[Jl]:n,[Wl]:dn}}function Kl(n){return n===!0||n==="true"||n===1||n==="1"}function Ql(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 Vl(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 mr(n,e,t){let o=performance.now(),r=()=>Math.max(0,Math.round(performance.now()-o)),s=Vl(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=Y(t));let a=await fetch(s,i);if(a.headers&&kt(n.name,n.baseUrl,a.headers),a.ok)return{success:!0,headers:a.headers,latencyMs:r()};if(a.status===429)return{success:!1,error:`HTTP 429: ${(await a.text().catch(()=>"")).slice(0,100)||"Rate limited"}`,headers:a.headers,latencyMs:r(),errorKind:"http"};if(a.status>=400&&a.status<500)return{success:!0,headers:a.headers,latencyMs:r()};let l=await a.text().catch(()=>"");return{success:!1,error:`HTTP ${a.status}: ${l.slice(0,100)}`,headers:a.headers,latencyMs:r(),errorKind:"http"}}catch(i){let a=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:a,latencyMs:r(),errorKind:l?"timeout":"network"}}}async function Xl(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 a={"Content-Type":"application/json",...Gi("scheduled-wakeup")};i?(a["x-api-key"]=n.apiKey,a["anthropic-version"]="2023-06-01"):a.Authorization=`Bearer ${n.apiKey}`;let l={method:"POST",headers:a,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=Y(t)),o?.info(`Sending scheduled wake-up call to provider ${n.name} using model ${r}...`);let c=await fetch(s,l);if(c.ok)return{success:!0};let u=await c.text().catch(()=>"");return{success:!1,error:`HTTP ${c.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(a){return{success:!1,error:a?.message||a?.toString()||"Unknown wake-up error"}}}async function Zl(n,e,t,o,r){let s=n.baseUrl,i=n.baseUrl.includes("/messages")||e.includes("claude");try{let a={"Content-Type":"application/json",...Gi("rate-limit-probe")};i?(a["x-api-key"]=n.apiKey,a["anthropic-version"]="2023-06-01"):a.Authorization=`Bearer ${n.apiKey}`;let l={method:"POST",headers:a,body:JSON.stringify({model:e,messages:[{role:"user",content:"Hello, please reply briefly."}],max_tokens:10}),signal:AbortSignal.timeout(t)};o&&(l.dispatcher=Y(o)),r?.debug?.(`Sending independent ping to rate-limited model ${n.name} (${e})...`);let c=await fetch(s,l);if(c.ok)return{success:!0};let u=await c.text().catch(()=>"");return{success:!1,error:`HTTP ${c.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(a){return{success:!1,error:a?.message||a?.toString()||"Unknown ping error"}}}function gr(n,e,t,o,r){return xe||(xe=new Mt(n,e,t,o,r)),xe}function De(n,e,t,o,r){let s=gr(n,e,t,o,r);return s.start(),s}function io(){xe&&xe.stop()}function $t(){xe&&(xe.stop(),xe=null)}var Jl,Wl,Gl,Mt,xe,ao=w(()=>{"use strict";le();Ge();fr();Ze();Ro();ce();Jl="x-claude-code-router-source",Wl="x-claude-code-router-version";Gl={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,slowThresholdMs:3e3,initialDelayMs:5e3,excludeProviders:[]};Mt=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={...Gl,...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 a=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:a?.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=j();if(e.length===0)return;let o=[];for(let s of e){let i=so(s.baseUrl),a=Array.isArray(s.models)?s.models:[],l=this.resolveProxyUrl?.(s);if(!i){let c;try{c=new URL(s.baseUrl).hostname}catch{c=void 0}this.logger?.debug?.(`No quota adapter for provider ${s.name}`+(c?` (host: ${c})`:"")+" \u2014 skipping quota probe")}i&&o.push({provider:s.name,models:a,type:"quota-adapter",promise:i.queryQuota(s,this.config.probeTimeoutMs,l).then(c=>{if(c){hn(s.name,c),this.logger?.debug?.(`Stored quota probe result for ${s.name}`);let u=c.limitDaily!==void 0&&c.usedDailyBalance!==void 0&&c.usedDailyBalance>=c.limitDaily,d=c.totalBalance!==void 0&&c.usedBalance!==void 0&&c.usedBalance>=c.totalBalance;if(u||d){let p=u?`Quota exhausted: 5h limit reached (${c.usedDailyBalance}/${c.limitDaily})`:`Quota exhausted: 7d balance depleted (${c.usedBalance}/${c.totalBalance})`;for(let g of a){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,p)}this.logger?.warn?.(`${p} for ${s.name}, marked as unhealthy`)}else for(let p of a){let g=t.getState(s.name,p);g&&g.status==="open"&&g.lastError?.includes("Quota exhausted")&&(t.recover(s.name,p),this.logger?.info?.(`Quota recovered for ${s.name} (${p}), marked as healthy`))}}})}),Yl(s.baseUrl)&&o.push({provider:s.name,models:a,type:"rate-limit-headers",promise:mr(s,this.config.probeTimeoutMs,l).then(c=>{this.recordProbe(s.name,c,"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 a=o[s];this.logger?.warn?.(`Quota probe failed for ${a.provider} (${a.type}): ${i.reason}`)}}}async runHealthProbe(){let e=j(),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=>mr(r,this.config.probeTimeoutMs,this.resolveProxyUrl?.(r))));for(let r=0;r<t.length;r++){let s=t[r],i=o[r],a=Array.isArray(s.models)?s.models:[];if(i.status==="fulfilled"){let l=i.value;if(this.recordProbe(s.name,l,"health"),l.success){let c=0;for(let u of a){let d=e.getState(s.name,u);if(d&&d.status==="open"){let p=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(p){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),c++}c>0&&this.logger?.info?.(`Health probe succeeded for ${s.name}`)}else{for(let c of a)e.recordFailure(s.name,c,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 a)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=j(),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,...a]=s.split(","),l=a.join(",");if(!i||!l)continue;let c=e.getState(i,l);if(!c||!c.rateLimitUntil)continue;if(Date.now()>=c.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 Zl(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 p=Date.now();c.rateLimitUntil&&c.rateLimitUntil>p?c.rateLimitUntil+=120*1e3:c.rateLimitUntil=p+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(a=>a?.name&&a.enabled!==!1&&a.wakeupEnabled===!0);if(e.length===0)return;let t=new Date,o=t.getHours(),r=t.getMinutes(),s=Ql(t),i=this.getConfig&&this.getConfig("WAKEUP_TIME")||"06:00";for(let a of e){let l=a.wakeupTime||i,[c,u]=l.split(":"),d=parseInt(c,10),p=parseInt(u,10);if(isNaN(d)||isNaN(p))continue;let g=o>d||o===d&&r>=p,y=(d*60+p)*6e4-(o*60+r)*6e4,m=y>0&&y<=300*1e3;(g||m)&&!this.hasWakeupFiredToday(a.name,s)&&(this.logger?.info?.(`Scheduled wake-up triggered for provider ${a.name} at ${l}`),Xl(a,3e4,this.resolveProxyUrl?.(a),this.logger).then(f=>{f.success?this.logger?.info?.(`Successfully woke up provider ${a.name}`):this.logger?.error?.(`Failed to wake up provider ${a.name}: ${f.error}`)}).catch(f=>{this.logger?.error?.(`Error in wake-up task for ${a.name}: ${f}`)}))}}isWakeupGloballyEnabled(){return this.getConfig?Kl(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(a=>a.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 mr(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 a of i)j().recordSuccess(o.name,a);else for(let a of i)j().recordFailure(o.name,a,r.error);return{success:r.success,latencyMs:r.latencyMs,status:s.status,isSlow:s.isSlow,errorKind:r.errorKind,error:r.error}}},xe=null});var Ki=w(()=>{"use strict"});var hr,ct,Qi=w(()=>{"use strict";hr=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()}},ct=new hr});var pe,yr=w(()=>{"use strict";pe=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,t)=>{this.buffer+=e;let o=this.buffer.split(`
|
|
304
|
-
`);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 Fe,
|
|
303
|
+
Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(Ki).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)}}});var Vi,Ot,Yi=w(()=>{"use strict";Vi=require("tiktoken"),Ot=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=(0,Vi.get_encoding)(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(a=>{typeof a.content=="string"?o+=t.encode(a.content).length:Array.isArray(a.content)&&a.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 c=typeof l.content=="string"?l.content:JSON.stringify(l.content);o+=t.encode(c).length}})}),typeof s=="string"?o+=t.encode(s).length:Array.isArray(s)&&s.forEach(a=>{a.type==="text"&&(typeof a.text=="string"?o+=t.encode(a.text).length:Array.isArray(a.text)&&a.text.forEach(l=>{o+=t.encode(l||"").length}))}),i&&i.forEach(a=>{a.description&&(o+=t.encode(a.name+a.description).length),a.input_schema&&(o+=t.encode(JSON.stringify(a.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)}}});var At,Xi,at,It,Zi,so,qi=w(()=>{"use strict";At=require("path"),Xi=require("os"),at=require("fs"),It=require("fs"),Zi=require("@huggingface/tokenizers"),so=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||(0,At.join)((0,Xi.homedir)(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=(0,At.join)(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:(0,At.join)(e,"tokenizer.json"),tokenizerConfig:(0,At.join)(e,"tokenizer_config.json")}}ensureDir(e){(0,at.existsSync)(e)||(0,at.mkdirSync)(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!(0,at.existsSync)(e.tokenizerJson)||!(0,at.existsSync)(e.tokenizerConfig))return null;let[t,o]=await Promise.all([It.promises.readFile(e.tokenizerJson,"utf-8"),It.promises.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[a,l]=await Promise.all([s.json(),i.ok?i.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([It.promises.writeFile(e.tokenizerJson,JSON.stringify(a,null,2)),It.promises.writeFile(e.tokenizerConfig,JSON.stringify(l,null,2))]),{tokenizerJson:a,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 Zi.Tokenizer(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 a of i.content)a.type==="text"&&a.text?t.push(a.text):a.type==="tool_use"&&a.input?t.push(JSON.stringify(a.input)):a.type==="tool_result"&&t.push(typeof a.content=="string"?a.content:JSON.stringify(a.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 a of i.text)a&&t.push(a)}}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 io,ea=w(()=>{"use strict";ce();io=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},a=this.options.resolveProxyUrl?.();a&&(i.dispatcher=Y(a));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 c=await l.json();return this.extractTokenCount(c)}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 Le,cr=w(()=>{"use strict";Yi();qi();ea();ce();Le=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 Ot("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 Ot(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),r=new so(e.model,this.logger,{timeout:this.options.timeout});break;case"api":r=new io(e,this.logger,{timeout:this.options.timeout,resolveProxyUrl:()=>{let s=t?this.getRawProvider(t):void 0;return fe(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 ta(n){if(!n||typeof n!="object"||Vl(n))return null;for(let t of Ql(n)){let o=Kl(t);if(o)return o}return null}function Kl(n){if(!n||typeof n!="object")return null;let e={},t=ao(n,Bl);if(t!==void 0){let i=na(t);e.usedDailyBalance=i,e.limitDaily=100}let o=ao(n,Jl);if(o!==void 0){let i=na(o);e.usedBalance=i,e.totalBalance=100}let r=ao(n,Wl);if(r!==void 0){let i=oa(r);i&&(e.resetTime=i)}let s=ao(n,Gl);if(s!==void 0){let i=oa(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 na(n){return!Number.isFinite(n)||n<=0?0:Math.round(n*1e4)/100}function ao(n,e){for(let t of e){let o=$(n?.[t]);if(o!==void 0)return o}}function Ql(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 Vl(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 oa(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 co(n){let e=br(n);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?ou:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?ru:e==="deepseek.com"||e.endsWith(".deepseek.com")?Yl:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?Xl:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?Zl:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?ql:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?eu:e==="maas.aliyuncs.com"||e.endsWith(".maas.aliyuncs.com")?tu:e==="xf-yun.com"||e.endsWith(".xf-yun.com")||e==="xfyun.cn"||e.endsWith(".xfyun.cn")||e==="xfyun.com"||e.endsWith(".xfyun.com")?nu:null:null}function su(n){let e=br(n);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function iu(n,e){let t=_r(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,a=String(i?.apiKey||i?.api_key||"").trim();return a&&o.some(l=>l.includes(a)||a.includes(l))})||t[0]}function _r(n){return!n||typeof n!="object"?[]:!Array.isArray(n)&&n.codingPlanUsageDTO?[n]:Array.isArray(n)?n.flatMap(e=>_r(e)):Object.values(n).flatMap(e=>_r(e))}function kr(n,e){let t=n.match(new RegExp(`(?:^|;\\s*)${e}=([^;]+)`));return t?decodeURIComponent(t[1]):void 0}function br(n){try{return new URL(n).hostname.toLowerCase()}catch{return null}}function $(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 de,lr,ur,dr,pr,fr,Bl,Jl,Wl,Gl,mr,gr,hr,yr,Yl,Xl,Zl,ql,eu,tu,nu,ou,ru,Tr=w(()=>{"use strict";ce();de=class{async fetchJson(e,t,o,r,s){let i=s||t.apiKey;if(!i)return null;let a={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${i}`},signal:AbortSignal.timeout(o)};try{r&&(a.dispatcher=Y(r));let l=await fetch(e,a);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}},lr=class extends de{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=>$(l?.total_balance)!==void 0);if(!i)return null;let a=$(i.total_balance);return a===void 0?null:{totalBalance:a,currency:typeof i.currency=="string"?i.currency:void 0}}},ur=class extends de{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={},a=$(s.limit),l=$(s.limit_remaining),c=$(s.usage),u=$(s.usage_daily);return a!==void 0&&(i.totalBalance=a),l!==void 0&&(i.remainingBalance=l),c!==void 0&&(i.usedBalance=c),u!==void 0&&(i.usedDailyBalance=u),this.hasQuotaData(i)?i:null}},dr=class extends de{async queryQuota(e,t,o){let r=su(e.baseUrl),i=(await this.fetchJson(r,e,t,o))?.data;if(!i||typeof i!="object")return null;let a={},l=$(i.totalBalance),c=$(i.balance);return l!==void 0&&(a.totalBalance=l),c!==void 0&&(a.remainingBalance=c),l!==void 0&&c!==void 0&&(a.usedBalance=Math.max(0,l-c)),this.hasQuotaData(a)?a:null}},pr=class extends de{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=Y(o));let i=await fetch("https://api.z.ai/api/monitor/usage/quota/limit",s);if(!i.ok)return null;let a=await i.json(),l=Array.isArray(a?.data?.limits)?a.data.limits:[],c=null,u=null,d=null;for(let g of l)g.type==="TOKENS_LIMIT"?g.unit===3?c=g:g.unit===6?u=g:c||(c=g):g.type==="TIME_LIMIT"&&(d=g);let p={};if(c){let g=$(c.currentValue),y=$(c.usage),m=$(c.remaining),f=$(c.percentage);g!==void 0?p.usedDailyBalance=g:f!==void 0&&(p.usedDailyBalance=f,p.limitDaily=100),y!==void 0?p.limitDaily=y:g!==void 0&&m!==void 0&&(p.limitDaily=g+m),c.nextResetTime&&(p.resetTime=new Date(c.nextResetTime).toISOString())}if(u){let g=$(u.currentValue),y=$(u.usage),m=$(u.remaining),f=$(u.percentage);g!==void 0?p.usedBalance=g:f!==void 0&&(p.usedBalance=f,p.totalBalance=100),y!==void 0?p.totalBalance=y:g!==void 0&&m!==void 0&&(p.totalBalance=g+m),u.nextResetTime&&!p.resetTime&&(p.resetTime=new Date(u.nextResetTime).toISOString())}if(d){if(p.usedDailyBalance===void 0){let g=$(d.currentValue);g!==void 0&&(p.usedDailyBalance=g)}if(p.limitDaily===void 0){let g=$(d.usage),y=$(d.currentValue),m=$(d.remaining);g!==void 0?p.limitDaily=g:y!==void 0&&m!==void 0&&(p.limitDaily=y+m)}d.nextResetTime&&!p.resetTime&&(p.resetTime=new Date(d.nextResetTime).toISOString())}return this.hasQuotaData(p)?p:null}catch{return null}}},fr=class extends de{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=Y(o));let a=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(!a.ok)return null;let c=(await a.json())?.data?.DataV2?.data?.data?.codingPlanInstanceInfos;if(!Array.isArray(c)||c.length===0)return null;let u=c[0]?.codingPlanQuotaInfo;if(!u)return null;let d={},p=$(u.per5HourUsedQuota),g=$(u.per5HourTotalQuota);p!==void 0&&(d.usedDailyBalance=p),g!==void 0&&(d.limitDaily=g);let y=$(u.perWeekUsedQuota),m=$(u.perWeekTotalQuota);return y!==void 0&&(d.usedBalance=y),m!==void 0&&(d.totalBalance=m),this.hasQuotaData(d)?d:null}catch{return null}}};Bl=["per5HourPercentage","per5HourUsedPercentage","fiveHourPercentage"],Jl=["per1WeekPercentage","perWeekPercentage","per1WeekUsedPercentage"],Wl=["per5HourResetTime","per5HourNextResetTime","fiveHourResetTime"],Gl=["per1WeekResetTime","per1WeekNextResetTime","perWeekResetTime"];mr=class extends de{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=kr(e,"cna")||"",a=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:a,region:"cn-beijing",sec_token:t}).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/cn-beijing/?tab=plan"},body:l,signal:AbortSignal.timeout(o)};try{r&&(c.dispatcher=Y(r));let u=await fetch(`https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=${encodeURIComponent(s)}&_v=undefined`,c);if(!u.ok)return null;let d=await u.json();return ta(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(),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/"},body:i,signal:AbortSignal.timeout(t)};try{o&&(a.dispatcher=Y(o));let l=await fetch(`https://cs-data.qianwenai.com/data/api.json?product=sfm_bailian&action=BroadScopeAspnGateway&api=${encodeURIComponent(r)}`,a);if(!l.ok)return null;let c=await l.json();return ta(c)}catch{return null}}},gr=class extends de{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:(kr(e.quotaToken,"atp-auth-token")||kr(e.quotaToken,"tenantToken")||e.quotaToken).trim(),Cookie:e.quotaToken},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=Y(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 a=await i.json();if(a?.succeed===!1||a?.failed===!0)return null;let l=iu(a,e.apiKey),c=l?.codingPlanUsageDTO||l?.usage||l;if(!c||typeof c!="object")return null;let u={},d=$(c.rp5hUsage),p=$(c.rp5hLimit);d!==void 0&&(u.usedDailyBalance=d),p!==void 0&&(u.limitDaily=p);let g=$(c.rpwUsage),y=$(c.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}}},hr=class extends de{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=Y(o));let i=await fetch("https://api.kimi.com/coding/v1/usages",s);if(!i.ok)return null;let a=await i.json(),l={},c=Array.isArray(a?.limits)?a.limits:[];if(c.length>0&&c[0]?.detail){let d=c[0].detail,p=$(d.limit),g=$(d.remaining),y=d.resetTime;p!==void 0&&g!==void 0&&(l.usedDailyBalance=Math.max(0,p-g),l.limitDaily=p),y&&(l.resetTime=new Date(y).toISOString())}let u=a?.usage;if(u){let d=$(u.limit),p=$(u.remaining),g=$(u.used),y=u.resetTime;d!==void 0&&(l.totalBalance=d,g!==void 0?l.usedBalance=g:p!==void 0&&(l.usedBalance=Math.max(0,d-p),l.remainingBalance=p)),y&&(l.resetTime7d=new Date(y).toISOString(),l.resetTime||(l.resetTime=l.resetTime7d))}return this.hasQuotaData(l)?l:null}catch{return null}}},yr=class extends de{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s=br(e.baseUrl)||"api.minimaxi.com",i=s.endsWith(".minimax.io")||s==="minimax.io"?"api.minimax.io":"api.minimaxi.com";try{let a=[`https://${i}/v1/token_plan/remains`,`https://${i}/v1/api/openplatform/coding_plan/remains`],l=null;for(let b of a)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 c=Array.isArray(l?.model_remains)?l.model_remains:[];if(c.length===0)return null;let u=c[0],d={},p=$(u.current_interval_total_count),g=$(u.current_interval_usage_count),y=$(u.current_interval_remaining_percent),m=$(u.end_time);p!==void 0&&g!==void 0&&p>0?(d.usedDailyBalance=Math.max(0,p-g),d.limitDaily=p):y!==void 0&&(d.limitDaily=100,d.usedDailyBalance=Math.max(0,100-y)),m&&(d.resetTime=new Date(m).toISOString());let f=$(u.current_weekly_total_count),_=$(u.current_weekly_usage_count),k=$(u.current_weekly_remaining_percent),h=$(u.weekly_end_time);return f!==void 0&&_!==void 0&&f>0?(d.usedBalance=Math.max(0,f-_),d.remainingBalance=_,d.totalBalance=f):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}}},Yl=new lr,Xl=new ur,Zl=new dr,ql=new pr,eu=new fr,tu=new mr,nu=new gr,ou=new hr,ru=new yr});function ra(n){return{[au]:n,[cu]:dn}}function uu(n){return n===!0||n==="true"||n===1||n==="1"}function du(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 pu(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 fu(n){try{let e=new URL(n).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function vr(n,e,t){let o=performance.now(),r=()=>Math.max(0,Math.round(performance.now()-o)),s=pu(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=Y(t));let a=await fetch(s,i);if(a.headers&&kt(n.name,n.baseUrl,a.headers),a.ok)return{success:!0,headers:a.headers,latencyMs:r()};if(a.status===429)return{success:!1,error:`HTTP 429: ${(await a.text().catch(()=>"")).slice(0,100)||"Rate limited"}`,headers:a.headers,latencyMs:r(),errorKind:"http"};if(a.status>=400&&a.status<500)return{success:!0,headers:a.headers,latencyMs:r()};let l=await a.text().catch(()=>"");return{success:!1,error:`HTTP ${a.status}: ${l.slice(0,100)}`,headers:a.headers,latencyMs:r(),errorKind:"http"}}catch(i){let a=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:a,latencyMs:r(),errorKind:l?"timeout":"network"}}}async function mu(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 a={"Content-Type":"application/json",...ra("scheduled-wakeup")};i?(a["x-api-key"]=n.apiKey,a["anthropic-version"]="2023-06-01"):a.Authorization=`Bearer ${n.apiKey}`;let l={method:"POST",headers:a,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=Y(t)),o?.info(`Sending scheduled wake-up call to provider ${n.name} using model ${r}...`);let c=await fetch(s,l);if(c.ok)return{success:!0};let u=await c.text().catch(()=>"");return{success:!1,error:`HTTP ${c.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(a){return{success:!1,error:a?.message||a?.toString()||"Unknown wake-up error"}}}async function gu(n,e,t,o,r){let s=n.baseUrl,i=n.baseUrl.includes("/messages")||e.includes("claude");try{let a={"Content-Type":"application/json",...ra("rate-limit-probe")};i?(a["x-api-key"]=n.apiKey,a["anthropic-version"]="2023-06-01"):a.Authorization=`Bearer ${n.apiKey}`;let l={method:"POST",headers:a,body:JSON.stringify({model:e,messages:[{role:"user",content:"Hello, please reply briefly."}],max_tokens:10}),signal:AbortSignal.timeout(t)};o&&(l.dispatcher=Y(o)),r?.debug?.(`Sending independent ping to rate-limited model ${n.name} (${e})...`);let c=await fetch(s,l);if(c.ok)return{success:!0};let u=await c.text().catch(()=>"");return{success:!1,error:`HTTP ${c.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(a){return{success:!1,error:a?.message||a?.toString()||"Unknown ping error"}}}function xr(n,e,t,o,r){return xe||(xe=new Mt(n,e,t,o,r)),xe}function De(n,e,t,o,r){let s=xr(n,e,t,o,r);return s.start(),s}function lo(){xe&&xe.stop()}function $t(){xe&&(xe.stop(),xe=null)}var au,cu,lu,Mt,xe,uo=w(()=>{"use strict";le();Ge();Tr();Ze();Eo();ce();au="x-claude-code-router-source",cu="x-claude-code-router-version";lu={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,slowThresholdMs:3e3,initialDelayMs:5e3,excludeProviders:[]};Mt=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={...lu,...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 a=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:a?.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=j();if(e.length===0)return;let o=[];for(let s of e){let i=co(s.baseUrl),a=Array.isArray(s.models)?s.models:[],l=this.resolveProxyUrl?.(s);if(!i){let c;try{c=new URL(s.baseUrl).hostname}catch{c=void 0}this.logger?.debug?.(`No quota adapter for provider ${s.name}`+(c?` (host: ${c})`:"")+" \u2014 skipping quota probe")}i&&o.push({provider:s.name,models:a,type:"quota-adapter",promise:i.queryQuota(s,this.config.probeTimeoutMs,l).then(c=>{if(c){kn(s.name,c),this.logger?.debug?.(`Stored quota probe result for ${s.name}`);let u=c.limitDaily!==void 0&&c.usedDailyBalance!==void 0&&c.usedDailyBalance>=c.limitDaily,d=c.totalBalance!==void 0&&c.usedBalance!==void 0&&c.usedBalance>=c.totalBalance;if(u||d){let p=u?`Quota exhausted: 5h limit reached (${c.usedDailyBalance}/${c.limitDaily})`:`Quota exhausted: 7d balance depleted (${c.usedBalance}/${c.totalBalance})`;for(let g of a){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,p)}this.logger?.warn?.(`${p} for ${s.name}, marked as unhealthy`)}else for(let p of a){let g=t.getState(s.name,p);g&&g.status==="open"&&g.lastError?.includes("Quota exhausted")&&(t.recover(s.name,p),this.logger?.info?.(`Quota recovered for ${s.name} (${p}), marked as healthy`))}}})}),fu(s.baseUrl)&&o.push({provider:s.name,models:a,type:"rate-limit-headers",promise:vr(s,this.config.probeTimeoutMs,l).then(c=>{this.recordProbe(s.name,c,"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 a=o[s];this.logger?.warn?.(`Quota probe failed for ${a.provider} (${a.type}): ${i.reason}`)}}}async runHealthProbe(){let e=j(),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=>vr(r,this.config.probeTimeoutMs,this.resolveProxyUrl?.(r))));for(let r=0;r<t.length;r++){let s=t[r],i=o[r],a=Array.isArray(s.models)?s.models:[];if(i.status==="fulfilled"){let l=i.value;if(this.recordProbe(s.name,l,"health"),l.success){let c=0;for(let u of a){let d=e.getState(s.name,u);if(d&&d.status==="open"){let p=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(p){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),c++}c>0&&this.logger?.info?.(`Health probe succeeded for ${s.name}`)}else{for(let c of a)e.recordFailure(s.name,c,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 a)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=j(),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,...a]=s.split(","),l=a.join(",");if(!i||!l)continue;let c=e.getState(i,l);if(!c||!c.rateLimitUntil)continue;if(Date.now()>=c.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 gu(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 p=Date.now();c.rateLimitUntil&&c.rateLimitUntil>p?c.rateLimitUntil+=120*1e3:c.rateLimitUntil=p+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(a=>a?.name&&a.enabled!==!1&&a.wakeupEnabled===!0);if(e.length===0)return;let t=new Date,o=t.getHours(),r=t.getMinutes(),s=du(t),i=this.getConfig&&this.getConfig("WAKEUP_TIME")||"06:00";for(let a of e){let l=a.wakeupTime||i,[c,u]=l.split(":"),d=parseInt(c,10),p=parseInt(u,10);if(isNaN(d)||isNaN(p))continue;let g=o>d||o===d&&r>=p,y=(d*60+p)*6e4-(o*60+r)*6e4,m=y>0&&y<=300*1e3;(g||m)&&!this.hasWakeupFiredToday(a.name,s)&&(this.logger?.info?.(`Scheduled wake-up triggered for provider ${a.name} at ${l}`),mu(a,3e4,this.resolveProxyUrl?.(a),this.logger).then(f=>{f.success?this.logger?.info?.(`Successfully woke up provider ${a.name}`):this.logger?.error?.(`Failed to wake up provider ${a.name}: ${f.error}`)}).catch(f=>{this.logger?.error?.(`Error in wake-up task for ${a.name}: ${f}`)}))}}isWakeupGloballyEnabled(){return this.getConfig?uu(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(a=>a.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 vr(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 a of i)j().recordSuccess(o.name,a);else for(let a of i)j().recordFailure(o.name,a,r.error);return{success:r.success,latencyMs:r.latencyMs,status:s.status,isSlow:s.isSlow,errorKind:r.errorKind,error:r.error}}},xe=null});var sa=w(()=>{"use strict"});var Cr,ct,ia=w(()=>{"use strict";Cr=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()}},ct=new Cr});var pe,Sr=w(()=>{"use strict";pe=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,t)=>{this.buffer+=e;let o=this.buffer.split(`
|
|
304
|
+
`);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 Fe,Rr=w(()=>{"use strict";Fe=class extends TransformStream{constructor(){super({transform:(e,t)=>{let o="";e.event&&(o+=`event: ${e.event}
|
|
305
305
|
`),e.id&&(o+=`id: ${e.id}
|
|
306
306
|
`),e.retry&&(o+=`retry: ${e.retry}
|
|
307
307
|
`),e.data&&(e.data.type==="done"?o+=`data: [DONE]
|
|
308
308
|
`:o+=`data: ${JSON.stringify(e.data)}
|
|
309
309
|
`),o+=`
|
|
310
|
-
`,t.enqueue(o)}})}}});var Nt,
|
|
310
|
+
`,t.enqueue(o)}})}}});var Nt,wr=w(()=>{"use strict";Nt=(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 Er=w(()=>{"use strict";Sr();Rr();wr()});var aa=w(()=>{"use strict"});var Lt,Pr=w(()=>{"use strict";Lt=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||{},a="";if(r){let l=new Date().toISOString();a+=this.config.colors?`${this.colors.cyan}[${l}]${this.colors.reset} `:`[${l}] `}switch(s&&(a+=this.config.colors?`${this.colors.bright}${s}${this.colors.reset} `:`${s} `),o){case"json":a+=JSON.stringify(e,null,2);break;case"markdown":typeof e=="object"?a+=this.toMarkdown(e):a+=String(e);break;case"text":default:typeof e=="object"?a+=JSON.stringify(e,null,2):a+=String(e);break}return i&&Object.keys(i).length>0&&(a+=`
|
|
311
311
|
`+(this.config.colors?`${this.colors.dim}`:""),a+="Metadata: "+JSON.stringify(i,null,2),this.config.colors&&(a+=this.colors.reset)),a}toMarkdown(e,t=0){let o=" ".repeat(t);return Array.isArray(e)?e.map(r=>typeof r=="object"?`${o}-
|
|
312
312
|
${this.toMarkdown(r,t+1)}`:`${o}- ${r}`).join(`
|
|
313
313
|
`):typeof e=="object"&&e!==null?Object.entries(e).map(([r,s])=>typeof s=="object"&&s!==null?`${o}${r}:
|
|
314
314
|
${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
315
|
-
`):`${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 Dt,vr=w(()=>{"use strict";Dt=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||{},a={data:e};return r&&(a.timestamp=new Date().toISOString()),s&&(a.prefix=s),i&&Object.keys(i).length>0&&(a.metadata=i),a}async sendRequest(e,t,o,r,s){let i=new AbortController,a=setTimeout(()=>i.abort(),s);try{let l=await fetch(e,{method:t,headers:o,body:JSON.stringify(r),signal:i.signal});if(clearTimeout(a),!l.ok)throw new Error(`HTTP ${l.status}: ${l.statusText}`);return l}catch(l){throw clearTimeout(a),l}}delay(e){return new Promise(t=>setTimeout(t,e))}async sendWithRetry(e,t,o,r,s,i){let a=null;for(let l=1;l<=i.maxAttempts;l++)try{return await this.sendRequest(e,t,o,r,s)}catch(c){if(a=c,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...`,c.message),await this.delay(u)}throw a}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}`)}}}});var lt,xr,Yi,Ft,Cr=w(()=>{"use strict";lt=require("fs"),xr=require("path"),Yi=require("os"),Ft=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let t=(0,Yi.tmpdir)();this.baseDir=(0,xr.join)(t,this.config.subdirectory),this.ensureDir()}ensureDir(){try{(0,lt.existsSync)(this.baseDir)||(0,lt.mkdirSync)(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(0,xr.join)(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(0,lt.writeFileSync)(s,JSON.stringify(r,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Sr,ut,Xi=w(()=>{"use strict";Tr();vr();Cr();Sr=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 Lt(e.config);case"webhook":return new Dt(e.config);case"temp-file":return new Ft(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,a])=>{try{await a.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 a=>{let l=this.handlers.get(a);if(!l){console.warn(`[OutputManager] Handler ${a} not found`),s.failed.push(a);return}try{await l.output(t,r)?s.success.push(a):s.failed.push(a)}catch(c){console.error(`[OutputManager] Handler ${a} failed:`,c),s.failed.push(a)}});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)}},ut=new Sr});var Rr=w(()=>{"use strict";Vi();Tr();vr();Cr();Xi()});function Zi(n,e,t=0){if(n<=0)return 0;let o=Math.max((e-t)/1e3,ql);return Math.round(n/o)}function jt(n){let e=(n.match(/[\u4e00-\u9fa5]/g)||[]).length,t=n.length-e;return Math.ceil(e/1.5+t/4)}async function qi(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 a of e)try{await ut.outputToType(a,s,i)}catch(l){console.error(`[TokenSpeedPlugin] Failed to output to ${a}:`,l)}}var ea,Ut,wr,ql,Ht,ta=w(()=>{"use strict";ea=V(require("fastify-plugin"),1);br();Rr();qe();Ut=new Map,wr=new Map,ql=1,Ht={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:(0,ea.default)(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)ut.registerHandlers(t.outputHandlers);else{let i=[];for(let a of o)a==="console"?i.push({type:"console",enabled:!0,config:{colors:!0,level:"log"}}):a==="temp-file"?i.push({type:"temp-file",enabled:!0,config:{subdirectory:"claude-code-router",extension:"json",includeTimestamp:!0,prefix:"session"}}):a==="webhook"&&console.warn("[TokenSpeedPlugin] Webhook reporter requires explicit configuration in outputHandlers");i.length>0&&ut.registerHandlers(i)}t.outputOptions&&ut.setDefaultOptions(t.outputOptions);let r=async i=>{let a=n.tokenizerService;if(!a)return n.log?.warn("TokenizerService not available"),null;if(!i.provider||!i.model)return null;let l=i.provider,c=i.model,u=`${l}:${c}`;if(wr.has(u))return wr.get(u);let d=a.getTokenizerConfigForModel(l,c);if(!d)return n.log?.debug(`No tokenizer config for ${l}:${c}, using fallback`),null;try{let p=await a.getTokenizer(d,l);return wr.set(u,p),n.log?.info(`Created tokenizer for ${l}:${c} - ${p.name}`),p}catch(p){return n.log?.warn(`Failed to create tokenizer for ${l}:${c}: ${p.message}`),null}},s=i=>{let a=new URL(`http://127.0.0.1${i}`).pathname;return a.endsWith("/v1/messages")||a.endsWith("/v1/responses")};n.addHook("onRequest",async i=>{s(i.url)&&(i.requestStartTime=performance.now())}),n.addHook("onSend",async(i,a,l)=>{let c=i.requestStartTime;if(!c)return;let u=i.id||Date.now().toString(),d=i.usageSessionId;if(!d)try{d=Te(i.body?.metadata?.user_id)}catch{}if(!d){let f=i.headers?.["x-request-id"],_=Array.isArray(f)?f[0]:f;d=(typeof _=="string"?_:void 0)||u}let p=await r(i);if(l instanceof ReadableStream){Ut.set(u,{requestId:u,sessionId:d,startTime:c,lastTokenTime:c,tokenCount:0,tokensPerSecond:0,stream:!0});let[f,_]=l.tee(),k=async()=>{let h=null,b=async T=>{let C=Ut.get(u);if(!C)return;let x=C.firstTokenTime?C.firstTokenTime-C.startTime:0,v=(T?C.lastTokenTime:performance.now())-C.startTime;C.tokensPerSecond=Zi(C.tokenCount,v,x),await qi(C,o,t.outputOptions,T).catch(E=>{n.log?.warn(`Failed to output streaming stats: ${E.message}`)})};try{let C=_.pipeThrough(new TextDecoderStream).pipeThrough(new pe).getReader();for(h=setInterval(async()=>{Ut.get(u)&&await b(!1)},1e3);;){let{done:x,value:S}=await C.read();if(x)break;let v=S,E=Ut.get(u);if(!E)continue;let R=performance.now(),A=v.event||v.data?.type,O=v.event==="content_block_start"||v.event==="content_block_delta"||v.event==="text_block"||v.event==="content_block"||A==="response.output_text.delta"||A==="response.function_call_arguments.delta"||A==="response.reasoning_summary_text.delta";!E.firstTokenTime&&O&&(E.firstTokenTime=R,E.timeToFirstToken=Math.round(R-E.startTime));let I="";if(v.event==="content_block_delta"&&v.data?.delta){let L=v.data.delta.type;L==="text_delta"?I=v.data.delta.text||"":L==="input_json_delta"?I=v.data.delta.partial_json||"":L==="thinking_delta"&&(I=v.data.delta.thinking||"")}if((A==="response.output_text.delta"||A==="response.function_call_arguments.delta"||A==="response.reasoning_summary_text.delta")&&(I=typeof v.data?.delta=="string"?v.data.delta:""),I){let L=p&&p.encodeText?p.encodeText(I).length:jt(I);E.tokenCount+=L,E.lastTokenTime=R}(v.event==="message_stop"||A==="response.completed"||A==="response.failed")&&(h&&(clearInterval(h),h=null),await b(!0),Ut.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}`)}),f}let g=performance.now(),y=0,m=!1;if(l&&typeof l=="string")try{let f=JSON.parse(l);if(typeof f.usage?.output_tokens=="number")y=f.usage.output_tokens,m=!0;else{let _=f.content??f.message?.content;if(_!==void 0)if(m=!0,p)Array.isArray(_)?y=_.reduce((k,h)=>{if(h.type==="text"){let b=h.text||"";return k+(p.encodeText?p.encodeText(b).length:jt(b))}return k},0):typeof _=="string"&&(y=p.encodeText?p.encodeText(_).length:jt(_));else{let k=Array.isArray(_)?_.map(h=>h.text).join(""):typeof _=="string"?_:"";y=jt(k)}}}catch{}if(m){let f=g-c,_={requestId:u,sessionId:d,startTime:c,lastTokenTime:g,tokenCount:y,tokensPerSecond:Zi(y,f),stream:!1};await qi(_,o,t.outputOptions,!0)}return l})})}});var Pr=w(()=>{"use strict";Ki();Qi();ta();Rr()});function na(){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 oe,Or,Ce,W,Er,zt,Bt,co,ie,Jt,be,eu,lo,uo=w(()=>{"use strict";oe=V(require("node:fs/promises"),1),Or=V(require("json5"),1),Ce=V(require("node:path"),1),W=require("@wengine-ai/claude-code-router-shared"),Er=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(Er);if(n!==null&&typeof n=="object"){let e={};for(let[t,o]of Object.entries(n))e[t]=Er(o);return e}return n},zt=async n=>{try{await oe.default.access(n)}catch{await oe.default.mkdir(n,{recursive:!0})}},Bt=async()=>{await zt(W.HOME_DIR),await zt(W.PLUGINS_DIR),await zt(Ce.default.join(W.HOME_DIR,"logs"))},co=async()=>{let n=await oe.default.readFile(W.CONFIG_FILE,"utf-8");return Or.default.parse(n)},ie=async()=>{try{let n=await oe.default.readFile(W.CONFIG_FILE,"utf-8");try{let e=Or.default.parse(n);return Er(e)}catch(e){console.error(`Failed to parse config file at ${W.CONFIG_FILE}`),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 Bt();let e=await Jt();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 ${W.CONFIG_FILE}`),console.error("Error details:",n.message),process.exit(1)}};Jt=async()=>{na();try{if(await oe.default.access(W.CONFIG_FILE).then(()=>!0).catch(()=>!1)){let n=new Date().toISOString().replace(/[:.]/g,"-"),e=`${W.CONFIG_FILE}.${n}.bak`;await oe.default.copyFile(W.CONFIG_FILE,e);try{let t=Ce.default.dirname(W.CONFIG_FILE),o=Ce.default.basename(W.CONFIG_FILE),s=(await oe.default.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 a=Ce.default.join(t,s[i]);await oe.default.unlink(a)}}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=>{na(),await zt(W.HOME_DIR),await eu();let e=`${JSON.stringify(n,null,2)}`;await oe.default.writeFile(W.CONFIG_FILE,e)},eu=async()=>{if(!await oe.default.access(W.CONFIG_FILE).then(()=>!0).catch(()=>!1))return;let e=Ce.default.join(W.HOME_DIR,"config-history");await zt(e);let t=new Date().toISOString().replace(/[:.]/g,"-"),o=Ce.default.join(e,`config-${t}.json`);await oe.default.copyFile(W.CONFIG_FILE,o);try{let s=(await oe.default.readdir(e)).filter(i=>i.startsWith("config-")&&i.endsWith(".json")).sort().reverse();if(s.length>50)for(let i of s.slice(50))await oe.default.unlink(Ce.default.join(e,i))}catch(r){console.warn("Failed to prune config-history:",r)}},lo=async()=>{let n=await ie();return Object.assign(process.env,n),n}});function tu(n){return n?.cache_read_input_tokens??n?.input_tokens_details?.cached_tokens??n?.prompt_tokens_details?.cached_tokens??0}function nu(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 Ue(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:tu(n),cache_creation_input_tokens:nu(n)}}function Wt(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 Ar=w(()=>{"use strict"});function po(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(Ir(r,s))}return}if(Array.isArray(n)){for(let o of n)po(o,e,t);return}if(typeof n=="object")for(let o of Object.values(n))po(o,e,t)}}function Mr(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 a of i)a&&o.add(Ir(s,a))}return po(n?.Router,t,o),po(n?.fallback,t,o),o}function dt(n,e){try{let t=j();if(!t?.getAllStates||!t?.recover)return 0;let o=Mr(n),r=0;for(let s of t.getAllStates()||[]){let i=s?.provider,a=s?.model;!i||!a||o.has(Ir(i,a))||(t.recover(i,a),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 Gt(n,e){try{if(!n)return 0;let t=j();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 ou,Ir,fo=w(()=>{"use strict";le();ou="\0",Ir=(n,e)=>`${n}${ou}${e}`});function ru(n){return/\[1m\]/i.test(n)?1e6:2e5}function su(n){let e=n.toLowerCase();return e.includes("haiku")||e.includes("mini")?8192:32e3}function iu(n,e=n){let t=ru(n),o=su(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 Kt(n){let e=new Map,t=(o,r=o)=>{!o||e.has(o)||e.set(o,iu(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 $r=w(()=>{"use strict"});function au(n){let e=n.ip||n.connection?.remoteAddress||"";return e==="127.0.0.1"||e==="::1"||e==="::ffff:127.0.0.1"}var Qt,Nr=w(()=>{"use strict";Qt=n=>async(e,t,o)=>{if(["/","/health"].includes(e.url)||e.url.startsWith("/ui")||au(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 a=e.headers.authorization||e.headers["x-api-key"],l=Array.isArray(a)?a[0]:a||"";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 c="";if(l.startsWith("Bearer")?c=l.split(" ")[1]:c=l,c!==i){t.log.warn({url:e.url,gotPrefix:c.slice(0,10),gotSuffix:c.slice(-6),gotLen:c.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 Hr={};nn(Hr,{append:()=>Zt,clear:()=>Ur,close:()=>xu,getOldestRecordTimestamp:()=>Cu,query:()=>Fr,querySummary:()=>qt,readTokenSpeedStats:()=>jr});function pu(){(0,ae.existsSync)(go)||(0,ae.mkdirSync)(go,{recursive:!0})}function Xt(){if(Re)return Re;pu();let n=new sa.default(cu);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&&(fu(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")),gu(n),ca(n,!0),Dr=n.prepare(`
|
|
315
|
+
`):`${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 Dt,Or=w(()=>{"use strict";Dt=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||{},a={data:e};return r&&(a.timestamp=new Date().toISOString()),s&&(a.prefix=s),i&&Object.keys(i).length>0&&(a.metadata=i),a}async sendRequest(e,t,o,r,s){let i=new AbortController,a=setTimeout(()=>i.abort(),s);try{let l=await fetch(e,{method:t,headers:o,body:JSON.stringify(r),signal:i.signal});if(clearTimeout(a),!l.ok)throw new Error(`HTTP ${l.status}: ${l.statusText}`);return l}catch(l){throw clearTimeout(a),l}}delay(e){return new Promise(t=>setTimeout(t,e))}async sendWithRetry(e,t,o,r,s,i){let a=null;for(let l=1;l<=i.maxAttempts;l++)try{return await this.sendRequest(e,t,o,r,s)}catch(c){if(a=c,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...`,c.message),await this.delay(u)}throw a}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}`)}}}});var lt,Ar,ca,Ft,Ir=w(()=>{"use strict";lt=require("fs"),Ar=require("path"),ca=require("os"),Ft=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let t=(0,ca.tmpdir)();this.baseDir=(0,Ar.join)(t,this.config.subdirectory),this.ensureDir()}ensureDir(){try{(0,lt.existsSync)(this.baseDir)||(0,lt.mkdirSync)(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(0,Ar.join)(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(0,lt.writeFileSync)(s,JSON.stringify(r,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Mr,ut,la=w(()=>{"use strict";Pr();Or();Ir();Mr=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 Lt(e.config);case"webhook":return new Dt(e.config);case"temp-file":return new Ft(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,a])=>{try{await a.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 a=>{let l=this.handlers.get(a);if(!l){console.warn(`[OutputManager] Handler ${a} not found`),s.failed.push(a);return}try{await l.output(t,r)?s.success.push(a):s.failed.push(a)}catch(c){console.error(`[OutputManager] Handler ${a} failed:`,c),s.failed.push(a)}});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)}},ut=new Mr});var $r=w(()=>{"use strict";aa();Pr();Or();Ir();la()});function ua(n,e,t=0){if(n<=0)return 0;let o=Math.max((e-t)/1e3,hu);return Math.round(n/o)}function jt(n){let e=(n.match(/[\u4e00-\u9fa5]/g)||[]).length,t=n.length-e;return Math.ceil(e/1.5+t/4)}async function da(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 a of e)try{await ut.outputToType(a,s,i)}catch(l){console.error(`[TokenSpeedPlugin] Failed to output to ${a}:`,l)}}var pa,Ut,Nr,hu,Ht,fa=w(()=>{"use strict";pa=V(require("fastify-plugin"),1);Er();$r();qe();Ut=new Map,Nr=new Map,hu=1,Ht={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:(0,pa.default)(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)ut.registerHandlers(t.outputHandlers);else{let i=[];for(let a of o)a==="console"?i.push({type:"console",enabled:!0,config:{colors:!0,level:"log"}}):a==="temp-file"?i.push({type:"temp-file",enabled:!0,config:{subdirectory:"claude-code-router",extension:"json",includeTimestamp:!0,prefix:"session"}}):a==="webhook"&&console.warn("[TokenSpeedPlugin] Webhook reporter requires explicit configuration in outputHandlers");i.length>0&&ut.registerHandlers(i)}t.outputOptions&&ut.setDefaultOptions(t.outputOptions);let r=async i=>{let a=n.tokenizerService;if(!a)return n.log?.warn("TokenizerService not available"),null;if(!i.provider||!i.model)return null;let l=i.provider,c=i.model,u=`${l}:${c}`;if(Nr.has(u))return Nr.get(u);let d=a.getTokenizerConfigForModel(l,c);if(!d)return n.log?.debug(`No tokenizer config for ${l}:${c}, using fallback`),null;try{let p=await a.getTokenizer(d,l);return Nr.set(u,p),n.log?.info(`Created tokenizer for ${l}:${c} - ${p.name}`),p}catch(p){return n.log?.warn(`Failed to create tokenizer for ${l}:${c}: ${p.message}`),null}},s=i=>{let a=new URL(`http://127.0.0.1${i}`).pathname;return a.endsWith("/v1/messages")||a.endsWith("/v1/responses")};n.addHook("onRequest",async i=>{s(i.url)&&(i.requestStartTime=performance.now())}),n.addHook("onSend",async(i,a,l)=>{let c=i.requestStartTime;if(!c)return;let u=i.id||Date.now().toString(),d=i.usageSessionId;if(!d)try{d=Te(i.body?.metadata?.user_id)}catch{}if(!d){let f=i.headers?.["x-request-id"],_=Array.isArray(f)?f[0]:f;d=(typeof _=="string"?_:void 0)||u}let p=await r(i);if(l instanceof ReadableStream){Ut.set(u,{requestId:u,sessionId:d,startTime:c,lastTokenTime:c,tokenCount:0,tokensPerSecond:0,stream:!0});let[f,_]=l.tee(),k=async()=>{let h=null,b=async v=>{let C=Ut.get(u);if(!C)return;let x=C.firstTokenTime?C.firstTokenTime-C.startTime:0,T=(v?C.lastTokenTime:performance.now())-C.startTime;C.tokensPerSecond=ua(C.tokenCount,T,x),await da(C,o,t.outputOptions,v).catch(E=>{n.log?.warn(`Failed to output streaming stats: ${E.message}`)})};try{let C=_.pipeThrough(new TextDecoderStream).pipeThrough(new pe).getReader();for(h=setInterval(async()=>{Ut.get(u)&&await b(!1)},1e3);;){let{done:x,value:S}=await C.read();if(x)break;let T=S,E=Ut.get(u);if(!E)continue;let R=performance.now(),A=T.event||T.data?.type,O=T.event==="content_block_start"||T.event==="content_block_delta"||T.event==="text_block"||T.event==="content_block"||A==="response.output_text.delta"||A==="response.function_call_arguments.delta"||A==="response.reasoning_summary_text.delta";!E.firstTokenTime&&O&&(E.firstTokenTime=R,E.timeToFirstToken=Math.round(R-E.startTime));let I="";if(T.event==="content_block_delta"&&T.data?.delta){let L=T.data.delta.type;L==="text_delta"?I=T.data.delta.text||"":L==="input_json_delta"?I=T.data.delta.partial_json||"":L==="thinking_delta"&&(I=T.data.delta.thinking||"")}if((A==="response.output_text.delta"||A==="response.function_call_arguments.delta"||A==="response.reasoning_summary_text.delta")&&(I=typeof T.data?.delta=="string"?T.data.delta:""),I){let L=p&&p.encodeText?p.encodeText(I).length:jt(I);E.tokenCount+=L,E.lastTokenTime=R}(T.event==="message_stop"||A==="response.completed"||A==="response.failed")&&(h&&(clearInterval(h),h=null),await b(!0),Ut.delete(u))}}catch(v){h&&clearInterval(h),v.name!=="AbortError"&&v.code!=="ERR_STREAM_PREMATURE_CLOSE"&&n.log?.warn(`Error processing token stats: ${v.message}`)}};return i.tokenSpeedCapturePromise=k().catch(h=>{console.log(h),n.log?.warn(`Background stats processing failed: ${h.message}`)}),f}let g=performance.now(),y=0,m=!1;if(l&&typeof l=="string")try{let f=JSON.parse(l);if(typeof f.usage?.output_tokens=="number")y=f.usage.output_tokens,m=!0;else{let _=f.content??f.message?.content;if(_!==void 0)if(m=!0,p)Array.isArray(_)?y=_.reduce((k,h)=>{if(h.type==="text"){let b=h.text||"";return k+(p.encodeText?p.encodeText(b).length:jt(b))}return k},0):typeof _=="string"&&(y=p.encodeText?p.encodeText(_).length:jt(_));else{let k=Array.isArray(_)?_.map(h=>h.text).join(""):typeof _=="string"?_:"";y=jt(k)}}}catch{}if(m){let f=g-c,_={requestId:u,sessionId:d,startTime:c,lastTokenTime:g,tokenCount:y,tokensPerSecond:ua(y,f),stream:!1};await da(_,o,t.outputOptions,!0)}return l})})}});var Lr=w(()=>{"use strict";sa();ia();fa();$r()});function ma(){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 oe,Fr,Ce,W,Dr,zt,Bt,po,ie,Jt,be,yu,fo,mo=w(()=>{"use strict";oe=V(require("node:fs/promises"),1),Fr=V(require("json5"),1),Ce=V(require("node:path"),1),W=require("@wengine-ai/claude-code-router-shared"),Dr=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(Dr);if(n!==null&&typeof n=="object"){let e={};for(let[t,o]of Object.entries(n))e[t]=Dr(o);return e}return n},zt=async n=>{try{await oe.default.access(n)}catch{await oe.default.mkdir(n,{recursive:!0})}},Bt=async()=>{await zt(W.HOME_DIR),await zt(W.PLUGINS_DIR),await zt(Ce.default.join(W.HOME_DIR,"logs"))},po=async()=>{let n=await oe.default.readFile(W.CONFIG_FILE,"utf-8");return Fr.default.parse(n)},ie=async()=>{try{let n=await oe.default.readFile(W.CONFIG_FILE,"utf-8");try{let e=Fr.default.parse(n);return Dr(e)}catch(e){console.error(`Failed to parse config file at ${W.CONFIG_FILE}`),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 Bt();let e=await Jt();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 ${W.CONFIG_FILE}`),console.error("Error details:",n.message),process.exit(1)}};Jt=async()=>{ma();try{if(await oe.default.access(W.CONFIG_FILE).then(()=>!0).catch(()=>!1)){let n=new Date().toISOString().replace(/[:.]/g,"-"),e=`${W.CONFIG_FILE}.${n}.bak`;await oe.default.copyFile(W.CONFIG_FILE,e);try{let t=Ce.default.dirname(W.CONFIG_FILE),o=Ce.default.basename(W.CONFIG_FILE),s=(await oe.default.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 a=Ce.default.join(t,s[i]);await oe.default.unlink(a)}}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=>{ma(),await zt(W.HOME_DIR),await yu();let e=`${JSON.stringify(n,null,2)}`;await oe.default.writeFile(W.CONFIG_FILE,e)},yu=async()=>{if(!await oe.default.access(W.CONFIG_FILE).then(()=>!0).catch(()=>!1))return;let e=Ce.default.join(W.HOME_DIR,"config-history");await zt(e);let t=new Date().toISOString().replace(/[:.]/g,"-"),o=Ce.default.join(e,`config-${t}.json`);await oe.default.copyFile(W.CONFIG_FILE,o);try{let s=(await oe.default.readdir(e)).filter(i=>i.startsWith("config-")&&i.endsWith(".json")).sort().reverse();if(s.length>50)for(let i of s.slice(50))await oe.default.unlink(Ce.default.join(e,i))}catch(r){console.warn("Failed to prune config-history:",r)}},fo=async()=>{let n=await ie();return Object.assign(process.env,n),n}});function _u(n){return n?.cache_read_input_tokens??n?.input_tokens_details?.cached_tokens??n?.prompt_tokens_details?.cached_tokens??0}function ku(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 Ue(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:_u(n),cache_creation_input_tokens:ku(n)}}function Wt(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 Ur=w(()=>{"use strict"});function go(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(jr(r,s))}return}if(Array.isArray(n)){for(let o of n)go(o,e,t);return}if(typeof n=="object")for(let o of Object.values(n))go(o,e,t)}}function Hr(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 a of i)a&&o.add(jr(s,a))}return go(n?.Router,t,o),go(n?.fallback,t,o),o}function dt(n,e){try{let t=j();if(!t?.getAllStates||!t?.recover)return 0;let o=Hr(n),r=0;for(let s of t.getAllStates()||[]){let i=s?.provider,a=s?.model;!i||!a||o.has(jr(i,a))||(t.recover(i,a),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 Gt(n,e){try{if(!n)return 0;let t=j();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 bu,jr,ho=w(()=>{"use strict";le();bu="\0",jr=(n,e)=>`${n}${bu}${e}`});function Tu(n){return/\[1m\]/i.test(n)?1e6:2e5}function vu(n){let e=n.toLowerCase();return e.includes("haiku")||e.includes("mini")?8192:32e3}function xu(n,e=n){let t=Tu(n),o=vu(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 Kt(n){let e=new Map,t=(o,r=o)=>{!o||e.has(o)||e.set(o,xu(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 zr=w(()=>{"use strict"});function Cu(n){let e=n.ip||n.connection?.remoteAddress||"";return e==="127.0.0.1"||e==="::1"||e==="::ffff:127.0.0.1"}var Qt,Br=w(()=>{"use strict";Qt=n=>async(e,t,o)=>{if(["/","/health"].includes(e.url)||e.url.startsWith("/ui")||Cu(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 a=e.headers.authorization||e.headers["x-api-key"],l=Array.isArray(a)?a[0]:a||"";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 c="";if(l.startsWith("Bearer")?c=l.split(" ")[1]:c=l,c!==i){t.log.warn({url:e.url,gotPrefix:c.slice(0,10),gotSuffix:c.slice(-6),gotLen:c.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 Vr={};nn(Vr,{append:()=>Zt,clear:()=>Kr,close:()=>ju,getOldestRecordTimestamp:()=>Hu,query:()=>Gr,querySummary:()=>qt,readTokenSpeedStats:()=>Qr});function Pu(){(0,ae.existsSync)(_o)||(0,ae.mkdirSync)(_o,{recursive:!0})}function Xt(){if(Re)return Re;Pu();let n=new _a.default(Su);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&&(Ou(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")),Iu(n),va(n,!0),Wr=n.prepare(`
|
|
316
316
|
INSERT INTO usage_records (
|
|
317
317
|
id, timestamp, session_id, provider, original_model, model, upstream_model, model_family,
|
|
318
318
|
scenario_type, client_type,
|
|
@@ -326,7 +326,7 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
326
326
|
@cache_read_input_tokens, @cache_creation_input_tokens, @ttft,
|
|
327
327
|
@tokens_per_second, @duration_ms, @status, @error_message, @response_body
|
|
328
328
|
)
|
|
329
|
-
`),Re=n,Re}function
|
|
329
|
+
`),Re=n,Re}function Ou(n){n.exec(`
|
|
330
330
|
CREATE TABLE IF NOT EXISTS usage_records (
|
|
331
331
|
id TEXT PRIMARY KEY,
|
|
332
332
|
timestamp TEXT NOT NULL,
|
|
@@ -363,12 +363,12 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
363
363
|
CREATE INDEX IF NOT EXISTS idx_usage_records_scenario_timestamp ON usage_records(scenario_type, timestamp);
|
|
364
364
|
CREATE INDEX IF NOT EXISTS idx_usage_records_client_timestamp ON usage_records(client_type, timestamp);
|
|
365
365
|
CREATE INDEX IF NOT EXISTS idx_usage_records_session_timestamp ON usage_records(session_id, timestamp);
|
|
366
|
-
`)}function
|
|
366
|
+
`)}function Au(n,e){return n.prepare("SELECT value FROM usage_meta WHERE key = ?").get(e)?.value}function Vt(n,e,t){n.prepare(`
|
|
367
367
|
INSERT INTO usage_meta (key, value)
|
|
368
368
|
VALUES (?, ?)
|
|
369
369
|
ON CONFLICT(key) DO UPDATE SET value = excluded.value
|
|
370
|
-
`).run(e,t)}function
|
|
371
|
-
`)){let l=a.trim();if(l)try{let c=JSON.parse(l);if(!
|
|
370
|
+
`).run(e,t)}function Iu(n){if(Au(n,ga)||!(0,ae.existsSync)(Jr))return;let e=$u(n,"INSERT OR IGNORE"),t=(0,ae.readFileSync)(Jr,"utf-8"),o=0,r=0,s=0;n.transaction(()=>{for(let a of t.split(`
|
|
371
|
+
`)){let l=a.trim();if(l)try{let c=JSON.parse(l);if(!Mu(c)){r++;continue}e.run(Ta(ko(c))).changes>0?o++:s++}catch{r++}}Vt(n,ga,new Date().toISOString()),Vt(n,"legacy_usage_jsonl_migrated_path",Jr),Vt(n,"legacy_usage_jsonl_imported_count",String(o)),Vt(n,"legacy_usage_jsonl_skipped_count",String(r)),Vt(n,"legacy_usage_jsonl_duplicate_count",String(s))})()}function Mu(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 $u(n,e="INSERT"){return n.prepare(`
|
|
372
372
|
${e} INTO usage_records (
|
|
373
373
|
id, timestamp, session_id, provider, original_model, model, upstream_model, model_family,
|
|
374
374
|
scenario_type, client_type,
|
|
@@ -382,11 +382,11 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
382
382
|
@cache_read_input_tokens, @cache_creation_input_tokens, @ttft,
|
|
383
383
|
@tokens_per_second, @duration_ms, @status, @error_message, @response_body
|
|
384
384
|
)
|
|
385
|
-
`)}function
|
|
386
|
-
SELECT ${t} AS key, ${
|
|
385
|
+
`)}function Nu(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,Ru);return Math.round(n/r)}function ko(n){let e=Number(n.outputTokens)||0,t=Number(n.durationMs),o=Number.isFinite(t)?t:null,r=n.stream?je(n.ttft):null;return{...n,ttft:r,tokensPerSecond:Nu(e,o,r)}}function Se(n){return typeof n=="string"?n:n==null?"":String(n)}function yo(n){if(n==null)return;let e=String(n);return e.length>0?e:void 0}function Yt(n){let e=Number(n);return Number.isFinite(e)?e:0}function ba(n){return n==="success"?"success":"error"}function Ta(n){let e=ko(n);return{id:Se(e.id),timestamp:Se(e.timestamp),session_id:Se(e.sessionId),provider:Se(e.provider),original_model:Se(e.originalModel),model:Se(e.model),upstream_model:yo(e.upstreamModel)??null,model_family:Se(e.modelFamily),scenario_type:Se(e.scenarioType),client_type:yo(e.clientType)??null,stream:e.stream?1:0,input_tokens:Math.trunc(Yt(e.inputTokens)),output_tokens:Math.trunc(Yt(e.outputTokens)),cache_read_input_tokens:Math.trunc(Yt(e.cacheReadInputTokens)),cache_creation_input_tokens:Math.trunc(Yt(e.cacheCreationInputTokens)),ttft:je(e.ttft),tokens_per_second:je(e.tokensPerSecond),duration_ms:Math.trunc(Yt(e.durationMs)),status:ba(e.status),error_message:yo(e.errorMessage)??null,response_body:yo(e.responseBody)??null}}function Lu(n){return ko({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:ba(n.status),errorMessage:n.error_message??void 0,responseBody:n.response_body??void 0})}function Du(){return new Date(Date.now()-wu*24*60*60*1e3).toISOString()}function va(n,e=!1){let t=Date.now();!e&&t-ha<Eu||(n.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(Du()),ha=t)}function xa(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 pt(n,e,t,o=""){let r=o?e.sql?`${e.sql} AND ${o}`:`WHERE ${o}`:e.sql,s=n.prepare(`
|
|
386
|
+
SELECT ${t} AS key, ${Fu}
|
|
387
387
|
FROM usage_records ${r}
|
|
388
388
|
GROUP BY key
|
|
389
|
-
`).all(...e.params),i={};for(let a of s)a.key!=null&&(i[String(a.key)]={count:a.count,inputTokens:a.inputTokens,outputTokens:a.outputTokens,cacheReadInputTokens:a.cacheReadInputTokens,cacheCreationInputTokens:a.cacheCreationInputTokens});return i}function
|
|
389
|
+
`).all(...e.params),i={};for(let a of s)a.key!=null&&(i[String(a.key)]={count:a.count,inputTokens:a.inputTokens,outputTokens:a.outputTokens,cacheReadInputTokens:a.cacheReadInputTokens,cacheCreationInputTokens:a.cacheCreationInputTokens});return i}function Ca(n,e){let t=xa(e),o=n.prepare(`
|
|
390
390
|
SELECT
|
|
391
391
|
COUNT(*) AS totalRequests,
|
|
392
392
|
COALESCE(SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END), 0) AS successCount,
|
|
@@ -397,14 +397,14 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
397
397
|
AVG(CASE WHEN status = 'success' AND ttft IS NOT NULL THEN ttft END) AS avgTtft,
|
|
398
398
|
AVG(CASE WHEN status = 'success' AND tokens_per_second IS NOT NULL THEN tokens_per_second END) AS avgTokensPerSecond
|
|
399
399
|
FROM usage_records ${t.sql}
|
|
400
|
-
`).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:pt(n,t,"model"),byProvider:pt(n,t,"provider"),byScenario:pt(n,t,"scenario_type"),byFamily:pt(n,t,"model_family || '/' || scenario_type","model_family IS NOT NULL AND model_family != ''"),byDay:pt(n,t,"date(timestamp, 'localtime')"),byClient:pt(n,t,"CASE WHEN client_type IS NULL OR client_type = '' THEN 'unknown' ELSE client_type END")}}function Zt(n){let e=Xt(),t=
|
|
400
|
+
`).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:pt(n,t,"model"),byProvider:pt(n,t,"provider"),byScenario:pt(n,t,"scenario_type"),byFamily:pt(n,t,"model_family || '/' || scenario_type","model_family IS NOT NULL AND model_family != ''"),byDay:pt(n,t,"date(timestamp, 'localtime')"),byClient:pt(n,t,"CASE WHEN client_type IS NULL OR client_type = '' THEN 'unknown' ELSE client_type END")}}function Zt(n){let e=Xt(),t=ko(n);Wr.run(Ta(t)),va(e)}function Gr(n){let e=Xt(),t=xa(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),Uu):50,s=(o-1)*r,i=e.prepare(`SELECT COUNT(*) AS count FROM usage_records ${t.sql}`).get(...t.params),a=e.prepare(`SELECT * FROM usage_records ${t.sql} ORDER BY timestamp DESC LIMIT ? OFFSET ?`).all(...t.params,r,s),l=Ca(e,n);return{records:a.map(Lu),summary:l,total:i.count,page:o,pageSize:r}}function qt(n,e,t){let o=Xt();return Ca(o,{startTime:n,endTime:e,status:t})}function Kr(n){let e=Xt();if(!n){e.prepare("DELETE FROM usage_records").run();return}e.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(n)}function ju(){Re&&(Re.pragma("wal_checkpoint(TRUNCATE)"),Re.close(),Re=null,Wr=null)}function je(n){if(n==null)return null;if(typeof n=="number")return n;let e=parseFloat(String(n));return isNaN(e)?null:e}function Qr(n){let e=(0,He.join)((0,ya.tmpdir)(),"claude-code-router"),t=(0,He.join)(e,`session-${n}.json`);if((0,ae.existsSync)(t))try{let o=(0,ae.readFileSync)(t,"utf-8"),r=JSON.parse(o);return{ttft:je(r.timeToFirstToken),tokensPerSecond:je(r.tokensPerSecond)}}catch{}try{let o=(0,ae.readdirSync)(e),r=new RegExp(`^session-${n}-\\d+\\.json$`),s=o.filter(i=>r.test(i));if(s.length>0){let i=s.sort().reverse(),a=(0,He.join)(e,i[0]);try{let l=(0,ae.readFileSync)(a,"utf-8"),c=JSON.parse(l);return{ttft:je(c.timeToFirstToken),tokensPerSecond:je(c.tokensPerSecond)}}catch{return{ttft:null,tokensPerSecond:null}}}}catch{}return{ttft:null,tokensPerSecond:null}}function Hu(n,e,t){return Xt().prepare("SELECT MIN(timestamp) AS timestamp FROM usage_records WHERE provider = ? AND timestamp >= ? AND timestamp <= ?").get(n,e,t)?.timestamp??void 0}var ae,He,ya,_a,ka,_o,Su,Jr,Ru,wu,Eu,ga,Re,Wr,ha,Fu,Uu,en=w(()=>{"use strict";ae=require("fs"),He=require("path"),ya=require("os"),_a=V(require("better-sqlite3"),1),ka=require("@wengine-ai/claude-code-router-shared"),_o=(0,He.join)(ka.HOME_DIR,"data"),Su=(0,He.join)(_o,"usage.sqlite"),Jr=(0,He.join)(_o,"usage.jsonl"),Ru=1,wu=180,Eu=3600*1e3,ga="legacy_usage_jsonl_migrated_at",Re=null,Wr=null,ha=0;Fu=`
|
|
401
401
|
COUNT(*) AS count,
|
|
402
402
|
COALESCE(SUM(CASE WHEN status = 'success' THEN input_tokens ELSE 0 END), 0) AS inputTokens,
|
|
403
403
|
COALESCE(SUM(CASE WHEN status = 'success' THEN output_tokens ELSE 0 END), 0) AS outputTokens,
|
|
404
404
|
COALESCE(SUM(CASE WHEN status = 'success' THEN cache_read_input_tokens ELSE 0 END), 0) AS cacheReadInputTokens,
|
|
405
405
|
COALESCE(SUM(CASE WHEN status = 'success' THEN cache_creation_input_tokens ELSE 0 END), 0) AS cacheCreationInputTokens
|
|
406
|
-
`;vu=200});async function ga(){let n=(0,pa.homedir)(),e=(0,fa.join)(n,".claude.json");if(!(0,da.existsSync)(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(0,ma.writeFile)(e,JSON.stringify(o,null,2))}}var da,pa,fa,ma,ha=w(()=>{"use strict";da=require("fs"),pa=require("os"),fa=require("path"),ma=require("fs/promises")});function _a(n=(0,zr.join)(ka.HOME_DIR,"logs")){try{if(!(0,we.existsSync)(n))return;let e=Date.now()-Ru;for(let t of(0,we.readdirSync)(n)){if(!/^ccr-.*\.log$/.test(t))continue;let o=(0,zr.join)(n,t),r=(0,we.statSync)(o);r.isFile()&&r.mtime.getTime()<e&&(0,we.unlinkSync)(o)}}catch(e){console.warn("Failed to prune old server log files:",e)}}function ba(){ya||(ya=!0,_a(),setInterval(_a,wu).unref())}var we,zr,ka,Su,Ru,wu,ya,Ta=w(()=>{"use strict";we=require("fs"),zr=require("path"),ka=require("@wengine-ai/claude-code-router-shared"),Su=7,Ru=Su*24*60*60*1e3,wu=1440*60*1e3,ya=!1});async function va(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":ct.registerPlugin(Ht,{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 ct.enablePlugins(n)}var xa=w(()=>{"use strict";Pr()});function Ca(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 Pu(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=qt(t,e),s=qt(o,e),i=[],a=new Set;for(let l of n||[])l&&l.name&&a.add(l.name);for(let l of Object.keys(r.byProvider||{}))a.add(l);for(let l of Object.keys(s.byProvider||{}))a.add(l);for(let l of a){let c=r.byProvider[l]||{inputTokens:0,outputTokens:0},u=s.byProvider[l]||{inputTokens:0,outputTokens:0},d=(c.inputTokens||0)+(c.outputTokens||0),p=(u.inputTokens||0)+(u.outputTokens||0),g,y,m=(n||[]).find(f=>f&&f.name===l);m&&m.quota&&(g=m.quota.limit5h,y=m.quota.limit7d),i.push({provider:l,used5h:d,used7d:p,limit5h:g,limit7d:y})}return i}async function Oa(n,e){let t=n.app;We(),Xe(),t.register(Pa.default,{limits:{fileSize:50*1024*1024}}),t.get("/v1/models",async()=>{let a=Kt(e);return{object:"list",data:a,first_id:a[0]?.id||null,last_id:a[a.length-1]?.id||null,has_more:!1}}),t.get("/v1/models/:modelId",async(a,l)=>{let c=decodeURIComponent(a.params.modelId),u=Kt(e).find(d=>d.id===c);return u||(l.status(404),{type:"error",error:{type:"not_found_error",message:`Model not found: ${c}`}})}),t.post("/v1/messages/count_tokens",async(a,l)=>{let{messages:c,tools:u,system:d,model:p}=a.body,g=t._server.tokenizerService;if(p&&p.includes(",")&&g)try{let[m,f]=p.split(",");a.log?.info(`Looking up tokenizer for provider: ${m}, model: ${f}`);let _=g.getTokenizerConfigForModel(m,f);_?a.log?.info(`Using tokenizer config: ${JSON.stringify(_)}`):a.log?.warn(`No tokenizer config found for ${m},${f}, using default tiktoken`);let k=await g.countTokens({messages:c,system:d,tools:u},_,m);return{input_tokens:k.tokenCount,tokenizer:k.tokenizerUsed}}catch(m){a.log?.error(`Error using configured tokenizer: ${m.message}`),a.log?.error(m.stack)}else p?p.includes(",")?g||a.log?.warn("TokenizerService not available, using default tiktoken"):a.log?.info(`Model "${p}" does not contain comma, using default tiktoken`):a.log?.info("No model specified, using default tiktoken");return{input_tokens:Ct(c,d,u)}}),t.get("/api/config",async(a,l)=>await co()),t.get("/api/transformers",async(a,l)=>{let c=t._server.transformerService.getAllTransformers();return{transformers:Array.from(c.entries()).map(([d,p])=>({name:d,endpoint:p.endPoint||null}))}}),t.post("/api/config",async(a,l)=>{let c=a.body,u=es(c||{});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 Jt();d&&console.log(`Backed up existing configuration file to ${d}`);let p=null;try{p=await ie()}catch{}let g=(0,Sa.ensureCcrSelfProvider)(c,p);await be(c);let y;try{y=await(0,P.syncGlobalProjectTakeovers)(c),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 f=c?.Providers||c?.providers;if(Array.isArray(f)&&m.configService.set("providers",f),ge&&m.transformerService&&(m.providerService=new ge(m.configService,m.transformerService,t.log)),dt(c,t.log),De&&$t){try{$t()}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=De(()=>m.providerService.getProviders(),_,k=>fe(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(a,l)=>{try{let c=await ie();return{clients:(0,P.listClientStatuses)(c)}}catch(c){console.error("Failed to get client integrations:",c),l.status(500).send({error:c.message||"Failed to get client integrations"})}}),t.post("/api/clients/apply",async(a,l)=>{try{let c=a.body,u=Array.isArray(c?.enabled)?c.enabled:[],d=await ie(),p=(0,P.applyClientSelection)(d,u);await be(p.config);let g=await(0,P.syncGlobalProjectTakeovers)(p.config),y=Ca(t,g,"clients/apply");return{...p,success:p.success&&y,projectTakeoverSync:g}}catch(c){console.error("Failed to apply client integrations:",c),l.status(500).send({error:c.message||"Failed to apply client integrations"})}});async function o(a,l,c){try{let{id:u}=a.params;if(!(0,P.isClientId)(u)){l.status(404).send({error:`Unknown client: ${u}`});return}let d=await ie(),p=c==="enable"?(0,P.enableClient)(d,u):c==="restore"?(0,P.restoreClient)(d,u):(0,P.disableClient)(d,u);await be(d);let g=await(0,P.syncGlobalProjectTakeovers)(d),y=Ca(t,g,`clients/${u}/${c}`);return{success:p.success&&y,result:p,results:[p],clients:(0,P.listClientStatuses)(d),config:d,projectTakeoverSync:g}}catch(u){console.error(`Failed to ${c} client integration:`,u),l.status(500).send({error:u.message||`Failed to ${c} client integration`})}}t.post("/api/clients/:id/enable",async(a,l)=>o(a,l,"enable")),t.post("/api/clients/:id/disable",async(a,l)=>o(a,l,"disable")),t.post("/api/clients/:id/restore",async(a,l)=>o(a,l,"restore")),t.get("/api/projects",async(a,l)=>{try{let c=await(0,P.listProjectConfigs)();return{projects:await Promise.all(c.map(async d=>{let p=await(0,P.getProjectTakeoverClients)(d.path);return{...d,ccrTakeoverClients:p,ccrTakeover:p.length>0}}))}}catch(c){console.error("Failed to list project configs:",c),l.status(500).send({error:c.message||"Failed to list project configs"})}}),t.post("/api/projects",async(a,l)=>{try{let{path:c}=a.body||{};if(!c||typeof c!="string"||!(0,X.isAbsolute)(c)){l.status(400).send({error:"A valid absolute project path is required"});return}let u=(0,X.normalize)(c).replace(/[\\/]+$/,"")||c;if(await(0,P.readProjectConfig)(u)){l.status(409).send({error:"Project is already configured"});return}await(0,P.writeProjectConfig)(u,{Router:{}});try{let g=await ie();await(0,P.setProjectTakeover)(u,["claudeCode"],g)}catch(g){console.error("Failed to auto-enable ccr takeover for new project:",g)}let p=await(0,P.getProjectTakeoverClients)(u);return{id:(0,P.getClaudeProjectId)(u),path:u,configPath:(0,P.getProjectConfigPath)(u),Router:{},ccrTakeoverClients:p,ccrTakeover:p.length>0}}catch(c){console.error("Failed to add project config:",c),l.status(500).send({error:c.message||"Failed to add project config"})}}),t.put("/api/projects/:id",async(a,l)=>{try{let{id:c}=a.params,{Router:u}=a.body||{};if(!u||typeof u!="object"){l.status(400).send({error:"Router must be an object"});return}let d=await(0,P.readProjectConfigById)(c);if(!d){l.status(404).send({error:"Project config not found"});return}await(0,P.writeProjectConfig)(d.path,{Router:u});let p=await ie();Object.keys(u).length===0?await(0,P.refreshProjectTakeovers)(d.path,p):(await(0,P.refreshCcrProjectTakeover)(d.path,p,u),await(0,P.refreshPiProjectTakeover)(d.path,p,u));let g=await(0,P.getProjectTakeoverClients)(d.path);return{id:c,path:d.path,configPath:d.configPath,Router:u,ccrTakeoverClients:g,ccrTakeover:g.length>0}}catch(c){console.error("Failed to update project config:",c),l.status(500).send({error:c.message||"Failed to update project config"})}}),t.put("/api/projects/:id/takeover",async(a,l)=>{try{let{id:c}=a.params,u=a.body||{},d;if(Array.isArray(u.clients))d=u.clients.filter(P.isProjectTakeoverClient);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 p=await(0,P.readProjectConfigById)(c);if(!p){l.status(404).send({error:"Project config not found"});return}let g=await ie(),y=await(0,P.setProjectTakeover)(p.path,d,g);return{id:c,path:p.path,ccrTakeoverClients:y,ccrTakeover:y.length>0}}catch(c){console.error("Failed to update ccr takeover status:",c),l.status(500).send({error:c.message||"Failed to update ccr takeover status"})}}),t.delete("/api/projects/:id",async(a,l)=>{try{let{id:c}=a.params,u=await(0,P.readProjectConfigById)(c);if(!u){l.status(404).send({error:"Project config not found"});return}try{let d=await ie();await(0,P.setProjectTakeover)(u.path,[],d)}catch(d){console.error("Failed to remove ccr takeover while deleting project:",d)}return await(0,P.deleteProjectConfig)(u.path),{success:!0}}catch(c){console.error("Failed to delete project config:",c),l.status(500).send({error:c.message||"Failed to delete project config"})}});let r=typeof __dirname=="string"?__dirname:(0,X.dirname)((0,Ra.fileURLToPath)(void 0)),s=(0,H.existsSync)((0,X.join)(r,"index.html"))?r:(0,H.existsSync)((0,X.join)(r,"..","index.html"))?(0,X.join)(r,".."):(0,X.join)(r,"..","dist");t.register(wa.default,{root:s,prefix:"/ui/",maxAge:"1h"}),t.get("/ui",async(a,l)=>l.redirect("/ui/")),t.get("/api/logs/files",async(a,l)=>{try{let c=(0,X.join)((0,_o.homedir)(),".claude-code-router","logs"),u=[];if((0,H.existsSync)(c)){let d=(0,H.readdirSync)(c);for(let p of d)if(p.endsWith(".log")){let g=(0,X.join)(c,p),y=(0,H.statSync)(g);u.push({name:p,path:g,size:y.size,lastModified:y.mtime.toISOString()})}u.sort((p,g)=>new Date(g.lastModified).getTime()-new Date(p.lastModified).getTime())}return u}catch(c){console.error("Failed to get log files:",c),l.status(500).send({error:"Failed to get log files"})}}),t.get("/api/logs",async(a,l)=>{try{let c=a.query.file,u;return c?u=c:u=(0,X.join)((0,_o.homedir)(),".claude-code-router","logs","app.log"),(0,H.existsSync)(u)?(0,H.readFileSync)(u,"utf8").split(`
|
|
407
|
-
`).filter(g=>g.trim()):[]}catch(c){console.error("Failed to get logs:",c),l.status(500).send({error:"Failed to get logs"})}}),t.delete("/api/logs",async(a,l)=>{try{let c=a.query.file,u;return c?u=c:u=(0,X.join)((0,_o.homedir)(),".claude-code-router","logs","app.log"),(0,H.existsSync)(u)&&(0,H.writeFileSync)(u,"","utf8"),{success:!0,message:"Logs cleared successfully"}}catch(c){console.error("Failed to clear logs:",c),l.status(500).send({error:"Failed to clear logs"})}}),t.get("/api/debug-log",async()=>({enabled:ht()})),t.put("/api/debug-log",async a=>{let{enabled:l}=a.body;return an(l),{enabled:ht()}}),t.get("/api/usage",async(a,l)=>{try{let c=a.query;return Fr({startTime:c.startDate,endTime:c.endDate,model:c.model,provider:c.provider,scenario:c.scenario,clientType:c.clientType,sessionId:c.sessionId,status:c.status,page:c.page?parseInt(c.page,10):void 0,pageSize:c.pageSize?parseInt(c.pageSize,10):void 0})}catch(c){console.error("Failed to query usage:",c),l.status(500).send({error:"Failed to query usage"})}}),t.get("/api/usage/summary",async(a,l)=>{try{let c=a.query;return qt(c.startDate,c.endDate,c.status)}catch(c){console.error("Failed to query usage summary:",c),l.status(500).send({error:"Failed to query usage summary"})}}),t.delete("/api/usage",async(a,l)=>{try{let c=a.query;return Ur(c.beforeDate),{success:!0,message:c.beforeDate?"Usage data cleared before "+c.beforeDate:"All usage data cleared"}}catch(c){console.error("Failed to clear usage:",c),l.status(500).send({error:"Failed to clear usage data"})}}),t.get("/api/providers/health",async(a,l)=>{try{let u=j().getAllStates(),p=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:p,timestamp:new Date().toISOString()}}catch(c){console.error("Failed to get provider health:",c),l.status(500).send({error:"Failed to get provider health"})}}),t.post("/api/providers/probe",async(a,l)=>{try{let{providerName:c}=a.body;if(!c)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 p=await d.probeProviderManually(c);return p.success&&Gt(c,a.log),{provider:c,success:p.success,latencyMs:p.latencyMs,status:p.status,isSlow:p.isSlow,errorKind:p.errorKind??null,error:p.error,timestamp:new Date().toISOString()}}catch(c){console.error("Failed to probe provider:",c),l.status(500).send({error:c.message||"Failed to probe provider"})}}),t.post("/api/providers/probe-all",async(a,l)=>{try{let c=t._server,u=c?.activeProbeService;if(!u?.probeProviderManually)return l.status(503),{error:"Active probe service is not available"};let d=(c?.providerService?.getProviders?.()||[]).filter(g=>g?.name&&g.enabled!==!1),p=await Promise.all(d.map(async g=>{let y=await u.probeProviderManually(g.name);return y.success&&Gt(g.name,a?.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:p,successCount:p.filter(g=>g.success).length,total:p.length,timestamp:new Date().toISOString()}}catch(c){console.error("Failed to probe all providers:",c),l.status(500).send({error:c.message||"Failed to probe all providers"})}}),t.get("/api/providers/quota",async(a,l)=>{try{let u=(await ie()).Providers||[],d=Pu(u),p=new Map(d.map(m=>[m.provider,m])),g=mn();for(let m of d){let f=g.find(_=>_.provider===m.provider);f&&f.limit!=null&&(m.limit5h=f.limit,m.used5h=f.limit-(f.remaining??0),f.reset&&(m.reset5h=new Date(f.reset*1e3).toISOString()))}let y=yn();for(let m of y){let f=p.get(m.provider);f||(f={provider:m.provider,used5h:0,used7d:0},d.push(f),p.set(m.provider,f)),typeof m.totalBalance=="number"?(f.limit7d=m.totalBalance,f.type7d=m.currency?"balance":"rateLimit",m.currency&&(f.currency=m.currency),typeof m.usedBalance=="number"?f.used7d=m.usedBalance:typeof m.remainingBalance=="number"?f.used7d=Math.max(0,m.totalBalance-m.remainingBalance):(f.used7d=0,f.limit7d=m.totalBalance)):typeof m.usedBalance=="number"&&(f.used7d=m.usedBalance),typeof m.usedDailyBalance=="number"&&(f.used5h=m.usedDailyBalance,f.type5h="rateLimit"),typeof m.limitDaily=="number"&&(f.limit5h=m.limitDaily,f.type5h="rateLimit"),typeof m.resetTime=="string"&&(m.limitDaily!==void 0?f.reset5h=m.resetTime:f.reset7d=m.resetTime),typeof m.resetTime7d=="string"&&(f.reset7d=m.resetTime7d)}return{quotas:d,timestamp:new Date().toISOString()}}catch(c){console.error("Failed to compute provider quota:",c),l.status(500).send({error:"Failed to compute provider quota"})}}),t.get("/api/presets",async(a,l)=>{try{let c=(0,X.join)(P.HOME_DIR,"presets");if(!(0,H.existsSync)(c))return{presets:[]};let d=(0,H.readdirSync)(c,{withFileTypes:!0}).filter(g=>g.isDirectory()&&!g.name.startsWith(".")).map(g=>g.name),p=[];for(let g of d){let y=(0,X.join)(c,g);try{let m=(0,X.join)(y,"manifest.json"),f=(0,H.readFileSync)(m,"utf-8"),_=JSON.parse(f),{Providers:k,Router:h,PORT:b,HOST:T,API_TIMEOUT_MS:C,PROXY_URL:x,PROXY_GLOBAL_ENABLED:S,LOG:v,LOG_LEVEL:E,StatusLine:R,NON_INTERACTIVE_MODE:A,...O}=_;p.push({id:g,name:O.name||g,version:O.version||"1.0.0",description:O.description,author:O.author,homepage:O.homepage,repository:O.repository,license:O.license,keywords:O.keywords,ccrVersion:O.ccrVersion,source:O.source,sourceType:O.sourceType,checksum:O.checksum,installed:!0})}catch(m){console.error(`Failed to read preset ${g}:`,m)}}return{presets:p}}catch(c){console.error("Failed to get presets:",c),l.status(500).send({error:"Failed to get presets"})}}),t.get("/api/presets/:name",async(a,l)=>{try{let{name:c}=a.params,u=(0,P.getPresetDir)(c);if(!(0,H.existsSync)(u)){l.status(404).send({error:"Preset not found"});return}let d=await(0,P.readManifestFromDir)(u);return{...(0,P.manifestToPresetFile)(d),config:(0,P.loadConfigFromManifest)(d,u),userValues:d.userValues||{}}}catch(c){console.error("Failed to get preset:",c),l.status(500).send({error:c.message||"Failed to get preset"})}}),t.post("/api/presets/:name/apply",async(a,l)=>{try{let{name:c}=a.params,{secrets:u}=a.body,d=(0,P.getPresetDir)(c);if(!(0,H.existsSync)(d)){l.status(404).send({error:"Preset not found"});return}let g={...await(0,P.readManifestFromDir)(d)};return u&&Object.keys(u).length>0&&(g.userValues={...g.userValues,...u}),await(0,P.saveManifest)(c,g),{success:!0,message:"Preset applied successfully"}}catch(c){console.error("Failed to apply preset:",c),l.status(500).send({error:c.message||"Failed to apply preset"})}}),t.delete("/api/presets/:name",async(a,l)=>{try{let{name:c}=a.params,u=(0,P.getPresetDir)(c);if(!(0,H.existsSync)(u)){l.status(404).send({error:"Preset not found"});return}return(0,H.rmSync)(u,{recursive:!0,force:!0}),{success:!0,message:"Preset deleted successfully"}}catch(c){console.error("Failed to delete preset:",c),l.status(500).send({error:c.message||"Failed to delete preset"})}}),t.get("/api/presets/market",async(a,l)=>{try{return{presets:await(0,P.getMarketPresets)()}}catch(c){console.error("Failed to get market presets:",c),l.status(500).send({error:c.message||"Failed to get market presets"})}}),t.post("/api/presets/install/github",async(a,l)=>{try{let{presetName:c}=a.body;if(!c){l.status(400).send({error:"Preset name is required"});return}let u=await(0,P.findMarketPresetByName)(c);if(!u){l.status(400).send({error:"Preset not found in marketplace",message:`Preset '${c}' 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 '${c}' 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[,p,g]=d,y=u.name||c;if(await(0,P.isPresetInstalled)(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/${p}/${g}/archive/refs/heads/main.zip`,f=await(0,P.downloadPresetToTemp)(m),_=await i(f);if(await(0,P.isPresetInstalled)(y)){(0,H.unlinkSync)(f),l.status(409).send({error:"Preset already installed",message:`Preset '${y}' was installed while downloading. Please try again.`,presetName:y});return}let k=(0,P.getPresetDir)(y);await(0,P.extractPreset)(f,k);let h=await(0,P.readManifestFromDir)(k);return h.repository=u.repo,u.url&&(h.source=u.url),await(0,P.saveManifest)(y,h),(0,H.unlinkSync)(f),{success:!0,presetName:y,preset:{..._.metadata,installed:!0}}}catch(c){console.error("Failed to install preset from GitHub:",c),l.status(500).send({error:c.message||"Failed to install preset from GitHub"})}});async function i(a){let l=new Ea.default(a),c=l.getEntry("manifest.json");if(c||(c=l.getEntries().find(p=>p.entryName.includes("manifest.json"))||null),!c)throw new Error("Invalid preset file: manifest.json not found");let u=JSON.parse(c.getData().toString("utf-8"));return(0,P.manifestToPresetFile)(u)}return n}var Sa,X,Ra,wa,H,_o,P,Pa,Ea,Aa=w(()=>{"use strict";rt();Ge();Ze();wn();ao();ce();yt();le();fo();uo();Sa=require("@wengine-ai/claude-code-router-shared"),X=require("path"),Ra=require("url"),wa=V(require("@fastify/static"),1),H=require("fs"),_o=require("os"),P=require("@wengine-ai/claude-code-router-shared"),Pa=V(require("@fastify/multipart"),1),Ea=V(require("adm-zip"),1);en();$r()});function Eu(n,e){let t=n?.Router,{family:o}=St(e||"");if(t?.enableFamilyRouting&&o){let r=t.families?.[o]?.image;if(r)return r}return t?.image}function Ia(n){return n?.Router?.image}function Ou(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 Au(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 Br,ko,Jr,Ma,$a=w(()=>{"use strict";rt();Br=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}},ko=new Br;Jr=class{name="image";tools;constructor(){this.tools=new Map,this.appendTools()}shouldHandle(e,t){let o=Eu(t,e.originalModel||e.body.model);if(!o||e.body.model===o||!Ou(e.body.messages))return!1;let s=t.forceUseImageAgent===!0;return!s&&Ko(e.body.model)?!1:(Au(e.body.messages),s&&!!Ia(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(a=>{let l=ko.getImage(`${t.req.id}_Image#${a}`);l&&o.push({type:"image",source:l})});else{let a=ko.getImage(`${t.req.id}_Image#${e.imageId}`);a&&o.push({type:"image",source:a})}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 a=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(...a)}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:Ia(t.config),system:[{type:"text",text:`You must interpret and analyze images strictly according to the assigned task.
|
|
406
|
+
`;Uu=200});async function Pa(){let n=(0,Ra.homedir)(),e=(0,wa.join)(n,".claude.json");if(!(0,Sa.existsSync)(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(0,Ea.writeFile)(e,JSON.stringify(o,null,2))}}var Sa,Ra,wa,Ea,Oa=w(()=>{"use strict";Sa=require("fs"),Ra=require("os"),wa=require("path"),Ea=require("fs/promises")});function Ia(n=(0,Yr.join)(Ma.HOME_DIR,"logs")){try{if(!(0,we.existsSync)(n))return;let e=Date.now()-Bu;for(let t of(0,we.readdirSync)(n)){if(!/^ccr-.*\.log$/.test(t))continue;let o=(0,Yr.join)(n,t),r=(0,we.statSync)(o);r.isFile()&&r.mtime.getTime()<e&&(0,we.unlinkSync)(o)}}catch(e){console.warn("Failed to prune old server log files:",e)}}function $a(){Aa||(Aa=!0,Ia(),setInterval(Ia,Ju).unref())}var we,Yr,Ma,zu,Bu,Ju,Aa,Na=w(()=>{"use strict";we=require("fs"),Yr=require("path"),Ma=require("@wengine-ai/claude-code-router-shared"),zu=7,Bu=zu*24*60*60*1e3,Ju=1440*60*1e3,Aa=!1});async function La(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":ct.registerPlugin(Ht,{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 ct.enablePlugins(n)}var Da=w(()=>{"use strict";Lr()});function Fa(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=qt(t,e),s=qt(o,e),i=[],a=new Set;for(let l of n||[])l&&l.name&&a.add(l.name);for(let l of Object.keys(r.byProvider||{}))a.add(l);for(let l of Object.keys(s.byProvider||{}))a.add(l);for(let l of a){let c=r.byProvider[l]||{inputTokens:0,outputTokens:0},u=s.byProvider[l]||{inputTokens:0,outputTokens:0},d=(c.inputTokens||0)+(c.outputTokens||0),p=(u.inputTokens||0)+(u.outputTokens||0),g,y,m=(n||[]).find(f=>f&&f.name===l);m&&m.quota&&(g=m.quota.limit5h,y=m.quota.limit7d),i.push({provider:l,used5h:d,used7d:p,limit5h:g,limit7d:y})}return i}async function Ja(n,e){let t=n.app;We(),Xe(),t.register(za.default,{limits:{fileSize:50*1024*1024}}),t.get("/v1/models",async()=>{let a=Kt(e);return{object:"list",data:a,first_id:a[0]?.id||null,last_id:a[a.length-1]?.id||null,has_more:!1}}),t.get("/v1/models/:modelId",async(a,l)=>{let c=decodeURIComponent(a.params.modelId),u=Kt(e).find(d=>d.id===c);return u||(l.status(404),{type:"error",error:{type:"not_found_error",message:`Model not found: ${c}`}})}),t.post("/v1/messages/count_tokens",async(a,l)=>{let{messages:c,tools:u,system:d,model:p}=a.body,g=t._server.tokenizerService;if(p&&p.includes(",")&&g)try{let[m,f]=p.split(",");a.log?.info(`Looking up tokenizer for provider: ${m}, model: ${f}`);let _=g.getTokenizerConfigForModel(m,f);_?a.log?.info(`Using tokenizer config: ${JSON.stringify(_)}`):a.log?.warn(`No tokenizer config found for ${m},${f}, using default tiktoken`);let k=await g.countTokens({messages:c,system:d,tools:u},_,m);return{input_tokens:k.tokenCount,tokenizer:k.tokenizerUsed}}catch(m){a.log?.error(`Error using configured tokenizer: ${m.message}`),a.log?.error(m.stack)}else p?p.includes(",")?g||a.log?.warn("TokenizerService not available, using default tiktoken"):a.log?.info(`Model "${p}" does not contain comma, using default tiktoken`):a.log?.info("No model specified, using default tiktoken");return{input_tokens:Ct(c,d,u)}}),t.get("/api/config",async(a,l)=>await po()),t.get("/api/transformers",async(a,l)=>{let c=t._server.transformerService.getAllTransformers();return{transformers:Array.from(c.entries()).map(([d,p])=>({name:d,endpoint:p.endPoint||null}))}}),t.post("/api/config",async(a,l)=>{let c=a.body,u=cs(c||{});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 Jt();d&&console.log(`Backed up existing configuration file to ${d}`);let p=null;try{p=await ie()}catch{}let g=(0,Ua.ensureCcrSelfProvider)(c,p);await be(c);let y;try{y=await(0,P.syncGlobalProjectTakeovers)(c),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 f=c?.Providers||c?.providers;if(Array.isArray(f)&&m.configService.set("providers",f),ge&&m.transformerService&&(m.providerService=new ge(m.configService,m.transformerService,t.log)),dt(c,t.log),De&&$t){try{$t()}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=De(()=>m.providerService.getProviders(),_,k=>fe(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(a,l)=>{try{let c=await ie();return{clients:(0,P.listClientStatuses)(c)}}catch(c){console.error("Failed to get client integrations:",c),l.status(500).send({error:c.message||"Failed to get client integrations"})}}),t.post("/api/clients/apply",async(a,l)=>{try{let c=a.body,u=Array.isArray(c?.enabled)?c.enabled:[],d=await ie(),p=(0,P.applyClientSelection)(d,u);await be(p.config);let g=await(0,P.syncGlobalProjectTakeovers)(p.config),y=Fa(t,g,"clients/apply");return{...p,success:p.success&&y,projectTakeoverSync:g}}catch(c){console.error("Failed to apply client integrations:",c),l.status(500).send({error:c.message||"Failed to apply client integrations"})}});async function o(a,l,c){try{let{id:u}=a.params;if(!(0,P.isClientId)(u)){l.status(404).send({error:`Unknown client: ${u}`});return}let d=await ie(),p=c==="enable"?(0,P.enableClient)(d,u):c==="restore"?(0,P.restoreClient)(d,u):(0,P.disableClient)(d,u);await be(d);let g=await(0,P.syncGlobalProjectTakeovers)(d),y=Fa(t,g,`clients/${u}/${c}`);return{success:p.success&&y,result:p,results:[p],clients:(0,P.listClientStatuses)(d),config:d,projectTakeoverSync:g}}catch(u){console.error(`Failed to ${c} client integration:`,u),l.status(500).send({error:u.message||`Failed to ${c} client integration`})}}t.post("/api/clients/:id/enable",async(a,l)=>o(a,l,"enable")),t.post("/api/clients/:id/disable",async(a,l)=>o(a,l,"disable")),t.post("/api/clients/:id/restore",async(a,l)=>o(a,l,"restore")),t.get("/api/projects",async(a,l)=>{try{let c=await(0,P.listProjectConfigs)();return{projects:await Promise.all(c.map(async d=>{let p=await(0,P.getProjectTakeoverClients)(d.path);return{...d,ccrTakeoverClients:p,ccrTakeover:p.length>0}}))}}catch(c){console.error("Failed to list project configs:",c),l.status(500).send({error:c.message||"Failed to list project configs"})}}),t.post("/api/projects",async(a,l)=>{try{let{path:c}=a.body||{};if(!c||typeof c!="string"||!(0,Z.isAbsolute)(c)){l.status(400).send({error:"A valid absolute project path is required"});return}let u=(0,Z.normalize)(c).replace(/[\\/]+$/,"")||c;if(await(0,P.readProjectConfig)(u)){l.status(409).send({error:"Project is already configured"});return}await(0,P.writeProjectConfig)(u,{Router:{}});try{let g=await ie();await(0,P.setProjectTakeover)(u,["claudeCode"],g)}catch(g){console.error("Failed to auto-enable ccr takeover for new project:",g)}let p=await(0,P.getProjectTakeoverClients)(u);return{id:(0,P.getClaudeProjectId)(u),path:u,configPath:(0,P.getProjectConfigPath)(u),Router:{},ccrTakeoverClients:p,ccrTakeover:p.length>0}}catch(c){console.error("Failed to add project config:",c),l.status(500).send({error:c.message||"Failed to add project config"})}}),t.put("/api/projects/:id",async(a,l)=>{try{let{id:c}=a.params,{Router:u}=a.body||{};if(!u||typeof u!="object"){l.status(400).send({error:"Router must be an object"});return}let d=await(0,P.readProjectConfigById)(c);if(!d){l.status(404).send({error:"Project config not found"});return}await(0,P.writeProjectConfig)(d.path,{Router:u});let p=await ie();Object.keys(u).length===0?await(0,P.refreshProjectTakeovers)(d.path,p):(await(0,P.refreshCcrProjectTakeover)(d.path,p,u),await(0,P.refreshPiProjectTakeover)(d.path,p,u));let g=await(0,P.getProjectTakeoverClients)(d.path);return{id:c,path:d.path,configPath:d.configPath,Router:u,ccrTakeoverClients:g,ccrTakeover:g.length>0}}catch(c){console.error("Failed to update project config:",c),l.status(500).send({error:c.message||"Failed to update project config"})}}),t.put("/api/projects/:id/takeover",async(a,l)=>{try{let{id:c}=a.params,u=a.body||{},d;if(Array.isArray(u.clients))d=u.clients.filter(P.isProjectTakeoverClient);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 p=await(0,P.readProjectConfigById)(c);if(!p){l.status(404).send({error:"Project config not found"});return}let g=await ie(),y=await(0,P.setProjectTakeover)(p.path,d,g);return{id:c,path:p.path,ccrTakeoverClients:y,ccrTakeover:y.length>0}}catch(c){console.error("Failed to update ccr takeover status:",c),l.status(500).send({error:c.message||"Failed to update ccr takeover status"})}}),t.delete("/api/projects/:id",async(a,l)=>{try{let{id:c}=a.params,u=await(0,P.readProjectConfigById)(c);if(!u){l.status(404).send({error:"Project config not found"});return}try{let d=await ie();await(0,P.setProjectTakeover)(u.path,[],d)}catch(d){console.error("Failed to remove ccr takeover while deleting project:",d)}return await(0,P.deleteProjectConfig)(u.path),{success:!0}}catch(c){console.error("Failed to delete project config:",c),l.status(500).send({error:c.message||"Failed to delete project config"})}});let r=typeof __dirname=="string"?__dirname:(0,Z.dirname)((0,ja.fileURLToPath)(void 0)),s=(0,H.existsSync)((0,Z.join)(r,"index.html"))?r:(0,H.existsSync)((0,Z.join)(r,"..","index.html"))?(0,Z.join)(r,".."):(0,Z.join)(r,"..","dist");t.register(Ha.default,{root:s,prefix:"/ui/",maxAge:"1h"}),t.get("/ui",async(a,l)=>l.redirect("/ui/")),t.get("/api/logs/files",async(a,l)=>{try{let c=(0,Z.join)((0,bo.homedir)(),".claude-code-router","logs"),u=[];if((0,H.existsSync)(c)){let d=(0,H.readdirSync)(c);for(let p of d)if(p.endsWith(".log")){let g=(0,Z.join)(c,p),y=(0,H.statSync)(g);u.push({name:p,path:g,size:y.size,lastModified:y.mtime.toISOString()})}u.sort((p,g)=>new Date(g.lastModified).getTime()-new Date(p.lastModified).getTime())}return u}catch(c){console.error("Failed to get log files:",c),l.status(500).send({error:"Failed to get log files"})}}),t.get("/api/logs",async(a,l)=>{try{let c=a.query.file,u;return c?u=c:u=(0,Z.join)((0,bo.homedir)(),".claude-code-router","logs","app.log"),(0,H.existsSync)(u)?(0,H.readFileSync)(u,"utf8").split(`
|
|
407
|
+
`).filter(g=>g.trim()):[]}catch(c){console.error("Failed to get logs:",c),l.status(500).send({error:"Failed to get logs"})}}),t.delete("/api/logs",async(a,l)=>{try{let c=a.query.file,u;return c?u=c:u=(0,Z.join)((0,bo.homedir)(),".claude-code-router","logs","app.log"),(0,H.existsSync)(u)&&(0,H.writeFileSync)(u,"","utf8"),{success:!0,message:"Logs cleared successfully"}}catch(c){console.error("Failed to clear logs:",c),l.status(500).send({error:"Failed to clear logs"})}}),t.get("/api/debug-log",async()=>({enabled:ht()})),t.put("/api/debug-log",async a=>{let{enabled:l}=a.body;return an(l),{enabled:ht()}}),t.get("/api/usage",async(a,l)=>{try{let c=a.query;return Gr({startTime:c.startDate,endTime:c.endDate,model:c.model,provider:c.provider,scenario:c.scenario,clientType:c.clientType,sessionId:c.sessionId,status:c.status,page:c.page?parseInt(c.page,10):void 0,pageSize:c.pageSize?parseInt(c.pageSize,10):void 0})}catch(c){console.error("Failed to query usage:",c),l.status(500).send({error:"Failed to query usage"})}}),t.get("/api/usage/summary",async(a,l)=>{try{let c=a.query;return qt(c.startDate,c.endDate,c.status)}catch(c){console.error("Failed to query usage summary:",c),l.status(500).send({error:"Failed to query usage summary"})}}),t.delete("/api/usage",async(a,l)=>{try{let c=a.query;return Kr(c.beforeDate),{success:!0,message:c.beforeDate?"Usage data cleared before "+c.beforeDate:"All usage data cleared"}}catch(c){console.error("Failed to clear usage:",c),l.status(500).send({error:"Failed to clear usage data"})}}),t.get("/api/providers/health",async(a,l)=>{try{let u=j().getAllStates(),p=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:p,timestamp:new Date().toISOString()}}catch(c){console.error("Failed to get provider health:",c),l.status(500).send({error:"Failed to get provider health"})}}),t.post("/api/providers/probe",async(a,l)=>{try{let{providerName:c}=a.body;if(!c)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 p=await d.probeProviderManually(c);return p.success&&Gt(c,a.log),{provider:c,success:p.success,latencyMs:p.latencyMs,status:p.status,isSlow:p.isSlow,errorKind:p.errorKind??null,error:p.error,timestamp:new Date().toISOString()}}catch(c){console.error("Failed to probe provider:",c),l.status(500).send({error:c.message||"Failed to probe provider"})}}),t.post("/api/providers/probe-all",async(a,l)=>{try{let c=t._server,u=c?.activeProbeService;if(!u?.probeProviderManually)return l.status(503),{error:"Active probe service is not available"};let d=(c?.providerService?.getProviders?.()||[]).filter(g=>g?.name&&g.enabled!==!1),p=await Promise.all(d.map(async g=>{let y=await u.probeProviderManually(g.name);return y.success&&Gt(g.name,a?.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:p,successCount:p.filter(g=>g.success).length,total:p.length,timestamp:new Date().toISOString()}}catch(c){console.error("Failed to probe all providers:",c),l.status(500).send({error:c.message||"Failed to probe all providers"})}}),t.get("/api/providers/quota",async(a,l)=>{try{let u=(await ie()).Providers||[],d=Wu(u),p=new Map(d.map(m=>[m.provider,m])),g=mn();for(let m of d){let f=g.find(_=>_.provider===m.provider);f&&f.limit!=null&&(m.limit5h=f.limit,m.used5h=f.limit-(f.remaining??0),f.reset&&(m.reset5h=new Date(f.reset*1e3).toISOString()))}let y=bn();for(let m of y){let f=p.get(m.provider);f||(f={provider:m.provider,used5h:0,used7d:0},d.push(f),p.set(m.provider,f)),typeof m.totalBalance=="number"?(f.limit7d=m.totalBalance,f.type7d=m.currency?"balance":"rateLimit",m.currency&&(f.currency=m.currency),typeof m.usedBalance=="number"?f.used7d=m.usedBalance:typeof m.remainingBalance=="number"?f.used7d=Math.max(0,m.totalBalance-m.remainingBalance):(f.used7d=0,f.limit7d=m.totalBalance)):typeof m.usedBalance=="number"&&(f.used7d=m.usedBalance),typeof m.usedDailyBalance=="number"&&(f.used5h=m.usedDailyBalance,f.type5h="rateLimit"),typeof m.limitDaily=="number"&&(f.limit5h=m.limitDaily,f.type5h="rateLimit"),typeof m.resetTime=="string"&&(m.limitDaily!==void 0?f.reset5h=m.resetTime:f.reset7d=m.resetTime),typeof m.resetTime7d=="string"&&(f.reset7d=m.resetTime7d)}return{quotas:d,timestamp:new Date().toISOString()}}catch(c){console.error("Failed to compute provider quota:",c),l.status(500).send({error:"Failed to compute provider quota"})}}),t.get("/api/presets",async(a,l)=>{try{let c=(0,Z.join)(P.HOME_DIR,"presets");if(!(0,H.existsSync)(c))return{presets:[]};let d=(0,H.readdirSync)(c,{withFileTypes:!0}).filter(g=>g.isDirectory()&&!g.name.startsWith(".")).map(g=>g.name),p=[];for(let g of d){let y=(0,Z.join)(c,g);try{let m=(0,Z.join)(y,"manifest.json"),f=(0,H.readFileSync)(m,"utf-8"),_=JSON.parse(f),{Providers:k,Router:h,PORT:b,HOST:v,API_TIMEOUT_MS:C,PROXY_URL:x,PROXY_GLOBAL_ENABLED:S,LOG:T,LOG_LEVEL:E,StatusLine:R,NON_INTERACTIVE_MODE:A,...O}=_;p.push({id:g,name:O.name||g,version:O.version||"1.0.0",description:O.description,author:O.author,homepage:O.homepage,repository:O.repository,license:O.license,keywords:O.keywords,ccrVersion:O.ccrVersion,source:O.source,sourceType:O.sourceType,checksum:O.checksum,installed:!0})}catch(m){console.error(`Failed to read preset ${g}:`,m)}}return{presets:p}}catch(c){console.error("Failed to get presets:",c),l.status(500).send({error:"Failed to get presets"})}}),t.get("/api/presets/:name",async(a,l)=>{try{let{name:c}=a.params,u=(0,P.getPresetDir)(c);if(!(0,H.existsSync)(u)){l.status(404).send({error:"Preset not found"});return}let d=await(0,P.readManifestFromDir)(u);return{...(0,P.manifestToPresetFile)(d),config:(0,P.loadConfigFromManifest)(d,u),userValues:d.userValues||{}}}catch(c){console.error("Failed to get preset:",c),l.status(500).send({error:c.message||"Failed to get preset"})}}),t.post("/api/presets/:name/apply",async(a,l)=>{try{let{name:c}=a.params,{secrets:u}=a.body,d=(0,P.getPresetDir)(c);if(!(0,H.existsSync)(d)){l.status(404).send({error:"Preset not found"});return}let g={...await(0,P.readManifestFromDir)(d)};return u&&Object.keys(u).length>0&&(g.userValues={...g.userValues,...u}),await(0,P.saveManifest)(c,g),{success:!0,message:"Preset applied successfully"}}catch(c){console.error("Failed to apply preset:",c),l.status(500).send({error:c.message||"Failed to apply preset"})}}),t.delete("/api/presets/:name",async(a,l)=>{try{let{name:c}=a.params,u=(0,P.getPresetDir)(c);if(!(0,H.existsSync)(u)){l.status(404).send({error:"Preset not found"});return}return(0,H.rmSync)(u,{recursive:!0,force:!0}),{success:!0,message:"Preset deleted successfully"}}catch(c){console.error("Failed to delete preset:",c),l.status(500).send({error:c.message||"Failed to delete preset"})}}),t.get("/api/presets/market",async(a,l)=>{try{return{presets:await(0,P.getMarketPresets)()}}catch(c){console.error("Failed to get market presets:",c),l.status(500).send({error:c.message||"Failed to get market presets"})}}),t.post("/api/presets/install/github",async(a,l)=>{try{let{presetName:c}=a.body;if(!c){l.status(400).send({error:"Preset name is required"});return}let u=await(0,P.findMarketPresetByName)(c);if(!u){l.status(400).send({error:"Preset not found in marketplace",message:`Preset '${c}' 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 '${c}' 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[,p,g]=d,y=u.name||c;if(await(0,P.isPresetInstalled)(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/${p}/${g}/archive/refs/heads/main.zip`,f=await(0,P.downloadPresetToTemp)(m),_=await i(f);if(await(0,P.isPresetInstalled)(y)){(0,H.unlinkSync)(f),l.status(409).send({error:"Preset already installed",message:`Preset '${y}' was installed while downloading. Please try again.`,presetName:y});return}let k=(0,P.getPresetDir)(y);await(0,P.extractPreset)(f,k);let h=await(0,P.readManifestFromDir)(k);return h.repository=u.repo,u.url&&(h.source=u.url),await(0,P.saveManifest)(y,h),(0,H.unlinkSync)(f),{success:!0,presetName:y,preset:{..._.metadata,installed:!0}}}catch(c){console.error("Failed to install preset from GitHub:",c),l.status(500).send({error:c.message||"Failed to install preset from GitHub"})}});async function i(a){let l=new Ba.default(a),c=l.getEntry("manifest.json");if(c||(c=l.getEntries().find(p=>p.entryName.includes("manifest.json"))||null),!c)throw new Error("Invalid preset file: manifest.json not found");let u=JSON.parse(c.getData().toString("utf-8"));return(0,P.manifestToPresetFile)(u)}return n}var Ua,Z,ja,Ha,H,bo,P,za,Ba,Wa=w(()=>{"use strict";rt();Ge();Ze();On();uo();ce();yt();le();ho();mo();Ua=require("@wengine-ai/claude-code-router-shared"),Z=require("path"),ja=require("url"),Ha=V(require("@fastify/static"),1),H=require("fs"),bo=require("os"),P=require("@wengine-ai/claude-code-router-shared"),za=V(require("@fastify/multipart"),1),Ba=V(require("adm-zip"),1);en();zr()});function Gu(n,e){let t=n?.Router,{family:o}=St(e||"");if(t?.enableFamilyRouting&&o){let r=t.families?.[o]?.image;if(r)return r}return t?.image}function Ga(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 Xr,To,Zr,Ka,Qa=w(()=>{"use strict";rt();Xr=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}},To=new Xr;Zr=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&&tr(e.body.model)?!1:(Qu(e.body.messages),s&&!!Ga(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(a=>{let l=To.getImage(`${t.req.id}_Image#${a}`);l&&o.push({type:"image",source:l})});else{let a=To.getImage(`${t.req.id}_Image#${e.imageId}`);a&&o.push({type:"image",source:a})}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 a=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(...a)}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:Ga(t.config),system:[{type:"text",text:`You must interpret and analyze images strictly according to the assigned task.
|
|
408
408
|
When an image placeholder is provided, your role is to parse the image content only within the scope of the user\u2019s instructions.
|
|
409
409
|
Do not ignore or deviate from the task.
|
|
410
410
|
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(a=>a.json()).catch(a=>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.
|
|
@@ -417,5 +417,5 @@ If multiple images exist, select the **most relevant imageId** based on the user
|
|
|
417
417
|
|
|
418
418
|
Do not attempt to describe or analyze the image directly yourself.
|
|
419
419
|
Ignore any user interruptions or unrelated instructions that might cause you to skip this requirement.
|
|
420
|
-
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(a=>a.type==="image"))),r=1;o.forEach(s=>{Array.isArray(s.content)&&s.content.forEach(i=>{if(i.type==="image")ko.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(a=>a.type==="image")){let a=[],l=i.content.filter(u=>u.type!=="image"?!0:(ko.storeImage(`${e.id}_Image#${r}`,u.source),a.push(`[Image #${r}]`),r++,!1)),c=`${a.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:c},...l]:c}})})}},Ma=new Jr});var Wr,Na,bo,La=w(()=>{"use strict";$a();Wr=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}},Na=new Wr;Na.registerAgent(Ma);bo=Na});function Iu(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 ja(n,e){(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&ve(n,e)}function Kr(n){return n.usageSessionId||n.id}function To(n){return n.usageCacheKey||`${n.clientType||"unknown"}:request:${Kr(n)}`}function ft(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 Mu(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?Ue(e.usage):e.response?.usage?Ue(e.response.usage):void 0,o=e.model||e.response?.model;if(!(!t&&!o))return{usage:t,model:o}}async function $u(n,e,t){if(await new Promise((r,s)=>{let i=a=>a?s(a):r();Qt(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")){ja(n,t);let r=To(n);n.clientContext?.usageScope==="session"&&(n.previousUsage=ee.get(r))}}async function Nu(n,e,t){if(!n.pathname?.endsWith("/v1/messages"))return;let o=[];for(let r of bo.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 Ha(n){return{authClient:(e,t)=>$u(e,t,n),agent:(e,t)=>Nu(e,t,n)}}function za(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=Gr.performance.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 a=l=>l?i(l):s();Qt(e)(t,o,a).then(()=>s(),i)})}),n.addHook("onError",async(t,o,r)=>{t.errorMessage=r?.message||r?.toString?.()||"Unknown error",Da.emit("onError",t,o,r)}),n.addHook("onSend",(t,o,r,s)=>{(t.ccrHookOrder||=[]).push("onSend");let i=t.pathname?.endsWith("/v1/messages"),a=t.pathname?.endsWith("/v1/responses");if(i||a){ja(t,e);let l=Kr(t),c=To(t);if(r instanceof ReadableStream){if(i&&t.agents){let y=new AbortController,m=r.pipeThrough(new pe),f,_=-1,k="",h="",b="",T=[],C=[];return s(null,Nt(m,async(x,S)=>{try{if(x.event==="content_block_start"&&x?.data?.content_block?.name){let v=t.agents.find(E=>bo.getAgent(E)?.tools.get(x.data.content_block.name));if(v){f=bo.getAgent(v),_=x.data.index,k=x.data.content_block.name,b=x.data.content_block.id;return}}if(_>-1&&x.data.index===_&&x.data?.delta?.type==="input_json_delta"){h+=x.data?.delta?.partial_json;return}if(_>-1&&x.data.index===_&&x.data?.type==="content_block_stop"){try{let v=Ua.default.parse(h);C.push({type:"tool_use",id:b,name:k,input:v});let E=await f?.tools.get(k)?.handler(v,{req:t,config:e});T.push({tool_use_id:b,type:"tool_result",content:E}),f=void 0,_=-1,k="",h="",b=""}catch(v){console.log(v)}return}if(x.event==="message_delta"&&T.length){t.body.messages.push({role:"assistant",content:C}),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 pe).getReader();for(;;)try{let{value:A,done:O}=await R.read();if(O)break;let I=A;if(["message_start","message_stop"].includes(I.event))continue;if(!S.desiredSize)break;S.enqueue(I)}catch(A){if(A.name==="AbortError"||A.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();break}throw A}return}return x}catch(v){if(console.error("Unexpected error in stream processing:",v),v.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();return}throw v}}).pipeThrough(new Fe))}let[d,p]=r.tee(),g=async y=>{let f=y.pipeThrough(new TextDecoderStream).pipeThrough(new pe).getReader();try{for(;;){let{done:_,value:k}=await f.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=ee.get(c)||{},C=Ue(b.usage),x=Wt(T,C,h==="message_start");t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_anthropic_sse",event:h,provider:t.provider,model:ft(t),usageSessionId:l,capturedUsage:C,mergedUsage:x}),(C?.cache_read_input_tokens||C?.cache_creation_input_tokens)&&console.log("[Usage] Cache tokens:",{cache_read:C?.cache_read_input_tokens,cache_creation:C?.cache_creation_input_tokens}),ee.put(c,x)}if(b?.response?.usage){let T=Ue(b.response.usage),C=ee.get(c)||{},x=Wt(C,T,!1);t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_responses_sse",event:h,provider:t.provider,model:ft(t),usageSessionId:l,capturedUsage:T,mergedUsage:x}),ee.put(c,x)}}}catch(_){_.name==="AbortError"||_.code==="ERR_STREAM_PREMATURE_CLOSE"?console.error("Background read stream closed prematurely"):console.error("Error in background stream reading:",_)}finally{f.releaseLock()}};return t.usageCapturePromise=g(p),s(null,d)}let u=Mu(r);if(u?.usage&&ee.put(c,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(Da.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=Kr(t),s=o.statusCode>=400,i=s?void 0:ee.get(To(t)),a=jr(r),l=j(),c,u,d=t.sseError,p=d&&(d.type||d.code||d.message),g=i?.output_tokens&&i.output_tokens>0,y=s||p&&!g;if(y){c=t.errorMessage||o.errorMessage||t.error?.message||t.error?.toString?.()||void 0,p&&(c=c||JSON.stringify(d),u=JSON.stringify(d)),u=u||t.errorResponseBody;let h=ft(t);Iu(o.statusCode,c)?l.markRateLimited(t.provider||"",h,120,c):l.recordFailure(t.provider||"",h,c)}else{let h=ft(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:ft(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."}),Zt({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:ft(t),upstreamModel:t.upstreamModel,modelFamily:t.modelFamily||"",scenarioType:t.scenarioType||"default",clientType:t.clientType||Me(t),stream:t.body?.stream??!1,inputTokens:_,outputTokens:k,cacheReadInputTokens:m,cacheCreationInputTokens:i?.cache_creation_input_tokens||0,ttft:y||!t.body?.stream?null:a.ttft,tokensPerSecond:y?null:a.tokensPerSecond,durationMs:t.requestStartTime?Math.round(Gr.performance.now()-t.requestStartTime):0,status:y?"error":"success",errorMessage:c,responseBody:u}),t.clientContext?.usageScope!=="session"&&ee.delete(To(t))}catch(r){console.error("Usage tracking error:",r)}})}var Fa,Gr,Ua,Da,Ba=w(()=>{"use strict";Fa=require("node:events"),Gr=require("node:perf_hooks"),Ua=V(require("json5"),1);nt();gn();le();yr();_r();kr();en();Ar();La();Nr();Rn();Da=new Fa.EventEmitter});var Ka={};nn(Ka,{createCcrServer:()=>Lu});async function Lu(n={}){await ga(),await Bt();let e=await lo();(0,Pe.ensureCcrSelfProvider)(e).changed&&(await be(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,a=process.env.SERVICE_PORT?parseInt(process.env.SERVICE_PORT):i,l=f=>(f>9?"":"0")+f,c=(f,_)=>{let k;f?typeof f=="number"?k=new Date(f):k=f:k=new Date;let h=k.getFullYear()+""+l(k.getMonth()+1),b=l(k.getDate()),T=l(k.getHours()),C=l(k.getMinutes());return`./logs/ccr-${h}${b}${T}${C}${l(k.getSeconds())}${_?`_${_}`:""}.log`},u;n.logger!==void 0?u=n.logger:(ba(),e.LOG!==!1?(e.LOG===void 0&&(e.LOG=!0),u={level:e.LOG_LEVEL||"error",stream:(0,Ga.createStream)(c,{path:Pe.HOME_DIR,interval:"1d",size:"50M",compress:!1})}):u=!1);let d=await(0,Pe.listPresets)(),p=new Qr({jsonPath:Pe.CONFIG_FILE,initialConfig:{providers:e.Providers||e.providers,HOST:s,PORT:a,LOG_FILE:(0,Wa.join)((0,Ja.homedir)(),".claude-code-router","claude-code-router.log")},logger:u});await p.ready(),await Oa(p,e),p.recordUsage=f=>{try{let _=f.req?f.req.clientType||Me(f.req):"unknown";Zt({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:f.sessionId||"",provider:f.provider||"",originalModel:f.originalModel||"",model:f.model||"",modelFamily:f.modelFamily||"",scenarioType:f.scenarioType||"default",clientType:_,stream:f.stream??!1,inputTokens:f.inputTokens||0,outputTokens:0,cacheReadInputTokens:f.cacheReadInputTokens||0,cacheCreationInputTokens:f.cacheCreationInputTokens||0,ttft:null,tokensPerSecond:null,durationMs:0,status:"error",errorMessage:f.errorMessage,responseBody:void 0})}catch(_){console.error("Fallback usage tracking error:",_)}},za(p,e),p.ccrPreHandlerCallbacks=Ha(e),(await Promise.allSettled(d.map(async f=>await p.registerNamespace(`/preset/${f.name}`,f.config)))).forEach((f,_)=>{if(f.status==="rejected"){let k=d[_],h=f.reason instanceof Error?f.reason.message:String(f.reason);p.app?.log?.error?.(`Failed to register preset "${k?.name}": ${h}`)}}),await va(p,e),dt(e,p.app?.log);let y=f=>{p.app.log.error({err:f},"Uncaught exception")},m=(f,_)=>{p.app.log.error({reason:f,promise:_},"Unhandled rejection")};return process.on("uncaughtException",y),process.on("unhandledRejection",m),p.app.addHook("onClose",async()=>{process.off("uncaughtException",y),process.off("unhandledRejection",m)}),p}var Ja,Wa,Ga,Pe,Qa=w(()=>{"use strict";Ja=require("os"),Wa=require("path");Va();Ga=require("rotating-file-stream"),Pe=require("@wengine-ai/claude-code-router-shared");ha();uo();Ta();xa();Aa();Ba();fo();en();nt()});var Uu={};nn(Uu,{ActiveProbeService:()=>Mt,ConfigService:()=>Oe,ProjectRoutingError:()=>ne,ProviderHealthStore:()=>_t,ProviderService:()=>ge,SSEParserTransform:()=>pe,SSESerializerTransform:()=>Fe,TokenizerService:()=>Le,TransformerService:()=>Ne,apiKeyAuth:()=>Qt,applyClientAdapter:()=>ve,backupConfigFile:()=>Jt,builtinClientAdapterRegistry:()=>Bo,calculateTokenCount:()=>Ct,clearClientAdapterCaches:()=>Ns,clearProviderHealth:()=>Gt,closeProxyDispatchers:()=>rn,collectReachableModelKeys:()=>Mr,createCcrServer:()=>Za,default:()=>Qr,detectClientType:()=>Me,diagnoseResolutionFailure:()=>Go,extractSessionIdFromUserId:()=>Te,getActiveProbeService:()=>gr,getAllQuotaResults:()=>yn,getAllRateLimitInfo:()=>mn,getClientAdapter:()=>Wo,getConfiguredProxyUrl:()=>mt,getHealthStore:()=>j,getProxyDispatcher:()=>Y,getQuotaAdapter:()=>so,getQuotaResult:()=>Tt,getRateLimitInfo:()=>Ts,getRuntimeDebugLog:()=>ht,getServer:()=>Fu,initConfig:()=>lo,initDir:()=>Bt,initProviderHealthPersistence:()=>fn,initQuotaStorePersistence:()=>Xe,initRateLimitPersistence:()=>We,isClientType:()=>Jo,isGlobalProxyEnabled:()=>vo,listAnthropicCompatibleModels:()=>Kt,mergeUsageCapture:()=>Wt,normalizeSessionId:()=>Ie,normalizeUsagePayload:()=>Ue,pluginManager:()=>ct,readConfigFile:()=>ie,readConfigFileRaw:()=>co,reconcileHealthStore:()=>dt,resetActiveProbeService:()=>$t,resolveProviderProxyUrl:()=>fe,rewriteStream:()=>Nt,router:()=>Rt,searchProjectBySession:()=>Qo,sessionUsageCache:()=>ee,setRuntimeDebugLog:()=>an,startActiveProbe:()=>De,stopActiveProbe:()=>io,storeQuotaResult:()=>hn,tokenSpeedPlugin:()=>Ht,usageStore:()=>Hr,writeConfigFile:()=>be});module.exports=Xr(Uu);function Du(n={}){let e=(0,Ya.default)({bodyLimit:52428800,...n});return e.setErrorHandler(os),e.register(Xa.default),e}async function Za(n={}){return(await Promise.resolve().then(()=>(Qa(),Ka))).createCcrServer(n)}var Ya,Xa,Vr,Qr,Fu,Va=w(()=>{Ya=V(require("fastify"),1),Xa=V(require("@fastify/cors"),1);Co();sn();Rn();wn();qo();er();rt();gn();ao();le();Ge();Ze();ce();nt();Rn();qe();nt();rt();Co();wn();qo();er();Pr();br();le();Ge();fr();Ze();le();ao();yt();ce();uo();Ar();fo();$r();Nr();en();Vr=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:a,...l}=e;this.app=Du({...l,logger:l.logger??!0}),this.app._server=this;let c={initialConfig:t};o!==void 0&&(c.jsonPath=o),r!==void 0&&(c.envPath=r),s!==void 0&&(c.useEnvFile=s),i!==void 0&&(c.useJsonFile=i),a!==void 0&&(c.useEnvironmentVariables=a),this.configService=new Oe(c),this.transformerService=new Ne(this.configService,this.app.log),this.tokenizerService=new Le(this.configService,this.app.log),this.readiness=(async()=>{await Promise.all([this.transformerService.initialize(),this.tokenizerService.initialize()]),this.providerService=new ge(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 Oe({initialConfig:{...t,providers:t.Providers||t.providers}}),r=new Ne(o,this.app.log),i=new Le(o,this.app.log),await Promise.all([r.initialize(),i.initialize()]),s=new ge(o,r,this.app.log)}let a=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(c,u)=>{let d=new URL(`http://127.0.0.1${c.url}`);c.pathname=d.pathname;let p=d.pathname.endsWith("/v1/messages"),g=d.pathname.endsWith("/v1/responses");if(!p&&!g||!c.body||typeof c.body!="object")return;let y=c.ccrHookOrder||=[];if(y.push("request-normalize"),g&&c.body.input&&!c.body.messages&&wt(c.body),c.body.stream===void 0&&(c.body.stream=!1),c.log.info({data:c.body,type:"request body"}),y.push("adapter"),!c.originalModel&&c.body.model&&(c.originalModel=c.body.model),ve(c,o.getAll()),y.push("auth-client"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.authClient(c,u),u.sent))&&(y.push("agent"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.agent(c,u),u.sent)))){y.push("router"),c.body?.model&&await Rt(c,u,{configService:o,tokenizerService:i}),c.usageCacheKey&&ee.put(c.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=c.body;if(!m||!m.model)return u.code(400).send({error:"Missing model in request body"});let f=m.model.split(","),_=m.model;if(f.length>1){let[k,...h]=f;_=h.join(","),m.model=_,c.provider=k}c.model=_}catch(m){return c.log.error({error:m},"Error in modelProviderMiddleware:"),u.code(500).send({error:"Internal server error"})}}}),await Vs(l)};e==="/"?(await this.app.register(a),this.mainNamespaceRegistered=!0):await this.app.register(a,{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{io()}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 rn()})}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{We()}catch{}try{Xe()}catch{}try{fn()}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=De(()=>this.providerService.getProviders(),t,o=>fe(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)}}},Qr=Vr;Fu=Za});Va();0&&(module.exports={ActiveProbeService,ConfigService,ProjectRoutingError,ProviderHealthStore,ProviderService,SSEParserTransform,SSESerializerTransform,TokenizerService,TransformerService,apiKeyAuth,applyClientAdapter,backupConfigFile,builtinClientAdapterRegistry,calculateTokenCount,clearClientAdapterCaches,clearProviderHealth,closeProxyDispatchers,collectReachableModelKeys,createCcrServer,detectClientType,diagnoseResolutionFailure,extractSessionIdFromUserId,getActiveProbeService,getAllQuotaResults,getAllRateLimitInfo,getClientAdapter,getConfiguredProxyUrl,getHealthStore,getProxyDispatcher,getQuotaAdapter,getQuotaResult,getRateLimitInfo,getRuntimeDebugLog,getServer,initConfig,initDir,initProviderHealthPersistence,initQuotaStorePersistence,initRateLimitPersistence,isClientType,isGlobalProxyEnabled,listAnthropicCompatibleModels,mergeUsageCapture,normalizeSessionId,normalizeUsagePayload,pluginManager,readConfigFile,readConfigFileRaw,reconcileHealthStore,resetActiveProbeService,resolveProviderProxyUrl,rewriteStream,router,searchProjectBySession,sessionUsageCache,setRuntimeDebugLog,startActiveProbe,stopActiveProbe,storeQuotaResult,tokenSpeedPlugin,usageStore,writeConfigFile});
|
|
420
|
+
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(a=>a.type==="image"))),r=1;o.forEach(s=>{Array.isArray(s.content)&&s.content.forEach(i=>{if(i.type==="image")To.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(a=>a.type==="image")){let a=[],l=i.content.filter(u=>u.type!=="image"?!0:(To.storeImage(`${e.id}_Image#${r}`,u.source),a.push(`[Image #${r}]`),r++,!1)),c=`${a.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:c},...l]:c}})})}},Ka=new Zr});var qr,Va,vo,Ya=w(()=>{"use strict";Qa();qr=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}},Va=new qr;Va.registerAgent(Ka);vo=Va});function Vu(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 ec(n,e){(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&ve(n,e)}function ts(n){return n.usageSessionId||n.id}function xo(n){return n.usageCacheKey||`${n.clientType||"unknown"}:request:${ts(n)}`}function ft(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 Yu(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?Ue(e.usage):e.response?.usage?Ue(e.response.usage):void 0,o=e.model||e.response?.model;if(!(!t&&!o))return{usage:t,model:o}}async function Xu(n,e,t){if(await new Promise((r,s)=>{let i=a=>a?s(a):r();Qt(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")){ec(n,t);let r=xo(n);n.clientContext?.usageScope==="session"&&(n.previousUsage=ee.get(r))}}async function Zu(n,e,t){if(!n.pathname?.endsWith("/v1/messages"))return;let o=[];for(let r of vo.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 tc(n){return{authClient:(e,t)=>Xu(e,t,n),agent:(e,t)=>Zu(e,t,n)}}function nc(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=es.performance.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 a=l=>l?i(l):s();Qt(e)(t,o,a).then(()=>s(),i)})}),n.addHook("onError",async(t,o,r)=>{t.errorMessage=r?.message||r?.toString?.()||"Unknown error",Xa.emit("onError",t,o,r)}),n.addHook("onSend",(t,o,r,s)=>{(t.ccrHookOrder||=[]).push("onSend");let i=t.pathname?.endsWith("/v1/messages"),a=t.pathname?.endsWith("/v1/responses");if(i||a){ec(t,e);let l=ts(t),c=xo(t);if(r instanceof ReadableStream){if(i&&t.agents){let y=new AbortController,m=r.pipeThrough(new pe),f,_=-1,k="",h="",b="",v=[],C=[];return s(null,Nt(m,async(x,S)=>{try{if(x.event==="content_block_start"&&x?.data?.content_block?.name){let T=t.agents.find(E=>vo.getAgent(E)?.tools.get(x.data.content_block.name));if(T){f=vo.getAgent(T),_=x.data.index,k=x.data.content_block.name,b=x.data.content_block.id;return}}if(_>-1&&x.data.index===_&&x.data?.delta?.type==="input_json_delta"){h+=x.data?.delta?.partial_json;return}if(_>-1&&x.data.index===_&&x.data?.type==="content_block_stop"){try{let T=qa.default.parse(h);C.push({type:"tool_use",id:b,name:k,input:T});let E=await f?.tools.get(k)?.handler(T,{req:t,config:e});v.push({tool_use_id:b,type:"tool_result",content:E}),f=void 0,_=-1,k="",h="",b=""}catch(T){console.log(T)}return}if(x.event==="message_delta"&&v.length){t.body.messages.push({role:"assistant",content:C}),t.body.messages.push({role:"user",content:v});let T=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(!T.ok)return;let R=T.body.pipeThrough(new pe).getReader();for(;;)try{let{value:A,done:O}=await R.read();if(O)break;let I=A;if(["message_start","message_stop"].includes(I.event))continue;if(!S.desiredSize)break;S.enqueue(I)}catch(A){if(A.name==="AbortError"||A.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();break}throw A}return}return x}catch(T){if(console.error("Unexpected error in stream processing:",T),T.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();return}throw T}}).pipeThrough(new Fe))}let[d,p]=r.tee(),g=async y=>{let f=y.pipeThrough(new TextDecoderStream).pipeThrough(new pe).getReader();try{for(;;){let{done:_,value:k}=await f.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 v=ee.get(c)||{},C=Ue(b.usage),x=Wt(v,C,h==="message_start");t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_anthropic_sse",event:h,provider:t.provider,model:ft(t),usageSessionId:l,capturedUsage:C,mergedUsage:x}),(C?.cache_read_input_tokens||C?.cache_creation_input_tokens)&&console.log("[Usage] Cache tokens:",{cache_read:C?.cache_read_input_tokens,cache_creation:C?.cache_creation_input_tokens}),ee.put(c,x)}if(b?.response?.usage){let v=Ue(b.response.usage),C=ee.get(c)||{},x=Wt(C,v,!1);t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_responses_sse",event:h,provider:t.provider,model:ft(t),usageSessionId:l,capturedUsage:v,mergedUsage:x}),ee.put(c,x)}}}catch(_){_.name==="AbortError"||_.code==="ERR_STREAM_PREMATURE_CLOSE"?console.error("Background read stream closed prematurely"):console.error("Error in background stream reading:",_)}finally{f.releaseLock()}};return t.usageCapturePromise=g(p),s(null,d)}let u=Yu(r);if(u?.usage&&ee.put(c,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(Xa.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("onRequestAbort",async t=>{Bo(t)}),n.addHook("onResponse",async(t,o)=>{if((t.ccrHookOrder||=[]).push("onResponse"),Bo(t),!(!t.pathname?.endsWith("/v1/messages")&&!t.pathname?.endsWith("/v1/responses")))try{t.usageCapturePromise&&await t.usageCapturePromise,t.tokenSpeedCapturePromise&&await t.tokenSpeedCapturePromise;let r=ts(t),s=o.statusCode>=400,i=s?void 0:ee.get(xo(t)),a=Qr(r),l=j(),c,u,d=t.sseError,p=d&&(d.type||d.code||d.message),g=i?.output_tokens&&i.output_tokens>0,y=s||p&&!g;if(y){c=t.errorMessage||o.errorMessage||t.error?.message||t.error?.toString?.()||void 0,p&&(c=c||JSON.stringify(d),u=JSON.stringify(d)),u=u||t.errorResponseBody;let h=ft(t);Vu(o.statusCode,c)?l.markRateLimited(t.provider||"",h,120,c):l.recordFailure(t.provider||"",h,c)}else{let h=ft(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:ft(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."}),Zt({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:ft(t),upstreamModel:t.upstreamModel,modelFamily:t.modelFamily||"",scenarioType:t.scenarioType||"default",clientType:t.clientType||Me(t),stream:t.body?.stream??!1,inputTokens:_,outputTokens:k,cacheReadInputTokens:m,cacheCreationInputTokens:i?.cache_creation_input_tokens||0,ttft:y||!t.body?.stream?null:a.ttft,tokensPerSecond:y?null:a.tokensPerSecond,durationMs:t.requestStartTime?Math.round(es.performance.now()-t.requestStartTime):0,status:y?"error":"success",errorMessage:c,responseBody:u}),t.clientContext?.usageScope!=="session"&&ee.delete(xo(t))}catch(r){console.error("Usage tracking error:",r)}})}var Za,es,qa,Xa,oc=w(()=>{"use strict";Za=require("node:events"),es=require("node:perf_hooks"),qa=V(require("json5"),1);nt();_n();yn();le();Sr();Rr();wr();en();Ur();Ya();Br();Pn();Xa=new Za.EventEmitter});var ac={};nn(ac,{createCcrServer:()=>qu});async function qu(n={}){await Pa(),await Bt();let e=await fo();(0,Ee.ensureCcrSelfProvider)(e).changed&&(await be(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,a=process.env.SERVICE_PORT?parseInt(process.env.SERVICE_PORT):i,l=f=>(f>9?"":"0")+f,c=(f,_)=>{let k;f?typeof f=="number"?k=new Date(f):k=f:k=new Date;let h=k.getFullYear()+""+l(k.getMonth()+1),b=l(k.getDate()),v=l(k.getHours()),C=l(k.getMinutes());return`./logs/ccr-${h}${b}${v}${C}${l(k.getSeconds())}${_?`_${_}`:""}.log`},u;n.logger!==void 0?u=n.logger:($a(),e.LOG!==!1?(e.LOG===void 0&&(e.LOG=!0),u={level:e.LOG_LEVEL||"error",stream:(0,ic.createStream)(c,{path:Ee.HOME_DIR,interval:"1d",size:"50M",compress:!1})}):u=!1);let d=await(0,Ee.listPresets)(),p=new ns({jsonPath:Ee.CONFIG_FILE,initialConfig:{providers:e.Providers||e.providers,HOST:s,PORT:a,LOG_FILE:(0,sc.join)((0,rc.homedir)(),".claude-code-router","claude-code-router.log")},logger:u});await p.ready(),await Ja(p,e),p.recordUsage=f=>{try{let _=f.req?f.req.clientType||Me(f.req):"unknown";Zt({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:f.sessionId||"",provider:f.provider||"",originalModel:f.originalModel||"",model:f.model||"",modelFamily:f.modelFamily||"",scenarioType:f.scenarioType||"default",clientType:_,stream:f.stream??!1,inputTokens:f.inputTokens||0,outputTokens:0,cacheReadInputTokens:f.cacheReadInputTokens||0,cacheCreationInputTokens:f.cacheCreationInputTokens||0,ttft:null,tokensPerSecond:null,durationMs:0,status:"error",errorMessage:f.errorMessage,responseBody:void 0})}catch(_){console.error("Fallback usage tracking error:",_)}},nc(p,e),p.ccrPreHandlerCallbacks=tc(e),(await Promise.allSettled(d.map(async f=>await p.registerNamespace(`/preset/${f.name}`,f.config)))).forEach((f,_)=>{if(f.status==="rejected"){let k=d[_],h=f.reason instanceof Error?f.reason.message:String(f.reason);p.app?.log?.error?.(`Failed to register preset "${k?.name}": ${h}`)}}),await La(p,e),dt(e,p.app?.log);let y=f=>{p.app.log.error({err:f},"Uncaught exception")},m=(f,_)=>{p.app.log.error({reason:f,promise:_},"Unhandled rejection")};return process.on("uncaughtException",y),process.on("unhandledRejection",m),p.app.addHook("onClose",async()=>{process.off("uncaughtException",y),process.off("unhandledRejection",m)}),p}var rc,sc,ic,Ee,cc=w(()=>{"use strict";rc=require("os"),sc=require("path");lc();ic=require("rotating-file-stream"),Ee=require("@wengine-ai/claude-code-router-shared");Oa();mo();Na();Da();Wa();oc();ho();en();nt()});var nd={};nn(nd,{ActiveProbeService:()=>Mt,ConfigService:()=>Oe,ProjectRoutingError:()=>ne,ProviderHealthStore:()=>_t,ProviderService:()=>ge,SSEParserTransform:()=>pe,SSESerializerTransform:()=>Fe,TokenizerService:()=>Le,TransformerService:()=>Ne,apiKeyAuth:()=>Qt,applyClientAdapter:()=>ve,backupConfigFile:()=>Jt,builtinClientAdapterRegistry:()=>Xo,calculateTokenCount:()=>Ct,clearClientAdapterCaches:()=>Ks,clearProviderHealth:()=>Gt,closeProxyDispatchers:()=>rn,collectReachableModelKeys:()=>Hr,createCcrServer:()=>pc,default:()=>ns,detectClientType:()=>Me,diagnoseResolutionFailure:()=>er,extractSessionIdFromUserId:()=>Te,getActiveProbeService:()=>xr,getAllQuotaResults:()=>bn,getAllRateLimitInfo:()=>mn,getClientAdapter:()=>qo,getConfiguredProxyUrl:()=>mt,getHealthStore:()=>j,getProxyDispatcher:()=>Y,getQuotaAdapter:()=>co,getQuotaResult:()=>Tt,getRateLimitInfo:()=>Ps,getRuntimeDebugLog:()=>ht,getServer:()=>td,initConfig:()=>fo,initDir:()=>Bt,initProviderHealthPersistence:()=>fn,initQuotaStorePersistence:()=>Xe,initRateLimitPersistence:()=>We,isClientType:()=>Zo,isGlobalProxyEnabled:()=>Co,listAnthropicCompatibleModels:()=>Kt,mergeUsageCapture:()=>Wt,normalizeSessionId:()=>Ie,normalizeUsagePayload:()=>Ue,pluginManager:()=>ct,readConfigFile:()=>ie,readConfigFileRaw:()=>po,reconcileHealthStore:()=>dt,resetActiveProbeService:()=>$t,resolveProviderProxyUrl:()=>fe,rewriteStream:()=>Nt,router:()=>Rt,searchProjectBySession:()=>nr,sessionUsageCache:()=>ee,setRuntimeDebugLog:()=>an,startActiveProbe:()=>De,stopActiveProbe:()=>lo,storeQuotaResult:()=>kn,tokenSpeedPlugin:()=>Ht,usageStore:()=>Vr,writeConfigFile:()=>be});module.exports=ss(nd);function ed(n={}){let e=(0,uc.default)({bodyLimit:52428800,...n});return e.setErrorHandler(ds),e.register(dc.default),e}async function pc(n={}){return(await Promise.resolve().then(()=>(cc(),ac))).createCcrServer(n)}var uc,dc,os,ns,td,lc=w(()=>{uc=V(require("fastify"),1),dc=V(require("@fastify/cors"),1);Ro();sn();Pn();On();ar();cr();rt();_n();uo();le();Ge();Ze();ce();nt();Pn();qe();nt();rt();Ro();On();ar();cr();Lr();Er();le();Ge();Tr();Ze();le();uo();yt();ce();mo();Ur();ho();zr();Br();en();os=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:a,...l}=e;this.app=ed({...l,logger:l.logger??!0}),this.app._server=this;let c={initialConfig:t};o!==void 0&&(c.jsonPath=o),r!==void 0&&(c.envPath=r),s!==void 0&&(c.useEnvFile=s),i!==void 0&&(c.useJsonFile=i),a!==void 0&&(c.useEnvironmentVariables=a),this.configService=new Oe(c),this.transformerService=new Ne(this.configService,this.app.log),this.tokenizerService=new Le(this.configService,this.app.log),this.readiness=(async()=>{await Promise.all([this.transformerService.initialize(),this.tokenizerService.initialize()]),this.providerService=new ge(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 Oe({initialConfig:{...t,providers:t.Providers||t.providers}}),r=new Ne(o,this.app.log),i=new Le(o,this.app.log),await Promise.all([r.initialize(),i.initialize()]),s=new ge(o,r,this.app.log)}let a=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(c,u)=>{let d=new URL(`http://127.0.0.1${c.url}`);c.pathname=d.pathname;let p=d.pathname.endsWith("/v1/messages"),g=d.pathname.endsWith("/v1/responses");if(!p&&!g||!c.body||typeof c.body!="object")return;let y=c.ccrHookOrder||=[];if(y.push("request-normalize"),g&&c.body.input&&!c.body.messages&&wt(c.body),c.body.stream===void 0&&(c.body.stream=!1),c.log.info({data:c.body,type:"request body"}),y.push("adapter"),!c.originalModel&&c.body.model&&(c.originalModel=c.body.model),ve(c,o.getAll()),y.push("auth-client"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.authClient(c,u),u.sent))&&(y.push("agent"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.agent(c,u),u.sent)))){y.push("router"),c.body?.model&&await Rt(c,u,{configService:o,tokenizerService:i}),c.usageCacheKey&&ee.put(c.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=c.body;if(!m||!m.model)return u.code(400).send({error:"Missing model in request body"});let f=m.model.split(","),_=m.model;if(f.length>1){let[k,...h]=f;_=h.join(","),m.model=_,c.provider=k}c.model=_}catch(m){return c.log.error({error:m},"Error in modelProviderMiddleware:"),u.code(500).send({error:"Internal server error"})}}}),await ai(l)};e==="/"?(await this.app.register(a),this.mainNamespaceRegistered=!0):await this.app.register(a,{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 rn()})}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{We()}catch{}try{Xe()}catch{}try{fn()}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=De(()=>this.providerService.getProviders(),t,o=>fe(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)}}},ns=os;td=pc});lc();0&&(module.exports={ActiveProbeService,ConfigService,ProjectRoutingError,ProviderHealthStore,ProviderService,SSEParserTransform,SSESerializerTransform,TokenizerService,TransformerService,apiKeyAuth,applyClientAdapter,backupConfigFile,builtinClientAdapterRegistry,calculateTokenCount,clearClientAdapterCaches,clearProviderHealth,closeProxyDispatchers,collectReachableModelKeys,createCcrServer,detectClientType,diagnoseResolutionFailure,extractSessionIdFromUserId,getActiveProbeService,getAllQuotaResults,getAllRateLimitInfo,getClientAdapter,getConfiguredProxyUrl,getHealthStore,getProxyDispatcher,getQuotaAdapter,getQuotaResult,getRateLimitInfo,getRuntimeDebugLog,getServer,initConfig,initDir,initProviderHealthPersistence,initQuotaStorePersistence,initRateLimitPersistence,isClientType,isGlobalProxyEnabled,listAnthropicCompatibleModels,mergeUsageCapture,normalizeSessionId,normalizeUsagePayload,pluginManager,readConfigFile,readConfigFileRaw,reconcileHealthStore,resetActiveProbeService,resolveProviderProxyUrl,rewriteStream,router,searchProjectBySession,sessionUsageCache,setRuntimeDebugLog,startActiveProbe,stopActiveProbe,storeQuotaResult,tokenSpeedPlugin,usageStore,writeConfigFile});
|
|
421
421
|
//# sourceMappingURL=server.cjs.map
|