@thxp/llms 3.2.21 → 3.2.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -69,7 +69,7 @@ https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys`};var Hl
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 oo,Cd=je(()=>{"use strict";oo=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 Ly}from"path";import{tmpdir as wT}from"os";var io,wd=je(()=>{"use strict";io=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=Ly(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 Ly(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 Ed,us,Uy=je(()=>{"use strict";bd();Cd();wd();Ed=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 so(e.config);case"webhook":return new oo(e.config);case"temp-file":return new io(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)}},us=new Ed});import ET from"fastify";import AT from"@fastify/cors";var qf=Fr(ku(),1);import{readFileSync as L_,existsSync as Lf}from"fs";import{join as Uf}from"path";import{config as U_}from"dotenv";var Tn=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:Uf(process.cwd(),this.options.jsonPath);if(Lf(e))try{let r=L_(e,"utf-8"),n=qf.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:Uf(process.cwd(),this.options.envPath);if(Lf(e))try{let r=U_({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 $f(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 q_}from"undici";function zf(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 q_(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 $_,writeFileSync as z_,readFileSync as H_}from"fs";import{join as W_}from"path";import{HOME_DIR as G_}from"@thxp/shared";var Du=W_(G_,"daily_usage.json"),rn=null;function J_(){let t=new Date;return`${t.getFullYear()}-${String(t.getMonth()+1).padStart(2,"0")}-${String(t.getDate()).padStart(2,"0")}`}function Hf(){let t=J_();if(rn&&rn.date===t)return rn;try{if($_(Du)){let e=JSON.parse(H_(Du,"utf-8"));if(e?.date===t)return rn=e,e}}catch{}return rn={date:t,models:{}},Wf(rn),rn}function Wf(t){try{z_(Du,JSON.stringify(t,null,2),"utf-8")}catch{}}function Tu(t,e,r){let n=Hf(),s=`${t.toLowerCase()},${e.toLowerCase()}`;n.models[s]=(n.models[s]||0)+r,Wf(n)}function bs(t,e){return Hf().models[`${t.toLowerCase()},${e.toLowerCase()}`]||0}var Gf="3.2.21";var qo=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 qo(100),Y_=300*1e3,vu=new qo(1e3),Jf=t=>{vu.put(t,Date.now())},Cs=t=>{let e=vu.get(t);return e===void 0?!1:Date.now()-e>Y_?(vu.del(t),!1):!0};var ws=(t,e)=>`${t},${e}`;var Ir=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,r)=>{this.buffer+=e;let n=this.buffer.split(`
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 oo,Cd=je(()=>{"use strict";oo=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 Ly}from"path";import{tmpdir as wT}from"os";var io,wd=je(()=>{"use strict";io=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=Ly(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 Ly(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 Ed,us,Uy=je(()=>{"use strict";bd();Cd();wd();Ed=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 so(e.config);case"webhook":return new oo(e.config);case"temp-file":return new io(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)}},us=new Ed});import ET from"fastify";import AT from"@fastify/cors";var qf=Fr(ku(),1);import{readFileSync as L_,existsSync as Lf}from"fs";import{join as Uf}from"path";import{config as U_}from"dotenv";var Tn=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:Uf(process.cwd(),this.options.jsonPath);if(Lf(e))try{let r=L_(e,"utf-8"),n=qf.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:Uf(process.cwd(),this.options.envPath);if(Lf(e))try{let r=U_({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 $f(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 q_}from"undici";function zf(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 q_(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 $_,writeFileSync as z_,readFileSync as H_}from"fs";import{join as W_}from"path";import{HOME_DIR as G_}from"@thxp/shared";var Du=W_(G_,"daily_usage.json"),rn=null;function J_(){let t=new Date;return`${t.getFullYear()}-${String(t.getMonth()+1).padStart(2,"0")}-${String(t.getDate()).padStart(2,"0")}`}function Hf(){let t=J_();if(rn&&rn.date===t)return rn;try{if($_(Du)){let e=JSON.parse(H_(Du,"utf-8"));if(e?.date===t)return rn=e,e}}catch{}return rn={date:t,models:{}},Wf(rn),rn}function Wf(t){try{z_(Du,JSON.stringify(t,null,2),"utf-8")}catch{}}function Tu(t,e,r){let n=Hf(),s=`${t.toLowerCase()},${e.toLowerCase()}`;n.models[s]=(n.models[s]||0)+r,Wf(n)}function bs(t,e){return Hf().models[`${t.toLowerCase()},${e.toLowerCase()}`]||0}var Gf="3.2.22";var qo=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 qo(100),Y_=300*1e3,vu=new qo(1e3),Jf=t=>{vu.put(t,Date.now())},Cs=t=>{let e=vu.get(t);return e===void 0?!1:Date.now()-e>Y_?(vu.del(t),!1):!0};var ws=(t,e)=>`${t},${e}`;var Ir=class extends TransformStream{buffer="";currentEvent={};constructor(){super({transform:(e,r)=>{this.buffer+=e;let n=this.buffer.split(`
73
73
  `);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 vn=class extends TransformStream{constructor(){super({transform:(e,r)=>{let n="";e.event&&(n+=`event: ${e.event}
74
74
  `),e.id&&(n+=`id: ${e.id}
75
75
  `),e.retry&&(n+=`retry: ${e.retry}
@@ -345,7 +345,7 @@ ${s.content}`),delete s.thinking)});let n=e.messages[e.messages.length-1];return
345
345
  error: ${r.message}
346
346
  stack: ${r.stack}`),!1}}async initialize(){try{await this.registerDefaultTransformersInternal(),await this.loadFromConfig()}catch(e){this.logger.error(`TransformerService init error: ${e.message}
347
347
  Stack: ${e.stack}`)}}async registerDefaultTransformersInternal(){try{Object.values(hy).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 Zs=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 Ma}from"path";import{homedir as oT}from"os";import{existsSync as fd,mkdirSync as iT}from"fs";import{promises as La}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,Cy=(()=>{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(Cy),py=".,!?\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|[${py}])]+`,` ?[^\\s${py}]+`]]),eo="\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E",Ex=new RegExp(`^[${eo}]+$`,"gu"),ld=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"),Na=(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=>wy(t.toLowerCase()),Ot=(...t)=>Array.prototype.concat.apply([],t),dd=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},wy=t=>t.replace(new RegExp("\\p{M}","gu"),""),my=(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)}},to=dk,fk=class extends to{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`
348
- `: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=>Ey(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=Na(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}},Ba=wk,Ek=class extends Ba{constructor(){super(...arguments),this.form="NFC"}},Ak=Ek,Sk=class extends Ba{constructor(){super(...arguments),this.form="NFD"}},kk=Sk,Dk=class extends Ba{constructor(){super(...arguments),this.form="NFKC"}},Tk=Dk,vk=class extends Ba{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 wy(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 Ey=Mk,Lk=class extends to{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=Cy,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=Na(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(`[^${eo}]+|[${eo}]+`,"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${eo}]+|[${eo}]`,"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=Na(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=>Ay(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 Ay=iD,aD=class extends to{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)}},ja=aD,uD=class extends ja{constructor(t){super(t),this.max_input_chars_per_word=100,this.tokens_to_ids=dd(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}},gy=uD,yy=class Sy{constructor(e,r){this.is_leaf=e,this.children=r}static default(){return new Sy(!1,new Map)}},cD=class{constructor(){this.root=yy.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=yy.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,cd=class ky{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 ky(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 cd(this.bos_token_id??0,0,0,0,0),s=new cd(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 cd(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 ja{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],p=ak(l);t.insert(n,p,h,d),!s&&p===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}},_y=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 ja{constructor(t){super(t),this.tokens_to_ids=dd(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}},by=bD,CD=class extends ja{constructor(t,e){super(t);let r=t.vocab;this.tokens_to_ids=dd(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 gy(t);case"Unigram":return new _y(t,e.eos_token);case"BPE":return new by(t);default:if(t.vocab)return Array.isArray(t.vocab)?new _y(t,e.eos_token):Object.hasOwn(t,"continuing_subword_prefix")&&Object.hasOwn(t,"unk_token")?Object.hasOwn(t,"merges")?new by(t):new gy(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 to{constructor(t){super(),this.config=t}_call(t,...e){return this.post_process(t,...e)}},ro=SD,kD=class extends ro{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 ro{post_process(t,e=null){return{tokens:e?Ot(t,e):t}}},vD=TD,RD=class extends ro{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 ro{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 ro{constructor(t){super(t),this.processors=(t.processors??[]).map(e=>Dy(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 Dy=ND,BD=class extends to{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=ld(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=ld(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=>Ty(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=Na(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 Ty=nT,sT=class{constructor(t,e){let r=my(t,"Tokenizer",["model","decoder","post_processor","pre_tokenizer","normalizer"]);if(r)throw new Error(r);let n=my(e,"Config");if(n)throw new Error(n);this.tokenizer=t,this.config=e,this.normalizer=Ey(this.tokenizer.normalizer),this.pre_tokenizer=Ay(this.tokenizer.pre_tokenizer),this.model=AD(this.tokenizer.model,this.config),this.post_processor=Dy(this.tokenizer.post_processor),this.decoder=Ty(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=ld(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}},vy=sT;var Ua=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||Ma(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=Ma(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:Ma(e,"tokenizer.json"),tokenizerConfig:Ma(e,"tokenizer_config.json")}}ensureDir(e){fd(e)||iT(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!fd(e.tokenizerJson)||!fd(e.tokenizerConfig))return null;let[r,n]=await Promise.all([La.readFile(e.tokenizerJson,"utf-8"),La.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([La.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),La.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 vy(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 qa=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 Zs("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 Zs(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),n=new Ua(e.model,this.logger,{timeout:this.options.timeout});break;case"api":n=new qa(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 hd}from"fs/promises";import{opendir as uT,stat as cT}from"fs/promises";import{join as pd}from"path";import{CLAUDE_PROJECTS_DIR as Ry,HOME_DIR as Oy}from"@thxp/shared";import{LRUCache as lT}from"lru-cache";var Ar=aT("cl100k_base"),xy=(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 Fy(t.sessionId);if(r){let n=pd(Oy,r,"config.json"),s=pd(Oy,r,`${t.sessionId}.json`);try{let i=JSON.parse(await hd(s,"utf8"));if(i&&i.Router)return i.Router}catch{}try{let i=JSON.parse(await hd(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"),u=(w,g)=>{typeof g=="string"&&!g.includes(",")&&t.log?.warn(`Router.${w} uses bare model name "${g}" without provider prefix. Consider using "ProviderName,${g}" format for deterministic routing.`),Array.isArray(g)&&g.forEach(y=>{typeof y=="string"&&!y.includes(",")&&t.log?.warn(`Router.${w} contains bare model name "${y}" without provider prefix.`)})};if(i&&["default","background","think","webSearch","image"].forEach(w=>{i[w]&&u(w,i[w])}),t.body.model.includes(",")){let[w,g]=t.body.model.split(","),y=s.find(k=>k.name.toLowerCase()===w),m=y?.models?.find(k=>k.toLowerCase()===g);return y&&m?{model:`${y.name},${m}`,scenarioType:"default"}:{model:t.body.model,scenarioType:"default"}}let c=(w,g)=>w.model_limits?.[g]!==void 0?bs(w.name,g)>=w.model_limits[g]:!1,l=w=>{if(!w)return null;if(typeof w=="string"){let g="",y="";if(w.includes(",")?[g,y]=w.split(","):y=w,g&&y){let m=s.find(k=>k.name.toLowerCase()===g.toLowerCase());if(m&&Cs(ws(g,y))||m&&c(m,y))return null}return w}if(Array.isArray(w)){for(let g of w)if(typeof g=="string"&&g.trim())if(g.includes(",")){let[y,m]=g.split(","),k=s.find(_=>_.name.toLowerCase()===y.toLowerCase());if(k&&k.models.includes(m)&&!Cs(ws(y,m))&&!c(k,m))return g}else continue}return null};if(t.body?.system?.length>1&&t.body?.system[1]?.text?.startsWith("<CCR-SUBAGENT-MODEL>")){let w=t.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);if(w)return t.body.system[1].text=t.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${w[1]}</CCR-SUBAGENT-MODEL>`,""),{model:w[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 w=l(i.background);if(w)return{model:w,scenarioType:"background"}}if((Array.isArray(t.body.tools)?t.body.tools:[]).some(w=>w.type&&w.type.includes("web_search"))&&i?.webSearch){let w=l(i.webSearch);if(w)return{model:w,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 w=l(i.think);if(w)return{model:w,scenarioType:"think"}}let b=l(i?.default);return b?{model:b,scenarioType:"default"}:{model:Array.isArray(i?.default)?i.default[0]:i?.default,scenarioType:"default"}},md=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 hd(l,"utf-8");u[1].text=`${d}<env>${u[1].text.split("<env>").pop()}`}try{let[d,h]=(t.body.model||"").split(","),p=r.tokenizerService?.getTokenizerConfigForModel(d,h),b;r.tokenizerService?b=(await r.tokenizerService.countTokens({messages:i,system:u,tools:c},p)).tokenCount:b=xy(i,u,c);let D,w=n.get("CUSTOM_ROUTER_PATH");if(w)try{let g=Q(w);t.tokenCount=b,D=await g(t,n.getAll(),{event:s})}catch(g){t.log.error(`failed to load custom router: ${g.message}`)}if(D)t.scenarioType="default";else{let g=await fT(t,b,n);D=g.model,t.scenarioType=g.scenarioType}t.body.model=D,D&&D.includes(",")&&(t.provider=D.split(",")[0])}catch(d){t.log.error(`Error in router middleware: ${d.message}`);let p=n.get("Router")?.default;t.body.model=Array.isArray(p)?p[0]:p,t.scenarioType="default",!t.provider&&t.body.model&&t.body.model.includes(",")&&(t.provider=t.body.model.split(",")[0])}},no=new lT({max:1e3}),Fy=async t=>{if(no.has(t)){let e=no.get(t);return!e||e===""?null:e}try{let e=await uT(Ry),r=[];for await(let i of e)i.isDirectory()&&r.push(i.name);let n=r.map(async i=>{let u=pd(Ry,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 no.set(t,i),i;return no.set(t,""),null}catch(e){return console.error("Error searching for project by session:",e),no.set(t,""),null}};var gd=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()}},Py=new gd;var $y=Fr(My(),1);bd();Cd();wd();Uy();var ao=new Map,Ad=new Map,zy={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:(0,$y.default)(async(t,e)=>{let r={reporter:["console","temp-file"],...e},n=Array.isArray(r.reporter)?r.reporter:[r.reporter];if(r.outputHandlers&&r.outputHandlers.length>0)us.registerHandlers(r.outputHandlers);else{let i=[];for(let u of n)u==="console"?i.push({type:"console",enabled:!0,config:{colors:!0,level:"log"}}):u==="temp-file"?i.push({type:"temp-file",enabled:!0,config:{subdirectory:"claude-code-router",extension:"json",includeTimestamp:!0,prefix:"session"}}):u==="webhook"&&console.warn("[TokenSpeedPlugin] Webhook reporter requires explicit configuration in outputHandlers");i.length>0&&us.registerHandlers(i)}r.outputOptions&&us.setDefaultOptions(r.outputOptions);let s=async i=>{let u=t.tokenizerService;if(!u)return t.log?.warn("TokenizerService not available"),null;if(!i.provider||!i.model)return null;let c=i.provider,l=i.model,d=`${c}:${l}`;if(Ad.has(d))return Ad.get(d);let h=u.getTokenizerConfigForModel(c,l);if(!h)return t.log?.debug(`No tokenizer config for ${c}:${l}, using fallback`),null;try{let p=await u.getTokenizer(h);return Ad.set(d,p),t.log?.info(`Created tokenizer for ${c}:${l} - ${p.name}`),p}catch(p){return t.log?.warn(`Failed to create tokenizer for ${c}:${l}: ${p.message}`),null}};t.addHook("onRequest",async i=>{new URL(`http://127.0.0.1${i.url}`).pathname.endsWith("/v1/messages")&&(i.requestStartTime=performance.now())}),t.addHook("onSend",async(i,u,c)=>{let l=i.requestStartTime;if(!l)return;let d=i.id||Date.now().toString(),h;try{let w=i.body?.metadata?.user_id;if(w&&typeof w=="string"){let g=w.match(/_session_([a-f0-9-]+)/i);h=g?g[1]:void 0}}catch{}if(!h)return;let p=await s(i);if(c instanceof ReadableStream){ao.set(d,{requestId:d,sessionId:h,startTime:l,lastTokenTime:l,tokenCount:0,tokensPerSecond:0,tokenTimestamps:[],stream:!0});let[w,g]=c.tee();return(async()=>{let m=null,k=async _=>{let A=ao.get(d);if(!A)return;let v=performance.now();if(_){let x=(A.lastTokenTime-A.startTime)/1e3;x>0&&(A.tokensPerSecond=Math.round(A.tokenCount/x))}else{let x=v-1e3;A.tokenTimestamps=A.tokenTimestamps.filter(q=>q>x),A.tokensPerSecond=A.tokenTimestamps.length}await qy(A,n,r.outputOptions,_).catch(x=>{t.log?.warn(`Failed to output streaming stats: ${x.message}`)})};try{let A=g.pipeThrough(new TextDecoderStream).pipeThrough(new Ir).getReader();for(m=setInterval(async()=>{ao.get(d)&&await k(!1)},1e3);;){let{done:v,value:x}=await A.read();if(v)break;let q=x,V=ao.get(d);if(!V)continue;let U=performance.now();if(!V.firstTokenTime&&(q.event==="content_block_start"||q.event==="content_block_delta"||q.event==="text_block"||q.event==="content_block")&&(V.firstTokenTime=U,V.timeToFirstToken=Math.round(U-V.startTime)),q.event==="content_block_delta"&&q.data?.delta){let X=q.data.delta.type,re="";if(X==="text_delta"?re=q.data.delta.text||"":X==="input_json_delta"?re=q.data.delta.partial_json||"":X==="thinking_delta"&&(re=q.data.delta.thinking||""),re){let se=p&&p.encodeText?p.encodeText(re).length:uo(re);V.tokenCount+=se,V.lastTokenTime=U;for(let de=0;de<se;de++)V.tokenTimestamps.push(U)}}q.event==="message_stop"&&(m&&(clearInterval(m),m=null),await k(!0),ao.delete(d))}}catch(_){m&&clearInterval(m),_.name!=="AbortError"&&_.code!=="ERR_STREAM_PREMATURE_CLOSE"&&t.log?.warn(`Error processing token stats: ${_.message}`)}})().catch(m=>{console.log(m),t.log?.warn(`Background stats processing failed: ${m.message}`)}),w}let b=performance.now(),D=0;if(c&&typeof c=="string")try{let w=JSON.parse(c);if(w.usage?.output_tokens)D=w.usage.output_tokens;else{let g=w.content||w.message?.content||"";if(p)Array.isArray(g)?D=g.reduce((y,m)=>{if(m.type==="text"){let k=m.text||"";return y+(p.encodeText?p.encodeText(k).length:uo(k))}return y},0):typeof g=="string"&&(D=p.encodeText?p.encodeText(g).length:uo(g));else{let y=Array.isArray(g)?g.map(m=>m.text).join(""):g;D=uo(y)}}}catch{}if(D>0){let w=(b-l)/1e3,g={requestId:d,sessionId:h,startTime:l,lastTokenTime:b,tokenCount:D,tokensPerSecond:w>0?Math.round(D/w):0,timeToFirstToken:Math.round(b-l),stream:!1,tokenTimestamps:[]};await qy(g,n,r.outputOptions,!0)}return c})})};function uo(t){let e=(t.match(/[\u4e00-\u9fa5]/g)||[]).length,r=t.length-e;return Math.ceil(e/1.5+r/4)}async function qy(t,e,r,n=!1){let s=n?"[Token Speed Final]":"[Token Speed]",i={requestId:t.requestId.substring(0,8),sessionId:t.sessionId,stream:t.stream,tokenCount:t.tokenCount,tokensPerSecond:t.tokensPerSecond,timeToFirstToken:t.timeToFirstToken?`${t.timeToFirstToken}ms`:"N/A",duration:`${((t.lastTokenTime-t.startTime)/1e3).toFixed(2)}s`,timestamp:Date.now()},u={prefix:s,metadata:{sessionId:t.sessionId},...r};for(let c of e)try{await us.outputToType(c,i,u)}catch(l){console.error(`[TokenSpeedPlugin] Failed to output to ${c}:`,l)}}function ST(t={}){let e=ET({bodyLimit:52428800,...t});return e.setErrorHandler($f),e.register(AT),e}var Sd=class{app;configService;providerService;transformerService;tokenizerService;constructor(e={}){let{initialConfig:r,...n}=e;this.app=ST({...n,logger:n.logger??!0}),this.configService=new Tn(e),this.transformerService=new is(this.configService,this.app.log),this.tokenizerService=new as(this.configService,this.app.log),this.transformerService.initialize().finally(()=>{this.providerService=new Rn(this.configService,this.transformerService,this.app.log)}),this.tokenizerService.initialize().catch(s=>{this.app.log.error(`Failed to initialize TokenizerService: ${s}`)})}async register(e,r){await this.app.register(e,r)}addHook(e,r){this.app.addHook(e,r)}async registerNamespace(e,r){if(!e)throw new Error("name is required");if(e==="/"){await this.app.register(async c=>{c.decorate("configService",this.configService),c.decorate("transformerService",this.transformerService),c.decorate("providerService",this.providerService),c.decorate("tokenizerService",this.tokenizerService),c.addHook("preHandler",async(l,d)=>{new URL(`http://127.0.0.1${l.url}`).pathname.endsWith("/v1/messages")&&await md(l,d,{configService:this.configService,tokenizerService:this.tokenizerService})}),c.addHook("preHandler",async(l,d)=>{if(new URL(`http://127.0.0.1${l.url}`).pathname.endsWith("/v1/messages")&&l.body)try{let p=l.body;if(!p||!p.model)return d.code(400).send({error:"Missing model in request body"});let b=p.model;if(Array.isArray(b)&&(b=b[0]),typeof b!="string")return d.code(400).send({error:"Invalid model type in request body"});let[D,...w]=b.split(",");p.model=w.join(","),l.provider=D,l.model=w;return}catch(p){return l.log.error({error:p},"Error in modelProviderMiddleware:"),d.code(500).send({error:"Internal server error"})}}),await Ru(c)});return}if(!r)throw new Error("options is required");let n=new Tn({initialConfig:{providers:r.Providers,Router:r.Router}}),s=new is(n,this.app.log);await s.initialize();let i=new Rn(n,s,this.app.log),u=new as(n,this.app.log);await u.initialize(),await this.app.register(async c=>{c.decorate("configService",n),c.decorate("transformerService",s),c.decorate("providerService",i),c.decorate("tokenizerService",u),c.addHook("preHandler",async(l,d)=>{new URL(`http://127.0.0.1${l.url}`).pathname.endsWith("/v1/messages")&&await md(l,d,{configService:n,tokenizerService:u})}),c.addHook("preHandler",async(l,d)=>{if(new URL(`http://127.0.0.1${l.url}`).pathname.endsWith("/v1/messages")&&l.body)try{let p=l.body;if(!p||!p.model)return d.code(400).send({error:"Missing model in request body"});let b=p.model;if(Array.isArray(b)&&(b=b[0]),typeof b!="string")return d.code(400).send({error:"Invalid model type in request body"});let[D,...w]=b.split(",");p.model=w.join(","),l.provider=D,l.model=w;return}catch(p){return l.log.error({error:p},"Error in modelProviderMiddleware:"),d.code(500).send({error:"Internal server error"})}}),await Ru(c)},{prefix:e})}async start(){try{this.app._server=this,this.app.addHook("preHandler",(n,s,i)=>{if(new URL(`http://127.0.0.1${n.url}`).pathname.endsWith("/v1/messages")&&n.body){let c=n.body;n.log.info({data:c,type:"request body"}),c.stream||(c.stream=!1)}i()}),await this.registerNamespace("/");let e=await this.app.listen({port:parseInt(this.configService.get("PORT")||"3000",10),host:this.configService.get("HOST")||"127.0.0.1"});this.app.log.info(`\u{1F680} LLMs API server listening on ${e}`);let r=async n=>{this.app.log.info(`Received ${n}, shutting down gracefully...`),await this.app.close(),process.exit(0)};process.on("SIGINT",()=>r("SIGINT")),process.on("SIGTERM",()=>r("SIGTERM"))}catch(e){this.app.log.error(`Error starting server: ${e}`),process.exit(1)}}},aI=Sd;export{Tn as ConfigService,Rn as ProviderService,Ir as SSEParserTransform,vn as SSESerializerTransform,as as TokenizerService,is as TransformerService,xy as calculateTokenCount,aI as default,bs as getModelUsage,Py as pluginManager,Tu as recordModelUsage,$o as rewriteStream,md as router,Fy as searchProjectBySession,K_ as sessionUsageCache,zy as tokenSpeedPlugin};
348
+ `: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=>Ey(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=Na(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}},Ba=wk,Ek=class extends Ba{constructor(){super(...arguments),this.form="NFC"}},Ak=Ek,Sk=class extends Ba{constructor(){super(...arguments),this.form="NFD"}},kk=Sk,Dk=class extends Ba{constructor(){super(...arguments),this.form="NFKC"}},Tk=Dk,vk=class extends Ba{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 wy(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 Ey=Mk,Lk=class extends to{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=Cy,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=Na(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(`[^${eo}]+|[${eo}]+`,"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${eo}]+|[${eo}]`,"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=Na(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=>Ay(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 Ay=iD,aD=class extends to{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)}},ja=aD,uD=class extends ja{constructor(t){super(t),this.max_input_chars_per_word=100,this.tokens_to_ids=dd(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}},gy=uD,yy=class Sy{constructor(e,r){this.is_leaf=e,this.children=r}static default(){return new Sy(!1,new Map)}},cD=class{constructor(){this.root=yy.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=yy.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,cd=class ky{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 ky(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 cd(this.bos_token_id??0,0,0,0,0),s=new cd(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 cd(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 ja{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],p=ak(l);t.insert(n,p,h,d),!s&&p===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}},_y=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 ja{constructor(t){super(t),this.tokens_to_ids=dd(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}},by=bD,CD=class extends ja{constructor(t,e){super(t);let r=t.vocab;this.tokens_to_ids=dd(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 gy(t);case"Unigram":return new _y(t,e.eos_token);case"BPE":return new by(t);default:if(t.vocab)return Array.isArray(t.vocab)?new _y(t,e.eos_token):Object.hasOwn(t,"continuing_subword_prefix")&&Object.hasOwn(t,"unk_token")?Object.hasOwn(t,"merges")?new by(t):new gy(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 to{constructor(t){super(),this.config=t}_call(t,...e){return this.post_process(t,...e)}},ro=SD,kD=class extends ro{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 ro{post_process(t,e=null){return{tokens:e?Ot(t,e):t}}},vD=TD,RD=class extends ro{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 ro{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 ro{constructor(t){super(t),this.processors=(t.processors??[]).map(e=>Dy(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 Dy=ND,BD=class extends to{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=ld(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=ld(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=>Ty(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=Na(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 Ty=nT,sT=class{constructor(t,e){let r=my(t,"Tokenizer",["model","decoder","post_processor","pre_tokenizer","normalizer"]);if(r)throw new Error(r);let n=my(e,"Config");if(n)throw new Error(n);this.tokenizer=t,this.config=e,this.normalizer=Ey(this.tokenizer.normalizer),this.pre_tokenizer=Ay(this.tokenizer.pre_tokenizer),this.model=AD(this.tokenizer.model,this.config),this.post_processor=Dy(this.tokenizer.post_processor),this.decoder=Ty(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=ld(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}},vy=sT;var Ua=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||Ma(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=Ma(this.cacheDir,this.safeModelName);return{modelDir:e,tokenizerJson:Ma(e,"tokenizer.json"),tokenizerConfig:Ma(e,"tokenizer_config.json")}}ensureDir(e){fd(e)||iT(e,{recursive:!0})}async loadFromCache(){try{let e=this.getCachePaths();if(!fd(e.tokenizerJson)||!fd(e.tokenizerConfig))return null;let[r,n]=await Promise.all([La.readFile(e.tokenizerJson,"utf-8"),La.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([La.writeFile(e.tokenizerJson,JSON.stringify(c,null,2)),La.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 vy(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 qa=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 Zs("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 Zs(e.encoding||"cl100k_base");break;case"huggingface":this.logger?.info(`Initializing HuggingFace tokenizer for model: ${e.model}`),n=new Ua(e.model,this.logger,{timeout:this.options.timeout});break;case"api":n=new qa(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 hd}from"fs/promises";import{opendir as uT,stat as cT}from"fs/promises";import{join as pd}from"path";import{CLAUDE_PROJECTS_DIR as Ry,HOME_DIR as Oy}from"@thxp/shared";import{LRUCache as lT}from"lru-cache";var Ar=aT("cl100k_base"),xy=(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 Fy(t.sessionId);if(r){let n=pd(Oy,r,"config.json"),s=pd(Oy,r,`${t.sessionId}.json`);try{let i=JSON.parse(await hd(s,"utf8"));if(i&&i.Router)return i.Router}catch{}try{let i=JSON.parse(await hd(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"),u=(w,g)=>{typeof g=="string"&&!g.includes(",")&&t.log?.warn(`Router.${w} uses bare model name "${g}" without provider prefix. Consider using "ProviderName,${g}" format for deterministic routing.`),Array.isArray(g)&&g.forEach(y=>{typeof y=="string"&&!y.includes(",")&&t.log?.warn(`Router.${w} contains bare model name "${y}" without provider prefix.`)})};if(i&&["default","background","think","webSearch","image"].forEach(w=>{i[w]&&u(w,i[w])}),t.body.model.includes(",")){let[w,g]=t.body.model.split(","),y=s.find(k=>k.name.toLowerCase()===w),m=y?.models?.find(k=>k.toLowerCase()===g);return y&&m?{model:`${y.name},${m}`,scenarioType:"default"}:{model:t.body.model,scenarioType:"default"}}let c=(w,g)=>w.model_limits?.[g]!==void 0?bs(w.name,g)>=w.model_limits[g]:!1,l=w=>{if(!w)return null;if(typeof w=="string"){let g="",y="";if(w.includes(",")?[g,y]=w.split(","):y=w,g&&y){let m=s.find(k=>k.name.toLowerCase()===g.toLowerCase());if(m&&Cs(ws(g,y))||m&&c(m,y))return null}return w}if(Array.isArray(w)){for(let g of w)if(typeof g=="string"&&g.trim())if(g.includes(",")){let[y,m]=g.split(","),k=s.find(_=>_.name.toLowerCase()===y.toLowerCase());if(k&&k.models.includes(m)&&!Cs(ws(y,m))&&!c(k,m))return g}else continue}return null};if(t.body?.system?.length>1&&t.body?.system[1]?.text?.startsWith("<CCR-SUBAGENT-MODEL>")){let w=t.body?.system[1].text.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);if(w)return t.body.system[1].text=t.body.system[1].text.replace(`<CCR-SUBAGENT-MODEL>${w[1]}</CCR-SUBAGENT-MODEL>`,""),{model:w[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 w=l(i.background);if(w)return{model:w,scenarioType:"background"}}if((Array.isArray(t.body.tools)?t.body.tools:[]).some(w=>w.type&&w.type.includes("web_search"))&&i?.webSearch){let w=l(i.webSearch);if(w)return{model:w,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 w=l(i.think);if(w)return{model:w,scenarioType:"think"}}let b=l(i?.default);return b?{model:b,scenarioType:"default"}:{model:Array.isArray(i?.default)?i.default[0]:i?.default,scenarioType:"default"}},md=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 hd(l,"utf-8");u[1].text=`${d}<env>${u[1].text.split("<env>").pop()}`}try{let[d,h]=(t.body.model||"").split(","),p=r.tokenizerService?.getTokenizerConfigForModel(d,h),b;r.tokenizerService?b=(await r.tokenizerService.countTokens({messages:i,system:u,tools:c},p)).tokenCount:b=xy(i,u,c);let D,w=n.get("CUSTOM_ROUTER_PATH");if(w)try{let g=Q(w);t.tokenCount=b,D=await g(t,n.getAll(),{event:s})}catch(g){t.log.error(`failed to load custom router: ${g.message}`)}if(D)t.scenarioType="default";else{let g=await fT(t,b,n);D=g.model,t.scenarioType=g.scenarioType}t.body.model=D,D&&D.includes(",")&&(t.provider=D.split(",")[0])}catch(d){t.log.error(`Error in router middleware: ${d.message}`);let p=n.get("Router")?.default;t.body.model=Array.isArray(p)?p[0]:p,t.scenarioType="default",!t.provider&&t.body.model&&t.body.model.includes(",")&&(t.provider=t.body.model.split(",")[0])}},no=new lT({max:1e3}),Fy=async t=>{if(no.has(t)){let e=no.get(t);return!e||e===""?null:e}try{let e=await uT(Ry),r=[];for await(let i of e)i.isDirectory()&&r.push(i.name);let n=r.map(async i=>{let u=pd(Ry,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 no.set(t,i),i;return no.set(t,""),null}catch(e){return console.error("Error searching for project by session:",e),no.set(t,""),null}};var gd=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()}},Py=new gd;var $y=Fr(My(),1);bd();Cd();wd();Uy();var ao=new Map,Ad=new Map,zy={name:"token-speed",version:"1.0.0",description:"Statistics for streaming response token generation speed",register:(0,$y.default)(async(t,e)=>{let r={reporter:["console","temp-file"],...e},n=Array.isArray(r.reporter)?r.reporter:[r.reporter];if(r.outputHandlers&&r.outputHandlers.length>0)us.registerHandlers(r.outputHandlers);else{let i=[];for(let u of n)u==="console"?i.push({type:"console",enabled:!0,config:{colors:!0,level:"log"}}):u==="temp-file"?i.push({type:"temp-file",enabled:!0,config:{subdirectory:"claude-code-router",extension:"json",includeTimestamp:!0,prefix:"session"}}):u==="webhook"&&console.warn("[TokenSpeedPlugin] Webhook reporter requires explicit configuration in outputHandlers");i.length>0&&us.registerHandlers(i)}r.outputOptions&&us.setDefaultOptions(r.outputOptions);let s=async i=>{let u=t.tokenizerService;if(!u)return t.log?.warn("TokenizerService not available"),null;if(!i.provider||!i.model)return null;let c=i.provider,l=i.model,d=`${c}:${l}`;if(Ad.has(d))return Ad.get(d);let h=u.getTokenizerConfigForModel(c,l);if(!h)return t.log?.debug(`No tokenizer config for ${c}:${l}, using fallback`),null;try{let p=await u.getTokenizer(h);return Ad.set(d,p),t.log?.info(`Created tokenizer for ${c}:${l} - ${p.name}`),p}catch(p){return t.log?.warn(`Failed to create tokenizer for ${c}:${l}: ${p.message}`),null}};t.addHook("onRequest",async i=>{new URL(`http://127.0.0.1${i.url}`).pathname.endsWith("/v1/messages")&&(i.requestStartTime=performance.now())}),t.addHook("onSend",async(i,u,c)=>{let l=i.requestStartTime;if(!l)return;let d=i.id||Date.now().toString(),h;try{let w=i.body?.metadata?.user_id;if(w&&typeof w=="string"){let g=w.match(/_session_([a-f0-9-]+)/i);h=g?g[1]:void 0}}catch{}if(!h)return;let p=await s(i);if(c instanceof ReadableStream){ao.set(d,{requestId:d,sessionId:h,startTime:l,lastTokenTime:l,tokenCount:0,tokensPerSecond:0,tokenTimestamps:[],stream:!0});let[w,g]=c.tee();return(async()=>{let m=null,k=async _=>{let A=ao.get(d);if(!A)return;let v=performance.now();if(_){let x=(A.lastTokenTime-A.startTime)/1e3;x>0&&(A.tokensPerSecond=Math.round(A.tokenCount/x))}else{let x=v-1e3;A.tokenTimestamps=A.tokenTimestamps.filter(q=>q>x),A.tokensPerSecond=A.tokenTimestamps.length}await qy(A,n,r.outputOptions,_).catch(x=>{t.log?.warn(`Failed to output streaming stats: ${x.message}`)})};try{let A=g.pipeThrough(new TextDecoderStream).pipeThrough(new Ir).getReader();for(m=setInterval(async()=>{ao.get(d)&&await k(!1)},1e3);;){let{done:v,value:x}=await A.read();if(v)break;let q=x,V=ao.get(d);if(!V)continue;let U=performance.now();if(!V.firstTokenTime&&(q.event==="content_block_start"||q.event==="content_block_delta"||q.event==="text_block"||q.event==="content_block")&&(V.firstTokenTime=U,V.timeToFirstToken=Math.round(U-V.startTime)),q.event==="content_block_delta"&&q.data?.delta){let X=q.data.delta.type,re="";if(X==="text_delta"?re=q.data.delta.text||"":X==="input_json_delta"?re=q.data.delta.partial_json||"":X==="thinking_delta"&&(re=q.data.delta.thinking||""),re){let se=p&&p.encodeText?p.encodeText(re).length:uo(re);V.tokenCount+=se,V.lastTokenTime=U;for(let de=0;de<se;de++)V.tokenTimestamps.push(U)}}q.event==="message_stop"&&(m&&(clearInterval(m),m=null),await k(!0),ao.delete(d))}}catch(_){m&&clearInterval(m),_.name!=="AbortError"&&_.code!=="ERR_STREAM_PREMATURE_CLOSE"&&t.log?.warn(`Error processing token stats: ${_.message}`)}})().catch(m=>{console.log(m),t.log?.warn(`Background stats processing failed: ${m.message}`)}),w}let b=performance.now(),D=0;if(c&&typeof c=="string")try{let w=JSON.parse(c);if(w.usage?.output_tokens)D=w.usage.output_tokens;else{let g=w.content||w.message?.content||"";if(p)Array.isArray(g)?D=g.reduce((y,m)=>{if(m.type==="text"){let k=m.text||"";return y+(p.encodeText?p.encodeText(k).length:uo(k))}return y},0):typeof g=="string"&&(D=p.encodeText?p.encodeText(g).length:uo(g));else{let y=Array.isArray(g)?g.map(m=>m.text).join(""):g;D=uo(y)}}}catch{}if(D>0){let w=(b-l)/1e3,g={requestId:d,sessionId:h,startTime:l,lastTokenTime:b,tokenCount:D,tokensPerSecond:w>0?Math.round(D/w):0,timeToFirstToken:Math.round(b-l),stream:!1,tokenTimestamps:[]};await qy(g,n,r.outputOptions,!0)}return c})})};function uo(t){let e=(t.match(/[\u4e00-\u9fa5]/g)||[]).length,r=t.length-e;return Math.ceil(e/1.5+r/4)}async function qy(t,e,r,n=!1){let s=n?"[Token Speed Final]":"[Token Speed]",i={requestId:t.requestId.substring(0,8),sessionId:t.sessionId,stream:t.stream,tokenCount:t.tokenCount,tokensPerSecond:t.tokensPerSecond,timeToFirstToken:t.timeToFirstToken?`${t.timeToFirstToken}ms`:"N/A",duration:`${((t.lastTokenTime-t.startTime)/1e3).toFixed(2)}s`,timestamp:Date.now()},u={prefix:s,metadata:{sessionId:t.sessionId},...r};for(let c of e)try{await us.outputToType(c,i,u)}catch(l){console.error(`[TokenSpeedPlugin] Failed to output to ${c}:`,l)}}function ST(t={}){let e=ET({bodyLimit:52428800,...t});return e.setErrorHandler($f),e.register(AT),e}var Sd=class{app;configService;providerService;transformerService;tokenizerService;constructor(e={}){let{initialConfig:r,...n}=e;this.app=ST({...n,logger:n.logger??!0}),this.configService=new Tn(e),this.transformerService=new is(this.configService,this.app.log),this.tokenizerService=new as(this.configService,this.app.log),this.transformerService.initialize().finally(()=>{this.providerService=new Rn(this.configService,this.transformerService,this.app.log)}),this.tokenizerService.initialize().catch(s=>{this.app.log.error(`Failed to initialize TokenizerService: ${s}`)})}async register(e,r){await this.app.register(e,r)}addHook(e,r){this.app.addHook(e,r)}async registerNamespace(e,r){if(!e)throw new Error("name is required");if(e==="/"){await this.app.register(async c=>{c.decorate("configService",this.configService),c.decorate("transformerService",this.transformerService),c.decorate("providerService",this.providerService),c.decorate("tokenizerService",this.tokenizerService),c.addHook("preHandler",async(l,d)=>{new URL(`http://127.0.0.1${l.url}`).pathname.endsWith("/v1/messages")&&await md(l,d,{configService:this.configService,tokenizerService:this.tokenizerService})}),c.addHook("preHandler",async(l,d)=>{if(new URL(`http://127.0.0.1${l.url}`).pathname.endsWith("/v1/messages")&&l.body)try{let p=l.body;if(!p||!p.model)return d.code(400).send({error:"Missing model in request body"});let b=p.model;if(Array.isArray(b)&&(b=b[0]),typeof b!="string")return d.code(400).send({error:"Invalid model type in request body"});let[D,...w]=b.split(",");p.model=w.join(","),l.provider=D,l.model=w;return}catch(p){return l.log.error({error:p},"Error in modelProviderMiddleware:"),d.code(500).send({error:"Internal server error"})}}),await Ru(c)});return}if(!r)throw new Error("options is required");let n=new Tn({initialConfig:{providers:r.Providers,Router:r.Router}}),s=new is(n,this.app.log);await s.initialize();let i=new Rn(n,s,this.app.log),u=new as(n,this.app.log);await u.initialize(),await this.app.register(async c=>{c.decorate("configService",n),c.decorate("transformerService",s),c.decorate("providerService",i),c.decorate("tokenizerService",u),c.addHook("preHandler",async(l,d)=>{new URL(`http://127.0.0.1${l.url}`).pathname.endsWith("/v1/messages")&&await md(l,d,{configService:n,tokenizerService:u})}),c.addHook("preHandler",async(l,d)=>{if(new URL(`http://127.0.0.1${l.url}`).pathname.endsWith("/v1/messages")&&l.body)try{let p=l.body;if(!p||!p.model)return d.code(400).send({error:"Missing model in request body"});let b=p.model;if(Array.isArray(b)&&(b=b[0]),typeof b!="string")return d.code(400).send({error:"Invalid model type in request body"});let[D,...w]=b.split(",");p.model=w.join(","),l.provider=D,l.model=w;return}catch(p){return l.log.error({error:p},"Error in modelProviderMiddleware:"),d.code(500).send({error:"Internal server error"})}}),await Ru(c)},{prefix:e})}async start(){try{this.app._server=this,this.app.addHook("preHandler",(n,s,i)=>{if(new URL(`http://127.0.0.1${n.url}`).pathname.endsWith("/v1/messages")&&n.body){let c=n.body;n.log.info({type:"request body",data:{model:c.model,messageCount:c.messages?.length,maxTokens:c.max_tokens,stream:c.stream}}),c.stream||(c.stream=!1)}i()}),await this.registerNamespace("/");let e=await this.app.listen({port:parseInt(this.configService.get("PORT")||"3000",10),host:this.configService.get("HOST")||"127.0.0.1"});this.app.log.info(`\u{1F680} LLMs API server listening on ${e}`);let r=async n=>{this.app.log.info(`Received ${n}, shutting down gracefully...`),await this.app.close(),process.exit(0)};process.on("SIGINT",()=>r("SIGINT")),process.on("SIGTERM",()=>r("SIGTERM"))}catch(e){this.app.log.error(`Error starting server: ${e}`),process.exit(1)}}},aI=Sd;export{Tn as ConfigService,Rn as ProviderService,Ir as SSEParserTransform,vn as SSESerializerTransform,as as TokenizerService,is as TransformerService,xy as calculateTokenCount,aI as default,bs as getModelUsage,Py as pluginManager,Tu as recordModelUsage,$o as rewriteStream,md as router,Fy as searchProjectBySession,K_ as sessionUsageCache,zy as tokenSpeedPlugin};
349
349
  /*! Bundled license information:
350
350
 
351
351
  web-streams-polyfill/dist/ponyfill.es2018.js: