@wengine-ai/llms 2.3.2410 → 2.3.2412
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 +2 -2
- package/dist/api/routes.d.ts +18 -0
- package/dist/ccr/usage-store.d.ts +16 -0
- package/dist/cjs/server.cjs +193 -193
- package/dist/cjs/server.cjs.map +4 -4
- package/dist/esm/server.mjs +192 -192
- package/dist/esm/server.mjs.map +4 -4
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/output/temp-file-handler.d.ts +8 -38
- package/dist/plugins/output/temp-file-retention.d.ts +14 -0
- package/dist/plugins/output/types.d.ts +7 -1
- package/dist/plugins/token-speed.d.ts +15 -0
- package/dist/transformer/amd.transformer.d.ts +13 -0
- package/dist/transformer/index.d.ts +2 -0
- package/package.json +2 -2
package/dist/esm/server.mjs
CHANGED
|
@@ -1,220 +1,220 @@
|
|
|
1
1
|
import { createRequire as __createRequire } from "node:module"; import { fileURLToPath as __fileURLToPath } from "node:url"; import { dirname as __pathDirname } from "node:path"; const require = __createRequire(import.meta.url); const __filename = __fileURLToPath(import.meta.url); const __dirname = __pathDirname(__filename);
|
|
2
|
-
var
|
|
2
|
+
var to=Object.defineProperty;var nc=Object.getOwnPropertyDescriptor;var oc=Object.getOwnPropertyNames;var rc=Object.prototype.hasOwnProperty;var Xe=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var E=(n,e)=>()=>(n&&(e=n(n=0)),e);var no=(n,e)=>{for(var t in e)to(n,t,{get:e[t],enumerable:!0})},sc=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of oc(e))!rc.call(n,r)&&r!==t&&to(n,r,{get:()=>e[r],enumerable:!(o=nc(e,r))||o.enumerable});return n};var ic=n=>sc(to({},"__esModule",{value:!0}),n);import{ProxyAgent as ac}from"undici";function lc(n){if(n==null)return{ok:!0};let e=String(n).trim();if(!e)return{ok:!0};if(cc.test(e))return{ok:!0};try{let t=new URL(e);return t.protocol!=="http:"&&t.protocol!=="https:"?{ok:!1,error:`Unsupported proxy protocol: ${t.protocol}. Only http:// and https:// are allowed.`}:{ok:!0}}catch{return{ok:!1,error:`Invalid proxy URL: ${e}`}}}function Kr(n){let e=[];for(let t of Wr){let o=n[t];if(o==null)continue;let r=lc(o);r.ok||e.push({key:t,error:r.error})}return e}function Rt(n){for(let e of Wr){let t=n.get(e);if(t==null)continue;let o=String(t).trim();if(o)return o}}function Gr(n){if(n==null||n==="")return!0;if(typeof n=="string"){let e=n.trim().toLowerCase();return e==="false"||e==="0"?!1:e==="true"||e==="1"?!0:!!e}return n===!1||n===0?!1:n===!0||n===1?!0:!!n}function ce(n,e){let t=Rt(n);if(t)return Gr(n.get("PROXY_GLOBAL_ENABLED"))||e?.proxyEnabled===!0||e?.proxy_enabled===!0?t:void 0}function G(n){let e=uc(n),t=St.get(e);if(t)return t;let o=new ac(e);return St.set(e,o),o}async function oo(){let n=Array.from(St.values());St.clear(),await Promise.allSettled(n.map(e=>e.close()))}function uc(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 Wr,cc,St,oe=E(()=>{"use strict";Wr=["HTTPS_PROXY","https_proxy","httpsProxy","PROXY_URL"],cc=/\$\{[^}]+\}|\$[A-Z_][A-Z0-9_]*/;St=new Map});import{readFileSync as dc,existsSync as Vr}from"fs";import{join as Qr}from"path";import{config as pc}from"dotenv";import fc from"json5";var Re,ro=E(()=>{"use strict";oe();Re=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:Qr(process.cwd(),this.options.jsonPath);if(Vr(e))try{let t=dc(e,"utf-8"),o=fc.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:Qr(process.cwd(),this.options.envPath);if(Vr(e))try{let t=pc({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 Rt(this)}has(e){return this.config[e]!==void 0}set(e,t){this.config[e]=t}reload(){this.config={},this.loadConfig()}getConfigSummary(){let e=[];return this.options.initialConfig&&e.push("Initial Config"),this.options.useJsonFile&&this.options.jsonPath&&e.push(`JSON: ${this.options.jsonPath}`),this.options.useEnvFile&&e.push(`ENV: ${this.options.envPath}`),this.options.useEnvironmentVariables&&e.push("Environment Variables"),`Config sources: ${e.join(", ")}`}}});function J(n,e=500,t="internal_error",o="api_error"){let r=new Error(n);return r.statusCode=e,r.code=t,r.type=o,r}async function Yr(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 wt=E(()=>{"use strict"});function io(n){so=n}function Et(){return so}function Pt(n){return so||n.get("DEBUG_LOG")===!0}function qr(n){let e=n.get("DEBUG_LOG_OPTIONS");return!e||typeof e!="object"?we:{maxBodyLength:e.maxBodyLength??we.maxBodyLength,maxStreamChunks:e.maxStreamChunks??we.maxStreamChunks}}function Ot(n){let e={};for(let[t,o]of Object.entries(n))mc.has(t.toLowerCase())?e[t]="***MASKED***":e[t]=o;return e}function At(n,e){return n.length<=e?n:n.slice(0,e)+`...[truncated, total ${n.length} bytes]`}function es(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:Ot(t.headers),body:At(o,we.maxBodyLength)})}function ts(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_response",status:t.status,headers:Ot(t.headers),...t.body!==void 0?{body:At(t.body,we.maxBodyLength)}:{}})}function Xr(n,e,t,o){let r=gc(o);n.info({debug_log:!0,reqId:e,phase:"provider_stream_chunk",chunkIndex:t,...r?{usage_monitor:r}:{},data:At(o,we.maxBodyLength)})}function gc(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:At(o,512)})}}if(e.length)return e.length===1?e[0]:e}function Zr(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_stream_end",totalChunks:t})}function ns(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(`
|
|
3
3
|
|
|
4
|
-
`);i=c.pop()||"";for(let u of c)u.trim()&&(s<o.maxStreamChunks?Hr(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&&Hr(e,t,s,i.trim()),s++),zr(e,t,s)}catch(i){console.error(`[debug_log] Error reading debug stream for reqId=${t}:`,i),zr(e,t,s)}finally{r.releaseLock()}})()}var Ka,Ce,qn,qe=w(()=>{"use strict";Ka=new Set(["authorization","x-api-key","x-goog-api-key","cookie","set-cookie"]),Ce={maxBodyLength:4096,maxStreamChunks:100},qn=!1});function Kr(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=K(t.httpsProxy)),r?.debug({reqId:o.req.id,method:"POST",headers:At(Object.fromEntries(s.entries())),requestUrl:typeof n=="string"?n:n.toString(),useProxy:Va(t.httpsProxy)},"final request"),fetch(typeof n=="string"?n:n.toString(),l)}function Va(n){if(!(typeof n!="string"||!n.trim()))try{let e=new URL(n);return e.username||e.password?(e.username="",e.password="",`${e.toString()} (credentials redacted)`):e.toString()}catch{return"[invalid proxy URL]"}}var Qr=w(()=>{oe();qe()});var Mt,to=w(()=>{Mt="2.3.2410"});var Vr={};Yn(Vr,{getFallbackPromotionStore:()=>Se,resetFallbackPromotionStore:()=>Za});function Se(n){return ye||(ye=new no,n&&ye.configure(n)),ye}function Za(){ye&&(ye.clearAll(),ye=null)}var Xa,no,ye,$t=w(()=>{"use strict";re();Xa=600*1e3,no=class{promotions=new Map;enabled=!0;ttlMs=Xa;configure(e){e?.enabled!==void 0&&(this.enabled=e.enabled),e?.ttlMinutes!==void 0&&(this.ttlMs=e.ttlMinutes*60*1e3)}getKey(e,t,o){return`${e},${t}:${o}`}promote(e,t,o,r,s,i){if(!this.enabled)return;let 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(!U().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}},ye=null});import{existsSync as Zr,mkdirSync as qa,readFileSync as ec,writeFileSync as tc}from"fs";import{join as qr}from"path";import{HOME_DIR as nc}from"@wengine-ai/claude-code-router-shared";function ic(n){try{if(!Zr(so))return;let e=JSON.parse(ec(so,"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 Xr(n){try{let e=n.getAllStates();if(e.length===0)return;Zr(ro)||qa(ro,{recursive:!0});let t=e.map(o=>({...o,updatedAt:Date.now()}));tc(so,JSON.stringify(t,null,2),"utf-8")}catch{}}function io(){if(Yr)return;Yr=!0;let n=U();ic(n),sc=setInterval(()=>Xr(n),6e4),process.on("exit",()=>Xr(n))}function U(n){return oo||(oo=new Nt(n)),oo}var oc,rc,Nt,ro,so,Yr,sc,oo,re=w(()=>{"use strict";oc=120,rc={enabled:!0,failureThreshold:3,probeIntervalMinutes:5,halfOpenSuccessThreshold:2},Nt=class{states=new Map;config;probeTimer;constructor(e){this.config={...rc,...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}=($t(),Ua(Vr));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??oc)*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)}},ro=qr(nc,"runtime"),so=qr(ro,"provider-health.json"),Yr=!1,sc=null;oo=null});import{existsSync as rs,mkdirSync as ac,readFileSync as cc,writeFileSync as lc}from"fs";import{join as ss}from"path";import{HOME_DIR as uc}from"@wengine-ai/claude-code-router-shared";function et(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=ao(o,dc),s=ao(o,pc),i=ao(o,fc);(r||s||i)&&Re.set(n,{provider:n,remaining:ns(r),limit:ns(s),reset:gc(i),capturedAt:Date.now()})}function mc(n){return Re.get(n)}function uo(){return Array.from(Re.values())}function ao(n,e){for(let t of e){let o=n(t);if(o)return o}return null}function ns(n){if(!n)return null;let e=Number(n.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function gc(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=hc(e);return r===null?null:Math.floor((Date.now()+r)/1e3)}function hc(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 yc(){try{if(!rs(lo))return;let n=JSON.parse(cc(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;Re.set(t.provider,t)}}catch{}}function os(){try{if(Re.size===0)return;rs(co)||ac(co,{recursive:!0}),lc(lo,JSON.stringify(Array.from(Re.values()),null,2),"utf-8")}catch{}}function tt(){es||(es=!0,yc(),ts=setInterval(os,6e4),ts.unref?.(),process.on("exit",os))}var co,lo,Re,es,ts,dc,pc,fc,we=w(()=>{"use strict";co=ss(uc,"runtime"),lo=ss(co,"rate-limit.json"),Re=new Map,es=!1,ts=null,dc=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],pc=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],fc=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"]});function Dt(){return po||(po=new mo),po}function ho(){return fo||(fo=new go),fo}function _c(n){return n?.sessionId||n?.clientContext?.stableSessionId||void 0}function is(n){let e=_c(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=Dt(),o=n?.[Lt];if(o&&!o.released){if(o.key===e)return;t.release(o)}n[Lt]=t.acquire(e)}function yo(n){let e=n?.[Lt];e&&Dt().release(e)}function cs(n,e){let t=is(n),o=n?.[Lt],r=t?ho().get(t)!==void 0:!1;!o&&!r||(t&&ho().bind(t,e),as(n,e))}function kc(n,e,t){let o=Dt(),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 ls(n,e,t){let o=is(n),r=ho(),s=Dt(),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=kc(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 mo,go,po,fo,Lt,Ft=w(()=>{"use strict";mo=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()}},go=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()}},po=null,fo=null;Lt="__concurrencyLease"});async function F(n){let e=await n.text();return bc(e,n.status)}function bc(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 us(n){return(n.headers.get("Content-Type")||"").includes("text/event-stream")}function nt(n){return/^\s*(event:|data:|:\s*[A-Z])/m.test(n)||n.startsWith("data:")}async function Ee(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})),nt(s))||/^\s*[\[{]/.test(s))break}s+=r.decode()}finally{o.releaseLock()}return e.cancel().catch(()=>{}),{body:t,isSSE:nt(s)}}async function Pe(n){try{let e=await n.text();if(nt(e)){let o=new TextEncoder,r=new ReadableStream({start(s){s.enqueue(o.encode(e)),s.close()}});return{response:new Response(r,{status:n.status,statusText:n.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}}),isSSE:!0}}return{response:new Response(e,{status:n.status,statusText:n.statusText,headers:n.headers}),isSSE:!1}}catch{return{response:n,isSSE:!1}}}var Q=w(()=>{"use strict"});var Oe,_o=w(()=>{"use strict";Q();Oe=class{name="openai-responses";endPoint="/v1/responses";getCacheReadInputTokens(e){return e?.cache_read_input_tokens??e?.input_tokens_details?.cached_tokens??e?.prompt_tokens_details?.cached_tokens??0}getCacheCreationInputTokens(e){return e?.cache_creation_input_tokens??e?.input_tokens_details?.cache_creation_tokens??e?.input_tokens_details?.cache_write_tokens??e?.prompt_tokens_details?.cache_creation_tokens??e?.prompt_tokens_details?.cache_write_tokens??0}buildResponsesUsage(e){let t=e?.input_tokens??e?.prompt_tokens??0,o=e?.output_tokens??e?.completion_tokens??0,r=this.getCacheReadInputTokens(e),s=this.getCacheCreationInputTokens(e);return{input_tokens:t,output_tokens:o,total_tokens:e?.total_tokens??t+o,input_tokens_details:{cached_tokens:r,cache_creation_tokens:s},output_tokens_details:{reasoning_tokens:e?.output_tokens_details?.reasoning_tokens??0},cache_read_input_tokens:r,cache_creation_input_tokens:s}}async transformRequestIn(e){delete e.temperature,delete e.max_tokens,e.reasoning&&(e.reasoning={effort:e.reasoning.effort,summary:e.reasoning.summary||"auto"});let t=[],o=e.messages.filter(r=>r.role==="system");if(o.length>0){let r=o[0];Array.isArray(r.content)?r.content.forEach(s=>{let i="";typeof s=="string"?i=s:s&&typeof s=="object"&&"text"in s&&(i=s.text),t.push({role:"system",content:i})}):e.instructions=r.content}if(e.messages.forEach(r=>{if(r.role!=="system"){if(Array.isArray(r.content)){let s=r.content.map(i=>this.normalizeRequestContent(i,r.role)).filter(i=>i!==null);s.length>0?r.content=s:delete r.content}if(r.role==="tool"){let s={...r};s.type="function_call_output",s.call_id=r.tool_call_id,s.output=r.content,delete s.cache_control,delete s.role,delete s.tool_call_id,delete s.content,t.push(s);return}if(r.role==="assistant"&&Array.isArray(r.tool_calls)){r.tool_calls.forEach(s=>{t.push({type:"function_call",arguments:s.function.arguments,name:s.function.name,call_id:s.id})});return}t.push(r)}}),e.input=t,delete e.messages,Array.isArray(e.tools)){let r=e.tools.find(s=>s.function.name==="web_search");e.tools=e.tools.filter(s=>s.function.name!=="web_search").map(s=>(s.function.name==="WebSearch"&&delete s.function.parameters.properties.allowed_domains,s.function.name==="Edit"?{type:s.type,name:s.function.name,description:s.function.description,parameters:{...s.function.parameters,required:["file_path","old_string","new_string","replace_all"]},strict:!0}:{type:s.type,name:s.function.name,description:s.function.description,parameters:s.function.parameters})),r&&e.tools.push({type:"web_search"})}return e.parallel_tool_calls=!1,e}async transformResponseOut(e){let t=e.headers.get("Content-Type")||"";if(t.includes("application/json")){let o=await Ee(e);if(o?.isSSE&&o.body){let r=new Response(o.body,{status:e.status,statusText:e.statusText,headers:new Headers({...Object.fromEntries(e.headers.entries()),"content-type":"text/event-stream"})});return this.transformResponseOut(r)}if(o?.body)e=new Response(o.body,{status:e.status,statusText:e.statusText,headers:e.headers});else if(o===null){let r=await Pe(e);if(r.isSSE)return this.transformResponseOut(r.response);e=r.response}}if(t.includes("application/json")){let o=await F(e);if(o.object==="response"&&o.output){let r=this.convertResponseToChat(o);return new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers})}return o.type==="message"&&Array.isArray(o.content)?new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers}):new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(t.includes("text/event-stream")){if(!e.body)return e;let[o,r]=e.body.tee(),s=o.getReader(),i="";try{let{value: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
|
+
`);i=c.pop()||"";for(let u of c)u.trim()&&(s<o.maxStreamChunks?Xr(e,t,s,u):s===o.maxStreamChunks&&e.info({debug_log:!0,reqId:t,phase:"provider_stream_chunk_omitted",message:`[chunks after #${s} omitted, maxStreamChunks=${o.maxStreamChunks}]`}),s++)}i.trim()&&(s<o.maxStreamChunks&&Xr(e,t,s,i.trim()),s++),Zr(e,t,s)}catch(i){console.error(`[debug_log] Error reading debug stream for reqId=${t}:`,i),Zr(e,t,s)}finally{r.releaseLock()}})()}var mc,we,so,Ze=E(()=>{"use strict";mc=new Set(["authorization","x-api-key","x-goog-api-key","cookie","set-cookie"]),we={maxBodyLength:4096,maxStreamChunks:100},so=!1});function os(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=G(t.httpsProxy)),r?.debug({reqId:o.req.id,method:"POST",headers:Ot(Object.fromEntries(s.entries())),requestUrl:typeof n=="string"?n:n.toString(),useProxy:hc(t.httpsProxy)},"final request"),fetch(typeof n=="string"?n:n.toString(),l)}function hc(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 rs=E(()=>{oe();Ze()});var It,ao=E(()=>{It="2.3.2412"});var ss={};no(ss,{getFallbackPromotionStore:()=>Ee,resetFallbackPromotionStore:()=>kc});function Ee(n){return ve||(ve=new co,n&&ve.configure(n)),ve}function kc(){ve&&(ve.clearAll(),ve=null)}var _c,co,ve,Mt=E(()=>{"use strict";re();_c=600*1e3,co=class{promotions=new Map;enabled=!0;ttlMs=_c;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(!U().isAvailable(l,u))return this.promotions.delete(s),null;let p=r.find(h=>h.name.toLowerCase()===l.toLowerCase()),m=p?.models?.find(h=>String(h).toLowerCase()===u.toLowerCase());return!p||!m||p.enabled===!1?(this.promotions.delete(s),null):`${p.name},${m}`}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}},ve=null});import{existsSync as cs,mkdirSync as bc,readFileSync as Tc,writeFileSync as vc}from"fs";import{join as ls}from"path";import{HOME_DIR as xc}from"@wengine-ai/claude-code-router-shared";function wc(n){try{if(!cs(po))return;let e=JSON.parse(Tc(po,"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 as(n){try{let e=n.getAllStates();if(e.length===0)return;cs(uo)||bc(uo,{recursive:!0});let t=e.map(o=>({...o,updatedAt:Date.now()}));vc(po,JSON.stringify(t,null,2),"utf-8")}catch{}}function fo(){if(is)return;is=!0;let n=U();wc(n),Rc=setInterval(()=>as(n),6e4),process.on("exit",()=>as(n))}function U(n){return lo||(lo=new Nt(n)),lo}var Cc,Sc,Nt,uo,po,is,Rc,lo,re=E(()=>{"use strict";Cc=120,Sc={enabled:!0,failureThreshold:3,probeIntervalMinutes:5,halfOpenSuccessThreshold:2},Nt=class{states=new Map;config;probeTimer;constructor(e){this.config={...Sc,...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}=(Mt(),ic(ss));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??Cc)*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)}},uo=ls(xc,"runtime"),po=ls(uo,"provider-health.json"),is=!1,Rc=null;lo=null});import{existsSync as ms,mkdirSync as Ec,readFileSync as Pc,writeFileSync as Oc}from"fs";import{join as gs}from"path";import{HOME_DIR as Ac}from"@wengine-ai/claude-code-router-shared";function qe(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=mo(o,Ic),s=mo(o,Mc),i=mo(o,Nc);(r||s||i)&&Pe.set(n,{provider:n,remaining:ps(r),limit:ps(s),reset:Lc(i),capturedAt:Date.now()})}function $c(n){return Pe.get(n)}function yo(){return Array.from(Pe.values())}function mo(n,e){for(let t of e){let o=n(t);if(o)return o}return null}function ps(n){if(!n)return null;let e=Number(n.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function Lc(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=Dc(e);return r===null?null:Math.floor((Date.now()+r)/1e3)}function Dc(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 Fc(){try{if(!ms(ho))return;let n=JSON.parse(Pc(ho,"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;Pe.set(t.provider,t)}}catch{}}function fs(){try{if(Pe.size===0)return;ms(go)||Ec(go,{recursive:!0}),Oc(ho,JSON.stringify(Array.from(Pe.values()),null,2),"utf-8")}catch{}}function et(){us||(us=!0,Fc(),ds=setInterval(fs,6e4),ds.unref?.(),process.on("exit",fs))}var go,ho,Pe,us,ds,Ic,Mc,Nc,Oe=E(()=>{"use strict";go=gs(Ac,"runtime"),ho=gs(go,"rate-limit.json"),Pe=new Map,us=!1,ds=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"],Nc=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"]});function Lt(){return _o||(_o=new bo),_o}function vo(){return ko||(ko=new To),ko}function Uc(n){return n?.sessionId||n?.clientContext?.stableSessionId||void 0}function hs(n){let e=Uc(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 ys(n,e){let t=Lt(),o=n?.[$t];if(o&&!o.released){if(o.key===e)return;t.release(o)}n[$t]=t.acquire(e)}function xo(n){let e=n?.[$t];e&&Lt().release(e)}function _s(n,e){let t=hs(n),o=n?.[$t],r=t?vo().get(t)!==void 0:!1;!o&&!r||(t&&vo().bind(t,e),ys(n,e))}function jc(n,e,t){let o=Lt(),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 ks(n,e,t){let o=hs(n),r=vo(),s=Lt(),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=jc(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 ys(n,a),a}var bo,To,_o,ko,$t,Dt=E(()=>{"use strict";bo=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()}},To=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()}},_o=null,ko=null;$t="__concurrencyLease"});async function F(n){let e=await n.text();return Hc(e,n.status)}function Hc(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 bs(n){return(n.headers.get("Content-Type")||"").includes("text/event-stream")}function tt(n){return/^\s*(event:|data:|:\s*[A-Z])/m.test(n)||n.startsWith("data:")}async function pe(n){if(!n.body||n.body.locked)return null;let[e,t]=n.body.tee(),o=e.getReader(),r=new TextDecoder,s="";try{for(let i=0;i<8;i++){let{value:a,done:l}=await o.read();if(l||(a&&(s+=r.decode(a,{stream:!0})),tt(s))||/^\s*[\[{]/.test(s))break}s+=r.decode()}finally{o.releaseLock()}return e.cancel().catch(()=>{}),{body:t,isSSE:tt(s)}}async function fe(n){try{let e=await n.text();if(tt(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 V=E(()=>{"use strict"});var Ae,Co=E(()=>{"use strict";V();Ae=class{name="openai-responses";endPoint="/v1/responses";getCacheReadInputTokens(e){return e?.cache_read_input_tokens??e?.input_tokens_details?.cached_tokens??e?.prompt_tokens_details?.cached_tokens??0}getCacheCreationInputTokens(e){return e?.cache_creation_input_tokens??e?.input_tokens_details?.cache_creation_tokens??e?.input_tokens_details?.cache_write_tokens??e?.prompt_tokens_details?.cache_creation_tokens??e?.prompt_tokens_details?.cache_write_tokens??0}buildResponsesUsage(e){let t=e?.input_tokens??e?.prompt_tokens??0,o=e?.output_tokens??e?.completion_tokens??0,r=this.getCacheReadInputTokens(e),s=this.getCacheCreationInputTokens(e);return{input_tokens:t,output_tokens:o,total_tokens:e?.total_tokens??t+o,input_tokens_details:{cached_tokens:r,cache_creation_tokens:s},output_tokens_details:{reasoning_tokens:e?.output_tokens_details?.reasoning_tokens??0},cache_read_input_tokens:r,cache_creation_input_tokens:s}}async transformRequestIn(e){delete e.temperature,delete e.max_tokens,e.reasoning&&(e.reasoning={effort:e.reasoning.effort,summary:e.reasoning.summary||"auto"});let t=[],o=e.messages.filter(r=>r.role==="system");if(o.length>0){let r=o[0];Array.isArray(r.content)?r.content.forEach(s=>{let i="";typeof s=="string"?i=s:s&&typeof s=="object"&&"text"in s&&(i=s.text),t.push({role:"system",content:i})}):e.instructions=r.content}if(e.messages.forEach(r=>{if(r.role!=="system"){if(Array.isArray(r.content)){let s=r.content.map(i=>this.normalizeRequestContent(i,r.role)).filter(i=>i!==null);s.length>0?r.content=s:delete r.content}if(r.role==="tool"){let s={...r};s.type="function_call_output",s.call_id=r.tool_call_id,s.output=r.content,delete s.cache_control,delete s.role,delete s.tool_call_id,delete s.content,t.push(s);return}if(r.role==="assistant"&&Array.isArray(r.tool_calls)){r.tool_calls.forEach(s=>{t.push({type:"function_call",arguments:s.function.arguments,name:s.function.name,call_id:s.id})});return}t.push(r)}}),e.input=t,delete e.messages,Array.isArray(e.tools)){let r=e.tools.find(s=>s.function.name==="web_search");e.tools=e.tools.filter(s=>s.function.name!=="web_search").map(s=>(s.function.name==="WebSearch"&&delete s.function.parameters.properties.allowed_domains,s.function.name==="Edit"?{type:s.type,name:s.function.name,description:s.function.description,parameters:{...s.function.parameters,required:["file_path","old_string","new_string","replace_all"]},strict:!0}:{type:s.type,name:s.function.name,description:s.function.description,parameters:s.function.parameters})),r&&e.tools.push({type:"web_search"})}return e.parallel_tool_calls=!1,e}async transformResponseOut(e){let t=e.headers.get("Content-Type")||"";if(t.includes("application/json")){let o=await pe(e);if(o?.isSSE&&o.body){let r=new Response(o.body,{status:e.status,statusText:e.statusText,headers:new Headers({...Object.fromEntries(e.headers.entries()),"content-type":"text/event-stream"})});return this.transformResponseOut(r)}if(o?.body)e=new Response(o.body,{status:e.status,statusText:e.statusText,headers:e.headers});else if(o===null){let r=await fe(e);if(r.isSSE)return this.transformResponseOut(r.response);e=r.response}}if(t.includes("application/json")){let o=await F(e);if(o.object==="response"&&o.output){let r=this.convertResponseToChat(o);return new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers})}return o.type==="message"&&Array.isArray(o.content)?new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers}):new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(t.includes("text/event-stream")){if(!e.body)return e;let[o,r]=e.body.tee(),s=o.getReader(),i="";try{let{value: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="",m=!1,h=this,g=new ReadableStream({async start(f){let _=c.getReader(),k=-1,y="",b=T=>(T!==y&&(k++,y=T),k);try{for(;;){let{done:T,value:C}=await _.read();if(T){m||f.enqueue(d.encode(`data: [DONE]
|
|
5
5
|
|
|
6
|
-
`));break}let
|
|
6
|
+
`));break}let v=u.decode(C,{stream:!0});p+=v;let S=p.split(/\r?\n/);p=S.pop()||"";for(let x of S)if(x.trim())try{if(x.startsWith("event: "))continue;if(x.startsWith("data: ")){let w=x.slice(5).trim();if(w==="[DONE]"){m=!0,f.enqueue(d.encode(`data: [DONE]
|
|
7
7
|
|
|
8
|
-
`));continue}try{let R=JSON.parse(
|
|
8
|
+
`));continue}try{let R=JSON.parse(w);if(R.type==="response.output_text.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:b(R.type),delta:{content:R.delta||""},finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(O)}
|
|
9
9
|
|
|
10
10
|
`))}else if(R.type==="response.output_item.added"&&R.item?.type==="function_call"){let O={id:R.item.call_id||R.item.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:b(R.type),delta:{role:"assistant",tool_calls:[{index:0,id:R.item.call_id||R.item.id,function:{name:R.item.name||"",arguments:""},type:"function"}]},finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(O)}
|
|
11
11
|
|
|
12
|
-
`))}else if(R.type==="response.output_item.added"&&R.item?.type==="message"){let O=[];(R.item.content||[]).forEach(
|
|
12
|
+
`))}else if(R.type==="response.output_item.added"&&R.item?.type==="message"){let O=[];(R.item.content||[]).forEach(M=>{M.type==="output_text"&&O.push({type:"text",text:M.text||""})});let P={role:"assistant"};if(O.length===1&&O[0].type==="text"?P.content=O[0].text:O.length>0&&(P.content=O),P.content){let M={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:P,finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(M)}
|
|
13
13
|
|
|
14
14
|
`))}}else if(R.type==="response.output_text.annotation.added"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex",choices:[{index:b(R.type),delta:{annotations:[{type:"url_citation",url_citation:{url:R.annotation?.url||"",title:R.annotation?.title||"",content:"",start_index:R.annotation?.start_index||0,end_index:R.annotation?.end_index||0}}]},finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(O)}
|
|
15
15
|
|
|
16
16
|
`))}else if(R.type==="response.function_call_arguments.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:b(R.type),delta:{tool_calls:[{index:0,function:{arguments:R.delta||""}}]},finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(O)}
|
|
17
17
|
|
|
18
|
-
`))}else if(R.type==="response.completed"){let O=R.response?.output?.some(
|
|
18
|
+
`))}else if(R.type==="response.completed"){let O=R.response?.output?.some(M=>M.type==="function_call")?"tool_calls":"stop",P={id:R.response?.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model||"gpt-5-codex-",choices:[{index:0,delta:{},finish_reason:O}]};f.enqueue(d.encode(`data: ${JSON.stringify(P)}
|
|
19
19
|
|
|
20
|
-
`)),
|
|
20
|
+
`)),m=!0}else if(R.type==="response.reasoning_summary_text.delta"){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:b(R.type),delta:{thinking:{content:R.delta||""}},finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(O)}
|
|
21
21
|
|
|
22
22
|
`))}else if(R.type==="response.reasoning_summary_part.done"&&R.part){let O={id:R.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:R.response?.model,choices:[{index:k,delta:{thinking:{signature:R.item_id}},finish_reason:null}]};f.enqueue(d.encode(`data: ${JSON.stringify(O)}
|
|
23
23
|
|
|
24
|
-
`))}}catch{f.enqueue(d.encode(
|
|
25
|
-
`))}}else f.enqueue(d.encode(
|
|
26
|
-
`))}catch(
|
|
24
|
+
`))}}catch{f.enqueue(d.encode(x+`
|
|
25
|
+
`))}}else f.enqueue(d.encode(x+`
|
|
26
|
+
`))}catch(w){console.error("Error processing line:",x,w),f.enqueue(d.encode(x+`
|
|
27
27
|
`))}}p.trim()&&f.enqueue(d.encode(p+`
|
|
28
|
-
`)),
|
|
28
|
+
`)),m||f.enqueue(d.encode(`data: [DONE]
|
|
29
29
|
|
|
30
|
-
`))}catch(
|
|
30
|
+
`))}catch(T){console.error("Stream error:",T),f.error(T)}finally{try{_.releaseLock()}catch(T){console.error("Error releasing reader lock:",T)}f.close()}}});return new Response(g,{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:m}=await i.read();!m&&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 F(e);return new Response(JSON.stringify(this.wrapChatInResponses(r)),{headers:{"Content-Type":"application/json"}})}catch{return e}}prependToStream(e,t){let r=new TextEncoder().encode(e),s=t.getReader(),i=!1;return new ReadableStream({async pull(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=[],m=(g,f)=>r.encode(`event: ${g}
|
|
31
31
|
data: ${JSON.stringify(f)}
|
|
32
32
|
|
|
33
|
-
`),
|
|
34
|
-
`);_=C.pop()||"";for(let
|
|
33
|
+
`),h=(g,f=[])=>({id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:g,model:s,output:f,usage:this.buildResponsesUsage()});return new ReadableStream({async start(g){try{let f=e.getReader(),_="",k=!1,y=b=>{if(!k){k=!0;for(let T of p)if(T.type==="function_call"){let C=p.indexOf(T);g.enqueue(m("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:T.call_id||T.id,output_index:C,arguments:T.arguments||""})),T.status="completed",g.enqueue(m("response.output_item.done",{type:"response.output_item.done",output_index:C,item:T}))}for(let T of p)if(T.type==="message"){let C=p.indexOf(T);T.status="completed",g.enqueue(m("response.output_item.done",{type:"response.output_item.done",output_index:C,item:T}))}g.enqueue(m("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:T}=await f.read();if(b)break;_+=o.decode(T,{stream:!0});let C=_.split(`
|
|
34
|
+
`);_=C.pop()||"";for(let v of C){let S=v.trim();if(!S||S.startsWith("event:")||!S.startsWith("data:"))continue;let x=S.slice(5).trim();if(x==="[DONE]"){y({}),g.enqueue(r.encode(`data: [DONE]
|
|
35
35
|
|
|
36
|
-
`));continue}let
|
|
37
|
-
data: ${JSON.stringify(
|
|
36
|
+
`));continue}let w;try{w=JSON.parse(x)}catch{continue}l||(l=!0,g.enqueue(m("response.created",{type:"response.created",response:h("in_progress")})),g.enqueue(m("response.in_progress",{type:"response.in_progress",response:h("in_progress")})));let R=w.choices?.[0];if(!R)continue;let O=R.delta;if(O?.content){if(!c){c=!0;let P=a++,M={id:`${i}_msg`,type:"message",status:"in_progress",role:"assistant",content:[]};p.push(M),g.enqueue(m("response.output_item.added",{type:"response.output_item.added",output_index:P,item:M}))}g.enqueue(m("response.output_text.delta",{type:"response.output_text.delta",item_id:`${i}_msg`,output_index:a-1,content_index:0,delta:O.content}))}if(Array.isArray(O?.tool_calls))for(let P of O.tool_calls){if(P.id){u=P.id,d=a++;let M={id:P.id,type:"function_call",status:"in_progress",call_id:P.id,name:P.function?.name||"",arguments:""};p.push(M),g.enqueue(m("response.output_item.added",{type:"response.output_item.added",output_index:d,item:M}))}if(P.function?.arguments){g.enqueue(m("response.function_call_arguments.delta",{type:"response.function_call_arguments.delta",item_id:u,output_index:d,delta:P.function.arguments}));let M=p.find($=>$.call_id===u);M&&(M.arguments+=P.function.arguments)}}R.finish_reason&&y(w)}}k||y({}),g.close()}catch(f){g.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="",m=-1,h="",g="",f=-1,_=0,k=!1,y=!1,b=!1,T=!1,C=null,v=[],S=(M,$)=>r.encode(`event: ${M}
|
|
37
|
+
data: ${JSON.stringify($)}
|
|
38
38
|
|
|
39
|
-
`),
|
|
40
|
-
`);
|
|
39
|
+
`),x=(M,$=[])=>({id:i,object:"response",created_at:Math.floor(Date.now()/1e3),status:M,model:s,output:$,usage:this.buildResponsesUsage()}),w=()=>{k||(k=!0,P.enqueue(S("response.created",{type:"response.created",response:x("in_progress")})),P.enqueue(S("response.in_progress",{type:"response.in_progress",response:x("in_progress")})))},R=(M,$)=>{P.enqueue(S("response.output_item.done",{type:"response.output_item.done",output_index:M,item:{...$,status:"completed"}}))},O=M=>{if(!T){if(T=!0,y){let $=v.find(L=>L.type==="message");$&&($.content=[{type:"output_text",text:a}],$.status="completed")}for(let $ of v)$.type==="function_call"&&($.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:v,usage:M?{...this.buildResponsesUsage(M)}:this.buildResponsesUsage()}})}},P;return new ReadableStream({async start(M){P=M;try{let $=e.getReader(),L="";for(;;){let{done:N,value:D}=await $.read();if(N)break;L+=o.decode(D,{stream:!0});let W=L.split(`
|
|
40
|
+
`);L=W.pop()||"";for(let Te of W){let j=Te.trim();if(!j||j.startsWith("event:")||!j.startsWith("data:"))continue;let Y=j.slice(5).trim();if(Y==="[DONE]")continue;let z;try{z=JSON.parse(Y)}catch{continue}if(z.type==="message_start"&&(i=z.message?.id||i,w()),z.type==="content_block_start"){let A=z.content_block;if(C=A?.type||null,A?.type==="tool_use"){u=A.id||"",d=A.name||"",p="",m=_++;let B={id:u,type:"function_call",status:"in_progress",call_id:u,name:d,arguments:""};v.push(B),P.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:m,item:B}))}if(A?.type==="thinking"&&(h=A.thinking||"",!b)){b=!0,f=_++,g=`rs_${i}`;let B={id:g,type:"reasoning",status:"in_progress",summary:[]};v.push(B),P.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:f,item:B}))}if(A?.type==="text"&&(a=A?.text||"",!y)){y=!0,c=_++,l=`${i}_msg`;let B={id:l,type:"message",status:"in_progress",role:"assistant",content:[]};v.push(B),P.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:c,item:B}))}}if(z.type==="content_block_delta"){let A=z.delta;if(A?.type==="text_delta"){if(!y){y=!0,c=_++,l=`${i}_msg`;let B={id:l,type:"message",status:"in_progress",role:"assistant",content:[]};v.push(B),P.enqueue(S("response.output_item.added",{type:"response.output_item.added",output_index:c,item:B}))}a+=A.text||"",P.enqueue(S("response.output_text.delta",{type:"response.output_text.delta",item_id:l,output_index:c,content_index:0,delta:A.text||""}))}A?.type==="thinking_delta"&&(h+=A.thinking||"",P.enqueue(S("response.reasoning_summary_text.delta",{type:"response.reasoning_summary_text.delta",item_id:g||`rs_${i}`,output_index:f>=0?f:0,summary_index:0,delta:A.thinking||""}))),A?.type==="input_json_delta"&&(p+=A.partial_json||"",P.enqueue(S("response.function_call_arguments.delta",{type:"response.function_call_arguments.delta",item_id:u||"call_0",output_index:m>=0?m:0,delta:A.partial_json||""})))}if(z.type==="content_block_stop"){if(C==="tool_use"&&u){P.enqueue(S("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:u,output_index:m,arguments:p}));let A=v.find(B=>B.call_id===u);A&&(A.arguments=p,A.status="completed"),A&&R(m,A)}if(C==="text"&&y){P.enqueue(S("response.output_text.done",{type:"response.output_text.done",item_id:l,output_index:c,content_index:0,text:a}));let A=v.find(B=>B.type==="message");A&&(A.content=[{type:"output_text",text:a}],R(c,A))}if(C==="thinking"&&b){let A=v.find(B=>B.type==="reasoning");A&&(A.summary=[{type:"summary_text",text:h}],R(f,A))}C=null}if(z.type==="message_delta"){let A=O(z.usage);A&&P.enqueue(A)}if(z.type==="message_stop"){let A=O();A&&P.enqueue(A),P.enqueue(r.encode(`data: [DONE]
|
|
41
41
|
|
|
42
|
-
`))}}}E();let H=O();H&&P.enqueue(H),D="",P.close()}catch(N){P.error(N)}}})}wrapChatInResponses(e){let t=e?.choices?.[0],o=[],r=[];if(t?.message?.content&&r.push({type:"output_text",text:t.message.content}),o.push({type:"message",role:"assistant",content:r}),Array.isArray(t?.message?.tool_calls))for(let s of t.message.tool_calls)o.push({type:"function_call",id:s.id,call_id:s.id,name:s.function?.name||"",arguments:s.function?.arguments||"{}"});return{id:e.id||`resp_${Date.now()}`,object:"response",model:e.model,output:o,usage:e.usage?{...this.buildResponsesUsage(e.usage)}:void 0}}convertResponseToChat(e){let t=e.output?.find(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 ko,X,Ut=w(()=>{"use strict";ko=class{capacity;cache;constructor(e){this.capacity=e,this.cache=new Map}get(e){if(!this.cache.has(e))return;let t=this.cache.get(e);return this.cache.delete(e),this.cache.set(e,t),t}put(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.capacity){let o=this.cache.keys().next().value;o!==void 0&&this.cache.delete(o)}this.cache.set(e,t)}values(){return Array.from(this.cache.values())}delete(e){return this.cache.delete(e)}},X=new ko(100)});import{existsSync as ms,mkdirSync as Tc,readFileSync as vc,writeFileSync as xc}from"fs";import{join as gs}from"path";import{HOME_DIR as Cc}from"@wengine-ai/claude-code-router-shared";function vo(n,e){n&&Ae.set(n,{...e,provider:n,capturedAt:Date.now()})}function jt(n){let e=Ae.get(n);return e?{...e}:void 0}function xo(){return Array.from(Ae.values()).map(n=>({...n}))}function Sc(){try{if(!ms(To))return;let n=JSON.parse(vc(To,"utf-8"));if(!Array.isArray(n))return;let e=Date.now();for(let t of n)if(t&&t.provider){if(t.capturedAt&&e-t.capturedAt>1440*60*1e3)continue;Ae.set(t.provider,t)}}catch{}}function fs(){try{if(Ae.size===0)return;ms(bo)||Tc(bo,{recursive:!0}),xc(To,JSON.stringify(Array.from(Ae.values()),null,2),"utf-8")}catch{}}function ot(){ds||(ds=!0,Sc(),ps=setInterval(fs,6e4),ps.unref?.(),process.on("exit",fs))}var bo,To,Ae,ds,ps,Ie=w(()=>{"use strict";bo=gs(Cc,"runtime"),To=gs(bo,"quota-store.json"),Ae=new Map,ds=!1,ps=null});function Me(n){if(typeof n!="string")return;let e=n.trim();if(!(!e||!Rc.test(e)))return e}function _e(n){if(!n)return;if(typeof n=="object")return Me(n.session_id??n.sessionId);if(typeof n!="string")return;let e=n.trim();if(!e)return;try{let r=JSON.parse(e),s=Me(r?.session_id??r?.sessionId);if(s)return s}catch{}let t="_session_",o=e.lastIndexOf(t);if(o!==-1)return Me(e.slice(o+t.length))}var Rc,$e=w(()=>{"use strict";Rc=/^[A-Za-z0-9_-]+$/});import{existsSync as _s}from"fs";import{readFile as wc,readdir as Ec}from"fs/promises";import{homedir as Pc}from"os";import{isAbsolute as Oc,join as rt,normalize as Ac}from"path";import Ic from"better-sqlite3";function jc(){if(Ne.size<=hs)return;let n=[...Ne.keys()].slice(0,Ne.size-hs);for(let e of n)Ne.delete(e)}function Hc(){if(Le.size<=ys)return;let n=[...Le].slice(0,Le.size-ys);for(let e of n)Le.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 Bc(n,e){let t;try{t=new Ic(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 Jc(n,e){let t;try{t=await Ec(n)}catch{return null}for(let o of t)for(let r of e){let s=rt(n,o,`${r}.json`);if(_s(s))try{let i=JSON.parse(await wc(s,"utf8")),a=i?.meta?.workspacePath??i?.workspacePath;if(typeof a=="string"&&a.trim())return a.trim()}catch{}}return null}async function ks(n){let e=typeof n=="string"?n.trim():"";if(!e)return null;let t=Ne.get(e);if(t)return t;let o=async()=>{let r=zc(e);if(r.length===0)return null;let s=rt(Mc(),$c),i=rt(s,Nc);if(_s(i)){let a=Bc(i,r);if(a)return a}return Jc(rt(s,Lc),r)};try{let r=await o();if(!r&&!Le.has(e)){Le.add(e),Hc();for(let i=0;i<Dc&&!r;i++)await Uc(Fc),r=await o()}if(!r||!Oc(r))return null;let s=Ac(r);return Ne.set(e,s),jc(),s}catch{return null}}var Mc,$c,Nc,Lc,hs,ys,Dc,Fc,Ne,Le,Uc,bs=w(()=>{"use strict";Mc=()=>process.env.CCR_ZCODE_DIR||rt(Pc(),".zcode"),$c="v2",Nc="tasks-index.sqlite",Lc="sessions",hs=500,ys=500,Dc=3,Fc=50,Ne=new Map,Le=new Set,Uc=n=>new Promise(e=>setTimeout(e,n))});import{randomUUID as Wc}from"node:crypto";import{CCR_PROJECT_HEADER as Co}from"@wengine-ai/claude-code-router-shared";function Kc(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 Qc(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 Ts(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 Vc(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 st(n,e=!0){return{clientType:n,usageScope:"request",supportsExplicitExtendedContext:e}}function So(n,e,t=!0){let o=_e(n?.body?.metadata?.user_id);return{clientType:e,usageScope:o?"session":"request",stableSessionId:o,supportsExplicitExtendedContext:t}}function xs(n){return typeof n=="string"&&Gc.includes(n)}function rl(n,e){return/zcode/i.test(e)?!0:Object.keys(n).some(t=>t.toLowerCase().startsWith("x-zcode"))}function De(n){if(xs(n?.clientType))return n.clientType;let e=Qc(n),t=n?.headers||{},o=typeof t["user-agent"]=="string"?t["user-agent"]:"",r=n?.body||{},s=Kc(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(Ts(n,Co).present)return"pi";if(rl(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 Cs(n){return vs[n]}function sl(n){if(typeof n?.__ccrRequestScopeId=="string"&&n.__ccrRequestScopeId)return n.__ccrRequestScopeId;let e=typeof n?.id=="string"&&n.id||Wc();return n.__ccrRequestScopeId=e,e}function ke(n,e={}){let t=De(n),o=Cs(t).createContext(n,e),r=o.stableSessionId||sl(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,Vc(n,Co),n.usageSessionId=r,n.usageCacheKey=`${t}:${s}:${r}`,o}function il(){}var Gc,Yc,Xc,Zc,qc,el,tl,nl,ol,vs,Fe=w(()=>{"use strict";$e();Gc=["claude-code","zcode","pi","qwen-code","opencode","codex","api","unknown"];Yc={type:"claude-code",createContext(n){return So(n,"claude-code")}},Xc={type:"zcode",createContext(n){return So(n,"zcode")}},Zc={type:"pi",createContext(n){let e=Ts(n,Co);return{...st("pi",!1),projectId:e.value,projectHeaderPresent:e.present}}},qc={type:"qwen-code",createContext(n){return So(n,"qwen-code")}},el={type:"opencode",createContext(){return st("opencode")}},tl={type:"codex",createContext(){return st("codex")}},nl={type:"api",createContext(){return st("api")}},ol={type:"unknown",createContext(){return st("unknown")}},vs={"claude-code":Yc,zcode:Xc,pi:Zc,"qwen-code":qc,opencode:el,codex:tl,api:nl,unknown:ol}});import{get_encoding as cl}from"tiktoken";import{readFile as Ro}from"fs/promises";import{opendir as ll,stat as ul}from"fs/promises";import{join as wo}from"path";import{CLAUDE_PROJECTS_DIR as Ss,HOME_DIR as Rs,getClaudeProjectId as ws,isZcodeProjectTakeoverActive as dl}from"@wengine-ai/claude-code-router-shared";function Z(n,e,t,o){let r=Is(e,t);if(r.code==="model_unhealthy"){let a=G(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=As[r.code]||502;throw new q(`Project routing error (${o}): ${r.reason}. Project-level Router target: "${e}". Session: "${s||"unknown"}".`,{sessionId:s,configuredTarget:e,code:r.code,statusCode:i})}function zt(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 at(n){let e=zt(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 fl(n,e){if(!e||!n)return null;let t=zt(n);if(e[n])return e[n];if(e[t])return e[t];let{family:o}=at(n);if(o&&e[o])return e[o];for(let[r,s]of Object.entries(e)){let i=zt(r);if(i&&t.includes(i))return s}return null}function ml(n){return n?(n.input_tokens||0)+(n.cache_read_input_tokens||0)+(n.cache_creation_input_tokens||0):0}function Ht(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 it(n,e,t){let o=n.find(s=>s.name.toLowerCase()===e.toLowerCase());if(!o||o.enabled===!1)return null;let r=o.models?.find(s=>String(s).toLowerCase()===t.toLowerCase());return r?{provider:o,model:String(r)}:null}function Is(n,e){let t=Ht(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(!U().isAvailable(s.name,String(i)))return{reason:`model "${r}" in provider "${o}" is temporarily unavailable (health fail-pool)`,code:"model_unhealthy"};let l=jt(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 G(n,e,t,o,r,s,i){let a=Ht(n);if(!a)return i?null:n;let{providerName:l,routeModel:c}=a,u=it(e,l,c);if(!u){if(r===!0&&o&&!t&&s!==!1){let p=Se(),g=p.getPromotion(l,c,o,e);if(g){let y=Ht(g);if(y){let m=it(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=Se(),g=p.getPromotion(l,c,o,e);if(g){let y=Ht(g);if(y){let m=it(e,y.providerName,y.routeModel);if(m)return`${m.provider.name},${m.model}`}p.clear(l,c,o)}}if(!t&&!U().isAvailable(u.provider.name,u.model))return null;let d=jt(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 V(n,e,t,o,r,s){let i=U(),a=[t?.[n],o?.[n]];for(let l of a)if(!(!Array.isArray(l)||l.length===0))for(let c of l){let u=G(c,e,!1,n,void 0,void 0,s);if(u)return u}return null}function gl(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 Eo(n){let e=zt(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 Ms(n,e,t){return t?.usageScope!=="session"?n:Math.max(n,ml(e))}function $s(n,e,t){return n?.extendedContextThreshold??e?.extendedContextThreshold??t??2e5}function hl(n,e,t,o,r,s,i,a,l,c,u,d){let p=n.clientContext,g=p?.longContextThreshold??t.longContextThreshold??l??6e4,y=Ms(e,r,p),m=n.modelFamily,f=$s(p,t,c),_=p?.supportsExplicitExtendedContext!==!1&&s===!0;if((_||y>f)&&t.extendedContext){let b=G(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?V("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=Z(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?G(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?V("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=Z(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=G(t.webSearch,o,!1,"webSearch",a,d,u);if(b)return{model:b,scenarioType:"webSearch",isFallback:!1};let v=a?V("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=Z(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=G(t.think,o,!1,"think",a,d,u);if(b)return{model:b,scenarioType:"think",isFallback:!1};let v=a?V("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=Z(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=G(t.default,o,!1,"default",a,d,u);if(b)return{model:b,scenarioType:"default",isFallback:!1};let v=a?V("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=Z(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 Tl(){if(be.size<=Es)return;let n=[...be.keys()].slice(0,be.size-Es);for(let e of n)be.delete(e)}function vl(){if(Ue.size<=Ps)return;let n=[...Ue].slice(0,Ue.size-Ps);for(let e of n)Ue.delete(e)}var As,q,le,Bt,pl,yl,Jt,be,Es,Ue,Ps,_l,kl,bl,Os,Ns,je=w(()=>{"use strict";Ut();re();Ie();$t();Ft();$e();bs();Fe();As={provider_not_found:404,model_not_found:404,invalid_model_format:400,provider_disabled:503,model_unhealthy:503,quota_exhausted:503,resolution_failed:502,project_config_error:500,invalid_project_id:400,project_config_not_found:404},q=class extends Error{statusCode;code;type;sessionId;configuredTarget;constructor(e,t){super(e),this.name="ProjectRoutingError",this.statusCode=t?.statusCode||As[t?.code||""]||502,this.code=t?.code||"project_routing_error",this.type="api_error",this.sessionId=t?.sessionId,this.configuredTarget=t?.configuredTarget}};le=cl("cl100k_base"),Bt=(n,e,t)=>{let o=0;return Array.isArray(n)&&n.forEach(r=>{typeof r.content=="string"?o+=le.encode(r.content).length:Array.isArray(r.content)&&r.content.forEach(s=>{s.type==="text"?o+=le.encode(s.text).length:s.type==="tool_use"?o+=le.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(o+=le.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?o+=le.encode(e).length:Array.isArray(e)&&e.forEach(r=>{r.type==="text"&&(typeof r.text=="string"?o+=le.encode(r.text).length:Array.isArray(r.text)&&r.text.forEach(s=>{o+=le.encode(s||"").length}))}),t&&t.forEach(r=>{r.description&&(o+=le.encode(r.name+r.description).length),r.input_schema&&(o+=le.encode(JSON.stringify(r.input_schema)).length)}),o},pl=async(n,e)=>{let t=n.clientContext?.projectHeaderPresent===!0,o=n.clientContext?.projectId;if(t&&(typeof o!="string"||o.length===0||o.length>1024||o==="."||o===".."||/[/\\\0]/.test(o)))throw new q(`Project routing error: invalid managed project id "${String(o)}".`,{configuredTarget:String(o),code:"invalid_project_id"});let r=t?o:n.sessionId?await Ns(n.sessionId):null;if(!r&&!t&&n.clientType==="zcode"&&n.sessionId){let s=await ks(n.sessionId);s&&dl(s)&&(r=ws(s))}if(r){n.projectId=r;let s=wo(Rs,r,"config.json");if(!t&&n.sessionId){let i=wo(Rs,r,`${n.sessionId}.json`);try{let a=JSON.parse(await Ro(i,"utf8"));if(a&&a.Router&&Object.keys(a.Router).length>0)return a.Router}catch{}}try{let i=JSON.parse(await Ro(s,"utf8"));if(t&&(typeof i?.projectPath!="string"||ws(i.projectPath)!==o))throw new q(`Project routing error: managed project id "${o}" does not match its stored project path.`,{configuredTarget:s,code:"invalid_project_id"});if(i&&i.Router&&Object.keys(i.Router).length>0)return i.Router}catch(i){if(i instanceof q)throw i;if(t&&i?.code==="ENOENT")throw new q(`Project routing error: managed project "${o}" no longer has a project config.`,{configuredTarget:s,code:"project_config_not_found"});if(!i||i.code!=="ENOENT"){let a=n.sessionId||n.clientContext?.stableSessionId;throw new q(`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})}}}};yl=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}=at(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=hl(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=G(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?V("default",i,void 0,u,void 0,s):null;if(S)return n.log.info(`Using fallback for explicit model: ${S}`),{model:S,scenarioType:"default"};n.log.warn(`No fallback available for explicit model ${n.body.model}, continuing through routing logic`)}let _=m&&!a?.enableFamilyRouting?null:fl(n.body.model,a?.models);if(_){let x=G(_,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?V("modelMapping",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"modelMapping"};if(s){let T=Z(n,_,i,"modelMapping");if(T)return{model:T,scenarioType:"modelMapping"}}n.log.warn(`Mapped model ${_} unavailable (fail pool), skipping`)}let k=n.clientContext,h=Ms(e,o,k),b=$s(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=G(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?V("extendedContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"extendedContext"};if(s){let T=Z(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=G(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?V("longContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"longContext"};if(s){let T=Z(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=G(a.background,i,!1,"background",l,d,s);if(x)return{model:x,scenarioType:"background"};let S=l?V("background",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"background"};if(s){let T=Z(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=G(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?V("webSearch",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"webSearch"};if(s){let T=Z(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=G(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?V("think",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"think"};if(s){let T=Z(n,a.think,i,"think");if(T)return{model:T,scenarioType:"think"}}}if(a?.default){let x=G(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?V("default",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"default"};let T=G(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=Z(n,a?.default||n.body.model,i,"default");if(x)return{model:x,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},Jt=async(n,e,t)=>{let{configService:o,event:r}=t;!n.originalModel&&n.body.model&&(n.originalModel=n.body.model),(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&ke(n,o.getAll());let s=await pl(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??X.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 Ro(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=Bt(d,p,g),n.tokenCount=k;let h,b=c?void 0:o.get("CUSTOM_ROUTER_PATH");if(b)try{h=await Ze(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 yl(n,k,o,u,s,!!s);h=S.model,n.scenarioType=S.scenarioType}let{family:v}=at(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&&gl(n)&&!Eo(h))if(x.includes(h))n.scenarioType="image";else{let S=x.map(T=>G(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?V("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=Z(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=ls(n,h,{threshold:Number(i?.concurrencyThreshold)||3,fallbackModels:T,resolve:E=>G(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 q)throw m;if(n.log.error(`Error in router middleware: ${m.message}`),c){let f=n.sessionId||n.clientContext?.stableSessionId;throw new q(`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"}},be=new Map,Es=1e3,Ue=new Set,Ps=1e3,_l=3,kl=50,bl=n=>new Promise(e=>setTimeout(e,n));Os=async n=>{let e=await ll(Ss),t=[];for await(let s of e)s.isDirectory()&&t.push(s.name);let o=t.map(async s=>{let i=wo(Ss,s,`${n}.jsonl`);try{return(await ul(i)).isFile()?s:null}catch{return null}}),r=await Promise.all(o);for(let s of r)if(s)return s;return null},Ns=async n=>{let e=Me(n);if(!e)return null;if(be.has(e))return be.get(e)||null;try{let t=await Os(e);if(!t&&!Ue.has(e)){Ue.add(e),vl();for(let o=0;o<_l&&!t;o++)await bl(kl),t=await Os(e)}return t?(be.set(e,t),Tl(),t):null}catch(t){return console.error("Error searching for project by session:",t),null}}});function ct(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
|
+
`))}}}w();let H=O();H&&P.enqueue(H),L="",P.close()}catch($){P.error($)}}})}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 So,X,Ft=E(()=>{"use strict";So=class{capacity;cache;constructor(e){this.capacity=e,this.cache=new Map}get(e){if(!this.cache.has(e))return;let t=this.cache.get(e);return this.cache.delete(e),this.cache.set(e,t),t}put(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.capacity){let o=this.cache.keys().next().value;o!==void 0&&this.cache.delete(o)}this.cache.set(e,t)}values(){return Array.from(this.cache.values())}delete(e){return this.cache.delete(e)}},X=new So(100)});import{existsSync as Cs,mkdirSync as zc,readFileSync as Bc,writeFileSync as Jc}from"fs";import{join as Ss}from"path";import{HOME_DIR as Wc}from"@wengine-ai/claude-code-router-shared";function Eo(n,e){n&&Ie.set(n,{...e,provider:n,capturedAt:Date.now()})}function Ut(n){let e=Ie.get(n);return e?{...e}:void 0}function Po(){return Array.from(Ie.values()).map(n=>({...n}))}function Kc(){try{if(!Cs(wo))return;let n=JSON.parse(Bc(wo,"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;Ie.set(t.provider,t)}}catch{}}function xs(){try{if(Ie.size===0)return;Cs(Ro)||zc(Ro,{recursive:!0}),Jc(wo,JSON.stringify(Array.from(Ie.values()),null,2),"utf-8")}catch{}}function nt(){Ts||(Ts=!0,Kc(),vs=setInterval(xs,6e4),vs.unref?.(),process.on("exit",xs))}var Ro,wo,Ie,Ts,vs,Me=E(()=>{"use strict";Ro=Ss(Wc,"runtime"),wo=Ss(Ro,"quota-store.json"),Ie=new Map,Ts=!1,vs=null});function me(n){if(typeof n!="string")return;let e=n.trim();if(!(!e||!Gc.test(e)))return e}function ge(n){if(!n)return;if(typeof n=="object")return me(n.session_id??n.sessionId);if(typeof n!="string")return;let e=n.trim();if(!e)return;try{let r=JSON.parse(e),s=me(r?.session_id??r?.sessionId);if(s)return s}catch{}let t="_session_",o=e.lastIndexOf(t);if(o!==-1)return me(e.slice(o+t.length))}var Gc,Ne=E(()=>{"use strict";Gc=/^[A-Za-z0-9_-]+$/});import{existsSync as Es}from"fs";import{readFile as Vc,readdir as Qc}from"fs/promises";import{homedir as Yc}from"os";import{isAbsolute as Xc,join as ot,normalize as Zc}from"path";import{openSqliteDatabase as qc}from"@wengine-ai/claude-code-router-shared";function al(){if($e.size<=Rs)return;let n=[...$e.keys()].slice(0,$e.size-Rs);for(let e of n)$e.delete(e)}function cl(){if(Le.size<=ws)return;let n=[...Le].slice(0,Le.size-ws);for(let e of n)Le.delete(e)}function ll(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 ul(n,e){let t;try{t=qc(n,{readonly:!0}).db;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 dl(n,e){let t;try{t=await Qc(n)}catch{return null}for(let o of t)for(let r of e){let s=ot(n,o,`${r}.json`);if(Es(s))try{let i=JSON.parse(await Vc(s,"utf8")),a=i?.meta?.workspacePath??i?.workspacePath;if(typeof a=="string"&&a.trim())return a.trim()}catch{}}return null}async function Ps(n){let e=typeof n=="string"?n.trim():"";if(!e)return null;let t=$e.get(e);if(t)return t;let o=async()=>{let r=ll(e);if(r.length===0)return null;let s=ot(el(),tl),i=ot(s,nl);if(Es(i)){let a=ul(i,r);if(a)return a}return dl(ot(s,ol),r)};try{let r=await o();if(!r&&!Le.has(e)){Le.add(e),cl();for(let i=0;i<rl&&!r;i++)await il(sl),r=await o()}if(!r||!Xc(r))return null;let s=Zc(r);return $e.set(e,s),al(),s}catch{return null}}var el,tl,nl,ol,Rs,ws,rl,sl,$e,Le,il,Os=E(()=>{"use strict";el=()=>process.env.CCR_ZCODE_DIR||ot(Yc(),".zcode"),tl="v2",nl="tasks-index.sqlite",ol="sessions",Rs=500,ws=500,rl=3,sl=50,$e=new Map,Le=new Set,il=n=>new Promise(e=>setTimeout(e,n))});import{randomUUID as pl}from"node:crypto";import{CCR_PROJECT_HEADER as Oo}from"@wengine-ai/claude-code-router-shared";function Ms(n){return typeof n=="string"&&n.length<=fl}function ml(n){if(!(!n?.headers||typeof n.headers!="object"))for(let e of Is){let{value:t}=jt(n,e);if(!Ms(t))continue;let o=me(t);if(o)return o}}function hl(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 yl(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 jt(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 As(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 rt(n,e=!0){return{clientType:n,usageScope:"request",supportsExplicitExtendedContext:e}}function Ao(n,e,t=!0){let o=ge(n?.body?.metadata?.user_id);return{clientType:e,usageScope:o?"session":"request",stableSessionId:o,supportsExplicitExtendedContext:t}}function $s(n){return typeof n=="string"&&gl.includes(n)}function Rl(n,e){return/zcode/i.test(e)?!0:Object.keys(n).some(t=>t.toLowerCase().startsWith("x-zcode"))}function De(n){if($s(n?.clientType))return n.clientType;let e=yl(n),t=n?.headers||{},o=typeof t["user-agent"]=="string"?t["user-agent"]:"",r=n?.body||{},s=hl(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(jt(n,Oo).present||jt(n,"x-session-affinity").present)return"pi";if(Rl(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 Ls(n){return Ns[n]}function wl(n){if(typeof n?.__ccrRequestScopeId=="string"&&n.__ccrRequestScopeId)return n.__ccrRequestScopeId;let e=typeof n?.id=="string"&&n.id||pl();return n.__ccrRequestScopeId=e,e}function xe(n,e={}){let t=De(n),o=Ls(t).createContext(n,e),r=o.usageScope==="session"&&o.stableSessionId?o.stableSessionId:wl(n),s=o.usageScope==="session"?"session":"request";n.clientType=t,n.clientContext=o,o.stableSessionId?n.sessionId=o.stableSessionId:delete n.sessionId,o.projectHeaderPresent?n.projectId=o.projectId:delete n.projectId,As(n,Oo),t==="pi"&&o.stableSessionId&&(n.__ccrSessionAffinity=o.stableSessionId);for(let i of Is)As(n,i);return n.usageSessionId=r,n.usageCacheKey=`${t}:${s}:${r}`,o}function El(){}var Is,fl,gl,_l,kl,bl,Tl,vl,xl,Cl,Sl,Ns,Fe=E(()=>{"use strict";Ne();Is=["x-session-affinity","x-session-id","x-litellm-session-id"],fl=256;gl=["claude-code","zcode","pi","qwen-code","opencode","codex","api","unknown"];_l={type:"claude-code",createContext(n){return Ao(n,"claude-code")}},kl={type:"zcode",createContext(n){return Ao(n,"zcode")}},bl={type:"pi",createContext(n){let e=jt(n,Oo),t=ml(n)||(typeof n?.__ccrSessionAffinity=="string"?n.__ccrSessionAffinity:void 0)||ge(n?.body?.metadata?.user_id),o=Ms(t)?t:void 0;return{...rt("pi",!1),stableSessionId:o,projectId:e.value,projectHeaderPresent:e.present}}},Tl={type:"qwen-code",createContext(n){return Ao(n,"qwen-code")}},vl={type:"opencode",createContext(){return rt("opencode")}},xl={type:"codex",createContext(){return rt("codex")}},Cl={type:"api",createContext(){return rt("api")}},Sl={type:"unknown",createContext(){return rt("unknown")}},Ns={"claude-code":_l,zcode:kl,pi:bl,"qwen-code":Tl,opencode:vl,codex:xl,api:Cl,unknown:Sl}});import{get_encoding as Ol}from"tiktoken";import{readFile as Io}from"fs/promises";import{opendir as Al,stat as Il}from"fs/promises";import{join as Mo}from"path";import{CLAUDE_PROJECTS_DIR as Ds,HOME_DIR as Fs,getClaudeProjectId as Us,isZcodeProjectTakeoverActive as Ml}from"@wengine-ai/claude-code-router-shared";function Z(n,e,t,o){let r=Js(e,t);if(r.code==="model_unhealthy"){let a=K(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=Bs[r.code]||502;throw new q(`Project routing error (${o}): ${r.reason}. Project-level Router target: "${e}". Session: "${s||"unknown"}".`,{sessionId:s,configuredTarget:e,code:r.code,statusCode:i})}function zt(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 it(n){let e=zt(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 $l(n,e){if(!e||!n)return null;let t=zt(n);if(e[n])return e[n];if(e[t])return e[t];let{family:o}=it(n);if(o&&e[o])return e[o];for(let[r,s]of Object.entries(e)){let i=zt(r);if(i&&t.includes(i))return s}return null}function Ll(n){return n?(n.input_tokens||0)+(n.cache_read_input_tokens||0)+(n.cache_creation_input_tokens||0):0}function Ht(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 st(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 Js(n,e){let t=Ht(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(!U().isAvailable(s.name,String(i)))return{reason:`model "${r}" in provider "${o}" is temporarily unavailable (health fail-pool)`,code:"model_unhealthy"};let l=Ut(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 K(n,e,t,o,r,s,i){let a=Ht(n);if(!a)return i?null:n;let{providerName:l,routeModel:c}=a,u=st(e,l,c);if(!u){if(r===!0&&o&&!t&&s!==!1){let p=Ee(),m=p.getPromotion(l,c,o,e);if(m){let h=Ht(m);if(h){let g=st(e,h.providerName,h.routeModel);if(g)return`${g.provider.name},${g.model}`}p.clear(l,c,o)}}return null}if(r===!0&&o&&!t&&s!==!1){let p=Ee(),m=p.getPromotion(l,c,o,e);if(m){let h=Ht(m);if(h){let g=st(e,h.providerName,h.routeModel);if(g)return`${g.provider.name},${g.model}`}p.clear(l,c,o)}}if(!t&&!U().isAvailable(u.provider.name,u.model))return null;let d=Ut(u.provider.name);if(d){let p=d.limitDaily!==void 0&&d.usedDailyBalance!==void 0&&d.usedDailyBalance>=d.limitDaily,m=d.totalBalance!==void 0&&d.usedBalance!==void 0&&d.usedBalance>=d.totalBalance;if(p||m)return null}return`${u.provider.name},${u.model}`}function Q(n,e,t,o,r,s){let i=U(),a=[t?.[n],o?.[n]];for(let l of a)if(!(!Array.isArray(l)||l.length===0))for(let c of l){let u=K(c,e,!1,n,void 0,void 0,s);if(u)return u}return null}function Dl(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 No(n){let e=zt(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 Ws(n,e,t){return t?.usageScope!=="session"?n:Math.max(n,Ll(e))}function Ks(n,e,t){return n?.extendedContextThreshold??e?.extendedContextThreshold??t??2e5}function Fl(n,e,t,o,r,s,i,a,l,c,u,d){let p=n.clientContext,m=p?.longContextThreshold??t.longContextThreshold??l??6e4,h=Ws(e,r,p),g=n.modelFamily,f=Ks(p,t,c),_=p?.supportsExplicitExtendedContext!==!1&&s===!0;if((_||h>f)&&t.extendedContext){let b=K(t.extendedContext,o,!1,"extendedContext",a,d,u);if(b)return n.log.info(`Family: using extended context model (1M+), tokens: ${h}, estimated: ${e}, explicit: ${_}, threshold: ${f}`),{model:b,scenarioType:"extendedContext",isFallback:!1};let T=a?Q("extendedContext",o,t.fallback,i,g,u):null;if(T)return n.log.info(`Family: using extended context fallback model (1M+), tokens: ${h}, estimated: ${e}, explicit: ${_}, threshold: ${f}`),{model:T,scenarioType:"extendedContext",isFallback:!0};if(u){let C=Z(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(h>m&&(t.longContext||t.fallback?.longContext?.length||i?.longContext?.length)){let b=t.longContext?K(t.longContext,o,!1,"longContext",a,d,u):null;if(b)return n.log.info(`Family: using long context model, tokens: ${h}, estimated: ${e}`),{model:b,scenarioType:"longContext",isFallback:!1};let T=a?Q("longContext",o,t.fallback,i,g,u):null;if(T)return n.log.info(`Family: using long context fallback model, tokens: ${h}, estimated: ${e}`),{model:T,scenarioType:"longContext",isFallback:!0};if(u&&t.longContext){let C=Z(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=K(t.webSearch,o,!1,"webSearch",a,d,u);if(b)return{model:b,scenarioType:"webSearch",isFallback:!1};let T=a?Q("webSearch",o,t.fallback,i,g,u):null;if(T)return n.log.info("Family: using webSearch fallback model"),{model:T,scenarioType:"webSearch",isFallback:!0};if(u){let C=Z(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=K(t.think,o,!1,"think",a,d,u);if(b)return{model:b,scenarioType:"think",isFallback:!1};let T=a?Q("think",o,t.fallback,i,g,u):null;if(T)return n.log.info("Family: using think fallback model"),{model:T,scenarioType:"think",isFallback:!0};if(u){let C=Z(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=K(t.default,o,!1,"default",a,d,u);if(b)return{model:b,scenarioType:"default",isFallback:!1};let T=a?Q("default",o,t.fallback,i,g,u):null;if(T)return n.log.info("Family: using default fallback model"),{model:T,scenarioType:"default",isFallback:!0};if(u){let C=Z(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 Bl(){if(Ce.size<=js)return;let n=[...Ce.keys()].slice(0,Ce.size-js);for(let e of n)Ce.delete(e)}function Jl(){if(Ue.size<=Hs)return;let n=[...Ue].slice(0,Ue.size-Hs);for(let e of n)Ue.delete(e)}var Bs,q,le,Bt,Nl,Ul,Jt,Ce,js,Ue,Hs,jl,Hl,zl,zs,Gs,je=E(()=>{"use strict";Ft();re();Me();Mt();Dt();Ne();Os();Fe();Bs={provider_not_found:404,model_not_found:404,invalid_model_format:400,provider_disabled:503,model_unhealthy:503,quota_exhausted:503,resolution_failed:502,project_config_error:500,invalid_project_id:400,project_config_not_found:404},q=class extends Error{statusCode;code;type;sessionId;configuredTarget;constructor(e,t){super(e),this.name="ProjectRoutingError",this.statusCode=t?.statusCode||Bs[t?.code||""]||502,this.code=t?.code||"project_routing_error",this.type="api_error",this.sessionId=t?.sessionId,this.configuredTarget=t?.configuredTarget}};le=Ol("cl100k_base"),Bt=(n,e,t)=>{let o=0;return Array.isArray(n)&&n.forEach(r=>{typeof r.content=="string"?o+=le.encode(r.content).length:Array.isArray(r.content)&&r.content.forEach(s=>{s.type==="text"?o+=le.encode(s.text).length:s.type==="tool_use"?o+=le.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(o+=le.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?o+=le.encode(e).length:Array.isArray(e)&&e.forEach(r=>{r.type==="text"&&(typeof r.text=="string"?o+=le.encode(r.text).length:Array.isArray(r.text)&&r.text.forEach(s=>{o+=le.encode(s||"").length}))}),t&&t.forEach(r=>{r.description&&(o+=le.encode(r.name+r.description).length),r.input_schema&&(o+=le.encode(JSON.stringify(r.input_schema)).length)}),o},Nl=async(n,e)=>{let t=n.clientContext?.projectHeaderPresent===!0,o=n.clientContext?.projectId;if(t&&(typeof o!="string"||o.length===0||o.length>1024||o==="."||o===".."||/[/\\\0]/.test(o)))throw new q(`Project routing error: invalid managed project id "${String(o)}".`,{configuredTarget:String(o),code:"invalid_project_id"});let r=t?o:n.sessionId?await Gs(n.sessionId):null;if(!r&&!t&&n.clientType==="zcode"&&n.sessionId){let s=await Ps(n.sessionId);s&&Ml(s)&&(r=Us(s))}if(r){n.projectId=r;let s=Mo(Fs,r,"config.json");if(!t&&n.sessionId){let i=Mo(Fs,r,`${n.sessionId}.json`);try{let a=JSON.parse(await Io(i,"utf8"));if(a&&a.Router&&Object.keys(a.Router).length>0)return a.Router}catch{}}try{let i=JSON.parse(await Io(s,"utf8"));if(t&&(typeof i?.projectPath!="string"||Us(i.projectPath)!==o))throw new q(`Project routing error: managed project id "${o}" does not match its stored project path.`,{configuredTarget:s,code:"invalid_project_id"});if(i&&i.Router&&Object.keys(i.Router).length>0)return i.Router}catch(i){if(i instanceof q)throw i;if(t&&i?.code==="ENOENT")throw new q(`Project routing error: managed project "${o}" no longer has a project config.`,{configuredTarget:s,code:"project_config_not_found"});if(!i||i.code!=="ENOENT"){let a=n.sessionId||n.clientContext?.stableSessionId;throw new q(`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})}}}};Ul=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 v=n.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);v&&(n.body.system[1].text=n.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${v[1]}</CCR-SUBAGENT-MODEL>`,""),p=v[1])}let{family:m,extended:h,isCcrAlias:g}=it(n.body.model),f=a?.families?.[m||""];if(f&&a?.enableFamilyRouting){n.log.info(`Using model family routing for: ${m}${h?" (1M)":""}`),n.modelFamily=m,n.familyFallback=f.fallback;let v=Fl(n,e,f,i,o,h,u,l,a?.longContextThreshold,a?.extendedContextThreshold,s,d);if(v)return{model:v.model,scenarioType:v.scenarioType}}if(n.body.model.includes(",")&&!s){let v=K(n.body.model,i,!1,"default",l,d,s);if(v)return{model:v,scenarioType:"default"};n.log.warn(`Explicit model ${n.body.model} unavailable (fail pool), trying fallback`);let S=l?Q("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 _=g&&!a?.enableFamilyRouting?null:$l(n.body.model,a?.models);if(_){let v=K(_,i,!1,"modelMapping",l,d,s);if(v)return n.log.info(`Using mapped model for ${n.body.model}: ${_}`),{model:v,scenarioType:"modelMapping"};let S=l?Q("modelMapping",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"modelMapping"};if(s){let x=Z(n,_,i,"modelMapping");if(x)return{model:x,scenarioType:"modelMapping"}}n.log.warn(`Mapped model ${_} unavailable (fail pool), skipping`)}let k=n.clientContext,y=Ws(e,o,k),b=Ks(k,void 0,a?.extendedContextThreshold);if(y>b&&a?.extendedContext){n.log.info(`Using extended context (1M) model due to token count: ${y}, estimated: ${e}, threshold: ${b}`);let v=K(a.extendedContext,i,!1,"extendedContext",l,d,s);if(v)return{model:v,scenarioType:"extendedContext"};n.log.warn(`Extended context model ${a.extendedContext} unavailable (fail pool), trying fallback`);let S=l?Q("extendedContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"extendedContext"};if(s){let x=Z(n,a.extendedContext,i,"extendedContext");if(x)return{model:x,scenarioType:"extendedContext"}}}let T=k?.longContextThreshold??a?.longContextThreshold??6e4;if(y>T&&a?.longContext){n.log.info(`Using long context model due to token count: ${y}, estimated: ${e}, threshold: ${T}`);let v=K(a.longContext,i,!1,"longContext",l,d,s);if(v)return{model:v,scenarioType:"longContext"};n.log.warn(`Long context model ${a.longContext} unavailable (fail pool), trying fallback`);let S=l?Q("longContext",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"longContext"};if(s){let x=Z(n,a.longContext,i,"longContext");if(x)return{model:x,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 v=K(a.background,i,!1,"background",l,d,s);if(v)return{model:v,scenarioType:"background"};let S=l?Q("background",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"background"};if(s){let x=Z(n,a.background,i,"background");if(x)return{model:x,scenarioType:"background"}}n.log.warn(`Background model ${a.background} unavailable (fail pool), falling through`)}if(Array.isArray(n.body.tools)&&n.body.tools.some(v=>v.type?.startsWith("web_search"))&&a?.webSearch){let v=K(a.webSearch,i,!1,"webSearch",l,d,s);if(v)return{model:v,scenarioType:"webSearch"};n.log.warn(`WebSearch model ${a.webSearch} unavailable (fail pool), trying fallback`);let S=l?Q("webSearch",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"webSearch"};if(s){let x=Z(n,a.webSearch,i,"webSearch");if(x)return{model:x,scenarioType:"webSearch"}}}if(n.body.thinking?.type==="enabled"&&a?.think){n.log.info(`Using think model for ${n.body.thinking}`);let v=K(a.think,i,!1,"think",l,d,s);if(v)return{model:v,scenarioType:"think"};n.log.warn(`Think model ${a.think} unavailable (fail pool), trying fallback`);let S=l?Q("think",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"think"};if(s){let x=Z(n,a.think,i,"think");if(x)return{model:x,scenarioType:"think"}}}if(a?.default){let v=K(a.default,i,!1,"default",l,d,s);if(v)return{model:v,scenarioType:"default"};n.log.warn(`Default model ${a.default} unavailable (fail pool), trying fallback`);let S=l?Q("default",i,void 0,u,void 0,s):null;if(S)return{model:S,scenarioType:"default"};let x=K(a.default,i,!0,"default",l,d,s);if(x)return n.log.warn(`No fallback available; retrying default model ${a.default} despite fail-pool state (strict=${s})`),{model:x,scenarioType:"default"}}if(s){let v=Z(n,a?.default||n.body.model,i,"default");if(v)return{model:v,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},Jt=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)&&xe(n,o.getAll());let s=await Nl(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??X.get(n.usageCacheKey):void 0,{messages:d,system:p=[],tools:m}=n.body,h=o.get("REWRITE_SYSTEM_PROMPT");if(h&&p.length>1&&p[1]?.text?.includes("<env>")){let g=await Io(h,"utf-8");p[1].text=`${g}<env>${p[1].text.split("<env>").pop()}`}try{let[g,f]=n.body.model.split(","),_=t.tokenizerService?.getTokenizerConfigForModel(g,f),k;t.tokenizerService?k=(await t.tokenizerService.countTokens({messages:d,system:p,tools:m},_,g)).tokenCount:k=Bt(d,p,m),n.tokenCount=k;let y,b=c?void 0:o.get("CUSTOM_ROUTER_PATH");if(b)try{y=await Xe(b)(n,o.getAll(),{event:r})}catch(S){n.log.error(`failed to load custom router: ${S.message}`)}if(y)n.scenarioType="default";else{let S=await Ul(n,k,o,u,s,!!s);y=S.model,n.scenarioType=S.scenarioType}let{family:T}=it(n.originalModel||n.body.model),C=i?.enableFamilyRouting&&T?i.families?.[T]:void 0,v=[C?.image,i?.image].filter((S,x,w)=>!!S&&w.indexOf(S)===x);if(v.length>0&&Dl(n)&&!No(y))if(v.includes(y))n.scenarioType="image";else{let S=v.map(x=>K(x,l,!1,"image",a,!c,c)).find(x=>!!x);if(S)n.log.info(`Using image model fallback for ${y}`),y=S,n.scenarioType="image";else{let x=a?Q("image",l,C?.fallback,n.fallbackConfig,T||void 0,c):null;if(x)n.log.info(`Using image fallback model: ${x}`),y=x,n.scenarioType="image";else if(c){let w=Z(n,v[0],l,"image");w&&(y=w,n.scenarioType="image")}else n.log.warn(`Image models ${v.join(", ")} unavailable (fail pool), keeping ${y}`),n.scenarioType="image"}}if(i?.enableConcurrencyPriority===!0&&!(n.originalModel||"").includes(",")&&!n.subagentOverrideModel&&typeof y=="string"&&y.trim()){let S=n.scenarioType||"default",x=[...(n.familyFallback||{})[S]||[],...(n.fallbackConfig||{})[S]||[]];y=ks(n,y,{threshold:Number(i?.concurrencyThreshold)||3,fallbackModels:x,resolve:w=>K(w,l,!1,S,a,!c,c),log:w=>n.log.info(w)})}typeof y=="string"&&y.trim()?n.body.model=y:(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(g){if(g instanceof q)throw g;if(n.log.error(`Error in router middleware: ${g.message}`),c){let f=n.sessionId||n.clientContext?.stableSessionId;throw new q(`Project routing error: router failure in project mode. ${g.message}. Session: "${f||"unknown"}".`,{sessionId:f,configuredTarget:i?.default,code:"project_router_failure",statusCode:502})}n.body.model=i?.default,n.scenarioType="default"}},Ce=new Map,js=1e3,Ue=new Set,Hs=1e3,jl=3,Hl=50,zl=n=>new Promise(e=>setTimeout(e,n));zs=async n=>{let e=await Al(Ds),t=[];for await(let s of e)s.isDirectory()&&t.push(s.name);let o=t.map(async s=>{let i=Mo(Ds,s,`${n}.jsonl`);try{return(await Il(i)).isFile()?s:null}catch{return null}}),r=await Promise.all(o);for(let s of r)if(s)return s;return null},Gs=async n=>{let e=me(n);if(!e)return null;if(Ce.has(e))return Ce.get(e)||null;try{let t=await zs(e);if(!t&&!Ue.has(e)){Ue.add(e),Jl();for(let o=0;o<jl&&!t;o++)await zl(Hl),t=await zs(e)}return t?(Ce.set(e,t),Bl(),t):null}catch(t){return console.error("Error searching for project by session:",t),null}}});function at(n){let e=[],t=Array.isArray(n.input)?n.input:[n.input];for(let o of t)if(o.type==="function_call")e.push({role:"assistant",tool_calls:[{id:o.call_id,type:"function",function:{name:o.name,arguments:o.arguments||"{}"}}]});else if(o.type==="function_call_output")e.push({role:"tool",tool_call_id:o.call_id,content:typeof o.output=="string"?o.output:JSON.stringify(o.output)});else if(o.type==="reasoning"){let r=Array.isArray(o.summary)?o.summary.filter(s=>s.type==="summary_text"&&s.text).map(s=>s.text).join(`
|
|
43
43
|
`):"";r&&e.push({role:"assistant",content:[{type:"text",text:`[reasoning]
|
|
44
|
-
${r}`}],_reasoningItem:!0})}else if(o.role){let r=o.content;Array.isArray(r)&&(r=r.map(s=>s.type==="input_text"?{type:"text",text:s.text}:s.type==="output_text"?{type:"text",text:s.text}:s.type==="input_image"?{type:"image_url",image_url:{url:s.image_url},media_type:s.mime_type}:s)),e.push({role:o.role,content:r})}n.messages=e,delete n.input,n.instructions&&typeof n.instructions=="string"&&(n.system=n.instructions,delete n.instructions),Array.isArray(n.tools)&&(n.tools=n.tools.filter(o=>o&&typeof o=="object").map(o=>o.type==="function"&&o.name&&!o.function?{type:"function",function:{name:o.name,description:o.description||"",parameters:o.parameters||{type:"object",properties:{}}}}:o.function?.name?o:o.type==="web_search"?{type:"function",function:{name:"web_search",description:"Search the web",parameters:{type:"object",properties:{query:{type:"string"}}}}}:null).filter(Boolean)),n.reasoning&&typeof n.reasoning=="object"&&(n.reasoning={enabled:!0,effort:n.reasoning.effort}),delete n.store,delete n.previous_response_id,delete n.include}async function
|
|
44
|
+
${r}`}],_reasoningItem:!0})}else if(o.role){let r=o.content;Array.isArray(r)&&(r=r.map(s=>s.type==="input_text"?{type:"text",text:s.text}:s.type==="output_text"?{type:"text",text:s.text}:s.type==="input_image"?{type:"image_url",image_url:{url:s.image_url},media_type:s.mime_type}:s)),e.push({role:o.role,content:r})}n.messages=e,delete n.input,n.instructions&&typeof n.instructions=="string"&&(n.system=n.instructions,delete n.instructions),Array.isArray(n.tools)&&(n.tools=n.tools.filter(o=>o&&typeof o=="object").map(o=>o.type==="function"&&o.name&&!o.function?{type:"function",function:{name:o.name,description:o.description||"",parameters:o.parameters||{type:"object",properties:{}}}}:o.function?.name?o:o.type==="web_search"?{type:"function",function:{name:"web_search",description:"Search the web",parameters:{type:"object",properties:{query:{type:"string"}}}}}:null).filter(Boolean)),n.reasoning&&typeof n.reasoning=="object"&&(n.reasoning={enabled:!0,effort:n.reasoning.effort}),delete n.store,delete n.previous_response_id,delete n.include}async function Vs(n,e,t,o){let r=n.body;(n.ccrHookOrder||=[]).push("handler"),o.endPoint==="/v1/responses"&&r?.input&&!r?.messages&&at(r),o.endPoint==="/v1/responses"&&!n.provider&&typeof r?.model=="string"&&Wl.test(r.model)&&await Jt(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(","),m=t.providerService.getProvider(d);m&&(i=m,r.model=p.join(","))}}if(n.originalModel||(n.originalModel=r?.model),!i)throw J(`Provider '${s}' not found`,404,"provider_not_found");n.provider=i.name||s;let a=Ql(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&&at(r);let{requestBody:c,config:u,bypass:d}=await Ys(r,i,o,n.headers,{req:n});if(c?.messages&&!d&&!n.isTargetAnthropic){let g=c.messages,f=new Set,_=[],k=[];for(let y of g){if(y?.role!=="system"){k.push(y);continue}let b=JSON.stringify(y.content??"");f.has(b)||(f.add(b),_.push(y))}_.length>0&&k.length>0&&(c.messages=[..._,...k])}let p=await Xs(c,u,i,t,d,o,{req:n});try{i?.baseUrl&&p?.headers&&qe(s,i.baseUrl,p.headers)}catch{}let m=await Zs(c,p,i,o,d,{req:n,isCodexOnMessagesEndpoint:l});m=await Qs(r,m,i.name||s);let h=qs(m,e,r,t);try{let g=r.model||n.originalModel;s&&g&&U().recordSuccess(s,g)}catch{}return h}catch(c){if(c instanceof q)throw c;if(!n.__retryAttempted){n.__retryAttempted=!0;try{return await Vs(n,e,t,o)}catch(d){if(n.__fallbackExhausted)throw d;c=d}}let u=await Kl(n,e,t,o,c);if(u)return u;throw n.__fallbackExhausted=!0,c}}async function Qs(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(`
|
|
45
45
|
`).some(d=>d.startsWith("data:"))))break}if(i+=r.decode(),s.length===0)throw o.releaseLock(),J(`Provider returned empty streaming response from ${t}`,400,"provider_response_error");if(i.includes("event: error")){o.releaseLock();let c=i.length<500?i:i.slice(0,500);throw J(`Provider ${t} returned error in SSE stream: ${c}`,400,"provider_response_error")}if(i.split(`
|
|
46
|
-
`).filter(c=>c.startsWith("data:")&&c.trim().length>5).length===0){o.releaseLock();let c=i.length<500?i:i.slice(0,500);throw J(`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 Cl(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=U(),u=n.provider||"",d=n.body.model||"",p=new Set,g=r?.statusCode===429||r?.isRateLimit===!0||r?.rawBody&&Wt(r.rawBody)||r?.message&&Wt(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=it(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 O={...n,provider:x,body:R},P=C.provider,{requestBody:A,config:N,bypass:D}=await Fs(R,P,o,n.headers,{req:O}),H=await Us(A,N,P,t,D,o,{req:O});try{P?.baseUrl&&H?.headers&&et(x,P.baseUrl,H.headers)}catch{}let $=await js(A,H,P,o,D,{req:O,isCodexOnMessagesEndpoint:n.isCodexOnMessagesEndpoint||!1});return $=await Ds(R,$,x),n.log.info(`Fallback model ${x},${S} succeeded`),c.recordSuccess(x,S),u&&d&&!n.strictProjectRouting&&(Se().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,cs(n,`${x},${S}`),Hs($,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 Fs(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=Sl(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 Sl(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 Rl(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 Us(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];Ot(o.configService)&&Jr(o.log,i.req.id,{url:String(a),headers:l,body:n});let c=await Kr(a,n,{...e,headers:JSON.parse(JSON.stringify(l)),httpsProxy:ce(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=J(`Error from provider(${t.name},${n.model}: ${c.status}): ${u}`,c.status,"provider_response_error");throw d.isRateLimit=c.status===429||Wt(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(nt(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=Wt(g),_=J(`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=J(`API returned an empty or malformed response (HTTP 200) from ${t.name} \u2014 check for a proxy or gateway intercepting the request`,400,"provider_response_error");throw m.rawBody=g,m}throw y}}}if(!n.stream&&Ot(o.configService)){let d=await c.clone().text();Wr(o.log,i.req.id,{status:c.status,headers:Object.fromEntries(c.headers.entries()),body:d})}return c}async function js(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 Oe().transformResponseIn(i,{...s,provider:t})}catch(a){console.error("Failed to apply Responses API conversion for codex client:",a)}return i}async function Hs(n,e,t,o){n.ok||e.code(n.status);let r=us(n);if(!r){let s=await Ee(n);if(s)s.isSSE&&(r=!0),n=new Response(s.body,{status:n.status,statusText:n.statusText,headers:n.headers});else{let i=await Pe(n);n=i.response,i.isSSE&&(r=!0)}}if(r){if(e.header("Content-Type","text/event-stream"),e.header("Cache-Control","no-cache"),e.header("Connection","keep-alive"),o&&Ot(o.configService)){let[s,i]=n.body.tee(),a=Br(o.configService);return Gr(s,o.log,e.request.id,a),e.send(i)}return e.send(n.body)}else return F(n)}function Wt(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 wl(n){try{return new URL(n),!0}catch{return!1}}var xl,zs,Gt=w(()=>{"use strict";Qr();Et();qe();to();re();we();$t();Ft();_o();Q();je();oe();xl=/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i;zs=async n=>{n.get("/",async()=>({message:"LLMs API",version:Mt})),n.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),n.get("/providers/health",async()=>({states:U().getAllStates().map(r=>({provider:r.provider,model:r.model,status:r.status,failureCount:r.failureCount,successCount:r.successCount,lastFailureTime:r.lastFailureTime,lastError:r.lastError,rateLimitUntil:r.rateLimitUntil??null})),timestamp:new Date().toISOString()}));let e=n.transformerService.getTransformersWithEndpoint();for(let{transformer:t}of e)t.endPoint&&n.post(t.endPoint,async(o,r)=>Ls(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 J("Provider name is required",400,"invalid_request");if(!s||!wl(s))throw J("Valid base URL is required",400,"invalid_request");if(!i?.trim())throw J("API key is required",400,"invalid_request");if(!a||!Array.isArray(a)||a.length===0)throw J("At least one model is required",400,"invalid_request");if(n.providerService.getProvider(t.body.name))throw J(`Provider with name '${t.body.name}' already exists`,400,"provider_exists");return n.providerService.registerProvider(t.body)}),n.get("/providers",async()=>n.providerService.getProviders()),n.get("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async t=>{let o=n.providerService.getProvider(t.params.id);if(!o)throw J("Provider not found",404,"provider_not_found");return o}),n.put("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{name:{type:"string"},type:{type:"string",enum:["openai","anthropic"]},baseUrl:{type:"string"},apiKey:{type:"string"},models:{type:"array",items:{type:"string"}},proxyEnabled:{type:"boolean"},enabled:{type:"boolean"}}}}},async(t,o)=>{let r=n.providerService.updateProvider(t.params.id,t.body);if(!r)throw J("Provider not found",404,"provider_not_found");return r}),n.delete("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async t=>{if(!n.providerService.deleteProvider(t.params.id))throw J("Provider not found",404,"provider_not_found");return{message:"Provider deleted successfully"}}),n.patch("/providers/:id/toggle",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{enabled:{type:"boolean"}},required:["enabled"]}}},async(t,o)=>{if(!n.providerService.toggleProvider(t.params.id,t.body.enabled))throw J("Provider not found",404,"provider_not_found");return{message:`Provider ${t.body.enabled?"enabled":"disabled"} successfully`}})}});var ue,Kt=w(()=>{"use strict";ue=class{constructor(e,t,o){this.configService=e;this.transformerService=t;this.logger=o;this.initializeCustomProviders()}providers=new Map;modelRoutes=new Map;initializeCustomProviders(){let e=this.configService.get("providers");if(e&&Array.isArray(e)){this.initializeFromProvidersArray(e);return}}initializeFromProvidersArray(e){e.forEach(t=>{try{if(!t.name||!t.api_base_url||!t.api_key)return;let o={};t.transformer&&Object.keys(t.transformer).forEach(s=>{s==="use"?Array.isArray(t.transformer.use)&&(o.use=t.transformer.use.map(i=>{if(Array.isArray(i)&&typeof i[0]=="string"){let 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 He,Qt,Bs,El,Js,lt=w(()=>{"use strict";He=n=>n>0?n<=1024?"low":n<=8192?"medium":n<=16384?"high":"max":"none",Qt=n=>n==="none"?0:n==="low"?1024:n==="medium"?8192:n==="high"?16384:32768,Bs=["low","medium","high"],El={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"},Js=n=>{if(typeof n=="string")return El[n.trim().toLowerCase()]}});var Ws,Gs=w(()=>{Ws=(n,e)=>(n.includes("base64")&&(n=n.split("base64").pop(),n.startsWith(",")&&(n=n.slice(1))),`data:${e};base64,${n}`)});function Pl(n){return n.filter(e=>!!e?.function?.name).map(e=>({name:e.function.name,description:e.function.description,input_schema:e.function.parameters}))}function Ks(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||Qt(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:He(p);Bs.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
|
+
`).filter(c=>c.startsWith("data:")&&c.trim().length>5).length===0){o.releaseLock();let c=i.length<500?i:i.slice(0,500);throw J(`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 Kl(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=U(),u=n.provider||"",d=n.body.model||"",p=new Set,m=r?.statusCode===429||r?.isRateLimit===!0||r?.rawBody&&Wt(r.rawBody)||r?.message&&Wt(r.message);if(u&&d&&(m?(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.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 g=k=>{let[y,...b]=k.split(","),T=y?.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 y=n.strictProjectRouting;n.log.warn(`No ${a} fallback configured for ${s}; ${y?"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,y)=>k+y.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 y of k.models){let b=g(y);if(!b){n.log.warn(`Fallback model '${y}' is invalid, skipping`);continue}if(p.has(b.key))continue;p.add(b.key);let T=t.providerService.getProviders(),C=st(T,b.provider,b.model);if(!C){n.log.warn(`Fallback model ${y} is not configured or provider is disabled, skipping`);continue}let v=C.provider.name,S=C.model;if(!c.isAvailable(v,S)){n.log.warn(`Fallback model ${y} unavailable (fail pool), skipping`);continue}let x;for(let w=1;w<=2;w++)try{n.log.info(`Trying fallback model: ${v},${S}${w>1?" (retry)":""}`);let R={...n.body};R.model=S;let O={...n,provider:v,body:R},P=C.provider,{requestBody:M,config:$,bypass:L}=await Ys(R,P,o,n.headers,{req:O}),H=await Xs(M,$,P,t,L,o,{req:O});try{P?.baseUrl&&H?.headers&&qe(v,P.baseUrl,H.headers)}catch{}let N=await Zs(M,H,P,o,L,{req:O,isCodexOnMessagesEndpoint:n.isCodexOnMessagesEndpoint||!1});return N=await Qs(R,N,v),n.log.info(`Fallback model ${v},${S} succeeded`),c.recordSuccess(v,S),u&&d&&!n.strictProjectRouting&&(Ee().promote(u,d,s,v,S),n.log.info(`Promoted fallback model ${v},${S} for ${u},${d}:${s}`),m?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=v,n.body=R,_s(n,`${v},${S}`),qs(N,e,R,t)}catch(R){if(x=R,w===1){n.log.warn(`Fallback model ${v},${S} failed, retrying once: ${R.message}`);continue}}c.recordFailure(v,S,x?.message),n.log.warn(`Fallback model ${v},${S} failed after retry: ${x?.message}`),t.recordUsage?.({provider:v,model:S,originalModel:n.originalModel||d||n.body.model,scenarioType:s,modelFamily:n.modelFamily,errorMessage:x?.message||String(x),sessionId:n.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 Ys(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=Gl(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 Gl(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 Vl(n,e){if(e?.clientType!=="pi")return;let t=e?.clientContext?.stableSessionId;if(!(typeof t!="string"||!t)){for(let o of Object.keys(n))o.toLowerCase()==="x-litellm-session-id"&&delete n[o];n["x-litellm-session-id"]=t}}function Ql(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 Xs(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||{}};Vl(l,i?.req);for(let u in l)(l[u]==="undefined"||["authorization","Authorization"].includes(u)&&l[u]?.includes("undefined"))&&delete l[u];Pt(o.configService)&&es(o.log,i.req.id,{url:String(a),headers:l,body:n});let c=await os(a,n,{...e,headers:JSON.parse(JSON.stringify(l)),httpsProxy:ce(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=J(`Error from provider(${t.name},${n.model}: ${c.status}): ${u}`,c.status,"provider_response_error");throw d.isRateLimit=c.status===429||Wt(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(),m="";try{if(m=await p.text(),!m||m.trim()==="")throw new SyntaxError("Empty JSON body");if(tt(m))return o.log.info(`[hidden-error-check] body is SSE mislabeled as ${u}, skipping JSON validation`),c;let h=JSON.parse(m);if(h&&typeof h=="object"&&h.error&&(typeof h.error=="string"||typeof h.error=="object"&&Object.keys(h.error).length>0)){o.log.error(`[provider_response_error] Hidden error from provider(${t.name},${n.model}: ${c.status}): ${m}`);let f=Wt(m),_=J(`Error from provider(${t.name},${n.model}: ${c.status}): ${m}`,400,"provider_response_error");throw _.isRateLimit=f,_.rawBody=m,_}}catch(h){if(h instanceof SyntaxError){o.log.warn(`[provider_response_error] Empty or malformed JSON response from provider(${t.name},${n.model}: 200): "${m}"`);let g=J(`API returned an empty or malformed response (HTTP 200) from ${t.name} \u2014 check for a proxy or gateway intercepting the request`,400,"provider_response_error");throw g.rawBody=m,g}throw h}}}if(!n.stream&&Pt(o.configService)){let d=await c.clone().text();ts(o.log,i.req.id,{status:c.status,headers:Object.fromEntries(c.headers.entries()),body:d})}return c}async function Zs(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 Ae().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=bs(n);if(!r){let s=await pe(n);if(s)s.isSSE&&(r=!0),n=new Response(s.body,{status:n.status,statusText:n.statusText,headers:n.headers});else{let i=await fe(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&&Pt(o.configService)){let[s,i]=n.body.tee(),a=qr(o.configService);return ns(s,o.log,e.request.id,a),e.send(i)}return e.send(n.body)}else return F(n)}function Wt(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 Yl(n){try{return new URL(n),!0}catch{return!1}}var Wl,ei,Kt=E(()=>{"use strict";rs();wt();Ze();ao();re();Oe();Mt();Dt();Co();V();je();oe();Wl=/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i;ei=async n=>{n.get("/",async()=>({message:"LLMs API",version:It})),n.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),n.get("/providers/health",async()=>({states:U().getAllStates().map(r=>({provider:r.provider,model:r.model,status:r.status,failureCount:r.failureCount,successCount:r.successCount,lastFailureTime:r.lastFailureTime,lastError:r.lastError,rateLimitUntil:r.rateLimitUntil??null})),timestamp:new Date().toISOString()}));let e=n.transformerService.getTransformersWithEndpoint();for(let{transformer:t}of e)t.endPoint&&n.post(t.endPoint,async(o,r)=>Vs(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 J("Provider name is required",400,"invalid_request");if(!s||!Yl(s))throw J("Valid base URL is required",400,"invalid_request");if(!i?.trim())throw J("API key is required",400,"invalid_request");if(!a||!Array.isArray(a)||a.length===0)throw J("At least one model is required",400,"invalid_request");if(n.providerService.getProvider(t.body.name))throw J(`Provider with name '${t.body.name}' already exists`,400,"provider_exists");return n.providerService.registerProvider(t.body)}),n.get("/providers",async()=>n.providerService.getProviders()),n.get("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async t=>{let o=n.providerService.getProvider(t.params.id);if(!o)throw J("Provider not found",404,"provider_not_found");return o}),n.put("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{name:{type:"string"},type:{type:"string",enum:["openai","anthropic"]},baseUrl:{type:"string"},apiKey:{type:"string"},models:{type:"array",items:{type:"string"}},proxyEnabled:{type:"boolean"},enabled:{type:"boolean"}}}}},async(t,o)=>{let r=n.providerService.updateProvider(t.params.id,t.body);if(!r)throw J("Provider not found",404,"provider_not_found");return r}),n.delete("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async t=>{if(!n.providerService.deleteProvider(t.params.id))throw J("Provider not found",404,"provider_not_found");return{message:"Provider deleted successfully"}}),n.patch("/providers/:id/toggle",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{enabled:{type:"boolean"}},required:["enabled"]}}},async(t,o)=>{if(!n.providerService.toggleProvider(t.params.id,t.body.enabled))throw J("Provider not found",404,"provider_not_found");return{message:`Provider ${t.body.enabled?"enabled":"disabled"} successfully`}})}});var ue,Gt=E(()=>{"use strict";ue=class{constructor(e,t,o){this.configService=e;this.transformerService=t;this.logger=o;this.initializeCustomProviders()}providers=new Map;modelRoutes=new Map;initializeCustomProviders(){let e=this.configService.get("providers");if(e&&Array.isArray(e)){this.initializeFromProvidersArray(e);return}}initializeFromProvidersArray(e){e.forEach(t=>{try{if(!t.name||!t.api_base_url||!t.api_key)return;let o={};t.transformer&&Object.keys(t.transformer).forEach(s=>{s==="use"?Array.isArray(t.transformer.use)&&(o.use=t.transformer.use.map(i=>{if(Array.isArray(i)&&typeof i[0]=="string"){let 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 he,Vt,ti,Xl,ni,He=E(()=>{"use strict";he=n=>n>0?n<=1024?"low":n<=8192?"medium":n<=16384?"high":"max":"none",Vt=n=>n==="none"?0:n==="low"?1024:n==="medium"?8192:n==="high"?16384:32768,ti=["low","medium","high"],Xl={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"},ni=n=>{if(typeof n=="string")return Xl[n.trim().toLowerCase()]}});var oi,ri=E(()=>{oi=(n,e)=>(n.includes("base64")&&(n=n.split("base64").pop(),n.startsWith(",")&&(n=n.slice(1))),`data:${e};base64,${n}`)});function Zl(n){return n.filter(e=>!!e?.function?.name).map(e=>({name:e.function.name,description:e.function.description,input_schema:e.function.parameters}))}function si(n){let e=[],t=[],o=p=>{if(typeof p=="string"){t.push({type:"text",text:p});return}Array.isArray(p)&&p.forEach(m=>{if(typeof m=="string"){t.push({type:"text",text:m});return}m&&typeof m=="object"&&m.type==="text"&&m.text&&t.push({...m})})},r=()=>{let p=new Map;for(let m=0;m<t.length;m++){let h=t[m],g=h?.text??JSON.stringify(h),f=p.get(g);if(f===void 0){p.set(g,m);continue}!t[f]?.cache_control&&h?.cache_control&&(t[f]=h),t.splice(m,1),m--}};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 m=e[p];if(m.role==="tool"){let h=m.content;try{typeof m.content=="string"&&(h=JSON.parse(m.content))}catch{}h!=null&&typeof h!="object"&&typeof h!="string"&&(h=String(h));let g={type:"tool_result",tool_use_id:m.tool_call_id,content:h};m.cache_control&&(g.cache_control=m.cache_control);let f=s[s.length-1];f&&f.role==="user"&&Array.isArray(f.content)?f.content.push(g):s.push({role:"user",content:[g]})}else if(m.role==="user"){let h=[];typeof m.content=="string"?h.push({type:"text",text:m.content}):Array.isArray(m.content)&&m.content.forEach(f=>{f&&typeof f=="object"&&f.type==="image_url"&&f.image_url?.url?h.push({type:"image",source:{type:"base64",media_type:f.media_type||"image/jpeg",data:f.image_url.url.split(",")[1]||f.image_url.url}}):h.push(f)});let g=s[s.length-1];g&&g.role==="user"&&Array.isArray(g.content)?g.content.push(...h):s.push({role:"user",content:h.length>0?h:[{type:"text",text:" "}]})}else if(m.role==="assistant"){let h=[];m.content&&(typeof m.content=="string"?h.push({type:"text",text:m.content}):Array.isArray(m.content)&&h.push(...m.content)),m.tool_calls&&m.tool_calls.length>0&&m.tool_calls.forEach(f=>{let _={};try{_=JSON.parse(f.function.arguments)}catch{_=f.function.arguments}h.push({type:"tool_use",id:f.id,name:f.function.name,input:_})}),m.thinking&&h.unshift({type:"thinking",thinking:m.thinking.content,signature:m.thinking.signature});let g=s[s.length-1];g&&g.role==="assistant"&&Array.isArray(g.content)?g.content.push(...h):s.push({role:"assistant",content:h.length>0?h:[{type:"text",text:" "}]})}}let i=()=>{let p=t.filter(m=>m?.cache_control).length;for(let m of s)Array.isArray(m.content)&&(p+=m.content.filter(h=>h?.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 m=s[p]?.content;if(!Array.isArray(m))continue;let h=m.find(g=>g?.cache_control);if(h?.cache_control)return h.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,m)=>{if(!Array.isArray(p?.content)||p.content.some(g=>g?.cache_control))return!1;let h=p.content.find(g=>g&&typeof g=="object"&&!g.cache_control);return h?(h.cache_control=a(m),!0):!1};(()=>{let p=4-i();if(p<=0||s.length===0)return;let m=s,h=l(),g=[];for(let f=m.length-1;f>=0;f--)m[f]?.role!=="user"||!Array.isArray(m[f].content)||g.push(f);for(let f of g){if(p<=0)break;c(m[f],h)&&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||Vt(n.reasoning.effort||"medium"),m=Math.min(p,d.max_tokens-1024);m>=1024&&(d.thinking={type:"enabled",budget_tokens:m});let h=n.reasoning.effort&&n.reasoning.effort!=="none"?n.reasoning.effort:he(p);ti.includes(h)&&(d.output_config={...d.output_config||{},effort:h})}if(t.length>0){let p=t.some(m=>m?.cache_control);d.system=p?t:t.map(m=>m.text||"").join(`
|
|
47
47
|
|
|
48
|
-
`)}return n.tools&&n.tools.length>0&&(d.tools=
|
|
49
|
-
`));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=
|
|
50
|
-
data: ${JSON.stringify(
|
|
48
|
+
`)}return n.tools&&n.tools.length>0&&(d.tools=Zl(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 ii=E(()=>{He()});import{v4 as $o}from"uuid";function ql(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 Qt,ai=E(()=>{"use strict";He();wt();V();ri();ii();Qt=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"?oi(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(`
|
|
49
|
+
`));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=ni(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=ql(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=he(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=si(e),s={};return this.useBearer?(s.authorization=`Bearer ${t.apiKey}`,s["x-api-key"]=void 0):(s["x-api-key"]=t.apiKey,s.authorization=void 0),s["anthropic-version"]="2023-06-01",s["content-type"]="application/json",{body:r,config:{headers:s}}}isAnthropicMessagesEndpoint(e){try{return new URL(e.trim()).pathname.replace(/\/+$/,"").endsWith("/messages")}catch{return!1}}async transformResponseIn(e,t){if(t?.req?.isTargetAnthropic)return e;if(e.headers.get("Content-Type")?.includes("text/event-stream")){if(!e.body)throw new Error("Stream response body is null");let r=await this.convertOpenAIStreamToAnthropic(e.body,t);return new Response(r,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}else{let r=await pe(e);if((r?.isSSE??!1)&&r?.body){let 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 fe(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 F(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,m=new Map,h=0,g=0,f=0,_=!1,k=!1,y=0,b=-1,T=()=>{let x=y;return y++,x},C=x=>{if(!_)try{r.enqueue(x);let w=new TextDecoder().decode(x);this.logger.debug({reqId:t.req.id,data:w,type:"send data"})}catch(w){if(w instanceof TypeError&&w.message.includes("Controller is already closed"))_=!0;else throw this.logger.debug({reqId:t.req.id,error:w instanceof Error?w.message:String(w),type:"send data error"}),w}},v=()=>{if(!_)try{if(!c){this.logger?.warn("[empty_stream] Upstream produced no data, closing silently"),r.close(),_=!0;return}if(b>=0){let w={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
50
|
+
data: ${JSON.stringify(w)}
|
|
51
51
|
|
|
52
52
|
`)),b=-1}if(a)C(s.encode(`event: message_delta
|
|
53
53
|
data: ${JSON.stringify(a)}
|
|
54
54
|
|
|
55
|
-
`)),a=null;else{let
|
|
56
|
-
data: ${JSON.stringify({type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{input_tokens:
|
|
55
|
+
`)),a=null;else{let w=t?.req?.tokenCount||0;C(s.encode(`event: message_delta
|
|
56
|
+
data: ${JSON.stringify({type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{input_tokens:w,output_tokens:0,cache_read_input_tokens:0}})}
|
|
57
57
|
|
|
58
|
-
`))}let
|
|
59
|
-
data: ${JSON.stringify(
|
|
58
|
+
`))}let x={type:"message_stop"};C(s.encode(`event: message_stop
|
|
59
|
+
data: ${JSON.stringify(x)}
|
|
60
60
|
|
|
61
|
-
`)),r.close(),_=!0}catch(
|
|
62
|
-
`);
|
|
63
|
-
data: ${JSON.stringify(
|
|
61
|
+
`)),r.close(),_=!0}catch(x){if(x instanceof TypeError&&x.message.includes("Controller is already closed"))_=!0;else throw x}},S=null;try{S=e.getReader();let x=new TextDecoder,w="";for(;!_;){let{done:R,value:O}=await S.read();if(R)break;w+=x.decode(O,{stream:!0});let P=w.split(`
|
|
62
|
+
`);w=P.pop()||"";for(let M of P){if(_)break;if(!M.startsWith("data:"))continue;let $=M.slice(5).trim();if(this.logger.debug({reqId:t.req.id,type:"recieved data",data:$}),$!=="[DONE]")try{let L=JSON.parse($);if(h++,this.logger.debug({reqId:t.req.id,response:L,tppe:"Original Response"}),L.error){t?.req&&(t.req.sseError=L.error);let N={type:"error",message:{type:"api_error",message:JSON.stringify(L.error)}};C(s.encode(`event: error
|
|
63
|
+
data: ${JSON.stringify(N)}
|
|
64
64
|
|
|
65
|
-
`));continue}if(l=
|
|
66
|
-
data: ${JSON.stringify(
|
|
65
|
+
`));continue}if(l=L.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
|
|
66
|
+
data: ${JSON.stringify(D)}
|
|
67
67
|
|
|
68
|
-
`))}let H=
|
|
69
|
-
data: ${JSON.stringify(
|
|
68
|
+
`))}let H=L.choices?.[0];if(L.usage){let N=L.usage?.prompt_tokens_details?.cached_tokens??L.usage?.prompt_cache_hit_tokens??0,W=(L.usage?.prompt_tokens||0)-N,Te=L.usage?.completion_tokens||0;this.logger?.info({debug_log:!0,reqId:t.req.id,phase:"anthropic_transformer_usage",providerUsage:L.usage,mappedUsage:{input_tokens:W,output_tokens:Te,cache_read_input_tokens:N},choicesLength:Array.isArray(L.choices)?L.choices.length:void 0,finishReason:H?.finish_reason});let j={input_tokens:W,output_tokens:Te,cache_read_input_tokens:N};if(!a)a={type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{...j}};else{let Y=a.usage||{};a.usage={input_tokens:j.input_tokens||Y.input_tokens||0,output_tokens:j.output_tokens||Y.output_tokens||0,cache_read_input_tokens:j.cache_read_input_tokens||Y.cache_read_input_tokens||0}}}if(!H)continue;if(H?.delta?.thinking&&!_&&!d&&!u){if(!k){let N=T(),D={type:"content_block_start",index:N,content_block:{type:"thinking",thinking:""}};C(s.encode(`event: content_block_start
|
|
69
|
+
data: ${JSON.stringify(D)}
|
|
70
70
|
|
|
71
|
-
`)),b
|
|
72
|
-
data: ${JSON.stringify(
|
|
71
|
+
`)),b=N,k=!0}if(H.delta.thinking.signature){let N={type:"content_block_delta",index:b,delta:{type:"signature_delta",signature:H.delta.thinking.signature}};C(s.encode(`event: content_block_delta
|
|
72
|
+
data: ${JSON.stringify(N)}
|
|
73
73
|
|
|
74
|
-
`));let
|
|
75
|
-
data: ${JSON.stringify(
|
|
74
|
+
`));let D={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
75
|
+
data: ${JSON.stringify(D)}
|
|
76
76
|
|
|
77
|
-
`)),b=-1}else if(H.delta.thinking.content){let
|
|
78
|
-
data: ${JSON.stringify(
|
|
77
|
+
`)),b=-1}else if(H.delta.thinking.content){let N={type:"content_block_delta",index:b,delta:{type:"thinking_delta",thinking:H.delta.thinking.content||""}};C(s.encode(`event: content_block_delta
|
|
78
|
+
data: ${JSON.stringify(N)}
|
|
79
79
|
|
|
80
|
-
`))}}if(H?.delta?.content&&!_&&!d){if(
|
|
81
|
-
data: ${JSON.stringify(
|
|
80
|
+
`))}}if(H?.delta?.content&&!_&&!d){if(g++,b>=0&&!u){let D={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
81
|
+
data: ${JSON.stringify(D)}
|
|
82
82
|
|
|
83
|
-
`)),b=-1}if(!u&&!d){u=!0;let
|
|
84
|
-
data: ${JSON.stringify(
|
|
83
|
+
`)),b=-1}if(!u&&!d){u=!0;let N=T(),D={type:"content_block_start",index:N,content_block:{type:"text",text:""}};C(s.encode(`event: content_block_start
|
|
84
|
+
data: ${JSON.stringify(D)}
|
|
85
85
|
|
|
86
|
-
`)),b
|
|
87
|
-
data: ${JSON.stringify(
|
|
86
|
+
`)),b=N}if(!_&&!d){let N={type:"content_block_delta",index:b,delta:{type:"text_delta",text:H.delta.content}};C(s.encode(`event: content_block_delta
|
|
87
|
+
data: ${JSON.stringify(N)}
|
|
88
88
|
|
|
89
|
-
`))}}if(H?.delta?.annotations?.length&&!_&&!d){if(b>=0&&u){let
|
|
90
|
-
data: ${JSON.stringify(
|
|
89
|
+
`))}}if(H?.delta?.annotations?.length&&!_&&!d){if(b>=0&&u){let N={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
90
|
+
data: ${JSON.stringify(N)}
|
|
91
91
|
|
|
92
|
-
`)),b=-1,u=!1}H?.delta?.annotations.forEach(
|
|
92
|
+
`)),b=-1,u=!1}H?.delta?.annotations.forEach(N=>{let D=T(),W={type:"content_block_start",index:D,content_block:{type:"web_search_tool_result",tool_use_id:`srvtoolu_${$o()}`,content:[{type:"web_search_result",title:N.url_citation.title,url:N.url_citation.url}]}};C(s.encode(`event: content_block_start
|
|
93
93
|
data: ${JSON.stringify(W)}
|
|
94
94
|
|
|
95
|
-
`));let
|
|
96
|
-
data: ${JSON.stringify(
|
|
95
|
+
`));let Te={type:"content_block_stop",index:D};C(s.encode(`event: content_block_stop
|
|
96
|
+
data: ${JSON.stringify(Te)}
|
|
97
97
|
|
|
98
|
-
`)),b=-1})}if(H?.delta?.tool_calls&&!_&&!d){f++;let
|
|
99
|
-
data: ${JSON.stringify(
|
|
98
|
+
`)),b=-1})}if(H?.delta?.tool_calls&&!_&&!d){f++;let N=new Set;for(let D of H.delta.tool_calls){if(_)break;let W=D.index??0;if(N.has(W))continue;if(N.add(W),!m.has(W)){if(b>=0){let tc={type:"content_block_stop",index:b};C(s.encode(`event: content_block_stop
|
|
99
|
+
data: ${JSON.stringify(tc)}
|
|
100
100
|
|
|
101
|
-
`)),b=-1}let j=
|
|
102
|
-
data: ${JSON.stringify(
|
|
101
|
+
`)),b=-1}let j=T();m.set(W,j);let Y=D.id||`call_ccr_${W}`,z=D.function?.name||`tool_${W}`,A={type:"content_block_start",index:j,content_block:{type:"tool_use",id:Y,name:z,input:{}}};C(s.encode(`event: content_block_start
|
|
102
|
+
data: ${JSON.stringify(A)}
|
|
103
103
|
|
|
104
|
-
`)),b=j;let B={id:Y,name:z,arguments:"",contentBlockIndex:j};p.set(W,B)}else if(
|
|
104
|
+
`)),b=j;let B={id:Y,name:z,arguments:"",contentBlockIndex:j};p.set(W,B)}else if(D.id&&D.function?.name){let j=p.get(W);j.id.startsWith("call_")&&j.name.startsWith("tool_")&&(j.id=D.id,j.name=D.function.name)}if(D.function?.arguments&&!_&&!d){let j=m.get(W);if(j===void 0)continue;let Y=p.get(W);Y&&(Y.arguments+=D.function.arguments);try{let z={type:"content_block_delta",index:j,delta:{type:"input_json_delta",partial_json:D.function.arguments}};C(s.encode(`event: content_block_delta
|
|
105
105
|
data: ${JSON.stringify(z)}
|
|
106
106
|
|
|
107
|
-
`))}catch{try{let z=
|
|
108
|
-
data: ${JSON.stringify(
|
|
107
|
+
`))}catch{try{let z=D.function.arguments.replace(/[\x00-\x1F\x7F-\x9F]/g,"").replace(/\\/g,"\\\\").replace(/"/g,'\\"'),A={type:"content_block_delta",index:j,delta:{type:"input_json_delta",partial_json:z}};C(s.encode(`event: content_block_delta
|
|
108
|
+
data: ${JSON.stringify(A)}
|
|
109
109
|
|
|
110
|
-
`))}catch(z){console.error(z)}}}}}if(H?.finish_reason&&!_&&!d){if(
|
|
111
|
-
data: ${JSON.stringify(
|
|
110
|
+
`))}catch(z){console.error(z)}}}}}if(H?.finish_reason&&!_&&!d){if(g===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
|
|
111
|
+
data: ${JSON.stringify(N)}
|
|
112
112
|
|
|
113
|
-
`)),b=-1}if(!_){let
|
|
114
|
-
`)||"",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
|
|
113
|
+
`)),b=-1}if(!_){let D={stop:"end_turn",length:"max_tokens",tool_calls:"tool_use",content_filter:"stop_sequence"}[H.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(L){this.logger?.error(`parseError: ${L.name} message: ${L.message} stack: ${L.stack} data: ${$}`)}}}v()}catch(x){if(!_)try{r.error(x)}catch(w){console.error(w)}}finally{if(S)try{S.releaseLock()}catch(x){console.error(x)}}},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_${$o()}`;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_${$o().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 J(`Provider error: ${JSON.stringify(e)}`,500,"provider_error")}}}});function eu(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(ze).includes(o)?o:ze.TYPE_UNSPECIFIED}else{e.anyOf=[];for(let o of t){let r=o.toUpperCase();e.anyOf.push({type:Object.values(ze).includes(r)?r:ze.TYPE_UNSPECIFIED})}}}function ct(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)&&eu(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(ze).includes(l)?l:ze.TYPE_UNSPECIFIED}else if(t.includes(i))e[i]=ct(a);else if(o.includes(i)){let l=[];for(let c of a){if(c.type=="null"){e.nullable=!0;continue}l.push(ct(c))}e[i]=l}else if(r.includes(i)){let l={};for(let[c,u]of Object.entries(a))l[c]=ct(u);e[i]=l}else{if(i==="additionalProperties")continue;e[i]=a}return e}function tu(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=ct(e.parameters)),e.response&&(Object.keys(e.response).includes("$schema")?e.responseJsonSchema||(e.responseJsonSchema=e.response,delete e.response):e.response=ct(e.response));return n}function Yt(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(tu({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 m=s.find(h=>h.tool_call_id===p.id);return{functionResponse:{name:p?.function?.name,response:{result:m?.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 Xt(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 Zt(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(`
|
|
114
|
+
`)||"",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 m=async(f,_)=>{if(f.startsWith("data: ")){let k=f.slice(6).trim();if(k){t?.debug({chunkStr:k},`${e} chunk:`);try{let y=JSON.parse(k);if(!y.candidates||!y.candidates[0]){t?.debug({chunkStr:k},"Invalid chunk structure");return}let b=y.candidates[0],T=b.content?.parts||[];T.filter(x=>x.text&&x.thought===!0).forEach(x=>{a||(a=!0);let w={choices:[{delta:{role:"assistant",content:null,thinking:{content:x.text}},finish_reason:null,index:c,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:y.responseId||"",model:y.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};_.enqueue(r.encode(`data: ${JSON.stringify(w)}
|
|
115
115
|
|
|
116
|
-
`))});let C=
|
|
116
|
+
`))});let C=T.find(x=>x.thoughtSignature)?.thoughtSignature;if(C&&!s){if(!a){let w={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:y.responseId||"",model:y.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};_.enqueue(r.encode(`data: ${JSON.stringify(w)}
|
|
117
117
|
|
|
118
|
-
`))}let
|
|
118
|
+
`))}let x={choices:[{delta:{role:"assistant",content:null,thinking:{signature:C}},finish_reason:null,index:c,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:y.responseId||"",model:y.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};if(_.enqueue(r.encode(`data: ${JSON.stringify(x)}
|
|
119
119
|
|
|
120
|
-
`)),s=!0,c++,l){let
|
|
120
|
+
`)),s=!0,c++,l){let w={choices:[{delta:{role:"assistant",content:l},finish_reason:null,index:c,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:y.responseId||"",model:y.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};_.enqueue(r.encode(`data: ${JSON.stringify(w)}
|
|
121
121
|
|
|
122
|
-
`)),l="",i||(i=!0)}}let
|
|
123
|
-
`);if(!S&&s&&!i){let
|
|
122
|
+
`)),l="",i||(i=!0)}}let v=T.filter(x=>x.functionCall).map(x=>({id:x.functionCall?.id||`ccr_tool_${Math.random().toString(36).substring(2,15)}`,type:"function",function:{name:x.functionCall?.name,arguments:JSON.stringify(x.functionCall?.args||{})}})),S=T.filter(x=>x.text&&x.thought!==!0).map(x=>x.text).join(`
|
|
123
|
+
`);if(!S&&s&&!i){let x={choices:[{delta:{role:"assistant",content:"(no content)"},index:c,finish_reason:null,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:y.responseId||"",model:y.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};_.enqueue(r.encode(`data: ${JSON.stringify(x)}
|
|
124
124
|
|
|
125
|
-
`)),i||(i=!0)}if(a&&S&&!s)if(
|
|
125
|
+
`)),i||(i=!0)}if(a&&S&&!s)if(y.modelVersion.includes("3")){l+=S;return}else{let x={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:y.responseId||"",model:y.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1"};_.enqueue(r.encode(`data: ${JSON.stringify(x)}
|
|
126
126
|
|
|
127
|
-
`)),s=!0}if(S){l||c++;let
|
|
127
|
+
`)),s=!0}if(S){l||c++;let x={choices:[{delta:{role:"assistant",content:S},finish_reason:b.finishReason?.toLowerCase()||null,index:c,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:y.responseId||"",model:y.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:y.usageMetadata?.candidatesTokenCount||0,prompt_tokens:y.usageMetadata?.promptTokenCount||0,prompt_tokens_details:{cached_tokens:y.usageMetadata?.cachedContentTokenCount||0},total_tokens:y.usageMetadata?.totalTokenCount||0,output_tokens_details:{reasoning_tokens:y.usageMetadata?.thoughtsTokenCount||0}}};b?.groundingMetadata?.groundingChunks?.length&&(x.choices[0].delta.annotations=b.groundingMetadata.groundingChunks.map((w,R)=>{let O=b?.groundingMetadata?.groundingSupports?.filter(P=>P.groundingChunkIndices?.includes(R));return{type:"url_citation",url_citation:{url:w?.web?.uri||"",title:w?.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(x)}
|
|
128
128
|
|
|
129
|
-
`)),!i&&S&&(i=!0)}
|
|
129
|
+
`)),!i&&S&&(i=!0)}v.length>0&&(v.forEach(x=>{c++,u++;let w={choices:[{delta:{role:"assistant",tool_calls:[{...x,index:u}]},finish_reason:b.finishReason?.toLowerCase()||null,index:c,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:y.responseId||"",model:y.modelVersion||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:y.usageMetadata?.candidatesTokenCount||0,prompt_tokens:y.usageMetadata?.promptTokenCount||0,prompt_tokens_details:{cached_tokens:y.usageMetadata?.cachedContentTokenCount||0},total_tokens:y.usageMetadata?.totalTokenCount||0,output_tokens_details:{reasoning_tokens:y.usageMetadata?.thoughtsTokenCount||0}}};b?.groundingMetadata?.groundingChunks?.length&&(w.choices[0].delta.annotations=b.groundingMetadata.groundingChunks.map((R,O)=>{let P=b?.groundingMetadata?.groundingSupports?.filter(M=>M.groundingChunkIndices?.includes(O));return{type:"url_citation",url_citation:{url:R?.web?.uri||"",title:R?.web?.title||"",content:P?.[0]?.segment?.text||"",start_index:P?.[0]?.segment?.startIndex||0,end_index:P?.[0]?.segment?.endIndex||0}}})),_.enqueue(r.encode(`data: ${JSON.stringify(w)}
|
|
130
130
|
|
|
131
|
-
`))}),!i&&S&&(i=!0))}catch(
|
|
132
|
-
`);
|
|
131
|
+
`))}),!i&&S&&(i=!0))}catch(y){t?.error(`Error parsing ${e} stream chunk`,k,y.message)}}}},h=n.body.getReader(),g="";try{for(;;){let{done:f,value:_}=await h.read();if(f){g&&await m(g,p);break}g+=o.decode(_,{stream:!0});let k=g.split(`
|
|
132
|
+
`);g=k.pop()||"";for(let y of k)await m(y,p)}}catch(f){p.error(f)}finally{p.close()}}});return new Response(d,{status:n.status,statusText:n.statusText,headers:n.headers})}}var ze,Lo=E(()=>{"use strict";ze={TYPE_UNSPECIFIED:"TYPE_UNSPECIFIED",STRING:"STRING",NUMBER:"NUMBER",INTEGER:"INTEGER",BOOLEAN:"BOOLEAN",ARRAY:"ARRAY",OBJECT:"OBJECT",NULL:"NULL"}});var qt,ci=E(()=>{"use strict";Lo();qt=class{name="gemini";endPoint="/v1beta/models/:modelAndAction";async transformRequestIn(e,t){return{body:Yt(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=Xt;async transformResponseOut(e){return Zt(e,this.name,this.logger)}}});async function nu(){try{let{GoogleAuth:n}=await import("google-auth-library");return(await(await new n({scopes:["https://www.googleapis.com/auth/cloud-platform"]}).getClient()).getAccessToken()).token||""}catch(n){throw console.error("Error getting access token:",n),new Error(`Failed to get access token for Vertex AI. Please ensure you have set up authentication using one of these methods:
|
|
133
133
|
1. Set GOOGLE_APPLICATION_CREDENTIALS to point to service account key file
|
|
134
134
|
2. Run "gcloud auth application-default login"
|
|
135
|
-
3. Use Google Cloud environment with default service account`)}}var en,
|
|
136
|
-
`);for(let f of
|
|
137
|
-
`))},d=(p,
|
|
135
|
+
3. Use Google Cloud environment with default service account`)}}var en,li=E(()=>{"use strict";Lo();en=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 nu();return{body:Yt(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 Xt(e)}async transformResponseOut(e){return Zt(e,this.name)}}});var tn,ui=E(()=>{"use strict";V();tn=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 F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r="",s=!1,i="",a=new ReadableStream({async start(l){let c=e.body.getReader(),u=(p,m,h)=>{let g=p.split(`
|
|
136
|
+
`);for(let f of g)f.trim()&&m.enqueue(h.encode(f+`
|
|
137
|
+
`))},d=(p,m)=>{let{controller:h,encoder:g}=m;if(p.startsWith("data: ")&&p.trim()!=="data: [DONE]")try{let f=JSON.parse(p.slice(6));if(f.choices?.[0]?.delta?.reasoning_content){m.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(_)}
|
|
138
138
|
|
|
139
|
-
`;
|
|
139
|
+
`;h.enqueue(g.encode(k));return}if(f.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let _="ccr_think_signature",k=f.choices[0].delta.content,y={...f,choices:[{...f.choices[0],delta:{...f.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:_}}}]};delete y.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(y)}
|
|
140
140
|
|
|
141
|
-
`;
|
|
141
|
+
`;h.enqueue(g.encode(b));let T={...f,choices:[{...f.choices[0],delta:{...f.choices[0].delta,content:k}}]};delete T.choices[0].delta.reasoning_content;let C=`data: ${JSON.stringify(T)}
|
|
142
142
|
|
|
143
|
-
`;
|
|
143
|
+
`;h.enqueue(g.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)}
|
|
144
144
|
|
|
145
|
-
`;
|
|
146
|
-
`))}else
|
|
147
|
-
`))};try{for(;;){let{done:p,value:
|
|
148
|
-
`);i=
|
|
149
|
-
`))}}}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 nn,
|
|
145
|
+
`;h.enqueue(g.encode(_))}}catch{h.enqueue(g.encode(p+`
|
|
146
|
+
`))}else h.enqueue(g.encode(p+`
|
|
147
|
+
`))};try{for(;;){let{done:p,value:m}=await c.read();if(p){i.trim()&&u(i,l,o);break}let h=t.decode(m,{stream:!0});i+=h;let g=i.split(`
|
|
148
|
+
`);i=g.pop()||"";for(let f of g)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+`
|
|
149
|
+
`))}}}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 nn,di=E(()=>{"use strict";V();nn=class{name="tooluse";transformRequestIn(e){return e.messages.push({role:"system",content:"<system-reminder>Tool mode is active. The user expects you to proactively execute the most suitable tool to help complete the task. \nBefore invoking a tool, you must carefully evaluate whether it matches the current task. If no available tool is appropriate for the task, you MUST call the `ExitTool` to exit tool mode \u2014 this is the only valid way to terminate tool mode.\nAlways prioritize completing the user's task effectively and efficiently by using tools whenever appropriate.</system-reminder>"}),e.tools?.length&&(e.tool_choice="required",e.tools.push({type:"function",function:{name:"ExitTool",description:`Use this tool when you are in tool mode and have completed the task. This is the only valid way to exit tool mode.
|
|
150
150
|
IMPORTANT: Before using this tool, ensure that none of the available tools are applicable to the current task. You must evaluate all available options \u2014 only if no suitable tool can help you complete the task should you use ExitTool to terminate tool mode.
|
|
151
151
|
Examples:
|
|
152
152
|
1. Task: "Use a tool to summarize this document" \u2014 Do not use ExitTool if a summarization tool is available.
|
|
153
|
-
2. Task: "What\u2019s the weather today?" \u2014 If no tool is available to answer, use ExitTool after reasoning that none can fulfill the task.`,parameters:{type:"object",properties:{response:{type:"string",description:"Your response will be forwarded to the user exactly as returned \u2014 the tool will not modify or post-process it in any way."}},required:["response"]}}})),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);if(t?.choices?.[0]?.message.tool_calls?.length&&t?.choices?.[0]?.message.tool_calls[0]?.function?.name==="ExitTool"){let o=t?.choices[0]?.message.tool_calls[0],r=JSON.parse(o.function.arguments||"{}");t.choices[0].message.content=r.response||"",delete t.choices[0].message.tool_calls}return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=-1,s="",i="",a=new ReadableStream({async start(l){let c=e.body.getReader(),u=(p,
|
|
154
|
-
`);for(let f of
|
|
155
|
-
`))},d=(p,
|
|
153
|
+
2. Task: "What\u2019s the weather today?" \u2014 If no tool is available to answer, use ExitTool after reasoning that none can fulfill the task.`,parameters:{type:"object",properties:{response:{type:"string",description:"Your response will be forwarded to the user exactly as returned \u2014 the tool will not modify or post-process it in any way."}},required:["response"]}}})),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);if(t?.choices?.[0]?.message.tool_calls?.length&&t?.choices?.[0]?.message.tool_calls[0]?.function?.name==="ExitTool"){let o=t?.choices[0]?.message.tool_calls[0],r=JSON.parse(o.function.arguments||"{}");t.choices[0].message.content=r.response||"",delete t.choices[0].message.tool_calls}return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=-1,s="",i="",a=new ReadableStream({async start(l){let c=e.body.getReader(),u=(p,m,h)=>{let g=p.split(`
|
|
154
|
+
`);for(let f of g)f.trim()&&m.enqueue(h.encode(f+`
|
|
155
|
+
`))},d=(p,m)=>{let{controller:h,encoder:g,exitToolIndex:f,setExitToolIndex:_,appendExitToolResponse:k}=m;if(p.startsWith("data: ")&&p.trim()!=="data: [DONE]")try{let y=JSON.parse(p.slice(6));if(y.choices[0]?.delta?.tool_calls?.length){let b=y.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 T=JSON.parse(m.exitToolResponse());y.choices=[{delta:{role:"assistant",content:T.response||""}}];let C=`data: ${JSON.stringify(y)}
|
|
156
156
|
|
|
157
|
-
`;
|
|
157
|
+
`;h.enqueue(g.encode(C))}catch{}return}}if(y.choices?.[0]?.delta&&Object.keys(y.choices[0].delta).length>0){let b=`data: ${JSON.stringify(y)}
|
|
158
158
|
|
|
159
|
-
`;
|
|
160
|
-
`))}else
|
|
161
|
-
`))};try{for(;;){let{done:p,value:
|
|
162
|
-
`);i=
|
|
163
|
-
`))}}}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}}});import{v4 as
|
|
164
|
-
`);for(let k of _)k.trim()&&
|
|
165
|
-
`))},
|
|
159
|
+
`;h.enqueue(g.encode(b))}}catch{h.enqueue(g.encode(p+`
|
|
160
|
+
`))}else h.enqueue(g.encode(p+`
|
|
161
|
+
`))};try{for(;;){let{done:p,value:m}=await c.read();if(p){i.trim()&&u(i,l,o);break}let h=t.decode(m,{stream:!0});i+=h;let g=i.split(`
|
|
162
|
+
`);i=g.pop()||"";for(let f of g)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+`
|
|
163
|
+
`))}}}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}}});import{v4 as ou}from"uuid";var on,pi=E(()=>{"use strict";V();on=class{constructor(e){this.options=e}static TransformerName="openrouter";async transformRequestIn(e){return e.model.includes("claude")?e.messages.forEach(t=>{Array.isArray(t.content)&&t.content.forEach(o=>{o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)})}):e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control,o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`${o.image_url.url}`),delete o.media_type)}):t.cache_control&&delete t.cache_control}),Object.assign(e,this.options||{}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,a=!1,l="",c=new ReadableStream({async start(u){let d=e.body.getReader(),p=(h,g,f)=>{let _=h.split(`
|
|
164
|
+
`);for(let k of _)k.trim()&&g.enqueue(f.encode(k+`
|
|
165
|
+
`))},m=(h,g)=>{let{controller:f,encoder:_}=g;if(h.startsWith("data: ")&&h.trim()!=="data: [DONE]"){let k=h.slice(6);try{let y=JSON.parse(k);if(y.usage&&(this.logger?.debug({usage:y.usage,hasToolCall:a},"usage"),y.choices[0].finish_reason=a?"tool_calls":"stop"),y.choices?.[0]?.finish_reason==="error"){f.enqueue(_.encode(`data: ${JSON.stringify({error:y.choices?.[0].error})}
|
|
166
166
|
|
|
167
|
-
`));return}
|
|
167
|
+
`));return}y.choices?.[0]?.delta?.content&&!g.hasTextContent()&&g.setHasTextContent(!0);let b=y.choices?.[0]?.delta?.reasoning||y.choices?.[0]?.delta?.reasoning_content;if(b){g.appendReasoningContent(b);let x={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,thinking:{content:b}}}]};x.choices?.[0]?.delta&&(delete x.choices[0].delta.reasoning,delete x.choices[0].delta.reasoning_content);let w=`data: ${JSON.stringify(x)}
|
|
168
168
|
|
|
169
|
-
`;f.enqueue(_.encode(
|
|
169
|
+
`;f.enqueue(_.encode(w));return}if(y.choices?.[0]?.delta?.content&&g.reasoningContent()&&!g.isReasoningComplete()){g.setReasoningComplete(!0);let x="ccr_think_signature",w=y.choices[0].delta.content,R={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,content:null,thinking:{content:g.reasoningContent(),signature:x}}}]};R.choices?.[0]?.delta&&delete R.choices[0].delta.reasoning;let O=`data: ${JSON.stringify(R)}
|
|
170
170
|
|
|
171
|
-
`;f.enqueue(_.encode(O));let P={...
|
|
171
|
+
`;f.enqueue(_.encode(O));let P={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,content:w}}]};P.choices?.[0]?.delta&&(delete P.choices[0].delta.reasoning,delete P.choices[0].delta.reasoning_content);let M=`data: ${JSON.stringify(P)}
|
|
172
172
|
|
|
173
|
-
`;f.enqueue(_.encode(
|
|
173
|
+
`;f.enqueue(_.encode(M));return}y.choices?.[0]?.delta?.reasoning&&delete y.choices[0].delta.reasoning,y.choices?.[0]?.delta?.reasoning_content&&delete y.choices[0].delta.reasoning_content,y.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(y.choices?.[0]?.delta?.tool_calls[0].id,10))&&y.choices?.[0]?.delta?.tool_calls.forEach(x=>{x.id=`call_${ou()}`}),y.choices?.[0]?.delta?.tool_calls?.length&&!a&&(a=!0);let T=y.choices?.[0]?.delta,C=T&&Object.keys(T).length>0,v=y.choices?.[0]?.finish_reason,S=y.usage;if(C||v||S){let x=`data: ${JSON.stringify(y)}
|
|
174
174
|
|
|
175
|
-
`;f.enqueue(_.encode(
|
|
176
|
-
`))}}else f.enqueue(_.encode(
|
|
177
|
-
`))};try{for(;;){let{done:
|
|
178
|
-
`);l=k.pop()||"";for(let
|
|
175
|
+
`;f.enqueue(_.encode(x))}}catch{f.enqueue(_.encode(h+`
|
|
176
|
+
`))}}else f.enqueue(_.encode(h+`
|
|
177
|
+
`))};try{for(;;){let{done:h,value:g}=await d.read();if(h){l.trim()&&p(l,u,o);break}if(!g||g.length===0)continue;let f;try{f=t.decode(g,{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(`
|
|
178
|
+
`);l=k.pop()||"";for(let y of k)if(y.trim())try{m(y,{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:",y,b),u.enqueue(o.encode(y+`
|
|
179
179
|
`))}continue}let _=l.split(`
|
|
180
|
-
`);l=_.pop()||"";for(let k of _)if(k.trim())try{
|
|
181
|
-
`))}}}catch(
|
|
182
|
-
`);for(let _ of f)_.trim()&&
|
|
183
|
-
`))},p=(
|
|
184
|
-
|
|
185
|
-
`;
|
|
186
|
-
`))}}else
|
|
187
|
-
`))};try{for(;;){let{done:
|
|
188
|
-
`);a=_.pop()||"";for(let k of _)if(k.trim())try{p(k,{controller:c,encoder:o,hasTextContent:()=>r,setHasTextContent:
|
|
180
|
+
`);l=_.pop()||"";for(let k of _)if(k.trim())try{m(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:y=>r=y,reasoningContent:()=>s,appendReasoningContent:y=>s+=y,isReasoningComplete:()=>i,setReasoningComplete:y=>i=y})}catch(y){console.error("Error processing line:",k,y),u.enqueue(o.encode(k+`
|
|
181
|
+
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{d.releaseLock()}catch(h){console.error("Error releasing reader lock:",h)}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 rn,fi=E(()=>{"use strict";rn=class{constructor(e){this.options=e;this.max_tokens=this.options?.max_tokens}static TransformerName="maxtoken";max_tokens;async transformRequestIn(e){return e.max_tokens&&e.max_tokens>this.max_tokens&&(e.max_tokens=this.max_tokens),e}}});import{v4 as ru}from"uuid";var sn,mi=E(()=>{"use strict";V();sn=class{name="groq";async transformRequestIn(e){return e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control}):t.cache_control&&delete t.cache_control}),Array.isArray(e.tools)&&e.tools.forEach(t=>{delete t.function.parameters.$schema}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,a="",l=new ReadableStream({async start(c){let u=e.body.getReader(),d=(m,h,g)=>{let f=m.split(`
|
|
182
|
+
`);for(let _ of f)_.trim()&&h.enqueue(g.encode(_+`
|
|
183
|
+
`))},p=(m,h)=>{let{controller:g,encoder:f}=h;if(m.startsWith("data: ")&&m.trim()!=="data: [DONE]"){let _=m.slice(6);try{let k=JSON.parse(_);if(k.error)throw new Error(JSON.stringify(k));k.choices?.[0]?.delta?.content&&!h.hasTextContent()&&h.setHasTextContent(!0),k.choices?.[0]?.delta?.tool_calls?.length&&k.choices?.[0]?.delta?.tool_calls.forEach(b=>{b.id=`call_${ru()}`}),k.choices?.[0]?.delta?.tool_calls?.length&&h.hasTextContent()&&(typeof k.choices[0].index=="number"?k.choices[0].index+=1:k.choices[0].index=1);let y=`data: ${JSON.stringify(k)}
|
|
184
|
+
|
|
185
|
+
`;g.enqueue(f.encode(y))}catch{g.enqueue(f.encode(m+`
|
|
186
|
+
`))}}else g.enqueue(f.encode(m+`
|
|
187
|
+
`))};try{for(;;){let{done:m,value:h}=await u.read();if(m){a.trim()&&d(a,c,o);break}if(!h||h.length===0)continue;let g;try{g=t.decode(h,{stream:!0})}catch(_){console.warn("Failed to decode chunk",_);continue}if(g.length===0)continue;if(a+=g,a.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let _=a.split(`
|
|
188
|
+
`);a=_.pop()||"";for(let k of _)if(k.trim())try{p(k,{controller:c,encoder:o,hasTextContent:()=>r,setHasTextContent:y=>r=y,reasoningContent:()=>s,appendReasoningContent:y=>s+=y,isReasoningComplete:()=>i,setReasoningComplete:y=>i=y})}catch(y){console.error("Error processing line:",k,y),c.enqueue(o.encode(k+`
|
|
189
189
|
`))}continue}let f=a.split(`
|
|
190
190
|
`);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(_+`
|
|
191
|
-
`))}}}catch(
|
|
192
|
-
`);for(let b of
|
|
193
|
-
`))},
|
|
191
|
+
`))}}}catch(m){console.error("Stream error:",m),c.error(m)}finally{try{u.releaseLock()}catch(m){console.error("Error releasing reader lock:",m)}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 an,gi=E(()=>{"use strict";an=class{name="cleancache";async transformRequestIn(e){return Array.isArray(e.messages)&&e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control}):t.cache_control&&delete t.cache_control}),e}}});import su from"json5";import{jsonrepair as iu}from"jsonrepair";function Do(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=su.parse(n);return e?.debug("Tool arguments JSON5 parsing successful"),JSON.stringify(o)}catch(o){try{let r=iu(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 hi=E(()=>{});var cn,yi=E(()=>{"use strict";hi();V();cn=class{name="enhancetool";async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);if(t?.choices?.[0]?.message?.tool_calls?.length)for(let o of t.choices[0].message.tool_calls)o.function?.arguments&&(o.function.arguments=Do(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(),m=(f,_,k)=>{let y=f.split(`
|
|
192
|
+
`);for(let b of y)b.trim()&&_.enqueue(k.encode(b+`
|
|
193
|
+
`))},h=(f,_,k)=>{let y="";try{y=Do(r.arguments||"",this.logger)}catch(v){console.error(`${v.message} ${v.stack} \u5DE5\u5177\u8C03\u7528\u53C2\u6570\u89E3\u6790\u5931\u8D25: ${JSON.stringify(r)}`),y=r.arguments||""}let b={role:"assistant",tool_calls:[{function:{name:r.name,arguments:y},id:r.id,index:r.index,type:"function"}]},T={...f,choices:[{...f.choices[0],delta:b}]};T.choices[0].delta.content!==void 0&&delete T.choices[0].delta.content;let C=`data: ${JSON.stringify(T)}
|
|
194
194
|
|
|
195
|
-
`;_.enqueue(k.encode(C))},
|
|
195
|
+
`;_.enqueue(k.encode(C))},g=(f,_)=>{let{controller:k,encoder:y}=_;if(f.startsWith("data: ")&&f.trim()!=="data: [DONE]"){let b=f.slice(6);try{let T=JSON.parse(b);if(T.choices?.[0]?.delta?.tool_calls?.length){let v=T.choices[0].delta.tool_calls[0];if(typeof r.index>"u"){r={index:v.index,name:v.function?.name||"",id:v.id||"",arguments:v.function?.arguments||""},v.function?.arguments&&(v.function.arguments="");let S=`data: ${JSON.stringify(T)}
|
|
196
196
|
|
|
197
|
-
`;k.enqueue(
|
|
197
|
+
`;k.enqueue(y.encode(S));return}else if(r.index===v.index){v.function?.arguments&&(r.arguments+=v.function.arguments);return}else{h(T,k,y),r={index:v.index,name:v.function?.name||"",id:v.id||"",arguments:v.function?.arguments||""};return}}if(T.choices?.[0]?.finish_reason==="tool_calls"&&r.index!==void 0){h(T,k,y),r={};return}T.choices?.[0]?.delta?.tool_calls?.length&&_.hasTextContent()&&(typeof T.choices[0].index=="number"?T.choices[0].index+=1:T.choices[0].index=1);let C=`data: ${JSON.stringify(T)}
|
|
198
198
|
|
|
199
|
-
`;k.enqueue(
|
|
200
|
-
`))}}else k.enqueue(
|
|
201
|
-
`))};try{for(;;){let{done:f,value:_}=await p.read();if(f){c.trim()&&
|
|
202
|
-
`);c=b.pop()||"";for(let
|
|
203
|
-
`))}continue}let
|
|
204
|
-
`);c=
|
|
205
|
-
`))}}}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 ln,
|
|
206
|
-
`);for(let f of
|
|
207
|
-
`))},d=(p,
|
|
199
|
+
`;k.enqueue(y.encode(C))}catch{k.enqueue(y.encode(f+`
|
|
200
|
+
`))}}else k.enqueue(y.encode(f+`
|
|
201
|
+
`))};try{for(;;){let{done:f,value:_}=await p.read();if(f){c.trim()&&m(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(`
|
|
202
|
+
`);c=b.pop()||"";for(let T of b)if(T.trim())try{g(T,{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:",T,C),d.enqueue(o.encode(T+`
|
|
203
|
+
`))}continue}let y=c.split(`
|
|
204
|
+
`);c=y.pop()||"";for(let b of y)if(b.trim())try{g(b,{controller:d,encoder:o,hasTextContent:()=>s,setHasTextContent:T=>s=T,reasoningContent:()=>i,appendReasoningContent:T=>i+=T,isReasoningComplete:()=>a,setReasoningComplete:T=>a=T})}catch(T){console.error("Error processing line:",b,T),d.enqueue(o.encode(b+`
|
|
205
|
+
`))}}}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 ln,_i=E(()=>{"use strict";V();He();ln=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??Vt(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 F(e);return t.choices[0]?.message.reasoning_content&&(t.thinking={content:t.choices[0]?.message.reasoning_content}),new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r="",s=!1,i="",a=new ReadableStream({async start(l){let c=e.body.getReader(),u=(p,m,h)=>{let g=p.split(`
|
|
206
|
+
`);for(let f of g)f.trim()&&m.enqueue(h.encode(f+`
|
|
207
|
+
`))},d=(p,m)=>{let{controller:h,encoder:g}=m;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){m.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(_)}
|
|
208
208
|
|
|
209
|
-
`;
|
|
209
|
+
`;h.enqueue(g.encode(k));return}if(f.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let _="ccr_think_signature",k=f.choices[0].delta.content,y={...f,choices:[{...f.choices[0],delta:{...f.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:_}}}]};delete y.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(y)}
|
|
210
210
|
|
|
211
|
-
`;
|
|
211
|
+
`;h.enqueue(g.encode(b))}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){m.isReasoningComplete()&&f.choices[0].index++;let _=`data: ${JSON.stringify(f)}
|
|
212
212
|
|
|
213
|
-
`;
|
|
214
|
-
`))}else
|
|
215
|
-
`))};try{for(;;){let{done:p,value:
|
|
216
|
-
`);i=
|
|
217
|
-
`))}}}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 un,
|
|
213
|
+
`;h.enqueue(g.encode(_))}}catch{h.enqueue(g.encode(p+`
|
|
214
|
+
`))}else h.enqueue(g.encode(p+`
|
|
215
|
+
`))};try{for(;;){let{done:p,value:m}=await c.read();if(p){i.trim()&&u(i,l,o);break}let h=t.decode(m,{stream:!0});i+=h;let g=i.split(`
|
|
216
|
+
`);i=g.pop()||"";for(let f of g)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+`
|
|
217
|
+
`))}}}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 un,ki=E(()=>{"use strict";un=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 dn,bi=E(()=>{"use strict";dn=class{static TransformerName="maxcompletiontokens";async transformRequestIn(e){return e.max_tokens&&(e.max_completion_tokens=e.max_tokens,delete e.max_tokens),e}}});function Ti(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 vi(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 xi(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)}
|
|
218
218
|
|
|
219
219
|
`))}else if(u.type==="content_block_delta"&&u.delta?.type==="input_json_delta"){let d={choices:[{delta:{tool_calls:[{index:u.index||0,function:{arguments:u.delta.partial_json||""}}]},finish_reason:null,index:0,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:u.id||"",model:u.model||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:u.usage?.output_tokens||0,prompt_tokens:u.usage?.input_tokens||0,total_tokens:(u.usage?.input_tokens||0)+(u.usage?.output_tokens||0)}};l.enqueue(r.encode(`data: ${JSON.stringify(d)}
|
|
220
220
|
|
|
@@ -227,93 +227,93 @@ Examples:
|
|
|
227
227
|
`));else{let d={choices:[{delta:{role:"assistant",content:u.content?.[0]?.text||""},finish_reason:u.stop_reason?.toLowerCase()||null,index:0,logprobs:null}],created:parseInt(new Date().getTime()/1e3+"",10),id:u.id||"",model:u.model||"",object:"chat.completion.chunk",system_fingerprint:"fp_a49d71b8a1",usage:{completion_tokens:u.usage?.output_tokens||0,prompt_tokens:u.usage?.input_tokens||0,total_tokens:(u.usage?.input_tokens||0)+(u.usage?.output_tokens||0)}};l.enqueue(r.encode(`data: ${JSON.stringify(d)}
|
|
228
228
|
|
|
229
229
|
`))}}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(`
|
|
230
|
-
`);c=p.pop()||"";for(let
|
|
230
|
+
`);c=p.pop()||"";for(let m of p)s(m,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 Ci=E(()=>{"use strict"});async function au(){try{let{GoogleAuth:n}=await import("google-auth-library");return(await(await new n({scopes:["https://www.googleapis.com/auth/cloud-platform"]}).getClient()).getAccessToken()).token||""}catch(n){throw console.error("Error getting access token:",n),new Error(`Failed to get access token for Vertex AI. Please ensure you have set up authentication using one of these methods:
|
|
231
231
|
1. Set GOOGLE_APPLICATION_CREDENTIALS to point to service account key file
|
|
232
232
|
2. Run "gcloud auth application-default login"
|
|
233
|
-
3. Use Google Cloud environment with default service account`)}}var pn,
|
|
234
|
-
`);for(let k of _)k.trim()&&
|
|
235
|
-
`))},
|
|
233
|
+
3. Use Google Cloud environment with default service account`)}}var pn,Si=E(()=>{"use strict";Ci();pn=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 au();return{body:Ti(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 vi(e)}async transformResponseOut(e){return xi(e,this.name,this.logger)}}});var fn,Ri=E(()=>{"use strict";fn=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 mn,wi=E(()=>{"use strict";mn=class{name="streamoptions";async transformRequestIn(e){return e.stream&&(e.stream_options={include_usage:!0}),e}}});var gn,Ei=E(()=>{"use strict";gn=class{name="OpenAI";endPoint="/v1/chat/completions"}});var hn,Pi=E(()=>{"use strict";hn=class{static TransformerName="customparams";options;constructor(e={}){this.options=e}async transformRequestIn(e){let t={...e},o=Object.entries(this.options);for(let[r,s]of o)if(r in t)if(typeof t[r]=="object"&&typeof s=="object"&&!Array.isArray(t[r])&&!Array.isArray(s)&&t[r]!==null&&s!==null)t[r]=this.deepMergeObjects(t[r],s);else continue;else t[r]=this.cloneValue(s);return t}async transformResponseOut(e){return e}deepMergeObjects(e,t){let o={...e};for(let[r,s]of Object.entries(t))r in o&&typeof o[r]=="object"&&typeof s=="object"&&!Array.isArray(o[r])&&!Array.isArray(s)&&o[r]!==null&&s!==null?o[r]=this.deepMergeObjects(o[r],s):o[r]=this.cloneValue(s);return o}cloneValue(e){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return e.map(o=>this.cloneValue(o));let t={};for(let[o,r]of Object.entries(e))t[o]=this.cloneValue(r);return t}}});import{v4 as cu}from"uuid";var yn,Oi=E(()=>{"use strict";V();yn=class{constructor(e){this.options=e}static TransformerName="vercel";endPoint="/v1/chat/completions";async transformRequestIn(e){return e.model.includes("claude")?e.messages.forEach(t=>{Array.isArray(t.content)&&t.content.forEach(o=>{o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)})}):e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control,o.type==="image_url"&&(o.image_url.url.startsWith("http")||(o.image_url.url=`data:${o.media_type};base64,${o.image_url.url}`),delete o.media_type)}):t.cache_control&&delete t.cache_control}),Object.assign(e,this.options||{}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await F(e);return new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,a=!1,l="",c=new ReadableStream({async start(u){let d=e.body.getReader(),p=(h,g,f)=>{let _=h.split(`
|
|
234
|
+
`);for(let k of _)k.trim()&&g.enqueue(f.encode(k+`
|
|
235
|
+
`))},m=(h,g)=>{let{controller:f,encoder:_}=g;if(h.startsWith("data: ")&&h.trim()!=="data: [DONE]"){let k=h.slice(6);try{let y=JSON.parse(k);if(y.usage&&(this.logger?.debug({usage:y.usage,hasToolCall:a},"usage"),y.choices[0].finish_reason=a?"tool_calls":"stop"),y.choices?.[0]?.finish_reason==="error"&&f.enqueue(_.encode(`data: ${JSON.stringify({error:y.choices?.[0].error})}
|
|
236
236
|
|
|
237
|
-
`)),
|
|
237
|
+
`)),y.choices?.[0]?.delta?.content&&!g.hasTextContent()&&g.setHasTextContent(!0),y.choices?.[0]?.delta?.reasoning){g.appendReasoningContent(y.choices[0].delta.reasoning);let T={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,thinking:{content:y.choices[0].delta.reasoning}}}]};T.choices?.[0]?.delta&&delete T.choices[0].delta.reasoning;let C=`data: ${JSON.stringify(T)}
|
|
238
238
|
|
|
239
|
-
`;f.enqueue(_.encode(C));return}if(
|
|
239
|
+
`;f.enqueue(_.encode(C));return}if(y.choices?.[0]?.delta?.content&&g.reasoningContent()&&!g.isReasoningComplete()){g.setReasoningComplete(!0);let C={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,content:null,thinking:{content:g.reasoningContent(),signature:"ccr_think_signature"}}}]};C.choices?.[0]?.delta&&delete C.choices[0].delta.reasoning;let v=`data: ${JSON.stringify(C)}
|
|
240
240
|
|
|
241
|
-
`;f.enqueue(_.encode(
|
|
241
|
+
`;f.enqueue(_.encode(v))}y.choices?.[0]?.delta?.reasoning&&delete y.choices[0].delta.reasoning,y.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(y.choices?.[0]?.delta?.tool_calls[0].id,10))&&y.choices?.[0]?.delta?.tool_calls.forEach(T=>{T.id=`call_${cu()}`}),y.choices?.[0]?.delta?.tool_calls?.length&&!a&&(a=!0),y.choices?.[0]?.delta?.tool_calls?.length&&g.hasTextContent()&&(typeof y.choices[0].index=="number"?y.choices[0].index+=1:y.choices[0].index=1);let b=`data: ${JSON.stringify(y)}
|
|
242
242
|
|
|
243
|
-
`;f.enqueue(_.encode(b))}catch{f.enqueue(_.encode(
|
|
244
|
-
`))}}else f.enqueue(_.encode(
|
|
245
|
-
`))};try{for(;;){let{done:
|
|
246
|
-
`);l=k.pop()||"";for(let
|
|
243
|
+
`;f.enqueue(_.encode(b))}catch{f.enqueue(_.encode(h+`
|
|
244
|
+
`))}}else f.enqueue(_.encode(h+`
|
|
245
|
+
`))};try{for(;;){let{done:h,value:g}=await d.read();if(h){l.trim()&&p(l,u,o);break}if(!g||g.length===0)continue;let f;try{f=t.decode(g,{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(`
|
|
246
|
+
`);l=k.pop()||"";for(let y of k)if(y.trim())try{m(y,{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:",y,b),u.enqueue(o.encode(y+`
|
|
247
247
|
`))}continue}let _=l.split(`
|
|
248
|
-
`);l=_.pop()||"";for(let k of _)if(k.trim())try{
|
|
249
|
-
`))}}}catch(
|
|
248
|
+
`);l=_.pop()||"";for(let k of _)if(k.trim())try{m(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:y=>r=y,reasoningContent:()=>s,appendReasoningContent:y=>s+=y,isReasoningComplete:()=>i,setReasoningComplete:y=>i=y})}catch(y){console.error("Error processing line:",k,y),u.enqueue(o.encode(k+`
|
|
249
|
+
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{d.releaseLock()}catch(h){console.error("Error releasing reader lock:",h)}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 Fo,lu,_n,Ai=E(()=>{"use strict";V();Fo=`Always think before answering. Even if the problem seems simple, always write down your reasoning process explicitly.
|
|
250
250
|
|
|
251
251
|
Output format:
|
|
252
252
|
<reasoning_content>
|
|
253
253
|
Your detailed thinking process goes here
|
|
254
254
|
</reasoning_content>
|
|
255
|
-
Your final answer must follow after the closing tag above.`,
|
|
256
|
-
${r.content}`),delete r.thinking)});let o=e.messages[e.messages.length-1];return o.role==="user"&&(Array.isArray(o.content)?o.content.push({type:"text",text:
|
|
255
|
+
Your final answer must follow after the closing tag above.`,lu=10,_n=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<lu)&&(t++,r.content=`<reasoning_content>${r.thinking.content}</reasoning_content>
|
|
256
|
+
${r.content}`),delete r.thinking)});let o=e.messages[e.messages.length-1];return o.role==="user"&&(Array.isArray(o.content)?o.content.push({type:"text",text:Fo}):o.content=[{type:"text",text:Fo},{type:"text",text:o.content||""}]),o.role==="tool"&&e.messages.push({role:"user",content:[{type:"text",text:Fo}]}),e}async transformResponseOut(e){let t="<reasoning_content>",o="</reasoning_content>";if(e.headers.get("Content-Type")?.includes("application/json")){let r=await F(e);if(r.choices[0]?.message.content){let s=/<reasoning_content>(.*?)<\/reasoning_content>/s,i=r.choices[0]?.message.content.match(s);i&&i[1]&&(r.thinking={content:i[1]})}return new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let r=0,s=new TextDecoder,i=new TextEncoder,a=new ReadableStream({async start(l){let c=e.body.getReader(),u="",d="SEARCHING",p="",m="",h=(g,f)=>{if(typeof f!="string"){g.choices?.[0]?.delta&&Object.keys(g.choices[0].delta).length>0&&!g.choices[0].delta.content&&(g.choices[0].index=r,l.enqueue(i.encode(`data: ${JSON.stringify(g)}
|
|
257
257
|
|
|
258
|
-
`)));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
|
|
258
|
+
`)));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 y=t.length-1;y>0;y--)if(_.endsWith(t.substring(0,y))){p=_.substring(_.length-y);break}_=""}}else if(d==="REASONING"){let k=_.indexOf(o);if(k!==-1){let y=_.substring(0,k);if(y.length>0){let C={...g.choices[0].delta,thinking:{content:y}};delete C.content;let v={...g,choices:[{...g.choices[0],delta:C,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(v)}
|
|
259
259
|
|
|
260
|
-
`))}let b={...
|
|
260
|
+
`))}let b={...g.choices[0].delta,thinking:{signature:"ccr_think_signature"}};delete b.content;let T={...g,choices:[{...g.choices[0],delta:b,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
|
|
261
261
|
|
|
262
|
-
`)),r++,_=_.substring(k+o.length),d="FINAL"}else{let
|
|
262
|
+
`)),r++,_=_.substring(k+o.length),d="FINAL"}else{let y=_;for(let b=o.length-1;b>0;b--)if(_.endsWith(o.substring(0,b))){p=_.substring(_.length-b),y=_.substring(0,_.length-b);break}if(y.length>0){let b={...g.choices[0].delta,thinking:{content:y}};delete b.content;let T={...g,choices:[{...g.choices[0],delta:b,index:r}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
|
|
263
263
|
|
|
264
|
-
`))}_=""}}else if(d==="FINAL"){if(_.length>0)if(/^\s*$/.test(_))
|
|
264
|
+
`))}_=""}}else if(d==="FINAL"){if(_.length>0)if(/^\s*$/.test(_))m+=_;else{let y=m+_,b={...g.choices[0].delta,content:y};b.thinking&&delete b.thinking;let T={...g,choices:[{...g.choices[0],delta:b}]};l.enqueue(i.encode(`data: ${JSON.stringify(T)}
|
|
265
265
|
|
|
266
|
-
`)),
|
|
267
|
-
`);u=k.pop()||"";for(let
|
|
266
|
+
`)),m=""}r++,_=""}};try{for(;;){let{done:g,value:f}=await c.read();if(g)break;let _=s.decode(f,{stream:!0});u+=_;let k=u.split(`
|
|
267
|
+
`);u=k.pop()||"";for(let y of k)if(y.trim()){if(y.trim()==="data: [DONE]"){l.enqueue(i.encode(y+`
|
|
268
268
|
|
|
269
|
-
`));break}if(
|
|
270
|
-
`))}else l.enqueue(i.encode(
|
|
271
|
-
`))}}}catch(
|
|
269
|
+
`));break}if(y.startsWith("data:"))try{let b=JSON.parse(y.slice(5));h(b,b.choices?.[0]?.delta?.content)}catch{l.enqueue(i.encode(y+`
|
|
270
|
+
`))}else l.enqueue(i.encode(y+`
|
|
271
|
+
`))}}}catch(g){console.error("Stream error:",g),l.error(g)}finally{try{c.releaseLock()}catch(g){console.error("Error releasing reader lock:",g)}if(d==="REASONING"){let f={choices:[{delta:{thinking:{signature:"ccr_think_signature"}}}]};l.enqueue(i.encode(`data: ${JSON.stringify(f)}
|
|
272
272
|
|
|
273
|
-
`))}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 kn,
|
|
274
|
-
`));break}let p=o.decode(d,{stream:!0});i+=p;let
|
|
275
|
-
`);i=
|
|
273
|
+
`))}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 kn,Ii=E(()=>{"use strict";V();kn=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+`
|
|
274
|
+
`));break}let p=o.decode(d,{stream:!0});i+=p;let m=i.split(`
|
|
275
|
+
`);i=m.pop()||"";for(let h of m)if(h.trim())if(h.startsWith("data: ")&&h.trim()!=="data: [DONE]")try{let g=JSON.parse(h.slice(6));if(g.choices?.[0]?.delta?.reasoning_content){s+=g.choices[0].delta.reasoning_content;let f={...g,choices:[{...g.choices[0],delta:{thinking:{content:g.choices[0].delta.reasoning_content}}}]};delete f.choices[0].delta.reasoning_content,l.enqueue(r.encode(`data: ${JSON.stringify(f)}
|
|
276
276
|
|
|
277
|
-
`));continue}if(
|
|
277
|
+
`));continue}if(g.choices?.[0]?.delta?.content&&s&&!g.choices[0]?.delta?.reasoning_content){let f=Date.now().toString(),_={...g,choices:[{...g.choices[0],delta:{content:null,thinking:{content:s,signature:f}}}]};l.enqueue(r.encode(`data: ${JSON.stringify(_)}
|
|
278
278
|
|
|
279
|
-
`)),s=""}
|
|
279
|
+
`)),s=""}g.choices?.[0]?.delta?.reasoning_content&&delete g.choices[0].delta.reasoning_content,g.choices?.[0]?.delta&&Object.keys(g.choices[0].delta).length>0&&l.enqueue(r.encode(`data: ${JSON.stringify(g)}
|
|
280
280
|
|
|
281
|
-
`))}catch{l.enqueue(r.encode(
|
|
282
|
-
`))}else l.enqueue(r.encode(
|
|
283
|
-
`))}}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 F(e);return o.choices?.[0]?.message?.reasoning_content&&(o.choices[0].message.thinking={content:o.choices[0].message.reasoning_content},delete o.choices[0].message.reasoning_content),new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}return e}}});import{v4 as
|
|
284
|
-
`);for(let k of _)k.trim()&&
|
|
285
|
-
`))},
|
|
281
|
+
`))}catch{l.enqueue(r.encode(h+`
|
|
282
|
+
`))}else l.enqueue(r.encode(h+`
|
|
283
|
+
`))}}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 F(e);return o.choices?.[0]?.message?.reasoning_content&&(o.choices[0].message.thinking={content:o.choices[0].message.reasoning_content},delete o.choices[0].message.reasoning_content),new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}return e}}});import{v4 as Mi}from"uuid";var bn,Ni=E(()=>{"use strict";V();Ne();bn=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||ge(e.metadata?.user_id)||s("session_id")||Mi()}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 F(e);return t.choices?.[0]?.message?.reasoning_content&&(t.choices[0].message.thinking={content:t.choices[0].message.reasoning_content},delete t.choices[0].message.reasoning_content),new Response(JSON.stringify(t),{status:e.status,statusText:e.statusText,headers:e.headers})}if(e.headers.get("Content-Type")?.includes("stream")){if(!e.body)return e;let t=new TextDecoder,o=new TextEncoder,r=!1,s="",i=!1,a=!1,l="",c=new ReadableStream({async start(u){let d=e.body.getReader(),p=(h,g,f)=>{let _=h.split(`
|
|
284
|
+
`);for(let k of _)k.trim()&&g.enqueue(f.encode(k+`
|
|
285
|
+
`))},m=(h,g)=>{let{controller:f,encoder:_}=g;if(h.startsWith("data: ")&&h.trim()!=="data: [DONE]")try{let k=JSON.parse(h.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})}
|
|
286
286
|
|
|
287
|
-
`));return}k.choices?.[0]?.delta?.content&&!
|
|
287
|
+
`));return}k.choices?.[0]?.delta?.content&&!g.hasTextContent()&&g.setHasTextContent(!0);let y=k.choices?.[0]?.delta?.reasoning||k.choices?.[0]?.delta?.reasoning_content;if(y){g.appendReasoningContent(y);let S={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,thinking:{content:y}}}]};S.choices?.[0]?.delta&&(delete S.choices[0].delta.reasoning,delete S.choices[0].delta.reasoning_content),f.enqueue(_.encode(`data: ${JSON.stringify(S)}
|
|
288
288
|
|
|
289
|
-
`));return}if(k.choices?.[0]?.delta?.content&&
|
|
289
|
+
`));return}if(k.choices?.[0]?.delta?.content&&g.reasoningContent()&&!g.isReasoningComplete()){g.setReasoningComplete(!0);let S="ccr_think_signature",x=k.choices[0].delta.content,w={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:null,thinking:{content:g.reasoningContent(),signature:S}}}]};w.choices?.[0]?.delta&&(delete w.choices[0].delta.reasoning,delete w.choices[0].delta.reasoning_content),f.enqueue(_.encode(`data: ${JSON.stringify(w)}
|
|
290
290
|
|
|
291
|
-
`));let R={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:
|
|
291
|
+
`));let R={...k,choices:[{...k.choices?.[0],delta:{...k.choices[0].delta,content:x}}]};R.choices?.[0]?.delta&&(delete R.choices[0].delta.reasoning,delete R.choices[0].delta.reasoning_content),f.enqueue(_.encode(`data: ${JSON.stringify(R)}
|
|
292
292
|
|
|
293
|
-
`));return}k.choices?.[0]?.delta?.reasoning&&delete k.choices[0].delta.reasoning,k.choices?.[0]?.delta?.reasoning_content&&delete k.choices[0].delta.reasoning_content,k.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(k.choices?.[0]?.delta?.tool_calls[0].id,10))&&k.choices?.[0]?.delta?.tool_calls.forEach(S=>{S.id=`call_${
|
|
293
|
+
`));return}k.choices?.[0]?.delta?.reasoning&&delete k.choices[0].delta.reasoning,k.choices?.[0]?.delta?.reasoning_content&&delete k.choices[0].delta.reasoning_content,k.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(k.choices?.[0]?.delta?.tool_calls[0].id,10))&&k.choices?.[0]?.delta?.tool_calls.forEach(S=>{S.id=`call_${Mi()}`}),k.choices?.[0]?.delta?.tool_calls?.length&&!a&&(a=!0);let b=k.choices?.[0]?.delta,T=b&&Object.keys(b).length>0,C=k.choices?.[0]?.finish_reason,v=k.usage;(T||C||v)&&f.enqueue(_.encode(`data: ${JSON.stringify(k)}
|
|
294
294
|
|
|
295
|
-
`))}catch{f.enqueue(_.encode(
|
|
296
|
-
`))}else f.enqueue(_.encode(
|
|
297
|
-
`))};try{for(;;){let{done:
|
|
298
|
-
`);l=k.pop()||"";for(let
|
|
295
|
+
`))}catch{f.enqueue(_.encode(h+`
|
|
296
|
+
`))}else f.enqueue(_.encode(h+`
|
|
297
|
+
`))};try{for(;;){let{done:h,value:g}=await d.read();if(h){l.trim()&&p(l,u,o);break}if(!g||g.length===0)continue;let f;try{f=t.decode(g,{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(`
|
|
298
|
+
`);l=k.pop()||"";for(let y of k)if(y.trim())try{m(y,{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:",y,b),u.enqueue(o.encode(y+`
|
|
299
299
|
`))}continue}let _=l.split(`
|
|
300
|
-
`);l=_.pop()||"";for(let k of _)if(k.trim())try{
|
|
301
|
-
`))}}}catch(
|
|
300
|
+
`);l=_.pop()||"";for(let k of _)if(k.trim())try{m(k,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:y=>r=y,reasoningContent:()=>s,appendReasoningContent:y=>s+=y,isReasoningComplete:()=>i,setReasoningComplete:y=>i=y})}catch(y){console.error("Error processing line:",k,y),u.enqueue(o.encode(k+`
|
|
301
|
+
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{d.releaseLock()}catch(h){console.error("Error releasing reader lock:",h)}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 du(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 pu(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 uu,Tn,$i=E(()=>{"use strict";He();uu={min:"low",minimal:"low",light:"low",xhigh:"max",maximum:"max",ultra:"max",ultracode:"max"};Tn=class{constructor(e){this.options=e}static TransformerName="defaultthinking";async transformRequestIn(e,t){let o=du(this.options?.level??this.options?.value);if(o===void 0||o==="none"||e.reasoning)return e;let r=pu(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=he(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=uu[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)}}});function Li(n,e=!1){if(!n||typeof n!="object"||n.thinking)return!1;let t=n.reasoning,o=typeof t=="string"?t:t?.content;return typeof o=="string"&&o.length>0?(e&&typeof t=="object"?n.thinking={content:o}:(n.thinking=typeof t=="string"?{content:o}:{...t},delete n.reasoning),!0):typeof n.reasoning_content=="string"&&n.reasoning_content?(n.thinking={content:n.reasoning_content},!0):!1}var vn,Di=E(()=>{"use strict";He();V();vn=class{static TransformerName="amd";name="amd";resolveEffort(e,t){if(!e)return;let o=(t??"").toLowerCase(),r=o.includes("glm"),s=!r&&o.includes("qwen"),i=!r&&!s&&o.includes("deepseek");if(e==="none")return i||o.split("/").pop()==="qwen3.8-flash-next"?"none":void 0;switch(e){case"minimal":return i?"minimal":"low";case"low":case"medium":return e;case"high":return s?"xhigh":"high";case"xhigh":case"max":return i?e:s?"xhigh":"high";default:return"medium"}}async transformRequestIn(e){let t=JSON.parse(JSON.stringify(e)),o=t.model,r=t.reasoning,s,i=r?.enabled===!1;r&&typeof r.effort=="string"?(s=r.effort===""?void 0:r.effort,i=i||r.effort==="none"):typeof r?.max_tokens=="number"?s=he(r.max_tokens):r?.enabled===!0?s=t.reasoning_effort??"medium":(s=t.reasoning_effort,!i&&(t.enable_thinking===!1||t.thinking?.type==="disabled")&&(i=!0));let a=this.resolveEffort(i?"none":s,o);return a?t.reasoning_effort=a:delete t.reasoning_effort,delete t.reasoning,delete t.thinking,delete t.enable_thinking,t}async transformResponseOut(e){let t=e.headers.get("Content-Type")??"";if(t.includes("application/json")){if(!e.body)return e;let o=await pe(e);if(o){let i=new Headers(e.headers);if(o.isSSE&&i.set("Content-Type","text/event-stream"),e=new Response(o.body,{status:e.status,statusText:e.statusText,headers:i}),o.isSSE)return this.transformStream(e)}else{let i=await fe(e);if(e=i.response,i.isSSE)return this.transformStream(e)}let r=await F(e);this.liftMessageReasoning(r);let s=new Headers(e.headers);return s.delete("content-length"),new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:s})}return(t.includes("stream")||t.includes("event-stream"))&&e.body?this.transformStream(e):e}liftMessageReasoning(e){if(Array.isArray(e?.choices))for(let t of e.choices)Li(t?.message)}transformStream(e){let t=new TextDecoder,o=new TextEncoder,r="",s=c=>{if(!c.startsWith("data:"))return c;let u=c.slice(5).trim();if(u==="[DONE]")return c;let d;try{d=JSON.parse(u)}catch{return c}let p=!1,m=Array.isArray(d?.choices)?d.choices:[];for(let h of m)Li(h?.delta,!0)&&(p=!0);return p?`data: ${JSON.stringify(d)}`:c},i=(c,u=!1)=>{let d=/\r\n|\r|\n/g,p=0,m,h="";for(;(m=d.exec(r))!==null&&!(!u&&m[0]==="\r"&&d.lastIndex===r.length);)h+=s(r.slice(p,m.index))+m[0],p=d.lastIndex;r=r.slice(p),u&&(h+=s(r),r=""),h&&c.enqueue(o.encode(h))},a=e.body.pipeThrough(new TransformStream({transform(c,u){r+=t.decode(c,{stream:!0}),i(u)},flush(c){r+=t.decode(),i(c,!0)}})),l=new Headers(e.headers);return l.delete("content-length"),new Response(a,{status:e.status,statusText:e.statusText,headers:l})}}});var Fi,Ui=E(()=>{ai();ci();li();ui();di();pi();fi();mi();gi();yi();_i();ki();bi();Si();Ri();wi();Ei();Pi();Oi();Co();Ai();Ii();Ni();$i();Di();Fi={AnthropicTransformer:Qt,GeminiTransformer:qt,VertexGeminiTransformer:en,VertexClaudeTransformer:pn,DeepseekTransformer:tn,TooluseTransformer:nn,OpenrouterTransformer:on,OpenAITransformer:gn,MaxTokenTransformer:rn,GroqTransformer:sn,CleancacheTransformer:an,EnhanceToolTransformer:cn,ReasoningTransformer:ln,SamplingTransformer:un,MaxCompletionTokens:dn,CerebrasTransformer:fn,StreamOptionsTransformer:mn,CustomParamsTransformer:hn,VercelTransformer:yn,OpenAIResponsesTransformer:Ae,ForceReasoningTransformer:_n,KimiTransformer:kn,OpenCodeTransformer:bn,DefaultThinkingTransformer:Tn,AmdTransformer:vn}});var Be,Uo=E(()=>{"use strict";Ui();Be=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=Xe(Xe.resolve(e.path));if(t){let o=new t(e.options);if(o&&typeof o=="object"&&(o.logger=this.logger),!o.name)throw new Error(`Transformer instance from ${e.path} does not have a name property.`);return this.registerTransformer(o.name,o),!0}}return!1}catch(t){return this.logger.error(`load transformer (${e.path})
|
|
302
302
|
error: ${t.message}
|
|
303
303
|
stack: ${t.stack}`),!1}}async initialize(){try{await this.registerDefaultTransformersInternal(),await this.loadFromConfig()}catch(e){this.logger.error(`TransformerService init error: ${e.message}
|
|
304
|
-
Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(Ci).forEach(e=>{if("TransformerName"in e&&typeof e.TransformerName=="string")this.registerTransformer(e.TransformerName,e);else{let t=new e;t&&typeof t=="object"&&(t.logger=this.logger),this.registerTransformer(t.name,t)}})}catch(e){this.logger.error({error:e},"transformer regist error:")}}async loadFromConfig(){let e=this.configService.get("transformers",[]);for(let t of e)await this.registerTransformerFromConfig(t)}}});import{get_encoding as Jl}from"tiktoken";var dt,Ri=w(()=>{"use strict";dt=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=Jl(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)}}});import{join as vn}from"path";import{homedir as Wl}from"os";import{existsSync as $o,mkdirSync as Gl}from"fs";import{promises as xn}from"fs";import{Tokenizer as Kl}from"@huggingface/tokenizers";var Cn,wi=w(()=>{"use strict";Cn=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||vn(Wl(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=vn(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:vn(e,"tokenizer.json"),tokenizerConfig:vn(e,"tokenizer_config.json")}}ensureDir(e){$o(e)||Gl(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!$o(e.tokenizerJson)||!$o(e.tokenizerConfig))return null;let[t,o]=await Promise.all([xn.readFile(e.tokenizerJson,"utf-8"),xn.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([xn.writeFile(e.tokenizerJson,JSON.stringify(a,null,2)),xn.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 Kl(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 Sn,Ei=w(()=>{"use strict";oe();Sn=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=K(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 Je,No=w(()=>{"use strict";Ri();wi();Ei();oe();Je=class{tokenizers=new Map;configService;logger;options;fallbackTokenizer;constructor(e,t,o={}){this.configService=e,this.logger=t,this.options={timeout:o.timeout??3e4,...o}}async initialize(){try{this.fallbackTokenizer=new dt("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 dt(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),r=new Cn(e.model,this.logger,{timeout:this.options.timeout});break;case"api":r=new Sn(e,this.logger,{timeout:this.options.timeout,resolveProxyUrl:()=>{let s=t?this.getRawProvider(t):void 0;return ce(this.configService,s)}});break;default:throw new Error(`Unknown tokenizer type: ${e.type}`)}return this.logger?.info(`Calling initialize() on ${e.type} tokenizer...`),await r.initialize(),this.tokenizers.set(o,r),this.logger?.info(`Tokenizer initialized successfully: ${e.type} (${o})`),r}catch(s){return this.logger?.error(`Failed to initialize ${e.type} tokenizer: ${s.message}`),this.logger?.error(`Error stack: ${s.stack}`),this.fallbackTokenizer||await this.initialize(),this.fallbackTokenizer}}async countTokens(e,t,o){let r=t?await this.getTokenizer(t,o):this.fallbackTokenizer;return{tokenCount:await r.countTokens(e),tokenizerUsed:r.name,cached:!1}}getTokenizerConfigForModel(e,t){let r=(this.configService.get("providers")||[]).find(s=>s.name===e);if(r?.tokenizer)return r.tokenizer.models?.[t]?r.tokenizer.models[t]:r.tokenizer.default}dispose(){this.tokenizers.forEach(e=>{try{e.dispose()}catch(t){this.logger?.error(`Error disposing tokenizer: ${t}`)}}),this.tokenizers.clear()}getCacheKey(e,t){switch(e.type){case"tiktoken":return`tiktoken:${e.encoding||"cl100k_base"}`;case"huggingface":return`hf:${e.model}`;case"api":return`api:${t||"unscoped"}:${e.url}`;default:return`unknown:${JSON.stringify(e)}`}}getRawProvider(e){return(this.configService.get("providers")||[]).find(o=>o?.name===e)}}});function Pi(n){if(!n||typeof n!="object"||eu(n))return null;for(let t of ql(n)){let o=Zl(t);if(o)return o}return null}function Zl(n){if(!n||typeof n!="object")return null;let e={},t=Rn(n,Ql);if(t!==void 0){let i=Oi(t);e.usedDailyBalance=i,e.limitDaily=100}let o=Rn(n,Vl);if(o!==void 0){let i=Oi(o);e.usedBalance=i,e.totalBalance=100}let r=Rn(n,Yl);if(r!==void 0){let i=Ai(r);i&&(e.resetTime=i)}let s=Rn(n,Xl);if(s!==void 0){let i=Ai(s);i&&(e.resetTime7d=i,e.resetTime||(e.resetTime=i))}return e.usedDailyBalance===void 0&&e.limitDaily===void 0&&e.usedBalance===void 0&&e.totalBalance===void 0&&e.resetTime===void 0?null:e}function Oi(n){return!Number.isFinite(n)||n<=0?0:Math.round(n*1e4)/100}function Rn(n,e){for(let t of e){let o=M(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 eu(n){let e=n?.data;return e&&typeof e=="object"&&(e.success===!1||e.errorCode||e.errorMsg)?String(e.errorCode||e.errorMsg||"gateway-error"):null}function Ai(n){if(typeof n!="string"&&typeof n!="number")return;let e=n;if(typeof n=="number"&&n<1e12&&(e=n*1e3),typeof n=="string"){let o=Number(n.trim());Number.isFinite(o)&&o<1e12&&(e=o*1e3)}let t=new Date(e);return isNaN(t.getTime())?void 0:t.toISOString()}function Ko(n){let e=Qo(n);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?cu:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?lu:e==="deepseek.com"||e.endsWith(".deepseek.com")?tu:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?nu:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?ou:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?ru:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?su:e==="maas.aliyuncs.com"||e.endsWith(".maas.aliyuncs.com")?iu:e==="xf-yun.com"||e.endsWith(".xf-yun.com")||e==="xfyun.cn"||e.endsWith(".xfyun.cn")||e==="xfyun.com"||e.endsWith(".xfyun.com")?au:null:null}function uu(n){let e=Qo(n);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function du(n,e){let t=Wo(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 Wo(n){return!n||typeof n!="object"?[]:!Array.isArray(n)&&n.codingPlanUsageDTO?[n]:Array.isArray(n)?n.flatMap(e=>Wo(e)):Object.values(n).flatMap(e=>Wo(e))}function Go(n,e){let t=n.match(new RegExp(`(?:^|;\\s*)${e}=([^;]+)`));return t?decodeURIComponent(t[1]):void 0}function Qo(n){try{return new URL(n).hostname.toLowerCase()}catch{return null}}function M(n){if(typeof n=="number")return Number.isFinite(n)?n:void 0;if(typeof n!="string")return;let e=n.trim().replace(/,/g,"");if(!e)return;let t=Number(e);return Number.isFinite(t)?t:void 0}var se,Lo,Do,Fo,Uo,jo,Ql,Vl,Yl,Xl,Ho,zo,Bo,Jo,tu,nu,ou,ru,su,iu,au,cu,lu,Vo=w(()=>{"use strict";oe();se=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=K(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}},Lo=class extends se{async queryQuota(e,t,o){let r=await this.fetchJson("https://api.deepseek.com/user/balance",e,t,o),i=(Array.isArray(r?.balance_infos)?r.balance_infos:[]).find(l=>M(l?.total_balance)!==void 0);if(!i)return null;let a=M(i.total_balance);return a===void 0?null:{totalBalance:a,currency:typeof i.currency=="string"?i.currency:void 0}}},Do=class extends se{async queryQuota(e,t,o){let s=(await this.fetchJson("https://openrouter.ai/api/v1/key",e,t,o))?.data;if(!s||typeof s!="object")return null;let i={},a=M(s.limit),l=M(s.limit_remaining),c=M(s.usage),u=M(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}},Fo=class extends se{async queryQuota(e,t,o){let r=uu(e.baseUrl),i=(await this.fetchJson(r,e,t,o))?.data;if(!i||typeof i!="object")return null;let a={},l=M(i.totalBalance),c=M(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}},Uo=class extends se{async queryQuota(e,t,o){if(!e.apiKey)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:e.apiKey.trim()},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=K(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=M(c.currentValue),y=M(c.usage),m=M(c.remaining),f=M(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=M(u.currentValue),y=M(u.usage),m=M(u.remaining),f=M(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=M(d.currentValue);g!==void 0&&(p.usedDailyBalance=g)}if(p.limitDaily===void 0){let g=M(d.usage),y=M(d.currentValue),m=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}}},jo=class extends se{async queryQuota(e,t,o){if(!e.quotaToken)return null;let r={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://bailian.console.aliyun.com",Referer:"https://bailian.console.aliyun.com/cn-beijing",Cookie:e.quotaToken},signal:AbortSignal.timeout(t)},s=JSON.stringify({Api:"zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",V:"1.0",Data:{queryCodingPlanInstanceInfoRequest:{commodityCode:"sfm_codingplan_public_cn",onlyLatestOne:!0},cornerstoneParam:{feTraceId:`ccr-${Date.now()}`,feURL:"https://bailian.console.aliyun.com/cn-beijing",protocol:"V2",console:"ONE_CONSOLE",productCode:"p_efm",switchAgent:10736808,switchUserType:3,domain:"bailian.console.aliyun.com",consoleSite:"BAILIAN_ALIYUN",userNickName:"",userPrincipalName:"",xsp_lang:"zh-CN"}}}),i=new URLSearchParams({params:s,region:"cn-beijing"}).toString();try{o&&(r.dispatcher=K(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=M(u.per5HourUsedQuota),g=M(u.per5HourTotalQuota);p!==void 0&&(d.usedDailyBalance=p),g!==void 0&&(d.limitDaily=g);let y=M(u.perWeekUsedQuota),m=M(u.perWeekTotalQuota);return y!==void 0&&(d.usedBalance=y),m!==void 0&&(d.totalBalance=m),this.hasQuotaData(d)?d:null}catch{return null}}};Ql=["per5HourPercentage","per5HourUsedPercentage","fiveHourPercentage"],Vl=["per1WeekPercentage","perWeekPercentage","per1WeekUsedPercentage"],Yl=["per5HourResetTime","per5HourNextResetTime","fiveHourResetTime"],Xl=["per1WeekResetTime","per1WeekNextResetTime","perWeekResetTime"];Ho=class extends se{async queryQuota(e,t,o){let r=e.quotaToken?.trim();if(!r)return null;let s=e.quotaSecToken?.trim();if(s){try{let i=await this.queryOfficialEndpoint(r,s,t,o);if(i)return i}catch{}return this.queryLegacyEndpoint(r,t,o)}return this.queryLegacyEndpoint(r,t,o)}async queryOfficialEndpoint(e,t,o,r){let s="zeldaHttp.apikeyMgr./tokenplan/personal/api/v2/usage",i=Go(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=K(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 Pi(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=K(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 Pi(c)}catch{return null}}},zo=class extends se{async queryQuota(e,t,o){if(!e.quotaToken)return null;let s={method:"GET",headers:{Accept:"application/json","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://maas.xfyun.cn",Referer:"https://maas.xfyun.cn/packageSubscription",Authorization:(Go(e.quotaToken,"atp-auth-token")||Go(e.quotaToken,"tenantToken")||e.quotaToken).trim(),Cookie:e.quotaToken},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=K(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=du(a,e.apiKey),c=l?.codingPlanUsageDTO||l?.usage||l;if(!c||typeof c!="object")return null;let u={},d=M(c.rp5hUsage),p=M(c.rp5hLimit);d!==void 0&&(u.usedDailyBalance=d),p!==void 0&&(u.limitDaily=p);let g=M(c.rpwUsage),y=M(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}}},Bo=class extends se{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${r.trim()}`},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=K(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=M(d.limit),g=M(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=M(u.limit),p=M(u.remaining),g=M(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}}},Jo=class extends se{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s=Qo(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=M(u.current_interval_total_count),g=M(u.current_interval_usage_count),y=M(u.current_interval_remaining_percent),m=M(u.end_time);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=M(u.current_weekly_total_count),_=M(u.current_weekly_usage_count),k=M(u.current_weekly_remaining_percent),h=M(u.weekly_end_time);return 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}}},tu=new Lo,nu=new Do,ou=new Fo,ru=new Uo,su=new jo,iu=new Ho,au=new zo,cu=new Bo,lu=new Jo});function Ii(n){return{[pu]:n,[fu]:Mt}}function gu(n){return n===!0||n==="true"||n===1||n==="1"}function hu(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 yu(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 _u(n){try{let e=new URL(n).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function Yo(n,e,t){let o=performance.now(),r=()=>Math.max(0,Math.round(performance.now()-o)),s=yu(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=K(t));let a=await fetch(s,i);if(a.headers&&et(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 ku(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",...Ii("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=K(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 bu(n,e,t,o,r){let s=n.baseUrl,i=n.baseUrl.includes("/messages")||e.includes("claude");try{let a={"Content-Type":"application/json",...Ii("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=K(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 Mi(n,e,t,o,r){return pe||(pe=new wn(n,e,t,o,r)),pe}function We(n,e,t,o,r){let s=Mi(n,e,t,o,r);return s.start(),s}function Xo(){pe&&pe.stop()}function En(){pe&&(pe.stop(),pe=null)}var pu,fu,mu,wn,pe,Pn=w(()=>{"use strict";re();we();Vo();Ie();to();oe();pu="x-claude-code-router-source",fu="x-claude-code-router-version";mu={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,slowThresholdMs:3e3,initialDelayMs:5e3,excludeProviders:[]};wn=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={...mu,...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=U();if(e.length===0)return;let o=[];for(let s of e){let i=Ko(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){vo(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`))}}})}),_u(s.baseUrl)&&o.push({provider:s.name,models:a,type:"rate-limit-headers",promise:Yo(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=U(),t=this.getProviders().filter(r=>r?.name&&r.enabled!==!1&&!this.config.excludeProviders.includes(r.name));if(t.length===0)return;this.logger?.debug?.(`Running health probe for ${t.length} providers`);let o=await Promise.allSettled(t.map(r=>Yo(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=U(),t=e.getRateLimitedModels();if(t.length===0)return;let o=this.getProviders(),r=this.config.probeTimeoutMs;this.logger?.debug?.(`Running rate-limit recovery probe for ${t.length} model(s)`);for(let s of t){let[i,...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 bu(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=hu(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}`),ku(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?gu(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 Yo(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)U().recordSuccess(o.name,a);else for(let a of i)U().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}}},pe=null});var $i=w(()=>{"use strict"});var Zo,pt,Ni=w(()=>{"use strict";Zo=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()}},pt=new Zo});var ae,qo=w(()=>{"use strict";ae=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,t)=>{this.buffer+=e;let o=this.buffer.split(`
|
|
305
|
-
`);this.buffer=o.pop()||"";for(let r of o){let s=this.processLine(r);s&&t.enqueue(s)}},flush:e=>{if(this.buffer.trim()){let t=[];this.processLine(this.buffer.trim(),t),t.forEach(o=>e.enqueue(o))}Object.keys(this.currentEvent).length>0&&e.enqueue(this.currentEvent)}})}processLine(e,t){if(!e.trim()){if(Object.keys(this.currentEvent).length>0){let o={...this.currentEvent};return this.currentEvent={},t?(t.push(o),null):o}return null}if(e.startsWith("event:"))this.currentEvent.event=e.slice(6).trim();else if(e.startsWith("data:")){let o=e.slice(5).trim();if(o==="[DONE]")this.currentEvent.data={type:"done"};else try{this.currentEvent.data=JSON.parse(o)}catch{this.currentEvent.data={raw:o,error:"JSON parse failed"}}}else e.startsWith("id:")?this.currentEvent.id=e.slice(3).trim():e.startsWith("retry:")&&(this.currentEvent.retry=parseInt(e.slice(6).trim()));return null}}});var
|
|
304
|
+
Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(Fi).forEach(e=>{if("TransformerName"in e&&typeof e.TransformerName=="string")this.registerTransformer(e.TransformerName,e);else{let t=new e;t&&typeof t=="object"&&(t.logger=this.logger),this.registerTransformer(t.name,t)}})}catch(e){this.logger.error({error:e},"transformer regist error:")}}async loadFromConfig(){let e=this.configService.get("transformers",[]);for(let t of e)await this.registerTransformerFromConfig(t)}}});import{get_encoding as fu}from"tiktoken";var lt,ji=E(()=>{"use strict";lt=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=fu(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)}}});import{join as xn}from"path";import{homedir as mu}from"os";import{existsSync as jo,mkdirSync as gu}from"fs";import{promises as Cn}from"fs";import{Tokenizer as hu}from"@huggingface/tokenizers";var Sn,Hi=E(()=>{"use strict";Sn=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||xn(mu(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=xn(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:xn(e,"tokenizer.json"),tokenizerConfig:xn(e,"tokenizer_config.json")}}ensureDir(e){jo(e)||gu(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!jo(e.tokenizerJson)||!jo(e.tokenizerConfig))return null;let[t,o]=await Promise.all([Cn.readFile(e.tokenizerJson,"utf-8"),Cn.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([Cn.writeFile(e.tokenizerJson,JSON.stringify(a,null,2)),Cn.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 hu(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 Rn,zi=E(()=>{"use strict";oe();Rn=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=G(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 Je,Ho=E(()=>{"use strict";ji();Hi();zi();oe();Je=class{tokenizers=new Map;configService;logger;options;fallbackTokenizer;constructor(e,t,o={}){this.configService=e,this.logger=t,this.options={timeout:o.timeout??3e4,...o}}async initialize(){try{this.fallbackTokenizer=new lt("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 lt(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),r=new Sn(e.model,this.logger,{timeout:this.options.timeout});break;case"api":r=new Rn(e,this.logger,{timeout:this.options.timeout,resolveProxyUrl:()=>{let s=t?this.getRawProvider(t):void 0;return ce(this.configService,s)}});break;default:throw new Error(`Unknown tokenizer type: ${e.type}`)}return this.logger?.info(`Calling initialize() on ${e.type} tokenizer...`),await r.initialize(),this.tokenizers.set(o,r),this.logger?.info(`Tokenizer initialized successfully: ${e.type} (${o})`),r}catch(s){return this.logger?.error(`Failed to initialize ${e.type} tokenizer: ${s.message}`),this.logger?.error(`Error stack: ${s.stack}`),this.fallbackTokenizer||await this.initialize(),this.fallbackTokenizer}}async countTokens(e,t,o){let r=t?await this.getTokenizer(t,o):this.fallbackTokenizer;return{tokenCount:await r.countTokens(e),tokenizerUsed:r.name,cached:!1}}getTokenizerConfigForModel(e,t){let r=(this.configService.get("providers")||[]).find(s=>s.name===e);if(r?.tokenizer)return r.tokenizer.models?.[t]?r.tokenizer.models[t]:r.tokenizer.default}dispose(){this.tokenizers.forEach(e=>{try{e.dispose()}catch(t){this.logger?.error(`Error disposing tokenizer: ${t}`)}}),this.tokenizers.clear()}getCacheKey(e,t){switch(e.type){case"tiktoken":return`tiktoken:${e.encoding||"cl100k_base"}`;case"huggingface":return`hf:${e.model}`;case"api":return`api:${t||"unscoped"}:${e.url}`;default:return`unknown:${JSON.stringify(e)}`}}getRawProvider(e){return(this.configService.get("providers")||[]).find(o=>o?.name===e)}}});function Bi(n){if(!n||typeof n!="object"||xu(n))return null;for(let t of vu(n)){let o=Tu(t);if(o)return o}return null}function Tu(n){if(!n||typeof n!="object")return null;let e={},t=wn(n,yu);if(t!==void 0){let i=Ji(t);e.usedDailyBalance=i,e.limitDaily=100}let o=wn(n,_u);if(o!==void 0){let i=Ji(o);e.usedBalance=i,e.totalBalance=100}let r=wn(n,ku);if(r!==void 0){let i=Wi(r);i&&(e.resetTime=i)}let s=wn(n,bu);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 wn(n,e){for(let t of e){let o=I(n?.[t]);if(o!==void 0)return o}}function vu(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 xu(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 qo(n){let e=er(n);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?Au:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?Iu:e==="deepseek.com"||e.endsWith(".deepseek.com")?Cu:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?Su:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?Ru:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?wu:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?Eu:e==="maas.aliyuncs.com"||e.endsWith(".maas.aliyuncs.com")?Pu:e==="xf-yun.com"||e.endsWith(".xf-yun.com")||e==="xfyun.cn"||e.endsWith(".xfyun.cn")||e==="xfyun.com"||e.endsWith(".xfyun.com")?Ou:null:null}function Mu(n){let e=er(n);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function Nu(n,e){let t=Xo(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 Xo(n){return!n||typeof n!="object"?[]:!Array.isArray(n)&&n.codingPlanUsageDTO?[n]:Array.isArray(n)?n.flatMap(e=>Xo(e)):Object.values(n).flatMap(e=>Xo(e))}function Zo(n,e){let t=n.match(new RegExp(`(?:^|;\\s*)${e}=([^;]+)`));return t?decodeURIComponent(t[1]):void 0}function er(n){try{return new URL(n).hostname.toLowerCase()}catch{return null}}function I(n){if(typeof n=="number")return Number.isFinite(n)?n:void 0;if(typeof n!="string")return;let e=n.trim().replace(/,/g,"");if(!e)return;let t=Number(e);return Number.isFinite(t)?t:void 0}var se,zo,Bo,Jo,Wo,Ko,yu,_u,ku,bu,Go,Vo,Qo,Yo,Cu,Su,Ru,wu,Eu,Pu,Ou,Au,Iu,tr=E(()=>{"use strict";oe();se=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=G(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}},zo=class extends se{async queryQuota(e,t,o){let r=await this.fetchJson("https://api.deepseek.com/user/balance",e,t,o),i=(Array.isArray(r?.balance_infos)?r.balance_infos:[]).find(l=>I(l?.total_balance)!==void 0);if(!i)return null;let a=I(i.total_balance);return a===void 0?null:{totalBalance:a,currency:typeof i.currency=="string"?i.currency:void 0}}},Bo=class extends se{async queryQuota(e,t,o){let s=(await this.fetchJson("https://openrouter.ai/api/v1/key",e,t,o))?.data;if(!s||typeof s!="object")return null;let i={},a=I(s.limit),l=I(s.limit_remaining),c=I(s.usage),u=I(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}},Jo=class extends se{async queryQuota(e,t,o){let r=Mu(e.baseUrl),i=(await this.fetchJson(r,e,t,o))?.data;if(!i||typeof i!="object")return null;let a={},l=I(i.totalBalance),c=I(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}},Wo=class extends se{async queryQuota(e,t,o){if(!e.apiKey)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:e.apiKey.trim()},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://api.z.ai/api/monitor/usage/quota/limit",s);if(!i.ok)return null;let a=await i.json(),l=Array.isArray(a?.data?.limits)?a.data.limits:[],c=null,u=null,d=null;for(let m of l)m.type==="TOKENS_LIMIT"?m.unit===3?c=m:m.unit===6?u=m:c||(c=m):m.type==="TIME_LIMIT"&&(d=m);let p={};if(c){let m=I(c.currentValue),h=I(c.usage),g=I(c.remaining),f=I(c.percentage);m!==void 0?p.usedDailyBalance=m:f!==void 0&&(p.usedDailyBalance=f,p.limitDaily=100),h!==void 0?p.limitDaily=h:m!==void 0&&g!==void 0&&(p.limitDaily=m+g),c.nextResetTime&&(p.resetTime=new Date(c.nextResetTime).toISOString())}if(u){let m=I(u.currentValue),h=I(u.usage),g=I(u.remaining),f=I(u.percentage);m!==void 0?p.usedBalance=m:f!==void 0&&(p.usedBalance=f,p.totalBalance=100),h!==void 0?p.totalBalance=h:m!==void 0&&g!==void 0&&(p.totalBalance=m+g),u.nextResetTime&&!p.resetTime&&(p.resetTime=new Date(u.nextResetTime).toISOString())}if(d){if(p.usedDailyBalance===void 0){let m=I(d.currentValue);m!==void 0&&(p.usedDailyBalance=m)}if(p.limitDaily===void 0){let m=I(d.usage),h=I(d.currentValue),g=I(d.remaining);m!==void 0?p.limitDaily=m:h!==void 0&&g!==void 0&&(p.limitDaily=h+g)}d.nextResetTime&&!p.resetTime&&(p.resetTime=new Date(d.nextResetTime).toISOString())}return this.hasQuotaData(p)?p:null}catch{return null}}},Ko=class extends se{async queryQuota(e,t,o){if(!e.quotaToken)return null;let r={method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://bailian.console.aliyun.com",Referer:"https://bailian.console.aliyun.com/cn-beijing",Cookie:e.quotaToken},signal:AbortSignal.timeout(t)},s=JSON.stringify({Api:"zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",V:"1.0",Data:{queryCodingPlanInstanceInfoRequest:{commodityCode:"sfm_codingplan_public_cn",onlyLatestOne:!0},cornerstoneParam:{feTraceId:`ccr-${Date.now()}`,feURL:"https://bailian.console.aliyun.com/cn-beijing",protocol:"V2",console:"ONE_CONSOLE",productCode:"p_efm",switchAgent:10736808,switchUserType:3,domain:"bailian.console.aliyun.com",consoleSite:"BAILIAN_ALIYUN",userNickName:"",userPrincipalName:"",xsp_lang:"zh-CN"}}}),i=new URLSearchParams({params:s,region:"cn-beijing"}).toString();try{o&&(r.dispatcher=G(o));let 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=I(u.per5HourUsedQuota),m=I(u.per5HourTotalQuota);p!==void 0&&(d.usedDailyBalance=p),m!==void 0&&(d.limitDaily=m);let h=I(u.perWeekUsedQuota),g=I(u.perWeekTotalQuota);return h!==void 0&&(d.usedBalance=h),g!==void 0&&(d.totalBalance=g),this.hasQuotaData(d)?d:null}catch{return null}}};yu=["per5HourPercentage","per5HourUsedPercentage","fiveHourPercentage"],_u=["per1WeekPercentage","perWeekPercentage","per1WeekUsedPercentage"],ku=["per5HourResetTime","per5HourNextResetTime","fiveHourResetTime"],bu=["per1WeekResetTime","per1WeekNextResetTime","perWeekResetTime"];Go=class extends se{async queryQuota(e,t,o){let r=e.quotaToken?.trim();if(!r)return null;let s=e.quotaSecToken?.trim();if(s){try{let i=await this.queryOfficialEndpoint(r,s,t,o);if(i)return i}catch{}return this.queryLegacyEndpoint(r,t,o)}return this.queryLegacyEndpoint(r,t,o)}async queryOfficialEndpoint(e,t,o,r){let s="zeldaHttp.apikeyMgr./tokenplan/personal/api/v2/usage",i=Zo(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=G(r));let u=await fetch(`https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=${encodeURIComponent(s)}&_v=undefined`,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=G(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}}},Vo=class extends se{async queryQuota(e,t,o){if(!e.quotaToken)return null;let s={method:"GET",headers:{Accept:"application/json","User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",Origin:"https://maas.xfyun.cn",Referer:"https://maas.xfyun.cn/packageSubscription",Authorization:(Zo(e.quotaToken,"atp-auth-token")||Zo(e.quotaToken,"tenantToken")||e.quotaToken).trim(),Cookie:e.quotaToken},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://maas.xfyun.cn/api/v1/gpt-finetune/coding-plan/list?page=1&size=10",s);if(!i.ok)return null;let a=await i.json();if(a?.succeed===!1||a?.failed===!0)return null;let l=Nu(a,e.apiKey),c=l?.codingPlanUsageDTO||l?.usage||l;if(!c||typeof c!="object")return null;let u={},d=I(c.rp5hUsage),p=I(c.rp5hLimit);d!==void 0&&(u.usedDailyBalance=d),p!==void 0&&(u.limitDaily=p);let m=I(c.rpwUsage),h=I(c.rpwLimit);return m!==void 0&&(u.usedBalance=m),h!==void 0&&(u.totalBalance=h),u.remainingBalance===void 0&&h!==void 0&&m!==void 0&&(u.remainingBalance=Math.max(0,h-m)),this.hasQuotaData(u)?u:null}catch{return null}}},Qo=class extends se{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${r.trim()}`},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=G(o));let i=await fetch("https://api.kimi.com/coding/v1/usages",s);if(!i.ok)return null;let 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=I(d.limit),m=I(d.remaining),h=d.resetTime;p!==void 0&&m!==void 0&&(l.usedDailyBalance=Math.max(0,p-m),l.limitDaily=p),h&&(l.resetTime=new Date(h).toISOString())}let u=a?.usage;if(u){let d=I(u.limit),p=I(u.remaining),m=I(u.used),h=u.resetTime;d!==void 0&&(l.totalBalance=d,m!==void 0?l.usedBalance=m:p!==void 0&&(l.usedBalance=Math.max(0,d-p),l.remainingBalance=p)),h&&(l.resetTime7d=new Date(h).toISOString(),l.resetTime||(l.resetTime=l.resetTime7d))}return this.hasQuotaData(l)?l:null}catch{return null}}},Yo=class extends se{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s=er(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=I(u.current_interval_total_count),m=I(u.current_interval_usage_count),h=I(u.current_interval_remaining_percent),g=I(u.end_time);p!==void 0&&m!==void 0&&p>0?(d.usedDailyBalance=Math.max(0,p-m),d.limitDaily=p):h!==void 0&&(d.limitDaily=100,d.usedDailyBalance=Math.max(0,100-h)),g&&(d.resetTime=new Date(g).toISOString());let f=I(u.current_weekly_total_count),_=I(u.current_weekly_usage_count),k=I(u.current_weekly_remaining_percent),y=I(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),y&&!d.resetTime&&(d.resetTime=new Date(y).toISOString()),this.hasQuotaData(d)?d:null}catch{return null}}},Cu=new zo,Su=new Bo,Ru=new Jo,wu=new Wo,Eu=new Ko,Pu=new Go,Ou=new Vo,Au=new Qo,Iu=new Yo});function Ki(n){return{[$u]:n,[Lu]:It}}function Fu(n){return n===!0||n==="true"||n===1||n==="1"}function Uu(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 ju(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 Hu(n){try{let e=new URL(n).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function nr(n,e,t){let o=performance.now(),r=()=>Math.max(0,Math.round(performance.now()-o)),s=ju(n.baseUrl);if(!s)return{success:!1,error:"Cannot derive models endpoint from baseUrl",errorKind:"network",latencyMs:r()};try{let i={method:"GET",headers:{Authorization:`Bearer ${n.apiKey}`},signal:AbortSignal.timeout(e)};t&&(i.dispatcher=G(t));let a=await fetch(s,i);if(a.headers&&qe(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 zu(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",...Ki("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=G(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 Bu(n,e,t,o,r){let s=n.baseUrl,i=n.baseUrl.includes("/messages")||e.includes("claude");try{let a={"Content-Type":"application/json",...Ki("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=G(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 Gi(n,e,t,o,r){return ye||(ye=new En(n,e,t,o,r)),ye}function We(n,e,t,o,r){let s=Gi(n,e,t,o,r);return s.start(),s}function or(){ye&&ye.stop()}function Pn(){ye&&(ye.stop(),ye=null)}var $u,Lu,Du,En,ye,On=E(()=>{"use strict";re();Oe();tr();Me();ao();oe();$u="x-claude-code-router-source",Lu="x-claude-code-router-version";Du={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,slowThresholdMs:3e3,initialDelayMs:5e3,excludeProviders:[]};En=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={...Du,...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=U();if(e.length===0)return;let o=[];for(let s of e){let i=qo(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){Eo(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 m of a){if(t.getState(s.name,m)?.rateLimitUntil){this.logger?.debug?.(`Skip forceOpen for ${s.name} (${m}): already rate-limited with active cooldown`);continue}t.forceOpen(s.name,m,p)}this.logger?.warn?.(`${p} for ${s.name}, marked as unhealthy`)}else for(let p of a){let m=t.getState(s.name,p);m&&m.status==="open"&&m.lastError?.includes("Quota exhausted")&&(t.recover(s.name,p),this.logger?.info?.(`Quota recovered for ${s.name} (${p}), marked as healthy`))}}})}),Hu(s.baseUrl)&&o.push({provider:s.name,models:a,type:"rate-limit-headers",promise:nr(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=U(),t=this.getProviders().filter(r=>r?.name&&r.enabled!==!1&&!this.config.excludeProviders.includes(r.name));if(t.length===0)return;this.logger?.debug?.(`Running health probe for ${t.length} providers`);let o=await Promise.allSettled(t.map(r=>nr(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"),m=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(m||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=U(),t=e.getRateLimitedModels();if(t.length===0)return;let o=this.getProviders(),r=this.config.probeTimeoutMs;this.logger?.debug?.(`Running rate-limit recovery probe for ${t.length} model(s)`);for(let s of t){let[i,...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 Bu(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=Uu(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 m=o>d||o===d&&r>=p,h=(d*60+p)*6e4-(o*60+r)*6e4,g=h>0&&h<=300*1e3;(m||g)&&!this.hasWakeupFiredToday(a.name,s)&&(this.logger?.info?.(`Scheduled wake-up triggered for provider ${a.name} at ${l}`),zu(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?Fu(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 nr(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)U().recordSuccess(o.name,a);else for(let a of i)U().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}}},ye=null});var Vi=E(()=>{"use strict"});var rr,ut,Qi=E(()=>{"use strict";rr=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()}},ut=new rr});var ae,sr=E(()=>{"use strict";ae=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,t)=>{this.buffer+=e;let o=this.buffer.split(`
|
|
305
|
+
`);this.buffer=o.pop()||"";for(let r of o){let s=this.processLine(r);s&&t.enqueue(s)}},flush:e=>{if(this.buffer.trim()){let t=[];this.processLine(this.buffer.trim(),t),t.forEach(o=>e.enqueue(o))}Object.keys(this.currentEvent).length>0&&e.enqueue(this.currentEvent)}})}processLine(e,t){if(!e.trim()){if(Object.keys(this.currentEvent).length>0){let o={...this.currentEvent};return this.currentEvent={},t?(t.push(o),null):o}return null}if(e.startsWith("event:"))this.currentEvent.event=e.slice(6).trim();else if(e.startsWith("data:")){let o=e.slice(5).trim();if(o==="[DONE]")this.currentEvent.data={type:"done"};else try{this.currentEvent.data=JSON.parse(o)}catch{this.currentEvent.data={raw:o,error:"JSON parse failed"}}}else e.startsWith("id:")?this.currentEvent.id=e.slice(3).trim():e.startsWith("retry:")&&(this.currentEvent.retry=parseInt(e.slice(6).trim()));return null}}});var Ke,ir=E(()=>{"use strict";Ke=class extends TransformStream{constructor(){super({transform:(e,t)=>{let o="";e.event&&(o+=`event: ${e.event}
|
|
306
306
|
`),e.id&&(o+=`id: ${e.id}
|
|
307
307
|
`),e.retry&&(o+=`retry: ${e.retry}
|
|
308
308
|
`),e.data&&(e.data.type==="done"?o+=`data: [DONE]
|
|
309
309
|
`:o+=`data: ${JSON.stringify(e.data)}
|
|
310
310
|
`),o+=`
|
|
311
|
-
`,t.enqueue(o)}})}}});var
|
|
311
|
+
`,t.enqueue(o)}})}}});var An,ar=E(()=>{"use strict";An=(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 cr=E(()=>{"use strict";sr();ir();ar()});var Yi=E(()=>{"use strict"});var dt,lr=E(()=>{"use strict";dt=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+=`
|
|
312
312
|
`+(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}-
|
|
313
313
|
${this.toMarkdown(r,t+1)}`:`${o}- ${r}`).join(`
|
|
314
314
|
`):typeof e=="object"&&e!==null?Object.entries(e).map(([r,s])=>typeof s=="object"&&s!==null?`${o}${r}:
|
|
315
315
|
${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
316
|
-
`):`${o}${e}`}async output(e,t={}){try{let o=this.formatData(e,t);switch(this.config.level||"log"){case"info":console.info(o);break;case"warn":console.warn(o);break;case"error":console.error(o);break;case"debug":console.debug(o);break;case"log":default:console.log(o);break}return!0}catch(o){return console.error("[ConsoleOutputHandler] Output failed:",o),!1}}}});var mt,rr=w(()=>{"use strict";mt=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}`)}}}});import{writeFileSync as Tu,existsSync as vu,mkdirSync as xu}from"fs";import{join as Di}from"path";import{tmpdir as Cu}from"os";var gt,sr=w(()=>{"use strict";gt=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let t=Cu();this.baseDir=Di(t,this.config.subdirectory),this.ensureDir()}ensureDir(){try{vu(this.baseDir)||xu(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 Di(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 Tu(s,JSON.stringify(r,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var ir,Ke,Fi=w(()=>{"use strict";or();rr();sr();ir=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 ft(e.config);case"webhook":return new mt(e.config);case"temp-file":return new gt(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)}},Ke=new ir});var ar=w(()=>{"use strict";Li();or();rr();sr();Fi()});import Su from"fastify-plugin";function Ui(n,e,t=0){if(n<=0)return 0;let o=Math.max((e-t)/1e3,Ru);return Math.round(n/o)}function yt(n){let e=(n.match(/[\u4e00-\u9fa5]/g)||[]).length,t=n.length-e;return Math.ceil(e/1.5+t/4)}async function ji(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 Ke.outputToType(a,s,i)}catch(l){console.error(`[TokenSpeedPlugin] Failed to output to ${a}:`,l)}}var ht,cr,Ru,An,Hi=w(()=>{"use strict";nr();ar();$e();ht=new Map,cr=new Map,Ru=1,An={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:Su(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)Ke.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&&Ke.registerHandlers(i)}t.outputOptions&&Ke.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(cr.has(u))return cr.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 cr.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=_e(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){ht.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=ht.get(u);if(!C)return;let x=C.firstTokenTime?C.firstTokenTime-C.startTime:0,T=(v?C.lastTokenTime:performance.now())-C.startTime;C.tokensPerSecond=Ui(C.tokenCount,T,x),await ji(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 ae).getReader();for(h=setInterval(async()=>{ht.get(u)&&await b(!1)},1e3);;){let{done:x,value:S}=await C.read();if(x)break;let T=S,E=ht.get(u);if(!E)continue;let R=performance.now(),O=T.event||T.data?.type,P=T.event==="content_block_start"||T.event==="content_block_delta"||T.event==="text_block"||T.event==="content_block"||O==="response.output_text.delta"||O==="response.function_call_arguments.delta"||O==="response.reasoning_summary_text.delta";!E.firstTokenTime&&P&&(E.firstTokenTime=R,E.timeToFirstToken=Math.round(R-E.startTime));let A="";if(T.event==="content_block_delta"&&T.data?.delta){let N=T.data.delta.type;N==="text_delta"?A=T.data.delta.text||"":N==="input_json_delta"?A=T.data.delta.partial_json||"":N==="thinking_delta"&&(A=T.data.delta.thinking||"")}if((O==="response.output_text.delta"||O==="response.function_call_arguments.delta"||O==="response.reasoning_summary_text.delta")&&(A=typeof T.data?.delta=="string"?T.data.delta:""),A){let N=p&&p.encodeText?p.encodeText(A).length:yt(A);E.tokenCount+=N,E.lastTokenTime=R}(T.event==="message_stop"||O==="response.completed"||O==="response.failed")&&(h&&(clearInterval(h),h=null),await b(!0),ht.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:yt(b))}return k},0):typeof _=="string"&&(y=p.encodeText?p.encodeText(_).length:yt(_));else{let k=Array.isArray(_)?_.map(h=>h.text).join(""):typeof _=="string"?_:"";y=yt(k)}}}catch{}if(m){let f=g-c,_={requestId:u,sessionId:d,startTime:c,lastTokenTime:g,tokenCount:y,tokensPerSecond:Ui(y,f),stream:!1};await ji(_,o,t.outputOptions,!0)}return l})})}});var lr=w(()=>{"use strict";$i();Ni();Hi();ar()});import ee from"node:fs/promises";import zi from"json5";import Te from"node:path";import{CONFIG_FILE as te,HOME_DIR as In,PLUGINS_DIR as wu}from"@wengine-ai/claude-code-router-shared";function Bi(){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 ur,_t,Mn,dr,ne,$n,fe,Eu,pr,Nn=w(()=>{"use strict";ur=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(ur);if(n!==null&&typeof n=="object"){let e={};for(let[t,o]of Object.entries(n))e[t]=ur(o);return e}return n},_t=async n=>{try{await ee.access(n)}catch{await ee.mkdir(n,{recursive:!0})}},Mn=async()=>{await _t(In),await _t(wu),await _t(Te.join(In,"logs"))},dr=async()=>{let n=await ee.readFile(te,"utf-8");return zi.parse(n)},ne=async()=>{try{let n=await ee.readFile(te,"utf-8");try{let e=zi.parse(n);return ur(e)}catch(e){console.error(`Failed to parse config file at ${te}`),console.error("Error details:",e.message),console.error("Please check your config file syntax."),process.exit(1)}}catch(n){if(n.code==="ENOENT")try{await Mn();let e=await $n();e&&console.log(`Backed up existing configuration file to ${e}`);let t={PORT:3456,Providers:[],Router:{}};return await fe(t),console.log("Created minimal default configuration file at ~/.claude-code-router/config.json"),console.log("Please edit this file with your actual configuration."),t}catch(e){console.error("Failed to create default configuration:",e.message),process.exit(1)}else console.error(`Failed to read config file at ${te}`),console.error("Error details:",n.message),process.exit(1)}};$n=async()=>{Bi();try{if(await ee.access(te).then(()=>!0).catch(()=>!1)){let n=new Date().toISOString().replace(/[:.]/g,"-"),e=`${te}.${n}.bak`;await ee.copyFile(te,e);try{let t=Te.dirname(te),o=Te.basename(te),s=(await ee.readdir(t)).filter(i=>i.startsWith(o)&&i.endsWith(".bak")).sort().reverse();if(s.length>3)for(let i=3;i<s.length;i++){let a=Te.join(t,s[i]);await ee.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},fe=async n=>{Bi(),await _t(In),await Eu();let e=`${JSON.stringify(n,null,2)}`;await ee.writeFile(te,e)},Eu=async()=>{if(!await ee.access(te).then(()=>!0).catch(()=>!1))return;let e=Te.join(In,"config-history");await _t(e);let t=new Date().toISOString().replace(/[:.]/g,"-"),o=Te.join(e,`config-${t}.json`);await ee.copyFile(te,o);try{let s=(await ee.readdir(e)).filter(i=>i.startsWith("config-")&&i.endsWith(".json")).sort().reverse();if(s.length>50)for(let i of s.slice(50))await ee.unlink(Te.join(e,i))}catch(r){console.warn("Failed to prune config-history:",r)}},pr=async()=>{let n=await ne();return Object.assign(process.env,n),n}});function Pu(n){return n?.cache_read_input_tokens??n?.input_tokens_details?.cached_tokens??n?.prompt_tokens_details?.cached_tokens??0}function Ou(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 Qe(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:Pu(n),cache_creation_input_tokens:Ou(n)}}function Ln(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 fr=w(()=>{"use strict"});function Dn(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(mr(r,s))}return}if(Array.isArray(n)){for(let o of n)Dn(o,e,t);return}if(typeof n=="object")for(let o of Object.values(n))Dn(o,e,t)}}function Ji(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(mr(s,a))}return Dn(n?.Router,t,o),Dn(n?.fallback,t,o),o}function kt(n,e){try{let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=Ji(n),r=0;for(let s of t.getAllStates()||[]){let i=s?.provider,a=s?.model;!i||!a||o.has(mr(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 Fn(n,e){try{if(!n)return 0;let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=0;for(let r of t.getAllStates()||[])r?.provider===n&&r?.model&&(t.recover(n,r.model),o++);return o>0&&e?.info?.(`[health-reconcile] Cleared ${o} health entr${o===1?"y":"ies"} for probed provider ${n}`),o}catch(t){return e?.warn?.(`[health-reconcile] Failed to clear provider health for ${n}: ${t?.message||t}`),0}}var Au,mr,Un=w(()=>{"use strict";re();Au="\0",mr=(n,e)=>`${n}${Au}${e}`});function Iu(n){return/\[1m\]/i.test(n)?1e6:2e5}function Mu(n){let e=n.toLowerCase();return e.includes("haiku")||e.includes("mini")?8192:32e3}function $u(n,e=n){let t=Iu(n),o=Mu(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 jn(n){let e=new Map,t=(o,r=o)=>{!o||e.has(o)||e.set(o,$u(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 gr=w(()=>{"use strict"});function Nu(n){let e=n.ip||n.connection?.remoteAddress||"";return e==="127.0.0.1"||e==="::1"||e==="::ffff:127.0.0.1"}var Hn,hr=w(()=>{"use strict";Hn=n=>async(e,t,o)=>{if(["/","/health"].includes(e.url)||e.url.startsWith("/ui")||Nu(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 Zi={};Yn(Zi,{append:()=>xt,clear:()=>vr,close:()=>nd,getOldestRecordTimestamp:()=>od,query:()=>Tr,querySummary:()=>Ct,readTokenSpeedStats:()=>xr});import{existsSync as kr,mkdirSync as Lu,readFileSync as _r,readdirSync as Du}from"fs";import{join as Ye}from"path";import{tmpdir as Fu}from"os";import Uu from"better-sqlite3";import{HOME_DIR as ju}from"@wengine-ai/claude-code-router-shared";function Wu(){kr(Bn)||Lu(Bn,{recursive:!0})}function vt(){if(ge)return ge;Wu();let n=new Uu(Hu);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&&(Gu(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")),Qu(n),Vi(n,!0),br=n.prepare(`
|
|
316
|
+
`):`${o}${e}`}async output(e,t={}){try{let o=this.formatData(e,t);switch(this.config.level||"log"){case"info":console.info(o);break;case"warn":console.warn(o);break;case"error":console.error(o);break;case"debug":console.debug(o);break;case"log":default:console.log(o);break}return!0}catch(o){return console.error("[ConsoleOutputHandler] Output failed:",o),!1}}}});var pt,ur=E(()=>{"use strict";pt=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}`)}}}});import{lstat as Mn,opendir as Ju,unlink as Wu}from"node:fs/promises";import{join as Zi,resolve as fr}from"node:path";import{tmpdir as Ku}from"node:os";async function Nn(n,e){let t=fr(n),o=In.get(t),r,s=new Promise(i=>{r=i});In.set(t,s);try{return await o,await e()}finally{r(),In.get(t)===s&&In.delete(t)}}function qi(n,e){return n.dev===e.dev&&n.ino===e.ino}async function Xi(n,e){try{let t=await Mn(n);return t.isDirectory()&&!t.isSymbolicLink()&&qi(e,t)}catch{return!1}}function Xu(){return new Promise(n=>{setTimeout(n,10).unref()})}async function ea(n,e,t,o){let r=fr(n);if(o?.aborted||dr.has(r))return 0;dr.add(r);let s=0;try{let i=await Mn(r);if(!i.isDirectory()||i.isSymbolicLink()||o?.aborted)return 0;let a=await Ju(r,{bufferSize:32});try{let l=0,c=!0;for await(let u of a){if(o?.aborted||!c)break;try{if(!u.isFile()||u.isSymbolicLink()||!t(u.name))continue;let d=Zi(r,u.name);try{let p=await Mn(d);if(!p.isFile()||p.isSymbolicLink()||p.mtimeMs>=e)continue;await Nn(d,async()=>{if(o?.aborted||!await Xi(r,i))return!1;let h=await Mn(d);return!h.isFile()||h.isSymbolicLink()||!qi(p,h)||h.mtimeMs!==p.mtimeMs||h.mtimeMs>=e||o?.aborted?!1:(await Wu(d),!0)})&&s++}catch{}}finally{++l%64===0&&(await Xu(),c=!o?.aborted&&await Xi(r,i))}}}finally{await a.close().catch(()=>{})}}catch{}finally{dr.delete(r)}return s}function ta(n,e,t,o,r){if(!Number.isFinite(o)||o<=0)return Promise.resolve(0);let s=`${e}-`,i=t?`.${t}`:"";return ea(n,Date.now()-o,a=>Yu.test(a)?!0:!a.endsWith(".tmp")&&a.startsWith(s)&&a.endsWith(i),r)}function Zu(n,e){let t=Date.now()-Gu;return ea(n,t,o=>{let r=Qu.exec(o);return r!==null&&Number(r[1])<t},e)}function mr(n,e,t){let o=pr.get(n);if(!o){let s=new AbortController,i,a=()=>{s.signal.aborted||Promise.resolve().then(()=>e(s.signal)).catch(()=>{})},l=setTimeout(()=>{s.signal.aborted||(a(),i=setInterval(a,Vu),i.unref())},t);l.unref(),o={users:0,stop:()=>{s.abort(),clearTimeout(l),i&&clearInterval(i)}},pr.set(n,o)}o.users++;let r=!1;return()=>{r||(r=!0,--o.users===0&&(o.stop(),pr.delete(n)))}}function na(n=Zi(Ku(),"claude-code-router")){return mr(`legacy:${fr(n)}`,e=>Zu(n,e),1e3)}var Gu,Vu,Qu,Yu,dr,In,pr,gr=E(()=>{"use strict";Gu=3600*1e3,Vu=600*1e3,Qu=/^session-[A-Za-z0-9_.-]+-(\d{13})\.json$/,Yu=/^\.ccr-tmp-[1-9]\d*-[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\.tmp$/,dr=new Set,In=new Map,pr=new Map});import{lstat as qu,mkdir as ed,open as td,rename as nd,unlink as od}from"node:fs/promises";import{randomUUID as rd}from"node:crypto";import{sanitizeTempFileKey as sd}from"@wengine-ai/claude-code-router-shared";import{basename as id,join as hr,resolve as ad}from"node:path";import{tmpdir as cd}from"node:os";var ft,yr=E(()=>{"use strict";gr();ft=class{type="temp-file";config;baseDir;controller=new AbortController;stopSchedule;constructor(e={}){if(this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",retentionMs:36e5,...e},this.baseDir=ad(hr(cd(),this.config.subdirectory)),Number.isFinite(this.config.retentionMs)&&this.config.retentionMs>0){let t=JSON.stringify([this.baseDir,this.config.prefix,this.config.extension,this.config.retentionMs]);this.stopSchedule=mr(`handler:${t}`,o=>this.pruneOldFiles(o),6e4)}}async ensureDir(){await ed(this.baseDir,{recursive:!0});let e=await qu(this.baseDir);if(!e.isDirectory()||e.isSymbolicLink())throw new Error("Unsafe temp output directory")}getFilePath(e){let t=this.config.prefix||"session",o=this.config.extension?`.${this.config.extension}`:"",r=this.config.includeTimestamp?`-${Date.now()}`:"",s=`${t}-${sd(e)}${r}${o}`;if(id(s)!==s||s.includes("\\"))throw new Error("Temp file configuration must not contain path separators");return hr(this.baseDir,s)}async output(e,t={}){try{let o=t.metadata?.sessionId;if(!o)return!1;let r=JSON.stringify({...e,timestamp:Date.now(),sessionId:o},null,2),s=this.getFilePath(o);return await Nn(s,async()=>{await this.ensureDir();let i=hr(this.baseDir,`.ccr-tmp-${process.pid}-${rd()}.tmp`);return Nn(i,async()=>{let a=!1;try{let l=await td(i,"wx");a=!0;try{await l.writeFile(r,"utf-8")}finally{await l.close()}return await nd(i,s),!0}finally{a&&await od(i).catch(()=>{})}})})}catch{return!1}}pruneOldFiles(e=this.controller.signal){return ta(this.baseDir,this.config.prefix||"session",this.config.extension||"",this.config.retentionMs,e)}dispose(){this.controller.abort(),this.stopSchedule?.()}getBaseDir(){return this.baseDir}}});var _r,Ge,oa=E(()=>{"use strict";lr();ur();yr();_r=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 dt(e.config);case"webhook":return new pt(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)}},Ge=new _r});var kr=E(()=>{"use strict";Yi();lr();ur();yr();oa()});import ld from"fastify-plugin";import{TOKEN_SPEED_TEMP_SUBDIRECTORY as ud}from"@wengine-ai/claude-code-router-shared";function $n(){return{subdirectory:ud,extension:"json",includeTimestamp:!1,prefix:"session"}}function ra(n,e,t=0){if(n<=0)return 0;let o=Math.max((e-t)/1e3,dd);return Math.round(n/o)}function gt(n){let e=(n.match(/[\u4e00-\u9fa5]/g)||[]).length,t=n.length-e;return Math.ceil(e/1.5+t/4)}async function sa(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,statsKey:n.statsKey},...t};for(let a of e)try{await Ge.outputToType(a,s,i)}catch(l){console.error(`[TokenSpeedPlugin] Failed to output to ${a}:`,l)}}var mt,br,dd,Ln,ia=E(()=>{"use strict";cr();kr();Ne();mt=new Map,br=new Map,dd=1,Ln={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:ld(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)Ge.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:$n()}):a==="webhook"&&console.warn("[TokenSpeedPlugin] Webhook reporter requires explicit configuration in outputHandlers");i.length>0&&Ge.registerHandlers(i)}t.outputOptions&&Ge.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(br.has(u))return br.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 br.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||u,p=i.sessionId;if(!p)try{p=ge(i.body?.metadata?.user_id)||d}catch{p=d}let m=await r(i);if(l instanceof ReadableStream){mt.set(u,{requestId:u,sessionId:p,statsKey:d,startTime:c,lastTokenTime:c,tokenCount:0,tokensPerSecond:0,stream:!0});let[_,k]=l.tee(),y=async()=>{let b=null,T=async C=>{let v=mt.get(u);if(!v)return;let S=v.firstTokenTime?v.firstTokenTime-v.startTime:0,w=(C?v.lastTokenTime:performance.now())-v.startTime;v.tokensPerSecond=ra(v.tokenCount,w,S),await sa(v,o,t.outputOptions,C).catch(R=>{n.log?.warn(`Failed to output streaming stats: ${R.message}`)})};try{let v=k.pipeThrough(new TextDecoderStream).pipeThrough(new ae).getReader();for(b=setInterval(async()=>{mt.get(u)&&await T(!1)},1e3);;){let{done:S,value:x}=await v.read();if(S)break;let w=x,R=mt.get(u);if(!R)continue;let O=performance.now(),P=w.event||w.data?.type,M=w.event==="content_block_start"||w.event==="content_block_delta"||w.event==="text_block"||w.event==="content_block"||P==="response.output_text.delta"||P==="response.function_call_arguments.delta"||P==="response.reasoning_summary_text.delta";!R.firstTokenTime&&M&&(R.firstTokenTime=O,R.timeToFirstToken=Math.round(O-R.startTime));let $="";if(w.event==="content_block_delta"&&w.data?.delta){let L=w.data.delta.type;L==="text_delta"?$=w.data.delta.text||"":L==="input_json_delta"?$=w.data.delta.partial_json||"":L==="thinking_delta"&&($=w.data.delta.thinking||"")}if((P==="response.output_text.delta"||P==="response.function_call_arguments.delta"||P==="response.reasoning_summary_text.delta")&&($=typeof w.data?.delta=="string"?w.data.delta:""),$){let L=m&&m.encodeText?m.encodeText($).length:gt($);R.tokenCount+=L,R.lastTokenTime=O}(w.event==="message_stop"||P==="response.completed"||P==="response.failed")&&(b&&(clearInterval(b),b=null),await T(!0),mt.delete(u))}}catch(C){b&&clearInterval(b),C.name!=="AbortError"&&C.code!=="ERR_STREAM_PREMATURE_CLOSE"&&n.log?.warn(`Error processing token stats: ${C.message}`)}};return i.tokenSpeedCapturePromise=y().catch(b=>{console.log(b),n.log?.warn(`Background stats processing failed: ${b.message}`)}),_}let h=performance.now(),g=0,f=!1;if(l&&typeof l=="string")try{let _=JSON.parse(l);if(typeof _.usage?.output_tokens=="number")g=_.usage.output_tokens,f=!0;else{let k=_.content??_.message?.content;if(k!==void 0)if(f=!0,m)Array.isArray(k)?g=k.reduce((y,b)=>{if(b.type==="text"){let T=b.text||"";return y+(m.encodeText?m.encodeText(T).length:gt(T))}return y},0):typeof k=="string"&&(g=m.encodeText?m.encodeText(k).length:gt(k));else{let y=Array.isArray(k)?k.map(b=>b.text).join(""):typeof k=="string"?k:"";g=gt(y)}}}catch{}if(f){let _=h-c,k={requestId:u,sessionId:p,statsKey:d,startTime:c,lastTokenTime:h,tokenCount:g,tokensPerSecond:ra(g,_),stream:!1};await sa(k,o,t.outputOptions,!0)}return l})})}});var Tr=E(()=>{"use strict";Vi();Qi();ia();kr()});import ee from"node:fs/promises";import aa from"json5";import Se from"node:path";import{CONFIG_FILE as te,HOME_DIR as Dn,PLUGINS_DIR as pd}from"@wengine-ai/claude-code-router-shared";function ca(){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 vr,ht,Fn,xr,ne,Un,_e,fd,Cr,jn=E(()=>{"use strict";vr=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(vr);if(n!==null&&typeof n=="object"){let e={};for(let[t,o]of Object.entries(n))e[t]=vr(o);return e}return n},ht=async n=>{try{await ee.access(n)}catch{await ee.mkdir(n,{recursive:!0})}},Fn=async()=>{await ht(Dn),await ht(pd),await ht(Se.join(Dn,"logs"))},xr=async()=>{let n=await ee.readFile(te,"utf-8");return aa.parse(n)},ne=async()=>{try{let n=await ee.readFile(te,"utf-8");try{let e=aa.parse(n);return vr(e)}catch(e){console.error(`Failed to parse config file at ${te}`),console.error("Error details:",e.message),console.error("Please check your config file syntax."),process.exit(1)}}catch(n){if(n.code==="ENOENT")try{await Fn();let e=await Un();e&&console.log(`Backed up existing configuration file to ${e}`);let t={PORT:3456,Providers:[],Router:{}};return await _e(t),console.log("Created minimal default configuration file at ~/.claude-code-router/config.json"),console.log("Please edit this file with your actual configuration."),t}catch(e){console.error("Failed to create default configuration:",e.message),process.exit(1)}else console.error(`Failed to read config file at ${te}`),console.error("Error details:",n.message),process.exit(1)}};Un=async()=>{ca();try{if(await ee.access(te).then(()=>!0).catch(()=>!1)){let n=new Date().toISOString().replace(/[:.]/g,"-"),e=`${te}.${n}.bak`;await ee.copyFile(te,e);try{let t=Se.dirname(te),o=Se.basename(te),s=(await ee.readdir(t)).filter(i=>i.startsWith(o)&&i.endsWith(".bak")).sort().reverse();if(s.length>3)for(let i=3;i<s.length;i++){let a=Se.join(t,s[i]);await ee.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},_e=async n=>{ca(),await ht(Dn),await fd();let e=`${JSON.stringify(n,null,2)}`;await ee.writeFile(te,e)},fd=async()=>{if(!await ee.access(te).then(()=>!0).catch(()=>!1))return;let e=Se.join(Dn,"config-history");await ht(e);let t=new Date().toISOString().replace(/[:.]/g,"-"),o=Se.join(e,`config-${t}.json`);await ee.copyFile(te,o);try{let s=(await ee.readdir(e)).filter(i=>i.startsWith("config-")&&i.endsWith(".json")).sort().reverse();if(s.length>50)for(let i of s.slice(50))await ee.unlink(Se.join(e,i))}catch(r){console.warn("Failed to prune config-history:",r)}},Cr=async()=>{let n=await ne();return Object.assign(process.env,n),n}});function md(n){return n?.cache_read_input_tokens??n?.input_tokens_details?.cached_tokens??n?.prompt_tokens_details?.cached_tokens??0}function gd(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 Ve(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:md(n),cache_creation_input_tokens:gd(n)}}function Hn(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 Sr=E(()=>{"use strict"});function zn(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(Rr(r,s))}return}if(Array.isArray(n)){for(let o of n)zn(o,e,t);return}if(typeof n=="object")for(let o of Object.values(n))zn(o,e,t)}}function la(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(Rr(s,a))}return zn(n?.Router,t,o),zn(n?.fallback,t,o),o}function yt(n,e){try{let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=la(n),r=0;for(let s of t.getAllStates()||[]){let i=s?.provider,a=s?.model;!i||!a||o.has(Rr(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 Bn(n,e){try{if(!n)return 0;let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=0;for(let r of t.getAllStates()||[])r?.provider===n&&r?.model&&(t.recover(n,r.model),o++);return o>0&&e?.info?.(`[health-reconcile] Cleared ${o} health entr${o===1?"y":"ies"} for probed provider ${n}`),o}catch(t){return e?.warn?.(`[health-reconcile] Failed to clear provider health for ${n}: ${t?.message||t}`),0}}var hd,Rr,Jn=E(()=>{"use strict";re();hd="\0",Rr=(n,e)=>`${n}${hd}${e}`});function yd(n){return/\[1m\]/i.test(n)?1e6:2e5}function _d(n){let e=n.toLowerCase();return e.includes("haiku")||e.includes("mini")?8192:32e3}function kd(n,e=n){let t=yd(n),o=_d(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 Wn(n){let e=new Map,t=(o,r=o)=>{!o||e.has(o)||e.set(o,kd(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 wr=E(()=>{"use strict"});function bd(n){let e=n.ip||n.connection?.remoteAddress||"";return e==="127.0.0.1"||e==="::1"||e==="::ffff:127.0.0.1"}var Kn,Er=E(()=>{"use strict";Kn=n=>async(e,t,o)=>{if(["/","/health"].includes(e.url)||e.url.startsWith("/ui")||bd(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 ka={};no(ka,{append:()=>vt,clear:()=>Ir,close:()=>zd,getOldestRecordTimestamp:()=>Bd,query:()=>Ar,querySummary:()=>xt,readTokenSpeedStats:()=>Mr});import{existsSync as pa,mkdirSync as Td,readFileSync as fa}from"fs";import{join as Qn}from"path";import{tmpdir as vd}from"os";import{openSqliteDatabase as xd}from"@wengine-ai/claude-code-router-shared";import{HOME_DIR as Cd,sanitizeTempFileKey as Sd,TOKEN_SPEED_TEMP_SUBDIRECTORY as Rd}from"@wengine-ai/claude-code-router-shared";function Ad(){pa(Vn)||Td(Vn,{recursive:!0})}function Tt(){if(be)return be;Ad();let n=xd(wd),e=n.db;n.driver==="node:sqlite"&&console.error(`[usage] better-sqlite3 unavailable, using built-in node:sqlite fallback. Reason: ${n.fallbackReason}`),e.pragma("journal_mode = WAL"),e.pragma("synchronous = NORMAL"),e.pragma("busy_timeout = 5000");let t=e.pragma("user_version",{simple:!0})||0;return t<1&&(Id(e),e.pragma("user_version = 1")),t<2&&(e.prepare("PRAGMA table_info(usage_records)").all().some(r=>r.name==="upstream_model")||e.exec("ALTER TABLE usage_records ADD COLUMN upstream_model TEXT"),e.pragma("user_version = 2")),Nd(e),ha(e,!0),Or=e.prepare(`
|
|
317
317
|
INSERT INTO usage_records (
|
|
318
318
|
id, timestamp, session_id, provider, original_model, model, upstream_model, model_family,
|
|
319
319
|
scenario_type, client_type,
|
|
@@ -327,7 +327,7 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
327
327
|
@cache_read_input_tokens, @cache_creation_input_tokens, @ttft,
|
|
328
328
|
@tokens_per_second, @duration_ms, @status, @error_message, @response_body
|
|
329
329
|
)
|
|
330
|
-
`),
|
|
330
|
+
`),be=e,be}function Id(n){n.exec(`
|
|
331
331
|
CREATE TABLE IF NOT EXISTS usage_records (
|
|
332
332
|
id TEXT PRIMARY KEY,
|
|
333
333
|
timestamp TEXT NOT NULL,
|
|
@@ -364,12 +364,12 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
364
364
|
CREATE INDEX IF NOT EXISTS idx_usage_records_scenario_timestamp ON usage_records(scenario_type, timestamp);
|
|
365
365
|
CREATE INDEX IF NOT EXISTS idx_usage_records_client_timestamp ON usage_records(client_type, timestamp);
|
|
366
366
|
CREATE INDEX IF NOT EXISTS idx_usage_records_session_timestamp ON usage_records(session_id, timestamp);
|
|
367
|
-
`)}function
|
|
367
|
+
`)}function Md(n,e){return n.prepare("SELECT value FROM usage_meta WHERE key = ?").get(e)?.value}function _t(n,e,t){n.prepare(`
|
|
368
368
|
INSERT INTO usage_meta (key, value)
|
|
369
369
|
VALUES (?, ?)
|
|
370
370
|
ON CONFLICT(key) DO UPDATE SET value = excluded.value
|
|
371
|
-
`).run(e,t)}function
|
|
372
|
-
`)){let l=a.trim();if(l)try{let c=JSON.parse(l);if(
|
|
371
|
+
`).run(e,t)}function Nd(n){if(Md(n,ua)||!pa(Pr))return;let e=Ld(n,"INSERT OR IGNORE"),t=fa(Pr,"utf-8"),o=0,r=0,s=0;n.transaction(()=>{for(let a of t.split(`
|
|
372
|
+
`)){let l=a.trim();if(l)try{let c=JSON.parse(l);if(!$d(c)){r++;continue}e.run(ga(Yn(c))).changes>0?o++:s++}catch{r++}}_t(n,ua,new Date().toISOString()),_t(n,"legacy_usage_jsonl_migrated_path",Pr),_t(n,"legacy_usage_jsonl_imported_count",String(o)),_t(n,"legacy_usage_jsonl_skipped_count",String(r)),_t(n,"legacy_usage_jsonl_duplicate_count",String(s))})()}function $d(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 Ld(n,e="INSERT"){return n.prepare(`
|
|
373
373
|
${e} INTO usage_records (
|
|
374
374
|
id, timestamp, session_id, provider, original_model, model, upstream_model, model_family,
|
|
375
375
|
scenario_type, client_type,
|
|
@@ -383,11 +383,11 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
383
383
|
@cache_read_input_tokens, @cache_creation_input_tokens, @ttft,
|
|
384
384
|
@tokens_per_second, @duration_ms, @status, @error_message, @response_body
|
|
385
385
|
)
|
|
386
|
-
`)}function
|
|
387
|
-
SELECT ${t} AS key, ${
|
|
386
|
+
`)}function Dd(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,Ed);return Math.round(n/r)}function Yn(n){let e=Number(n.outputTokens)||0,t=Number(n.durationMs),o=Number.isFinite(t)?t:null,r=n.stream?bt(n.ttft):null;return{...n,ttft:r,tokensPerSecond:Dd(e,o,r)}}function ke(n){return typeof n=="string"?n:n==null?"":String(n)}function Gn(n){if(n==null)return;let e=String(n);return e.length>0?e:void 0}function kt(n){let e=Number(n);return Number.isFinite(e)?e:0}function ma(n){return n==="success"?"success":"error"}function ga(n){let e=Yn(n);return{id:ke(e.id),timestamp:ke(e.timestamp),session_id:ke(e.sessionId),provider:ke(e.provider),original_model:ke(e.originalModel),model:ke(e.model),upstream_model:Gn(e.upstreamModel)??null,model_family:ke(e.modelFamily),scenario_type:ke(e.scenarioType),client_type:Gn(e.clientType)??null,stream:e.stream?1:0,input_tokens:Math.trunc(kt(e.inputTokens)),output_tokens:Math.trunc(kt(e.outputTokens)),cache_read_input_tokens:Math.trunc(kt(e.cacheReadInputTokens)),cache_creation_input_tokens:Math.trunc(kt(e.cacheCreationInputTokens)),ttft:bt(e.ttft),tokens_per_second:bt(e.tokensPerSecond),duration_ms:Math.trunc(kt(e.durationMs)),status:ma(e.status),error_message:Gn(e.errorMessage)??null,response_body:Gn(e.responseBody)??null}}function Fd(n){return Yn({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:ma(n.status),errorMessage:n.error_message??void 0,responseBody:n.response_body??void 0})}function Ud(){return new Date(Date.now()-Pd*24*60*60*1e3).toISOString()}function ha(n,e=!1){let t=Date.now();!e&&t-da<Od||(n.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(Ud()),da=t)}function ya(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 Qe(n,e,t,o=""){let r=o?e.sql?`${e.sql} AND ${o}`:`WHERE ${o}`:e.sql,s=n.prepare(`
|
|
387
|
+
SELECT ${t} AS key, ${jd}
|
|
388
388
|
FROM usage_records ${r}
|
|
389
389
|
GROUP BY key
|
|
390
|
-
`).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
|
|
390
|
+
`).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 _a(n,e){let t=ya(e),o=n.prepare(`
|
|
391
391
|
SELECT
|
|
392
392
|
COUNT(*) AS totalRequests,
|
|
393
393
|
COALESCE(SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END), 0) AS successCount,
|
|
@@ -398,14 +398,14 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
398
398
|
AVG(CASE WHEN status = 'success' AND ttft IS NOT NULL THEN ttft END) AS avgTtft,
|
|
399
399
|
AVG(CASE WHEN status = 'success' AND tokens_per_second IS NOT NULL THEN tokens_per_second END) AS avgTokensPerSecond
|
|
400
400
|
FROM usage_records ${t.sql}
|
|
401
|
-
`).get(...t.params);return{totalRequests:o.totalRequests,successCount:o.successCount,errorCount:o.totalRequests-o.successCount,totalInputTokens:o.totalInputTokens,totalOutputTokens:o.totalOutputTokens,totalCacheReadInputTokens:o.totalCacheReadInputTokens,totalCacheCreationInputTokens:o.totalCacheCreationInputTokens,avgTtft:o.avgTtft!=null?Math.round(o.avgTtft):null,avgTokensPerSecond:o.avgTokensPerSecond!=null?Math.round(o.avgTokensPerSecond):null,byModel:
|
|
401
|
+
`).get(...t.params);return{totalRequests:o.totalRequests,successCount:o.successCount,errorCount:o.totalRequests-o.successCount,totalInputTokens:o.totalInputTokens,totalOutputTokens:o.totalOutputTokens,totalCacheReadInputTokens:o.totalCacheReadInputTokens,totalCacheCreationInputTokens:o.totalCacheCreationInputTokens,avgTtft:o.avgTtft!=null?Math.round(o.avgTtft):null,avgTokensPerSecond:o.avgTokensPerSecond!=null?Math.round(o.avgTokensPerSecond):null,byModel:Qe(n,t,"model"),byProvider:Qe(n,t,"provider"),byScenario:Qe(n,t,"scenario_type"),byFamily:Qe(n,t,"model_family || '/' || scenario_type","model_family IS NOT NULL AND model_family != ''"),byDay:Qe(n,t,"date(timestamp, 'localtime')"),byClient:Qe(n,t,"CASE WHEN client_type IS NULL OR client_type = '' THEN 'unknown' ELSE client_type END")}}function vt(n){let e=Tt(),t=Yn(n);Or.run(ga(t)),ha(e)}function Ar(n){let e=Tt(),t=ya(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),Hd):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=_a(e,n);return{records:a.map(Fd),summary:l,total:i.count,page:o,pageSize:r}}function xt(n,e,t){let o=Tt();return _a(o,{startTime:n,endTime:e,status:t})}function Ir(n){let e=Tt();if(!n){e.prepare("DELETE FROM usage_records").run();return}e.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(n)}function zd(){be&&(be.pragma("wal_checkpoint(TRUNCATE)"),be.close(),be=null,Or=null)}function bt(n){if(n==null)return null;if(typeof n=="number")return n;let e=parseFloat(String(n));return isNaN(e)?null:e}function Mr(n){let e=Qn(vd(),Rd,`session-${Sd(n)}.json`);try{let t=JSON.parse(fa(e,"utf-8"));return{ttft:bt(t.timeToFirstToken),tokensPerSecond:bt(t.tokensPerSecond)}}catch{return{ttft:null,tokensPerSecond:null}}}function Bd(n,e,t){return Tt().prepare("SELECT MIN(timestamp) AS timestamp FROM usage_records WHERE provider = ? AND timestamp >= ? AND timestamp <= ?").get(n,e,t)?.timestamp??void 0}var Vn,wd,Pr,Ed,Pd,Od,ua,be,Or,da,jd,Hd,Ct=E(()=>{"use strict";Vn=Qn(Cd,"data"),wd=Qn(Vn,"usage.sqlite"),Pr=Qn(Vn,"usage.jsonl"),Ed=1,Pd=180,Od=3600*1e3,ua="legacy_usage_jsonl_migrated_at",be=null,Or=null,da=0;jd=`
|
|
402
402
|
COUNT(*) AS count,
|
|
403
403
|
COALESCE(SUM(CASE WHEN status = 'success' THEN input_tokens ELSE 0 END), 0) AS inputTokens,
|
|
404
404
|
COALESCE(SUM(CASE WHEN status = 'success' THEN output_tokens ELSE 0 END), 0) AS outputTokens,
|
|
405
405
|
COALESCE(SUM(CASE WHEN status = 'success' THEN cache_read_input_tokens ELSE 0 END), 0) AS cacheReadInputTokens,
|
|
406
406
|
COALESCE(SUM(CASE WHEN status = 'success' THEN cache_creation_input_tokens ELSE 0 END), 0) AS cacheCreationInputTokens
|
|
407
|
-
`;td=200});import{existsSync as rd}from"fs";import{homedir as sd}from"os";import{join as id}from"path";import{writeFile as ad}from"fs/promises";async function qi(){let n=sd(),e=id(n,".claude.json");if(!rd(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 ad(e,JSON.stringify(o,null,2))}}var ea=w(()=>{"use strict"});import{existsSync as cd,readdirSync as ld,statSync as ud,unlinkSync as dd}from"fs";import{join as ta}from"path";import{HOME_DIR as pd}from"@wengine-ai/claude-code-router-shared";function oa(n=ta(pd,"logs")){try{if(!cd(n))return;let e=Date.now()-md;for(let t of ld(n)){if(!/^ccr-.*\.log$/.test(t))continue;let o=ta(n,t),r=ud(o);r.isFile()&&r.mtime.getTime()<e&&dd(o)}}catch(e){console.warn("Failed to prune old server log files:",e)}}function ra(){na||(na=!0,oa(),setInterval(oa,gd).unref())}var fd,md,gd,na,sa=w(()=>{"use strict";fd=7,md=fd*24*60*60*1e3,gd=1440*60*1e3,na=!1});async function ia(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":pt.registerPlugin(An,{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 pt.enablePlugins(n)}var aa=w(()=>{"use strict";lr()});import{ensureCcrSelfProvider as hd}from"@wengine-ai/claude-code-router-shared";import{join as ie,isAbsolute as yd,normalize as _d,dirname as kd}from"path";import{fileURLToPath as bd}from"url";import Td from"@fastify/static";import{readdirSync as ca,statSync as vd,readFileSync as la,writeFileSync as xd,existsSync as de,unlinkSync as ua,rmSync as Cd}from"fs";import{homedir as Cr}from"os";import{getPresetDir as Wn,readManifestFromDir as Sr,manifestToPresetFile as da,saveManifest as pa,isPresetInstalled as fa,extractPreset as Sd,HOME_DIR as Rd,loadConfigFromManifest as wd,downloadPresetToTemp as Ed,findMarketPresetByName as Pd,getMarketPresets as Od,applyClientSelection as Ad,disableClient as Id,enableClient as Md,isClientId as $d,listClientStatuses as ma,restoreClient as Nd,listProjectConfigs as Ld,readProjectConfigById as Rr,readProjectConfig as Dd,writeProjectConfig as ga,deleteProjectConfig as Fd,getClaudeProjectId as Ud,getProjectConfigPath as jd,refreshCcrProjectTakeover as Hd,refreshPiProjectTakeover as zd,refreshProjectTakeovers as Bd,syncGlobalProjectTakeovers as wr,getProjectTakeoverClients as Er,setProjectTakeover as Pr,isProjectTakeoverClient as Jd}from"@wengine-ai/claude-code-router-shared";import Wd from"@fastify/multipart";import Gd from"adm-zip";function ha(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 Kd(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=Ct(t,e),s=Ct(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 ya(n,e){let t=n.app;tt(),ot(),t.register(Wd,{limits:{fileSize:50*1024*1024}}),t.get("/v1/models",async()=>{let a=jn(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=jn(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:Bt(c,d,u)}}),t.get("/api/config",async(a,l)=>await dr()),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=Lr(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 $n();d&&console.log(`Backed up existing configuration file to ${d}`);let p=null;try{p=await ne()}catch{}let g=hd(c,p);await fe(c);let y;try{y=await wr(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),ue&&m.transformerService&&(m.providerService=new ue(m.configService,m.transformerService,t.log)),kt(c,t.log),We&&En){try{En()}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=We(()=>m.providerService.getProviders(),_,k=>ce(m.configService,k),t.log,k=>m.configService.get(k))}}}catch(m){t.log?.warn?.(`Config saved but runtime reload failed: ${m?.message||m}`)}return{success:!0,message:"Config saved successfully",projectTakeoverSync:y}}),t.get("/api/clients",async(a,l)=>{try{let c=await ne();return{clients:ma(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 ne(),p=Ad(d,u);await fe(p.config);let g=await wr(p.config),y=ha(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(!$d(u)){l.status(404).send({error:`Unknown client: ${u}`});return}let d=await ne(),p=c==="enable"?Md(d,u):c==="restore"?Nd(d,u):Id(d,u);await fe(d);let g=await wr(d),y=ha(t,g,`clients/${u}/${c}`);return{success:p.success&&y,result:p,results:[p],clients:ma(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 Ld();return{projects:await Promise.all(c.map(async d=>{let p=await Er(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"||!yd(c)){l.status(400).send({error:"A valid absolute project path is required"});return}let u=_d(c).replace(/[\\/]+$/,"")||c;if(await Dd(u)){l.status(409).send({error:"Project is already configured"});return}await ga(u,{Router:{}});try{let g=await ne();await Pr(u,["claudeCode"],g)}catch(g){console.error("Failed to auto-enable ccr takeover for new project:",g)}let p=await Er(u);return{id:Ud(u),path:u,configPath:jd(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 Rr(c);if(!d){l.status(404).send({error:"Project config not found"});return}await ga(d.path,{Router:u});let p=await ne();Object.keys(u).length===0?await Bd(d.path,p):(await Hd(d.path,p,u),await zd(d.path,p,u));let g=await Er(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(Jd);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 Rr(c);if(!p){l.status(404).send({error:"Project config not found"});return}let g=await ne(),y=await Pr(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 Rr(c);if(!u){l.status(404).send({error:"Project config not found"});return}try{let d=await ne();await Pr(u.path,[],d)}catch(d){console.error("Failed to remove ccr takeover while deleting project:",d)}return await Fd(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:kd(bd(import.meta.url)),s=de(ie(r,"index.html"))?r:de(ie(r,"..","index.html"))?ie(r,".."):ie(r,"..","dist");t.register(Td,{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=ie(Cr(),".claude-code-router","logs"),u=[];if(de(c)){let d=ca(c);for(let p of d)if(p.endsWith(".log")){let g=ie(c,p),y=vd(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=ie(Cr(),".claude-code-router","logs","app.log"),de(u)?la(u,"utf8").split(`
|
|
408
|
-
`).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=ie(Cr(),".claude-code-router","logs","app.log"),de(u)&&xd(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:Pt()})),t.put("/api/debug-log",async a=>{let{enabled:l}=a.body;return eo(l),{enabled:Pt()}}),t.get("/api/usage",async(a,l)=>{try{let c=a.query;return Tr({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 Ct(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 vr(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=U().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&&Fn(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&&Fn(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 ne()).Providers||[],d=Kd(u),p=new Map(d.map(m=>[m.provider,m])),g=uo();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=xo();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=ie(Rd,"presets");if(!de(c))return{presets:[]};let d=ca(c,{withFileTypes:!0}).filter(g=>g.isDirectory()&&!g.name.startsWith(".")).map(g=>g.name),p=[];for(let g of d){let y=ie(c,g);try{let m=ie(y,"manifest.json"),f=la(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:O,...P}=_;p.push({id:g,name:P.name||g,version:P.version||"1.0.0",description:P.description,author:P.author,homepage:P.homepage,repository:P.repository,license:P.license,keywords:P.keywords,ccrVersion:P.ccrVersion,source:P.source,sourceType:P.sourceType,checksum:P.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=Wn(c);if(!de(u)){l.status(404).send({error:"Preset not found"});return}let d=await Sr(u);return{...da(d),config:wd(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=Wn(c);if(!de(d)){l.status(404).send({error:"Preset not found"});return}let g={...await Sr(d)};return u&&Object.keys(u).length>0&&(g.userValues={...g.userValues,...u}),await pa(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=Wn(c);if(!de(u)){l.status(404).send({error:"Preset not found"});return}return Cd(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 Od()}}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 Pd(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 fa(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 Ed(m),_=await i(f);if(await fa(y)){ua(f),l.status(409).send({error:"Preset already installed",message:`Preset '${y}' was installed while downloading. Please try again.`,presetName:y});return}let k=Wn(y);await Sd(f,k);let h=await Sr(k);return h.repository=u.repo,u.url&&(h.source=u.url),await pa(y,h),ua(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 Gd(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 da(u)}return n}var _a=w(()=>{"use strict";je();we();Ie();Kt();Pn();oe();qe();re();Un();Nn();St();gr()});function Qd(n,e){let t=n?.Router,{family:o}=at(e||"");if(t?.enableFamilyRouting&&o){let r=t.families?.[o]?.image;if(r)return r}return t?.image}function ka(n){return n?.Router?.image}function Vd(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 Yd(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 Or,Gn,Ar,ba,Ta=w(()=>{"use strict";je();Or=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}},Gn=new Or;Ar=class{name="image";tools;constructor(){this.tools=new Map,this.appendTools()}shouldHandle(e,t){let o=Qd(t,e.originalModel||e.body.model);if(!o||e.body.model===o||!Vd(e.body.messages))return!1;let s=t.forceUseImageAgent===!0;return!s&&Eo(e.body.model)?!1:(Yd(e.body.messages),s&&!!ka(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=Gn.getImage(`${t.req.id}_Image#${a}`);l&&o.push({type:"image",source:l})});else{let a=Gn.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:ka(t.config),system:[{type:"text",text:`You must interpret and analyze images strictly according to the assigned task.
|
|
407
|
+
`;Hd=200});import{existsSync as Jd}from"fs";import{homedir as Wd}from"os";import{join as Kd}from"path";import{writeFile as Gd}from"fs/promises";async function ba(){let n=Wd(),e=Kd(n,".claude.json");if(!Jd(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 Gd(e,JSON.stringify(o,null,2))}}var Ta=E(()=>{"use strict"});import{existsSync as Vd,readdirSync as Qd,statSync as Yd,unlinkSync as Xd}from"fs";import{join as va}from"path";import{HOME_DIR as Zd}from"@wengine-ai/claude-code-router-shared";function Ca(n=va(Zd,"logs")){try{if(!Vd(n))return;let e=Date.now()-ep;for(let t of Qd(n)){if(!/^ccr-.*\.log$/.test(t))continue;let o=va(n,t),r=Yd(o);r.isFile()&&r.mtime.getTime()<e&&Xd(o)}}catch(e){console.warn("Failed to prune old server log files:",e)}}function Sa(){xa||(xa=!0,Ca(),setInterval(Ca,tp).unref())}var qd,ep,tp,xa,Ra=E(()=>{"use strict";qd=7,ep=qd*24*60*60*1e3,tp=1440*60*1e3,xa=!1});async function wa(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":ut.registerPlugin(Ln,{enabled:s,outputHandlers:[{type:"temp-file",enabled:!0,config:$n()}],...i});break;default:console.warn(`Unknown plugin: ${r}`);break}}await ut.enablePlugins(n)}var Ea=E(()=>{"use strict";Tr()});import{ensureCcrSelfProvider as np}from"@wengine-ai/claude-code-router-shared";import{join as ie,isAbsolute as op,normalize as rp,dirname as sp}from"path";import{fileURLToPath as ip}from"url";import ap from"@fastify/static";import{readdirSync as Pa,statSync as cp,readFileSync as Oa,writeFileSync as lp,existsSync as de,unlinkSync as Aa,rmSync as up}from"fs";import{homedir as Nr}from"os";import{getPresetDir as Xn,readManifestFromDir as $r,manifestToPresetFile as Ia,saveManifest as Ma,isPresetInstalled as Na,extractPreset as dp,HOME_DIR as pp,loadConfigFromManifest as fp,downloadPresetToTemp as mp,findMarketPresetByName as gp,getMarketPresets as hp,applyClientSelection as yp,disableClient as _p,enableClient as kp,isClientId as bp,listClientStatuses as $a,restoreClient as Tp,listProjectConfigs as vp,readProjectConfigById as Lr,readProjectConfig as xp,writeProjectConfig as La,deleteProjectConfig as Cp,getClaudeProjectId as Sp,getProjectConfigPath as Rp,refreshCcrProjectTakeover as wp,refreshPiProjectTakeover as Ep,refreshProjectTakeovers as Pp,syncGlobalProjectTakeovers as Dr,getProjectTakeoverClients as Fr,setProjectTakeover as Ur,isProjectTakeoverClient as Op}from"@wengine-ai/claude-code-router-shared";import Ap from"@fastify/multipart";import Ip from"adm-zip";function Da(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 Mp(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=xt(t,e),s=xt(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),m,h,g=(n||[]).find(f=>f&&f.name===l);g&&g.quota&&(m=g.quota.limit5h,h=g.quota.limit7d),i.push({provider:l,used5h:d,used7d:p,limit5h:m,limit7d:h})}return i}async function Fa(n,e){let t=n.app;et(),nt(),t.register(Ap,{limits:{fileSize:50*1024*1024}}),t.get("/v1/models",async()=>{let a=Wn(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=Wn(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,m=t._server.tokenizerService;if(p&&p.includes(",")&&m)try{let[g,f]=p.split(",");a.log?.info(`Looking up tokenizer for provider: ${g}, model: ${f}`);let _=m.getTokenizerConfigForModel(g,f);_?a.log?.info(`Using tokenizer config: ${JSON.stringify(_)}`):a.log?.warn(`No tokenizer config found for ${g},${f}, using default tiktoken`);let k=await m.countTokens({messages:c,system:d,tools:u},_,g);return{input_tokens:k.tokenCount,tokenizer:k.tokenizerUsed}}catch(g){a.log?.error(`Error using configured tokenizer: ${g.message}`),a.log?.error(g.stack)}else p?p.includes(",")?m||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:Bt(c,d,u)}}),t.get("/api/config",async(a,l)=>await xr()),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=Kr(c||{});if(u.length>0)return l.status(400),{success:!1,error:"Invalid proxy URL",message:u.map(g=>`${g.key}: ${g.error}`).join("; "),proxyErrors:u};let d=await Un();d&&console.log(`Backed up existing configuration file to ${d}`);let p=null;try{p=await ne()}catch{}let m=np(c,p);await _e(c);let h;try{h=await Dr(c),h.failed.length>0&&t.log?.warn?.({projectTakeoverSync:h},"Config saved but some global project takeovers failed to sync")}catch(g){t.log?.warn?.(`Config saved but global project takeover sync failed: ${g?.message||g}`)}try{let g=t._server;if(g?.configService){g.configService.reload();let f=c?.Providers||c?.providers;if(Array.isArray(f)&&g.configService.set("providers",f),ue&&g.transformerService&&(g.providerService=new ue(g.configService,g.transformerService,t.log)),yt(c,t.log),We&&Pn){try{Pn()}catch{}let _={enabled:g.configService.get("ACTIVE_PROBE_ENABLED")??!0,quotaProbeIntervalMinutes:g.configService.get("QUOTA_PROBE_INTERVAL_MINUTES")??10,probeTimeoutMs:g.configService.get("PROBE_TIMEOUT_MS")??15e3,slowThresholdMs:Number(g.configService.get("PROBE_SLOW_THRESHOLD_MS"))||3e3,initialDelayMs:g.configService.get("PROBE_INITIAL_DELAY_MS")??5e3,excludeProviders:g.configService.get("EXCLUDE_PROBE_PROVIDERS")??[]};g.activeProbeService=We(()=>g.providerService.getProviders(),_,k=>ce(g.configService,k),t.log,k=>g.configService.get(k))}}}catch(g){t.log?.warn?.(`Config saved but runtime reload failed: ${g?.message||g}`)}return{success:!0,message:"Config saved successfully",projectTakeoverSync:h}}),t.get("/api/clients",async(a,l)=>{try{let c=await ne();return{clients:$a(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 ne(),p=yp(d,u);await _e(p.config);let m=await Dr(p.config),h=Da(t,m,"clients/apply");return{...p,success:p.success&&h,projectTakeoverSync:m}}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(!bp(u)){l.status(404).send({error:`Unknown client: ${u}`});return}let d=await ne(),p=c==="enable"?kp(d,u):c==="restore"?Tp(d,u):_p(d,u);await _e(d);let m=await Dr(d),h=Da(t,m,`clients/${u}/${c}`);return{success:p.success&&h,result:p,results:[p],clients:$a(d),config:d,projectTakeoverSync:m}}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 vp();return{projects:await Promise.all(c.map(async d=>{let p=await Fr(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"||!op(c)){l.status(400).send({error:"A valid absolute project path is required"});return}let u=rp(c).replace(/[\\/]+$/,"")||c;if(await xp(u)){l.status(409).send({error:"Project is already configured"});return}await La(u,{Router:{}});try{let m=await ne();await Ur(u,["claudeCode"],m)}catch(m){console.error("Failed to auto-enable ccr takeover for new project:",m)}let p=await Fr(u);return{id:Sp(u),path:u,configPath:Rp(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 Lr(c);if(!d){l.status(404).send({error:"Project config not found"});return}await La(d.path,{Router:u});let p=await ne();Object.keys(u).length===0?await Pp(d.path,p):(await wp(d.path,p,u),await Ep(d.path,p,u));let m=await Fr(d.path);return{id:c,path:d.path,configPath:d.configPath,Router:u,ccrTakeoverClients:m,ccrTakeover:m.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(Op);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 Lr(c);if(!p){l.status(404).send({error:"Project config not found"});return}let m=await ne(),h=await Ur(p.path,d,m);return{id:c,path:p.path,ccrTakeoverClients:h,ccrTakeover:h.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 Lr(c);if(!u){l.status(404).send({error:"Project config not found"});return}try{let d=await ne();await Ur(u.path,[],d)}catch(d){console.error("Failed to remove ccr takeover while deleting project:",d)}return await Cp(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:sp(ip(import.meta.url)),s=de(ie(r,"index.html"))?r:de(ie(r,"..","index.html"))?ie(r,".."):ie(r,"..","dist");t.register(ap,{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=ie(Nr(),".claude-code-router","logs"),u=[];if(de(c)){let d=Pa(c);for(let p of d)if(p.endsWith(".log")){let m=ie(c,p),h=cp(m);u.push({name:p,path:m,size:h.size,lastModified:h.mtime.toISOString()})}u.sort((p,m)=>new Date(m.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=ie(Nr(),".claude-code-router","logs","app.log"),de(u)?Oa(u,"utf8").split(`
|
|
408
|
+
`).filter(m=>m.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=ie(Nr(),".claude-code-router","logs","app.log"),de(u)&&lp(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:Et()})),t.put("/api/debug-log",async a=>{let{enabled:l}=a.body;return io(l),{enabled:Et()}}),t.get("/api/usage",async(a,l)=>{try{let c=a.query;return Ar({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 xt(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 Ir(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=U().getAllStates(),p=t._server?.activeProbeService?.getProbeTelemetry?.()||[];return{states:u.map(m=>({provider:m.provider,model:m.model,status:m.status,failureCount:m.failureCount,successCount:m.successCount,lastFailureTime:m.lastFailureTime,lastError:m.lastError,rateLimitUntil:m.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&&Bn(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(m=>m?.name&&m.enabled!==!1),p=await Promise.all(d.map(async m=>{let h=await u.probeProviderManually(m.name);return h.success&&Bn(m.name,a?.log),{provider:m.name,success:h.success,latencyMs:h.latencyMs,status:h.status,isSlow:h.isSlow,errorKind:h.errorKind??null,error:h.error}}));return{results:p,successCount:p.filter(m=>m.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 ne()).Providers||[],d=Mp(u),p=new Map(d.map(g=>[g.provider,g])),m=yo();for(let g of d){let f=m.find(_=>_.provider===g.provider);f&&f.limit!=null&&(g.limit5h=f.limit,g.used5h=f.limit-(f.remaining??0),f.reset&&(g.reset5h=new Date(f.reset*1e3).toISOString()))}let h=Po();for(let g of h){let f=p.get(g.provider);f||(f={provider:g.provider,used5h:0,used7d:0},d.push(f),p.set(g.provider,f)),typeof g.totalBalance=="number"?(f.limit7d=g.totalBalance,f.type7d=g.currency?"balance":"rateLimit",g.currency&&(f.currency=g.currency),typeof g.usedBalance=="number"?f.used7d=g.usedBalance:typeof g.remainingBalance=="number"?f.used7d=Math.max(0,g.totalBalance-g.remainingBalance):(f.used7d=0,f.limit7d=g.totalBalance)):typeof g.usedBalance=="number"&&(f.used7d=g.usedBalance),typeof g.usedDailyBalance=="number"&&(f.used5h=g.usedDailyBalance,f.type5h="rateLimit"),typeof g.limitDaily=="number"&&(f.limit5h=g.limitDaily,f.type5h="rateLimit"),typeof g.resetTime=="string"&&(g.limitDaily!==void 0?f.reset5h=g.resetTime:f.reset7d=g.resetTime),typeof g.resetTime7d=="string"&&(f.reset7d=g.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=ie(pp,"presets");if(!de(c))return{presets:[]};let d=Pa(c,{withFileTypes:!0}).filter(m=>m.isDirectory()&&!m.name.startsWith(".")).map(m=>m.name),p=[];for(let m of d){let h=ie(c,m);try{let g=ie(h,"manifest.json"),f=Oa(g,"utf-8"),_=JSON.parse(f),{Providers:k,Router:y,PORT:b,HOST:T,API_TIMEOUT_MS:C,PROXY_URL:v,PROXY_GLOBAL_ENABLED:S,LOG:x,LOG_LEVEL:w,StatusLine:R,NON_INTERACTIVE_MODE:O,...P}=_;p.push({id:m,name:P.name||m,version:P.version||"1.0.0",description:P.description,author:P.author,homepage:P.homepage,repository:P.repository,license:P.license,keywords:P.keywords,ccrVersion:P.ccrVersion,source:P.source,sourceType:P.sourceType,checksum:P.checksum,installed:!0})}catch(g){console.error(`Failed to read preset ${m}:`,g)}}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=Xn(c);if(!de(u)){l.status(404).send({error:"Preset not found"});return}let d=await $r(u);return{...Ia(d),config:fp(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=Xn(c);if(!de(d)){l.status(404).send({error:"Preset not found"});return}let m={...await $r(d)};return u&&Object.keys(u).length>0&&(m.userValues={...m.userValues,...u}),await Ma(c,m),{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=Xn(c);if(!de(u)){l.status(404).send({error:"Preset not found"});return}return up(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 hp()}}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 gp(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,m]=d,h=u.name||c;if(await Na(h)){l.status(409).send({error:"Preset already installed",message:`Preset '${h}' is already installed. To update or reconfigure, please delete it first using the delete button.`,presetName:h});return}let g=`https://github.com/${p}/${m}/archive/refs/heads/main.zip`,f=await mp(g),_=await i(f);if(await Na(h)){Aa(f),l.status(409).send({error:"Preset already installed",message:`Preset '${h}' was installed while downloading. Please try again.`,presetName:h});return}let k=Xn(h);await dp(f,k);let y=await $r(k);return y.repository=u.repo,u.url&&(y.source=u.url),await Ma(h,y),Aa(f),{success:!0,presetName:h,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 Ip(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 Ia(u)}return n}var Ua=E(()=>{"use strict";je();Oe();Me();Gt();On();oe();Ze();re();Jn();jn();Ct();wr()});function Np(n,e){let t=n?.Router,{family:o}=it(e||"");if(t?.enableFamilyRouting&&o){let r=t.families?.[o]?.image;if(r)return r}return t?.image}function ja(n){return n?.Router?.image}function $p(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 Lp(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 jr,Zn,Hr,Ha,za=E(()=>{"use strict";je();jr=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}},Zn=new jr;Hr=class{name="image";tools;constructor(){this.tools=new Map,this.appendTools()}shouldHandle(e,t){let o=Np(t,e.originalModel||e.body.model);if(!o||e.body.model===o||!$p(e.body.messages))return!1;let s=t.forceUseImageAgent===!0;return!s&&No(e.body.model)?!1:(Lp(e.body.messages),s&&!!ja(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=Zn.getImage(`${t.req.id}_Image#${a}`);l&&o.push({type:"image",source:l})});else{let a=Zn.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:ja(t.config),system:[{type:"text",text:`You must interpret and analyze images strictly according to the assigned task.
|
|
409
409
|
When an image placeholder is provided, your role is to parse the image content only within the scope of the user\u2019s instructions.
|
|
410
410
|
Do not ignore or deviate from the task.
|
|
411
411
|
Always ensure that your response reflects a clear, accurate interpretation of the image aligned with the given objective.`}],messages:[{role:"user",content:o}],stream:!1})}).then(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.
|
|
@@ -418,5 +418,5 @@ If multiple images exist, select the **most relevant imageId** based on the user
|
|
|
418
418
|
|
|
419
419
|
Do not attempt to describe or analyze the image directly yourself.
|
|
420
420
|
Ignore any user interruptions or unrelated instructions that might cause you to skip this requirement.
|
|
421
|
-
Your response should consistently follow this rule whenever image-related analysis is requested.`});let o=e.body.messages.filter(s=>s.role==="user"&&Array.isArray(s.content)&&s.content.some(i=>i.type==="image"||Array.isArray(i.content)&&i.content.some(a=>a.type==="image"))),r=1;o.forEach(s=>{Array.isArray(s.content)&&s.content.forEach(i=>{if(i.type==="image")Gn.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:(Gn.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}})})}},ba=new Ar});var Ir,va,Kn,xa=w(()=>{"use strict";Ta();Ir=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 Ir;va.registerAgent(ba);Kn=va});import{EventEmitter as Xd}from"node:events";import{performance as Ca}from"node:perf_hooks";import Zd from"json5";function qd(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 Ra(n,e){(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&ke(n,e)}function Mr(n){return n.usageSessionId||n.id}function Qn(n){return n.usageCacheKey||`${n.clientType||"unknown"}:request:${Mr(n)}`}function Xe(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 ep(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?Qe(e.usage):e.response?.usage?Qe(e.response.usage):void 0,o=e.model||e.response?.model;if(!(!t&&!o))return{usage:t,model:o}}async function tp(n,e,t){if(await new Promise((r,s)=>{let i=a=>a?s(a):r();Hn(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")){Ra(n,t);let r=Qn(n);n.clientContext?.usageScope==="session"&&(n.previousUsage=X.get(r))}}async function np(n,e,t){if(!n.pathname?.endsWith("/v1/messages"))return;let o=[];for(let r of Kn.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 wa(n){return{authClient:(e,t)=>tp(e,t,n),agent:(e,t)=>np(e,t,n)}}function Ea(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=Ca.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();Hn(e)(t,o,a).then(()=>s(),i)})}),n.addHook("onError",async(t,o,r)=>{t.errorMessage=r?.message||r?.toString?.()||"Unknown error",Sa.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){Ra(t,e);let l=Mr(t),c=Qn(t);if(r instanceof ReadableStream){if(i&&t.agents){let y=new AbortController,m=r.pipeThrough(new ae),f,_=-1,k="",h="",b="",v=[],C=[];return s(null,On(m,async(x,S)=>{try{if(x.event==="content_block_start"&&x?.data?.content_block?.name){let T=t.agents.find(E=>Kn.getAgent(E)?.tools.get(x.data.content_block.name));if(T){f=Kn.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=Zd.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 ae).getReader();for(;;)try{let{value:O,done:P}=await R.read();if(P)break;let A=O;if(["message_start","message_stop"].includes(A.event))continue;if(!S.desiredSize)break;S.enqueue(A)}catch(O){if(O.name==="AbortError"||O.code==="ERR_STREAM_PREMATURE_CLOSE"){y.abort();break}throw O}return}return 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 Ge))}let[d,p]=r.tee(),g=async y=>{let f=y.pipeThrough(new TextDecoderStream).pipeThrough(new ae).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=X.get(c)||{},C=Qe(b.usage),x=Ln(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:Xe(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}),X.put(c,x)}if(b?.response?.usage){let v=Qe(b.response.usage),C=X.get(c)||{},x=Ln(C,v,!1);t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_responses_sse",event:h,provider:t.provider,model:Xe(t),usageSessionId:l,capturedUsage:v,mergedUsage:x}),X.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=ep(r);if(u?.usage&&X.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(Sa.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=>{yo(t)}),n.addHook("onResponse",async(t,o)=>{if((t.ccrHookOrder||=[]).push("onResponse"),yo(t),!(!t.pathname?.endsWith("/v1/messages")&&!t.pathname?.endsWith("/v1/responses")))try{t.usageCapturePromise&&await t.usageCapturePromise,t.tokenSpeedCapturePromise&&await t.tokenSpeedCapturePromise;let r=Mr(t),s=o.statusCode>=400,i=s?void 0:X.get(Qn(t)),a=xr(r),l=U(),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=Xe(t);qd(o.statusCode,c)?l.markRateLimited(t.provider||"",h,120,c):l.recordFailure(t.provider||"",h,c)}else{let h=Xe(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:Xe(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."}),xt({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:Xe(t),upstreamModel:t.upstreamModel,modelFamily:t.modelFamily||"",scenarioType:t.scenarioType||"default",clientType:t.clientType||De(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(Ca.now()-t.requestStartTime):0,status:y?"error":"success",errorMessage:c,responseBody:u}),t.clientContext?.usageScope!=="session"&&X.delete(Qn(t))}catch(r){console.error("Usage tracking error:",r)}})}var Sa,Pa=w(()=>{"use strict";Fe();Ut();Ft();re();qo();er();tr();St();fr();xa();hr();Gt();Sa=new Xd});var Oa={};Yn(Oa,{createCcrServer:()=>up});import{homedir as op}from"os";import{join as rp}from"path";import{createStream as sp}from"rotating-file-stream";import{CONFIG_FILE as ip,HOME_DIR as ap,listPresets as cp,ensureCcrSelfProvider as lp}from"@wengine-ai/claude-code-router-shared";async function up(n={}){await qi(),await Mn();let e=await pr();lp(e).changed&&(await fe(e),console.log('\u2139\uFE0F Added the preset "ccr" provider pointing at this server (delete it in the UI to remove it for good).'));let o=e.Providers||e.providers||[],r=o&&o.length>0,s=e.HOST||"127.0.0.1";r?(s=e.HOST,e.APIKEY||(s="127.0.0.1")):(s="0.0.0.0",console.log("\u2139\uFE0F No providers configured. Listening on 0.0.0.0 without authentication."));let i=n.port??e.PORT??3456,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:(ra(),e.LOG!==!1?(e.LOG===void 0&&(e.LOG=!0),u={level:e.LOG_LEVEL||"error",stream:sp(c,{path:ap,interval:"1d",size:"50M",compress:!1})}):u=!1);let d=await cp(),p=new Ia({jsonPath:ip,initialConfig:{providers:e.Providers||e.providers,HOST:s,PORT:a,LOG_FILE:rp(op(),".claude-code-router","claude-code-router.log")},logger:u});await p.ready(),await ya(p,e),p.recordUsage=f=>{try{let _=f.req?f.req.clientType||De(f.req):"unknown";xt({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:",_)}},Ea(p,e),p.ccrPreHandlerCallbacks=wa(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 ia(p,e),kt(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 Aa=w(()=>{"use strict";Ma();ea();Nn();sa();aa();_a();Pa();Un();St();Fe()});import dp from"fastify";import pp from"@fastify/cors";function fp(n={}){let e=dp({bodyLimit:52428800,...n});return e.setErrorHandler(jr),e.register(pp),e}async function mp(n={}){return(await Promise.resolve().then(()=>(Aa(),Oa))).createCcrServer(n)}var $r,Ia,V_,Ma=w(()=>{Zn();Et();Gt();Kt();Mo();No();je();Ut();Pn();re();we();Ie();oe();Fe();Gt();$e();Fe();je();Zn();Kt();Mo();No();lr();nr();re();we();Vo();Ie();re();Pn();qe();oe();Nn();fr();Un();gr();hr();St();$r=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=fp({...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 xe(c),this.transformerService=new Be(this.configService,this.app.log),this.tokenizerService=new Je(this.configService,this.app.log),this.readiness=(async()=>{await Promise.all([this.transformerService.initialize(),this.tokenizerService.initialize()]),this.providerService=new ue(this.configService,this.transformerService,this.app.log)})()}async ready(){await this.readiness}async register(e,t){await this.app.register(e,t)}addHook(e,t){this.app.addHook(e,t)}async registerNamespace(e,t){if(!e)throw new Error("name is required");if(await this.ready(),e==="/"&&this.mainNamespaceRegistered)return;let o=this.configService,r=this.transformerService,s=this.providerService,i=this.tokenizerService;if(e!=="/"){if(!t)throw new Error("options is required");o=new xe({initialConfig:{...t,providers:t.Providers||t.providers}}),r=new Be(o,this.app.log),i=new Je(o,this.app.log),await Promise.all([r.initialize(),i.initialize()]),s=new ue(o,r,this.app.log)}let 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&&ct(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),ke(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 Jt(c,u,{configService:o,tokenizerService:i}),c.usageCacheKey&&X.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 zs(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{Xo()}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 Xn()})}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{tt()}catch{}try{ot()}catch{}try{io()}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=We(()=>this.providerService.getProviders(),t,o=>ce(this.configService,o),this.app.log,o=>this.configService.get(o))}catch(t){this.app.log.warn?.(`Failed to start active probe service: ${t.message}`)}}catch(e){this.app.log.error(`Error starting server: ${e}`),process.exit(1)}}},Ia=$r;V_=mp});Ma();export{wn as ActiveProbeService,xe as ConfigService,q as ProjectRoutingError,Nt as ProviderHealthStore,ue as ProviderService,ae as SSEParserTransform,Ge as SSESerializerTransform,Je as TokenizerService,Be as TransformerService,Hn as apiKeyAuth,ke as applyClientAdapter,$n as backupConfigFile,vs as builtinClientAdapterRegistry,Bt as calculateTokenCount,il as clearClientAdapterCaches,Fn as clearProviderHealth,Xn as closeProxyDispatchers,Ji as collectReachableModelKeys,mp as createCcrServer,Ia as default,De as detectClientType,Is as diagnoseResolutionFailure,_e as extractSessionIdFromUserId,Mi as getActiveProbeService,xo as getAllQuotaResults,uo as getAllRateLimitInfo,Cs as getClientAdapter,wt as getConfiguredProxyUrl,U as getHealthStore,K as getProxyDispatcher,Ko as getQuotaAdapter,jt as getQuotaResult,mc as getRateLimitInfo,Pt as getRuntimeDebugLog,V_ as getServer,pr as initConfig,Mn as initDir,io as initProviderHealthPersistence,ot as initQuotaStorePersistence,tt as initRateLimitPersistence,xs as isClientType,Dr as isGlobalProxyEnabled,jn as listAnthropicCompatibleModels,Ln as mergeUsageCapture,Me as normalizeSessionId,Qe as normalizeUsagePayload,pt as pluginManager,ne as readConfigFile,dr as readConfigFileRaw,kt as reconcileHealthStore,En as resetActiveProbeService,ce as resolveProviderProxyUrl,On as rewriteStream,Jt as router,Ns as searchProjectBySession,X as sessionUsageCache,eo as setRuntimeDebugLog,We as startActiveProbe,Xo as stopActiveProbe,vo as storeQuotaResult,An as tokenSpeedPlugin,Zi as usageStore,fe as writeConfigFile};
|
|
421
|
+
Your response should consistently follow this rule whenever image-related analysis is requested.`});let o=e.body.messages.filter(s=>s.role==="user"&&Array.isArray(s.content)&&s.content.some(i=>i.type==="image"||Array.isArray(i.content)&&i.content.some(a=>a.type==="image"))),r=1;o.forEach(s=>{Array.isArray(s.content)&&s.content.forEach(i=>{if(i.type==="image")Zn.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:(Zn.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}})})}},Ha=new Hr});var zr,Ba,qn,Ja=E(()=>{"use strict";za();zr=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}},Ba=new zr;Ba.registerAgent(Ha);qn=Ba});import{EventEmitter as Dp}from"node:events";import{performance as Wa}from"node:perf_hooks";import Fp from"json5";function Up(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 Ga(n,e){(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&xe(n,e)}function Br(n){return n.usageSessionId||n.id}function eo(n){return n.usageCacheKey||`${n.clientType||"unknown"}:request:${Br(n)}`}function Ye(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 jp(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?Ve(e.usage):e.response?.usage?Ve(e.response.usage):void 0,o=e.model||e.response?.model;if(!(!t&&!o))return{usage:t,model:o}}async function Hp(n,e,t){if(await new Promise((r,s)=>{let i=a=>a?s(a):r();Kn(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")){Ga(n,t);let r=eo(n);n.clientContext?.usageScope==="session"&&(n.previousUsage=X.get(r))}}async function zp(n,e,t){if(!n.pathname?.endsWith("/v1/messages"))return;let o=[];for(let r of qn.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 Va(n){return{authClient:(e,t)=>Hp(e,t,n),agent:(e,t)=>zp(e,t,n)}}function Qa(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=Wa.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();Kn(e)(t,o,a).then(()=>s(),i)})}),n.addHook("onError",async(t,o,r)=>{t.errorMessage=r?.message||r?.toString?.()||"Unknown error",Ka.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){Ga(t,e);let l=Br(t),c=eo(t);if(r instanceof ReadableStream){if(i&&t.agents){let h=new AbortController,g=r.pipeThrough(new ae),f,_=-1,k="",y="",b="",T=[],C=[];return s(null,An(g,async(v,S)=>{try{if(v.event==="content_block_start"&&v?.data?.content_block?.name){let x=t.agents.find(w=>qn.getAgent(w)?.tools.get(v.data.content_block.name));if(x){f=qn.getAgent(x),_=v.data.index,k=v.data.content_block.name,b=v.data.content_block.id;return}}if(_>-1&&v.data.index===_&&v.data?.delta?.type==="input_json_delta"){y+=v.data?.delta?.partial_json;return}if(_>-1&&v.data.index===_&&v.data?.type==="content_block_stop"){try{let x=Fp.parse(y);C.push({type:"tool_use",id:b,name:k,input:x});let w=await f?.tools.get(k)?.handler(x,{req:t,config:e});T.push({tool_use_id:b,type:"tool_result",content:w}),f=void 0,_=-1,k="",y="",b=""}catch(x){console.log(x)}return}if(v.event==="message_delta"&&T.length){t.body.messages.push({role:"assistant",content:C}),t.body.messages.push({role:"user",content:T});let x=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(!x.ok)return;let R=x.body.pipeThrough(new ae).getReader();for(;;)try{let{value:O,done:P}=await R.read();if(P)break;let M=O;if(["message_start","message_stop"].includes(M.event))continue;if(!S.desiredSize)break;S.enqueue(M)}catch(O){if(O.name==="AbortError"||O.code==="ERR_STREAM_PREMATURE_CLOSE"){h.abort();break}throw O}return}return v}catch(x){if(console.error("Unexpected error in stream processing:",x),x.code==="ERR_STREAM_PREMATURE_CLOSE"){h.abort();return}throw x}}).pipeThrough(new Ke))}let[d,p]=r.tee(),m=async h=>{let f=h.pipeThrough(new TextDecoderStream).pipeThrough(new ae).getReader();try{for(;;){let{done:_,value:k}=await f.read();if(_)break;let y=k.event,b=k.data;if(y==="message_start"&&b?.message?.model&&(t.upstreamModel=b.message.model),y==="response.completed"&&b?.response?.model&&(t.upstreamModel=b.response.model),b?.usage){let T=X.get(c)||{},C=Ve(b.usage),v=Hn(T,C,y==="message_start");t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_anthropic_sse",event:y,provider:t.provider,model:Ye(t),usageSessionId:l,capturedUsage:C,mergedUsage:v}),(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}),X.put(c,v)}if(b?.response?.usage){let T=Ve(b.response.usage),C=X.get(c)||{},v=Hn(C,T,!1);t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_responses_sse",event:y,provider:t.provider,model:Ye(t),usageSessionId:l,capturedUsage:T,mergedUsage:v}),X.put(c,v)}}}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=m(p),s(null,d)}let u=jp(r);if(u?.usage&&X.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(Ka.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=>{xo(t)}),n.addHook("onResponse",async(t,o)=>{if((t.ccrHookOrder||=[]).push("onResponse"),xo(t),!(!t.pathname?.endsWith("/v1/messages")&&!t.pathname?.endsWith("/v1/responses")))try{t.usageCapturePromise&&await t.usageCapturePromise,t.tokenSpeedCapturePromise&&await t.tokenSpeedCapturePromise;let r=Br(t),s=r,i=t.sessionId||r,a=o.statusCode>=400,l=a?void 0:X.get(eo(t)),c=Mr(s),u=U(),d,p,m=t.sseError,h=m&&(m.type||m.code||m.message),g=l?.output_tokens&&l.output_tokens>0,f=a||h&&!g;if(f){d=t.errorMessage||o.errorMessage||t.error?.message||t.error?.toString?.()||void 0,h&&(d=d||JSON.stringify(m),p=JSON.stringify(m)),p=p||t.errorResponseBody;let T=Ye(t);Up(o.statusCode,d)?u.markRateLimited(t.provider||"",T,120,d):u.recordFailure(t.provider||"",T,d)}else{let T=Ye(t);u.recordSuccess(t.provider||"",T)}let _=l?.cache_read_input_tokens??0,y=l?.input_tokens||0||t.tokenCount||0;!_&&t.tokenCount&&l?.input_tokens&&l.input_tokens<t.tokenCount&&(_=t.tokenCount-l.input_tokens,_&&console.log("[Usage] Inferred cache tokens:",{tokenCount:t.tokenCount,netInput:l.input_tokens,impliedCache:_}));let b=f?0:l?.output_tokens||0;!f&&t.body?.stream&&y===0&&b===0&&t.log?.warn?.({debug_log:!0,reqId:t.id,phase:"usage_zero_stream",provider:t.provider,originalModel:t.originalModel||t.body?.model||"",model:Ye(t),usageSessionId:i,tokenCount:t.tokenCount,cachedUsage:l,note:"Successful streaming request ended with zero usage. Check provider_stream_chunk usage_monitor and usage_capture_* logs."}),vt({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:i,provider:t.provider||"",originalModel:t.originalModel||t.body?.model||"",model:Ye(t),upstreamModel:t.upstreamModel,modelFamily:t.modelFamily||"",scenarioType:t.scenarioType||"default",clientType:t.clientType||De(t),stream:t.body?.stream??!1,inputTokens:y,outputTokens:b,cacheReadInputTokens:_,cacheCreationInputTokens:l?.cache_creation_input_tokens||0,ttft:f||!t.body?.stream?null:c.ttft,tokensPerSecond:f?null:c.tokensPerSecond,durationMs:t.requestStartTime?Math.round(Wa.now()-t.requestStartTime):0,status:f?"error":"success",errorMessage:d,responseBody:p}),t.clientContext?.usageScope!=="session"&&X.delete(eo(t))}catch(r){console.error("Usage tracking error:",r)}})}var Ka,Ya=E(()=>{"use strict";Fe();Ft();Dt();re();sr();ir();ar();Ct();Sr();Ja();Er();Kt();Ka=new Dp});var Xa={};no(Xa,{createCcrServer:()=>Yp});import{homedir as Bp}from"os";import{join as Jp}from"path";import{createStream as Wp}from"rotating-file-stream";import{CONFIG_FILE as Kp,HOME_DIR as Gp,listPresets as Vp,ensureCcrSelfProvider as Qp}from"@wengine-ai/claude-code-router-shared";async function Yp(n={}){await ba(),await Fn();let e=await Cr();Qp(e).changed&&(await _e(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;na();let l=f=>(f>9?"":"0")+f,c=(f,_)=>{let k;f?typeof f=="number"?k=new Date(f):k=f:k=new Date;let y=k.getFullYear()+""+l(k.getMonth()+1),b=l(k.getDate()),T=l(k.getHours()),C=l(k.getMinutes());return`./logs/ccr-${y}${b}${T}${C}${l(k.getSeconds())}${_?`_${_}`:""}.log`},u;n.logger!==void 0?u=n.logger:(Sa(),e.LOG!==!1?(e.LOG===void 0&&(e.LOG=!0),u={level:e.LOG_LEVEL||"error",stream:Wp(c,{path:Gp,interval:"1d",size:"50M",compress:!1})}):u=!1);let d=await Vp(),p=new qa({jsonPath:Kp,initialConfig:{providers:e.Providers||e.providers,HOST:s,PORT:a,LOG_FILE:Jp(Bp(),".claude-code-router","claude-code-router.log")},logger:u});await p.ready(),await Fa(p,e),p.recordUsage=f=>{try{let _=f.req?f.req.clientType||De(f.req):"unknown";vt({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:",_)}},Qa(p,e),p.ccrPreHandlerCallbacks=Va(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[_],y=f.reason instanceof Error?f.reason.message:String(f.reason);p.app?.log?.error?.(`Failed to register preset "${k?.name}": ${y}`)}}),await wa(p,e),yt(e,p.app?.log);let h=f=>{p.app.log.error({err:f},"Uncaught exception")},g=(f,_)=>{p.app.log.error({reason:f,promise:_},"Unhandled rejection")};return process.on("uncaughtException",h),process.on("unhandledRejection",g),p.app.addHook("onClose",async()=>{process.off("uncaughtException",h),process.off("unhandledRejection",g)}),p}var Za=E(()=>{"use strict";ec();Ta();jn();Ra();gr();Ea();Ua();Ya();Jn();Ct();Fe()});import Xp from"fastify";import Zp from"@fastify/cors";function qp(n={}){let e=Xp({bodyLimit:52428800,...n});return e.setErrorHandler(Yr),e.register(Zp),e}async function ef(n={}){return(await Promise.resolve().then(()=>(Za(),Xa))).createCcrServer(n)}var Jr,qa,Vk,ec=E(()=>{ro();wt();Kt();Gt();Uo();Ho();je();Ft();On();re();Oe();Me();oe();Fe();Kt();Ne();Fe();je();ro();Gt();Uo();Ho();Tr();cr();re();Oe();tr();Me();re();On();Ze();oe();jn();Sr();Jn();wr();Er();Ct();Jr=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=qp({...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 Re(c),this.transformerService=new Be(this.configService,this.app.log),this.tokenizerService=new Je(this.configService,this.app.log),this.readiness=(async()=>{await Promise.all([this.transformerService.initialize(),this.tokenizerService.initialize()]),this.providerService=new ue(this.configService,this.transformerService,this.app.log)})()}async ready(){await this.readiness}async register(e,t){await this.app.register(e,t)}addHook(e,t){this.app.addHook(e,t)}async registerNamespace(e,t){if(!e)throw new Error("name is required");if(await this.ready(),e==="/"&&this.mainNamespaceRegistered)return;let o=this.configService,r=this.transformerService,s=this.providerService,i=this.tokenizerService;if(e!=="/"){if(!t)throw new Error("options is required");o=new Re({initialConfig:{...t,providers:t.Providers||t.providers}}),r=new Be(o,this.app.log),i=new Je(o,this.app.log),await Promise.all([r.initialize(),i.initialize()]),s=new ue(o,r,this.app.log)}let 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"),m=d.pathname.endsWith("/v1/responses");if(!p&&!m||!c.body||typeof c.body!="object")return;let h=c.ccrHookOrder||=[];if(h.push("request-normalize"),m&&c.body.input&&!c.body.messages&&at(c.body),c.body.stream===void 0&&(c.body.stream=!1),c.log.info({data:c.body,type:"request body"}),h.push("adapter"),!c.originalModel&&c.body.model&&(c.originalModel=c.body.model),xe(c,o.getAll()),h.push("auth-client"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.authClient(c,u),u.sent))&&(h.push("agent"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.agent(c,u),u.sent)))){h.push("router"),c.body?.model&&await Jt(c,u,{configService:o,tokenizerService:i}),c.usageCacheKey&&X.put(c.usageCacheKey,{input_tokens:0,output_tokens:0,cache_read_input_tokens:0,cache_creation_input_tokens:0}),h.push("provider-model-normalize");try{let g=c.body;if(!g||!g.model)return u.code(400).send({error:"Missing model in request body"});let f=g.model.split(","),_=g.model;if(f.length>1){let[k,...y]=f;_=y.join(","),g.model=_,c.provider=k}c.model=_}catch(g){return c.log.error({error:g},"Error in modelProviderMiddleware:"),u.code(500).send({error:"Internal server error"})}}}),await ei(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{or()}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 oo()})}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{et()}catch{}try{nt()}catch{}try{fo()}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=We(()=>this.providerService.getProviders(),t,o=>ce(this.configService,o),this.app.log,o=>this.configService.get(o))}catch(t){this.app.log.warn?.(`Failed to start active probe service: ${t.message}`)}}catch(e){this.app.log.error(`Error starting server: ${e}`),process.exit(1)}}},qa=Jr;Vk=ef});ec();export{En as ActiveProbeService,Re as ConfigService,q as ProjectRoutingError,Nt as ProviderHealthStore,ue as ProviderService,ae as SSEParserTransform,Ke as SSESerializerTransform,Je as TokenizerService,Be as TransformerService,Kn as apiKeyAuth,xe as applyClientAdapter,Un as backupConfigFile,Ns as builtinClientAdapterRegistry,Bt as calculateTokenCount,El as clearClientAdapterCaches,Bn as clearProviderHealth,oo as closeProxyDispatchers,la as collectReachableModelKeys,ef as createCcrServer,qa as default,De as detectClientType,Js as diagnoseResolutionFailure,ge as extractSessionIdFromUserId,Gi as getActiveProbeService,Po as getAllQuotaResults,yo as getAllRateLimitInfo,Ls as getClientAdapter,Rt as getConfiguredProxyUrl,U as getHealthStore,G as getProxyDispatcher,qo as getQuotaAdapter,Ut as getQuotaResult,$c as getRateLimitInfo,Et as getRuntimeDebugLog,Vk as getServer,Cr as initConfig,Fn as initDir,fo as initProviderHealthPersistence,nt as initQuotaStorePersistence,et as initRateLimitPersistence,$s as isClientType,Gr as isGlobalProxyEnabled,Wn as listAnthropicCompatibleModels,Hn as mergeUsageCapture,me as normalizeSessionId,Ve as normalizeUsagePayload,ut as pluginManager,ne as readConfigFile,xr as readConfigFileRaw,yt as reconcileHealthStore,Pn as resetActiveProbeService,ce as resolveProviderProxyUrl,An as rewriteStream,Jt as router,Gs as searchProjectBySession,X as sessionUsageCache,io as setRuntimeDebugLog,We as startActiveProbe,or as stopActiveProbe,Eo as storeQuotaResult,Ln as tokenSpeedPlugin,ka as usageStore,_e as writeConfigFile};
|
|
422
422
|
//# sourceMappingURL=server.mjs.map
|