@wengine-ai/llms 2.3.2414 → 2.3.2415
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 +1 -0
- package/THIRD_PARTY_NOTICES.md +3 -0
- package/dist/api/plugin-request-state.d.ts +22 -0
- package/dist/api/plugin-response-error.d.ts +5 -0
- package/dist/api/routes.d.ts +2 -0
- package/dist/ccr/plugin-install-routes.d.ts +79 -0
- package/dist/cjs/server.cjs +213 -209
- package/dist/cjs/server.cjs.map +4 -4
- package/dist/esm/server.mjs +213 -209
- package/dist/esm/server.mjs.map +4 -4
- package/dist/index.html +41 -41
- package/dist/provider-plugins/host.d.ts +11 -0
- package/dist/provider-plugins/loader.d.ts +33 -0
- package/dist/provider-plugins/registry.d.ts +39 -0
- package/dist/provider-plugins/transformer-hook.d.ts +3 -0
- package/dist/provider-plugins/types.d.ts +182 -0
- package/dist/server.d.ts +8 -0
- package/dist/services/active-probe.d.ts +2 -1
- package/dist/services/provider-health.d.ts +1 -1
- package/dist/services/provider.d.ts +2 -0
- package/dist/services/proxy.d.ts +19 -1
- package/dist/services/transformer.d.ts +2 -0
- package/dist/transformer/openai.responses.transformer.d.ts +0 -6
- package/dist/types/llm.d.ts +4 -0
- package/package.json +2 -2
- package/tsconfig.tsbuildinfo +0 -1
package/dist/esm/server.mjs
CHANGED
|
@@ -1,319 +1,323 @@
|
|
|
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 fo=Object.defineProperty;var Jc=Object.getOwnPropertyDescriptor;var Wc=Object.getOwnPropertyNames;var Kc=Object.prototype.hasOwnProperty;var ot=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var w=(n,e)=>()=>(n&&(e=n(n=0)),e);var mo=(n,e)=>{for(var t in e)fo(n,t,{get:e[t],enumerable:!0})},Gc=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Wc(e))!Kc.call(n,r)&&r!==t&&fo(n,r,{get:()=>e[r],enumerable:!(o=Jc(e,r))||o.enumerable});return n};var Vc=n=>Gc(fo({},"__esModule",{value:!0}),n);import{Agent as Qc,ProxyAgent as Yc}from"undici";function Zc(n){if(n==null)return{ok:!0};let e=String(n).trim();if(!e)return{ok:!0};if(Xc.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 _s(n){let e=[];for(let t of[...hs,ys]){let o=n[t];if(o==null)continue;let r=Zc(o);r.ok||e.push({key:t,error:r.error})}return e}function ho(){return go||(go=new Qc({connect:{timeout:15e3}})),go}function bs(n){let e=n.get(ys);return(typeof e=="string"?e.trim():"")||void 0}function ks(n){return n?K(n):ho()}function Ut(n){for(let e of hs){let t=n.get(e);if(t==null)continue;let o=String(t).trim();if(o)return o}}function Ts(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 ue(n,e){let t=Ut(n);if(t)return Ts(n.get("PROXY_GLOBAL_ENABLED"))||e?.proxyEnabled===!0||e?.proxy_enabled===!0?t:void 0}function K(n){let e=qc(n),t=Ft.get(e);if(t)return t;let o=new Yc(e);return Ft.set(e,o),o}async function yo(){let n=Array.from(Ft.values());Ft.clear(),await Promise.allSettled(n.map(e=>e.close()))}function qc(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 hs,ys,Xc,Ft,go,Z=w(()=>{"use strict";hs=["HTTPS_PROXY","https_proxy","httpsProxy","PROXY_URL"],ys="SUBSCRIPTION_PROXY_URL",Xc=/\$\{[^}]+\}|\$[A-Z_][A-Z0-9_]*/;Ft=new Map});import{readFileSync as el,existsSync as vs}from"fs";import{join as xs}from"path";import{config as tl}from"dotenv";import nl from"json5";var Pe,_o=w(()=>{"use strict";Z();Pe=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:xs(process.cwd(),this.options.jsonPath);if(vs(e))try{let t=el(e,"utf-8"),o=nl.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:xs(process.cwd(),this.options.envPath);if(vs(e))try{let t=tl({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 Ut(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 Cs(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 jt=w(()=>{"use strict"});function ko(n){bo=n}function Ht(){return bo}function zt(n){return bo||n.get("DEBUG_LOG")===!0}function ws(n){let e=n.get("DEBUG_LOG_OPTIONS");return!e||typeof e!="object"?Ee:{maxBodyLength:e.maxBodyLength??Ee.maxBodyLength,maxStreamChunks:e.maxStreamChunks??Ee.maxStreamChunks}}function Bt(n){let e={};for(let[t,o]of Object.entries(n)){let r=t.toLowerCase();ol.has(r)||rl.test(r)&&!/^(?:x-|anthropic-)?ratelimit-/.test(r)?e[t]="***MASKED***":e[t]=o}return e}function Jt(n,e){return n.length<=e?n:n.slice(0,e)+`...[truncated, total ${n.length} bytes]`}function Ps(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:Bt(t.headers),body:Jt(o,Ee.maxBodyLength)})}function Es(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_response",status:t.status,headers:Bt(t.headers),...t.body!==void 0?{body:Jt(t.body,Ee.maxBodyLength)}:{}})}function Ss(n,e,t,o){let r=sl(o);n.info({debug_log:!0,reqId:e,phase:"provider_stream_chunk",chunkIndex:t,...r?{usage_monitor:r}:{},data:Jt(o,Ee.maxBodyLength)})}function sl(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:Jt(o,512)})}}if(e.length)return e.length===1?e[0]:e}function Rs(n,e,t){n.info({debug_log:!0,reqId:e,phase:"provider_stream_end",totalChunks:t})}function Os(n,e,t,o){(async()=>{let r=n.pipeThrough(new TextDecoderStream).getReader(),s=0;try{let a="";for(;;){let{done:i,value:c}=await r.read();if(i)break;a+=c;let l=a.split(`
|
|
3
3
|
|
|
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.2414"});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]
|
|
4
|
+
`);a=l.pop()||"";for(let u of l)u.trim()&&(s<o.maxStreamChunks?Ss(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++)}a.trim()&&(s<o.maxStreamChunks&&Ss(e,t,s,a.trim()),s++),Rs(e,t,s)}catch(a){console.error(`[debug_log] Error reading debug stream for reqId=${t}:`,a),Rs(e,t,s)}finally{r.releaseLock()}})()}var ol,rl,Ee,bo,rt=w(()=>{"use strict";ol=new Set(["authorization","x-api-key","x-goog-api-key","cookie","set-cookie"]),rl=/token|secret|credential|passwd|password/i,Ee={maxBodyLength:4096,maxStreamChunks:100},bo=!1});function As(n,e,t,o,r){let s=new Headers({"Content-Type":"application/json"});t.headers&&Object.entries(t.headers).forEach(([l,u])=>{u&&s.set(l,u)});let a,i=AbortSignal.timeout(t.TIMEOUT??60*1e3*60);if(t.signal){let l=new AbortController,u=()=>l.abort();t.signal.addEventListener("abort",u),i.addEventListener("abort",u),a=l.signal}else a=i;let c={method:"POST",headers:s,body:JSON.stringify(e),signal:a,...t.redirect?{redirect:t.redirect}:{}};return t.dispatcher?c.dispatcher=t.dispatcher:t.httpsProxy&&(c.dispatcher=K(t.httpsProxy)),r?.debug({reqId:o.req.id,method:"POST",headers:Bt(Object.fromEntries(s.entries())),requestUrl:typeof n=="string"?n:n.toString(),useProxy:il(t.httpsProxy)},"final request"),fetch(typeof n=="string"?n:n.toString(),c)}function il(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 Is=w(()=>{Z();rt()});function al(n,e){return`${n.name}|${e?.name??""}`}function To(n){return n.__ccrPluginRequestState??={budgets:new Map}}function Oe(n,e,t){let o=To(n),r=al(e,t),s=o.budgets.get(r);return s||(s={refreshUsed:!1},o.budgets.set(r,s)),s}function vo(n,e,t,o){return!e||t?.nonRetryable!==!0?!1:(Oe(n,e,o).terminalError=t,!0)}var Ms=w(()=>{"use strict"});var xo=w(()=>{"use strict"});import cl from"fastify";function Ns(n,e){let t=new Error(e.message);return t.code=e.code,t.statusCode=e.httpStatus,e.cooldownMs&&(t.cooldownMs=e.cooldownMs),e.validationUrl&&(t.validationUrl=e.validationUrl),t.plugin=n,t.nonRetryable=!e.retryable,t.pluginClassified=!0,t}var st,ll,So,Wt=w(()=>{"use strict";xo();st=class{plugins=new Map;transformerServices=new Set;disposed=!1;revision=0;attachTransformerService(e){this.assertOpen();let t=[...this.plugins.values()].flatMap(r=>[...r.transformers].map(([s,a])=>{if(e.hasTransformer(s))throw new Error(`transformer "${s}" is already registered`);return[s,e.preparePluginTransformer(a)]})),o=[];try{for(let[r,s]of t)o.push(r),e.registerTransformer(r,s);this.transformerServices.add(e)}catch(r){for(let s of o)e.removeTransformer(s);throw r}}beginActivation(e,t,o,r){if(this.assertOpen(),this.plugins.has(e.name))throw new Error(`plugin "${e.name}" is already registered`);let s={manifest:{...e},directory:r,runtimes:new Map,transformers:new Map,protectors:[],routes:[],preparedRoutes:[]},a="staging",i,c=u=>{try{if(this.assertOpen(),a!=="staging")throw new Error("plugin activation is no longer open");u()}catch(p){throw i=p,p}};return{context:{manifest:s.manifest,log:t,host:o,registerTransformer:u=>c(()=>{let p=typeof u.TransformerName=="string"?u.TransformerName:new u().name;if(typeof p!="string"||!p)throw new Error("plugin transformer has no name");if(s.transformers.has(p))throw new Error(`transformer "${p}" is already registered`);s.transformers.set(p,u)}),registerManagedRuntime:u=>c(()=>{if(!u.name||typeof u.isManaged!="function"||typeof u.signRequest!="function")throw new Error("invalid managed runtime");if(s.runtimes.has(u.name))throw new Error(`managed runtime "${u.name}" is already registered`);s.runtimes.set(u.name,u)}),registerAdminRoutes:u=>c(()=>{s.routes.push(u)}),registerConfigProtector:u=>c(()=>{s.protectors.push(u)}),registerUiCard:u=>c(()=>{if(s.cardPath)throw new Error("plugin card is already registered");if(!/^[A-Za-z0-9][A-Za-z0-9._-]*\.js$/.test(u.cardPath))throw new Error("card must be a single JavaScript file");s.cardPath=u.cardPath}),registerLocales:u=>c(()=>{s.locales=u})},commit:async(u,p)=>{this.assertOpen();let d=this.revision;if(s.routes.length){if(!p)throw new Error("plugin admin routes require a host app");s.preparedRoutes=await this.prepareRoutes(s.routes,p)}if(this.assertOpen(),this.revision!==d)throw new Error("plugin registry changed during activation; retry activation");if(a!=="staging")throw new Error("plugin activation is no longer open");if(i)throw i;if(this.plugins.has(e.name))throw new Error(`plugin "${e.name}" is already registered`);for(let f of this.plugins.values()){for(let g of s.runtimes.keys())if(f.runtimes.has(g))throw new Error(`managed runtime "${g}" is already registered`);for(let g of s.transformers.keys())if(f.transformers.has(g))throw new Error(`transformer "${g}" is already registered`)}let m=[...this.transformerServices].flatMap(f=>[...s.transformers].map(([g,_])=>{if(f.hasTransformer(g))throw new Error(`transformer "${g}" is already registered`);return{service:f,name:g,value:f.preparePluginTransformer(_)}})),h=[];try{for(let f of m)h.push(f),f.service.registerTransformer(f.name,f.value);s.card=u,this.plugins.set(e.name,s),this.revision++,a="committed"}catch(f){for(let g of h)g.service.removeTransformer(g.name);throw a="aborted",f}},get cardPath(){return s.cardPath},abort:()=>{a="aborted"}}}getManagedRuntimeFor(e){for(let t of this.plugins.values())for(let o of t.runtimes.values())try{if(o.isManaged(e))return o}catch{}}getRegisteredRuntimes(){return[...this.plugins.values()].flatMap(e=>[...e.runtimes.values()])}applyConfigProtectors(e,t){for(let o of this.plugins.values())for(let r of o.protectors)r(e,t)}listLoadedPlugins(){return[...this.plugins.values()].map(e=>({name:e.manifest.name,version:e.manifest.version,enabled:!0,...e.card?{cardUrl:`/plugins/${e.manifest.name}/${e.cardPath}`}:{},...e.locales?{locales:e.locales}:{}}))}getPublicCard(e,t){let o=this.plugins.get(e);return o?.cardPath===t&&o.card?Buffer.from(o.card):void 0}async prepareRoutes(e,t){let o=new Set(["get","post","put","patch","delete","head","options","all"]),r=[],s=new Proxy(t,{get:(i,c)=>{if(c==="route")return u=>(r.push(u),s);if(o.has(String(c)))return(u,p,d)=>(r.push({...typeof p=="function"?{}:p,method:c==="all"?["DELETE","GET","HEAD","PATCH","POST","PUT","OPTIONS"]:String(c).toUpperCase(),url:u,handler:d??p}),s);let l=Reflect.get(i,c);return typeof l=="function"?()=>{throw new Error(`plugin route registration cannot call ${String(c)}`)}:l}}),a=cl({logger:!1});try{for(let i of e){let c=i(s);c!==s&&await c}for(let i of this.plugins.values())for(let c of i.preparedRoutes)a.route(c);for(let i of r){let c=Array.isArray(i.method)?i.method:[i.method];for(let l of c)if(t.hasRoute({method:l,url:i.url,constraints:i.constraints}))throw new Error(`plugin route ${l} ${i.url} is already registered`);a.route(i)}return await a.ready(),r}finally{await a.close()}}async registerAllRoutes(e){this.assertOpen();for(let t of this.plugins.values())for(let o of t.preparedRoutes)e.route(o)}dispose(){if(!this.disposed){for(let e of this.transformerServices)for(let t of this.plugins.values())for(let o of t.transformers.keys())e.removeTransformer(o);this.plugins.clear(),this.transformerServices.clear(),this.disposed=!0}}assertOpen(){if(this.disposed)throw new Error("plugin host is disposed")}},ll=new st,So=()=>ll});async function Te(n,e,t,o,r){let s;try{s=n.classifyError?.(e,t,o)}catch{}let a=s&&/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,95}$/.test(s.code)?s.code:"plugin_upstream_error",i=s?{...s,code:a,message:`Provider plugin request failed (${a}).`}:{code:a,message:"Provider plugin returned an invalid upstream response.",httpStatus:e>=400&&e<=599?e:502,retryable:e===0||e>=500};try{await n.noteUpstreamError?.(i)}catch{}let c=Ns(n.name,i);return c.isRateLimit=i.httpStatus===429,c.upstreamStatus=e,r?.errorName&&(c.upstreamErrorName=r.errorName),c}var $s=w(()=>{"use strict";Wt()});var Kt,Ro=w(()=>{Kt="2.3.2415"});var Ls={};mo(Ls,{getFallbackPromotionStore:()=>Ae,resetFallbackPromotionStore:()=>pl});function Ae(n){return ve||(ve=new wo,n&&ve.configure(n)),ve}function pl(){ve&&(ve.clearAll(),ve=null)}var dl,wo,ve,Gt=w(()=>{"use strict";re();dl=600*1e3,wo=class{promotions=new Map;enabled=!0;ttlMs=dl;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,a){if(!this.enabled)return;let i=this.getKey(e,t,o),c=`${r},${s}`;this.promotions.set(i,{promotedModel:c,timestamp:Date.now(),ttlMs:a??this.ttlMs})}getPromotion(e,t,o,r){if(!this.enabled)return null;let s=this.getKey(e,t,o),a=this.promotions.get(s);if(!a)return null;if(Date.now()-a.timestamp>=a.ttlMs)return this.promotions.delete(s),null;let[c,...l]=a.promotedModel.split(","),u=l.join(",");if(!U().isAvailable(c,u))return this.promotions.delete(s),null;let d=r.find(h=>h.name.toLowerCase()===c.toLowerCase()),m=d?.models?.find(h=>String(h).toLowerCase()===u.toLowerCase());return!d||!m||d.enabled===!1?(this.promotions.delete(s),null):`${d.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 Us,mkdirSync as fl,readFileSync as ml,writeFileSync as gl}from"fs";import{join as js}from"path";import{HOME_DIR as hl}from"@wengine-ai/claude-code-router-shared";function kl(n){try{if(!Us(Oo))return;let e=JSON.parse(ml(Oo,"utf-8"));if(!Array.isArray(e))return;let t=Date.now();for(let o of e){if(!o||!o.provider||!o.model)continue;let r=o.updatedAt||o.lastFailureTime||0;r&&t-r>1440*60*1e3||n.restore({provider:o.provider,model:o.model,status:o.status,failureCount:o.failureCount??0,successCount:o.successCount??0,lastFailureTime:o.lastFailureTime??0,lastProbeTime:o.lastProbeTime??0,lastError:o.lastError,rateLimitUntil:o.rateLimitUntil})}}catch{}}function Fs(n){try{let e=n.getAllStates();if(e.length===0)return;Us(Eo)||fl(Eo,{recursive:!0});let t=e.map(o=>({...o,updatedAt:Date.now()}));gl(Oo,JSON.stringify(t,null,2),"utf-8")}catch{}}function Ao(){if(Ds)return;Ds=!0;let n=U();kl(n),bl=setInterval(()=>Fs(n),6e4),process.on("exit",()=>Fs(n))}function U(n){return Po||(Po=new Vt(n)),Po}var yl,_l,Vt,Eo,Oo,Ds,bl,Po,re=w(()=>{"use strict";yl=120,_l={enabled:!0,failureThreshold:3,probeIntervalMinutes:5,halfOpenSuccessThreshold:2},Vt=class{states=new Map;config;probeTimer;constructor(e){this.config={..._l,...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);if(!r||(r.successCount++,r.rateLimitUntil&&r.rateLimitUntil>Date.now()))return;let s=!!r.rateLimitUntil;r.rateLimitUntil=void 0,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),s&&r.status==="half-open"&&r.successCount>=this.config.halfOpenSuccessThreshold&&(this.clearPromotionsForPrimary(e,t),this.states.delete(o))}clearPromotionsForPrimary(e,t){try{let{getFallbackPromotionStore:o}=(Gt(),Vc(Ls));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,o=!1){if(!this.config.enabled)return!0;if(!this.getKey(e,t))return!1;let s=this.getState(e,t);return s?s.status==="open"&&s.rateLimitUntil?!(Date.now()<s.rateLimitUntil):s.status==="open"&&o&&Date.now()-Math.max(s.lastFailureTime,s.lastProbeTime)>=this.config.probeIntervalMinutes*6e4?(s.status="half-open",s.successCount=0,s.lastProbeTime=Date.now(),!0):s.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 a=this.states.get(s);a?(a.status="open",a.lastFailureTime=Date.now(),a.lastError=r||"rate-limited",a.lastProbeTime=0):(a={provider:e,model:t,status:"open",failureCount:0,successCount:0,lastFailureTime:Date.now(),lastProbeTime:0,lastError:r||"rate-limited"},this.states.set(s,a));let i=(o??yl)*1e3;a.rateLimitUntil=Date.now()+i}getRateLimitedModels(){let e=[];for(let[t,o]of this.states)o.status==="open"&&o.rateLimitUntil&&e.push(t);return e}needsProbe(e){return Date.now()-e.lastProbeTime>=this.config.probeIntervalMinutes*60*1e3}markProbeAttempt(e,t){let o=this.getKey(e,t);if(!o)return;let r=this.states.get(o);r&&r.status==="open"&&(r.lastProbeTime=Date.now())}clear(){this.states.clear()}recover(e,t){let o=this.getKey(e,t);o&&this.states.delete(o)}getAllStates(){return Array.from(this.states.values())}restore(e){let t=this.getKey(e.provider,e.model);t&&this.states.set(t,{...e})}stopProbeTimer(){this.probeTimer&&(clearInterval(this.probeTimer),this.probeTimer=void 0)}},Eo=js(hl,"runtime"),Oo=js(Eo,"provider-health.json"),Ds=!1,bl=null;Po=null});import{existsSync as Ws,mkdirSync as Tl,readFileSync as vl,writeFileSync as xl}from"fs";import{join as Ks}from"path";import{HOME_DIR as Cl}from"@wengine-ai/claude-code-router-shared";function it(n,e,t){let o=i=>{if(t instanceof Headers)return t.get(i);let c=Object.keys(t).find(l=>l.toLowerCase()===i);return c?t[c]:null},r=Io(o,Sl),s=Io(o,Rl),a=Io(o,wl);(r||s||a)&&Ie.set(n,{provider:n,remaining:Bs(r),limit:Bs(s),reset:El(a),capturedAt:Date.now()})}function Pl(n){return Ie.get(n)}function $o(){return Array.from(Ie.values())}function Io(n,e){for(let t of e){let o=n(t);if(o)return o}return null}function Bs(n){if(!n)return null;let e=Number(n.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function El(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=Ol(e);return r===null?null:Math.floor((Date.now()+r)/1e3)}function Ol(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]),a=r[2].toLowerCase();if(Number.isFinite(s))switch(a){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 Al(){try{if(!Ws(No))return;let n=JSON.parse(vl(No,"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;Ie.set(t.provider,t)}}catch{}}function Js(){try{if(Ie.size===0)return;Ws(Mo)||Tl(Mo,{recursive:!0}),xl(No,JSON.stringify(Array.from(Ie.values()),null,2),"utf-8")}catch{}}function at(){Hs||(Hs=!0,Al(),zs=setInterval(Js,6e4),zs.unref?.(),process.on("exit",Js))}var Mo,No,Ie,Hs,zs,Sl,Rl,wl,Me=w(()=>{"use strict";Mo=Ks(Cl,"runtime"),No=Ks(Mo,"rate-limit.json"),Ie=new Map,Hs=!1,zs=null,Sl=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],Rl=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],wl=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"]});function Yt(){return Lo||(Lo=new Fo),Lo}function jo(){return Do||(Do=new Uo),Do}function Il(n){return n?.sessionId||n?.clientContext?.stableSessionId||void 0}function Gs(n){let e=Il(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 Vs(n,e){let t=Yt(),o=n?.[Qt];if(o&&!o.released){if(o.key===e)return;t.release(o)}n[Qt]=t.acquire(e)}function Ho(n){let e=n?.[Qt];e&&Yt().release(e)}function Qs(n,e){let t=Gs(n),o=n?.[Qt],r=t?jo().get(t)!==void 0:!1;!o&&!r||(t&&jo().bind(t,e),Vs(n,e))}function Ml(n,e,t){let o=Yt(),r=null,s=1/0;for(let a of e){if(!a||typeof a!="string")continue;let i=t(a);if(!i||i===n)continue;let c=o.count(i);c<s&&(r=i,s=c)}return r}function Ys(n,e,t){let o=Gs(n),r=jo(),s=Yt(),a=Number.isFinite(t.threshold)&&t.threshold>0?Math.floor(t.threshold):3,i;if(o){let c=r.get(o);if(c&&c!==e){let l=t.resolve(c);l?(i=l,t.log?.(`Concurrency priority: session slot ${o} sticking to bound model ${l}`)):r.unbind(o)}else c&&(i=e)}if(!i){let c=s.count(e);if(c>=a){let l=Ml(e,t.fallbackModels,t.resolve);l&&(i=l,t.log?.(`Concurrency priority: model ${e} has ${c} in-flight request(s) (>= ${a}); assigning ${o?`session slot ${o} `:"request "}to overflow model ${l}`))}i=i||e,o&&r.bind(o,i)}return Vs(n,i),i}var Fo,Uo,Lo,Do,Qt,Xt=w(()=>{"use strict";Fo=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()}},Uo=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()}},Lo=null,Do=null;Qt="__concurrencyLease"});async function F(n){let e=await n.text();return Nl(e,n.status)}function Nl(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 Xs(n){return(n.headers.get("Content-Type")||"").includes("text/event-stream")}function ct(n){return/^\s*(event:|data:|:\s*[A-Z])/m.test(n)||n.startsWith("data:")}async function de(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 a=0;a<8;a++){let{value:i,done:c}=await o.read();if(c||(i&&(s+=r.decode(i,{stream:!0})),ct(s))||/^\s*[\[{]/.test(s))break}s+=r.decode()}finally{o.releaseLock()}return e.cancel().catch(()=>{}),{body:t,isSSE:ct(s)}}async function me(n){try{let e=await n.text();if(ct(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=w(()=>{"use strict"});var Ne,zo=w(()=>{"use strict";V();Ne=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 a="";typeof s=="string"?a=s:s&&typeof s=="object"&&"text"in s&&(a=s.text),t.push({role:"system",content:a})}):e.instructions=r.content}if(e.messages.forEach(r=>{if(r.role!=="system"){if(Array.isArray(r.content)){let s=r.content.map(a=>this.normalizeRequestContent(a,r.role)).filter(a=>a!==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("text/event-stream")&&!t.includes("application/json")&&e.body){let o=await de(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)}o?.body&&(e=new Response(o.body,{status:e.status,statusText:e.statusText,headers:e.headers}))}if(t.includes("application/json")){let o=await de(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 me(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(),a="";try{let{value:f,done:g}=await s.read();!g&&f&&(a=new TextDecoder().decode(f))}finally{s.releaseLock()}if(o.cancel().catch(()=>{}),a.includes("message_start")||a.includes("content_block_start"))return new Response(r,{status:e.status,statusText:e.statusText,headers:e.headers});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(r,{status:e.status,statusText:e.statusText,headers:e.headers});let l=new TextDecoder,u=new TextEncoder,p="",d=!1,m=this,h=new ReadableStream({async start(f){let g=r.getReader(),_=-1,b="",y=k=>(k!==b&&(_++,b=k),_);try{for(;;){let{done:k,value:T}=await g.read();if(k){d||f.enqueue(u.encode(`data: [DONE]
|
|
5
5
|
|
|
6
|
-
`));break}let
|
|
6
|
+
`));break}let S=l.decode(T,{stream:!0});p+=S;let v=p.split(/\r?\n/);p=v.pop()||"";for(let R of v)if(R.trim())try{if(R.startsWith("event: "))continue;if(R.startsWith("data: ")){let C=R.slice(5).trim();if(C==="[DONE]"){d=!0,f.enqueue(u.encode(`data: [DONE]
|
|
7
7
|
|
|
8
|
-
`));continue}try{let
|
|
8
|
+
`));continue}try{let x=JSON.parse(C);if(x.type==="response.output_text.delta"){let E={id:x.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:x.response?.model,choices:[{index:y(x.type),delta:{content:x.delta||""},finish_reason:null}]};f.enqueue(u.encode(`data: ${JSON.stringify(E)}
|
|
9
9
|
|
|
10
|
-
`))}else if(
|
|
10
|
+
`))}else if(x.type==="response.output_item.added"&&x.item?.type==="function_call"){let E={id:x.item.call_id||x.item.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:x.response?.model||"gpt-5-codex-",choices:[{index:y(x.type),delta:{role:"assistant",tool_calls:[{index:0,id:x.item.call_id||x.item.id,function:{name:x.item.name||"",arguments:""},type:"function"}]},finish_reason:null}]};f.enqueue(u.encode(`data: ${JSON.stringify(E)}
|
|
11
11
|
|
|
12
|
-
`))}else if(
|
|
12
|
+
`))}else if(x.type==="response.output_item.added"&&x.item?.type==="message"){let E=[];(x.item.content||[]).forEach(P=>{P.type==="output_text"&&E.push({type:"text",text:P.text||""})});let O={role:"assistant"};if(E.length===1&&E[0].type==="text"?O.content=E[0].text:E.length>0&&(O.content=E),O.content){let P={id:x.item.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:x.response?.model,choices:[{index:y(x.type),delta:O,finish_reason:null}]};f.enqueue(u.encode(`data: ${JSON.stringify(P)}
|
|
13
13
|
|
|
14
|
-
`))}}else if(
|
|
14
|
+
`))}}else if(x.type==="response.output_text.annotation.added"){let E={id:x.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:x.response?.model||"gpt-5-codex",choices:[{index:y(x.type),delta:{annotations:[{type:"url_citation",url_citation:{url:x.annotation?.url||"",title:x.annotation?.title||"",content:"",start_index:x.annotation?.start_index||0,end_index:x.annotation?.end_index||0}}]},finish_reason:null}]};f.enqueue(u.encode(`data: ${JSON.stringify(E)}
|
|
15
15
|
|
|
16
|
-
`))}else if(
|
|
16
|
+
`))}else if(x.type==="response.function_call_arguments.delta"){let E={id:x.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:x.response?.model||"gpt-5-codex-",choices:[{index:y(x.type),delta:{tool_calls:[{index:0,function:{arguments:x.delta||""}}]},finish_reason:null}]};f.enqueue(u.encode(`data: ${JSON.stringify(E)}
|
|
17
17
|
|
|
18
|
-
`))}else if(
|
|
18
|
+
`))}else if(x.type==="response.completed"){let E=x.response?.output?.some(P=>P.type==="function_call")?"tool_calls":"stop",O={id:x.response?.id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:x.response?.model||"gpt-5-codex-",choices:[{index:0,delta:{},finish_reason:E}]};f.enqueue(u.encode(`data: ${JSON.stringify(O)}
|
|
19
19
|
|
|
20
|
-
`)),
|
|
20
|
+
`)),d=!0}else if(x.type==="response.reasoning_summary_text.delta"){let E={id:x.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:x.response?.model,choices:[{index:y(x.type),delta:{thinking:{content:x.delta||""}},finish_reason:null}]};f.enqueue(u.encode(`data: ${JSON.stringify(E)}
|
|
21
21
|
|
|
22
|
-
`))}else if(
|
|
22
|
+
`))}else if(x.type==="response.reasoning_summary_part.done"&&x.part){let E={id:x.item_id||"chatcmpl-"+Date.now(),object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:x.response?.model,choices:[{index:_,delta:{thinking:{signature:x.item_id}},finish_reason:null}]};f.enqueue(u.encode(`data: ${JSON.stringify(E)}
|
|
23
23
|
|
|
24
|
-
`))}}catch{f.enqueue(
|
|
25
|
-
`))}}else f.enqueue(
|
|
26
|
-
`))}catch(
|
|
27
|
-
`))}}p.trim()&&f.enqueue(
|
|
28
|
-
`)),
|
|
24
|
+
`))}}catch{f.enqueue(u.encode(R+`
|
|
25
|
+
`))}}else f.enqueue(u.encode(R+`
|
|
26
|
+
`))}catch(C){console.error("Error processing line:",R,C),f.enqueue(u.encode(R+`
|
|
27
|
+
`))}}p.trim()&&f.enqueue(u.encode(p+`
|
|
28
|
+
`)),d||f.enqueue(u.encode(`data: [DONE]
|
|
29
29
|
|
|
30
|
-
`))}catch(
|
|
31
|
-
data: ${JSON.stringify(
|
|
30
|
+
`))}catch(k){console.error("Stream error:",k),f.error(k)}finally{try{g.releaseLock()}catch(k){console.error("Error releasing reader lock:",k)}f.close()}}});return new Response(h,{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"){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(),a=r.getReader(),i="";try{let{value:d,done:m}=await a.read();!m&&d&&(i=new TextDecoder().decode(d))}finally{a.releaseLock()}r.cancel().catch(()=>{});let c=i.includes("message_start")||i.includes("content_block_start"),l=i.includes("chat.completion.chunk");if(i.includes("response.created")||i.includes("response.output_text.delta")||i.includes("response.in_progress")||i.includes("response.output_item.added"))return new Response(s,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}});if(l&&!c){let d=this.convertOpenAIChatStreamToResponsesAPI(s,t);return new Response(d,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}let p=this.convertAnthropicStreamToResponsesAPI(s,t);return new Response(p,{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}}convertOpenAIChatStreamToResponsesAPI(e,t){let o=new TextDecoder,r=new TextEncoder,s=t?.provider?.models?.[0]||"gpt-5.4",a=`resp_${Date.now()}`,i=0,c=!1,l=!1,u="",p=-1,d=[],m=(f,g)=>r.encode(`event: ${f}
|
|
31
|
+
data: ${JSON.stringify(g)}
|
|
32
32
|
|
|
33
|
-
`),h=(g
|
|
34
|
-
`);_=
|
|
33
|
+
`),h=(f,g=[])=>({id:a,object:"response",created_at:Math.floor(Date.now()/1e3),status:f,model:s,output:g,usage:this.buildResponsesUsage()});return new ReadableStream({async start(f){try{let g=e.getReader(),_="",b=!1,y=k=>{if(!b){b=!0;for(let T of d)if(T.type==="function_call"){let S=d.indexOf(T);f.enqueue(m("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:T.call_id||T.id,output_index:S,arguments:T.arguments||""})),T.status="completed",f.enqueue(m("response.output_item.done",{type:"response.output_item.done",output_index:S,item:T}))}for(let T of d)if(T.type==="message"){let S=d.indexOf(T);T.status="completed",f.enqueue(m("response.output_item.done",{type:"response.output_item.done",output_index:S,item:T}))}f.enqueue(m("response.completed",{type:"response.completed",response:{id:a,object:"response",created_at:Math.floor(Date.now()/1e3),status:"completed",model:k?.model||s,output:d.slice(),usage:k?.usage?{...this.buildResponsesUsage(k.usage)}:this.buildResponsesUsage()}}))}};for(;;){let{done:k,value:T}=await g.read();if(k)break;_+=o.decode(T,{stream:!0});let S=_.split(`
|
|
34
|
+
`);_=S.pop()||"";for(let v of S){let R=v.trim();if(!R||R.startsWith("event:")||!R.startsWith("data:"))continue;let C=R.slice(5).trim();if(C==="[DONE]"){y({}),f.enqueue(r.encode(`data: [DONE]
|
|
35
35
|
|
|
36
|
-
`));continue}let
|
|
37
|
-
data: ${JSON.stringify(
|
|
36
|
+
`));continue}let x;try{x=JSON.parse(C)}catch{continue}c||(c=!0,f.enqueue(m("response.created",{type:"response.created",response:h("in_progress")})),f.enqueue(m("response.in_progress",{type:"response.in_progress",response:h("in_progress")})));let E=x.choices?.[0];if(!E)continue;let O=E.delta;if(O?.content){if(!l){l=!0;let P=i++,M={id:`${a}_msg`,type:"message",status:"in_progress",role:"assistant",content:[]};d.push(M),f.enqueue(m("response.output_item.added",{type:"response.output_item.added",output_index:P,item:M}))}f.enqueue(m("response.output_text.delta",{type:"response.output_text.delta",item_id:`${a}_msg`,output_index:i-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,p=i++;let M={id:P.id,type:"function_call",status:"in_progress",call_id:P.id,name:P.function?.name||"",arguments:""};d.push(M),f.enqueue(m("response.output_item.added",{type:"response.output_item.added",output_index:p,item:M}))}if(P.function?.arguments){f.enqueue(m("response.function_call_arguments.delta",{type:"response.function_call_arguments.delta",item_id:u,output_index:p,delta:P.function.arguments}));let M=d.find(N=>N.call_id===u);M&&(M.arguments+=P.function.arguments)}}E.finish_reason&&y(x)}}b||y({}),f.close()}catch(g){f.error(g)}}})}convertAnthropicStreamToResponsesAPI(e,t){let o=new TextDecoder,r=new TextEncoder,s=t?.provider?.models?.[0]||"gpt-5.4",a=`resp_${Date.now()}`,i="",c="",l=-1,u="",p="",d="",m=-1,h="",f="",g=-1,_=0,b=!1,y=!1,k=!1,T=!1,S=null,v=[],R=(M,N)=>r.encode(`event: ${M}
|
|
37
|
+
data: ${JSON.stringify(N)}
|
|
38
38
|
|
|
39
|
-
`),
|
|
40
|
-
`);L=
|
|
39
|
+
`),C=(M,N=[])=>({id:a,object:"response",created_at:Math.floor(Date.now()/1e3),status:M,model:s,output:N,usage:this.buildResponsesUsage()}),x=()=>{b||(b=!0,P.enqueue(R("response.created",{type:"response.created",response:C("in_progress")})),P.enqueue(R("response.in_progress",{type:"response.in_progress",response:C("in_progress")})))},E=(M,N)=>{P.enqueue(R("response.output_item.done",{type:"response.output_item.done",output_index:M,item:{...N,status:"completed"}}))},O=M=>{if(!T){if(T=!0,y){let N=v.find(L=>L.type==="message");N&&(N.content=[{type:"output_text",text:i}],N.status="completed")}for(let N of v)N.type==="function_call"&&(N.status="completed");return R("response.completed",{type:"response.completed",response:{id:a,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 N=e.getReader(),L="";for(;;){let{done:$,value:D}=await N.read();if($)break;L+=o.decode(D,{stream:!0});let H=L.split(`
|
|
40
|
+
`);L=H.pop()||"";for(let fe of H){let j=fe.trim();if(!j||j.startsWith("event:")||!j.startsWith("data:"))continue;let X=j.slice(5).trim();if(X==="[DONE]")continue;let B;try{B=JSON.parse(X)}catch{continue}if(B.type==="message_start"&&(a=B.message?.id||a,x()),B.type==="content_block_start"){let A=B.content_block;if(S=A?.type||null,A?.type==="tool_use"){u=A.id||"",p=A.name||"",d="",m=_++;let W={id:u,type:"function_call",status:"in_progress",call_id:u,name:p,arguments:""};v.push(W),P.enqueue(R("response.output_item.added",{type:"response.output_item.added",output_index:m,item:W}))}if(A?.type==="thinking"&&(h=A.thinking||"",!k)){k=!0,g=_++,f=`rs_${a}`;let W={id:f,type:"reasoning",status:"in_progress",summary:[]};v.push(W),P.enqueue(R("response.output_item.added",{type:"response.output_item.added",output_index:g,item:W}))}if(A?.type==="text"&&(i=A?.text||"",!y)){y=!0,l=_++,c=`${a}_msg`;let W={id:c,type:"message",status:"in_progress",role:"assistant",content:[]};v.push(W),P.enqueue(R("response.output_item.added",{type:"response.output_item.added",output_index:l,item:W}))}}if(B.type==="content_block_delta"){let A=B.delta;if(A?.type==="text_delta"){if(!y){y=!0,l=_++,c=`${a}_msg`;let W={id:c,type:"message",status:"in_progress",role:"assistant",content:[]};v.push(W),P.enqueue(R("response.output_item.added",{type:"response.output_item.added",output_index:l,item:W}))}i+=A.text||"",P.enqueue(R("response.output_text.delta",{type:"response.output_text.delta",item_id:c,output_index:l,content_index:0,delta:A.text||""}))}A?.type==="thinking_delta"&&(h+=A.thinking||"",P.enqueue(R("response.reasoning_summary_text.delta",{type:"response.reasoning_summary_text.delta",item_id:f||`rs_${a}`,output_index:g>=0?g:0,summary_index:0,delta:A.thinking||""}))),A?.type==="input_json_delta"&&(d+=A.partial_json||"",P.enqueue(R("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(B.type==="content_block_stop"){if(S==="tool_use"&&u){P.enqueue(R("response.function_call_arguments.done",{type:"response.function_call_arguments.done",item_id:u,output_index:m,arguments:d}));let A=v.find(W=>W.call_id===u);A&&(A.arguments=d,A.status="completed"),A&&E(m,A)}if(S==="text"&&y){P.enqueue(R("response.output_text.done",{type:"response.output_text.done",item_id:c,output_index:l,content_index:0,text:i}));let A=v.find(W=>W.type==="message");A&&(A.content=[{type:"output_text",text:i}],E(l,A))}if(S==="thinking"&&k){let A=v.find(W=>W.type==="reasoning");A&&(A.summary=[{type:"summary_text",text:h}],E(g,A))}S=null}if(B.type==="message_delta"){let A=O(B.usage);A&&P.enqueue(A)}if(B.type==="message_stop"){let A=O();A&&P.enqueue(A),P.enqueue(r.encode(`data: [DONE]
|
|
41
41
|
|
|
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(`
|
|
42
|
+
`))}}}x();let z=O();z&&P.enqueue(z),L="",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(l=>l.type==="message"),o=e.output?.find(l=>l.type==="function_call"),r;t?.content?.length&&t?.content[0].annotations&&(r=t.content[0].annotations.map(l=>({type:"url_citation",url_citation:{url:l.url||"",title:l.title||"",content:"",start_index:l.start_index||0,end_index:l.end_index||0}}))),this.logger.debug({data:r,type:"url_citation"});let s=null,a=null,i=null;if(t&&t.reasoning&&(i={content:t.reasoning}),t&&t.content){let l=[],u=[];if(t.content.forEach(p=>{if(p.type==="output_text")l.push(p.text||"");else if(p.type==="output_image"){let d=this.buildImageContent({url:p.image_url,mime_type:p.mime_type});d&&u.push(d)}else if(p.type==="output_image_base64"){let d=this.buildImageContent({b64_json:p.image_base64,mime_type:p.mime_type});d&&u.push(d)}}),u.length>0){let p=[];l.length>0&&p.push({type:"text",text:l.join("")}),p.push(...u),s=p}else s=l.join("")}return o&&(a=[{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:a,thinking:i,annotations:r},logprobs:null,finish_reason:a?"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 Bo,q,Zt=w(()=>{"use strict";Bo=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)}},q=new Bo(100)});import{existsSync as ti,mkdirSync as $l,readFileSync as Ll,writeFileSync as Dl}from"fs";import{join as ni}from"path";import{HOME_DIR as Fl}from"@wengine-ai/claude-code-router-shared";function Ko(n,e){n&&$e.set(n,{...e,provider:n,capturedAt:Date.now()})}function qt(n){let e=$e.get(n);return e?{...e}:void 0}function Go(){return Array.from($e.values()).map(n=>({...n}))}function Ul(){try{if(!ti(Wo))return;let n=JSON.parse(Ll(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;$e.set(t.provider,t)}}catch{}}function ei(){try{if($e.size===0)return;ti(Jo)||$l(Jo,{recursive:!0}),Dl(Wo,JSON.stringify(Array.from($e.values()),null,2),"utf-8")}catch{}}function lt(){Zs||(Zs=!0,Ul(),qs=setInterval(ei,6e4),qs.unref?.(),process.on("exit",ei))}var Jo,Wo,$e,Zs,qs,Le=w(()=>{"use strict";Jo=ni(Fl,"runtime"),Wo=ni(Jo,"quota-store.json"),$e=new Map,Zs=!1,qs=null});function ge(n){if(typeof n!="string")return;let e=n.trim();if(!(!e||!jl.test(e)))return e}function he(n){if(!n)return;if(typeof n=="object")return ge(n.session_id??n.sessionId);if(typeof n!="string")return;let e=n.trim();if(!e)return;try{let r=JSON.parse(e),s=ge(r?.session_id??r?.sessionId);if(s)return s}catch{}let t="_session_",o=e.lastIndexOf(t);if(o!==-1)return ge(e.slice(o+t.length))}var jl,De=w(()=>{"use strict";jl=/^[A-Za-z0-9_-]+$/});import{existsSync as si}from"fs";import{readFile as Hl,readdir as zl}from"fs/promises";import{homedir as Bl}from"os";import{isAbsolute as Jl,join as ut,normalize as Wl}from"path";import{openSqliteDatabase as Kl}from"@wengine-ai/claude-code-router-shared";function eu(){if(Fe.size<=oi)return;let n=[...Fe.keys()].slice(0,Fe.size-oi);for(let e of n)Fe.delete(e)}function tu(){if(Ue.size<=ri)return;let n=[...Ue].slice(0,Ue.size-ri);for(let e of n)Ue.delete(e)}function nu(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 ou(n,e){let t;try{t=Kl(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 a=o.get(r,r)?.workspace_path;if(typeof a=="string"&&a.trim())return a.trim()}return null}catch{return null}finally{try{t?.close()}catch{}}}async function ru(n,e){let t;try{t=await zl(n)}catch{return null}for(let o of t)for(let r of e){let s=ut(n,o,`${r}.json`);if(si(s))try{let a=JSON.parse(await Hl(s,"utf8")),i=a?.meta?.workspacePath??a?.workspacePath;if(typeof i=="string"&&i.trim())return i.trim()}catch{}}return null}async function ii(n){let e=typeof n=="string"?n.trim():"";if(!e)return null;let t=Fe.get(e);if(t)return t;let o=async()=>{let r=nu(e);if(r.length===0)return null;let s=ut(Gl(),Vl),a=ut(s,Ql);if(si(a)){let i=ou(a,r);if(i)return i}return ru(ut(s,Yl),r)};try{let r=await o();if(!r&&!Ue.has(e)){Ue.add(e),tu();for(let a=0;a<Xl&&!r;a++)await ql(Zl),r=await o()}if(!r||!Jl(r))return null;let s=Wl(r);return Fe.set(e,s),eu(),s}catch{return null}}var Gl,Vl,Ql,Yl,oi,ri,Xl,Zl,Fe,Ue,ql,ai=w(()=>{"use strict";Gl=()=>process.env.CCR_ZCODE_DIR||ut(Bl(),".zcode"),Vl="v2",Ql="tasks-index.sqlite",Yl="sessions",oi=500,ri=500,Xl=3,Zl=50,Fe=new Map,Ue=new Set,ql=n=>new Promise(e=>setTimeout(e,n))});import{randomUUID as su}from"node:crypto";import{CCR_PROJECT_HEADER as Vo}from"@wengine-ai/claude-code-router-shared";function ui(n){return typeof n=="string"&&n.length<=iu}function au(n){if(!(!n?.headers||typeof n.headers!="object"))for(let e of li){let{value:t}=en(n,e);if(!ui(t))continue;let o=ge(t);if(o)return o}}function lu(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 uu(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 en(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 ci(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 dt(n,e=!0){return{clientType:n,usageScope:"request",supportsExplicitExtendedContext:e}}function Qo(n,e,t=!0){let o=he(n?.body?.metadata?.user_id);return{clientType:e,usageScope:o?"session":"request",stableSessionId:o,supportsExplicitExtendedContext:t}}function pi(n){return typeof n=="string"&&cu.includes(n)}function bu(n,e){return/zcode/i.test(e)?!0:Object.keys(n).some(t=>t.toLowerCase().startsWith("x-zcode"))}function je(n){if(pi(n?.clientType))return n.clientType;let e=uu(n),t=n?.headers||{},o=typeof t["user-agent"]=="string"?t["user-agent"]:"",r=n?.body||{},s=lu(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(en(n,Vo).present||en(n,"x-session-affinity").present)return"pi";if(bu(t,o))return"zcode";let a=t["x-anthropic-billing-header"];if(typeof a=="string"&&a.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 i=n?.originalModel||r.model||"";return/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i.test(i)?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(i).toLowerCase()==="ccr-codex"?"codex":e.endsWith("/v1/messages")?"api":"unknown"}function fi(n){return di[n]}function ku(n){if(typeof n?.__ccrRequestScopeId=="string"&&n.__ccrRequestScopeId)return n.__ccrRequestScopeId;let e=typeof n?.id=="string"&&n.id||su();return n.__ccrRequestScopeId=e,e}function xe(n,e={}){let t=je(n),o=fi(t).createContext(n,e),r=o.usageScope==="session"&&o.stableSessionId?o.stableSessionId:ku(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,ci(n,Vo),t==="pi"&&o.stableSessionId&&(n.__ccrSessionAffinity=o.stableSessionId);for(let a of li)ci(n,a);return n.usageSessionId=r,n.usageCacheKey=`${t}:${s}:${r}`,o}function Tu(){}var li,iu,cu,du,pu,fu,mu,gu,hu,yu,_u,di,He=w(()=>{"use strict";De();li=["x-session-affinity","x-session-id","x-litellm-session-id"],iu=256;cu=["claude-code","zcode","pi","qwen-code","opencode","codex","api","unknown"];du={type:"claude-code",createContext(n){return Qo(n,"claude-code")}},pu={type:"zcode",createContext(n){return Qo(n,"zcode")}},fu={type:"pi",createContext(n){let e=en(n,Vo),t=au(n)||(typeof n?.__ccrSessionAffinity=="string"?n.__ccrSessionAffinity:void 0)||he(n?.body?.metadata?.user_id),o=ui(t)?t:void 0;return{...dt("pi",!1),stableSessionId:o,projectId:e.value,projectHeaderPresent:e.present}}},mu={type:"qwen-code",createContext(n){return Qo(n,"qwen-code")}},gu={type:"opencode",createContext(){return dt("opencode")}},hu={type:"codex",createContext(){return dt("codex")}},yu={type:"api",createContext(){return dt("api")}},_u={type:"unknown",createContext(){return dt("unknown")}},di={"claude-code":du,zcode:pu,pi:fu,"qwen-code":mu,opencode:gu,codex:hu,api:yu,unknown:_u}});import{get_encoding as xu}from"tiktoken";import{readFile as Yo}from"fs/promises";import{opendir as Cu,stat as Su}from"fs/promises";import{join as Xo}from"path";import{CLAUDE_PROJECTS_DIR as mi,HOME_DIR as gi,getClaudeProjectId as hi,isZcodeProjectTakeoverActive as Ru}from"@wengine-ai/claude-code-router-shared";function ee(n,e,t,o){let r=Ti(e,t);if(r.code==="model_unhealthy"){let i=G(e,t,!0);if(i)return n.log.warn(`Strict project target ${e} is in the health fail-pool; retrying it anyway (no fallback available). scenario=${o}`),i}let s=n.sessionId||n.clientContext?.stableSessionId,a=ki[r.code]||502;throw new te(`Project routing error (${o}): ${r.reason}. Project-level Router target: "${e}". Session: "${s||"unknown"}".`,{sessionId:s,configuredTarget:e,code:r.code,statusCode:a})}function nn(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 ft(n){let e=nn(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 Pu(n,e){if(!e||!n)return null;let t=nn(n);if(e[n])return e[n];if(e[t])return e[t];let{family:o}=ft(n);if(o&&e[o])return e[o];for(let[r,s]of Object.entries(e)){let a=nn(r);if(a&&t.includes(a))return s}return null}function Eu(n){return n?(n.input_tokens||0)+(n.cache_read_input_tokens||0)+(n.cache_creation_input_tokens||0):0}function tn(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 pt(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 Ti(n,e){let t=tn(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(l=>l.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 a=s.models?.find(l=>String(l).toLowerCase()===r.toLowerCase());if(!a)return{reason:`model "${r}" not found in provider "${o}"`,code:"model_not_found"};if(!U().isAvailable(s.name,String(a),s.apiKey?.startsWith("ccr-managed-")===!0||s.api_key?.startsWith("ccr-managed-")===!0))return{reason:`model "${r}" in provider "${o}" is temporarily unavailable (health fail-pool)`,code:"model_unhealthy"};let c=qt(s.name);if(c){let l=c.limitDaily!==void 0&&c.usedDailyBalance!==void 0&&c.usedDailyBalance>=c.limitDaily,u=c.totalBalance!==void 0&&c.usedBalance!==void 0&&c.usedBalance>=c.totalBalance;if(l||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,a){let i=tn(n);if(!i)return a?null:n;let{providerName:c,routeModel:l}=i,u=pt(e,c,l);if(!u){if(r===!0&&o&&!t&&s!==!1){let d=Ae(),m=d.getPromotion(c,l,o,e);if(m){let h=tn(m);if(h){let f=pt(e,h.providerName,h.routeModel);if(f)return`${f.provider.name},${f.model}`}d.clear(c,l,o)}}return null}if(r===!0&&o&&!t&&s!==!1){let d=Ae(),m=d.getPromotion(c,l,o,e);if(m){let h=tn(m);if(h){let f=pt(e,h.providerName,h.routeModel);if(f)return`${f.provider.name},${f.model}`}d.clear(c,l,o)}}if(!t&&!U().isAvailable(u.provider.name,u.model,u.provider.apiKey?.startsWith("ccr-managed-")===!0||u.provider.api_key?.startsWith("ccr-managed-")===!0))return null;let p=qt(u.provider.name);if(p){let d=p.limitDaily!==void 0&&p.usedDailyBalance!==void 0&&p.usedDailyBalance>=p.limitDaily,m=p.totalBalance!==void 0&&p.usedBalance!==void 0&&p.usedBalance>=p.totalBalance;if(d||m)return null}return`${u.provider.name},${u.model}`}function Y(n,e,t,o,r,s){let a=U(),i=[t?.[n],o?.[n]];for(let c of i)if(!(!Array.isArray(c)||c.length===0))for(let l of c){let u=G(l,e,!1,n,void 0,void 0,s);if(u)return u}return null}function Ou(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 mt(n){let e=nn(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 vi(n,e,t){return t?.usageScope!=="session"?n:Math.max(n,Eu(e))}function xi(n,e,t){return n?.extendedContextThreshold??e?.extendedContextThreshold??t??2e5}function Au(n,e,t,o,r,s,a,i,c,l,u,p){let d=n.clientContext,m=d?.longContextThreshold??t.longContextThreshold??c??6e4,h=vi(e,r,d),f=n.modelFamily,g=xi(d,t,l),_=d?.supportsExplicitExtendedContext!==!1&&s===!0;if((_||h>g)&&t.extendedContext){let k=G(t.extendedContext,o,!1,"extendedContext",i,p,u);if(k)return n.log.info(`Family: using extended context model (1M+), tokens: ${h}, estimated: ${e}, explicit: ${_}, threshold: ${g}`),{model:k,scenarioType:"extendedContext",isFallback:!1};let T=i?Y("extendedContext",o,t.fallback,a,f,u):null;if(T)return n.log.info(`Family: using extended context fallback model (1M+), tokens: ${h}, estimated: ${e}, explicit: ${_}, threshold: ${g}`),{model:T,scenarioType:"extendedContext",isFallback:!0};if(u){let S=ee(n,t.extendedContext,o,"extendedContext");if(S)return{model:S,scenarioType:"extendedContext",isFallback:!1}}n.log.warn("Family: extendedContext model unavailable (fail pool), skipping")}if(h>m&&(t.longContext||t.fallback?.longContext?.length||a?.longContext?.length)){let k=t.longContext?G(t.longContext,o,!1,"longContext",i,p,u):null;if(k)return n.log.info(`Family: using long context model, tokens: ${h}, estimated: ${e}`),{model:k,scenarioType:"longContext",isFallback:!1};let T=i?Y("longContext",o,t.fallback,a,f,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 S=ee(n,t.longContext,o,"longContext");if(S)return{model:S,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(k=>k.type?.startsWith("web_search"))&&t.webSearch){let k=G(t.webSearch,o,!1,"webSearch",i,p,u);if(k)return{model:k,scenarioType:"webSearch",isFallback:!1};let T=i?Y("webSearch",o,t.fallback,a,f,u):null;if(T)return n.log.info("Family: using webSearch fallback model"),{model:T,scenarioType:"webSearch",isFallback:!0};if(u){let S=ee(n,t.webSearch,o,"webSearch");if(S)return{model:S,scenarioType:"webSearch",isFallback:!1}}n.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(n.body.thinking?.type==="enabled"&&t.think){let k=G(t.think,o,!1,"think",i,p,u);if(k)return{model:k,scenarioType:"think",isFallback:!1};let T=i?Y("think",o,t.fallback,a,f,u):null;if(T)return n.log.info("Family: using think fallback model"),{model:T,scenarioType:"think",isFallback:!0};if(u){let S=ee(n,t.think,o,"think");if(S)return{model:S,scenarioType:"think",isFallback:!1}}n.log.warn("Family: think model unavailable (fail pool), skipping")}if(t.default){let k=G(t.default,o,!1,"default",i,p,u);if(k)return{model:k,scenarioType:"default",isFallback:!1};let T=i?Y("default",o,t.fallback,a,f,u):null;if(T)return n.log.info("Family: using default fallback model"),{model:T,scenarioType:"default",isFallback:!0};if(u){let S=ee(n,t.default,o,"default");if(S)return{model:S,scenarioType:"default",isFallback:!1}}n.log.warn("Family: default model unavailable (fail pool), skipping")}return null}function Lu(){if(Ce.size<=yi)return;let n=[...Ce.keys()].slice(0,Ce.size-yi);for(let e of n)Ce.delete(e)}function Du(){if(ze.size<=_i)return;let n=[...ze].slice(0,ze.size-_i);for(let e of n)ze.delete(e)}var ki,te,ae,on,wu,Iu,rn,Ce,yi,ze,_i,Mu,Nu,$u,bi,Ci,Se=w(()=>{"use strict";Zt();re();Le();Gt();Xt();De();ai();He();ki={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},te=class extends Error{statusCode;code;type;sessionId;configuredTarget;constructor(e,t){super(e),this.name="ProjectRoutingError",this.statusCode=t?.statusCode||ki[t?.code||""]||502,this.code=t?.code||"project_routing_error",this.type="api_error",this.sessionId=t?.sessionId,this.configuredTarget=t?.configuredTarget}};ae=xu("cl100k_base"),on=(n,e,t)=>{let o=0;return Array.isArray(n)&&n.forEach(r=>{typeof r.content=="string"?o+=ae.encode(r.content).length:Array.isArray(r.content)&&r.content.forEach(s=>{if(s.type==="text")o+=ae.encode(s.text).length;else if(s.type==="tool_use")o+=ae.encode(JSON.stringify(s.input)).length;else if(s.type==="tool_result"){let a=s.content;if(Array.isArray(a))for(let i of a)i?.type!=="image"&&(o+=ae.encode(i?.type==="text"?String(i.text??""):JSON.stringify(i)).length);else o+=ae.encode(typeof a=="string"?a:JSON.stringify(a)).length}})}),typeof e=="string"?o+=ae.encode(e).length:Array.isArray(e)&&e.forEach(r=>{r.type==="text"&&(typeof r.text=="string"?o+=ae.encode(r.text).length:Array.isArray(r.text)&&r.text.forEach(s=>{o+=ae.encode(s||"").length}))}),t&&t.forEach(r=>{r.description&&(o+=ae.encode(r.name+r.description).length),r.input_schema&&(o+=ae.encode(JSON.stringify(r.input_schema)).length)}),o},wu=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 te(`Project routing error: invalid managed project id "${String(o)}".`,{configuredTarget:String(o),code:"invalid_project_id"});let r=t?o:n.sessionId?await Ci(n.sessionId):null;if(!r&&!t&&n.clientType==="zcode"&&n.sessionId){let s=await ii(n.sessionId);s&&Ru(s)&&(r=hi(s))}if(r){n.projectId=r;let s=Xo(gi,r,"config.json");if(!t&&n.sessionId){let a=Xo(gi,r,`${n.sessionId}.json`);try{let i=JSON.parse(await Yo(a,"utf8"));if(i&&i.Router&&Object.keys(i.Router).length>0)return i.Router}catch{}}try{let a=JSON.parse(await Yo(s,"utf8"));if(t&&(typeof a?.projectPath!="string"||hi(a.projectPath)!==o))throw new te(`Project routing error: managed project id "${o}" does not match its stored project path.`,{configuredTarget:s,code:"invalid_project_id"});if(a&&a.Router&&Object.keys(a.Router).length>0)return a.Router}catch(a){if(a instanceof te)throw a;if(t&&a?.code==="ENOENT")throw new te(`Project routing error: managed project "${o}" no longer has a project config.`,{configuredTarget:s,code:"project_config_not_found"});if(!a||a.code!=="ENOENT"){let i=n.sessionId||n.clientContext?.stableSessionId;throw new te(`Project routing error: project config.json is malformed or unreadable in project "${r}". ${a.message}. Session: "${i||"unknown"}".`,{sessionId:i,configuredTarget:s,code:"project_config_error",statusCode:500})}}}};Iu=async(n,e,t,o,r,s)=>{let a=t.get("providers")||[],i=r||t.get("Router"),c=i?.enableFallback===!0,l=c?i?.fallback:void 0,u=c&&!s?l||t.get("fallback"):l,p=!s,d=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>`,""),d=v[1])}let{family:m,extended:h,isCcrAlias:f}=ft(n.body.model),g=i?.families?.[m||""];if(g&&i?.enableFamilyRouting){n.log.info(`Using model family routing for: ${m}${h?" (1M)":""}`),n.modelFamily=m,n.familyFallback=g.fallback;let v=Au(n,e,g,a,o,h,u,c,i?.longContextThreshold,i?.extendedContextThreshold,s,p);if(v)return{model:v.model,scenarioType:v.scenarioType}}if(n.body.model.includes(",")&&!s){let v=G(n.body.model,a,!1,"default",c,p,s);if(v)return{model:v,scenarioType:"default"};n.log.warn(`Explicit model ${n.body.model} unavailable (fail pool), trying fallback`);let R=c?Y("default",a,void 0,u,void 0,s):null;if(R)return n.log.info(`Using fallback for explicit model: ${R}`),{model:R,scenarioType:"default"};n.log.warn(`No fallback available for explicit model ${n.body.model}, continuing through routing logic`)}let _=f&&!i?.enableFamilyRouting?null:Pu(n.body.model,i?.models);if(_){let v=G(_,a,!1,"modelMapping",c,p,s);if(v)return n.log.info(`Using mapped model for ${n.body.model}: ${_}`),{model:v,scenarioType:"modelMapping"};let R=c?Y("modelMapping",a,void 0,u,void 0,s):null;if(R)return{model:R,scenarioType:"modelMapping"};if(s){let C=ee(n,_,a,"modelMapping");if(C)return{model:C,scenarioType:"modelMapping"}}n.log.warn(`Mapped model ${_} unavailable (fail pool), skipping`)}let b=n.clientContext,y=vi(e,o,b),k=xi(b,void 0,i?.extendedContextThreshold);if(y>k&&i?.extendedContext){n.log.info(`Using extended context (1M) model due to token count: ${y}, estimated: ${e}, threshold: ${k}`);let v=G(i.extendedContext,a,!1,"extendedContext",c,p,s);if(v)return{model:v,scenarioType:"extendedContext"};n.log.warn(`Extended context model ${i.extendedContext} unavailable (fail pool), trying fallback`);let R=c?Y("extendedContext",a,void 0,u,void 0,s):null;if(R)return{model:R,scenarioType:"extendedContext"};if(s){let C=ee(n,i.extendedContext,a,"extendedContext");if(C)return{model:C,scenarioType:"extendedContext"}}}let T=b?.longContextThreshold??i?.longContextThreshold??6e4;if(y>T&&i?.longContext){n.log.info(`Using long context model due to token count: ${y}, estimated: ${e}, threshold: ${T}`);let v=G(i.longContext,a,!1,"longContext",c,p,s);if(v)return{model:v,scenarioType:"longContext"};n.log.warn(`Long context model ${i.longContext} unavailable (fail pool), trying fallback`);let R=c?Y("longContext",a,void 0,u,void 0,s):null;if(R)return{model:R,scenarioType:"longContext"};if(s){let C=ee(n,i.longContext,a,"longContext");if(C)return{model:C,scenarioType:"longContext"}}}if(!s&&d)return n.subagentOverrideModel=d,{model:d,scenarioType:"default"};if(n.body.model?.includes("claude")&&n.body.model?.includes("haiku")&&i?.background){n.log.info(`Using background model for ${n.body.model}`);let v=G(i.background,a,!1,"background",c,p,s);if(v)return{model:v,scenarioType:"background"};let R=c?Y("background",a,void 0,u,void 0,s):null;if(R)return{model:R,scenarioType:"background"};if(s){let C=ee(n,i.background,a,"background");if(C)return{model:C,scenarioType:"background"}}n.log.warn(`Background model ${i.background} unavailable (fail pool), falling through`)}if(Array.isArray(n.body.tools)&&n.body.tools.some(v=>v.type?.startsWith("web_search"))&&i?.webSearch){let v=G(i.webSearch,a,!1,"webSearch",c,p,s);if(v)return{model:v,scenarioType:"webSearch"};n.log.warn(`WebSearch model ${i.webSearch} unavailable (fail pool), trying fallback`);let R=c?Y("webSearch",a,void 0,u,void 0,s):null;if(R)return{model:R,scenarioType:"webSearch"};if(s){let C=ee(n,i.webSearch,a,"webSearch");if(C)return{model:C,scenarioType:"webSearch"}}}if(n.body.thinking?.type==="enabled"&&i?.think){n.log.info(`Using think model for ${n.body.thinking}`);let v=G(i.think,a,!1,"think",c,p,s);if(v)return{model:v,scenarioType:"think"};n.log.warn(`Think model ${i.think} unavailable (fail pool), trying fallback`);let R=c?Y("think",a,void 0,u,void 0,s):null;if(R)return{model:R,scenarioType:"think"};if(s){let C=ee(n,i.think,a,"think");if(C)return{model:C,scenarioType:"think"}}}if(i?.default){let v=G(i.default,a,!1,"default",c,p,s);if(v)return{model:v,scenarioType:"default"};n.log.warn(`Default model ${i.default} unavailable (fail pool), trying fallback`);let R=c?Y("default",a,void 0,u,void 0,s):null;if(R)return{model:R,scenarioType:"default"};let C=G(i.default,a,!0,"default",c,p,s);if(C)return n.log.warn(`No fallback available; retrying default model ${i.default} despite fail-pool state (strict=${s})`),{model:C,scenarioType:"default"}}if(s){let v=ee(n,i?.default||n.body.model,a,"default");if(v)return{model:v,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},rn=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 wu(n,o),a=s||o.get("Router"),i=a?.enableFallback===!0,c=o.get("providers")||[],l=!!s;n.enableFallback=i,n.strictProjectRouting=l,n.fallbackConfig=l?a?.fallback:a?.fallback||o.get("fallback");let u=n.clientContext?.usageScope==="session"?n.previousUsage??q.get(n.usageCacheKey):void 0,{messages:p,system:d=[],tools:m}=n.body,h=o.get("REWRITE_SYSTEM_PROMPT");if(h&&d.length>1&&d[1]?.text?.includes("<env>")){let f=await Yo(h,"utf-8");d[1].text=`${f}<env>${d[1].text.split("<env>").pop()}`}try{let[f,g]=n.body.model.split(","),_=t.tokenizerService?.getTokenizerConfigForModel(f,g),b;t.tokenizerService?b=(await t.tokenizerService.countTokens({messages:p,system:d,tools:m},_,f)).tokenCount:b=on(p,d,m),n.tokenCount=b;let y,k=l?void 0:o.get("CUSTOM_ROUTER_PATH");if(k)try{y=await ot(k)(n,o.getAll(),{event:r})}catch(R){n.log.error(`failed to load custom router: ${R.message}`)}if(y)n.scenarioType="default";else{let R=await Iu(n,b,o,u,s,!!s);y=R.model,n.scenarioType=R.scenarioType}let{family:T}=ft(n.originalModel||n.body.model),S=a?.enableFamilyRouting&&T?a.families?.[T]:void 0,v=[S?.image,a?.image].filter((R,C,x)=>!!R&&x.indexOf(R)===C);if(v.length>0&&Ou(n)&&!mt(y))if(v.includes(y))n.scenarioType="image";else{let R=v.map(C=>G(C,c,!1,"image",i,!l,l)).find(C=>!!C);if(R)n.log.info(`Using image model fallback for ${y}`),y=R,n.scenarioType="image";else{let C=i?Y("image",c,S?.fallback,n.fallbackConfig,T||void 0,l):null;if(C)n.log.info(`Using image fallback model: ${C}`),y=C,n.scenarioType="image";else if(l){let x=ee(n,v[0],c,"image");x&&(y=x,n.scenarioType="image")}else n.log.warn(`Image models ${v.join(", ")} unavailable (fail pool), keeping ${y}`),n.scenarioType="image"}}if(a?.enableConcurrencyPriority===!0&&!(n.originalModel||"").includes(",")&&!n.subagentOverrideModel&&typeof y=="string"&&y.trim()){let R=n.scenarioType||"default",C=[...(n.familyFallback||{})[R]||[],...(n.fallbackConfig||{})[R]||[]];y=Ys(n,y,{threshold:Number(a?.concurrencyThreshold)||3,fallbackModels:C,resolve:x=>G(x,c,!1,R,i,!l,l),log:x=>n.log.info(x)})}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(f){if(f instanceof te)throw f;if(n.log.error(`Error in router middleware: ${f.message}`),l){let g=n.sessionId||n.clientContext?.stableSessionId;throw new te(`Project routing error: router failure in project mode. ${f.message}. Session: "${g||"unknown"}".`,{sessionId:g,configuredTarget:a?.default,code:"project_router_failure",statusCode:502})}n.body.model=a?.default,n.scenarioType="default"}},Ce=new Map,yi=1e3,ze=new Set,_i=1e3,Mu=3,Nu=50,$u=n=>new Promise(e=>setTimeout(e,n));bi=async n=>{let e=await Cu(mi),t=[];for await(let s of e)s.isDirectory()&&t.push(s.name);let o=t.map(async s=>{let a=Xo(mi,s,`${n}.jsonl`);try{return(await Su(a)).isFile()?s:null}catch{return null}}),r=await Promise.all(o);for(let s of r)if(s)return s;return null},Ci=async n=>{let e=ge(n);if(!e)return null;if(Ce.has(e))return Ce.get(e)||null;try{let t=await bi(e);if(!t&&!ze.has(e)){ze.add(e),Du();for(let o=0;o<Mu&&!t;o++)await $u(Nu),t=await bi(e)}return t?(Ce.set(e,t),Lu(),t):null}catch(t){return console.error("Error searching for project by session:",t),null}}});function gt(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
|
|
45
|
-
`).some(
|
|
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(()=>{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
|
-
|
|
48
|
-
|
|
49
|
-
`)
|
|
50
|
-
|
|
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 Ri(n,e,t,o){let r=n.body;(n.ccrHookOrder||=[]).push("handler"),To(n),o.endPoint==="/v1/responses"&&r?.input&&!r?.messages&>(r),o.endPoint==="/v1/responses"&&!n.provider&&typeof r?.model=="string"&&Fu.test(r.model)&&await rn(n,e,{configService:t.configService,tokenizerService:t.tokenizerService});let s=n.provider;if(!s&&r?.model){let l=r.model.split(",");l.length>1&&(s=l[0],r.model=l.slice(1).join(","))}let a=s?t.providerService.getProvider(s):void 0;if(!a&&r?.model){let l=t.providerService.resolveModelRoute(r.model);l&&(a=l.provider,r.model=l.targetModel)}if(!a&&r?.model){let u=t.configService.get("Router")?.default;if(u&&u.includes(",")){let[p,...d]=u.split(","),m=t.providerService.getProvider(p);m&&(a=m,r.model=d.join(","))}}if(n.originalModel||(n.originalModel=r?.model),!a)throw J(`Provider '${s}' not found`,404,"provider_not_found");n.provider=a.name||s;let i=zu(n),c=o.endPoint==="/v1/messages"&&i;i&&e.header("X-Codex-Detected","true"),c&&e.header("X-Codex-On-Messages","true"),n.isCodexOnMessagesEndpoint=c,n.codexDetected=i;try{o.endPoint==="/v1/responses"&&r.input&&!r.messages&>(r);let l={req:n,isCodexOnMessagesEndpoint:c,pluginRegistry:t.pluginRegistry},{requestBody:u,config:p,bypass:d}=await Pi(r,a,o,n.headers,l);if(u?.messages&&!d&&!n.isTargetAnthropic){let g=u.messages,_=new Set,b=[],y=[];for(let k of g){if(k?.role!=="system"){y.push(k);continue}let T=JSON.stringify(k.content??"");_.has(T)||(_.add(T),b.push(k))}b.length>0&&y.length>0&&(u.messages=[...b,...y])}let m=await Ei(u,p,a,t,d,o,l);try{a?.baseUrl&&m?.headers&&it(s,a.baseUrl,m.headers)}catch{}let h=await Oi(u,m,a,o,d,l);h=await wi(r,h,a.name||s);let f=Ai(h,e,r,t);try{let g=r.model||n.originalModel;s&&g&&U().recordSuccess(s,g)}catch{}return f}catch(l){if(l instanceof te)throw l;if(!vo(n,t.pluginRegistry.getManagedRuntimeFor(a),l,a)&&!n.__retryAttempted){n.__retryAttempted=!0;try{return await Ri(n,e,t,o)}catch(d){if(n.__fallbackExhausted)throw d;l=d}}let p=await Uu(n,e,t,o,l);if(p)return p;throw n.__fallbackExhausted=!0,l}}async function wi(n,e,t){if(!n?.stream||!e?.body)return e;let o=e.body.getReader(),r=new TextDecoder,s=[],a="";for(let l=0;l<8;l++){let u=await o.read();if(u.done||(u.value&&(s.push(u.value),a+=r.decode(u.value,{stream:!0})),a.split(`
|
|
45
|
+
`).some(p=>p.startsWith("data:"))))break}if(a+=r.decode(),s.length===0)throw o.releaseLock(),J(`Provider returned empty streaming response from ${t}`,400,"provider_response_error");if(a.includes("event: error")){o.releaseLock();let l=a.length<500?a:a.slice(0,500);throw J(`Provider ${t} returned error in SSE stream: ${l}`,400,"provider_response_error")}if(a.split(`
|
|
46
|
+
`).filter(l=>l.startsWith("data:")&&l.trim().length>5).length===0){o.releaseLock();let l=a.length<500?a:a.slice(0,500);throw J(`Provider ${t} returned streaming response with no SSE data lines: ${l}`,400,"provider_response_error")}let c=new ReadableStream({start:l=>{for(let p of s)l.enqueue(p);let u=()=>{o.read().then(({done:p,value:d})=>{p?l.close():(l.enqueue(d),u())}).catch(p=>l.error(p))};u()},cancel:()=>{o.cancel()}});return new Response(c,{headers:e.headers,status:e.status,statusText:e.statusText})}async function Uu(n,e,t,o,r){let s=n.scenarioType||"default",a=n.familyFallback,i=n.modelFamily,c=n.strictProjectRouting?n.fallbackConfig:n.fallbackConfig??t.configService.get("fallback"),l=U(),u=n.provider||"",p=n.body.model||"",d=new Set,m=r?.statusCode===429||r?.isRateLimit===!0||r?.rawBody&&sn(r.rawBody)||r?.message&&sn(r.message);if(u&&p&&(m?(l.markRateLimited(u,p,120,r?.message),n.log.warn(`Primary model ${u},${p} rate-limited, will try fallbacks`)):l.recordFailure(u,p,r?.message),d.add(`${u},${p}`),t.recordUsage?.({provider:u,model:p,originalModel:n.originalModel||p,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 f=b=>{let[y,...k]=b.split(","),T=y?.trim(),S=k.join(",").trim();return!T||!S?null:{provider:T,model:S,key:`${T},${S}`}},g=[];if(i){let b=a?.[s];if(Array.isArray(b)&&b.length>0)g.push({name:`${i}/${s}`,models:b});else{let y=n.strictProjectRouting;n.log.warn(`No ${i} fallback configured for ${s}; ${y?"no project fallback available":"will try global fallback"}`)}}if(Array.isArray(c?.[s])&&c[s].length>0&&g.push({name:`global/${s}`,models:c[s]}),g.length===0&&s!=="image"&&n.body?.messages?.some(k=>k.role==="user"&&Array.isArray(k.content)&&k.content.some(T=>T.type==="image"||T.type==="image_url"||Array.isArray(T?.content)&&T.content.some(S=>S.type==="image"||S.type==="image_url")))&&Array.isArray(c?.image)&&c.image.length>0&&(n.log.info(`No fallback for scenario '${s}', but request has images \u2014 trying fallback.image`),g.push({name:"global/image (auto-detected)",models:c.image})),g.length===0)return null;let _=g.reduce((b,y)=>b+y.models.length,0);n.log.warn(`Request failed for ${s}, trying ${_} fallback models across ${g.length} fallback stage(s)`);for(let b of g){n.log.info(`Trying ${b.name} fallback stage with ${b.models.length} models`);for(let y of b.models){let k=f(y);if(!k){n.log.warn(`Fallback model '${y}' is invalid, skipping`);continue}if(d.has(k.key))continue;d.add(k.key);let T=t.providerService.getProviders(),S=pt(T,k.provider,k.model);if(!S){n.log.warn(`Fallback model ${y} is not configured or provider is disabled, skipping`);continue}let v=S.provider.name,R=S.model;if(!l.isAvailable(v,R,S.provider.apiKey?.startsWith("ccr-managed-")===!0)){n.log.warn(`Fallback model ${y} unavailable (fail pool), skipping`);continue}let C=t.pluginRegistry.getManagedRuntimeFor(S.provider);if(C&&Oe(n,C,{name:v}).terminalError)continue;let x;for(let E=1;E<=2;E++)try{n.log.info(`Trying fallback model: ${v},${R}${E>1?" (retry)":""}`);let O={...n.body};O.model=R;let P={...n,provider:v,body:O},M=S.provider,N={pluginRegistry:t.pluginRegistry,req:P,isCodexOnMessagesEndpoint:n.isCodexOnMessagesEndpoint||!1},{requestBody:L,config:z,bypass:$}=await Pi(O,M,o,n.headers,N),D=await Ei(L,z,M,t,$,o,N);try{M?.baseUrl&&D?.headers&&it(v,M.baseUrl,D.headers)}catch{}let H=await Oi(L,D,M,o,$,N);return H=await wi(O,H,v),n.log.info(`Fallback model ${v},${R} succeeded`),l.recordSuccess(v,R),u&&p&&!n.strictProjectRouting&&(Ae().promote(u,p,s,v,R),n.log.info(`Promoted fallback model ${v},${R} for ${u},${p}:${s}`),m?n.log.info(`Rate-limited original model ${u},${p} already marked unavailable with cooldown`):(l.forceOpen(u,p,r?.message),n.log.info(`Marked original model ${u},${p} as unavailable`))),n.provider=v,n.body=O,Qs(n,`${v},${R}`),Ai(H,e,O,t)}catch(O){if(x=O,vo(n,C,O,{name:v}))break;if(E===1){n.log.warn(`Fallback model ${v},${R} failed, retrying once: ${O.message}`);continue}}l.recordFailure(v,R,x?.message),n.log.warn(`Fallback model ${v},${R} failed after retry: ${x?.message}`),t.recordUsage?.({provider:v,model:R,originalModel:n.originalModel||p||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 Pi(n,e,t,o,r){if(typeof e.apiKey=="string"&&e.apiKey.startsWith("ccr-managed-")&&!r.pluginRegistry.getManagedRuntimeFor(e))throw J("Provider plugin missing or disabled; install and enable its provider plugin.",503,"plugin_missing");let s=r.pluginRegistry.getManagedRuntimeFor(e);if(s){let u=Oe(r.req,s,e).terminalError;if(u)throw u;await s.ensureUsable?.()}let a=JSON.parse(JSON.stringify(n)),i=a?.system;if(typeof i=="string"&&i.includes("cch="))a.system=i.replace(/cch=[^;]+;?/g,"cch=ccr-stable;");else if(Array.isArray(i))for(let u=0;u<i.length;u++){let p=i[u],d=typeof p=="string"?p:p?.text??"";if(d.includes("cch=")){let m=d.replace(/cch=[^;]+;?/g,"cch=ccr-stable;");typeof p=="string"?i[u]=m:typeof p?.text=="string"&&(p.text=m)}}let c={},l=!1;if(l=ju(e,t,n),l&&(o instanceof Headers?o.delete("content-length"):delete o["content-length"],c.headers=o),!l&&typeof t.transformRequestOut=="function"){let u=await t.transformRequestOut(a);u.body?(a=u.body,c=u.config||{}):a=u}if(!l&&e.transformer?.use?.length)for(let u of e.transformer.use){if(!u||typeof u.transformRequestIn!="function")continue;let p=await u.transformRequestIn(a,e,r);p.body?(a=p.body,c={...c,...p.config}):a=p}if(!l&&e.transformer?.[n.model]?.use?.length)for(let u of e.transformer[n.model].use)!u||typeof u.transformRequestIn!="function"||(a=await u.transformRequestIn(a,e,r));return{requestBody:a,config:c,bypass:l}}function ju(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 Hu(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 zu(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 Ei(n,e,t,o,r,s,a){let i=o.pluginRegistry.getManagedRuntimeFor(t);return i?.dispatchRequest?i.dispatchRequest({attempt:()=>Si(n,e,t,o,r,s,a),requestBody:n,config:e,provider:t,req:a?.req}):Si(n,e,t,o,r,s,a)}function Be(n){let e=n?.request?.systemInstruction?.parts??n?.systemInstruction?.parts;return Array.isArray(e)?e.map(t=>typeof t?.text=="string"?t.text:"").join(`
|
|
47
|
+
`):""}async function Si(n,e,t,o,r,s,a){let i=o.pluginRegistry.getManagedRuntimeFor(t);i&&(await i.ensureUsable?.(),await i.lockUpstream?.(e,t));let c=e.url||new URL(t.baseUrl);if(!e.TIMEOUT){let d=o.configService.get("API_TIMEOUT_MS");d&&(e.TIMEOUT=typeof d=="string"?parseInt(d,10):d)}if(r&&typeof s.auth=="function"){let d=await s.auth(n,t,a);if(d.body){n=d.body;let m=e.headers||{};d.config?.headers&&(m={...m,...d.config.headers},delete m.host,delete d.config.headers),e={...e,...d.config,headers:m}}else n=d}let l={Authorization:`Bearer ${t.apiKey}`,...e?.headers||{}};i&&(l=await i.signRequest({headers:l,requestBody:n,provider:t,config:e})),Hu(l,a?.req);for(let d in l)(l[d]==="undefined"||["authorization","Authorization"].includes(d)&&l[d]?.includes("undefined"))&&delete l[d];!i?.suppressDebugLog&&zt(o.configService)&&Ps(o.log,a.req.id,{url:String(c),headers:l,body:n});let u,p=()=>As(c,n,{...e,headers:JSON.parse(JSON.stringify(l)),httpsProxy:i?void 0:ue(o.configService,t)},a,i?void 0:o.log);try{u=await p()}catch(d){throw i?await Te(i,0,void 0,Be(n),{errorName:d?.name}):d}if(!u.ok&&i?.authRefresh&&u.status===401&&!Oe(a.req,i,t).refreshUsed){Oe(a.req,i,t).refreshUsed=!0;try{await i.authRefresh.forceRefresh(),l=await i.signRequest({headers:l,requestBody:n,provider:t}),u=await p()}catch{}u.status===401&&(a.req.__retryAttempted=!0)}if(!u.ok){let d;try{d=await u.text()}catch(h){throw i?await Te(i,u.status,void 0,Be(n)):h}if(i){let h=await Te(i,u.status,d,Be(n));throw o.log.error(`[provider_response_error] Plugin(${i.name}) status=${u.status} code=${h.code}`),h}o.log.error(`[provider_response_error] Error from provider(${t.name},${n.model}: ${u.status}): ${d}`);let m=J(`Error from provider(${t.name},${n.model}: ${u.status}): ${d}`,u.status,"provider_response_error");throw m.isRateLimit=u.status===429||sn(d),m.rawBody=d,m}if(u.ok){let d=u.headers.get("content-type")||"",m=n.stream===!0;if(o.log.info(`[hidden-error-check] provider=${t.name} model=${n.model} stream=${n.stream} isStreamRequest=${m} contentType=${d} ok=${u.ok} status=${u.status} hasBody=${!!u.body}`),!m||d.includes("application/json")){let h="";try{if(h=await u.clone().text(),!h||h.trim()==="")throw new SyntaxError("Empty JSON body");if(ct(h))return o.log.info(`[hidden-error-check] body is SSE mislabeled as ${d}, skipping JSON validation`),u;let g=JSON.parse(h);if(i?.isErrorEnvelope?.(g))throw await Te(i,u.status,h,Be(n));if(g&&typeof g=="object"&&g.error&&(typeof g.error=="string"||typeof g.error=="object"&&Object.keys(g.error).length>0)){if(i)throw await Te(i,u.status,h,Be(n));o.log.error(`[provider_response_error] Hidden error from provider(${t.name},${n.model}: ${u.status}): ${h}`);let b=sn(h),y=J(`Error from provider(${t.name},${n.model}: ${u.status}): ${h}`,400,"provider_response_error");throw y.isRateLimit=b,y.rawBody=h,y}}catch(f){if(i)throw f?.pluginClassified===!0?f:await Te(i,u.status,void 0,Be(n));if(f instanceof SyntaxError){o.log.warn(`[provider_response_error] Empty or malformed JSON response from provider(${t.name},${n.model}: 200): "${h}"`);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=h,g}throw f}}}if(!i?.suppressDebugLog&&!n.stream&&zt(o.configService)){let m=await u.clone().text();Es(o.log,a.req.id,{status:u.status,headers:Object.fromEntries(Array.from(u.headers)),body:m})}return u}async function Oi(n,e,t,o,r,s){let a=e;if(!r&&t.transformer?.use?.length)for(let i of Array.from(t.transformer.use).reverse())!i||typeof i.transformResponseOut!="function"||(a=await i.transformResponseOut(a,s));if(!r&&t.transformer?.[n.model]?.use?.length)for(let i of Array.from(t.transformer[n.model].use).reverse())!i||typeof i.transformResponseOut!="function"||(a=await i.transformResponseOut(a,s));if(!r&&o.transformResponseIn&&(s.provider||(s.provider=t),a=await o.transformResponseIn(a,s)),s?.isCodexOnMessagesEndpoint)try{a=await new Ne().transformResponseIn(a,{...s,provider:t})}catch(i){console.error("Failed to apply Responses API conversion for codex client:",i)}return a}async function Ai(n,e,t,o){n.ok||e.code(n.status);let r=Xs(n);if(!r){let s=await de(n);if(s)s.isSSE&&(r=!0),n=new Response(s.body,{status:n.status,statusText:n.statusText,headers:n.headers});else{let a=await me(n);n=a.response,a.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&&!o.pluginRegistry.getManagedRuntimeFor(o.providerService.getProvider(e.request.provider||""))?.suppressDebugLog&&zt(o.configService)){let[s,a]=n.body.tee(),i=ws(o.configService);return Os(s,o.log,e.request.id,i),e.send(a)}return e.send(n.body)}else return F(n)}function sn(n){let e=n.toLowerCase();return e.includes("rate limit")||e.includes("rate_limit")||e.includes("rate_limit_error")||e.includes("too many requests")||e.includes("too many")||e.includes("quota exhausted")||e.includes("qps_limit")||e.includes("token_limit")||e.includes("\u9650\u6D41")||e.includes("\u9891\u7387\u9650\u5236")}function Bu(n){try{return new URL(n),!0}catch{return!1}}var Fu,Ii,an=w(()=>{"use strict";Is();jt();Ms();$s();rt();Ro();re();Me();Gt();Xt();zo();V();Se();Z();Fu=/^ccr-(opus|sonnet|haiku)(\[1m\])?$/i;Ii=async n=>{n.get("/",async()=>({message:"LLMs API",version:Kt})),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)=>Ri(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:a,models:i}=t.body;if(!r?.trim())throw J("Provider name is required",400,"invalid_request");if(!s||!Bu(s))throw J("Valid base URL is required",400,"invalid_request");if(!a?.trim())throw J("API key is required",400,"invalid_request");if(!i||!Array.isArray(i)||i.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 Je,Zo=w(()=>{"use strict";Je=class n{constructor(e,t,o){this.configService=e;this.transformerService=t;this.logger=o;this.initializeCustomProviders()}providers=new Map;modelRoutes=new Map;reload(){let e=new n(this.configService,this.transformerService,this.logger);this.providers=e.providers,this.modelRoutes=e.modelRoutes}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(a=>{if(Array.isArray(a)&&typeof a[0]=="string"){let i=this.transformerService.getTransformer(a[0]);if(i)return new i(a[1])}if(typeof a=="string"){let i=this.transformerService.getTransformer(a);return typeof i=="function"?new i:i}}).filter(a=>typeof a<"u")):Array.isArray(t.transformer[s]?.use)&&(o[s]={use:t.transformer[s].use.map(a=>{if(Array.isArray(a)&&typeof a[0]=="string"){let i=this.transformerService.getTransformer(a[0]);if(i)return new i(a[1])}if(typeof a=="string"){let i=this.transformerService.getTransformer(a);return typeof i=="function"?new i:i}}).filter(a=>typeof a<"u")})});let r=t.default_thinking_level;if(r){let s=this.transformerService.getTransformer("defaultthinking");if(s){let a=new s({level:r});o.use=[a,...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,pluginManaged:t.ccr_managed===!0,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 a=`${o.name},${s}`;this.modelRoutes.delete(a),this.modelRoutes.delete(s)}),t.models.forEach(s=>{let a=`${o.name},${s}`,i={provider:o.name,model:s,fullModel:a};this.modelRoutes.set(a,i),this.modelRoutes.has(s)||this.modelRoutes.set(s,i)})),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(a=>a.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 ye,cn,Mi,Ju,Ni,We=w(()=>{ye=n=>n>0?n<=1024?"low":n<=8192?"medium":n<=16384?"high":"max":"none",cn=n=>n==="none"?0:n==="low"?1024:n==="medium"?8192:n==="high"?16384:32768,Mi=["low","medium","high"],Ju={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 Ju[n.trim().toLowerCase()]}});var qo,$i=w(()=>{qo=(n,e)=>(n.includes("base64")&&(n=n.split("base64").pop(),n.startsWith(",")&&(n=n.slice(1))),`data:${e};base64,${n}`)});function Wu(n){return n.filter(e=>!!e?.function?.name).map(e=>({name:e.function.name,description:e.function.description,input_schema:e.function.parameters}))}function Li(n){let e=[],t=[],o=d=>{if(typeof d=="string"){t.push({type:"text",text:d});return}Array.isArray(d)&&d.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 d=new Map;for(let m=0;m<t.length;m++){let h=t[m],f=h?.text??JSON.stringify(h),g=d.get(f);if(g===void 0){d.set(f,m);continue}!t[g]?.cache_control&&h?.cache_control&&(t[g]=h),t.splice(m,1),m--}};n.messages.forEach(d=>{d.role==="system"?o(d.content):e.push(d)}),r();let s=[];for(let d=0;d<e.length;d++){let m=e[d];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 f={type:"tool_result",tool_use_id:m.tool_call_id,content:h};m.cache_control&&(f.cache_control=m.cache_control);let g=s[s.length-1];g&&g.role==="user"&&Array.isArray(g.content)?g.content.push(f):s.push({role:"user",content:[f]})}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(g=>{g&&typeof g=="object"&&g.type==="image_url"&&g.image_url?.url?h.push({type:"image",source:{type:"base64",media_type:g.media_type||"image/jpeg",data:g.image_url.url.split(",")[1]||g.image_url.url}}):h.push(g)});let f=s[s.length-1];f&&f.role==="user"&&Array.isArray(f.content)?f.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(g=>{let _={};try{_=JSON.parse(g.function.arguments)}catch{_=g.function.arguments}h.push({type:"tool_use",id:g.id,name:g.function.name,input:_})}),m.thinking&&h.unshift({type:"thinking",thinking:m.thinking.content,signature:m.thinking.signature});let f=s[s.length-1];f&&f.role==="assistant"&&Array.isArray(f.content)?f.content.push(...h):s.push({role:"assistant",content:h.length>0?h:[{type:"text",text:" "}]})}}let a=()=>{let d=t.filter(m=>m?.cache_control).length;for(let m of s)Array.isArray(m.content)&&(d+=m.content.filter(h=>h?.cache_control).length);return d},i=d=>d&&typeof d=="object"?{...d}:{type:"ephemeral"},c=()=>{for(let d=s.length-1;d>=0;d--){let m=s[d]?.content;if(!Array.isArray(m))continue;let h=m.find(f=>f?.cache_control);if(h?.cache_control)return h.cache_control}for(let d=t.length-1;d>=0;d--)if(t[d]?.cache_control)return t[d].cache_control;return{type:"ephemeral"}},l=(d,m)=>{if(!Array.isArray(d?.content)||d.content.some(f=>f?.cache_control))return!1;let h=d.content.find(f=>f&&typeof f=="object"&&!f.cache_control);return h?(h.cache_control=i(m),!0):!1};(()=>{let d=4-a();if(d<=0||s.length===0)return;let m=s,h=c(),f=[];for(let g=m.length-1;g>=0;g--)m[g]?.role!=="user"||!Array.isArray(m[g].content)||f.push(g);for(let g of f){if(d<=0)break;l(m[g],h)&&d--}})();let p={messages:s,model:n.model,max_tokens:n.max_tokens||4096,temperature:n.temperature,stream:n.stream};if(n.reasoning?.enabled){let d=n.reasoning.max_tokens||cn(n.reasoning.effort||"medium"),m=Math.min(d,p.max_tokens-1024);m>=1024&&(p.thinking={type:"enabled",budget_tokens:m});let h=n.reasoning.effort&&n.reasoning.effort!=="none"?n.reasoning.effort:ye(d);Mi.includes(h)&&(p.output_config={...p.output_config||{},effort:h})}if(t.length>0){let d=t.some(m=>m?.cache_control);p.system=d?t:t.map(m=>m.text||"").join(`
|
|
48
|
+
|
|
49
|
+
`)}return n.tools&&n.tools.length>0&&(p.tools=Wu(n.tools),n.tool_choice&&(n.tool_choice==="auto"?p.tool_choice={type:"auto"}:n.tool_choice==="any"||n.tool_choice==="required"?p.tool_choice={type:"any"}:typeof n.tool_choice=="string"?p.tool_choice={type:"tool",name:n.tool_choice}:typeof n.tool_choice=="object"&&n.tool_choice.type==="function"&&(p.tool_choice={type:"tool",name:n.tool_choice.function.name}))),p}var Di=w(()=>{We()});import{v4 as er}from"uuid";function Ku(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 ln,Fi=w(()=>{"use strict";We();jt();V();$i();Di();Se();ln=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 i=e.system.filter(c=>c.type==="text"&&c.text).map(c=>({type:"text",text:c.text,cache_control:c.cache_control}));t.push({role:"system",content:i})}}let o=JSON.parse(JSON.stringify(e.messages||[]));o?.forEach(i=>{i.role==="system"&&t.push({role:"system",content:i.content})}),o?.forEach(i=>{if(i.role!=="system"&&(i.role==="user"||i.role==="assistant")){if(typeof i.content=="string"){t.push({role:i.role,content:i.content});return}if(Array.isArray(i.content)){if(i.role==="user"){let c=i.content.filter(u=>u.type==="tool_result"&&u.tool_use_id);if(c.length){let u=mt(String(e.model??"")),p=[];c.forEach(d=>{let m=Array.isArray(d.content)?d.content:null,h=m?m.filter(_=>_?.type==="image"&&_?.source):[],f=typeof d.content=="string"?d.content:JSON.stringify(d.content);if(h.length>0){let _=m.filter(b=>!(b?.type==="image"&&b?.source)).map(b=>b?.type==="text"?String(b.text??""):JSON.stringify(b)).filter(b=>b.length>0).join(`
|
|
50
|
+
`);u?(f=_||"[tool image]",p.push(...h)):(f=[_,...h.map(()=>"(tool image omitted: target model does not support image input)")].filter(b=>b.length>0).join(`
|
|
51
|
+
`),this.logger?.debug?.({type:"tool_image_omitted",model:String(e.model??""),count:h.length}))}let g={role:"tool",content:f,tool_call_id:d.tool_use_id,cache_control:d.cache_control};t.push(g)}),p.length>0&&t.push({role:"user",content:p.map(d=>({type:"image_url",image_url:{url:d.source?.type==="base64"?qo(d.source.data,d.source.media_type):d.source.url},media_type:d.source.media_type}))})}let l=i.content.filter(u=>u.type==="text"&&u.text||u.type==="image"&&u.source);l.length&&t.push({role:"user",content:l.map(u=>u?.type==="image"?{type:"image_url",image_url:{url:u.source?.type==="base64"?qo(u.source.data,u.source.media_type):u.source.url},media_type:u.source.media_type}:u)})}else if(i.role==="assistant"){let c={role:"assistant",content:""},l=i.content.filter(d=>d.type==="text"&&d.text);l.length&&(c.content=l.map(d=>d.text).join(`
|
|
52
|
+
`));let u=i.content.filter(d=>d.type==="tool_use"&&d.id);u.length&&(c.tool_calls=u.map(d=>({id:d.id,type:"function",function:{name:d.name,arguments:JSON.stringify(d.input||{})}})));let p=i.content.find(d=>d.type==="thinking");p&&(c.thinking={content:p.thinking,signature:p.signature}),t.push(c)}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),a=typeof e.thinking?.budget_tokens=="number"?e.thinking.budget_tokens:void 0;if(e.thinking!==void 0||s!==void 0){let i={},l=Ku(e.thinking?.type)??(s!==void 0||(a??0)>0);l!==void 0&&(i.enabled=l),s!==void 0?i.effort=s:a!==void 0&&(i.effort=ye(a)),a!==void 0&&a>0&&(i.max_tokens=a),Object.keys(i).length>0&&(r.reasoning=i)}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=Li(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 de(e);if((r?.isSSE??!1)&&r?.body){let l=await this.convertOpenAIStreamToAnthropic(r.body,t);return new Response(l,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}let a;if(r?.body)a=new Response(r.body,{status:e.status,statusText:e.statusText,headers:e.headers});else{let l=await me(e);if(l.isSSE){let u=await this.convertOpenAIStreamToAnthropic(l.response.body,t);return new Response(u,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}a=l.response}let i=await F(a),c=this.convertOpenAIResponseToAnthropic(i,t);return new Response(JSON.stringify(c),{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,a=`msg_${Date.now()}`,i=null,c="unknown",l=!1,u=!1,p=!1,d=new Map,m=new Map,h=0,f=0,g=0,_=!1,b=!1,y=0,k=-1,T=()=>{let C=y;return y++,C},S=C=>{if(!_)try{r.enqueue(C);let x=new TextDecoder().decode(C);this.logger.debug({reqId:t.req.id,data:x,type:"send data"})}catch(x){if(x instanceof TypeError&&x.message.includes("Controller is already closed"))_=!0;else throw this.logger.debug({reqId:t.req.id,error:x instanceof Error?x.message:String(x),type:"send data error"}),x}},v=()=>{if(!_)try{if(!l){this.logger?.warn("[empty_stream] Upstream produced no data, closing silently"),r.close(),_=!0;return}if(k>=0){let x={type:"content_block_stop",index:k};S(s.encode(`event: content_block_stop
|
|
53
|
+
data: ${JSON.stringify(x)}
|
|
51
54
|
|
|
52
|
-
`)),
|
|
53
|
-
data: ${JSON.stringify(
|
|
55
|
+
`)),k=-1}if(i)S(s.encode(`event: message_delta
|
|
56
|
+
data: ${JSON.stringify(i)}
|
|
54
57
|
|
|
55
|
-
`)),
|
|
56
|
-
data: ${JSON.stringify({type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{input_tokens:
|
|
58
|
+
`)),i=null;else{let x=t?.req?.tokenCount||0;S(s.encode(`event: message_delta
|
|
59
|
+
data: ${JSON.stringify({type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{input_tokens:x,output_tokens:0,cache_read_input_tokens:0}})}
|
|
57
60
|
|
|
58
|
-
`))}let
|
|
59
|
-
data: ${JSON.stringify(
|
|
61
|
+
`))}let C={type:"message_stop"};S(s.encode(`event: message_stop
|
|
62
|
+
data: ${JSON.stringify(C)}
|
|
60
63
|
|
|
61
|
-
`)),r.close(),_=!0}catch(
|
|
62
|
-
`);
|
|
63
|
-
data: ${JSON.stringify(
|
|
64
|
+
`)),r.close(),_=!0}catch(C){if(C instanceof TypeError&&C.message.includes("Controller is already closed"))_=!0;else throw C}},R=null;try{R=e.getReader();let C=new TextDecoder,x="";for(;!_;){let{done:E,value:O}=await R.read();if(E)break;x+=C.decode(O,{stream:!0});let P=x.split(`
|
|
65
|
+
`);x=P.pop()||"";for(let M of P){if(_)break;if(!M.startsWith("data:"))continue;let N=M.slice(5).trim();if(this.logger.debug({reqId:t.req.id,type:"recieved data",data:N}),N!=="[DONE]")try{let L=JSON.parse(N);if(h++,this.logger.debug({reqId:t.req.id,response:L,tppe:"Original Response"}),L.error){t?.req&&(t.req.sseError=L.error);let $={type:"error",message:{type:"api_error",message:JSON.stringify(L.error)}};S(s.encode(`event: error
|
|
66
|
+
data: ${JSON.stringify($)}
|
|
64
67
|
|
|
65
|
-
`));continue}if(
|
|
68
|
+
`));continue}if(c=L.model||c,!l&&!_&&!p){l=!0;let $=t?.req?.tokenCount||0,D={type:"message_start",message:{id:a,type:"message",role:"assistant",content:[],model:c,stop_reason:null,stop_sequence:null,usage:{input_tokens:$,output_tokens:0}}};S(s.encode(`event: message_start
|
|
66
69
|
data: ${JSON.stringify(D)}
|
|
67
70
|
|
|
68
|
-
`))}let
|
|
71
|
+
`))}let z=L.choices?.[0];if(L.usage){let $=L.usage?.prompt_tokens_details?.cached_tokens??L.usage?.prompt_cache_hit_tokens??0,H=(L.usage?.prompt_tokens||0)-$,fe=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:H,output_tokens:fe,cache_read_input_tokens:$},choicesLength:Array.isArray(L.choices)?L.choices.length:void 0,finishReason:z?.finish_reason});let j={input_tokens:H,output_tokens:fe,cache_read_input_tokens:$};if(!i)i={type:"message_delta",delta:{stop_reason:"end_turn",stop_sequence:null},usage:{...j}};else{let X=i.usage||{};i.usage={input_tokens:j.input_tokens||X.input_tokens||0,output_tokens:j.output_tokens||X.output_tokens||0,cache_read_input_tokens:j.cache_read_input_tokens||X.cache_read_input_tokens||0}}}if(!z)continue;if(z?.delta?.thinking&&!_&&!p&&!u){if(!b){let $=T(),D={type:"content_block_start",index:$,content_block:{type:"thinking",thinking:""}};S(s.encode(`event: content_block_start
|
|
69
72
|
data: ${JSON.stringify(D)}
|
|
70
73
|
|
|
71
|
-
`)),b
|
|
72
|
-
data: ${JSON.stringify(
|
|
74
|
+
`)),k=$,b=!0}if(z.delta.thinking.signature){let $={type:"content_block_delta",index:k,delta:{type:"signature_delta",signature:z.delta.thinking.signature}};S(s.encode(`event: content_block_delta
|
|
75
|
+
data: ${JSON.stringify($)}
|
|
73
76
|
|
|
74
|
-
`));let D={type:"content_block_stop",index:
|
|
77
|
+
`));let D={type:"content_block_stop",index:k};S(s.encode(`event: content_block_stop
|
|
75
78
|
data: ${JSON.stringify(D)}
|
|
76
79
|
|
|
77
|
-
`)),
|
|
78
|
-
data: ${JSON.stringify(
|
|
80
|
+
`)),k=-1}else if(z.delta.thinking.content){let $={type:"content_block_delta",index:k,delta:{type:"thinking_delta",thinking:z.delta.thinking.content||""}};S(s.encode(`event: content_block_delta
|
|
81
|
+
data: ${JSON.stringify($)}
|
|
79
82
|
|
|
80
|
-
`))}}if(
|
|
83
|
+
`))}}if(z?.delta?.content&&!_&&!p){if(f++,k>=0&&!u){let D={type:"content_block_stop",index:k};S(s.encode(`event: content_block_stop
|
|
81
84
|
data: ${JSON.stringify(D)}
|
|
82
85
|
|
|
83
|
-
`)),
|
|
86
|
+
`)),k=-1}if(!u&&!p){u=!0;let $=T(),D={type:"content_block_start",index:$,content_block:{type:"text",text:""}};S(s.encode(`event: content_block_start
|
|
84
87
|
data: ${JSON.stringify(D)}
|
|
85
88
|
|
|
86
|
-
`)),
|
|
87
|
-
data: ${JSON.stringify(
|
|
89
|
+
`)),k=$}if(!_&&!p){let $={type:"content_block_delta",index:k,delta:{type:"text_delta",text:z.delta.content}};S(s.encode(`event: content_block_delta
|
|
90
|
+
data: ${JSON.stringify($)}
|
|
88
91
|
|
|
89
|
-
`))}}if(
|
|
90
|
-
data: ${JSON.stringify(
|
|
92
|
+
`))}}if(z?.delta?.annotations?.length&&!_&&!p){if(k>=0&&u){let $={type:"content_block_stop",index:k};S(s.encode(`event: content_block_stop
|
|
93
|
+
data: ${JSON.stringify($)}
|
|
91
94
|
|
|
92
|
-
`)),
|
|
93
|
-
data: ${JSON.stringify(
|
|
95
|
+
`)),k=-1,u=!1}z?.delta?.annotations.forEach($=>{let D=T(),H={type:"content_block_start",index:D,content_block:{type:"web_search_tool_result",tool_use_id:`srvtoolu_${er()}`,content:[{type:"web_search_result",title:$.url_citation.title,url:$.url_citation.url}]}};S(s.encode(`event: content_block_start
|
|
96
|
+
data: ${JSON.stringify(H)}
|
|
94
97
|
|
|
95
|
-
`));let
|
|
96
|
-
data: ${JSON.stringify(
|
|
98
|
+
`));let fe={type:"content_block_stop",index:D};S(s.encode(`event: content_block_stop
|
|
99
|
+
data: ${JSON.stringify(fe)}
|
|
97
100
|
|
|
98
|
-
`)),
|
|
99
|
-
data: ${JSON.stringify(
|
|
101
|
+
`)),k=-1})}if(z?.delta?.tool_calls&&!_&&!p){g++;let $=new Set;for(let D of z.delta.tool_calls){if(_)break;let H=D.index??0;if($.has(H))continue;if($.add(H),!m.has(H)){if(k>=0){let Bc={type:"content_block_stop",index:k};S(s.encode(`event: content_block_stop
|
|
102
|
+
data: ${JSON.stringify(Bc)}
|
|
100
103
|
|
|
101
|
-
`)),
|
|
104
|
+
`)),k=-1}let j=T();m.set(H,j);let X=D.id||`call_ccr_${H}`,B=D.function?.name||`tool_${H}`,A={type:"content_block_start",index:j,content_block:{type:"tool_use",id:X,name:B,input:{}}};S(s.encode(`event: content_block_start
|
|
102
105
|
data: ${JSON.stringify(A)}
|
|
103
106
|
|
|
104
|
-
`)),
|
|
105
|
-
data: ${JSON.stringify(
|
|
107
|
+
`)),k=j;let W={id:X,name:B,arguments:"",contentBlockIndex:j};d.set(H,W)}else if(D.id&&D.function?.name){let j=d.get(H);j.id.startsWith("call_")&&j.name.startsWith("tool_")&&(j.id=D.id,j.name=D.function.name)}if(D.function?.arguments&&!_&&!p){let j=m.get(H);if(j===void 0)continue;let X=d.get(H);X&&(X.arguments+=D.function.arguments);try{let B={type:"content_block_delta",index:j,delta:{type:"input_json_delta",partial_json:D.function.arguments}};S(s.encode(`event: content_block_delta
|
|
108
|
+
data: ${JSON.stringify(B)}
|
|
106
109
|
|
|
107
|
-
`))}catch{try{let
|
|
110
|
+
`))}catch{try{let B=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:B}};S(s.encode(`event: content_block_delta
|
|
108
111
|
data: ${JSON.stringify(A)}
|
|
109
112
|
|
|
110
|
-
`))}catch(
|
|
111
|
-
data: ${JSON.stringify(
|
|
113
|
+
`))}catch(B){console.error(B)}}}}}if(z?.finish_reason&&!_&&!p){if(f===0&&g===0&&console.error("Warning: No content in the stream response!"),k>=0){let $={type:"content_block_stop",index:k};S(s.encode(`event: content_block_stop
|
|
114
|
+
data: ${JSON.stringify($)}
|
|
112
115
|
|
|
113
|
-
`)),
|
|
114
|
-
`)||"",u={id:o.responseId,choices:[{finish_reason:o.candidates[0].finishReason?.toLowerCase()||null,index:0,message:{content:
|
|
116
|
+
`)),k=-1}if(!_){let D={stop:"end_turn",length:"max_tokens",tool_calls:"tool_use",content_filter:"stop_sequence"}[z.finish_reason]||"end_turn";i?i.delta={...i.delta,stop_reason:D,stop_sequence:null}:i={type:"message_delta",delta:{stop_reason:D,stop_sequence:null},usage:{input_tokens:0,output_tokens:0,cache_read_input_tokens:0}}}p=!0}}catch(L){this.logger?.error(`parseError: ${L.name} message: ${L.message} stack: ${L.stack} data: ${N}`)}}}v()}catch(C){if(!_)try{r.error(C)}catch(x){console.error(x)}}finally{if(R)try{R.releaseLock()}catch(C){console.error(C)}}},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 c=`srvtoolu_${er()}`;r.push({type:"server_tool_use",id:c,name:"web_search",input:{query:""}}),r.push({type:"web_search_tool_result",tool_use_id:c,content:o.message.annotations.map(l=>({type:"web_search_result",url:l.url_citation.url,title:l.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(c=>{let l={};try{let u=c.function.arguments||"{}";typeof u=="object"?l=u:typeof u=="string"&&(l=JSON.parse(u))}catch{l={text:c.function.arguments||""}}r.push({type:"tool_use",id:c.id,name:c.function.name,input:l})}),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,a=(e.usage?.prompt_tokens||0)-s,i={id:e.id?.startsWith("msg_")?e.id:`msg_${er().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:a,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:i},"Conversion complete, final Anthropic response"),i}catch{throw J(`Provider error: ${JSON.stringify(e)}`,500,"provider_error")}}}});function Gu(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(Ke).includes(o)?o:Ke.TYPE_UNSPECIFIED}else{e.anyOf=[];for(let o of t){let r=o.toUpperCase();e.anyOf.push({type:Object.values(Ke).includes(r)?r:Ke.TYPE_UNSPECIFIED})}}}function ht(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)&&Gu(n.type,e);for(let[a,i]of Object.entries(n))if(i!=null)if(a=="type"){if(i==="null")throw new Error("type: null can not be the only possible type for the field.");if(Array.isArray(i))continue;let c=i.toUpperCase();e.type=Object.values(Ke).includes(c)?c:Ke.TYPE_UNSPECIFIED}else if(t.includes(a))e[a]=ht(i);else if(o.includes(a)){let c=[];for(let l of i){if(l.type=="null"){e.nullable=!0;continue}c.push(ht(l))}e[a]=c}else if(r.includes(a)){let c={};for(let[l,u]of Object.entries(i))c[l]=ht(u);e[a]=c}else{if(a==="additionalProperties")continue;e[a]=i}return e}function Vu(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=ht(e.parameters)),e.response&&(Object.keys(e.response).includes("$schema")?e.responseJsonSchema||(e.responseJsonSchema=e.response,delete e.response):e.response=ht(e.response));return n}function un(n){let e=[],t=n.tools?.filter(c=>c.function.name!=="web_search")?.map(c=>({name:c.function.name,description:c.function.description,parametersJsonSchema:c.function.parameters}));t?.length&&e.push(Vu({functionDeclarations:t})),n.tools?.find(c=>c.function.name==="web_search")&&e.push({googleSearch:{}});let r=[],s=n.messages.filter(c=>c.role==="tool");n.messages.filter(c=>c.role!=="tool").forEach(c=>{let l;c.role==="assistant"?l="model":(["user","system"].includes(c.role),l="user");let u=[];if(typeof c.content=="string"){let p={text:c.content};c.thinking?.signature&&(p.thoughtSignature=c.thinking.signature),u.push(p)}else Array.isArray(c.content)?u.push(...c.content.map(p=>{if(p.type==="text")return{text:p.text||""};if(p.type==="image_url")return p.image_url.url.startsWith("http")?{file_data:{mime_type:p.media_type,file_uri:p.image_url.url}}:{inlineData:{mime_type:p.media_type,data:p.image_url.url?.split(",")?.pop()||p.image_url.url}}})):c.content&&typeof c.content=="object"&&(c.content.text?u.push({text:c.content.text}):u.push({text:JSON.stringify(c.content)}));if(Array.isArray(c.tool_calls)&&u.push(...c.tool_calls.map(p=>{let d={functionCall:{id:p.id||`tool_${Math.random().toString(36).substring(2,15)}`,name:p.function.name,args:JSON.parse(p.function.arguments||"{}")}};return c.thinking?.signature&&(d.thoughtSignature=c.thinking.signature),d})),u.length===0&&u.push({text:""}),r.push({role:l,parts:u}),l==="model"&&c.tool_calls){let p=c.tool_calls.map(d=>{let m=s.find(h=>h.tool_call_id===d.id);return{functionResponse:{name:d?.function?.name,response:{result:m?.content}}}});r.push({role:"user",parts:p})}});let a={};if(n.reasoning&&n.reasoning.effort&&n.reasoning.effort!=="none")if(a.thinkingConfig={includeThoughts:!0},n.model.includes("gemini-3"))a.thinkingConfig.thinkingLevel=n.reasoning.effort;else{let c=n.model.includes("pro")?[128,32768]:[0,24576],l,u=n.reasoning.max_tokens;typeof u<"u"&&(u>=c[0]&&u<=c[1]?l=u:u<c[0]?l=c[0]:u>c[1]&&(l=c[1]),a.thinkingConfig.thinkingBudget=l)}let i={contents:r,tools:e.length?e:void 0,generationConfig:a};if(n.tool_choice){let c={functionCallingConfig:{}};n.tool_choice==="auto"?c.functionCallingConfig.mode="auto":n.tool_choice==="none"?c.functionCallingConfig.mode="none":n.tool_choice==="required"?c.functionCallingConfig.mode="any":n.tool_choice?.function?.name&&(c.functionCallingConfig.mode="any",c.functionCallingConfig.allowedFunctionNames=[n.tool_choice?.function?.name]),i.toolConfig=c}return i}function dn(n){let e=n.contents,t=n.tools,o=n.model,r=n.max_tokens,s=n.temperature,a=n.stream,i=n.tool_choice,c={messages:[],model:o,max_tokens:r,temperature:s,stream:a,tool_choice:i};return Array.isArray(e)&&e.forEach(l=>{typeof l=="string"?c.messages.push({role:"user",content:l}):typeof l.text=="string"?c.messages.push({role:"user",content:l.text||null}):l.role==="user"?c.messages.push({role:"user",content:l?.parts?.map(u=>({type:"text",text:u.text||""}))||[]}):l.role==="model"&&c.messages.push({role:"assistant",content:l?.parts?.map(u=>({type:"text",text:u.text||""}))||[]})}),Array.isArray(t)&&(c.tools=[],t.forEach(l=>{Array.isArray(l.functionDeclarations)&&l.functionDeclarations.forEach(u=>{c.tools.push({type:"function",function:{name:u.name,description:u.description,parameters:u.parameters}})})})),c}async function pn(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="",a=o.candidates[0]?.content?.parts||[],i=[];for(let p of a)p.text&&p.thought===!0?r+=p.text:i.push(p);s=a.find(p=>p.thoughtSignature)?.thoughtSignature;let c=i?.filter(p=>p.functionCall)?.map(p=>({id:p.functionCall?.id||`tool_${Math.random().toString(36).substring(2,15)}`,type:"function",function:{name:p.functionCall?.name,arguments:JSON.stringify(p.functionCall?.args||{})}}))||[],l=i?.filter(p=>p.text)?.map(p=>p.text)?.join(`
|
|
117
|
+
`)||"",u={id:o.responseId,choices:[{finish_reason:o.candidates[0].finishReason?.toLowerCase()||null,index:0,message:{content:l,role:"assistant",tool_calls:c.length>0?c: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,a=!1,i=!1,c="",l=0,u=-1,p=new ReadableStream({async start(d){let m=async(g,_)=>{if(g.startsWith("data: ")){let b=g.slice(6).trim();if(b){t?.debug({chunkStr:b},`${e} chunk:`);try{let y=JSON.parse(b);if(!y.candidates||!y.candidates[0]){t?.debug({chunkStr:b},"Invalid chunk structure");return}let k=y.candidates[0],T=k.content?.parts||[];T.filter(C=>C.text&&C.thought===!0).forEach(C=>{i||(i=!0);let x={choices:[{delta:{role:"assistant",content:null,thinking:{content:C.text}},finish_reason:null,index:l,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)}
|
|
115
118
|
|
|
116
|
-
`))});let
|
|
119
|
+
`))});let S=T.find(C=>C.thoughtSignature)?.thoughtSignature;if(S&&!s){if(!i){let x={choices:[{delta:{role:"assistant",content:null,thinking:{content:"(no content)"}},finish_reason:null,index:l,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)}
|
|
117
120
|
|
|
118
|
-
`))}let
|
|
121
|
+
`))}let C={choices:[{delta:{role:"assistant",content:null,thinking:{signature:S}},finish_reason:null,index:l,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(C)}
|
|
119
122
|
|
|
120
|
-
`)),s=!0,c
|
|
123
|
+
`)),s=!0,l++,c){let x={choices:[{delta:{role:"assistant",content:c},finish_reason:null,index:l,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)}
|
|
121
124
|
|
|
122
|
-
`)),
|
|
123
|
-
`);if(!
|
|
125
|
+
`)),c="",a||(a=!0)}}let v=T.filter(C=>C.functionCall).map(C=>({id:C.functionCall?.id||`ccr_tool_${Math.random().toString(36).substring(2,15)}`,type:"function",function:{name:C.functionCall?.name,arguments:JSON.stringify(C.functionCall?.args||{})}})),R=T.filter(C=>C.text&&C.thought!==!0).map(C=>C.text).join(`
|
|
126
|
+
`);if(!R&&s&&!a){let C={choices:[{delta:{role:"assistant",content:"(no content)"},index:l,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(C)}
|
|
124
127
|
|
|
125
|
-
`)),
|
|
128
|
+
`)),a||(a=!0)}if(i&&R&&!s)if(y.modelVersion.includes("3")){c+=R;return}else{let C={choices:[{delta:{role:"assistant",content:null,thinking:{signature:`ccr_${+new Date}`}},finish_reason:null,index:l,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(C)}
|
|
126
129
|
|
|
127
|
-
`)),s=!0}if(
|
|
130
|
+
`)),s=!0}if(R){c||l++;let C={choices:[{delta:{role:"assistant",content:R},finish_reason:k.finishReason?.toLowerCase()||null,index:l,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}}};k?.groundingMetadata?.groundingChunks?.length&&(C.choices[0].delta.annotations=k.groundingMetadata.groundingChunks.map((x,E)=>{let O=k?.groundingMetadata?.groundingSupports?.filter(P=>P.groundingChunkIndices?.includes(E));return{type:"url_citation",url_citation:{url:x?.web?.uri||"",title:x?.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(C)}
|
|
128
131
|
|
|
129
|
-
`)),!
|
|
132
|
+
`)),!a&&R&&(a=!0)}v.length>0&&(v.forEach(C=>{l++,u++;let x={choices:[{delta:{role:"assistant",tool_calls:[{...C,index:u}]},finish_reason:k.finishReason?.toLowerCase()||null,index:l,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}}};k?.groundingMetadata?.groundingChunks?.length&&(x.choices[0].delta.annotations=k.groundingMetadata.groundingChunks.map((E,O)=>{let P=k?.groundingMetadata?.groundingSupports?.filter(M=>M.groundingChunkIndices?.includes(O));return{type:"url_citation",url_citation:{url:E?.web?.uri||"",title:E?.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(x)}
|
|
130
133
|
|
|
131
|
-
`))}),!
|
|
132
|
-
`);
|
|
134
|
+
`))}),!a&&R&&(a=!0))}catch(y){t?.error(`Error parsing ${e} stream chunk`,b,y.message)}}}},h=n.body.getReader(),f="";try{for(;;){let{done:g,value:_}=await h.read();if(g){f&&await m(f,d);break}f+=o.decode(_,{stream:!0});let b=f.split(`
|
|
135
|
+
`);f=b.pop()||"";for(let y of b)await m(y,d)}}catch(g){d.error(g)}finally{d.close()}}});return new Response(p,{status:n.status,statusText:n.statusText,headers:n.headers})}}var Ke,tr=w(()=>{"use strict";Ke={TYPE_UNSPECIFIED:"TYPE_UNSPECIFIED",STRING:"STRING",NUMBER:"NUMBER",INTEGER:"INTEGER",BOOLEAN:"BOOLEAN",ARRAY:"ARRAY",OBJECT:"OBJECT",NULL:"NULL"}});var fn,Ui=w(()=>{"use strict";tr();fn=class{name="gemini";endPoint="/v1beta/models/:modelAndAction";async transformRequestIn(e,t){return{body:un(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=dn;async transformResponseOut(e){return pn(e,this.name,this.logger)}}});async function Qu(){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
136
|
1. Set GOOGLE_APPLICATION_CREDENTIALS to point to service account key file
|
|
134
137
|
2. Run "gcloud auth application-default login"
|
|
135
|
-
3. Use Google Cloud environment with default service account`)}}var
|
|
136
|
-
`);for(let
|
|
137
|
-
`))},
|
|
138
|
+
3. Use Google Cloud environment with default service account`)}}var mn,ji=w(()=>{"use strict";tr();mn=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 i=(await import("fs")).readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS,"utf8"),c=JSON.parse(i);c&&c.project_id&&(o=c.project_id)}catch(a){console.error("Error extracting project_id from GOOGLE_APPLICATION_CREDENTIALS:",a)}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 Qu();return{body:un(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 dn(e)}async transformResponseOut(e){return pn(e,this.name)}}});var gn,Hi=w(()=>{"use strict";V();gn=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,a=r.thinking?.signature,i="";if(typeof r.content=="string"){let l=/<thinking>([\s\S]*?)<\/thinking>/g.exec(r.content);l&&(i=l[1].trim(),r.content=r.content.replace(l[0],"").trim())}s&&typeof s=="string"&&s.trim()?(r.reasoning_content=s,a&&(r.reasoning_content_signature=a)):i&&(r.reasoning_content=i),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,a="",i=new ReadableStream({async start(c){let l=e.body.getReader(),u=(d,m,h)=>{let f=d.split(`
|
|
139
|
+
`);for(let g of f)g.trim()&&m.enqueue(h.encode(g+`
|
|
140
|
+
`))},p=(d,m)=>{let{controller:h,encoder:f}=m;if(d.startsWith("data: ")&&d.trim()!=="data: [DONE]")try{let g=JSON.parse(d.slice(6));if(g.choices?.[0]?.delta?.reasoning_content){m.appendReasoningContent(g.choices[0].delta.reasoning_content);let _={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,thinking:{content:g.choices[0].delta.reasoning_content}}}]};delete _.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(_)}
|
|
138
141
|
|
|
139
|
-
`;h.enqueue(
|
|
142
|
+
`;h.enqueue(f.encode(b));return}if(g.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let _="ccr_think_signature",b=g.choices[0].delta.content,y={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:_}}}]};delete y.choices[0].delta.reasoning_content;let k=`data: ${JSON.stringify(y)}
|
|
140
143
|
|
|
141
|
-
`;h.enqueue(
|
|
144
|
+
`;h.enqueue(f.encode(k));let T={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,content:b}}]};delete T.choices[0].delta.reasoning_content;let S=`data: ${JSON.stringify(T)}
|
|
142
145
|
|
|
143
|
-
`;h.enqueue(
|
|
146
|
+
`;h.enqueue(f.encode(S));return}if(g.choices[0]?.delta?.reasoning_content&&delete g.choices[0].delta.reasoning_content,g.choices?.[0]?.delta&&Object.keys(g.choices[0].delta).length>0){let _=`data: ${JSON.stringify(g)}
|
|
144
147
|
|
|
145
|
-
`;h.enqueue(
|
|
146
|
-
`))}else h.enqueue(
|
|
147
|
-
`))};try{for(;;){let{done:
|
|
148
|
-
`);
|
|
149
|
-
`))}}}catch(
|
|
148
|
+
`;h.enqueue(f.encode(_))}}catch{h.enqueue(f.encode(d+`
|
|
149
|
+
`))}else h.enqueue(f.encode(d+`
|
|
150
|
+
`))};try{for(;;){let{done:d,value:m}=await l.read();if(d){a.trim()&&u(a,c,o);break}let h=t.decode(m,{stream:!0});a+=h;let f=a.split(`
|
|
151
|
+
`);a=f.pop()||"";for(let g of f)if(g.trim())try{p(g,{controller:c,encoder:o,reasoningContent:()=>r,appendReasoningContent:_=>r+=_,isReasoningComplete:()=>s,setReasoningComplete:_=>s=_})}catch(_){console.error("Error processing line:",g,_),c.enqueue(o.encode(g+`
|
|
152
|
+
`))}}}catch(d){console.error("Stream error:",d),c.error(d)}finally{try{l.releaseLock()}catch(d){console.error("Error releasing reader lock:",d)}c.close()}}});return new Response(i,{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 hn,zi=w(()=>{"use strict";V();hn=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
153
|
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
154
|
Examples:
|
|
152
155
|
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="",
|
|
154
|
-
`);for(let
|
|
155
|
-
`))},
|
|
156
|
+
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="",a="",i=new ReadableStream({async start(c){let l=e.body.getReader(),u=(d,m,h)=>{let f=d.split(`
|
|
157
|
+
`);for(let g of f)g.trim()&&m.enqueue(h.encode(g+`
|
|
158
|
+
`))},p=(d,m)=>{let{controller:h,encoder:f,exitToolIndex:g,setExitToolIndex:_,appendExitToolResponse:b}=m;if(d.startsWith("data: ")&&d.trim()!=="data: [DONE]")try{let y=JSON.parse(d.slice(6));if(y.choices[0]?.delta?.tool_calls?.length){let k=y.choices[0].delta.tool_calls[0];if(k.function?.name==="ExitTool"){_(k.index);return}else if(g()>-1&&k.index===g()&&k.function.arguments){b(k.function.arguments);try{let T=JSON.parse(m.exitToolResponse());y.choices=[{delta:{role:"assistant",content:T.response||""}}];let S=`data: ${JSON.stringify(y)}
|
|
156
159
|
|
|
157
|
-
`;h.enqueue(
|
|
160
|
+
`;h.enqueue(f.encode(S))}catch{}return}}if(y.choices?.[0]?.delta&&Object.keys(y.choices[0].delta).length>0){let k=`data: ${JSON.stringify(y)}
|
|
158
161
|
|
|
159
|
-
`;h.enqueue(
|
|
160
|
-
`))}else h.enqueue(
|
|
161
|
-
`))};try{for(;;){let{done:
|
|
162
|
-
`);
|
|
163
|
-
`))}}}catch(
|
|
164
|
-
`);for(let
|
|
165
|
-
`))},m=(h,
|
|
162
|
+
`;h.enqueue(f.encode(k))}}catch{h.enqueue(f.encode(d+`
|
|
163
|
+
`))}else h.enqueue(f.encode(d+`
|
|
164
|
+
`))};try{for(;;){let{done:d,value:m}=await l.read();if(d){a.trim()&&u(a,c,o);break}let h=t.decode(m,{stream:!0});a+=h;let f=a.split(`
|
|
165
|
+
`);a=f.pop()||"";for(let g of f)if(g.trim())try{p(g,{controller:c,encoder:o,exitToolIndex:()=>r,setExitToolIndex:_=>r=_,exitToolResponse:()=>s,appendExitToolResponse:_=>s+=_})}catch(_){console.error("Error processing line:",g,_),c.enqueue(o.encode(g+`
|
|
166
|
+
`))}}}catch(d){console.error("Stream error:",d),c.error(d)}finally{try{l.releaseLock()}catch(d){console.error("Error releasing reader lock:",d)}c.close()}}});return new Response(i,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});import{v4 as Yu}from"uuid";var yn,Bi=w(()=>{"use strict";V();yn=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="",a=!1,i=!1,c="",l=new ReadableStream({async start(u){let p=e.body.getReader(),d=(h,f,g)=>{let _=h.split(`
|
|
167
|
+
`);for(let b of _)b.trim()&&f.enqueue(g.encode(b+`
|
|
168
|
+
`))},m=(h,f)=>{let{controller:g,encoder:_}=f;if(h.startsWith("data: ")&&h.trim()!=="data: [DONE]"){let b=h.slice(6);try{let y=JSON.parse(b);if(y.usage&&(this.logger?.debug({usage:y.usage,hasToolCall:i},"usage"),y.choices[0].finish_reason=i?"tool_calls":"stop"),y.choices?.[0]?.finish_reason==="error"){g.enqueue(_.encode(`data: ${JSON.stringify({error:y.choices?.[0].error})}
|
|
166
169
|
|
|
167
|
-
`));return}y.choices?.[0]?.delta?.content&&!
|
|
170
|
+
`));return}y.choices?.[0]?.delta?.content&&!f.hasTextContent()&&f.setHasTextContent(!0);let k=y.choices?.[0]?.delta?.reasoning||y.choices?.[0]?.delta?.reasoning_content;if(k){f.appendReasoningContent(k);let C={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,thinking:{content:k}}}]};C.choices?.[0]?.delta&&(delete C.choices[0].delta.reasoning,delete C.choices[0].delta.reasoning_content);let x=`data: ${JSON.stringify(C)}
|
|
168
171
|
|
|
169
|
-
`;
|
|
172
|
+
`;g.enqueue(_.encode(x));return}if(y.choices?.[0]?.delta?.content&&f.reasoningContent()&&!f.isReasoningComplete()){f.setReasoningComplete(!0);let C="ccr_think_signature",x=y.choices[0].delta.content,E={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,content:null,thinking:{content:f.reasoningContent(),signature:C}}}]};E.choices?.[0]?.delta&&delete E.choices[0].delta.reasoning;let O=`data: ${JSON.stringify(E)}
|
|
170
173
|
|
|
171
|
-
`;
|
|
174
|
+
`;g.enqueue(_.encode(O));let P={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,content:x}}]};P.choices?.[0]?.delta&&(delete P.choices[0].delta.reasoning,delete P.choices[0].delta.reasoning_content);let M=`data: ${JSON.stringify(P)}
|
|
172
175
|
|
|
173
|
-
`;
|
|
176
|
+
`;g.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(C=>{C.id=`call_${Yu()}`}),y.choices?.[0]?.delta?.tool_calls?.length&&!i&&(i=!0);let T=y.choices?.[0]?.delta,S=T&&Object.keys(T).length>0,v=y.choices?.[0]?.finish_reason,R=y.usage;if(S||v||R){let C=`data: ${JSON.stringify(y)}
|
|
174
177
|
|
|
175
|
-
`;
|
|
176
|
-
`))}}else
|
|
177
|
-
`))};try{for(;;){let{done:h,value:
|
|
178
|
-
`);
|
|
179
|
-
`))}continue}let _=
|
|
180
|
-
`);
|
|
181
|
-
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{
|
|
182
|
-
`);for(let _ of
|
|
183
|
-
`))},
|
|
178
|
+
`;g.enqueue(_.encode(C))}}catch{g.enqueue(_.encode(h+`
|
|
179
|
+
`))}}else g.enqueue(_.encode(h+`
|
|
180
|
+
`))};try{for(;;){let{done:h,value:f}=await p.read();if(h){c.trim()&&d(c,u,o);break}if(!f||f.length===0)continue;let g;try{g=t.decode(f,{stream:!0})}catch(b){console.warn("Failed to decode chunk",b);continue}if(g.length===0)continue;if(c+=g,c.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let b=c.split(`
|
|
181
|
+
`);c=b.pop()||"";for(let y of b)if(y.trim())try{m(y,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:k=>r=k,reasoningContent:()=>s,appendReasoningContent:k=>s+=k,isReasoningComplete:()=>a,setReasoningComplete:k=>a=k})}catch(k){console.error("Error processing line:",y,k),u.enqueue(o.encode(y+`
|
|
182
|
+
`))}continue}let _=c.split(`
|
|
183
|
+
`);c=_.pop()||"";for(let b of _)if(b.trim())try{m(b,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:y=>r=y,reasoningContent:()=>s,appendReasoningContent:y=>s+=y,isReasoningComplete:()=>a,setReasoningComplete:y=>a=y})}catch(y){console.error("Error processing line:",b,y),u.enqueue(o.encode(b+`
|
|
184
|
+
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{p.releaseLock()}catch(h){console.error("Error releasing reader lock:",h)}u.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 _n,Ji=w(()=>{"use strict";_n=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 Xu}from"uuid";var bn,Wi=w(()=>{"use strict";V();bn=class{name="groq";async transformRequestIn(e){return e.messages.forEach(t=>{Array.isArray(t.content)?t.content.forEach(o=>{o.cache_control&&delete o.cache_control}):t.cache_control&&delete t.cache_control}),Array.isArray(e.tools)&&e.tools.forEach(t=>{delete t.function.parameters.$schema}),e}async transformResponseOut(e){if(e.headers.get("Content-Type")?.includes("application/json")){let t=await 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="",a=!1,i="",c=new ReadableStream({async start(l){let u=e.body.getReader(),p=(m,h,f)=>{let g=m.split(`
|
|
185
|
+
`);for(let _ of g)_.trim()&&h.enqueue(f.encode(_+`
|
|
186
|
+
`))},d=(m,h)=>{let{controller:f,encoder:g}=h;if(m.startsWith("data: ")&&m.trim()!=="data: [DONE]"){let _=m.slice(6);try{let b=JSON.parse(_);if(b.error)throw new Error(JSON.stringify(b));b.choices?.[0]?.delta?.content&&!h.hasTextContent()&&h.setHasTextContent(!0),b.choices?.[0]?.delta?.tool_calls?.length&&b.choices?.[0]?.delta?.tool_calls.forEach(k=>{k.id=`call_${Xu()}`}),b.choices?.[0]?.delta?.tool_calls?.length&&h.hasTextContent()&&(typeof b.choices[0].index=="number"?b.choices[0].index+=1:b.choices[0].index=1);let y=`data: ${JSON.stringify(b)}
|
|
184
187
|
|
|
185
|
-
`;
|
|
186
|
-
`))}}else
|
|
187
|
-
`))};try{for(;;){let{done:m,value:h}=await u.read();if(m){
|
|
188
|
-
`);
|
|
189
|
-
`))}continue}let
|
|
190
|
-
`);
|
|
191
|
-
`))}}}catch(m){console.error("Stream error:",m),
|
|
192
|
-
`);for(let
|
|
193
|
-
`))},h=(
|
|
188
|
+
`;f.enqueue(g.encode(y))}catch{f.enqueue(g.encode(m+`
|
|
189
|
+
`))}}else f.enqueue(g.encode(m+`
|
|
190
|
+
`))};try{for(;;){let{done:m,value:h}=await u.read();if(m){i.trim()&&p(i,l,o);break}if(!h||h.length===0)continue;let f;try{f=t.decode(h,{stream:!0})}catch(_){console.warn("Failed to decode chunk",_);continue}if(f.length===0)continue;if(i+=f,i.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let _=i.split(`
|
|
191
|
+
`);i=_.pop()||"";for(let b of _)if(b.trim())try{d(b,{controller:l,encoder:o,hasTextContent:()=>r,setHasTextContent:y=>r=y,reasoningContent:()=>s,appendReasoningContent:y=>s+=y,isReasoningComplete:()=>a,setReasoningComplete:y=>a=y})}catch(y){console.error("Error processing line:",b,y),l.enqueue(o.encode(b+`
|
|
192
|
+
`))}continue}let g=i.split(`
|
|
193
|
+
`);i=g.pop()||"";for(let _ of g)if(_.trim())try{d(_,{controller:l,encoder:o,hasTextContent:()=>r,setHasTextContent:b=>r=b,reasoningContent:()=>s,appendReasoningContent:b=>s+=b,isReasoningComplete:()=>a,setReasoningComplete:b=>a=b})}catch(b){console.error("Error processing line:",_,b),l.enqueue(o.encode(_+`
|
|
194
|
+
`))}}}catch(m){console.error("Stream error:",m),l.error(m)}finally{try{u.releaseLock()}catch(m){console.error("Error releasing reader lock:",m)}l.close()}}});return new Response(c,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var kn,Ki=w(()=>{"use strict";kn=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 Zu from"json5";import{jsonrepair as qu}from"jsonrepair";function nr(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=Zu.parse(n);return e?.debug("Tool arguments JSON5 parsing successful"),JSON.stringify(o)}catch(o){try{let r=qu(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 Gi=w(()=>{});var Tn,Vi=w(()=>{"use strict";Gi();V();Tn=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=nr(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,a="",i=!1,c=!1,l="",u=new ReadableStream({async start(p){let d=e.body.getReader(),m=(g,_,b)=>{let y=g.split(`
|
|
195
|
+
`);for(let k of y)k.trim()&&_.enqueue(b.encode(k+`
|
|
196
|
+
`))},h=(g,_,b)=>{let y="";try{y=nr(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 k={role:"assistant",tool_calls:[{function:{name:r.name,arguments:y},id:r.id,index:r.index,type:"function"}]},T={...g,choices:[{...g.choices[0],delta:k}]};T.choices[0].delta.content!==void 0&&delete T.choices[0].delta.content;let S=`data: ${JSON.stringify(T)}
|
|
194
197
|
|
|
195
|
-
`;_.enqueue(
|
|
198
|
+
`;_.enqueue(b.encode(S))},f=(g,_)=>{let{controller:b,encoder:y}=_;if(g.startsWith("data: ")&&g.trim()!=="data: [DONE]"){let k=g.slice(6);try{let T=JSON.parse(k);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 R=`data: ${JSON.stringify(T)}
|
|
196
199
|
|
|
197
|
-
`;
|
|
200
|
+
`;b.enqueue(y.encode(R));return}else if(r.index===v.index){v.function?.arguments&&(r.arguments+=v.function.arguments);return}else{h(T,b,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,b,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 S=`data: ${JSON.stringify(T)}
|
|
198
201
|
|
|
199
|
-
`;
|
|
200
|
-
`))}}else
|
|
201
|
-
`))};try{for(;;){let{done:
|
|
202
|
-
`);
|
|
203
|
-
`))}continue}let y=
|
|
204
|
-
`);
|
|
205
|
-
`))}}}catch(
|
|
206
|
-
`);for(let
|
|
207
|
-
`))},
|
|
202
|
+
`;b.enqueue(y.encode(S))}catch{b.enqueue(y.encode(g+`
|
|
203
|
+
`))}}else b.enqueue(y.encode(g+`
|
|
204
|
+
`))};try{for(;;){let{done:g,value:_}=await d.read();if(g){l.trim()&&m(l,p,o);break}if(!_||_.length===0)continue;let b;try{b=t.decode(_,{stream:!0})}catch(k){console.warn("Failed to decode chunk",k);continue}if(b.length===0)continue;if(l+=b,l.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let k=l.split(`
|
|
205
|
+
`);l=k.pop()||"";for(let T of k)if(T.trim())try{f(T,{controller:p,encoder:o,hasTextContent:()=>s,setHasTextContent:S=>s=S,reasoningContent:()=>a,appendReasoningContent:S=>a+=S,isReasoningComplete:()=>i,setReasoningComplete:S=>i=S})}catch(S){console.error("Error processing line:",T,S),p.enqueue(o.encode(T+`
|
|
206
|
+
`))}continue}let y=l.split(`
|
|
207
|
+
`);l=y.pop()||"";for(let k of y)if(k.trim())try{f(k,{controller:p,encoder:o,hasTextContent:()=>s,setHasTextContent:T=>s=T,reasoningContent:()=>a,appendReasoningContent:T=>a+=T,isReasoningComplete:()=>i,setReasoningComplete:T=>i=T})}catch(T){console.error("Error processing line:",k,T),p.enqueue(o.encode(k+`
|
|
208
|
+
`))}}}catch(g){console.error("Stream error:",g),p.error(g)}finally{try{d.releaseLock()}catch(g){console.error("Error releasing reader lock:",g)}p.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 vn,Qi=w(()=>{"use strict";V();We();vn=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??cn(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,a="",i=new ReadableStream({async start(c){let l=e.body.getReader(),u=(d,m,h)=>{let f=d.split(`
|
|
209
|
+
`);for(let g of f)g.trim()&&m.enqueue(h.encode(g+`
|
|
210
|
+
`))},p=(d,m)=>{let{controller:h,encoder:f}=m;if(this.logger?.debug({line:d},"Processing reason line"),d.startsWith("data: ")&&d.trim()!=="data: [DONE]")try{let g=JSON.parse(d.slice(6));if(console.log(JSON.stringify(g)),g.choices?.[0]?.delta?.reasoning_content){m.appendReasoningContent(g.choices[0].delta.reasoning_content);let _={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,thinking:{content:g.choices[0].delta.reasoning_content}}}]};delete _.choices[0].delta.reasoning_content;let b=`data: ${JSON.stringify(_)}
|
|
208
211
|
|
|
209
|
-
`;h.enqueue(
|
|
212
|
+
`;h.enqueue(f.encode(b));return}if(g.choices?.[0]?.delta?.content&&m.reasoningContent()&&!m.isReasoningComplete()){m.setReasoningComplete(!0);let _="ccr_think_signature",b=g.choices[0].delta.content,y={...g,choices:[{...g.choices[0],delta:{...g.choices[0].delta,content:null,thinking:{content:m.reasoningContent(),signature:_}}}]};delete y.choices[0].delta.reasoning_content;let k=`data: ${JSON.stringify(y)}
|
|
210
213
|
|
|
211
|
-
`;h.enqueue(
|
|
214
|
+
`;h.enqueue(f.encode(k))}if(g.choices?.[0]?.delta?.reasoning_content&&delete g.choices[0].delta.reasoning_content,g.choices?.[0]?.delta&&Object.keys(g.choices[0].delta).length>0){m.isReasoningComplete()&&g.choices[0].index++;let _=`data: ${JSON.stringify(g)}
|
|
212
215
|
|
|
213
|
-
`;h.enqueue(
|
|
214
|
-
`))}else h.enqueue(
|
|
215
|
-
`))};try{for(;;){let{done:
|
|
216
|
-
`);
|
|
217
|
-
`))}}}catch(
|
|
216
|
+
`;h.enqueue(f.encode(_))}}catch{h.enqueue(f.encode(d+`
|
|
217
|
+
`))}else h.enqueue(f.encode(d+`
|
|
218
|
+
`))};try{for(;;){let{done:d,value:m}=await l.read();if(d){a.trim()&&u(a,c,o);break}let h=t.decode(m,{stream:!0});a+=h;let f=a.split(`
|
|
219
|
+
`);a=f.pop()||"";for(let g of f)if(g.trim())try{p(g,{controller:c,encoder:o,reasoningContent:()=>r,appendReasoningContent:_=>r+=_,isReasoningComplete:()=>s,setReasoningComplete:_=>s=_})}catch(_){console.error("Error processing line:",g,_),c.enqueue(o.encode(g+`
|
|
220
|
+
`))}}}catch(d){console.error("Stream error:",d),c.error(d)}finally{try{l.releaseLock()}catch(d){console.error("Error releasing reader lock:",d)}c.close()}}});return new Response(i,{status:e.status,statusText:e.statusText,headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}return e}}});var xn,Yi=w(()=>{"use strict";xn=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 Cn,Xi=w(()=>{"use strict";Cn=class{static TransformerName="maxcompletiontokens";async transformRequestIn(e){return e.max_tokens&&(e.max_completion_tokens=e.max_tokens,delete e.max_tokens),e}}});function Zi(n){let e=[];for(let o=0;o<n.messages.length;o++){let r=n.messages[o],s=o===n.messages.length-1,a=r.role==="assistant",i=[];typeof r.content=="string"?i.push({type:"text",text:r.content}):Array.isArray(r.content)&&r.content.forEach(c=>{c.type==="text"?i.push({type:"text",text:c.text||""}):c.type==="image_url"&&i.push({type:"image",source:{type:"base64",media_type:c.media_type||"image/jpeg",data:c.image_url.url}})}),!(!s&&i.length===0&&!r.tool_calls&&!r.content)&&(s&&a&&i.length===0&&r.tool_calls&&i.push({type:"text",text:""}),e.push({role:r.role==="assistant"?"assistant":"user",content:i}))}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 qi(n){let e=n,o={messages:e.messages.map(r=>{let s=r.content.map(a=>a.type==="text"?{type:"text",text:a.text||""}:a.type==="image"&&a.source?{type:"image_url",image_url:{url:a.source.data},media_type:a.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 ea(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(a=>({id:a.id,type:"function",function:{name:a.name,arguments:JSON.stringify(a.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=(i,c)=>{if(i.startsWith("data: ")){let l=i.slice(6).trim();if(l){t?.debug({chunkStr:l},`${e} chunk:`);try{let u=JSON.parse(l);if(u.type==="content_block_delta"&&u.delta?.type==="text_delta"){let p={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)}};c.enqueue(r.encode(`data: ${JSON.stringify(p)}
|
|
218
221
|
|
|
219
|
-
`))}else if(u.type==="content_block_delta"&&u.delta?.type==="input_json_delta"){let
|
|
222
|
+
`))}else if(u.type==="content_block_delta"&&u.delta?.type==="input_json_delta"){let p={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)}};c.enqueue(r.encode(`data: ${JSON.stringify(p)}
|
|
220
223
|
|
|
221
|
-
`))}else if(u.type==="content_block_start"&&u.content_block?.type==="tool_use"){let
|
|
224
|
+
`))}else if(u.type==="content_block_start"&&u.content_block?.type==="tool_use"){let p={choices:[{delta:{tool_calls:[{index:u.index||0,id:u.content_block.id,type:"function",function:{name:u.content_block.name,arguments:""}}]},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)}};c.enqueue(r.encode(`data: ${JSON.stringify(p)}
|
|
222
225
|
|
|
223
|
-
`))}else if(u.type==="message_delta"){let
|
|
226
|
+
`))}else if(u.type==="message_delta"){let p={choices:[{delta:{},finish_reason:u.delta?.stop_reason==="tool_use"?"tool_calls":u.delta?.stop_reason==="max_tokens"?"length":u.delta?.stop_reason==="stop_sequence"?"content_filter":"stop",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)}};c.enqueue(r.encode(`data: ${JSON.stringify(p)}
|
|
224
227
|
|
|
225
|
-
`))}else if(u.type==="message_stop")
|
|
228
|
+
`))}else if(u.type==="message_stop")c.enqueue(r.encode(`data: [DONE]
|
|
226
229
|
|
|
227
|
-
`));else{let
|
|
230
|
+
`));else{let p={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)}};c.enqueue(r.encode(`data: ${JSON.stringify(p)}
|
|
228
231
|
|
|
229
|
-
`))}}catch(u){t?.error(`Error parsing ${e} stream chunk`,
|
|
230
|
-
`);
|
|
232
|
+
`))}}catch(u){t?.error(`Error parsing ${e} stream chunk`,l,u.message)}}}},a=new ReadableStream({async start(i){let c=n.body.getReader(),l="";try{for(;;){let{done:u,value:p}=await c.read();if(u){l&&s(l,i);break}l+=o.decode(p,{stream:!0});let d=l.split(`
|
|
233
|
+
`);l=d.pop()||"";for(let m of d)s(m,i)}}catch(u){i.error(u)}finally{i.close()}}});return new Response(a,{status:n.status,statusText:n.statusText,headers:n.headers})}return n}var ta=w(()=>{"use strict"});async function ed(){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
234
|
1. Set GOOGLE_APPLICATION_CREDENTIALS to point to service account key file
|
|
232
235
|
2. Run "gcloud auth application-default login"
|
|
233
|
-
3. Use Google Cloud environment with default service account`)}}var
|
|
234
|
-
`);for(let
|
|
235
|
-
`))},m=(h,
|
|
236
|
+
3. Use Google Cloud environment with default service account`)}}var Sn,na=w(()=>{"use strict";ta();Sn=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 i=(await import("fs")).readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS,"utf8"),c=JSON.parse(i);c&&c.project_id&&(o=c.project_id)}catch(a){console.error("Error extracting project_id from GOOGLE_APPLICATION_CREDENTIALS:",a)}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 ed();return{body:Zi(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 qi(e)}async transformResponseOut(e){return ea(e,this.name,this.logger)}}});var Rn,oa=w(()=>{"use strict";Rn=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 wn,ra=w(()=>{"use strict";wn=class{name="streamoptions";async transformRequestIn(e){return e.stream&&(e.stream_options={include_usage:!0}),e}}});var Pn,sa=w(()=>{"use strict";Pn=class{name="OpenAI";endPoint="/v1/chat/completions"}});var En,ia=w(()=>{"use strict";En=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 td}from"uuid";var On,aa=w(()=>{"use strict";V();On=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="",a=!1,i=!1,c="",l=new ReadableStream({async start(u){let p=e.body.getReader(),d=(h,f,g)=>{let _=h.split(`
|
|
237
|
+
`);for(let b of _)b.trim()&&f.enqueue(g.encode(b+`
|
|
238
|
+
`))},m=(h,f)=>{let{controller:g,encoder:_}=f;if(h.startsWith("data: ")&&h.trim()!=="data: [DONE]"){let b=h.slice(6);try{let y=JSON.parse(b);if(y.usage&&(this.logger?.debug({usage:y.usage,hasToolCall:i},"usage"),y.choices[0].finish_reason=i?"tool_calls":"stop"),y.choices?.[0]?.finish_reason==="error"&&g.enqueue(_.encode(`data: ${JSON.stringify({error:y.choices?.[0].error})}
|
|
236
239
|
|
|
237
|
-
`)),y.choices?.[0]?.delta?.content&&!
|
|
240
|
+
`)),y.choices?.[0]?.delta?.content&&!f.hasTextContent()&&f.setHasTextContent(!0),y.choices?.[0]?.delta?.reasoning){f.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 S=`data: ${JSON.stringify(T)}
|
|
238
241
|
|
|
239
|
-
`;
|
|
242
|
+
`;g.enqueue(_.encode(S));return}if(y.choices?.[0]?.delta?.content&&f.reasoningContent()&&!f.isReasoningComplete()){f.setReasoningComplete(!0);let S={...y,choices:[{...y.choices?.[0],delta:{...y.choices[0].delta,content:null,thinking:{content:f.reasoningContent(),signature:"ccr_think_signature"}}}]};S.choices?.[0]?.delta&&delete S.choices[0].delta.reasoning;let v=`data: ${JSON.stringify(S)}
|
|
240
243
|
|
|
241
|
-
`;
|
|
244
|
+
`;g.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_${td()}`}),y.choices?.[0]?.delta?.tool_calls?.length&&!i&&(i=!0),y.choices?.[0]?.delta?.tool_calls?.length&&f.hasTextContent()&&(typeof y.choices[0].index=="number"?y.choices[0].index+=1:y.choices[0].index=1);let k=`data: ${JSON.stringify(y)}
|
|
242
245
|
|
|
243
|
-
`;
|
|
244
|
-
`))}}else
|
|
245
|
-
`))};try{for(;;){let{done:h,value:
|
|
246
|
-
`);
|
|
247
|
-
`))}continue}let _=
|
|
248
|
-
`);
|
|
249
|
-
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{
|
|
246
|
+
`;g.enqueue(_.encode(k))}catch{g.enqueue(_.encode(h+`
|
|
247
|
+
`))}}else g.enqueue(_.encode(h+`
|
|
248
|
+
`))};try{for(;;){let{done:h,value:f}=await p.read();if(h){c.trim()&&d(c,u,o);break}if(!f||f.length===0)continue;let g;try{g=t.decode(f,{stream:!0})}catch(b){console.warn("Failed to decode chunk",b);continue}if(g.length===0)continue;if(c+=g,c.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let b=c.split(`
|
|
249
|
+
`);c=b.pop()||"";for(let y of b)if(y.trim())try{m(y,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:k=>r=k,reasoningContent:()=>s,appendReasoningContent:k=>s+=k,isReasoningComplete:()=>a,setReasoningComplete:k=>a=k})}catch(k){console.error("Error processing line:",y,k),u.enqueue(o.encode(y+`
|
|
250
|
+
`))}continue}let _=c.split(`
|
|
251
|
+
`);c=_.pop()||"";for(let b of _)if(b.trim())try{m(b,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:y=>r=y,reasoningContent:()=>s,appendReasoningContent:y=>s+=y,isReasoningComplete:()=>a,setReasoningComplete:y=>a=y})}catch(y){console.error("Error processing line:",b,y),u.enqueue(o.encode(b+`
|
|
252
|
+
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{p.releaseLock()}catch(h){console.error("Error releasing reader lock:",h)}u.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 or,nd,An,ca=w(()=>{"use strict";V();or=`Always think before answering. Even if the problem seems simple, always write down your reasoning process explicitly.
|
|
250
253
|
|
|
251
254
|
Output format:
|
|
252
255
|
<reasoning_content>
|
|
253
256
|
Your detailed thinking process goes here
|
|
254
257
|
</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:
|
|
258
|
+
Your final answer must follow after the closing tag above.`,nd=10,An=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<nd)&&(t++,r.content=`<reasoning_content>${r.thinking.content}</reasoning_content>
|
|
259
|
+
${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:or}):o.content=[{type:"text",text:or},{type:"text",text:o.content||""}]),o.role==="tool"&&e.messages.push({role:"user",content:[{type:"text",text:or}]}),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,a=r.choices[0]?.message.content.match(s);a&&a[1]&&(r.thinking={content:a[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,a=new TextEncoder,i=new ReadableStream({async start(c){let l=e.body.getReader(),u="",p="SEARCHING",d="",m="",h=(f,g)=>{if(typeof g!="string"){f.choices?.[0]?.delta&&Object.keys(f.choices[0].delta).length>0&&!f.choices[0].delta.content&&(f.choices[0].index=r,c.enqueue(a.encode(`data: ${JSON.stringify(f)}
|
|
257
260
|
|
|
258
|
-
`)));return}let _=
|
|
261
|
+
`)));return}let _=d+g;for(d="";_.length>0;)if(p==="SEARCHING"){let b=_.indexOf(t);if(b!==-1)_=_.substring(b+t.length),p="REASONING";else{for(let y=t.length-1;y>0;y--)if(_.endsWith(t.substring(0,y))){d=_.substring(_.length-y);break}_=""}}else if(p==="REASONING"){let b=_.indexOf(o);if(b!==-1){let y=_.substring(0,b);if(y.length>0){let S={...f.choices[0].delta,thinking:{content:y}};delete S.content;let v={...f,choices:[{...f.choices[0],delta:S,index:r}]};c.enqueue(a.encode(`data: ${JSON.stringify(v)}
|
|
259
262
|
|
|
260
|
-
`))}let
|
|
263
|
+
`))}let k={...f.choices[0].delta,thinking:{signature:"ccr_think_signature"}};delete k.content;let T={...f,choices:[{...f.choices[0],delta:k,index:r}]};c.enqueue(a.encode(`data: ${JSON.stringify(T)}
|
|
261
264
|
|
|
262
|
-
`)),r++,_=_.substring(
|
|
265
|
+
`)),r++,_=_.substring(b+o.length),p="FINAL"}else{let y=_;for(let k=o.length-1;k>0;k--)if(_.endsWith(o.substring(0,k))){d=_.substring(_.length-k),y=_.substring(0,_.length-k);break}if(y.length>0){let k={...f.choices[0].delta,thinking:{content:y}};delete k.content;let T={...f,choices:[{...f.choices[0],delta:k,index:r}]};c.enqueue(a.encode(`data: ${JSON.stringify(T)}
|
|
263
266
|
|
|
264
|
-
`))}_=""}}else if(
|
|
267
|
+
`))}_=""}}else if(p==="FINAL"){if(_.length>0)if(/^\s*$/.test(_))m+=_;else{let y=m+_,k={...f.choices[0].delta,content:y};k.thinking&&delete k.thinking;let T={...f,choices:[{...f.choices[0],delta:k}]};c.enqueue(a.encode(`data: ${JSON.stringify(T)}
|
|
265
268
|
|
|
266
|
-
`)),m=""}r++,_=""}};try{for(;;){let{done:
|
|
267
|
-
`);u=
|
|
269
|
+
`)),m=""}r++,_=""}};try{for(;;){let{done:f,value:g}=await l.read();if(f)break;let _=s.decode(g,{stream:!0});u+=_;let b=u.split(`
|
|
270
|
+
`);u=b.pop()||"";for(let y of b)if(y.trim()){if(y.trim()==="data: [DONE]"){c.enqueue(a.encode(y+`
|
|
268
271
|
|
|
269
|
-
`));break}if(y.startsWith("data:"))try{let
|
|
270
|
-
`))}else
|
|
271
|
-
`))}}}catch(
|
|
272
|
+
`));break}if(y.startsWith("data:"))try{let k=JSON.parse(y.slice(5));h(k,k.choices?.[0]?.delta?.content)}catch{c.enqueue(a.encode(y+`
|
|
273
|
+
`))}else c.enqueue(a.encode(y+`
|
|
274
|
+
`))}}}catch(f){console.error("Stream error:",f),c.error(f)}finally{try{l.releaseLock()}catch(f){console.error("Error releasing reader lock:",f)}if(p==="REASONING"){let g={choices:[{delta:{thinking:{signature:"ccr_think_signature"}}}]};c.enqueue(a.encode(`data: ${JSON.stringify(g)}
|
|
272
275
|
|
|
273
|
-
`))}
|
|
274
|
-
`));break}let
|
|
275
|
-
`);
|
|
276
|
+
`))}c.close()}}});return new Response(i,{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 In,la=w(()=>{"use strict";V();In=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="",a="",i=new ReadableStream({async start(c){let l=e.body.getReader();try{for(;;){let{done:u,value:p}=await l.read();if(u){a.trim()&&c.enqueue(r.encode(a+`
|
|
277
|
+
`));break}let d=o.decode(p,{stream:!0});a+=d;let m=a.split(`
|
|
278
|
+
`);a=m.pop()||"";for(let h of m)if(h.trim())if(h.startsWith("data: ")&&h.trim()!=="data: [DONE]")try{let f=JSON.parse(h.slice(6));if(f.choices?.[0]?.delta?.reasoning_content){s+=f.choices[0].delta.reasoning_content;let g={...f,choices:[{...f.choices[0],delta:{thinking:{content:f.choices[0].delta.reasoning_content}}}]};delete g.choices[0].delta.reasoning_content,c.enqueue(r.encode(`data: ${JSON.stringify(g)}
|
|
276
279
|
|
|
277
|
-
`));continue}if(
|
|
280
|
+
`));continue}if(f.choices?.[0]?.delta?.content&&s&&!f.choices[0]?.delta?.reasoning_content){let g=Date.now().toString(),_={...f,choices:[{...f.choices[0],delta:{content:null,thinking:{content:s,signature:g}}}]};c.enqueue(r.encode(`data: ${JSON.stringify(_)}
|
|
278
281
|
|
|
279
|
-
`)),s=""}
|
|
282
|
+
`)),s=""}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&&c.enqueue(r.encode(`data: ${JSON.stringify(f)}
|
|
280
283
|
|
|
281
|
-
`))}catch{
|
|
282
|
-
`))}else
|
|
283
|
-
`))}}catch(u){
|
|
284
|
-
`);for(let
|
|
285
|
-
`))},m=(h,
|
|
284
|
+
`))}catch{c.enqueue(r.encode(h+`
|
|
285
|
+
`))}else c.enqueue(r.encode(h+`
|
|
286
|
+
`))}}catch(u){c.error(u)}finally{try{l.releaseLock()}catch{}c.close()}}});return new Response(i,{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 ua}from"uuid";var Mn,da=w(()=>{"use strict";V();De();Mn=class{constructor(e){this.options=e}static TransformerName="opencode";endPoint="/v1/chat/completions";resolveSessionId(e,t){let o=t?.req,r=o?.headers||{},s=a=>{let i=Object.keys(r).find(l=>l.toLowerCase()===a),c=i?r[i]:void 0;return typeof c=="string"&&c.trim()?c.trim():void 0};return s("x-opencode-session")||o?.clientContext?.stableSessionId||o?.sessionId||he(e.metadata?.user_id)||s("session_id")||ua()}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(a=>{a.type==="image_url"&&(a.image_url.url.startsWith("http")||(a.image_url.url=`data:${a.media_type};base64,${a.image_url.url}`),delete a.media_type)}),s.role==="assistant"){let a=s.thinking?.content,i=s.thinking?.signature;a&&typeof a=="string"&&a.trim()?(s.reasoning_content=a,i&&(s.reasoning_content_signature=i)):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="",a=!1,i=!1,c="",l=new ReadableStream({async start(u){let p=e.body.getReader(),d=(h,f,g)=>{let _=h.split(`
|
|
287
|
+
`);for(let b of _)b.trim()&&f.enqueue(g.encode(b+`
|
|
288
|
+
`))},m=(h,f)=>{let{controller:g,encoder:_}=f;if(h.startsWith("data: ")&&h.trim()!=="data: [DONE]")try{let b=JSON.parse(h.slice(6));if(b.usage&&(b.choices[0].finish_reason=i?"tool_calls":"stop"),b.choices?.[0]?.finish_reason==="error"){g.enqueue(_.encode(`data: ${JSON.stringify({error:b.choices?.[0]?.error})}
|
|
286
289
|
|
|
287
|
-
`));return}
|
|
290
|
+
`));return}b.choices?.[0]?.delta?.content&&!f.hasTextContent()&&f.setHasTextContent(!0);let y=b.choices?.[0]?.delta?.reasoning||b.choices?.[0]?.delta?.reasoning_content;if(y){f.appendReasoningContent(y);let R={...b,choices:[{...b.choices?.[0],delta:{...b.choices[0].delta,thinking:{content:y}}}]};R.choices?.[0]?.delta&&(delete R.choices[0].delta.reasoning,delete R.choices[0].delta.reasoning_content),g.enqueue(_.encode(`data: ${JSON.stringify(R)}
|
|
288
291
|
|
|
289
|
-
`));return}if(
|
|
292
|
+
`));return}if(b.choices?.[0]?.delta?.content&&f.reasoningContent()&&!f.isReasoningComplete()){f.setReasoningComplete(!0);let R="ccr_think_signature",C=b.choices[0].delta.content,x={...b,choices:[{...b.choices?.[0],delta:{...b.choices[0].delta,content:null,thinking:{content:f.reasoningContent(),signature:R}}}]};x.choices?.[0]?.delta&&(delete x.choices[0].delta.reasoning,delete x.choices[0].delta.reasoning_content),g.enqueue(_.encode(`data: ${JSON.stringify(x)}
|
|
290
293
|
|
|
291
|
-
`));let
|
|
294
|
+
`));let E={...b,choices:[{...b.choices?.[0],delta:{...b.choices[0].delta,content:C}}]};E.choices?.[0]?.delta&&(delete E.choices[0].delta.reasoning,delete E.choices[0].delta.reasoning_content),g.enqueue(_.encode(`data: ${JSON.stringify(E)}
|
|
292
295
|
|
|
293
|
-
`));return}
|
|
296
|
+
`));return}b.choices?.[0]?.delta?.reasoning&&delete b.choices[0].delta.reasoning,b.choices?.[0]?.delta?.reasoning_content&&delete b.choices[0].delta.reasoning_content,b.choices?.[0]?.delta?.tool_calls?.length&&!Number.isNaN(parseInt(b.choices?.[0]?.delta?.tool_calls[0].id,10))&&b.choices?.[0]?.delta?.tool_calls.forEach(R=>{R.id=`call_${ua()}`}),b.choices?.[0]?.delta?.tool_calls?.length&&!i&&(i=!0);let k=b.choices?.[0]?.delta,T=k&&Object.keys(k).length>0,S=b.choices?.[0]?.finish_reason,v=b.usage;(T||S||v)&&g.enqueue(_.encode(`data: ${JSON.stringify(b)}
|
|
294
297
|
|
|
295
|
-
`))}catch{
|
|
296
|
-
`))}else
|
|
297
|
-
`))};try{for(;;){let{done:h,value:
|
|
298
|
-
`);
|
|
299
|
-
`))}continue}let _=
|
|
300
|
-
`);
|
|
301
|
-
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{
|
|
298
|
+
`))}catch{g.enqueue(_.encode(h+`
|
|
299
|
+
`))}else g.enqueue(_.encode(h+`
|
|
300
|
+
`))};try{for(;;){let{done:h,value:f}=await p.read();if(h){c.trim()&&d(c,u,o);break}if(!f||f.length===0)continue;let g;try{g=t.decode(f,{stream:!0})}catch(b){console.warn("Failed to decode chunk",b);continue}if(g.length===0)continue;if(c+=g,c.length>1e6){console.warn("Buffer size exceeds limit, processing partial data");let b=c.split(`
|
|
301
|
+
`);c=b.pop()||"";for(let y of b)if(y.trim())try{m(y,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:k=>r=k,reasoningContent:()=>s,appendReasoningContent:k=>s+=k,isReasoningComplete:()=>a,setReasoningComplete:k=>a=k})}catch(k){console.error("Error processing line:",y,k),u.enqueue(o.encode(y+`
|
|
302
|
+
`))}continue}let _=c.split(`
|
|
303
|
+
`);c=_.pop()||"";for(let b of _)if(b.trim())try{m(b,{controller:u,encoder:o,hasTextContent:()=>r,setHasTextContent:y=>r=y,reasoningContent:()=>s,appendReasoningContent:y=>s+=y,isReasoningComplete:()=>a,setReasoningComplete:y=>a=y})}catch(y){console.error("Error processing line:",b,y),u.enqueue(o.encode(b+`
|
|
304
|
+
`))}}}catch(h){console.error("Stream error:",h),u.error(h)}finally{try{p.releaseLock()}catch(h){console.error("Error releasing reader lock:",h)}u.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}}});function rd(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 sd(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 od,Nn,pa=w(()=>{"use strict";We();od={min:"low",minimal:"low",light:"low",xhigh:"max",maximum:"max",ultra:"max",ultracode:"max"};Nn=class{constructor(e){this.options=e}static TransformerName="defaultthinking";async transformRequestIn(e,t){let o=rd(this.options?.level??this.options?.value);if(o===void 0||o==="none"||e.reasoning)return e;let r=sd(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 a=ye(o);r==="chat"?e.reasoning_effort=a:e.reasoning={enabled:!0,effort:a}}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=od[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 fa(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 $n,ma=w(()=>{"use strict";We();V();$n=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"),a=!r&&!s&&o.includes("deepseek");if(e==="none")return a||o.split("/").pop()==="qwen3.8-flash-next"?"none":void 0;switch(e){case"minimal":return a?"minimal":"low";case"low":case"medium":return e;case"high":return s?"xhigh":"high";case"xhigh":case"max":return a?e:s?"xhigh":"high";default:return"medium"}}async transformRequestIn(e){let t=JSON.parse(JSON.stringify(e)),o=t.model,r=t.reasoning,s,a=r?.enabled===!1;r&&typeof r.effort=="string"?(s=r.effort===""?void 0:r.effort,a=a||r.effort==="none"):typeof r?.max_tokens=="number"?s=ye(r.max_tokens):r?.enabled===!0?s=t.reasoning_effort??"medium":(s=t.reasoning_effort,!a&&(t.enable_thinking===!1||t.thinking?.type==="disabled")&&(a=!0));let i=this.resolveEffort(a?"none":s,o);return i?t.reasoning_effort=i: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 de(e);if(o){let a=new Headers(e.headers);if(o.isSSE&&a.set("Content-Type","text/event-stream"),e=new Response(o.body,{status:e.status,statusText:e.statusText,headers:a}),o.isSSE)return this.transformStream(e)}else{let a=await me(e);if(e=a.response,a.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)fa(t?.message)}transformStream(e){let t=new TextDecoder,o=new TextEncoder,r="",s=l=>{if(!l.startsWith("data:"))return l;let u=l.slice(5).trim();if(u==="[DONE]")return l;let p;try{p=JSON.parse(u)}catch{return l}let d=!1,m=Array.isArray(p?.choices)?p.choices:[];for(let h of m)fa(h?.delta,!0)&&(d=!0);return d?`data: ${JSON.stringify(p)}`:l},a=(l,u=!1)=>{let p=/\r\n|\r|\n/g,d=0,m,h="";for(;(m=p.exec(r))!==null&&!(!u&&m[0]==="\r"&&p.lastIndex===r.length);)h+=s(r.slice(d,m.index))+m[0],d=p.lastIndex;r=r.slice(d),u&&(h+=s(r),r=""),h&&l.enqueue(o.encode(h))},i=e.body.pipeThrough(new TransformStream({transform(l,u){r+=t.decode(l,{stream:!0}),a(u)},flush(l){r+=t.decode(),a(l,!0)}})),c=new Headers(e.headers);return c.delete("content-length"),new Response(i,{status:e.status,statusText:e.statusText,headers:c})}}});var ga,ha=w(()=>{Fi();Ui();ji();Hi();zi();Bi();Ji();Wi();Ki();Vi();Qi();Yi();Xi();na();oa();ra();sa();ia();aa();zo();ca();la();da();pa();ma();ga={AnthropicTransformer:ln,GeminiTransformer:fn,VertexGeminiTransformer:mn,VertexClaudeTransformer:Sn,DeepseekTransformer:gn,TooluseTransformer:hn,OpenrouterTransformer:yn,OpenAITransformer:Pn,MaxTokenTransformer:_n,GroqTransformer:bn,CleancacheTransformer:kn,EnhanceToolTransformer:Tn,ReasoningTransformer:vn,SamplingTransformer:xn,MaxCompletionTokens:Cn,CerebrasTransformer:Rn,StreamOptionsTransformer:wn,CustomParamsTransformer:En,VercelTransformer:On,OpenAIResponsesTransformer:Ne,ForceReasoningTransformer:An,KimiTransformer:In,OpenCodeTransformer:Mn,DefaultThinkingTransformer:Nn,AmdTransformer:$n}});var Ge,rr=w(()=>{"use strict";ha();Ge=class{constructor(e,t){this.configService=e;this.logger=t}transformers=new Map;preparePluginTransformer(e){if(typeof e.TransformerName=="string")return e;let t=new e;if(!t?.name)throw new Error("plugin transformer has no name");return t.logger=this.logger,t}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=ot(ot.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
305
|
error: ${t.message}
|
|
303
306
|
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(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}
|
|
307
|
+
Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(ga).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 id}from"tiktoken";var yt,ya=w(()=>{"use strict";yt=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=id(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:a}=e;return Array.isArray(r)&&r.forEach(i=>{typeof i.content=="string"?o+=t.encode(i.content).length:Array.isArray(i.content)&&i.content.forEach(c=>{if(c.type==="text")o+=t.encode(c.text).length;else if(c.type==="tool_use")o+=t.encode(JSON.stringify(c.input)).length;else if(c.type==="tool_result"){let l=c.content;if(Array.isArray(l))for(let u of l)u?.type!=="image"&&(o+=t.encode(u?.type==="text"?String(u.text??""):JSON.stringify(u)).length);else{let u=typeof l=="string"?l:JSON.stringify(l);o+=t.encode(u).length}}})}),typeof s=="string"?o+=t.encode(s).length:Array.isArray(s)&&s.forEach(i=>{i.type==="text"&&(typeof i.text=="string"?o+=t.encode(i.text).length:Array.isArray(i.text)&&i.text.forEach(c=>{o+=t.encode(c||"").length}))}),a&&a.forEach(i=>{i.description&&(o+=t.encode(i.name+i.description).length),i.input_schema&&(o+=t.encode(JSON.stringify(i.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 Ln}from"path";import{homedir as ad}from"os";import{existsSync as sr,mkdirSync as cd}from"fs";import{promises as Dn}from"fs";import{Tokenizer as ld}from"@huggingface/tokenizers";var Fn,_a=w(()=>{"use strict";Fn=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||Ln(ad(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=Ln(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:Ln(e,"tokenizer.json"),tokenizerConfig:Ln(e,"tokenizer_config.json")}}ensureDir(e){sr(e)||cd(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!sr(e.tokenizerJson)||!sr(e.tokenizerConfig))return null;let[t,o]=await Promise.all([Dn.readFile(e.tokenizerJson,"utf-8"),Dn.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,a]=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[i,c]=await Promise.all([s.json(),a.ok?a.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([Dn.writeFile(e.tokenizerJson,JSON.stringify(i,null,2)),Dn.writeFile(e.tokenizerConfig,JSON.stringify(c,null,2))]),{tokenizerJson:i,tokenizerConfig:c}}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 ld(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 a of o)if(typeof a.content=="string")t.push(a.content);else if(Array.isArray(a.content)){for(let i of a.content)if(i.type==="text"&&i.text)t.push(i.text);else if(i.type==="tool_use"&&i.input)t.push(JSON.stringify(i.input));else if(i.type==="tool_result")if(Array.isArray(i.content))for(let c of i.content)c?.type!=="image"&&t.push(c?.type==="text"?String(c.text??""):JSON.stringify(c));else t.push(typeof i.content=="string"?i.content:JSON.stringify(i.content))}}if(typeof r=="string")t.push(r);else if(Array.isArray(r)){for(let a of r)if(a.type==="text"){if(typeof a.text=="string")t.push(a.text);else if(Array.isArray(a.text))for(let i of a.text)i&&t.push(i)}}if(s)for(let a of s)a.name&&t.push(a.name),a.description&&t.push(a.description),a.input_schema&&t.push(JSON.stringify(a.input_schema));return t.join(" ")}}});var Un,ba=w(()=>{"use strict";Z();Un=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),a={method:"POST",headers:o,body:JSON.stringify(t),signal:r.signal},i=this.options.resolveProxyUrl?.();i&&(a.dispatcher=K(i));let c=await fetch(this.config.url,a);if(clearTimeout(s),!c.ok)throw new Error(`API tokenizer request failed: ${c.status} ${c.statusText}`);let l=await c.json();return this.extractTokenCount(l)}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"?Array.isArray(t.content)?t.content.filter(o=>o?.type!=="image").map(o=>o?.type==="text"?String(o.text??""):JSON.stringify(o)).join(`
|
|
308
|
+
`):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 Ve,ir=w(()=>{"use strict";ya();_a();ba();Z();Ve=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 yt("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 yt(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),r=new Fn(e.model,this.logger,{timeout:this.options.timeout});break;case"api":r=new Un(e,this.logger,{timeout:this.options.timeout,resolveProxyUrl:()=>{let s=t?this.getRawProvider(t):void 0;return ue(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 ka(n){if(!n||typeof n!="object"||hd(n))return null;for(let t of gd(n)){let o=md(t);if(o)return o}return null}function md(n){if(!n||typeof n!="object")return null;let e={},t=jn(n,ud);if(t!==void 0){let a=Ta(t);e.usedDailyBalance=a,e.limitDaily=100}let o=jn(n,dd);if(o!==void 0){let a=Ta(o);e.usedBalance=a,e.totalBalance=100}let r=jn(n,pd);if(r!==void 0){let a=va(r);a&&(e.resetTime=a)}let s=jn(n,fd);if(s!==void 0){let a=va(s);a&&(e.resetTime7d=a,e.resetTime||(e.resetTime=a))}return e.usedDailyBalance===void 0&&e.limitDaily===void 0&&e.usedBalance===void 0&&e.totalBalance===void 0&&e.resetTime===void 0?null:e}function Ta(n){return!Number.isFinite(n)||n<=0?0:Math.round(n*1e4)/100}function jn(n,e){for(let t of e){let o=I(n?.[t]);if(o!==void 0)return o}}function gd(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 a of o)s[a]&&typeof s[a]=="object"&&r(s[a])}};return r(n),e}function hd(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 va(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 _r(n){let e=br(n);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?Cd:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?Sd:e==="deepseek.com"||e.endsWith(".deepseek.com")?yd:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?_d:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?bd:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?kd:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?Td:e==="maas.aliyuncs.com"||e.endsWith(".maas.aliyuncs.com")?vd:e==="xf-yun.com"||e.endsWith(".xf-yun.com")||e==="xfyun.cn"||e.endsWith(".xfyun.cn")||e==="xfyun.com"||e.endsWith(".xfyun.com")?xd:null:null}function Rd(n){let e=br(n);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function wd(n,e){let t=hr(n);if(t.length===0)return null;let o=String(e||"").split(":").map(s=>s.trim()).filter(Boolean);return t.find(s=>{let a=s?.codingPlanAppCredentialDTO||s?.appCredential||s,i=String(a?.apiKey||a?.api_key||"").trim();return i&&o.some(c=>c.includes(i)||i.includes(c))})||t[0]}function hr(n){return!n||typeof n!="object"?[]:!Array.isArray(n)&&n.codingPlanUsageDTO?[n]:Array.isArray(n)?n.flatMap(e=>hr(e)):Object.values(n).flatMap(e=>hr(e))}function yr(n,e){let t=n.match(new RegExp(`(?:^|;\\s*)${e}=([^;]+)`));return t?decodeURIComponent(t[1]):void 0}function br(n){try{return new URL(n).hostname.toLowerCase()}catch{return null}}function 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,ar,cr,lr,ur,dr,ud,dd,pd,fd,pr,fr,mr,gr,yd,_d,bd,kd,Td,vd,xd,Cd,Sd,kr=w(()=>{"use strict";Z();se=class{async fetchJson(e,t,o,r,s){let a=s||t.apiKey;if(!a)return null;let i={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${a}`},signal:AbortSignal.timeout(o)};try{r&&(i.dispatcher=K(r));let c=await fetch(e,i);return c.ok?await c.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}},ar=class extends se{async queryQuota(e,t,o){let r=await this.fetchJson("https://api.deepseek.com/user/balance",e,t,o),a=(Array.isArray(r?.balance_infos)?r.balance_infos:[]).find(c=>I(c?.total_balance)!==void 0);if(!a)return null;let i=I(a.total_balance);return i===void 0?null:{totalBalance:i,currency:typeof a.currency=="string"?a.currency:void 0}}},cr=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 a={},i=I(s.limit),c=I(s.limit_remaining),l=I(s.usage),u=I(s.usage_daily);return i!==void 0&&(a.totalBalance=i),c!==void 0&&(a.remainingBalance=c),l!==void 0&&(a.usedBalance=l),u!==void 0&&(a.usedDailyBalance=u),this.hasQuotaData(a)?a:null}},lr=class extends se{async queryQuota(e,t,o){let r=Rd(e.baseUrl),a=(await this.fetchJson(r,e,t,o))?.data;if(!a||typeof a!="object")return null;let i={},c=I(a.totalBalance),l=I(a.balance);return c!==void 0&&(i.totalBalance=c),l!==void 0&&(i.remainingBalance=l),c!==void 0&&l!==void 0&&(i.usedBalance=Math.max(0,c-l)),this.hasQuotaData(i)?i:null}},ur=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 a=await fetch("https://api.z.ai/api/monitor/usage/quota/limit",s);if(!a.ok)return null;let i=await a.json(),c=Array.isArray(i?.data?.limits)?i.data.limits:[],l=null,u=null,p=null;for(let m of c)m.type==="TOKENS_LIMIT"?m.unit===3?l=m:m.unit===6?u=m:l||(l=m):m.type==="TIME_LIMIT"&&(p=m);let d={};if(l){let m=I(l.currentValue),h=I(l.usage),f=I(l.remaining),g=I(l.percentage);m!==void 0?d.usedDailyBalance=m:g!==void 0&&(d.usedDailyBalance=g,d.limitDaily=100),h!==void 0?d.limitDaily=h:m!==void 0&&f!==void 0&&(d.limitDaily=m+f),l.nextResetTime&&(d.resetTime=new Date(l.nextResetTime).toISOString())}if(u){let m=I(u.currentValue),h=I(u.usage),f=I(u.remaining),g=I(u.percentage);m!==void 0?d.usedBalance=m:g!==void 0&&(d.usedBalance=g,d.totalBalance=100),h!==void 0?d.totalBalance=h:m!==void 0&&f!==void 0&&(d.totalBalance=m+f),u.nextResetTime&&!d.resetTime&&(d.resetTime=new Date(u.nextResetTime).toISOString())}if(p){if(d.usedDailyBalance===void 0){let m=I(p.currentValue);m!==void 0&&(d.usedDailyBalance=m)}if(d.limitDaily===void 0){let m=I(p.usage),h=I(p.currentValue),f=I(p.remaining);m!==void 0?d.limitDaily=m:h!==void 0&&f!==void 0&&(d.limitDaily=h+f)}p.nextResetTime&&!d.resetTime&&(d.resetTime=new Date(p.nextResetTime).toISOString())}return this.hasQuotaData(d)?d:null}catch{return null}}},dr=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"}}}),a=new URLSearchParams({params:s,region:"cn-beijing"}).toString();try{o&&(r.dispatcher=K(o));let i=await fetch("https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",{...r,body:a});if(!i.ok)return null;let l=(await i.json())?.data?.DataV2?.data?.data?.codingPlanInstanceInfos;if(!Array.isArray(l)||l.length===0)return null;let u=l[0]?.codingPlanQuotaInfo;if(!u)return null;let p={},d=I(u.per5HourUsedQuota),m=I(u.per5HourTotalQuota);d!==void 0&&(p.usedDailyBalance=d),m!==void 0&&(p.limitDaily=m);let h=I(u.perWeekUsedQuota),f=I(u.perWeekTotalQuota);return h!==void 0&&(p.usedBalance=h),f!==void 0&&(p.totalBalance=f),this.hasQuotaData(p)?p:null}catch{return null}}};ud=["per5HourPercentage","per5HourUsedPercentage","fiveHourPercentage"],dd=["per1WeekPercentage","perWeekPercentage","per1WeekUsedPercentage"],pd=["per5HourResetTime","per5HourNextResetTime","fiveHourResetTime"],fd=["per1WeekResetTime","per1WeekNextResetTime","perWeekResetTime"];pr=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 a=await this.queryOfficialEndpoint(r,s,t,o);if(a)return a}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",a=yr(e,"cna")||"",i=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":a}}}),c=new URLSearchParams({params:i,region:"cn-beijing",sec_token:t}).toString(),l={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:c,signal:AbortSignal.timeout(o)};try{r&&(l.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`,l);if(!u.ok)return null;let p=await u.json();return ka(p)}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"}}}),a=new URLSearchParams({params:s,region:"cn-beijing"}).toString(),i={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:a,signal:AbortSignal.timeout(t)};try{o&&(i.dispatcher=K(o));let c=await fetch(`https://cs-data.qianwenai.com/data/api.json?product=sfm_bailian&action=BroadScopeAspnGateway&api=${encodeURIComponent(r)}`,i);if(!c.ok)return null;let l=await c.json();return ka(l)}catch{return null}}},fr=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:(yr(e.quotaToken,"atp-auth-token")||yr(e.quotaToken,"tenantToken")||e.quotaToken).trim(),Cookie:e.quotaToken},signal:AbortSignal.timeout(t)};try{o&&(s.dispatcher=K(o));let a=await fetch("https://maas.xfyun.cn/api/v1/gpt-finetune/coding-plan/list?page=1&size=10",s);if(!a.ok)return null;let i=await a.json();if(i?.succeed===!1||i?.failed===!0)return null;let c=wd(i,e.apiKey),l=c?.codingPlanUsageDTO||c?.usage||c;if(!l||typeof l!="object")return null;let u={},p=I(l.rp5hUsage),d=I(l.rp5hLimit);p!==void 0&&(u.usedDailyBalance=p),d!==void 0&&(u.limitDaily=d);let m=I(l.rpwUsage),h=I(l.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}}},mr=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 a=await fetch("https://api.kimi.com/coding/v1/usages",s);if(!a.ok)return null;let i=await a.json(),c={},l=Array.isArray(i?.limits)?i.limits:[];if(l.length>0&&l[0]?.detail){let p=l[0].detail,d=I(p.limit),m=I(p.remaining),h=p.resetTime;d!==void 0&&m!==void 0&&(c.usedDailyBalance=Math.max(0,d-m),c.limitDaily=d),h&&(c.resetTime=new Date(h).toISOString())}let u=i?.usage;if(u){let p=I(u.limit),d=I(u.remaining),m=I(u.used),h=u.resetTime;p!==void 0&&(c.totalBalance=p,m!==void 0?c.usedBalance=m:d!==void 0&&(c.usedBalance=Math.max(0,p-d),c.remainingBalance=d)),h&&(c.resetTime7d=new Date(h).toISOString(),c.resetTime||(c.resetTime=c.resetTime7d))}return this.hasQuotaData(c)?c:null}catch{return null}}},gr=class extends se{async queryQuota(e,t,o){let r=e.quotaToken||e.apiKey;if(!r)return null;let s=br(e.baseUrl)||"api.minimaxi.com",a=s.endsWith(".minimax.io")||s==="minimax.io"?"api.minimax.io":"api.minimaxi.com";try{let i=[`https://${a}/v1/token_plan/remains`,`https://${a}/v1/api/openplatform/coding_plan/remains`],c=null;for(let k of i)if(c=await this.fetchJson(k,e,t,o,r.trim()),c)break;if(!c)return null;if(c?.base_resp){let k=c.base_resp.status_code;if(k!==void 0&&k!==0)return null}let l=Array.isArray(c?.model_remains)?c.model_remains:[];if(l.length===0)return null;let u=l[0],p={},d=I(u.current_interval_total_count),m=I(u.current_interval_usage_count),h=I(u.current_interval_remaining_percent),f=I(u.end_time);d!==void 0&&m!==void 0&&d>0?(p.usedDailyBalance=Math.max(0,d-m),p.limitDaily=d):h!==void 0&&(p.limitDaily=100,p.usedDailyBalance=Math.max(0,100-h)),f&&(p.resetTime=new Date(f).toISOString());let g=I(u.current_weekly_total_count),_=I(u.current_weekly_usage_count),b=I(u.current_weekly_remaining_percent),y=I(u.weekly_end_time);return g!==void 0&&_!==void 0&&g>0?(p.usedBalance=Math.max(0,g-_),p.remainingBalance=_,p.totalBalance=g):b!==void 0&&(p.totalBalance=100,p.usedBalance=Math.max(0,100-b),p.remainingBalance=b),y&&!p.resetTime&&(p.resetTime=new Date(y).toISOString()),this.hasQuotaData(p)?p:null}catch{return null}}},yd=new ar,_d=new cr,bd=new lr,kd=new ur,Td=new dr,vd=new pr,xd=new fr,Cd=new mr,Sd=new gr});function xa(n){return{[Pd]:n,[Ed]:Kt}}function Re(n){return typeof n.apiKey=="string"&&n.apiKey.startsWith("ccr-managed-")}function Ad(n){return n===!0||n==="true"||n===1||n==="1"}function Id(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 Md(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 Nd(n){try{let e=new URL(n).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function Tr(n,e,t){let o=performance.now(),r=()=>Math.max(0,Math.round(performance.now()-o));if(Re(n))return{success:!1,skipped:!0,error:vr,latencyMs:r()};let s=Md(n.baseUrl);if(!s)return{success:!1,error:"Cannot derive models endpoint from baseUrl",errorKind:"network",latencyMs:r()};try{let a={method:"GET",headers:{Authorization:`Bearer ${n.apiKey}`},signal:AbortSignal.timeout(e)};t&&(a.dispatcher=K(t));let i=await fetch(s,a);if(i.headers&&it(n.name,n.baseUrl,i.headers),i.ok)return{success:!0,headers:i.headers,latencyMs:r()};if(i.status===429)return{success:!1,error:`HTTP 429: ${(await i.text().catch(()=>"")).slice(0,100)||"Rate limited"}`,headers:i.headers,latencyMs:r(),errorKind:"http"};if(i.status>=400&&i.status<500)return{success:!0,headers:i.headers,latencyMs:r()};let c=await i.text().catch(()=>"");return{success:!1,error:`HTTP ${i.status}: ${c.slice(0,100)}`,headers:i.headers,latencyMs:r(),errorKind:"http"}}catch(a){let i=a?.message||a?.toString()||"Unknown probe error",c=a?.name==="TimeoutError"||a?.name==="AbortError"||a?.code===20||a?.code==="ABORT_ERR";return{success:!1,error:i,latencyMs:r(),errorKind:c?"timeout":"network"}}}async function $d(n,e,t,o){if(Re(n))return{success:!1,skipped:!0,error:vr};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,a=n.baseUrl.includes("/messages")||r.includes("claude");try{let i={"Content-Type":"application/json",...xa("scheduled-wakeup")};a?(i["x-api-key"]=n.apiKey,i["anthropic-version"]="2023-06-01"):i.Authorization=`Bearer ${n.apiKey}`;let c={method:"POST",headers:i,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&&(c.dispatcher=K(t)),o?.info(`Sending scheduled wake-up call to provider ${n.name} using model ${r}...`);let l=await fetch(s,c);if(l.ok)return{success:!0};let u=await l.text().catch(()=>"");return{success:!1,error:`HTTP ${l.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(i){return{success:!1,error:i?.message||i?.toString()||"Unknown wake-up error"}}}async function Ld(n,e,t,o,r){if(Re(n))return{success:!1,skipped:!0,error:vr};let s=n.baseUrl,a=n.baseUrl.includes("/messages")||e.includes("claude");try{let i={"Content-Type":"application/json",...xa("rate-limit-probe")};a?(i["x-api-key"]=n.apiKey,i["anthropic-version"]="2023-06-01"):i.Authorization=`Bearer ${n.apiKey}`;let c={method:"POST",headers:i,body:JSON.stringify({model:e,messages:[{role:"user",content:"Hello, please reply briefly."}],max_tokens:10}),signal:AbortSignal.timeout(t)};o&&(c.dispatcher=K(o)),r?.debug?.(`Sending independent ping to rate-limited model ${n.name} (${e})...`);let l=await fetch(s,c);if(l.ok)return{success:!0};let u=await l.text().catch(()=>"");return{success:!1,error:`HTTP ${l.status}: ${u.slice(0,200)||"Unknown error"}`}}catch(i){return{success:!1,error:i?.message||i?.toString()||"Unknown ping error"}}}function Ca(n,e,t,o,r){return _e||(_e=new Hn(n,e,t,o,r)),_e}function _t(n,e,t,o,r){let s=Ca(n,e,t,o,r);return s.start(),s}function xr(){_e&&_e.stop()}function Cr(){_e&&(_e.stop(),_e=null)}var Pd,Ed,Od,vr,Hn,_e,zn=w(()=>{"use strict";re();Me();kr();Le();Ro();Z();Pd="x-claude-code-router-source",Ed="x-claude-code-router-version";Od={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,slowThresholdMs:3e3,initialDelayMs:5e3,excludeProviders:[]};vr="Managed provider requires its plugin's authenticated model sync";Hn=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={...Od,...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,a;t.success?a=s?"slow":"healthy":a=t.errorKind==="timeout"?"timeout":"error";let i=this.probeTelemetry.get(e),c={provider:e,latencyMs:t.latencyMs,status:a,isSlow:s,errorKind:t.errorKind,errorMessage:t.error,lastProbeAt:r,lastSuccessAt:t.success?r:i?.lastSuccessAt,source:o};return this.probeTelemetry.set(e,c),s&&this.logger?.warn?.(`Slow network detected for provider ${e}: probe took ${t.latencyMs} ms (threshold ${this.config.slowThresholdMs} ms)`),c}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&&!Re(s)&&!this.config.excludeProviders.includes(s.name)),t=U();if(e.length===0)return;let o=[];for(let s of e){let a=_r(s.baseUrl),i=Array.isArray(s.models)?s.models:[],c=this.resolveProxyUrl?.(s);if(!a){let l;try{l=new URL(s.baseUrl).hostname}catch{l=void 0}this.logger?.debug?.(`No quota adapter for provider ${s.name}`+(l?` (host: ${l})`:"")+" \u2014 skipping quota probe")}a&&o.push({provider:s.name,models:i,type:"quota-adapter",promise:a.queryQuota(s,this.config.probeTimeoutMs,c).then(l=>{if(l){Ko(s.name,l),this.logger?.debug?.(`Stored quota probe result for ${s.name}`);let u=l.limitDaily!==void 0&&l.usedDailyBalance!==void 0&&l.usedDailyBalance>=l.limitDaily,p=l.totalBalance!==void 0&&l.usedBalance!==void 0&&l.usedBalance>=l.totalBalance;if(u||p){let d=u?`Quota exhausted: 5h limit reached (${l.usedDailyBalance}/${l.limitDaily})`:`Quota exhausted: 7d balance depleted (${l.usedBalance}/${l.totalBalance})`;for(let m of i){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,d)}this.logger?.warn?.(`${d} for ${s.name}, marked as unhealthy`)}else for(let d of i){let m=t.getState(s.name,d);m&&m.status==="open"&&m.lastError?.includes("Quota exhausted")&&(t.recover(s.name,d),this.logger?.info?.(`Quota recovered for ${s.name} (${d}), marked as healthy`))}}})}),Nd(s.baseUrl)&&o.push({provider:s.name,models:i,type:"rate-limit-headers",promise:Tr(s,this.config.probeTimeoutMs,c).then(l=>{this.recordProbe(s.name,l,"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 a=r[s];if(a.status==="rejected"){let i=o[s];this.logger?.warn?.(`Quota probe failed for ${i.provider} (${i.type}): ${a.reason}`)}}}async runHealthProbe(){let e=U(),t=this.getProviders().filter(r=>r?.name&&r.enabled!==!1&&!Re(r)&&!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=>Tr(r,this.config.probeTimeoutMs,this.resolveProxyUrl?.(r))));for(let r=0;r<t.length;r++){let s=t[r],a=o[r],i=Array.isArray(s.models)?s.models:[];if(a.status==="fulfilled"){let c=a.value;if(c.skipped)continue;if(this.recordProbe(s.name,c,"health"),c.success){let l=0;for(let u of i){let p=e.getState(s.name,u);if(p&&p.status==="open"){let d=p.lastError?.includes("Quota exhausted"),m=p.rateLimitUntil||p.lastError&&(p.lastError.includes("429")||p.lastError.toLowerCase().includes("rate limit")||p.lastError.toLowerCase().includes("rate_limit")||p.lastError.toLowerCase().includes("too many requests")||p.lastError.toLowerCase().includes("\u9650\u6D41"));if(d){this.logger?.debug?.(`Skipping health probe recovery for ${s.name} (${u}) because quota is exhausted`);continue}if(m||p.rateLimitUntil){this.logger?.debug?.(`Skipping health probe recovery for rate-limited ${s.name} (${u})`);continue}}e.recordSuccess(s.name,u),l++}l>0&&this.logger?.info?.(`Health probe succeeded for ${s.name}`)}else{for(let l of i)e.recordFailure(s.name,l,c.error);this.logger?.warn?.(`Health probe failed for ${s.name}: ${c.error}`)}}else{this.recordProbe(s.name,{success:!1,error:a.reason?.message||String(a.reason||"Probe error"),latencyMs:0,errorKind:"network"},"health");for(let c of i)e.recordFailure(s.name,c,a.reason?.message||"Probe error");this.logger?.warn?.(`Health probe error for ${s.name}: ${a.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[a,...i]=s.split(","),c=i.join(",");if(!a||!c)continue;let l=o.find(d=>d?.name===a);if(l&&Re(l))continue;let u=e.getState(a,c);if(!u||!u.rateLimitUntil)continue;if(Date.now()>=u.rateLimitUntil){e.recover(a,c),this.logger?.info?.(`Rate-limit cooldown expired, auto-recovered ${s}`);continue}let p=o.find(d=>d?.name===a&&d.enabled!==!1);if(!p){this.logger?.warn?.(`Provider ${a} not found or disabled for rate-limit probe`);continue}try{let d=await Ld(p,c,r,this.resolveProxyUrl?.(p),this.logger);if(d.success)this.logger?.info?.(`Rate-limit probe succeeded for ${s}, recovering model`),e.recover(a,c);else if(d.error&&(d.error.includes("429")||d.error.toLowerCase().includes("rate limit")||d.error.toLowerCase().includes("rate_limit")||d.error.toLowerCase().includes("too many requests")||d.error.toLowerCase().includes("\u9650\u6D41"))){let m=Date.now();u.rateLimitUntil&&u.rateLimitUntil>m?u.rateLimitUntil+=120*1e3:u.rateLimitUntil=m+120*1e3,this.logger?.debug?.(`Rate-limit probe for ${s} confirmed still limited, extending cooldown`)}else this.logger?.debug?.(`Rate-limit probe for ${s} returned non-rate-limit error: ${d.error?.slice(0,100)}`)}catch(d){this.logger?.warn?.(`Rate-limit probe exception for ${s}: ${d?.message||d}`)}}}async runScheduledWakeup(){if(!this.isWakeupGloballyEnabled())return;let e=this.getProviders().filter(i=>i?.name&&i.enabled!==!1&&!Re(i)&&i.wakeupEnabled===!0);if(e.length===0)return;let t=new Date,o=t.getHours(),r=t.getMinutes(),s=Id(t),a=this.getConfig&&this.getConfig("WAKEUP_TIME")||"06:00";for(let i of e){let c=i.wakeupTime||a,[l,u]=c.split(":"),p=parseInt(l,10),d=parseInt(u,10);if(isNaN(p)||isNaN(d))continue;let m=o>p||o===p&&r>=d,h=(p*60+d)*6e4-(o*60+r)*6e4,f=h>0&&h<=300*1e3;(m||f)&&!this.hasWakeupFiredToday(i.name,s)&&(this.logger?.info?.(`Scheduled wake-up triggered for provider ${i.name} at ${c}`),$d(i,3e4,this.resolveProxyUrl?.(i),this.logger).then(g=>{g.success?this.logger?.info?.(`Successfully woke up provider ${i.name}`):this.logger?.error?.(`Failed to wake up provider ${i.name}: ${g.error}`)}).catch(g=>{this.logger?.error?.(`Error in wake-up task for ${i.name}: ${g}`)}))}}isWakeupGloballyEnabled(){return this.getConfig?Ad(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(i=>i.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 Tr(o,this.config.probeTimeoutMs,this.resolveProxyUrl?.(o));if(r.skipped)return{success:!1,skipped:!0,latencyMs:r.latencyMs,status:"skipped",isSlow:!1,error:r.error};let s=this.recordProbe(o.name,r,"manual"),a=Array.isArray(o.models)?o.models:[];if(r.success)for(let i of a)U().recordSuccess(o.name,i);else for(let i of a)U().recordFailure(o.name,i,r.error);return{success:r.success,latencyMs:r.latencyMs,status:s.status,isSlow:s.isSlow,errorKind:r.errorKind,error:r.error}}},_e=null});var Sa=w(()=>{"use strict"});var Sr,bt,Ra=w(()=>{"use strict";Sr=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()}},bt=new Sr});var ce,Rr=w(()=>{"use strict";ce=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,t)=>{this.buffer+=e;let o=this.buffer.split(`
|
|
309
|
+
`);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 Qe,wr=w(()=>{"use strict";Qe=class extends TransformStream{constructor(){super({transform:(e,t)=>{let o="";e.event&&(o+=`event: ${e.event}
|
|
306
310
|
`),e.id&&(o+=`id: ${e.id}
|
|
307
311
|
`),e.retry&&(o+=`retry: ${e.retry}
|
|
308
312
|
`),e.data&&(e.data.type==="done"?o+=`data: [DONE]
|
|
309
313
|
`:o+=`data: ${JSON.stringify(e.data)}
|
|
310
314
|
`),o+=`
|
|
311
|
-
`,t.enqueue(o)}})}}});var
|
|
312
|
-
`+(this.config.colors?`${this.colors.dim}`:""),
|
|
315
|
+
`,t.enqueue(o)}})}}});var Bn,Pr=w(()=>{"use strict";Bn=(n,e)=>{let t=n.getReader();return new ReadableStream({async start(o){try{for(;;){let{done:r,value:s}=await t.read();if(r){o.close();break}let a=await e(s,o);a!==void 0&&o.enqueue(a)}}catch(r){o.error(r)}finally{t.releaseLock()}}})}});var Er=w(()=>{"use strict";Rr();wr();Pr()});var wa=w(()=>{"use strict"});var kt,Or=w(()=>{"use strict";kt=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:a}=t||{},i="";if(r){let c=new Date().toISOString();i+=this.config.colors?`${this.colors.cyan}[${c}]${this.colors.reset} `:`[${c}] `}switch(s&&(i+=this.config.colors?`${this.colors.bright}${s}${this.colors.reset} `:`${s} `),o){case"json":i+=JSON.stringify(e,null,2);break;case"markdown":typeof e=="object"?i+=this.toMarkdown(e):i+=String(e);break;case"text":default:typeof e=="object"?i+=JSON.stringify(e,null,2):i+=String(e);break}return a&&Object.keys(a).length>0&&(i+=`
|
|
316
|
+
`+(this.config.colors?`${this.colors.dim}`:""),i+="Metadata: "+JSON.stringify(a,null,2),this.config.colors&&(i+=this.colors.reset)),i}toMarkdown(e,t=0){let o=" ".repeat(t);return Array.isArray(e)?e.map(r=>typeof r=="object"?`${o}-
|
|
313
317
|
${this.toMarkdown(r,t+1)}`:`${o}- ${r}`).join(`
|
|
314
318
|
`):typeof e=="object"&&e!==null?Object.entries(e).map(([r,s])=>typeof s=="object"&&s!==null?`${o}${r}:
|
|
315
319
|
${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 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=typeof t.metadata?.statsKey=="string"&&t.metadata.statsKey?t.metadata.statsKey:t.metadata?.sessionId;if(!o)return!1;let r=t.metadata?.sessionId??o,s=JSON.stringify({...e,timestamp:Date.now(),sessionId:r},null,2),i=this.getFilePath(o);return await Nn(i,async()=>{await this.ensureDir();let a=hr(this.baseDir,`.ccr-tmp-${process.pid}-${rd()}.tmp`);return Nn(a,async()=>{let l=!1;try{let c=await td(a,"wx");l=!0;try{await c.writeFile(s,"utf-8")}finally{await c.close()}return await nd(a,i),!0}finally{l&&await od(a).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(`
|
|
320
|
+
`):`${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 Tt,Ar=w(()=>{"use strict";Tt=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:a}=t||{},i={data:e};return r&&(i.timestamp=new Date().toISOString()),s&&(i.prefix=s),a&&Object.keys(a).length>0&&(i.metadata=a),i}async sendRequest(e,t,o,r,s){let a=new AbortController,i=setTimeout(()=>a.abort(),s);try{let c=await fetch(e,{method:t,headers:o,body:JSON.stringify(r),signal:a.signal});if(clearTimeout(i),!c.ok)throw new Error(`HTTP ${c.status}: ${c.statusText}`);return c}catch(c){throw clearTimeout(i),c}}delay(e){return new Promise(t=>setTimeout(t,e))}async sendWithRetry(e,t,o,r,s,a){let i=null;for(let c=1;c<=a.maxAttempts;c++)try{return await this.sendRequest(e,t,o,r,s)}catch(l){if(i=l,c===a.maxAttempts)break;let u=a.backoffMs*Math.pow(2,c-1);console.warn(`[WebhookOutputHandler] Request failed (attempt ${c}/${a.maxAttempts}), retrying in ${u}ms...`,l.message),await this.delay(u)}throw i}async output(e,t={}){let o=t.timeout||this.defaultTimeout;try{let r=this.buildHeaders(),s=this.buildBody(e,t),a=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 Wn,opendir as Fd,unlink as Ud}from"node:fs/promises";import{join as Ea,resolve as Nr}from"node:path";import{tmpdir as jd}from"node:os";async function Kn(n,e){let t=Nr(n),o=Jn.get(t),r,s=new Promise(a=>{r=a});Jn.set(t,s);try{return await o,await e()}finally{r(),Jn.get(t)===s&&Jn.delete(t)}}function Oa(n,e){return n.dev===e.dev&&n.ino===e.ino}async function Pa(n,e){try{let t=await Wn(n);return t.isDirectory()&&!t.isSymbolicLink()&&Oa(e,t)}catch{return!1}}function Wd(){return new Promise(n=>{setTimeout(n,10).unref()})}async function Aa(n,e,t,o){let r=Nr(n);if(o?.aborted||Ir.has(r))return 0;Ir.add(r);let s=0;try{let a=await Wn(r);if(!a.isDirectory()||a.isSymbolicLink()||o?.aborted)return 0;let i=await Fd(r,{bufferSize:32});try{let c=0,l=!0;for await(let u of i){if(o?.aborted||!l)break;try{if(!u.isFile()||u.isSymbolicLink()||!t(u.name))continue;let p=Ea(r,u.name);try{let d=await Wn(p);if(!d.isFile()||d.isSymbolicLink()||d.mtimeMs>=e)continue;await Kn(p,async()=>{if(o?.aborted||!await Pa(r,a))return!1;let h=await Wn(p);return!h.isFile()||h.isSymbolicLink()||!Oa(d,h)||h.mtimeMs!==d.mtimeMs||h.mtimeMs>=e||o?.aborted?!1:(await Ud(p),!0)})&&s++}catch{}}finally{++c%64===0&&(await Wd(),l=!o?.aborted&&await Pa(r,a))}}}finally{await i.close().catch(()=>{})}}catch{}finally{Ir.delete(r)}return s}function Ia(n,e,t,o,r){if(!Number.isFinite(o)||o<=0)return Promise.resolve(0);let s=`${e}-`,a=t?`.${t}`:"";return Aa(n,Date.now()-o,i=>Jd.test(i)?!0:!i.endsWith(".tmp")&&i.startsWith(s)&&i.endsWith(a),r)}function Kd(n,e){let t=Date.now()-Hd;return Aa(n,t,o=>{let r=Bd.exec(o);return r!==null&&Number(r[1])<t},e)}function $r(n,e,t){let o=Mr.get(n);if(!o){let s=new AbortController,a,i=()=>{s.signal.aborted||Promise.resolve().then(()=>e(s.signal)).catch(()=>{})},c=setTimeout(()=>{s.signal.aborted||(i(),a=setInterval(i,zd),a.unref())},t);c.unref(),o={users:0,stop:()=>{s.abort(),clearTimeout(c),a&&clearInterval(a)}},Mr.set(n,o)}o.users++;let r=!1;return()=>{r||(r=!0,--o.users===0&&(o.stop(),Mr.delete(n)))}}function Ma(n=Ea(jd(),"claude-code-router")){return $r(`legacy:${Nr(n)}`,e=>Kd(n,e),1e3)}var Hd,zd,Bd,Jd,Ir,Jn,Mr,Lr=w(()=>{"use strict";Hd=3600*1e3,zd=600*1e3,Bd=/^session-[A-Za-z0-9_.-]+-(\d{13})\.json$/,Jd=/^\.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$/,Ir=new Set,Jn=new Map,Mr=new Map});import{lstat as Gd,mkdir as Vd,open as Qd,rename as Yd,unlink as Xd}from"node:fs/promises";import{randomUUID as Zd}from"node:crypto";import{sanitizeTempFileKey as qd}from"@wengine-ai/claude-code-router-shared";import{basename as ep,join as Dr,resolve as tp}from"node:path";import{tmpdir as np}from"node:os";var vt,Fr=w(()=>{"use strict";Lr();vt=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=tp(Dr(np(),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=$r(`handler:${t}`,o=>this.pruneOldFiles(o),6e4)}}async ensureDir(){await Vd(this.baseDir,{recursive:!0});let e=await Gd(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}-${qd(e)}${r}${o}`;if(ep(s)!==s||s.includes("\\"))throw new Error("Temp file configuration must not contain path separators");return Dr(this.baseDir,s)}async output(e,t={}){try{let o=typeof t.metadata?.statsKey=="string"&&t.metadata.statsKey?t.metadata.statsKey:t.metadata?.sessionId;if(!o)return!1;let r=t.metadata?.sessionId??o,s=JSON.stringify({...e,timestamp:Date.now(),sessionId:r},null,2),a=this.getFilePath(o);return await Kn(a,async()=>{await this.ensureDir();let i=Dr(this.baseDir,`.ccr-tmp-${process.pid}-${Zd()}.tmp`);return Kn(i,async()=>{let c=!1;try{let l=await Qd(i,"wx");c=!0;try{await l.writeFile(s,"utf-8")}finally{await l.close()}return await Yd(i,a),!0}finally{c&&await Xd(i).catch(()=>{})}})})}catch{return!1}}pruneOldFiles(e=this.controller.signal){return Ia(this.baseDir,this.config.prefix||"session",this.config.extension||"",this.config.retentionMs,e)}dispose(){this.controller.abort(),this.stopSchedule?.()}getBaseDir(){return this.baseDir}}});var Ur,Ye,Na=w(()=>{"use strict";Or();Ar();Fr();Ur=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 kt(e.config);case"webhook":return new Tt(e.config);case"temp-file":return new vt(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([a,i])=>{try{await i.output(e,o)?r.success.push(a):r.failed.push(a)}catch(c){console.error(`[OutputManager] Handler ${a} failed:`,c),r.failed.push(a)}});return await Promise.all(s),r}async outputTo(e,t,o){let r={...this.defaultOptions,...o},s={success:[],failed:[]},a=e.map(async i=>{let c=this.handlers.get(i);if(!c){console.warn(`[OutputManager] Handler ${i} not found`),s.failed.push(i);return}try{await c.output(t,r)?s.success.push(i):s.failed.push(i)}catch(l){console.error(`[OutputManager] Handler ${i} failed:`,l),s.failed.push(i)}});return await Promise.all(a),s}async outputToType(e,t,o){let r=Array.from(this.handlers.entries()).filter(([s,a])=>a.type===e).map(([s])=>s);return this.outputTo(r,t,o)}},Ye=new Ur});var jr=w(()=>{"use strict";wa();Or();Ar();Fr();Na()});import op from"fastify-plugin";import{TOKEN_SPEED_TEMP_SUBDIRECTORY as rp}from"@wengine-ai/claude-code-router-shared";function Gn(){return{subdirectory:rp,extension:"json",includeTimestamp:!1,prefix:"session"}}function $a(n,e,t=0){if(n<=0)return 0;let o=Math.max((e-t)/1e3,sp);return Math.round(n/o)}function Ct(n){let e=(n.match(/[\u4e00-\u9fa5]/g)||[]).length,t=n.length-e;return Math.ceil(e/1.5+t/4)}async function La(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()},a={prefix:r,metadata:{sessionId:n.sessionId,statsKey:n.statsKey},...t};for(let i of e)try{await Ye.outputToType(i,s,a)}catch(c){console.error(`[TokenSpeedPlugin] Failed to output to ${i}:`,c)}}var xt,Hr,sp,Vn,Da=w(()=>{"use strict";Er();jr();De();xt=new Map,Hr=new Map,sp=1,Vn={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:op(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)Ye.registerHandlers(t.outputHandlers);else{let a=[];for(let i of o)i==="console"?a.push({type:"console",enabled:!0,config:{colors:!0,level:"log"}}):i==="temp-file"?a.push({type:"temp-file",enabled:!0,config:Gn()}):i==="webhook"&&console.warn("[TokenSpeedPlugin] Webhook reporter requires explicit configuration in outputHandlers");a.length>0&&Ye.registerHandlers(a)}t.outputOptions&&Ye.setDefaultOptions(t.outputOptions);let r=async a=>{let i=n.tokenizerService;if(!i)return n.log?.warn("TokenizerService not available"),null;if(!a.provider||!a.model)return null;let c=a.provider,l=a.model,u=`${c}:${l}`;if(Hr.has(u))return Hr.get(u);let p=i.getTokenizerConfigForModel(c,l);if(!p)return n.log?.debug(`No tokenizer config for ${c}:${l}, using fallback`),null;try{let d=await i.getTokenizer(p,c);return Hr.set(u,d),n.log?.info(`Created tokenizer for ${c}:${l} - ${d.name}`),d}catch(d){return n.log?.warn(`Failed to create tokenizer for ${c}:${l}: ${d.message}`),null}},s=a=>{let i=new URL(`http://127.0.0.1${a}`).pathname;return i.endsWith("/v1/messages")||i.endsWith("/v1/responses")};n.addHook("onRequest",async a=>{s(a.url)&&(a.requestStartTime=performance.now())}),n.addHook("onSend",async(a,i,c)=>{let l=a.requestStartTime;if(!l)return;let u=a.id||Date.now().toString(),p=a.usageSessionId||u,d=a.sessionId;if(!d)try{d=he(a.body?.metadata?.user_id)||p}catch{d=p}let m=await r(a);if(c instanceof ReadableStream){xt.set(u,{requestId:u,sessionId:d,statsKey:p,startTime:l,lastTokenTime:l,tokenCount:0,tokensPerSecond:0,stream:!0});let[_,b]=c.tee(),y=async()=>{let k=null,T=async S=>{let v=xt.get(u);if(!v)return;let R=v.firstTokenTime?v.firstTokenTime-v.startTime:0,x=(S?v.lastTokenTime:performance.now())-v.startTime;v.tokensPerSecond=$a(v.tokenCount,x,R),await La(v,o,t.outputOptions,S).catch(E=>{n.log?.warn(`Failed to output streaming stats: ${E.message}`)})};try{let v=b.pipeThrough(new TextDecoderStream).pipeThrough(new ce).getReader();for(k=setInterval(async()=>{xt.get(u)&&await T(!1)},1e3);;){let{done:R,value:C}=await v.read();if(R)break;let x=C,E=xt.get(u);if(!E)continue;let O=performance.now(),P=x.event||x.data?.type,M=x.event==="content_block_start"||x.event==="content_block_delta"||x.event==="text_block"||x.event==="content_block"||P==="response.output_text.delta"||P==="response.function_call_arguments.delta"||P==="response.reasoning_summary_text.delta";!E.firstTokenTime&&M&&(E.firstTokenTime=O,E.timeToFirstToken=Math.round(O-E.startTime));let N="";if(x.event==="content_block_delta"&&x.data?.delta){let L=x.data.delta.type;L==="text_delta"?N=x.data.delta.text||"":L==="input_json_delta"?N=x.data.delta.partial_json||"":L==="thinking_delta"&&(N=x.data.delta.thinking||"")}if((P==="response.output_text.delta"||P==="response.function_call_arguments.delta"||P==="response.reasoning_summary_text.delta")&&(N=typeof x.data?.delta=="string"?x.data.delta:""),N){let L=m&&m.encodeText?m.encodeText(N).length:Ct(N);E.tokenCount+=L,E.lastTokenTime=O}(x.event==="message_stop"||P==="response.completed"||P==="response.failed")&&(k&&(clearInterval(k),k=null),await T(!0),xt.delete(u))}}catch(S){k&&clearInterval(k),S.name!=="AbortError"&&S.code!=="ERR_STREAM_PREMATURE_CLOSE"&&n.log?.warn(`Error processing token stats: ${S.message}`)}};return a.tokenSpeedCapturePromise=y().catch(k=>{console.log(k),n.log?.warn(`Background stats processing failed: ${k.message}`)}),_}let h=performance.now(),f=0,g=!1;if(c&&typeof c=="string")try{let _=JSON.parse(c);if(typeof _.usage?.output_tokens=="number")f=_.usage.output_tokens,g=!0;else{let b=_.content??_.message?.content;if(b!==void 0)if(g=!0,m)Array.isArray(b)?f=b.reduce((y,k)=>{if(k.type==="text"){let T=k.text||"";return y+(m.encodeText?m.encodeText(T).length:Ct(T))}return y},0):typeof b=="string"&&(f=m.encodeText?m.encodeText(b).length:Ct(b));else{let y=Array.isArray(b)?b.map(k=>k.text).join(""):typeof b=="string"?b:"";f=Ct(y)}}}catch{}if(g){let _=h-l,b={requestId:u,sessionId:d,statsKey:p,startTime:l,lastTokenTime:h,tokenCount:f,tokensPerSecond:$a(f,_),stream:!1};await La(b,o,t.outputOptions,!0)}return c})})}});var zr=w(()=>{"use strict";Sa();Ra();Da();jr()});import ne from"node:fs/promises";import Fa from"json5";import we from"node:path";import{CONFIG_FILE as oe,HOME_DIR as Qn,PLUGINS_DIR as ip}from"@wengine-ai/claude-code-router-shared";function Ua(){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 Br,St,Yn,Rt,Q,Xn,le,ap,Jr,wt=w(()=>{"use strict";Br=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(Br);if(n!==null&&typeof n=="object"){let e={};for(let[t,o]of Object.entries(n))e[t]=Br(o);return e}return n},St=async n=>{try{await ne.access(n)}catch{await ne.mkdir(n,{recursive:!0})}},Yn=async()=>{await St(Qn),await St(ip),await St(we.join(Qn,"logs"))},Rt=async()=>{let n=await ne.readFile(oe,"utf-8");return Fa.parse(n)},Q=async()=>{try{let n=await ne.readFile(oe,"utf-8");try{let e=Fa.parse(n);return Br(e)}catch(e){console.error(`Failed to parse config file at ${oe}`),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 Yn();let e=await Xn();e&&console.log(`Backed up existing configuration file to ${e}`);let t={PORT:3456,Providers:[],Router:{}};return await le(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 ${oe}`),console.error("Error details:",n.message),process.exit(1)}};Xn=async()=>{Ua();try{if(await ne.access(oe).then(()=>!0).catch(()=>!1)){let n=new Date().toISOString().replace(/[:.]/g,"-"),e=`${oe}.${n}.bak`;await ne.copyFile(oe,e);try{let t=we.dirname(oe),o=we.basename(oe),s=(await ne.readdir(t)).filter(a=>a.startsWith(o)&&a.endsWith(".bak")).sort().reverse();if(s.length>3)for(let a=3;a<s.length;a++){let i=we.join(t,s[a]);await ne.unlink(i)}}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},le=async n=>{Ua(),await St(Qn),await ap();let e=`${JSON.stringify(n,null,2)}`;await ne.writeFile(oe,e)},ap=async()=>{if(!await ne.access(oe).then(()=>!0).catch(()=>!1))return;let e=we.join(Qn,"config-history");await St(e);let t=new Date().toISOString().replace(/[:.]/g,"-"),o=we.join(e,`config-${t}.json`);await ne.copyFile(oe,o);try{let s=(await ne.readdir(e)).filter(a=>a.startsWith("config-")&&a.endsWith(".json")).sort().reverse();if(s.length>50)for(let a of s.slice(50))await ne.unlink(we.join(e,a))}catch(r){console.warn("Failed to prune config-history:",r)}},Jr=async()=>{let n=await Q();return Object.assign(process.env,n),n}});function cp(n){return n?.cache_read_input_tokens??n?.input_tokens_details?.cached_tokens??n?.prompt_tokens_details?.cached_tokens??0}function lp(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 Xe(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:cp(n),cache_creation_input_tokens:lp(n)}}function Zn(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 Wr=w(()=>{"use strict"});function qn(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(Kr(r,s))}return}if(Array.isArray(n)){for(let o of n)qn(o,e,t);return}if(typeof n=="object")for(let o of Object.values(n))qn(o,e,t)}}function ja(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 a=Array.isArray(r?.models)?r.models:[];for(let i of a)i&&o.add(Kr(s,i))}return qn(n?.Router,t,o),qn(n?.fallback,t,o),o}function Pt(n,e){try{let t=U();if(!t?.getAllStates||!t?.recover)return 0;let o=ja(n),r=0;for(let s of t.getAllStates()||[]){let a=s?.provider,i=s?.model;!a||!i||o.has(Kr(a,i))||(t.recover(a,i),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 eo(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 up,Kr,Et=w(()=>{"use strict";re();up="\0",Kr=(n,e)=>`${n}${up}${e}`});function dp(n){return/\[1m\]/i.test(n)?1e6:2e5}function pp(n){let e=n.toLowerCase();return e.includes("haiku")||e.includes("mini")?8192:32e3}function fp(n,e=n){let t=dp(n),o=pp(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 to(n){let e=new Map,t=(o,r=o)=>{!o||e.has(o)||e.set(o,fp(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 mp(n){let e=n.ip||n.connection?.remoteAddress||"";return e==="127.0.0.1"||e==="::1"||e==="::ffff:127.0.0.1"}var no,Vr=w(()=>{"use strict";no=n=>async(e,t,o)=>{if(["/","/health"].includes(e.url)||e.url.startsWith("/ui")||mp(e))return o();let s=n.Providers||n.providers||[];if(!s||s.length===0)return o();let a=n.APIKEY;if(!a){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 i=e.headers.authorization||e.headers["x-api-key"],c=Array.isArray(i)?i[0]:i||"";if(!c){t.log.warn({url:e.url,headers:Object.keys(e.headers)},"[AUTH] No auth header"),t.status(401).send("APIKEY is missing");return}let l="";if(c.startsWith("Bearer")?l=c.split(" ")[1]:l=c,l!==a){t.log.warn({url:e.url,gotPrefix:l.slice(0,10),gotSuffix:l.slice(-6),gotLen:l.length,expectedPrefix:a.slice(0,6),expectedSuffix:a.slice(-6),expectedLen:a.length},"[AUTH] Key mismatch"),t.status(401).send("Invalid API key");return}o()}});var Ya={};mo(Ya,{append:()=>Nt,clear:()=>Zr,close:()=>Lp,getOldestRecordTimestamp:()=>Dp,query:()=>Xr,querySummary:()=>$t,readTokenSpeedStats:()=>qr});import{existsSync as Ba,mkdirSync as gp,readFileSync as Ja}from"fs";import{join as so}from"path";import{tmpdir as hp}from"os";import{openSqliteDatabase as yp}from"@wengine-ai/claude-code-router-shared";import{HOME_DIR as _p,sanitizeTempFileKey as bp,TOKEN_SPEED_TEMP_SUBDIRECTORY as kp}from"@wengine-ai/claude-code-router-shared";function Sp(){Ba(ro)||gp(ro,{recursive:!0})}function Mt(){if(ke)return ke;Sp();let n=yp(Tp),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&&(Rp(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")),Pp(e),Ga(e,!0),Yr=e.prepare(`
|
|
317
321
|
INSERT INTO usage_records (
|
|
318
322
|
id, timestamp, session_id, provider, original_model, model, upstream_model, model_family,
|
|
319
323
|
scenario_type, client_type,
|
|
@@ -327,7 +331,7 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
327
331
|
@cache_read_input_tokens, @cache_creation_input_tokens, @ttft,
|
|
328
332
|
@tokens_per_second, @duration_ms, @status, @error_message, @response_body
|
|
329
333
|
)
|
|
330
|
-
`),
|
|
334
|
+
`),ke=e,ke}function Rp(n){n.exec(`
|
|
331
335
|
CREATE TABLE IF NOT EXISTS usage_records (
|
|
332
336
|
id TEXT PRIMARY KEY,
|
|
333
337
|
timestamp TEXT NOT NULL,
|
|
@@ -364,12 +368,12 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
364
368
|
CREATE INDEX IF NOT EXISTS idx_usage_records_scenario_timestamp ON usage_records(scenario_type, timestamp);
|
|
365
369
|
CREATE INDEX IF NOT EXISTS idx_usage_records_client_timestamp ON usage_records(client_type, timestamp);
|
|
366
370
|
CREATE INDEX IF NOT EXISTS idx_usage_records_session_timestamp ON usage_records(session_id, timestamp);
|
|
367
|
-
`)}function
|
|
371
|
+
`)}function wp(n,e){return n.prepare("SELECT value FROM usage_meta WHERE key = ?").get(e)?.value}function Ot(n,e,t){n.prepare(`
|
|
368
372
|
INSERT INTO usage_meta (key, value)
|
|
369
373
|
VALUES (?, ?)
|
|
370
374
|
ON CONFLICT(key) DO UPDATE SET value = excluded.value
|
|
371
|
-
`).run(e,t)}function
|
|
372
|
-
`)){let
|
|
375
|
+
`).run(e,t)}function Pp(n){if(wp(n,Ha)||!Ba(Qr))return;let e=Op(n,"INSERT OR IGNORE"),t=Ja(Qr,"utf-8"),o=0,r=0,s=0;n.transaction(()=>{for(let i of t.split(`
|
|
376
|
+
`)){let c=i.trim();if(c)try{let l=JSON.parse(c);if(!Ep(l)){r++;continue}e.run(Ka(io(l))).changes>0?o++:s++}catch{r++}}Ot(n,Ha,new Date().toISOString()),Ot(n,"legacy_usage_jsonl_migrated_path",Qr),Ot(n,"legacy_usage_jsonl_imported_count",String(o)),Ot(n,"legacy_usage_jsonl_skipped_count",String(r)),Ot(n,"legacy_usage_jsonl_duplicate_count",String(s))})()}function Ep(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 Op(n,e="INSERT"){return n.prepare(`
|
|
373
377
|
${e} INTO usage_records (
|
|
374
378
|
id, timestamp, session_id, provider, original_model, model, upstream_model, model_family,
|
|
375
379
|
scenario_type, client_type,
|
|
@@ -383,11 +387,11 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
383
387
|
@cache_read_input_tokens, @cache_creation_input_tokens, @ttft,
|
|
384
388
|
@tokens_per_second, @duration_ms, @status, @error_message, @response_body
|
|
385
389
|
)
|
|
386
|
-
`)}function
|
|
387
|
-
SELECT ${t} AS key, ${
|
|
390
|
+
`)}function Ap(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,vp);return Math.round(n/r)}function io(n){let e=Number(n.outputTokens)||0,t=Number(n.durationMs),o=Number.isFinite(t)?t:null,r=n.stream?It(n.ttft):null;return{...n,ttft:r,tokensPerSecond:Ap(e,o,r)}}function be(n){return typeof n=="string"?n:n==null?"":String(n)}function oo(n){if(n==null)return;let e=String(n);return e.length>0?e:void 0}function At(n){let e=Number(n);return Number.isFinite(e)?e:0}function Wa(n){return n==="success"?"success":"error"}function Ka(n){let e=io(n);return{id:be(e.id),timestamp:be(e.timestamp),session_id:be(e.sessionId),provider:be(e.provider),original_model:be(e.originalModel),model:be(e.model),upstream_model:oo(e.upstreamModel)??null,model_family:be(e.modelFamily),scenario_type:be(e.scenarioType),client_type:oo(e.clientType)??null,stream:e.stream?1:0,input_tokens:Math.trunc(At(e.inputTokens)),output_tokens:Math.trunc(At(e.outputTokens)),cache_read_input_tokens:Math.trunc(At(e.cacheReadInputTokens)),cache_creation_input_tokens:Math.trunc(At(e.cacheCreationInputTokens)),ttft:It(e.ttft),tokens_per_second:It(e.tokensPerSecond),duration_ms:Math.trunc(At(e.durationMs)),status:Wa(e.status),error_message:oo(e.errorMessage)??null,response_body:oo(e.responseBody)??null}}function Ip(n){return io({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:Wa(n.status),errorMessage:n.error_message??void 0,responseBody:n.response_body??void 0})}function Mp(){return new Date(Date.now()-xp*24*60*60*1e3).toISOString()}function Ga(n,e=!1){let t=Date.now();!e&&t-za<Cp||(n.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(Mp()),za=t)}function Va(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 Ze(n,e,t,o=""){let r=o?e.sql?`${e.sql} AND ${o}`:`WHERE ${o}`:e.sql,s=n.prepare(`
|
|
391
|
+
SELECT ${t} AS key, ${Np}
|
|
388
392
|
FROM usage_records ${r}
|
|
389
393
|
GROUP BY key
|
|
390
|
-
`).all(...e.params),
|
|
394
|
+
`).all(...e.params),a={};for(let i of s)i.key!=null&&(a[String(i.key)]={count:i.count,inputTokens:i.inputTokens,outputTokens:i.outputTokens,cacheReadInputTokens:i.cacheReadInputTokens,cacheCreationInputTokens:i.cacheCreationInputTokens});return a}function Qa(n,e){let t=Va(e),o=n.prepare(`
|
|
391
395
|
SELECT
|
|
392
396
|
COUNT(*) AS totalRequests,
|
|
393
397
|
COALESCE(SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END), 0) AS successCount,
|
|
@@ -398,17 +402,17 @@ ${this.toMarkdown(s,t+1)}`:`${o}${r}: ${s}`).join(`
|
|
|
398
402
|
AVG(CASE WHEN status = 'success' AND ttft IS NOT NULL THEN ttft END) AS avgTtft,
|
|
399
403
|
AVG(CASE WHEN status = 'success' AND tokens_per_second IS NOT NULL THEN tokens_per_second END) AS avgTokensPerSecond
|
|
400
404
|
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:
|
|
405
|
+
`).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:Ze(n,t,"model"),byProvider:Ze(n,t,"provider"),byScenario:Ze(n,t,"scenario_type"),byFamily:Ze(n,t,"model_family || '/' || scenario_type","model_family IS NOT NULL AND model_family != ''"),byDay:Ze(n,t,"date(timestamp, 'localtime')"),byClient:Ze(n,t,"CASE WHEN client_type IS NULL OR client_type = '' THEN 'unknown' ELSE client_type END")}}function Nt(n){let e=Mt(),t=io(n);Yr.run(Ka(t)),Ga(e)}function Xr(n){let e=Mt(),t=Va(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),$p):50,s=(o-1)*r,a=e.prepare(`SELECT COUNT(*) AS count FROM usage_records ${t.sql}`).get(...t.params),i=e.prepare(`SELECT * FROM usage_records ${t.sql} ORDER BY timestamp DESC LIMIT ? OFFSET ?`).all(...t.params,r,s),c=Qa(e,n);return{records:i.map(Ip),summary:c,total:a.count,page:o,pageSize:r}}function $t(n,e,t){let o=Mt();return Qa(o,{startTime:n,endTime:e,status:t})}function Zr(n){let e=Mt();if(!n){e.prepare("DELETE FROM usage_records").run();return}e.prepare("DELETE FROM usage_records WHERE timestamp < ?").run(n)}function Lp(){ke&&(ke.pragma("wal_checkpoint(TRUNCATE)"),ke.close(),ke=null,Yr=null)}function It(n){if(n==null)return null;if(typeof n=="number")return n;let e=parseFloat(String(n));return isNaN(e)?null:e}function qr(n){let e=so(hp(),kp,`session-${bp(n)}.json`);try{let t=JSON.parse(Ja(e,"utf-8"));return{ttft:It(t.timeToFirstToken),tokensPerSecond:It(t.tokensPerSecond)}}catch{return{ttft:null,tokensPerSecond:null}}}function Dp(n,e,t){return Mt().prepare("SELECT MIN(timestamp) AS timestamp FROM usage_records WHERE provider = ? AND timestamp >= ? AND timestamp <= ?").get(n,e,t)?.timestamp??void 0}var ro,Tp,Qr,vp,xp,Cp,Ha,ke,Yr,za,Np,$p,Lt=w(()=>{"use strict";ro=so(_p,"data"),Tp=so(ro,"usage.sqlite"),Qr=so(ro,"usage.jsonl"),vp=1,xp=180,Cp=3600*1e3,Ha="legacy_usage_jsonl_migrated_at",ke=null,Yr=null,za=0;Np=`
|
|
402
406
|
COUNT(*) AS count,
|
|
403
407
|
COALESCE(SUM(CASE WHEN status = 'success' THEN input_tokens ELSE 0 END), 0) AS inputTokens,
|
|
404
408
|
COALESCE(SUM(CASE WHEN status = 'success' THEN output_tokens ELSE 0 END), 0) AS outputTokens,
|
|
405
409
|
COALESCE(SUM(CASE WHEN status = 'success' THEN cache_read_input_tokens ELSE 0 END), 0) AS cacheReadInputTokens,
|
|
406
410
|
COALESCE(SUM(CASE WHEN status = 'success' THEN cache_creation_input_tokens ELSE 0 END), 0) AS cacheCreationInputTokens
|
|
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.
|
|
411
|
+
`;$p=200});import{existsSync as Fp}from"fs";import{homedir as Up}from"os";import{join as jp}from"path";import{writeFile as Hp}from"fs/promises";async function Xa(){let n=Up(),e=jp(n,".claude.json");if(!Fp(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 Hp(e,JSON.stringify(o,null,2))}}var Za=w(()=>{"use strict"});import{existsSync as zp,readdirSync as Bp,statSync as Jp,unlinkSync as Wp}from"fs";import{join as qa}from"path";import{HOME_DIR as Kp}from"@wengine-ai/claude-code-router-shared";function tc(n=qa(Kp,"logs")){try{if(!zp(n))return;let e=Date.now()-Vp;for(let t of Bp(n)){if(!/^ccr-.*\.log$/.test(t))continue;let o=qa(n,t),r=Jp(o);r.isFile()&&r.mtime.getTime()<e&&Wp(o)}}catch(e){console.warn("Failed to prune old server log files:",e)}}function nc(){ec||(ec=!0,tc(),setInterval(tc,Qp).unref())}var Gp,Vp,Qp,ec,oc=w(()=>{"use strict";Gp=7,Vp=Gp*24*60*60*1e3,Qp=1440*60*1e3,ec=!1});async function rc(n,e){let t=e.plugins||e.Plugins||[];for(let o of t){let{name:r,enabled:s=!1,options:a={}}=o;switch(r){case"token-speed":bt.registerPlugin(Vn,{enabled:s,outputHandlers:[{type:"temp-file",enabled:!0,config:Gn()}],...a});break;default:console.warn(`Unknown plugin: ${r}`);break}}await bt.enablePlugins(n)}var sc=w(()=>{"use strict";zr()});function qe(n,e,t){if(n?.configService)try{n.configService.reload(),n.reloadProviders?n.reloadProviders(e):(n.configService.set("providers",e?.Providers??e?.providers??[]),n.providerService?.reload()),Pt(e,t),Cr();let o={enabled:n.configService.get("ACTIVE_PROBE_ENABLED")??!0,quotaProbeIntervalMinutes:n.configService.get("QUOTA_PROBE_INTERVAL_MINUTES")??10,probeTimeoutMs:n.configService.get("PROBE_TIMEOUT_MS")??15e3,slowThresholdMs:Number(n.configService.get("PROBE_SLOW_THRESHOLD_MS"))||3e3,initialDelayMs:n.configService.get("PROBE_INITIAL_DELAY_MS")??5e3,excludeProviders:n.configService.get("EXCLUDE_PROBE_PROVIDERS")??[]};n.activeProbeService=_t(()=>n.providerService.getProviders(),o,r=>ue(n.configService,r),t,r=>n.configService.get(r))}catch(o){t.warn(`Config saved but runtime reload failed: ${o?.message||o}`)}}var ao=w(()=>{"use strict";Et();zn();Z()});import{constants as es,lstatSync as et,realpathSync as Dt,openSync as Yp,closeSync as Xp,fstatSync as Zp,readFileSync as ic}from"node:fs";import{dirname as kb,join as tt,sep as qp}from"node:path";import{pathToFileURL as ef}from"node:url";function cc(n){try{let e=JSON.parse(ic(tt(tf(n),"plugins.json"),"utf8"));return Array.isArray(e)?e.filter(t=>t&&typeof t.name=="string"):[]}catch{return[]}}function ns(n,e){if(!ac(e))return null;try{let t=Dt(n),o=tt(t,"plugins"),r=tt(o,e);return et(o).isSymbolicLink()||!et(o).isDirectory()||et(r).isSymbolicLink()||!et(r).isDirectory()||Dt(o)!==o||Dt(r)!==r||!r.startsWith(o+qp)?null:r}catch{return null}}function ts(n,e,t){if(!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(t))throw new Error("invalid plugin file");let o=ns(n,e);if(!o)throw new Error("unsafe plugin directory");let r=tt(o,t),s=et(r);if(s.isSymbolicLink()||!s.isFile()||Dt(r)!==r)throw new Error("unsafe plugin file");let a=Yp(r,es.O_RDONLY|es.O_NOFOLLOW|es.O_NONBLOCK);try{let i=Zp(a),c=et(r);if(!i.isFile()||c.isSymbolicLink()||i.dev!==s.dev||i.ino!==s.ino||i.dev!==c.dev||i.ino!==c.ino||ns(n,e)!==o||Dt(r)!==r)throw new Error("plugin file changed while opening");return ic(a)}finally{Xp(a)}}function nf(n){let e=n?.configService,t=e?bs(e):String(process.env.SUBSCRIPTION_PROXY_URL??"").trim()||void 0;return ks(t)}async function lc(n){let{configDir:e,log:t}=n,o=n.registry??n.getServer()?.pluginRegistry??So(),r={configDir:e,readConfig:()=>Q(),readRawConfig:()=>Rt(),writeConfig:i=>le(i),reloadRuntime:async i=>qe(n.getServer(),i??await Q(),t),getDirectDispatcher:ho,getSubscriptionDispatcher:()=>nf(n.getServer())},s={loaded:[],failed:[],skipped:[]},a=new Set;for(let i of cc(e)){let c;try{if(!ac(i.name))throw new Error("invalid plugin install name");if(a.has(i.name))throw new Error(`duplicate plugin install record "${i.name}"`);if(a.add(i.name),i.enabled===!1){s.skipped.push({name:i.name,reason:"disabled"});continue}let l=ns(e,i.name);if(!l){s.skipped.push({name:i.name,reason:"directory missing or unsafe"});continue}let u;try{let f=JSON.parse(ts(e,i.name,"plugin.json").toString("utf8"));if(!f||typeof f.name!="string"||typeof f.version!="string")throw new Error;u={name:f.name,version:f.version,coreApi:String(f.coreApi??"1"),description:f.description}}catch{throw new Error("plugin.json missing or malformed")}if(u.name!==i.name)throw new Error("manifest name does not match install directory");if(u.coreApi.split(".")[0]!=="1".split(".")[0])throw new Error(`coreApi ${u.coreApi} incompatible with core ${"1"}`);let p=o.beginActivation(u,{info:f=>t.info(`[plugin:${u.name}] ${f}`),warn:f=>t.warn(`[plugin:${u.name}] ${f}`),error:f=>t.error(`[plugin:${u.name}] ${f}`)},r,l);c=p,ts(e,i.name,"plugin.js");let d=await import(ef(tt(l,"plugin.js")).href),m=d?.activate??d?.default?.activate;if(typeof m!="function")throw new Error("entry exports no activate()");await m(p.context);let h=p.cardPath?ts(e,i.name,p.cardPath):void 0;await p.commit(h,n.getServer()?.app),s.loaded.push(u.name),t.info(`plugin loaded: ${u.name}@${u.version}`)}catch(l){c?.abort(),s.failed.push({name:i.name,error:l?.message??String(l)}),t.warn(`plugin ${i.name} failed to load: ${l?.message??String(l)}`)}}return s}function uc(n,e=So()){let t=new Map(e.listLoadedPlugins().map(o=>[o.name,o]));return cc(n).map(o=>{let r=o.enabled!==!1?t.get(o.name):void 0;return r?{...r,enabled:!0}:{name:o.name,version:null,enabled:o.enabled!==!1,error:"plugin missing or failed to load \u2014 its providers are unavailable until it is reinstalled"}})}var ac,tf,os=w(()=>{"use strict";xo();Wt();ao();wt();Z();ac=n=>/^[a-z0-9][a-z0-9-]*$/i.test(n),tf=n=>tt(n,"plugins")});import{existsSync as of,readFileSync as rf}from"node:fs";import{join as dc}from"node:path";import{NPM_SPEC_PATTERN as sf,PluginInstallError as af,installPluginFromNpm as cf,readPluginRecords as lf}from"@wengine-ai/claude-code-router-shared";async function df(n,e,t){let o=`${n}/-/v1/search?text=${encodeURIComponent(`keywords:${e}`)}&size=100`,r=await fetch(o,{signal:AbortSignal.timeout(t),headers:{accept:"application/json"}});if(!r.ok)throw new Error(`registry_status_${r.status}`);let s=await r.json();return Array.isArray(s?.objects)?s.objects:[]}async function pf(n,e={}){let t=(e.registryBase??uf).replace(/\/$/,""),o=e.timeoutMs??3e4,r=await Promise.allSettled(pc.map(d=>df(t,d,o))),s=new Map,a=!1;for(let d of r)if(d.status==="fulfilled"){a=!0;for(let m of d.value){let h=m?.package,f=typeof h?.name=="string"?h.name:"";if(!f||!sf.test(f)||!(Array.isArray(h?.keywords)?h.keywords.filter(b=>typeof b=="string"):[]).some(b=>pc.includes(b)))continue;let _=typeof h?.version=="string"?h.version:"";s.has(f)||s.set(f,{name:f,version:_,description:typeof h?.description=="string"?h.description:""})}}if(!a)return{ok:!0,plugins:[],warning:"registry_unavailable"};let i=lf(n),c=new Map(i.map(d=>[d.name,d])),l=new Map(i.filter(d=>typeof d.source=="string"&&d.source!=="").map(d=>[d.source,d])),u=d=>{try{let m=JSON.parse(rf(dc(n,"plugins",d,"plugin.json"),"utf8"));return typeof m?.version=="string"?m.version:null}catch{return null}};return{ok:!0,plugins:[...s.values()].sort((d,m)=>d.name.localeCompare(m.name)).map(d=>{let m=c.get(d.name)??l.get(d.name),h=m!==void 0||of(dc(n,"plugins",d.name));return{...d,installed:h,installedVersion:h?u(m?m.name:d.name):null}})}}function ff(n){switch(n){case"invalid_spec":return 400;case"already_installed":case"plugins_json_corrupted":return 409;case"npm_pack_failed":case"npm_pack_timeout":return 502;case"archive_too_large":case"archive_too_many_files":case"unsafe_archive_member":case"extract_failed":case"invalid_manifest":return 422;default:return 500}}function fc(n,e,t){let o=n.ip||n.connection?.remoteAddress||"";if(!(o==="127.0.0.1"||o==="::1"||o==="::ffff:127.0.0.1"))return!1;let s=e.server?.address?.(),a=typeof s?.port=="number"&&s.port>0?s.port:t,i=new Set([`127.0.0.1:${a}`,`localhost:${a}`,`[::1]:${a}`]),c=n.headers?.host;if(typeof c=="string"&&!i.has(c))return!1;let l=n.headers?.origin;if(l!==void 0&&l!==""){if(typeof l!="string")return!1;let u="";try{let p=new URL(l);if(!["http:","https:"].includes(p.protocol))return!1;u=p.host}catch{return!1}if(!i.has(u))return!1}return!0}function mc(n,e){let t=e.configuredPort??3456,o=(r,s,a)=>r.status(s).send({ok:!1,error:a});n.get("/api/plugins/catalog",async(r,s)=>{if(!fc(r,n,t))return o(s,403,"local_only");try{return await pf(e.configDir,{registryBase:e.catalogRegistryBase??e.registry,timeoutMs:e.catalogTimeoutMs})}catch(a){return n.log?.warn?.(`plugin catalog failed: ${a?.code??a?.name??"error"}`),{ok:!0,plugins:[],warning:"registry_unavailable"}}}),n.post("/api/plugins/install",async(r,s)=>{if(!fc(r,n,t))return o(s,403,"local_only");let a=r.body??{},i=a.name??a.spec,c=a.force===!0;try{let l=await cf({spec:i,configDir:e.configDir,force:c,registry:e.registry,timeoutMs:e.npmTimeoutMs});return n.log?.info?.(`provider plugin installed via API: ${l.manifest.name}@${l.manifest.version} (restart to activate)`),{ok:!0,name:l.manifest.name,version:l.manifest.version,restartRequired:!0}}catch(l){if(l instanceof af){let u=ff(l.code);return u===502?n.log?.warn?.(`plugin install failed: ${l.code}`):n.log?.warn?.(`plugin install rejected: ${l.code}: ${l.message}`),o(s,u,l.code)}return n.log?.error?.(`plugin install crashed: ${l?.message??l}`),o(s,500,"install_failed")}})}var pc,uf,gc=w(()=>{"use strict";pc=["ccr-provider-plugin","claude-code-router-plugin"],uf="https://registry.npmjs.org"});import{ensureCcrSelfProvider as mf}from"@wengine-ai/claude-code-router-shared";import{join as ie,isAbsolute as gf,normalize as hf,dirname as yf}from"path";import{fileURLToPath as _f}from"url";import bf from"@fastify/static";import{readdirSync as hc,statSync as kf,readFileSync as yc,writeFileSync as Tf,existsSync as pe,unlinkSync as _c,rmSync as vf}from"fs";import{homedir as rs}from"os";import{getPresetDir as co,readManifestFromDir as ss,manifestToPresetFile as bc,saveManifest as kc,isPresetInstalled as Tc,extractPreset as xf,HOME_DIR as is,loadConfigFromManifest as Cf,downloadPresetToTemp as Sf,findMarketPresetByName as Rf,getMarketPresets as wf,applyClientSelection as Pf,disableClient as Ef,enableClient as Of,isClientId as Af,listClientStatuses as vc,restoreClient as If,listProjectConfigs as Mf,readProjectConfigById as as,readProjectConfig as Nf,writeProjectConfig as xc,deleteProjectConfig as $f,getClaudeProjectId as Lf,getProjectConfigPath as Df,refreshCcrProjectTakeover as Ff,refreshPiProjectTakeover as Uf,refreshProjectTakeovers as jf,syncGlobalProjectTakeovers as cs,getProjectTakeoverClients as ls,setProjectTakeover as us,isProjectTakeoverClient as Hf}from"@wengine-ai/claude-code-router-shared";import zf from"@fastify/multipart";import Bf from"adm-zip";function Cc(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 Jf(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=$t(t,e),s=$t(o,e),a=[],i=new Set;for(let c of n||[])c&&c.name&&i.add(c.name);for(let c of Object.keys(r.byProvider||{}))i.add(c);for(let c of Object.keys(s.byProvider||{}))i.add(c);for(let c of i){let l=r.byProvider[c]||{inputTokens:0,outputTokens:0},u=s.byProvider[c]||{inputTokens:0,outputTokens:0},p=(l.inputTokens||0)+(l.outputTokens||0),d=(u.inputTokens||0)+(u.outputTokens||0),m,h,f=(n||[]).find(g=>g&&g.name===c);f&&f.quota&&(m=f.quota.limit5h,h=f.quota.limit7d),a.push({provider:c,used5h:p,used7d:d,limit5h:m,limit7d:h})}return a}async function Sc(n,e){let t=n.app;at(),lt(),t.register(zf,{limits:{fileSize:50*1024*1024}}),t.get("/v1/models",async()=>{let i=to(e);return{object:"list",data:i,first_id:i[0]?.id||null,last_id:i[i.length-1]?.id||null,has_more:!1}}),t.get("/v1/models/:modelId",async(i,c)=>{let l=decodeURIComponent(i.params.modelId),u=to(e).find(p=>p.id===l);return u||(c.status(404),{type:"error",error:{type:"not_found_error",message:`Model not found: ${l}`}})}),t.post("/v1/messages/count_tokens",async(i,c)=>{let{messages:l,tools:u,system:p,model:d}=i.body,m=t._server.tokenizerService;if(d&&d.includes(",")&&m)try{let[f,g]=d.split(",");i.log?.info(`Looking up tokenizer for provider: ${f}, model: ${g}`);let _=m.getTokenizerConfigForModel(f,g);_?i.log?.info(`Using tokenizer config: ${JSON.stringify(_)}`):i.log?.warn(`No tokenizer config found for ${f},${g}, using default tiktoken`);let b=await m.countTokens({messages:l,system:p,tools:u},_,f);return{input_tokens:b.tokenCount,tokenizer:b.tokenizerUsed}}catch(f){i.log?.error(`Error using configured tokenizer: ${f.message}`),i.log?.error(f.stack)}else d?d.includes(",")?m||i.log?.warn("TokenizerService not available, using default tiktoken"):i.log?.info(`Model "${d}" does not contain comma, using default tiktoken`):i.log?.info("No model specified, using default tiktoken");return{input_tokens:on(l,p,u)}}),t.get("/api/config",async(i,c)=>await Rt()),t.get("/api/transformers",async(i,c)=>{let l=t._server.transformerService.getAllTransformers();return{transformers:Array.from(l.entries()).map(([p,d])=>({name:p,endpoint:d.endPoint||null}))}}),t.post("/api/config",async(i,c)=>{let l=i.body,u=_s(l||{});if(u.length>0)return c.status(400),{success:!1,error:"Invalid proxy URL",message:u.map(f=>`${f.key}: ${f.error}`).join("; "),proxyErrors:u};let p=await Xn();p&&console.log(`Backed up existing configuration file to ${p}`);let d=null;try{d=await Q()}catch{}let m=mf(l,d);n.pluginRegistry.applyConfigProtectors(l,d),await le(l);let h;try{h=await cs(l),h.failed.length>0&&t.log?.warn?.({projectTakeoverSync:h},"Config saved but some global project takeovers failed to sync")}catch(f){t.log?.warn?.(`Config saved but global project takeover sync failed: ${f?.message||f}`)}return qe(n,l,t.log),{success:!0,message:"Config saved successfully",projectTakeoverSync:h}}),t.get("/api/clients",async(i,c)=>{try{let l=await Q();return{clients:vc(l)}}catch(l){console.error("Failed to get client integrations:",l),c.status(500).send({error:l.message||"Failed to get client integrations"})}}),t.post("/api/clients/apply",async(i,c)=>{try{let l=i.body,u=Array.isArray(l?.enabled)?l.enabled:[],p=await Q(),d=Pf(p,u);await le(d.config);let m=await cs(d.config),h=Cc(t,m,"clients/apply");return{...d,success:d.success&&h,projectTakeoverSync:m}}catch(l){console.error("Failed to apply client integrations:",l),c.status(500).send({error:l.message||"Failed to apply client integrations"})}});async function o(i,c,l){try{let{id:u}=i.params;if(!Af(u)){c.status(404).send({error:`Unknown client: ${u}`});return}let p=await Q(),d=l==="enable"?Of(p,u):l==="restore"?If(p,u):Ef(p,u);await le(p);let m=await cs(p),h=Cc(t,m,`clients/${u}/${l}`);return{success:d.success&&h,result:d,results:[d],clients:vc(p),config:p,projectTakeoverSync:m}}catch(u){console.error(`Failed to ${l} client integration:`,u),c.status(500).send({error:u.message||`Failed to ${l} client integration`})}}t.post("/api/clients/:id/enable",async(i,c)=>o(i,c,"enable")),t.post("/api/clients/:id/disable",async(i,c)=>o(i,c,"disable")),t.post("/api/clients/:id/restore",async(i,c)=>o(i,c,"restore")),t.get("/api/projects",async(i,c)=>{try{let l=await Mf();return{projects:await Promise.all(l.map(async p=>{let d=await ls(p.path);return{...p,ccrTakeoverClients:d,ccrTakeover:d.length>0}}))}}catch(l){console.error("Failed to list project configs:",l),c.status(500).send({error:l.message||"Failed to list project configs"})}}),t.post("/api/projects",async(i,c)=>{try{let{path:l}=i.body||{};if(!l||typeof l!="string"||!gf(l)){c.status(400).send({error:"A valid absolute project path is required"});return}let u=hf(l).replace(/[\\/]+$/,"")||l;if(await Nf(u)){c.status(409).send({error:"Project is already configured"});return}await xc(u,{Router:{}});try{let m=await Q();await us(u,["claudeCode"],m)}catch(m){console.error("Failed to auto-enable ccr takeover for new project:",m)}let d=await ls(u);return{id:Lf(u),path:u,configPath:Df(u),Router:{},ccrTakeoverClients:d,ccrTakeover:d.length>0}}catch(l){console.error("Failed to add project config:",l),c.status(500).send({error:l.message||"Failed to add project config"})}}),t.put("/api/projects/:id",async(i,c)=>{try{let{id:l}=i.params,{Router:u}=i.body||{};if(!u||typeof u!="object"){c.status(400).send({error:"Router must be an object"});return}let p=await as(l);if(!p){c.status(404).send({error:"Project config not found"});return}await xc(p.path,{Router:u});let d=await Q();Object.keys(u).length===0?await jf(p.path,d):(await Ff(p.path,d,u),await Uf(p.path,d,u));let m=await ls(p.path);return{id:l,path:p.path,configPath:p.configPath,Router:u,ccrTakeoverClients:m,ccrTakeover:m.length>0}}catch(l){console.error("Failed to update project config:",l),c.status(500).send({error:l.message||"Failed to update project config"})}}),t.put("/api/projects/:id/takeover",async(i,c)=>{try{let{id:l}=i.params,u=i.body||{},p;if(Array.isArray(u.clients))p=u.clients.filter(Hf);else if(typeof u.enabled=="boolean")p=u.enabled?["claudeCode"]:[];else{c.status(400).send({error:"clients (array) or enabled (boolean) is required"});return}let d=await as(l);if(!d){c.status(404).send({error:"Project config not found"});return}let m=await Q(),h=await us(d.path,p,m);return{id:l,path:d.path,ccrTakeoverClients:h,ccrTakeover:h.length>0}}catch(l){console.error("Failed to update ccr takeover status:",l),c.status(500).send({error:l.message||"Failed to update ccr takeover status"})}}),t.delete("/api/projects/:id",async(i,c)=>{try{let{id:l}=i.params,u=await as(l);if(!u){c.status(404).send({error:"Project config not found"});return}try{let p=await Q();await us(u.path,[],p)}catch(p){console.error("Failed to remove ccr takeover while deleting project:",p)}return await $f(u.path),{success:!0}}catch(l){console.error("Failed to delete project config:",l),c.status(500).send({error:l.message||"Failed to delete project config"})}});let r=typeof __dirname=="string"?__dirname:yf(_f(import.meta.url)),s=pe(ie(r,"index.html"))?r:pe(ie(r,"..","index.html"))?ie(r,".."):ie(r,"..","dist");t.register(bf,{root:s,prefix:"/ui/",maxAge:"1h"}),t.get("/ui",async(i,c)=>c.redirect("/ui/")),t.get("/api/logs/files",async(i,c)=>{try{let l=ie(rs(),".claude-code-router","logs"),u=[];if(pe(l)){let p=hc(l);for(let d of p)if(d.endsWith(".log")){let m=ie(l,d),h=kf(m);u.push({name:d,path:m,size:h.size,lastModified:h.mtime.toISOString()})}u.sort((d,m)=>new Date(m.lastModified).getTime()-new Date(d.lastModified).getTime())}return u}catch(l){console.error("Failed to get log files:",l),c.status(500).send({error:"Failed to get log files"})}}),t.get("/api/logs",async(i,c)=>{try{let l=i.query.file,u;return l?u=l:u=ie(rs(),".claude-code-router","logs","app.log"),pe(u)?yc(u,"utf8").split(`
|
|
412
|
+
`).filter(m=>m.trim()):[]}catch(l){console.error("Failed to get logs:",l),c.status(500).send({error:"Failed to get logs"})}}),t.delete("/api/logs",async(i,c)=>{try{let l=i.query.file,u;return l?u=l:u=ie(rs(),".claude-code-router","logs","app.log"),pe(u)&&Tf(u,"","utf8"),{success:!0,message:"Logs cleared successfully"}}catch(l){console.error("Failed to clear logs:",l),c.status(500).send({error:"Failed to clear logs"})}}),t.get("/api/debug-log",async()=>({enabled:Ht()})),t.put("/api/debug-log",async i=>{let{enabled:c}=i.body;return ko(c),{enabled:Ht()}}),t.get("/api/usage",async(i,c)=>{try{let l=i.query;return Xr({startTime:l.startDate,endTime:l.endDate,model:l.model,provider:l.provider,scenario:l.scenario,clientType:l.clientType,sessionId:l.sessionId,status:l.status,page:l.page?parseInt(l.page,10):void 0,pageSize:l.pageSize?parseInt(l.pageSize,10):void 0})}catch(l){console.error("Failed to query usage:",l),c.status(500).send({error:"Failed to query usage"})}}),t.get("/api/usage/summary",async(i,c)=>{try{let l=i.query;return $t(l.startDate,l.endDate,l.status)}catch(l){console.error("Failed to query usage summary:",l),c.status(500).send({error:"Failed to query usage summary"})}}),t.delete("/api/usage",async(i,c)=>{try{let l=i.query;return Zr(l.beforeDate),{success:!0,message:l.beforeDate?"Usage data cleared before "+l.beforeDate:"All usage data cleared"}}catch(l){console.error("Failed to clear usage:",l),c.status(500).send({error:"Failed to clear usage data"})}}),t.get("/api/providers/health",async(i,c)=>{try{let u=U().getAllStates(),d=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:d,timestamp:new Date().toISOString()}}catch(l){console.error("Failed to get provider health:",l),c.status(500).send({error:"Failed to get provider health"})}}),t.post("/api/providers/probe",async(i,c)=>{try{let{providerName:l}=i.body;if(!l)return c.status(400),{error:"providerName is required"};let p=t._server?.activeProbeService;if(!p?.probeProviderManually)return c.status(503),{error:"Active probe service is not available"};let d=await p.probeProviderManually(l);return d.success&&eo(l,i.log),{provider:l,success:d.success,latencyMs:d.latencyMs,status:d.status,isSlow:d.isSlow,errorKind:d.errorKind??null,error:d.error,timestamp:new Date().toISOString()}}catch(l){console.error("Failed to probe provider:",l),c.status(500).send({error:l.message||"Failed to probe provider"})}}),t.post("/api/providers/probe-all",async(i,c)=>{try{let l=t._server,u=l?.activeProbeService;if(!u?.probeProviderManually)return c.status(503),{error:"Active probe service is not available"};let p=(l?.providerService?.getProviders?.()||[]).filter(m=>m?.name&&m.enabled!==!1),d=await Promise.all(p.map(async m=>{let h=await u.probeProviderManually(m.name);return h.success&&eo(m.name,i?.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:d,successCount:d.filter(m=>m.success).length,total:d.length,timestamp:new Date().toISOString()}}catch(l){console.error("Failed to probe all providers:",l),c.status(500).send({error:l.message||"Failed to probe all providers"})}}),t.get("/api/providers/quota",async(i,c)=>{try{let u=(await Q()).Providers||[],p=Jf(u),d=new Map(p.map(f=>[f.provider,f])),m=$o();for(let f of p){let g=m.find(_=>_.provider===f.provider);g&&g.limit!=null&&(f.limit5h=g.limit,f.used5h=g.limit-(g.remaining??0),g.reset&&(f.reset5h=new Date(g.reset*1e3).toISOString()))}let h=Go();for(let f of h){let g=d.get(f.provider);g||(g={provider:f.provider,used5h:0,used7d:0},p.push(g),d.set(f.provider,g)),typeof f.totalBalance=="number"?(g.limit7d=f.totalBalance,g.type7d=f.currency?"balance":"rateLimit",f.currency&&(g.currency=f.currency),typeof f.usedBalance=="number"?g.used7d=f.usedBalance:typeof f.remainingBalance=="number"?g.used7d=Math.max(0,f.totalBalance-f.remainingBalance):(g.used7d=0,g.limit7d=f.totalBalance)):typeof f.usedBalance=="number"&&(g.used7d=f.usedBalance),typeof f.usedDailyBalance=="number"&&(g.used5h=f.usedDailyBalance,g.type5h="rateLimit"),typeof f.limitDaily=="number"&&(g.limit5h=f.limitDaily,g.type5h="rateLimit"),typeof f.resetTime=="string"&&(f.limitDaily!==void 0?g.reset5h=f.resetTime:g.reset7d=f.resetTime),typeof f.resetTime7d=="string"&&(g.reset7d=f.resetTime7d)}return{quotas:p,timestamp:new Date().toISOString()}}catch(l){console.error("Failed to compute provider quota:",l),c.status(500).send({error:"Failed to compute provider quota"})}}),t.get("/api/presets",async(i,c)=>{try{let l=ie(is,"presets");if(!pe(l))return{presets:[]};let p=hc(l,{withFileTypes:!0}).filter(m=>m.isDirectory()&&!m.name.startsWith(".")).map(m=>m.name),d=[];for(let m of p){let h=ie(l,m);try{let f=ie(h,"manifest.json"),g=yc(f,"utf-8"),_=JSON.parse(g),{Providers:b,Router:y,PORT:k,HOST:T,API_TIMEOUT_MS:S,PROXY_URL:v,PROXY_GLOBAL_ENABLED:R,LOG:C,LOG_LEVEL:x,StatusLine:E,NON_INTERACTIVE_MODE:O,...P}=_;d.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(f){console.error(`Failed to read preset ${m}:`,f)}}return{presets:d}}catch(l){console.error("Failed to get presets:",l),c.status(500).send({error:"Failed to get presets"})}}),t.get("/api/presets/:name",async(i,c)=>{try{let{name:l}=i.params,u=co(l);if(!pe(u)){c.status(404).send({error:"Preset not found"});return}let p=await ss(u);return{...bc(p),config:Cf(p,u),userValues:p.userValues||{}}}catch(l){console.error("Failed to get preset:",l),c.status(500).send({error:l.message||"Failed to get preset"})}}),t.post("/api/presets/:name/apply",async(i,c)=>{try{let{name:l}=i.params,{secrets:u}=i.body,p=co(l);if(!pe(p)){c.status(404).send({error:"Preset not found"});return}let m={...await ss(p)};return u&&Object.keys(u).length>0&&(m.userValues={...m.userValues,...u}),await kc(l,m),{success:!0,message:"Preset applied successfully"}}catch(l){console.error("Failed to apply preset:",l),c.status(500).send({error:l.message||"Failed to apply preset"})}}),t.delete("/api/presets/:name",async(i,c)=>{try{let{name:l}=i.params,u=co(l);if(!pe(u)){c.status(404).send({error:"Preset not found"});return}return vf(u,{recursive:!0,force:!0}),{success:!0,message:"Preset deleted successfully"}}catch(l){console.error("Failed to delete preset:",l),c.status(500).send({error:l.message||"Failed to delete preset"})}}),t.get("/api/presets/market",async(i,c)=>{try{return{presets:await wf()}}catch(l){console.error("Failed to get market presets:",l),c.status(500).send({error:l.message||"Failed to get market presets"})}}),t.post("/api/presets/install/github",async(i,c)=>{try{let{presetName:l}=i.body;if(!l){c.status(400).send({error:"Preset name is required"});return}let u=await Rf(l);if(!u){c.status(400).send({error:"Preset not found in marketplace",message:`Preset '${l}' is not available in the official marketplace. Please check the available presets.`});return}if(!u.repo){c.status(400).send({error:"Invalid preset data",message:`Preset '${l}' does not have repository information`});return}let p=u.repo.match(/(?:github\.com[:/]|^)([^/]+)\/([^/\s#]+?)(?:\.git)?$/);if(!p){c.status(400).send({error:"Invalid GitHub repository URL"});return}let[,d,m]=p,h=u.name||l;if(await Tc(h)){c.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 f=`https://github.com/${d}/${m}/archive/refs/heads/main.zip`,g=await Sf(f),_=await a(g);if(await Tc(h)){_c(g),c.status(409).send({error:"Preset already installed",message:`Preset '${h}' was installed while downloading. Please try again.`,presetName:h});return}let b=co(h);await xf(g,b);let y=await ss(b);return y.repository=u.repo,u.url&&(y.source=u.url),await kc(h,y),_c(g),{success:!0,presetName:h,preset:{..._.metadata,installed:!0}}}catch(l){console.error("Failed to install preset from GitHub:",l),c.status(500).send({error:l.message||"Failed to install preset from GitHub"})}});async function a(i){let c=new Bf(i),l=c.getEntry("manifest.json");if(l||(l=c.getEntries().find(d=>d.entryName.includes("manifest.json"))||null),!l)throw new Error("Invalid preset file: manifest.json not found");let u=JSON.parse(l.getData().toString("utf-8"));return bc(u)}return t.get("/api/plugins",async()=>({plugins:uc(is,n.pluginRegistry)})),mc(t,{configDir:is,configuredPort:Number(e.PORT)||3456,registry:typeof e.CCR_PLUGIN_NPM_REGISTRY=="string"&&/^https?:\/\//.test(e.CCR_PLUGIN_NPM_REGISTRY)?e.CCR_PLUGIN_NPM_REGISTRY:void 0}),t.get("/plugins/:name/:file",async(i,c)=>{let{name:l,file:u}=i.params,p=n.pluginRegistry.getPublicCard(l,u);return p?(c.header("Content-Type","application/javascript; charset=utf-8"),c.header("X-Content-Type-Options","nosniff"),c.header("Cache-Control","no-store"),c.send(p)):c.status(404).send({error:"plugin_not_found"})}),await n.pluginRegistry.registerAllRoutes(n.app??n),n}var Rc=w(()=>{"use strict";Se();Me();Le();Z();rt();re();Et();ao();os();gc();wt();Lt();Gr()});function Wf(n,e){let t=n?.Router,{family:o}=ft(e||"");if(t?.enableFamilyRouting&&o){let r=t.families?.[o]?.image;if(r)return r}return t?.image}function wc(n){return n?.Router?.image}function Kf(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 Gf(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 ds,lo,ps,Pc,Ec=w(()=>{"use strict";Se();ds=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}},lo=new ds;ps=class{name="image";tools;constructor(){this.tools=new Map,this.appendTools()}shouldHandle(e,t){let o=Wf(t,e.originalModel||e.body.model);if(!o||e.body.model===o||!Kf(e.body.messages))return!1;let s=t.forceUseImageAgent===!0;return!s&&mt(e.body.model)?!1:(Gf(e.body.messages),s&&!!wc(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(i=>{let c=lo.getImage(`${t.req.id}_Image#${i}`);c&&o.push({type:"image",source:c})});else{let i=lo.getImage(`${t.req.id}_Image#${e.imageId}`);i&&o.push({type:"image",source:i})}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 i=s.content.filter(c=>c.type==="text"&&!c.text.includes("This is an image, if you need to view or analyze it, you need to extract the imageId"));o.push(...i)}Object.keys(e).length>0&&o.push({type:"text",text:JSON.stringify(e)});let a=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:wc(t.config),system:[{type:"text",text:`You must interpret and analyze images strictly according to the assigned task.
|
|
409
413
|
When an image placeholder is provided, your role is to parse the image content only within the scope of the user\u2019s instructions.
|
|
410
414
|
Do not ignore or deviate from the task.
|
|
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(
|
|
415
|
+
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(i=>i.json()).catch(i=>null);return!a||!a.content?"analyzeImage Error":a.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.
|
|
412
416
|
If the user requests you to view, analyze, or extract information from an image, you **must** call the \`analyzeImage\` tool.
|
|
413
417
|
|
|
414
418
|
When invoking this tool, you must pass the correct \`imageId\` extracted from the prior conversation.
|
|
@@ -418,5 +422,5 @@ If multiple images exist, select the **most relevant imageId** based on the user
|
|
|
418
422
|
|
|
419
423
|
Do not attempt to describe or analyze the image directly yourself.
|
|
420
424
|
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")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};
|
|
425
|
+
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(a=>a.type==="image"||Array.isArray(a.content)&&a.content.some(i=>i.type==="image"))),r=1;o.forEach(s=>{Array.isArray(s.content)&&s.content.forEach(a=>{if(a.type==="image")lo.storeImage(`${e.id}_Image#${r}`,a.source),a.type="text",delete a.source,a.text=`[Image #${r}]This is an image, if you need to view or analyze it, you need to extract the imageId`,r++;else if(a.type==="text"&&a.text.includes("[Image #"))a.text=a.text.replace(/\[Image #\d+\]/g,"");else if(a.type==="tool_result"&&Array.isArray(a.content)&&a.content.some(i=>i.type==="image")){let i=[],c=a.content.filter(u=>u.type!=="image"?!0:(lo.storeImage(`${e.id}_Image#${r}`,u.source),i.push(`[Image #${r}]`),r++,!1)),l=`${i.join("")}This is an image, if you need to view or analyze it, you need to extract the imageId`;a.content=c.length>0?[{type:"text",text:l},...c]:l}})})}},Pc=new ps});var fs,Oc,uo,Ac=w(()=>{"use strict";Ec();fs=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}},Oc=new fs;Oc.registerAgent(Pc);uo=Oc});import{EventEmitter as Vf}from"node:events";import{performance as Ic}from"node:perf_hooks";import Qf from"json5";function Yf(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 Nc(n,e){(!n.clientContext||!n.usageCacheKey||!n.usageSessionId)&&xe(n,e)}function ms(n){return n.usageSessionId||n.id}function po(n){return n.usageCacheKey||`${n.clientType||"unknown"}:request:${ms(n)}`}function nt(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 Xf(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?Xe(e.usage):e.response?.usage?Xe(e.response.usage):void 0,o=e.model||e.response?.model;if(!(!t&&!o))return{usage:t,model:o}}async function Zf(n,e,t){if(await new Promise((r,s)=>{let a=i=>i?s(i):r();no(t)(n,e,a).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")){Nc(n,t);let r=po(n);n.clientContext?.usageScope==="session"&&(n.previousUsage=q.get(r))}}async function qf(n,e,t){if(!n.pathname?.endsWith("/v1/messages"))return;let o=[];for(let r of uo.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 $c(n){return{authClient:(e,t)=>Zf(e,t,n),agent:(e,t)=>qf(e,t,n)}}function Lc(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=Ic.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,a)=>{let i=c=>c?a(c):s();no(e)(t,o,i).then(()=>s(),a)})}),n.addHook("onError",async(t,o,r)=>{t.errorMessage=r?.message||r?.toString?.()||"Unknown error",Mc.emit("onError",t,o,r)}),n.addHook("onSend",(t,o,r,s)=>{(t.ccrHookOrder||=[]).push("onSend");let a=t.pathname?.endsWith("/v1/messages"),i=t.pathname?.endsWith("/v1/responses");if(a||i){Nc(t,e);let c=ms(t),l=po(t);if(r instanceof ReadableStream){if(a&&t.agents){let h=new AbortController,f=r.pipeThrough(new ce),g,_=-1,b="",y="",k="",T=[],S=[];return s(null,Bn(f,async(v,R)=>{try{if(v.event==="content_block_start"&&v?.data?.content_block?.name){let C=t.agents.find(x=>uo.getAgent(x)?.tools.get(v.data.content_block.name));if(C){g=uo.getAgent(C),_=v.data.index,b=v.data.content_block.name,k=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 C=Qf.parse(y);S.push({type:"tool_use",id:k,name:b,input:C});let x=await g?.tools.get(b)?.handler(C,{req:t,config:e});T.push({tool_use_id:k,type:"tool_result",content:x}),g=void 0,_=-1,b="",y="",k=""}catch(C){console.log(C)}return}if(v.event==="message_delta"&&T.length){t.body.messages.push({role:"assistant",content:S}),t.body.messages.push({role:"user",content:T});let C=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(!C.ok)return;let E=C.body.pipeThrough(new ce).getReader();for(;;)try{let{value:O,done:P}=await E.read();if(P)break;let M=O;if(["message_start","message_stop"].includes(M.event))continue;if(!R.desiredSize)break;R.enqueue(M)}catch(O){if(O.name==="AbortError"||O.code==="ERR_STREAM_PREMATURE_CLOSE"){h.abort();break}throw O}return}return v}catch(C){if(console.error("Unexpected error in stream processing:",C),C.code==="ERR_STREAM_PREMATURE_CLOSE"){h.abort();return}throw C}}).pipeThrough(new Qe))}let[p,d]=r.tee(),m=async h=>{let g=h.pipeThrough(new TextDecoderStream).pipeThrough(new ce).getReader();try{for(;;){let{done:_,value:b}=await g.read();if(_)break;let y=b.event,k=b.data;if(y==="message_start"&&k?.message?.model&&(t.upstreamModel=k.message.model),y==="response.completed"&&k?.response?.model&&(t.upstreamModel=k.response.model),k?.usage){let T=q.get(l)||{},S=Xe(k.usage),v=Zn(T,S,y==="message_start");t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_anthropic_sse",event:y,provider:t.provider,model:nt(t),usageSessionId:c,capturedUsage:S,mergedUsage:v}),(S?.cache_read_input_tokens||S?.cache_creation_input_tokens)&&console.log("[Usage] Cache tokens:",{cache_read:S?.cache_read_input_tokens,cache_creation:S?.cache_creation_input_tokens}),q.put(l,v)}if(k?.response?.usage){let T=Xe(k.response.usage),S=q.get(l)||{},v=Zn(S,T,!1);t.log?.info?.({debug_log:!0,reqId:t.id,phase:"usage_capture_responses_sse",event:y,provider:t.provider,model:nt(t),usageSessionId:c,capturedUsage:T,mergedUsage:v}),q.put(l,v)}}}catch(_){_.name==="AbortError"||_.code==="ERR_STREAM_PREMATURE_CLOSE"?console.error("Background read stream closed prematurely"):console.error("Error in background stream reading:",_)}finally{g.releaseLock()}};return t.usageCapturePromise=m(d),s(null,p)}let u=Xf(r);if(u?.usage&&q.put(l,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(Mc.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=>{Ho(t)}),n.addHook("onResponse",async(t,o)=>{if((t.ccrHookOrder||=[]).push("onResponse"),Ho(t),!(!t.pathname?.endsWith("/v1/messages")&&!t.pathname?.endsWith("/v1/responses")))try{t.usageCapturePromise&&await t.usageCapturePromise,t.tokenSpeedCapturePromise&&await t.tokenSpeedCapturePromise;let r=ms(t),s=r,a=t.sessionId||r,i=o.statusCode>=400,c=i?void 0:q.get(po(t)),l=qr(s),u=U(),p,d,m=t.sseError,h=m&&(m.type||m.code||m.message),f=c?.output_tokens&&c.output_tokens>0,g=i||h&&!f;if(g){p=t.errorMessage||o.errorMessage||t.error?.message||t.error?.toString?.()||void 0,h&&(p=p||JSON.stringify(m),d=JSON.stringify(m)),d=d||t.errorResponseBody;let T=nt(t);Yf(o.statusCode,p)?u.markRateLimited(t.provider||"",T,120,p):u.recordFailure(t.provider||"",T,p)}else{let T=nt(t);u.recordSuccess(t.provider||"",T)}let _=c?.cache_read_input_tokens??0,y=c?.input_tokens||0||t.tokenCount||0;!_&&t.tokenCount&&c?.input_tokens&&c.input_tokens<t.tokenCount&&(_=t.tokenCount-c.input_tokens,_&&console.log("[Usage] Inferred cache tokens:",{tokenCount:t.tokenCount,netInput:c.input_tokens,impliedCache:_}));let k=g?0:c?.output_tokens||0;!g&&t.body?.stream&&y===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:nt(t),usageSessionId:a,tokenCount:t.tokenCount,cachedUsage:c,note:"Successful streaming request ended with zero usage. Check provider_stream_chunk usage_monitor and usage_capture_* logs."}),Nt({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:a,provider:t.provider||"",originalModel:t.originalModel||t.body?.model||"",model:nt(t),upstreamModel:t.upstreamModel,modelFamily:t.modelFamily||"",scenarioType:t.scenarioType||"default",clientType:t.clientType||je(t),stream:t.body?.stream??!1,inputTokens:y,outputTokens:k,cacheReadInputTokens:_,cacheCreationInputTokens:c?.cache_creation_input_tokens||0,ttft:g||!t.body?.stream?null:l.ttft,tokensPerSecond:g?null:l.tokensPerSecond,durationMs:t.requestStartTime?Math.round(Ic.now()-t.requestStartTime):0,status:g?"error":"success",errorMessage:p,responseBody:d}),t.clientContext?.usageScope!=="session"&&q.delete(po(t))}catch(r){console.error("Usage tracking error:",r)}})}var Mc,Dc=w(()=>{"use strict";He();Zt();Xt();re();Rr();wr();Pr();Lt();Wr();Ac();Vr();an();Mc=new Vf});var Uc={};mo(Uc,{createCcrServer:()=>im});import{homedir as em}from"os";import{join as tm}from"path";import{createStream as nm}from"rotating-file-stream";import{CONFIG_FILE as om,HOME_DIR as Fc,listPresets as rm,ensureCcrSelfProvider as sm}from"@wengine-ai/claude-code-router-shared";async function im(n={}){await Xa(),await Yn();let e=await Jr();sm(e).changed&&(await le(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 a=n.port??e.PORT??3456,i=process.env.SERVICE_PORT?parseInt(process.env.SERVICE_PORT):a;Ma();let c=m=>(m>9?"":"0")+m,l=(m,h)=>{let f;m?typeof m=="number"?f=new Date(m):f=m:f=new Date;let g=f.getFullYear()+""+c(f.getMonth()+1),_=c(f.getDate()),b=c(f.getHours()),y=c(f.getMinutes());return`./logs/ccr-${g}${_}${b}${y}${c(f.getSeconds())}${h?`_${h}`:""}.log`},u;n.logger!==void 0?u=n.logger:(nc(),e.LOG!==!1?(e.LOG===void 0&&(e.LOG=!0),u={level:e.LOG_LEVEL||"error",stream:nm(l,{path:Fc,interval:"1d",size:"50M",compress:!1})}):u=!1);let p=await rm(),d=new Hc({jsonPath:om,initialConfig:{providers:e.Providers||e.providers,HOST:s,PORT:i,LOG_FILE:tm(em(),".claude-code-router","claude-code-router.log")},logger:u});try{await d.ready();let m=await lc({configDir:Fc,registry:d.pluginRegistry,getServer:()=>d,log:{info:_=>d.app?.log?.info?.(_),warn:_=>d.app?.log?.warn?.(_),error:_=>d.app?.log?.error?.(_)}});m.failed.length&&d.app?.log?.warn?.(`plugins failed to load: ${m.failed.map(_=>`${_.name} (${_.error})`).join("; ")}`),d.pluginRegistry.listLoadedPlugins().length&&d.app?.log?.info?.(`provider plugins active: ${d.pluginRegistry.listLoadedPlugins().map(_=>`${_.name}@${_.version}`).join(", ")}`),m.loaded.length&&qe(d,e,d.app.log),await Sc(d,e),d.recordUsage=_=>{try{let b=_.req?_.req.clientType||je(_.req):"unknown";Nt({id:`${Date.now()}-${Math.random().toString(36).slice(2,8)}`,timestamp:new Date().toISOString(),sessionId:_.sessionId||"",provider:_.provider||"",originalModel:_.originalModel||"",model:_.model||"",modelFamily:_.modelFamily||"",scenarioType:_.scenarioType||"default",clientType:b,stream:_.stream??!1,inputTokens:_.inputTokens||0,outputTokens:0,cacheReadInputTokens:_.cacheReadInputTokens||0,cacheCreationInputTokens:_.cacheCreationInputTokens||0,ttft:null,tokensPerSecond:null,durationMs:0,status:"error",errorMessage:_.errorMessage,responseBody:void 0})}catch(b){console.error("Fallback usage tracking error:",b)}},Lc(d,e),d.ccrPreHandlerCallbacks=$c(e),(await Promise.allSettled(p.map(async _=>await d.registerNamespace(`/preset/${_.name}`,_.config)))).forEach((_,b)=>{if(_.status==="rejected"){let y=p[b],k=_.reason instanceof Error?_.reason.message:String(_.reason);d.app?.log?.error?.(`Failed to register preset "${y?.name}": ${k}`)}}),await rc(d,e),Pt(e,d.app?.log);let f=_=>{d.app.log.error({err:_},"Uncaught exception")},g=(_,b)=>{d.app.log.error({reason:_,promise:b},"Unhandled rejection")};return process.on("uncaughtException",f),process.on("unhandledRejection",g),d.app.addHook("onClose",async()=>{process.off("uncaughtException",f),process.off("unhandledRejection",g)}),d}catch(m){throw d.pluginRegistry.dispose(),await d.app.close().catch(()=>{}),m}}var jc=w(()=>{"use strict";zc();Za();wt();oc();Lr();sc();Rc();os();ao();Dc();Et();Lt();He()});import am from"fastify";import cm from"@fastify/cors";function lm(n={}){let e=am({bodyLimit:52428800,...n});return e.setErrorHandler(Cs),e.register(cm),e}async function um(n={}){return(await Promise.resolve().then(()=>(jc(),Uc))).createCcrServer(n)}var gs,Hc,$T,zc=w(()=>{_o();jt();an();Zo();rr();ir();Se();Zt();zn();re();Me();Le();Z();He();an();Wt();De();He();Se();_o();Zo();rr();ir();zr();Er();re();Me();kr();Le();re();zn();rt();Z();wt();Wr();Et();Gr();Vr();Lt();gs=class{app;pluginRegistry=new st;namespaceProviders=[];reloadProviders(e){let t=e?.Providers??e?.providers??[];this.configService.set("providers",t),this.providerService.reload();for(let o of this.namespaceProviders)o.providers.reload()}reloadNamespaceProviders(e,t){let o=this.namespaceProviders.find(r=>r.name===e);if(!o)throw new Error(`unknown namespace ${e}`);o.config.set("providers",t),o.providers.reload()}configService;providerService;transformerService;tokenizerService;activeProbeService;recordUsage;readiness;mainNamespaceRegistered=!1;signalHandlersRegistered=!1;ccrPreHandlerCallbacks;constructor(e={}){let{initialConfig:t,jsonPath:o,envPath:r,useEnvFile:s,useJsonFile:a,useEnvironmentVariables:i,...c}=e;this.app=lm({...c,logger:c.logger??!0}),this.app._server=this,this.app.addHook("onClose",async()=>{this.pluginRegistry.dispose(),this.tokenizerService?.dispose()});let l={initialConfig:t};o!==void 0&&(l.jsonPath=o),r!==void 0&&(l.envPath=r),s!==void 0&&(l.useEnvFile=s),a!==void 0&&(l.useJsonFile=a),i!==void 0&&(l.useEnvironmentVariables=i),this.configService=new Pe(l),this.transformerService=new Ge(this.configService,this.app.log),this.tokenizerService=new Ve(this.configService,this.app.log);let u=this.transformerService;this.readiness=(async()=>{await Promise.all([this.transformerService.initialize(),this.tokenizerService.initialize()]),this.pluginRegistry.attachTransformerService(u),this.providerService=new Je(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,a=this.tokenizerService;if(e!=="/"){if(!t)throw new Error("options is required");o=new Pe({initialConfig:{...t,providers:t.Providers||t.providers}}),r=new Ge(o,this.app.log),a=new Ve(o,this.app.log),await Promise.all([r.initialize(),a.initialize()]),this.pluginRegistry.attachTransformerService(r),s=new Je(o,r,this.app.log),this.namespaceProviders.push({name:e,config:o,providers:s})}let i=async c=>{c.decorate("pluginRegistry",this.pluginRegistry),c.decorate("configService",o),c.decorate("transformerService",r),c.decorate("providerService",s),c.decorate("tokenizerService",a),c.decorate("recordUsage",this.recordUsage||(()=>{})),c.addHook("preHandler",async(l,u)=>{let p=new URL(`http://127.0.0.1${l.url}`);l.pathname=p.pathname;let d=p.pathname.endsWith("/v1/messages"),m=p.pathname.endsWith("/v1/responses");if(!d&&!m||!l.body||typeof l.body!="object")return;let h=l.ccrHookOrder||=[];if(h.push("request-normalize"),m&&l.body.input&&!l.body.messages&>(l.body),l.body.stream===void 0&&(l.body.stream=!1),l.log.info({data:l.body,type:"request body"}),h.push("adapter"),!l.originalModel&&l.body.model&&(l.originalModel=l.body.model),xe(l,o.getAll()),h.push("auth-client"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.authClient(l,u),u.sent))&&(h.push("agent"),!(this.ccrPreHandlerCallbacks&&(await this.ccrPreHandlerCallbacks.agent(l,u),u.sent)))){h.push("router"),l.body?.model&&await rn(l,u,{configService:o,tokenizerService:a}),l.usageCacheKey&&q.put(l.usageCacheKey,{input_tokens:0,output_tokens:0,cache_read_input_tokens:0,cache_creation_input_tokens:0}),h.push("provider-model-normalize");try{let f=l.body;if(!f||!f.model)return u.code(400).send({error:"Missing model in request body"});let g=f.model.split(","),_=f.model;if(g.length>1){let[b,...y]=g;_=y.join(","),f.model=_,l.provider=b}l.model=_}catch(f){return l.log.error({error:f},"Error in modelProviderMiddleware:"),u.code(500).send({error:"Internal server error"})}}}),await Ii(c)};e==="/"?(await this.app.register(i),this.mainNamespaceRegistered=!0):await this.app.register(i,{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{xr()}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 yo()})}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{at()}catch{}try{lt()}catch{}try{Ao()}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=_t(()=>this.providerService.getProviders(),t,o=>ue(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)}}},Hc=gs;$T=um});zc();export{Hn as ActiveProbeService,Pe as ConfigService,te as ProjectRoutingError,Vt as ProviderHealthStore,Je as ProviderService,ce as SSEParserTransform,Qe as SSESerializerTransform,Ve as TokenizerService,Ge as TransformerService,no as apiKeyAuth,xe as applyClientAdapter,Xn as backupConfigFile,di as builtinClientAdapterRegistry,on as calculateTokenCount,Tu as clearClientAdapterCaches,eo as clearProviderHealth,yo as closeProxyDispatchers,ja as collectReachableModelKeys,um as createCcrServer,Hc as default,je as detectClientType,Ti as diagnoseResolutionFailure,he as extractSessionIdFromUserId,Ca as getActiveProbeService,Go as getAllQuotaResults,$o as getAllRateLimitInfo,fi as getClientAdapter,Ut as getConfiguredProxyUrl,U as getHealthStore,K as getProxyDispatcher,_r as getQuotaAdapter,qt as getQuotaResult,Pl as getRateLimitInfo,Ht as getRuntimeDebugLog,$T as getServer,Jr as initConfig,Yn as initDir,Ao as initProviderHealthPersistence,lt as initQuotaStorePersistence,at as initRateLimitPersistence,pi as isClientType,Ts as isGlobalProxyEnabled,to as listAnthropicCompatibleModels,Zn as mergeUsageCapture,ge as normalizeSessionId,Xe as normalizeUsagePayload,bt as pluginManager,Q as readConfigFile,Rt as readConfigFileRaw,Pt as reconcileHealthStore,Cr as resetActiveProbeService,ue as resolveProviderProxyUrl,Bn as rewriteStream,rn as router,Ci as searchProjectBySession,q as sessionUsageCache,ko as setRuntimeDebugLog,_t as startActiveProbe,xr as stopActiveProbe,Ko as storeQuotaResult,Vn as tokenSpeedPlugin,Ya as usageStore,le as writeConfigFile};
|
|
422
426
|
//# sourceMappingURL=server.mjs.map
|