@wengine-ai/llms 2.0.87 → 2.0.88

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.
@@ -69,7 +69,7 @@ https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys`};var gd
69
69
  ${this.toMarkdown(o,r+1)}`:`${n}- ${o}`).join(`
70
70
  `):typeof e=="object"&&e!==null?Object.entries(e).map(([o,s])=>typeof s=="object"&&s!==null?`${n}${o}:
71
71
  ${this.toMarkdown(s,r+1)}`:`${n}${o}: ${s}`).join(`
72
- `):`${n}${e}`}async output(e,r={}){try{let n=this.formatData(e,r);switch(this.config.level||"log"){case"info":console.info(n);break;case"warn":console.warn(n);break;case"error":console.error(n);break;case"debug":console.debug(n);break;case"log":default:console.log(n);break}return!0}catch(n){return console.error("[ConsoleOutputHandler] Output failed:",n),!1}}}});var As,sf=Me(()=>{"use strict";As=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 r=Buffer.from(`${this.config.auth.username}:${this.config.auth.password}`).toString("base64");e.Authorization=`Basic ${r}`}break;case"custom":this.config.auth.custom&&(e[this.config.auth.custom.header]=this.config.auth.custom.value);break}return e}buildBody(e,r){let{format:n="json",timestamp:o=!0,prefix:s,metadata:a}=r||{},c={data:e};return o&&(c.timestamp=new Date().toISOString()),s&&(c.prefix=s),a&&Object.keys(a).length>0&&(c.metadata=a),c}async sendRequest(e,r,n,o,s){let a=new AbortController,c=setTimeout(()=>a.abort(),s);try{let l=await fetch(e,{method:r,headers:n,body:JSON.stringify(o),signal:a.signal});if(clearTimeout(c),!l.ok)throw new Error(`HTTP ${l.status}: ${l.statusText}`);return l}catch(l){throw clearTimeout(c),l}}delay(e){return new Promise(r=>setTimeout(r,e))}async sendWithRetry(e,r,n,o,s,a){let c=null;for(let l=1;l<=a.maxAttempts;l++)try{return await this.sendRequest(e,r,n,o,s)}catch(d){if(c=d,l===a.maxAttempts)break;let f=a.backoffMs*Math.pow(2,l-1);console.warn(`[WebhookOutputHandler] Request failed (attempt ${l}/${a.maxAttempts}), retrying in ${f}ms...`,d.message),await this.delay(f)}throw c}async output(e,r={}){let n=r.timeout||this.defaultTimeout;try{let o=this.buildHeaders(),s=this.buildBody(e,r),a=await this.sendWithRetry(this.config.url,this.config.method,o,s,n,this.config.retry);return!0}catch(o){let s=o instanceof Error?o.message:String(o);if(this.config.silent)return console.error(`[WebhookOutputHandler] Failed to send data: ${s}`),!1;throw new Error(`Webhook output failed: ${s}`)}}}});var bo,af,L0,ks,uf=Me(()=>{"use strict";bo=require("fs"),af=require("path"),L0=require("os"),ks=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let r=(0,L0.tmpdir)();this.baseDir=(0,af.join)(r,this.config.subdirectory),this.ensureDir()}ensureDir(){try{(0,bo.existsSync)(this.baseDir)||(0,bo.mkdirSync)(this.baseDir,{recursive:!0})}catch{}}extractSessionId(e){try{let r=e.match(/_session_([a-f0-9-]+)/i);return r?r[1]:null}catch{return null}}getFilePath(e){let r=this.config.prefix||"session",n=this.config.extension?`.${this.config.extension}`:"",o;if(this.config.includeTimestamp){let s=Date.now();o=`${r}-${e}-${s}${n}`}else o=`${r}-${e}${n}`;return(0,af.join)(this.baseDir,o)}async output(e,r={}){try{let n=r.metadata?.sessionId;if(!n)return!1;let o={...e,timestamp:Date.now(),sessionId:n},s=this.getFilePath(n);return(0,bo.writeFileSync)(s,JSON.stringify(o,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var cf,Co,U0=Me(()=>{"use strict";of();sf();uf();cf=class{handlers=new Map;defaultOptions={};registerHandler(e,r){this.handlers.set(e,r)}registerHandlers(e){for(let r of e)if(r.enabled!==!1)try{let n=this.createHandler(r),o=r.type+"_"+Date.now();this.registerHandler(o,n)}catch(n){console.error(`[OutputManager] Failed to register ${r.type} handler:`,n)}}createHandler(e){switch(e.type){case"console":return new Es(e.config);case"webhook":return new As(e.config);case"temp-file":return new ks(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,r){let n={...this.defaultOptions,...r},o={success:[],failed:[]},s=Array.from(this.handlers.entries()).map(async([a,c])=>{try{await c.output(e,n)?o.success.push(a):o.failed.push(a)}catch(l){console.error(`[OutputManager] Handler ${a} failed:`,l),o.failed.push(a)}});return await Promise.all(s),o}async outputTo(e,r,n){let o={...this.defaultOptions,...n},s={success:[],failed:[]},a=e.map(async c=>{let l=this.handlers.get(c);if(!l){console.warn(`[OutputManager] Handler ${c} not found`),s.failed.push(c);return}try{await l.output(r,o)?s.success.push(c):s.failed.push(c)}catch(d){console.error(`[OutputManager] Handler ${c} failed:`,d),s.failed.push(c)}});return await Promise.all(a),s}async outputToType(e,r,n){let o=Array.from(this.handlers.entries()).filter(([s,a])=>a.type===e).map(([s])=>s);return this.outputTo(o,r,n)}},Co=new cf});var lD={};ti(lD,{ActiveProbeService:()=>Cs,ConfigService:()=>ln,ProviderHealthStore:()=>Po,ProviderService:()=>fn,SSEParserTransform:()=>vn,SSESerializerTransform:()=>ws,TokenizerService:()=>Sn,TransformerService:()=>kn,calculateTokenCount:()=>$d,default:()=>cD,getActiveProbeService:()=>Xd,getAllQuotaResults:()=>v0,getAllRateLimitInfo:()=>Nh,getHealthStore:()=>tt,getQuotaAdapter:()=>lu,getQuotaResult:()=>ou,getRateLimitInfo:()=>Bh,pluginManager:()=>ef,resetActiveProbeService:()=>P0,rewriteStream:()=>nf,router:()=>cu,searchProjectBySession:()=>qd,sessionUsageCache:()=>ru,startActiveProbe:()=>du,stopActiveProbe:()=>fu,storeQuotaResult:()=>nu,tokenSpeedPlugin:()=>df});module.exports=gh(lD);var H0=Pe(require("fastify"),1),W0=Pe(require("@fastify/cors"),1);var Oo=require("fs"),Yu=require("path"),Th=require("dotenv"),Dh=Pe(Ku(),1),ln=class{config={};options;constructor(e={jsonPath:"./config.json"}){this.options={envPath:e.envPath||".env",jsonPath:e.jsonPath,useEnvFile:!1,useJsonFile:e.useJsonFile!==!1,useEnvironmentVariables:e.useEnvironmentVariables!==!1,...e},this.loadConfig()}loadConfig(){this.options.useJsonFile&&this.options.jsonPath&&this.loadJsonConfig(),this.options.initialConfig&&(this.config={...this.config,...this.options.initialConfig}),this.options.useEnvFile&&this.loadEnvConfig(),this.config.LOG_FILE&&(process.env.LOG_FILE=this.config.LOG_FILE),this.config.LOG&&(process.env.LOG=this.config.LOG)}loadJsonConfig(){if(!this.options.jsonPath)return;let e=this.isAbsolutePath(this.options.jsonPath)?this.options.jsonPath:(0,Yu.join)(process.cwd(),this.options.jsonPath);if((0,Oo.existsSync)(e))try{let r=(0,Oo.readFileSync)(e,"utf-8"),n=Dh.default.parse(r);this.config={...this.config,...n},console.log(`Loaded JSON config from: ${e}`)}catch(r){console.warn(`Failed to load JSON config from ${e}:`,r)}else console.warn(`JSON config file not found: ${e}`)}loadEnvConfig(){let e=this.isAbsolutePath(this.options.envPath)?this.options.envPath:(0,Yu.join)(process.cwd(),this.options.envPath);if((0,Oo.existsSync)(e))try{let r=(0,Th.config)({path:e});r.parsed&&(this.config={...this.config,...this.parseEnvConfig(r.parsed)})}catch(r){console.warn(`Failed to load .env config from ${e}:`,r)}}loadEnvironmentVariables(){let e=this.parseEnvConfig(process.env);this.config={...this.config,...e}}parseEnvConfig(e){let r={};return Object.assign(r,e),r}isAbsolutePath(e){return e.startsWith("/")||e.includes(":")}get(e,r){let n=this.config[e];return n!==void 0?n:r}getAll(){return{...this.config}}getHttpsProxy(){return this.get("HTTPS_PROXY")||this.get("https_proxy")||this.get("httpsProxy")||this.get("PROXY_URL")}has(e){return this.config[e]!==void 0}set(e,r){this.config[e]=r}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 it(t,e=500,r="internal_error",n="api_error"){let o=new Error(t);return o.statusCode=e,o.code=r,o.type=n,o}async function Rh(t,e,r){e.log.error(t);let n=t.statusCode||500,o={error:{message:t.message+t.stack||"Internal Server Error",type:t.type||"api_error",code:t.code||"internal_error"}};return r.code(n).send(o)}var xh=require("undici");function Oh(t,e,r,n,o){let s=new Headers({"Content-Type":"application/json"});r.headers&&Object.entries(r.headers).forEach(([d,f])=>{f&&s.set(d,f)});let a,c=AbortSignal.timeout(r.TIMEOUT??60*1e3*60);if(r.signal){let d=new AbortController,f=()=>d.abort();r.signal.addEventListener("abort",f),c.addEventListener("abort",f),a=d.signal}else a=c;let l={method:"POST",headers:s,body:JSON.stringify(e),signal:a};return r.httpsProxy&&(l.dispatcher=new xh.ProxyAgent(new URL(r.httpsProxy).toString())),o?.debug({reqId:n.req.id,request:l,headers:Object.fromEntries(s.entries()),requestUrl:typeof t=="string"?t:t.toString(),useProxy:r.httpsProxy},"final request"),fetch(typeof t=="string"?t:t.toString(),l)}var Fh="2.0.87";$n();var ec=new Map,Hb=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],Wb=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],Gb=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"];function Io(t,e,r){let n=c=>{if(r instanceof Headers)return r.get(c);let l=Object.keys(r).find(d=>d.toLowerCase()===c);return l?r[l]:null},o=Zu(n,Hb),s=Zu(n,Wb),a=Zu(n,Gb);(o||s||a)&&ec.set(t,{provider:t,remaining:Ih(o),limit:Ih(s),reset:Jb(a),capturedAt:Date.now()})}function Bh(t){return ec.get(t)}function Nh(){return Array.from(ec.values())}function Zu(t,e){for(let r of e){let n=t(r);if(n)return n}return null}function Ih(t){if(!t)return null;let e=Number(t.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function Jb(t){if(!t)return null;let e=t.trim();if(!e)return null;let r=Number(e);if(Number.isFinite(r))return r>1e12?Math.floor(r/1e3):r>1e9?Math.floor(r):Math.floor(Date.now()/1e3+r);let n=Date.parse(e);if(Number.isFinite(n))return Math.floor(n/1e3);let o=Vb(e);return o===null?null:Math.floor((Date.now()+o)/1e3)}function Vb(t){let e=/(\d+(?:\.\d+)?)(ms|s|m|h|d)/gi,r=0,n=!1;for(let o of t.matchAll(e)){n=!0;let s=Number(o[1]),a=o[2].toLowerCase();if(Number.isFinite(s))switch(a){case"ms":r+=s;break;case"s":r+=s*1e3;break;case"m":r+=s*60*1e3;break;case"h":r+=s*60*60*1e3;break;case"d":r+=s*24*60*60*1e3;break}}return n?r:null}ii();async function Kb(t,e,r,n){let o=t.body,s=t.provider,a=r.providerService.getProvider(s);if(!a)throw it(`Provider '${s}' not found`,404,"provider_not_found");try{let{requestBody:c,config:l,bypass:d}=await Mh(o,a,n,t.headers,{req:t}),f=await jh(c,l,a,r,d,n,{req:t});try{a?.baseUrl&&f?.headers&&Io(s,a.baseUrl,f.headers)}catch{}let p=await Lh(c,f,a,n,d,{req:t});return Uh(p,e,o)}catch(c){let l=await Yb(t,e,r,n,c);if(l)return l;throw c}}async function Yb(t,e,r,n,o){let s=t.scenarioType||"default",a=t.familyFallback,c=t.modelFamily,l=r.configService.get("fallback"),d=tt();if(!r.configService.get("Router")?.enableFallback)return t.log.info("Fallback disabled by configuration, skipping fallback attempts"),null;let p=m=>{let[v,...b]=m.split(","),k=v?.trim(),D=b.join(",").trim();return!k||!D?null:{provider:k,model:D,key:`${k},${D}`}},y=[];if(c){let m=a?.[s];Array.isArray(m)&&m.length>0?y.push({name:`${c}/${s}`,models:m}):t.log.warn(`No ${c} fallback configured for ${s}; will try global ${s} fallback`)}if(Array.isArray(l?.[s])&&l[s].length>0&&y.push({name:`global/${s}`,models:l[s]}),y.length===0)return null;let A=t.provider||"",w=t.body.model||"",_=new Set;A&&w&&(d.recordFailure(A,w,o?.message),_.add(`${A},${w}`),r.recordUsage?.({provider:A,model:w,originalModel:t.originalModel||w,scenarioType:s,modelFamily:t.modelFamily,errorMessage:o?.message||String(o),sessionId:t.usageSessionId||t.id,stream:t.body.stream,inputTokens:t.tokenCount||0}));let g=y.reduce((m,v)=>m+v.models.length,0);t.log.warn(`Request failed for ${s}, trying ${g} fallback models across ${y.length} fallback stage(s)`);for(let m of y){t.log.info(`Trying ${m.name} fallback stage with ${m.models.length} models`);for(let v of m.models){let b=p(v);if(!b){t.log.warn(`Fallback model '${v}' is invalid, skipping`);continue}if(_.has(b.key))continue;_.add(b.key);let k=b.provider,D=b.model;try{if(!d.isAvailable(k,D)){t.log.warn(`Fallback model ${v} unavailable (fail pool), skipping`);continue}t.log.info(`Trying fallback model: ${v}`);let O={...t.body};O.model=D;let q={...t,provider:k,body:O},J=r.providerService.getProvider(k);if(!J){t.log.warn(`Fallback provider '${k}' not found, skipping`);continue}let{requestBody:B,config:Y,bypass:te}=await Mh(O,J,n,t.headers,{req:q}),Z=await jh(B,Y,J,r,te,n,{req:q});try{J?.baseUrl&&Z?.headers&&Io(k,J.baseUrl,Z.headers)}catch{}let ae=await Lh(B,Z,J,n,te,{req:q});return t.log.info(`Fallback model ${v} succeeded`),d.recordSuccess(k,D),A&&w&&(Fo().promote(A,w,s,k,D),t.log.info(`Promoted fallback model ${k},${D} for ${A},${w}:${s}`),d.forceOpen(A,w,o?.message),t.log.info(`Marked original model ${A},${w} as unavailable`)),t.provider=k,t.body=O,Uh(ae,e,O)}catch(O){d.recordFailure(k,D,O.message),t.log.warn(`Fallback model ${b.key} failed: ${O.message}`),r.recordUsage?.({provider:k,model:D,originalModel:t.body.model,scenarioType:s,modelFamily:t.modelFamily,errorMessage:O.message,sessionId:t.usageSessionId||t.id,stream:t.body.stream});continue}}}return t.log.error(`All fallback models failed for ${s}`),null}async function Mh(t,e,r,n,o){let s=JSON.parse(JSON.stringify(t)),a={},c=!1;if(c=Qb(e,r,t),c&&(n instanceof Headers?n.delete("content-length"):delete n["content-length"],a.headers=n),!c&&typeof r.transformRequestOut=="function"){let l=await r.transformRequestOut(s);l.body?(s=l.body,a=l.config||{}):s=l}if(!c&&e.transformer?.use?.length)for(let l of e.transformer.use){if(!l||typeof l.transformRequestIn!="function")continue;let d=await l.transformRequestIn(s,e,o);d.body?(s=d.body,a={...a,...d.config}):s=d}if(!c&&e.transformer?.[t.model]?.use?.length)for(let l of e.transformer[t.model].use)!l||typeof l.transformRequestIn!="function"||(s=await l.transformRequestIn(s,e,o));return{requestBody:s,config:a,bypass:c}}function Qb(t,e,r){return t.transformer?.use?.length===1&&t.transformer.use[0].name===e.name&&(!t.transformer?.[r.model]?.use.length||t.transformer?.[r.model]?.use.length===1&&t.transformer?.[r.model]?.use[0].name===e.name)}async function jh(t,e,r,n,o,s,a){let c=e.url||new URL(r.baseUrl);if(!e.TIMEOUT){let f=n.configService.get("API_TIMEOUT_MS");f&&(e.TIMEOUT=typeof f=="string"?parseInt(f,10):f)}if(o&&typeof s.auth=="function"){let f=await s.auth(t,r);if(f.body){t=f.body;let p=e.headers||{};f.config?.headers&&(p={...p,...f.config.headers},delete p.host,delete f.config.headers),e={...e,...f.config,headers:p}}else t=f}let l={Authorization:`Bearer ${r.apiKey}`,...e?.headers||{}};for(let f in l)(l[f]==="undefined"||["authorization","Authorization"].includes(f)&&l[f]?.includes("undefined"))&&delete l[f];let d=await Oh(c,t,{httpsProxy:n.configService.getHttpsProxy(),...e,headers:JSON.parse(JSON.stringify(l))},a,n.log);if(!d.ok){let f=await d.text();throw n.log.error(`[provider_response_error] Error from provider(${r.name},${t.model}: ${d.status}): ${f}`),it(`Error from provider(${r.name},${t.model}: ${d.status}): ${f}`,d.status,"provider_response_error")}return d}async function Lh(t,e,r,n,o,s){let a=e;if(!o&&r.transformer?.use?.length)for(let c of Array.from(r.transformer.use).reverse())!c||typeof c.transformResponseOut!="function"||(a=await c.transformResponseOut(a,s));if(!o&&r.transformer?.[t.model]?.use?.length)for(let c of Array.from(r.transformer[t.model].use).reverse())!c||typeof c.transformResponseOut!="function"||(a=await c.transformResponseOut(a,s));return!o&&n.transformResponseIn&&(a=await n.transformResponseIn(a,s)),a}function Uh(t,e,r){return t.ok||e.code(t.status),r.stream===!0?(e.header("Content-Type","text/event-stream"),e.header("Cache-Control","no-cache"),e.header("Connection","keep-alive"),e.send(t.body)):t.json()}var tc=async t=>{t.get("/",async()=>({message:"LLMs API",version:Fh})),t.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),t.get("/providers/health",async()=>({states:tt().getAllStates().map(o=>({provider:o.provider,model:o.model,status:o.status,failureCount:o.failureCount,successCount:o.successCount,lastFailureTime:o.lastFailureTime,lastError:o.lastError})),timestamp:new Date().toISOString()}));let e=t.transformerService.getTransformersWithEndpoint();for(let{transformer:r}of e)r.endPoint&&t.post(r.endPoint,async(n,o)=>Kb(n,o,t,r));t.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"}}},required:["id","name","type","baseUrl","apiKey","models"]}}},async(r,n)=>{let{name:o,baseUrl:s,apiKey:a,models:c}=r.body;if(!o?.trim())throw it("Provider name is required",400,"invalid_request");if(!s||!Xb(s))throw it("Valid base URL is required",400,"invalid_request");if(!a?.trim())throw it("API key is required",400,"invalid_request");if(!c||!Array.isArray(c)||c.length===0)throw it("At least one model is required",400,"invalid_request");if(t.providerService.getProvider(r.body.name))throw it(`Provider with name '${r.body.name}' already exists`,400,"provider_exists");return t.providerService.registerProvider(r.body)}),t.get("/providers",async()=>t.providerService.getProviders()),t.get("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async r=>{let n=t.providerService.getProvider(r.params.id);if(!n)throw it("Provider not found",404,"provider_not_found");return n}),t.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"}},enabled:{type:"boolean"}}}}},async(r,n)=>{let o=t.providerService.updateProvider(r.params.id,r.body);if(!o)throw it("Provider not found",404,"provider_not_found");return o}),t.delete("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async r=>{if(!t.providerService.deleteProvider(r.params.id))throw it("Provider not found",404,"provider_not_found");return{message:"Provider deleted successfully"}}),t.patch("/providers/:id/toggle",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{enabled:{type:"boolean"}},required:["enabled"]}}},async(r,n)=>{if(!t.providerService.toggleProvider(r.params.id,r.body.enabled))throw it("Provider not found",404,"provider_not_found");return{message:`Provider ${r.body.enabled?"enabled":"disabled"} successfully`}})};function Xb(t){try{return new URL(t),!0}catch{return!1}}var fn=class{constructor(e,r,n){this.configService=e;this.transformerService=r;this.logger=n;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(r=>{try{if(!r.name||!r.api_base_url||!r.api_key)return;let n={};r.transformer&&Object.keys(r.transformer).forEach(o=>{o==="use"?Array.isArray(r.transformer.use)&&(n.use=r.transformer.use.map(s=>{if(Array.isArray(s)&&typeof s[0]=="string"){let a=this.transformerService.getTransformer(s[0]);if(a)return new a(s[1])}if(typeof s=="string"){let a=this.transformerService.getTransformer(s);return typeof a=="function"?new a:a}}).filter(s=>typeof s<"u")):Array.isArray(r.transformer[o]?.use)&&(n[o]={use:r.transformer[o].use.map(s=>{if(Array.isArray(s)&&typeof s[0]=="string"){let a=this.transformerService.getTransformer(s[0]);if(a)return new a(s[1])}if(typeof s=="string"){let a=this.transformerService.getTransformer(s);return typeof a=="function"?new a:a}}).filter(s=>typeof s<"u")})}),this.registerProvider({name:r.name,baseUrl:r.api_base_url,apiKey:r.api_key,models:r.models||[],quotaToken:r.quota_token,transformer:r.transformer?n:void 0}),this.logger.info(`${r.name} provider registered`)}catch(n){this.logger.error(`${r.name} provider registered error: ${n}`)}})}registerProvider(e){let r={...e};return this.providers.set(r.name,r),e.models.forEach(n=>{let o=`${r.name},${n}`,s={provider:r.name,model:n,fullModel:o};this.modelRoutes.set(o,s),this.modelRoutes.has(n)||this.modelRoutes.set(n,s)}),r}getProviders(){return Array.from(this.providers.values())}getProvider(e){return this.providers.get(e)}updateProvider(e,r){let n=this.providers.get(e);if(!n)return null;let o={...n,...r,updatedAt:new Date};return this.providers.set(e,o),r.models&&(n.models.forEach(s=>{let a=`${n.name},${s}`;this.modelRoutes.delete(a),this.modelRoutes.delete(s)}),r.models.forEach(s=>{let a=`${n.name},${s}`,c={provider:n.name,model:s,fullModel:a};this.modelRoutes.set(a,c),this.modelRoutes.has(s)||this.modelRoutes.set(s,c)})),o}deleteProvider(e){let r=this.providers.get(e);return r?(r.models.forEach(n=>{let o=`${r.name},${n}`;this.modelRoutes.delete(o),this.modelRoutes.delete(n)}),this.providers.delete(e),!0):!1}toggleProvider(e,r){return!!this.providers.get(e)}resolveModelRoute(e){let r=this.modelRoutes.get(e);if(!r)return null;let n=this.providers.get(r.provider);return n?{provider:n,originalModel:e,targetModel:r.model}:null}getAvailableModelNames(){let e=[];return this.providers.forEach(r=>{r.models.forEach(n=>{e.push(n),e.push(`${r.name},${n}`)})}),e}getModelRoutes(){return Array.from(this.modelRoutes.values())}parseTransformerConfig(e){return e?Array.isArray(e)?e.reduce((r,n)=>{if(Array.isArray(n)){let[o,s={}]=n;r[o]=s}else r[n]={};return r},{}):e:{}}async getAvailableModels(){let e=[];return this.providers.forEach(r=>{r.models.forEach(n=>{e.push({id:n,object:"model",owned_by:r.name,provider:r.name}),e.push({id:`${r.name},${n}`,object:"model",owned_by:r.name,provider:r.name})})}),{object:"list",data:e}}};var Ve=[];for(let t=0;t<256;++t)Ve.push((t+256).toString(16).slice(1));function $h(t,e=0){return(Ve[t[e+0]]+Ve[t[e+1]]+Ve[t[e+2]]+Ve[t[e+3]]+"-"+Ve[t[e+4]]+Ve[t[e+5]]+"-"+Ve[t[e+6]]+Ve[t[e+7]]+"-"+Ve[t[e+8]]+Ve[t[e+9]]+"-"+Ve[t[e+10]]+Ve[t[e+11]]+Ve[t[e+12]]+Ve[t[e+13]]+Ve[t[e+14]]+Ve[t[e+15]]).toLowerCase()}var qh=require("crypto"),ui=new Uint8Array(256),ai=ui.length;function rc(){return ai>ui.length-16&&((0,qh.randomFillSync)(ui),ai=0),ui.slice(ai,ai+=16)}var zh=require("crypto"),nc={randomUUID:zh.randomUUID};function Zb(t,e,r){if(nc.randomUUID&&!e&&!t)return nc.randomUUID();t=t||{};let n=t.random??t.rng?.()??rc();if(n.length<16)throw new Error("Random bytes length must be >= 16");if(n[6]=n[6]&15|64,n[8]=n[8]&63|128,e){if(r=r||0,r<0||r+16>e.length)throw new RangeError(`UUID byte range ${r}:${r+15} is out of buffer bounds`);for(let o=0;o<16;++o)e[r+o]=n[o];return e}return $h(n)}var Xt=Zb;var Hh=t=>t<=0?"none":t<=1024?"low":t<=8192?"medium":"high";var Wh=(t,e)=>(t.includes("base64")&&(t=t.split("base64").pop(),t.startsWith(",")&&(t=t.slice(1))),`data:${e};base64,${t}`);var ci=class{constructor(e){this.options=e;this.useBearer=this.options?.UseBearer??!1}name="Anthropic";endPoint="/v1/messages";useBearer;logger;async auth(e,r){let n={};return this.useBearer?(n.authorization=`Bearer ${r.apiKey}`,n["x-api-key"]=void 0):(n["x-api-key"]=r.apiKey,n.authorization=void 0),{body:e,config:{headers:n}}}async transformRequestOut(e){let r=[];if(e.system){if(typeof e.system=="string")r.push({role:"system",content:e.system});else if(Array.isArray(e.system)&&e.system.length){let s=e.system.filter(a=>a.type==="text"&&a.text).map(a=>({type:"text",text:a.text,cache_control:a.cache_control}));r.push({role:"system",content:s})}}JSON.parse(JSON.stringify(e.messages||[]))?.forEach(s=>{if(s.role==="user"||s.role==="assistant"){if(typeof s.content=="string"){r.push({role:s.role,content:s.content});return}if(Array.isArray(s.content)){if(s.role==="user"){let a=s.content.filter(l=>l.type==="tool_result"&&l.tool_use_id);a.length&&a.forEach(l=>{let d={role:"tool",content:typeof l.content=="string"?l.content:JSON.stringify(l.content),tool_call_id:l.tool_use_id,cache_control:l.cache_control};r.push(d)});let c=s.content.filter(l=>l.type==="text"&&l.text||l.type==="image"&&l.source);c.length&&r.push({role:"user",content:c.map(l=>l?.type==="image"?{type:"image_url",image_url:{url:l.source?.type==="base64"?Wh(l.source.data,l.source.media_type):l.source.url},media_type:l.source.media_type}:l)})}else if(s.role==="assistant"){let a={role:"assistant",content:""},c=s.content.filter(f=>f.type==="text"&&f.text);c.length&&(a.content=c.map(f=>f.text).join(`
72
+ `):`${n}${e}`}async output(e,r={}){try{let n=this.formatData(e,r);switch(this.config.level||"log"){case"info":console.info(n);break;case"warn":console.warn(n);break;case"error":console.error(n);break;case"debug":console.debug(n);break;case"log":default:console.log(n);break}return!0}catch(n){return console.error("[ConsoleOutputHandler] Output failed:",n),!1}}}});var As,sf=Me(()=>{"use strict";As=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 r=Buffer.from(`${this.config.auth.username}:${this.config.auth.password}`).toString("base64");e.Authorization=`Basic ${r}`}break;case"custom":this.config.auth.custom&&(e[this.config.auth.custom.header]=this.config.auth.custom.value);break}return e}buildBody(e,r){let{format:n="json",timestamp:o=!0,prefix:s,metadata:a}=r||{},c={data:e};return o&&(c.timestamp=new Date().toISOString()),s&&(c.prefix=s),a&&Object.keys(a).length>0&&(c.metadata=a),c}async sendRequest(e,r,n,o,s){let a=new AbortController,c=setTimeout(()=>a.abort(),s);try{let l=await fetch(e,{method:r,headers:n,body:JSON.stringify(o),signal:a.signal});if(clearTimeout(c),!l.ok)throw new Error(`HTTP ${l.status}: ${l.statusText}`);return l}catch(l){throw clearTimeout(c),l}}delay(e){return new Promise(r=>setTimeout(r,e))}async sendWithRetry(e,r,n,o,s,a){let c=null;for(let l=1;l<=a.maxAttempts;l++)try{return await this.sendRequest(e,r,n,o,s)}catch(d){if(c=d,l===a.maxAttempts)break;let f=a.backoffMs*Math.pow(2,l-1);console.warn(`[WebhookOutputHandler] Request failed (attempt ${l}/${a.maxAttempts}), retrying in ${f}ms...`,d.message),await this.delay(f)}throw c}async output(e,r={}){let n=r.timeout||this.defaultTimeout;try{let o=this.buildHeaders(),s=this.buildBody(e,r),a=await this.sendWithRetry(this.config.url,this.config.method,o,s,n,this.config.retry);return!0}catch(o){let s=o instanceof Error?o.message:String(o);if(this.config.silent)return console.error(`[WebhookOutputHandler] Failed to send data: ${s}`),!1;throw new Error(`Webhook output failed: ${s}`)}}}});var bo,af,L0,ks,uf=Me(()=>{"use strict";bo=require("fs"),af=require("path"),L0=require("os"),ks=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let r=(0,L0.tmpdir)();this.baseDir=(0,af.join)(r,this.config.subdirectory),this.ensureDir()}ensureDir(){try{(0,bo.existsSync)(this.baseDir)||(0,bo.mkdirSync)(this.baseDir,{recursive:!0})}catch{}}extractSessionId(e){try{let r=e.match(/_session_([a-f0-9-]+)/i);return r?r[1]:null}catch{return null}}getFilePath(e){let r=this.config.prefix||"session",n=this.config.extension?`.${this.config.extension}`:"",o;if(this.config.includeTimestamp){let s=Date.now();o=`${r}-${e}-${s}${n}`}else o=`${r}-${e}${n}`;return(0,af.join)(this.baseDir,o)}async output(e,r={}){try{let n=r.metadata?.sessionId;if(!n)return!1;let o={...e,timestamp:Date.now(),sessionId:n},s=this.getFilePath(n);return(0,bo.writeFileSync)(s,JSON.stringify(o,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var cf,Co,U0=Me(()=>{"use strict";of();sf();uf();cf=class{handlers=new Map;defaultOptions={};registerHandler(e,r){this.handlers.set(e,r)}registerHandlers(e){for(let r of e)if(r.enabled!==!1)try{let n=this.createHandler(r),o=r.type+"_"+Date.now();this.registerHandler(o,n)}catch(n){console.error(`[OutputManager] Failed to register ${r.type} handler:`,n)}}createHandler(e){switch(e.type){case"console":return new Es(e.config);case"webhook":return new As(e.config);case"temp-file":return new ks(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,r){let n={...this.defaultOptions,...r},o={success:[],failed:[]},s=Array.from(this.handlers.entries()).map(async([a,c])=>{try{await c.output(e,n)?o.success.push(a):o.failed.push(a)}catch(l){console.error(`[OutputManager] Handler ${a} failed:`,l),o.failed.push(a)}});return await Promise.all(s),o}async outputTo(e,r,n){let o={...this.defaultOptions,...n},s={success:[],failed:[]},a=e.map(async c=>{let l=this.handlers.get(c);if(!l){console.warn(`[OutputManager] Handler ${c} not found`),s.failed.push(c);return}try{await l.output(r,o)?s.success.push(c):s.failed.push(c)}catch(d){console.error(`[OutputManager] Handler ${c} failed:`,d),s.failed.push(c)}});return await Promise.all(a),s}async outputToType(e,r,n){let o=Array.from(this.handlers.entries()).filter(([s,a])=>a.type===e).map(([s])=>s);return this.outputTo(o,r,n)}},Co=new cf});var lD={};ti(lD,{ActiveProbeService:()=>Cs,ConfigService:()=>ln,ProviderHealthStore:()=>Po,ProviderService:()=>fn,SSEParserTransform:()=>vn,SSESerializerTransform:()=>ws,TokenizerService:()=>Sn,TransformerService:()=>kn,calculateTokenCount:()=>$d,default:()=>cD,getActiveProbeService:()=>Xd,getAllQuotaResults:()=>v0,getAllRateLimitInfo:()=>Nh,getHealthStore:()=>tt,getQuotaAdapter:()=>lu,getQuotaResult:()=>ou,getRateLimitInfo:()=>Bh,pluginManager:()=>ef,resetActiveProbeService:()=>P0,rewriteStream:()=>nf,router:()=>cu,searchProjectBySession:()=>qd,sessionUsageCache:()=>ru,startActiveProbe:()=>du,stopActiveProbe:()=>fu,storeQuotaResult:()=>nu,tokenSpeedPlugin:()=>df});module.exports=gh(lD);var H0=Pe(require("fastify"),1),W0=Pe(require("@fastify/cors"),1);var Oo=require("fs"),Yu=require("path"),Th=require("dotenv"),Dh=Pe(Ku(),1),ln=class{config={};options;constructor(e={jsonPath:"./config.json"}){this.options={envPath:e.envPath||".env",jsonPath:e.jsonPath,useEnvFile:!1,useJsonFile:e.useJsonFile!==!1,useEnvironmentVariables:e.useEnvironmentVariables!==!1,...e},this.loadConfig()}loadConfig(){this.options.useJsonFile&&this.options.jsonPath&&this.loadJsonConfig(),this.options.initialConfig&&(this.config={...this.config,...this.options.initialConfig}),this.options.useEnvFile&&this.loadEnvConfig(),this.config.LOG_FILE&&(process.env.LOG_FILE=this.config.LOG_FILE),this.config.LOG&&(process.env.LOG=this.config.LOG)}loadJsonConfig(){if(!this.options.jsonPath)return;let e=this.isAbsolutePath(this.options.jsonPath)?this.options.jsonPath:(0,Yu.join)(process.cwd(),this.options.jsonPath);if((0,Oo.existsSync)(e))try{let r=(0,Oo.readFileSync)(e,"utf-8"),n=Dh.default.parse(r);this.config={...this.config,...n},console.log(`Loaded JSON config from: ${e}`)}catch(r){console.warn(`Failed to load JSON config from ${e}:`,r)}else console.warn(`JSON config file not found: ${e}`)}loadEnvConfig(){let e=this.isAbsolutePath(this.options.envPath)?this.options.envPath:(0,Yu.join)(process.cwd(),this.options.envPath);if((0,Oo.existsSync)(e))try{let r=(0,Th.config)({path:e});r.parsed&&(this.config={...this.config,...this.parseEnvConfig(r.parsed)})}catch(r){console.warn(`Failed to load .env config from ${e}:`,r)}}loadEnvironmentVariables(){let e=this.parseEnvConfig(process.env);this.config={...this.config,...e}}parseEnvConfig(e){let r={};return Object.assign(r,e),r}isAbsolutePath(e){return e.startsWith("/")||e.includes(":")}get(e,r){let n=this.config[e];return n!==void 0?n:r}getAll(){return{...this.config}}getHttpsProxy(){return this.get("HTTPS_PROXY")||this.get("https_proxy")||this.get("httpsProxy")||this.get("PROXY_URL")}has(e){return this.config[e]!==void 0}set(e,r){this.config[e]=r}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 it(t,e=500,r="internal_error",n="api_error"){let o=new Error(t);return o.statusCode=e,o.code=r,o.type=n,o}async function Rh(t,e,r){e.log.error(t);let n=t.statusCode||500,o={error:{message:t.message+t.stack||"Internal Server Error",type:t.type||"api_error",code:t.code||"internal_error"}};return r.code(n).send(o)}var xh=require("undici");function Oh(t,e,r,n,o){let s=new Headers({"Content-Type":"application/json"});r.headers&&Object.entries(r.headers).forEach(([d,f])=>{f&&s.set(d,f)});let a,c=AbortSignal.timeout(r.TIMEOUT??60*1e3*60);if(r.signal){let d=new AbortController,f=()=>d.abort();r.signal.addEventListener("abort",f),c.addEventListener("abort",f),a=d.signal}else a=c;let l={method:"POST",headers:s,body:JSON.stringify(e),signal:a};return r.httpsProxy&&(l.dispatcher=new xh.ProxyAgent(new URL(r.httpsProxy).toString())),o?.debug({reqId:n.req.id,request:l,headers:Object.fromEntries(s.entries()),requestUrl:typeof t=="string"?t:t.toString(),useProxy:r.httpsProxy},"final request"),fetch(typeof t=="string"?t:t.toString(),l)}var Fh="2.0.88";$n();var ec=new Map,Hb=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],Wb=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],Gb=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"];function Io(t,e,r){let n=c=>{if(r instanceof Headers)return r.get(c);let l=Object.keys(r).find(d=>d.toLowerCase()===c);return l?r[l]:null},o=Zu(n,Hb),s=Zu(n,Wb),a=Zu(n,Gb);(o||s||a)&&ec.set(t,{provider:t,remaining:Ih(o),limit:Ih(s),reset:Jb(a),capturedAt:Date.now()})}function Bh(t){return ec.get(t)}function Nh(){return Array.from(ec.values())}function Zu(t,e){for(let r of e){let n=t(r);if(n)return n}return null}function Ih(t){if(!t)return null;let e=Number(t.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function Jb(t){if(!t)return null;let e=t.trim();if(!e)return null;let r=Number(e);if(Number.isFinite(r))return r>1e12?Math.floor(r/1e3):r>1e9?Math.floor(r):Math.floor(Date.now()/1e3+r);let n=Date.parse(e);if(Number.isFinite(n))return Math.floor(n/1e3);let o=Vb(e);return o===null?null:Math.floor((Date.now()+o)/1e3)}function Vb(t){let e=/(\d+(?:\.\d+)?)(ms|s|m|h|d)/gi,r=0,n=!1;for(let o of t.matchAll(e)){n=!0;let s=Number(o[1]),a=o[2].toLowerCase();if(Number.isFinite(s))switch(a){case"ms":r+=s;break;case"s":r+=s*1e3;break;case"m":r+=s*60*1e3;break;case"h":r+=s*60*60*1e3;break;case"d":r+=s*24*60*60*1e3;break}}return n?r:null}ii();async function Kb(t,e,r,n){let o=t.body,s=t.provider,a=r.providerService.getProvider(s);if(!a)throw it(`Provider '${s}' not found`,404,"provider_not_found");try{let{requestBody:c,config:l,bypass:d}=await Mh(o,a,n,t.headers,{req:t}),f=await jh(c,l,a,r,d,n,{req:t});try{a?.baseUrl&&f?.headers&&Io(s,a.baseUrl,f.headers)}catch{}let p=await Lh(c,f,a,n,d,{req:t});return Uh(p,e,o)}catch(c){let l=await Yb(t,e,r,n,c);if(l)return l;throw c}}async function Yb(t,e,r,n,o){let s=t.scenarioType||"default",a=t.familyFallback,c=t.modelFamily,l=r.configService.get("fallback"),d=tt();if(!r.configService.get("Router")?.enableFallback)return t.log.info("Fallback disabled by configuration, skipping fallback attempts"),null;let p=m=>{let[v,...b]=m.split(","),k=v?.trim(),D=b.join(",").trim();return!k||!D?null:{provider:k,model:D,key:`${k},${D}`}},y=[];if(c){let m=a?.[s];Array.isArray(m)&&m.length>0?y.push({name:`${c}/${s}`,models:m}):t.log.warn(`No ${c} fallback configured for ${s}; will try global ${s} fallback`)}if(Array.isArray(l?.[s])&&l[s].length>0&&y.push({name:`global/${s}`,models:l[s]}),y.length===0)return null;let A=t.provider||"",w=t.body.model||"",_=new Set;A&&w&&(d.recordFailure(A,w,o?.message),_.add(`${A},${w}`),r.recordUsage?.({provider:A,model:w,originalModel:t.originalModel||w,scenarioType:s,modelFamily:t.modelFamily,errorMessage:o?.message||String(o),sessionId:t.usageSessionId||t.id,stream:t.body.stream,inputTokens:t.tokenCount||0}));let g=y.reduce((m,v)=>m+v.models.length,0);t.log.warn(`Request failed for ${s}, trying ${g} fallback models across ${y.length} fallback stage(s)`);for(let m of y){t.log.info(`Trying ${m.name} fallback stage with ${m.models.length} models`);for(let v of m.models){let b=p(v);if(!b){t.log.warn(`Fallback model '${v}' is invalid, skipping`);continue}if(_.has(b.key))continue;_.add(b.key);let k=b.provider,D=b.model;try{if(!d.isAvailable(k,D)){t.log.warn(`Fallback model ${v} unavailable (fail pool), skipping`);continue}t.log.info(`Trying fallback model: ${v}`);let O={...t.body};O.model=D;let q={...t,provider:k,body:O},J=r.providerService.getProvider(k);if(!J){t.log.warn(`Fallback provider '${k}' not found, skipping`);continue}let{requestBody:B,config:Y,bypass:te}=await Mh(O,J,n,t.headers,{req:q}),Z=await jh(B,Y,J,r,te,n,{req:q});try{J?.baseUrl&&Z?.headers&&Io(k,J.baseUrl,Z.headers)}catch{}let ae=await Lh(B,Z,J,n,te,{req:q});return t.log.info(`Fallback model ${v} succeeded`),d.recordSuccess(k,D),A&&w&&(Fo().promote(A,w,s,k,D),t.log.info(`Promoted fallback model ${k},${D} for ${A},${w}:${s}`),d.forceOpen(A,w,o?.message),t.log.info(`Marked original model ${A},${w} as unavailable`)),t.provider=k,t.body=O,Uh(ae,e,O)}catch(O){d.recordFailure(k,D,O.message),t.log.warn(`Fallback model ${b.key} failed: ${O.message}`),r.recordUsage?.({provider:k,model:D,originalModel:t.body.model,scenarioType:s,modelFamily:t.modelFamily,errorMessage:O.message,sessionId:t.usageSessionId||t.id,stream:t.body.stream});continue}}}return t.log.error(`All fallback models failed for ${s}`),null}async function Mh(t,e,r,n,o){let s=JSON.parse(JSON.stringify(t)),a={},c=!1;if(c=Qb(e,r,t),c&&(n instanceof Headers?n.delete("content-length"):delete n["content-length"],a.headers=n),!c&&typeof r.transformRequestOut=="function"){let l=await r.transformRequestOut(s);l.body?(s=l.body,a=l.config||{}):s=l}if(!c&&e.transformer?.use?.length)for(let l of e.transformer.use){if(!l||typeof l.transformRequestIn!="function")continue;let d=await l.transformRequestIn(s,e,o);d.body?(s=d.body,a={...a,...d.config}):s=d}if(!c&&e.transformer?.[t.model]?.use?.length)for(let l of e.transformer[t.model].use)!l||typeof l.transformRequestIn!="function"||(s=await l.transformRequestIn(s,e,o));return{requestBody:s,config:a,bypass:c}}function Qb(t,e,r){return t.transformer?.use?.length===1&&t.transformer.use[0].name===e.name&&(!t.transformer?.[r.model]?.use.length||t.transformer?.[r.model]?.use.length===1&&t.transformer?.[r.model]?.use[0].name===e.name)}async function jh(t,e,r,n,o,s,a){let c=e.url||new URL(r.baseUrl);if(!e.TIMEOUT){let f=n.configService.get("API_TIMEOUT_MS");f&&(e.TIMEOUT=typeof f=="string"?parseInt(f,10):f)}if(o&&typeof s.auth=="function"){let f=await s.auth(t,r);if(f.body){t=f.body;let p=e.headers||{};f.config?.headers&&(p={...p,...f.config.headers},delete p.host,delete f.config.headers),e={...e,...f.config,headers:p}}else t=f}let l={Authorization:`Bearer ${r.apiKey}`,...e?.headers||{}};for(let f in l)(l[f]==="undefined"||["authorization","Authorization"].includes(f)&&l[f]?.includes("undefined"))&&delete l[f];let d=await Oh(c,t,{httpsProxy:n.configService.getHttpsProxy(),...e,headers:JSON.parse(JSON.stringify(l))},a,n.log);if(!d.ok){let f=await d.text();throw n.log.error(`[provider_response_error] Error from provider(${r.name},${t.model}: ${d.status}): ${f}`),it(`Error from provider(${r.name},${t.model}: ${d.status}): ${f}`,d.status,"provider_response_error")}return d}async function Lh(t,e,r,n,o,s){let a=e;if(!o&&r.transformer?.use?.length)for(let c of Array.from(r.transformer.use).reverse())!c||typeof c.transformResponseOut!="function"||(a=await c.transformResponseOut(a,s));if(!o&&r.transformer?.[t.model]?.use?.length)for(let c of Array.from(r.transformer[t.model].use).reverse())!c||typeof c.transformResponseOut!="function"||(a=await c.transformResponseOut(a,s));return!o&&n.transformResponseIn&&(a=await n.transformResponseIn(a,s)),a}function Uh(t,e,r){return t.ok||e.code(t.status),r.stream===!0?(e.header("Content-Type","text/event-stream"),e.header("Cache-Control","no-cache"),e.header("Connection","keep-alive"),e.send(t.body)):t.json()}var tc=async t=>{t.get("/",async()=>({message:"LLMs API",version:Fh})),t.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),t.get("/providers/health",async()=>({states:tt().getAllStates().map(o=>({provider:o.provider,model:o.model,status:o.status,failureCount:o.failureCount,successCount:o.successCount,lastFailureTime:o.lastFailureTime,lastError:o.lastError})),timestamp:new Date().toISOString()}));let e=t.transformerService.getTransformersWithEndpoint();for(let{transformer:r}of e)r.endPoint&&t.post(r.endPoint,async(n,o)=>Kb(n,o,t,r));t.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"}}},required:["id","name","type","baseUrl","apiKey","models"]}}},async(r,n)=>{let{name:o,baseUrl:s,apiKey:a,models:c}=r.body;if(!o?.trim())throw it("Provider name is required",400,"invalid_request");if(!s||!Xb(s))throw it("Valid base URL is required",400,"invalid_request");if(!a?.trim())throw it("API key is required",400,"invalid_request");if(!c||!Array.isArray(c)||c.length===0)throw it("At least one model is required",400,"invalid_request");if(t.providerService.getProvider(r.body.name))throw it(`Provider with name '${r.body.name}' already exists`,400,"provider_exists");return t.providerService.registerProvider(r.body)}),t.get("/providers",async()=>t.providerService.getProviders()),t.get("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async r=>{let n=t.providerService.getProvider(r.params.id);if(!n)throw it("Provider not found",404,"provider_not_found");return n}),t.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"}},enabled:{type:"boolean"}}}}},async(r,n)=>{let o=t.providerService.updateProvider(r.params.id,r.body);if(!o)throw it("Provider not found",404,"provider_not_found");return o}),t.delete("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async r=>{if(!t.providerService.deleteProvider(r.params.id))throw it("Provider not found",404,"provider_not_found");return{message:"Provider deleted successfully"}}),t.patch("/providers/:id/toggle",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]},body:{type:"object",properties:{enabled:{type:"boolean"}},required:["enabled"]}}},async(r,n)=>{if(!t.providerService.toggleProvider(r.params.id,r.body.enabled))throw it("Provider not found",404,"provider_not_found");return{message:`Provider ${r.body.enabled?"enabled":"disabled"} successfully`}})};function Xb(t){try{return new URL(t),!0}catch{return!1}}var fn=class{constructor(e,r,n){this.configService=e;this.transformerService=r;this.logger=n;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(r=>{try{if(!r.name||!r.api_base_url||!r.api_key)return;let n={};r.transformer&&Object.keys(r.transformer).forEach(o=>{o==="use"?Array.isArray(r.transformer.use)&&(n.use=r.transformer.use.map(s=>{if(Array.isArray(s)&&typeof s[0]=="string"){let a=this.transformerService.getTransformer(s[0]);if(a)return new a(s[1])}if(typeof s=="string"){let a=this.transformerService.getTransformer(s);return typeof a=="function"?new a:a}}).filter(s=>typeof s<"u")):Array.isArray(r.transformer[o]?.use)&&(n[o]={use:r.transformer[o].use.map(s=>{if(Array.isArray(s)&&typeof s[0]=="string"){let a=this.transformerService.getTransformer(s[0]);if(a)return new a(s[1])}if(typeof s=="string"){let a=this.transformerService.getTransformer(s);return typeof a=="function"?new a:a}}).filter(s=>typeof s<"u")})}),this.registerProvider({name:r.name,baseUrl:r.api_base_url,apiKey:r.api_key,models:r.models||[],quotaToken:r.quota_token,transformer:r.transformer?n:void 0}),this.logger.info(`${r.name} provider registered`)}catch(n){this.logger.error(`${r.name} provider registered error: ${n}`)}})}registerProvider(e){let r={...e};return this.providers.set(r.name,r),e.models.forEach(n=>{let o=`${r.name},${n}`,s={provider:r.name,model:n,fullModel:o};this.modelRoutes.set(o,s),this.modelRoutes.has(n)||this.modelRoutes.set(n,s)}),r}getProviders(){return Array.from(this.providers.values())}getProvider(e){return this.providers.get(e)}updateProvider(e,r){let n=this.providers.get(e);if(!n)return null;let o={...n,...r,updatedAt:new Date};return this.providers.set(e,o),r.models&&(n.models.forEach(s=>{let a=`${n.name},${s}`;this.modelRoutes.delete(a),this.modelRoutes.delete(s)}),r.models.forEach(s=>{let a=`${n.name},${s}`,c={provider:n.name,model:s,fullModel:a};this.modelRoutes.set(a,c),this.modelRoutes.has(s)||this.modelRoutes.set(s,c)})),o}deleteProvider(e){let r=this.providers.get(e);return r?(r.models.forEach(n=>{let o=`${r.name},${n}`;this.modelRoutes.delete(o),this.modelRoutes.delete(n)}),this.providers.delete(e),!0):!1}toggleProvider(e,r){return!!this.providers.get(e)}resolveModelRoute(e){let r=this.modelRoutes.get(e);if(!r)return null;let n=this.providers.get(r.provider);return n?{provider:n,originalModel:e,targetModel:r.model}:null}getAvailableModelNames(){let e=[];return this.providers.forEach(r=>{r.models.forEach(n=>{e.push(n),e.push(`${r.name},${n}`)})}),e}getModelRoutes(){return Array.from(this.modelRoutes.values())}parseTransformerConfig(e){return e?Array.isArray(e)?e.reduce((r,n)=>{if(Array.isArray(n)){let[o,s={}]=n;r[o]=s}else r[n]={};return r},{}):e:{}}async getAvailableModels(){let e=[];return this.providers.forEach(r=>{r.models.forEach(n=>{e.push({id:n,object:"model",owned_by:r.name,provider:r.name}),e.push({id:`${r.name},${n}`,object:"model",owned_by:r.name,provider:r.name})})}),{object:"list",data:e}}};var Ve=[];for(let t=0;t<256;++t)Ve.push((t+256).toString(16).slice(1));function $h(t,e=0){return(Ve[t[e+0]]+Ve[t[e+1]]+Ve[t[e+2]]+Ve[t[e+3]]+"-"+Ve[t[e+4]]+Ve[t[e+5]]+"-"+Ve[t[e+6]]+Ve[t[e+7]]+"-"+Ve[t[e+8]]+Ve[t[e+9]]+"-"+Ve[t[e+10]]+Ve[t[e+11]]+Ve[t[e+12]]+Ve[t[e+13]]+Ve[t[e+14]]+Ve[t[e+15]]).toLowerCase()}var qh=require("crypto"),ui=new Uint8Array(256),ai=ui.length;function rc(){return ai>ui.length-16&&((0,qh.randomFillSync)(ui),ai=0),ui.slice(ai,ai+=16)}var zh=require("crypto"),nc={randomUUID:zh.randomUUID};function Zb(t,e,r){if(nc.randomUUID&&!e&&!t)return nc.randomUUID();t=t||{};let n=t.random??t.rng?.()??rc();if(n.length<16)throw new Error("Random bytes length must be >= 16");if(n[6]=n[6]&15|64,n[8]=n[8]&63|128,e){if(r=r||0,r<0||r+16>e.length)throw new RangeError(`UUID byte range ${r}:${r+15} is out of buffer bounds`);for(let o=0;o<16;++o)e[r+o]=n[o];return e}return $h(n)}var Xt=Zb;var Hh=t=>t<=0?"none":t<=1024?"low":t<=8192?"medium":"high";var Wh=(t,e)=>(t.includes("base64")&&(t=t.split("base64").pop(),t.startsWith(",")&&(t=t.slice(1))),`data:${e};base64,${t}`);var ci=class{constructor(e){this.options=e;this.useBearer=this.options?.UseBearer??!1}name="Anthropic";endPoint="/v1/messages";useBearer;logger;async auth(e,r){let n={};return this.useBearer?(n.authorization=`Bearer ${r.apiKey}`,n["x-api-key"]=void 0):(n["x-api-key"]=r.apiKey,n.authorization=void 0),{body:e,config:{headers:n}}}async transformRequestOut(e){let r=[];if(e.system){if(typeof e.system=="string")r.push({role:"system",content:e.system});else if(Array.isArray(e.system)&&e.system.length){let s=e.system.filter(a=>a.type==="text"&&a.text).map(a=>({type:"text",text:a.text,cache_control:a.cache_control}));r.push({role:"system",content:s})}}JSON.parse(JSON.stringify(e.messages||[]))?.forEach(s=>{if(s.role==="user"||s.role==="assistant"){if(typeof s.content=="string"){r.push({role:s.role,content:s.content});return}if(Array.isArray(s.content)){if(s.role==="user"){let a=s.content.filter(l=>l.type==="tool_result"&&l.tool_use_id);a.length&&a.forEach(l=>{let d={role:"tool",content:typeof l.content=="string"?l.content:JSON.stringify(l.content),tool_call_id:l.tool_use_id,cache_control:l.cache_control};r.push(d)});let c=s.content.filter(l=>l.type==="text"&&l.text||l.type==="image"&&l.source);c.length&&r.push({role:"user",content:c.map(l=>l?.type==="image"?{type:"image_url",image_url:{url:l.source?.type==="base64"?Wh(l.source.data,l.source.media_type):l.source.url},media_type:l.source.media_type}:l)})}else if(s.role==="assistant"){let a={role:"assistant",content:""},c=s.content.filter(f=>f.type==="text"&&f.text);c.length&&(a.content=c.map(f=>f.text).join(`
73
73
  `));let l=s.content.filter(f=>f.type==="tool_use"&&f.id);l.length&&(a.tool_calls=l.map(f=>({id:f.id,type:"function",function:{name:f.name,arguments:JSON.stringify(f.input||{})}})));let d=s.content.find(f=>f.type==="thinking");d&&(a.thinking={content:d.thinking,signature:d.signature}),r.push(a)}return}}});let o={messages:r,model:e.model,max_tokens:e.max_tokens,temperature:e.temperature,stream:e.stream,tools:e.tools?.length?this.convertAnthropicToolsToUnified(e.tools):void 0,tool_choice:e.tool_choice};return e.thinking&&(o.reasoning={effort:Hh(e.thinking.budget_tokens),enabled:e.thinking.type==="enabled"}),e.tool_choice&&(e.tool_choice.type==="tool"?o.tool_choice={type:"function",function:{name:e.tool_choice.name}}:o.tool_choice=e.tool_choice.type),o}async transformResponseIn(e,r){if(e.headers.get("Content-Type")?.includes("text/event-stream")){if(!e.body)throw new Error("Stream response body is null");let o=await this.convertOpenAIStreamToAnthropic(e.body,r);return new Response(o,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}else{let o=await e.json(),s=this.convertOpenAIResponseToAnthropic(o,r);return new Response(JSON.stringify(s),{headers:{"Content-Type":"application/json"}})}}convertAnthropicToolsToUnified(e){return e.map(r=>({type:"function",function:{name:r.name,description:r.description||"",parameters:r.input_schema}}))}async convertOpenAIStreamToAnthropic(e,r){return new ReadableStream({start:async o=>{let s=new TextEncoder,a=`msg_${Date.now()}`,c=null,l="unknown",d=!1,f=!1,p=!1,y=new Map,A=new Map,w=0,_=0,g=0,m=!1,v=!1,b=0,k=-1,D=()=>{let B=b;return b++,B},O=B=>{if(!m)try{o.enqueue(B);let Y=new TextDecoder().decode(B);this.logger.debug({reqId:r.req.id,data:Y,type:"send data"})}catch(Y){if(Y instanceof TypeError&&Y.message.includes("Controller is already closed"))m=!0;else throw this.logger.debug({reqId:r.req.id,error:Y instanceof Error?Y.message:String(Y),type:"send data error"}),Y}},q=()=>{if(!m)try{if(k>=0){let Y={type:"content_block_stop",index:k};O(s.encode(`event: content_block_stop
74
74
  data: ${JSON.stringify(Y)}
75
75
 
@@ -341,7 +341,7 @@ ${o.content}`),delete o.thinking)});let n=e.messages[e.messages.length-1];return
341
341
  error: ${r.message}
342
342
  stack: ${r.stack}`),!1}}async initialize(){try{await this.registerDefaultTransformersInternal(),await this.loadFromConfig()}catch(e){this.logger.error(`TransformerService init error: ${e.message}
343
343
  Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(u0).forEach(e=>{if("TransformerName"in e&&typeof e.TransformerName=="string")this.registerTransformer(e.TransformerName,e);else{let r=new e;r&&typeof r=="object"&&(r.logger=this.logger),this.registerTransformer(r.name,r)}})}catch(e){this.logger.error({error:e},"transformer regist error:")}}async loadFromConfig(){let e=this.configService.get("transformers",[]);for(let r of e)await this.registerTransformerFromConfig(r)}};var c0=require("tiktoken"),ps=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=(0,c0.get_encoding)(e)}catch{throw new Error(`Failed to initialize tiktoken encoding: ${e}`)}}async initialize(){if(!this.encoding)throw new Error("Tiktoken encoding not initialized")}async countTokens(e){let r=this.encoding;if(!r)throw new Error("Encoding not initialized");let n=0,{messages:o,system:s,tools:a}=e;return Array.isArray(o)&&o.forEach(c=>{typeof c.content=="string"?n+=r.encode(c.content).length:Array.isArray(c.content)&&c.content.forEach(l=>{if(l.type==="text")n+=r.encode(l.text).length;else if(l.type==="tool_use")n+=r.encode(JSON.stringify(l.input)).length;else if(l.type==="tool_result"){let d=typeof l.content=="string"?l.content:JSON.stringify(l.content);n+=r.encode(d).length}})}),typeof s=="string"?n+=r.encode(s).length:Array.isArray(s)&&s.forEach(c=>{c.type==="text"&&(typeof c.text=="string"?n+=r.encode(c.text).length:Array.isArray(c.text)&&c.text.forEach(l=>{n+=r.encode(l||"").length}))}),a&&a.forEach(c=>{c.description&&(n+=r.encode(c.name+c.description).length),c.input_schema&&(n+=r.encode(JSON.stringify(c.input_schema)).length)}),n}isInitialized(){return this.encoding!==void 0}encodeText(e){let r=this.encoding;if(!r)throw new Error("Encoding not initialized");return Array.from(r.encode(e))}dispose(){this.encoding&&(this.encoding.free(),this.encoding=void 0)}};var _s=require("path"),S0=require("os"),yo=require("fs"),bs=require("fs");var OS=class{constructor(t){this.trie=this._build_trie(t)}_build_trie(t){let e=Object.create(null);for(let r of t){let n=e;for(let o=0;o<r.length;++o){let s=r[o];n=n[s]??=Object.create(null)}n.end=r}return e}split(t){let e=[],r=t.length,n=0,o=0;for(;o<r;){let s=this.trie,a=null,c=o;for(;c<r&&(s=s[t[c]]);)s.end&&(a=s.end),++c;a?(o>n&&e.push(t.slice(n,o)),e.push(a),o+=a.length,n=o):++o}return n<r&&e.push(t.slice(n)),e}},FS=OS,PS=class{constructor(t){this.content=t.content,this.id=t.id,this.single_word=t.single_word??!1,this.lstrip=t.lstrip??!1,this.rstrip=t.rstrip??!1,this.special=t.special??!1,this.normalized=t.normalized??null}},IS=PS,g0=(()=>{let t=[...Array.from({length:94},(o,s)=>s+33),...Array.from({length:12},(o,s)=>s+161),...Array.from({length:82},(o,s)=>s+174)],e=t.slice(),r=0;for(let o=0;o<256;++o)t.includes(o)||(t.push(o),e.push(256+r),r+=1);let n=e.map(o=>String.fromCharCode(o));return Object.fromEntries(t.map((o,s)=>[o,n[s]]))})(),BS=t=>Object.fromEntries(Object.entries(t).map(([e,r])=>[r,e])),NS=BS(g0),l0=".,!?\u2026\u3002\uFF0C\u3001\u0964\u06D4\u060C",MS=new Map([["(?i:'s|'t|'re|'ve|'m|'ll|'d)","(?:'([sS]|[tT]|[rR][eE]|[vV][eE]|[mM]|[lL][lL]|[dD]))"],["(?i:[sdmt]|ll|ve|re)","(?:[sS]|[dD]|[mM]|[tT]|[lL][lL]|[vV][eE]|[rR][eE])"],["[^\\r\\n\\p{L}\\p{N}]?+","[^\\r\\n\\p{L}\\p{N}]?"],["[^\\s\\p{L}\\p{N}]++","[^\\s\\p{L}\\p{N}]+"],[` ?[^(\\s|[${l0}])]+`,` ?[^\\s${l0}]+`]]),ms="\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E",RO=new RegExp(`^[${ms}]+$`,"gu"),Nd=t=>t.replace(/ \./g,".").replace(/ \?/g,"?").replace(/ \!/g,"!").replace(/ ,/g,",").replace(/ \' /g,"'").replace(/ n't/g,"n't").replace(/ 'm/g,"'m").replace(/ 's/g,"'s").replace(/ 've/g,"'ve").replace(/ 're/g,"'re"),Qa=(t,e=!0)=>{if(t.Regex!==void 0){let r=t.Regex.replace(/\\([#&~])/g,"$1");for(let[n,o]of MS)r=r.replaceAll(n,o);return new RegExp(r,"gu")}else if(t.String!==void 0){let r=jS(t.String);return new RegExp(e?r:`(${r})`,"gu")}else return console.warn("Unknown pattern type:",t),null},jS=t=>t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),LS=(t,e,r)=>{let n=[],o=0;for(;o<t.length;){if(n.push(t[o]),(e.get(t[o])??r)!==r){++o;continue}for(;++o<t.length&&(e.get(t[o])??r)===r;)e.get(n.at(-1))!==r&&(n[n.length-1]+=t[o])}return n},US=t=>t>=19968&&t<=40959||t>=13312&&t<=19903||t>=131072&&t<=173791||t>=173824&&t<=177983||t>=177984&&t<=178207||t>=178208&&t<=183983||t>=63744&&t<=64255||t>=194560&&t<=195103,$S=t=>Number.isInteger(t)||typeof t=="bigint",qS=t=>{let e=0;for(let r of t)++e;return e},zS=t=>y0(t.toLowerCase()),Bt=(...t)=>Array.prototype.concat.apply([],t),Md=t=>new Map(Object.entries(t)),HS=(t,e)=>{let r=[],n=0;for(let o of t.matchAll(e)){let s=o[0];n<o.index&&r.push(t.slice(n,o.index)),s.length>0&&r.push(s),n=o.index+s.length}return n<t.length&&r.push(t.slice(n)),r},y0=t=>t.replace(new RegExp("\\p{M}","gu"),""),d0=(t,e,r=[])=>{if(!t||Array.isArray(t)||typeof t!="object")return`${e} must be a valid object`;for(let n of r)if(!(n in t))return`${e} must contain a "${n}" property`;return null},WS=t=>t.match(/\S+/g)||[],GS=class{constructor(){let t=function(...e){return t._call(...e)};return Object.setPrototypeOf(t,new.target.prototype)}},gs=GS,JS=class extends gs{constructor(t){super(),this.config=t}_call(t){return this.normalize(t)}},Rr=JS,VS=class extends Rr{tokenize_chinese_chars(t){let e=[];for(let r=0;r<t.length;++r){let n=t[r],o=n.charCodeAt(0);US(o)?(e.push(" "),e.push(n),e.push(" ")):e.push(n)}return e.join("")}strip_accents(t){return t.normalize("NFD").replace(new RegExp("\\p{Mn}","gu"),"")}is_control(t){switch(t){case" ":case`
344
- `:case"\r":return!1;default:return new RegExp("^\\p{Cc}|\\p{Cf}|\\p{Co}|\\p{Cs}$","u").test(t)}}clean_text(t){let e=[];for(let r of t){let n=r.charCodeAt(0);n===0||n===65533||this.is_control(r)||(/^\s$/.test(r)?e.push(" "):e.push(r))}return e.join("")}normalize(t){return this.config.clean_text&&(t=this.clean_text(t)),this.config.handle_chinese_chars&&(t=this.tokenize_chinese_chars(t)),this.config.lowercase?(t=t.toLowerCase(),this.config.strip_accents!==!1&&(t=this.strip_accents(t))):this.config.strip_accents&&(t=this.strip_accents(t)),t}},KS=VS,YS=class extends Rr{constructor(t){super(t),this.charsmap=t.precompiled_charsmap??null}normalize(t){return t=t.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm,""),t=t.replace(/[\u0009\u000A\u000C\u000D\u00A0\u1680\u2000-\u200F\u2028\u2029\u202F\u205F\u2581\u3000\uFEFF\uFFFD]/gm," "),t.includes("\uFF5E")?t=t.split("\uFF5E").map(e=>e.normalize("NFKC")).join("\uFF5E"):t=t.normalize("NFKC"),t}},QS=YS,XS=class extends Rr{constructor(t){super(t),this.normalizers=(t.normalizers??[]).map(e=>_0(e))}normalize(t){return this.normalizers.reduce((e,r)=>r?r.normalize(e):e,t)}},ZS=XS,ev=class extends Rr{normalize(t){let e=Qa(this.config.pattern??{});return e===null?t:t.replaceAll(e,this.config.content??"")}},tv=ev,rv=class extends Rr{constructor(){super(...arguments),this.form="NFC"}normalize(t){return t=t.normalize(this.form),t}},Xa=rv,nv=class extends Xa{constructor(){super(...arguments),this.form="NFC"}},ov=nv,sv=class extends Xa{constructor(){super(...arguments),this.form="NFD"}},iv=sv,av=class extends Xa{constructor(){super(...arguments),this.form="NFKC"}},uv=av,cv=class extends Xa{constructor(){super(...arguments),this.form="NFKD"}},lv=cv,dv=class extends Rr{normalize(t){return this.config.strip_left&&this.config.strip_right?t=t.trim():(this.config.strip_left&&(t=t.trimStart()),this.config.strip_right&&(t=t.trimEnd())),t}},fv=dv,hv=class extends Rr{normalize(t){return y0(t)}},pv=hv,mv=class extends Rr{normalize(t){return t.toLowerCase()}},gv=mv,yv=class extends Rr{normalize(t){return t=this.config.prepend+t,t}},_v=yv;function bv(t){if(t===null)return null;switch(t.type){case"BertNormalizer":return new KS(t);case"Precompiled":return new QS(t);case"Sequence":return new ZS(t);case"Replace":return new tv(t);case"NFC":return new ov(t);case"NFD":return new iv(t);case"NFKC":return new uv(t);case"NFKD":return new lv(t);case"Strip":return new fv(t);case"StripAccents":return new pv(t);case"Lowercase":return new gv(t);case"Prepend":return new _v(t);default:throw new Error(`Unknown Normalizer type: ${t.type}`)}}var _0=bv,Cv=class extends gs{pre_tokenize(t,e){return(Array.isArray(t)?t.map(r=>this.pre_tokenize_text(r,e)):this.pre_tokenize_text(t,e)).flat()}_call(t,e){return this.pre_tokenize(t,e)}},lr=Cv,wv=class extends lr{constructor(t){super(),this.config=t,this.add_prefix_space=this.config.add_prefix_space??!1,this.trim_offsets=this.config.trim_offsets??!1,this.use_regex=this.config.use_regex??!0,this.pattern=new RegExp("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+","gu"),this.byte_encoder=g0,this.text_encoder=new TextEncoder}pre_tokenize_text(t,e){return this.add_prefix_space&&!t.startsWith(" ")&&(t=" "+t),(this.use_regex?t.match(this.pattern)||[]:[t]).map(r=>Array.from(this.text_encoder.encode(r),n=>this.byte_encoder[n]).join(""))}},Ev=wv,Av=class extends lr{pre_tokenize_text(t,e){return t.match(/\w+|[^\w\s]+/g)||[]}},kv=Av,Sv=class extends lr{constructor(t){super(),this.replacement=t.replacement??"\u2581",this.str_rep=t.str_rep||this.replacement,this.prepend_scheme=t.prepend_scheme??"always"}pre_tokenize_text(t,e){let{section_index:r=void 0}=e??{},n=t.replaceAll(" ",this.str_rep);return!n.startsWith(this.replacement)&&(this.prepend_scheme==="always"||this.prepend_scheme==="first"&&r===0)&&(n=this.str_rep+n),[n]}},vv=Sv,Tv=class extends lr{constructor(t){super(),this.config=t,this.pattern=Qa(this.config.pattern??{},this.config.invert??!0)}pre_tokenize_text(t){return this.pattern===null?[]:this.config.invert?t.match(this.pattern)||[]:this.config.behavior?.toLowerCase()==="removed"?t.split(this.pattern).filter(e=>e):HS(t,this.pattern)}},Dv=Tv,Rv=class extends lr{constructor(t){super(),this.config=t,this.pattern=new RegExp(`[^${ms}]+|[${ms}]+`,"gu")}pre_tokenize_text(t){return t.match(this.pattern)||[]}},xv=Rv,Ov=class extends lr{constructor(t){super(),this.config=t;let e=`[^\\d]+|\\d${this.config.individual_digits?"":"+"}`;this.pattern=new RegExp(e,"gu")}pre_tokenize_text(t){return t.match(this.pattern)||[]}},Fv=Ov,Pv=class extends lr{constructor(){super(),this.pattern=new RegExp(`[^\\s${ms}]+|[${ms}]`,"gu")}pre_tokenize_text(t,e){return t.trim().match(this.pattern)||[]}},Iv=Pv,Bv=class extends lr{constructor(t){super(),this.config=t,this.pattern=Qa(this.config.pattern??{}),this.content=this.config.content??""}pre_tokenize_text(t){return this.pattern===null?[t]:[t.replaceAll(this.pattern,this.config.content??"")]}},Nv=Bv,Mv=class extends lr{constructor(t){super(),this.tokenizers=(t.pretokenizers??[]).map(e=>b0(e))}pre_tokenize_text(t,e){return this.tokenizers.reduce((r,n)=>n?n.pre_tokenize(r,e):r,[t])}},jv=Mv,Lv=class extends lr{pre_tokenize_text(t){return WS(t)}},Uv=Lv;function $v(t){if(t===null)return null;switch(t.type){case"BertPreTokenizer":return new Iv;case"Sequence":return new jv(t);case"Whitespace":return new kv;case"WhitespaceSplit":return new Uv;case"Metaspace":return new vv(t);case"ByteLevel":return new Ev(t);case"Split":return new Dv(t);case"Punctuation":return new xv(t);case"Digits":return new Fv(t);case"Replace":return new Nv(t);default:throw new Error(`Unknown PreTokenizer type: ${t.type}`)}}var b0=$v,qv=class extends gs{constructor(t){super(),this.config=t,this.vocab=[],this.tokens_to_ids=new Map,this.unk_token_id=void 0,this.unk_token=void 0,this.end_of_word_suffix=void 0,this.fuse_unk=this.config.fuse_unk??!1}_call(t){let e=this.encode(t);return this.fuse_unk&&(e=LS(e,this.tokens_to_ids,this.unk_token_id)),e}convert_tokens_to_ids(t){return t.map(e=>this.tokens_to_ids.get(e)??this.unk_token_id)}convert_ids_to_tokens(t){return t.map(e=>this.vocab[Number(e)]??this.unk_token)}},Za=qv,zv=class extends Za{constructor(t){super(t),this.max_input_chars_per_word=100,this.tokens_to_ids=Md(t.vocab),this.unk_token_id=this.tokens_to_ids.get(t.unk_token),this.unk_token=t.unk_token,this.max_input_chars_per_word=t.max_input_chars_per_word??100,this.vocab=new Array(this.tokens_to_ids.size);for(let[e,r]of this.tokens_to_ids)this.vocab[r]=e}encode(t){let e=[];for(let r of t){let n=[...r];if(n.length>this.max_input_chars_per_word){e.push(this.unk_token);continue}let o=!1,s=0,a=[];for(;s<n.length;){let c=n.length,l=null;for(;s<c;){let d=n.slice(s,c).join("");if(s>0&&(d=this.config.continuing_subword_prefix+d),this.tokens_to_ids.has(d)){l=d;break}--c}if(l===null){o=!0;break}a.push(l),s=c}o?e.push(this.unk_token):e.push(...a)}return e}},f0=zv,h0=class C0{constructor(e,r){this.is_leaf=e,this.children=r}static default(){return new C0(!1,new Map)}},Hv=class{constructor(){this.root=h0.default()}extend(t){for(let e of t)this.push(e)}push(t){let e=this.root;for(let r of t){let n=e.children.get(r);n===void 0&&(n=h0.default(),e.children.set(r,n)),e=n}e.is_leaf=!0}*common_prefix_search(t){let e=this.root;if(e===void 0)return;let r="";for(let n of t){if(r+=n,e=e.children.get(n),e===void 0)return;e.is_leaf&&(yield r)}}},Wv=Hv,Bd=class w0{constructor(e,r,n,o,s){this.token_id=e,this.node_id=r,this.pos=n,this.length=o,this.score=s,this.prev=null,this.backtrace_score=0}clone(){let e=new w0(this.token_id,this.node_id,this.pos,this.length,this.score);return e.prev=this.prev,e.backtrace_score=this.backtrace_score,e}},Gv=class{constructor(t,e,r){this.chars=Array.from(t),this.len=this.chars.length,this.bos_token_id=e,this.eos_token_id=r,this.nodes=[],this.begin_nodes=Array.from({length:this.len+1},()=>[]),this.end_nodes=Array.from({length:this.len+1},()=>[]);let n=new Bd(this.bos_token_id??0,0,0,0,0),o=new Bd(this.eos_token_id??0,1,this.len,0,0);this.nodes.push(n.clone()),this.nodes.push(o.clone()),this.begin_nodes[this.len].push(o),this.end_nodes[0].push(n)}insert(t,e,r,n){let o=this.nodes.length,s=new Bd(n,o,t,e,r);this.begin_nodes[t].push(s),this.end_nodes[t+e].push(s),this.nodes.push(s)}viterbi(){let t=this.len,e=0;for(;e<=t;){if(this.begin_nodes[e].length==0)return[];for(let s of this.begin_nodes[e]){s.prev=null;let a=0,c=null;for(let l of this.end_nodes[e]){let d=l.backtrace_score+s.score;(c===null||d>a)&&(c=l.clone(),a=d)}if(c!==null)s.prev=c,s.backtrace_score=a;else return[]}++e}let r=[],n=this.begin_nodes[t][0].prev;if(n===null)return[];let o=n.clone();for(;o.prev!==null;)r.push(o.clone()),o=o.clone().prev.clone();return r.reverse(),r}piece(t){return this.chars.slice(t.pos,t.pos+t.length).join("")}tokens(){return this.viterbi().map(t=>this.piece(t))}token_ids(){return this.viterbi().map(t=>t.token_id)}},Jv=Gv;function Vv(t){if(t.length===0)throw new Error("Array must not be empty");let e=t[0],r=0;for(let n=1;n<t.length;++n)t[n]<e&&(e=t[n],r=n);return[e,r]}var Kv=class extends Za{constructor(t,e){super(t);let r=t.vocab.length;this.vocab=new Array(r),this.scores=new Array(r);for(let n=0;n<r;++n)[this.vocab[n],this.scores[n]]=t.vocab[n];this.unk_token_id=t.unk_id,this.unk_token=this.vocab[t.unk_id],this.tokens_to_ids=new Map(this.vocab.map((n,o)=>[n,o])),this.bos_token=" ",this.bos_token_id=this.tokens_to_ids.get(this.bos_token),this.eos_token=e,this.eos_token_id=this.tokens_to_ids.get(this.eos_token),this.unk_token=this.vocab[this.unk_token_id],this.min_score=Vv(this.scores)[0],this.unk_score=this.min_score-10,this.scores[this.unk_token_id]=this.unk_score,this.trie=new Wv,this.trie.extend(this.vocab),this.fuse_unk=!0}populate_nodes(t){let e=t.chars,r=1,n=0;for(;n<e.length;){let o=!1,s=[],a=e.slice(n).join(""),c=this.trie.common_prefix_search(a);for(let l of c){s.push(l);let d=this.tokens_to_ids.get(l),f=this.scores[d],p=qS(l);t.insert(n,p,f,d),!o&&p===r&&(o=!0)}o||t.insert(n,r,this.unk_score,this.unk_token_id),n+=r}}tokenize(t){let e=new Jv(t,this.bos_token_id,this.eos_token_id);return this.populate_nodes(e),e.tokens()}encode(t){let e=[];for(let r of t){let n=this.tokenize(r);e.push(...n)}return e}},p0=Kv,Yv=class{constructor(t=(r,n)=>r>n,e=1/0){this._heap=[],this._comparator=t,this._max_size=e}get size(){return this._heap.length}is_empty(){return this.size===0}peek(){return this._heap[0]}push(...t){return this.extend(t)}extend(t){for(let e of t)if(this.size<this._max_size)this._heap.push(e),this._sift_up();else{let r=this._smallest();this._comparator(e,this._heap[r])&&(this._heap[r]=e,this._sift_up_from(r))}return this.size}pop(){let t=this.peek(),e=this.size-1;return e>0&&this._swap(0,e),this._heap.pop(),this._sift_down(),t}replace(t){let e=this.peek();return this._heap[0]=t,this._sift_down(),e}_parent(t){return(t+1>>>1)-1}_left(t){return(t<<1)+1}_right(t){return t+1<<1}_greater(t,e){return this._comparator(this._heap[t],this._heap[e])}_swap(t,e){let r=this._heap[t];this._heap[t]=this._heap[e],this._heap[e]=r}_sift_up(){this._sift_up_from(this.size-1)}_sift_up_from(t){for(;t>0&&this._greater(t,this._parent(t));)this._swap(t,this._parent(t)),t=this._parent(t)}_sift_down(){let t=0;for(;this._left(t)<this.size&&this._greater(this._left(t),t)||this._right(t)<this.size&&this._greater(this._right(t),t);){let e=this._right(t)<this.size&&this._greater(this._right(t),this._left(t))?this._right(t):this._left(t);this._swap(t,e),t=e}}_smallest(){return 2**Math.floor(Math.log2(this.size))-1}},Qv=Yv,Xv=class{constructor(t){this.capacity=t,this.cache=new Map}get(t){if(!this.cache.has(t))return;let e=this.cache.get(t);return this.cache.delete(t),this.cache.set(t,e),e}put(t,e){this.cache.has(t)&&this.cache.delete(t),this.cache.set(t,e),this.cache.size>this.capacity&&this.cache.delete(this.cache.keys().next().value)}clear(){this.cache.clear()}},Zv=Xv,eT=class extends Za{constructor(t){super(t),this.tokens_to_ids=Md(t.vocab),this.unk_token_id=this.tokens_to_ids.get(t.unk_token),this.unk_token=t.unk_token,this.vocab=new Array(this.tokens_to_ids.size);for(let[r,n]of this.tokens_to_ids)this.vocab[n]=r;let e=Array.isArray(t.merges[0]);this.merges=e?t.merges:t.merges.map(r=>r.split(" ",2)),this.bpe_ranks=new Map(this.merges.map((r,n)=>[JSON.stringify(r),n])),this.end_of_word_suffix=t.end_of_word_suffix,this.continuing_subword_suffix=t.continuing_subword_suffix??null,this.byte_fallback=this.config.byte_fallback??!1,this.byte_fallback&&(this.text_encoder=new TextEncoder),this.ignore_merges=this.config.ignore_merges??!1,this.max_length_to_cache=256,this.cache_capacity=1e4,this.cache=new Zv(this.cache_capacity)}clear_cache(){this.cache.clear()}bpe(t){if(t.length===0)return[];let e=this.cache.get(t);if(e!==void 0)return e;let r=Array.from(t);this.end_of_word_suffix&&(r[r.length-1]+=this.end_of_word_suffix);let n=[];if(r.length>1){let o=new Qv((c,l)=>c.score<l.score),s={token:r[0],bias:0,prev:null,next:null},a=s;for(let c=1;c<r.length;++c){let l={bias:c/r.length,token:r[c],prev:a,next:null};a.next=l,this.add_node(o,a),a=l}for(;!o.is_empty();){let c=o.pop();if(c.deleted||!c.next||c.next.deleted)continue;if(c.deleted=!0,c.next.deleted=!0,c.prev){let d={...c.prev};c.prev.deleted=!0,c.prev=d,d.prev?d.prev.next=d:s=d}let l={token:c.token+c.next.token,bias:c.bias,prev:c.prev,next:c.next.next};l.prev?(l.prev.next=l,this.add_node(o,l.prev)):s=l,l.next&&(l.next.prev=l,this.add_node(o,l))}for(let c=s;c!==null;c=c.next)n.push(c.token)}else n=r;if(this.continuing_subword_suffix)for(let o=0;o<n.length-1;++o)n[o]+=this.continuing_subword_suffix;return t.length<this.max_length_to_cache&&this.cache.put(t,n),n}add_node(t,e){let r=this.bpe_ranks.get(JSON.stringify([e.token,e.next.token]));r!==void 0&&(e.score=r+e.bias,t.push(e))}encode(t){let e=[];for(let r of t){if(this.ignore_merges&&this.tokens_to_ids.has(r)){e.push(r);continue}let n=this.bpe(r);for(let o of n)if(this.tokens_to_ids.has(o))e.push(o);else if(this.byte_fallback){let s=Array.from(this.text_encoder.encode(o)).map(a=>`<0x${a.toString(16).toUpperCase().padStart(2,"0")}>`);s.every(a=>this.tokens_to_ids.has(a))?e.push(...s):e.push(this.unk_token)}else e.push(this.unk_token)}return e}},m0=eT,tT=class extends Za{constructor(t,e){super(t);let r=t.vocab;this.tokens_to_ids=Md(e.target_lang?r[e.target_lang]:r),this.bos_token=e.bos_token,this.bos_token_id=this.tokens_to_ids.get(this.bos_token),this.eos_token=e.eos_token,this.eos_token_id=this.tokens_to_ids.get(this.eos_token),this.pad_token=e.pad_token,this.pad_token_id=this.tokens_to_ids.get(this.pad_token),this.unk_token=e.unk_token,this.unk_token_id=this.tokens_to_ids.get(this.unk_token),this.vocab=new Array(this.tokens_to_ids.size);for(let[n,o]of this.tokens_to_ids)this.vocab[o]=n}encode(t){return t}},rT=tT;function nT(t,e){switch(t.type){case"WordPiece":return new f0(t);case"Unigram":return new p0(t,e.eos_token);case"BPE":return new m0(t);default:if(t.vocab)return Array.isArray(t.vocab)?new p0(t,e.eos_token):Object.hasOwn(t,"continuing_subword_prefix")&&Object.hasOwn(t,"unk_token")?Object.hasOwn(t,"merges")?new m0(t):new f0(t):new rT(t,{target_lang:e.target_lang,bos_token:e.bos_token,eos_token:e.eos_token,pad_token:e.pad_token,unk_token:e.unk_token});throw new Error(`Unknown TokenizerModel type: ${t?.type}`)}}var oT=nT,sT=class extends gs{constructor(t){super(),this.config=t}_call(t,...e){return this.post_process(t,...e)}},ys=sT,iT=class extends ys{post_process(t,e=null,r=!0){let n=e===null?this.config.single:this.config.pair,o=[],s=[];for(let a of n)"SpecialToken"in a?r&&(o.push(a.SpecialToken.id),s.push(a.SpecialToken.type_id)):"Sequence"in a&&(a.Sequence.id==="A"?(o=Bt(o,t),s=Bt(s,new Array(t.length).fill(a.Sequence.type_id))):a.Sequence.id==="B"&&(o=Bt(o,e),s=Bt(s,new Array(e.length).fill(a.Sequence.type_id))));return{tokens:o,token_type_ids:s}}},aT=iT,uT=class extends ys{post_process(t,e=null){return{tokens:e?Bt(t,e):t}}},cT=uT,lT=class extends ys{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e=null,r=!0){r&&(t=Bt([this.cls[0]],t,[this.sep[0]]));let n=new Array(t.length).fill(0);if(e){let o=[],s=r?[this.sep[0]]:[];t=Bt(t,o,e,s),n=Bt(n,new Array(e.length+o.length+s.length).fill(1))}return{tokens:t,token_type_ids:n}}},dT=lT,fT=class extends ys{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e,r=!0){r&&(t=Bt([this.cls[0]],t,[this.sep[0]]));let n=new Array(t.length).fill(0);if(e){let o=r?[this.sep[0]]:[],s=r?[this.sep[0]]:[];t=Bt(t,o,e,s),n=Bt(n,new Array(e.length+o.length+s.length).fill(1))}return{tokens:t,token_type_ids:n}}},hT=fT,pT=class extends ys{constructor(t){super(t),this.processors=(t.processors??[]).map(e=>E0(e))}post_process(t,e=null,r=!0){let n={tokens:t};for(let o of this.processors)n=o.post_process(n.tokens,e,r);return n}},mT=pT;function gT(t){if(t===null)return null;switch(t.type){case"TemplateProcessing":return new aT(t);case"ByteLevel":return new cT(t);case"BertProcessing":return new dT(t);case"RobertaProcessing":return new hT(t);case"Sequence":return new mT(t);default:throw new Error(`Unknown PostProcessor type: ${t.type}`)}}var E0=gT,yT=class extends gs{constructor(t){super(),this.config=t,this.added_tokens=[],this.end_of_word_suffix=null,this.trim_offsets="trim_offsets"in t?t.trim_offsets:!1}_call(t){return this.decode(t)}decode(t){return this.decode_chain(t).join("")}},dr=yT,_T=class extends dr{constructor(t){super(t),this.byte_decoder=NS,this.text_decoder=new TextDecoder("utf-8",{fatal:!1,ignoreBOM:!0}),this.end_of_word_suffix=null}convert_tokens_to_string(t){let e=t.join(""),r=new Uint8Array([...e].map(n=>this.byte_decoder[n]));return this.text_decoder.decode(r)}decode_chain(t){let e=[],r=[];for(let n of t)this.added_tokens.find(o=>o.content===n)!==void 0?(r.length>0&&(e.push(this.convert_tokens_to_string(r)),r=[]),e.push(n)):r.push(n);return r.length>0&&e.push(this.convert_tokens_to_string(r)),e}},bT=_T,CT=class extends dr{constructor(t){super(t),this.cleanup=t.cleanup}decode_chain(t){return t.map((e,r)=>{if(r!==0){let n=this.config.prefix;n&&e.startsWith(n)?e=e.replace(n,""):e=" "+e}return this.cleanup&&(e=Nd(e)),e})}},wT=CT,ET=class extends dr{constructor(t){super(t),this.replacement=t.replacement??"\u2581"}decode_chain(t){let e=[];for(let r=0;r<t.length;++r){let n=t[r].replaceAll(this.replacement," ");r==0&&n.startsWith(" ")&&(n=n.substring(1)),e.push(n)}return e}},AT=ET,kT=class extends dr{constructor(t){super(t),this.suffix=t.suffix??""}decode_chain(t){return t.map((e,r)=>e.replaceAll(this.suffix,r===t.length-1?"":" "))}},ST=kT,vT=class extends dr{constructor(t){super(t),this.pad_token=t.pad_token??"",this.word_delimiter_token=t.word_delimiter_token??"",this.cleanup=t.cleanup}convert_tokens_to_string(t){if(t.length===0)return"";let e=[t[0]];for(let n=1;n<t.length;++n)t[n]!==e.at(-1)&&e.push(t[n]);let r=e.filter(n=>n!==this.pad_token).join("");return this.cleanup&&(r=Nd(r).replaceAll(this.word_delimiter_token," ").trim()),r}decode_chain(t){return[this.convert_tokens_to_string(t)]}},TT=vT,DT=class extends dr{constructor(t){super(t),this.decoders=(t.decoders??[]).map(e=>A0(e))}decode_chain(t){return this.decoders.reduce((e,r)=>r.decode_chain(e),t)}},RT=DT,xT=class extends dr{decode_chain(t){let e=Qa(this.config.pattern),r=this.config.content??"";return e===null?t:t.map(n=>n.replaceAll(e,r))}},OT=xT,FT=class extends dr{decode_chain(t){return[t.join("")]}},PT=FT,IT=class extends dr{constructor(t){super(t),this.content=t.content??"",this.start=t.start??0,this.stop=t.stop??0}decode_chain(t){return t.map(e=>{let r=0;for(let o=0;o<this.start&&e[o]===this.content;++o)r=o+1;let n=e.length;for(let o=0;o<this.stop;++o){let s=e.length-o-1;if(e[s]===this.content){n=s;continue}else break}return e.slice(r,n)})}},BT=IT,NT=class extends dr{constructor(t){super(t),this.text_decoder=new TextDecoder}decode_chain(t){let e=[],r=[];for(let n of t){let o=null;if(n.length===6&&n.startsWith("<0x")&&n.endsWith(">")){let s=parseInt(n.slice(3,5),16);isNaN(s)||(o=s)}if(o!==null)r.push(o);else{if(r.length>0){let s=this.text_decoder.decode(Uint8Array.from(r));e.push(s),r=[]}e.push(n)}}if(r.length>0){let n=this.text_decoder.decode(Uint8Array.from(r));e.push(n),r=[]}return e}},MT=NT;function jT(t){if(t===null)return null;switch(t.type){case"ByteLevel":return new bT(t);case"WordPiece":return new wT(t);case"Metaspace":return new AT(t);case"BPEDecoder":return new ST(t);case"CTC":return new TT(t);case"Sequence":return new RT(t);case"Replace":return new OT(t);case"Fuse":return new PT(t);case"Strip":return new BT(t);case"ByteFallback":return new MT(t);default:throw new Error(`Unknown Decoder type: ${t.type}`)}}var A0=jT,LT=class{constructor(t,e){let r=d0(t,"Tokenizer",["model","decoder","post_processor","pre_tokenizer","normalizer"]);if(r)throw new Error(r);let n=d0(e,"Config");if(n)throw new Error(n);this.tokenizer=t,this.config=e,this.normalizer=_0(this.tokenizer.normalizer),this.pre_tokenizer=b0(this.tokenizer.pre_tokenizer),this.model=oT(this.tokenizer.model,this.config),this.post_processor=E0(this.tokenizer.post_processor),this.decoder=A0(this.tokenizer.decoder),this.special_tokens=[],this.all_special_ids=[],this.added_tokens=[],this.tokenizer.added_tokens.forEach(o=>{let s=new IS(o);this.added_tokens.push(s),this.model.tokens_to_ids.set(s.content,s.id),this.model.vocab[s.id]=s.content,s.special&&(this.special_tokens.push(s.content),this.all_special_ids.push(s.id))}),(this.config.additional_special_tokens??[]).forEach(o=>{this.special_tokens.includes(o)||this.special_tokens.push(o)}),this.decoder&&(this.decoder.added_tokens=this.added_tokens,this.decoder.end_of_word_suffix=this.model.end_of_word_suffix),this.added_tokens_splitter=new FS(this.added_tokens.map(o=>o.content)),this.added_tokens_map=new Map(this.added_tokens.map(o=>[o.content,o])),this.remove_space=this.config.remove_space,this.clean_up_tokenization_spaces=this.config.clean_up_tokenization_spaces??!0,this.do_lowercase_and_remove_accent=this.config.do_lowercase_and_remove_accent??!1}encode(t,{text_pair:e=null,add_special_tokens:r=!0,return_token_type_ids:n=null}={}){let{tokens:o,token_type_ids:s}=this.tokenize_helper(t,{text_pair:e,add_special_tokens:r}),a=this.model.convert_tokens_to_ids(o),c={ids:a,tokens:o,attention_mask:new Array(a.length).fill(1)};return n&&s&&(c.token_type_ids=s),c}decode(t,e={}){if(!Array.isArray(t)||t.length===0||!$S(t[0]))throw Error("token_ids must be a non-empty array of integers.");let r=this.model.convert_ids_to_tokens(t);e.skip_special_tokens&&(r=r.filter(o=>!this.special_tokens.includes(o)));let n=this.decoder?this.decoder(r):r.join(" ");return this.decoder&&this.decoder.end_of_word_suffix&&(n=n.replaceAll(this.decoder.end_of_word_suffix," "),e.skip_special_tokens&&(n=n.trim())),(e.clean_up_tokenization_spaces??this.clean_up_tokenization_spaces)&&(n=Nd(n)),n}tokenize(t,{text_pair:e=null,add_special_tokens:r=!1}={}){return this.tokenize_helper(t,{text_pair:e,add_special_tokens:r}).tokens}encode_text(t){if(t===null)return null;let e=this.added_tokens_splitter.split(t);return e.forEach((r,n)=>{let o=this.added_tokens_map.get(r);o&&(o.lstrip&&n>0&&(e[n-1]=e[n-1].trimEnd()),o.rstrip&&n<e.length-1&&(e[n+1]=e[n+1].trimStart()))}),e.flatMap((r,n)=>{if(r.length===0)return[];if(this.added_tokens_map.has(r))return[r];if(this.remove_space===!0&&(r=r.trim().split(/\s+/).join(" ")),this.do_lowercase_and_remove_accent&&(r=zS(r)),this.normalizer!==null&&(r=this.normalizer(r)),r.length===0)return[];let o=this.pre_tokenizer!==null?this.pre_tokenizer(r,{section_index:n}):[r];return this.model(o)})}tokenize_helper(t,{text_pair:e=null,add_special_tokens:r=!0}){let n=this.encode_text(t),o=this.encode_text(e||null);return this.post_processor?this.post_processor(n,o,r):{tokens:Bt(n??[],o??[])}}token_to_id(t){return this.model.tokens_to_ids.get(t)}id_to_token(t){return this.model.vocab[t]}get_added_tokens_decoder(){let t=new Map;for(let e of this.added_tokens)t.set(e.id,e);return t}},k0=LT;var eu=class{type="huggingface";name;modelId;logger;options;tokenizer=null;cacheDir;safeModelName;constructor(e,r,n={}){this.modelId=e,this.logger=r,this.options=n,this.cacheDir=n.cacheDir||(0,_s.join)((0,S0.homedir)(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=(0,_s.join)(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:(0,_s.join)(e,"tokenizer.json"),tokenizerConfig:(0,_s.join)(e,"tokenizer_config.json")}}ensureDir(e){(0,yo.existsSync)(e)||(0,yo.mkdirSync)(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!(0,yo.existsSync)(e.tokenizerJson)||!(0,yo.existsSync)(e.tokenizerConfig))return null;let[r,n]=await Promise.all([bs.promises.readFile(e.tokenizerJson,"utf-8"),bs.promises.readFile(e.tokenizerConfig,"utf-8")]);return{tokenizerJson:JSON.parse(r),tokenizerConfig:JSON.parse(n)}}catch(e){return this.logger?.warn(`Failed to load from cache: ${e.message}`),null}}async downloadAndCache(){let e=this.getCachePaths(),r={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 n=new AbortController,o=setTimeout(()=>n.abort(),this.options.timeout||3e4);try{let[s,a]=await Promise.all([fetch(r.json,{signal:n.signal}),fetch(r.config,{signal:n.signal})]);if(!s.ok)throw new Error(`Failed to fetch tokenizer.json: ${s.statusText}`);let[c,l]=await Promise.all([s.json(),a.ok?a.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([bs.promises.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),bs.promises.writeFile(e.tokenizerConfig,JSON.stringify(l,null,2))]),{tokenizerJson:c,tokenizerConfig:l}}finally{clearTimeout(o)}}async initialize(){try{this.logger?.info(`Initializing HuggingFace tokenizer: ${this.modelId}`);let e=this.getCachePaths();this.ensureDir(this.cacheDir);let r=await this.loadFromCache()||await this.downloadAndCache();this.tokenizer=new k0(r.tokenizerJson,r.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 r=this.extractTextFromRequest(e);return this.tokenizer.encode(r).ids.length}catch(r){throw this.logger?.error(`Error counting tokens: ${r.message}`),r}}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 r=[],{messages:n,system:o,tools:s}=e;if(Array.isArray(n)){for(let a of n)if(typeof a.content=="string")r.push(a.content);else if(Array.isArray(a.content))for(let c of a.content)c.type==="text"&&c.text?r.push(c.text):c.type==="tool_use"&&c.input?r.push(JSON.stringify(c.input)):c.type==="tool_result"&&r.push(typeof c.content=="string"?c.content:JSON.stringify(c.content))}if(typeof o=="string")r.push(o);else if(Array.isArray(o)){for(let a of o)if(a.type==="text"){if(typeof a.text=="string")r.push(a.text);else if(Array.isArray(a.text))for(let c of a.text)c&&r.push(c)}}if(s)for(let a of s)a.name&&r.push(a.name),a.description&&r.push(a.description),a.input_schema&&r.push(JSON.stringify(a.input_schema));return r.join(" ")}};var tu=class{type="api";name;config;logger;options;constructor(e,r,n={}){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=r,this.options=n;try{let o=new URL(e.url);this.name=`api-${o.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 r=this.formatRequestBody(e),n={"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`,...this.config.headers},o=new AbortController,s=setTimeout(()=>o.abort(),this.options.timeout||3e4),a=await fetch(this.config.url,{method:"POST",headers:n,body:JSON.stringify(r),signal:o.signal});if(clearTimeout(s),!a.ok)throw new Error(`API tokenizer request failed: ${a.status} ${a.statusText}`);let c=await a.json();return this.extractTokenCount(c)}catch(r){throw r.name==="AbortError"?new Error("API tokenizer request timed out"):r}}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(r=>({role:r.role,content:this.extractTextFromMessage(r)})):[]}extractTextFromMessage(e){return typeof e.content=="string"?e.content:Array.isArray(e.content)?e.content.map(r=>r.type==="text"&&r.text?r.text:r.type==="tool_use"&&r.input?JSON.stringify(r.input):r.type==="tool_result"?typeof r.content=="string"?r.content:JSON.stringify(r.content):"").join(" "):""}extractConcatenatedText(e){let r=[];return e.messages&&e.messages.forEach(n=>{r.push(this.extractTextFromMessage(n))}),typeof e.system=="string"?r.push(e.system):Array.isArray(e.system)&&e.system.forEach(n=>{n.type==="text"&&(typeof n.text=="string"?r.push(n.text):Array.isArray(n.text)&&n.text.forEach(o=>{o&&r.push(o)}))}),e.tools&&e.tools.forEach(n=>{n.name&&r.push(n.name),n.description&&r.push(n.description),n.input_schema&&r.push(JSON.stringify(n.input_schema))}),r.join(" ")}extractTokenCount(e){try{let r=this.config.responseField,n=r.split("."),o=e;for(let s of n){if(o==null)throw new Error(`Field path '${r}' not found in response`);o=o[s]}if(typeof o!="number")throw new Error(`Expected number at field path '${r}', got ${typeof o}`);return o}catch(r){throw this.logger?.error(`Failed to extract token count from API response: ${r.message}. Response: ${JSON.stringify(e)}`),new Error(`Invalid response from API tokenizer: ${r.message}`)}}};var Sn=class{tokenizers=new Map;configService;logger;options;fallbackTokenizer;constructor(e,r,n={}){this.configService=e,this.logger=r,this.options={timeout:n.timeout??3e4,...n}}async initialize(){try{this.fallbackTokenizer=new ps("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){let r=this.getCacheKey(e);if(this.tokenizers.has(r))return this.tokenizers.get(r);let n;try{switch(e.type){case"tiktoken":n=new ps(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),n=new eu(e.model,this.logger,{timeout:this.options.timeout});break;case"api":n=new tu(e,this.logger,{timeout:this.options.timeout});break;default:throw new Error(`Unknown tokenizer type: ${e.type}`)}return this.logger?.info(`Calling initialize() on ${e.type} tokenizer...`),await n.initialize(),this.tokenizers.set(r,n),this.logger?.info(`Tokenizer initialized successfully: ${e.type} (${r})`),n}catch(o){return this.logger?.error(`Failed to initialize ${e.type} tokenizer: ${o.message}`),this.logger?.error(`Error stack: ${o.stack}`),this.fallbackTokenizer||await this.initialize(),this.fallbackTokenizer}}async countTokens(e,r){let n=r?await this.getTokenizer(r):this.fallbackTokenizer;return{tokenCount:await n.countTokens(e),tokenizerUsed:n.name,cached:!1}}getTokenizerConfigForModel(e,r){let o=(this.configService.get("providers")||[]).find(s=>s.name===e);if(o?.tokenizer)return o.tokenizer.models?.[r]?o.tokenizer.models[r]:o.tokenizer.default}dispose(){this.tokenizers.forEach(e=>{try{e.dispose()}catch(r){this.logger?.error(`Error disposing tokenizer: ${r}`)}}),this.tokenizers.clear()}getCacheKey(e){switch(e.type){case"tiktoken":return`tiktoken:${e.encoding||"cl100k_base"}`;case"huggingface":return`hf:${e.model}`;case"api":return`api:${e.url}`;default:return`unknown:${JSON.stringify(e)}`}}};var R0=require("tiktoken");var jd=class{capacity;cache;constructor(e){this.capacity=e,this.cache=new Map}get(e){if(!this.cache.has(e))return;let r=this.cache.get(e);return this.cache.delete(e),this.cache.set(e,r),r}put(e,r){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.capacity){let n=this.cache.keys().next().value;n!==void 0&&this.cache.delete(n)}this.cache.set(e,r)}values(){return Array.from(this.cache.values())}},ru=new jd(100);var su=require("fs/promises"),uu=require("fs/promises"),iu=require("path"),_o=require("@wengine-ai/claude-code-router-shared");$n();var Ld=new Map;function nu(t,e){t&&Ld.set(t,{...e,provider:t,capturedAt:Date.now()})}function ou(t){let e=Ld.get(t);return e?{...e}:void 0}function v0(){return Array.from(Ld.values()).map(t=>({...t}))}ii();var xr=(0,R0.get_encoding)("cl100k_base"),$d=(t,e,r)=>{let n=0;return Array.isArray(t)&&t.forEach(o=>{typeof o.content=="string"?n+=xr.encode(o.content).length:Array.isArray(o.content)&&o.content.forEach(s=>{s.type==="text"?n+=xr.encode(s.text).length:s.type==="tool_use"?n+=xr.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(n+=xr.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?n+=xr.encode(e).length:Array.isArray(e)&&e.forEach(o=>{o.type==="text"&&(typeof o.text=="string"?n+=xr.encode(o.text).length:Array.isArray(o.text)&&o.text.forEach(s=>{n+=xr.encode(s||"").length}))}),r&&r.forEach(o=>{o.description&&(n+=xr.encode(o.name+o.description).length),o.input_schema&&(n+=xr.encode(JSON.stringify(o.input_schema)).length)}),n},x0=async(t,e)=>{if(t.sessionId){let r=await qd(t.sessionId);if(r){let n=(0,iu.join)(_o.HOME_DIR,r,"config.json"),o=(0,iu.join)(_o.HOME_DIR,r,`${t.sessionId}.json`);try{let s=JSON.parse(await(0,su.readFile)(o,"utf8"));if(s&&s.Router)return s.Router}catch{}try{let s=JSON.parse(await(0,su.readFile)(n,"utf8"));if(s&&s.Router)return s.Router}catch{}}}};function au(t){let e=t||"";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 O0(t){let e=au(t),r=e.includes("[1m]")||e.endsWith("[1m"),n=e.replace(/\[1m\]|\[1m$/g,""),o=n.match(/^ccr-(opus|sonnet|haiku)$/i);if(o)return{family:o[1].toLowerCase(),extended:r};let s=n.match(/claude-(?:\d+-\d+-|\d+-)?(sonnet|opus|haiku)(?:-|$)/i)||n.match(/claude-(sonnet|opus|haiku)(?:-|$)/i);return s?{family:s[1].toLowerCase(),extended:r}:{family:null,extended:r}}function UT(t,e){if(!e||!t)return null;let r=au(t);if(e[t])return e[t];if(e[r])return e[r];let{family:n}=O0(t);if(n&&e[n])return e[n];for(let[o,s]of Object.entries(e)){let a=au(o);if(a&&r.includes(a))return s}return null}function F0(t){return t?(t.input_tokens||0)+(t.cache_read_input_tokens||0)+(t.cache_creation_input_tokens||0):0}function T0(t){if(!t?.includes(","))return null;let[e,...r]=t.split(","),n=e.trim(),o=r.join(",").trim();return!n||!o?null:{providerName:n,routeModel:o}}function ot(t,e,r,n){let o=T0(t);if(!o)return t;let{providerName:s,routeModel:a}=o;if(n&&!r){let f=Fo(),p=f.getPromotion(s,a,n,e);if(p){let y=T0(p);if(y){let A=e.find(_=>_.name.toLowerCase()===y.providerName.toLowerCase()),w=A?.models?.find(_=>String(_).toLowerCase()===y.routeModel.toLowerCase());if(A&&w)return`${A.name},${w}`}f.clear(s,a,n)}}if(!r&&!tt().isAvailable(s,a))return null;let c=ou(s);if(c){let f=c.limitDaily!==void 0&&c.usedDailyBalance!==void 0&&c.usedDailyBalance>=c.limitDaily,p=c.totalBalance!==void 0&&c.usedBalance!==void 0&&c.usedBalance>=c.totalBalance;if(f||p)return null}let l=e.find(f=>f.name.toLowerCase()===s.toLowerCase()),d=l?.models?.find(f=>String(f).toLowerCase()===a.toLowerCase());return l&&d?`${l.name},${d}`:t}function Yt(t,e,r,n,o){let s=tt(),a=[r?.[t],n?.[t]];for(let c of a)if(!(!Array.isArray(c)||c.length===0))for(let l of c){let d=ot(l,e);if(d)return d}return null}function $T(t){return t.body.messages?.some(e=>e.role==="user"&&Array.isArray(e.content)&&e.content.some(r=>r.type==="image"||r.type==="image_url"||Array.isArray(r?.content)&&r.content.some(n=>n.type==="image"||n.type==="image_url")))}function qT(t){let e=au(t);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(n=>n.test(e))}function zT(t,e,r,n,o,s,a){let c=r.longContextThreshold||6e4,l=Math.max(e,F0(o)),d=t.modelFamily;if((s||l>2e5)&&r.extendedContext){let A=ot(r.extendedContext,n,!1,"extendedContext");if(A)return t.log.info(`Family: using extended context model (1M+), tokens: ${l}, estimated: ${e}, explicit: ${s}`),{model:A,scenarioType:"extendedContext",isFallback:!1};let w=Yt("extendedContext",n,r.fallback,a,d);if(w)return t.log.info(`Family: using extended context fallback model (1M+), tokens: ${l}, estimated: ${e}, explicit: ${s}`),{model:w,scenarioType:"extendedContext",isFallback:!0};t.log.warn("Family: extendedContext model unavailable (fail pool), skipping")}if(l>c&&(r.longContext||r.fallback?.longContext?.length||a?.longContext?.length)){let A=r.longContext?ot(r.longContext,n,!1,"longContext"):null;if(A)return t.log.info(`Family: using long context model, tokens: ${l}, estimated: ${e}`),{model:A,scenarioType:"longContext",isFallback:!1};let w=Yt("longContext",n,r.fallback,a,d);if(w)return t.log.info(`Family: using long context fallback model, tokens: ${l}, estimated: ${e}`),{model:w,scenarioType:"longContext",isFallback:!0};t.log.warn("Family: no healthy longContext model available, falling through to other scenarios")}if(Array.isArray(t.body.tools)&&t.body.tools.some(A=>A.type?.startsWith("web_search"))&&r.webSearch){let A=ot(r.webSearch,n,!1,"webSearch");if(A)return{model:A,scenarioType:"webSearch",isFallback:!1};let w=Yt("webSearch",n,r.fallback,a,d);if(w)return t.log.info("Family: using webSearch fallback model"),{model:w,scenarioType:"webSearch",isFallback:!0};t.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(t.body.thinking&&r.think){let A=ot(r.think,n,!1,"think");if(A)return{model:A,scenarioType:"think",isFallback:!1};let w=Yt("think",n,r.fallback,a,d);if(w)return t.log.info("Family: using think fallback model"),{model:w,scenarioType:"think",isFallback:!0};t.log.warn("Family: think model unavailable (fail pool), skipping")}if(r.default){let A=ot(r.default,n,!1,"default");if(A)return{model:A,scenarioType:"default",isFallback:!1};let w=Yt("default",n,r.fallback,a,d);if(w)return t.log.info("Family: using default fallback model"),{model:w,scenarioType:"default",isFallback:!0};t.log.warn("Family: default model unavailable (fail pool), skipping")}return null}var HT=async(t,e,r,n)=>{let o=await x0(t,r),s=r.get("providers")||[],a=o||r.get("Router"),c=r.get("fallback");if(t.body.model.includes(",")){let m=ot(t.body.model,s,!1,"default");if(m)return{model:m,scenarioType:"default"};t.log.warn(`Explicit model ${t.body.model} unavailable (fail pool), trying fallback`);let v=Yt("default",s,void 0,c);if(v)return t.log.info(`Using fallback for explicit model: ${v}`),{model:v,scenarioType:"default"};t.log.warn(`No fallback available for explicit model ${t.body.model}, continuing through routing logic`)}let{family:l,extended:d}=O0(t.body.model),f=a?.families?.[l||""];if(f&&a?.enableFamilyRouting){t.log.info(`Using model family routing for: ${l}${d?" (1M)":""}`),t.modelFamily=l,t.familyFallback=f.fallback;let m=zT(t,e,f,s,n,d,c);if(m)return{model:m.model,scenarioType:m.scenarioType}}let p=UT(t.body.model,a?.models);if(p){let m=ot(p,s,!1,"modelMapping");if(m)return t.log.info(`Using mapped model for ${t.body.model}: ${p}`),{model:m,scenarioType:"modelMapping"};t.log.warn(`Mapped model ${p} unavailable (fail pool), skipping`)}let y=Math.max(e,F0(n)),A=a?.extendedContextThreshold||2e5;if(y>A&&a?.extendedContext){t.log.info(`Using extended context (1M) model due to token count: ${y}, estimated: ${e}, threshold: ${A}`);let m=ot(a.extendedContext,s,!1,"extendedContext");if(m)return{model:m,scenarioType:"extendedContext"};t.log.warn(`Extended context model ${a.extendedContext} unavailable (fail pool), trying fallback`);let v=Yt("extendedContext",s,void 0,c);if(v)return{model:v,scenarioType:"extendedContext"}}let w=a?.longContextThreshold||6e4;if(y>w&&a?.longContext){t.log.info(`Using long context model due to token count: ${y}, estimated: ${e}, threshold: ${w}`);let m=ot(a.longContext,s,!1,"longContext");if(m)return{model:m,scenarioType:"longContext"};t.log.warn(`Long context model ${a.longContext} unavailable (fail pool), trying fallback`);let v=Yt("longContext",s,void 0,c);if(v)return{model:v,scenarioType:"longContext"}}if(t.body?.system?.length>1&&t.body?.system[1]?.text?.startsWith("<CCR-SUBAGENT-MODEL>")){let m=t.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);if(m)return t.body.system[1].text=t.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${m[1]}</CCR-SUBAGENT-MODEL>`,""),{model:m[1],scenarioType:"default"}}let g=r.get("Router");if(t.body.model?.includes("claude")&&t.body.model?.includes("haiku")&&g?.background){t.log.info(`Using background model for ${t.body.model}`);let m=ot(g.background,s,!1,"background");if(m)return{model:m,scenarioType:"background"};t.log.warn(`Background model ${g.background} unavailable (fail pool), falling through`)}if(Array.isArray(t.body.tools)&&t.body.tools.some(m=>m.type?.startsWith("web_search"))&&a?.webSearch){let m=ot(a.webSearch,s,!1,"webSearch");if(m)return{model:m,scenarioType:"webSearch"};t.log.warn(`WebSearch model ${a.webSearch} unavailable (fail pool), trying fallback`);let v=Yt("webSearch",s,void 0,c);if(v)return{model:v,scenarioType:"webSearch"}}if(t.body.thinking&&a?.think){t.log.info(`Using think model for ${t.body.thinking}`);let m=ot(a.think,s,!1,"think");if(m)return{model:m,scenarioType:"think"};t.log.warn(`Think model ${a.think} unavailable (fail pool), trying fallback`);let v=Yt("think",s,void 0,c);if(v)return{model:v,scenarioType:"think"}}if(a?.default){let m=ot(a.default,s,!1,"default");if(m)return{model:m,scenarioType:"default"};t.log.warn(`Default model ${a.default} unavailable (fail pool), trying fallback`);let v=Yt("default",s,void 0,c);if(v)return{model:v,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},cu=async(t,e,r)=>{let{configService:n,event:o}=r;if(t.originalModel=t.body.model,t.body.metadata?.user_id){let A=t.body.metadata.user_id.split("_session_");A.length>1&&(t.sessionId=A[1])}let a=await x0(t,n)||n.get("Router"),c=n.get("providers")||[],l=ru.get(t.sessionId),{messages:d,system:f=[],tools:p}=t.body,y=n.get("REWRITE_SYSTEM_PROMPT");if(y&&f.length>1&&f[1]?.text?.includes("<env>")){let A=await(0,su.readFile)(y,"utf-8");f[1].text=`${A}<env>${f[1].text.split("<env>").pop()}`}try{let[A,w]=t.body.model.split(","),_=r.tokenizerService?.getTokenizerConfigForModel(A,w),g;r.tokenizerService?g=(await r.tokenizerService.countTokens({messages:d,system:f,tools:p},_)).tokenCount:g=$d(d,f,p),t.tokenCount=g;let m,v=n.get("CUSTOM_ROUTER_PATH");if(v)try{m=await require(v)(t,n.getAll(),{event:o})}catch(b){t.log.error(`failed to load custom router: ${b.message}`)}if(m)t.scenarioType="default";else{let b=await HT(t,g,n,l);m=b.model,t.scenarioType=b.scenarioType}if(a?.image&&m!==a.image&&$T(t)&&!qT(m)){let b=ot(a.image,c,!1,"image");b?(t.log.info(`Using image model fallback for ${m}`),m=b,t.scenarioType="image"):t.log.warn(`Image model ${a.image} unavailable (fail pool), keeping ${m}`)}t.body.model=m}catch(A){t.log.error(`Error in router middleware: ${A.message}`),t.body.model=a?.default,t.scenarioType="default"}},Or=new Map,D0=1e3;function Ud(){if(Or.size<=D0)return;let t=[...Or.keys()].slice(0,Or.size-D0);for(let e of t)Or.delete(e)}var qd=async t=>{if(Or.has(t)){let e=Or.get(t);return!e||e===""?null:e}try{let e=await(0,uu.opendir)(_o.CLAUDE_PROJECTS_DIR),r=[];for await(let s of e)s.isDirectory()&&r.push(s.name);let n=r.map(async s=>{let a=(0,iu.join)(_o.CLAUDE_PROJECTS_DIR,s,`${t}.jsonl`);try{return(await(0,uu.stat)(a)).isFile()?s:null}catch{return null}}),o=await Promise.all(n);for(let s of o)if(s)return Or.set(t,s),Ud(),s;return Or.set(t,""),Ud(),null}catch(e){return console.error("Error searching for project by session:",e),Or.set(t,""),Ud(),null}};$n();var Fr=class{async fetchJson(e,r,n,o){if(!r.apiKey)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${r.apiKey}`},signal:AbortSignal.timeout(n)};if(o)try{let{ProxyAgent:a}=await import("undici");s.dispatcher=new a(new URL(o).toString())}catch{}try{let a=await fetch(e,s);return a.ok?await a.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}},zd=class extends Fr{async queryQuota(e,r,n){let o=await this.fetchJson("https://api.deepseek.com/user/balance",e,r,n),a=(Array.isArray(o?.balance_infos)?o.balance_infos:[]).find(l=>we(l?.total_balance)!==void 0);if(!a)return null;let c=we(a.total_balance);return c===void 0?null:{totalBalance:c,currency:typeof a.currency=="string"?a.currency:void 0}}},Hd=class extends Fr{async queryQuota(e,r,n){let s=(await this.fetchJson("https://openrouter.ai/api/v1/key",e,r,n))?.data;if(!s||typeof s!="object")return null;let a={},c=we(s.limit),l=we(s.limit_remaining),d=we(s.usage),f=we(s.usage_daily);return c!==void 0&&(a.totalBalance=c),l!==void 0&&(a.remainingBalance=l),d!==void 0&&(a.usedBalance=d),f!==void 0&&(a.usedDailyBalance=f),this.hasQuotaData(a)?a:null}},Wd=class extends Fr{async queryQuota(e,r,n){let o=XT(e.baseUrl),a=(await this.fetchJson(o,e,r,n))?.data;if(!a||typeof a!="object")return null;let c={},l=we(a.totalBalance),d=we(a.balance);return l!==void 0&&(c.totalBalance=l),d!==void 0&&(c.remainingBalance=d),l!==void 0&&d!==void 0&&(c.usedBalance=Math.max(0,l-d)),this.hasQuotaData(c)?c:null}},Gd=class extends Fr{async queryQuota(e,r,n){if(!e.apiKey)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:e.apiKey.trim()},signal:AbortSignal.timeout(r)};if(n)try{let{ProxyAgent:a}=await import("undici");s.dispatcher=new a(new URL(n).toString())}catch{}try{let a=await fetch("https://api.z.ai/api/monitor/usage/quota/limit",s);if(!a.ok)return null;let c=await a.json(),l=Array.isArray(c?.data?.limits)?c.data.limits:[],d=null,f=null,p=null;for(let A of l)A.type==="TOKENS_LIMIT"?A.unit===3?d=A:A.unit===6?f=A:d||(d=A):A.type==="TIME_LIMIT"&&(p=A);let y={};if(d){let A=we(d.currentValue),w=we(d.usage),_=we(d.remaining),g=we(d.percentage);A!==void 0?y.usedDailyBalance=A:g!==void 0&&(y.usedDailyBalance=g,y.limitDaily=100),w!==void 0?y.limitDaily=w:A!==void 0&&_!==void 0&&(y.limitDaily=A+_),d.nextResetTime&&(y.resetTime=new Date(d.nextResetTime).toISOString())}if(f){let A=we(f.currentValue),w=we(f.usage),_=we(f.remaining),g=we(f.percentage);A!==void 0?y.usedBalance=A:g!==void 0&&(y.usedBalance=g,y.totalBalance=100),w!==void 0?y.totalBalance=w:A!==void 0&&_!==void 0&&(y.totalBalance=A+_),f.nextResetTime&&!y.resetTime&&(y.resetTime=new Date(f.nextResetTime).toISOString())}if(p){if(y.usedDailyBalance===void 0){let A=we(p.currentValue);A!==void 0&&(y.usedDailyBalance=A)}if(y.limitDaily===void 0){let A=we(p.usage),w=we(p.currentValue),_=we(p.remaining);A!==void 0?y.limitDaily=A:w!==void 0&&_!==void 0&&(y.limitDaily=w+_)}p.nextResetTime&&!y.resetTime&&(y.resetTime=new Date(p.nextResetTime).toISOString())}return this.hasQuotaData(y)?y:null}catch{return null}}},Jd=class extends Fr{async queryQuota(e,r,n){if(!e.quotaToken)return null;let o={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(r)};if(n)try{let{ProxyAgent:c}=await import("undici");o.dispatcher=new c(new URL(n).toString())}catch{}let 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{let c=await fetch("https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",{...o,body:a});if(!c.ok)return null;let d=(await c.json())?.data?.DataV2?.data?.data?.codingPlanInstanceInfos;if(!Array.isArray(d)||d.length===0)return null;let f=d[0]?.codingPlanQuotaInfo;if(!f)return null;let p={},y=we(f.per5HourUsedQuota),A=we(f.per5HourTotalQuota);y!==void 0&&(p.usedDailyBalance=y),A!==void 0&&(p.limitDaily=A);let w=we(f.perWeekUsedQuota),_=we(f.perWeekTotalQuota);return w!==void 0&&(p.usedBalance=w),_!==void 0&&(p.totalBalance=_),this.hasQuotaData(p)?p:null}catch{return null}}},Vd=class extends Fr{async queryQuota(e,r,n){let o=e.quotaToken||e.apiKey;if(!o)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${o.trim()}`},signal:AbortSignal.timeout(r)};if(n)try{let{ProxyAgent:a}=await import("undici");s.dispatcher=new a(new URL(n).toString())}catch{}try{let a=await fetch("https://api.kimi.com/coding/v1/usages",s);if(!a.ok)return null;let c=await a.json(),l={},d=Array.isArray(c?.limits)?c.limits:[];if(d.length>0&&d[0]?.detail){let p=d[0].detail,y=we(p.limit),A=we(p.remaining),w=p.resetTime;y!==void 0&&A!==void 0&&(l.usedDailyBalance=Math.max(0,y-A),l.limitDaily=y),w&&(l.resetTime=new Date(w).toISOString())}let f=c?.usage;if(f){let p=we(f.limit),y=we(f.remaining),A=we(f.used),w=f.resetTime;p!==void 0&&(l.totalBalance=p,A!==void 0?l.usedBalance=A:y!==void 0&&(l.usedBalance=Math.max(0,p-y),l.remainingBalance=y)),w&&!l.resetTime&&(l.resetTime=new Date(w).toISOString())}return this.hasQuotaData(l)?l:null}catch{return null}}},Kd=class extends Fr{async queryQuota(e,r,n){let o=e.quotaToken||e.apiKey;if(!o)return null;let s=Yd(e.baseUrl)||"api.minimaxi.com",a=s.endsWith(".minimax.io")||s==="minimax.io"?"api.minimax.io":"api.minimaxi.com",c={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${o.trim()}`},signal:AbortSignal.timeout(r)};if(n)try{let{ProxyAgent:l}=await import("undici");c.dispatcher=new l(new URL(n).toString())}catch{}try{let l=await fetch(`https://${a}/v1/api/openplatform/coding_plan/remains`,c);if(!l.ok)return null;let d=await l.json();if(d?.base_resp){let b=d.base_resp.status_code;if(b!==void 0&&b!==0)return null}let f=Array.isArray(d?.model_remains)?d.model_remains:[];if(f.length===0)return null;let p=f[0],y={},A=we(p.current_interval_total_count),w=we(p.current_interval_usage_count),_=we(p.end_time);A!==void 0&&w!==void 0&&(y.usedDailyBalance=Math.max(0,A-w),y.limitDaily=A),_&&(y.resetTime=new Date(_).toISOString());let g=we(p.current_weekly_total_count),m=we(p.current_weekly_usage_count),v=we(p.weekly_end_time);return g!==void 0&&m!==void 0&&(y.usedBalance=Math.max(0,g-m),y.remainingBalance=m,y.totalBalance=g),v&&!y.resetTime&&(y.resetTime=new Date(v).toISOString()),this.hasQuotaData(y)?y:null}catch{return null}}},WT=new zd,GT=new Hd,JT=new Wd,VT=new Gd,KT=new Jd,YT=new Vd,QT=new Kd;function lu(t){let e=Yd(t);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?YT:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?QT:e==="deepseek.com"||e.endsWith(".deepseek.com")?WT:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?GT:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?JT:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?VT:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?KT:null:null}function XT(t){let e=Yd(t);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function Yd(t){try{return new URL(t).hostname.toLowerCase()}catch{return null}}function we(t){if(typeof t=="number")return Number.isFinite(t)?t:void 0;if(typeof t!="string")return;let e=t.trim().replace(/,/g,"");if(!e)return;let r=Number(e);return Number.isFinite(r)?r:void 0}var ZT={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,initialDelayMs:5e3,excludeProviders:[]};function eD(t){try{let e=new URL(t),r=e.pathname;return r.endsWith("/v1/messages")?r=r.slice(0,-12):r.endsWith("/messages")?r=r.slice(0,-9):r.endsWith("/v1/chat/completions")?r=r.slice(0,-20):r.endsWith("/chat/completions")&&(r=r.slice(0,-17)),r.endsWith("/v1")||(r=r.endsWith("/")?`${r}v1`:`${r}/v1`),e.pathname=`${r}/models`,e.toString()}catch{return null}}function tD(t){try{let e=new URL(t).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function Qd(t,e,r){let n=eD(t.baseUrl);if(!n)return{success:!1,error:"Cannot derive models endpoint from baseUrl"};try{let o={method:"GET",headers:{Authorization:`Bearer ${t.apiKey}`},signal:AbortSignal.timeout(e)};if(r)try{let{ProxyAgent:c}=await import("undici");o.dispatcher=new c(new URL(r).toString())}catch{}let s=await fetch(n,o);if(s.headers&&Io(t.name,t.baseUrl,s.headers),s.ok)return{success:!0,headers:s.headers};if(s.status===429)return{success:!1,error:`HTTP 429: ${(await s.text().catch(()=>"")).slice(0,100)||"Rate limited"}`,headers:s.headers};if(s.status>=400&&s.status<500)return{success:!0,headers:s.headers};let a=await s.text().catch(()=>"");return{success:!1,error:`HTTP ${s.status}: ${a.slice(0,100)}`,headers:s.headers}}catch(o){return{success:!1,error:o?.message||o?.toString()||"Unknown probe error"}}}var Cs=class{config;quotaProbeTimer;healthProbeTimer;getProviders;getHttpsProxy;logger;running=!1;constructor(e,r,n,o){this.config={...ZT,...r},this.getProviders=e,this.getHttpsProxy=n,this.logger=o}start(){!this.config.enabled||this.running||(this.running=!0,setTimeout(()=>{this.running&&(this.runQuotaProbe(),this.runHealthProbe())},this.config.initialDelayMs),this.quotaProbeTimer=setInterval(()=>this.runQuotaProbe(),this.config.quotaProbeIntervalMinutes*60*1e3),this.healthProbeTimer=setInterval(()=>this.runHealthProbe(),300*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.logger?.info("Active probe service stopped")}async runQuotaProbe(){let e=this.getProviders().filter(a=>a?.name&&!this.config.excludeProviders.includes(a.name)),r=this.getHttpsProxy?.(),n=tt();if(e.length===0)return;let o=[];for(let a of e){let c=lu(a.baseUrl),l=Array.isArray(a.models)?a.models:[];c&&o.push({provider:a.name,models:l,type:"quota-adapter",promise:c.queryQuota(a,this.config.probeTimeoutMs,r).then(d=>{if(d){nu(a.name,d),this.logger?.debug?.(`Stored quota probe result for ${a.name}`);let f=d.limitDaily!==void 0&&d.usedDailyBalance!==void 0&&d.usedDailyBalance>=d.limitDaily,p=d.totalBalance!==void 0&&d.usedBalance!==void 0&&d.usedBalance>=d.totalBalance;if(f||p){let y=f?`Quota exhausted: 5h limit reached (${d.usedDailyBalance}/${d.limitDaily})`:`Quota exhausted: 7d balance depleted (${d.usedBalance}/${d.totalBalance})`;for(let A of l)n.forceOpen(a.name,A,y);this.logger?.warn?.(`${y} for ${a.name}, marked as unhealthy`)}else for(let y of l){let A=n.getState(a.name,y);A&&A.status==="open"&&A.lastError?.includes("Quota exhausted")&&(n.recover(a.name,y),this.logger?.info?.(`Quota recovered for ${a.name} (${y}), marked as healthy`))}}})}),tD(a.baseUrl)&&o.push({provider:a.name,models:l,type:"rate-limit-headers",promise:Qd(a,this.config.probeTimeoutMs,r).then(()=>{})})}if(o.length===0)return;this.logger?.debug?.(`Running quota probe with ${o.length} tasks for ${e.length} providers`);let s=await Promise.allSettled(o.map(a=>a.promise));for(let a=0;a<o.length;a++){let c=s[a];if(c.status==="rejected"){let l=o[a];this.logger?.warn?.(`Quota probe failed for ${l.provider} (${l.type}): ${c.reason}`)}}}async runHealthProbe(){let e=tt(),r=this.getProviders().filter(s=>s?.name&&!this.config.excludeProviders.includes(s.name)),n=this.getHttpsProxy?.();if(r.length===0)return;this.logger?.debug?.(`Running health probe for ${r.length} providers`);let o=await Promise.allSettled(r.map(s=>Qd(s,this.config.probeTimeoutMs,n)));for(let s=0;s<r.length;s++){let a=r[s],c=o[s],l=Array.isArray(a.models)?a.models:[];if(c.status==="fulfilled"){let d=c.value;if(d.success){let f=0;for(let p of l){let y=e.getState(a.name,p);if(y&&y.status==="open"&&y.lastError?.includes("Quota exhausted")){this.logger?.debug?.(`Skipping health probe recovery for ${a.name} (${p}) because quota is exhausted`);continue}e.recordSuccess(a.name,p),f++}f>0&&this.logger?.info?.(`Health probe succeeded for ${a.name}`)}else{for(let f of l)e.recordFailure(a.name,f,d.error);this.logger?.warn?.(`Health probe failed for ${a.name}: ${d.error}`)}}else{for(let d of l)e.recordFailure(a.name,d,c.reason?.message||"Probe error");this.logger?.warn?.(`Health probe error for ${a.name}: ${c.reason}`)}}}async probeProviderManually(e){let n=this.getProviders().find(c=>c.name===e);if(!n)return this.logger?.warn?.(`Provider ${e} not found for manual probe`),!1;let o=this.getHttpsProxy?.(),s=await Qd(n,this.config.probeTimeoutMs,o),a=Array.isArray(n.models)?n.models:[];if(s.success)for(let c of a)tt().recordSuccess(n.name,c);else for(let c of a)tt().recordFailure(n.name,c,s.error);return s.success}},Xr=null;function Xd(t,e,r,n){return Xr||(Xr=new Cs(t,e,r,n)),Xr}function du(t,e,r,n){let o=Xd(t,e,r,n);return o.start(),o}function fu(){Xr&&Xr.stop()}function P0(){Xr&&(Xr.stop(),Xr=null)}var Zd=class{plugins=new Map;pluginInstances=new Map;registerPlugin(e,r={}){this.pluginInstances.set(e.name,e),this.plugins.set(e.name,{name:e.name,enabled:r.enabled!==!1,options:r})}async enablePlugin(e,r){let n=this.plugins.get(e),o=this.pluginInstances.get(e);if(!n||!o)throw new Error(`Plugin ${e} not found`);n.enabled&&await r.register(o.register,n.options)}async enablePlugins(e){for(let[r,n]of this.plugins)if(n.enabled)try{await this.enablePlugin(r,e)}catch(o){let s=o instanceof Error?o.message:String(o);e.log?.error(`Failed to enable plugin ${r}: ${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,r){let n=this.plugins.get(e);n&&(n.enabled=r)}removePlugin(e){this.plugins.delete(e),this.pluginInstances.delete(e)}clear(){this.plugins.clear(),this.pluginInstances.clear()}},ef=new Zd;var z0=Pe(j0(),1);var vn=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,r)=>{this.buffer+=e;let n=this.buffer.split(`
344
+ `:case"\r":return!1;default:return new RegExp("^\\p{Cc}|\\p{Cf}|\\p{Co}|\\p{Cs}$","u").test(t)}}clean_text(t){let e=[];for(let r of t){let n=r.charCodeAt(0);n===0||n===65533||this.is_control(r)||(/^\s$/.test(r)?e.push(" "):e.push(r))}return e.join("")}normalize(t){return this.config.clean_text&&(t=this.clean_text(t)),this.config.handle_chinese_chars&&(t=this.tokenize_chinese_chars(t)),this.config.lowercase?(t=t.toLowerCase(),this.config.strip_accents!==!1&&(t=this.strip_accents(t))):this.config.strip_accents&&(t=this.strip_accents(t)),t}},KS=VS,YS=class extends Rr{constructor(t){super(t),this.charsmap=t.precompiled_charsmap??null}normalize(t){return t=t.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm,""),t=t.replace(/[\u0009\u000A\u000C\u000D\u00A0\u1680\u2000-\u200F\u2028\u2029\u202F\u205F\u2581\u3000\uFEFF\uFFFD]/gm," "),t.includes("\uFF5E")?t=t.split("\uFF5E").map(e=>e.normalize("NFKC")).join("\uFF5E"):t=t.normalize("NFKC"),t}},QS=YS,XS=class extends Rr{constructor(t){super(t),this.normalizers=(t.normalizers??[]).map(e=>_0(e))}normalize(t){return this.normalizers.reduce((e,r)=>r?r.normalize(e):e,t)}},ZS=XS,ev=class extends Rr{normalize(t){let e=Qa(this.config.pattern??{});return e===null?t:t.replaceAll(e,this.config.content??"")}},tv=ev,rv=class extends Rr{constructor(){super(...arguments),this.form="NFC"}normalize(t){return t=t.normalize(this.form),t}},Xa=rv,nv=class extends Xa{constructor(){super(...arguments),this.form="NFC"}},ov=nv,sv=class extends Xa{constructor(){super(...arguments),this.form="NFD"}},iv=sv,av=class extends Xa{constructor(){super(...arguments),this.form="NFKC"}},uv=av,cv=class extends Xa{constructor(){super(...arguments),this.form="NFKD"}},lv=cv,dv=class extends Rr{normalize(t){return this.config.strip_left&&this.config.strip_right?t=t.trim():(this.config.strip_left&&(t=t.trimStart()),this.config.strip_right&&(t=t.trimEnd())),t}},fv=dv,hv=class extends Rr{normalize(t){return y0(t)}},pv=hv,mv=class extends Rr{normalize(t){return t.toLowerCase()}},gv=mv,yv=class extends Rr{normalize(t){return t=this.config.prepend+t,t}},_v=yv;function bv(t){if(t===null)return null;switch(t.type){case"BertNormalizer":return new KS(t);case"Precompiled":return new QS(t);case"Sequence":return new ZS(t);case"Replace":return new tv(t);case"NFC":return new ov(t);case"NFD":return new iv(t);case"NFKC":return new uv(t);case"NFKD":return new lv(t);case"Strip":return new fv(t);case"StripAccents":return new pv(t);case"Lowercase":return new gv(t);case"Prepend":return new _v(t);default:throw new Error(`Unknown Normalizer type: ${t.type}`)}}var _0=bv,Cv=class extends gs{pre_tokenize(t,e){return(Array.isArray(t)?t.map(r=>this.pre_tokenize_text(r,e)):this.pre_tokenize_text(t,e)).flat()}_call(t,e){return this.pre_tokenize(t,e)}},lr=Cv,wv=class extends lr{constructor(t){super(),this.config=t,this.add_prefix_space=this.config.add_prefix_space??!1,this.trim_offsets=this.config.trim_offsets??!1,this.use_regex=this.config.use_regex??!0,this.pattern=new RegExp("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+","gu"),this.byte_encoder=g0,this.text_encoder=new TextEncoder}pre_tokenize_text(t,e){return this.add_prefix_space&&!t.startsWith(" ")&&(t=" "+t),(this.use_regex?t.match(this.pattern)||[]:[t]).map(r=>Array.from(this.text_encoder.encode(r),n=>this.byte_encoder[n]).join(""))}},Ev=wv,Av=class extends lr{pre_tokenize_text(t,e){return t.match(/\w+|[^\w\s]+/g)||[]}},kv=Av,Sv=class extends lr{constructor(t){super(),this.replacement=t.replacement??"\u2581",this.str_rep=t.str_rep||this.replacement,this.prepend_scheme=t.prepend_scheme??"always"}pre_tokenize_text(t,e){let{section_index:r=void 0}=e??{},n=t.replaceAll(" ",this.str_rep);return!n.startsWith(this.replacement)&&(this.prepend_scheme==="always"||this.prepend_scheme==="first"&&r===0)&&(n=this.str_rep+n),[n]}},vv=Sv,Tv=class extends lr{constructor(t){super(),this.config=t,this.pattern=Qa(this.config.pattern??{},this.config.invert??!0)}pre_tokenize_text(t){return this.pattern===null?[]:this.config.invert?t.match(this.pattern)||[]:this.config.behavior?.toLowerCase()==="removed"?t.split(this.pattern).filter(e=>e):HS(t,this.pattern)}},Dv=Tv,Rv=class extends lr{constructor(t){super(),this.config=t,this.pattern=new RegExp(`[^${ms}]+|[${ms}]+`,"gu")}pre_tokenize_text(t){return t.match(this.pattern)||[]}},xv=Rv,Ov=class extends lr{constructor(t){super(),this.config=t;let e=`[^\\d]+|\\d${this.config.individual_digits?"":"+"}`;this.pattern=new RegExp(e,"gu")}pre_tokenize_text(t){return t.match(this.pattern)||[]}},Fv=Ov,Pv=class extends lr{constructor(){super(),this.pattern=new RegExp(`[^\\s${ms}]+|[${ms}]`,"gu")}pre_tokenize_text(t,e){return t.trim().match(this.pattern)||[]}},Iv=Pv,Bv=class extends lr{constructor(t){super(),this.config=t,this.pattern=Qa(this.config.pattern??{}),this.content=this.config.content??""}pre_tokenize_text(t){return this.pattern===null?[t]:[t.replaceAll(this.pattern,this.config.content??"")]}},Nv=Bv,Mv=class extends lr{constructor(t){super(),this.tokenizers=(t.pretokenizers??[]).map(e=>b0(e))}pre_tokenize_text(t,e){return this.tokenizers.reduce((r,n)=>n?n.pre_tokenize(r,e):r,[t])}},jv=Mv,Lv=class extends lr{pre_tokenize_text(t){return WS(t)}},Uv=Lv;function $v(t){if(t===null)return null;switch(t.type){case"BertPreTokenizer":return new Iv;case"Sequence":return new jv(t);case"Whitespace":return new kv;case"WhitespaceSplit":return new Uv;case"Metaspace":return new vv(t);case"ByteLevel":return new Ev(t);case"Split":return new Dv(t);case"Punctuation":return new xv(t);case"Digits":return new Fv(t);case"Replace":return new Nv(t);default:throw new Error(`Unknown PreTokenizer type: ${t.type}`)}}var b0=$v,qv=class extends gs{constructor(t){super(),this.config=t,this.vocab=[],this.tokens_to_ids=new Map,this.unk_token_id=void 0,this.unk_token=void 0,this.end_of_word_suffix=void 0,this.fuse_unk=this.config.fuse_unk??!1}_call(t){let e=this.encode(t);return this.fuse_unk&&(e=LS(e,this.tokens_to_ids,this.unk_token_id)),e}convert_tokens_to_ids(t){return t.map(e=>this.tokens_to_ids.get(e)??this.unk_token_id)}convert_ids_to_tokens(t){return t.map(e=>this.vocab[Number(e)]??this.unk_token)}},Za=qv,zv=class extends Za{constructor(t){super(t),this.max_input_chars_per_word=100,this.tokens_to_ids=Md(t.vocab),this.unk_token_id=this.tokens_to_ids.get(t.unk_token),this.unk_token=t.unk_token,this.max_input_chars_per_word=t.max_input_chars_per_word??100,this.vocab=new Array(this.tokens_to_ids.size);for(let[e,r]of this.tokens_to_ids)this.vocab[r]=e}encode(t){let e=[];for(let r of t){let n=[...r];if(n.length>this.max_input_chars_per_word){e.push(this.unk_token);continue}let o=!1,s=0,a=[];for(;s<n.length;){let c=n.length,l=null;for(;s<c;){let d=n.slice(s,c).join("");if(s>0&&(d=this.config.continuing_subword_prefix+d),this.tokens_to_ids.has(d)){l=d;break}--c}if(l===null){o=!0;break}a.push(l),s=c}o?e.push(this.unk_token):e.push(...a)}return e}},f0=zv,h0=class C0{constructor(e,r){this.is_leaf=e,this.children=r}static default(){return new C0(!1,new Map)}},Hv=class{constructor(){this.root=h0.default()}extend(t){for(let e of t)this.push(e)}push(t){let e=this.root;for(let r of t){let n=e.children.get(r);n===void 0&&(n=h0.default(),e.children.set(r,n)),e=n}e.is_leaf=!0}*common_prefix_search(t){let e=this.root;if(e===void 0)return;let r="";for(let n of t){if(r+=n,e=e.children.get(n),e===void 0)return;e.is_leaf&&(yield r)}}},Wv=Hv,Bd=class w0{constructor(e,r,n,o,s){this.token_id=e,this.node_id=r,this.pos=n,this.length=o,this.score=s,this.prev=null,this.backtrace_score=0}clone(){let e=new w0(this.token_id,this.node_id,this.pos,this.length,this.score);return e.prev=this.prev,e.backtrace_score=this.backtrace_score,e}},Gv=class{constructor(t,e,r){this.chars=Array.from(t),this.len=this.chars.length,this.bos_token_id=e,this.eos_token_id=r,this.nodes=[],this.begin_nodes=Array.from({length:this.len+1},()=>[]),this.end_nodes=Array.from({length:this.len+1},()=>[]);let n=new Bd(this.bos_token_id??0,0,0,0,0),o=new Bd(this.eos_token_id??0,1,this.len,0,0);this.nodes.push(n.clone()),this.nodes.push(o.clone()),this.begin_nodes[this.len].push(o),this.end_nodes[0].push(n)}insert(t,e,r,n){let o=this.nodes.length,s=new Bd(n,o,t,e,r);this.begin_nodes[t].push(s),this.end_nodes[t+e].push(s),this.nodes.push(s)}viterbi(){let t=this.len,e=0;for(;e<=t;){if(this.begin_nodes[e].length==0)return[];for(let s of this.begin_nodes[e]){s.prev=null;let a=0,c=null;for(let l of this.end_nodes[e]){let d=l.backtrace_score+s.score;(c===null||d>a)&&(c=l.clone(),a=d)}if(c!==null)s.prev=c,s.backtrace_score=a;else return[]}++e}let r=[],n=this.begin_nodes[t][0].prev;if(n===null)return[];let o=n.clone();for(;o.prev!==null;)r.push(o.clone()),o=o.clone().prev.clone();return r.reverse(),r}piece(t){return this.chars.slice(t.pos,t.pos+t.length).join("")}tokens(){return this.viterbi().map(t=>this.piece(t))}token_ids(){return this.viterbi().map(t=>t.token_id)}},Jv=Gv;function Vv(t){if(t.length===0)throw new Error("Array must not be empty");let e=t[0],r=0;for(let n=1;n<t.length;++n)t[n]<e&&(e=t[n],r=n);return[e,r]}var Kv=class extends Za{constructor(t,e){super(t);let r=t.vocab.length;this.vocab=new Array(r),this.scores=new Array(r);for(let n=0;n<r;++n)[this.vocab[n],this.scores[n]]=t.vocab[n];this.unk_token_id=t.unk_id,this.unk_token=this.vocab[t.unk_id],this.tokens_to_ids=new Map(this.vocab.map((n,o)=>[n,o])),this.bos_token=" ",this.bos_token_id=this.tokens_to_ids.get(this.bos_token),this.eos_token=e,this.eos_token_id=this.tokens_to_ids.get(this.eos_token),this.unk_token=this.vocab[this.unk_token_id],this.min_score=Vv(this.scores)[0],this.unk_score=this.min_score-10,this.scores[this.unk_token_id]=this.unk_score,this.trie=new Wv,this.trie.extend(this.vocab),this.fuse_unk=!0}populate_nodes(t){let e=t.chars,r=1,n=0;for(;n<e.length;){let o=!1,s=[],a=e.slice(n).join(""),c=this.trie.common_prefix_search(a);for(let l of c){s.push(l);let d=this.tokens_to_ids.get(l),f=this.scores[d],p=qS(l);t.insert(n,p,f,d),!o&&p===r&&(o=!0)}o||t.insert(n,r,this.unk_score,this.unk_token_id),n+=r}}tokenize(t){let e=new Jv(t,this.bos_token_id,this.eos_token_id);return this.populate_nodes(e),e.tokens()}encode(t){let e=[];for(let r of t){let n=this.tokenize(r);e.push(...n)}return e}},p0=Kv,Yv=class{constructor(t=(r,n)=>r>n,e=1/0){this._heap=[],this._comparator=t,this._max_size=e}get size(){return this._heap.length}is_empty(){return this.size===0}peek(){return this._heap[0]}push(...t){return this.extend(t)}extend(t){for(let e of t)if(this.size<this._max_size)this._heap.push(e),this._sift_up();else{let r=this._smallest();this._comparator(e,this._heap[r])&&(this._heap[r]=e,this._sift_up_from(r))}return this.size}pop(){let t=this.peek(),e=this.size-1;return e>0&&this._swap(0,e),this._heap.pop(),this._sift_down(),t}replace(t){let e=this.peek();return this._heap[0]=t,this._sift_down(),e}_parent(t){return(t+1>>>1)-1}_left(t){return(t<<1)+1}_right(t){return t+1<<1}_greater(t,e){return this._comparator(this._heap[t],this._heap[e])}_swap(t,e){let r=this._heap[t];this._heap[t]=this._heap[e],this._heap[e]=r}_sift_up(){this._sift_up_from(this.size-1)}_sift_up_from(t){for(;t>0&&this._greater(t,this._parent(t));)this._swap(t,this._parent(t)),t=this._parent(t)}_sift_down(){let t=0;for(;this._left(t)<this.size&&this._greater(this._left(t),t)||this._right(t)<this.size&&this._greater(this._right(t),t);){let e=this._right(t)<this.size&&this._greater(this._right(t),this._left(t))?this._right(t):this._left(t);this._swap(t,e),t=e}}_smallest(){return 2**Math.floor(Math.log2(this.size))-1}},Qv=Yv,Xv=class{constructor(t){this.capacity=t,this.cache=new Map}get(t){if(!this.cache.has(t))return;let e=this.cache.get(t);return this.cache.delete(t),this.cache.set(t,e),e}put(t,e){this.cache.has(t)&&this.cache.delete(t),this.cache.set(t,e),this.cache.size>this.capacity&&this.cache.delete(this.cache.keys().next().value)}clear(){this.cache.clear()}},Zv=Xv,eT=class extends Za{constructor(t){super(t),this.tokens_to_ids=Md(t.vocab),this.unk_token_id=this.tokens_to_ids.get(t.unk_token),this.unk_token=t.unk_token,this.vocab=new Array(this.tokens_to_ids.size);for(let[r,n]of this.tokens_to_ids)this.vocab[n]=r;let e=Array.isArray(t.merges[0]);this.merges=e?t.merges:t.merges.map(r=>r.split(" ",2)),this.bpe_ranks=new Map(this.merges.map((r,n)=>[JSON.stringify(r),n])),this.end_of_word_suffix=t.end_of_word_suffix,this.continuing_subword_suffix=t.continuing_subword_suffix??null,this.byte_fallback=this.config.byte_fallback??!1,this.byte_fallback&&(this.text_encoder=new TextEncoder),this.ignore_merges=this.config.ignore_merges??!1,this.max_length_to_cache=256,this.cache_capacity=1e4,this.cache=new Zv(this.cache_capacity)}clear_cache(){this.cache.clear()}bpe(t){if(t.length===0)return[];let e=this.cache.get(t);if(e!==void 0)return e;let r=Array.from(t);this.end_of_word_suffix&&(r[r.length-1]+=this.end_of_word_suffix);let n=[];if(r.length>1){let o=new Qv((c,l)=>c.score<l.score),s={token:r[0],bias:0,prev:null,next:null},a=s;for(let c=1;c<r.length;++c){let l={bias:c/r.length,token:r[c],prev:a,next:null};a.next=l,this.add_node(o,a),a=l}for(;!o.is_empty();){let c=o.pop();if(c.deleted||!c.next||c.next.deleted)continue;if(c.deleted=!0,c.next.deleted=!0,c.prev){let d={...c.prev};c.prev.deleted=!0,c.prev=d,d.prev?d.prev.next=d:s=d}let l={token:c.token+c.next.token,bias:c.bias,prev:c.prev,next:c.next.next};l.prev?(l.prev.next=l,this.add_node(o,l.prev)):s=l,l.next&&(l.next.prev=l,this.add_node(o,l))}for(let c=s;c!==null;c=c.next)n.push(c.token)}else n=r;if(this.continuing_subword_suffix)for(let o=0;o<n.length-1;++o)n[o]+=this.continuing_subword_suffix;return t.length<this.max_length_to_cache&&this.cache.put(t,n),n}add_node(t,e){let r=this.bpe_ranks.get(JSON.stringify([e.token,e.next.token]));r!==void 0&&(e.score=r+e.bias,t.push(e))}encode(t){let e=[];for(let r of t){if(this.ignore_merges&&this.tokens_to_ids.has(r)){e.push(r);continue}let n=this.bpe(r);for(let o of n)if(this.tokens_to_ids.has(o))e.push(o);else if(this.byte_fallback){let s=Array.from(this.text_encoder.encode(o)).map(a=>`<0x${a.toString(16).toUpperCase().padStart(2,"0")}>`);s.every(a=>this.tokens_to_ids.has(a))?e.push(...s):e.push(this.unk_token)}else e.push(this.unk_token)}return e}},m0=eT,tT=class extends Za{constructor(t,e){super(t);let r=t.vocab;this.tokens_to_ids=Md(e.target_lang?r[e.target_lang]:r),this.bos_token=e.bos_token,this.bos_token_id=this.tokens_to_ids.get(this.bos_token),this.eos_token=e.eos_token,this.eos_token_id=this.tokens_to_ids.get(this.eos_token),this.pad_token=e.pad_token,this.pad_token_id=this.tokens_to_ids.get(this.pad_token),this.unk_token=e.unk_token,this.unk_token_id=this.tokens_to_ids.get(this.unk_token),this.vocab=new Array(this.tokens_to_ids.size);for(let[n,o]of this.tokens_to_ids)this.vocab[o]=n}encode(t){return t}},rT=tT;function nT(t,e){switch(t.type){case"WordPiece":return new f0(t);case"Unigram":return new p0(t,e.eos_token);case"BPE":return new m0(t);default:if(t.vocab)return Array.isArray(t.vocab)?new p0(t,e.eos_token):Object.hasOwn(t,"continuing_subword_prefix")&&Object.hasOwn(t,"unk_token")?Object.hasOwn(t,"merges")?new m0(t):new f0(t):new rT(t,{target_lang:e.target_lang,bos_token:e.bos_token,eos_token:e.eos_token,pad_token:e.pad_token,unk_token:e.unk_token});throw new Error(`Unknown TokenizerModel type: ${t?.type}`)}}var oT=nT,sT=class extends gs{constructor(t){super(),this.config=t}_call(t,...e){return this.post_process(t,...e)}},ys=sT,iT=class extends ys{post_process(t,e=null,r=!0){let n=e===null?this.config.single:this.config.pair,o=[],s=[];for(let a of n)"SpecialToken"in a?r&&(o.push(a.SpecialToken.id),s.push(a.SpecialToken.type_id)):"Sequence"in a&&(a.Sequence.id==="A"?(o=Bt(o,t),s=Bt(s,new Array(t.length).fill(a.Sequence.type_id))):a.Sequence.id==="B"&&(o=Bt(o,e),s=Bt(s,new Array(e.length).fill(a.Sequence.type_id))));return{tokens:o,token_type_ids:s}}},aT=iT,uT=class extends ys{post_process(t,e=null){return{tokens:e?Bt(t,e):t}}},cT=uT,lT=class extends ys{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e=null,r=!0){r&&(t=Bt([this.cls[0]],t,[this.sep[0]]));let n=new Array(t.length).fill(0);if(e){let o=[],s=r?[this.sep[0]]:[];t=Bt(t,o,e,s),n=Bt(n,new Array(e.length+o.length+s.length).fill(1))}return{tokens:t,token_type_ids:n}}},dT=lT,fT=class extends ys{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e,r=!0){r&&(t=Bt([this.cls[0]],t,[this.sep[0]]));let n=new Array(t.length).fill(0);if(e){let o=r?[this.sep[0]]:[],s=r?[this.sep[0]]:[];t=Bt(t,o,e,s),n=Bt(n,new Array(e.length+o.length+s.length).fill(1))}return{tokens:t,token_type_ids:n}}},hT=fT,pT=class extends ys{constructor(t){super(t),this.processors=(t.processors??[]).map(e=>E0(e))}post_process(t,e=null,r=!0){let n={tokens:t};for(let o of this.processors)n=o.post_process(n.tokens,e,r);return n}},mT=pT;function gT(t){if(t===null)return null;switch(t.type){case"TemplateProcessing":return new aT(t);case"ByteLevel":return new cT(t);case"BertProcessing":return new dT(t);case"RobertaProcessing":return new hT(t);case"Sequence":return new mT(t);default:throw new Error(`Unknown PostProcessor type: ${t.type}`)}}var E0=gT,yT=class extends gs{constructor(t){super(),this.config=t,this.added_tokens=[],this.end_of_word_suffix=null,this.trim_offsets="trim_offsets"in t?t.trim_offsets:!1}_call(t){return this.decode(t)}decode(t){return this.decode_chain(t).join("")}},dr=yT,_T=class extends dr{constructor(t){super(t),this.byte_decoder=NS,this.text_decoder=new TextDecoder("utf-8",{fatal:!1,ignoreBOM:!0}),this.end_of_word_suffix=null}convert_tokens_to_string(t){let e=t.join(""),r=new Uint8Array([...e].map(n=>this.byte_decoder[n]));return this.text_decoder.decode(r)}decode_chain(t){let e=[],r=[];for(let n of t)this.added_tokens.find(o=>o.content===n)!==void 0?(r.length>0&&(e.push(this.convert_tokens_to_string(r)),r=[]),e.push(n)):r.push(n);return r.length>0&&e.push(this.convert_tokens_to_string(r)),e}},bT=_T,CT=class extends dr{constructor(t){super(t),this.cleanup=t.cleanup}decode_chain(t){return t.map((e,r)=>{if(r!==0){let n=this.config.prefix;n&&e.startsWith(n)?e=e.replace(n,""):e=" "+e}return this.cleanup&&(e=Nd(e)),e})}},wT=CT,ET=class extends dr{constructor(t){super(t),this.replacement=t.replacement??"\u2581"}decode_chain(t){let e=[];for(let r=0;r<t.length;++r){let n=t[r].replaceAll(this.replacement," ");r==0&&n.startsWith(" ")&&(n=n.substring(1)),e.push(n)}return e}},AT=ET,kT=class extends dr{constructor(t){super(t),this.suffix=t.suffix??""}decode_chain(t){return t.map((e,r)=>e.replaceAll(this.suffix,r===t.length-1?"":" "))}},ST=kT,vT=class extends dr{constructor(t){super(t),this.pad_token=t.pad_token??"",this.word_delimiter_token=t.word_delimiter_token??"",this.cleanup=t.cleanup}convert_tokens_to_string(t){if(t.length===0)return"";let e=[t[0]];for(let n=1;n<t.length;++n)t[n]!==e.at(-1)&&e.push(t[n]);let r=e.filter(n=>n!==this.pad_token).join("");return this.cleanup&&(r=Nd(r).replaceAll(this.word_delimiter_token," ").trim()),r}decode_chain(t){return[this.convert_tokens_to_string(t)]}},TT=vT,DT=class extends dr{constructor(t){super(t),this.decoders=(t.decoders??[]).map(e=>A0(e))}decode_chain(t){return this.decoders.reduce((e,r)=>r.decode_chain(e),t)}},RT=DT,xT=class extends dr{decode_chain(t){let e=Qa(this.config.pattern),r=this.config.content??"";return e===null?t:t.map(n=>n.replaceAll(e,r))}},OT=xT,FT=class extends dr{decode_chain(t){return[t.join("")]}},PT=FT,IT=class extends dr{constructor(t){super(t),this.content=t.content??"",this.start=t.start??0,this.stop=t.stop??0}decode_chain(t){return t.map(e=>{let r=0;for(let o=0;o<this.start&&e[o]===this.content;++o)r=o+1;let n=e.length;for(let o=0;o<this.stop;++o){let s=e.length-o-1;if(e[s]===this.content){n=s;continue}else break}return e.slice(r,n)})}},BT=IT,NT=class extends dr{constructor(t){super(t),this.text_decoder=new TextDecoder}decode_chain(t){let e=[],r=[];for(let n of t){let o=null;if(n.length===6&&n.startsWith("<0x")&&n.endsWith(">")){let s=parseInt(n.slice(3,5),16);isNaN(s)||(o=s)}if(o!==null)r.push(o);else{if(r.length>0){let s=this.text_decoder.decode(Uint8Array.from(r));e.push(s),r=[]}e.push(n)}}if(r.length>0){let n=this.text_decoder.decode(Uint8Array.from(r));e.push(n),r=[]}return e}},MT=NT;function jT(t){if(t===null)return null;switch(t.type){case"ByteLevel":return new bT(t);case"WordPiece":return new wT(t);case"Metaspace":return new AT(t);case"BPEDecoder":return new ST(t);case"CTC":return new TT(t);case"Sequence":return new RT(t);case"Replace":return new OT(t);case"Fuse":return new PT(t);case"Strip":return new BT(t);case"ByteFallback":return new MT(t);default:throw new Error(`Unknown Decoder type: ${t.type}`)}}var A0=jT,LT=class{constructor(t,e){let r=d0(t,"Tokenizer",["model","decoder","post_processor","pre_tokenizer","normalizer"]);if(r)throw new Error(r);let n=d0(e,"Config");if(n)throw new Error(n);this.tokenizer=t,this.config=e,this.normalizer=_0(this.tokenizer.normalizer),this.pre_tokenizer=b0(this.tokenizer.pre_tokenizer),this.model=oT(this.tokenizer.model,this.config),this.post_processor=E0(this.tokenizer.post_processor),this.decoder=A0(this.tokenizer.decoder),this.special_tokens=[],this.all_special_ids=[],this.added_tokens=[],this.tokenizer.added_tokens.forEach(o=>{let s=new IS(o);this.added_tokens.push(s),this.model.tokens_to_ids.set(s.content,s.id),this.model.vocab[s.id]=s.content,s.special&&(this.special_tokens.push(s.content),this.all_special_ids.push(s.id))}),(this.config.additional_special_tokens??[]).forEach(o=>{this.special_tokens.includes(o)||this.special_tokens.push(o)}),this.decoder&&(this.decoder.added_tokens=this.added_tokens,this.decoder.end_of_word_suffix=this.model.end_of_word_suffix),this.added_tokens_splitter=new FS(this.added_tokens.map(o=>o.content)),this.added_tokens_map=new Map(this.added_tokens.map(o=>[o.content,o])),this.remove_space=this.config.remove_space,this.clean_up_tokenization_spaces=this.config.clean_up_tokenization_spaces??!0,this.do_lowercase_and_remove_accent=this.config.do_lowercase_and_remove_accent??!1}encode(t,{text_pair:e=null,add_special_tokens:r=!0,return_token_type_ids:n=null}={}){let{tokens:o,token_type_ids:s}=this.tokenize_helper(t,{text_pair:e,add_special_tokens:r}),a=this.model.convert_tokens_to_ids(o),c={ids:a,tokens:o,attention_mask:new Array(a.length).fill(1)};return n&&s&&(c.token_type_ids=s),c}decode(t,e={}){if(!Array.isArray(t)||t.length===0||!$S(t[0]))throw Error("token_ids must be a non-empty array of integers.");let r=this.model.convert_ids_to_tokens(t);e.skip_special_tokens&&(r=r.filter(o=>!this.special_tokens.includes(o)));let n=this.decoder?this.decoder(r):r.join(" ");return this.decoder&&this.decoder.end_of_word_suffix&&(n=n.replaceAll(this.decoder.end_of_word_suffix," "),e.skip_special_tokens&&(n=n.trim())),(e.clean_up_tokenization_spaces??this.clean_up_tokenization_spaces)&&(n=Nd(n)),n}tokenize(t,{text_pair:e=null,add_special_tokens:r=!1}={}){return this.tokenize_helper(t,{text_pair:e,add_special_tokens:r}).tokens}encode_text(t){if(t===null)return null;let e=this.added_tokens_splitter.split(t);return e.forEach((r,n)=>{let o=this.added_tokens_map.get(r);o&&(o.lstrip&&n>0&&(e[n-1]=e[n-1].trimEnd()),o.rstrip&&n<e.length-1&&(e[n+1]=e[n+1].trimStart()))}),e.flatMap((r,n)=>{if(r.length===0)return[];if(this.added_tokens_map.has(r))return[r];if(this.remove_space===!0&&(r=r.trim().split(/\s+/).join(" ")),this.do_lowercase_and_remove_accent&&(r=zS(r)),this.normalizer!==null&&(r=this.normalizer(r)),r.length===0)return[];let o=this.pre_tokenizer!==null?this.pre_tokenizer(r,{section_index:n}):[r];return this.model(o)})}tokenize_helper(t,{text_pair:e=null,add_special_tokens:r=!0}){let n=this.encode_text(t),o=this.encode_text(e||null);return this.post_processor?this.post_processor(n,o,r):{tokens:Bt(n??[],o??[])}}token_to_id(t){return this.model.tokens_to_ids.get(t)}id_to_token(t){return this.model.vocab[t]}get_added_tokens_decoder(){let t=new Map;for(let e of this.added_tokens)t.set(e.id,e);return t}},k0=LT;var eu=class{type="huggingface";name;modelId;logger;options;tokenizer=null;cacheDir;safeModelName;constructor(e,r,n={}){this.modelId=e,this.logger=r,this.options=n,this.cacheDir=n.cacheDir||(0,_s.join)((0,S0.homedir)(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=(0,_s.join)(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:(0,_s.join)(e,"tokenizer.json"),tokenizerConfig:(0,_s.join)(e,"tokenizer_config.json")}}ensureDir(e){(0,yo.existsSync)(e)||(0,yo.mkdirSync)(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!(0,yo.existsSync)(e.tokenizerJson)||!(0,yo.existsSync)(e.tokenizerConfig))return null;let[r,n]=await Promise.all([bs.promises.readFile(e.tokenizerJson,"utf-8"),bs.promises.readFile(e.tokenizerConfig,"utf-8")]);return{tokenizerJson:JSON.parse(r),tokenizerConfig:JSON.parse(n)}}catch(e){return this.logger?.warn(`Failed to load from cache: ${e.message}`),null}}async downloadAndCache(){let e=this.getCachePaths(),r={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 n=new AbortController,o=setTimeout(()=>n.abort(),this.options.timeout||3e4);try{let[s,a]=await Promise.all([fetch(r.json,{signal:n.signal}),fetch(r.config,{signal:n.signal})]);if(!s.ok)throw new Error(`Failed to fetch tokenizer.json: ${s.statusText}`);let[c,l]=await Promise.all([s.json(),a.ok?a.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([bs.promises.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),bs.promises.writeFile(e.tokenizerConfig,JSON.stringify(l,null,2))]),{tokenizerJson:c,tokenizerConfig:l}}finally{clearTimeout(o)}}async initialize(){try{this.logger?.info(`Initializing HuggingFace tokenizer: ${this.modelId}`);let e=this.getCachePaths();this.ensureDir(this.cacheDir);let r=await this.loadFromCache()||await this.downloadAndCache();this.tokenizer=new k0(r.tokenizerJson,r.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 r=this.extractTextFromRequest(e);return this.tokenizer.encode(r).ids.length}catch(r){throw this.logger?.error(`Error counting tokens: ${r.message}`),r}}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 r=[],{messages:n,system:o,tools:s}=e;if(Array.isArray(n)){for(let a of n)if(typeof a.content=="string")r.push(a.content);else if(Array.isArray(a.content))for(let c of a.content)c.type==="text"&&c.text?r.push(c.text):c.type==="tool_use"&&c.input?r.push(JSON.stringify(c.input)):c.type==="tool_result"&&r.push(typeof c.content=="string"?c.content:JSON.stringify(c.content))}if(typeof o=="string")r.push(o);else if(Array.isArray(o)){for(let a of o)if(a.type==="text"){if(typeof a.text=="string")r.push(a.text);else if(Array.isArray(a.text))for(let c of a.text)c&&r.push(c)}}if(s)for(let a of s)a.name&&r.push(a.name),a.description&&r.push(a.description),a.input_schema&&r.push(JSON.stringify(a.input_schema));return r.join(" ")}};var tu=class{type="api";name;config;logger;options;constructor(e,r,n={}){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=r,this.options=n;try{let o=new URL(e.url);this.name=`api-${o.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 r=this.formatRequestBody(e),n={"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`,...this.config.headers},o=new AbortController,s=setTimeout(()=>o.abort(),this.options.timeout||3e4),a=await fetch(this.config.url,{method:"POST",headers:n,body:JSON.stringify(r),signal:o.signal});if(clearTimeout(s),!a.ok)throw new Error(`API tokenizer request failed: ${a.status} ${a.statusText}`);let c=await a.json();return this.extractTokenCount(c)}catch(r){throw r.name==="AbortError"?new Error("API tokenizer request timed out"):r}}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(r=>({role:r.role,content:this.extractTextFromMessage(r)})):[]}extractTextFromMessage(e){return typeof e.content=="string"?e.content:Array.isArray(e.content)?e.content.map(r=>r.type==="text"&&r.text?r.text:r.type==="tool_use"&&r.input?JSON.stringify(r.input):r.type==="tool_result"?typeof r.content=="string"?r.content:JSON.stringify(r.content):"").join(" "):""}extractConcatenatedText(e){let r=[];return e.messages&&e.messages.forEach(n=>{r.push(this.extractTextFromMessage(n))}),typeof e.system=="string"?r.push(e.system):Array.isArray(e.system)&&e.system.forEach(n=>{n.type==="text"&&(typeof n.text=="string"?r.push(n.text):Array.isArray(n.text)&&n.text.forEach(o=>{o&&r.push(o)}))}),e.tools&&e.tools.forEach(n=>{n.name&&r.push(n.name),n.description&&r.push(n.description),n.input_schema&&r.push(JSON.stringify(n.input_schema))}),r.join(" ")}extractTokenCount(e){try{let r=this.config.responseField,n=r.split("."),o=e;for(let s of n){if(o==null)throw new Error(`Field path '${r}' not found in response`);o=o[s]}if(typeof o!="number")throw new Error(`Expected number at field path '${r}', got ${typeof o}`);return o}catch(r){throw this.logger?.error(`Failed to extract token count from API response: ${r.message}. Response: ${JSON.stringify(e)}`),new Error(`Invalid response from API tokenizer: ${r.message}`)}}};var Sn=class{tokenizers=new Map;configService;logger;options;fallbackTokenizer;constructor(e,r,n={}){this.configService=e,this.logger=r,this.options={timeout:n.timeout??3e4,...n}}async initialize(){try{this.fallbackTokenizer=new ps("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){let r=this.getCacheKey(e);if(this.tokenizers.has(r))return this.tokenizers.get(r);let n;try{switch(e.type){case"tiktoken":n=new ps(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),n=new eu(e.model,this.logger,{timeout:this.options.timeout});break;case"api":n=new tu(e,this.logger,{timeout:this.options.timeout});break;default:throw new Error(`Unknown tokenizer type: ${e.type}`)}return this.logger?.info(`Calling initialize() on ${e.type} tokenizer...`),await n.initialize(),this.tokenizers.set(r,n),this.logger?.info(`Tokenizer initialized successfully: ${e.type} (${r})`),n}catch(o){return this.logger?.error(`Failed to initialize ${e.type} tokenizer: ${o.message}`),this.logger?.error(`Error stack: ${o.stack}`),this.fallbackTokenizer||await this.initialize(),this.fallbackTokenizer}}async countTokens(e,r){let n=r?await this.getTokenizer(r):this.fallbackTokenizer;return{tokenCount:await n.countTokens(e),tokenizerUsed:n.name,cached:!1}}getTokenizerConfigForModel(e,r){let o=(this.configService.get("providers")||[]).find(s=>s.name===e);if(o?.tokenizer)return o.tokenizer.models?.[r]?o.tokenizer.models[r]:o.tokenizer.default}dispose(){this.tokenizers.forEach(e=>{try{e.dispose()}catch(r){this.logger?.error(`Error disposing tokenizer: ${r}`)}}),this.tokenizers.clear()}getCacheKey(e){switch(e.type){case"tiktoken":return`tiktoken:${e.encoding||"cl100k_base"}`;case"huggingface":return`hf:${e.model}`;case"api":return`api:${e.url}`;default:return`unknown:${JSON.stringify(e)}`}}};var R0=require("tiktoken");var jd=class{capacity;cache;constructor(e){this.capacity=e,this.cache=new Map}get(e){if(!this.cache.has(e))return;let r=this.cache.get(e);return this.cache.delete(e),this.cache.set(e,r),r}put(e,r){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this.capacity){let n=this.cache.keys().next().value;n!==void 0&&this.cache.delete(n)}this.cache.set(e,r)}values(){return Array.from(this.cache.values())}},ru=new jd(100);var su=require("fs/promises"),uu=require("fs/promises"),iu=require("path"),_o=require("@wengine-ai/claude-code-router-shared");$n();var Ld=new Map;function nu(t,e){t&&Ld.set(t,{...e,provider:t,capturedAt:Date.now()})}function ou(t){let e=Ld.get(t);return e?{...e}:void 0}function v0(){return Array.from(Ld.values()).map(t=>({...t}))}ii();var xr=(0,R0.get_encoding)("cl100k_base"),$d=(t,e,r)=>{let n=0;return Array.isArray(t)&&t.forEach(o=>{typeof o.content=="string"?n+=xr.encode(o.content).length:Array.isArray(o.content)&&o.content.forEach(s=>{s.type==="text"?n+=xr.encode(s.text).length:s.type==="tool_use"?n+=xr.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(n+=xr.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?n+=xr.encode(e).length:Array.isArray(e)&&e.forEach(o=>{o.type==="text"&&(typeof o.text=="string"?n+=xr.encode(o.text).length:Array.isArray(o.text)&&o.text.forEach(s=>{n+=xr.encode(s||"").length}))}),r&&r.forEach(o=>{o.description&&(n+=xr.encode(o.name+o.description).length),o.input_schema&&(n+=xr.encode(JSON.stringify(o.input_schema)).length)}),n},x0=async(t,e)=>{if(t.sessionId){let r=await qd(t.sessionId);if(r){let n=(0,iu.join)(_o.HOME_DIR,r,"config.json"),o=(0,iu.join)(_o.HOME_DIR,r,`${t.sessionId}.json`);try{let s=JSON.parse(await(0,su.readFile)(o,"utf8"));if(s&&s.Router)return s.Router}catch{}try{let s=JSON.parse(await(0,su.readFile)(n,"utf8"));if(s&&s.Router)return s.Router}catch{}}}};function au(t){let e=t||"";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 O0(t){let e=au(t),r=e.includes("[1m]")||e.endsWith("[1m"),n=e.replace(/\[1m\]|\[1m$/g,""),o=n.match(/^ccr-(opus|sonnet|haiku)$/i);if(o)return{family:o[1].toLowerCase(),extended:r};let s=n.match(/claude-(?:\d+-\d+-|\d+-)?(sonnet|opus|haiku)(?:-|$)/i)||n.match(/claude-(sonnet|opus|haiku)(?:-|$)/i);return s?{family:s[1].toLowerCase(),extended:r}:{family:null,extended:r}}function UT(t,e){if(!e||!t)return null;let r=au(t);if(e[t])return e[t];if(e[r])return e[r];let{family:n}=O0(t);if(n&&e[n])return e[n];for(let[o,s]of Object.entries(e)){let a=au(o);if(a&&r.includes(a))return s}return null}function F0(t){return t?(t.input_tokens||0)+(t.cache_read_input_tokens||0)+(t.cache_creation_input_tokens||0):0}function T0(t){if(!t?.includes(","))return null;let[e,...r]=t.split(","),n=e.trim(),o=r.join(",").trim();return!n||!o?null:{providerName:n,routeModel:o}}function ot(t,e,r,n){let o=T0(t);if(!o)return t;let{providerName:s,routeModel:a}=o;if(n&&!r){let f=Fo(),p=f.getPromotion(s,a,n,e);if(p){let y=T0(p);if(y){let A=e.find(_=>_.name.toLowerCase()===y.providerName.toLowerCase()),w=A?.models?.find(_=>String(_).toLowerCase()===y.routeModel.toLowerCase());if(A&&w)return`${A.name},${w}`}f.clear(s,a,n)}}if(!r&&!tt().isAvailable(s,a))return null;let c=ou(s);if(c){let f=c.limitDaily!==void 0&&c.usedDailyBalance!==void 0&&c.usedDailyBalance>=c.limitDaily,p=c.totalBalance!==void 0&&c.usedBalance!==void 0&&c.usedBalance>=c.totalBalance;if(f||p)return null}let l=e.find(f=>f.name.toLowerCase()===s.toLowerCase()),d=l?.models?.find(f=>String(f).toLowerCase()===a.toLowerCase());return l&&d?`${l.name},${d}`:t}function Yt(t,e,r,n,o){let s=tt(),a=[r?.[t],n?.[t]];for(let c of a)if(!(!Array.isArray(c)||c.length===0))for(let l of c){let d=ot(l,e);if(d)return d}return null}function $T(t){return t.body.messages?.some(e=>e.role==="user"&&Array.isArray(e.content)&&e.content.some(r=>r.type==="image"||r.type==="image_url"||Array.isArray(r?.content)&&r.content.some(n=>n.type==="image"||n.type==="image_url")))}function qT(t){let e=au(t);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(n=>n.test(e))}function zT(t,e,r,n,o,s,a){let c=r.longContextThreshold||6e4,l=Math.max(e,F0(o)),d=t.modelFamily;if((s||l>2e5)&&r.extendedContext){let A=ot(r.extendedContext,n,!1,"extendedContext");if(A)return t.log.info(`Family: using extended context model (1M+), tokens: ${l}, estimated: ${e}, explicit: ${s}`),{model:A,scenarioType:"extendedContext",isFallback:!1};let w=Yt("extendedContext",n,r.fallback,a,d);if(w)return t.log.info(`Family: using extended context fallback model (1M+), tokens: ${l}, estimated: ${e}, explicit: ${s}`),{model:w,scenarioType:"extendedContext",isFallback:!0};t.log.warn("Family: extendedContext model unavailable (fail pool), skipping")}if(l>c&&(r.longContext||r.fallback?.longContext?.length||a?.longContext?.length)){let A=r.longContext?ot(r.longContext,n,!1,"longContext"):null;if(A)return t.log.info(`Family: using long context model, tokens: ${l}, estimated: ${e}`),{model:A,scenarioType:"longContext",isFallback:!1};let w=Yt("longContext",n,r.fallback,a,d);if(w)return t.log.info(`Family: using long context fallback model, tokens: ${l}, estimated: ${e}`),{model:w,scenarioType:"longContext",isFallback:!0};t.log.warn("Family: no healthy longContext model available, falling through to other scenarios")}if(Array.isArray(t.body.tools)&&t.body.tools.some(A=>A.type?.startsWith("web_search"))&&r.webSearch){let A=ot(r.webSearch,n,!1,"webSearch");if(A)return{model:A,scenarioType:"webSearch",isFallback:!1};let w=Yt("webSearch",n,r.fallback,a,d);if(w)return t.log.info("Family: using webSearch fallback model"),{model:w,scenarioType:"webSearch",isFallback:!0};t.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(t.body.thinking&&r.think){let A=ot(r.think,n,!1,"think");if(A)return{model:A,scenarioType:"think",isFallback:!1};let w=Yt("think",n,r.fallback,a,d);if(w)return t.log.info("Family: using think fallback model"),{model:w,scenarioType:"think",isFallback:!0};t.log.warn("Family: think model unavailable (fail pool), skipping")}if(r.default){let A=ot(r.default,n,!1,"default");if(A)return{model:A,scenarioType:"default",isFallback:!1};let w=Yt("default",n,r.fallback,a,d);if(w)return t.log.info("Family: using default fallback model"),{model:w,scenarioType:"default",isFallback:!0};t.log.warn("Family: default model unavailable (fail pool), skipping")}return null}var HT=async(t,e,r,n)=>{let o=await x0(t,r),s=r.get("providers")||[],a=o||r.get("Router"),c=r.get("fallback");if(t.body.model.includes(",")){let m=ot(t.body.model,s,!1,"default");if(m)return{model:m,scenarioType:"default"};t.log.warn(`Explicit model ${t.body.model} unavailable (fail pool), trying fallback`);let v=Yt("default",s,void 0,c);if(v)return t.log.info(`Using fallback for explicit model: ${v}`),{model:v,scenarioType:"default"};t.log.warn(`No fallback available for explicit model ${t.body.model}, continuing through routing logic`)}let{family:l,extended:d}=O0(t.body.model),f=a?.families?.[l||""];if(f&&a?.enableFamilyRouting){t.log.info(`Using model family routing for: ${l}${d?" (1M)":""}`),t.modelFamily=l,t.familyFallback=f.fallback;let m=zT(t,e,f,s,n,d,c);if(m)return{model:m.model,scenarioType:m.scenarioType}}let p=UT(t.body.model,a?.models);if(p){let m=ot(p,s,!1,"modelMapping");if(m)return t.log.info(`Using mapped model for ${t.body.model}: ${p}`),{model:m,scenarioType:"modelMapping"};t.log.warn(`Mapped model ${p} unavailable (fail pool), skipping`)}let y=Math.max(e,F0(n)),A=a?.extendedContextThreshold||2e5;if(y>A&&a?.extendedContext){t.log.info(`Using extended context (1M) model due to token count: ${y}, estimated: ${e}, threshold: ${A}`);let m=ot(a.extendedContext,s,!1,"extendedContext");if(m)return{model:m,scenarioType:"extendedContext"};t.log.warn(`Extended context model ${a.extendedContext} unavailable (fail pool), trying fallback`);let v=Yt("extendedContext",s,void 0,c);if(v)return{model:v,scenarioType:"extendedContext"}}let w=a?.longContextThreshold||6e4;if(y>w&&a?.longContext){t.log.info(`Using long context model due to token count: ${y}, estimated: ${e}, threshold: ${w}`);let m=ot(a.longContext,s,!1,"longContext");if(m)return{model:m,scenarioType:"longContext"};t.log.warn(`Long context model ${a.longContext} unavailable (fail pool), trying fallback`);let v=Yt("longContext",s,void 0,c);if(v)return{model:v,scenarioType:"longContext"}}if(t.body?.system?.length>1&&t.body?.system[1]?.text?.startsWith("<CCR-SUBAGENT-MODEL>")){let m=t.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);if(m)return t.body.system[1].text=t.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${m[1]}</CCR-SUBAGENT-MODEL>`,""),{model:m[1],scenarioType:"default"}}let g=r.get("Router");if(t.body.model?.includes("claude")&&t.body.model?.includes("haiku")&&g?.background){t.log.info(`Using background model for ${t.body.model}`);let m=ot(g.background,s,!1,"background");if(m)return{model:m,scenarioType:"background"};t.log.warn(`Background model ${g.background} unavailable (fail pool), falling through`)}if(Array.isArray(t.body.tools)&&t.body.tools.some(m=>m.type?.startsWith("web_search"))&&a?.webSearch){let m=ot(a.webSearch,s,!1,"webSearch");if(m)return{model:m,scenarioType:"webSearch"};t.log.warn(`WebSearch model ${a.webSearch} unavailable (fail pool), trying fallback`);let v=Yt("webSearch",s,void 0,c);if(v)return{model:v,scenarioType:"webSearch"}}if(t.body.thinking&&a?.think){t.log.info(`Using think model for ${t.body.thinking}`);let m=ot(a.think,s,!1,"think");if(m)return{model:m,scenarioType:"think"};t.log.warn(`Think model ${a.think} unavailable (fail pool), trying fallback`);let v=Yt("think",s,void 0,c);if(v)return{model:v,scenarioType:"think"}}if(a?.default){let m=ot(a.default,s,!1,"default");if(m)return{model:m,scenarioType:"default"};t.log.warn(`Default model ${a.default} unavailable (fail pool), trying fallback`);let v=Yt("default",s,void 0,c);if(v)return{model:v,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},cu=async(t,e,r)=>{let{configService:n,event:o}=r;if(t.originalModel=t.body.model,t.body.metadata?.user_id){let A=t.body.metadata.user_id.split("_session_");A.length>1&&(t.sessionId=A[1])}let a=await x0(t,n)||n.get("Router"),c=n.get("providers")||[],l=ru.get(t.sessionId),{messages:d,system:f=[],tools:p}=t.body,y=n.get("REWRITE_SYSTEM_PROMPT");if(y&&f.length>1&&f[1]?.text?.includes("<env>")){let A=await(0,su.readFile)(y,"utf-8");f[1].text=`${A}<env>${f[1].text.split("<env>").pop()}`}try{let[A,w]=t.body.model.split(","),_=r.tokenizerService?.getTokenizerConfigForModel(A,w),g;r.tokenizerService?g=(await r.tokenizerService.countTokens({messages:d,system:f,tools:p},_)).tokenCount:g=$d(d,f,p),t.tokenCount=g;let m,v=n.get("CUSTOM_ROUTER_PATH");if(v)try{m=await require(v)(t,n.getAll(),{event:o})}catch(b){t.log.error(`failed to load custom router: ${b.message}`)}if(m)t.scenarioType="default";else{let b=await HT(t,g,n,l);m=b.model,t.scenarioType=b.scenarioType}if(a?.image&&m!==a.image&&$T(t)&&!qT(m)){let b=ot(a.image,c,!1,"image");b?(t.log.info(`Using image model fallback for ${m}`),m=b,t.scenarioType="image"):t.log.warn(`Image model ${a.image} unavailable (fail pool), keeping ${m}`)}t.body.model=m}catch(A){t.log.error(`Error in router middleware: ${A.message}`),t.body.model=a?.default,t.scenarioType="default"}},Or=new Map,D0=1e3;function Ud(){if(Or.size<=D0)return;let t=[...Or.keys()].slice(0,Or.size-D0);for(let e of t)Or.delete(e)}var qd=async t=>{if(Or.has(t)){let e=Or.get(t);return!e||e===""?null:e}try{let e=await(0,uu.opendir)(_o.CLAUDE_PROJECTS_DIR),r=[];for await(let s of e)s.isDirectory()&&r.push(s.name);let n=r.map(async s=>{let a=(0,iu.join)(_o.CLAUDE_PROJECTS_DIR,s,`${t}.jsonl`);try{return(await(0,uu.stat)(a)).isFile()?s:null}catch{return null}}),o=await Promise.all(n);for(let s of o)if(s)return Or.set(t,s),Ud(),s;return Or.set(t,""),Ud(),null}catch(e){return console.error("Error searching for project by session:",e),Or.set(t,""),Ud(),null}};$n();var Fr=class{async fetchJson(e,r,n,o){if(!r.apiKey)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${r.apiKey}`},signal:AbortSignal.timeout(n)};if(o)try{let{ProxyAgent:a}=await import("undici");s.dispatcher=new a(new URL(o).toString())}catch{}try{let a=await fetch(e,s);return a.ok?await a.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}},zd=class extends Fr{async queryQuota(e,r,n){let o=await this.fetchJson("https://api.deepseek.com/user/balance",e,r,n),a=(Array.isArray(o?.balance_infos)?o.balance_infos:[]).find(l=>we(l?.total_balance)!==void 0);if(!a)return null;let c=we(a.total_balance);return c===void 0?null:{totalBalance:c,currency:typeof a.currency=="string"?a.currency:void 0}}},Hd=class extends Fr{async queryQuota(e,r,n){let s=(await this.fetchJson("https://openrouter.ai/api/v1/key",e,r,n))?.data;if(!s||typeof s!="object")return null;let a={},c=we(s.limit),l=we(s.limit_remaining),d=we(s.usage),f=we(s.usage_daily);return c!==void 0&&(a.totalBalance=c),l!==void 0&&(a.remainingBalance=l),d!==void 0&&(a.usedBalance=d),f!==void 0&&(a.usedDailyBalance=f),this.hasQuotaData(a)?a:null}},Wd=class extends Fr{async queryQuota(e,r,n){let o=XT(e.baseUrl),a=(await this.fetchJson(o,e,r,n))?.data;if(!a||typeof a!="object")return null;let c={},l=we(a.totalBalance),d=we(a.balance);return l!==void 0&&(c.totalBalance=l),d!==void 0&&(c.remainingBalance=d),l!==void 0&&d!==void 0&&(c.usedBalance=Math.max(0,l-d)),this.hasQuotaData(c)?c:null}},Gd=class extends Fr{async queryQuota(e,r,n){if(!e.apiKey)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:e.apiKey.trim()},signal:AbortSignal.timeout(r)};if(n)try{let{ProxyAgent:a}=await import("undici");s.dispatcher=new a(new URL(n).toString())}catch{}try{let a=await fetch("https://api.z.ai/api/monitor/usage/quota/limit",s);if(!a.ok)return null;let c=await a.json(),l=Array.isArray(c?.data?.limits)?c.data.limits:[],d=null,f=null,p=null;for(let A of l)A.type==="TOKENS_LIMIT"?A.unit===3?d=A:A.unit===6?f=A:d||(d=A):A.type==="TIME_LIMIT"&&(p=A);let y={};if(d){let A=we(d.currentValue),w=we(d.usage),_=we(d.remaining),g=we(d.percentage);A!==void 0?y.usedDailyBalance=A:g!==void 0&&(y.usedDailyBalance=g,y.limitDaily=100),w!==void 0?y.limitDaily=w:A!==void 0&&_!==void 0&&(y.limitDaily=A+_),d.nextResetTime&&(y.resetTime=new Date(d.nextResetTime).toISOString())}if(f){let A=we(f.currentValue),w=we(f.usage),_=we(f.remaining),g=we(f.percentage);A!==void 0?y.usedBalance=A:g!==void 0&&(y.usedBalance=g,y.totalBalance=100),w!==void 0?y.totalBalance=w:A!==void 0&&_!==void 0&&(y.totalBalance=A+_),f.nextResetTime&&!y.resetTime&&(y.resetTime=new Date(f.nextResetTime).toISOString())}if(p){if(y.usedDailyBalance===void 0){let A=we(p.currentValue);A!==void 0&&(y.usedDailyBalance=A)}if(y.limitDaily===void 0){let A=we(p.usage),w=we(p.currentValue),_=we(p.remaining);A!==void 0?y.limitDaily=A:w!==void 0&&_!==void 0&&(y.limitDaily=w+_)}p.nextResetTime&&!y.resetTime&&(y.resetTime=new Date(p.nextResetTime).toISOString())}return this.hasQuotaData(y)?y:null}catch{return null}}},Jd=class extends Fr{async queryQuota(e,r,n){if(!e.quotaToken)return null;let o={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(r)};if(n)try{let{ProxyAgent:c}=await import("undici");o.dispatcher=new c(new URL(n).toString())}catch{}let 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{let c=await fetch("https://bailian-cs.console.aliyun.com/data/api.json?action=BroadScopeAspnGateway&product=sfm_bailian&api=zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2",{...o,body:a});if(!c.ok)return null;let d=(await c.json())?.data?.DataV2?.data?.data?.codingPlanInstanceInfos;if(!Array.isArray(d)||d.length===0)return null;let f=d[0]?.codingPlanQuotaInfo;if(!f)return null;let p={},y=we(f.per5HourUsedQuota),A=we(f.per5HourTotalQuota);y!==void 0&&(p.usedDailyBalance=y),A!==void 0&&(p.limitDaily=A);let w=we(f.perWeekUsedQuota),_=we(f.perWeekTotalQuota);return w!==void 0&&(p.usedBalance=w),_!==void 0&&(p.totalBalance=_),this.hasQuotaData(p)?p:null}catch{return null}}},Vd=class extends Fr{async queryQuota(e,r,n){let o=e.quotaToken||e.apiKey;if(!o)return null;let s={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${o.trim()}`},signal:AbortSignal.timeout(r)};if(n)try{let{ProxyAgent:a}=await import("undici");s.dispatcher=new a(new URL(n).toString())}catch{}try{let a=await fetch("https://api.kimi.com/coding/v1/usages",s);if(!a.ok)return null;let c=await a.json(),l={},d=Array.isArray(c?.limits)?c.limits:[];if(d.length>0&&d[0]?.detail){let p=d[0].detail,y=we(p.limit),A=we(p.remaining),w=p.resetTime;y!==void 0&&A!==void 0&&(l.usedDailyBalance=Math.max(0,y-A),l.limitDaily=y),w&&(l.resetTime=new Date(w).toISOString())}let f=c?.usage;if(f){let p=we(f.limit),y=we(f.remaining),A=we(f.used),w=f.resetTime;p!==void 0&&(l.totalBalance=p,A!==void 0?l.usedBalance=A:y!==void 0&&(l.usedBalance=Math.max(0,p-y),l.remainingBalance=y)),w&&(l.resetTime7d=new Date(w).toISOString(),l.resetTime||(l.resetTime=l.resetTime7d))}return this.hasQuotaData(l)?l:null}catch{return null}}},Kd=class extends Fr{async queryQuota(e,r,n){let o=e.quotaToken||e.apiKey;if(!o)return null;let s=Yd(e.baseUrl)||"api.minimaxi.com",a=s.endsWith(".minimax.io")||s==="minimax.io"?"api.minimax.io":"api.minimaxi.com",c={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${o.trim()}`},signal:AbortSignal.timeout(r)};if(n)try{let{ProxyAgent:l}=await import("undici");c.dispatcher=new l(new URL(n).toString())}catch{}try{let l=await fetch(`https://${a}/v1/api/openplatform/coding_plan/remains`,c);if(!l.ok)return null;let d=await l.json();if(d?.base_resp){let b=d.base_resp.status_code;if(b!==void 0&&b!==0)return null}let f=Array.isArray(d?.model_remains)?d.model_remains:[];if(f.length===0)return null;let p=f[0],y={},A=we(p.current_interval_total_count),w=we(p.current_interval_usage_count),_=we(p.end_time);A!==void 0&&w!==void 0&&(y.usedDailyBalance=Math.max(0,A-w),y.limitDaily=A),_&&(y.resetTime=new Date(_).toISOString());let g=we(p.current_weekly_total_count),m=we(p.current_weekly_usage_count),v=we(p.weekly_end_time);return g!==void 0&&m!==void 0&&(y.usedBalance=Math.max(0,g-m),y.remainingBalance=m,y.totalBalance=g),v&&!y.resetTime&&(y.resetTime=new Date(v).toISOString()),this.hasQuotaData(y)?y:null}catch{return null}}},WT=new zd,GT=new Hd,JT=new Wd,VT=new Gd,KT=new Jd,YT=new Vd,QT=new Kd;function lu(t){let e=Yd(t);return e?e==="kimi.com"||e.endsWith(".kimi.com")||e==="moonshot.cn"||e.endsWith(".moonshot.cn")?YT:e==="minimaxi.com"||e.endsWith(".minimaxi.com")||e==="minimax.io"||e.endsWith(".minimax.io")?QT:e==="deepseek.com"||e.endsWith(".deepseek.com")?WT:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?GT:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?JT:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?VT:e==="dashscope.aliyuncs.com"||e.endsWith(".dashscope.aliyuncs.com")?KT:null:null}function XT(t){let e=Yd(t);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function Yd(t){try{return new URL(t).hostname.toLowerCase()}catch{return null}}function we(t){if(typeof t=="number")return Number.isFinite(t)?t:void 0;if(typeof t!="string")return;let e=t.trim().replace(/,/g,"");if(!e)return;let r=Number(e);return Number.isFinite(r)?r:void 0}var ZT={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,initialDelayMs:5e3,excludeProviders:[]};function eD(t){try{let e=new URL(t),r=e.pathname;return r.endsWith("/v1/messages")?r=r.slice(0,-12):r.endsWith("/messages")?r=r.slice(0,-9):r.endsWith("/v1/chat/completions")?r=r.slice(0,-20):r.endsWith("/chat/completions")&&(r=r.slice(0,-17)),r.endsWith("/v1")||(r=r.endsWith("/")?`${r}v1`:`${r}/v1`),e.pathname=`${r}/models`,e.toString()}catch{return null}}function tD(t){try{let e=new URL(t).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function Qd(t,e,r){let n=eD(t.baseUrl);if(!n)return{success:!1,error:"Cannot derive models endpoint from baseUrl"};try{let o={method:"GET",headers:{Authorization:`Bearer ${t.apiKey}`},signal:AbortSignal.timeout(e)};if(r)try{let{ProxyAgent:c}=await import("undici");o.dispatcher=new c(new URL(r).toString())}catch{}let s=await fetch(n,o);if(s.headers&&Io(t.name,t.baseUrl,s.headers),s.ok)return{success:!0,headers:s.headers};if(s.status===429)return{success:!1,error:`HTTP 429: ${(await s.text().catch(()=>"")).slice(0,100)||"Rate limited"}`,headers:s.headers};if(s.status>=400&&s.status<500)return{success:!0,headers:s.headers};let a=await s.text().catch(()=>"");return{success:!1,error:`HTTP ${s.status}: ${a.slice(0,100)}`,headers:s.headers}}catch(o){return{success:!1,error:o?.message||o?.toString()||"Unknown probe error"}}}var Cs=class{config;quotaProbeTimer;healthProbeTimer;getProviders;getHttpsProxy;logger;running=!1;constructor(e,r,n,o){this.config={...ZT,...r},this.getProviders=e,this.getHttpsProxy=n,this.logger=o}start(){!this.config.enabled||this.running||(this.running=!0,setTimeout(()=>{this.running&&(this.runQuotaProbe(),this.runHealthProbe())},this.config.initialDelayMs),this.quotaProbeTimer=setInterval(()=>this.runQuotaProbe(),this.config.quotaProbeIntervalMinutes*60*1e3),this.healthProbeTimer=setInterval(()=>this.runHealthProbe(),300*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.logger?.info("Active probe service stopped")}async runQuotaProbe(){let e=this.getProviders().filter(a=>a?.name&&!this.config.excludeProviders.includes(a.name)),r=this.getHttpsProxy?.(),n=tt();if(e.length===0)return;let o=[];for(let a of e){let c=lu(a.baseUrl),l=Array.isArray(a.models)?a.models:[];c&&o.push({provider:a.name,models:l,type:"quota-adapter",promise:c.queryQuota(a,this.config.probeTimeoutMs,r).then(d=>{if(d){nu(a.name,d),this.logger?.debug?.(`Stored quota probe result for ${a.name}`);let f=d.limitDaily!==void 0&&d.usedDailyBalance!==void 0&&d.usedDailyBalance>=d.limitDaily,p=d.totalBalance!==void 0&&d.usedBalance!==void 0&&d.usedBalance>=d.totalBalance;if(f||p){let y=f?`Quota exhausted: 5h limit reached (${d.usedDailyBalance}/${d.limitDaily})`:`Quota exhausted: 7d balance depleted (${d.usedBalance}/${d.totalBalance})`;for(let A of l)n.forceOpen(a.name,A,y);this.logger?.warn?.(`${y} for ${a.name}, marked as unhealthy`)}else for(let y of l){let A=n.getState(a.name,y);A&&A.status==="open"&&A.lastError?.includes("Quota exhausted")&&(n.recover(a.name,y),this.logger?.info?.(`Quota recovered for ${a.name} (${y}), marked as healthy`))}}})}),tD(a.baseUrl)&&o.push({provider:a.name,models:l,type:"rate-limit-headers",promise:Qd(a,this.config.probeTimeoutMs,r).then(()=>{})})}if(o.length===0)return;this.logger?.debug?.(`Running quota probe with ${o.length} tasks for ${e.length} providers`);let s=await Promise.allSettled(o.map(a=>a.promise));for(let a=0;a<o.length;a++){let c=s[a];if(c.status==="rejected"){let l=o[a];this.logger?.warn?.(`Quota probe failed for ${l.provider} (${l.type}): ${c.reason}`)}}}async runHealthProbe(){let e=tt(),r=this.getProviders().filter(s=>s?.name&&!this.config.excludeProviders.includes(s.name)),n=this.getHttpsProxy?.();if(r.length===0)return;this.logger?.debug?.(`Running health probe for ${r.length} providers`);let o=await Promise.allSettled(r.map(s=>Qd(s,this.config.probeTimeoutMs,n)));for(let s=0;s<r.length;s++){let a=r[s],c=o[s],l=Array.isArray(a.models)?a.models:[];if(c.status==="fulfilled"){let d=c.value;if(d.success){let f=0;for(let p of l){let y=e.getState(a.name,p);if(y&&y.status==="open"&&y.lastError?.includes("Quota exhausted")){this.logger?.debug?.(`Skipping health probe recovery for ${a.name} (${p}) because quota is exhausted`);continue}e.recordSuccess(a.name,p),f++}f>0&&this.logger?.info?.(`Health probe succeeded for ${a.name}`)}else{for(let f of l)e.recordFailure(a.name,f,d.error);this.logger?.warn?.(`Health probe failed for ${a.name}: ${d.error}`)}}else{for(let d of l)e.recordFailure(a.name,d,c.reason?.message||"Probe error");this.logger?.warn?.(`Health probe error for ${a.name}: ${c.reason}`)}}}async probeProviderManually(e){let n=this.getProviders().find(c=>c.name===e);if(!n)return this.logger?.warn?.(`Provider ${e} not found for manual probe`),!1;let o=this.getHttpsProxy?.(),s=await Qd(n,this.config.probeTimeoutMs,o),a=Array.isArray(n.models)?n.models:[];if(s.success)for(let c of a)tt().recordSuccess(n.name,c);else for(let c of a)tt().recordFailure(n.name,c,s.error);return s.success}},Xr=null;function Xd(t,e,r,n){return Xr||(Xr=new Cs(t,e,r,n)),Xr}function du(t,e,r,n){let o=Xd(t,e,r,n);return o.start(),o}function fu(){Xr&&Xr.stop()}function P0(){Xr&&(Xr.stop(),Xr=null)}var Zd=class{plugins=new Map;pluginInstances=new Map;registerPlugin(e,r={}){this.pluginInstances.set(e.name,e),this.plugins.set(e.name,{name:e.name,enabled:r.enabled!==!1,options:r})}async enablePlugin(e,r){let n=this.plugins.get(e),o=this.pluginInstances.get(e);if(!n||!o)throw new Error(`Plugin ${e} not found`);n.enabled&&await r.register(o.register,n.options)}async enablePlugins(e){for(let[r,n]of this.plugins)if(n.enabled)try{await this.enablePlugin(r,e)}catch(o){let s=o instanceof Error?o.message:String(o);e.log?.error(`Failed to enable plugin ${r}: ${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,r){let n=this.plugins.get(e);n&&(n.enabled=r)}removePlugin(e){this.plugins.delete(e),this.pluginInstances.delete(e)}clear(){this.plugins.clear(),this.pluginInstances.clear()}},ef=new Zd;var z0=Pe(j0(),1);var vn=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,r)=>{this.buffer+=e;let n=this.buffer.split(`
345
345
  `);this.buffer=n.pop()||"";for(let o of n){let s=this.processLine(o);s&&r.enqueue(s)}},flush:e=>{if(this.buffer.trim()){let r=[];this.processLine(this.buffer.trim(),r),r.forEach(n=>e.enqueue(n))}Object.keys(this.currentEvent).length>0&&e.enqueue(this.currentEvent)}})}processLine(e,r){if(!e.trim()){if(Object.keys(this.currentEvent).length>0){let n={...this.currentEvent};return this.currentEvent={},r?(r.push(n),null):n}return null}if(e.startsWith("event:"))this.currentEvent.event=e.slice(6).trim();else if(e.startsWith("data:")){let n=e.slice(5).trim();if(n==="[DONE]")this.currentEvent.data={type:"done"};else try{this.currentEvent.data=JSON.parse(n)}catch{this.currentEvent.data={raw:n,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 ws=class extends TransformStream{constructor(){super({transform:(e,r)=>{let n="";e.event&&(n+=`event: ${e.event}
346
346
  `),e.id&&(n+=`id: ${e.id}
347
347
  `),e.retry&&(n+=`retry: ${e.retry}