@thxp/llms 3.2.12 → 3.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/server.cjs +2 -2
- package/dist/cjs/server.cjs.map +3 -3
- package/dist/esm/server.mjs +2 -2
- package/dist/esm/server.mjs.map +3 -3
- package/package.json +1 -1
package/dist/esm/server.mjs
CHANGED
|
@@ -69,7 +69,7 @@ https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys`};var $l
|
|
|
69
69
|
${this.toMarkdown(s,r+1)}`:`${n}- ${s}`).join(`
|
|
70
70
|
`):typeof e=="object"&&e!==null?Object.entries(e).map(([s,i])=>typeof i=="object"&&i!==null?`${n}${s}:
|
|
71
71
|
${this.toMarkdown(i,r+1)}`:`${n}${s}: ${i}`).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 so,_d=je(()=>{"use strict";so=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:s=!0,prefix:i,metadata:u}=r||{},c={data:e};return s&&(c.timestamp=new Date().toISOString()),i&&(c.prefix=i),u&&Object.keys(u).length>0&&(c.metadata=u),c}async sendRequest(e,r,n,s,i){let u=new AbortController,c=setTimeout(()=>u.abort(),i);try{let l=await fetch(e,{method:r,headers:n,body:JSON.stringify(s),signal:u.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,s,i,u){let c=null;for(let l=1;l<=u.maxAttempts;l++)try{return await this.sendRequest(e,r,n,s,i)}catch(d){if(c=d,l===u.maxAttempts)break;let h=u.backoffMs*Math.pow(2,l-1);console.warn(`[WebhookOutputHandler] Request failed (attempt ${l}/${u.maxAttempts}), retrying in ${h}ms...`,d.message),await this.delay(h)}throw c}async output(e,r={}){let n=r.timeout||this.defaultTimeout;try{let s=this.buildHeaders(),i=this.buildBody(e,r),u=await this.sendWithRetry(this.config.url,this.config.method,s,i,n,this.config.retry);return!0}catch(s){let i=s instanceof Error?s.message:String(s);if(this.config.silent)return console.error(`[WebhookOutputHandler] Failed to send data: ${i}`),!1;throw new Error(`Webhook output failed: ${i}`)}}}});import{writeFileSync as _T,existsSync as bT,mkdirSync as CT}from"fs";import{join as My}from"path";import{tmpdir as wT}from"os";var oo,bd=je(()=>{"use strict";oo=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let r=wT();this.baseDir=My(r,this.config.subdirectory),this.ensureDir()}ensureDir(){try{bT(this.baseDir)||CT(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}`:"",s;if(this.config.includeTimestamp){let i=Date.now();s=`${r}-${e}-${i}${n}`}else s=`${r}-${e}${n}`;return My(this.baseDir,s)}async output(e,r={}){try{let n=r.metadata?.sessionId;if(!n)return!1;let s={...e,timestamp:Date.now(),sessionId:n},i=this.getFilePath(n);return _T(i,JSON.stringify(s,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Cd,cs,Ly=je(()=>{"use strict";yd();_d();bd();Cd=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),s=r.type+"_"+Date.now();this.registerHandler(s,n)}catch(n){console.error(`[OutputManager] Failed to register ${r.type} handler:`,n)}}createHandler(e){switch(e.type){case"console":return new no(e.config);case"webhook":return new so(e.config);case"temp-file":return new oo(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},s={success:[],failed:[]},i=Array.from(this.handlers.entries()).map(async([u,c])=>{try{await c.output(e,n)?s.success.push(u):s.failed.push(u)}catch(l){console.error(`[OutputManager] Handler ${u} failed:`,l),s.failed.push(u)}});return await Promise.all(i),s}async outputTo(e,r,n){let s={...this.defaultOptions,...n},i={success:[],failed:[]},u=e.map(async c=>{let l=this.handlers.get(c);if(!l){console.warn(`[OutputManager] Handler ${c} not found`),i.failed.push(c);return}try{await l.output(r,s)?i.success.push(c):i.failed.push(c)}catch(d){console.error(`[OutputManager] Handler ${c} failed:`,d),i.failed.push(c)}});return await Promise.all(u),i}async outputToType(e,r,n){let s=Array.from(this.handlers.entries()).filter(([i,u])=>u.type===e).map(([i])=>i);return this.outputTo(s,r,n)}},cs=new Cd});import ET from"fastify";import AT from"@fastify/cors";var Lf=Fr(Su(),1);import{readFileSync as M_,existsSync as jf}from"fs";import{join as Mf}from"path";import{config as L_}from"dotenv";var Dn=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:Mf(process.cwd(),this.options.jsonPath);if(jf(e))try{let r=M_(e,"utf-8"),n=Lf.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:Mf(process.cwd(),this.options.envPath);if(jf(e))try{let r=L_({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")||process.env.HTTPS_PROXY||process.env.https_proxy||process.env.HTTP_PROXY||process.env.http_proxy}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 Xe(t,e=500,r="internal_error",n="api_error"){let s=new Error(t);return s.statusCode=e,s.code=r,s.type=n,s}async function Uf(t,e,r){e.log.error(t);let n=t.statusCode||500,s={error:{message:t.message+t.stack||"Internal Server Error",type:t.type||"api_error",code:t.code||"internal_error"}};return r.code(n).send(s)}import{ProxyAgent as U_}from"undici";function qf(t,e,r,n,s){let i=new Headers({"Content-Type":"application/json"});r.headers&&Object.entries(r.headers).forEach(([d,h])=>{h&&i.set(d,h)});let u,c=AbortSignal.timeout(r.TIMEOUT??60*1e3*60);if(r.signal){let d=new AbortController,h=()=>d.abort();r.signal.addEventListener("abort",h),c.addEventListener("abort",h),u=d.signal}else u=c;let l={method:"POST",headers:i,body:JSON.stringify(e),signal:u};return r.httpsProxy&&(l.dispatcher=new U_(new URL(r.httpsProxy).toString())),s?.debug({reqId:n.req.id,request:l,headers:Object.fromEntries(i.entries()),requestUrl:typeof t=="string"?t:t.toString(),useProxy:r.httpsProxy},"final request"),fetch(typeof t=="string"?t:t.toString(),l)}import{existsSync as q_,writeFileSync as $_,readFileSync as z_}from"fs";import{join as H_}from"path";import{HOME_DIR as W_}from"@thxp/shared";var ku=H_(W_,"daily_usage.json"),tn=null;function G_(){let t=new Date;return`${t.getFullYear()}-${String(t.getMonth()+1).padStart(2,"0")}-${String(t.getDate()).padStart(2,"0")}`}function $f(){let t=G_();if(tn&&tn.date===t)return tn;try{if(q_(ku)){let e=JSON.parse(z_(ku,"utf-8"));if(e?.date===t)return tn=e,e}}catch{}return tn={date:t,models:{}},zf(tn),tn}function zf(t){try{$_(ku,JSON.stringify(t,null,2),"utf-8")}catch{}}function J_(t,e,r){let n=$f(),s=`${t.toLowerCase()},${e.toLowerCase()}`;n.models[s]=(n.models[s]||0)+r,zf(n)}function Cs(t,e){return $f().models[`${t.toLowerCase()},${e.toLowerCase()}`]||0}var Hf="3.2.11";var Uo=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)}has(e){return this.cache.has(e)}del(e){return this.cache.delete(e)}clear(){this.cache.clear()}values(){return Array.from(this.cache.values())}},K_=new Uo(100),Y_=300*1e3,Du=new Uo(1e3),Wf=t=>{Du.put(t,Date.now())},Tn=t=>{let e=Du.get(t);return e===void 0?!1:Date.now()-e>Y_?(Du.del(t),!1):!0};var vn=(t,e)=>`${t},${e}`;async function X_(t,e,r,n){let s=t.body,i=t.provider,u=r.providerService.getProvider(i);if(!u)throw Xe(`Provider '${i}' not found`,404,"provider_not_found");let c=s,l=u,d=n,h=0,m=15;for(;h<m;)try{t.provider=l.name,t.scenarioType=t.scenarioType||"default";let w=c.model;if(l.model_limits?.[w]!==void 0&&Cs(l.name,w)>=l.model_limits[w])throw Xe(`Model '${w}' daily token limit exceeded`,429,"daily_limit_exceeded");let{requestBody:k,config:E,bypass:g}=await eb(c,l,d,t.headers,{req:t},r),y=await rb(k,E,l,r,g,d,{req:t}),p=await nb(k,y,l,d,g,{req:t});return await sb(p,e,c)}catch(w){h++,t.log.warn(`[Fallback] Error on attempt ${h}: ${w.message?.substring(0,100)}`);let k=w.message||w.error?.message||"",E=k.match(/(?:max_tokens|should be\s*<=\s*|a value\s*<=\s*)(\d{2,})/i)||k.match(/(?:<=|<)\s*(\d{4,})/);if(E&&w.statusCode===400){let y=parseInt(E[1],10);if(y>0&&y<(c.max_tokens||1/0)){t.log.warn(`[AutoFix] max_tokens ${c.max_tokens} -> ${y} for ${l.name},${c.model}`),c={...c,max_tokens:y},s.max_tokens=y;try{let p=Q("os").homedir()+"/.claude-code-router/config.json",D=Q("fs"),_=JSON.parse(D.readFileSync(p,"utf-8")),v=(_.Providers||_.providers)?.find(x=>x.name===l.name);if(v){v.transformer||(v.transformer={}),v.transformer[c.model]?v.transformer[c.model].use||(v.transformer[c.model].use=[]):v.transformer[c.model]={use:[]};let x=v.transformer[c.model].use,q=x.find(V=>Array.isArray(V)&&V[0]==="maxtoken");q?q[1].max_tokens=y:x.push(["maxtoken",{max_tokens:y}]),D.writeFileSync(p,JSON.stringify(_,null,2)),t.log.info(`[AutoFix] Config updated: ${l.name} ${c.model} max_tokens=${y}`)}}catch(p){t.log.error(`[AutoFix] Failed to update config.json: ${p.message}`)}continue}}let g=await Z_(t,r,w);if(g){t.log.warn(`[Fallback] Rotating to ${g.provider.name},${g.modelName}`),l=g.provider,d=g.transformerConfig||d,c={...s,model:g.modelName},s.model=g.modelName;continue}throw w}}function Q_(t){return t?Array.isArray(t)?t:typeof t=="string"?t?[t]:[]:[]:[]}async function Z_(t,e,r){let n=!r.statusCode&&(r.code||r.name==="TypeError"||r.name==="SyntaxError"||r.message?.includes("fetch")||r.message?.includes("connect")||r.message?.includes("timeout")||r.message?.includes("JSON"));if(!(r.code==="provider_response_error"||r.statusCode>=400&&r.statusCode<600||n))return null;let i=vn(t.provider,t.body.model);Wf(i);let u=t.scenarioType||"default",c=e.configService,l=e.providerService,d=e.transformerService,m=c.getAll().Router||{},w=Q_(m[u]||m.default);if(!Array.isArray(w)||w.length===0)return null;let k=w.filter(D=>!Tn(D));if(k.length===0)return null;let E=k[0],[g,y]=E.split(","),p=l.getProvider(g);return p?{provider:p,modelName:y,transformerConfig:null}:null}async function eb(t,e,r,n,s,i){let u=t,c={},l=!1;if(l=tb(e,r,t),l&&(n instanceof Headers?n.delete("content-length"):delete n["content-length"],c.headers=n),!l&&typeof r.transformRequestOut=="function"){let d=await r.transformRequestOut(u);d.body?(u=d.body,c=d.config||{}):u=d}if(!l&&e.transformer?.use?.length)for(let d of e.transformer.use){let h=d;if(typeof d=="string"&&i?.transformerService&&(h=i.transformerService.getTransformer(d)),!h||typeof h.transformRequestIn!="function")continue;let m=await h.transformRequestIn(u,e,s);m.body?(u=m.body,c={...c,...m.config}):u=m}if(!l&&e.transformer?.[t.model]?.use?.length)for(let d of e.transformer[t.model].use){let h=d;typeof d=="string"&&i?.transformerService&&(h=i.transformerService.getTransformer(d)),!(!h||typeof h.transformRequestIn!="function")&&(u=await h.transformRequestIn(u,e,s))}return{requestBody:u,config:c,bypass:l}}function tb(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 rb(t,e,r,n,s,i,u){let c=e.url||new URL(r.baseUrl);if(s&&typeof i.auth=="function"){let h=await i.auth(t,r);if(h.body){t=h.body;let m=e.headers||{};h.config?.headers&&(m={...m,...h.config.headers},delete m.host,delete h.config.headers),e={...e,...h.config,headers:m}}else t=h}t.stream&&r.baseUrl?.includes("/chat/completions")&&(t.stream_options={include_usage:!0});let l={Authorization:`Bearer ${r.apiKey}`,...e?.headers||{}};for(let h in l)(l[h]==="undefined"||["authorization","Authorization"].includes(h)&&l[h]?.includes("undefined"))&&delete l[h];let d=await qf(c,t,{httpsProxy:n.configService.getHttpsProxy(),...e,headers:JSON.parse(JSON.stringify(l))},u,n.log);if(!d.ok){let h;try{h=await d.text()}catch{h="(failed to read error body)"}throw n.log.error(`[provider_response_error] Error from provider(${r.name},${t.model}: ${d.status}): ${h}`),Xe(`Error from provider(${r.name},${t.model}: ${d.status}): ${h}`,d.status,"provider_response_error")}return d}async function nb(t,e,r,n,s,i){let u=e;if(!s&&r.transformer?.use?.length)for(let c of Array.from(r.transformer.use).reverse())!c||typeof c.transformResponseOut!="function"||(u=await c.transformResponseOut(u,i));if(!s&&r.transformer?.[t.model]?.use?.length)for(let c of Array.from(r.transformer[t.model].use).reverse())!c||typeof c.transformResponseOut!="function"||(u=await c.transformResponseOut(u,i));return!s&&n.transformResponseIn&&(u=await n.transformResponseIn(u,i)),u}async function sb(t,e,r){if(t.ok||e.code(t.status),r.stream===!0)return e.header("Content-Type","text/event-stream"),e.header("Cache-Control","no-cache"),e.header("Connection","keep-alive"),e.send(t.body);{let s=t.headers.get("Content-Encoding"),i;if(s==="gzip"){let{gunzipSync:u}=await import("zlib"),c=await t.arrayBuffer();i=u(Buffer.from(c)).toString("utf-8")}else i=await t.text();try{return JSON.parse(i)}catch(u){throw Xe(`Failed to parse JSON response from provider: ${u.message}. Raw response: ${i.substring(0,500)}`,t.status||500,"provider_response_error")}}}var Tu=async t=>{t.get("/",async()=>({message:"LLMs API",version:Hf})),t.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()}));let e=t.transformerService.getTransformersWithEndpoint();for(let{transformer:r}of e)r.endPoint&&t.post(r.endPoint,async(n,s)=>X_(n,s,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"}},model_limits:{type:"object"}},required:["id","name","type","baseUrl","apiKey","models"]}}},async(r,n)=>{let{name:s,baseUrl:i,apiKey:u,models:c}=r.body;if(!s?.trim())throw Xe("Provider name is required",400,"invalid_request");if(!i||!ob(i))throw Xe("Valid base URL is required",400,"invalid_request");if(!u?.trim())throw Xe("API key is required",400,"invalid_request");if(!c||!Array.isArray(c)||c.length===0)throw Xe("At least one model is required",400,"invalid_request");if(t.providerService.getProvider(r.body.name))throw Xe(`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 Xe("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"}},model_limits:{type:"object"},enabled:{type:"boolean"}}}}},async(r,n)=>{let s=t.providerService.updateProvider(r.params.id,r.body);if(!s)throw Xe("Provider not found",404,"provider_not_found");return s}),t.delete("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async r=>{if(!t.providerService.deleteProvider(r.params.id))throw Xe("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 Xe("Provider not found",404,"provider_not_found");return{message:`Provider ${r.body.enabled?"enabled":"disabled"} successfully`}})};function ob(t){try{return new URL(t),!0}catch{return!1}}var Rn=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(s=>{s==="use"?Array.isArray(r.transformer.use)&&(n.use=r.transformer.use.map(i=>{if(Array.isArray(i)&&typeof i[0]=="string"){let u=this.transformerService.getTransformer(i[0]);if(u)return new u(i[1])}if(typeof i=="string"){let u=this.transformerService.getTransformer(i);return typeof u=="function"?new u:u}}).filter(i=>typeof i<"u")):Array.isArray(r.transformer[s]?.use)&&(n[s]={use:r.transformer[s].use.map(i=>{if(Array.isArray(i)&&typeof i[0]=="string"){let u=this.transformerService.getTransformer(i[0]);if(u)return new u(i[1])}if(typeof i=="string"){let u=this.transformerService.getTransformer(i);return typeof u=="function"?new u:u}}).filter(i=>typeof i<"u")})}),this.registerProvider({name:r.name,baseUrl:r.api_base_url,apiKey:r.api_key,models:r.models||[],transformer:r.transformer?n:void 0,model_limits:r.model_limits}),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 s=`${r.name},${n}`,i={provider:r.name,model:n,fullModel:s};this.modelRoutes.set(s,i),this.modelRoutes.has(n)||this.modelRoutes.set(n,i)}),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 s={...n,...r,updatedAt:new Date};return this.providers.set(e,s),r.models&&(n.models.forEach(i=>{let u=`${n.name},${i}`;this.modelRoutes.delete(u),this.modelRoutes.delete(i)}),r.models.forEach(i=>{let u=`${n.name},${i}`,c={provider:n.name,model:i,fullModel:u};this.modelRoutes.set(u,c),this.modelRoutes.has(i)||this.modelRoutes.set(i,c)})),s}deleteProvider(e){let r=this.providers.get(e);return r?(r.models.forEach(n=>{let s=`${r.name},${n}`;this.modelRoutes.delete(s),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[s,i={}]=n;r[s]=i}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 Gf(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 ib}from"crypto";var $o=new Uint8Array(256),qo=$o.length;function vu(){return qo>$o.length-16&&(ib($o),qo=0),$o.slice(qo,qo+=16)}import{randomUUID as ab}from"crypto";var Ru={randomUUID:ab};function ub(t,e,r){if(Ru.randomUUID&&!e&&!t)return Ru.randomUUID();t=t||{};let n=t.random??t.rng?.()??vu();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 s=0;s<16;++s)e[r+s]=n[s];return e}return Gf(n)}var Gt=ub;var Jf=t=>t<=0?"none":t<=1024?"low":t<=8192?"medium":"high";var Vf=(t,e)=>(t.includes("base64")&&(t=t.split("base64").pop(),t.startsWith(",")&&(t=t.slice(1))),`data:${e};base64,${t}`);var zo=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 i=e.system.filter(u=>u.type==="text"&&u.text).map(u=>({type:"text",text:u.text,cache_control:u.cache_control}));r.push({role:"system",content:i})}}JSON.parse(JSON.stringify(e.messages||[]))?.forEach(i=>{if(i.role==="user"||i.role==="assistant"){if(typeof i.content=="string"){r.push({role:i.role,content:i.content});return}if(Array.isArray(i.content)){if(i.role==="user"){let u=i.content.filter(l=>l.type==="tool_result"&&l.tool_use_id);u.length&&u.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=i.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"?Vf(l.source.data,l.source.media_type):l.source.url},media_type:l.source.media_type}:l)})}else if(i.role==="assistant"){let u={role:"assistant",content:""},c=i.content.filter(h=>h.type==="text"&&h.text);c.length&&(u.content=c.map(h=>h.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 so,_d=je(()=>{"use strict";so=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:s=!0,prefix:i,metadata:u}=r||{},c={data:e};return s&&(c.timestamp=new Date().toISOString()),i&&(c.prefix=i),u&&Object.keys(u).length>0&&(c.metadata=u),c}async sendRequest(e,r,n,s,i){let u=new AbortController,c=setTimeout(()=>u.abort(),i);try{let l=await fetch(e,{method:r,headers:n,body:JSON.stringify(s),signal:u.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,s,i,u){let c=null;for(let l=1;l<=u.maxAttempts;l++)try{return await this.sendRequest(e,r,n,s,i)}catch(d){if(c=d,l===u.maxAttempts)break;let h=u.backoffMs*Math.pow(2,l-1);console.warn(`[WebhookOutputHandler] Request failed (attempt ${l}/${u.maxAttempts}), retrying in ${h}ms...`,d.message),await this.delay(h)}throw c}async output(e,r={}){let n=r.timeout||this.defaultTimeout;try{let s=this.buildHeaders(),i=this.buildBody(e,r),u=await this.sendWithRetry(this.config.url,this.config.method,s,i,n,this.config.retry);return!0}catch(s){let i=s instanceof Error?s.message:String(s);if(this.config.silent)return console.error(`[WebhookOutputHandler] Failed to send data: ${i}`),!1;throw new Error(`Webhook output failed: ${i}`)}}}});import{writeFileSync as _T,existsSync as bT,mkdirSync as CT}from"fs";import{join as My}from"path";import{tmpdir as wT}from"os";var oo,bd=je(()=>{"use strict";oo=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let r=wT();this.baseDir=My(r,this.config.subdirectory),this.ensureDir()}ensureDir(){try{bT(this.baseDir)||CT(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}`:"",s;if(this.config.includeTimestamp){let i=Date.now();s=`${r}-${e}-${i}${n}`}else s=`${r}-${e}${n}`;return My(this.baseDir,s)}async output(e,r={}){try{let n=r.metadata?.sessionId;if(!n)return!1;let s={...e,timestamp:Date.now(),sessionId:n},i=this.getFilePath(n);return _T(i,JSON.stringify(s,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Cd,cs,Ly=je(()=>{"use strict";yd();_d();bd();Cd=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),s=r.type+"_"+Date.now();this.registerHandler(s,n)}catch(n){console.error(`[OutputManager] Failed to register ${r.type} handler:`,n)}}createHandler(e){switch(e.type){case"console":return new no(e.config);case"webhook":return new so(e.config);case"temp-file":return new oo(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},s={success:[],failed:[]},i=Array.from(this.handlers.entries()).map(async([u,c])=>{try{await c.output(e,n)?s.success.push(u):s.failed.push(u)}catch(l){console.error(`[OutputManager] Handler ${u} failed:`,l),s.failed.push(u)}});return await Promise.all(i),s}async outputTo(e,r,n){let s={...this.defaultOptions,...n},i={success:[],failed:[]},u=e.map(async c=>{let l=this.handlers.get(c);if(!l){console.warn(`[OutputManager] Handler ${c} not found`),i.failed.push(c);return}try{await l.output(r,s)?i.success.push(c):i.failed.push(c)}catch(d){console.error(`[OutputManager] Handler ${c} failed:`,d),i.failed.push(c)}});return await Promise.all(u),i}async outputToType(e,r,n){let s=Array.from(this.handlers.entries()).filter(([i,u])=>u.type===e).map(([i])=>i);return this.outputTo(s,r,n)}},cs=new Cd});import ET from"fastify";import AT from"@fastify/cors";var Lf=Fr(Su(),1);import{readFileSync as M_,existsSync as jf}from"fs";import{join as Mf}from"path";import{config as L_}from"dotenv";var Dn=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:Mf(process.cwd(),this.options.jsonPath);if(jf(e))try{let r=M_(e,"utf-8"),n=Lf.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:Mf(process.cwd(),this.options.envPath);if(jf(e))try{let r=L_({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")||process.env.HTTPS_PROXY||process.env.https_proxy||process.env.HTTP_PROXY||process.env.http_proxy}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 Xe(t,e=500,r="internal_error",n="api_error"){let s=new Error(t);return s.statusCode=e,s.code=r,s.type=n,s}async function Uf(t,e,r){e.log.error(t);let n=t.statusCode||500,s={error:{message:t.message+t.stack||"Internal Server Error",type:t.type||"api_error",code:t.code||"internal_error"}};return r.code(n).send(s)}import{ProxyAgent as U_}from"undici";function qf(t,e,r,n,s){let i=new Headers({"Content-Type":"application/json"});r.headers&&Object.entries(r.headers).forEach(([d,h])=>{h&&i.set(d,h)});let u,c=AbortSignal.timeout(r.TIMEOUT??60*1e3*60);if(r.signal){let d=new AbortController,h=()=>d.abort();r.signal.addEventListener("abort",h),c.addEventListener("abort",h),u=d.signal}else u=c;let l={method:"POST",headers:i,body:JSON.stringify(e),signal:u};return r.httpsProxy&&(l.dispatcher=new U_(new URL(r.httpsProxy).toString())),s?.debug({reqId:n.req.id,request:l,headers:Object.fromEntries(i.entries()),requestUrl:typeof t=="string"?t:t.toString(),useProxy:r.httpsProxy},"final request"),fetch(typeof t=="string"?t:t.toString(),l)}import{existsSync as q_,writeFileSync as $_,readFileSync as z_}from"fs";import{join as H_}from"path";import{HOME_DIR as W_}from"@thxp/shared";var ku=H_(W_,"daily_usage.json"),tn=null;function G_(){let t=new Date;return`${t.getFullYear()}-${String(t.getMonth()+1).padStart(2,"0")}-${String(t.getDate()).padStart(2,"0")}`}function $f(){let t=G_();if(tn&&tn.date===t)return tn;try{if(q_(ku)){let e=JSON.parse(z_(ku,"utf-8"));if(e?.date===t)return tn=e,e}}catch{}return tn={date:t,models:{}},zf(tn),tn}function zf(t){try{$_(ku,JSON.stringify(t,null,2),"utf-8")}catch{}}function J_(t,e,r){let n=$f(),s=`${t.toLowerCase()},${e.toLowerCase()}`;n.models[s]=(n.models[s]||0)+r,zf(n)}function Cs(t,e){return $f().models[`${t.toLowerCase()},${e.toLowerCase()}`]||0}var Hf="3.2.14";var Uo=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)}has(e){return this.cache.has(e)}del(e){return this.cache.delete(e)}clear(){this.cache.clear()}values(){return Array.from(this.cache.values())}},K_=new Uo(100),Y_=300*1e3,Du=new Uo(1e3),Wf=t=>{Du.put(t,Date.now())},Tn=t=>{let e=Du.get(t);return e===void 0?!1:Date.now()-e>Y_?(Du.del(t),!1):!0};var vn=(t,e)=>`${t},${e}`;async function X_(t,e,r,n){let s=t.body,i=t.provider,u=r.providerService.getProvider(i);if(!u)throw Xe(`Provider '${i}' not found`,404,"provider_not_found");let c=s,l=u,d=n,h=0,m=15;for(;h<m;)try{t.provider=l.name,t.scenarioType=t.scenarioType||"default";let w=c.model;if(l.model_limits?.[w]!==void 0&&Cs(l.name,w)>=l.model_limits[w])throw Xe(`Model '${w}' daily token limit exceeded`,429,"daily_limit_exceeded");let{requestBody:k,config:E,bypass:g}=await eb(c,l,d,t.headers,{req:t},r),y=await rb(k,E,l,r,g,d,{req:t}),p=await nb(k,y,l,d,g,{req:t});return await sb(p,e,c)}catch(w){h++,t.log.warn(`[Fallback] Error on attempt ${h}: ${w.message?.substring(0,100)}`);let k=w.message||w.error?.message||"",E=k.match(/(?:max_tokens|should be\s*<=\s*|a value\s*<=\s*)(\d{2,})/i)||k.match(/(?:<=|<)\s*(\d{4,})/);if(E&&w.statusCode===400){let y=parseInt(E[1],10);if(y>0&&y<(c.max_tokens||1/0)){t.log.warn(`[AutoFix] max_tokens ${c.max_tokens} -> ${y} for ${l.name},${c.model}`),c={...c,max_tokens:y},s.max_tokens=y;try{let p=Q("os").homedir()+"/.claude-code-router/config.json",D=Q("fs"),_=JSON.parse(D.readFileSync(p,"utf-8")),v=(_.Providers||_.providers)?.find(x=>x.name===l.name);if(v){v.transformer||(v.transformer={}),v.transformer[c.model]?v.transformer[c.model].use||(v.transformer[c.model].use=[]):v.transformer[c.model]={use:[]};let x=v.transformer[c.model].use,q=x.find(V=>Array.isArray(V)&&V[0]==="maxtoken");q?q[1].max_tokens=y:x.push(["maxtoken",{max_tokens:y}]),D.writeFileSync(p,JSON.stringify(_,null,2)),t.log.info(`[AutoFix] Config updated: ${l.name} ${c.model} max_tokens=${y}`)}}catch(p){t.log.error(`[AutoFix] Failed to update config.json: ${p.message}`)}continue}}let g=await Z_(t,r,w);if(g){t.log.warn(`[Fallback] Rotating to ${g.provider.name},${g.modelName}`),l=g.provider,d=g.transformerConfig||d,c={...s,model:g.modelName},s.model=g.modelName;continue}throw w}}function Q_(t){return t?Array.isArray(t)?t:typeof t=="string"?t?[t]:[]:[]:[]}async function Z_(t,e,r){let n=!r.statusCode&&(r.code||r.name==="TypeError"||r.name==="SyntaxError"||r.message?.includes("fetch")||r.message?.includes("connect")||r.message?.includes("timeout")||r.message?.includes("JSON"));if(!(r.code==="provider_response_error"||r.statusCode>=400&&r.statusCode<600||n))return null;let i=vn(t.provider,t.body.model);Wf(i);let u=t.scenarioType||"default",c=e.configService,l=e.providerService,d=e.transformerService,m=c.getAll().Router||{},w=Q_(m[u]||m.default);if(!Array.isArray(w)||w.length===0)return null;let k=w.filter(D=>!Tn(D));if(k.length===0)return null;let E=k[0],[g,y]=E.split(","),p=l.getProvider(g);return p?{provider:p,modelName:y,transformerConfig:null}:null}async function eb(t,e,r,n,s,i){let u=t,c={},l=!1;if(l=tb(e,r,t),l&&(n instanceof Headers?n.delete("content-length"):delete n["content-length"],c.headers=n),!l&&typeof r.transformRequestOut=="function"){let d=await r.transformRequestOut(u);d.body?(u=d.body,c=d.config||{}):u=d}if(!l&&e.transformer?.use?.length)for(let d of e.transformer.use){let h=d;if(typeof d=="string"&&i?.transformerService&&(h=i.transformerService.getTransformer(d)),!h||typeof h.transformRequestIn!="function")continue;let m=await h.transformRequestIn(u,e,s);m.body?(u=m.body,c={...c,...m.config}):u=m}if(!l&&e.transformer?.[t.model]?.use?.length)for(let d of e.transformer[t.model].use){let h=d;typeof d=="string"&&i?.transformerService&&(h=i.transformerService.getTransformer(d)),!(!h||typeof h.transformRequestIn!="function")&&(u=await h.transformRequestIn(u,e,s))}return{requestBody:u,config:c,bypass:l}}function tb(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 rb(t,e,r,n,s,i,u){let c=e.url||new URL(r.baseUrl);if(s&&typeof i.auth=="function"){let h=await i.auth(t,r);if(h.body){t=h.body;let m=e.headers||{};h.config?.headers&&(m={...m,...h.config.headers},delete m.host,delete h.config.headers),e={...e,...h.config,headers:m}}else t=h}t.stream&&r.baseUrl?.includes("/chat/completions")&&(t.stream_options={include_usage:!0});let l={Authorization:`Bearer ${r.apiKey}`,...e?.headers||{}};for(let h in l)(l[h]==="undefined"||["authorization","Authorization"].includes(h)&&l[h]?.includes("undefined"))&&delete l[h];let d=await qf(c,t,{httpsProxy:n.configService.getHttpsProxy(),...e,headers:JSON.parse(JSON.stringify(l))},u,n.log);if(!d.ok){let h;try{h=await d.text()}catch{h="(failed to read error body)"}throw n.log.error(`[provider_response_error] Error from provider(${r.name},${t.model}: ${d.status}): ${h}`),Xe(`Error from provider(${r.name},${t.model}: ${d.status}): ${h}`,d.status,"provider_response_error")}return d}async function nb(t,e,r,n,s,i){let u=e;if(!s&&r.transformer?.use?.length)for(let c of Array.from(r.transformer.use).reverse())!c||typeof c.transformResponseOut!="function"||(u=await c.transformResponseOut(u,i));if(!s&&r.transformer?.[t.model]?.use?.length)for(let c of Array.from(r.transformer[t.model].use).reverse())!c||typeof c.transformResponseOut!="function"||(u=await c.transformResponseOut(u,i));return!s&&n.transformResponseIn&&(u=await n.transformResponseIn(u,i)),u}async function sb(t,e,r){if(t.ok||e.code(t.status),r.stream===!0)return e.header("Content-Type","text/event-stream"),e.header("Cache-Control","no-cache"),e.header("Connection","keep-alive"),e.send(t.body);{let s=t.headers.get("Content-Encoding"),i;if(s==="gzip"){let{gunzipSync:u}=await import("zlib"),c=await t.arrayBuffer();i=u(Buffer.from(c)).toString("utf-8")}else i=await t.text();try{return JSON.parse(i)}catch(u){throw Xe(`Failed to parse JSON response from provider: ${u.message}. Raw response: ${i.substring(0,500)}`,t.status||500,"provider_response_error")}}}var Tu=async t=>{t.get("/",async()=>({message:"LLMs API",version:Hf})),t.get("/health",async()=>({status:"ok",timestamp:new Date().toISOString()}));let e=t.transformerService.getTransformersWithEndpoint();for(let{transformer:r}of e)r.endPoint&&t.post(r.endPoint,async(n,s)=>X_(n,s,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"}},model_limits:{type:"object"}},required:["id","name","type","baseUrl","apiKey","models"]}}},async(r,n)=>{let{name:s,baseUrl:i,apiKey:u,models:c}=r.body;if(!s?.trim())throw Xe("Provider name is required",400,"invalid_request");if(!i||!ob(i))throw Xe("Valid base URL is required",400,"invalid_request");if(!u?.trim())throw Xe("API key is required",400,"invalid_request");if(!c||!Array.isArray(c)||c.length===0)throw Xe("At least one model is required",400,"invalid_request");if(t.providerService.getProvider(r.body.name))throw Xe(`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 Xe("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"}},model_limits:{type:"object"},enabled:{type:"boolean"}}}}},async(r,n)=>{let s=t.providerService.updateProvider(r.params.id,r.body);if(!s)throw Xe("Provider not found",404,"provider_not_found");return s}),t.delete("/providers/:id",{schema:{params:{type:"object",properties:{id:{type:"string"}},required:["id"]}}},async r=>{if(!t.providerService.deleteProvider(r.params.id))throw Xe("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 Xe("Provider not found",404,"provider_not_found");return{message:`Provider ${r.body.enabled?"enabled":"disabled"} successfully`}})};function ob(t){try{return new URL(t),!0}catch{return!1}}var Rn=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(s=>{s==="use"?Array.isArray(r.transformer.use)&&(n.use=r.transformer.use.map(i=>{if(Array.isArray(i)&&typeof i[0]=="string"){let u=this.transformerService.getTransformer(i[0]);if(u)return new u(i[1])}if(typeof i=="string"){let u=this.transformerService.getTransformer(i);return typeof u=="function"?new u:u}}).filter(i=>typeof i<"u")):Array.isArray(r.transformer[s]?.use)&&(n[s]={use:r.transformer[s].use.map(i=>{if(Array.isArray(i)&&typeof i[0]=="string"){let u=this.transformerService.getTransformer(i[0]);if(u)return new u(i[1])}if(typeof i=="string"){let u=this.transformerService.getTransformer(i);return typeof u=="function"?new u:u}}).filter(i=>typeof i<"u")})}),this.registerProvider({name:r.name,baseUrl:r.api_base_url,apiKey:r.api_key,models:r.models||[],transformer:r.transformer?n:void 0,model_limits:r.model_limits}),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 s=`${r.name},${n}`,i={provider:r.name,model:n,fullModel:s};this.modelRoutes.set(s,i),this.modelRoutes.has(n)||this.modelRoutes.set(n,i)}),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 s={...n,...r,updatedAt:new Date};return this.providers.set(e,s),r.models&&(n.models.forEach(i=>{let u=`${n.name},${i}`;this.modelRoutes.delete(u),this.modelRoutes.delete(i)}),r.models.forEach(i=>{let u=`${n.name},${i}`,c={provider:n.name,model:i,fullModel:u};this.modelRoutes.set(u,c),this.modelRoutes.has(i)||this.modelRoutes.set(i,c)})),s}deleteProvider(e){let r=this.providers.get(e);return r?(r.models.forEach(n=>{let s=`${r.name},${n}`;this.modelRoutes.delete(s),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[s,i={}]=n;r[s]=i}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 Gf(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 ib}from"crypto";var $o=new Uint8Array(256),qo=$o.length;function vu(){return qo>$o.length-16&&(ib($o),qo=0),$o.slice(qo,qo+=16)}import{randomUUID as ab}from"crypto";var Ru={randomUUID:ab};function ub(t,e,r){if(Ru.randomUUID&&!e&&!t)return Ru.randomUUID();t=t||{};let n=t.random??t.rng?.()??vu();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 s=0;s<16;++s)e[r+s]=n[s];return e}return Gf(n)}var Gt=ub;var Jf=t=>t<=0?"none":t<=1024?"low":t<=8192?"medium":"high";var Vf=(t,e)=>(t.includes("base64")&&(t=t.split("base64").pop(),t.startsWith(",")&&(t=t.slice(1))),`data:${e};base64,${t}`);var zo=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 i=e.system.filter(u=>u.type==="text"&&u.text).map(u=>({type:"text",text:u.text,cache_control:u.cache_control}));r.push({role:"system",content:i})}}JSON.parse(JSON.stringify(e.messages||[]))?.forEach(i=>{if(i.role==="user"||i.role==="assistant"){if(typeof i.content=="string"){r.push({role:i.role,content:i.content});return}if(Array.isArray(i.content)){if(i.role==="user"){let u=i.content.filter(l=>l.type==="tool_result"&&l.tool_use_id);u.length&&u.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=i.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"?Vf(l.source.data,l.source.media_type):l.source.url},media_type:l.source.media_type}:l)})}else if(i.role==="assistant"){let u={role:"assistant",content:""},c=i.content.filter(h=>h.type==="text"&&h.text);c.length&&(u.content=c.map(h=>h.text).join(`
|
|
73
73
|
`));let l=i.content.filter(h=>h.type==="tool_use"&&h.id);l.length&&(u.tool_calls=l.map(h=>{let m=h.input;return(typeof m!="object"||m===null)&&(m=m?{text:String(m)}:{}),{id:h.id,type:"function",function:{name:h.name,arguments:JSON.stringify(m)}}}));let d=i.content.find(h=>h.type==="thinking"&&h.signature);d&&(u.thinking={content:d.thinking,signature:d.signature}),r.push(u)}return}}});let s={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&&(s.reasoning={effort:Jf(e.thinking.budget_tokens),enabled:e.thinking.type==="enabled"}),e.tool_choice&&(e.tool_choice.type==="tool"?s.tool_choice={type:"function",function:{name:e.tool_choice.name}}:s.tool_choice=e.tool_choice.type),s}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 s=await this.convertOpenAIStreamToAnthropic(e.body,r);return new Response(s,{headers:{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}})}else{let s=await e.json(),i=this.convertOpenAIResponseToAnthropic(s,r);return new Response(JSON.stringify(i),{headers:{"Content-Type":"application/json"}})}}async transformRequestIn(e,r){let n=[],s="";for(let u of e.messages)if(u.role==="system"){let c="";typeof u.content=="string"?c=u.content:Array.isArray(u.content)&&(c=u.content.filter(l=>l.type==="text"&&l.text).map(l=>l.text).join(`
|
|
74
74
|
`)),c&&(s=s?`${s}
|
|
75
75
|
|
|
@@ -338,7 +338,7 @@ ${s.content}`),delete s.thinking)});let n=e.messages[e.messages.length-1];return
|
|
|
338
338
|
error: ${r.message}
|
|
339
339
|
stack: ${r.stack}`),!1}}async initialize(){try{await this.registerDefaultTransformersInternal(),await this.loadFromConfig()}catch(e){this.logger.error(`TransformerService init error: ${e.message}
|
|
340
340
|
Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(dy).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 KS}from"tiktoken";var Qs=class{type="tiktoken";name;encoding;constructor(e="cl100k_base"){this.name=`tiktoken-${e}`;try{this.encoding=KS(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:s,system:i,tools:u}=e;return Array.isArray(s)&&s.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 i=="string"?n+=r.encode(i).length:Array.isArray(i)&&i.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}))}),u&&u.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 Ba}from"path";import{homedir as oT}from"os";import{existsSync as ld,mkdirSync as iT}from"fs";import{promises as ja}from"fs";var YS=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 s=0;s<r.length;++s){let i=r[s];n=n[i]??=Object.create(null)}n.end=r}return e}split(t){let e=[],r=t.length,n=0,s=0;for(;s<r;){let i=this.trie,u=null,c=s;for(;c<r&&(i=i[t[c]]);)i.end&&(u=i.end),++c;u?(s>n&&e.push(t.slice(n,s)),e.push(u),s+=u.length,n=s):++s}return n<r&&e.push(t.slice(n)),e}},XS=YS,QS=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}},ZS=QS,_y=(()=>{let t=[...Array.from({length:94},(s,i)=>i+33),...Array.from({length:12},(s,i)=>i+161),...Array.from({length:82},(s,i)=>i+174)],e=t.slice(),r=0;for(let s=0;s<256;++s)t.includes(s)||(t.push(s),e.push(256+r),r+=1);let n=e.map(s=>String.fromCharCode(s));return Object.fromEntries(t.map((s,i)=>[s,n[i]]))})(),ek=t=>Object.fromEntries(Object.entries(t).map(([e,r])=>[r,e])),tk=ek(_y),fy=".,!?\u2026\u3002\uFF0C\u3001\u0964\u06D4\u060C",rk=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|[${fy}])]+`,` ?[^\\s${fy}]+`]]),Zs="\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E",hx=new RegExp(`^[${Zs}]+$`,"gu"),ud=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"),Pa=(t,e=!0)=>{if(t.Regex!==void 0){let r=t.Regex.replace(/\\([#&~])/g,"$1");for(let[n,s]of rk)r=r.replaceAll(n,s);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,"\\$&"),sk=(t,e,r)=>{let n=[],s=0;for(;s<t.length;){if(n.push(t[s]),(e.get(t[s])??r)!==r){++s;continue}for(;++s<t.length&&(e.get(t[s])??r)===r;)e.get(n.at(-1))!==r&&(n[n.length-1]+=t[s])}return n},ok=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,ik=t=>Number.isInteger(t)||typeof t=="bigint",ak=t=>{let e=0;for(let r of t)++e;return e},uk=t=>by(t.toLowerCase()),Ot=(...t)=>Array.prototype.concat.apply([],t),cd=t=>new Map(Object.entries(t)),ck=(t,e)=>{let r=[],n=0;for(let s of t.matchAll(e)){let i=s[0];n<s.index&&r.push(t.slice(n,s.index)),i.length>0&&r.push(i),n=s.index+i.length}return n<t.length&&r.push(t.slice(n)),r},by=t=>t.replace(new RegExp("\\p{M}","gu"),""),hy=(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},lk=t=>t.match(/\S+/g)||[],dk=class{constructor(){let t=function(...e){return t._call(...e)};return Object.setPrototypeOf(t,new.target.prototype)}},eo=dk,fk=class extends eo{constructor(t){super(),this.config=t}_call(t){return this.normalize(t)}},Er=fk,hk=class extends Er{tokenize_chinese_chars(t){let e=[];for(let r=0;r<t.length;++r){let n=t[r],s=n.charCodeAt(0);ok(s)?(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`
|
|
341
|
-
`: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}},pk=hk,mk=class extends Er{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}},gk=mk,yk=class extends Er{constructor(t){super(t),this.normalizers=(t.normalizers??[]).map(e=>Cy(e))}normalize(t){return this.normalizers.reduce((e,r)=>r?r.normalize(e):e,t)}},_k=yk,bk=class extends Er{normalize(t){let e=Pa(this.config.pattern??{});return e===null?t:t.replaceAll(e,this.config.content??"")}},Ck=bk,wk=class extends Er{constructor(){super(...arguments),this.form="NFC"}normalize(t){return t=t.normalize(this.form),t}},Ia=wk,Ek=class extends Ia{constructor(){super(...arguments),this.form="NFC"}},Ak=Ek,Sk=class extends Ia{constructor(){super(...arguments),this.form="NFD"}},kk=Sk,Dk=class extends Ia{constructor(){super(...arguments),this.form="NFKC"}},Tk=Dk,vk=class extends Ia{constructor(){super(...arguments),this.form="NFKD"}},Rk=vk,Ok=class extends Er{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}},xk=Ok,Fk=class extends Er{normalize(t){return by(t)}},Pk=Fk,Ik=class extends Er{normalize(t){return t.toLowerCase()}},Nk=Ik,Bk=class extends Er{normalize(t){return t=this.config.prepend+t,t}},jk=Bk;function Mk(t){if(t===null)return null;switch(t.type){case"BertNormalizer":return new pk(t);case"Precompiled":return new gk(t);case"Sequence":return new _k(t);case"Replace":return new Ck(t);case"NFC":return new Ak(t);case"NFD":return new kk(t);case"NFKC":return new Tk(t);case"NFKD":return new Rk(t);case"Strip":return new xk(t);case"StripAccents":return new Pk(t);case"Lowercase":return new Nk(t);case"Prepend":return new jk(t);default:throw new Error(`Unknown Normalizer type: ${t.type}`)}}var Cy=Mk,Lk=class extends eo{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)}},sr=Lk,Uk=class extends sr{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=_y,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(""))}},qk=Uk,$k=class extends sr{pre_tokenize_text(t,e){return t.match(/\w+|[^\w\s]+/g)||[]}},zk=$k,Hk=class extends sr{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]}},Wk=Hk,Gk=class extends sr{constructor(t){super(),this.config=t,this.pattern=Pa(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):ck(t,this.pattern)}},Jk=Gk,Vk=class extends sr{constructor(t){super(),this.config=t,this.pattern=new RegExp(`[^${Zs}]+|[${Zs}]+`,"gu")}pre_tokenize_text(t){return t.match(this.pattern)||[]}},Kk=Vk,Yk=class extends sr{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)||[]}},Xk=Yk,Qk=class extends sr{constructor(){super(),this.pattern=new RegExp(`[^\\s${Zs}]+|[${Zs}]`,"gu")}pre_tokenize_text(t,e){return t.trim().match(this.pattern)||[]}},Zk=Qk,eD=class extends sr{constructor(t){super(),this.config=t,this.pattern=Pa(this.config.pattern??{}),this.content=this.config.content??""}pre_tokenize_text(t){return this.pattern===null?[t]:[t.replaceAll(this.pattern,this.config.content??"")]}},tD=eD,rD=class extends sr{constructor(t){super(),this.tokenizers=(t.pretokenizers??[]).map(e=>wy(e))}pre_tokenize_text(t,e){return this.tokenizers.reduce((r,n)=>n?n.pre_tokenize(r,e):r,[t])}},nD=rD,sD=class extends sr{pre_tokenize_text(t){return lk(t)}},oD=sD;function iD(t){if(t===null)return null;switch(t.type){case"BertPreTokenizer":return new Zk;case"Sequence":return new nD(t);case"Whitespace":return new zk;case"WhitespaceSplit":return new oD;case"Metaspace":return new Wk(t);case"ByteLevel":return new qk(t);case"Split":return new Jk(t);case"Punctuation":return new Kk(t);case"Digits":return new Xk(t);case"Replace":return new tD(t);default:throw new Error(`Unknown PreTokenizer type: ${t.type}`)}}var wy=iD,aD=class extends eo{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=sk(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)}},Na=aD,uD=class extends Na{constructor(t){super(t),this.max_input_chars_per_word=100,this.tokens_to_ids=cd(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 s=!1,i=0,u=[];for(;i<n.length;){let c=n.length,l=null;for(;i<c;){let d=n.slice(i,c).join("");if(i>0&&(d=this.config.continuing_subword_prefix+d),this.tokens_to_ids.has(d)){l=d;break}--c}if(l===null){s=!0;break}u.push(l),i=c}s?e.push(this.unk_token):e.push(...u)}return e}},py=uD,my=class Ey{constructor(e,r){this.is_leaf=e,this.children=r}static default(){return new Ey(!1,new Map)}},cD=class{constructor(){this.root=my.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=my.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)}}},lD=cD,ad=class Ay{constructor(e,r,n,s,i){this.token_id=e,this.node_id=r,this.pos=n,this.length=s,this.score=i,this.prev=null,this.backtrace_score=0}clone(){let e=new Ay(this.token_id,this.node_id,this.pos,this.length,this.score);return e.prev=this.prev,e.backtrace_score=this.backtrace_score,e}},dD=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 ad(this.bos_token_id??0,0,0,0,0),s=new ad(this.eos_token_id??0,1,this.len,0,0);this.nodes.push(n.clone()),this.nodes.push(s.clone()),this.begin_nodes[this.len].push(s),this.end_nodes[0].push(n)}insert(t,e,r,n){let s=this.nodes.length,i=new ad(n,s,t,e,r);this.begin_nodes[t].push(i),this.end_nodes[t+e].push(i),this.nodes.push(i)}viterbi(){let t=this.len,e=0;for(;e<=t;){if(this.begin_nodes[e].length==0)return[];for(let i of this.begin_nodes[e]){i.prev=null;let u=0,c=null;for(let l of this.end_nodes[e]){let d=l.backtrace_score+i.score;(c===null||d>u)&&(c=l.clone(),u=d)}if(c!==null)i.prev=c,i.backtrace_score=u;else return[]}++e}let r=[],n=this.begin_nodes[t][0].prev;if(n===null)return[];let s=n.clone();for(;s.prev!==null;)r.push(s.clone()),s=s.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)}},fD=dD;function hD(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 pD=class extends Na{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,s)=>[n,s])),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=hD(this.scores)[0],this.unk_score=this.min_score-10,this.scores[this.unk_token_id]=this.unk_score,this.trie=new lD,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 s=!1,i=[],u=e.slice(n).join(""),c=this.trie.common_prefix_search(u);for(let l of c){i.push(l);let d=this.tokens_to_ids.get(l),h=this.scores[d],m=ak(l);t.insert(n,m,h,d),!s&&m===r&&(s=!0)}s||t.insert(n,r,this.unk_score,this.unk_token_id),n+=r}}tokenize(t){let e=new fD(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}},gy=pD,mD=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}},gD=mD,yD=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()}},_D=yD,bD=class extends Na{constructor(t){super(t),this.tokens_to_ids=cd(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 _D(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 s=new gD((c,l)=>c.score<l.score),i={token:r[0],bias:0,prev:null,next:null},u=i;for(let c=1;c<r.length;++c){let l={bias:c/r.length,token:r[c],prev:u,next:null};u.next=l,this.add_node(s,u),u=l}for(;!s.is_empty();){let c=s.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:i=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(s,l.prev)):i=l,l.next&&(l.next.prev=l,this.add_node(s,l))}for(let c=i;c!==null;c=c.next)n.push(c.token)}else n=r;if(this.continuing_subword_suffix)for(let s=0;s<n.length-1;++s)n[s]+=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 s of n)if(this.tokens_to_ids.has(s))e.push(s);else if(this.byte_fallback){let i=Array.from(this.text_encoder.encode(s)).map(u=>`<0x${u.toString(16).toUpperCase().padStart(2,"0")}>`);i.every(u=>this.tokens_to_ids.has(u))?e.push(...i):e.push(this.unk_token)}else e.push(this.unk_token)}return e}},yy=bD,CD=class extends Na{constructor(t,e){super(t);let r=t.vocab;this.tokens_to_ids=cd(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,s]of this.tokens_to_ids)this.vocab[s]=n}encode(t){return t}},wD=CD;function ED(t,e){switch(t.type){case"WordPiece":return new py(t);case"Unigram":return new gy(t,e.eos_token);case"BPE":return new yy(t);default:if(t.vocab)return Array.isArray(t.vocab)?new gy(t,e.eos_token):Object.hasOwn(t,"continuing_subword_prefix")&&Object.hasOwn(t,"unk_token")?Object.hasOwn(t,"merges")?new yy(t):new py(t):new wD(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 AD=ED,SD=class extends eo{constructor(t){super(),this.config=t}_call(t,...e){return this.post_process(t,...e)}},to=SD,kD=class extends to{post_process(t,e=null,r=!0){let n=e===null?this.config.single:this.config.pair,s=[],i=[];for(let u of n)"SpecialToken"in u?r&&(s.push(u.SpecialToken.id),i.push(u.SpecialToken.type_id)):"Sequence"in u&&(u.Sequence.id==="A"?(s=Ot(s,t),i=Ot(i,new Array(t.length).fill(u.Sequence.type_id))):u.Sequence.id==="B"&&(s=Ot(s,e),i=Ot(i,new Array(e.length).fill(u.Sequence.type_id))));return{tokens:s,token_type_ids:i}}},DD=kD,TD=class extends to{post_process(t,e=null){return{tokens:e?Ot(t,e):t}}},vD=TD,RD=class extends to{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e=null,r=!0){r&&(t=Ot([this.cls[0]],t,[this.sep[0]]));let n=new Array(t.length).fill(0);if(e){let s=[],i=r?[this.sep[0]]:[];t=Ot(t,s,e,i),n=Ot(n,new Array(e.length+s.length+i.length).fill(1))}return{tokens:t,token_type_ids:n}}},OD=RD,xD=class extends to{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e,r=!0){r&&(t=Ot([this.cls[0]],t,[this.sep[0]]));let n=new Array(t.length).fill(0);if(e){let s=r?[this.sep[0]]:[],i=r?[this.sep[0]]:[];t=Ot(t,s,e,i),n=Ot(n,new Array(e.length+s.length+i.length).fill(1))}return{tokens:t,token_type_ids:n}}},FD=xD,PD=class extends to{constructor(t){super(t),this.processors=(t.processors??[]).map(e=>Sy(e))}post_process(t,e=null,r=!0){let n={tokens:t};for(let s of this.processors)n=s.post_process(n.tokens,e,r);return n}},ID=PD;function ND(t){if(t===null)return null;switch(t.type){case"TemplateProcessing":return new DD(t);case"ByteLevel":return new vD(t);case"BertProcessing":return new OD(t);case"RobertaProcessing":return new FD(t);case"Sequence":return new ID(t);default:throw new Error(`Unknown PostProcessor type: ${t.type}`)}}var Sy=ND,BD=class extends eo{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("")}},or=BD,jD=class extends or{constructor(t){super(t),this.byte_decoder=tk,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(s=>s.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}},MD=jD,LD=class extends or{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=ud(e)),e})}},UD=LD,qD=class extends or{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}},$D=qD,zD=class extends or{constructor(t){super(t),this.suffix=t.suffix??""}decode_chain(t){return t.map((e,r)=>e.replaceAll(this.suffix,r===t.length-1?"":" "))}},HD=zD,WD=class extends or{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=ud(r).replaceAll(this.word_delimiter_token," ").trim()),r}decode_chain(t){return[this.convert_tokens_to_string(t)]}},GD=WD,JD=class extends or{constructor(t){super(t),this.decoders=(t.decoders??[]).map(e=>ky(e))}decode_chain(t){return this.decoders.reduce((e,r)=>r.decode_chain(e),t)}},VD=JD,KD=class extends or{decode_chain(t){let e=Pa(this.config.pattern),r=this.config.content??"";return e===null?t:t.map(n=>n.replaceAll(e,r))}},YD=KD,XD=class extends or{decode_chain(t){return[t.join("")]}},QD=XD,ZD=class extends or{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 s=0;s<this.start&&e[s]===this.content;++s)r=s+1;let n=e.length;for(let s=0;s<this.stop;++s){let i=e.length-s-1;if(e[i]===this.content){n=i;continue}else break}return e.slice(r,n)})}},eT=ZD,tT=class extends or{constructor(t){super(t),this.text_decoder=new TextDecoder}decode_chain(t){let e=[],r=[];for(let n of t){let s=null;if(n.length===6&&n.startsWith("<0x")&&n.endsWith(">")){let i=parseInt(n.slice(3,5),16);isNaN(i)||(s=i)}if(s!==null)r.push(s);else{if(r.length>0){let i=this.text_decoder.decode(Uint8Array.from(r));e.push(i),r=[]}e.push(n)}}if(r.length>0){let n=this.text_decoder.decode(Uint8Array.from(r));e.push(n),r=[]}return e}},rT=tT;function nT(t){if(t===null)return null;switch(t.type){case"ByteLevel":return new MD(t);case"WordPiece":return new UD(t);case"Metaspace":return new $D(t);case"BPEDecoder":return new HD(t);case"CTC":return new GD(t);case"Sequence":return new VD(t);case"Replace":return new YD(t);case"Fuse":return new QD(t);case"Strip":return new eT(t);case"ByteFallback":return new rT(t);default:throw new Error(`Unknown Decoder type: ${t.type}`)}}var ky=nT,sT=class{constructor(t,e){let r=hy(t,"Tokenizer",["model","decoder","post_processor","pre_tokenizer","normalizer"]);if(r)throw new Error(r);let n=hy(e,"Config");if(n)throw new Error(n);this.tokenizer=t,this.config=e,this.normalizer=Cy(this.tokenizer.normalizer),this.pre_tokenizer=wy(this.tokenizer.pre_tokenizer),this.model=AD(this.tokenizer.model,this.config),this.post_processor=Sy(this.tokenizer.post_processor),this.decoder=ky(this.tokenizer.decoder),this.special_tokens=[],this.all_special_ids=[],this.added_tokens=[],this.tokenizer.added_tokens.forEach(s=>{let i=new ZS(s);this.added_tokens.push(i),this.model.tokens_to_ids.set(i.content,i.id),this.model.vocab[i.id]=i.content,i.special&&(this.special_tokens.push(i.content),this.all_special_ids.push(i.id))}),(this.config.additional_special_tokens??[]).forEach(s=>{this.special_tokens.includes(s)||this.special_tokens.push(s)}),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 XS(this.added_tokens.map(s=>s.content)),this.added_tokens_map=new Map(this.added_tokens.map(s=>[s.content,s])),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:s,token_type_ids:i}=this.tokenize_helper(t,{text_pair:e,add_special_tokens:r}),u=this.model.convert_tokens_to_ids(s),c={ids:u,tokens:s,attention_mask:new Array(u.length).fill(1)};return n&&i&&(c.token_type_ids=i),c}decode(t,e={}){if(!Array.isArray(t)||t.length===0||!ik(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(s=>!this.special_tokens.includes(s)));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=ud(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 s=this.added_tokens_map.get(r);s&&(s.lstrip&&n>0&&(e[n-1]=e[n-1].trimEnd()),s.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 s=this.pre_tokenizer!==null?this.pre_tokenizer(r,{section_index:n}):[r];return this.model(s)})}tokenize_helper(t,{text_pair:e=null,add_special_tokens:r=!0}){let n=this.encode_text(t),s=this.encode_text(e||null);return this.post_processor?this.post_processor(n,s,r):{tokens:Ot(n??[],s??[])}}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}},Dy=sT;var Ma=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||Ba(oT(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=Ba(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:Ba(e,"tokenizer.json"),tokenizerConfig:Ba(e,"tokenizer_config.json")}}ensureDir(e){ld(e)||iT(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!ld(e.tokenizerJson)||!ld(e.tokenizerConfig))return null;let[r,n]=await Promise.all([ja.readFile(e.tokenizerJson,"utf-8"),ja.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,s=setTimeout(()=>n.abort(),this.options.timeout||3e4);try{let[i,u]=await Promise.all([fetch(r.json,{signal:n.signal}),fetch(r.config,{signal:n.signal})]);if(!i.ok)throw new Error(`Failed to fetch tokenizer.json: ${i.statusText}`);let[c,l]=await Promise.all([i.json(),u.ok?u.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([ja.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),ja.writeFile(e.tokenizerConfig,JSON.stringify(l,null,2))]),{tokenizerJson:c,tokenizerConfig:l}}finally{clearTimeout(s)}}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 Dy(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:s,tools:i}=e;if(Array.isArray(n)){for(let u of n)if(typeof u.content=="string")r.push(u.content);else if(Array.isArray(u.content))for(let c of u.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 s=="string")r.push(s);else if(Array.isArray(s)){for(let u of s)if(u.type==="text"){if(typeof u.text=="string")r.push(u.text);else if(Array.isArray(u.text))for(let c of u.text)c&&r.push(c)}}if(i)for(let u of i)u.name&&r.push(u.name),u.description&&r.push(u.description),u.input_schema&&r.push(JSON.stringify(u.input_schema));return r.join(" ")}};var La=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 s=new URL(e.url);this.name=`api-${s.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},s=new AbortController,i=setTimeout(()=>s.abort(),this.options.timeout||3e4),u=await fetch(this.config.url,{method:"POST",headers:n,body:JSON.stringify(r),signal:s.signal});if(clearTimeout(i),!u.ok)throw new Error(`API tokenizer request failed: ${u.status} ${u.statusText}`);let c=await u.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(s=>{s&&r.push(s)}))}),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("."),s=e;for(let i of n){if(s==null)throw new Error(`Field path '${r}' not found in response`);s=s[i]}if(typeof s!="number")throw new Error(`Expected number at field path '${r}', got ${typeof s}`);return s}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 as=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 Qs("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 Qs(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),n=new Ma(e.model,this.logger,{timeout:this.options.timeout});break;case"api":n=new La(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(s){return this.logger?.error(`Failed to initialize ${e.type} tokenizer: ${s.message}`),this.logger?.error(`Error stack: ${s.stack}`),this.fallbackTokenizer||await this.initialize(),this.tokenizers.set(r,this.fallbackTokenizer),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 s=(this.configService.get("providers")||[]).find(i=>i.name===e);if(s?.tokenizer)return s.tokenizer.models?.[r]?s.tokenizer.models[r]:s.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 aT}from"tiktoken";import{readFile as dd}from"fs/promises";import{opendir as uT,stat as cT}from"fs/promises";import{join as fd}from"path";import{CLAUDE_PROJECTS_DIR as Ty,HOME_DIR as vy}from"@thxp/shared";import{LRUCache as lT}from"lru-cache";var Ar=aT("cl100k_base"),Ry=(t,e,r)=>{let n=0;return Array.isArray(t)&&t.forEach(s=>{typeof s.content=="string"?n+=Ar.encode(s.content).length:Array.isArray(s.content)&&s.content.forEach(i=>{i.type==="text"?n+=Ar.encode(i.text).length:i.type==="tool_use"?n+=Ar.encode(JSON.stringify(i.input)).length:i.type==="tool_result"&&(n+=Ar.encode(typeof i.content=="string"?i.content:JSON.stringify(i.content)).length)})}),typeof e=="string"?n+=Ar.encode(e).length:Array.isArray(e)&&e.forEach(s=>{s.type==="text"&&(typeof s.text=="string"?n+=Ar.encode(s.text).length:Array.isArray(s.text)&&s.text.forEach(i=>{n+=Ar.encode(i||"").length}))}),r&&r.forEach(s=>{s.description&&(n+=Ar.encode(s.name+s.description).length),s.input_schema&&(n+=Ar.encode(JSON.stringify(s.input_schema)).length)}),n},dT=async(t,e)=>{if(t.sessionId){let r=await Oy(t.sessionId);if(r){let n=fd(vy,r,"config.json"),s=fd(vy,r,`${t.sessionId}.json`);try{let i=JSON.parse(await dd(s,"utf8"));if(i&&i.Router)return i.Router}catch{}try{let i=JSON.parse(await dd(n,"utf8"));if(i&&i.Router)return i.Router}catch{}}}},fT=async(t,e,r)=>{let n=await dT(t,r),s=r.get("providers")||[],i=n||r.get("Router");if(t.body.model.includes(",")){let[k,E]=t.body.model.split(","),g=s.find(p=>p.name.toLowerCase()===k),y=g?.models?.find(p=>p.toLowerCase()===E);return g&&y?{model:`${g.name},${y}`,scenarioType:"default"}:{model:t.body.model,scenarioType:"default"}}let u=(k,E)=>k.model_limits?.[E]!==void 0?Cs(k.name,E)>=k.model_limits[E]:!1,c=k=>{if(!k)return null;if(typeof k=="string"){let E="",g="";if(k.includes(","))[E,g]=k.split(",");else{g=k;let y=s.find(p=>p.models.includes(g));y&&(E=y.name)}if(E&&g){let y=s.find(p=>p.name.toLowerCase()===E.toLowerCase());if(y&&Tn(vn(E,g))||y&&u(y,g))return null}return k}if(Array.isArray(k)){for(let E of k)if(typeof E=="string"&&E.trim())if(E.includes(",")){let[g,y]=E.split(","),p=s.find(D=>D.name.toLowerCase()===g.toLowerCase());if(p&&p.models.includes(y)&&!Tn(vn(g,y))&&!u(p,y))return E}else{let g=s.find(y=>y.models.includes(E));if(g&&!Tn(vn(g.name,E))&&!u(g,E))return E}}return null};if(t.body?.system?.length>1&&t.body?.system[1]?.text?.startsWith("<CCR-SUBAGENT-MODEL>")){let k=t.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);if(k)return t.body.system[1].text=t.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${k[1]}</CCR-SUBAGENT-MODEL>`,""),{model:k[1],scenarioType:"default"}}if(t.body.model?.includes("claude")&&t.body.model?.includes("haiku")&&i?.background){t.log.info(`Using background model for ${t.body.model}`);let k=c(i.background);if(k)return{model:k,scenarioType:"background"}}let l=Array.isArray(t.body.messages)?t.body.messages:[],d=!1;for(let k=l.length-1;k>=0;k--){let E=l[k];if(E.role==="assistant"){if(!Array.isArray(E.content))break;d=E.content.some(g=>g.type==="tool_use"&&(g.name==="WebSearch"||g.name==="web_search"));break}}if(d&&i?.webSearch){let k=c(i.webSearch);if(k)return{model:k,scenarioType:"webSearch"}}if((t.body.thinking===!0||typeof t.body.thinking=="object"&&t.body.thinking!==null&&t.body.thinking.type==="enabled")&&i?.think){t.log.info(`Using think model for ${t.body.thinking}`);let k=c(i.think);if(k)return{model:k,scenarioType:"think"}}let m=c(i?.default);return m?{model:m,scenarioType:"default"}:{model:Array.isArray(i?.default)?i.default[0]:i?.default,scenarioType:"default"}},hd=async(t,e,r)=>{let{configService:n,event:s}=r;if(t.body&&Array.isArray(t.body.model)&&(t.body.model=t.body.model[0]),t.body.metadata?.user_id)try{let d=JSON.parse(t.body.metadata.user_id);d.session_id&&(t.sessionId=d.session_id)}catch{let d=t.body.metadata.user_id.split("_session_");d.length>1&&(t.sessionId=d[1])}let{messages:i,system:u=[],tools:c}=t.body,l=n.get("REWRITE_SYSTEM_PROMPT");if(l&&u.length>1&&u[1]?.text?.includes("<env>")){let d=await dd(l,"utf-8");u[1].text=`${d}<env>${u[1].text.split("<env>").pop()}`}try{let[d,h]=(t.body.model||"").split(","),m=r.tokenizerService?.getTokenizerConfigForModel(d,h),w;r.tokenizerService?w=(await r.tokenizerService.countTokens({messages:i,system:u,tools:c},m)).tokenCount:w=Ry(i,u,c);let k,E=n.get("CUSTOM_ROUTER_PATH");if(E)try{let g=Q(E);t.tokenCount=w,k=await g(t,n.getAll(),{event:s})}catch(g){t.log.error(`failed to load custom router: ${g.message}`)}if(k)t.scenarioType="default";else{let g=await fT(t,w,n);k=g.model,t.scenarioType=g.scenarioType}t.body.model=k,k&&k.includes(",")&&(t.provider=k.split(",")[0])}catch(d){t.log.error(`Error in router middleware: ${d.message}`);let m=n.get("Router")?.default;t.body.model=Array.isArray(m)?m[0]:m,t.scenarioType="default",!t.provider&&t.body.model&&t.body.model.includes(",")&&(t.provider=t.body.model.split(",")[0])}},ro=new lT({max:1e3}),Oy=async t=>{if(ro.has(t)){let e=ro.get(t);return!e||e===""?null:e}try{let e=await uT(Ty),r=[];for await(let i of e)i.isDirectory()&&r.push(i.name);let n=r.map(async i=>{let u=fd(Ty,i,`${t}.jsonl`);try{return(await cT(u)).isFile()?i:null}catch{return null}}),s=await Promise.all(n);for(let i of s)if(i)return ro.set(t,i),i;return ro.set(t,""),null}catch(e){return console.error("Error searching for project by session:",e),ro.set(t,""),null}};var pd=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),s=this.pluginInstances.get(e);if(!n||!s)throw new Error(`Plugin ${e} not found`);n.enabled&&await r.register(s.register,n.options)}async enablePlugins(e){for(let[r,n]of this.plugins)if(n.enabled)try{await this.enablePlugin(r,e)}catch(s){let i=s instanceof Error?s.message:String(s);e.log?.error(`Failed to enable plugin ${r}: ${i}`)}}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()}},xy=new pd;var qy=Fr(By(),1);var us=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,r)=>{this.buffer+=e;let n=this.buffer.split(`
|
|
341
|
+
`: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}},pk=hk,mk=class extends Er{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}},gk=mk,yk=class extends Er{constructor(t){super(t),this.normalizers=(t.normalizers??[]).map(e=>Cy(e))}normalize(t){return this.normalizers.reduce((e,r)=>r?r.normalize(e):e,t)}},_k=yk,bk=class extends Er{normalize(t){let e=Pa(this.config.pattern??{});return e===null?t:t.replaceAll(e,this.config.content??"")}},Ck=bk,wk=class extends Er{constructor(){super(...arguments),this.form="NFC"}normalize(t){return t=t.normalize(this.form),t}},Ia=wk,Ek=class extends Ia{constructor(){super(...arguments),this.form="NFC"}},Ak=Ek,Sk=class extends Ia{constructor(){super(...arguments),this.form="NFD"}},kk=Sk,Dk=class extends Ia{constructor(){super(...arguments),this.form="NFKC"}},Tk=Dk,vk=class extends Ia{constructor(){super(...arguments),this.form="NFKD"}},Rk=vk,Ok=class extends Er{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}},xk=Ok,Fk=class extends Er{normalize(t){return by(t)}},Pk=Fk,Ik=class extends Er{normalize(t){return t.toLowerCase()}},Nk=Ik,Bk=class extends Er{normalize(t){return t=this.config.prepend+t,t}},jk=Bk;function Mk(t){if(t===null)return null;switch(t.type){case"BertNormalizer":return new pk(t);case"Precompiled":return new gk(t);case"Sequence":return new _k(t);case"Replace":return new Ck(t);case"NFC":return new Ak(t);case"NFD":return new kk(t);case"NFKC":return new Tk(t);case"NFKD":return new Rk(t);case"Strip":return new xk(t);case"StripAccents":return new Pk(t);case"Lowercase":return new Nk(t);case"Prepend":return new jk(t);default:throw new Error(`Unknown Normalizer type: ${t.type}`)}}var Cy=Mk,Lk=class extends eo{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)}},sr=Lk,Uk=class extends sr{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=_y,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(""))}},qk=Uk,$k=class extends sr{pre_tokenize_text(t,e){return t.match(/\w+|[^\w\s]+/g)||[]}},zk=$k,Hk=class extends sr{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]}},Wk=Hk,Gk=class extends sr{constructor(t){super(),this.config=t,this.pattern=Pa(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):ck(t,this.pattern)}},Jk=Gk,Vk=class extends sr{constructor(t){super(),this.config=t,this.pattern=new RegExp(`[^${Zs}]+|[${Zs}]+`,"gu")}pre_tokenize_text(t){return t.match(this.pattern)||[]}},Kk=Vk,Yk=class extends sr{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)||[]}},Xk=Yk,Qk=class extends sr{constructor(){super(),this.pattern=new RegExp(`[^\\s${Zs}]+|[${Zs}]`,"gu")}pre_tokenize_text(t,e){return t.trim().match(this.pattern)||[]}},Zk=Qk,eD=class extends sr{constructor(t){super(),this.config=t,this.pattern=Pa(this.config.pattern??{}),this.content=this.config.content??""}pre_tokenize_text(t){return this.pattern===null?[t]:[t.replaceAll(this.pattern,this.config.content??"")]}},tD=eD,rD=class extends sr{constructor(t){super(),this.tokenizers=(t.pretokenizers??[]).map(e=>wy(e))}pre_tokenize_text(t,e){return this.tokenizers.reduce((r,n)=>n?n.pre_tokenize(r,e):r,[t])}},nD=rD,sD=class extends sr{pre_tokenize_text(t){return lk(t)}},oD=sD;function iD(t){if(t===null)return null;switch(t.type){case"BertPreTokenizer":return new Zk;case"Sequence":return new nD(t);case"Whitespace":return new zk;case"WhitespaceSplit":return new oD;case"Metaspace":return new Wk(t);case"ByteLevel":return new qk(t);case"Split":return new Jk(t);case"Punctuation":return new Kk(t);case"Digits":return new Xk(t);case"Replace":return new tD(t);default:throw new Error(`Unknown PreTokenizer type: ${t.type}`)}}var wy=iD,aD=class extends eo{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=sk(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)}},Na=aD,uD=class extends Na{constructor(t){super(t),this.max_input_chars_per_word=100,this.tokens_to_ids=cd(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 s=!1,i=0,u=[];for(;i<n.length;){let c=n.length,l=null;for(;i<c;){let d=n.slice(i,c).join("");if(i>0&&(d=this.config.continuing_subword_prefix+d),this.tokens_to_ids.has(d)){l=d;break}--c}if(l===null){s=!0;break}u.push(l),i=c}s?e.push(this.unk_token):e.push(...u)}return e}},py=uD,my=class Ey{constructor(e,r){this.is_leaf=e,this.children=r}static default(){return new Ey(!1,new Map)}},cD=class{constructor(){this.root=my.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=my.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)}}},lD=cD,ad=class Ay{constructor(e,r,n,s,i){this.token_id=e,this.node_id=r,this.pos=n,this.length=s,this.score=i,this.prev=null,this.backtrace_score=0}clone(){let e=new Ay(this.token_id,this.node_id,this.pos,this.length,this.score);return e.prev=this.prev,e.backtrace_score=this.backtrace_score,e}},dD=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 ad(this.bos_token_id??0,0,0,0,0),s=new ad(this.eos_token_id??0,1,this.len,0,0);this.nodes.push(n.clone()),this.nodes.push(s.clone()),this.begin_nodes[this.len].push(s),this.end_nodes[0].push(n)}insert(t,e,r,n){let s=this.nodes.length,i=new ad(n,s,t,e,r);this.begin_nodes[t].push(i),this.end_nodes[t+e].push(i),this.nodes.push(i)}viterbi(){let t=this.len,e=0;for(;e<=t;){if(this.begin_nodes[e].length==0)return[];for(let i of this.begin_nodes[e]){i.prev=null;let u=0,c=null;for(let l of this.end_nodes[e]){let d=l.backtrace_score+i.score;(c===null||d>u)&&(c=l.clone(),u=d)}if(c!==null)i.prev=c,i.backtrace_score=u;else return[]}++e}let r=[],n=this.begin_nodes[t][0].prev;if(n===null)return[];let s=n.clone();for(;s.prev!==null;)r.push(s.clone()),s=s.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)}},fD=dD;function hD(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 pD=class extends Na{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,s)=>[n,s])),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=hD(this.scores)[0],this.unk_score=this.min_score-10,this.scores[this.unk_token_id]=this.unk_score,this.trie=new lD,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 s=!1,i=[],u=e.slice(n).join(""),c=this.trie.common_prefix_search(u);for(let l of c){i.push(l);let d=this.tokens_to_ids.get(l),h=this.scores[d],m=ak(l);t.insert(n,m,h,d),!s&&m===r&&(s=!0)}s||t.insert(n,r,this.unk_score,this.unk_token_id),n+=r}}tokenize(t){let e=new fD(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}},gy=pD,mD=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}},gD=mD,yD=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()}},_D=yD,bD=class extends Na{constructor(t){super(t),this.tokens_to_ids=cd(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 _D(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 s=new gD((c,l)=>c.score<l.score),i={token:r[0],bias:0,prev:null,next:null},u=i;for(let c=1;c<r.length;++c){let l={bias:c/r.length,token:r[c],prev:u,next:null};u.next=l,this.add_node(s,u),u=l}for(;!s.is_empty();){let c=s.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:i=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(s,l.prev)):i=l,l.next&&(l.next.prev=l,this.add_node(s,l))}for(let c=i;c!==null;c=c.next)n.push(c.token)}else n=r;if(this.continuing_subword_suffix)for(let s=0;s<n.length-1;++s)n[s]+=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 s of n)if(this.tokens_to_ids.has(s))e.push(s);else if(this.byte_fallback){let i=Array.from(this.text_encoder.encode(s)).map(u=>`<0x${u.toString(16).toUpperCase().padStart(2,"0")}>`);i.every(u=>this.tokens_to_ids.has(u))?e.push(...i):e.push(this.unk_token)}else e.push(this.unk_token)}return e}},yy=bD,CD=class extends Na{constructor(t,e){super(t);let r=t.vocab;this.tokens_to_ids=cd(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,s]of this.tokens_to_ids)this.vocab[s]=n}encode(t){return t}},wD=CD;function ED(t,e){switch(t.type){case"WordPiece":return new py(t);case"Unigram":return new gy(t,e.eos_token);case"BPE":return new yy(t);default:if(t.vocab)return Array.isArray(t.vocab)?new gy(t,e.eos_token):Object.hasOwn(t,"continuing_subword_prefix")&&Object.hasOwn(t,"unk_token")?Object.hasOwn(t,"merges")?new yy(t):new py(t):new wD(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 AD=ED,SD=class extends eo{constructor(t){super(),this.config=t}_call(t,...e){return this.post_process(t,...e)}},to=SD,kD=class extends to{post_process(t,e=null,r=!0){let n=e===null?this.config.single:this.config.pair,s=[],i=[];for(let u of n)"SpecialToken"in u?r&&(s.push(u.SpecialToken.id),i.push(u.SpecialToken.type_id)):"Sequence"in u&&(u.Sequence.id==="A"?(s=Ot(s,t),i=Ot(i,new Array(t.length).fill(u.Sequence.type_id))):u.Sequence.id==="B"&&(s=Ot(s,e),i=Ot(i,new Array(e.length).fill(u.Sequence.type_id))));return{tokens:s,token_type_ids:i}}},DD=kD,TD=class extends to{post_process(t,e=null){return{tokens:e?Ot(t,e):t}}},vD=TD,RD=class extends to{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e=null,r=!0){r&&(t=Ot([this.cls[0]],t,[this.sep[0]]));let n=new Array(t.length).fill(0);if(e){let s=[],i=r?[this.sep[0]]:[];t=Ot(t,s,e,i),n=Ot(n,new Array(e.length+s.length+i.length).fill(1))}return{tokens:t,token_type_ids:n}}},OD=RD,xD=class extends to{constructor(t){super(t),this.sep=t.sep,this.cls=t.cls}post_process(t,e,r=!0){r&&(t=Ot([this.cls[0]],t,[this.sep[0]]));let n=new Array(t.length).fill(0);if(e){let s=r?[this.sep[0]]:[],i=r?[this.sep[0]]:[];t=Ot(t,s,e,i),n=Ot(n,new Array(e.length+s.length+i.length).fill(1))}return{tokens:t,token_type_ids:n}}},FD=xD,PD=class extends to{constructor(t){super(t),this.processors=(t.processors??[]).map(e=>Sy(e))}post_process(t,e=null,r=!0){let n={tokens:t};for(let s of this.processors)n=s.post_process(n.tokens,e,r);return n}},ID=PD;function ND(t){if(t===null)return null;switch(t.type){case"TemplateProcessing":return new DD(t);case"ByteLevel":return new vD(t);case"BertProcessing":return new OD(t);case"RobertaProcessing":return new FD(t);case"Sequence":return new ID(t);default:throw new Error(`Unknown PostProcessor type: ${t.type}`)}}var Sy=ND,BD=class extends eo{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("")}},or=BD,jD=class extends or{constructor(t){super(t),this.byte_decoder=tk,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(s=>s.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}},MD=jD,LD=class extends or{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=ud(e)),e})}},UD=LD,qD=class extends or{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}},$D=qD,zD=class extends or{constructor(t){super(t),this.suffix=t.suffix??""}decode_chain(t){return t.map((e,r)=>e.replaceAll(this.suffix,r===t.length-1?"":" "))}},HD=zD,WD=class extends or{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=ud(r).replaceAll(this.word_delimiter_token," ").trim()),r}decode_chain(t){return[this.convert_tokens_to_string(t)]}},GD=WD,JD=class extends or{constructor(t){super(t),this.decoders=(t.decoders??[]).map(e=>ky(e))}decode_chain(t){return this.decoders.reduce((e,r)=>r.decode_chain(e),t)}},VD=JD,KD=class extends or{decode_chain(t){let e=Pa(this.config.pattern),r=this.config.content??"";return e===null?t:t.map(n=>n.replaceAll(e,r))}},YD=KD,XD=class extends or{decode_chain(t){return[t.join("")]}},QD=XD,ZD=class extends or{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 s=0;s<this.start&&e[s]===this.content;++s)r=s+1;let n=e.length;for(let s=0;s<this.stop;++s){let i=e.length-s-1;if(e[i]===this.content){n=i;continue}else break}return e.slice(r,n)})}},eT=ZD,tT=class extends or{constructor(t){super(t),this.text_decoder=new TextDecoder}decode_chain(t){let e=[],r=[];for(let n of t){let s=null;if(n.length===6&&n.startsWith("<0x")&&n.endsWith(">")){let i=parseInt(n.slice(3,5),16);isNaN(i)||(s=i)}if(s!==null)r.push(s);else{if(r.length>0){let i=this.text_decoder.decode(Uint8Array.from(r));e.push(i),r=[]}e.push(n)}}if(r.length>0){let n=this.text_decoder.decode(Uint8Array.from(r));e.push(n),r=[]}return e}},rT=tT;function nT(t){if(t===null)return null;switch(t.type){case"ByteLevel":return new MD(t);case"WordPiece":return new UD(t);case"Metaspace":return new $D(t);case"BPEDecoder":return new HD(t);case"CTC":return new GD(t);case"Sequence":return new VD(t);case"Replace":return new YD(t);case"Fuse":return new QD(t);case"Strip":return new eT(t);case"ByteFallback":return new rT(t);default:throw new Error(`Unknown Decoder type: ${t.type}`)}}var ky=nT,sT=class{constructor(t,e){let r=hy(t,"Tokenizer",["model","decoder","post_processor","pre_tokenizer","normalizer"]);if(r)throw new Error(r);let n=hy(e,"Config");if(n)throw new Error(n);this.tokenizer=t,this.config=e,this.normalizer=Cy(this.tokenizer.normalizer),this.pre_tokenizer=wy(this.tokenizer.pre_tokenizer),this.model=AD(this.tokenizer.model,this.config),this.post_processor=Sy(this.tokenizer.post_processor),this.decoder=ky(this.tokenizer.decoder),this.special_tokens=[],this.all_special_ids=[],this.added_tokens=[],this.tokenizer.added_tokens.forEach(s=>{let i=new ZS(s);this.added_tokens.push(i),this.model.tokens_to_ids.set(i.content,i.id),this.model.vocab[i.id]=i.content,i.special&&(this.special_tokens.push(i.content),this.all_special_ids.push(i.id))}),(this.config.additional_special_tokens??[]).forEach(s=>{this.special_tokens.includes(s)||this.special_tokens.push(s)}),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 XS(this.added_tokens.map(s=>s.content)),this.added_tokens_map=new Map(this.added_tokens.map(s=>[s.content,s])),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:s,token_type_ids:i}=this.tokenize_helper(t,{text_pair:e,add_special_tokens:r}),u=this.model.convert_tokens_to_ids(s),c={ids:u,tokens:s,attention_mask:new Array(u.length).fill(1)};return n&&i&&(c.token_type_ids=i),c}decode(t,e={}){if(!Array.isArray(t)||t.length===0||!ik(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(s=>!this.special_tokens.includes(s)));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=ud(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 s=this.added_tokens_map.get(r);s&&(s.lstrip&&n>0&&(e[n-1]=e[n-1].trimEnd()),s.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 s=this.pre_tokenizer!==null?this.pre_tokenizer(r,{section_index:n}):[r];return this.model(s)})}tokenize_helper(t,{text_pair:e=null,add_special_tokens:r=!0}){let n=this.encode_text(t),s=this.encode_text(e||null);return this.post_processor?this.post_processor(n,s,r):{tokens:Ot(n??[],s??[])}}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}},Dy=sT;var Ma=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||Ba(oT(),".claude-code-router",".huggingface"),this.safeModelName=e.replace(/\//g,"_").replace(/[^a-zA-Z0-9_-]/g,"_"),this.name=`huggingface-${e.split("/").pop()}`}getCachePaths(){let e=Ba(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:Ba(e,"tokenizer.json"),tokenizerConfig:Ba(e,"tokenizer_config.json")}}ensureDir(e){ld(e)||iT(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!ld(e.tokenizerJson)||!ld(e.tokenizerConfig))return null;let[r,n]=await Promise.all([ja.readFile(e.tokenizerJson,"utf-8"),ja.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,s=setTimeout(()=>n.abort(),this.options.timeout||3e4);try{let[i,u]=await Promise.all([fetch(r.json,{signal:n.signal}),fetch(r.config,{signal:n.signal})]);if(!i.ok)throw new Error(`Failed to fetch tokenizer.json: ${i.statusText}`);let[c,l]=await Promise.all([i.json(),u.ok?u.json():Promise.resolve({})]);return this.ensureDir(e.modelDir),await Promise.all([ja.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),ja.writeFile(e.tokenizerConfig,JSON.stringify(l,null,2))]),{tokenizerJson:c,tokenizerConfig:l}}finally{clearTimeout(s)}}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 Dy(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:s,tools:i}=e;if(Array.isArray(n)){for(let u of n)if(typeof u.content=="string")r.push(u.content);else if(Array.isArray(u.content))for(let c of u.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 s=="string")r.push(s);else if(Array.isArray(s)){for(let u of s)if(u.type==="text"){if(typeof u.text=="string")r.push(u.text);else if(Array.isArray(u.text))for(let c of u.text)c&&r.push(c)}}if(i)for(let u of i)u.name&&r.push(u.name),u.description&&r.push(u.description),u.input_schema&&r.push(JSON.stringify(u.input_schema));return r.join(" ")}};var La=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 s=new URL(e.url);this.name=`api-${s.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},s=new AbortController,i=setTimeout(()=>s.abort(),this.options.timeout||3e4),u=await fetch(this.config.url,{method:"POST",headers:n,body:JSON.stringify(r),signal:s.signal});if(clearTimeout(i),!u.ok)throw new Error(`API tokenizer request failed: ${u.status} ${u.statusText}`);let c=await u.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(s=>{s&&r.push(s)}))}),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("."),s=e;for(let i of n){if(s==null)throw new Error(`Field path '${r}' not found in response`);s=s[i]}if(typeof s!="number")throw new Error(`Expected number at field path '${r}', got ${typeof s}`);return s}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 as=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 Qs("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 Qs(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),n=new Ma(e.model,this.logger,{timeout:this.options.timeout});break;case"api":n=new La(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(s){return this.logger?.error(`Failed to initialize ${e.type} tokenizer: ${s.message}`),this.logger?.error(`Error stack: ${s.stack}`),this.fallbackTokenizer||await this.initialize(),this.tokenizers.set(r,this.fallbackTokenizer),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 s=(this.configService.get("providers")||[]).find(i=>i.name===e);if(s?.tokenizer)return s.tokenizer.models?.[r]?s.tokenizer.models[r]:s.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 aT}from"tiktoken";import{readFile as dd}from"fs/promises";import{opendir as uT,stat as cT}from"fs/promises";import{join as fd}from"path";import{CLAUDE_PROJECTS_DIR as Ty,HOME_DIR as vy}from"@thxp/shared";import{LRUCache as lT}from"lru-cache";var Ar=aT("cl100k_base"),Ry=(t,e,r)=>{let n=0;return Array.isArray(t)&&t.forEach(s=>{typeof s.content=="string"?n+=Ar.encode(s.content).length:Array.isArray(s.content)&&s.content.forEach(i=>{i.type==="text"?n+=Ar.encode(i.text).length:i.type==="tool_use"?n+=Ar.encode(JSON.stringify(i.input)).length:i.type==="tool_result"&&(n+=Ar.encode(typeof i.content=="string"?i.content:JSON.stringify(i.content)).length)})}),typeof e=="string"?n+=Ar.encode(e).length:Array.isArray(e)&&e.forEach(s=>{s.type==="text"&&(typeof s.text=="string"?n+=Ar.encode(s.text).length:Array.isArray(s.text)&&s.text.forEach(i=>{n+=Ar.encode(i||"").length}))}),r&&r.forEach(s=>{s.description&&(n+=Ar.encode(s.name+s.description).length),s.input_schema&&(n+=Ar.encode(JSON.stringify(s.input_schema)).length)}),n},dT=async(t,e)=>{if(t.sessionId){let r=await Oy(t.sessionId);if(r){let n=fd(vy,r,"config.json"),s=fd(vy,r,`${t.sessionId}.json`);try{let i=JSON.parse(await dd(s,"utf8"));if(i&&i.Router)return i.Router}catch{}try{let i=JSON.parse(await dd(n,"utf8"));if(i&&i.Router)return i.Router}catch{}}}},fT=async(t,e,r)=>{let n=await dT(t,r),s=r.get("providers")||[],i=n||r.get("Router");if(t.body.model.includes(",")){let[k,E]=t.body.model.split(","),g=s.find(p=>p.name.toLowerCase()===k),y=g?.models?.find(p=>p.toLowerCase()===E);return g&&y?{model:`${g.name},${y}`,scenarioType:"default"}:{model:t.body.model,scenarioType:"default"}}let u=(k,E)=>k.model_limits?.[E]!==void 0?Cs(k.name,E)>=k.model_limits[E]:!1,c=k=>{if(!k)return null;if(typeof k=="string"){let E="",g="";if(k.includes(","))[E,g]=k.split(",");else{g=k;let y=s.find(p=>p.models.includes(g));y&&(E=y.name)}if(E&&g){let y=s.find(p=>p.name.toLowerCase()===E.toLowerCase());if(y&&Tn(vn(E,g))||y&&u(y,g))return null}return k}if(Array.isArray(k)){for(let E of k)if(typeof E=="string"&&E.trim())if(E.includes(",")){let[g,y]=E.split(","),p=s.find(D=>D.name.toLowerCase()===g.toLowerCase());if(p&&p.models.includes(y)&&!Tn(vn(g,y))&&!u(p,y))return E}else{let g=s.find(y=>y.models.includes(E));if(g&&!Tn(vn(g.name,E))&&!u(g,E))return E}}return null};if(t.body?.system?.length>1&&t.body?.system[1]?.text?.startsWith("<CCR-SUBAGENT-MODEL>")){let k=t.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);if(k)return t.body.system[1].text=t.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${k[1]}</CCR-SUBAGENT-MODEL>`,""),{model:k[1],scenarioType:"default"}}if(t.body.model?.includes("claude")&&t.body.model?.includes("haiku")&&i?.background){t.log.info(`Using background model for ${t.body.model}`);let k=c(i.background);if(k)return{model:k,scenarioType:"background"}}if((Array.isArray(t.body.tools)?t.body.tools:[]).some(k=>k.type&&k.type.includes("web_search"))&&i?.webSearch){let k=c(i.webSearch);if(k)return{model:k,scenarioType:"webSearch"}}if((t.body.thinking===!0||typeof t.body.thinking=="object"&&t.body.thinking!==null&&t.body.thinking.type==="enabled")&&i?.think){t.log.info(`Using think model for ${t.body.thinking}`);let k=c(i.think);if(k)return{model:k,scenarioType:"think"}}let m=c(i?.default);return m?{model:m,scenarioType:"default"}:{model:Array.isArray(i?.default)?i.default[0]:i?.default,scenarioType:"default"}},hd=async(t,e,r)=>{let{configService:n,event:s}=r;if(t.body&&Array.isArray(t.body.model)&&(t.body.model=t.body.model[0]),t.body.metadata?.user_id)try{let d=JSON.parse(t.body.metadata.user_id);d.session_id&&(t.sessionId=d.session_id)}catch{let d=t.body.metadata.user_id.split("_session_");d.length>1&&(t.sessionId=d[1])}let{messages:i,system:u=[],tools:c}=t.body,l=n.get("REWRITE_SYSTEM_PROMPT");if(l&&u.length>1&&u[1]?.text?.includes("<env>")){let d=await dd(l,"utf-8");u[1].text=`${d}<env>${u[1].text.split("<env>").pop()}`}try{let[d,h]=(t.body.model||"").split(","),m=r.tokenizerService?.getTokenizerConfigForModel(d,h),w;r.tokenizerService?w=(await r.tokenizerService.countTokens({messages:i,system:u,tools:c},m)).tokenCount:w=Ry(i,u,c);let k,E=n.get("CUSTOM_ROUTER_PATH");if(E)try{let g=Q(E);t.tokenCount=w,k=await g(t,n.getAll(),{event:s})}catch(g){t.log.error(`failed to load custom router: ${g.message}`)}if(k)t.scenarioType="default";else{let g=await fT(t,w,n);k=g.model,t.scenarioType=g.scenarioType}t.body.model=k,k&&k.includes(",")&&(t.provider=k.split(",")[0])}catch(d){t.log.error(`Error in router middleware: ${d.message}`);let m=n.get("Router")?.default;t.body.model=Array.isArray(m)?m[0]:m,t.scenarioType="default",!t.provider&&t.body.model&&t.body.model.includes(",")&&(t.provider=t.body.model.split(",")[0])}},ro=new lT({max:1e3}),Oy=async t=>{if(ro.has(t)){let e=ro.get(t);return!e||e===""?null:e}try{let e=await uT(Ty),r=[];for await(let i of e)i.isDirectory()&&r.push(i.name);let n=r.map(async i=>{let u=fd(Ty,i,`${t}.jsonl`);try{return(await cT(u)).isFile()?i:null}catch{return null}}),s=await Promise.all(n);for(let i of s)if(i)return ro.set(t,i),i;return ro.set(t,""),null}catch(e){return console.error("Error searching for project by session:",e),ro.set(t,""),null}};var pd=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),s=this.pluginInstances.get(e);if(!n||!s)throw new Error(`Plugin ${e} not found`);n.enabled&&await r.register(s.register,n.options)}async enablePlugins(e){for(let[r,n]of this.plugins)if(n.enabled)try{await this.enablePlugin(r,e)}catch(s){let i=s instanceof Error?s.message:String(s);e.log?.error(`Failed to enable plugin ${r}: ${i}`)}}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()}},xy=new pd;var qy=Fr(By(),1);var us=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,r)=>{this.buffer+=e;let n=this.buffer.split(`
|
|
342
342
|
`);this.buffer=n.pop()||"";for(let s of n){let i=this.processLine(s);i&&r.enqueue(i)}},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}
|
|
343
343
|
`),e.id&&(n+=`id: ${e.id}
|
|
344
344
|
`),e.retry&&(n+=`retry: ${e.retry}
|