@wengine-ai/llms 2.0.78 → 2.0.79

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 ed
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 ds,zd=Be(()=>{"use strict";ds=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}`)}}}});import{writeFileSync as cD,existsSync as lD,mkdirSync as dD}from"fs";import{join as b0}from"path";import{tmpdir as fD}from"os";var fs,Hd=Be(()=>{"use strict";fs=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let r=fD();this.baseDir=b0(r,this.config.subdirectory),this.ensureDir()}ensureDir(){try{lD(this.baseDir)||dD(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 b0(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 cD(s,JSON.stringify(o,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Wd,go,C0=Be(()=>{"use strict";qd();zd();Hd();Wd=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 ls(e.config);case"webhook":return new ds(e.config);case"temp-file":return new fs(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)}},go=new Wd});import pD from"fastify";import mD from"@fastify/cors";var lh=jr(Iu(),1);import{readFileSync as Cb,existsSync as uh}from"fs";import{join as ch}from"path";import{config as wb}from"dotenv";var Fn=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:ch(process.cwd(),this.options.jsonPath);if(uh(e))try{let r=Cb(e,"utf-8"),n=lh.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:ch(process.cwd(),this.options.envPath);if(uh(e))try{let r=wb({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 st(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 dh(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)}import{ProxyAgent as Eb}from"undici";function fh(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 Eb(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 hh="2.0.78";Pn();var Mu=new Map,Tb=["x-ratelimit-remaining","x-ratelimit-remaining-requests","x-ratelimit-remaining-tokens"],Db=["x-ratelimit-limit","x-ratelimit-limit-requests","x-ratelimit-limit-tokens"],Rb=["x-ratelimit-reset","x-ratelimit-reset-requests","x-ratelimit-reset-tokens"];function Do(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=ju(n,Tb),s=ju(n,Db),a=ju(n,Rb);(o||s||a)&&Mu.set(t,{provider:t,remaining:mh(o),limit:mh(s),reset:Fb(a),capturedAt:Date.now()})}function xb(t){return Mu.get(t)}function Ob(){return Array.from(Mu.values())}function ju(t,e){for(let r of e){let n=t(r);if(n)return n}return null}function mh(t){if(!t)return null;let e=Number(t.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function Fb(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=Pb(e);return o===null?null:Math.floor((Date.now()+o)/1e3)}function Pb(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}Js();async function Ib(t,e,r,n){let o=t.body,s=t.provider,a=r.providerService.getProvider(s);if(!a)throw st(`Provider '${s}' not found`,404,"provider_not_found");try{let{requestBody:c,config:l,bypass:d}=await gh(o,a,n,t.headers,{req:t}),f=await yh(c,l,a,r,d,n,{req:t});try{a?.baseUrl&&f?.headers&&Do(s,a.baseUrl,f.headers)}catch{}let m=await _h(c,f,a,n,d,{req:t});return bh(m,e,o)}catch(c){let l=await Nb(t,e,r,n,c);if(l)return l;throw c}}async function Nb(t,e,r,n,o){let s=t.scenarioType||"default",a=t.familyFallback,c=t.modelFamily,l=r.configService.get("fallback"),d=ft();if(!r.configService.get("Router")?.enableFallback)return t.log.info("Fallback disabled by configuration, skipping fallback attempts"),null;let m=p=>{let[k,...y]=p.split(","),E=k?.trim(),D=y.join(",").trim();return!E||!D?null:{provider:E,model:D,key:`${E},${D}`}},w=[];if(c){let p=a?.[s];Array.isArray(p)&&p.length>0?w.push({name:`${c}/${s}`,models:p}):t.log.warn(`No ${c} fallback configured for ${s}; will try global ${s} fallback`)}if(Array.isArray(l?.[s])&&l[s].length>0&&w.push({name:`global/${s}`,models:l[s]}),w.length===0)return null;let v=t.provider||"",A=t.body.model||"",b=new Set;v&&A&&(d.recordFailure(v,A,o?.message),b.add(`${v},${A}`));let g=w.reduce((p,k)=>p+k.models.length,0);t.log.warn(`Request failed for ${s}, trying ${g} fallback models across ${w.length} fallback stage(s)`);for(let p of w){t.log.info(`Trying ${p.name} fallback stage with ${p.models.length} models`);for(let k of p.models){let y=m(k);if(!y){t.log.warn(`Fallback model '${k}' is invalid, skipping`);continue}if(b.has(y.key))continue;b.add(y.key);let E=y.provider,D=y.model;try{if(!d.isAvailable(E,D)){t.log.warn(`Fallback model ${k} unavailable (fail pool), skipping`);continue}t.log.info(`Trying fallback model: ${k}`);let O={...t.body};O.model=D;let q={...t,provider:E,body:O},J=r.providerService.getProvider(E);if(!J){t.log.warn(`Fallback provider '${E}' not found, skipping`);continue}let{requestBody:N,config:Y,bypass:re}=await gh(O,J,n,t.headers,{req:q}),ee=await yh(N,Y,J,r,re,n,{req:q});try{J?.baseUrl&&ee?.headers&&Do(E,J.baseUrl,ee.headers)}catch{}let ue=await _h(N,ee,J,n,re,{req:q});return t.log.info(`Fallback model ${k} succeeded`),d.recordSuccess(E,D),v&&A&&(To().promote(v,A,s,E,D),t.log.info(`Promoted fallback model ${E},${D} for ${v},${A}:${s}`),d.forceOpen(v,A,o?.message),t.log.info(`Marked original model ${v},${A} as unavailable`)),t.provider=E,t.body=O,bh(ue,e,O)}catch(O){d.recordFailure(E,D,O.message),t.log.warn(`Fallback model ${y.key} failed: ${O.message}`),r.recordUsage?.({provider:E,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 gh(t,e,r,n,o){let s=JSON.parse(JSON.stringify(t)),a={},c=!1;if(c=Bb(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 Bb(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 yh(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 m=e.headers||{};f.config?.headers&&(m={...m,...f.config.headers},delete m.host,delete f.config.headers),e={...e,...f.config,headers:m}}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 fh(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}`),st(`Error from provider(${r.name},${t.model}: ${d.status}): ${f}`,d.status,"provider_response_error")}return d}async function _h(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 bh(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 Lu=async t=>{t.get("/",async()=>({message:"LLMs API",version:hh})),t.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),t.get("/providers/health",async()=>({states:ft().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)=>Ib(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 st("Provider name is required",400,"invalid_request");if(!s||!jb(s))throw st("Valid base URL is required",400,"invalid_request");if(!a?.trim())throw st("API key is required",400,"invalid_request");if(!c||!Array.isArray(c)||c.length===0)throw st("At least one model is required",400,"invalid_request");if(t.providerService.getProvider(r.body.name))throw st(`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 st("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 st("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 st("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 st("Provider not found",404,"provider_not_found");return{message:`Provider ${r.body.enabled?"enabled":"disabled"} successfully`}})};function jb(t){try{return new URL(t),!0}catch{return!1}}var In=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 Je=[];for(let t=0;t<256;++t)Je.push((t+256).toString(16).slice(1));function Ch(t,e=0){return(Je[t[e+0]]+Je[t[e+1]]+Je[t[e+2]]+Je[t[e+3]]+"-"+Je[t[e+4]]+Je[t[e+5]]+"-"+Je[t[e+6]]+Je[t[e+7]]+"-"+Je[t[e+8]]+Je[t[e+9]]+"-"+Je[t[e+10]]+Je[t[e+11]]+Je[t[e+12]]+Je[t[e+13]]+Je[t[e+14]]+Je[t[e+15]]).toLowerCase()}import{randomFillSync as Mb}from"crypto";var Ys=new Uint8Array(256),Ks=Ys.length;function Uu(){return Ks>Ys.length-16&&(Mb(Ys),Ks=0),Ys.slice(Ks,Ks+=16)}import{randomUUID as Lb}from"crypto";var $u={randomUUID:Lb};function Ub(t,e,r){if($u.randomUUID&&!e&&!t)return $u.randomUUID();t=t||{};let n=t.random??t.rng?.()??Uu();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 Ch(n)}var Yt=Ub;var wh=t=>t<=0?"none":t<=1024?"low":t<=8192?"medium":"high";var Eh=(t,e)=>(t.includes("base64")&&(t=t.split("base64").pop(),t.startsWith(",")&&(t=t.slice(1))),`data:${e};base64,${t}`);var Qs=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"?Eh(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 ds,zd=Be(()=>{"use strict";ds=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}`)}}}});import{writeFileSync as cD,existsSync as lD,mkdirSync as dD}from"fs";import{join as b0}from"path";import{tmpdir as fD}from"os";var fs,Hd=Be(()=>{"use strict";fs=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let r=fD();this.baseDir=b0(r,this.config.subdirectory),this.ensureDir()}ensureDir(){try{lD(this.baseDir)||dD(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 b0(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 cD(s,JSON.stringify(o,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Wd,go,C0=Be(()=>{"use strict";qd();zd();Hd();Wd=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 ls(e.config);case"webhook":return new ds(e.config);case"temp-file":return new fs(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)}},go=new Wd});import pD from"fastify";import mD from"@fastify/cors";var lh=jr(Iu(),1);import{readFileSync as Cb,existsSync as uh}from"fs";import{join as ch}from"path";import{config as wb}from"dotenv";var Fn=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:ch(process.cwd(),this.options.jsonPath);if(uh(e))try{let r=Cb(e,"utf-8"),n=lh.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:ch(process.cwd(),this.options.envPath);if(uh(e))try{let r=wb({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 st(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 dh(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)}import{ProxyAgent as Eb}from"undici";function fh(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 Eb(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 hh="2.0.79";Pn();var Mu=new Map,Tb=["x-ratelimit-remaining","x-ratelimit-remaining-requests","x-ratelimit-remaining-tokens"],Db=["x-ratelimit-limit","x-ratelimit-limit-requests","x-ratelimit-limit-tokens"],Rb=["x-ratelimit-reset","x-ratelimit-reset-requests","x-ratelimit-reset-tokens"];function Do(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=ju(n,Tb),s=ju(n,Db),a=ju(n,Rb);(o||s||a)&&Mu.set(t,{provider:t,remaining:mh(o),limit:mh(s),reset:Fb(a),capturedAt:Date.now()})}function xb(t){return Mu.get(t)}function Ob(){return Array.from(Mu.values())}function ju(t,e){for(let r of e){let n=t(r);if(n)return n}return null}function mh(t){if(!t)return null;let e=Number(t.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function Fb(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=Pb(e);return o===null?null:Math.floor((Date.now()+o)/1e3)}function Pb(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}Js();async function Ib(t,e,r,n){let o=t.body,s=t.provider,a=r.providerService.getProvider(s);if(!a)throw st(`Provider '${s}' not found`,404,"provider_not_found");try{let{requestBody:c,config:l,bypass:d}=await gh(o,a,n,t.headers,{req:t}),f=await yh(c,l,a,r,d,n,{req:t});try{a?.baseUrl&&f?.headers&&Do(s,a.baseUrl,f.headers)}catch{}let m=await _h(c,f,a,n,d,{req:t});return bh(m,e,o)}catch(c){let l=await Nb(t,e,r,n,c);if(l)return l;throw c}}async function Nb(t,e,r,n,o){let s=t.scenarioType||"default",a=t.familyFallback,c=t.modelFamily,l=r.configService.get("fallback"),d=ft();if(!r.configService.get("Router")?.enableFallback)return t.log.info("Fallback disabled by configuration, skipping fallback attempts"),null;let m=p=>{let[k,...y]=p.split(","),E=k?.trim(),D=y.join(",").trim();return!E||!D?null:{provider:E,model:D,key:`${E},${D}`}},w=[];if(c){let p=a?.[s];Array.isArray(p)&&p.length>0?w.push({name:`${c}/${s}`,models:p}):t.log.warn(`No ${c} fallback configured for ${s}; will try global ${s} fallback`)}if(Array.isArray(l?.[s])&&l[s].length>0&&w.push({name:`global/${s}`,models:l[s]}),w.length===0)return null;let v=t.provider||"",A=t.body.model||"",b=new Set;v&&A&&(d.recordFailure(v,A,o?.message),b.add(`${v},${A}`));let g=w.reduce((p,k)=>p+k.models.length,0);t.log.warn(`Request failed for ${s}, trying ${g} fallback models across ${w.length} fallback stage(s)`);for(let p of w){t.log.info(`Trying ${p.name} fallback stage with ${p.models.length} models`);for(let k of p.models){let y=m(k);if(!y){t.log.warn(`Fallback model '${k}' is invalid, skipping`);continue}if(b.has(y.key))continue;b.add(y.key);let E=y.provider,D=y.model;try{if(!d.isAvailable(E,D)){t.log.warn(`Fallback model ${k} unavailable (fail pool), skipping`);continue}t.log.info(`Trying fallback model: ${k}`);let O={...t.body};O.model=D;let q={...t,provider:E,body:O},J=r.providerService.getProvider(E);if(!J){t.log.warn(`Fallback provider '${E}' not found, skipping`);continue}let{requestBody:N,config:Y,bypass:re}=await gh(O,J,n,t.headers,{req:q}),ee=await yh(N,Y,J,r,re,n,{req:q});try{J?.baseUrl&&ee?.headers&&Do(E,J.baseUrl,ee.headers)}catch{}let ue=await _h(N,ee,J,n,re,{req:q});return t.log.info(`Fallback model ${k} succeeded`),d.recordSuccess(E,D),v&&A&&(To().promote(v,A,s,E,D),t.log.info(`Promoted fallback model ${E},${D} for ${v},${A}:${s}`),d.forceOpen(v,A,o?.message),t.log.info(`Marked original model ${v},${A} as unavailable`)),t.provider=E,t.body=O,bh(ue,e,O)}catch(O){d.recordFailure(E,D,O.message),t.log.warn(`Fallback model ${y.key} failed: ${O.message}`),r.recordUsage?.({provider:E,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 gh(t,e,r,n,o){let s=JSON.parse(JSON.stringify(t)),a={},c=!1;if(c=Bb(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 Bb(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 yh(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 m=e.headers||{};f.config?.headers&&(m={...m,...f.config.headers},delete m.host,delete f.config.headers),e={...e,...f.config,headers:m}}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 fh(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}`),st(`Error from provider(${r.name},${t.model}: ${d.status}): ${f}`,d.status,"provider_response_error")}return d}async function _h(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 bh(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 Lu=async t=>{t.get("/",async()=>({message:"LLMs API",version:hh})),t.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()})),t.get("/providers/health",async()=>({states:ft().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)=>Ib(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 st("Provider name is required",400,"invalid_request");if(!s||!jb(s))throw st("Valid base URL is required",400,"invalid_request");if(!a?.trim())throw st("API key is required",400,"invalid_request");if(!c||!Array.isArray(c)||c.length===0)throw st("At least one model is required",400,"invalid_request");if(t.providerService.getProvider(r.body.name))throw st(`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 st("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 st("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 st("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 st("Provider not found",404,"provider_not_found");return{message:`Provider ${r.body.enabled?"enabled":"disabled"} successfully`}})};function jb(t){try{return new URL(t),!0}catch{return!1}}var In=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 Je=[];for(let t=0;t<256;++t)Je.push((t+256).toString(16).slice(1));function Ch(t,e=0){return(Je[t[e+0]]+Je[t[e+1]]+Je[t[e+2]]+Je[t[e+3]]+"-"+Je[t[e+4]]+Je[t[e+5]]+"-"+Je[t[e+6]]+Je[t[e+7]]+"-"+Je[t[e+8]]+Je[t[e+9]]+"-"+Je[t[e+10]]+Je[t[e+11]]+Je[t[e+12]]+Je[t[e+13]]+Je[t[e+14]]+Je[t[e+15]]).toLowerCase()}import{randomFillSync as Mb}from"crypto";var Ys=new Uint8Array(256),Ks=Ys.length;function Uu(){return Ks>Ys.length-16&&(Mb(Ys),Ks=0),Ys.slice(Ks,Ks+=16)}import{randomUUID as Lb}from"crypto";var $u={randomUUID:Lb};function Ub(t,e,r){if($u.randomUUID&&!e&&!t)return $u.randomUUID();t=t||{};let n=t.random??t.rng?.()??Uu();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 Ch(n)}var Yt=Ub;var wh=t=>t<=0?"none":t<=1024?"low":t<=8192?"medium":"high";var Eh=(t,e)=>(t.includes("base64")&&(t=t.split("base64").pop(),t.startsWith(",")&&(t=t.slice(1))),`data:${e};base64,${t}`);var Qs=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"?Eh(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"&&f.signature);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:wh(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,m=!1,w=new Map,v=new Map,A=0,b=0,g=0,p=!1,k=!1,y=0,E=-1,D=()=>{let N=y;return y++,N},O=N=>{if(!p)try{o.enqueue(N);let Y=new TextDecoder().decode(N);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"))p=!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(!p)try{if(E>=0){let Y={type:"content_block_stop",index:E};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(Ly).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)}};import{get_encoding as Tk}from"tiktoken";var is=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=Tk(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)}};import{join as Ha}from"path";import{homedir as jT}from"os";import{existsSync as Ed,mkdirSync as MT}from"fs";import{promises as Wa}from"fs";var Dk=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}},Rk=Dk,xk=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}},Ok=xk,Gy=(()=>{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]]))})(),Fk=t=>Object.fromEntries(Object.entries(t).map(([e,r])=>[r,e])),Pk=Fk(Gy),Uy=".,!?\u2026\u3002\uFF0C\u3001\u0964\u06D4\u060C",Ik=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|[${Uy}])]+`,` ?[^\\s${Uy}]+`]]),as="\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E",oF=new RegExp(`^[${as}]+$`,"gu"),Cd=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"),$a=(t,e=!0)=>{if(t.Regex!==void 0){let r=t.Regex.replace(/\\([#&~])/g,"$1");for(let[n,o]of Ik)r=r.replaceAll(n,o);return new RegExp(r,"gu")}else if(t.String!==void 0){let r=Nk(t.String);return new RegExp(e?r:`(${r})`,"gu")}else return console.warn("Unknown pattern type:",t),null},Nk=t=>t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),Bk=(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},jk=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,Mk=t=>Number.isInteger(t)||typeof t=="bigint",Lk=t=>{let e=0;for(let r of t)++e;return e},Uk=t=>Jy(t.toLowerCase()),Pt=(...t)=>Array.prototype.concat.apply([],t),wd=t=>new Map(Object.entries(t)),$k=(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},Jy=t=>t.replace(new RegExp("\\p{M}","gu"),""),$y=(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},qk=t=>t.match(/\S+/g)||[],zk=class{constructor(){let t=function(...e){return t._call(...e)};return Object.setPrototypeOf(t,new.target.prototype)}},us=zk,Hk=class extends us{constructor(t){super(),this.config=t}_call(t){return this.normalize(t)}},vr=Hk,Wk=class extends vr{tokenize_chinese_chars(t){let e=[];for(let r=0;r<t.length;++r){let n=t[r],o=n.charCodeAt(0);jk(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}},Gk=Wk,Jk=class extends vr{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}},Vk=Jk,Kk=class extends vr{constructor(t){super(t),this.normalizers=(t.normalizers??[]).map(e=>Vy(e))}normalize(t){return this.normalizers.reduce((e,r)=>r?r.normalize(e):e,t)}},Yk=Kk,Qk=class extends vr{normalize(t){let e=$a(this.config.pattern??{});return e===null?t:t.replaceAll(e,this.config.content??"")}},Xk=Qk,Zk=class extends vr{constructor(){super(...arguments),this.form="NFC"}normalize(t){return t=t.normalize(this.form),t}},qa=Zk,ev=class extends qa{constructor(){super(...arguments),this.form="NFC"}},tv=ev,rv=class extends qa{constructor(){super(...arguments),this.form="NFD"}},nv=rv,ov=class extends qa{constructor(){super(...arguments),this.form="NFKC"}},sv=ov,iv=class extends qa{constructor(){super(...arguments),this.form="NFKD"}},av=iv,uv=class extends vr{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}},cv=uv,lv=class extends vr{normalize(t){return Jy(t)}},dv=lv,fv=class extends vr{normalize(t){return t.toLowerCase()}},hv=fv,pv=class extends vr{normalize(t){return t=this.config.prepend+t,t}},mv=pv;function gv(t){if(t===null)return null;switch(t.type){case"BertNormalizer":return new Gk(t);case"Precompiled":return new Vk(t);case"Sequence":return new Yk(t);case"Replace":return new Xk(t);case"NFC":return new tv(t);case"NFD":return new nv(t);case"NFKC":return new sv(t);case"NFKD":return new av(t);case"Strip":return new cv(t);case"StripAccents":return new dv(t);case"Lowercase":return new hv(t);case"Prepend":return new mv(t);default:throw new Error(`Unknown Normalizer type: ${t.type}`)}}var Vy=gv,yv=class extends us{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)}},ur=yv,_v=class extends ur{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=Gy,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(""))}},bv=_v,Cv=class extends ur{pre_tokenize_text(t,e){return t.match(/\w+|[^\w\s]+/g)||[]}},wv=Cv,Ev=class extends ur{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]}},Av=Ev,Sv=class extends ur{constructor(t){super(),this.config=t,this.pattern=$a(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):$k(t,this.pattern)}},kv=Sv,vv=class extends ur{constructor(t){super(),this.config=t,this.pattern=new RegExp(`[^${as}]+|[${as}]+`,"gu")}pre_tokenize_text(t){return t.match(this.pattern)||[]}},Tv=vv,Dv=class extends ur{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)||[]}},Rv=Dv,xv=class extends ur{constructor(){super(),this.pattern=new RegExp(`[^\\s${as}]+|[${as}]`,"gu")}pre_tokenize_text(t,e){return t.trim().match(this.pattern)||[]}},Ov=xv,Fv=class extends ur{constructor(t){super(),this.config=t,this.pattern=$a(this.config.pattern??{}),this.content=this.config.content??""}pre_tokenize_text(t){return this.pattern===null?[t]:[t.replaceAll(this.pattern,this.config.content??"")]}},Pv=Fv,Iv=class extends ur{constructor(t){super(),this.tokenizers=(t.pretokenizers??[]).map(e=>Ky(e))}pre_tokenize_text(t,e){return this.tokenizers.reduce((r,n)=>n?n.pre_tokenize(r,e):r,[t])}},Nv=Iv,Bv=class extends ur{pre_tokenize_text(t){return qk(t)}},jv=Bv;function Mv(t){if(t===null)return null;switch(t.type){case"BertPreTokenizer":return new Ov;case"Sequence":return new Nv(t);case"Whitespace":return new wv;case"WhitespaceSplit":return new jv;case"Metaspace":return new Av(t);case"ByteLevel":return new bv(t);case"Split":return new kv(t);case"Punctuation":return new Tv(t);case"Digits":return new Rv(t);case"Replace":return new Pv(t);default:throw new Error(`Unknown PreTokenizer type: ${t.type}`)}}var Ky=Mv,Lv=class extends us{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=Bk(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=Lv,Uv=class extends za{constructor(t){super(t),this.max_input_chars_per_word=100,this.tokens_to_ids=wd(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}},qy=Uv,zy=class Yy{constructor(e,r){this.is_leaf=e,this.children=r}static default(){return new Yy(!1,new Map)}},$v=class{constructor(){this.root=zy.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=zy.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)}}},qv=$v,bd=class Qy{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 Qy(this.token_id,this.node_id,this.pos,this.length,this.score);return e.prev=this.prev,e.backtrace_score=this.backtrace_score,e}},zv=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)}},Hv=zv;function Wv(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 Gv=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=Wv(this.scores)[0],this.unk_score=this.min_score-10,this.scores[this.unk_token_id]=this.unk_score,this.trie=new qv,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],m=Lk(l);t.insert(n,m,f,d),!o&&m===r&&(o=!0)}o||t.insert(n,r,this.unk_score,this.unk_token_id),n+=r}}tokenize(t){let e=new Hv(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}},Hy=Gv,Jv=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}},Vv=Jv,Kv=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()}},Yv=Kv,Qv=class extends za{constructor(t){super(t),this.tokens_to_ids=wd(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 Yv(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 Vv((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}},Wy=Qv,Xv=class extends za{constructor(t,e){super(t);let r=t.vocab;this.tokens_to_ids=wd(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}},Zv=Xv;function eT(t,e){switch(t.type){case"WordPiece":return new qy(t);case"Unigram":return new Hy(t,e.eos_token);case"BPE":return new Wy(t);default:if(t.vocab)return Array.isArray(t.vocab)?new Hy(t,e.eos_token):Object.hasOwn(t,"continuing_subword_prefix")&&Object.hasOwn(t,"unk_token")?Object.hasOwn(t,"merges")?new Wy(t):new qy(t):new Zv(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 tT=eT,rT=class extends us{constructor(t){super(),this.config=t}_call(t,...e){return this.post_process(t,...e)}},cs=rT,nT=class extends cs{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=Pt(o,t),s=Pt(s,new Array(t.length).fill(a.Sequence.type_id))):a.Sequence.id==="B"&&(o=Pt(o,e),s=Pt(s,new Array(e.length).fill(a.Sequence.type_id))));return{tokens:o,token_type_ids:s}}},oT=nT,sT=class extends cs{post_process(t,e=null){return{tokens:e?Pt(t,e):t}}},iT=sT,aT=class extends cs{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e=null,r=!0){r&&(t=Pt([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=Pt(t,o,e,s),n=Pt(n,new Array(e.length+o.length+s.length).fill(1))}return{tokens:t,token_type_ids:n}}},uT=aT,cT=class extends cs{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e,r=!0){r&&(t=Pt([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=Pt(t,o,e,s),n=Pt(n,new Array(e.length+o.length+s.length).fill(1))}return{tokens:t,token_type_ids:n}}},lT=cT,dT=class extends cs{constructor(t){super(t),this.processors=(t.processors??[]).map(e=>Xy(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}},fT=dT;function hT(t){if(t===null)return null;switch(t.type){case"TemplateProcessing":return new oT(t);case"ByteLevel":return new iT(t);case"BertProcessing":return new uT(t);case"RobertaProcessing":return new lT(t);case"Sequence":return new fT(t);default:throw new Error(`Unknown PostProcessor type: ${t.type}`)}}var Xy=hT,pT=class extends us{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("")}},cr=pT,mT=class extends cr{constructor(t){super(t),this.byte_decoder=Pk,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}},gT=mT,yT=class extends cr{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=Cd(e)),e})}},_T=yT,bT=class extends cr{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}},CT=bT,wT=class extends cr{constructor(t){super(t),this.suffix=t.suffix??""}decode_chain(t){return t.map((e,r)=>e.replaceAll(this.suffix,r===t.length-1?"":" "))}},ET=wT,AT=class extends cr{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=Cd(r).replaceAll(this.word_delimiter_token," ").trim()),r}decode_chain(t){return[this.convert_tokens_to_string(t)]}},ST=AT,kT=class extends cr{constructor(t){super(t),this.decoders=(t.decoders??[]).map(e=>Zy(e))}decode_chain(t){return this.decoders.reduce((e,r)=>r.decode_chain(e),t)}},vT=kT,TT=class extends cr{decode_chain(t){let e=$a(this.config.pattern),r=this.config.content??"";return e===null?t:t.map(n=>n.replaceAll(e,r))}},DT=TT,RT=class extends cr{decode_chain(t){return[t.join("")]}},xT=RT,OT=class extends cr{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)})}},FT=OT,PT=class extends cr{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}},IT=PT;function NT(t){if(t===null)return null;switch(t.type){case"ByteLevel":return new gT(t);case"WordPiece":return new _T(t);case"Metaspace":return new CT(t);case"BPEDecoder":return new ET(t);case"CTC":return new ST(t);case"Sequence":return new vT(t);case"Replace":return new DT(t);case"Fuse":return new xT(t);case"Strip":return new FT(t);case"ByteFallback":return new IT(t);default:throw new Error(`Unknown Decoder type: ${t.type}`)}}var Zy=NT,BT=class{constructor(t,e){let r=$y(t,"Tokenizer",["model","decoder","post_processor","pre_tokenizer","normalizer"]);if(r)throw new Error(r);let n=$y(e,"Config");if(n)throw new Error(n);this.tokenizer=t,this.config=e,this.normalizer=Vy(this.tokenizer.normalizer),this.pre_tokenizer=Ky(this.tokenizer.pre_tokenizer),this.model=tT(this.tokenizer.model,this.config),this.post_processor=Xy(this.tokenizer.post_processor),this.decoder=Zy(this.tokenizer.decoder),this.special_tokens=[],this.all_special_ids=[],this.added_tokens=[],this.tokenizer.added_tokens.forEach(o=>{let s=new Ok(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 Rk(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||!Mk(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=Cd(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=Uk(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:Pt(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}},e0=BT;var Ga=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||Ha(jT(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=Ha(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:Ha(e,"tokenizer.json"),tokenizerConfig:Ha(e,"tokenizer_config.json")}}ensureDir(e){Ed(e)||MT(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!Ed(e.tokenizerJson)||!Ed(e.tokenizerConfig))return null;let[r,n]=await Promise.all([Wa.readFile(e.tokenizerJson,"utf-8"),Wa.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([Wa.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),Wa.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 e0(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 Ja=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 ho=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 is("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 is(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),n=new Ga(e.model,this.logger,{timeout:this.options.timeout});break;case"api":n=new Ja(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)}`}}};import{get_encoding as LT}from"tiktoken";var Ad=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())}},Sd=new Ad(100);Pn();Js();import{readFile as vd}from"fs/promises";import{opendir as UT,stat as $T}from"fs/promises";import{join as Td}from"path";import{CLAUDE_PROJECTS_DIR as t0,HOME_DIR as r0}from"@wengine-ai/claude-code-router-shared";var Tr=LT("cl100k_base"),s0=(t,e,r)=>{let n=0;return Array.isArray(t)&&t.forEach(o=>{typeof o.content=="string"?n+=Tr.encode(o.content).length:Array.isArray(o.content)&&o.content.forEach(s=>{s.type==="text"?n+=Tr.encode(s.text).length:s.type==="tool_use"?n+=Tr.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(n+=Tr.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?n+=Tr.encode(e).length:Array.isArray(e)&&e.forEach(o=>{o.type==="text"&&(typeof o.text=="string"?n+=Tr.encode(o.text).length:Array.isArray(o.text)&&o.text.forEach(s=>{n+=Tr.encode(s||"").length}))}),r&&r.forEach(o=>{o.description&&(n+=Tr.encode(o.name+o.description).length),o.input_schema&&(n+=Tr.encode(JSON.stringify(o.input_schema)).length)}),n},i0=async(t,e)=>{if(t.sessionId){let r=await c0(t.sessionId);if(r){let n=Td(r0,r,"config.json"),o=Td(r0,r,`${t.sessionId}.json`);try{let s=JSON.parse(await vd(o,"utf8"));if(s&&s.Router)return s.Router}catch{}try{let s=JSON.parse(await vd(n,"utf8"));if(s&&s.Router)return s.Router}catch{}}}};function Va(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 a0(t){let e=Va(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 qT(t,e){if(!e||!t)return null;let r=Va(t);if(e[t])return e[t];if(e[r])return e[r];let{family:n}=a0(t);if(n&&e[n])return e[n];for(let[o,s]of Object.entries(e)){let a=Va(o);if(a&&r.includes(a))return s}return null}function u0(t){return t?(t.input_tokens||0)+(t.cache_read_input_tokens||0)+(t.cache_creation_input_tokens||0):0}function n0(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 nt(t,e,r,n){let o=n0(t);if(!o)return t;let{providerName:s,routeModel:a}=o;if(n&&!r){let d=To(),f=d.getPromotion(s,a,n,e);if(f){let m=n0(f);if(m){let w=e.find(A=>A.name.toLowerCase()===m.providerName.toLowerCase()),v=w?.models?.find(A=>String(A).toLowerCase()===m.routeModel.toLowerCase());if(w&&v)return`${w.name},${v}`}d.clear(s,a,n)}}if(!r&&!ft().isAvailable(s,a))return null;let c=e.find(d=>d.name.toLowerCase()===s.toLowerCase()),l=c?.models?.find(d=>String(d).toLowerCase()===a.toLowerCase());return c&&l?`${c.name},${l}`:t}function Vt(t,e,r,n,o){let s=ft(),a=[r?.[t],n?.[t]];for(let c of a)if(!(!Array.isArray(c)||c.length===0))for(let l of c){let d=nt(l,e);if(d)return d}return null}function zT(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 HT(t){let e=Va(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 WT(t,e,r,n,o,s,a){let c=r.longContextThreshold||6e4,l=Math.max(e,u0(o)),d=t.modelFamily;if((s||l>2e5)&&r.extendedContext){let v=nt(r.extendedContext,n,!1,"extendedContext");if(v)return t.log.info(`Family: using extended context model (1M+), tokens: ${l}, estimated: ${e}, explicit: ${s}`),{model:v,scenarioType:"extendedContext",isFallback:!1};let A=Vt("extendedContext",n,r.fallback,a,d);if(A)return t.log.info(`Family: using extended context fallback model (1M+), tokens: ${l}, estimated: ${e}, explicit: ${s}`),{model:A,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 v=r.longContext?nt(r.longContext,n,!1,"longContext"):null;if(v)return t.log.info(`Family: using long context model, tokens: ${l}, estimated: ${e}`),{model:v,scenarioType:"longContext",isFallback:!1};let A=Vt("longContext",n,r.fallback,a,d);if(A)return t.log.info(`Family: using long context fallback model, tokens: ${l}, estimated: ${e}`),{model:A,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(v=>v.type?.startsWith("web_search"))&&r.webSearch){let v=nt(r.webSearch,n,!1,"webSearch");if(v)return{model:v,scenarioType:"webSearch",isFallback:!1};let A=Vt("webSearch",n,r.fallback,a,d);if(A)return t.log.info("Family: using webSearch fallback model"),{model:A,scenarioType:"webSearch",isFallback:!0};t.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(t.body.thinking&&r.think){let v=nt(r.think,n,!1,"think");if(v)return{model:v,scenarioType:"think",isFallback:!1};let A=Vt("think",n,r.fallback,a,d);if(A)return t.log.info("Family: using think fallback model"),{model:A,scenarioType:"think",isFallback:!0};t.log.warn("Family: think model unavailable (fail pool), skipping")}if(r.default){let v=nt(r.default,n,!1,"default");if(v)return{model:v,scenarioType:"default",isFallback:!1};let A=Vt("default",n,r.fallback,a,d);if(A)return t.log.info("Family: using default fallback model"),{model:A,scenarioType:"default",isFallback:!0};t.log.warn("Family: default model unavailable (fail pool), skipping")}return null}var GT=async(t,e,r,n)=>{let o=await i0(t,r),s=r.get("providers")||[],a=o||r.get("Router"),c=r.get("fallback");if(t.body.model.includes(",")){let p=nt(t.body.model,s,!1,"default");if(p)return{model:p,scenarioType:"default"};t.log.warn(`Explicit model ${t.body.model} unavailable (fail pool), trying fallback`);let k=Vt("default",s,void 0,c);if(k)return t.log.info(`Using fallback for explicit model: ${k}`),{model:k,scenarioType:"default"};t.log.warn(`No fallback available for explicit model ${t.body.model}, continuing through routing logic`)}let{family:l,extended:d}=a0(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 p=WT(t,e,f,s,n,d,c);if(p)return{model:p.model,scenarioType:p.scenarioType}}let m=qT(t.body.model,a?.models);if(m){let p=nt(m,s,!1,"modelMapping");if(p)return t.log.info(`Using mapped model for ${t.body.model}: ${m}`),{model:p,scenarioType:"modelMapping"};t.log.warn(`Mapped model ${m} unavailable (fail pool), skipping`)}let w=Math.max(e,u0(n)),v=a?.extendedContextThreshold||2e5;if(w>v&&a?.extendedContext){t.log.info(`Using extended context (1M) model due to token count: ${w}, estimated: ${e}, threshold: ${v}`);let p=nt(a.extendedContext,s,!1,"extendedContext");if(p)return{model:p,scenarioType:"extendedContext"};t.log.warn(`Extended context model ${a.extendedContext} unavailable (fail pool), trying fallback`);let k=Vt("extendedContext",s,void 0,c);if(k)return{model:k,scenarioType:"extendedContext"}}let A=a?.longContextThreshold||6e4;if(w>A&&a?.longContext){t.log.info(`Using long context model due to token count: ${w}, estimated: ${e}, threshold: ${A}`);let p=nt(a.longContext,s,!1,"longContext");if(p)return{model:p,scenarioType:"longContext"};t.log.warn(`Long context model ${a.longContext} unavailable (fail pool), trying fallback`);let k=Vt("longContext",s,void 0,c);if(k)return{model:k,scenarioType:"longContext"}}if(t.body?.system?.length>1&&t.body?.system[1]?.text?.startsWith("<CCR-SUBAGENT-MODEL>")){let p=t.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);if(p)return t.body.system[1].text=t.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${p[1]}</CCR-SUBAGENT-MODEL>`,""),{model:p[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 p=nt(g.background,s,!1,"background");if(p)return{model:p,scenarioType:"background"};t.log.warn(`Background model ${g.background} unavailable (fail pool), falling through`)}if(Array.isArray(t.body.tools)&&t.body.tools.some(p=>p.type?.startsWith("web_search"))&&a?.webSearch){let p=nt(a.webSearch,s,!1,"webSearch");if(p)return{model:p,scenarioType:"webSearch"};t.log.warn(`WebSearch model ${a.webSearch} unavailable (fail pool), trying fallback`);let k=Vt("webSearch",s,void 0,c);if(k)return{model:k,scenarioType:"webSearch"}}if(t.body.thinking&&a?.think){t.log.info(`Using think model for ${t.body.thinking}`);let p=nt(a.think,s,!1,"think");if(p)return{model:p,scenarioType:"think"};t.log.warn(`Think model ${a.think} unavailable (fail pool), trying fallback`);let k=Vt("think",s,void 0,c);if(k)return{model:k,scenarioType:"think"}}if(a?.default){let p=nt(a.default,s,!1,"default");if(p)return{model:p,scenarioType:"default"};t.log.warn(`Default model ${a.default} unavailable (fail pool), trying fallback`);let k=Vt("default",s,void 0,c);if(k)return{model:k,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},Dd=async(t,e,r)=>{let{configService:n,event:o}=r;if(t.originalModel=t.body.model,t.body.metadata?.user_id){let v=t.body.metadata.user_id.split("_session_");v.length>1&&(t.sessionId=v[1])}let a=await i0(t,n)||n.get("Router"),c=n.get("providers")||[],l=Sd.get(t.sessionId),{messages:d,system:f=[],tools:m}=t.body,w=n.get("REWRITE_SYSTEM_PROMPT");if(w&&f.length>1&&f[1]?.text?.includes("<env>")){let v=await vd(w,"utf-8");f[1].text=`${v}<env>${f[1].text.split("<env>").pop()}`}try{let[v,A]=t.body.model.split(","),b=r.tokenizerService?.getTokenizerConfigForModel(v,A),g;r.tokenizerService?g=(await r.tokenizerService.countTokens({messages:d,system:f,tools:m},b)).tokenCount:g=s0(d,f,m),t.tokenCount=g;let p,k=n.get("CUSTOM_ROUTER_PATH");if(k)try{p=await X(k)(t,n.getAll(),{event:o})}catch(y){t.log.error(`failed to load custom router: ${y.message}`)}if(p)t.scenarioType="default";else{let y=await GT(t,g,n,l);p=y.model,t.scenarioType=y.scenarioType}if(a?.image&&p!==a.image&&zT(t)&&!HT(p)){let y=nt(a.image,c,!1,"image");y?(t.log.info(`Using image model fallback for ${p}`),p=y,t.scenarioType="image"):t.log.warn(`Image model ${a.image} unavailable (fail pool), keeping ${p}`)}t.body.model=p}catch(v){t.log.error(`Error in router middleware: ${v.message}`),t.body.model=a?.default,t.scenarioType="default"}},Dr=new Map,o0=1e3;function kd(){if(Dr.size<=o0)return;let t=[...Dr.keys()].slice(0,Dr.size-o0);for(let e of t)Dr.delete(e)}var c0=async t=>{if(Dr.has(t)){let e=Dr.get(t);return!e||e===""?null:e}try{let e=await UT(t0),r=[];for await(let s of e)s.isDirectory()&&r.push(s.name);let n=r.map(async s=>{let a=Td(t0,s,`${t}.jsonl`);try{return(await $T(a)).isFile()?s:null}catch{return null}}),o=await Promise.all(n);for(let s of o)if(s)return Dr.set(t,s),kd(),s;return Dr.set(t,""),kd(),null}catch(e){return console.error("Error searching for project by session:",e),Dr.set(t,""),kd(),null}};Pn();var po=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}},Rd=class extends po{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=>Xe(l?.total_balance)!==void 0);if(!a)return null;let c=Xe(a.total_balance);return c===void 0?null:{totalBalance:c,currency:typeof a.currency=="string"?a.currency:void 0}}},xd=class extends po{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=Xe(s.limit),l=Xe(s.limit_remaining),d=Xe(s.usage),f=Xe(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}},Od=class extends po{async queryQuota(e,r,n){let o=QT(e.baseUrl),a=(await this.fetchJson(o,e,r,n))?.data;if(!a||typeof a!="object")return null;let c={},l=Xe(a.totalBalance),d=Xe(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}},Fd=class extends po{async queryQuota(e,r,n){if(!e.quotaToken)return null;let o={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${e.quotaToken}`},signal:AbortSignal.timeout(r)};if(n)try{let{ProxyAgent:s}=await import("undici");o.dispatcher=new s(new URL(n).toString())}catch{}try{let s=await fetch("https://bigmodel.cn/api/monitor/usage/quota/limit",o);if(!s.ok)return null;let a=await s.json(),c=Array.isArray(a?.data?.limits)?a.data.limits:[],l={};for(let d of c)if(d.type==="TOKENS_LIMIT"){let f=Xe(d.currentValue),m=Xe(d.usage),w=Xe(d.remaining),v=Xe(d.percentage);f!==void 0?l.usedDailyBalance=f:v!==void 0&&(l.usedDailyBalance=v,l.limitDaily=100),m!==void 0?l.limitDaily=m:f!==void 0&&w!==void 0&&(l.limitDaily=f+w),d.nextResetTime&&(l.resetTime=new Date(d.nextResetTime).toISOString())}else if(d.type==="TIME_LIMIT"){if(l.usedDailyBalance===void 0){let f=Xe(d.currentValue);f!==void 0&&(l.usedDailyBalance=f)}if(l.limitDaily===void 0){let f=Xe(d.usage),m=Xe(d.currentValue),w=Xe(d.remaining);f!==void 0?l.limitDaily=f:m!==void 0&&w!==void 0&&(l.limitDaily=m+w)}}return this.hasQuotaData(l)?l:null}catch{return null}}},JT=new Rd,VT=new xd,KT=new Od,YT=new Fd;function Pd(t){let e=l0(t);return e?e==="deepseek.com"||e.endsWith(".deepseek.com")?JT:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?VT:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?KT:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?YT:null:null}function QT(t){let e=l0(t);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function l0(t){try{return new URL(t).hostname.toLowerCase()}catch{return null}}function Xe(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 Id=new Map;function Nd(t,e){t&&Id.set(t,{...e,provider:t,capturedAt:Date.now()})}function XT(t){let e=Id.get(t);return e?{...e}:void 0}function ZT(){return Array.from(Id.values()).map(t=>({...t}))}var eD={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,initialDelayMs:5e3,excludeProviders:[]};function tD(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 rD(t){try{let e=new URL(t).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function Bd(t,e,r){let n=tD(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&&Do(t.name,t.baseUrl,s.headers),s.ok)return{success:!0,headers:s.headers};if(s.status===401||s.status===403)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 Ka=class{config;quotaProbeTimer;healthProbeTimer;getProviders;getHttpsProxy;logger;running=!1;constructor(e,r,n,o){this.config={...eD,...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(s=>s?.name&&!this.config.excludeProviders.includes(s.name)),r=this.getHttpsProxy?.();if(e.length===0)return;let n=[];for(let s of e){let a=Pd(s.baseUrl);a&&n.push({provider:s.name,type:"quota-adapter",promise:a.queryQuota(s,this.config.probeTimeoutMs,r).then(c=>{c&&(Nd(s.name,c),this.logger?.debug?.(`Stored quota probe result for ${s.name}`))})}),rD(s.baseUrl)&&n.push({provider:s.name,type:"rate-limit-headers",promise:Bd(s,this.config.probeTimeoutMs,r).then(()=>{})})}if(n.length===0)return;this.logger?.debug?.(`Running quota probe with ${n.length} tasks for ${e.length} providers`);let o=await Promise.allSettled(n.map(s=>s.promise));for(let s=0;s<n.length;s++){let a=o[s];if(a.status==="rejected"){let c=n[s];this.logger?.warn?.(`Quota probe failed for ${c.provider} (${c.type}): ${a.reason}`)}}}async runHealthProbe(){let e=ft(),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=>Bd(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){for(let f of l)e.recordSuccess(a.name,f);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 Bd(n,this.config.probeTimeoutMs,o),a=Array.isArray(n.models)?n.models:[];if(s.success)for(let c of a)ft().recordSuccess(n.name,c);else for(let c of a)ft().recordFailure(n.name,c,s.error);return s.success}},Yr=null;function d0(t,e,r,n){return Yr||(Yr=new Ka(t,e,r,n)),Yr}function jd(t,e,r,n){let o=d0(t,e,r,n);return o.start(),o}function Md(){Yr&&Yr.stop()}function nD(){Yr&&(Yr.stop(),Yr=null)}var Ld=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()}},f0=new Ld;var A0=jr(y0(),1);var mo=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}},Gk=Wk,Jk=class extends vr{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}},Vk=Jk,Kk=class extends vr{constructor(t){super(t),this.normalizers=(t.normalizers??[]).map(e=>Vy(e))}normalize(t){return this.normalizers.reduce((e,r)=>r?r.normalize(e):e,t)}},Yk=Kk,Qk=class extends vr{normalize(t){let e=$a(this.config.pattern??{});return e===null?t:t.replaceAll(e,this.config.content??"")}},Xk=Qk,Zk=class extends vr{constructor(){super(...arguments),this.form="NFC"}normalize(t){return t=t.normalize(this.form),t}},qa=Zk,ev=class extends qa{constructor(){super(...arguments),this.form="NFC"}},tv=ev,rv=class extends qa{constructor(){super(...arguments),this.form="NFD"}},nv=rv,ov=class extends qa{constructor(){super(...arguments),this.form="NFKC"}},sv=ov,iv=class extends qa{constructor(){super(...arguments),this.form="NFKD"}},av=iv,uv=class extends vr{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}},cv=uv,lv=class extends vr{normalize(t){return Jy(t)}},dv=lv,fv=class extends vr{normalize(t){return t.toLowerCase()}},hv=fv,pv=class extends vr{normalize(t){return t=this.config.prepend+t,t}},mv=pv;function gv(t){if(t===null)return null;switch(t.type){case"BertNormalizer":return new Gk(t);case"Precompiled":return new Vk(t);case"Sequence":return new Yk(t);case"Replace":return new Xk(t);case"NFC":return new tv(t);case"NFD":return new nv(t);case"NFKC":return new sv(t);case"NFKD":return new av(t);case"Strip":return new cv(t);case"StripAccents":return new dv(t);case"Lowercase":return new hv(t);case"Prepend":return new mv(t);default:throw new Error(`Unknown Normalizer type: ${t.type}`)}}var Vy=gv,yv=class extends us{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)}},ur=yv,_v=class extends ur{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=Gy,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(""))}},bv=_v,Cv=class extends ur{pre_tokenize_text(t,e){return t.match(/\w+|[^\w\s]+/g)||[]}},wv=Cv,Ev=class extends ur{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]}},Av=Ev,Sv=class extends ur{constructor(t){super(),this.config=t,this.pattern=$a(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):$k(t,this.pattern)}},kv=Sv,vv=class extends ur{constructor(t){super(),this.config=t,this.pattern=new RegExp(`[^${as}]+|[${as}]+`,"gu")}pre_tokenize_text(t){return t.match(this.pattern)||[]}},Tv=vv,Dv=class extends ur{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)||[]}},Rv=Dv,xv=class extends ur{constructor(){super(),this.pattern=new RegExp(`[^\\s${as}]+|[${as}]`,"gu")}pre_tokenize_text(t,e){return t.trim().match(this.pattern)||[]}},Ov=xv,Fv=class extends ur{constructor(t){super(),this.config=t,this.pattern=$a(this.config.pattern??{}),this.content=this.config.content??""}pre_tokenize_text(t){return this.pattern===null?[t]:[t.replaceAll(this.pattern,this.config.content??"")]}},Pv=Fv,Iv=class extends ur{constructor(t){super(),this.tokenizers=(t.pretokenizers??[]).map(e=>Ky(e))}pre_tokenize_text(t,e){return this.tokenizers.reduce((r,n)=>n?n.pre_tokenize(r,e):r,[t])}},Nv=Iv,Bv=class extends ur{pre_tokenize_text(t){return qk(t)}},jv=Bv;function Mv(t){if(t===null)return null;switch(t.type){case"BertPreTokenizer":return new Ov;case"Sequence":return new Nv(t);case"Whitespace":return new wv;case"WhitespaceSplit":return new jv;case"Metaspace":return new Av(t);case"ByteLevel":return new bv(t);case"Split":return new kv(t);case"Punctuation":return new Tv(t);case"Digits":return new Rv(t);case"Replace":return new Pv(t);default:throw new Error(`Unknown PreTokenizer type: ${t.type}`)}}var Ky=Mv,Lv=class extends us{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=Bk(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=Lv,Uv=class extends za{constructor(t){super(t),this.max_input_chars_per_word=100,this.tokens_to_ids=wd(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}},qy=Uv,zy=class Yy{constructor(e,r){this.is_leaf=e,this.children=r}static default(){return new Yy(!1,new Map)}},$v=class{constructor(){this.root=zy.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=zy.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)}}},qv=$v,bd=class Qy{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 Qy(this.token_id,this.node_id,this.pos,this.length,this.score);return e.prev=this.prev,e.backtrace_score=this.backtrace_score,e}},zv=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)}},Hv=zv;function Wv(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 Gv=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=Wv(this.scores)[0],this.unk_score=this.min_score-10,this.scores[this.unk_token_id]=this.unk_score,this.trie=new qv,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],m=Lk(l);t.insert(n,m,f,d),!o&&m===r&&(o=!0)}o||t.insert(n,r,this.unk_score,this.unk_token_id),n+=r}}tokenize(t){let e=new Hv(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}},Hy=Gv,Jv=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}},Vv=Jv,Kv=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()}},Yv=Kv,Qv=class extends za{constructor(t){super(t),this.tokens_to_ids=wd(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 Yv(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 Vv((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}},Wy=Qv,Xv=class extends za{constructor(t,e){super(t);let r=t.vocab;this.tokens_to_ids=wd(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}},Zv=Xv;function eT(t,e){switch(t.type){case"WordPiece":return new qy(t);case"Unigram":return new Hy(t,e.eos_token);case"BPE":return new Wy(t);default:if(t.vocab)return Array.isArray(t.vocab)?new Hy(t,e.eos_token):Object.hasOwn(t,"continuing_subword_prefix")&&Object.hasOwn(t,"unk_token")?Object.hasOwn(t,"merges")?new Wy(t):new qy(t):new Zv(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 tT=eT,rT=class extends us{constructor(t){super(),this.config=t}_call(t,...e){return this.post_process(t,...e)}},cs=rT,nT=class extends cs{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=Pt(o,t),s=Pt(s,new Array(t.length).fill(a.Sequence.type_id))):a.Sequence.id==="B"&&(o=Pt(o,e),s=Pt(s,new Array(e.length).fill(a.Sequence.type_id))));return{tokens:o,token_type_ids:s}}},oT=nT,sT=class extends cs{post_process(t,e=null){return{tokens:e?Pt(t,e):t}}},iT=sT,aT=class extends cs{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e=null,r=!0){r&&(t=Pt([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=Pt(t,o,e,s),n=Pt(n,new Array(e.length+o.length+s.length).fill(1))}return{tokens:t,token_type_ids:n}}},uT=aT,cT=class extends cs{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e,r=!0){r&&(t=Pt([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=Pt(t,o,e,s),n=Pt(n,new Array(e.length+o.length+s.length).fill(1))}return{tokens:t,token_type_ids:n}}},lT=cT,dT=class extends cs{constructor(t){super(t),this.processors=(t.processors??[]).map(e=>Xy(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}},fT=dT;function hT(t){if(t===null)return null;switch(t.type){case"TemplateProcessing":return new oT(t);case"ByteLevel":return new iT(t);case"BertProcessing":return new uT(t);case"RobertaProcessing":return new lT(t);case"Sequence":return new fT(t);default:throw new Error(`Unknown PostProcessor type: ${t.type}`)}}var Xy=hT,pT=class extends us{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("")}},cr=pT,mT=class extends cr{constructor(t){super(t),this.byte_decoder=Pk,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}},gT=mT,yT=class extends cr{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=Cd(e)),e})}},_T=yT,bT=class extends cr{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}},CT=bT,wT=class extends cr{constructor(t){super(t),this.suffix=t.suffix??""}decode_chain(t){return t.map((e,r)=>e.replaceAll(this.suffix,r===t.length-1?"":" "))}},ET=wT,AT=class extends cr{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=Cd(r).replaceAll(this.word_delimiter_token," ").trim()),r}decode_chain(t){return[this.convert_tokens_to_string(t)]}},ST=AT,kT=class extends cr{constructor(t){super(t),this.decoders=(t.decoders??[]).map(e=>Zy(e))}decode_chain(t){return this.decoders.reduce((e,r)=>r.decode_chain(e),t)}},vT=kT,TT=class extends cr{decode_chain(t){let e=$a(this.config.pattern),r=this.config.content??"";return e===null?t:t.map(n=>n.replaceAll(e,r))}},DT=TT,RT=class extends cr{decode_chain(t){return[t.join("")]}},xT=RT,OT=class extends cr{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)})}},FT=OT,PT=class extends cr{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}},IT=PT;function NT(t){if(t===null)return null;switch(t.type){case"ByteLevel":return new gT(t);case"WordPiece":return new _T(t);case"Metaspace":return new CT(t);case"BPEDecoder":return new ET(t);case"CTC":return new ST(t);case"Sequence":return new vT(t);case"Replace":return new DT(t);case"Fuse":return new xT(t);case"Strip":return new FT(t);case"ByteFallback":return new IT(t);default:throw new Error(`Unknown Decoder type: ${t.type}`)}}var Zy=NT,BT=class{constructor(t,e){let r=$y(t,"Tokenizer",["model","decoder","post_processor","pre_tokenizer","normalizer"]);if(r)throw new Error(r);let n=$y(e,"Config");if(n)throw new Error(n);this.tokenizer=t,this.config=e,this.normalizer=Vy(this.tokenizer.normalizer),this.pre_tokenizer=Ky(this.tokenizer.pre_tokenizer),this.model=tT(this.tokenizer.model,this.config),this.post_processor=Xy(this.tokenizer.post_processor),this.decoder=Zy(this.tokenizer.decoder),this.special_tokens=[],this.all_special_ids=[],this.added_tokens=[],this.tokenizer.added_tokens.forEach(o=>{let s=new Ok(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 Rk(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||!Mk(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=Cd(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=Uk(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:Pt(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}},e0=BT;var Ga=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||Ha(jT(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=Ha(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:Ha(e,"tokenizer.json"),tokenizerConfig:Ha(e,"tokenizer_config.json")}}ensureDir(e){Ed(e)||MT(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!Ed(e.tokenizerJson)||!Ed(e.tokenizerConfig))return null;let[r,n]=await Promise.all([Wa.readFile(e.tokenizerJson,"utf-8"),Wa.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([Wa.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),Wa.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 e0(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 Ja=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 ho=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 is("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 is(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),n=new Ga(e.model,this.logger,{timeout:this.options.timeout});break;case"api":n=new Ja(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)}`}}};import{get_encoding as LT}from"tiktoken";var Ad=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())}},Sd=new Ad(100);Pn();Js();import{readFile as vd}from"fs/promises";import{opendir as UT,stat as $T}from"fs/promises";import{join as Td}from"path";import{CLAUDE_PROJECTS_DIR as t0,HOME_DIR as r0}from"@wengine-ai/claude-code-router-shared";var Tr=LT("cl100k_base"),s0=(t,e,r)=>{let n=0;return Array.isArray(t)&&t.forEach(o=>{typeof o.content=="string"?n+=Tr.encode(o.content).length:Array.isArray(o.content)&&o.content.forEach(s=>{s.type==="text"?n+=Tr.encode(s.text).length:s.type==="tool_use"?n+=Tr.encode(JSON.stringify(s.input)).length:s.type==="tool_result"&&(n+=Tr.encode(typeof s.content=="string"?s.content:JSON.stringify(s.content)).length)})}),typeof e=="string"?n+=Tr.encode(e).length:Array.isArray(e)&&e.forEach(o=>{o.type==="text"&&(typeof o.text=="string"?n+=Tr.encode(o.text).length:Array.isArray(o.text)&&o.text.forEach(s=>{n+=Tr.encode(s||"").length}))}),r&&r.forEach(o=>{o.description&&(n+=Tr.encode(o.name+o.description).length),o.input_schema&&(n+=Tr.encode(JSON.stringify(o.input_schema)).length)}),n},i0=async(t,e)=>{if(t.sessionId){let r=await c0(t.sessionId);if(r){let n=Td(r0,r,"config.json"),o=Td(r0,r,`${t.sessionId}.json`);try{let s=JSON.parse(await vd(o,"utf8"));if(s&&s.Router)return s.Router}catch{}try{let s=JSON.parse(await vd(n,"utf8"));if(s&&s.Router)return s.Router}catch{}}}};function Va(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 a0(t){let e=Va(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 qT(t,e){if(!e||!t)return null;let r=Va(t);if(e[t])return e[t];if(e[r])return e[r];let{family:n}=a0(t);if(n&&e[n])return e[n];for(let[o,s]of Object.entries(e)){let a=Va(o);if(a&&r.includes(a))return s}return null}function u0(t){return t?(t.input_tokens||0)+(t.cache_read_input_tokens||0)+(t.cache_creation_input_tokens||0):0}function n0(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 nt(t,e,r,n){let o=n0(t);if(!o)return t;let{providerName:s,routeModel:a}=o;if(n&&!r){let d=To(),f=d.getPromotion(s,a,n,e);if(f){let m=n0(f);if(m){let w=e.find(A=>A.name.toLowerCase()===m.providerName.toLowerCase()),v=w?.models?.find(A=>String(A).toLowerCase()===m.routeModel.toLowerCase());if(w&&v)return`${w.name},${v}`}d.clear(s,a,n)}}if(!r&&!ft().isAvailable(s,a))return null;let c=e.find(d=>d.name.toLowerCase()===s.toLowerCase()),l=c?.models?.find(d=>String(d).toLowerCase()===a.toLowerCase());return c&&l?`${c.name},${l}`:t}function Vt(t,e,r,n,o){let s=ft(),a=[r?.[t],n?.[t]];for(let c of a)if(!(!Array.isArray(c)||c.length===0))for(let l of c){let d=nt(l,e);if(d)return d}return null}function zT(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 HT(t){let e=Va(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 WT(t,e,r,n,o,s,a){let c=r.longContextThreshold||6e4,l=Math.max(e,u0(o)),d=t.modelFamily;if((s||l>2e5)&&r.extendedContext){let v=nt(r.extendedContext,n,!1,"extendedContext");if(v)return t.log.info(`Family: using extended context model (1M+), tokens: ${l}, estimated: ${e}, explicit: ${s}`),{model:v,scenarioType:"extendedContext",isFallback:!1};let A=Vt("extendedContext",n,r.fallback,a,d);if(A)return t.log.info(`Family: using extended context fallback model (1M+), tokens: ${l}, estimated: ${e}, explicit: ${s}`),{model:A,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 v=r.longContext?nt(r.longContext,n,!1,"longContext"):null;if(v)return t.log.info(`Family: using long context model, tokens: ${l}, estimated: ${e}`),{model:v,scenarioType:"longContext",isFallback:!1};let A=Vt("longContext",n,r.fallback,a,d);if(A)return t.log.info(`Family: using long context fallback model, tokens: ${l}, estimated: ${e}`),{model:A,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(v=>v.type?.startsWith("web_search"))&&r.webSearch){let v=nt(r.webSearch,n,!1,"webSearch");if(v)return{model:v,scenarioType:"webSearch",isFallback:!1};let A=Vt("webSearch",n,r.fallback,a,d);if(A)return t.log.info("Family: using webSearch fallback model"),{model:A,scenarioType:"webSearch",isFallback:!0};t.log.warn("Family: webSearch model unavailable (fail pool), skipping")}if(t.body.thinking&&r.think){let v=nt(r.think,n,!1,"think");if(v)return{model:v,scenarioType:"think",isFallback:!1};let A=Vt("think",n,r.fallback,a,d);if(A)return t.log.info("Family: using think fallback model"),{model:A,scenarioType:"think",isFallback:!0};t.log.warn("Family: think model unavailable (fail pool), skipping")}if(r.default){let v=nt(r.default,n,!1,"default");if(v)return{model:v,scenarioType:"default",isFallback:!1};let A=Vt("default",n,r.fallback,a,d);if(A)return t.log.info("Family: using default fallback model"),{model:A,scenarioType:"default",isFallback:!0};t.log.warn("Family: default model unavailable (fail pool), skipping")}return null}var GT=async(t,e,r,n)=>{let o=await i0(t,r),s=r.get("providers")||[],a=o||r.get("Router"),c=r.get("fallback");if(t.body.model.includes(",")){let p=nt(t.body.model,s,!1,"default");if(p)return{model:p,scenarioType:"default"};t.log.warn(`Explicit model ${t.body.model} unavailable (fail pool), trying fallback`);let k=Vt("default",s,void 0,c);if(k)return t.log.info(`Using fallback for explicit model: ${k}`),{model:k,scenarioType:"default"};t.log.warn(`No fallback available for explicit model ${t.body.model}, continuing through routing logic`)}let{family:l,extended:d}=a0(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 p=WT(t,e,f,s,n,d,c);if(p)return{model:p.model,scenarioType:p.scenarioType}}let m=qT(t.body.model,a?.models);if(m){let p=nt(m,s,!1,"modelMapping");if(p)return t.log.info(`Using mapped model for ${t.body.model}: ${m}`),{model:p,scenarioType:"modelMapping"};t.log.warn(`Mapped model ${m} unavailable (fail pool), skipping`)}let w=Math.max(e,u0(n)),v=a?.extendedContextThreshold||2e5;if(w>v&&a?.extendedContext){t.log.info(`Using extended context (1M) model due to token count: ${w}, estimated: ${e}, threshold: ${v}`);let p=nt(a.extendedContext,s,!1,"extendedContext");if(p)return{model:p,scenarioType:"extendedContext"};t.log.warn(`Extended context model ${a.extendedContext} unavailable (fail pool), trying fallback`);let k=Vt("extendedContext",s,void 0,c);if(k)return{model:k,scenarioType:"extendedContext"}}let A=a?.longContextThreshold||6e4;if(w>A&&a?.longContext){t.log.info(`Using long context model due to token count: ${w}, estimated: ${e}, threshold: ${A}`);let p=nt(a.longContext,s,!1,"longContext");if(p)return{model:p,scenarioType:"longContext"};t.log.warn(`Long context model ${a.longContext} unavailable (fail pool), trying fallback`);let k=Vt("longContext",s,void 0,c);if(k)return{model:k,scenarioType:"longContext"}}if(t.body?.system?.length>1&&t.body?.system[1]?.text?.startsWith("<CCR-SUBAGENT-MODEL>")){let p=t.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);if(p)return t.body.system[1].text=t.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${p[1]}</CCR-SUBAGENT-MODEL>`,""),{model:p[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 p=nt(g.background,s,!1,"background");if(p)return{model:p,scenarioType:"background"};t.log.warn(`Background model ${g.background} unavailable (fail pool), falling through`)}if(Array.isArray(t.body.tools)&&t.body.tools.some(p=>p.type?.startsWith("web_search"))&&a?.webSearch){let p=nt(a.webSearch,s,!1,"webSearch");if(p)return{model:p,scenarioType:"webSearch"};t.log.warn(`WebSearch model ${a.webSearch} unavailable (fail pool), trying fallback`);let k=Vt("webSearch",s,void 0,c);if(k)return{model:k,scenarioType:"webSearch"}}if(t.body.thinking&&a?.think){t.log.info(`Using think model for ${t.body.thinking}`);let p=nt(a.think,s,!1,"think");if(p)return{model:p,scenarioType:"think"};t.log.warn(`Think model ${a.think} unavailable (fail pool), trying fallback`);let k=Vt("think",s,void 0,c);if(k)return{model:k,scenarioType:"think"}}if(a?.default){let p=nt(a.default,s,!1,"default");if(p)return{model:p,scenarioType:"default"};t.log.warn(`Default model ${a.default} unavailable (fail pool), trying fallback`);let k=Vt("default",s,void 0,c);if(k)return{model:k,scenarioType:"default"}}return{model:void 0,scenarioType:"default"}},Dd=async(t,e,r)=>{let{configService:n,event:o}=r;if(t.originalModel=t.body.model,t.body.metadata?.user_id){let v=t.body.metadata.user_id.split("_session_");v.length>1&&(t.sessionId=v[1])}let a=await i0(t,n)||n.get("Router"),c=n.get("providers")||[],l=Sd.get(t.sessionId),{messages:d,system:f=[],tools:m}=t.body,w=n.get("REWRITE_SYSTEM_PROMPT");if(w&&f.length>1&&f[1]?.text?.includes("<env>")){let v=await vd(w,"utf-8");f[1].text=`${v}<env>${f[1].text.split("<env>").pop()}`}try{let[v,A]=t.body.model.split(","),b=r.tokenizerService?.getTokenizerConfigForModel(v,A),g;r.tokenizerService?g=(await r.tokenizerService.countTokens({messages:d,system:f,tools:m},b)).tokenCount:g=s0(d,f,m),t.tokenCount=g;let p,k=n.get("CUSTOM_ROUTER_PATH");if(k)try{p=await X(k)(t,n.getAll(),{event:o})}catch(y){t.log.error(`failed to load custom router: ${y.message}`)}if(p)t.scenarioType="default";else{let y=await GT(t,g,n,l);p=y.model,t.scenarioType=y.scenarioType}if(a?.image&&p!==a.image&&zT(t)&&!HT(p)){let y=nt(a.image,c,!1,"image");y?(t.log.info(`Using image model fallback for ${p}`),p=y,t.scenarioType="image"):t.log.warn(`Image model ${a.image} unavailable (fail pool), keeping ${p}`)}t.body.model=p}catch(v){t.log.error(`Error in router middleware: ${v.message}`),t.body.model=a?.default,t.scenarioType="default"}},Dr=new Map,o0=1e3;function kd(){if(Dr.size<=o0)return;let t=[...Dr.keys()].slice(0,Dr.size-o0);for(let e of t)Dr.delete(e)}var c0=async t=>{if(Dr.has(t)){let e=Dr.get(t);return!e||e===""?null:e}try{let e=await UT(t0),r=[];for await(let s of e)s.isDirectory()&&r.push(s.name);let n=r.map(async s=>{let a=Td(t0,s,`${t}.jsonl`);try{return(await $T(a)).isFile()?s:null}catch{return null}}),o=await Promise.all(n);for(let s of o)if(s)return Dr.set(t,s),kd(),s;return Dr.set(t,""),kd(),null}catch(e){return console.error("Error searching for project by session:",e),Dr.set(t,""),kd(),null}};Pn();var po=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}},Rd=class extends po{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=>Xe(l?.total_balance)!==void 0);if(!a)return null;let c=Xe(a.total_balance);return c===void 0?null:{totalBalance:c,currency:typeof a.currency=="string"?a.currency:void 0}}},xd=class extends po{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=Xe(s.limit),l=Xe(s.limit_remaining),d=Xe(s.usage),f=Xe(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}},Od=class extends po{async queryQuota(e,r,n){let o=QT(e.baseUrl),a=(await this.fetchJson(o,e,r,n))?.data;if(!a||typeof a!="object")return null;let c={},l=Xe(a.totalBalance),d=Xe(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}},Fd=class extends po{async queryQuota(e,r,n){if(!e.quotaToken)return null;let o={method:"GET",headers:{Accept:"application/json",Authorization:`Bearer ${e.quotaToken}`},signal:AbortSignal.timeout(r)};if(n)try{let{ProxyAgent:s}=await import("undici");o.dispatcher=new s(new URL(n).toString())}catch{}try{let s=await fetch("https://bigmodel.cn/api/monitor/usage/quota/limit",o);if(!s.ok)return null;let a=await s.json(),c=Array.isArray(a?.data?.limits)?a.data.limits:[],l={};for(let d of c)if(d.type==="TOKENS_LIMIT"){let f=Xe(d.currentValue),m=Xe(d.usage),w=Xe(d.remaining),v=Xe(d.percentage);f!==void 0?l.usedDailyBalance=f:v!==void 0&&(l.usedDailyBalance=v,l.limitDaily=100),m!==void 0?l.limitDaily=m:f!==void 0&&w!==void 0&&(l.limitDaily=f+w),d.nextResetTime&&(l.resetTime=new Date(d.nextResetTime).toISOString())}else if(d.type==="TIME_LIMIT"){if(l.usedDailyBalance===void 0){let f=Xe(d.currentValue);f!==void 0&&(l.usedDailyBalance=f)}if(l.limitDaily===void 0){let f=Xe(d.usage),m=Xe(d.currentValue),w=Xe(d.remaining);f!==void 0?l.limitDaily=f:m!==void 0&&w!==void 0&&(l.limitDaily=m+w)}}return this.hasQuotaData(l)?l:null}catch{return null}}},JT=new Rd,VT=new xd,KT=new Od,YT=new Fd;function Pd(t){let e=l0(t);return e?e==="deepseek.com"||e.endsWith(".deepseek.com")?JT:e==="openrouter.ai"||e.endsWith(".openrouter.ai")?VT:e==="siliconflow.com"||e.endsWith(".siliconflow.com")||e==="siliconflow.cn"||e.endsWith(".siliconflow.cn")?KT:e==="bigmodel.cn"||e.endsWith(".bigmodel.cn")?YT:null:null}function QT(t){let e=l0(t);return e?.endsWith(".siliconflow.cn")||e==="siliconflow.cn"?"https://api.siliconflow.cn/v1/user/info":"https://api.siliconflow.com/v1/user/info"}function l0(t){try{return new URL(t).hostname.toLowerCase()}catch{return null}}function Xe(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 Id=new Map;function Nd(t,e){t&&Id.set(t,{...e,provider:t,capturedAt:Date.now()})}function XT(t){let e=Id.get(t);return e?{...e}:void 0}function ZT(){return Array.from(Id.values()).map(t=>({...t}))}var eD={enabled:!0,quotaProbeIntervalMinutes:10,probeTimeoutMs:15e3,initialDelayMs:5e3,excludeProviders:[]};function tD(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 rD(t){try{let e=new URL(t).hostname.toLowerCase();return e.includes("moonshot")||e.includes("kimi")}catch{return!1}}async function Bd(t,e,r){let n=tD(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&&Do(t.name,t.baseUrl,s.headers),s.ok)return{success:!0,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 Ka=class{config;quotaProbeTimer;healthProbeTimer;getProviders;getHttpsProxy;logger;running=!1;constructor(e,r,n,o){this.config={...eD,...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(s=>s?.name&&!this.config.excludeProviders.includes(s.name)),r=this.getHttpsProxy?.();if(e.length===0)return;let n=[];for(let s of e){let a=Pd(s.baseUrl);a&&n.push({provider:s.name,type:"quota-adapter",promise:a.queryQuota(s,this.config.probeTimeoutMs,r).then(c=>{c&&(Nd(s.name,c),this.logger?.debug?.(`Stored quota probe result for ${s.name}`))})}),rD(s.baseUrl)&&n.push({provider:s.name,type:"rate-limit-headers",promise:Bd(s,this.config.probeTimeoutMs,r).then(()=>{})})}if(n.length===0)return;this.logger?.debug?.(`Running quota probe with ${n.length} tasks for ${e.length} providers`);let o=await Promise.allSettled(n.map(s=>s.promise));for(let s=0;s<n.length;s++){let a=o[s];if(a.status==="rejected"){let c=n[s];this.logger?.warn?.(`Quota probe failed for ${c.provider} (${c.type}): ${a.reason}`)}}}async runHealthProbe(){let e=ft(),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=>Bd(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){for(let f of l)e.recordSuccess(a.name,f);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 Bd(n,this.config.probeTimeoutMs,o),a=Array.isArray(n.models)?n.models:[];if(s.success)for(let c of a)ft().recordSuccess(n.name,c);else for(let c of a)ft().recordFailure(n.name,c,s.error);return s.success}},Yr=null;function d0(t,e,r,n){return Yr||(Yr=new Ka(t,e,r,n)),Yr}function jd(t,e,r,n){let o=d0(t,e,r,n);return o.start(),o}function Md(){Yr&&Yr.stop()}function nD(){Yr&&(Yr.stop(),Yr=null)}var Ld=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()}},f0=new Ld;var A0=jr(y0(),1);var mo=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 Qa=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}