@kunal_dutt/token-lens 0.2.26 → 0.2.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/gateway.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
2
- import{readFileSync as gs}from"node:fs";import{createHash as ds}from"node:crypto";var ye=()=>({requests:0,errors:0,replayed:0,inputTokens:0,inputTokensBefore:0,outputTokens:0,outputTokensAvoided:0,cachedInputTokens:0,reasoningTokens:0,savedInputTokens:0,costUsd:0,costBeforeUsd:0,pricedRequests:0,checks:0,checksMatched:0,checkCostUsd:0});function ct(a,e){let n=a.pricing,i=n?.longContext,r=a.priceTier??(i?{aboveInputTokens:i.aboveTokens,inputPerMTok:i.inputPerMTok,outputPerMTok:i.outputPerMTok}:void 0);if(!n||!r||e<=r.aboveInputTokens)return n;let t=r.inputPerMTok/n.inputPerMTok;return{inputPerMTok:r.inputPerMTok,outputPerMTok:r.outputPerMTok,cachedInputPerMTok:n.cachedInputPerMTok!==void 0?n.cachedInputPerMTok*t:void 0}}function ua(a,e){let n=ct(a,e.inputTokens??0);if(!n)return 0;let i=e.cachedInputTokens??0,r=e.cacheWriteTokens??0,t=Math.max(0,(e.inputTokens??0)-i-r),s=a.family==="anthropic"?n.inputPerMTok*1.25:n.inputPerMTok;return(t*n.inputPerMTok+i*(n.cachedInputPerMTok??n.inputPerMTok)+r*s+(e.outputTokens??0)*n.outputPerMTok)/1e6}function gn(a,e,n){let i=ct(a,(e.inputTokens??0)+n);return i?(((e.inputTokens??0)+n)*i.inputPerMTok+(e.outputTokens??0)*i.outputPerMTok)/1e6:0}function Na(a,e){a.requests++,e.status>=400&&a.errors++,e.profile.pricing&&a.pricedRequests++;let n={...e.usage,inputTokens:e.usage.inputTokens??e.estimatedInput};if(e.replayed){a.replayed++,a.inputTokensBefore+=n.inputTokens,a.savedInputTokens+=n.inputTokens,a.outputTokensAvoided+=n.outputTokens??0,a.costBeforeUsd+=gn(e.profile,n,0);return}a.inputTokens+=n.inputTokens,a.inputTokensBefore+=n.inputTokens+e.savedInput,a.outputTokens+=n.outputTokens??0,a.cachedInputTokens+=n.cachedInputTokens??0,a.reasoningTokens+=n.reasoningTokens??0,a.savedInputTokens+=e.savedInput,a.costUsd+=ua(e.profile,n)*(e.priceFactor??1),a.costBeforeUsd+=gn(e.profile,n,e.savedInput)}var us=["app","agent","model"],hn=class{rows=new Map;started=new Date().toISOString();row(e,n,i){let r=[e,n,i].join("\0"),t=this.rows.get(r);return t||this.rows.set(r,t=ye()),t}record(e){Na(this.row(e.app,e.agent,e.profile.id),e)}recordCheck(e,n,i,r,t,s=!0){let o=this.row(e,n,i.id);s&&o.checks++,s&&r&&o.checksMatched++,o.checkCostUsd+=t}snapshot(){return new Map([...this.rows].map(([e,n])=>[e,{...n}]))}grouped(e,n=this.rows){let i=new Map;for(let[r,t]of n){let s=r.split("\0"),o=e.map(c=>s[us.indexOf(c)]).join("\0"),l=i.get(o)??ye();for(let c of Object.keys(l))l[c]+=t[c];i.set(o,l)}return[...i].map(([r,t])=>({...Object.fromEntries(e.map((s,o)=>[s,r.split("\0")[o]])),..._a(t)}))}detail(e=this.rows){return this.grouped(["app","agent","model"],e)}json(e=this.rows,n=this.started){let[i]=this.grouped([],e);return{since:n,total:i??_a(ye()),byTeam:this.grouped(["app"],e),byAgent:this.grouped(["app","agent"],e),byModel:this.grouped(["model"],e),byAppAndModel:this.grouped(["app","model"],e)}}prometheus(e){let n=[["requests","token_lens_requests_total","Requests proxied"],["errors","token_lens_errors_total","Requests that ended in an error status"],["replayed","token_lens_replayed_total","Identical requests answered from the gateway's copy"],["inputTokens","token_lens_input_tokens_total","Input tokens sent upstream"],["inputTokensBefore","token_lens_input_tokens_before_total","Input tokens the same calls would have sent without Token Lens"],["outputTokens","token_lens_output_tokens_total","Output tokens generated"],["cachedInputTokens","token_lens_cached_input_tokens_total","Input tokens served from the provider's prompt cache"],["reasoningTokens","token_lens_reasoning_tokens_total","Hidden reasoning tokens"],["savedInputTokens","token_lens_saved_input_tokens_total","Input tokens removed by Token Lens"],["costUsd","token_lens_cost_usd_total","Spend after Token Lens, for models with pricing"],["costBeforeUsd","token_lens_cost_before_usd_total","Spend the same calls would have cost without Token Lens"],["checks","token_lens_output_checks_total","Output checks run (optimized vs original answer)"],["checksMatched","token_lens_output_checks_matched_total","Output checks where the answers matched"],["checkCostUsd","token_lens_output_check_cost_usd_total","Spend on output checks"]],i=t=>t.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\n/g,"\\n"),r=[];for(let[t,s,o]of n){r.push(`# HELP ${s} ${o}`,`# TYPE ${s} counter`);for(let[l,c]of this.rows){let[u,g,f]=l.split("\0");e!==void 0&&u!==e||r.push(`${s}{app="${i(u)}",agent="${i(g)}",model="${i(f)}"} ${c[t]}`)}}return r.join(`
2
+ import{readFileSync as gs}from"node:fs";import{createHash as ds}from"node:crypto";var ye=()=>({requests:0,errors:0,replayed:0,inputTokens:0,inputTokensBefore:0,outputTokens:0,outputTokensAvoided:0,cachedInputTokens:0,reasoningTokens:0,savedInputTokens:0,costUsd:0,costBeforeUsd:0,pricedRequests:0,checks:0,checksMatched:0,checkCostUsd:0});function ct(a,e){let n=a.pricing,i=n?.longContext,r=a.priceTier??(i?{aboveInputTokens:i.aboveTokens,inputPerMTok:i.inputPerMTok,outputPerMTok:i.outputPerMTok}:void 0);if(!n||!r||e<=r.aboveInputTokens)return n;let t=r.inputPerMTok/n.inputPerMTok;return{inputPerMTok:r.inputPerMTok,outputPerMTok:r.outputPerMTok,cachedInputPerMTok:n.cachedInputPerMTok!==void 0?n.cachedInputPerMTok*t:void 0}}function ua(a,e){let n=ct(a,e.inputTokens??0);if(!n)return 0;let i=e.cachedInputTokens??0,r=e.cacheWriteTokens??0,t=Math.max(0,(e.inputTokens??0)-i-r),s=a.family==="anthropic"?n.inputPerMTok*1.25:n.inputPerMTok;return(t*n.inputPerMTok+i*(n.cachedInputPerMTok??n.inputPerMTok)+r*s+(e.outputTokens??0)*n.outputPerMTok)/1e6}function gn(a,e,n){let i=ct(a,(e.inputTokens??0)+n);return i?(((e.inputTokens??0)+n)*i.inputPerMTok+(e.outputTokens??0)*i.outputPerMTok)/1e6:0}function Na(a,e){a.requests++,e.status>=400&&a.errors++,e.profile.pricing&&a.pricedRequests++;let n={...e.usage,inputTokens:e.usage.inputTokens??e.estimatedInput};if(e.replayed){a.replayed++,a.inputTokensBefore+=n.inputTokens,a.savedInputTokens+=n.inputTokens,a.outputTokensAvoided+=n.outputTokens??0,a.costBeforeUsd+=gn(e.profile,n,0);return}a.inputTokens+=n.inputTokens,a.inputTokensBefore+=n.inputTokens+e.savedInput,a.outputTokens+=n.outputTokens??0,a.cachedInputTokens+=n.cachedInputTokens??0,a.reasoningTokens+=n.reasoningTokens??0,a.savedInputTokens+=e.savedInput,a.costUsd+=ua(e.profile,n)*(e.priceFactor??1),a.costBeforeUsd+=gn(e.profile,n,e.savedInput)}var us=["app","agent","model"],hn=class{rows=new Map;started=new Date().toISOString();row(e,n,i){let r=[e,n,i].join("\0"),t=this.rows.get(r);return t||this.rows.set(r,t=ye()),t}record(e){Na(this.row(e.app,e.agent,e.profile.id),e)}recordCheck(e,n,i,r,t,s=!0){let o=this.row(e,n,i.id);s&&o.checks++,s&&r&&o.checksMatched++,o.checkCostUsd+=t}snapshot(){return new Map([...this.rows].map(([e,n])=>[e,{...n}]))}grouped(e,n=this.rows){let i=new Map;for(let[r,t]of n){let s=r.split("\0"),o=e.map(c=>s[us.indexOf(c)]).join("\0"),l=i.get(o)??ye();for(let c of Object.keys(l))l[c]+=t[c];i.set(o,l)}return[...i].map(([r,t])=>({...Object.fromEntries(e.map((s,o)=>[s,r.split("\0")[o]])),...Ea(t)}))}detail(e=this.rows){return this.grouped(["app","agent","model"],e)}json(e=this.rows,n=this.started){let[i]=this.grouped([],e);return{since:n,total:i??Ea(ye()),byTeam:this.grouped(["app"],e),byAgent:this.grouped(["app","agent"],e),byModel:this.grouped(["model"],e),byAppAndModel:this.grouped(["app","model"],e)}}prometheus(e){let n=[["requests","token_lens_requests_total","Requests proxied"],["errors","token_lens_errors_total","Requests that ended in an error status"],["replayed","token_lens_replayed_total","Identical requests answered from the gateway's copy"],["inputTokens","token_lens_input_tokens_total","Input tokens sent upstream"],["inputTokensBefore","token_lens_input_tokens_before_total","Input tokens the same calls would have sent without Token Lens"],["outputTokens","token_lens_output_tokens_total","Output tokens generated"],["cachedInputTokens","token_lens_cached_input_tokens_total","Input tokens served from the provider's prompt cache"],["reasoningTokens","token_lens_reasoning_tokens_total","Hidden reasoning tokens"],["savedInputTokens","token_lens_saved_input_tokens_total","Input tokens removed by Token Lens"],["costUsd","token_lens_cost_usd_total","Spend after Token Lens, for models with pricing"],["costBeforeUsd","token_lens_cost_before_usd_total","Spend the same calls would have cost without Token Lens"],["checks","token_lens_output_checks_total","Output checks run (optimized vs original answer)"],["checksMatched","token_lens_output_checks_matched_total","Output checks where the answers matched"],["checkCostUsd","token_lens_output_check_cost_usd_total","Spend on output checks"]],i=t=>t.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\n/g,"\\n"),r=[];for(let[t,s,o]of n){r.push(`# HELP ${s} ${o}`,`# TYPE ${s} counter`);for(let[l,c]of this.rows){let[u,g,f]=l.split("\0");e!==void 0&&u!==e||r.push(`${s}{app="${i(u)}",agent="${i(g)}",model="${i(f)}"} ${c[t]}`)}}return r.join(`
3
3
  `)+`
4
- `}},Ze=(a,e=6)=>Math.round(a*10**e)/10**e;function _a(a){let e=a.inputTokensBefore-a.inputTokens+a.outputTokensAvoided,n=a.costBeforeUsd-a.costUsd;return{...a,costUsd:Ze(a.costUsd),costBeforeUsd:Ze(a.costBeforeUsd),checkCostUsd:Ze(a.checkCostUsd),savedTokens:e,savedTokensPct:a.inputTokensBefore+a.outputTokens+a.outputTokensAvoided?Ze(e/(a.inputTokensBefore+a.outputTokens+a.outputTokensAvoided)*100,1):0,savedUsd:Ze(n),savedUsdPct:a.costBeforeUsd?Ze(n/a.costBeforeUsd*100,1):0,netSavedUsd:Ze(n-a.checkCostUsd),outputUnchangedPct:a.checks?Ze(a.checksMatched/a.checks*100,1):null,unpricedRequests:a.requests-a.pricedRequests}}var dt={fromBody:!1,hashIds:!1,maxCustomers:1e5,budget:{},plans:{},teamBudgets:{}};function ut(a,e){let n=e.toISOString();return a==="day"?`d${n.slice(0,10)}`:`m${n.slice(0,7)}`}var mt=a=>(a==="day"?2:32)*86400,pt=a=>`h:${ds("sha256").update(a).digest("hex").slice(0,16)}`;function fn(a){return typeof a!="string"?void 0:a.replace(/[\u0000-\u001f\u007f]/g,"").trim().slice(0,128)||void 0}function gt(a,e,n){let i=fn(a)??(n.fromBody?fn(e?.safety_identifier)??fn(e?.user)??fn(e?.metadata?.user_id):void 0);if(i)return n.hashIds?pt(i):i}var ht=(a,e)=>e.hashIds?pt(a):a,kn=[["dailyUsd","day","usd"],["monthlyUsd","month","usd"],["dailyTokens","day","tokens"],["monthlyTokens","month","tokens"]],ms={"daily-usd":"dailyUsd","monthly-usd":"monthlyUsd","daily-tokens":"dailyTokens","monthly-tokens":"monthlyTokens"},ps=a=>typeof a=="number"&&Number.isFinite(a)&&a>=0;function bn(a){let e={};for(let[n]of kn)ps(a?.[n])&&(e[n]=a[n]);return e}var Ea=class extends Error{};function si(a,e,n){if(e&&!a.plans[e])throw new Ea(`Unknown plan "${e}". Plans in this gateway's config: ${Object.keys(a.plans).join(", ")||"none"}.`);let i={};for(let[r,t]of Object.entries(ms)){let s=n(`x-token-lens-customer-${r}`);s!==void 0&&s!==""&&(i[t]=Number(s))}return{...bn(a.budget),...bn(e?a.plans[e]:void 0),...bn(i)}}function oi(a,e){return bn(a.teamBudgets[e]??a.teamBudgets["*"])}var li=a=>kn.some(([e])=>a[e]!==void 0),ri="",La=(...a)=>a.join("\0"),yn=class{constructor(e){this.maxCustomers=e}maxCustomers;rows=new Map;local=new Map;flushed=new Map;shared=new Map;periods="";prune(e){let n=ut("day",e),i=ut("month",e),r=n+i;if(r!==this.periods){this.periods=r;for(let t of[this.local,this.flushed,this.shared])for(let s of t.keys()){let o=s.slice(0,s.indexOf("\0"));o!==n&&o!==i&&t.delete(s)}}return[n,i]}record(e,n,i,r=new Date){let t=ye();if(Na(t,i),n){let o=La(e,n),l=this.rows.get(o);l?this.rows.delete(o):l={totals:ye(),firstSeen:r.toISOString(),lastSeen:""},l.lastSeen=r.toISOString();for(let c of Object.keys(t))l.totals[c]+=t[c];for(this.rows.set(o,l);this.rows.size>this.maxCustomers;)this.rows.delete(this.rows.keys().next().value)}if(i.status>=400||i.replayed)return;let s={usd:t.costUsd,tokens:t.inputTokens+t.outputTokens};if(!(!s.usd&&!s.tokens))for(let o of this.prune(r))for(let l of n?[n,ri]:[ri]){let c=La(o,e,l),u=this.local.get(c)??{usd:0,tokens:0};u.usd+=s.usd,u.tokens+=s.tokens,this.local.set(c,u)}}spent(e,n,i,r=new Date){let[t,s]=this.prune(r),o=La(i==="day"?t:s,e,n??ri),l=this.local.get(o),c=this.flushed.get(o),u=this.shared.get(o);return{usd:(u?.usd??0)+(l?.usd??0)-(c?.usd??0),tokens:(u?.tokens??0)+(l?.tokens??0)-(c?.tokens??0)}}over(e,n,i,r=new Date){let t=[["team",i.team,void 0]];n&&t.unshift(["customer",i.customer,n]);for(let[s,o,l]of t)for(let[c,u,g]of kn){let f=o[c];if(f===void 0)continue;let h=this.spent(e,l,u,r)[g];if(h>=f)return{who:s,limit:c,cap:f,spent:h}}}remaining(e,n,i,r=new Date){let t={},s=[[i.team,void 0]];n&&s.push([i.customer,n]);for(let[o,l]of s)for(let[c,u,g]of kn){let f=o[c];if(f===void 0)continue;let h=Math.max(0,f-this.spent(e,l,u,r)[g]);t[g]=Math.min(t[g]??1/0,h)}return t}snapshot(){return new Map([...this.rows].map(([e,n])=>[e,{...n.totals}]))}lastSeen(e,n){return this.rows.get(La(e,n))?.lastSeen}list(e,n=this.snapshot(),i=new Date){let r=[];for(let[t,s]of n){let[o,l]=t.split("\0");e!==void 0&&o!==e||r.push(this.view(o,l,s,i))}return r.sort((t,s)=>s.costBeforeUsd-t.costBeforeUsd)}view(e,n,i=ye(),r=new Date){let t=this.spent(e,n,"day",r),s=this.spent(e,n,"month",r),o=l=>Math.round(l*1e6)/1e6;return{team:e,customer:n,..._a(i),lastSeen:this.lastSeen(e,n),spentToday:{usd:o(t.usd),tokens:t.tokens},spentThisMonth:{usd:o(s.usd),tokens:s.tokens}}}takeSpendDeltas(e=new Date){this.prune(e);let n=[];for(let[i,r]of this.local){let t=this.flushed.get(i)??{usd:0,tokens:0},[s,...o]=i.split("\0"),l=o.join("\0");r.usd!==t.usd&&n.push({period:s,field:`${l}\0usd`,by:r.usd-t.usd}),r.tokens!==t.tokens&&n.push({period:s,field:`${l}\0tokens`,by:r.tokens-t.tokens}),this.flushed.set(i,{...r})}return n}setSharedSpend(e,n){for(let i of[...this.shared.keys()])i.startsWith(`${e}\0`)&&this.shared.delete(i);for(let[i,r]of Object.entries(n)){let t=i.split("\0"),s=t.pop();if(s!=="usd"&&s!=="tokens")continue;let o=La(e,...t),l=this.shared.get(o)??{usd:0,tokens:0};l[s]=Number(r),this.shared.set(o,l)}}currentPeriods(e=new Date){return this.prune(e)}};function ft(a){let e=["team","customer","requests","inputTokensBefore","inputTokens","outputTokens","savedTokens","savedTokensPct","costBeforeUsd","costUsd","savedUsd","savedUsdPct","spentTodayUsd","spentThisMonthUsd","lastSeen"],n=r=>{let t=r==null?"":String(r),s=/^[=+\-@]/.test(t)&&!/^-?\d/.test(t)?`'${t}`:t;return/[",\n]/.test(s)?`"${s.replace(/"/g,'""')}"`:s},i=[e.join(",")];for(let r of a)i.push([r.team,r.customer,r.requests,r.inputTokensBefore,r.inputTokens,r.outputTokens,r.savedTokens,r.savedTokensPct,r.costBeforeUsd,r.costUsd,r.savedUsd,r.savedUsdPct,r.spentToday.usd,r.spentThisMonth.usd,r.lastSeen].map(n).join(","));return i.join(`
4
+ `}},Ze=(a,e=6)=>Math.round(a*10**e)/10**e;function Ea(a){let e=a.inputTokensBefore-a.inputTokens+a.outputTokensAvoided,n=a.costBeforeUsd-a.costUsd;return{...a,costUsd:Ze(a.costUsd),costBeforeUsd:Ze(a.costBeforeUsd),checkCostUsd:Ze(a.checkCostUsd),savedTokens:e,savedTokensPct:a.inputTokensBefore+a.outputTokens+a.outputTokensAvoided?Ze(e/(a.inputTokensBefore+a.outputTokens+a.outputTokensAvoided)*100,1):0,savedUsd:Ze(n),savedUsdPct:a.costBeforeUsd?Ze(n/a.costBeforeUsd*100,1):0,netSavedUsd:Ze(n-a.checkCostUsd),outputUnchangedPct:a.checks?Ze(a.checksMatched/a.checks*100,1):null,unpricedRequests:a.requests-a.pricedRequests}}var dt={fromBody:!1,hashIds:!1,maxCustomers:1e5,budget:{},plans:{},teamBudgets:{}};function ut(a,e){let n=e.toISOString();return a==="day"?`d${n.slice(0,10)}`:`m${n.slice(0,7)}`}var mt=a=>(a==="day"?2:32)*86400,pt=a=>`h:${ds("sha256").update(a).digest("hex").slice(0,16)}`;function fn(a){return typeof a!="string"?void 0:a.replace(/[\u0000-\u001f\u007f]/g,"").trim().slice(0,128)||void 0}function gt(a,e,n){let i=fn(a)??(n.fromBody?fn(e?.safety_identifier)??fn(e?.user)??fn(e?.metadata?.user_id):void 0);if(i)return n.hashIds?pt(i):i}var ht=(a,e)=>e.hashIds?pt(a):a,kn=[["dailyUsd","day","usd"],["monthlyUsd","month","usd"],["dailyTokens","day","tokens"],["monthlyTokens","month","tokens"]],ms={"daily-usd":"dailyUsd","monthly-usd":"monthlyUsd","daily-tokens":"dailyTokens","monthly-tokens":"monthlyTokens"},ps=a=>typeof a=="number"&&Number.isFinite(a)&&a>=0;function bn(a){let e={};for(let[n]of kn)ps(a?.[n])&&(e[n]=a[n]);return e}var Aa=class extends Error{};function si(a,e,n){if(e&&!a.plans[e])throw new Aa(`Unknown plan "${e}". Plans in this gateway's config: ${Object.keys(a.plans).join(", ")||"none"}.`);let i={};for(let[r,t]of Object.entries(ms)){let s=n(`x-token-lens-customer-${r}`);s!==void 0&&s!==""&&(i[t]=Number(s))}return{...bn(a.budget),...bn(e?a.plans[e]:void 0),...bn(i)}}function oi(a,e){return bn(a.teamBudgets[e]??a.teamBudgets["*"])}var li=a=>kn.some(([e])=>a[e]!==void 0),ri="",La=(...a)=>a.join("\0"),yn=class{constructor(e){this.maxCustomers=e}maxCustomers;rows=new Map;local=new Map;flushed=new Map;shared=new Map;periods="";prune(e){let n=ut("day",e),i=ut("month",e),r=n+i;if(r!==this.periods){this.periods=r;for(let t of[this.local,this.flushed,this.shared])for(let s of t.keys()){let o=s.slice(0,s.indexOf("\0"));o!==n&&o!==i&&t.delete(s)}}return[n,i]}record(e,n,i,r=new Date){let t=ye();if(Na(t,i),n){let o=La(e,n),l=this.rows.get(o);l?this.rows.delete(o):l={totals:ye(),firstSeen:r.toISOString(),lastSeen:""},l.lastSeen=r.toISOString();for(let c of Object.keys(t))l.totals[c]+=t[c];for(this.rows.set(o,l);this.rows.size>this.maxCustomers;)this.rows.delete(this.rows.keys().next().value)}if(i.status>=400||i.replayed)return;let s={usd:t.costUsd,tokens:t.inputTokens+t.outputTokens};if(!(!s.usd&&!s.tokens))for(let o of this.prune(r))for(let l of n?[n,ri]:[ri]){let c=La(o,e,l),u=this.local.get(c)??{usd:0,tokens:0};u.usd+=s.usd,u.tokens+=s.tokens,this.local.set(c,u)}}spent(e,n,i,r=new Date){let[t,s]=this.prune(r),o=La(i==="day"?t:s,e,n??ri),l=this.local.get(o),c=this.flushed.get(o),u=this.shared.get(o);return{usd:(u?.usd??0)+(l?.usd??0)-(c?.usd??0),tokens:(u?.tokens??0)+(l?.tokens??0)-(c?.tokens??0)}}over(e,n,i,r=new Date){let t=[["team",i.team,void 0]];n&&t.unshift(["customer",i.customer,n]);for(let[s,o,l]of t)for(let[c,u,g]of kn){let f=o[c];if(f===void 0)continue;let h=this.spent(e,l,u,r)[g];if(h>=f)return{who:s,limit:c,cap:f,spent:h}}}remaining(e,n,i,r=new Date){let t={},s=[[i.team,void 0]];n&&s.push([i.customer,n]);for(let[o,l]of s)for(let[c,u,g]of kn){let f=o[c];if(f===void 0)continue;let h=Math.max(0,f-this.spent(e,l,u,r)[g]);t[g]=Math.min(t[g]??1/0,h)}return t}snapshot(){return new Map([...this.rows].map(([e,n])=>[e,{...n.totals}]))}lastSeen(e,n){return this.rows.get(La(e,n))?.lastSeen}list(e,n=this.snapshot(),i=new Date){let r=[];for(let[t,s]of n){let[o,l]=t.split("\0");e!==void 0&&o!==e||r.push(this.view(o,l,s,i))}return r.sort((t,s)=>s.costBeforeUsd-t.costBeforeUsd)}view(e,n,i=ye(),r=new Date){let t=this.spent(e,n,"day",r),s=this.spent(e,n,"month",r),o=l=>Math.round(l*1e6)/1e6;return{team:e,customer:n,...Ea(i),lastSeen:this.lastSeen(e,n),spentToday:{usd:o(t.usd),tokens:t.tokens},spentThisMonth:{usd:o(s.usd),tokens:s.tokens}}}takeSpendDeltas(e=new Date){this.prune(e);let n=[];for(let[i,r]of this.local){let t=this.flushed.get(i)??{usd:0,tokens:0},[s,...o]=i.split("\0"),l=o.join("\0");r.usd!==t.usd&&n.push({period:s,field:`${l}\0usd`,by:r.usd-t.usd}),r.tokens!==t.tokens&&n.push({period:s,field:`${l}\0tokens`,by:r.tokens-t.tokens}),this.flushed.set(i,{...r})}return n}setSharedSpend(e,n){for(let i of[...this.shared.keys()])i.startsWith(`${e}\0`)&&this.shared.delete(i);for(let[i,r]of Object.entries(n)){let t=i.split("\0"),s=t.pop();if(s!=="usd"&&s!=="tokens")continue;let o=La(e,...t),l=this.shared.get(o)??{usd:0,tokens:0};l[s]=Number(r),this.shared.set(o,l)}}currentPeriods(e=new Date){return this.prune(e)}};function ft(a){let e=["team","customer","requests","inputTokensBefore","inputTokens","outputTokens","savedTokens","savedTokensPct","costBeforeUsd","costUsd","savedUsd","savedUsdPct","spentTodayUsd","spentThisMonthUsd","lastSeen"],n=r=>{let t=r==null?"":String(r),s=/^[=+\-@]/.test(t)&&!/^-?\d/.test(t)?`'${t}`:t;return/[",\n]/.test(s)?`"${s.replace(/"/g,'""')}"`:s},i=[e.join(",")];for(let r of a)i.push([r.team,r.customer,r.requests,r.inputTokensBefore,r.inputTokens,r.outputTokens,r.savedTokens,r.savedTokensPct,r.costBeforeUsd,r.costUsd,r.savedUsd,r.savedUsdPct,r.spentToday.usd,r.spentThisMonth.usd,r.lastSeen].map(n).join(","));return i.join(`
5
5
  `)+`
6
- `}var da={port:8080,host:"0.0.0.0",mode:"optimize",level:"auto",safe:{prompt:!0,minifyJson:!0,collapseRepeats:!0,dedupeToolOutputs:!0,dedupePassages:!0,dedupeImages:!0,promptCaching:!0,dedupeRetries:!0,cacheEmbeddings:!0},optIn:{elideOldToolOutputs:!1,lazyTools:!1,trimToolFields:!1,compactJson:!1,parkToolOutputs:!1,flex:!1,warmCache:!1},verify:{sampleRate:.05,minSimilarity:.8,maxShareOfSavings:.2,freeChecks:5,probation:5},runBudget:{},batch:{flushSeconds:60,pollSeconds:60,maxRequests:1e3,discount:.5},flexDiscount:.5,embeddingsCacheMB:256,priceTiers:[],storage:{keyPrefix:"token-lens:",flushSeconds:10,runDays:7},retryWindowSeconds:120,blockOverContext:!1,upstreams:{},routes:[],models:[],clientKeys:[],customers:dt,isolateTeams:!1,maxBodyBytes:32*1024*1024,maxRuns:2e3,logRequests:!0,reportToolArgs:!1};function ci(a,e){if(typeof a=="string"&&a.startsWith("env:")){let n=a.slice(4);return e[n]??""}return Array.isArray(a)?a.map(n=>ci(n,e)):a&&typeof a=="object"?Object.fromEntries(Object.entries(a).map(([n,i])=>[n,ci(i,e)])):a}var bt=[{name:"openai",type:"openai",key:["OPENAI_API_KEY"],baseUrlEnv:"OPENAI_BASE_URL",baseUrl:"https://api.openai.com/v1",models:["gpt-*","chatgpt-*","o1*","o3*","o4*"]},{name:"anthropic",type:"anthropic",key:["ANTHROPIC_API_KEY"],baseUrlEnv:"ANTHROPIC_BASE_URL",baseUrl:"https://api.anthropic.com",models:["claude-*"]},{name:"gemini",type:"openai",key:["GEMINI_API_KEY","GOOGLE_API_KEY"],baseUrl:"https://generativelanguage.googleapis.com/v1beta/openai",models:["gemini-*","gemma-*"]},{name:"mistral",type:"openai",key:["MISTRAL_API_KEY"],baseUrl:"https://api.mistral.ai/v1",models:["mistral-*","magistral-*","codestral-*","devstral-*","ministral-*","pixtral-*","open-mistral-*","open-mixtral-*"]},{name:"deepseek",type:"openai",key:["DEEPSEEK_API_KEY"],baseUrl:"https://api.deepseek.com/v1",models:["deepseek-*"]},{name:"xai",type:"openai",key:["XAI_API_KEY"],baseUrl:"https://api.x.ai/v1",models:["grok-*"]},{name:"moonshot",type:"openai",key:["MOONSHOT_API_KEY"],baseUrl:"https://api.moonshot.ai/v1",models:["kimi-*","moonshot-*"]},{name:"qwen",type:"openai",key:["DASHSCOPE_API_KEY"],baseUrl:"https://dashscope-intl.aliyuncs.com/compatible-mode/v1",models:["qwen*","qwq-*"]},{name:"cohere",type:"openai",key:["COHERE_API_KEY"],baseUrl:"https://api.cohere.ai/compatibility/v1",models:["command-*"]},{name:"perplexity",type:"openai",key:["PERPLEXITY_API_KEY"],baseUrl:"https://api.perplexity.ai",models:["sonar*"]},{name:"openrouter",type:"openai",key:["OPENROUTER_API_KEY"],baseUrl:"https://openrouter.ai/api/v1",models:["*/*"]},{name:"groq",type:"openai",key:["GROQ_API_KEY"],baseUrl:"https://api.groq.com/openai/v1"},{name:"together",type:"openai",key:["TOGETHER_API_KEY"],baseUrl:"https://api.together.xyz/v1"},{name:"fireworks",type:"openai",key:["FIREWORKS_API_KEY"],baseUrl:"https://api.fireworks.ai/inference/v1"},{name:"cerebras",type:"openai",key:["CEREBRAS_API_KEY"],baseUrl:"https://api.cerebras.ai/v1"},{name:"azure",type:"openai",key:["AZURE_OPENAI_API_KEY"],baseUrlEnv:"AZURE_OPENAI_ENDPOINT",authHeader:"api-key"},{name:"ollama",type:"openai",baseUrlEnv:"OLLAMA_BASE_URL"},{name:"lmstudio",type:"openai",baseUrlEnv:"LMSTUDIO_BASE_URL"}];function hs(a){let e={};for(let n of bt){let i=n.key?.map(t=>a[t]).find(Boolean),r=n.baseUrlEnv&&a[n.baseUrlEnv]||void 0;if(!(!i&&!r)){if(n.name==="azure"){if(!r)continue;r=r.replace(/\/+$/,""),/\/openai\/v1$/.test(r)||(r+="/openai/v1")}(n.name==="ollama"||n.name==="lmstudio")&&r&&!/\/v1\/?$/.test(r)&&(r=r.replace(/\/+$/,"")+"/v1"),e[n.name]={type:n.type,baseUrl:r??n.baseUrl,...i?{apiKey:i}:{},...n.authHeader?{authHeader:n.authHeader}:{}}}}return e}function fs(a={},e={}){let n=ci(a,e),i={...da,...n,safe:{...da.safe,...n.safe},optIn:{...da.optIn,...n.optIn},verify:{...da.verify,...n.verify},batch:{...da.batch,...n.batch},storage:{...da.storage,...n.storage},customers:{...da.customers,...n.customers},upstreams:{...hs(e),...n.upstreams}};for(let r of bt)if(i.upstreams[r.name]&&!n.upstreams?.[r.name])for(let t of r.models??[])i.routes=[...i.routes,{model:t,upstream:r.name}];if(e.TOKEN_LENS_DEFAULT_UPSTREAM&&(i.defaultUpstream=e.TOKEN_LENS_DEFAULT_UPSTREAM),e.TOKEN_LENS_ISOLATE_TEAMS&&(i.isolateTeams=e.TOKEN_LENS_ISOLATE_TEAMS==="true"||e.TOKEN_LENS_ISOLATE_TEAMS==="1"),e.PORT&&(i.port=Number(e.PORT)),e.TOKEN_LENS_MODE&&(i.mode=e.TOKEN_LENS_MODE),e.TOKEN_LENS_VERIFY_RATE&&(i.verify.sampleRate=Number(e.TOKEN_LENS_VERIFY_RATE)),e.TOKEN_LENS_ADMIN_KEY&&(i.adminKey=e.TOKEN_LENS_ADMIN_KEY),e.TOKEN_LENS_REDIS_URL&&(i.storage.redisUrl=e.TOKEN_LENS_REDIS_URL),e.TOKEN_LENS_STORAGE_FILE&&(i.storage.file=e.TOKEN_LENS_STORAGE_FILE),e.TOKEN_LENS_CLIENT_KEYS&&(i.clientKeys=[...i.clientKeys,...e.TOKEN_LENS_CLIENT_KEYS.split(",").map(r=>r.trim()).filter(Boolean).map((r,t)=>{let[s,o]=r.includes(":")?r.split(/:(.*)/s):[`client-${t+1}`,r];return{name:s,key:o}})]),i.clientKeys=i.clientKeys.filter(r=>r.key),i.isolateTeams&&!i.clientKeys.length)throw new Error("isolateTeams needs client keys (clientKeys or TOKEN_LENS_CLIENT_KEYS): without them every caller is the same team.");for(let[r,t]of Object.entries(i.upstreams)){if(t.type!=="openai"&&t.type!=="anthropic")throw new Error(`Upstream "${r}": type must be "openai" or "anthropic".`);if(!t.baseUrl)throw new Error(`Upstream "${r}" needs a baseUrl.`);t.baseUrl=t.baseUrl.replace(/\/+$/,""),t.apiKey||delete t.apiKey}for(let r of i.routes)if(!i.upstreams[r.upstream])throw new Error(`Route "${r.model}" points at unknown upstream "${r.upstream}".`);if(i.defaultUpstream&&!i.upstreams[i.defaultUpstream])throw new Error(`defaultUpstream "${i.defaultUpstream}" is not defined.`);return i}function kt(a=process.env){let e=a.TOKEN_LENS_CONFIG,n=e?JSON.parse(gs(e,"utf8")):{};return fs(n,a)}import{createServer as ll}from"node:http";import{timingSafeEqual as cl}from"node:crypto";function Ba(a,e,n,i){let r=i&&i>0?i:n.contextWindow,t=(a+e)/r,s=t>=.95?"full":t>=.8?"hot":t>=.5?"warm":"ok";return{conversationTokens:a,promptTokens:e,window:r,ratio:t,zone:s,longChat:a>=4e4}}function yt(a,e){return a!=="auto"?a:e.zone==="hot"||e.zone==="full"?"aggressive":"balanced"}var bs={coding:"the files and functions involved, the current state of the code (only the parts still changing, verbatim), and what is left to build",debugging:"the bug, what has been ruled out, the most likely cause, and the exact error message",writing:"the audience, tone, the current outline, and any passages I approved (verbatim)",editing:"the style rules we settled on and which parts are already done",analysis:"the dataset's shape, the findings so far with their numbers, and the next questions",research:"the question, the answers found so far with their sources, and what is still open"},Aa="Optional: the new chat only knows what the brief keeps, so later answers can differ. Check the brief before you rely on it.";function vt(a){return["Write a handoff brief of this conversation that I can paste into a new chat to continue without the history.",`Cover: ${bs[a]??"the goal, the decisions made, and the current state"}; plus open questions and my preferences.`,"Use terse bullets, under 250 words, and nothing else."].join(" ")}var Ta=a=>a.toLocaleString("en-US");function Fa(a){let{profile:e,task:n,signals:i,promptTokens:r,usage:t,text:s}=a,o=[];if(n==="coding"&&i.pastedTokens>1500&&o.push({id:"paste-less-code",text:`About ${Ta(i.pastedTokens)} tokens of pasted code. Paste only the functions involved.`,saves:"input"}),n==="debugging"&&i.hasLogs&&i.pastedTokens>800&&o.push({id:"trim-logs",text:"Long log pasted. The first error and its stack are usually enough; Optimize collapses repeated lines and library frames.",saves:"input"}),n==="analysis"&&i.hasData&&i.pastedTokens>2e3&&o.push({id:"sample-data",text:"Large table pasted. The header plus ~20 sample rows and a description of the rest is usually enough; or attach the file instead.",saves:"input"}),i.pastedTokens>t.window*.4&&o.push({id:"split-material",text:`The pasted material is ${Math.round(i.pastedTokens/t.window*100)}% of ${e.label}'s context. Split it into parts or paste only what's needed.`,saves:"input"}),e.family==="anthropic"&&i.pastedTokens>300&&!/<[a-z][\w-]*>/i.test(s))for(let u of e.notes??[])o.push({id:"family-note",text:u,saves:"input"});t.zone==="hot"||t.zone==="full"?o.push({id:"handoff",text:`This chat fills about ${Math.round(t.ratio*100)}% of ${e.label}'s context. Older messages will be dropped or compacted soon. Hand off to a new chat. ${Aa}`,saves:"input"}):(t.longChat||t.zone==="warm")&&o.push({id:"handoff",text:`Every new message re-reads about ${Ta(t.conversationTokens)} tokens of history. A handoff brief and a new chat cut that. ${Aa}`,saves:"input"});let l=e.pricing?.longContext,c=t.conversationTokens+r;return l&&e.pricing&&c>=l.aboveTokens*.85&&o.push({id:"price-threshold",text:c>l.aboveTokens?`This message reads \u2248${Ta(c)} tokens, past ${Ta(l.aboveTokens)}, so ${e.label} bills all of it at $${l.inputPerMTok}/M instead of $${e.pricing.inputPerMTok}/M. Continuing from a handoff brief in a new chat brings it back under. ${Aa}`:`This chat is \u2248${Ta(c)} tokens; past ${Ta(l.aboveTokens)}, ${e.label} bills every message at $${l.inputPerMTok}/M instead of $${e.pricing.inputPerMTok}/M. Hand off to a new chat before then. ${Aa}`,saves:"input"}),o}var he="<|endoftext|>",Fe="<|fim_prefix|>",Ge="<|fim_middle|>",qe="<|fim_suffix|>",ma="<|im_start|>",pa="<|im_end|>",Ue="<|im_sep|>",He="<|endofprompt|>",ui="<|startoftext|>",Ga="<|start|>",qa="<|end|>",Ua="<|message|>",Ha="<|channel|>",Va="<|return|>",Wa="<|constrain|>",Ka="<|call|>";var wt=a=>a<=127;function di(a){if(a.length===0)return!1;let e=a.charCodeAt(a.length-1);return e>=55296&&e<=56319}function ks(a){let e=0;for(;e<a.length;){let n=a[e],i=0,r=0;if(n<=127)i=1,r=n;else if((n&224)===192){if(i=2,r=n&31,n<=193)return!1}else if((n&240)===224)i=3,r=n&15;else if((n&248)===240){if(i=4,r=n&7,n>244)return!1}else return!1;if(e+i>a.length)return!1;for(let t=1;t<i;t++){let s=a[e+t];if(s===void 0||(s&192)!==128)return!1;r=r<<6|s&63}if(i===2&&r<128||i===3&&r<2048||i===4&&r<65536||r>=55296&&r<=57343||r>1114111)return!1;e+=i}return!0}var ys=new TextDecoder("utf8",{fatal:!1});function St(a){if(ks(a))return ys.decode(a)}function _t(a,e){let n=Math.min(a.length,e.length);for(let i=0;i<n;i++)if(a[i]!==e[i])return a[i]-e[i];return a.length-e.length}function Et(a){let e=0;return a.forEach(n=>{e=Math.max(e,n)}),e}function mi(a){return a.replace(/[$()*+.?[\\\]^{|}]/g,"\\$&")}function pi(a){let e=[...a].map(mi).join("|");return new RegExp(`(${e})`)}var vn=new Uint8Array(0),Ca=new TextDecoder("utf8"),Tt=class{mergeableBytePairRankCount;bytePairRankDecoder;bytePairNonUtfRankDecoder=new Map;bytePairNonUtfSortedEncoder;bytePairStringRankEncoder;tokenSplitRegex;specialTokensEncoder;specialTokensDecoder;specialTokenPatternRegex;textEncoder=new TextEncoder;mergeCache;mergeCacheSize;constructor({bytePairRankDecoder:a,specialTokensEncoder:e,tokenSplitRegex:n,mergeCacheSize:i=1e5}){this.bytePairRankDecoder=a,this.bytePairStringRankEncoder=new Map,this.mergeCacheSize=i,i>0&&(this.mergeCache=new Map),this.mergeableBytePairRankCount=Object.keys(a).length;let r=[];a.forEach((s,o)=>{if(typeof s=="string"){this.bytePairStringRankEncoder.set(s,o);return}let l=new Uint8Array(s);r.push([l,o]),this.bytePairNonUtfRankDecoder.set(o,l)}),this.bytePairNonUtfSortedEncoder=r.sort((s,o)=>_t(s[0],o[0])),this.specialTokensEncoder=e??new Map,this.specialTokensDecoder=e?new Map([...e].map(([s,o])=>[o,s])):new Map,this.tokenSplitRegex=n;let t=[...this.specialTokensEncoder.keys()].map(mi).join("|");try{this.specialTokenPatternRegex=new RegExp(t,"y")}catch{throw new Error("Invalid regular expression pattern.")}}setMergeCacheSize(a){this.mergeCacheSize===0&&a>0&&(this.mergeCache=new Map),this.mergeCacheSize=a,a===0&&(this.mergeCache=void 0)}clearMergeCache(){this.mergeCache?.clear()}*encodeNativeGenerator(a,e){let n=0,i=0;for(;;){let r=this.findNextSpecialToken(a,e,n),t=r?.[0],s=t??a.length,o=n===0&&s===a.length?a:a.slice(n,s);for(let[l]of o.matchAll(this.tokenSplitRegex)){let c=this.getBpeRankFromString(l);if(c!==void 0){i=1,yield[c];continue}let u=this.bytePairEncode(l);i=u.length,yield u}if(t!==void 0){let l=r[1],c=this.specialTokensEncoder.get(l);if(c===void 0)throw new Error(`Special token "${l}" is not in the special token encoder.`);yield[c],n=t+l.length,i=1}else break}return i}encodeNative(a,e){let n=0,i=[];for(;;){let r=this.findNextSpecialToken(a,e,n),t=r?.[0],s=t??a.length,o=n===0&&s===a.length?a:a.slice(n,s);for(let[l]of o.matchAll(this.tokenSplitRegex)){let c=this.getBpeRankFromString(l);if(c!==void 0){i.push(c);continue}let u=this.bytePairEncode(l);i.push(...u)}if(t!==void 0){let l=r[1],c=this.specialTokensEncoder.get(l);if(c===void 0)throw new Error(`Special token "${l}" is not in the special token encoder.`);i.push(c),n=t+l.length}else break}return i}countNative(a,e){let n=0,i=0;for(;;){let r=this.findNextSpecialToken(a,e,n),t=r?.[0],s=t??a.length,o=n===0&&s===a.length?a:a.slice(n,s);for(let[l]of o.matchAll(this.tokenSplitRegex)){if(this.getBpeRankFromString(l)!==void 0){i++;continue}let c=this.bytePairEncode(l);i+=c.length}if(t!==void 0){let l=r[1];if(this.specialTokensEncoder.get(l)===void 0)throw new Error(`Special token "${l}" is not in the special token encoder.`);i++,n=t+l.length}else break}return i}*decodeNativeGenerator(a){for(let e of a){let n=this.tryDecodeToken(e);n&&(yield n)}}decodeNative(a){let e="",n=vn;for(let i of a){let r=this.tryDecodeToken(i);if(r===void 0)throw new Error(`Token ${i} is not in the byte pair encoder.`);if(typeof r=="string")n!==vn&&(e+=Ca.decode(n,{stream:!0}),n=vn),e+=r;else{let t=new Uint8Array(n.length+r.length);t.set(n),t.set(r,n.length),n=t}}return n!==vn&&(e+=Ca.decode(n,{stream:!0})),e}async*decodeNativeAsyncIterable(a){for await(let e of a){let n=this.tryDecodeToken(e);n&&(yield n)}}getBpeRankFromString(a){return this.bytePairStringRankEncoder.get(a)}getBpeRankFromStringOrThrow(a){let e=this.getBpeRankFromString(a);if(e===void 0)throw new Error(`The byte-pair encoding does not contain a value for: ${a}`);return e}getBpeRankFromBytes(a){let e=St(a);if(e!==void 0)return this.getBpeRankFromString(e);let n=this.binarySearch(a);if(n!==-1)return this.bytePairNonUtfSortedEncoder[n][1]}getBpeRankFromBytesOrThrow(a){let e=this.getBpeRankFromBytes(a);if(e===void 0)throw new Error(`The byte-pair encoding does not contain a value for: ${a.toString()}`);return e}binarySearch(a){let e=0,n=this.bytePairNonUtfSortedEncoder.length-1;for(;e<=n;){let i=e+n>>>1,r=this.bytePairNonUtfSortedEncoder[i][0],t=0,s=Math.min(r.length,a.length);for(let o=0;o<s&&(t=r[o]-a[o],t===0);o++);if(t===0&&(t=r.length-a.length),t===0)return i;t<0?e=i+1:n=i-1}return-1}findNextSpecialToken(a,e,n){let i=n;for(;;){this.specialTokenPatternRegex.lastIndex=i;let r=this.specialTokenPatternRegex.exec(a);if(!r)return;let t=r[0];if(e?.has(t))return[r.index+i,t];i=r.index+i+1}}tryDecodeToken(a){let e=this.bytePairRankDecoder[a];if(typeof e=="string")return e;if(typeof e=="object"){let n=this.bytePairNonUtfRankDecoder.get(a);if(n)return n}return this.specialTokensDecoder.get(a)}addToMergeCache(a,e){if(this.mergeCache){if(this.mergeCache.size>=this.mergeCacheSize){let n=this.mergeCache.keys().next().value;this.mergeCache.delete(n)}this.mergeCache.set(a,e)}}bytePairEncode(a){if(a.length===1&&wt(a.codePointAt(0)))return[this.getBpeRankFromStringOrThrow(a)];if(this.mergeCache?.has(a)){let i=this.mergeCache.get(a);return this.mergeCache.delete(a),this.mergeCache.set(a,i),i}let e=this.textEncoder.encode(a),n=this.bytePairMerge(e);return this.addToMergeCache(a,n),n}bytePairMerge(a){let e=[],n=[],i=(t,s=e[t],o=e[t+2])=>{if(o===void 0)return Number.POSITIVE_INFINITY;let l=a.subarray(s,o);return this.getBpeRankFromBytes(l)??Number.POSITIVE_INFINITY};for(let t=0;t<=a.length;t++)e.push(t),t<a.length-1?n.push(i(t,t,t+2)):n.push(Number.POSITIVE_INFINITY);for(;e.length>1;){let t=Number.POSITIVE_INFINITY,s=-1;for(let o=0;o<n.length-1;o++){let l=n[o];l<t&&(t=l,s=o)}if(t===Number.POSITIVE_INFINITY||s===-1)break;e.splice(s+1,1),n.splice(s,1),n[s]=i(s),s>0&&(n[s-1]=i(s-1))}let r=[];for(let t=0;t<e.length-1;t++){let s=e[t],o=e[t+1],l=this.getBpeRankFromBytesOrThrow(a.subarray(s,o));r.push(l)}return r}};function vs(a,e){let n=0;if(a.role&&(n+=e(a.role)),a.content&&(n+=e(a.content)),a.name&&(n+=e(a.name)+1),a.function_call){let{name:i,arguments:r}=a.function_call;i&&(n+=e(i)),r&&(n+=e(r)),n+=3}return n+=3,a.role==="function"&&(n-=2),n}function Ct(a,e,n){if(!a.properties)return"";let i=[],r=new Set(a.required??[]),t=" ".repeat(e);for(let[s,o]of Object.entries(a.properties)){o.description&&e<2&&i.push(`${t}// ${o.description}`);let l=r.has(s),c=n(o,e);i.push(`${t}${s}${l?"":"?"}: ${c},`)}return i.join(`
6
+ `}var da={port:8080,host:"0.0.0.0",mode:"optimize",level:"auto",safe:{prompt:!0,minifyJson:!0,collapseRepeats:!0,dedupeToolOutputs:!0,dedupePassages:!0,dedupeImages:!0,promptCaching:!0,dedupeRetries:!0,cacheEmbeddings:!0},optIn:{elideOldToolOutputs:!1,lazyTools:!1,trimToolFields:!1,compactJson:!1,parkToolOutputs:!1,flex:!1,warmCache:!1},verify:{sampleRate:.05,minSimilarity:.8,maxShareOfSavings:.2,freeChecks:5,probation:5},runBudget:{},batch:{flushSeconds:60,pollSeconds:60,maxRequests:1e3,discount:.5},flexDiscount:.5,embeddingsCacheMB:256,priceTiers:[],storage:{keyPrefix:"token-lens:",flushSeconds:10,runDays:7},retryWindowSeconds:120,blockOverContext:!1,upstreams:{},routes:[],models:[],clientKeys:[],customers:dt,isolateTeams:!1,maxBodyBytes:32*1024*1024,maxRuns:2e3,logRequests:!0,reportToolArgs:!1};function ci(a,e){if(typeof a=="string"&&a.startsWith("env:")){let n=a.slice(4);return e[n]??""}return Array.isArray(a)?a.map(n=>ci(n,e)):a&&typeof a=="object"?Object.fromEntries(Object.entries(a).map(([n,i])=>[n,ci(i,e)])):a}var bt=[{name:"openai",type:"openai",key:["OPENAI_API_KEY"],baseUrlEnv:"OPENAI_BASE_URL",baseUrl:"https://api.openai.com/v1",models:["gpt-*","chatgpt-*","o1*","o3*","o4*"]},{name:"anthropic",type:"anthropic",key:["ANTHROPIC_API_KEY"],baseUrlEnv:"ANTHROPIC_BASE_URL",baseUrl:"https://api.anthropic.com",models:["claude-*"]},{name:"gemini",type:"openai",key:["GEMINI_API_KEY","GOOGLE_API_KEY"],baseUrl:"https://generativelanguage.googleapis.com/v1beta/openai",models:["gemini-*","gemma-*"]},{name:"mistral",type:"openai",key:["MISTRAL_API_KEY"],baseUrl:"https://api.mistral.ai/v1",models:["mistral-*","magistral-*","codestral-*","devstral-*","ministral-*","pixtral-*","open-mistral-*","open-mixtral-*"]},{name:"deepseek",type:"openai",key:["DEEPSEEK_API_KEY"],baseUrl:"https://api.deepseek.com/v1",models:["deepseek-*"]},{name:"xai",type:"openai",key:["XAI_API_KEY"],baseUrl:"https://api.x.ai/v1",models:["grok-*"]},{name:"moonshot",type:"openai",key:["MOONSHOT_API_KEY"],baseUrl:"https://api.moonshot.ai/v1",models:["kimi-*","moonshot-*"]},{name:"qwen",type:"openai",key:["DASHSCOPE_API_KEY"],baseUrl:"https://dashscope-intl.aliyuncs.com/compatible-mode/v1",models:["qwen*","qwq-*"]},{name:"cohere",type:"openai",key:["COHERE_API_KEY"],baseUrl:"https://api.cohere.ai/compatibility/v1",models:["command-*"]},{name:"perplexity",type:"openai",key:["PERPLEXITY_API_KEY"],baseUrl:"https://api.perplexity.ai",models:["sonar*"]},{name:"openrouter",type:"openai",key:["OPENROUTER_API_KEY"],baseUrl:"https://openrouter.ai/api/v1",models:["*/*"]},{name:"groq",type:"openai",key:["GROQ_API_KEY"],baseUrl:"https://api.groq.com/openai/v1"},{name:"together",type:"openai",key:["TOGETHER_API_KEY"],baseUrl:"https://api.together.xyz/v1"},{name:"fireworks",type:"openai",key:["FIREWORKS_API_KEY"],baseUrl:"https://api.fireworks.ai/inference/v1"},{name:"cerebras",type:"openai",key:["CEREBRAS_API_KEY"],baseUrl:"https://api.cerebras.ai/v1"},{name:"azure",type:"openai",key:["AZURE_OPENAI_API_KEY"],baseUrlEnv:"AZURE_OPENAI_ENDPOINT",authHeader:"api-key"},{name:"ollama",type:"openai",baseUrlEnv:"OLLAMA_BASE_URL"},{name:"lmstudio",type:"openai",baseUrlEnv:"LMSTUDIO_BASE_URL"}];function hs(a){let e={};for(let n of bt){let i=n.key?.map(t=>a[t]).find(Boolean),r=n.baseUrlEnv&&a[n.baseUrlEnv]||void 0;if(!(!i&&!r)){if(n.name==="azure"){if(!r)continue;r=r.replace(/\/+$/,""),/\/openai\/v1$/.test(r)||(r+="/openai/v1")}(n.name==="ollama"||n.name==="lmstudio")&&r&&!/\/v1\/?$/.test(r)&&(r=r.replace(/\/+$/,"")+"/v1"),e[n.name]={type:n.type,baseUrl:r??n.baseUrl,...i?{apiKey:i}:{},...n.authHeader?{authHeader:n.authHeader}:{}}}}return e}function fs(a={},e={}){let n=ci(a,e),i={...da,...n,safe:{...da.safe,...n.safe},optIn:{...da.optIn,...n.optIn},verify:{...da.verify,...n.verify},batch:{...da.batch,...n.batch},storage:{...da.storage,...n.storage},customers:{...da.customers,...n.customers},upstreams:{...hs(e),...n.upstreams}};for(let r of bt)if(i.upstreams[r.name]&&!n.upstreams?.[r.name])for(let t of r.models??[])i.routes=[...i.routes,{model:t,upstream:r.name}];if(e.TOKEN_LENS_DEFAULT_UPSTREAM&&(i.defaultUpstream=e.TOKEN_LENS_DEFAULT_UPSTREAM),e.TOKEN_LENS_ISOLATE_TEAMS&&(i.isolateTeams=e.TOKEN_LENS_ISOLATE_TEAMS==="true"||e.TOKEN_LENS_ISOLATE_TEAMS==="1"),e.PORT&&(i.port=Number(e.PORT)),e.TOKEN_LENS_MODE&&(i.mode=e.TOKEN_LENS_MODE),e.TOKEN_LENS_VERIFY_RATE&&(i.verify.sampleRate=Number(e.TOKEN_LENS_VERIFY_RATE)),e.TOKEN_LENS_ADMIN_KEY&&(i.adminKey=e.TOKEN_LENS_ADMIN_KEY),e.TOKEN_LENS_REDIS_URL&&(i.storage.redisUrl=e.TOKEN_LENS_REDIS_URL),e.TOKEN_LENS_STORAGE_FILE&&(i.storage.file=e.TOKEN_LENS_STORAGE_FILE),e.TOKEN_LENS_CLIENT_KEYS&&(i.clientKeys=[...i.clientKeys,...e.TOKEN_LENS_CLIENT_KEYS.split(",").map(r=>r.trim()).filter(Boolean).map((r,t)=>{let[s,o]=r.includes(":")?r.split(/:(.*)/s):[`client-${t+1}`,r];return{name:s,key:o}})]),i.clientKeys=i.clientKeys.filter(r=>r.key),i.isolateTeams&&!i.clientKeys.length)throw new Error("isolateTeams needs client keys (clientKeys or TOKEN_LENS_CLIENT_KEYS): without them every caller is the same team.");for(let[r,t]of Object.entries(i.upstreams)){if(t.type!=="openai"&&t.type!=="anthropic")throw new Error(`Upstream "${r}": type must be "openai" or "anthropic".`);if(!t.baseUrl)throw new Error(`Upstream "${r}" needs a baseUrl.`);t.baseUrl=t.baseUrl.replace(/\/+$/,""),t.apiKey||delete t.apiKey}for(let r of i.routes)if(!i.upstreams[r.upstream])throw new Error(`Route "${r.model}" points at unknown upstream "${r.upstream}".`);if(i.defaultUpstream&&!i.upstreams[i.defaultUpstream])throw new Error(`defaultUpstream "${i.defaultUpstream}" is not defined.`);return i}function kt(a=process.env){let e=a.TOKEN_LENS_CONFIG,n=e?JSON.parse(gs(e,"utf8")):{};return fs(n,a)}import{createServer as ll}from"node:http";import{timingSafeEqual as cl}from"node:crypto";function Ba(a,e,n,i){let r=i&&i>0?i:n.contextWindow,t=(a+e)/r,s=t>=.95?"full":t>=.8?"hot":t>=.5?"warm":"ok";return{conversationTokens:a,promptTokens:e,window:r,ratio:t,zone:s,longChat:a>=4e4}}function yt(a,e){return a!=="auto"?a:e.zone==="hot"||e.zone==="full"?"aggressive":"balanced"}var bs={coding:"the files and functions involved, the current state of the code (only the parts still changing, verbatim), and what is left to build",debugging:"the bug, what has been ruled out, the most likely cause, and the exact error message",writing:"the audience, tone, the current outline, and any passages I approved (verbatim)",editing:"the style rules we settled on and which parts are already done",analysis:"the dataset's shape, the findings so far with their numbers, and the next questions",research:"the question, the answers found so far with their sources, and what is still open"},ma="Optional: the new chat only knows what the brief keeps, so later answers can differ. Check the brief before you rely on it.";function vt(a){return["Write a handoff brief of this conversation that I can paste into a new chat to continue without the history.",`Cover: ${bs[a]??"the goal, the decisions made, and the current state"}; plus open questions and my preferences.`,"Use terse bullets, under 250 words, and nothing else."].join(" ")}var Ta=a=>a.toLocaleString("en-US");function Fa(a){let{profile:e,task:n,signals:i,promptTokens:r,usage:t,text:s}=a,o=[];if(n==="coding"&&i.pastedTokens>1500&&o.push({id:"paste-less-code",text:`About ${Ta(i.pastedTokens)} tokens of pasted code. Paste only the functions involved.`,saves:"input"}),n==="debugging"&&i.hasLogs&&i.pastedTokens>800&&o.push({id:"trim-logs",text:"Long log pasted. The first error and its stack are usually enough; Optimize collapses repeated lines and library frames.",saves:"input"}),n==="analysis"&&i.hasData&&i.pastedTokens>2e3&&o.push({id:"sample-data",text:"Large table pasted. The header plus ~20 sample rows and a description of the rest is usually enough; or attach the file instead.",saves:"input"}),i.pastedTokens>t.window*.4&&o.push({id:"split-material",text:`The pasted material is ${Math.round(i.pastedTokens/t.window*100)}% of ${e.label}'s context. Split it into parts or paste only what's needed.`,saves:"input"}),e.family==="anthropic"&&i.pastedTokens>300&&!/<[a-z][\w-]*>/i.test(s))for(let u of e.notes??[])o.push({id:"family-note",text:u,saves:"input"});t.zone==="hot"||t.zone==="full"?o.push({id:"handoff",text:`This chat fills about ${Math.round(t.ratio*100)}% of ${e.label}'s context. Older messages will be dropped or compacted soon. Hand off to a new chat. ${ma}`,saves:"input"}):(t.longChat||t.zone==="warm")&&o.push({id:"handoff",text:`Every new message re-reads about ${Ta(t.conversationTokens)} tokens of history. A handoff brief and a new chat cut that. ${ma}`,saves:"input"});let l=e.pricing?.longContext,c=t.conversationTokens+r;return l&&e.pricing&&c>=l.aboveTokens*.85&&o.push({id:"price-threshold",text:c>l.aboveTokens?`This message reads \u2248${Ta(c)} tokens, past ${Ta(l.aboveTokens)}, so ${e.label} bills all of it at $${l.inputPerMTok}/M instead of $${e.pricing.inputPerMTok}/M. Continuing from a handoff brief in a new chat brings it back under. ${ma}`:`This chat is \u2248${Ta(c)} tokens; past ${Ta(l.aboveTokens)}, ${e.label} bills every message at $${l.inputPerMTok}/M instead of $${e.pricing.inputPerMTok}/M. Hand off to a new chat before then. ${ma}`,saves:"input"}),o}var he="<|endoftext|>",Fe="<|fim_prefix|>",Ge="<|fim_middle|>",qe="<|fim_suffix|>",pa="<|im_start|>",ga="<|im_end|>",Ue="<|im_sep|>",He="<|endofprompt|>",ui="<|startoftext|>",Ga="<|start|>",qa="<|end|>",Ua="<|message|>",Ha="<|channel|>",Va="<|return|>",Wa="<|constrain|>",Ka="<|call|>";var wt=a=>a<=127;function di(a){if(a.length===0)return!1;let e=a.charCodeAt(a.length-1);return e>=55296&&e<=56319}function ks(a){let e=0;for(;e<a.length;){let n=a[e],i=0,r=0;if(n<=127)i=1,r=n;else if((n&224)===192){if(i=2,r=n&31,n<=193)return!1}else if((n&240)===224)i=3,r=n&15;else if((n&248)===240){if(i=4,r=n&7,n>244)return!1}else return!1;if(e+i>a.length)return!1;for(let t=1;t<i;t++){let s=a[e+t];if(s===void 0||(s&192)!==128)return!1;r=r<<6|s&63}if(i===2&&r<128||i===3&&r<2048||i===4&&r<65536||r>=55296&&r<=57343||r>1114111)return!1;e+=i}return!0}var ys=new TextDecoder("utf8",{fatal:!1});function St(a){if(ks(a))return ys.decode(a)}function _t(a,e){let n=Math.min(a.length,e.length);for(let i=0;i<n;i++)if(a[i]!==e[i])return a[i]-e[i];return a.length-e.length}function Et(a){let e=0;return a.forEach(n=>{e=Math.max(e,n)}),e}function mi(a){return a.replace(/[$()*+.?[\\\]^{|}]/g,"\\$&")}function pi(a){let e=[...a].map(mi).join("|");return new RegExp(`(${e})`)}var vn=new Uint8Array(0),Ca=new TextDecoder("utf8"),Tt=class{mergeableBytePairRankCount;bytePairRankDecoder;bytePairNonUtfRankDecoder=new Map;bytePairNonUtfSortedEncoder;bytePairStringRankEncoder;tokenSplitRegex;specialTokensEncoder;specialTokensDecoder;specialTokenPatternRegex;textEncoder=new TextEncoder;mergeCache;mergeCacheSize;constructor({bytePairRankDecoder:a,specialTokensEncoder:e,tokenSplitRegex:n,mergeCacheSize:i=1e5}){this.bytePairRankDecoder=a,this.bytePairStringRankEncoder=new Map,this.mergeCacheSize=i,i>0&&(this.mergeCache=new Map),this.mergeableBytePairRankCount=Object.keys(a).length;let r=[];a.forEach((s,o)=>{if(typeof s=="string"){this.bytePairStringRankEncoder.set(s,o);return}let l=new Uint8Array(s);r.push([l,o]),this.bytePairNonUtfRankDecoder.set(o,l)}),this.bytePairNonUtfSortedEncoder=r.sort((s,o)=>_t(s[0],o[0])),this.specialTokensEncoder=e??new Map,this.specialTokensDecoder=e?new Map([...e].map(([s,o])=>[o,s])):new Map,this.tokenSplitRegex=n;let t=[...this.specialTokensEncoder.keys()].map(mi).join("|");try{this.specialTokenPatternRegex=new RegExp(t,"y")}catch{throw new Error("Invalid regular expression pattern.")}}setMergeCacheSize(a){this.mergeCacheSize===0&&a>0&&(this.mergeCache=new Map),this.mergeCacheSize=a,a===0&&(this.mergeCache=void 0)}clearMergeCache(){this.mergeCache?.clear()}*encodeNativeGenerator(a,e){let n=0,i=0;for(;;){let r=this.findNextSpecialToken(a,e,n),t=r?.[0],s=t??a.length,o=n===0&&s===a.length?a:a.slice(n,s);for(let[l]of o.matchAll(this.tokenSplitRegex)){let c=this.getBpeRankFromString(l);if(c!==void 0){i=1,yield[c];continue}let u=this.bytePairEncode(l);i=u.length,yield u}if(t!==void 0){let l=r[1],c=this.specialTokensEncoder.get(l);if(c===void 0)throw new Error(`Special token "${l}" is not in the special token encoder.`);yield[c],n=t+l.length,i=1}else break}return i}encodeNative(a,e){let n=0,i=[];for(;;){let r=this.findNextSpecialToken(a,e,n),t=r?.[0],s=t??a.length,o=n===0&&s===a.length?a:a.slice(n,s);for(let[l]of o.matchAll(this.tokenSplitRegex)){let c=this.getBpeRankFromString(l);if(c!==void 0){i.push(c);continue}let u=this.bytePairEncode(l);i.push(...u)}if(t!==void 0){let l=r[1],c=this.specialTokensEncoder.get(l);if(c===void 0)throw new Error(`Special token "${l}" is not in the special token encoder.`);i.push(c),n=t+l.length}else break}return i}countNative(a,e){let n=0,i=0;for(;;){let r=this.findNextSpecialToken(a,e,n),t=r?.[0],s=t??a.length,o=n===0&&s===a.length?a:a.slice(n,s);for(let[l]of o.matchAll(this.tokenSplitRegex)){if(this.getBpeRankFromString(l)!==void 0){i++;continue}let c=this.bytePairEncode(l);i+=c.length}if(t!==void 0){let l=r[1];if(this.specialTokensEncoder.get(l)===void 0)throw new Error(`Special token "${l}" is not in the special token encoder.`);i++,n=t+l.length}else break}return i}*decodeNativeGenerator(a){for(let e of a){let n=this.tryDecodeToken(e);n&&(yield n)}}decodeNative(a){let e="",n=vn;for(let i of a){let r=this.tryDecodeToken(i);if(r===void 0)throw new Error(`Token ${i} is not in the byte pair encoder.`);if(typeof r=="string")n!==vn&&(e+=Ca.decode(n,{stream:!0}),n=vn),e+=r;else{let t=new Uint8Array(n.length+r.length);t.set(n),t.set(r,n.length),n=t}}return n!==vn&&(e+=Ca.decode(n,{stream:!0})),e}async*decodeNativeAsyncIterable(a){for await(let e of a){let n=this.tryDecodeToken(e);n&&(yield n)}}getBpeRankFromString(a){return this.bytePairStringRankEncoder.get(a)}getBpeRankFromStringOrThrow(a){let e=this.getBpeRankFromString(a);if(e===void 0)throw new Error(`The byte-pair encoding does not contain a value for: ${a}`);return e}getBpeRankFromBytes(a){let e=St(a);if(e!==void 0)return this.getBpeRankFromString(e);let n=this.binarySearch(a);if(n!==-1)return this.bytePairNonUtfSortedEncoder[n][1]}getBpeRankFromBytesOrThrow(a){let e=this.getBpeRankFromBytes(a);if(e===void 0)throw new Error(`The byte-pair encoding does not contain a value for: ${a.toString()}`);return e}binarySearch(a){let e=0,n=this.bytePairNonUtfSortedEncoder.length-1;for(;e<=n;){let i=e+n>>>1,r=this.bytePairNonUtfSortedEncoder[i][0],t=0,s=Math.min(r.length,a.length);for(let o=0;o<s&&(t=r[o]-a[o],t===0);o++);if(t===0&&(t=r.length-a.length),t===0)return i;t<0?e=i+1:n=i-1}return-1}findNextSpecialToken(a,e,n){let i=n;for(;;){this.specialTokenPatternRegex.lastIndex=i;let r=this.specialTokenPatternRegex.exec(a);if(!r)return;let t=r[0];if(e?.has(t))return[r.index+i,t];i=r.index+i+1}}tryDecodeToken(a){let e=this.bytePairRankDecoder[a];if(typeof e=="string")return e;if(typeof e=="object"){let n=this.bytePairNonUtfRankDecoder.get(a);if(n)return n}return this.specialTokensDecoder.get(a)}addToMergeCache(a,e){if(this.mergeCache){if(this.mergeCache.size>=this.mergeCacheSize){let n=this.mergeCache.keys().next().value;this.mergeCache.delete(n)}this.mergeCache.set(a,e)}}bytePairEncode(a){if(a.length===1&&wt(a.codePointAt(0)))return[this.getBpeRankFromStringOrThrow(a)];if(this.mergeCache?.has(a)){let i=this.mergeCache.get(a);return this.mergeCache.delete(a),this.mergeCache.set(a,i),i}let e=this.textEncoder.encode(a),n=this.bytePairMerge(e);return this.addToMergeCache(a,n),n}bytePairMerge(a){let e=[],n=[],i=(t,s=e[t],o=e[t+2])=>{if(o===void 0)return Number.POSITIVE_INFINITY;let l=a.subarray(s,o);return this.getBpeRankFromBytes(l)??Number.POSITIVE_INFINITY};for(let t=0;t<=a.length;t++)e.push(t),t<a.length-1?n.push(i(t,t,t+2)):n.push(Number.POSITIVE_INFINITY);for(;e.length>1;){let t=Number.POSITIVE_INFINITY,s=-1;for(let o=0;o<n.length-1;o++){let l=n[o];l<t&&(t=l,s=o)}if(t===Number.POSITIVE_INFINITY||s===-1)break;e.splice(s+1,1),n.splice(s,1),n[s]=i(s),s>0&&(n[s-1]=i(s-1))}let r=[];for(let t=0;t<e.length-1;t++){let s=e[t],o=e[t+1],l=this.getBpeRankFromBytesOrThrow(a.subarray(s,o));r.push(l)}return r}};function vs(a,e){let n=0;if(a.role&&(n+=e(a.role)),a.content&&(n+=e(a.content)),a.name&&(n+=e(a.name)+1),a.function_call){let{name:i,arguments:r}=a.function_call;i&&(n+=e(i)),r&&(n+=e(r)),n+=3}return n+=3,a.role==="function"&&(n-=2),n}function Ct(a,e,n){if(!a.properties)return"";let i=[],r=new Set(a.required??[]),t=" ".repeat(e);for(let[s,o]of Object.entries(a.properties)){o.description&&e<2&&i.push(`${t}// ${o.description}`);let l=r.has(s),c=n(o,e);i.push(`${t}${s}${l?"":"?"}: ${c},`)}return i.join(`
7
7
  `)}function gi(a,e){switch(a.type){case"string":return a.enum?.map(n=>JSON.stringify(n)).join(" | ")??"string";case"integer":case"number":return a.enum?.map(n=>`${n}`).join(" | ")??"number";case"boolean":return"boolean";case"null":return"null";case"array":return a.items?`${gi(a.items,e)}[]`:"any[]";case"object":return`{
8
8
  ${Ct(a,e+2,gi)}
9
9
  ${" ".repeat(e)}}`;default:return"any"}}function ws(a){let e=["namespace functions {",""];for(let n of a){n.description&&e.push(`// ${n.description}`);let{parameters:i}=n,r=i?.properties;if(!i||!r||Object.keys(r).length===0)e.push(`type ${n.name} = () => any;`);else{e.push(`type ${n.name} = (_: {`);let t=Ct(i,0,gi);t.length>0&&e.push(t),e.push("}) => any;")}e.push("")}return e.push("} // namespace functions"),e.join(`
@@ -11,7 +11,7 @@ ${" ".repeat(e)}}`;default:return"any"}}function ws(a){let e=["namespace functio
11
11
  `)?a:{...a,content:`${a.content}
12
12
  `}}function Rt(a,e){let{messages:n,functions:i,function_call:r}=a,t=!!(i&&i.length>0),s=!1,o=0;for(let l of n){let c=_s(l,t,s);(c!==l&&l.role==="system"||l.role==="system"&&t&&!s)&&(s=!0),o+=vs(c,e)}return o+=3,t&&i&&(o+=Ss(i,e),n.some(l=>l.role==="system")&&(o-=4)),r&&r!=="auto"&&(r==="none"?o+=1:typeof r=="object"&&r.name&&(o+=e(r.name)+4)),o}var Pt=["chat-latest","chatgpt-4o-latest","codex-mini-latest","computer-use-preview","computer-use-preview-2025-03-11","daybreak-blue-latest","daybreak-red-latest","gpt-3.5","gpt-3.5-0301","gpt-3.5-turbo","gpt-3.5-turbo-0125","gpt-3.5-turbo-0613","gpt-3.5-turbo-1106","gpt-3.5-turbo-16k-0613","gpt-3.5-turbo-instruct","gpt-4","gpt-4-0125-preview","gpt-4-0314","gpt-4-0613","gpt-4-1106-preview","gpt-4-1106-vision-preview","gpt-4-32k","gpt-4-turbo","gpt-4-turbo-2024-04-09","gpt-4-turbo-preview","gpt-4.1","gpt-4.1-2025-04-14","gpt-4.1-mini","gpt-4.1-mini-2025-04-14","gpt-4.1-nano","gpt-4.1-nano-2025-04-14","gpt-4.5-preview","gpt-4.5-preview-2025-02-27","gpt-4o","gpt-4o-2024-05-13","gpt-4o-2024-08-06","gpt-4o-2024-11-20","gpt-4o-audio-preview","gpt-4o-audio-preview-2024-10-01","gpt-4o-audio-preview-2024-12-17","gpt-4o-audio-preview-2025-06-03","gpt-4o-mini","gpt-4o-mini-2024-07-18","gpt-4o-mini-audio-preview","gpt-4o-mini-audio-preview-2024-12-17","gpt-4o-mini-search-preview","gpt-4o-mini-search-preview-2025-03-11","gpt-4o-search-preview","gpt-4o-search-preview-2025-03-11","gpt-5","gpt-5-2025-08-07","gpt-5-chat-latest","gpt-5-codex","gpt-5-mini","gpt-5-mini-2025-08-07","gpt-5-nano","gpt-5-nano-2025-08-07","gpt-5-pro","gpt-5-pro-2025-10-06","gpt-5.1","gpt-5.1-2025-11-13","gpt-5.1-chat-latest","gpt-5.1-codex","gpt-5.1-codex-max","gpt-5.1-codex-mini","gpt-5.2","gpt-5.2-2025-12-11","gpt-5.2-chat-latest","gpt-5.2-codex","gpt-5.2-pro","gpt-5.2-pro-2025-12-11","gpt-5.3-chat-latest","gpt-5.3-codex","gpt-5.4","gpt-5.4-2026-03-05","gpt-5.4-mini","gpt-5.4-mini-2026-03-17","gpt-5.4-nano","gpt-5.4-nano-2026-03-17","gpt-5.4-pro","gpt-5.4-pro-2026-03-05","gpt-5.5","gpt-5.5-2026-04-23","gpt-5.5-pro","gpt-5.5-pro-2026-04-23","gpt-5.6-cyber","gpt-5.6-luna","gpt-5.6-sol","gpt-5.6-terra","gpt-audio","gpt-audio-1.5","gpt-audio-2025-08-28","gpt-audio-mini","gpt-audio-mini-2025-10-06","gpt-audio-mini-2025-12-15","gpt-image-1","gpt-oss-120b","gpt-oss-20b","o1","o1-2024-12-17","o1-mini","o1-mini-2024-09-12","o1-preview","o1-preview-2024-09-12","o1-pro","o1-pro-2025-03-19","o3","o3-2025-04-16","o3-deep-research","o3-deep-research-2025-06-26","o3-mini","o3-mini-2025-01-31","o3-pro","o3-pro-2025-06-10","o4-mini","o4-mini-2025-04-16","o4-mini-deep-research","o4-mini-deep-research-2025-06-26"];var zt=Object.defineProperty,It=(a,e)=>{let n={};for(var i in a)zt(n,i,{get:a[i],enumerable:!0});return e||zt(n,Symbol.toStringTag,{value:"Module"}),n};var xt=It({cl100k_base:()=>Rs,gpt2:()=>Es,o200k_base:()=>Ps,o200k_harmony:()=>zs,p50k_base:()=>As,p50k_edit:()=>Cs,r50k_base:()=>Ts}),Es=["gpt2","gpt-2"],As=["text-davinci-002","text-davinci-003","code-davinci-001","code-davinci-002","davinci-codex","code-cushman-001","code-cushman-002","cushman-codex"],Ts=["text-ada-001","text-babbage-001","text-curie-001","text-davinci-001","ada","babbage","curie","davinci","code-search-ada-code-001","code-search-ada-text-001","text-similarity-ada-001","text-search-ada-doc-001","text-search-ada-query-001","text-similarity-babbage-001","text-search-babbage-doc-001","text-search-babbage-query-001","code-search-babbage-code-001","code-search-babbage-text-001","text-similarity-curie-001","text-search-curie-doc-001","text-search-curie-query-001","text-similarity-davinci-001","text-search-davinci-doc-001","text-search-davinci-query-001"],Cs=["code-davinci-edit-001","text-davinci-edit-001"],Rs=["gpt-3.5","gpt-3.5-0301","gpt-3.5-turbo","gpt-3.5-turbo-0125","gpt-3.5-turbo-0613","gpt-3.5-turbo-1106","gpt-3.5-turbo-16k-0613","gpt-3.5-turbo-instruct","gpt-4","gpt-4-0125-preview","gpt-4-0314","gpt-4-0613","gpt-4-1106-preview","gpt-4-1106-vision-preview","gpt-4-32k","gpt-4-turbo","gpt-4-turbo-2024-04-09","gpt-4-turbo-preview","text-embedding-3-large","text-embedding-3-small","text-embedding-ada-002","babbage-002","davinci-002"],Ps=[],zs=["gpt-oss-20b","gpt-oss-120b"];var Mt=Object.fromEntries(Object.entries(xt).flatMap(([a,e])=>e.map(n=>[n,a]))),Is={messageSeparator:`
13
13
  `,roleSeparator:`
14
- `},xs={messageSeparator:"",roleSeparator:Ue},jt=Object.fromEntries(Pt.flatMap(a=>a.startsWith("gpt-3.5")?[[a,Is]]:[[a,xs]]));var Ra=new RegExp("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+","gu"),Dt=String.raw`'(?:[sS]|[dD]|[mM]|[tT]|[lL][lL]|[vV][eE]|[rR][eE])`,Ot=String.raw`(?:${Dt})?`,Ms=String.raw`${Dt}|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s+$|\s*[\r\n]|\s+(?!\S)|\s`,Nt=new RegExp(Ms,"gu"),js=String.raw`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+${Ot}|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*${Ot}|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`,wn=new RegExp(js,"gu");function Lt(a){return{tokenSplitRegex:Nt,bytePairRankDecoder:a,specialTokensEncoder:new Map([[he,100257],[Fe,100258],[Ge,100259],[qe,100260],[ma,100264],[pa,100265],[Ue,100266],[He,100276]])}}var Os=[[he,199999],[Fe,2e5],[Ge,200001],[qe,200002],[ma,200003],[pa,200004],[Ue,200005],[He,200006]],Ds=()=>new Map(Os);function Bt(a){return{tokenSplitRegex:wn,bytePairRankDecoder:a,specialTokensEncoder:Ds()}}var Ns=200013,Ls=201088,Bs=[[ui,199998],[he,199999],["<|reserved_200000|>",2e5],["<|reserved_200001|>",200001],[Va,200002],[Wa,200003],["<|reserved_200004|>",200004],[Ha,200005],[Ga,200006],[qa,200007],[Ua,200008],["<|reserved_200009|>",200009],["<|reserved_200010|>",200010],["<|reserved_200011|>",200011],[Ka,200012]];function Ft(a){let e=new Map(Bs);for(let n=Ns;n<Ls;n+=1)e.set(`<|reserved_${n}|>`,n);return e.set(He,200018),{tokenSplitRegex:wn,bytePairRankDecoder:a,specialTokensEncoder:e,chatFormatter:"harmony"}}function Gt(a){return{expectedVocabularySize:50281,tokenSplitRegex:Ra,bytePairRankDecoder:a,specialTokensEncoder:new Map([[he,50256]])}}function qt(a){return{tokenSplitRegex:Ra,bytePairRankDecoder:a,specialTokensEncoder:new Map([[he,50256],[Fe,50281],[Ge,50282],[qe,50283]])}}function Ut(a){return{expectedVocabularySize:50257,tokenSplitRegex:Ra,bytePairRankDecoder:a,specialTokensEncoder:new Map([[he,50256]])}}function hi(a,e){let n=e(a);switch(a.toLowerCase()){case"gpt2":case"r50k_base":return Ut(n);case"p50k_base":return Gt(n);case"p50k_edit":return qt(n);case"cl100k_base":return Lt(n);case"o200k_base":return Bt(n);case"o200k_harmony":return Ft(n);default:throw new Error(`Unknown encoding name: ${a}`)}}var Ht=class fi{static EndOfPrompt=He;static EndOfText=he;static FimMiddle=Ge;static FimPrefix=Fe;static FimSuffix=qe;modelName;modelSpec;bytePairEncodingCoreProcessor;specialTokensEncoder;specialTokensSet;allSpecialTokenRegex;defaultSpecialTokenConfig;chatFormatter;countChatCompletionTokens;vocabularySize;constructor({bytePairRankDecoder:e,specialTokensEncoder:n,expectedVocabularySize:i,modelName:r,modelSpec:t,chatFormatter:s,...o}){this.specialTokensEncoder=n,this.specialTokensSet=new Set(this.specialTokensEncoder.keys()),this.allSpecialTokenRegex=pi(this.specialTokensSet),this.bytePairEncodingCoreProcessor=new Tt({bytePairRankDecoder:e,specialTokensEncoder:n,...o}),this.defaultSpecialTokenConfig=this.processSpecialTokens();let l=Math.max(e.length-1,Et(n));if(this.vocabularySize=this.bytePairEncodingCoreProcessor.mergeableBytePairRankCount+n.size,i!==void 0){if(this.vocabularySize!==i)throw new Error("The number of mergeable tokens and special tokens must be equal to expectedVocabularySize.");if(l!==i-1)throw new Error(`The model encodings are invalid. The maximum token value must be equal to expectedVocabularySize - 1. Currently ${l}, expected ${i-1}`)}this.encode=this.encode.bind(this),this.decode=this.decode.bind(this),this.encodeGenerator=this.encodeGenerator.bind(this),this.decodeGenerator=this.decodeGenerator.bind(this),this.decodeAsyncGenerator=this.decodeAsyncGenerator.bind(this),this.decodeAsync=this.decodeAsync.bind(this),this.isWithinTokenLimit=this.isWithinTokenLimit.bind(this),this.encodeChat=this.encodeChat.bind(this),this.encodeChatGenerator=this.encodeChatGenerator.bind(this),this.countTokens=this.countTokens.bind(this),this.setMergeCacheSize=this.setMergeCacheSize.bind(this),this.clearMergeCache=this.clearMergeCache.bind(this),this.estimateCost=this.estimateCost.bind(this),t?.supported_features?.includes("function_calling")&&(this.countChatCompletionTokens=this.countChatCompletionTokensInternal.bind(this)),this.modelName=r,this.modelSpec=t,this.chatFormatter=s??"chatml"}*encodeHarmonyChatGenerator(e,n){let i=this.specialTokensEncoder.get(Ga),r=this.specialTokensEncoder.get(Ua),t=this.specialTokensEncoder.get(qa),s=this.specialTokensEncoder.get(Va),o=this.specialTokensEncoder.get(Ka),l=this.specialTokensEncoder.get(Ha),c=this.specialTokensEncoder.get(Wa);if(i===void 0||r===void 0||t===void 0||s===void 0||o===void 0||l===void 0||c===void 0)throw new Error("Harmony chat format requires dedicated special tokens.");let u=h=>h.length>0?this.encode(h):[],g=h=>{switch(h){case"<|return|>":return s;case"<|call|>":return o;default:return t}};for(let h of e){if(h.content===void 0)throw new Error("Content must be defined for all messages.");let C=h.name??h.role??"user";yield[i],yield u(C);let w=h.recipient&&(h.recipientPlacement==="role"||!h.channel),N=h.recipient&&!w;w&&(yield u(` to=${h.recipient}`)),h.channel&&(yield[l],yield u(h.channel),N&&(yield u(` to=${h.recipient}`))),h.constraint&&(yield[c],yield u(h.constraint)),yield[r],yield*this.encodeGenerator(h.content,n),yield[g(h.terminator)]}let f=n?.primeWithAssistantResponse??"assistant";f.length>0&&(yield[i],yield u(f))}static getEncodingApi(e,n){let i=hi(e,n);return new fi(i)}static getEncodingApiForModel(e,n,i){let r=Mt[e]??"o200k_base",t=hi(r,n);return new fi({...t,modelName:e,modelSpec:i})}processSpecialTokens({allowedSpecial:e,disallowedSpecial:n}={}){let i;if(e==="all"||e?.has("all")){e=new Set(this.specialTokensSet);let r=e;if(n==="all")throw new Error('allowedSpecial and disallowedSpecial cannot both be set to "all".');typeof n=="object"?n.forEach(t=>r.delete(t)):n=new Set}if(!n||n==="all"||n.has("all")){n=new Set(this.specialTokensSet);let r=n;e?.size?(e.forEach(t=>r.delete(t)),n.forEach(t=>e.delete(t)),n.size>0&&(i=pi(n))):i=this.allSpecialTokenRegex}return{allowedSpecial:e,regexPattern:i}}encodeGenerator(e,n){let i=n?this.processSpecialTokens(n):this.defaultSpecialTokenConfig;if(i.regexPattern){let r=e.match(i.regexPattern);if(r!==null)throw new Error(`Disallowed special token found: ${r[0]}`)}return this.bytePairEncodingCoreProcessor.encodeNativeGenerator(e,i.allowedSpecial)}encode(e,n){let i=n?this.processSpecialTokens(n):this.defaultSpecialTokenConfig;if(i.regexPattern){let r=e.match(i.regexPattern);if(r!==null)throw new Error(`Disallowed special token found: ${r[0]}`)}return this.bytePairEncodingCoreProcessor.encodeNative(e,i.allowedSpecial)}*encodeChatGenerator(e,n=this.modelName,i){if(!n)throw new Error("Model name must be provided either during initialization or passed in to the method.");let r=jt[n];if(!r)throw new Error(`Model '${n}' does not support chat.`);if(this.chatFormatter==="harmony"){yield*this.encodeHarmonyChatGenerator(e,i);return}let t=this.specialTokensEncoder.get(ma),s=this.specialTokensEncoder.get(pa);if(t===void 0||s===void 0)throw new Error(`Model '${n}' does not support chat.`);let o=new Set([Ue]),{messageSeparator:l,roleSeparator:c}=r,u=l.length>0?this.encode(l):[],g=c.length>0?this.encode(c,{allowedSpecial:o}):[],f=new Map;for(let{role:C="system",name:w=C,content:N}of e){if(N===void 0)throw new Error("Content must be defined for all messages.");yield[t];let F=f.get(w)??this.encode(w);f.set(w,F),yield F,g.length>0&&(yield g),yield*this.encodeGenerator(N,i),yield[s],yield u}let h=i?.primeWithAssistantResponse??"assistant";h.length>0&&(yield[t],yield*this.encodeGenerator(h,i)),g.length>0&&(yield g)}encodeChat(e,n=this.modelName,i){return[...this.encodeChatGenerator(e,n,i)].flat()}isWithinTokenLimit(e,n,i){let r=typeof e=="string"?this.encodeGenerator(e,i):this.encodeChatGenerator(e,void 0,i),t=0;for(let s of r)if(t+=s.length,t>n)return!1;return t}countTokens(e,n){if(typeof e=="string"){let t=n?this.processSpecialTokens(n):this.defaultSpecialTokenConfig;if(t.regexPattern){let s=e.match(t.regexPattern);if(s!==null)throw new Error(`Disallowed special token found: ${s[0]}`)}return this.bytePairEncodingCoreProcessor.countNative(e,t.allowedSpecial)}let i=this.encodeChatGenerator(e,void 0,n),r=0;for(let t of i)r+=t.length;return r}countStringTokens(e){return e?this.bytePairEncodingCoreProcessor.countNative(e):0}countChatCompletionTokensInternal(e){return Rt(e,n=>this.countStringTokens(n))}setMergeCacheSize(e){this.bytePairEncodingCoreProcessor.setMergeCacheSize(e)}clearMergeCache(){this.bytePairEncodingCoreProcessor.clearMergeCache()}decode(e){return this.bytePairEncodingCoreProcessor.decodeNative(e)}*decodeGenerator(e){let n=this.bytePairEncodingCoreProcessor.decodeNativeGenerator(e),i="";for(let r of n)i+=typeof r=="string"?r:Ca.decode(r,{stream:!0}),!(i.length===0||di(i))&&(yield i,i="");i.length>0&&(yield i)}async*decodeAsyncGenerator(e){let n=this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(e),i="";for await(let r of n)i+=typeof r=="string"?r:Ca.decode(r,{stream:!0}),!(i.length===0||di(i))&&(yield i,i="");i.length>0&&(yield i)}async decodeAsync(e){let n=this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(e),i="";for await(let r of n)i+=typeof r=="string"?r:Ca.decode(r,{stream:!0});return i}estimateCost(e,n=this.modelSpec){if(!n)throw new Error("Model spec must be provided either during initialization or passed in to the method.");if(!n.price_data)throw new Error(`No cost information available for model: ${n.name}`);let i=n.price_data,r={},t=e/1e6;return i.main&&(r.main={input:i.main.input&&i.main.input*t,output:i.main.output&&i.main.output*t,cached_input:i.main.cached_input&&i.main.cached_input*t,cached_output:i.main.cached_output&&i.main.cached_output*t}),i.batch&&(r.batch={input:i.batch.input&&i.batch.input*t,output:i.batch.output&&i.batch.output*t,cached_input:i.batch.cached_input&&i.batch.cached_input*t,cached_output:i.batch.cached_output&&i.batch.cached_output*t}),r}};var Vt=["!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~",[161],[162],[163],[164],[165],[166],[167],[168],[169],[170],[171],[172],[174],[175],[176],[177],[178],[179],[180],[181],[182],[183],[184],[185],[186],[187],[188],[189],[190],[191],[192],[193],[194],[195],[196],[197],[198],[199],[200],[201],[202],[203],[204],[205],[206],[207],[208],[209],[210],[211],[212],[213],[214],[215],[216],[217],[218],[219],[220],[221],[222],[223],[224],[225],[226],[227],[228],[229],[230],[231],[232],[233],[234],[235],[236],[237],[238],[239],[240],[241],[242],[243],[244],[245],[246],[247],[248],[249],[250],[251],[252],[253],[254],[255],"\0","","","","","","","\x07","\b"," ",`
14
+ `},xs={messageSeparator:"",roleSeparator:Ue},jt=Object.fromEntries(Pt.flatMap(a=>a.startsWith("gpt-3.5")?[[a,Is]]:[[a,xs]]));var Ra=new RegExp("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+","gu"),Dt=String.raw`'(?:[sS]|[dD]|[mM]|[tT]|[lL][lL]|[vV][eE]|[rR][eE])`,Ot=String.raw`(?:${Dt})?`,Ms=String.raw`${Dt}|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s+$|\s*[\r\n]|\s+(?!\S)|\s`,Nt=new RegExp(Ms,"gu"),js=String.raw`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+${Ot}|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*${Ot}|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`,wn=new RegExp(js,"gu");function Lt(a){return{tokenSplitRegex:Nt,bytePairRankDecoder:a,specialTokensEncoder:new Map([[he,100257],[Fe,100258],[Ge,100259],[qe,100260],[pa,100264],[ga,100265],[Ue,100266],[He,100276]])}}var Os=[[he,199999],[Fe,2e5],[Ge,200001],[qe,200002],[pa,200003],[ga,200004],[Ue,200005],[He,200006]],Ds=()=>new Map(Os);function Bt(a){return{tokenSplitRegex:wn,bytePairRankDecoder:a,specialTokensEncoder:Ds()}}var Ns=200013,Ls=201088,Bs=[[ui,199998],[he,199999],["<|reserved_200000|>",2e5],["<|reserved_200001|>",200001],[Va,200002],[Wa,200003],["<|reserved_200004|>",200004],[Ha,200005],[Ga,200006],[qa,200007],[Ua,200008],["<|reserved_200009|>",200009],["<|reserved_200010|>",200010],["<|reserved_200011|>",200011],[Ka,200012]];function Ft(a){let e=new Map(Bs);for(let n=Ns;n<Ls;n+=1)e.set(`<|reserved_${n}|>`,n);return e.set(He,200018),{tokenSplitRegex:wn,bytePairRankDecoder:a,specialTokensEncoder:e,chatFormatter:"harmony"}}function Gt(a){return{expectedVocabularySize:50281,tokenSplitRegex:Ra,bytePairRankDecoder:a,specialTokensEncoder:new Map([[he,50256]])}}function qt(a){return{tokenSplitRegex:Ra,bytePairRankDecoder:a,specialTokensEncoder:new Map([[he,50256],[Fe,50281],[Ge,50282],[qe,50283]])}}function Ut(a){return{expectedVocabularySize:50257,tokenSplitRegex:Ra,bytePairRankDecoder:a,specialTokensEncoder:new Map([[he,50256]])}}function hi(a,e){let n=e(a);switch(a.toLowerCase()){case"gpt2":case"r50k_base":return Ut(n);case"p50k_base":return Gt(n);case"p50k_edit":return qt(n);case"cl100k_base":return Lt(n);case"o200k_base":return Bt(n);case"o200k_harmony":return Ft(n);default:throw new Error(`Unknown encoding name: ${a}`)}}var Ht=class fi{static EndOfPrompt=He;static EndOfText=he;static FimMiddle=Ge;static FimPrefix=Fe;static FimSuffix=qe;modelName;modelSpec;bytePairEncodingCoreProcessor;specialTokensEncoder;specialTokensSet;allSpecialTokenRegex;defaultSpecialTokenConfig;chatFormatter;countChatCompletionTokens;vocabularySize;constructor({bytePairRankDecoder:e,specialTokensEncoder:n,expectedVocabularySize:i,modelName:r,modelSpec:t,chatFormatter:s,...o}){this.specialTokensEncoder=n,this.specialTokensSet=new Set(this.specialTokensEncoder.keys()),this.allSpecialTokenRegex=pi(this.specialTokensSet),this.bytePairEncodingCoreProcessor=new Tt({bytePairRankDecoder:e,specialTokensEncoder:n,...o}),this.defaultSpecialTokenConfig=this.processSpecialTokens();let l=Math.max(e.length-1,Et(n));if(this.vocabularySize=this.bytePairEncodingCoreProcessor.mergeableBytePairRankCount+n.size,i!==void 0){if(this.vocabularySize!==i)throw new Error("The number of mergeable tokens and special tokens must be equal to expectedVocabularySize.");if(l!==i-1)throw new Error(`The model encodings are invalid. The maximum token value must be equal to expectedVocabularySize - 1. Currently ${l}, expected ${i-1}`)}this.encode=this.encode.bind(this),this.decode=this.decode.bind(this),this.encodeGenerator=this.encodeGenerator.bind(this),this.decodeGenerator=this.decodeGenerator.bind(this),this.decodeAsyncGenerator=this.decodeAsyncGenerator.bind(this),this.decodeAsync=this.decodeAsync.bind(this),this.isWithinTokenLimit=this.isWithinTokenLimit.bind(this),this.encodeChat=this.encodeChat.bind(this),this.encodeChatGenerator=this.encodeChatGenerator.bind(this),this.countTokens=this.countTokens.bind(this),this.setMergeCacheSize=this.setMergeCacheSize.bind(this),this.clearMergeCache=this.clearMergeCache.bind(this),this.estimateCost=this.estimateCost.bind(this),t?.supported_features?.includes("function_calling")&&(this.countChatCompletionTokens=this.countChatCompletionTokensInternal.bind(this)),this.modelName=r,this.modelSpec=t,this.chatFormatter=s??"chatml"}*encodeHarmonyChatGenerator(e,n){let i=this.specialTokensEncoder.get(Ga),r=this.specialTokensEncoder.get(Ua),t=this.specialTokensEncoder.get(qa),s=this.specialTokensEncoder.get(Va),o=this.specialTokensEncoder.get(Ka),l=this.specialTokensEncoder.get(Ha),c=this.specialTokensEncoder.get(Wa);if(i===void 0||r===void 0||t===void 0||s===void 0||o===void 0||l===void 0||c===void 0)throw new Error("Harmony chat format requires dedicated special tokens.");let u=h=>h.length>0?this.encode(h):[],g=h=>{switch(h){case"<|return|>":return s;case"<|call|>":return o;default:return t}};for(let h of e){if(h.content===void 0)throw new Error("Content must be defined for all messages.");let C=h.name??h.role??"user";yield[i],yield u(C);let w=h.recipient&&(h.recipientPlacement==="role"||!h.channel),N=h.recipient&&!w;w&&(yield u(` to=${h.recipient}`)),h.channel&&(yield[l],yield u(h.channel),N&&(yield u(` to=${h.recipient}`))),h.constraint&&(yield[c],yield u(h.constraint)),yield[r],yield*this.encodeGenerator(h.content,n),yield[g(h.terminator)]}let f=n?.primeWithAssistantResponse??"assistant";f.length>0&&(yield[i],yield u(f))}static getEncodingApi(e,n){let i=hi(e,n);return new fi(i)}static getEncodingApiForModel(e,n,i){let r=Mt[e]??"o200k_base",t=hi(r,n);return new fi({...t,modelName:e,modelSpec:i})}processSpecialTokens({allowedSpecial:e,disallowedSpecial:n}={}){let i;if(e==="all"||e?.has("all")){e=new Set(this.specialTokensSet);let r=e;if(n==="all")throw new Error('allowedSpecial and disallowedSpecial cannot both be set to "all".');typeof n=="object"?n.forEach(t=>r.delete(t)):n=new Set}if(!n||n==="all"||n.has("all")){n=new Set(this.specialTokensSet);let r=n;e?.size?(e.forEach(t=>r.delete(t)),n.forEach(t=>e.delete(t)),n.size>0&&(i=pi(n))):i=this.allSpecialTokenRegex}return{allowedSpecial:e,regexPattern:i}}encodeGenerator(e,n){let i=n?this.processSpecialTokens(n):this.defaultSpecialTokenConfig;if(i.regexPattern){let r=e.match(i.regexPattern);if(r!==null)throw new Error(`Disallowed special token found: ${r[0]}`)}return this.bytePairEncodingCoreProcessor.encodeNativeGenerator(e,i.allowedSpecial)}encode(e,n){let i=n?this.processSpecialTokens(n):this.defaultSpecialTokenConfig;if(i.regexPattern){let r=e.match(i.regexPattern);if(r!==null)throw new Error(`Disallowed special token found: ${r[0]}`)}return this.bytePairEncodingCoreProcessor.encodeNative(e,i.allowedSpecial)}*encodeChatGenerator(e,n=this.modelName,i){if(!n)throw new Error("Model name must be provided either during initialization or passed in to the method.");let r=jt[n];if(!r)throw new Error(`Model '${n}' does not support chat.`);if(this.chatFormatter==="harmony"){yield*this.encodeHarmonyChatGenerator(e,i);return}let t=this.specialTokensEncoder.get(pa),s=this.specialTokensEncoder.get(ga);if(t===void 0||s===void 0)throw new Error(`Model '${n}' does not support chat.`);let o=new Set([Ue]),{messageSeparator:l,roleSeparator:c}=r,u=l.length>0?this.encode(l):[],g=c.length>0?this.encode(c,{allowedSpecial:o}):[],f=new Map;for(let{role:C="system",name:w=C,content:N}of e){if(N===void 0)throw new Error("Content must be defined for all messages.");yield[t];let F=f.get(w)??this.encode(w);f.set(w,F),yield F,g.length>0&&(yield g),yield*this.encodeGenerator(N,i),yield[s],yield u}let h=i?.primeWithAssistantResponse??"assistant";h.length>0&&(yield[t],yield*this.encodeGenerator(h,i)),g.length>0&&(yield g)}encodeChat(e,n=this.modelName,i){return[...this.encodeChatGenerator(e,n,i)].flat()}isWithinTokenLimit(e,n,i){let r=typeof e=="string"?this.encodeGenerator(e,i):this.encodeChatGenerator(e,void 0,i),t=0;for(let s of r)if(t+=s.length,t>n)return!1;return t}countTokens(e,n){if(typeof e=="string"){let t=n?this.processSpecialTokens(n):this.defaultSpecialTokenConfig;if(t.regexPattern){let s=e.match(t.regexPattern);if(s!==null)throw new Error(`Disallowed special token found: ${s[0]}`)}return this.bytePairEncodingCoreProcessor.countNative(e,t.allowedSpecial)}let i=this.encodeChatGenerator(e,void 0,n),r=0;for(let t of i)r+=t.length;return r}countStringTokens(e){return e?this.bytePairEncodingCoreProcessor.countNative(e):0}countChatCompletionTokensInternal(e){return Rt(e,n=>this.countStringTokens(n))}setMergeCacheSize(e){this.bytePairEncodingCoreProcessor.setMergeCacheSize(e)}clearMergeCache(){this.bytePairEncodingCoreProcessor.clearMergeCache()}decode(e){return this.bytePairEncodingCoreProcessor.decodeNative(e)}*decodeGenerator(e){let n=this.bytePairEncodingCoreProcessor.decodeNativeGenerator(e),i="";for(let r of n)i+=typeof r=="string"?r:Ca.decode(r,{stream:!0}),!(i.length===0||di(i))&&(yield i,i="");i.length>0&&(yield i)}async*decodeAsyncGenerator(e){let n=this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(e),i="";for await(let r of n)i+=typeof r=="string"?r:Ca.decode(r,{stream:!0}),!(i.length===0||di(i))&&(yield i,i="");i.length>0&&(yield i)}async decodeAsync(e){let n=this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(e),i="";for await(let r of n)i+=typeof r=="string"?r:Ca.decode(r,{stream:!0});return i}estimateCost(e,n=this.modelSpec){if(!n)throw new Error("Model spec must be provided either during initialization or passed in to the method.");if(!n.price_data)throw new Error(`No cost information available for model: ${n.name}`);let i=n.price_data,r={},t=e/1e6;return i.main&&(r.main={input:i.main.input&&i.main.input*t,output:i.main.output&&i.main.output*t,cached_input:i.main.cached_input&&i.main.cached_input*t,cached_output:i.main.cached_output&&i.main.cached_output*t}),i.batch&&(r.batch={input:i.batch.input&&i.batch.input*t,output:i.batch.output&&i.batch.output*t,cached_input:i.batch.cached_input&&i.batch.cached_input*t,cached_output:i.batch.cached_output&&i.batch.cached_output*t}),r}};var Vt=["!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~",[161],[162],[163],[164],[165],[166],[167],[168],[169],[170],[171],[172],[174],[175],[176],[177],[178],[179],[180],[181],[182],[183],[184],[185],[186],[187],[188],[189],[190],[191],[192],[193],[194],[195],[196],[197],[198],[199],[200],[201],[202],[203],[204],[205],[206],[207],[208],[209],[210],[211],[212],[213],[214],[215],[216],[217],[218],[219],[220],[221],[222],[223],[224],[225],[226],[227],[228],[229],[230],[231],[232],[233],[234],[235],[236],[237],[238],[239],[240],[241],[242],[243],[244],[245],[246],[247],[248],[249],[250],[251],[252],[253],[254],[255],"\0","","","","","","","\x07","\b"," ",`
15
15
  `,"\v","\f","\r","","","","","","","","","","","","","","\x1B","","","",""," ","\x7F",[128],[129],[130],[131],[132],[133],[134],[135],[136],[137],[138],[139],[140],[141],[142],[143],[144],[145],[146],[147],[148],[149],[150],[151],[152],[153],[154],[155],[156],[157],[158],[159],[160],[173]," "," ","in","er"," t"," a","en","on","re"," s","at","or","es"," ","an"," "," d","he"," c"," p","is","ar","it",`
16
16
 
17
17
  `,"al",[224,164],"le","ou"," m"," f"," w"," b","as","ing"," the","ic","et"," o","ion","ed","el"," n","ro","ent",[32,208],"nd","st",[225,131],"\u0430"," l"," in",`;
@@ -3271,7 +3271,7 @@ ${" ".repeat(e)}}`;default:return"any"}}function ws(a){let e=["namespace functio
3271
3271
  `," surfers"," \u05DE\u05EA\u05D0\u05D9\u05DD",`')):
3272
3272
  `,".apk","\u1EDBm"," \u05D4\u05DE\u05D6"," brincar"," AMOLED"," nate"," ayaan"," phosphorylation"," \u0985\u09AA\u09C7\u0995\u09CD\u09B7"," Eats"," Converse","\u094D\u092F\u0942\u091F\u0930","Traveler","inggi","\u05D5\u05E8\u05D2"," almoh"," \u0625\u0641\u0631\u064A\u0642\u064A\u0627"," Magazin"," Arme","\u0E23\u0E01\u0E34\u0E08"," a\u015Fk","-lines"," \u0627\u0644\u0623\u0645\u0627\u0643\u0646","\u516C\u5E73"," kv\xE4ll"," mlx"," \u092E\u0928\u094D\u0924\u094D\u0930\u0940","\u10D0\u10D5\u10D8\u10D0"," teks"," lumea"," legyen"," zwing"," boost"," J\xFA","\u5F69\u7968\u8BBA\u575B"," \u0434\u044D"," \u043E\u0448\u0438\u0431\u043E\u043A","\u0561\u056F\u0561\u0576\u056B","\u0E24\u0E29\u0E20\u0E32\u0E04\u0E21","ebwa"," tipu"," domini"," Gurgaon","combat"," Resist"," \u0930\u094B\u091C\u0917\u093E\u0930","strategie","\u0A17\u0A40","(locator","MONTH"," atlik"," \uB79C","\u0627\u0644\u0633\u064A"," rzecz"," W\xE4"," deportivos"," Facilit","-able"," evitare"," thong"," \u6D3B","\uBB34\uB8CC"," mace"," Extraordinary","\u0927\u093E"," konflik"," livelihoods"," \u043A\u043E\u043C\u043F\u043E\u043D\u0435\u043D\u0442\u043E\u0432"," kuitenkaan","\u5E78\u8FD0\u98DE\u8247","Corpus"," besch\xE4d"," \u05D3\u05D5\u05D3"," dentant"," nweta"," heath"," \u0936\u0941\u0915\u094D\u0930\u092C\u093E\u0930"," mnemonic",".Filters",`','".$`," \u0905\u0938\u094D\u0924","isite"," asawa","\u5E74\u6708"," gotas"," \u0C2A\u0C28\u0C3F\u0C1A","Dek"," Pact","'agence","fei\xE7o"," B\xF6yle","venteen","notif"," \u0410\u043D\u0434\u0440\u0435\u0439","\u591A\u5E74","mittedly","\u8CA1\u5E03"," \u043A\u043E\u043C\u043F\u0430\u043A\u0442","trk","\u0928\u094D\u0926\u094D\u0930\u0940\u092F"," miesto"," Kait"," \u0CAC\u0CB8"," spec"," Injector"," logarith","entions"," \u0447\u0435\u043B","Collateral"," confes","\u093F\u0939\u0930"," sufri\xF3","CRI"," tuge","urllib"," k\u013C"," verpleeg"," application's","(Constructor","\u09BE\u09B2\u09BE"," vulputate"," \u043E\u043F\u0443\u0445"," Nordrhein","allage","\xEDsimas","ornado","\u0438\u0434\u0435\u0440","m\xE9nyek","Qua"," pelicula","\uC131\uACFC","\u3011-"," hemi"," touristes"," absentee"," Nether",".PAGE"," i\u015Fg\xE4r"," efectivos"," saav"," ","flashdata"," sembler"," ikuku"," \u099F\u09BE\u0987",` \u201D
3273
3273
  `,"\u5F69\u7968\u5E73\u53F0\u5F00\u6237","\u65C1",".\u0413","uckles"," \u0627\u0644\u062E\u0627\u0631","\u0432\u043E\u043B\u044C"," \u043D\u0435\u0432\u043E\u0437\u043C\u043E\u0436",".gradient"," ebile"," Athlete"," \u0442\u04B1\u0440\u0493\u0430\u043D"," Syrie",".processing"," Consultancy"," thrills","\u0642\u0631\u0627\u0621","(varargin","k\xFCnfte"," \u5F6D","-knit","riedenheit"," t\xE4nka","']//","udies","hacer"," Th\xE9\xE2tre","av\u7535\u5F71"," Armada","-techn","ibris","veli"," razloga","-Hop"," \u05DE\u05D7\u05D3\u05E9"," Bloggers"," controversies"," \u0C1A\u0C30\u0C4D\u0C2F"," \u0D2A\u0D4D\u0D30\u0D2E\u0D41\u0D16","_unc","werfen"," \u043B\u043E\u043C"," \u043F\u043E\u043A\u0443\u043F\u0430\u0442\u0435\u043B\u0435\u0439"," \u0433\u043B\u0430\u0437\u0430\u043C\u0438"," \u05D1\u05E2\u05D5\u05D3"," nanot"," ws"," \u043A\u0435\u0437\u0434\u0435\u0441","):("," Produit","Aircraft","iffen"," patrones"," par\xE2metros","Cursos"," cocos"],Kt=Vt.concat(Wt);Vt.length=0;Wt.length=0;var Fs=Ht.getEncodingApi("o200k_base",()=>Kt),{decode:xc,decodeAsyncGenerator:Mc,decodeGenerator:jc,encode:Oc,encodeGenerator:Dc,isWithinTokenLimit:Nc,countTokens:$t,encodeChat:Lc,encodeChatGenerator:Bc,vocabularySize:Fc,setMergeCacheSize:Gc,clearMergeCache:qc,estimateCost:Uc}=Fs;var Gs=/[A-Za-z]+|\d+|[^\sA-Za-z\d]|\n[ \t]*/g;function bi(a,e=4){if(!a)return 0;let n=a.length/e,i=0;for(let r of a.match(Gs)??[]){let t=/^[A-Za-z]/.test(r)?6:/^\d/.test(r)?3:r[0]===`
3274
- `?16:1;i+=Math.max(1,Math.ceil(r.length/t))}return Math.max(1,Math.round(Math.max(n,i)))}function ce(a,e){if(!a)return{tokens:0,exact:e.tokenizer!=="heuristic"};if(e.tokenizer==="o200k")try{return{tokens:$t(a),exact:!0}}catch{}return{tokens:bi(a,e.charsPerToken),exact:!1}}var qs={debugging:[/\b(error|exception|traceback|stack ?trace|crash(es|ed|ing)?|segfault|undefined is not|cannot read propert|null pointer|stack overflow)\b/],coding:[/\b(code|function|method|class|script|refactor|implement|api|endpoint|regex|sql|query|unit tests?|typescript|javascript|python|java|rust|golang|c\+\+|react|component|compile|deploy)\b/],editing:[/\b(proofread|rewrite|rephrase|reword|edit|polish|improve|fix (the )?grammar|make (it|this) (sound|more|less)|tone of)\b/],summarizing:[/\b(summari[sz]e|summary|tl;?dr|key (points|takeaways)|condense|recap|gist)\b/],translation:[/\btranslat(e|ion)\b|\b(in|into) (english|hindi|spanish|french|german|japanese|chinese|arabic|portuguese|italian|korean|russian)\b/],analysis:[/\b(analy[sz]e|analysis|dataset|csv|spreadsheet|table|chart|trend|average|median|correlat|statistics?|metrics?)\b/],brainstorming:[/\b(brainstorm|ideas? for|suggest (some|a few)|come up with|names? for|list of ideas)\b/],writing:[/\b(write|draft|compose|blog|essay|article|email|cover letter|post|story|poem|speech|announcement|outline)\b/],research:[/^(what|why|how|when|where|who|which|is|are|can|does|do|should|explain|compare|tell me)\b/,/\b(explain|difference between|compare|pros and cons|research|sources?)\b/]},Us=/\b(bug|broken|fails?|failing|doesn'?t work|not working|wrong output|fix (this|the|my|it))\b/,Jt=["debugging","translation","summarizing","editing","analysis","coding","brainstorming","writing","research"],Hs=/```[\s\S]*?```|~~~[\s\S]*?~~~/g,ki=/^\s*(def |class |function |const |let |var |import |from \S+ import|#include|public |private |return\b|if\s*\(|for\s*\(|while\s*\(|\}|\{|<\/?[a-z][\w-]*[ >])|;\s*$/,Sn=/^\s*(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}|\[\d{2}:\d{2}:\d{2}|\[?(INFO|WARN|WARNING|ERROR|DEBUG|TRACE|FATAL)\]?\b|at .+[(:]\d+|File ".+", line \d+|Traceback \(most recent call last\)|\w+(Error|Exception)(: |$)|Caused by:)/,Vs=/^\s*([^,\t\n]+[,\t]){2,}[^,\t\n]+\s*$|^\s*[[{]\s*"\w+"\s*:/,Ws=/\b(in|under|within|at most|max(imum)?|no more than|about|around|exactly|only)\s+\d+\s*(words?|sentences?|bullets?|bullet points|lines?|paragraphs?|points?|items?|characters?|chars?|tokens?)\b|\b(\d+|one|two|three|four|five|six|seven|eight|nine|ten)[- ](words?|sentences?|bullets?|bullet points|lines?|paragraphs?|points?|ideas?|items?)\b|\b(brief(ly)?|concise(ly)?|short(ly)?|succinct(ly)?|tl;?dr|one[- ]liner|yes or no|just the (answer|code|diff))\b/,Ks=/\b(full|entire|whole|complete)\s+(file|code|script|program|document|text|essay)\b|\brewrite (the )?(whole|entire)\b/;function $s(a,e){let n=a.filter(i=>i.trim());return n.length?n.filter(i=>e.test(i)).length/n.length:0}function $a(a){let e=a.replace(/^\s+/,""),n=a.length-e.length,i=/\n\s*\n|:\s*\n/.exec(e);if(!i)return{instruction:a,material:""};let r=n+i.index+i[0].length;return{instruction:a.slice(0,r),material:a.slice(r)}}function ga(a){let e=a.split(`
3274
+ `?16:1;i+=Math.max(1,Math.ceil(r.length/t))}return Math.max(1,Math.round(Math.max(n,i)))}function ce(a,e){if(!a)return{tokens:0,exact:e.tokenizer!=="heuristic"};if(e.tokenizer==="o200k")try{return{tokens:$t(a),exact:!0}}catch{}return{tokens:bi(a,e.charsPerToken),exact:!1}}var qs={debugging:[/\b(error|exception|traceback|stack ?trace|crash(es|ed|ing)?|segfault|undefined is not|cannot read propert|null pointer|stack overflow)\b/],coding:[/\b(code|function|method|class|script|refactor|implement|api|endpoint|regex|sql|query|unit tests?|typescript|javascript|python|java|rust|golang|c\+\+|react|component|compile|deploy)\b/],editing:[/\b(proofread|rewrite|rephrase|reword|edit|polish|improve|fix (the )?grammar|make (it|this) (sound|more|less)|tone of)\b/],summarizing:[/\b(summari[sz]e|summary|tl;?dr|key (points|takeaways)|condense|recap|gist)\b/],translation:[/\btranslat(e|ion)\b|\b(in|into) (english|hindi|spanish|french|german|japanese|chinese|arabic|portuguese|italian|korean|russian)\b/],analysis:[/\b(analy[sz]e|analysis|dataset|csv|spreadsheet|table|chart|trend|average|median|correlat|statistics?|metrics?)\b/],brainstorming:[/\b(brainstorm|ideas? for|suggest (some|a few)|come up with|names? for|list of ideas)\b/],writing:[/\b(write|draft|compose|blog|essay|article|email|cover letter|post|story|poem|speech|announcement|outline)\b/],research:[/^(what|why|how|when|where|who|which|is|are|can|does|do|should|explain|compare|tell me)\b/,/\b(explain|difference between|compare|pros and cons|research|sources?)\b/]},Us=/\b(bug|broken|fails?|failing|doesn'?t work|not working|wrong output|fix (this|the|my|it))\b/,Jt=["debugging","translation","summarizing","editing","analysis","coding","brainstorming","writing","research"],Hs=/```[\s\S]*?```|~~~[\s\S]*?~~~/g,ki=/^\s*(def |class |function |const |let |var |import |from \S+ import|#include|public |private |return\b|if\s*\(|for\s*\(|while\s*\(|\}|\{|<\/?[a-z][\w-]*[ >])|;\s*$/,Sn=/^\s*(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}|\[\d{2}:\d{2}:\d{2}|\[?(INFO|WARN|WARNING|ERROR|DEBUG|TRACE|FATAL)\]?\b|at .+[(:]\d+|File ".+", line \d+|Traceback \(most recent call last\)|\w+(Error|Exception)(: |$)|Caused by:)/,Vs=/^\s*([^,\t\n]+[,\t]){2,}[^,\t\n]+\s*$|^\s*[[{]\s*"\w+"\s*:/,Ws=/\b(in|under|within|at most|max(imum)?|no more than|about|around|exactly|only)\s+\d+\s*(words?|sentences?|bullets?|bullet points|lines?|paragraphs?|points?|items?|characters?|chars?|tokens?)\b|\b(\d+|one|two|three|four|five|six|seven|eight|nine|ten)[- ](words?|sentences?|bullets?|bullet points|lines?|paragraphs?|points?|ideas?|items?)\b|\b(brief(ly)?|concise(ly)?|short(ly)?|succinct(ly)?|tl;?dr|one[- ]liner|yes or no|just the (answer|code|diff))\b/,Ks=/\b(full|entire|whole|complete)\s+(file|code|script|program|document|text|essay)\b|\brewrite (the )?(whole|entire)\b/;function $s(a,e){let n=a.filter(i=>i.trim());return n.length?n.filter(i=>e.test(i)).length/n.length:0}function $a(a){let e=a.replace(/^\s+/,""),n=a.length-e.length,i=/\n\s*\n|:\s*\n/.exec(e);if(!i)return{instruction:a,material:""};let r=n+i.index+i[0].length;return{instruction:a.slice(0,r),material:a.slice(r)}}function ha(a){let e=a.split(`
3275
3275
  `),n=a.match(Hs)??[],{instruction:i,material:r}=$a(a),t=i.toLowerCase().trim(),s={hasCode:n.length>0||$s(e,ki)>.3,hasLogs:e.filter(u=>Sn.test(u)).length>=2,hasData:e.filter(u=>Vs.test(u)).length>=3,pastedTokens:bi(r),hasLengthLimit:Ws.test(a.toLowerCase()),asksForFullOutput:Ks.test(t)},o=new Map;for(let u of Jt){let g=qs[u],f=g.reduce((h,C)=>h+(C.test(t)?2:0),0);!f&&r&&(f=g.reduce((h,C)=>h+(C.test(r.slice(0,2e3).toLowerCase())?.5:0),0)),o.set(u,f)}(s.hasCode||s.hasLogs)&&Us.test(t)&&o.set("debugging",(o.get("debugging")??0)+2),s.hasLogs&&o.set("debugging",(o.get("debugging")??0)+2),s.hasCode&&o.set("coding",(o.get("coding")??0)+1.5),s.hasData&&o.set("analysis",(o.get("analysis")??0)+1);let l="chat",c=0;for(let u of Jt){let g=o.get(u)??0;g>c&&(l=u,c=g)}return{task:l,confidence:Math.min(1,c/4),signals:s}}var Yt=new Set(["chat","writing","brainstorming","research"]);var Js=[["number",/\b\d+(?:[.,]\d+)*%?/g],["url",/https?:\/\/\S+/g],["code",/`[^`\n]+`/g],["quote",/"[^"\n]{1,200}"|“[^”\n]{1,200}”/g],["negation",/\b(?:not|no|never|none|nothing|nor|without|cannot|can't|don't|doesn't|didn't|won't|wouldn't|shouldn't|isn't|aren't|wasn't|mustn't)\b/gi],["constraint",/\b(?:must|only|exactly|always|avoid|at least|at most|required|mandatory|strictly|unless|except)\b/gi]];function _n(a){let e=new Map;for(let[n,i]of Js)for(let r of a.match(i)??[]){let t=`${n}:${r.toLowerCase()}`;e.set(t,(e.get(t)??0)+1)}return e}function En(a,e,n="count"){let i=_n(a),r=_n(e),t=[];for(let[s,o]of i)(r.get(s)??0)<(n==="count"?o:1)&&t.push(s);return{ok:t.length===0,lost:t}}function yi(a,e){let n=_n(a),i=_n(e),r={};for(let t of n.keys()){let s=t.slice(0,t.indexOf(":")),[o,l]=r[s]??[0,0];r[s]=[o+1,l+(i.has(t)?1:0)]}return r}var Jc=new Set("please kindly thank thanks much lot advance hi hello hey there chatgpt claude gemini basically actually really very just simply quite literally totally definitely important worth noting note make sure order regard respect fact purpose event point time near future large number majority able wondering like love want need you your would could will can helpful assistant language model act possible mind think step breath deep take reasoning thought process work before answering answer show explain spite due lets let's the that this with for are was is it's its and".split(" "));var V=(a,e,n,i,r=!1)=>({re:new RegExp(a,"gim"),repl:e,name:n,level:i,reasoningOnly:r}),Ys=[V(String.raw`\bdue to the fact that\b`,"because","wordy phrasing","light"),V(String.raw`\bin spite of the fact that\b`,"although","wordy phrasing","light"),V(String.raw`\bin the event that\b`,"if","wordy phrasing","light"),V(String.raw`\bfor the purpose of\b`,"for","wordy phrasing","light"),V(String.raw`\bin order to\b`,"to","wordy phrasing","light"),V(String.raw`\bwith (?:regard|respect) to\b`,"about","wordy phrasing","light"),V(String.raw`\bat this point in time\b`,"now","wordy phrasing","light"),V(String.raw`\bin the near future\b`,"soon","wordy phrasing","light"),V(String.raw`\ba large number of\b`,"many","wordy phrasing","light"),V(String.raw`\bthe majority of\b`,"most","wordy phrasing","light"),V(String.raw`\b(is|are) able to\b`,"can","wordy phrasing","light"),V(String.raw`\bmake sure that\b`,"ensure","wordy phrasing","light"),V(String.raw`\b(?:let'?s|please)?\s*think (?:about (?:this|it) )?step[- ]by[- ]step\b[.,]?\s*`,"","step-by-step (model already reasons)","light",!0),V(String.raw`\btake a deep breath(?: and)?[.,]?\s*`,"","step-by-step (model already reasons)","light",!0),V(String.raw`\b(?:show|explain) your (?:reasoning|thought process|work) (?:step[- ]by[- ]step )?(?:before (?:answering|you answer))[.,]?\s*`,"","step-by-step (model already reasons)","light",!0),V(String.raw`^\s*(?:hi|hello|hey)(?: there)?(?: chatgpt| claude| gemini)?[,!.]?\s*`,"","politeness","balanced"),V(String.raw`(^|[.!?]\s+)(?:could|can|would|will) you (?:please )?(?:kindly )?([^.!?\n]*)\?`,"$1$2.","politeness","balanced"),V(String.raw`(^|[.!?]\s+)(?:could|can|would|will) you (?:please )?(?:kindly )?`,"$1","politeness","balanced"),V(String.raw`\bi would (?:really )?(?:like|love) (?:for )?you to\b\s*`,"","politeness","balanced"),V(String.raw`\bi (?:want|need) you to\b\s*`,"","politeness","balanced"),V(String.raw`\bplease\b,?\s*`,"","politeness","balanced"),V(String.raw`\bkindly\b\s*`,"","politeness","balanced"),V(String.raw`\bthank you(?: so much| very much| a lot)?(?: in advance)?[.!]?`,"","politeness","balanced"),V(String.raw`\bthanks(?: so much| very much| a lot)?(?: in advance)?[.!]?`,"","politeness","balanced"),V(String.raw`\b(?:it is|it's) (?:important|worth noting) (?:to note )?that\b\s*`,"","filler","balanced"),V(String.raw`\b(?:please )?note that\b\s*`,"","filler","balanced"),V(String.raw`\b(?:you are|act as|as) (?:an?|my) (?:ai|large language model|helpful assistant|ai assistant)[,.]?\s*`,"","filler","balanced"),V(String.raw`\bi was wondering if\b\s*`,"","filler","balanced"),V(String.raw`(?<!\bnot )\b(?:basically|actually|really|very|just|simply|quite|literally|totally|definitely)\b\s*`,"","hedges","aggressive"),V(String.raw`\b(?:if possible|if you can|if you don't mind)\b[,.]?\s*`,"","hedges","aggressive")],An={light:0,balanced:1,aggressive:2},Xs=/(```[\s\S]*?```|~~~[\s\S]*?~~~)/,Qs=new RegExp(["`[^`\\n]+`","https?:\\/\\/\\S+","<([A-Za-z][\\w-]*)[^>]*>[\\s\\S]*?<\\/\\1>",'"""[\\s\\S]*?"""'].join("|"),"g");function Zs(a){return/^\s*>/.test(a)||/^\s{4,}\S/.test(a)||ki.test(a)||Sn.test(a)}function eo(a,e){return e&&/^[A-Z]/.test(a)?e[0].toUpperCase()+e.slice(1):e}function ao(a,e,n){for(let i of Ys){if(An[i.level]>An[e.level]||i.reasoningOnly&&!e.profile.reasoning)continue;i.re.lastIndex=0;let r=a.replace(i.re,(...t)=>{let s=t[0],o=t.slice(1,-2),l=i.repl.replace(/\$(\d)/g,(u,g)=>o[Number(g)-1]??""),c=i.repl.startsWith("$1")?o[0]??"":"";return c+eo(s.slice(c.length),l.slice(c.length))});r!==a&&(n.push(i.name),a=r)}return a}function no(a,e){let n=new Set;return a.split(`
3276
3276
  `).map(i=>i.split(/(?<=[.!?])\s+/).filter(r=>{let t=r.replace(/\W+/g," ").trim().toLowerCase();return t.split(" ").length<4?!0:n.has(t)?(e.push("repeated sentence"),!1):(n.add(t),!0)}).join(" ")).join(`
3277
3277
  `)}function io(a){return a.replace(/[ \t]+/g," ").replace(/ +([,.;:!?])/g,"$1").replace(/([,;:])(?=[.!?])/g,"").replace(/^[ ,;:]+/gm,"").replace(/ +$/gm,"").replace(/\n{3,}/g,`
@@ -3283,13 +3283,13 @@ ${" ".repeat(e)}}`;default:return"any"}}function ws(a){let e=["namespace functio
3283
3283
  `)}function Zt(a){let e=a.split(`
3284
3284
  `).filter(n=>n.trim());return e.length>=3&&e.filter(n=>Sn.test(n)||er.test(n)).length/e.length>=.4}function ro(a,e){let n=/^(?:```|~~~)\s*([\w+-]*)/.exec(a)?.[1]?.toLowerCase()??"",i=a.replace(/^(?:```|~~~).*\n?/,"");if(/^(md|markdown|diff|patch|text|txt)$/.test(n)||/"""|'''|`[^`\n]*\n|<<-?\s*['"]?\w+/.test(i))return a;let r=a.replace(/[ \t]+$/gm,"").replace(/\n{4,}/g,`
3285
3285
 
3286
- `);return r!==a&&e.push("whitespace in code"),r}function ea(a,e){let n=[],{instruction:i}=$a(a),r=i.length,t=e.task==="debugging"&&e.level!=="light",s=0,o=[];for(let[g,f]of a.split(Xs).entries()){let h=s;if(s+=f.length,g%2===1){o.push(t&&Zt(f)?Qt(f,e.level,n):e.level==="light"?f:ro(f,n));continue}let C=f.slice(0,Math.max(0,r-h)),w=f.slice(C.length),N=w;t&&Zt(w)?N=Qt(w,e.level,n):Yt.has(e.task)&&(N=Xt(w,e,n)),o.push(Xt(C,e,n)+N)}let l=o.join(""),c=ce(a,e.profile).tokens,u=ce(l,e.profile).tokens;return u>=c||!l.trim()?{text:a,rules:[],originalTokens:c,tokens:c,saved:0}:{text:l,rules:[...new Set(n)],originalTokens:c,tokens:u,saved:c-u}}function so(a,e="input"){let n=a.pricing;return n?(e==="output"?n.outputPerMTok:e==="cached"?n.cachedInputPerMTok??n.inputPerMTok:n.inputPerMTok)/1e6:null}function ha(a,e,n="input"){let i=so(e,n);return i===null?null:a*i}function Pa(a,e,n="input"){let i=ha(a,e,n);return{tokens:a,usd:i,usdPer1k:i===null?null:i*1e3}}function ar(a){if(a==null||!Number.isFinite(a))return"n/a";let e=Math.abs(a),n=a<0?"-":"";if(e===0)return"$0";if(e>=100)return`${n}$${e.toLocaleString("en-US",{maximumFractionDigits:0})}`;if(e>=.1)return`${n}$${e.toFixed(2)}`;let i=Math.min(8,Math.max(2,1-Math.floor(Math.log10(e))));return`${n}$${e.toFixed(i).replace(/(\.\d*?[1-9])0+$/,"$1")}`}function oo(a){let e="",n=!1;for(let i=0;i<a.length;i++){let r=a[i];n?(e+=r,r==="\\"?e+=a[++i]??"":r==='"'&&(n=!1)):r==='"'?(n=!0,e+=r):r!==" "&&r!==`
3286
+ `);return r!==a&&e.push("whitespace in code"),r}function ea(a,e){let n=[],{instruction:i}=$a(a),r=i.length,t=e.task==="debugging"&&e.level!=="light",s=0,o=[];for(let[g,f]of a.split(Xs).entries()){let h=s;if(s+=f.length,g%2===1){o.push(t&&Zt(f)?Qt(f,e.level,n):e.level==="light"?f:ro(f,n));continue}let C=f.slice(0,Math.max(0,r-h)),w=f.slice(C.length),N=w;t&&Zt(w)?N=Qt(w,e.level,n):Yt.has(e.task)&&(N=Xt(w,e,n)),o.push(Xt(C,e,n)+N)}let l=o.join(""),c=ce(a,e.profile).tokens,u=ce(l,e.profile).tokens;return u>=c||!l.trim()?{text:a,rules:[],originalTokens:c,tokens:c,saved:0}:{text:l,rules:[...new Set(n)],originalTokens:c,tokens:u,saved:c-u}}function so(a,e="input"){let n=a.pricing;return n?(e==="output"?n.outputPerMTok:e==="cached"?n.cachedInputPerMTok??n.inputPerMTok:n.inputPerMTok)/1e6:null}function fa(a,e,n="input"){let i=so(e,n);return i===null?null:a*i}function Pa(a,e,n="input"){let i=fa(a,e,n);return{tokens:a,usd:i,usdPer1k:i===null?null:i*1e3}}function ar(a){if(a==null||!Number.isFinite(a))return"n/a";let e=Math.abs(a),n=a<0?"-":"";if(e===0)return"$0";if(e>=100)return`${n}$${e.toLocaleString("en-US",{maximumFractionDigits:0})}`;if(e>=.1)return`${n}$${e.toFixed(2)}`;let i=Math.min(8,Math.max(2,1-Math.floor(Math.log10(e))));return`${n}$${e.toFixed(i).replace(/(\.\d*?[1-9])0+$/,"$1")}`}function oo(a){let e="",n=!1;for(let i=0;i<a.length;i++){let r=a[i];n?(e+=r,r==="\\"?e+=a[++i]??"":r==='"'&&(n=!1)):r==='"'?(n=!0,e+=r):r!==" "&&r!==`
3287
3287
  `&&r!=="\r"&&r!==" "&&(e+=r)}return e}function Tn(a){try{return{v:JSON.parse(a)}}catch{return null}}function lo(a,e){let n=Tn(a),i=Tn(e);return!!n&&!!i&&JSON.stringify(n.v)===JSON.stringify(i.v)}var nr=a=>/^\s*[[{]/.test(a)&&/\n/.test(a.trim()),co=/\b(?:indent\w*|pretty[- ]?print\w*|prettif\w*|format\w*|whitespace|reformat\w*|beautif\w*|lint\w*|line breaks?|spacing)\b/i;function ir(a,e){let n=ce(a,e).tokens,i=[],r=[],t=[],s=(h,C)=>{let w=ce(h,e).tokens;return w>=n&&h!==a?s(a,"Already compact: no change makes it shorter in this model's tokens."):{text:h,before:n,after:w,saved:n-w,blocks:i,changes:r,checks:t,skipped:C}},o=(h,C)=>{if(!nr(h)||!Tn(h))return h;let w=oo(h.trim()),N=lo(h,w),F=ce(h,e).tokens,L=ce(w,e).tokens;return!N||L>=F?h:(i.push({kind:"json",where:C,tokens:F,compactTokens:L}),t.push({name:`JSON ${C}`,passed:!0,detail:"parses to the same keys, values and order"}),w)};if(nr(a)&&Tn(a)){let h=o(a,"whole text");return h!==a&&r.push("removed JSON indentation and line breaks (same data)"),s(h,h===a?"Already compact JSON.":void 0)}let l=a.replace(/```[\s\S]*?```/g,"");if(co.test(l.slice(0,2e3)))return s(a,"The request is about formatting, so the layout is kept as written.");let c=a.split(/(```[^\n]*\n[\s\S]*?\n```)/g),u=0,g=!1,f=c.map((h,C)=>{if(C%2===1){u++;let z=/^(```[^\n]*\n)([\s\S]*?)(\n```)$/.exec(h);if(!z)return h;let K=o(z[2],`code block ${u}`);return K===z[2]?h:`${z[1]}${K}${z[3]}`}let w=h.split(`
3288
3288
  `),N=[];for(let z=0;z<w.length;z++){let K=/^(\s*)([[{])\s*$/.exec(w[z]);if(K){let ia=K[2]==="{"?"}":"]",se=w.findIndex((oe,ae)=>ae>z&&oe.startsWith(K[1]+ia)&&oe.trim().replace(/,$/,"")===ia);if(se>z){let oe=w.slice(z,se+1).join(`
3289
3289
  `),ae=w[se].trim().endsWith(",")?",":"",Se=o(ae?oe.replace(/,\s*$/,""):oe,`lines ${z+1}-${se+1}`);if(Se!==oe&&Se!==oe.replace(/,\s*$/,"")){N.push(K[1]+Se+ae),z=se;continue}}}N.push(w[z])}let F=N.join(`
3290
3290
  `),L=F.replace(/[ \t]+$/gm,"").replace(/\n{3,}/g,`
3291
3291
 
3292
- `);return L!==F&&(g=!0),F=L,F});return i.length&&r.push(`removed indentation and line breaks from ${i.length} JSON block${i.length>1?"s":""} (same data)`),g&&r.push("removed trailing spaces and extra blank lines outside code"),s(f.join(""),i.length||g?void 0:"Nothing to compact: no pretty-printed JSON or extra whitespace found.")}var X=a=>a,ue={"gpt-astra":X({id:"gpt-astra",label:"GPT-6 Astra",family:"openai",contextWindow:105e4,tokenizer:"heuristic",charsPerToken:4.2,reasoning:!0,pricing:{inputPerMTok:10,outputPerMTok:50}}),"gpt-reasoning":X({id:"gpt-reasoning",label:"GPT (thinking)",family:"openai",contextWindow:196e3,tokenizer:"o200k",charsPerToken:4.2,reasoning:!0,lighterAlternative:"the Instant / non-thinking model",pricing:{inputPerMTok:5,outputPerMTok:30,cachedInputPerMTok:.5}}),gpt:X({id:"gpt",label:"GPT",family:"openai",contextWindow:128e3,tokenizer:"o200k",charsPerToken:4.2,reasoning:!1,pricing:{inputPerMTok:2.5,outputPerMTok:15,cachedInputPerMTok:.25}}),"claude-fable":X({id:"claude-fable",label:"Claude Fable",family:"anthropic",contextWindow:1e6,tokenizer:"heuristic",charsPerToken:3.5,reasoning:!0,notes:["Claude reads XML-style tags well: wrap pasted material in <doc>\u2026</doc> instead of explaining it."],pricing:{inputPerMTok:10,outputPerMTok:50,cachedInputPerMTok:.25}}),"claude-opus":X({id:"claude-opus",label:"Claude Opus",family:"anthropic",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.5,reasoning:!1,lighterAlternative:"Sonnet or Haiku",notes:["Claude reads XML-style tags well: wrap pasted material in <doc>\u2026</doc> instead of explaining it."],pricing:{inputPerMTok:4,outputPerMTok:20,cachedInputPerMTok:.2}}),"claude-sonnet":X({id:"claude-sonnet",label:"Claude Sonnet",family:"anthropic",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.5,reasoning:!1,lighterAlternative:"Haiku",notes:["Claude reads XML-style tags well: wrap pasted material in <doc>\u2026</doc> instead of explaining it."],pricing:{inputPerMTok:2,outputPerMTok:10,cachedInputPerMTok:.2}}),"claude-haiku":X({id:"claude-haiku",label:"Claude Haiku",family:"anthropic",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.5,reasoning:!1,pricing:{inputPerMTok:.1,outputPerMTok:.5,cachedInputPerMTok:.01}}),"gemini-pro":X({id:"gemini-pro",label:"Gemini Pro",family:"google",contextWindow:1e6,tokenizer:"heuristic",charsPerToken:4,reasoning:!0,lighterAlternative:"Flash",pricing:{inputPerMTok:2,outputPerMTok:12,cachedInputPerMTok:.2,longContext:{aboveTokens:2e5,inputPerMTok:4,outputPerMTok:18}}}),"gemini-flash":X({id:"gemini-flash",label:"Gemini Flash",family:"google",contextWindow:1e6,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:1.5,outputPerMTok:9,cachedInputPerMTok:.15}}),deepseek:X({id:"deepseek",label:"DeepSeek",family:"deepseek",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.28,outputPerMTok:.42,cachedInputPerMTok:.028}}),"deepseek-reasoning":X({id:"deepseek-reasoning",label:"DeepSeek (thinking)",family:"deepseek",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!0,lighterAlternative:"the non-thinking mode",pricing:{inputPerMTok:.55,outputPerMTok:2.19,cachedInputPerMTok:.14}}),mistral:X({id:"mistral",label:"Mistral",family:"mistral",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:6}}),grok:X({id:"grok",label:"Grok",family:"xai",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:6}}),llama:X({id:"llama",label:"Llama",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.27,outputPerMTok:.85}}),qwen:X({id:"qwen",label:"Qwen",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.4,outputPerMTok:1.2}}),kimi:X({id:"kimi",label:"Kimi",family:"other",contextWindow:256e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.6,outputPerMTok:2.5,cachedInputPerMTok:.15}}),glm:X({id:"glm",label:"GLM",family:"other",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.6,outputPerMTok:2.2,cachedInputPerMTok:.11}}),command:X({id:"command",label:"Cohere Command",family:"other",contextWindow:256e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2.5,outputPerMTok:10}}),sonar:X({id:"sonar",label:"Perplexity Sonar",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:1,outputPerMTok:1}}),gemma:X({id:"gemma",label:"Gemma",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.1,outputPerMTok:.2}}),phi:X({id:"phi",label:"Phi",family:"other",contextWindow:16e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.07,outputPerMTok:.14}}),generic:X({id:"generic",label:"Unknown model",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:10}})},uo=[[/\b(fable|mythos)\b/,"claude-fable"],[/\bastra\b|\bgpt[- ]?6\b/,"gpt-astra"],[/\bclaude\b.*\bopus\b|\bopus\b/,"claude-opus"],[/\bsonnet\b/,"claude-sonnet"],[/\bhaiku\b/,"claude-haiku"],[/\b(qwen|qwq)/,"qwen"],[/\b(glm|chatglm|zhipu)\b|\bglm-?\d/,"glm"],[/\b(kimi|moonshot)/,"kimi"],[/\bgemma/,"gemma"],[/\bgemini\b.*\bflash\b|\bflash\b/,"gemini-flash"],[/\bgemini\b.*\b(pro|ultra|deep think)\b|^\s*\d(\.\d)?\s*pro\b|\bdeep think\b/,"gemini-pro"],[/\bdeepseek\b.*\b(r1|reason|think|deepthink)|\bdeepthink\b/,"deepseek-reasoning"],[/\bdeepseek\b/,"deepseek"],[/\b(mistral|le chat|magistral|codestral)\b/,"mistral"],[/\bgrok\b/,"grok"],[/\bllama|codellama/,"llama"],[/\bcommand[- ]?(r|a)\b|\bcohere\b/,"command"],[/\bsonar\b|\bperplexity\b/,"sonar"],[/\bphi-?\d/,"phi"],[/\bo[1-9](-mini|-pro)?\b|\bgpt[- ]?\S*\s*(thinking|pro)\b|\bthinking\b/,"gpt-reasoning"],[/\b(gpt|chatgpt)\b|\bgpt-?\d/,"gpt"],[/\bgemini\b/,"gemini-pro"],[/\bclaude\b/,"claude-sonnet"]];var mo={chatgpt:"gpt",claude:"claude-sonnet",gemini:"gemini-flash",deepseek:"deepseek",mistral:"mistral",grok:"grok",generic:"generic"},Ja=[],po=a=>a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function go(a){let e=/^\/(.+)\/([a-z]*)$/.exec(a);return e?new RegExp(e[1],e[2].replace(/[gy]/g,"").replace(/^(?!.*i)/,"i")):new RegExp(po(a.toLowerCase().trim()),"i")}function vi(a){if(!a.id||!(a.contextWindow>0))throw new Error("A custom model needs an id and a positive contextWindow.");let e={id:a.id,label:a.label??a.id,family:a.family??"other",contextWindow:a.contextWindow,tokenizer:a.tokenizer??"heuristic",charsPerToken:a.charsPerToken??4,reasoning:a.reasoning??!1,lighterAlternative:a.lighterAlternative,notes:a.notes,pricing:a.pricing};wi(a.id),ue[e.id]=e;for(let n of a.match?.length?a.match:[a.id])Ja.push([go(n),e.id]);return e}function wi(a){for(let e=Ja.length-1;e>=0;e--)Ja[e][1]===a&&Ja.splice(e,1);a in tr?ue[a]=tr[a]:delete ue[a]}var tr={...ue};function za(a,e="generic"){let n=(a??"").toLowerCase().replace(/\s+/g," ").trim();if(n){for(let[i,r]of Ja)if(i.test(n))return ue[r];if(e==="gemini"&&/\b(pro|deep think)\b/.test(n))return ue["gemini-pro"];if(e==="gemini"&&/\bflash\b/.test(n))return ue["gemini-flash"];if(e==="deepseek"&&/\b(deepthink|r1|think)/.test(n))return ue["deepseek-reasoning"];for(let[i,r]of uo)if(i.test(n))return ue[r]}return ue[mo[e]??"generic"]}function rr(a){let e=typeof a.model=="string"?za(a.model,a.site):a.model,n=a.pricing?{...e,pricing:a.pricing}:e,i=ga(a.prompt),{tokens:r,exact:t}=ce(a.prompt,n),s=Ba(a.conversationTokens??0,r,n,a.contextWindow),o=yt(a.level??"auto",s),l=ea(a.prompt,{profile:n,task:i.task,level:o}),c=ir(l.text,n),u=c.saved>0&&c.blocks.length?{...l,text:c.text,tokens:c.after,saved:l.originalTokens-c.after,rules:[...l.rules,"JSON whitespace (same data)"]}:l,g=Fa({profile:n,task:i.task,signals:i.signals,promptTokens:r,usage:s,text:a.prompt}),f={pricing:n.pricing,promptUsd:ha(r,n),sendUsd:ha((a.conversationTokens??0)+r,n),saved:Pa(u.saved,n)},h=Object.entries(yi(a.prompt,u.text)).map(([w,[N,F]])=>`${F}/${N} ${w}s`).join(", "),C={text:u.text,tokens:u.tokens,changes:u.saved>0?u.rules.map(w=>`removed ${w}`):[],kept:h||"every instruction"};return{profile:n,task:i,promptTokens:r,exact:t,usage:s,level:o,optimized:u,tips:g,handoffPrompt:vt(i.task),cost:f,best:C}}import{createHash as _o}from"node:crypto";import{createHash as bo}from"node:crypto";var Cn=a=>bo("sha256").update(a).digest("hex").slice(0,24);function ko(a){if(!(a.length<24)){if(a.readUInt32BE(0)===2303741511)return{width:a.readUInt32BE(16),height:a.readUInt32BE(20)};if(a.toString("ascii",0,3)==="GIF")return{width:a.readUInt16LE(6),height:a.readUInt16LE(8)};if(a.toString("ascii",0,4)==="RIFF"&&a.toString("ascii",8,12)==="WEBP"){let e=a.toString("ascii",12,16);if(e==="VP8X"&&a.length>=30)return{width:1+a.readUIntLE(24,3),height:1+a.readUIntLE(27,3)};if(e==="VP8 "&&a.length>=30)return{width:a.readUInt16LE(26)&16383,height:a.readUInt16LE(28)&16383};if(e==="VP8L"&&a.length>=25){let n=a.readUInt32LE(21);return{width:1+(n&16383),height:1+(n>>14&16383)}}return}if(a[0]===255&&a[1]===216){let e=2;for(;e+9<a.length;){if(a[e]!==255)return;let n=a[e+1],i=a.readUInt16BE(e+2);if(n>=192&&n<=207&&![196,200,204].includes(n))return{width:a.readUInt16BE(e+7),height:a.readUInt16BE(e+5)};e+=2+i}}}}function sr(a){let e=/^data:[^;,]*;base64,(.*)$/s.exec(a);return e?Buffer.from(e[1],"base64"):void 0}function Rn(a,e){let n=[],i=(t,s)=>{t.forEach((o,l)=>{if(!o||typeof o!="object")return;let c,u,g,f=C=>({type:"text",text:C});if(a==="openai-chat"&&o.type==="image_url"){let C=typeof o.image_url=="string"?o.image_url:o.image_url?.url;if(typeof C!="string")return;c=sr(C),u=Cn(C),g=o.image_url?.detail}else if(a==="openai-responses"&&o.type==="input_image"){if(typeof o.image_url!="string")return;c=sr(o.image_url),u=Cn(o.image_url),g=o.detail,f=C=>({type:"input_text",text:C})}else if(a==="anthropic"&&o.type==="image"){let C=o.source??{};if(C.type==="base64"&&typeof C.data=="string")c=Buffer.from(C.data,"base64"),u=Cn(C.data);else if(typeof C.url=="string")u=Cn(C.url);else return}else return a==="anthropic"&&o.type==="tool_result"&&Array.isArray(o.content)?i(o.content,s):void 0;let h=c?ko(c):void 0;n.push({turn:s,key:u,...h,detail:g,replace:C=>t[l]=f(C)})})};return(a==="openai-responses"?Array.isArray(e?.input)?e.input:[]:Array.isArray(e?.messages)?e.messages:[]).forEach((t,s)=>{Array.isArray(t?.content)&&i(t.content,s)}),n}function Si(a,e){if(a==="openai"&&e.detail==="low")return 85;if(!e.width||!e.height)return;let n=e.width,i=e.height;if(a==="anthropic"){let s=Math.min(1,1568/Math.max(n,i),Math.sqrt(115e4/(n*i)));return Math.min(1600,Math.ceil(n*s*i*s/750))}let r=Math.min(1,2048/Math.max(n,i));n*=r,i*=r;let t=Math.min(1,768/Math.min(n,i));return n*=t,i*=t,85+170*Math.ceil(n/512)*Math.ceil(i/512)}function or(a,e){let n=new Map,i=0,r=0;for(let t of a){let s=n.get(t.key);if(s===void 0){n.set(t.key,t.turn);continue}s!==t.turn&&(t.replace(`[Token Lens: same image as in message ${s} above]`),i+=Si(e,t)??0,r++)}return{saved:i,count:r}}var Pn=["system","tools","history","tool_output","context","prompt"];function _i(a){let e=a;if(typeof e=="string")try{e=JSON.parse(e)}catch{return e}let n=i=>Array.isArray(i)?i.map(n):i&&typeof i=="object"?Object.fromEntries(Object.keys(i).sort().map(r=>[r,n(i[r])])):i;return JSON.stringify(n(e??{}))}var Ya=(a,e,n,i)=>({part:a,turn:e,get:()=>n[i],set:r=>{n[i]=r}}),zn=(a,e,n)=>({part:a,turn:e,get:()=>n});function Re(a,e,n,i,r){let t=n[i];return typeof t=="string"?[Ya(a,e,n,i)]:Array.isArray(t)?t.filter(s=>s&&typeof s=="object"&&r.includes(s.type)&&typeof s.text=="string").map(s=>Ya(a,e,s,"text")):[]}var Ei=a=>a.map(e=>({...e,user:!0}));function Ai(a,e){for(let n=a.length-1;n>=0;n--)if(e(a[n]))return n;return-1}var Ti=a=>Array.isArray(a)&&a.length?[zn("tools",-1,JSON.stringify(a))]:[];function yo(a){let e=Array.isArray(a.messages)?a.messages:[],n=Ai(e,t=>t?.role==="assistant")+1,i=[...Ti(a.tools??a.functions)],r=[];return e.forEach((t,s)=>{if(!t||typeof t!="object")return;let o=["text"];if(t.role==="system"||t.role==="developer")i.push(...Re("system",-1,t,"content",o));else if(t.role==="tool"||t.role==="function")i.push(...Re("tool_output",s,t,"content",o));else if(t.role==="user")i.push(...Ei(Re(s>=n?"prompt":"history",s,t,"content",o)));else if(t.role==="assistant"){i.push(...Re("history",s,t,"content",o));for(let l of t.tool_calls??[])l?.function?.arguments&&i.push(zn("history",s,String(l.function.arguments))),l?.function?.name&&r.push({name:l.function.name,args:_i(l.function.arguments),turn:s})}}),{protocol:"openai-chat",model:String(a.model??""),stream:a.stream===!0,temperature:typeof a.temperature=="number"?a.temperature:void 0,maxOutputTokens:a.max_completion_tokens??a.max_tokens,refs:i,turns:e.length,toolCalls:r}}function vo(a){let e=[...Ti(a.tools)];typeof a.instructions=="string"&&e.push(Ya("system",-1,a,"instructions"));let n=[],i=1;if(typeof a.input=="string")e.push({...Ya("prompt",0,a,"input"),user:!0});else if(Array.isArray(a.input)){let r=a.input;i=r.length;let t=Ai(r,o=>o?.role==="assistant"||o?.type==="function_call"||o?.type==="reasoning")+1,s=["input_text","output_text","text"];r.forEach((o,l)=>{!o||typeof o!="object"||(o.type==="function_call_output"?typeof o.output=="string"?e.push(Ya("tool_output",l,o,"output")):Array.isArray(o.output)&&e.push(...Re("tool_output",l,o,"output",s)):o.type==="function_call"?(o.arguments&&e.push(zn("history",l,String(o.arguments))),o.name&&n.push({name:o.name,args:_i(o.arguments),turn:l})):o.role==="system"||o.role==="developer"?e.push(...Re("system",-1,o,"content",s)):o.role==="user"?e.push(...Ei(Re(l>=t?"prompt":"history",l,o,"content",s))):o.role==="assistant"&&e.push(...Re("history",l,o,"content",s)))})}return{protocol:"openai-responses",model:String(a.model??""),stream:a.stream===!0,temperature:typeof a.temperature=="number"?a.temperature:void 0,maxOutputTokens:a.max_output_tokens,refs:e,turns:i,toolCalls:n}}function wo(a){let e=Array.isArray(a.messages)?a.messages:[],n=Ai(e,t=>t?.role==="assistant")+1,i=[...Ti(a.tools)];i.push(...Re("system",-1,a,"system",["text"]));let r=[];return e.forEach((t,s)=>{if(!(!t||typeof t!="object")){if(t.role==="assistant"){i.push(...Re("history",s,t,"content",["text"]));for(let o of Array.isArray(t.content)?t.content:[])o?.type==="tool_use"&&(i.push(zn("history",s,JSON.stringify(o.input??{}))),r.push({name:o.name,args:_i(o.input),turn:s}));return}i.push(...Ei(Re(s>=n?"prompt":"history",s,t,"content",["text"])));for(let o of Array.isArray(t.content)?t.content:[])o?.type==="tool_result"&&i.push(...Re("tool_output",s,o,"content",["text"]))}}),{protocol:"anthropic",model:String(a.model??""),stream:a.stream===!0,temperature:typeof a.temperature=="number"?a.temperature:void 0,maxOutputTokens:a.max_tokens,refs:i,turns:e.length,toolCalls:r}}function Ci(a,e){return a==="anthropic"?wo(e):a==="openai-responses"?vo(e):yo(e)}function Ri(a){return/^\/v1\/chat\/completions\/?$/.test(a)?"openai-chat":/^\/v1\/responses\/?$/.test(a)?"openai-responses":/^\/v1\/messages\/?$/.test(a)?"anthropic":null}function In(a){let e=Object.fromEntries(Pn.map(n=>[n,[]]));for(let n of a){let i=n.get();if(!(typeof i!="string"||!i))if(n.part==="prompt"){let{instruction:r,material:t}=$a(i);e.prompt.push(r),t.trim()&&e.context.push(t)}else e[n.part].push(i)}return e}function So(a){if(!a||typeof a!="object")return{};let e={};if(typeof a.prompt_tokens=="number")e.inputTokens=a.prompt_tokens,e.outputTokens=a.completion_tokens,e.cachedInputTokens=a.prompt_tokens_details?.cached_tokens,e.reasoningTokens=a.completion_tokens_details?.reasoning_tokens;else if("cache_read_input_tokens"in a||"cache_creation_input_tokens"in a){let n=a.cache_read_input_tokens??0,i=a.cache_creation_input_tokens??0;typeof a.input_tokens=="number"&&(e.inputTokens=a.input_tokens+n+i),e.outputTokens=a.output_tokens,e.cachedInputTokens=n,e.cacheWriteTokens=i}else e.inputTokens=a.input_tokens,e.outputTokens=a.output_tokens,e.cachedInputTokens=a.input_tokens_details?.cached_tokens,e.reasoningTokens=a.output_tokens_details?.reasoning_tokens;for(let n of Object.keys(e))typeof e[n]!="number"&&delete e[n];return e}function lr(a,e){let n={...a};for(let[i,r]of Object.entries(e))n[i]=Math.max(n[i]??0,r);return n}function fa(a){return So(a?.usage??a?.message?.usage??a?.response?.usage)}function xn(a,e,n){return a==="anthropic"?{type:"error",error:{type:e===401?"authentication_error":"invalid_request_error",message:n}}:{error:{message:n,type:e===401?"authentication_error":"invalid_request_error",code:null}}}var Mn=class{factors=new Map;factor(e){return e.tokenizer==="heuristic"?this.factors.get(e.id)?.factor??1:1}raw(e,n){return ce(e,n).tokens}count(e,n){return Math.round(this.raw(e,n)*this.factor(n))}observe(e,n,i){if(e.tokenizer!=="heuristic"||n<50||!(i>0))return;let r=Math.min(3,Math.max(1/3,i/n)),t=this.factors.get(e.id),s=(t?.samples??0)+1,o=Math.max(1/s,.1);this.factors.set(e.id,{factor:t?t.factor+(r-t.factor)*o:r,samples:s})}snapshot(){return Object.fromEntries([...this.factors].map(([e,n])=>[e,{factor:Math.round(n.factor*1e3)/1e3,samples:n.samples}]))}};function jn(a,e,n){let i=In(a),r=Object.fromEntries(Pn.map(o=>[o,0])),t=0;for(let o of Pn)for(let l of i[o]){let c=n.raw(l,e);t+=c,r[o]+=Math.round(c*n.factor(e))}let s=a.filter(o=>o.part==="tool_output").map(o=>n.count(o.get()??"",e));return{parts:r,total:Object.values(r).reduce((o,l)=>o+l,0),raw:t,toolOutputs:s}}function Eo(a){let e=a.trim();if(!/^[[{]/.test(e)||!e.includes(`
3292
+ `);return L!==F&&(g=!0),F=L,F});return i.length&&r.push(`removed indentation and line breaks from ${i.length} JSON block${i.length>1?"s":""} (same data)`),g&&r.push("removed trailing spaces and extra blank lines outside code"),s(f.join(""),i.length||g?void 0:"Nothing to compact: no pretty-printed JSON or extra whitespace found.")}var X=a=>a,ue={"gpt-astra":X({id:"gpt-astra",label:"GPT-6 Astra",family:"openai",contextWindow:105e4,tokenizer:"heuristic",charsPerToken:4.2,reasoning:!0,pricing:{inputPerMTok:10,outputPerMTok:50}}),"gpt-reasoning":X({id:"gpt-reasoning",label:"GPT (thinking)",family:"openai",contextWindow:196e3,tokenizer:"o200k",charsPerToken:4.2,reasoning:!0,lighterAlternative:"the Instant / non-thinking model",pricing:{inputPerMTok:5,outputPerMTok:30,cachedInputPerMTok:.5}}),gpt:X({id:"gpt",label:"GPT",family:"openai",contextWindow:128e3,tokenizer:"o200k",charsPerToken:4.2,reasoning:!1,pricing:{inputPerMTok:2.5,outputPerMTok:15,cachedInputPerMTok:.25}}),"claude-fable":X({id:"claude-fable",label:"Claude Fable",family:"anthropic",contextWindow:1e6,tokenizer:"heuristic",charsPerToken:3.5,reasoning:!0,notes:["Claude reads XML-style tags well: wrap pasted material in <doc>\u2026</doc> instead of explaining it."],pricing:{inputPerMTok:10,outputPerMTok:50,cachedInputPerMTok:.25}}),"claude-opus":X({id:"claude-opus",label:"Claude Opus",family:"anthropic",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.5,reasoning:!1,lighterAlternative:"Sonnet or Haiku",notes:["Claude reads XML-style tags well: wrap pasted material in <doc>\u2026</doc> instead of explaining it."],pricing:{inputPerMTok:4,outputPerMTok:20,cachedInputPerMTok:.2}}),"claude-sonnet":X({id:"claude-sonnet",label:"Claude Sonnet",family:"anthropic",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.5,reasoning:!1,lighterAlternative:"Haiku",notes:["Claude reads XML-style tags well: wrap pasted material in <doc>\u2026</doc> instead of explaining it."],pricing:{inputPerMTok:2,outputPerMTok:10,cachedInputPerMTok:.2}}),"claude-haiku":X({id:"claude-haiku",label:"Claude Haiku",family:"anthropic",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.5,reasoning:!1,pricing:{inputPerMTok:.1,outputPerMTok:.5,cachedInputPerMTok:.01}}),"gemini-pro":X({id:"gemini-pro",label:"Gemini Pro",family:"google",contextWindow:1e6,tokenizer:"heuristic",charsPerToken:4,reasoning:!0,lighterAlternative:"Flash",pricing:{inputPerMTok:2,outputPerMTok:12,cachedInputPerMTok:.2,longContext:{aboveTokens:2e5,inputPerMTok:4,outputPerMTok:18}}}),"gemini-flash":X({id:"gemini-flash",label:"Gemini Flash",family:"google",contextWindow:1e6,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:1.5,outputPerMTok:9,cachedInputPerMTok:.15}}),deepseek:X({id:"deepseek",label:"DeepSeek",family:"deepseek",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.28,outputPerMTok:.42,cachedInputPerMTok:.028}}),"deepseek-reasoning":X({id:"deepseek-reasoning",label:"DeepSeek (thinking)",family:"deepseek",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!0,lighterAlternative:"the non-thinking mode",pricing:{inputPerMTok:.55,outputPerMTok:2.19,cachedInputPerMTok:.14}}),mistral:X({id:"mistral",label:"Mistral",family:"mistral",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:6}}),grok:X({id:"grok",label:"Grok",family:"xai",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:6}}),llama:X({id:"llama",label:"Llama",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.27,outputPerMTok:.85}}),qwen:X({id:"qwen",label:"Qwen",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.4,outputPerMTok:1.2}}),kimi:X({id:"kimi",label:"Kimi",family:"other",contextWindow:256e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.6,outputPerMTok:2.5,cachedInputPerMTok:.15}}),glm:X({id:"glm",label:"GLM",family:"other",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.6,outputPerMTok:2.2,cachedInputPerMTok:.11}}),command:X({id:"command",label:"Cohere Command",family:"other",contextWindow:256e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2.5,outputPerMTok:10}}),sonar:X({id:"sonar",label:"Perplexity Sonar",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:1,outputPerMTok:1}}),gemma:X({id:"gemma",label:"Gemma",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.1,outputPerMTok:.2}}),phi:X({id:"phi",label:"Phi",family:"other",contextWindow:16e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.07,outputPerMTok:.14}}),generic:X({id:"generic",label:"Unknown model",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:10}})},uo=[[/\b(fable|mythos)\b/,"claude-fable"],[/\bastra\b|\bgpt[- ]?6\b/,"gpt-astra"],[/\bclaude\b.*\bopus\b|\bopus\b/,"claude-opus"],[/\bsonnet\b/,"claude-sonnet"],[/\bhaiku\b/,"claude-haiku"],[/\b(qwen|qwq)/,"qwen"],[/\b(glm|chatglm|zhipu)\b|\bglm-?\d/,"glm"],[/\b(kimi|moonshot)/,"kimi"],[/\bgemma/,"gemma"],[/\bgemini\b.*\bflash\b|\bflash\b/,"gemini-flash"],[/\bgemini\b.*\b(pro|ultra|deep think)\b|^\s*\d(\.\d)?\s*pro\b|\bdeep think\b/,"gemini-pro"],[/\bdeepseek\b.*\b(r1|reason|think|deepthink)|\bdeepthink\b/,"deepseek-reasoning"],[/\bdeepseek\b/,"deepseek"],[/\b(mistral|le chat|magistral|codestral)\b/,"mistral"],[/\bgrok\b/,"grok"],[/\bllama|codellama/,"llama"],[/\bcommand[- ]?(r|a)\b|\bcohere\b/,"command"],[/\bsonar\b|\bperplexity\b/,"sonar"],[/\bphi-?\d/,"phi"],[/\bo[1-9](-mini|-pro)?\b|\bgpt[- ]?\S*\s*(thinking|pro)\b|\bthinking\b/,"gpt-reasoning"],[/\b(gpt|chatgpt)\b|\bgpt-?\d/,"gpt"],[/\bgemini\b/,"gemini-pro"],[/\bclaude\b/,"claude-sonnet"]];var mo={chatgpt:"gpt",claude:"claude-sonnet",gemini:"gemini-flash",deepseek:"deepseek",mistral:"mistral",grok:"grok",generic:"generic"},Ja=[],po=a=>a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function go(a){let e=/^\/(.+)\/([a-z]*)$/.exec(a);return e?new RegExp(e[1],e[2].replace(/[gy]/g,"").replace(/^(?!.*i)/,"i")):new RegExp(po(a.toLowerCase().trim()),"i")}function vi(a){if(!a.id||!(a.contextWindow>0))throw new Error("A custom model needs an id and a positive contextWindow.");let e={id:a.id,label:a.label??a.id,family:a.family??"other",contextWindow:a.contextWindow,tokenizer:a.tokenizer??"heuristic",charsPerToken:a.charsPerToken??4,reasoning:a.reasoning??!1,lighterAlternative:a.lighterAlternative,notes:a.notes,pricing:a.pricing};wi(a.id),ue[e.id]=e;for(let n of a.match?.length?a.match:[a.id])Ja.push([go(n),e.id]);return e}function wi(a){for(let e=Ja.length-1;e>=0;e--)Ja[e][1]===a&&Ja.splice(e,1);a in tr?ue[a]=tr[a]:delete ue[a]}var tr={...ue};function za(a,e="generic"){let n=(a??"").toLowerCase().replace(/\s+/g," ").trim();if(n){for(let[i,r]of Ja)if(i.test(n))return ue[r];if(e==="gemini"&&/\b(pro|deep think)\b/.test(n))return ue["gemini-pro"];if(e==="gemini"&&/\bflash\b/.test(n))return ue["gemini-flash"];if(e==="deepseek"&&/\b(deepthink|r1|think)/.test(n))return ue["deepseek-reasoning"];for(let[i,r]of uo)if(i.test(n))return ue[r]}return ue[mo[e]??"generic"]}var uu=5*36e5,du=7*864e5;function rr(a){let e=typeof a.model=="string"?za(a.model,a.site):a.model,n=a.pricing?{...e,pricing:a.pricing}:e,i=ha(a.prompt),{tokens:r,exact:t}=ce(a.prompt,n),s=Ba(a.conversationTokens??0,r,n,a.contextWindow),o=yt(a.level??"auto",s),l=ea(a.prompt,{profile:n,task:i.task,level:o}),c=ir(l.text,n),u=c.saved>0&&c.blocks.length?{...l,text:c.text,tokens:c.after,saved:l.originalTokens-c.after,rules:[...l.rules,"JSON whitespace (same data)"]}:l,g=Fa({profile:n,task:i.task,signals:i.signals,promptTokens:r,usage:s,text:a.prompt}),f={pricing:n.pricing,promptUsd:fa(r,n),sendUsd:fa((a.conversationTokens??0)+r,n),saved:Pa(u.saved,n)},h=Object.entries(yi(a.prompt,u.text)).map(([w,[N,F]])=>`${F}/${N} ${w}s`).join(", "),C={text:u.text,tokens:u.tokens,changes:u.saved>0?u.rules.map(w=>`removed ${w}`):[],kept:h||"every instruction"};return{profile:n,task:i,promptTokens:r,exact:t,usage:s,level:o,optimized:u,tips:g,handoffPrompt:vt(i.task),cost:f,best:C}}import{createHash as _o}from"node:crypto";import{createHash as bo}from"node:crypto";var Cn=a=>bo("sha256").update(a).digest("hex").slice(0,24);function ko(a){if(!(a.length<24)){if(a.readUInt32BE(0)===2303741511)return{width:a.readUInt32BE(16),height:a.readUInt32BE(20)};if(a.toString("ascii",0,3)==="GIF")return{width:a.readUInt16LE(6),height:a.readUInt16LE(8)};if(a.toString("ascii",0,4)==="RIFF"&&a.toString("ascii",8,12)==="WEBP"){let e=a.toString("ascii",12,16);if(e==="VP8X"&&a.length>=30)return{width:1+a.readUIntLE(24,3),height:1+a.readUIntLE(27,3)};if(e==="VP8 "&&a.length>=30)return{width:a.readUInt16LE(26)&16383,height:a.readUInt16LE(28)&16383};if(e==="VP8L"&&a.length>=25){let n=a.readUInt32LE(21);return{width:1+(n&16383),height:1+(n>>14&16383)}}return}if(a[0]===255&&a[1]===216){let e=2;for(;e+9<a.length;){if(a[e]!==255)return;let n=a[e+1],i=a.readUInt16BE(e+2);if(n>=192&&n<=207&&![196,200,204].includes(n))return{width:a.readUInt16BE(e+7),height:a.readUInt16BE(e+5)};e+=2+i}}}}function sr(a){let e=/^data:[^;,]*;base64,(.*)$/s.exec(a);return e?Buffer.from(e[1],"base64"):void 0}function Rn(a,e){let n=[],i=(t,s)=>{t.forEach((o,l)=>{if(!o||typeof o!="object")return;let c,u,g,f=C=>({type:"text",text:C});if(a==="openai-chat"&&o.type==="image_url"){let C=typeof o.image_url=="string"?o.image_url:o.image_url?.url;if(typeof C!="string")return;c=sr(C),u=Cn(C),g=o.image_url?.detail}else if(a==="openai-responses"&&o.type==="input_image"){if(typeof o.image_url!="string")return;c=sr(o.image_url),u=Cn(o.image_url),g=o.detail,f=C=>({type:"input_text",text:C})}else if(a==="anthropic"&&o.type==="image"){let C=o.source??{};if(C.type==="base64"&&typeof C.data=="string")c=Buffer.from(C.data,"base64"),u=Cn(C.data);else if(typeof C.url=="string")u=Cn(C.url);else return}else return a==="anthropic"&&o.type==="tool_result"&&Array.isArray(o.content)?i(o.content,s):void 0;let h=c?ko(c):void 0;n.push({turn:s,key:u,...h,detail:g,replace:C=>t[l]=f(C)})})};return(a==="openai-responses"?Array.isArray(e?.input)?e.input:[]:Array.isArray(e?.messages)?e.messages:[]).forEach((t,s)=>{Array.isArray(t?.content)&&i(t.content,s)}),n}function Si(a,e){if(a==="openai"&&e.detail==="low")return 85;if(!e.width||!e.height)return;let n=e.width,i=e.height;if(a==="anthropic"){let s=Math.min(1,1568/Math.max(n,i),Math.sqrt(115e4/(n*i)));return Math.min(1600,Math.ceil(n*s*i*s/750))}let r=Math.min(1,2048/Math.max(n,i));n*=r,i*=r;let t=Math.min(1,768/Math.min(n,i));return n*=t,i*=t,85+170*Math.ceil(n/512)*Math.ceil(i/512)}function or(a,e){let n=new Map,i=0,r=0;for(let t of a){let s=n.get(t.key);if(s===void 0){n.set(t.key,t.turn);continue}s!==t.turn&&(t.replace(`[Token Lens: same image as in message ${s} above]`),i+=Si(e,t)??0,r++)}return{saved:i,count:r}}var Pn=["system","tools","history","tool_output","context","prompt"];function _i(a){let e=a;if(typeof e=="string")try{e=JSON.parse(e)}catch{return e}let n=i=>Array.isArray(i)?i.map(n):i&&typeof i=="object"?Object.fromEntries(Object.keys(i).sort().map(r=>[r,n(i[r])])):i;return JSON.stringify(n(e??{}))}var Ya=(a,e,n,i)=>({part:a,turn:e,get:()=>n[i],set:r=>{n[i]=r}}),zn=(a,e,n)=>({part:a,turn:e,get:()=>n});function Re(a,e,n,i,r){let t=n[i];return typeof t=="string"?[Ya(a,e,n,i)]:Array.isArray(t)?t.filter(s=>s&&typeof s=="object"&&r.includes(s.type)&&typeof s.text=="string").map(s=>Ya(a,e,s,"text")):[]}var Ei=a=>a.map(e=>({...e,user:!0}));function Ai(a,e){for(let n=a.length-1;n>=0;n--)if(e(a[n]))return n;return-1}var Ti=a=>Array.isArray(a)&&a.length?[zn("tools",-1,JSON.stringify(a))]:[];function yo(a){let e=Array.isArray(a.messages)?a.messages:[],n=Ai(e,t=>t?.role==="assistant")+1,i=[...Ti(a.tools??a.functions)],r=[];return e.forEach((t,s)=>{if(!t||typeof t!="object")return;let o=["text"];if(t.role==="system"||t.role==="developer")i.push(...Re("system",-1,t,"content",o));else if(t.role==="tool"||t.role==="function")i.push(...Re("tool_output",s,t,"content",o));else if(t.role==="user")i.push(...Ei(Re(s>=n?"prompt":"history",s,t,"content",o)));else if(t.role==="assistant"){i.push(...Re("history",s,t,"content",o));for(let l of t.tool_calls??[])l?.function?.arguments&&i.push(zn("history",s,String(l.function.arguments))),l?.function?.name&&r.push({name:l.function.name,args:_i(l.function.arguments),turn:s})}}),{protocol:"openai-chat",model:String(a.model??""),stream:a.stream===!0,temperature:typeof a.temperature=="number"?a.temperature:void 0,maxOutputTokens:a.max_completion_tokens??a.max_tokens,refs:i,turns:e.length,toolCalls:r}}function vo(a){let e=[...Ti(a.tools)];typeof a.instructions=="string"&&e.push(Ya("system",-1,a,"instructions"));let n=[],i=1;if(typeof a.input=="string")e.push({...Ya("prompt",0,a,"input"),user:!0});else if(Array.isArray(a.input)){let r=a.input;i=r.length;let t=Ai(r,o=>o?.role==="assistant"||o?.type==="function_call"||o?.type==="reasoning")+1,s=["input_text","output_text","text"];r.forEach((o,l)=>{!o||typeof o!="object"||(o.type==="function_call_output"?typeof o.output=="string"?e.push(Ya("tool_output",l,o,"output")):Array.isArray(o.output)&&e.push(...Re("tool_output",l,o,"output",s)):o.type==="function_call"?(o.arguments&&e.push(zn("history",l,String(o.arguments))),o.name&&n.push({name:o.name,args:_i(o.arguments),turn:l})):o.role==="system"||o.role==="developer"?e.push(...Re("system",-1,o,"content",s)):o.role==="user"?e.push(...Ei(Re(l>=t?"prompt":"history",l,o,"content",s))):o.role==="assistant"&&e.push(...Re("history",l,o,"content",s)))})}return{protocol:"openai-responses",model:String(a.model??""),stream:a.stream===!0,temperature:typeof a.temperature=="number"?a.temperature:void 0,maxOutputTokens:a.max_output_tokens,refs:e,turns:i,toolCalls:n}}function wo(a){let e=Array.isArray(a.messages)?a.messages:[],n=Ai(e,t=>t?.role==="assistant")+1,i=[...Ti(a.tools)];i.push(...Re("system",-1,a,"system",["text"]));let r=[];return e.forEach((t,s)=>{if(!(!t||typeof t!="object")){if(t.role==="assistant"){i.push(...Re("history",s,t,"content",["text"]));for(let o of Array.isArray(t.content)?t.content:[])o?.type==="tool_use"&&(i.push(zn("history",s,JSON.stringify(o.input??{}))),r.push({name:o.name,args:_i(o.input),turn:s}));return}i.push(...Ei(Re(s>=n?"prompt":"history",s,t,"content",["text"])));for(let o of Array.isArray(t.content)?t.content:[])o?.type==="tool_result"&&i.push(...Re("tool_output",s,o,"content",["text"]))}}),{protocol:"anthropic",model:String(a.model??""),stream:a.stream===!0,temperature:typeof a.temperature=="number"?a.temperature:void 0,maxOutputTokens:a.max_tokens,refs:i,turns:e.length,toolCalls:r}}function Ci(a,e){return a==="anthropic"?wo(e):a==="openai-responses"?vo(e):yo(e)}function Ri(a){return/^\/v1\/chat\/completions\/?$/.test(a)?"openai-chat":/^\/v1\/responses\/?$/.test(a)?"openai-responses":/^\/v1\/messages\/?$/.test(a)?"anthropic":null}function In(a){let e=Object.fromEntries(Pn.map(n=>[n,[]]));for(let n of a){let i=n.get();if(!(typeof i!="string"||!i))if(n.part==="prompt"){let{instruction:r,material:t}=$a(i);e.prompt.push(r),t.trim()&&e.context.push(t)}else e[n.part].push(i)}return e}function So(a){if(!a||typeof a!="object")return{};let e={};if(typeof a.prompt_tokens=="number")e.inputTokens=a.prompt_tokens,e.outputTokens=a.completion_tokens,e.cachedInputTokens=a.prompt_tokens_details?.cached_tokens,e.reasoningTokens=a.completion_tokens_details?.reasoning_tokens;else if("cache_read_input_tokens"in a||"cache_creation_input_tokens"in a){let n=a.cache_read_input_tokens??0,i=a.cache_creation_input_tokens??0;typeof a.input_tokens=="number"&&(e.inputTokens=a.input_tokens+n+i),e.outputTokens=a.output_tokens,e.cachedInputTokens=n,e.cacheWriteTokens=i}else e.inputTokens=a.input_tokens,e.outputTokens=a.output_tokens,e.cachedInputTokens=a.input_tokens_details?.cached_tokens,e.reasoningTokens=a.output_tokens_details?.reasoning_tokens;for(let n of Object.keys(e))typeof e[n]!="number"&&delete e[n];return e}function lr(a,e){let n={...a};for(let[i,r]of Object.entries(e))n[i]=Math.max(n[i]??0,r);return n}function ba(a){return So(a?.usage??a?.message?.usage??a?.response?.usage)}function xn(a,e,n){return a==="anthropic"?{type:"error",error:{type:e===401?"authentication_error":"invalid_request_error",message:n}}:{error:{message:n,type:e===401?"authentication_error":"invalid_request_error",code:null}}}var Mn=class{factors=new Map;factor(e){return e.tokenizer==="heuristic"?this.factors.get(e.id)?.factor??1:1}raw(e,n){return ce(e,n).tokens}count(e,n){return Math.round(this.raw(e,n)*this.factor(n))}observe(e,n,i){if(e.tokenizer!=="heuristic"||n<50||!(i>0))return;let r=Math.min(3,Math.max(1/3,i/n)),t=this.factors.get(e.id),s=(t?.samples??0)+1,o=Math.max(1/s,.1);this.factors.set(e.id,{factor:t?t.factor+(r-t.factor)*o:r,samples:s})}snapshot(){return Object.fromEntries([...this.factors].map(([e,n])=>[e,{factor:Math.round(n.factor*1e3)/1e3,samples:n.samples}]))}};function jn(a,e,n){let i=In(a),r=Object.fromEntries(Pn.map(o=>[o,0])),t=0;for(let o of Pn)for(let l of i[o]){let c=n.raw(l,e);t+=c,r[o]+=Math.round(c*n.factor(e))}let s=a.filter(o=>o.part==="tool_output").map(o=>n.count(o.get()??"",e));return{parts:r,total:Object.values(r).reduce((o,l)=>o+l,0),raw:t,toolOutputs:s}}function Eo(a){let e=a.trim();if(!/^[[{]/.test(e)||!e.includes(`
3293
3293
  `))return null;try{let n=JSON.stringify(JSON.parse(e));return n.length<a.length?n:null}catch{return null}}function Ao(a,e=3){let n=a.split(`
3294
3294
  `);if(n.length<e)return null;let i=[],r=!1;for(let t=0;t<n.length;){let s=t+1;for(;s<n.length&&n[s]===n[t];)s++;i.push(n[t]),s-t>=e&&n[t].trim()?(i.push(`[previous line repeated ${s-t-1} more times]`),r=!0):i.push(...n.slice(t+1,s)),t=s}return r?i.join(`
3295
3295
  `):null}function To(a){let e;try{e=JSON.parse(a)}catch{return null}let n=o=>!!o&&typeof o=="object"&&!Array.isArray(o),i=!1,r=o=>{if(Array.isArray(o)){let l=o.map(r);if(l.length>=5&&l.every(n)){let c=Object.keys(l[0]);if(c.length>0&&l.every(g=>Object.keys(g).length===c.length&&c.every(f=>f in g)))return i=!0,{_columns:c,_rows:l.map(g=>c.map(f=>g[f]))}}return l}return n(o)?Object.fromEntries(Object.entries(o).map(([l,c])=>[l,r(c)])):o},t=r(e);if(!i)return null;let s=JSON.stringify(t);return s.length<=JSON.stringify(e).length*.9?s:null}function Co(a,e){let n;try{n=JSON.parse(a)}catch{return null}let i=(o,l=[],c=0)=>{if(l.length>=200||c>6)return l;if(typeof o=="string"&&o.trim().length>=6)l.push(o.trim().slice(0,24));else if(typeof o=="number"&&String(Math.abs(Math.trunc(o))).length>=4)l.push(String(o));else if(Array.isArray(o))for(let u of o)i(u,l,c+1);else if(o&&typeof o=="object")for(let u of Object.values(o))i(u,l,c+1);return l},r=o=>{let l=[];for(let[c,u]of Object.entries(o)){if(JSON.stringify(u).length<120||c.length>=4&&e.includes(c))continue;let g=i(u);!g.length||g.some(f=>e.includes(f))||(l.push(c),delete o[c])}return l.length&&(o._omitted_by_token_lens=l),l.length},t=o=>!!o&&typeof o=="object"&&!Array.isArray(o),s=0;if(Array.isArray(n))for(let o of n)s+=t(o)?r(o):0;else if(t(n)){for(let o of Object.values(n))if(Array.isArray(o))for(let l of o)s+=t(l)?r(l):0;s+=r(n)}return s?JSON.stringify(n):null}function Ro(a,e,n){let i=a.split(/(\n[ \t]*\n)/),r=!1,t=new Set;for(let s=0;s<i.length;s+=2){let o=i[s].trim().replace(/\s+/g," ");if(o.length<200||o.startsWith("[Token Lens:"))continue;let l=n.get(o);t.has(o)?(i[s]="[Token Lens: same passage as earlier in this message]",r=!0):l!==void 0&&l!==e?(i[s]=`[Token Lens: same passage as in message ${l} above]`,r=!0):(l===void 0&&n.set(o,e),t.add(o))}return r?i.join(""):null}function aa(a,e,n,i,r){let t=0,s=0;for(let o of a){let l=o.get();if(!o.set||typeof l!="string")continue;let c=i(l,o);if(c===null||c===l)continue;let u=f=>f.split(`
@@ -3297,11 +3297,11 @@ ${" ".repeat(e)}}`;default:return"any"}}function ws(a){let e=["namespace functio
3297
3297
  `)[0].slice(0,160);return`[Token Lens: earlier tool output omitted, about ${z} tokens. It began: ${K}]`},s)}(e.optIn.compactJson||t.has("compact-json"))&&aa(o,a,"compact-json",w=>To(w),s);let c=e.optIn.trimToolFields||(t.has("trim-tool-fields")?{keepLast:3}:!1);if(c){let w=[...new Set(o.map(L=>L.turn))].sort((L,z)=>L-z),N=w.length>c.keepLast?w[w.length-c.keepLast]:-1/0,F=L=>[...n.refs.filter(z=>z.turn>L&&z.part!=="tool_output").map(z=>z.get()),...n.toolCalls.filter(z=>z.turn>L).map(z=>z.args)].join(`
3298
3298
  `);aa(o.filter(L=>L.turn<N),a,"trim-tool-fields",(L,z)=>Co(L,F(z.turn)),s)}let u=[],g=a.level==="auto"?"balanced":a.level,f,C=n.refs.filter(w=>w.part==="prompt").map(w=>w.get()).join(`
3299
3299
 
3300
- `);if(C.trim()){let w=ga(C),N=a.counter.count(C,i);f=w.task,u=Fa({profile:i,task:w.task,signals:w.signals,promptTokens:N,usage:Ba(a.conversationTokens,N,i),text:C})}return e.safe.prompt&&aa(n.refs.filter(w=>w.user),a,"prompt",w=>ea(w,{profile:i,task:ga(w).task,level:g}).text,s),{actions:s,tips:u,level:g,task:f}}function ur(a,e){let{cfg:n,parsed:i,body:r}=a;if(!n.safe.promptCaching||a.prefixRepeats<1)return null;let t=e.parts;return t.system+t.tools+t.history+t.tool_output<zo?null:i.protocol==="anthropic"?Po(r)?{id:"prompt-caching",saved:0}:null:!(a.upstream.type==="anthropic"||(a.upstream.promptCacheKey??/(^|\.)api\.openai\.com$/.test(new URL(a.upstream.baseUrl).hostname)))||r.prompt_cache_key!==void 0?null:(r.prompt_cache_key=`tl-${_o("sha256").update(a.runId).digest("hex").slice(0,24)}`,{id:"prompt-caching",saved:0})}import{randomBytes as Io}from"node:crypto";var xo=a=>a.batch??(a.type==="anthropic"||/(^|\.)api\.openai\.com$/.test(new URL(a.baseUrl).hostname)),Mo=["/v1/chat/completions","/v1/responses","/v1/embeddings"],On=class{constructor(e,n){this.deps=e;this.opts=n}deps;opts;jobs=new Map;queued=new Map;submitted=[];timer;busy=!1;canBatch(e,n,i){return xo(i)?(i.type==="anthropic"?e!=="anthropic":!Mo.includes(n))?`Batching isn't available for ${n}.`:null:`This upstream has no batch option. Send the request without x-token-lens-batch, or set "batch": true on an upstream that supports OpenAI's Batch API.`}add(e,n,i){let{stream:r,...t}=e.body,s={...e,body:t,id:`tlb_${Io(12).toString("hex")}`,status:"queued",createdAt:Date.now()};this.jobs.set(s.id,s);let o=[e.upstream,e.path,JSON.stringify(i)].join("\0"),l=this.queued.get(o);return l||this.queued.set(o,l={upName:e.upstream,up:n,path:e.path,auth:i,jobs:[]}),l.jobs.push(s),l.jobs.length>=this.opts.maxRequests&&this.flush(),this.schedule(),s}take(e,n){let i=this.jobs.get(e);if(!(!i||i.app!==n))return(i.status==="completed"||i.status==="failed")&&this.jobs.delete(e),i}schedule(){if(this.timer)return;let e=(this.queued.size?this.opts.flushSeconds:this.opts.pollSeconds)*1e3;this.timer=setTimeout(()=>{this.timer=void 0,this.tick().finally(()=>{(this.queued.size||this.submitted.length)&&this.schedule()})},e),this.timer.unref?.()}async tick(){if(!this.busy){this.busy=!0;try{await this.flush(),await this.poll();for(let[e,n]of this.jobs)Date.now()-n.createdAt>864e5&&n.status!=="submitted"&&n.status!=="queued"&&this.jobs.delete(e)}finally{this.busy=!1}}}stop(){this.timer&&clearTimeout(this.timer),this.timer=void 0}async flush(){let e=[...this.queued.values()];this.queued.clear();for(let n of e)try{let i=n.up.type==="anthropic"?await this.submitAnthropic(n):await this.submitOpenAI(n);for(let r of n.jobs)r.status="submitted";this.submitted.push({group:n,batchId:i})}catch(i){this.fail(n.jobs,`Submitting the batch failed: ${i.message}`)}}async poll(){let e=[];for(let n of this.submitted)try{(n.group.up.type==="anthropic"?await this.collectAnthropic(n.group,n.batchId):await this.collectOpenAI(n.group,n.batchId))||e.push(n)}catch(i){this.deps.log(JSON.stringify({ts:new Date().toISOString(),level:"error",batch:n.batchId,error:String(i.message??i)})),e.push(n)}this.submitted=e}async call(e,n,i={}){let r={...this.deps.headers(e.up,e.auth),...i.json!==void 0?{"content-type":"application/json"}:{}},t=await this.deps.fetch(n.startsWith("http")?n:this.deps.url(e.up,n),{method:i.method??(i.json!==void 0||i.body?"POST":"GET"),headers:r,body:i.json!==void 0?JSON.stringify(i.json):i.body}),s=await t.text();if(!t.ok)throw new Error(`${t.status} ${s.slice(0,200)}`);return s}async submitOpenAI(e){let n=e.jobs.map(l=>JSON.stringify({custom_id:l.id,method:"POST",url:e.path,body:l.body})).join(`
3300
+ `);if(C.trim()){let w=ha(C),N=a.counter.count(C,i);f=w.task,u=Fa({profile:i,task:w.task,signals:w.signals,promptTokens:N,usage:Ba(a.conversationTokens,N,i),text:C})}return e.safe.prompt&&aa(n.refs.filter(w=>w.user),a,"prompt",w=>ea(w,{profile:i,task:ha(w).task,level:g}).text,s),{actions:s,tips:u,level:g,task:f}}function ur(a,e){let{cfg:n,parsed:i,body:r}=a;if(!n.safe.promptCaching||a.prefixRepeats<1)return null;let t=e.parts;return t.system+t.tools+t.history+t.tool_output<zo?null:i.protocol==="anthropic"?Po(r)?{id:"prompt-caching",saved:0}:null:!(a.upstream.type==="anthropic"||(a.upstream.promptCacheKey??/(^|\.)api\.openai\.com$/.test(new URL(a.upstream.baseUrl).hostname)))||r.prompt_cache_key!==void 0?null:(r.prompt_cache_key=`tl-${_o("sha256").update(a.runId).digest("hex").slice(0,24)}`,{id:"prompt-caching",saved:0})}import{randomBytes as Io}from"node:crypto";var xo=a=>a.batch??(a.type==="anthropic"||/(^|\.)api\.openai\.com$/.test(new URL(a.baseUrl).hostname)),Mo=["/v1/chat/completions","/v1/responses","/v1/embeddings"],On=class{constructor(e,n){this.deps=e;this.opts=n}deps;opts;jobs=new Map;queued=new Map;submitted=[];timer;busy=!1;canBatch(e,n,i){return xo(i)?(i.type==="anthropic"?e!=="anthropic":!Mo.includes(n))?`Batching isn't available for ${n}.`:null:`This upstream has no batch option. Send the request without x-token-lens-batch, or set "batch": true on an upstream that supports OpenAI's Batch API.`}add(e,n,i){let{stream:r,...t}=e.body,s={...e,body:t,id:`tlb_${Io(12).toString("hex")}`,status:"queued",createdAt:Date.now()};this.jobs.set(s.id,s);let o=[e.upstream,e.path,JSON.stringify(i)].join("\0"),l=this.queued.get(o);return l||this.queued.set(o,l={upName:e.upstream,up:n,path:e.path,auth:i,jobs:[]}),l.jobs.push(s),l.jobs.length>=this.opts.maxRequests&&this.flush(),this.schedule(),s}take(e,n){let i=this.jobs.get(e);if(!(!i||i.app!==n))return(i.status==="completed"||i.status==="failed")&&this.jobs.delete(e),i}schedule(){if(this.timer)return;let e=(this.queued.size?this.opts.flushSeconds:this.opts.pollSeconds)*1e3;this.timer=setTimeout(()=>{this.timer=void 0,this.tick().finally(()=>{(this.queued.size||this.submitted.length)&&this.schedule()})},e),this.timer.unref?.()}async tick(){if(!this.busy){this.busy=!0;try{await this.flush(),await this.poll();for(let[e,n]of this.jobs)Date.now()-n.createdAt>864e5&&n.status!=="submitted"&&n.status!=="queued"&&this.jobs.delete(e)}finally{this.busy=!1}}}stop(){this.timer&&clearTimeout(this.timer),this.timer=void 0}async flush(){let e=[...this.queued.values()];this.queued.clear();for(let n of e)try{let i=n.up.type==="anthropic"?await this.submitAnthropic(n):await this.submitOpenAI(n);for(let r of n.jobs)r.status="submitted";this.submitted.push({group:n,batchId:i})}catch(i){this.fail(n.jobs,`Submitting the batch failed: ${i.message}`)}}async poll(){let e=[];for(let n of this.submitted)try{(n.group.up.type==="anthropic"?await this.collectAnthropic(n.group,n.batchId):await this.collectOpenAI(n.group,n.batchId))||e.push(n)}catch(i){this.deps.log(JSON.stringify({ts:new Date().toISOString(),level:"error",batch:n.batchId,error:String(i.message??i)})),e.push(n)}this.submitted=e}async call(e,n,i={}){let r={...this.deps.headers(e.up,e.auth),...i.json!==void 0?{"content-type":"application/json"}:{}},t=await this.deps.fetch(n.startsWith("http")?n:this.deps.url(e.up,n),{method:i.method??(i.json!==void 0||i.body?"POST":"GET"),headers:r,body:i.json!==void 0?JSON.stringify(i.json):i.body}),s=await t.text();if(!t.ok)throw new Error(`${t.status} ${s.slice(0,200)}`);return s}async submitOpenAI(e){let n=e.jobs.map(l=>JSON.stringify({custom_id:l.id,method:"POST",url:e.path,body:l.body})).join(`
3301
3301
  `),i=new FormData;i.append("purpose","batch"),i.append("file",new Blob([n],{type:"application/jsonl"}),"token-lens-batch.jsonl");let r=this.deps.headers(e.up,e.auth);delete r["content-type"];let t=await this.deps.fetch(this.deps.url(e.up,"/v1/files"),{method:"POST",headers:r,body:i});if(!t.ok)throw new Error(`${t.status} ${(await t.text()).slice(0,200)}`);let s=await t.json();return JSON.parse(await this.call(e,"/v1/batches",{json:{input_file_id:s.id,endpoint:e.path,completion_window:"24h"}})).id}async collectOpenAI(e,n){let i=JSON.parse(await this.call(e,`/v1/batches/${n}`));if(["failed","expired","cancelled"].includes(i.status))return this.fail(e.jobs,`The provider's batch ended as "${i.status}".`),!0;if(i.status!=="completed")return!1;let r=new Map;for(let t of[i.output_file_id,i.error_file_id].filter(Boolean))for(let s of(await this.call(e,`/v1/files/${t}/content`)).split(`
3302
3302
  `))if(s.trim()){let o=JSON.parse(s);r.set(o.custom_id,o)}for(let t of e.jobs){let s=r.get(t.id);s?.response?.status_code>=200&&s.response.status_code<300?this.complete(t,s.response.body):this.fail([t],s?.error?.message??s?.response?.body?.error?.message??"No result for this request.")}return!0}async submitAnthropic(e){return JSON.parse(await this.call(e,"/v1/messages/batches",{json:{requests:e.jobs.map(i=>({custom_id:i.id,params:i.body}))}})).id}async collectAnthropic(e,n){let i=JSON.parse(await this.call(e,`/v1/messages/batches/${n}`));if(i.processing_status!=="ended")return!1;let r=new Map;if(i.results_url){for(let t of(await this.call(e,i.results_url)).split(`
3303
- `))if(t.trim()){let s=JSON.parse(t);r.set(s.custom_id,s.result)}}for(let t of e.jobs){let s=r.get(t.id);s?.type==="succeeded"?this.complete(t,s.message):this.fail([t],s?.error?.error?.message??s?.error?.message??`The request ${s?.type??"has no result"}.`)}return!0}complete(e,n){e.status="completed",e.response=n,e.onDone?.(fa(n??{}),!0)}fail(e,n){for(let i of e)i.status="failed",i.error=n,i.onDone?.({},!1)}};var Pi=a=>{if(typeof a!="string")return a??{};try{return JSON.parse(a)}catch{return a}};function Le(a,e){let n={text:"",toolCalls:[]};if(!e||typeof e!="object")return n;if(a==="anthropic")for(let i of e.content??[])i?.type==="text"?n.text+=i.text:i?.type==="tool_use"&&n.toolCalls.push({name:i.name,args:i.input??{}});else if(a==="openai-responses")for(let i of e.output??[]){if(i?.type==="message")for(let r of i.content??[])typeof r?.text=="string"&&(n.text+=r.text);i?.type==="function_call"&&n.toolCalls.push({name:i.name,args:Pi(i.arguments)})}else{let i=e.choices?.[0]?.message??{};typeof i.content=="string"&&(n.text=i.content);for(let r of i.tool_calls??[])n.toolCalls.push({name:r?.function?.name,args:Pi(r?.function?.arguments)})}return n}var Dn=a=>a&&typeof a=="object"?Array.isArray(a)?`[${a.map(Dn).join(",")}]`:`{${Object.keys(a).sort().map(e=>`${JSON.stringify(e)}:${Dn(a[e])}`).join(",")}}`:JSON.stringify(a),jo=a=>a.toLowerCase().match(/[\p{L}\p{N}]+/gu)??[];function Oo(a,e){if(a.trim()===e.trim())return 1;let n=c=>{let u=jo(c),g=new Map,f=h=>g.set(h,(g.get(h)??0)+1);return u.forEach((h,C)=>{f(h),C&&f(`${u[C-1]} ${h}`)}),g},i=n(a),r=n(e),t=0,s=0;for(let[c,u]of i)t+=Math.min(u,r.get(c)??0);for(let c of i.values())s+=c;for(let c of r.values())s+=c;let o=s?2*t/s:1,l=c=>(c.match(/\d+(?:[.,]\d+)*/g)??[]).sort().join(" ");return l(a)===l(e)?o:Math.min(o,.5)}function Nn(a,e,n,i){let r=a.toolCalls.length===e.toolCalls.length&&a.toolCalls.every((s,o)=>s.name===e.toolCalls[o].name&&Dn(s.args)===Dn(e.toolCalls[o].args)),t=Math.round(Oo(a.text,e.text)*1e3)/1e3;return{at:new Date().toISOString(),sameToolCalls:r,similarity:t,match:r&&t>=i,saved:n}}function dr(a){let e=a.length;return{checked:e,matched:a.filter(n=>n.match).length,sameToolCalls:a.filter(n=>n.sameToolCalls).length,averageSimilarity:e?Math.round(a.reduce((n,i)=>n+i.similarity,0)/e*1e3)/1e3:0,savedOnChecked:a.reduce((n,i)=>n+i.saved,0)}}function mr(a,e){let n=[];for(let t of e.split(`
3304
- `))if(t.startsWith("data:"))try{n.push(JSON.parse(t.slice(5)))}catch{}if(a==="openai-responses"){let t=n.find(s=>s?.type==="response.completed")?.response;return t?Le(a,t):{text:n.filter(s=>s?.type==="response.output_text.delta").map(s=>s.delta??"").join(""),toolCalls:[]}}let i={text:"",toolCalls:[]},r=new Map;if(a==="anthropic"){for(let t of n)if(t?.type==="content_block_start"&&t.content_block?.type==="tool_use")r.set(t.index,{name:t.content_block.name,args:""});else if(t?.type==="content_block_start"&&t.content_block?.type==="text")i.text+=t.content_block.text??"";else if(t?.type==="content_block_delta"&&t.delta?.type==="text_delta")i.text+=t.delta.text??"";else if(t?.type==="content_block_delta"&&t.delta?.type==="input_json_delta"){let s=r.get(t.index);s&&(s.args+=t.delta.partial_json??"")}}else for(let t of n){let s=t?.choices?.[0]?.delta;if(s){typeof s.content=="string"&&(i.text+=s.content);for(let o of s.tool_calls??[]){let l=r.get(o.index??0)??{name:"",args:""};o.function?.name&&(l.name+=o.function.name),o.function?.arguments&&(l.args+=o.function.arguments),r.set(o.index??0,l)}}}for(let[,t]of[...r].sort((s,o)=>s[0]-o[0]))i.toolCalls.push({name:t.name,args:t.args.trim()?Pi(t.args):{}});return i}var ba="token_lens_load_tools",zi="Some tools above are listed by name and a short summary only. Before calling any of them, call this tool with the names of the tools you need; you will then get their full definitions.",pr=(a,e)=>a==="openai-chat"?e?.function?.name:e?.name,Do=(a,e)=>a==="anthropic"?!e?.type||e.type==="custom":e?.type==="function",Ii=a=>{let n=(typeof a=="string"?a.trim():"").split(/(?<=[.!?])\s|\n/)[0]??"";return n.length>120?`${n.slice(0,117)}...`:n},xi={type:"object",properties:{}};function No(a,e){return a==="openai-chat"?{type:"function",function:{name:e.function.name,description:Ii(e.function.description),parameters:xi}}:a==="openai-responses"?{type:"function",name:e.name,description:Ii(e.description),parameters:xi}:{name:e.name,description:Ii(e.description),input_schema:xi}}function Lo(a,e){let n={type:"object",properties:{names:{type:"array",items:{type:"string",enum:e},description:"Tools to load."}},required:["names"]};return a==="openai-chat"?{type:"function",function:{name:ba,description:zi,parameters:n}}:a==="openai-responses"?{type:"function",name:ba,description:zi,parameters:n}:{name:ba,description:zi,input_schema:n}}function Bo(a,e){let n=e?.tool_choice;if(!(!n||typeof n!="object"))return a==="openai-chat"?n.function?.name:n.name}function gr(a,e,n){let i=Array.isArray(e?.tools)?e.tools:[];if(e?.stream||e?.tool_choice==="none"||i.length<n)return null;let r=i.filter(t=>Do(a,t)).map(t=>pr(a,t)).filter(Boolean);return r.length<n||r.includes(ba)?null:{original:i,lazy:r}}function hr(a,e,n,i){let r=Bo(a,e),t=[];return e.tools=n.original.map(s=>{let o=pr(a,s);return!o||!n.lazy.includes(o)||i.has(o)||o===r?s:(t.push(o),No(a,s))}),t.length?e.tools.push(Lo(a,t)):e.tools=n.original,t}function fr(a,e,n){let i=new Set;for(let r of Le(a,e).toolCalls)if(r.name===ba){let t=r.args?.names;for(let s of Array.isArray(t)?t:[])n.includes(s)&&i.add(s);if(!i.size)for(let s of n)i.add(s)}else n.includes(r.name)&&i.add(r.name);return[...i]}function Mi(a,e){let n={...a};for(let[i,r]of Object.entries(e))typeof r=="number"&&(n[i]=(n[i]??0)+r);return n}var br=a=>{if(typeof a!="string")return a??{};try{return JSON.parse(a)}catch{return{}}},Qa=a=>typeof a=="string"?a:Array.isArray(a)?a.map(e=>typeof e=="string"?e:e?.type==="text"?e.text:"").join(""):"";function kr(a){let e=[],n=Qa(a.system);n&&e.push({role:"system",content:n});for(let s of a.messages??[]){let o=typeof s.content=="string"?[{type:"text",text:s.content}]:Array.isArray(s.content)?s.content:[];if(s.role==="assistant"){let u=o.filter(f=>f.type==="text").map(f=>f.text).join(""),g=o.filter(f=>f.type==="tool_use").map(f=>({id:f.id,type:"function",function:{name:f.name,arguments:JSON.stringify(f.input??{})}}));e.push({role:"assistant",content:u||null,...g.length?{tool_calls:g}:{}});continue}let l=[],c=()=>{l.length&&e.push({role:"user",content:l.every(u=>u.type==="text")?l.map(u=>u.text).join(`
3303
+ `))if(t.trim()){let s=JSON.parse(t);r.set(s.custom_id,s.result)}}for(let t of e.jobs){let s=r.get(t.id);s?.type==="succeeded"?this.complete(t,s.message):this.fail([t],s?.error?.error?.message??s?.error?.message??`The request ${s?.type??"has no result"}.`)}return!0}complete(e,n){e.status="completed",e.response=n,e.onDone?.(ba(n??{}),!0)}fail(e,n){for(let i of e)i.status="failed",i.error=n,i.onDone?.({},!1)}};var Pi=a=>{if(typeof a!="string")return a??{};try{return JSON.parse(a)}catch{return a}};function Le(a,e){let n={text:"",toolCalls:[]};if(!e||typeof e!="object")return n;if(a==="anthropic")for(let i of e.content??[])i?.type==="text"?n.text+=i.text:i?.type==="tool_use"&&n.toolCalls.push({name:i.name,args:i.input??{}});else if(a==="openai-responses")for(let i of e.output??[]){if(i?.type==="message")for(let r of i.content??[])typeof r?.text=="string"&&(n.text+=r.text);i?.type==="function_call"&&n.toolCalls.push({name:i.name,args:Pi(i.arguments)})}else{let i=e.choices?.[0]?.message??{};typeof i.content=="string"&&(n.text=i.content);for(let r of i.tool_calls??[])n.toolCalls.push({name:r?.function?.name,args:Pi(r?.function?.arguments)})}return n}var Dn=a=>a&&typeof a=="object"?Array.isArray(a)?`[${a.map(Dn).join(",")}]`:`{${Object.keys(a).sort().map(e=>`${JSON.stringify(e)}:${Dn(a[e])}`).join(",")}}`:JSON.stringify(a),jo=a=>a.toLowerCase().match(/[\p{L}\p{N}]+/gu)??[];function Oo(a,e){if(a.trim()===e.trim())return 1;let n=c=>{let u=jo(c),g=new Map,f=h=>g.set(h,(g.get(h)??0)+1);return u.forEach((h,C)=>{f(h),C&&f(`${u[C-1]} ${h}`)}),g},i=n(a),r=n(e),t=0,s=0;for(let[c,u]of i)t+=Math.min(u,r.get(c)??0);for(let c of i.values())s+=c;for(let c of r.values())s+=c;let o=s?2*t/s:1,l=c=>(c.match(/\d+(?:[.,]\d+)*/g)??[]).sort().join(" ");return l(a)===l(e)?o:Math.min(o,.5)}function Nn(a,e,n,i){let r=a.toolCalls.length===e.toolCalls.length&&a.toolCalls.every((s,o)=>s.name===e.toolCalls[o].name&&Dn(s.args)===Dn(e.toolCalls[o].args)),t=Math.round(Oo(a.text,e.text)*1e3)/1e3;return{at:new Date().toISOString(),sameToolCalls:r,similarity:t,match:r&&t>=i,saved:n}}function dr(a){let e=a.length;return{checked:e,matched:a.filter(n=>n.match).length,sameToolCalls:a.filter(n=>n.sameToolCalls).length,averageSimilarity:e?Math.round(a.reduce((n,i)=>n+i.similarity,0)/e*1e3)/1e3:0,savedOnChecked:a.reduce((n,i)=>n+i.saved,0)}}function mr(a,e){let n=[];for(let t of e.split(`
3304
+ `))if(t.startsWith("data:"))try{n.push(JSON.parse(t.slice(5)))}catch{}if(a==="openai-responses"){let t=n.find(s=>s?.type==="response.completed")?.response;return t?Le(a,t):{text:n.filter(s=>s?.type==="response.output_text.delta").map(s=>s.delta??"").join(""),toolCalls:[]}}let i={text:"",toolCalls:[]},r=new Map;if(a==="anthropic"){for(let t of n)if(t?.type==="content_block_start"&&t.content_block?.type==="tool_use")r.set(t.index,{name:t.content_block.name,args:""});else if(t?.type==="content_block_start"&&t.content_block?.type==="text")i.text+=t.content_block.text??"";else if(t?.type==="content_block_delta"&&t.delta?.type==="text_delta")i.text+=t.delta.text??"";else if(t?.type==="content_block_delta"&&t.delta?.type==="input_json_delta"){let s=r.get(t.index);s&&(s.args+=t.delta.partial_json??"")}}else for(let t of n){let s=t?.choices?.[0]?.delta;if(s){typeof s.content=="string"&&(i.text+=s.content);for(let o of s.tool_calls??[]){let l=r.get(o.index??0)??{name:"",args:""};o.function?.name&&(l.name+=o.function.name),o.function?.arguments&&(l.args+=o.function.arguments),r.set(o.index??0,l)}}}for(let[,t]of[...r].sort((s,o)=>s[0]-o[0]))i.toolCalls.push({name:t.name,args:t.args.trim()?Pi(t.args):{}});return i}var ka="token_lens_load_tools",zi="Some tools above are listed by name and a short summary only. Before calling any of them, call this tool with the names of the tools you need; you will then get their full definitions.",pr=(a,e)=>a==="openai-chat"?e?.function?.name:e?.name,Do=(a,e)=>a==="anthropic"?!e?.type||e.type==="custom":e?.type==="function",Ii=a=>{let n=(typeof a=="string"?a.trim():"").split(/(?<=[.!?])\s|\n/)[0]??"";return n.length>120?`${n.slice(0,117)}...`:n},xi={type:"object",properties:{}};function No(a,e){return a==="openai-chat"?{type:"function",function:{name:e.function.name,description:Ii(e.function.description),parameters:xi}}:a==="openai-responses"?{type:"function",name:e.name,description:Ii(e.description),parameters:xi}:{name:e.name,description:Ii(e.description),input_schema:xi}}function Lo(a,e){let n={type:"object",properties:{names:{type:"array",items:{type:"string",enum:e},description:"Tools to load."}},required:["names"]};return a==="openai-chat"?{type:"function",function:{name:ka,description:zi,parameters:n}}:a==="openai-responses"?{type:"function",name:ka,description:zi,parameters:n}:{name:ka,description:zi,input_schema:n}}function Bo(a,e){let n=e?.tool_choice;if(!(!n||typeof n!="object"))return a==="openai-chat"?n.function?.name:n.name}function gr(a,e,n){let i=Array.isArray(e?.tools)?e.tools:[];if(e?.stream||e?.tool_choice==="none"||i.length<n)return null;let r=i.filter(t=>Do(a,t)).map(t=>pr(a,t)).filter(Boolean);return r.length<n||r.includes(ka)?null:{original:i,lazy:r}}function hr(a,e,n,i){let r=Bo(a,e),t=[];return e.tools=n.original.map(s=>{let o=pr(a,s);return!o||!n.lazy.includes(o)||i.has(o)||o===r?s:(t.push(o),No(a,s))}),t.length?e.tools.push(Lo(a,t)):e.tools=n.original,t}function fr(a,e,n){let i=new Set;for(let r of Le(a,e).toolCalls)if(r.name===ka){let t=r.args?.names;for(let s of Array.isArray(t)?t:[])n.includes(s)&&i.add(s);if(!i.size)for(let s of n)i.add(s)}else n.includes(r.name)&&i.add(r.name);return[...i]}function Mi(a,e){let n={...a};for(let[i,r]of Object.entries(e))typeof r=="number"&&(n[i]=(n[i]??0)+r);return n}var br=a=>{if(typeof a!="string")return a??{};try{return JSON.parse(a)}catch{return{}}},Qa=a=>typeof a=="string"?a:Array.isArray(a)?a.map(e=>typeof e=="string"?e:e?.type==="text"?e.text:"").join(""):"";function kr(a){let e=[],n=Qa(a.system);n&&e.push({role:"system",content:n});for(let s of a.messages??[]){let o=typeof s.content=="string"?[{type:"text",text:s.content}]:Array.isArray(s.content)?s.content:[];if(s.role==="assistant"){let u=o.filter(f=>f.type==="text").map(f=>f.text).join(""),g=o.filter(f=>f.type==="tool_use").map(f=>({id:f.id,type:"function",function:{name:f.name,arguments:JSON.stringify(f.input??{})}}));e.push({role:"assistant",content:u||null,...g.length?{tool_calls:g}:{}});continue}let l=[],c=()=>{l.length&&e.push({role:"user",content:l.every(u=>u.type==="text")?l.map(u=>u.text).join(`
3305
3305
  `):[...l]}),l.length=0};for(let u of o)if(u.type==="tool_result"){c();let g=typeof u.content=="string"?u.content:Qa(u.content);e.push({role:"tool",tool_call_id:u.tool_use_id,content:u.is_error?`Error: ${g}`:g})}else if(u.type==="text")l.push({type:"text",text:u.text});else if(u.type==="image"){let g=u.source??{},f=g.type==="base64"?`data:${g.media_type};base64,${g.data}`:g.url;f&&l.push({type:"image_url",image_url:{url:f}})}c()}let i={model:a.model,messages:e};a.max_tokens!==void 0&&(i.max_tokens=a.max_tokens);for(let s of["temperature","top_p","stream"])a[s]!==void 0&&(i[s]=a[s]);a.stop_sequences&&(i.stop=a.stop_sequences),a.metadata?.user_id&&(i.user=a.metadata.user_id),a.stream&&(i.stream_options={include_usage:!0});let r=(a.tools??[]).filter(s=>!s.type||s.type==="custom");r.length&&(i.tools=r.map(s=>({type:"function",function:{name:s.name,description:s.description,parameters:s.input_schema??{type:"object",properties:{}}}})));let t=a.tool_choice;return t&&r.length&&(t.type==="auto"?i.tool_choice="auto":t.type==="any"?i.tool_choice="required":t.type==="none"?i.tool_choice="none":t.type==="tool"&&(i.tool_choice={type:"function",function:{name:t.name}}),t.disable_parallel_tool_use&&(i.parallel_tool_calls=!1)),i}function yr(a){let e=[],n=[],i=(l,c)=>{let u=n.at(-1);u?.role===l?u.content.push(...c):n.push({role:l,content:c})};for(let l of a.messages??[])if(l.role==="system"||l.role==="developer")e.push(Qa(l.content));else if(l.role==="tool")i("user",[{type:"tool_result",tool_use_id:l.tool_call_id,content:Qa(l.content)}]);else if(l.role==="assistant"){let c=[],u=Qa(l.content);u&&c.push({type:"text",text:u});for(let g of l.tool_calls??[])c.push({type:"tool_use",id:g.id,name:g.function?.name,input:br(g.function?.arguments)});c.length&&i("assistant",c)}else{let c=typeof l.content=="string"?[{type:"text",text:l.content}]:Array.isArray(l.content)?l.content:[],u=[];for(let g of c)if(g.type==="text")u.push({type:"text",text:g.text});else if(g.type==="image_url"){let f=typeof g.image_url=="string"?g.image_url:g.image_url?.url,h=/^data:([^;,]+);base64,(.*)$/s.exec(f??"");u.push(h?{type:"image",source:{type:"base64",media_type:h[1],data:h[2]}}:{type:"image",source:{type:"url",url:f}})}u.length&&i("user",u)}let r={model:a.model,max_tokens:a.max_completion_tokens??a.max_tokens??4096,messages:n},t=typeof a.prompt_cache_key=="string";e.length&&(r.system=t?[{type:"text",text:e.join(`
3306
3306
 
3307
3307
  `),cache_control:{type:"ephemeral"}}]:e.join(`
@@ -3359,17 +3359,17 @@ ${Go(e)}
3359
3359
  `)+`\r
3360
3360
  `}import{createHash as Vo}from"node:crypto";var en=(...a)=>Vo("sha256").update(a.join("\0")).digest("hex").slice(0,16),xr=500,Bn=class{constructor(e=2e3){this.maxRuns=e}maxRuns;runs=new Map;get(e){return this.runs.get(e)}open(e,n,i,r="-"){let t=this.runs.get(e);if(t)this.runs.delete(e);else{let s=new Date().toISOString();t={id:e,app:n,agent:r,named:i,firstSeen:s,lastSeen:s,calls:[],prefixes:new Map,requestHashes:new Set,checks:[]}}for(this.runs.set(e,t);this.runs.size>this.maxRuns;)this.runs.delete(this.runs.keys().next().value);return t}record(e,n){e.calls.push(n),e.calls.length>xr&&e.calls.splice(0,e.calls.length-xr),e.lastSeen=n.at}list(){return[...this.runs.values()].reverse()}};function Wo(a,e){switch(a){case"repeated-prefix":return[...e.cachingOn?[]:[{where:"model server",what:"On a self-hosted model, cache the shared prefix (hosted OpenAI, Anthropic and Gemini models are cached by the gateway or the provider)",apply:"vllm serve <model> --enable-prefix-caching"}],{where:"agent code",what:"Keep the system prompt and tool list byte-identical between calls: no timestamps, ids or reordered tools in them",apply:"const SYSTEM = buildSystemPrompt(); // build once per run, reuse on every call"},...e.bigTools?[{where:"request header",what:"Send unused tools as a one-line summary and load full definitions only when the agent asks for them (opt-in; output checks run on a sample)",apply:"x-token-lens-allow: lazy-tools"}]:[],...e.bigTools?[{where:"agent code",what:"Send only the tools this step can use",apply:"tools: ALL_TOOLS.filter((t) => toolsForStep(step).includes(t.name))"}]:[]];case"growing-history":return[{where:"agent code",what:"Keep the last few turns plus a running summary instead of the full transcript",apply:'messages = [system, { role: "user", content: `Progress so far: ${summary}` }, ...messages.slice(-6)]'},{where:"system prompt",what:"Ask the model to restate only what the next step needs",apply:"When you finish a step, end with one line: STATE: <facts the next step needs>."}];case"tool-outputs":return[{where:"request header",what:"Leave out fields of older tool outputs the agent never referred to (opt-in; answers checked before it applies)",apply:"x-token-lens-allow: trim-tool-fields"},{where:"request header",what:"Replace older tool outputs with a stub (opt-in; answers checked before it applies)",apply:"x-token-lens-allow: elide-tool-outputs"},{where:"gateway config",what:"The same for every app",apply:'{ "optIn": { "elideOldToolOutputs": { "keepLast": 3, "minTokens": 300 } } }'},{where:"agent code",what:"Return only the fields the agent uses from each tool",apply:'return JSON.stringify(pick(result, ["id", "status", "total"])); // not the whole record, no logs'}];case"large-context":return[{where:"agent code",what:"Retrieve fewer, smaller chunks and drop near-duplicates",apply:"const chunks = dedupe(await search(query, { topK: 4, maxTokensPerChunk: 400 }));"},{where:"agent code",what:"Put a document reused across calls before the question, so it is cached",apply:'messages = [system, { role: "user", content: doc }, { role: "user", content: question }]'}];case"retries":return[{where:"gateway config",what:"Answer identical retries from the gateway's copy (temperature-0 calls)",apply:'{ "safe": { "dedupeRetries": true }, "retryWindowSeconds": 600 }'},{where:"agent code",what:"Retry only on rate limits and server errors, with backoff, never on a parse error with the same input",apply:"if (err.status === 429 || err.status >= 500) await sleep(2 ** attempt * 1000); else throw err;"}];case"reasoning-heavy":return[{where:"agent code",what:"Lower reasoning effort on simple steps (routing, extraction, formatting) on the same model, with output checks on",apply:'reasoning_effort: step.simple ? "low" : "high" // Anthropic: thinking: { type: "enabled", budget_tokens: step.simple ? 1024 : 8000 }'}];case"repeated-tool-calls":return[{where:"agent code",what:`Cache tool results for the run${e.repeat?`, so ${e.repeat.name} with the same input runs once`:""}`,apply:"const cache = new Map(); const run = (name, args) => { const k = name + JSON.stringify(args); if (!cache.has(k)) cache.set(k, tools[name](args)); return cache.get(k); };"},{where:"system prompt",what:"Tell the agent results don't change within a run",apply:"Tool results stay valid for this whole task. Do not call a tool again with the same input; use the earlier result."}];case"loop":return[{where:"gateway config",what:"Cap every run so a loop stops instead of spending",apply:`{ "runBudget": { "maxCalls": ${Math.max(10,e.steps*2)}, "maxCostUsd": 1 } }`},{where:"request header",what:"Or cap one agent's runs",apply:`x-token-lens-max-calls: ${Math.max(10,e.steps*2)}`},{where:"agent code",what:"Give the agent a step limit and stop when a call repeats",apply:`if (step >= MAX_STEPS || sameAsLastCall(call)) break;${e.loop?` // stuck on ${e.loop.name}`:""}`}];case"output-heavy":return[{where:"agent code",what:"Cap output on intermediate steps; leave the final answer as is",apply:"max_tokens: step.final ? undefined : 400"},{where:"system prompt",what:"Ask intermediate steps for brief, structured output",apply:'For intermediate steps reply only with JSON: {"next": ..., "facts": [...]}. No explanations.'}];case"images":return[{where:"agent code",what:"Once the model has described or used an image, keep the description in history instead of the image",apply:'messages = messages.map((m, i) => (i < messages.length - 2 && hasImage(m) ? withoutImages(m, "[screenshot: " + m.description + "]") : m));'},{where:"agent code",what:"Crop screenshots to the region the step needs before sending",apply:"const shot = await page.screenshot({ clip: { x, y, width: 800, height: 600 } });"},{where:"agent code",what:"Where fine detail isn't needed, ask for low detail (OpenAI; can change answers, so check with output checks on)",apply:'{ "type": "image_url", "image_url": { "url": dataUrl, "detail": "low" } }'}];case"cache-breaker":return e.breakKind==="reordered tools"||e.breakKind==="changed tools"?[{where:"agent code",what:"Send the tool list in one fixed order, with fixed descriptions",apply:"tools: [...TOOLS].sort((a, b) => a.name.localeCompare(b.name)) // built once, not per call"}]:[{where:"agent code",what:`Move the ${e.breakKind==="changed text"?"part that changes":e.breakKind} out of the ${e.breakWhere??"system prompt"} and into the newest message, so everything before it stays identical`,apply:'system: STATIC_SYSTEM, // no time, ids or per-call values here\nmessages: [...history, { role: "user", content: `${question}\\n\\nCurrent time: ${new Date().toISOString()}` }]'},{where:"system prompt",what:"If the model needs the date, give it at day precision so it changes once a day, not every call",apply:"Today's date is ${new Date().toISOString().slice(0, 10)}."}];case"json-retries":{let n=e.api??"openai-chat";return[{where:"agent code",what:"Ask for the provider's strict structured output on the same model: the reply always parses, so no retry is needed (run with output checks on)",apply:n==="anthropic"?`tools: [{ name: "result", description: "Return the result", input_schema: RESULT_SCHEMA }],
3361
3361
  tool_choice: { type: "tool", name: "result" } // read the answer from the tool_use block's input`:n==="openai-responses"?'text: { format: { type: "json_schema", name: "result", strict: true, schema: RESULT_SCHEMA } }':'response_format: { type: "json_schema", json_schema: { name: "result", strict: true, schema: RESULT_SCHEMA } }'},{where:"agent code",what:"If a reply still doesn't parse, repair it locally before asking the model again",apply:"const data = JSON.parse(jsonrepair(text)); // npm i jsonrepair; only re-ask if this throws"}]}case"shared-reads":return[{where:"agent code",what:"Read shared files once in the orchestrator and pass them to every sub-agent",apply:`const docs = await Promise.all(paths.map(readOnce));
3362
- await Promise.all(subAgents.map((a) => a.run({ task, context: docs })));`},{where:"agent code",what:"Or share one read cache between sub-agents",apply:'const cache = new Map(); const readOnce = (p) => cache.get(p) ?? (cache.set(p, fs.promises.readFile(p, "utf8")), cache.get(p));'},{where:"agent code",what:"Put the shared files in the same place at the top of each sub-agent's prompt, so the provider's cache serves them after the first",apply:'system: [STATIC_SYSTEM, ...docs.map((d) => `<doc path="${d.path}">${d.text}</doc>`)].join("\\n")'}];case"price-tier":return[{where:"agent code",what:`Keep each request under ${$(e.tierAbove??2e5)} input tokens: hand off to a fresh call with a summary first`,apply:`if (estimateTokens(messages) > ${Math.round((e.tierAbove??2e5)*.9)}) messages = [system, { role: "user", content: await summarize(messages) }];`},{where:"request header",what:"Replace older tool outputs with a stub (opt-in; answers checked before it applies)",apply:"x-token-lens-allow: elide-tool-outputs"},{where:"agent code",what:"Retrieve fewer, smaller chunks",apply:"const chunks = await search(query, { topK: 4, maxTokensPerChunk: 400 });"}];case"near-context-limit":return[{where:"agent code",what:"Hand off to a fresh run with a summary before the window fills",apply:'if (usage.input_tokens > 0.7 * CONTEXT_WINDOW) messages = [system, { role: "user", content: await summarize(messages) }];'}];default:return[]}}var $=a=>Math.round(a).toLocaleString("en-US"),ka=(a,e)=>`${e?Math.round(a/e*100):0}%`,re=a=>a.reduce((e,n)=>e+n,0),Mr=a=>a.length>60?a.slice(0,57)+"...":a;function Ko(a){let e=new Map;for(let n of a)for(let i of n.actions){let r=e.get(i.id)??{saved:0,calls:0};r.saved+=i.saved,r.calls++,e.set(i.id,r)}return e}var $o=a=>`Send the header "x-token-lens-allow: ${a}" on this app's requests, or enable it for everyone in the gateway config.`;function Gn(a){let e=a.calls.filter(_=>!_.replayed&&_.status<400),n=[];if(!e.length)return[];let i=e.length,r=e[0],t=e[i-1],s=t.input||1,o=_=>i>1?(t.parts[_]-r.parts[_])/(i-1):0,l=Ko(a.calls),c=(_,O)=>{let Q=l.get(_);return Q?[`${O} (${Q.calls} of ${a.calls.length} calls${Q.saved?`, ${$(Q.saved)} tokens saved so far`:""})`]:[]},u=re(e.map(_=>_.usage.cachedInputTokens??0)),g=u?[`provider reports ${$(u)} input tokens served from cache`]:[],f=t.parts.system+t.parts.tools;i>1&&f>=1024&&n.push({id:"repeated-prefix",part:t.parts.tools>t.parts.system?"tools":"system",title:"The same system prompt and tool definitions are re-sent on every call",evidence:`${$(f)} tokens (${ka(f,s)} of the latest call) repeated on ${i} calls: ${$(f*i)} tokens in total.`,automatic:[...c("prompt-caching","prompt caching turned on for the repeated prefix"),...g],advice:[l.has("prompt-caching")||u?"":"Keep this prefix byte-identical between calls so the provider can cache it: OpenAI and Gemini do so automatically; on a self-hosted server turn on prefix caching (vLLM --enable-prefix-caching; SGLang has it on by default).",t.parts.tools>2e3?"Send only the tools this step can use; long tool descriptions are paid for on every call.":""].filter(Boolean).join(" ")||void 0,estSavedTokensPerCall:f*.9});let h=o("history");if(i>2&&h>300&&t.parts.history/s>=.3){let _=t.parts.history;n.push({id:"growing-history",part:"history",title:"Each call re-reads a growing history",evidence:`History grows about ${$(h)} tokens per call and is now ${$(_)} tokens (${ka(_,s)} of the latest call). Over ${i} calls the model re-read ${$(re(e.map(O=>O.parts.history)))} history tokens.`,automatic:[...c("prompt-caching","the history so far is cached between calls"),...g],advice:"Start long runs from a summary instead of the full transcript (Token Lens's handoff brief), or keep only the turns the next step needs. Either can change later answers, so check the output first.",estSavedTokensPerCall:_*.5})}let C=t.parts.tool_output/s;if(t.parts.tool_output>1e3&&(C>=.3||o("tool_output")>500)){let _=t.toolOutputs,O=re(_.slice(0,Math.max(0,_.length-3)).filter(ya=>ya>=300)),Q=l.get("elide-tool-outputs");n.push({id:"tool-outputs",part:"tool_output",title:"Tool outputs take a large share of every call",evidence:`${_.length} tool outputs, ${$(t.parts.tool_output)} tokens (${ka(t.parts.tool_output,s)} of the latest call)${i>1?`, growing about ${$(o("tool_output"))} tokens per call`:""}.`,automatic:[...c("minify-json","pretty-printed JSON compacted"),...c("collapse-repeats","repeated lines collapsed"),...Q?c("elide-tool-outputs","older tool outputs replaced with a stub"):[]],optIn:!Q&&O>0?{id:"elide-tool-outputs",how:$o("elide-tool-outputs"),estSavedTokensPerCall:O}:void 0,advice:"Have tools return only the fields the agent uses, and page or truncate large results at the source.",estSavedTokensPerCall:Q?0:O})}t.parts.context>2e3&&t.parts.context/s>=.4&&n.push({id:"large-context",part:"context",title:"Retrieved or pasted context dominates the newest message",evidence:`${$(t.parts.context)} tokens of material (${ka(t.parts.context,s)} of the latest call) against a ${$(t.parts.prompt)}-token instruction.`,automatic:[],advice:"Retrieve fewer or smaller chunks, drop near-duplicate chunks, or put a stable document before the question so it can be cached across calls.",estSavedTokensPerCall:t.parts.context*.3});let w=a.calls.filter(_=>_.duplicate);if(w.length){let _=a.calls.filter(O=>O.replayed).length;n.push({id:"retries",part:"calls",title:"Identical requests were sent more than once",evidence:`${w.length} of ${a.calls.length} calls repeated an earlier request word for word.`,automatic:_?[`${_} answered from the gateway's copy at no token cost`]:[],advice:_?void 0:"Only temperature-0 requests are replayed automatically. Check the retry loop: a retry after a timeout or a parse error re-pays the whole input.",estSavedTokensPerCall:_?0:re(w.map(O=>O.input))/a.calls.length})}let N=re(e.map(_=>_.usage.reasoningTokens??0)),F=re(e.map(_=>_.usage.outputTokens??0));N>2e3&&N>.6*F&&n.push({id:"reasoning-heavy",part:"output",title:"Hidden reasoning is most of the output",evidence:`${$(N)} of ${$(F)} output tokens (${ka(N,F)}) were reasoning the app never sees.`,automatic:[],advice:"Not changed automatically, since it could change answers. If some steps are simple (routing, extraction, formatting), try a lower reasoning effort on those steps with output checks on; the model stays the same.",estSavedTokensPerCall:0});let L=t.repeatedToolCalls,z=re(L.map(_=>_.count-1));if(z>0){let _=re(e.map(Q=>Q.input))/i,O=[...L].sort((Q,ya)=>ya.count-Q.count)[0];n.push({id:"repeated-tool-calls",part:"tool_output",title:"The agent calls the same tool with the same input more than once",evidence:`${z} repeated tool call${z>1?"s":""} in ${$(t.toolCalls)}; for example ${O.name}(${Mr(O.args)}) ${O.count} times. Each repeat is an extra step that re-reads about ${$(_)} tokens of context.`,automatic:c("dedupe-tool-outputs","identical results sent once, later copies point back to the first"),advice:"Only the agent's code can skip the repeated step: cache tool results for the run (keyed by tool and input), or say in the system prompt that results don't change within a run.",estSavedTokensPerCall:z*_/i})}let K=t.sameCallStreak;if(K&&K.times>=3){let _=re(a.calls.map(O=>O.costUsd));n.push({id:"loop",part:"calls",title:"The agent looks stuck in a loop",evidence:`Its last ${K.times} tool calls were all ${K.name}(${Mr(K.args)}). The run has made ${a.calls.length} calls${_?` costing $${_.toFixed(4)}`:""} so far.`,automatic:a.calls.some(O=>O.overBudget)?["the run budget stopped further calls"]:[],optIn:{id:"run-budget",how:'Cap runs with "runBudget" in the gateway config, or the x-token-lens-max-calls / x-token-lens-max-cost-usd headers; calls past the cap get an error instead of being sent.'},advice:"Give the agent a step limit and a stop condition, and handle a tool error by changing the input instead of repeating the call.",estSavedTokensPerCall:(K.times-1)*(re(e.map(O=>O.input))/i)/i})}let ia=re(e.map(_=>(_.usage.outputTokens??0)*(ue[_.profile]?.pricing?.outputPerMTok??0)))/1e6,se=re(e.map(_=>_.costUsd)),oe=re(e.map(_=>_.usage.outputTokens??0))/i;se>0&&ia/se>=.5&&oe>500&&n.push({id:"output-heavy",part:"output",title:"Generated output is most of this run's cost",evidence:`Output is ${ka(ia,se)} of the run's cost, about ${$(oe)} tokens per step.`,automatic:[],advice:"Not changed automatically, since it shapes the answer. Asking intermediate steps for structured or brief output (and setting max_tokens on them) usually cuts this without changing the final result; check it with output checks on.",estSavedTokensPerCall:0});let ae=t.images;ae&&ae.count&&(ae.tokens>=1500||ae.count>=3)&&n.push({id:"images",part:"context",title:"Images are re-sent on every call",evidence:`${ae.count} image${ae.count>1?"s":""} in the latest call, about ${$(ae.tokens)} tokens (estimated from their sizes), ${$(ae.oldTokens)} of them in older messages. ${ae.provider==="anthropic"?"Anthropic shrinks images to 1,568 px":"OpenAI shrinks images to 2,048 px and a 768 px short side"} before counting, so sending them smaller than that saves upload time, not tokens.`,automatic:c("dedupe-images","a repeated image sent once"),advice:"Once the model has used an image, keep its description instead of the image; crop to the part that matters.",estSavedTokensPerCall:ae.oldTokens});let Se=e.filter(_=>_.cacheBreak),fe=Se.at(-1)?.cacheBreak;if(fe&&Se.length>=Math.max(1,(i-1)/3)){let _=re(Se.map(Q=>Q.cacheBreak.tokens)),O=fe.kind==="changed text"?"text that changes":fe.kind==="number"?"a number that changes":`a ${fe.kind}`;n.push({id:"cache-breaker",part:fe.where==="tools"?"tools":"system",title:fe.kind.endsWith("tools")?`The tool list ${fe.kind==="reordered tools"?"comes in a different order":"changes"} between calls, so the provider can't reuse its cache`:`The ${fe.where} contains ${O}, so the provider can't reuse its cache`,evidence:`On ${Se.length} of ${i} calls the ${fe.where} started the same as the call before and then changed, so about ${$(_)} tokens that could have been read from cache (about a tenth of the price) were billed in full.`,automatic:[],advice:"Only the app can fix this, by keeping everything before the changing part identical between calls.",estSavedTokensPerCall:_/i*.9})}let Ke=t.jsonRetries??0;if(Ke>0&&!t.structuredOutput){let _=re(e.map(O=>O.input))/i;n.push({id:"json-retries",part:"calls",title:"The agent re-asks the model after its JSON didn't parse",evidence:`${Ke} time${Ke>1?"s":""} in this conversation the model's JSON reply didn't parse and the agent asked again; each retry is a whole extra step of about ${$(_)} tokens.`,automatic:[],advice:"Strict structured output on the same model makes every reply parse.",estSavedTokensPerCall:Ke*_/i})}let ve=a.sharedReads;if(ve&&ve.files>0){let _=t.toolOutputs.length?t.parts.tool_output/t.toolOutputs.length:0;n.push({id:"shared-reads",part:"tool_output",title:"Several agents read the same files separately",evidence:`${ve.files} of the ${ve.of} files or documents this run read were also read by ${ve.runs} other run${ve.runs>1?"s":""}${ve.agents.length?` (agents: ${ve.agents.join(", ")})`:""}, each paying for them again.`,automatic:[],advice:"Load shared files once and pass them to each agent, or put them at the top of every agent's prompt so the provider's cache serves them.",estSavedTokensPerCall:ve.files*_*.9/i})}let ze=t.priceTier;if(ze&&t.input>.9*ze.aboveInputTokens){let _=t.input>ze.aboveInputTokens,O=ue[t.profile]?.pricing,Q=O?t.input*(ze.inputPerMTok-O.inputPerMTok)/1e6:0;n.push({id:"price-tier",part:"history",title:_?"Calls are past the size where this model charges a higher rate":"Calls are about to pass the size where this model charges a higher rate",evidence:`The latest call has ${$(t.input)} input tokens; above ${$(ze.aboveInputTokens)} the whole request is billed at $${ze.inputPerMTok} in / $${ze.outputPerMTok} out per 1M tokens${O?` instead of $${O.inputPerMTok} / $${O.outputPerMTok}`:""}.${_&&Q?` That adds about $${Q.toFixed(4)} to this call's input alone.`:""}`,automatic:[],advice:"Hand off to a fresh call with a summary, or drop older tool outputs, before crossing the limit. The model stays the same.",estSavedTokensPerCall:_?Math.max(0,t.input-ze.aboveInputTokens):0})}t.window&&t.input/t.window>=.8&&n.push({id:"near-context-limit",part:"history",title:"The run is close to the model's context limit",evidence:`The latest call uses ${ka(t.input,t.window)} of a ${$(t.window)}-token window.`,automatic:[],advice:"Hand off to a fresh run with a summary before the provider starts truncating or rejecting calls.",estSavedTokensPerCall:0});let $e=ue[t.profile]?.pricing?.inputPerMTok??0,tn={cachingOn:l.has("prompt-caching")||u>0,bigTools:t.parts.tools>2e3,repeat:[...t.repeatedToolCalls].sort((_,O)=>O.count-_.count)[0],loop:K&&K.times>=3?K:void 0,steps:i,api:t.api,breakKind:fe?.kind,breakWhere:fe?.where,tierAbove:ze?.aboveInputTokens};return n.map(_=>{let O=Math.round(_.estSavedTokensPerCall);return{..._,estSavedTokensPerCall:O,estSavedUsdPerCall:Fn(O*$e/1e6),estSavedTokensPerRun:O*i,estSavedUsdPerRun:Fn(O*i*$e/1e6),fixes:Wo(_.id,tn)}}).sort((_,O)=>O.estSavedTokensPerCall-_.estSavedTokensPerCall)}function Di(a){let e=a.calls.filter(s=>!s.replayed&&s.status<400);if(e.length<2)return;let n=e[0].parts,i=e[e.length-1].parts,r,t=0;for(let s of Object.keys(i)){let o=i[s]-n[s];o>t&&([r,t]=[s,o])}return r}function qn(a){let e=a.calls,n=ye();for(let r of e){let t=ue[r.profile];t&&Na(n,{profile:r.priceTier?{...t,priceTier:r.priceTier}:t,status:r.status,usage:r.usage,estimatedInput:r.input,savedInput:re(r.actions.map(s=>s.saved)),replayed:!!r.replayed,priceFactor:r.priceFactor})}let i=_a(n);return{id:a.id,app:a.app,agent:a.agent,...a.customer?{customer:a.customer}:{},named:a.named,firstSeen:a.firstSeen,lastSeen:a.lastSeen,calls:e.length,model:e.at(-1)?.model,tokensBefore:i.inputTokensBefore+i.outputTokens+i.outputTokensAvoided,tokensAfter:i.inputTokens+i.outputTokens,savedTokens:i.savedTokens,savedTokensPct:i.savedTokensPct,costBeforeUsd:i.costBeforeUsd,costUsd:i.costUsd,savedUsd:i.savedUsd,inputTokens:i.inputTokens,outputTokens:i.outputTokens,cachedInputTokens:i.cachedInputTokens,savedInputTokens:i.savedInputTokens,growing:Di(a),checkCostUsd:Fn(re(a.checks.map(r=>r.costUsd))),netSavedUsd:Fn(i.savedUsd-re(a.checks.map(r=>r.costUsd))),steps:e.filter(r=>!r.replayed&&!r.overBudget).length,retriesReplayed:e.filter(r=>r.replayed).length,stoppedByBudget:e.some(r=>r.overBudget),outputCheck:dr(a.checks)}}var Fn=a=>Math.round(a*1e6)/1e6;var Ma=new Set(["elide-tool-outputs","lazy-tools","trim-tool-fields","compact-json","park-tool-outputs"]),Ni={prompt:"prompt","minify-json":"minifyJson","collapse-repeats":"collapseRepeats","dedupe-tool-outputs":"dedupeToolOutputs","dedupe-passages":"dedupePassages","dedupe-images":"dedupeImages"},Jo=a=>a in Ni||Ma.has(a),Un=class{constructor(e){this.probation=e}probation;states=new Map;state(e,n){let i=`${e}\0${n}`,r=this.states.get(i);return r||this.states.set(i,r={app:e,agent:n,suspended:new Map,confirmed:new Map}),r}configFor(e,n,i,r,t){let s=this.states.get(`${n}\0${i}`),o=new Set(s?.suspended.keys()??[]);if(t)for(let u of Ma)this.onProbation(n,i,[u]).length&&o.add(u);if(!o.size)return{cfg:e,allow:r};let l={...e.safe};for(let u of o)Ni[u]&&(l[Ni[u]]=!1);let c={...e.optIn,elideOldToolOutputs:o.has("elide-tool-outputs")?!1:e.optIn.elideOldToolOutputs,lazyTools:o.has("lazy-tools")?!1:e.optIn.lazyTools,trimToolFields:o.has("trim-tool-fields")?!1:e.optIn.trimToolFields,compactJson:o.has("compact-json")?!1:e.optIn.compactJson,parkToolOutputs:o.has("park-tool-outputs")?!1:e.optIn.parkToolOutputs};return{cfg:{...e,safe:l,optIn:c},allow:new Set([...r].filter(u=>!o.has(u)))}}onProbation(e,n,i){let r=this.states.get(`${e}\0${n}`);return i.filter(t=>Ma.has(t)&&(r?.confirmed.get(t)??0)<this.probation)}confirm(e,n,i){let r=this.state(e,n);for(let t of i)Ma.has(t)&&r.confirmed.set(t,(r.confirmed.get(t)??0)+1)}suspend(e,n,i,r){let t=this.state(e,n),s=i.filter(Jo);for(let o of s)t.suspended.set(o,{at:r.at,similarity:r.similarity,sameToolCalls:r.sameToolCalls}),t.confirmed.delete(o);return s}isSuspended(e,n,i){return this.states.get(`${e}\0${n}`)?.suspended.has(i)??!1}reset(e,n){let i=0;for(let[r,t]of this.states)(!e||t.app===e)&&(n===void 0||t.agent===n)&&(i+=t.suspended.size,this.states.delete(r));return i}replaceSuspended(e){for(let n of this.states.values())n.suspended.clear();for(let n of e){let i=this.state(n.app,n.agent);i.suspended.set(n.fix,{at:n.since,similarity:n.similarity,sameToolCalls:n.sameToolCalls}),i.confirmed.delete(n.fix)}}list(){let e=[];for(let n of this.states.values())for(let[i,r]of n.suspended)e.push({app:n.app,agent:n.agent,fix:i,since:r.at,similarity:r.similarity,sameToolCalls:r.sameToolCalls});return e}};import{createHash as Yo}from"node:crypto";var ja=class{constructor(e){this.maxBytes=e}maxBytes;items=new Map;bytes=0;hits=0;misses=0;static scope(e,n,i){return[e,n,i.model,i.dimensions??"",i.encoding_format??"float",i.user??""].join("\0")}static key(e,n){return Yo("sha256").update(e).update("\0").update(JSON.stringify(n)).digest("hex")}get(e){let n=this.items.get(e);if(n!==void 0)return this.items.delete(e),this.items.set(e,n),JSON.parse(n)}set(e,n){let i=JSON.stringify(n);if(i.length>this.maxBytes)return;let r=this.items.get(e);for(r!==void 0&&(this.bytes-=r.length),this.items.delete(e),this.items.set(e,i),this.bytes+=i.length;this.bytes>this.maxBytes;){let[t,s]=this.items.entries().next().value;this.items.delete(t),this.bytes-=s.length}}get size(){return this.items.size}};function jr(a){let e=a.input;return typeof e=="string"?[e]:Array.isArray(e)&&e.length&&(e.every(n=>typeof n=="string")||e.every(n=>Array.isArray(n)))?e:Array.isArray(e)&&e.length&&e.every(n=>typeof n=="number")?[e]:null}import{createHash as Nr}from"node:crypto";var Li=a=>Nr("sha256").update(a).digest("hex").slice(0,8),Oa=256,Or=a=>{let e=[];for(let n=0;n<a.length;n+=Oa)e.push(Li(a.slice(n,n+Oa)));return e};function Lr(a,e,n){return{system:Or(a),tools:Or(e.join(`
3362
+ await Promise.all(subAgents.map((a) => a.run({ task, context: docs })));`},{where:"agent code",what:"Or share one read cache between sub-agents",apply:'const cache = new Map(); const readOnce = (p) => cache.get(p) ?? (cache.set(p, fs.promises.readFile(p, "utf8")), cache.get(p));'},{where:"agent code",what:"Put the shared files in the same place at the top of each sub-agent's prompt, so the provider's cache serves them after the first",apply:'system: [STATIC_SYSTEM, ...docs.map((d) => `<doc path="${d.path}">${d.text}</doc>`)].join("\\n")'}];case"price-tier":return[{where:"agent code",what:`Keep each request under ${$(e.tierAbove??2e5)} input tokens: hand off to a fresh call with a summary first`,apply:`if (estimateTokens(messages) > ${Math.round((e.tierAbove??2e5)*.9)}) messages = [system, { role: "user", content: await summarize(messages) }];`},{where:"request header",what:"Replace older tool outputs with a stub (opt-in; answers checked before it applies)",apply:"x-token-lens-allow: elide-tool-outputs"},{where:"agent code",what:"Retrieve fewer, smaller chunks",apply:"const chunks = await search(query, { topK: 4, maxTokensPerChunk: 400 });"}];case"near-context-limit":return[{where:"agent code",what:"Hand off to a fresh run with a summary before the window fills",apply:'if (usage.input_tokens > 0.7 * CONTEXT_WINDOW) messages = [system, { role: "user", content: await summarize(messages) }];'}];default:return[]}}var $=a=>Math.round(a).toLocaleString("en-US"),ya=(a,e)=>`${e?Math.round(a/e*100):0}%`,re=a=>a.reduce((e,n)=>e+n,0),Mr=a=>a.length>60?a.slice(0,57)+"...":a;function Ko(a){let e=new Map;for(let n of a)for(let i of n.actions){let r=e.get(i.id)??{saved:0,calls:0};r.saved+=i.saved,r.calls++,e.set(i.id,r)}return e}var $o=a=>`Send the header "x-token-lens-allow: ${a}" on this app's requests, or enable it for everyone in the gateway config.`;function Gn(a){let e=a.calls.filter(_=>!_.replayed&&_.status<400),n=[];if(!e.length)return[];let i=e.length,r=e[0],t=e[i-1],s=t.input||1,o=_=>i>1?(t.parts[_]-r.parts[_])/(i-1):0,l=Ko(a.calls),c=(_,O)=>{let Q=l.get(_);return Q?[`${O} (${Q.calls} of ${a.calls.length} calls${Q.saved?`, ${$(Q.saved)} tokens saved so far`:""})`]:[]},u=re(e.map(_=>_.usage.cachedInputTokens??0)),g=u?[`provider reports ${$(u)} input tokens served from cache`]:[],f=t.parts.system+t.parts.tools;i>1&&f>=1024&&n.push({id:"repeated-prefix",part:t.parts.tools>t.parts.system?"tools":"system",title:"The same system prompt and tool definitions are re-sent on every call",evidence:`${$(f)} tokens (${ya(f,s)} of the latest call) repeated on ${i} calls: ${$(f*i)} tokens in total.`,automatic:[...c("prompt-caching","prompt caching turned on for the repeated prefix"),...g],advice:[l.has("prompt-caching")||u?"":"Keep this prefix byte-identical between calls so the provider can cache it: OpenAI and Gemini do so automatically; on a self-hosted server turn on prefix caching (vLLM --enable-prefix-caching; SGLang has it on by default).",t.parts.tools>2e3?"Send only the tools this step can use; long tool descriptions are paid for on every call.":""].filter(Boolean).join(" ")||void 0,estSavedTokensPerCall:f*.9});let h=o("history");if(i>2&&h>300&&t.parts.history/s>=.3){let _=t.parts.history;n.push({id:"growing-history",part:"history",title:"Each call re-reads a growing history",evidence:`History grows about ${$(h)} tokens per call and is now ${$(_)} tokens (${ya(_,s)} of the latest call). Over ${i} calls the model re-read ${$(re(e.map(O=>O.parts.history)))} history tokens.`,automatic:[...c("prompt-caching","the history so far is cached between calls"),...g],advice:"Start long runs from a summary instead of the full transcript (Token Lens's handoff brief), or keep only the turns the next step needs. Either can change later answers, so check the output first.",estSavedTokensPerCall:_*.5})}let C=t.parts.tool_output/s;if(t.parts.tool_output>1e3&&(C>=.3||o("tool_output")>500)){let _=t.toolOutputs,O=re(_.slice(0,Math.max(0,_.length-3)).filter(va=>va>=300)),Q=l.get("elide-tool-outputs");n.push({id:"tool-outputs",part:"tool_output",title:"Tool outputs take a large share of every call",evidence:`${_.length} tool outputs, ${$(t.parts.tool_output)} tokens (${ya(t.parts.tool_output,s)} of the latest call)${i>1?`, growing about ${$(o("tool_output"))} tokens per call`:""}.`,automatic:[...c("minify-json","pretty-printed JSON compacted"),...c("collapse-repeats","repeated lines collapsed"),...Q?c("elide-tool-outputs","older tool outputs replaced with a stub"):[]],optIn:!Q&&O>0?{id:"elide-tool-outputs",how:$o("elide-tool-outputs"),estSavedTokensPerCall:O}:void 0,advice:"Have tools return only the fields the agent uses, and page or truncate large results at the source.",estSavedTokensPerCall:Q?0:O})}t.parts.context>2e3&&t.parts.context/s>=.4&&n.push({id:"large-context",part:"context",title:"Retrieved or pasted context dominates the newest message",evidence:`${$(t.parts.context)} tokens of material (${ya(t.parts.context,s)} of the latest call) against a ${$(t.parts.prompt)}-token instruction.`,automatic:[],advice:"Retrieve fewer or smaller chunks, drop near-duplicate chunks, or put a stable document before the question so it can be cached across calls.",estSavedTokensPerCall:t.parts.context*.3});let w=a.calls.filter(_=>_.duplicate);if(w.length){let _=a.calls.filter(O=>O.replayed).length;n.push({id:"retries",part:"calls",title:"Identical requests were sent more than once",evidence:`${w.length} of ${a.calls.length} calls repeated an earlier request word for word.`,automatic:_?[`${_} answered from the gateway's copy at no token cost`]:[],advice:_?void 0:"Only temperature-0 requests are replayed automatically. Check the retry loop: a retry after a timeout or a parse error re-pays the whole input.",estSavedTokensPerCall:_?0:re(w.map(O=>O.input))/a.calls.length})}let N=re(e.map(_=>_.usage.reasoningTokens??0)),F=re(e.map(_=>_.usage.outputTokens??0));N>2e3&&N>.6*F&&n.push({id:"reasoning-heavy",part:"output",title:"Hidden reasoning is most of the output",evidence:`${$(N)} of ${$(F)} output tokens (${ya(N,F)}) were reasoning the app never sees.`,automatic:[],advice:"Not changed automatically, since it could change answers. If some steps are simple (routing, extraction, formatting), try a lower reasoning effort on those steps with output checks on; the model stays the same.",estSavedTokensPerCall:0});let L=t.repeatedToolCalls,z=re(L.map(_=>_.count-1));if(z>0){let _=re(e.map(Q=>Q.input))/i,O=[...L].sort((Q,va)=>va.count-Q.count)[0];n.push({id:"repeated-tool-calls",part:"tool_output",title:"The agent calls the same tool with the same input more than once",evidence:`${z} repeated tool call${z>1?"s":""} in ${$(t.toolCalls)}; for example ${O.name}(${Mr(O.args)}) ${O.count} times. Each repeat is an extra step that re-reads about ${$(_)} tokens of context.`,automatic:c("dedupe-tool-outputs","identical results sent once, later copies point back to the first"),advice:"Only the agent's code can skip the repeated step: cache tool results for the run (keyed by tool and input), or say in the system prompt that results don't change within a run.",estSavedTokensPerCall:z*_/i})}let K=t.sameCallStreak;if(K&&K.times>=3){let _=re(a.calls.map(O=>O.costUsd));n.push({id:"loop",part:"calls",title:"The agent looks stuck in a loop",evidence:`Its last ${K.times} tool calls were all ${K.name}(${Mr(K.args)}). The run has made ${a.calls.length} calls${_?` costing $${_.toFixed(4)}`:""} so far.`,automatic:a.calls.some(O=>O.overBudget)?["the run budget stopped further calls"]:[],optIn:{id:"run-budget",how:'Cap runs with "runBudget" in the gateway config, or the x-token-lens-max-calls / x-token-lens-max-cost-usd headers; calls past the cap get an error instead of being sent.'},advice:"Give the agent a step limit and a stop condition, and handle a tool error by changing the input instead of repeating the call.",estSavedTokensPerCall:(K.times-1)*(re(e.map(O=>O.input))/i)/i})}let ia=re(e.map(_=>(_.usage.outputTokens??0)*(ue[_.profile]?.pricing?.outputPerMTok??0)))/1e6,se=re(e.map(_=>_.costUsd)),oe=re(e.map(_=>_.usage.outputTokens??0))/i;se>0&&ia/se>=.5&&oe>500&&n.push({id:"output-heavy",part:"output",title:"Generated output is most of this run's cost",evidence:`Output is ${ya(ia,se)} of the run's cost, about ${$(oe)} tokens per step.`,automatic:[],advice:"Not changed automatically, since it shapes the answer. Asking intermediate steps for structured or brief output (and setting max_tokens on them) usually cuts this without changing the final result; check it with output checks on.",estSavedTokensPerCall:0});let ae=t.images;ae&&ae.count&&(ae.tokens>=1500||ae.count>=3)&&n.push({id:"images",part:"context",title:"Images are re-sent on every call",evidence:`${ae.count} image${ae.count>1?"s":""} in the latest call, about ${$(ae.tokens)} tokens (estimated from their sizes), ${$(ae.oldTokens)} of them in older messages. ${ae.provider==="anthropic"?"Anthropic shrinks images to 1,568 px":"OpenAI shrinks images to 2,048 px and a 768 px short side"} before counting, so sending them smaller than that saves upload time, not tokens.`,automatic:c("dedupe-images","a repeated image sent once"),advice:"Once the model has used an image, keep its description instead of the image; crop to the part that matters.",estSavedTokensPerCall:ae.oldTokens});let Se=e.filter(_=>_.cacheBreak),fe=Se.at(-1)?.cacheBreak;if(fe&&Se.length>=Math.max(1,(i-1)/3)){let _=re(Se.map(Q=>Q.cacheBreak.tokens)),O=fe.kind==="changed text"?"text that changes":fe.kind==="number"?"a number that changes":`a ${fe.kind}`;n.push({id:"cache-breaker",part:fe.where==="tools"?"tools":"system",title:fe.kind.endsWith("tools")?`The tool list ${fe.kind==="reordered tools"?"comes in a different order":"changes"} between calls, so the provider can't reuse its cache`:`The ${fe.where} contains ${O}, so the provider can't reuse its cache`,evidence:`On ${Se.length} of ${i} calls the ${fe.where} started the same as the call before and then changed, so about ${$(_)} tokens that could have been read from cache (about a tenth of the price) were billed in full.`,automatic:[],advice:"Only the app can fix this, by keeping everything before the changing part identical between calls.",estSavedTokensPerCall:_/i*.9})}let Ke=t.jsonRetries??0;if(Ke>0&&!t.structuredOutput){let _=re(e.map(O=>O.input))/i;n.push({id:"json-retries",part:"calls",title:"The agent re-asks the model after its JSON didn't parse",evidence:`${Ke} time${Ke>1?"s":""} in this conversation the model's JSON reply didn't parse and the agent asked again; each retry is a whole extra step of about ${$(_)} tokens.`,automatic:[],advice:"Strict structured output on the same model makes every reply parse.",estSavedTokensPerCall:Ke*_/i})}let ve=a.sharedReads;if(ve&&ve.files>0){let _=t.toolOutputs.length?t.parts.tool_output/t.toolOutputs.length:0;n.push({id:"shared-reads",part:"tool_output",title:"Several agents read the same files separately",evidence:`${ve.files} of the ${ve.of} files or documents this run read were also read by ${ve.runs} other run${ve.runs>1?"s":""}${ve.agents.length?` (agents: ${ve.agents.join(", ")})`:""}, each paying for them again.`,automatic:[],advice:"Load shared files once and pass them to each agent, or put them at the top of every agent's prompt so the provider's cache serves them.",estSavedTokensPerCall:ve.files*_*.9/i})}let ze=t.priceTier;if(ze&&t.input>.9*ze.aboveInputTokens){let _=t.input>ze.aboveInputTokens,O=ue[t.profile]?.pricing,Q=O?t.input*(ze.inputPerMTok-O.inputPerMTok)/1e6:0;n.push({id:"price-tier",part:"history",title:_?"Calls are past the size where this model charges a higher rate":"Calls are about to pass the size where this model charges a higher rate",evidence:`The latest call has ${$(t.input)} input tokens; above ${$(ze.aboveInputTokens)} the whole request is billed at $${ze.inputPerMTok} in / $${ze.outputPerMTok} out per 1M tokens${O?` instead of $${O.inputPerMTok} / $${O.outputPerMTok}`:""}.${_&&Q?` That adds about $${Q.toFixed(4)} to this call's input alone.`:""}`,automatic:[],advice:"Hand off to a fresh call with a summary, or drop older tool outputs, before crossing the limit. The model stays the same.",estSavedTokensPerCall:_?Math.max(0,t.input-ze.aboveInputTokens):0})}t.window&&t.input/t.window>=.8&&n.push({id:"near-context-limit",part:"history",title:"The run is close to the model's context limit",evidence:`The latest call uses ${ya(t.input,t.window)} of a ${$(t.window)}-token window.`,automatic:[],advice:"Hand off to a fresh run with a summary before the provider starts truncating or rejecting calls.",estSavedTokensPerCall:0});let $e=ue[t.profile]?.pricing?.inputPerMTok??0,tn={cachingOn:l.has("prompt-caching")||u>0,bigTools:t.parts.tools>2e3,repeat:[...t.repeatedToolCalls].sort((_,O)=>O.count-_.count)[0],loop:K&&K.times>=3?K:void 0,steps:i,api:t.api,breakKind:fe?.kind,breakWhere:fe?.where,tierAbove:ze?.aboveInputTokens};return n.map(_=>{let O=Math.round(_.estSavedTokensPerCall);return{..._,estSavedTokensPerCall:O,estSavedUsdPerCall:Fn(O*$e/1e6),estSavedTokensPerRun:O*i,estSavedUsdPerRun:Fn(O*i*$e/1e6),fixes:Wo(_.id,tn)}}).sort((_,O)=>O.estSavedTokensPerCall-_.estSavedTokensPerCall)}function Di(a){let e=a.calls.filter(s=>!s.replayed&&s.status<400);if(e.length<2)return;let n=e[0].parts,i=e[e.length-1].parts,r,t=0;for(let s of Object.keys(i)){let o=i[s]-n[s];o>t&&([r,t]=[s,o])}return r}function qn(a){let e=a.calls,n=ye();for(let r of e){let t=ue[r.profile];t&&Na(n,{profile:r.priceTier?{...t,priceTier:r.priceTier}:t,status:r.status,usage:r.usage,estimatedInput:r.input,savedInput:re(r.actions.map(s=>s.saved)),replayed:!!r.replayed,priceFactor:r.priceFactor})}let i=Ea(n);return{id:a.id,app:a.app,agent:a.agent,...a.customer?{customer:a.customer}:{},named:a.named,firstSeen:a.firstSeen,lastSeen:a.lastSeen,calls:e.length,model:e.at(-1)?.model,tokensBefore:i.inputTokensBefore+i.outputTokens+i.outputTokensAvoided,tokensAfter:i.inputTokens+i.outputTokens,savedTokens:i.savedTokens,savedTokensPct:i.savedTokensPct,costBeforeUsd:i.costBeforeUsd,costUsd:i.costUsd,savedUsd:i.savedUsd,inputTokens:i.inputTokens,outputTokens:i.outputTokens,cachedInputTokens:i.cachedInputTokens,savedInputTokens:i.savedInputTokens,growing:Di(a),checkCostUsd:Fn(re(a.checks.map(r=>r.costUsd))),netSavedUsd:Fn(i.savedUsd-re(a.checks.map(r=>r.costUsd))),steps:e.filter(r=>!r.replayed&&!r.overBudget).length,retriesReplayed:e.filter(r=>r.replayed).length,stoppedByBudget:e.some(r=>r.overBudget),outputCheck:dr(a.checks)}}var Fn=a=>Math.round(a*1e6)/1e6;var Ma=new Set(["elide-tool-outputs","lazy-tools","trim-tool-fields","compact-json","park-tool-outputs"]),Ni={prompt:"prompt","minify-json":"minifyJson","collapse-repeats":"collapseRepeats","dedupe-tool-outputs":"dedupeToolOutputs","dedupe-passages":"dedupePassages","dedupe-images":"dedupeImages"},Jo=a=>a in Ni||Ma.has(a),Un=class{constructor(e){this.probation=e}probation;states=new Map;state(e,n){let i=`${e}\0${n}`,r=this.states.get(i);return r||this.states.set(i,r={app:e,agent:n,suspended:new Map,confirmed:new Map}),r}configFor(e,n,i,r,t){let s=this.states.get(`${n}\0${i}`),o=new Set(s?.suspended.keys()??[]);if(t)for(let u of Ma)this.onProbation(n,i,[u]).length&&o.add(u);if(!o.size)return{cfg:e,allow:r};let l={...e.safe};for(let u of o)Ni[u]&&(l[Ni[u]]=!1);let c={...e.optIn,elideOldToolOutputs:o.has("elide-tool-outputs")?!1:e.optIn.elideOldToolOutputs,lazyTools:o.has("lazy-tools")?!1:e.optIn.lazyTools,trimToolFields:o.has("trim-tool-fields")?!1:e.optIn.trimToolFields,compactJson:o.has("compact-json")?!1:e.optIn.compactJson,parkToolOutputs:o.has("park-tool-outputs")?!1:e.optIn.parkToolOutputs};return{cfg:{...e,safe:l,optIn:c},allow:new Set([...r].filter(u=>!o.has(u)))}}onProbation(e,n,i){let r=this.states.get(`${e}\0${n}`);return i.filter(t=>Ma.has(t)&&(r?.confirmed.get(t)??0)<this.probation)}confirm(e,n,i){let r=this.state(e,n);for(let t of i)Ma.has(t)&&r.confirmed.set(t,(r.confirmed.get(t)??0)+1)}suspend(e,n,i,r){let t=this.state(e,n),s=i.filter(Jo);for(let o of s)t.suspended.set(o,{at:r.at,similarity:r.similarity,sameToolCalls:r.sameToolCalls}),t.confirmed.delete(o);return s}isSuspended(e,n,i){return this.states.get(`${e}\0${n}`)?.suspended.has(i)??!1}reset(e,n){let i=0;for(let[r,t]of this.states)(!e||t.app===e)&&(n===void 0||t.agent===n)&&(i+=t.suspended.size,this.states.delete(r));return i}replaceSuspended(e){for(let n of this.states.values())n.suspended.clear();for(let n of e){let i=this.state(n.app,n.agent);i.suspended.set(n.fix,{at:n.since,similarity:n.similarity,sameToolCalls:n.sameToolCalls}),i.confirmed.delete(n.fix)}}list(){let e=[];for(let n of this.states.values())for(let[i,r]of n.suspended)e.push({app:n.app,agent:n.agent,fix:i,since:r.at,similarity:r.similarity,sameToolCalls:r.sameToolCalls});return e}};import{createHash as Yo}from"node:crypto";var ja=class{constructor(e){this.maxBytes=e}maxBytes;items=new Map;bytes=0;hits=0;misses=0;static scope(e,n,i){return[e,n,i.model,i.dimensions??"",i.encoding_format??"float",i.user??""].join("\0")}static key(e,n){return Yo("sha256").update(e).update("\0").update(JSON.stringify(n)).digest("hex")}get(e){let n=this.items.get(e);if(n!==void 0)return this.items.delete(e),this.items.set(e,n),JSON.parse(n)}set(e,n){let i=JSON.stringify(n);if(i.length>this.maxBytes)return;let r=this.items.get(e);for(r!==void 0&&(this.bytes-=r.length),this.items.delete(e),this.items.set(e,i),this.bytes+=i.length;this.bytes>this.maxBytes;){let[t,s]=this.items.entries().next().value;this.items.delete(t),this.bytes-=s.length}}get size(){return this.items.size}};function jr(a){let e=a.input;return typeof e=="string"?[e]:Array.isArray(e)&&e.length&&(e.every(n=>typeof n=="string")||e.every(n=>Array.isArray(n)))?e:Array.isArray(e)&&e.length&&e.every(n=>typeof n=="number")?[e]:null}import{createHash as Nr}from"node:crypto";var Li=a=>Nr("sha256").update(a).digest("hex").slice(0,8),Oa=256,Or=a=>{let e=[];for(let n=0;n<a.length;n+=Oa)e.push(Li(a.slice(n,n+Oa)));return e};function Lr(a,e,n){return{system:Or(a),tools:Or(e.join(`
3363
3363
  `)),toolOrder:Li(n.join("\0")),toolSet:Li([...n].sort().join("\0"))}}function Dr(a){return/\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}|\b\d{1,2}:\d{2}(:\d{2})?\s*(am|pm|utc|gmt)?\b|\b1[6-9]\d{8}(\d{3})?\b/i.test(a)?"timestamp":/\b\d{4}-\d{2}-\d{2}\b|\b(mon|tue|wed|thu|fri|sat|sun)[a-z]*,? \w+ \d{1,2}|\b(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]* \d{1,2},? \d{4}|\btoday is\b/i.test(a)?"date":/\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b|\b[0-9a-f]{16,}\b|\b(req|run|session|trace|user)[-_][a-z0-9]{6,}\b/i.test(a)?"id":/\d{3,}/.test(a)?"number":"changed text"}function Br(a,e,n,i){if(!a)return;let r=e.system.length+e.tools.length;if(!r)return;let t=(l,c)=>{let u=Math.min(l.length,c.length);for(let g=0;g<u;g++)if(l[g]!==c[g])return g;return l.length===c.length?-1:u},s=t(a.system,e.system);if(s>=0){if(e.system.filter(u=>a.system.includes(u)).length/Math.max(e.system.length,1)<.5)return;let c=s*Oa;return{kind:Dr(n.slice(Math.max(0,c-40),c+Oa+40)),where:"system prompt",lostShare:(r-s)/r}}if(a.toolOrder!==e.toolOrder&&a.toolSet===e.toolSet)return{kind:"reordered tools",where:"tools",lostShare:e.tools.length/r};let o=t(a.tools,e.tools);if(o>=0&&a.toolSet===e.toolSet){let l=o*Oa,c=Dr(i.slice(Math.max(0,l-40),l+Oa+40));return{kind:c==="changed text"?"changed tools":c,where:"tools",lostShare:(e.tools.length-o)/r}}}function Xo(a,e){let n=r=>typeof r=="string"?r:Array.isArray(r)?r.map(t=>typeof t=="string"?t:typeof t?.text=="string"?t.text:typeof t?.content=="string"?t.content:Array.isArray(t?.content)?n(t.content):"").join(`
3364
3364
  `):"";return(a==="openai-responses"?Array.isArray(e?.input)?e.input:[]:Array.isArray(e?.messages)?e.messages:[]).map(r=>({role:r?.role??(r?.type==="function_call_output"?"tool":r?.type??""),text:n(r?.content??r?.output)}))}var Qo=a=>/^\s*(```(json)?\s*)?[{[]/i.test(a),Zo=a=>{try{return JSON.parse(a.trim().replace(/^```(json)?\s*/i,"").replace(/```\s*$/,"")),!0}catch{return!1}};function Fr(a,e){let n=Xo(a,e),i=0;return n.forEach((r,t)=>{let s=n[t+1];r.role==="assistant"&&Qo(r.text)&&!Zo(r.text)&&s&&s.role!=="assistant"&&/json|pars|valid|format|schema/i.test(s.text)&&i++}),i}function Gr(a,e){return a==="openai-chat"?e?.response_format?.type==="json_schema":a==="openai-responses"?e?.text?.format?.type==="json_schema":!!e?.output_format||e?.tool_choice?.type==="tool"&&Array.isArray(e?.tools)}var el=/(^|[_.\-\s])(read|open|cat|view|load|get)([_.\-\s]?(file|doc|document|page|text))?($|[_.\-\s])|^read|file$/i,al=["path","file","file_path","filepath","filename","uri","url","document_id","doc_id"];function qr(a){let e=new Set;for(let n of a){if(!el.test(n.name))continue;let i;try{i=JSON.parse(n.args)}catch{continue}let r=i&&typeof i=="object"?al.map(t=>i[t]).find(t=>typeof t=="string"&&t.length>0):void 0;r&&e.add(Nr("sha256").update(r).digest("hex").slice(0,16))}return[...e]}var nl=a=>new RegExp("^"+a.split("*").map(e=>e.replace(/[.+?^${}()|[\]\\]/g,"\\$&")).join(".*")+"$","i");function Ur(a,e){return a.find(n=>nl(n.model).test(e))}import{readFileSync as il,renameSync as tl,writeFileSync as rl}from"node:fs";import{connect as sl}from"node:net";import{connect as ol}from"node:tls";var an=class{constructor(e){this.url=e}url;socket;ready;buf=Buffer.alloc(0);waiting=[];connect(){if(this.ready)return this.ready;let e=new URL(this.url),n=Number(e.port)||6379,i=e.hostname||"127.0.0.1";return this.ready=new Promise((r,t)=>{let s=e.protocol==="rediss:"?ol({host:i,port:n,servername:i},()=>r()):sl({host:i,port:n},()=>r());s.on("error",o=>{t(o),this.fail(o)}),s.on("close",()=>this.fail(new Error("Redis connection closed"))),s.on("data",o=>this.data(o)),s.unref(),this.socket=s}).then(async()=>{e.password&&await this.raw(e.username?["AUTH",decodeURIComponent(e.username),decodeURIComponent(e.password)]:["AUTH",decodeURIComponent(e.password)]);let r=e.pathname.replace("/","");r&&await this.raw(["SELECT",r])}),this.ready}fail(e){this.ready=void 0,this.socket=void 0,this.buf=Buffer.alloc(0);for(let n of this.waiting.splice(0))n.reject(e)}raw(e){return new Promise((n,i)=>{this.waiting.push({resolve:n,reject:i});let r=e.map(t=>Buffer.from(String(t)));this.socket.write(Buffer.concat([Buffer.from(`*${r.length}\r
3365
3365
  `),...r.flatMap(t=>[Buffer.from(`$${t.length}\r
3366
3366
  `),t,Buffer.from(`\r
3367
3367
  `)])]))})}async cmd(...e){await this.connect();let n=await this.raw(e);if(n instanceof Error)throw n;return n}data(e){for(this.buf=Buffer.concat([this.buf,e]);;){let n=Hr(this.buf,0);if(!n)return;this.buf=this.buf.subarray(n.end),this.waiting.shift()?.resolve(n.value)}}async hincrbyfloat(e,n,i){await this.cmd("HINCRBYFLOAT",e,n,i)}async hgetall(e){let n=await this.cmd("HGETALL",e),i={};for(let r=0;r+1<n.length;r+=2)i[n[r]]=n[r+1];return i}async hset(e,n,i){await this.cmd("HSET",e,n,i)}async hdel(e,n){await this.cmd("HDEL",e,n)}async setex(e,n,i){await this.cmd("SET",e,i,"EX",Math.max(1,Math.round(n)))}async get(e){return await this.cmd("GET",e)}async mget(e){return e.length?await this.cmd("MGET",...e):[]}async zadd(e,n,i){await this.cmd("ZADD",e,n,i)}async zrevrange(e,n,i){return await this.cmd("ZREVRANGE",e,n,i)}async zremrangebyscore(e,n){await this.cmd("ZREMRANGEBYSCORE",e,"-inf",n)}async setnx(e,n){await this.cmd("SETNX",e,n)}async expire(e,n){await this.cmd("EXPIRE",e,Math.max(1,Math.round(n)))}close(){this.socket?.end()}};function Hr(a,e){let n=a.indexOf(`\r
3368
3368
  `,e);if(n<0)return;let i=String.fromCharCode(a[e]),r=a.toString("utf8",e+1,n);switch(i){case"+":return{value:r,end:n+2};case"-":return{value:new Error(r),end:n+2};case":":return{value:Number(r),end:n+2};case"$":{let t=Number(r);return t<0?{value:null,end:n+2}:a.length<n+2+t+2?void 0:{value:a.toString("utf8",n+2,n+2+t),end:n+2+t+2}}case"*":{let t=Number(r);if(t<0)return{value:null,end:n+2};let s=[],o=n+2;for(let l=0;l<t;l++){let c=Hr(a,o);if(!c)return;s.push(c.value),o=c.end}return{value:s,end:o}}default:throw new Error(`Unexpected Redis reply type ${JSON.stringify(i)}`)}}var nn=class{constructor(e){this.path=e;try{let n=JSON.parse(il(e,"utf8"));for(let[i,r]of Object.entries(n.hashes??{}))this.hashes.set(i,new Map(Object.entries(r)));for(let[i,r]of Object.entries(n.strings??{}))this.strings.set(i,r);for(let[i,r]of Object.entries(n.zsets??{}))this.zsets.set(i,new Map(Object.entries(r)));for(let[i,r]of Object.entries(n.expiries??{}))this.expiries.set(i,Number(r))}catch{}}path;hashes=new Map;strings=new Map;zsets=new Map;expiries=new Map;dirty=!1;hash(e){this.dropExpired(e);let n=this.hashes.get(e);return n||this.hashes.set(e,n=new Map),n}async hincrbyfloat(e,n,i){let r=this.hash(e);r.set(n,String(Number(r.get(n)??0)+i)),this.dirty=!0}async hgetall(e){return this.dropExpired(e),Object.fromEntries(this.hashes.get(e)??[])}dropExpired(e){let n=this.expiries.get(e);n!==void 0&&n<Date.now()&&(this.hashes.delete(e),this.expiries.delete(e),this.dirty=!0)}async expire(e,n){this.expiries.set(e,Date.now()+n*1e3),this.dirty=!0}async hset(e,n,i){this.hash(e).set(n,i),this.dirty=!0}async hdel(e,n){this.hashes.get(e)?.delete(n),this.dirty=!0}async setex(e,n,i){this.strings.set(e,{value:i,until:Date.now()+n*1e3}),this.dirty=!0}async get(e){let n=this.strings.get(e);return n?n.until<Date.now()?(this.strings.delete(e),null):n.value:null}async mget(e){return Promise.all(e.map(n=>this.get(n)))}async zadd(e,n,i){let r=this.zsets.get(e);r||this.zsets.set(e,r=new Map),r.set(i,n),this.dirty=!0}async zrevrange(e,n,i){return[...this.zsets.get(e)??[]].sort((t,s)=>s[1]-t[1]).map(([t])=>t).slice(n,i+1)}async zremrangebyscore(e,n){let i=this.zsets.get(e);if(i)for(let[r,t]of i)t<=n&&i.delete(r);for(let[r,t]of this.strings)t.until<Date.now()&&this.strings.delete(r);this.dirty=!0}async setnx(e,n){await this.get(e)||await this.setex(e,10*365*86400,n)}save(){for(let n of[...this.expiries.keys()])this.dropExpired(n);if(!this.dirty)return;this.dirty=!1;let e={hashes:Object.fromEntries([...this.hashes].map(([n,i])=>[n,Object.fromEntries(i)])),strings:Object.fromEntries(this.strings),zsets:Object.fromEntries([...this.zsets].map(([n,i])=>[n,Object.fromEntries(i)])),expiries:Object.fromEntries(this.expiries)};rl(`${this.path}.tmp`,JSON.stringify(e)),tl(`${this.path}.tmp`,this.path)}close(){this.save()}};var Hn=class a{constructor(e,n,i){this.kv=e;this.cfg=n;this.deps=i;this.kind=e instanceof an?"redis":"file"}kv;cfg;deps;flushed=new Map;flushedCustomers=new Map;dirtyRuns=new Map;timer;busy;kind;static open(e,n){if(e.redisUrl)return new a(new an(e.redisUrl),e,n);if(e.file)return new a(new nn(e.file),e,n)}k=e=>`${this.cfg.keyPrefix}${e}`;start(){this.kv.setnx(this.k("since"),new Date().toISOString()).catch(e=>this.error(e)),this.flush(),this.timer=setInterval(()=>{this.flush()},this.cfg.flushSeconds*1e3),this.timer.unref?.()}stop(){this.timer&&clearInterval(this.timer),this.flush().finally(()=>this.kv.close())}markRun(e){this.dirtyRuns.set(e.id,e)}error(e){this.deps.log(JSON.stringify({ts:new Date().toISOString(),level:"error",storage:this.kind,error:String(e?.message??e)}))}flush(){return this.busy?this.busy:(this.busy=this.doFlush().catch(e=>this.error(e)).finally(()=>this.busy=void 0),this.busy)}async doFlush(){let e=this.deps.metrics.snapshot();for(let[s,o]of e){let l=this.flushed.get(s)??ye();for(let c of Object.keys(o)){let u=o[c]-l[c];u&&await this.kv.hincrbyfloat(this.k("metrics"),`${s}\0${c}`,u)}this.flushed.set(s,o)}let n=this.deps.ledger;if(n){let s=n.snapshot();for(let l of this.flushedCustomers.keys())s.has(l)||this.flushedCustomers.delete(l);for(let[l,c]of s){let u=this.flushedCustomers.get(l)??ye();for(let g of Object.keys(c)){let f=c[g]-u[g];f&&await this.kv.hincrbyfloat(this.k("customers"),`${l}\0${g}`,f)}this.flushedCustomers.set(l,c)}let o=new Set;for(let{period:l,field:c,by:u}of n.takeSpendDeltas())await this.kv.hincrbyfloat(this.k(`spend:${l}`),c,u),o.add(l);for(let l of o)await this.kv.expire(this.k(`spend:${l}`),mt(l.startsWith("d")?"day":"month"));for(let l of n.currentPeriods())n.setSharedSpend(l,await this.kv.hgetall(this.k(`spend:${l}`)))}let i=[...this.dirtyRuns.values()];this.dirtyRuns.clear();let r=this.cfg.runDays*86400;for(let s of i)await this.kv.setex(this.k(`run:${s.id}`),r,JSON.stringify(this.deps.runView(s))),await this.kv.zadd(this.k("runs"),Date.parse(s.lastSeen)||Date.now(),s.id);i.length&&await this.kv.zremrangebyscore(this.k("runs"),Date.now()-r*1e3);let t=await this.kv.hgetall(this.k("guard"));this.deps.guard.replaceSuspended(Object.values(t).map(s=>JSON.parse(s))),this.kv instanceof nn&&this.kv.save()}async totals(){await this.flush();let e=await this.kv.hgetall(this.k("metrics")),n=new Map;for(let[i,r]of Object.entries(e)){let t=i.split("\0"),s=t.pop(),o=t.join("\0"),l=n.get(o)??ye();s in l&&(l[s]=Number(r)),n.set(o,l)}return{rows:n,since:await this.kv.get(this.k("since"))??new Date().toISOString()}}async customerTotals(){await this.flush();let e=await this.kv.hgetall(this.k("customers")),n=new Map;for(let[i,r]of Object.entries(e)){let t=i.split("\0"),s=t.pop(),o=t.join("\0"),l=n.get(o)??ye();s in l&&(l[s]=Number(r)),n.set(o,l)}return n}async recentRuns(e){await this.flush();let n=await this.kv.zrevrange(this.k("runs"),0,e-1);return(await this.kv.mget(n.map(r=>this.k(`run:${r}`)))).filter(r=>!!r).map(r=>JSON.parse(r))}async run(e){await this.flush();let n=await this.kv.get(this.k(`run:${e}`));return n?JSON.parse(n):void 0}async suspend(e){for(let n of e)await this.kv.hset(this.k("guard"),[n.app,n.agent,n.fix].join("\0"),JSON.stringify(n))}async resetGuard(e,n){let i=await this.kv.hgetall(this.k("guard")),r=0;for(let[t,s]of Object.entries(i)){let o=JSON.parse(s);(!e||o.app===e)&&(n===void 0||o.agent===n)&&(await this.kv.hdel(this.k("guard"),t),r++)}return await this.flush(),r}};var ul=new Set(["connection","keep-alive","proxy-authenticate","proxy-authorization","te","trailer","transfer-encoding","upgrade","host","content-length","accept-encoding"]),dl=["authorization","x-api-key","api-key"],Vr=new Set(["content-encoding","content-length","transfer-encoding","connection","keep-alive"]),J=class extends Error{constructor(n,i){super(i);this.status=n}status};function Z(a,e){let n=a.headers[e];return Array.isArray(n)?n[0]:n}function Wr(a){let e=new URL(a.url??"/","http://gateway");if(/^\/token-lens\/report/.test(e.pathname)&&e.searchParams.get("key"))return e.searchParams.get("key");let n=Z(a,"authorization");return n?.toLowerCase().startsWith("bearer ")?n.slice(7).trim():Z(a,"x-api-key")??Z(a,"api-key")}function Kr(a,e){let n=Buffer.from(a),i=Buffer.from(e);return n.length===i.length&&cl(n,i)}function Vn(a,e){return new Promise((n,i)=>{let r=[],t=0;a.on("data",s=>{t+=s.length,t>e?(i(new J(413,`Request body is larger than ${e} bytes.`)),a.destroy()):r.push(s)}),a.on("end",()=>n(Buffer.concat(r))),a.on("error",i)})}function me(a,e,n,i={}){let r=JSON.stringify(n,null,2);a.writeHead(e,{"content-type":"application/json",...i}),a.end(r)}function Wn(a){try{return JSON.parse(a.toString("utf8"))}catch{return}}var ml=a=>new RegExp("^"+a.split("*").map(e=>e.replace(/[.+?^${}()|[\]\\]/g,"\\$&")).join(".*")+"$","i"),Bi=class{constructor(e,n=16*1024*1024){this.sse=e;this.limit=n}sse;limit;usage={};decoder=new TextDecoder;partial="";chunks=[];size=0;body;streamText;push(e){if(this.sse){this.size+e.length<=this.limit?(this.chunks.push(Buffer.from(e)),this.size+=e.length):this.size=1/0;let i=(this.partial+this.decoder.decode(e,{stream:!0})).split(`
3369
- `);this.partial=i.pop()??"";for(let r of i)this.line(r)}else this.size+e.length<=this.limit?(this.chunks.push(Buffer.from(e)),this.size+=e.length):this.size=1/0}line(e){if(!(!e.startsWith("data:")||!e.includes('"usage"')))try{this.usage=lr(this.usage,fa(JSON.parse(e.slice(5))))}catch{}}end(){if(this.sse)this.line(this.partial),this.size!==1/0&&(this.streamText=Buffer.concat(this.chunks).toString("utf8"));else if(this.size!==1/0){this.body=Buffer.concat(this.chunks);try{this.usage=fa(JSON.parse(this.body.toString("utf8")))}catch{}}return this.usage}};function $r(a,e={}){let n=e.log??(d=>console.log(d)),i=e.fetch??fetch,r=new Bn(a.maxRuns),t=new hn,s=new yn(a.customers.maxCustomers),o=new Mn,l=new Map,c=new Set,u=new On({fetch:(...d)=>i(...d),url:(d,p)=>z(d,p,""),headers:(d,p)=>({...p}),log:n},a.batch),g=new Map,f=new Un(a.verify.probation),h=Hn.open(a.storage,{metrics:t,ledger:s,guard:f,runView:d=>({...qn(d),findings:Gn(d),outputChecks:d.checks,calls:d.calls.slice(-100)}),log:n});h?.start();let C=new ja(a.embeddingsCacheMB*1024*1024),w=new Map,N=new Map,F=new Set;for(let d of a.models)vi(d);function L(d,p,b){let k=d==="anthropic"||p.startsWith("/v1/messages");if(b){for(let A of a.routes)if(ml(A.model).test(b)){let D=a.upstreams[A.upstream];if(k&&D.type!=="anthropic"&&d!=="anthropic")throw new J(400,`Model "${b}" is served by an OpenAI-compatible upstream; call it at /v1/chat/completions.`);if(d&&!k&&D.type!=="openai"&&d!=="openai-chat")throw new J(400,`Model "${b}" is served by an Anthropic upstream; call it at /v1/messages or /v1/chat/completions.`);return[A.upstream,D]}}let M=Object.entries(a.upstreams),I=k?M.find(([,A])=>A.type==="anthropic"):a.defaultUpstream?[a.defaultUpstream,a.upstreams[a.defaultUpstream]]:M.find(([,A])=>A.type==="openai");if(!I)throw new J(502,`No ${k?"Anthropic":"OpenAI-compatible"} upstream is configured for this gateway.`);return I}function z(d,p,b){return d.type==="anthropic"?d.baseUrl.replace(/\/v1$/,"")+p+b:d.baseUrl+p.replace(/^\/v1/,"")+b}function K(d,p){let b={};for(let[k,M]of Object.entries(d.headers))M===void 0||ul.has(k)||k.startsWith("x-token-lens-")||dl.includes(k)&&(p.apiKey||a.clientKeys.length)||(b[k]=Array.isArray(M)?M.join(", "):M);if(b["accept-encoding"]="identity",!p.apiKey&&!a.clientKeys.length&&(p.type==="anthropic"&&!b["x-api-key"]&&/^Bearer /i.test(b.authorization??"")?(b["x-api-key"]=b.authorization.replace(/^Bearer /i,""),delete b.authorization):p.type==="openai"&&!b.authorization&&b["x-api-key"]&&!p.authHeader&&(b.authorization=`Bearer ${b["x-api-key"]}`,delete b["x-api-key"],delete b["anthropic-version"])),p.type==="anthropic"&&!b["anthropic-version"]&&(b["anthropic-version"]="2023-06-01"),p.apiKey){let k=p.authHeader??(p.type==="anthropic"?"x-api-key":"authorization");b[k]=k==="authorization"?`Bearer ${p.apiKey}`:p.apiKey,p.type==="anthropic"&&!b["anthropic-version"]&&(b["anthropic-version"]="2023-06-01")}return{...b,...p.headers}}function ia(d){if(!a.clientKeys.length)return(Z(d,"x-token-lens-app")||"default").slice(0,64);let p=Wr(d),b=p?a.clientKeys.find(k=>Kr(k.key,p)):void 0;return b?b.name:null}let se=d=>{let p=Wr(d);return!!a.adminKey&&!!p&&Kr(a.adminKey,p)},oe=(d,p)=>!a.isolateTeams||se(d)?void 0:p??"\0",ae=(d,p)=>p===void 0?d:new Map([...d].filter(([b])=>b.split("\0")[0]===p)),Se=async()=>h?h.customerTotals():s.snapshot(),fe=(d,p,b)=>se(d)?p.searchParams.get("team")??void 0:b??"\0";async function Ke(){return h?h.totals():{}}async function ve(d){let p=r.list().slice(0,d).map(k=>({...qn(k),findings:Gn(k)}));if(!h)return p;let b=(await h.recentRuns(d)).filter(k=>!r.get(k.id));return[...p,...b].sort((k,M)=>String(M.lastSeen).localeCompare(String(k.lastSeen))).slice(0,d)}async function ze(d,p,b,k){let M=k,I=b.pathname.replace(/\/+$/,""),A=/^\/token-lens\/batches\/(tlb_[0-9a-f]+)$/.exec(I);if(d.method==="GET"&&A){let y=k?u.take(A[1],k):void 0;if(!y)throw new J(404,"No such batch job for this key (results are handed over once, and kept for a day).");return me(p,200,{id:y.id,object:"token_lens.batch_job",status:y.status,response:y.response,error:y.error})}if(d.method==="GET"&&I==="/metrics"){p.writeHead(200,{"content-type":"text/plain; version=0.0.4"}),p.end(t.prometheus(oe(d,k)));return}if(d.method==="GET"&&I==="/token-lens/report.csv"){let y=b.searchParams.get("by")==="run"?"run":"agent",S=oe(d,k),j=y==="run"?(await ve(2e3)).filter(B=>S===void 0||B.app===S):t.detail(ae((await Ke()).rows??t.snapshot(),S));p.writeHead(200,{"content-type":"text/csv; charset=utf-8","content-disposition":`attachment; filename="token-lens-savings-by-${y}.csv"`,"cache-control":"no-store"}),p.end(Ir(j,y));return}if(d.method==="GET"&&(I==="/token-lens/report"||I==="/token-lens/report.json")){let y=oe(d,k),S=(await ve(y===void 0?50:2e3)).filter(W=>y===void 0||W.app===y).slice(0,50),j=await Ke(),B=t.json(ae(j.rows??t.snapshot(),y),j.since),U=f.list().filter(W=>y===void 0||W.app===y),ne=s.list(se(d)?void 0:k??"\0",await Se());if(I.endsWith(".json"))return me(p,200,{...B,runs:S,switchedOff:U,byCustomer:ne.slice(0,100)});p.writeHead(200,{"content-type":"text/html; charset=utf-8","cache-control":"no-store"}),p.end(zr(B,S,U,ne));return}if(d.method==="GET"&&I==="/token-lens/stats"){let y=await Ke(),S=oe(d,k);return me(p,200,{...t.json(ae(y.rows??t.snapshot(),S),y.since),calibration:o.snapshot(),switchedOff:f.list().filter(j=>S===void 0||j.app===S),storage:h?.kind??"memory"})}if(d.method==="GET"&&I==="/token-lens/guard")return me(p,200,{switchedOff:f.list().filter(y=>se(d)||y.app===k)});if(d.method==="POST"&&I==="/token-lens/guard/reset"){if(!se(d))throw new J(401,"This endpoint needs the admin key.");let y=JSON.parse((await Vn(d,a.maxBodyBytes)).toString("utf8")||"{}"),[S,j]=[typeof y.app=="string"?y.app:void 0,typeof y.agent=="string"?y.agent:void 0],B=f.reset(S,j);return me(p,200,{turnedBackOn:h?await h.resetGuard(S,j):B})}if(d.method==="GET"&&I==="/token-lens/runs"){let y=Math.min(500,Number(b.searchParams.get("limit"))||50),S=oe(d,M)??b.searchParams.get("app"),j=(await ve(S?2e3:y)).filter(B=>!S||B.app===S).slice(0,y).map(({findings:B,...U})=>({...U,topFinding:B?.[0]?.title}));return me(p,200,{runs:j})}let D=/^\/token-lens\/runs\/(.+)$/.exec(I);if(d.method==="GET"&&D){let y=decodeURIComponent(D[1]),S=oe(d,M),j=r.get(y);if(j&&S!==void 0&&j.app!==S)throw new J(404,"No such run for this key.");if(j)return me(p,200,{...qn(j),findings:Gn(j),outputChecks:j.checks,calls:j.calls});let B=await h?.run(y);if(B&&S!==void 0&&B.app!==S)throw new J(404,"No such run for this key.");if(!B)throw new J(404,"No such run (runs are kept in memory, most recent first, or in shared storage when configured).");return me(p,200,B)}if(d.method==="GET"&&(I==="/token-lens/customers"||I==="/token-lens/customers.json"||I==="/token-lens/customers.csv")){let y=s.list(fe(d,b,k),await Se());if(I.endsWith(".csv")){p.writeHead(200,{"content-type":"text/csv; charset=utf-8","content-disposition":'attachment; filename="token-lens-savings-by-customer.csv"',"cache-control":"no-store"}),p.end(ft(y));return}let S=Math.min(1e4,Number(b.searchParams.get("limit"))||500);return me(p,200,{customers:y.slice(0,S),total:y.length})}let we=/^\/token-lens\/customers\/(.+)$/.exec(I);if(d.method==="GET"&&we){let y=fe(d,b,k)??"default",S=ht(decodeURIComponent(we[1]),a.customers),j=(await Se()).get(`${y}\0${S}`);if(!j)throw new J(404,"No calls for this customer yet (for the admin key, name the team with ?team=).");let B=b.searchParams.get("plan")??void 0,U;try{U={customer:si(a.customers,B,()=>{}),team:oi(a.customers,y)}}catch(x){throw x instanceof Ea?new J(400,x.message):x}let ne=s.view(y,S,j),W=s.remaining(y,S,U);return me(p,200,{...ne,limits:{customer:U.customer,team:U.team,left:W},summary:`Token Lens saved ${ne.savedTokens.toLocaleString("en-US")} tokens (${ne.savedTokensPct}%)`+(ne.costBeforeUsd?` and ${ar(ne.savedUsd)} (${ne.savedUsdPct}%) on ${ne.requests.toLocaleString("en-US")} calls.`:` on ${ne.requests.toLocaleString("en-US")} calls.`)})}if(d.method==="GET"&&I==="/token-lens/models"){let y=Object.values(ue).map(S=>({...S,calibration:o.snapshot()[S.id]}));return me(p,200,{models:y})}if(d.method==="POST"&&I==="/token-lens/analyze"){let y=JSON.parse((await Vn(d,a.maxBodyBytes)).toString("utf8")||"{}");if(typeof y.prompt!="string"||typeof y.model!="string")throw new J(400,'Send {"prompt": "...", "model": "..."}.');return me(p,200,rr({prompt:y.prompt,model:y.model,conversationTokens:y.conversationTokens,level:y.level}))}let E=/^\/token-lens\/models(?:\/(.+))?$/.exec(I);if(E&&(d.method==="POST"||d.method==="DELETE")){if(!a.adminKey)throw new J(403,'Set TOKEN_LENS_ADMIN_KEY to manage models at runtime, or list them under "models" in the config file.');if(!se(d))throw new J(401,"This endpoint needs the admin key.");if(d.method==="DELETE"&&E[1])return wi(decodeURIComponent(E[1])),me(p,200,{deleted:decodeURIComponent(E[1])});let y=JSON.parse((await Vn(d,a.maxBodyBytes)).toString("utf8")||"{}");try{return me(p,201,{model:vi(y)})}catch(S){throw new J(400,S.message)}}throw new J(404,"Not found.")}function $e(d,p,b,k){if(d.headersSent){d.end();return}me(d,b,xn(p,b,k))}let tn=d=>d==="anthropic-to-openai"?"/v1/chat/completions":"/v1/messages";function _(d,p){let b=JSON.parse(p.toString("utf8"));return Buffer.from(JSON.stringify(d==="anthropic-to-openai"?kr(b):yr(b)))}function O(d,p,b){let k=Wn(b);return p>=300?Buffer.from(JSON.stringify(Er(d,k,p))):k?Buffer.from(JSON.stringify(d==="anthropic-to-openai"?Sr(k):_r(k))):b}async function Q(d,p,b,k,M,I,A,D){let we=new AbortController;p.on("close",()=>{p.writableFinished||we.abort()}),D&&M&&(M=_(D,M));let E=W=>i(z(k,D?tn(D):b.pathname,b.search),{method:d.method,headers:K(d,k),body:W&&W.length&&d.method!=="GET"&&d.method!=="HEAD"?new Uint8Array(W):void 0,signal:we.signal}),y=await E(M);A&&M&&(y=await ya(y,M,A,W=>E(W)));let S={};y.headers.forEach((W,x)=>{Vr.has(x)||(S[x]=W)}),Object.assign(S,I);let j=(S["content-type"]??"").includes("event-stream");if(D&&!j){let W=O(D,y.status,Buffer.from(await y.arrayBuffer()));S["content-type"]="application/json",p.writeHead(y.status,S),p.write(W);let x=fa(Wn(W)??{});return{status:y.status,usage:x,replay:{at:Date.now(),status:y.status,headers:S,body:W,usage:x}}}p.writeHead(y.status,S);let B=new Bi(j);if(D&&y.body){let W=new Ln(D),x=new TextDecoder,Ie=async xe=>{if(!xe)return;let _e=Buffer.from(xe);B.push(_e),p.write(_e)||await new Promise(Je=>p.once("drain",Je))};for await(let xe of y.body)await Ie(W.push(x.decode(xe,{stream:!0})));await Ie(W.end())}else if(y.body)for await(let W of y.body)B.push(W),p.write(W)||await new Promise(x=>p.once("drain",x));let U=B.end(),ne=B.body?{at:Date.now(),status:y.status,headers:S,body:B.body,usage:U}:void 0;return{status:y.status,usage:U,replay:ne,streamText:B.streamText}}async function ya(d,p,b,k){if(d.ok||![400,408,422,429].includes(d.status))return d;let M=await d.text();if((d.status===400||d.status===422)&&!/service_tier|flex/i.test(M))return new Response(M,{status:d.status,headers:d.headers});let I=JSON.parse(p.toString("utf8"));return delete I.service_tier,b.fellBack=!0,k(Buffer.from(JSON.stringify(I)))}async function ta(d,p,b,k,M,I){I&&(k=_(I,k));let A=S=>i(z(b,I?tn(I):p.pathname,p.search),{method:"POST",headers:K(d,b),body:new Uint8Array(S)}),D=await A(k);M&&(D=await ya(D,k,M,A));let we={};D.headers.forEach((S,j)=>{Vr.has(j)||(we[j]=S)});let E=Buffer.from(await D.arrayBuffer());I&&(E=O(I,D.status,E),we["content-type"]="application/json");let y=Wn(E);return{status:D.status,headers:we,buf:E,json:y,usage:y?fa(y):{}}}async function Yr(d,p,b,k,M,I,A,D,we){let E=jr(D);if(!E||typeof D.model!="string")return!1;let y=Date.now(),S=ja.scope(M?`${k}\0${M}`:k,I,D),j=E.map(Y=>ja.key(S,Y)),B=j.map(Y=>C.get(Y)),U=E.map((Y,le)=>le).filter(Y=>B[Y]===void 0),ne,W={"content-type":"application/json"};if(U.length){let Y=U.length===E.length?we:Buffer.from(JSON.stringify({...D,input:U.map(Ee=>E[Ee])})),le=await ta(d,b,A,Y);if(le.status>=300||!Array.isArray(le.json?.data)||le.json.data.length!==U.length)return p.writeHead(le.status,le.headers),p.end(le.buf),!0;for(let Ee of le.json.data){let Ae=U[Ee.index];Ae!==void 0&&(B[Ae]=Ee.embedding,C.set(j[Ae],Ee.embedding))}ne=le.json,W=le.headers}C.hits+=E.length-U.length,C.misses+=U.length;let x=za(D.model,"generic"),Ie=x.id==="generic"?{...x,pricing:void 0}:x,xe=Y=>typeof Y=="string"?o.count(Y,Ie):Array.isArray(Y)?Y.length:0,_e=E.reduce((Y,le,Ee)=>Y+(U.includes(Ee)?0:xe(le)),0),Je=ne?.usage??{prompt_tokens:0,total_tokens:0},G={object:"list",data:B.map((Y,le)=>({object:"embedding",index:le,embedding:Y})),model:ne?.model??D.model,usage:Je};p.writeHead(200,{...W,"content-type":"application/json","x-token-lens-cached-embeddings":String(E.length-U.length),"x-token-lens-saved-tokens":String(_e)}),p.end(JSON.stringify(G));let Be=(Z(d,"x-token-lens-agent")||"-").slice(0,100);return t.record({app:k,agent:Be,run:"embeddings",profile:Ie,status:200,usage:{inputTokens:Je.prompt_tokens??0},estimatedInput:0,savedInput:_e,replayed:!1}),s.record(k,M,{profile:Ie,status:200,usage:{inputTokens:Je.prompt_tokens??0},estimatedInput:0,savedInput:_e,replayed:!1}),a.logRequests&&n(JSON.stringify({ts:new Date().toISOString(),app:k,model:D.model,upstream:I,embeddings:E.length,cached:E.length-U.length,saved:_e,ms:Date.now()-y})),!0}async function Xr(d,p,b,k){let M=Date.now(),I=b.pathname,A=d.method==="POST"?Ri(I):null,D=d.method==="GET"||d.method==="HEAD"?Buffer.alloc(0):await Vn(d,a.maxBodyBytes),we=(Z(d,"content-type")??"application/json").includes("json"),E;if(D.length&&we)try{E=JSON.parse(D.toString("utf8"))}catch{if(A)throw new J(400,"The request body is not valid JSON.")}let[y,S]=L(A,I,typeof E?.model=="string"?E.model:void 0),j=gt(Z(d,"x-token-lens-customer"),E,a.customers),B=A==="anthropic"&&S.type==="openai"?"anthropic-to-openai":A==="openai-chat"&&S.type==="anthropic"?"openai-to-anthropic":void 0;if(d.method==="POST"&&I==="/v1/embeddings"&&E&&S.type==="openai"&&a.safe.cacheEmbeddings&&a.mode!=="off"&&Z(d,"x-token-lens-mode")!=="off"&&await Yr(d,p,b,k,j,y,S,E,D))return;if(!A||!E||typeof E!="object"){await Q(d,p,b,S,D,{}),p.end();return}let U=["optimize","observe","off"].includes(Z(d,"x-token-lens-mode")??"")?Z(d,"x-token-lens-mode"):a.mode;if(U==="off"){await Q(d,p,b,S,D,{},void 0,B),p.end();return}let ne=["light","balanced","aggressive","auto"].includes(Z(d,"x-token-lens-level")??"")?Z(d,"x-token-lens-level"):a.level,W=new Set((Z(d,"x-token-lens-allow")??"").split(",").map(m=>m.trim()).filter(Boolean)),x=Ci(A,E),Ie=za(x.model,S.type==="anthropic"?"claude":"generic"),xe=Ie.pricing?Ur(a.priceTiers,x.model):void 0,_e=Ie.pricing?.longContext,Je=xe?{aboveInputTokens:xe.aboveInputTokens,inputPerMTok:xe.inputPerMTok,outputPerMTok:xe.outputPerMTok}:_e?{aboveInputTokens:_e.aboveTokens,inputPerMTok:_e.inputPerMTok,outputPerMTok:_e.outputPerMTok}:void 0,G=Je?{...Ie,priceTier:Je}:Ie,Be=In(x.refs),Y=Z(d,"x-token-lens-run")?.slice(0,200),le=x.refs.find(m=>m.user)?.get()??"",Ee=j?`${k}\0${j}`:k,Ae=Y?j?`${k}:${j}:${Y}`:`${k}:${Y}`:en(Ee,G.id,Be.system.join(`
3369
+ `);this.partial=i.pop()??"";for(let r of i)this.line(r)}else this.size+e.length<=this.limit?(this.chunks.push(Buffer.from(e)),this.size+=e.length):this.size=1/0}line(e){if(!(!e.startsWith("data:")||!e.includes('"usage"')))try{this.usage=lr(this.usage,ba(JSON.parse(e.slice(5))))}catch{}}end(){if(this.sse)this.line(this.partial),this.size!==1/0&&(this.streamText=Buffer.concat(this.chunks).toString("utf8"));else if(this.size!==1/0){this.body=Buffer.concat(this.chunks);try{this.usage=ba(JSON.parse(this.body.toString("utf8")))}catch{}}return this.usage}};function $r(a,e={}){let n=e.log??(d=>console.log(d)),i=e.fetch??fetch,r=new Bn(a.maxRuns),t=new hn,s=new yn(a.customers.maxCustomers),o=new Mn,l=new Map,c=new Set,u=new On({fetch:(...d)=>i(...d),url:(d,p)=>z(d,p,""),headers:(d,p)=>({...p}),log:n},a.batch),g=new Map,f=new Un(a.verify.probation),h=Hn.open(a.storage,{metrics:t,ledger:s,guard:f,runView:d=>({...qn(d),findings:Gn(d),outputChecks:d.checks,calls:d.calls.slice(-100)}),log:n});h?.start();let C=new ja(a.embeddingsCacheMB*1024*1024),w=new Map,N=new Map,F=new Set;for(let d of a.models)vi(d);function L(d,p,b){let k=d==="anthropic"||p.startsWith("/v1/messages");if(b){for(let A of a.routes)if(ml(A.model).test(b)){let D=a.upstreams[A.upstream];if(k&&D.type!=="anthropic"&&d!=="anthropic")throw new J(400,`Model "${b}" is served by an OpenAI-compatible upstream; call it at /v1/chat/completions.`);if(d&&!k&&D.type!=="openai"&&d!=="openai-chat")throw new J(400,`Model "${b}" is served by an Anthropic upstream; call it at /v1/messages or /v1/chat/completions.`);return[A.upstream,D]}}let M=Object.entries(a.upstreams),I=k?M.find(([,A])=>A.type==="anthropic"):a.defaultUpstream?[a.defaultUpstream,a.upstreams[a.defaultUpstream]]:M.find(([,A])=>A.type==="openai");if(!I)throw new J(502,`No ${k?"Anthropic":"OpenAI-compatible"} upstream is configured for this gateway.`);return I}function z(d,p,b){return d.type==="anthropic"?d.baseUrl.replace(/\/v1$/,"")+p+b:d.baseUrl+p.replace(/^\/v1/,"")+b}function K(d,p){let b={};for(let[k,M]of Object.entries(d.headers))M===void 0||ul.has(k)||k.startsWith("x-token-lens-")||dl.includes(k)&&(p.apiKey||a.clientKeys.length)||(b[k]=Array.isArray(M)?M.join(", "):M);if(b["accept-encoding"]="identity",!p.apiKey&&!a.clientKeys.length&&(p.type==="anthropic"&&!b["x-api-key"]&&/^Bearer /i.test(b.authorization??"")?(b["x-api-key"]=b.authorization.replace(/^Bearer /i,""),delete b.authorization):p.type==="openai"&&!b.authorization&&b["x-api-key"]&&!p.authHeader&&(b.authorization=`Bearer ${b["x-api-key"]}`,delete b["x-api-key"],delete b["anthropic-version"])),p.type==="anthropic"&&!b["anthropic-version"]&&(b["anthropic-version"]="2023-06-01"),p.apiKey){let k=p.authHeader??(p.type==="anthropic"?"x-api-key":"authorization");b[k]=k==="authorization"?`Bearer ${p.apiKey}`:p.apiKey,p.type==="anthropic"&&!b["anthropic-version"]&&(b["anthropic-version"]="2023-06-01")}return{...b,...p.headers}}function ia(d){if(!a.clientKeys.length)return(Z(d,"x-token-lens-app")||"default").slice(0,64);let p=Wr(d),b=p?a.clientKeys.find(k=>Kr(k.key,p)):void 0;return b?b.name:null}let se=d=>{let p=Wr(d);return!!a.adminKey&&!!p&&Kr(a.adminKey,p)},oe=(d,p)=>!a.isolateTeams||se(d)?void 0:p??"\0",ae=(d,p)=>p===void 0?d:new Map([...d].filter(([b])=>b.split("\0")[0]===p)),Se=async()=>h?h.customerTotals():s.snapshot(),fe=(d,p,b)=>se(d)?p.searchParams.get("team")??void 0:b??"\0";async function Ke(){return h?h.totals():{}}async function ve(d){let p=r.list().slice(0,d).map(k=>({...qn(k),findings:Gn(k)}));if(!h)return p;let b=(await h.recentRuns(d)).filter(k=>!r.get(k.id));return[...p,...b].sort((k,M)=>String(M.lastSeen).localeCompare(String(k.lastSeen))).slice(0,d)}async function ze(d,p,b,k){let M=k,I=b.pathname.replace(/\/+$/,""),A=/^\/token-lens\/batches\/(tlb_[0-9a-f]+)$/.exec(I);if(d.method==="GET"&&A){let y=k?u.take(A[1],k):void 0;if(!y)throw new J(404,"No such batch job for this key (results are handed over once, and kept for a day).");return me(p,200,{id:y.id,object:"token_lens.batch_job",status:y.status,response:y.response,error:y.error})}if(d.method==="GET"&&I==="/metrics"){p.writeHead(200,{"content-type":"text/plain; version=0.0.4"}),p.end(t.prometheus(oe(d,k)));return}if(d.method==="GET"&&I==="/token-lens/report.csv"){let y=b.searchParams.get("by")==="run"?"run":"agent",S=oe(d,k),j=y==="run"?(await ve(2e3)).filter(B=>S===void 0||B.app===S):t.detail(ae((await Ke()).rows??t.snapshot(),S));p.writeHead(200,{"content-type":"text/csv; charset=utf-8","content-disposition":`attachment; filename="token-lens-savings-by-${y}.csv"`,"cache-control":"no-store"}),p.end(Ir(j,y));return}if(d.method==="GET"&&(I==="/token-lens/report"||I==="/token-lens/report.json")){let y=oe(d,k),S=(await ve(y===void 0?50:2e3)).filter(W=>y===void 0||W.app===y).slice(0,50),j=await Ke(),B=t.json(ae(j.rows??t.snapshot(),y),j.since),U=f.list().filter(W=>y===void 0||W.app===y),ne=s.list(se(d)?void 0:k??"\0",await Se());if(I.endsWith(".json"))return me(p,200,{...B,runs:S,switchedOff:U,byCustomer:ne.slice(0,100)});p.writeHead(200,{"content-type":"text/html; charset=utf-8","cache-control":"no-store"}),p.end(zr(B,S,U,ne));return}if(d.method==="GET"&&I==="/token-lens/stats"){let y=await Ke(),S=oe(d,k);return me(p,200,{...t.json(ae(y.rows??t.snapshot(),S),y.since),calibration:o.snapshot(),switchedOff:f.list().filter(j=>S===void 0||j.app===S),storage:h?.kind??"memory"})}if(d.method==="GET"&&I==="/token-lens/guard")return me(p,200,{switchedOff:f.list().filter(y=>se(d)||y.app===k)});if(d.method==="POST"&&I==="/token-lens/guard/reset"){if(!se(d))throw new J(401,"This endpoint needs the admin key.");let y=JSON.parse((await Vn(d,a.maxBodyBytes)).toString("utf8")||"{}"),[S,j]=[typeof y.app=="string"?y.app:void 0,typeof y.agent=="string"?y.agent:void 0],B=f.reset(S,j);return me(p,200,{turnedBackOn:h?await h.resetGuard(S,j):B})}if(d.method==="GET"&&I==="/token-lens/runs"){let y=Math.min(500,Number(b.searchParams.get("limit"))||50),S=oe(d,M)??b.searchParams.get("app"),j=(await ve(S?2e3:y)).filter(B=>!S||B.app===S).slice(0,y).map(({findings:B,...U})=>({...U,topFinding:B?.[0]?.title}));return me(p,200,{runs:j})}let D=/^\/token-lens\/runs\/(.+)$/.exec(I);if(d.method==="GET"&&D){let y=decodeURIComponent(D[1]),S=oe(d,M),j=r.get(y);if(j&&S!==void 0&&j.app!==S)throw new J(404,"No such run for this key.");if(j)return me(p,200,{...qn(j),findings:Gn(j),outputChecks:j.checks,calls:j.calls});let B=await h?.run(y);if(B&&S!==void 0&&B.app!==S)throw new J(404,"No such run for this key.");if(!B)throw new J(404,"No such run (runs are kept in memory, most recent first, or in shared storage when configured).");return me(p,200,B)}if(d.method==="GET"&&(I==="/token-lens/customers"||I==="/token-lens/customers.json"||I==="/token-lens/customers.csv")){let y=s.list(fe(d,b,k),await Se());if(I.endsWith(".csv")){p.writeHead(200,{"content-type":"text/csv; charset=utf-8","content-disposition":'attachment; filename="token-lens-savings-by-customer.csv"',"cache-control":"no-store"}),p.end(ft(y));return}let S=Math.min(1e4,Number(b.searchParams.get("limit"))||500);return me(p,200,{customers:y.slice(0,S),total:y.length})}let we=/^\/token-lens\/customers\/(.+)$/.exec(I);if(d.method==="GET"&&we){let y=fe(d,b,k)??"default",S=ht(decodeURIComponent(we[1]),a.customers),j=(await Se()).get(`${y}\0${S}`);if(!j)throw new J(404,"No calls for this customer yet (for the admin key, name the team with ?team=).");let B=b.searchParams.get("plan")??void 0,U;try{U={customer:si(a.customers,B,()=>{}),team:oi(a.customers,y)}}catch(x){throw x instanceof Aa?new J(400,x.message):x}let ne=s.view(y,S,j),W=s.remaining(y,S,U);return me(p,200,{...ne,limits:{customer:U.customer,team:U.team,left:W},summary:`Token Lens saved ${ne.savedTokens.toLocaleString("en-US")} tokens (${ne.savedTokensPct}%)`+(ne.costBeforeUsd?` and ${ar(ne.savedUsd)} (${ne.savedUsdPct}%) on ${ne.requests.toLocaleString("en-US")} calls.`:` on ${ne.requests.toLocaleString("en-US")} calls.`)})}if(d.method==="GET"&&I==="/token-lens/models"){let y=Object.values(ue).map(S=>({...S,calibration:o.snapshot()[S.id]}));return me(p,200,{models:y})}if(d.method==="POST"&&I==="/token-lens/analyze"){let y=JSON.parse((await Vn(d,a.maxBodyBytes)).toString("utf8")||"{}");if(typeof y.prompt!="string"||typeof y.model!="string")throw new J(400,'Send {"prompt": "...", "model": "..."}.');return me(p,200,rr({prompt:y.prompt,model:y.model,conversationTokens:y.conversationTokens,level:y.level}))}let E=/^\/token-lens\/models(?:\/(.+))?$/.exec(I);if(E&&(d.method==="POST"||d.method==="DELETE")){if(!a.adminKey)throw new J(403,'Set TOKEN_LENS_ADMIN_KEY to manage models at runtime, or list them under "models" in the config file.');if(!se(d))throw new J(401,"This endpoint needs the admin key.");if(d.method==="DELETE"&&E[1])return wi(decodeURIComponent(E[1])),me(p,200,{deleted:decodeURIComponent(E[1])});let y=JSON.parse((await Vn(d,a.maxBodyBytes)).toString("utf8")||"{}");try{return me(p,201,{model:vi(y)})}catch(S){throw new J(400,S.message)}}throw new J(404,"Not found.")}function $e(d,p,b,k){if(d.headersSent){d.end();return}me(d,b,xn(p,b,k))}let tn=d=>d==="anthropic-to-openai"?"/v1/chat/completions":"/v1/messages";function _(d,p){let b=JSON.parse(p.toString("utf8"));return Buffer.from(JSON.stringify(d==="anthropic-to-openai"?kr(b):yr(b)))}function O(d,p,b){let k=Wn(b);return p>=300?Buffer.from(JSON.stringify(Er(d,k,p))):k?Buffer.from(JSON.stringify(d==="anthropic-to-openai"?Sr(k):_r(k))):b}async function Q(d,p,b,k,M,I,A,D){let we=new AbortController;p.on("close",()=>{p.writableFinished||we.abort()}),D&&M&&(M=_(D,M));let E=W=>i(z(k,D?tn(D):b.pathname,b.search),{method:d.method,headers:K(d,k),body:W&&W.length&&d.method!=="GET"&&d.method!=="HEAD"?new Uint8Array(W):void 0,signal:we.signal}),y=await E(M);A&&M&&(y=await va(y,M,A,W=>E(W)));let S={};y.headers.forEach((W,x)=>{Vr.has(x)||(S[x]=W)}),Object.assign(S,I);let j=(S["content-type"]??"").includes("event-stream");if(D&&!j){let W=O(D,y.status,Buffer.from(await y.arrayBuffer()));S["content-type"]="application/json",p.writeHead(y.status,S),p.write(W);let x=ba(Wn(W)??{});return{status:y.status,usage:x,replay:{at:Date.now(),status:y.status,headers:S,body:W,usage:x}}}p.writeHead(y.status,S);let B=new Bi(j);if(D&&y.body){let W=new Ln(D),x=new TextDecoder,Ie=async xe=>{if(!xe)return;let _e=Buffer.from(xe);B.push(_e),p.write(_e)||await new Promise(Je=>p.once("drain",Je))};for await(let xe of y.body)await Ie(W.push(x.decode(xe,{stream:!0})));await Ie(W.end())}else if(y.body)for await(let W of y.body)B.push(W),p.write(W)||await new Promise(x=>p.once("drain",x));let U=B.end(),ne=B.body?{at:Date.now(),status:y.status,headers:S,body:B.body,usage:U}:void 0;return{status:y.status,usage:U,replay:ne,streamText:B.streamText}}async function va(d,p,b,k){if(d.ok||![400,408,422,429].includes(d.status))return d;let M=await d.text();if((d.status===400||d.status===422)&&!/service_tier|flex/i.test(M))return new Response(M,{status:d.status,headers:d.headers});let I=JSON.parse(p.toString("utf8"));return delete I.service_tier,b.fellBack=!0,k(Buffer.from(JSON.stringify(I)))}async function ta(d,p,b,k,M,I){I&&(k=_(I,k));let A=S=>i(z(b,I?tn(I):p.pathname,p.search),{method:"POST",headers:K(d,b),body:new Uint8Array(S)}),D=await A(k);M&&(D=await va(D,k,M,A));let we={};D.headers.forEach((S,j)=>{Vr.has(j)||(we[j]=S)});let E=Buffer.from(await D.arrayBuffer());I&&(E=O(I,D.status,E),we["content-type"]="application/json");let y=Wn(E);return{status:D.status,headers:we,buf:E,json:y,usage:y?ba(y):{}}}async function Yr(d,p,b,k,M,I,A,D,we){let E=jr(D);if(!E||typeof D.model!="string")return!1;let y=Date.now(),S=ja.scope(M?`${k}\0${M}`:k,I,D),j=E.map(Y=>ja.key(S,Y)),B=j.map(Y=>C.get(Y)),U=E.map((Y,le)=>le).filter(Y=>B[Y]===void 0),ne,W={"content-type":"application/json"};if(U.length){let Y=U.length===E.length?we:Buffer.from(JSON.stringify({...D,input:U.map(Ee=>E[Ee])})),le=await ta(d,b,A,Y);if(le.status>=300||!Array.isArray(le.json?.data)||le.json.data.length!==U.length)return p.writeHead(le.status,le.headers),p.end(le.buf),!0;for(let Ee of le.json.data){let Ae=U[Ee.index];Ae!==void 0&&(B[Ae]=Ee.embedding,C.set(j[Ae],Ee.embedding))}ne=le.json,W=le.headers}C.hits+=E.length-U.length,C.misses+=U.length;let x=za(D.model,"generic"),Ie=x.id==="generic"?{...x,pricing:void 0}:x,xe=Y=>typeof Y=="string"?o.count(Y,Ie):Array.isArray(Y)?Y.length:0,_e=E.reduce((Y,le,Ee)=>Y+(U.includes(Ee)?0:xe(le)),0),Je=ne?.usage??{prompt_tokens:0,total_tokens:0},G={object:"list",data:B.map((Y,le)=>({object:"embedding",index:le,embedding:Y})),model:ne?.model??D.model,usage:Je};p.writeHead(200,{...W,"content-type":"application/json","x-token-lens-cached-embeddings":String(E.length-U.length),"x-token-lens-saved-tokens":String(_e)}),p.end(JSON.stringify(G));let Be=(Z(d,"x-token-lens-agent")||"-").slice(0,100);return t.record({app:k,agent:Be,run:"embeddings",profile:Ie,status:200,usage:{inputTokens:Je.prompt_tokens??0},estimatedInput:0,savedInput:_e,replayed:!1}),s.record(k,M,{profile:Ie,status:200,usage:{inputTokens:Je.prompt_tokens??0},estimatedInput:0,savedInput:_e,replayed:!1}),a.logRequests&&n(JSON.stringify({ts:new Date().toISOString(),app:k,model:D.model,upstream:I,embeddings:E.length,cached:E.length-U.length,saved:_e,ms:Date.now()-y})),!0}async function Xr(d,p,b,k){let M=Date.now(),I=b.pathname,A=d.method==="POST"?Ri(I):null,D=d.method==="GET"||d.method==="HEAD"?Buffer.alloc(0):await Vn(d,a.maxBodyBytes),we=(Z(d,"content-type")??"application/json").includes("json"),E;if(D.length&&we)try{E=JSON.parse(D.toString("utf8"))}catch{if(A)throw new J(400,"The request body is not valid JSON.")}let[y,S]=L(A,I,typeof E?.model=="string"?E.model:void 0),j=gt(Z(d,"x-token-lens-customer"),E,a.customers),B=A==="anthropic"&&S.type==="openai"?"anthropic-to-openai":A==="openai-chat"&&S.type==="anthropic"?"openai-to-anthropic":void 0;if(d.method==="POST"&&I==="/v1/embeddings"&&E&&S.type==="openai"&&a.safe.cacheEmbeddings&&a.mode!=="off"&&Z(d,"x-token-lens-mode")!=="off"&&await Yr(d,p,b,k,j,y,S,E,D))return;if(!A||!E||typeof E!="object"){await Q(d,p,b,S,D,{}),p.end();return}let U=["optimize","observe","off"].includes(Z(d,"x-token-lens-mode")??"")?Z(d,"x-token-lens-mode"):a.mode;if(U==="off"){await Q(d,p,b,S,D,{},void 0,B),p.end();return}let ne=["light","balanced","aggressive","auto"].includes(Z(d,"x-token-lens-level")??"")?Z(d,"x-token-lens-level"):a.level,W=new Set((Z(d,"x-token-lens-allow")??"").split(",").map(m=>m.trim()).filter(Boolean)),x=Ci(A,E),Ie=za(x.model,S.type==="anthropic"?"claude":"generic"),xe=Ie.pricing?Ur(a.priceTiers,x.model):void 0,_e=Ie.pricing?.longContext,Je=xe?{aboveInputTokens:xe.aboveInputTokens,inputPerMTok:xe.inputPerMTok,outputPerMTok:xe.outputPerMTok}:_e?{aboveInputTokens:_e.aboveTokens,inputPerMTok:_e.inputPerMTok,outputPerMTok:_e.outputPerMTok}:void 0,G=Je?{...Ie,priceTier:Je}:Ie,Be=In(x.refs),Y=Z(d,"x-token-lens-run")?.slice(0,200),le=x.refs.find(m=>m.user)?.get()??"",Ee=j?`${k}\0${j}`:k,Ae=Y?j?`${k}:${j}:${Y}`:`${k}:${Y}`:en(Ee,G.id,Be.system.join(`
3370
3370
  `),le),ra=(Z(d,"x-token-lens-agent")||"-").slice(0,100),H=r.open(Ae,k,!!Y,ra);j&&(H.customer=j);let Kn=en(y,I,D.toString("utf8")),Qr=H.requestHashes.has(Kn);H.requestHashes.size<5e3&&H.requestHashes.add(Kn);let $n=en(Be.system.join(`
3371
3371
  `),Be.tools.join(`
3372
3372
  `)),Jn=H.prefixes.get($n)??0;H.prefixes.set($n,Jn+1);let Zr=(Array.isArray(E.tools)?E.tools:[]).map(m=>m?.function?.name??m?.name??"").filter(Boolean),qi=Lr(Be.system.join(`
3373
3373
  `),Be.tools,Zr),rn=Br(H.prefixPrint,qi,Be.system.join(`
3374
3374
  `),Be.tools.join(`
3375
- `));H.prefixPrint=qi;let Ui=Fr(A,E),es=Gr(A,E),sn=qr(x.toolCalls);if(sn.length){let m=w.get(Ee);m||w.set(Ee,m=new Map);for(let P of sn){let T=m.get(P);T||(m.size>=2e4&&m.delete(m.keys().next().value),m.set(P,T=new Set)),T.size<50&&T.add(Ae)}let v=new Set,R=0;for(let P of sn){let T=m.get(P);if(T.size>1){R++;for(let q of T)q!==Ae&&v.add(q)}}H.sharedReads=R?{files:R,of:sn.length,runs:v.size,agents:[...new Set([...v].map(P=>r.get(P)?.agent??"-"))].slice(0,5)}:void 0}let Hi=m=>a.reportToolArgs?m:`input #${en(m).slice(0,8)}`,Yn=new Map;for(let m of x.toolCalls){let v=`${m.name}\0${m.args}`,R=Yn.get(v)??{name:m.name,args:Hi(m.args),count:0};R.count++,Yn.set(v,R)}let as=[...Yn.values()].filter(m=>m.count>1),Vi,Da=x.toolCalls.at(-1);if(Da){let m=0;for(let v=x.toolCalls.length-1;v>=0&&x.toolCalls[v].name===Da.name&&x.toolCalls[v].args===Da.args;v--)m++;m>1&&(Vi={name:Da.name,args:Hi(Da.args),times:m})}let Te=jn(x.refs,G,o),Xn=Te.parts.system+Te.parts.tools,sa={"x-token-lens-run":Ae,"x-token-lens-profile":G.id},ns=()=>{let m=S.type==="anthropic"?"anthropic":"openai",v=Rn(A,E);if(!v.length)return;let R=Math.max(...v.map(T=>T.turn)),P=T=>Si(m,T)??0;return{count:v.length,tokens:v.reduce((T,q)=>T+P(q),0),oldTokens:v.filter(T=>T.turn<R-1).reduce((T,q)=>T+P(q),0),provider:m}},va=(m,v,R,P,T={},q=1)=>{let ge={images:ns(),at:new Date().toISOString(),model:x.model,profile:G.id,upstream:y,status:m,parts:R.parts,input:R.total,toolOutputs:R.toolOutputs,usage:v,actions:P,duplicate:Qr,window:G.contextWindow,ms:Date.now()-M,costUsd:T.replayed||m>=400?0:ua(G,{...v,inputTokens:v.inputTokens??R.total})*q,costBeforeUsd:gn(G,{...v,inputTokens:v.inputTokens??R.total},P.reduce((Oe,ca)=>Oe+ca.saved,0)),toolCalls:x.toolCalls.length,repeatedToolCalls:as,sameCallStreak:Vi,api:A,...rn&&Xn>=1024?{cacheBreak:{kind:rn.kind,where:rn.where,tokens:Math.round(rn.lostShare*Xn)}}:{},...Ui?{jsonRetries:Ui,structuredOutput:es}:{},...G.priceTier?{priceTier:G.priceTier}:{},...q!==1?{priceFactor:q}:{},...T};r.record(H,ge),h?.markRun(H);let la=P.reduce((Oe,ca)=>Oe+ca.saved,0);t.record({app:k,agent:H.agent,run:Ae,profile:G,status:m,usage:v,estimatedInput:R.total,savedInput:la,replayed:!!T.replayed,priceFactor:q}),s.record(k,j,{profile:G,status:m,usage:v,estimatedInput:R.total,savedInput:la,replayed:!!T.replayed,priceFactor:q}),a.logRequests&&n(JSON.stringify({ts:ge.at,app:k,run:Ae,model:x.model,profile:G.id,upstream:y,status:m,input:v.inputTokens??R.total,output:v.outputTokens,cached:v.cachedInputTokens,saved:la,actions:P.map(Oe=>Oe.id),replayed:T.replayed||void 0,ms:ge.ms}))},Wi=`${Ee}\0${Kn}`,Ki=a.safe.dedupeRetries&&U==="optimize"&&!x.stream&&x.temperature===0,oa=Ki?l.get(Wi):void 0;if(oa&&Date.now()-oa.at<=a.retryWindowSeconds*1e3){p.writeHead(oa.status,{...oa.headers,...sa,"x-token-lens-replayed":"true"}),va(oa.status,oa.usage,Te,[],{replayed:!0}),p.end(oa.body);return}let on=Number(Z(d,"x-token-lens-max-calls"))||a.runBudget.maxCalls,ln=Number(Z(d,"x-token-lens-max-cost-usd"))||a.runBudget.maxCostUsd,Qn=H.calls.filter(m=>!m.replayed&&!m.overBudget&&m.status<400),$i=Qn.reduce((m,v)=>m+v.costUsd,0);if(on&&Qn.length>=on||ln&&$i>=ln){let m=`Token Lens stopped this run: it reached its budget (${Qn.length} calls, $${$i.toFixed(4)} spent; limit ${[on?`${on} calls`:"",ln?`$${ln}`:""].filter(Boolean).join(" / ")}). Start a new run or raise the limit.`;p.writeHead(429,{"content-type":"application/json",...sa,"x-token-lens-budget":"exceeded"}),p.end(JSON.stringify(xn(A,429,m))),va(429,{},Te,[],{overBudget:!0});return}let wa;try{wa={customer:si(a.customers,Z(d,"x-token-lens-plan")?.slice(0,64),m=>Z(d,m)),team:oi(a.customers,k)}}catch(m){throw m instanceof Ea?new J(400,m.message):m}if(j||(wa.customer={}),li(wa.customer)||li(wa.team)){let m=s.over(k,j,wa);if(m){let R=m.limit.endsWith("Usd"),P=ge=>R?`$${ge.toFixed(4)}`:`${Math.round(ge).toLocaleString("en-US")} tokens`,T=m.limit.startsWith("daily")?"daily":"monthly",q=`Token Lens stopped this call: ${m.who==="customer"?`customer "${j}"`:"this team"} reached its ${T} limit (${P(m.spent)} of ${P(m.cap)}). It resets at the start of the next UTC ${T==="daily"?"day":"month"}.`;p.writeHead(429,{"content-type":"application/json",...sa,"x-token-lens-budget":`${m.who}-${T}-${R?"usd":"tokens"}`}),p.end(JSON.stringify(xn(A,429,q))),va(429,{},Te,[],{overBudget:!0});return}let v=s.remaining(k,j,wa);v.usd!==void 0&&(sa["x-token-lens-budget-left-usd"]=v.usd.toFixed(6)),v.tokens!==void 0&&(sa["x-token-lens-budget-left-tokens"]=String(Math.floor(v.tokens)))}let Ji=Z(d,"x-token-lens-batch"),cn=Ji==="true"||Ji==="1",Me=f.configFor(a,k,ra,W,!!x.stream||cn),Yi=Me.cfg.optIn.warmCache||(Me.allow.has("warm-cache")?{maxWaitMs:15e3}:!1),un,dn;if(Yi&&U==="optimize"&&!cn&&Xn>=1024&&Me.cfg.safe.promptCaching){let m=[k,y,x.model,$n].join("\0"),v=N.get(m);if(v){let R=Date.now(),P;await Promise.race([v,new Promise(T=>P=setTimeout(T,Yi.maxWaitMs))]),clearTimeout(P),un=Date.now()-R}else if(!F.has(m)){let R;N.set(m,new Promise(P=>R=P)),dn=()=>{N.has(m)&&(N.delete(m),F.add(m),F.size>5e3&&F.delete(F.values().next().value),R())},p.on("close",dn)}}let ee={actions:[],tips:[]},te=Te;if(U==="optimize"){let m={cfg:Me.cfg,body:E,parsed:x,profile:G,upstream:S,counter:o,prefixRepeats:dn?Math.max(Jn,1):Jn,runId:Ae,level:ne,allow:Me.allow,conversationTokens:Te.total-Te.parts.prompt-Te.parts.context};ee=cr(m),ee.actions.length&&(te=jn(x.refs,G,o));let v=ur(m,te);v&&ee.actions.push(v),un!==void 0&&ee.actions.push({id:"warm-cache",saved:0})}let is=Me.cfg.optIn.flex||Me.allow.has("flex"),ts=S.flex??(S.type==="openai"&&/(^|\.)api\.openai\.com$/.test(new URL(S.baseUrl).hostname)),Ye=is&&ts&&A!=="anthropic"&&!cn&&E.service_tier===void 0?{fellBack:!1}:void 0;if(Ye&&(E.service_tier="flex",ee.actions.push({id:"flex",saved:0})),a.blockOverContext&&te.total+(x.maxOutputTokens??0)>G.contextWindow){let m=`This request needs about ${te.total+(x.maxOutputTokens??0)} tokens but ${G.label} has a ${G.contextWindow}-token context window. Rejected by Token Lens before it was sent.`;$e(p,A,400,m),va(400,{},te,ee.actions);return}if(cn){if(x.stream)return $e(p,A,400,"Requests sent with x-token-lens-batch can't stream.");let m=u.canBatch(A,I,S);if(m)return $e(p,A,400,m);let v=K(d,S);for(let T of["content-length","host","accept-encoding"])delete v[T];let R=[...ee.actions,{id:"batch",saved:0}],P=u.add({app:k,protocol:A,path:I,upstream:y,body:E,onDone:(T,q)=>va(q?200:502,T,te,R,{},q?a.batch.discount:1)},S,v);p.writeHead(202,{"content-type":"application/json",...sa,"x-token-lens-batch-id":P.id}),p.end(JSON.stringify({id:P.id,object:"token_lens.batch_job",status:P.status,result_url:`/token-lens/batches/${P.id}`}));return}let Xi=Di({...H,calls:[...H.calls,{parts:te.parts,status:200}]}),rs=ee.actions.reduce((m,v)=>m+v.saved,0),be={...sa,"x-token-lens-input-tokens":String(te.total),"x-token-lens-saved-tokens":String(rs)};ee.actions.length&&(be["x-token-lens-actions"]=ee.actions.map(m=>m.id).join(","));let Qi=ee.tips.filter(m=>m.id!=="lighter-model");Qi.length&&(be["x-token-lens-tips"]=Qi.map(m=>m.id).join(",")),Xi&&(be["x-token-lens-growing"]=Xi),un!==void 0&&(be["x-token-lens-warm-wait-ms"]=String(un)),G.priceTier&&te.total>.9*G.priceTier.aboveInputTokens&&(be["x-token-lens-price-tier"]=`${te.total>G.priceTier.aboveInputTokens?"above":"near"} ${G.priceTier.aboveInputTokens}`);let Zi=Me.cfg.optIn.lazyTools||(Me.allow.has("lazy-tools")?{minTools:8}:!1),je=U==="optimize"&&Zi?gr(A,E,Zi.minTools):null,Zn=Me.cfg.optIn.parkToolOutputs||(Me.allow.has("park-tool-outputs")?{minTokens:1500,keepLast:1}:!1),ss=je||Zn?structuredClone(E):void 0,Xe=U==="optimize"&&Zn&&!x.stream&&(A==="openai-chat"||A==="anthropic")&&E.tool_choice!=="none"?Tr(x,Zn,o,G,H.parked??=new Map):[];if(H.parked&&H.parked.size>50)for(let m of[...H.parked.keys()].slice(0,H.parked.size-50))H.parked.delete(m);let et=f.onProbation(k,ra,[...ee.actions.map(m=>m.id),...je?["lazy-tools"]:[],...Xe.length?["park-tool-outputs"]:[]]),ei=et.length?ta(d,b,S,D,void 0,B).catch(()=>{}):void 0,ie;if(je||Xe.length){let m=H.loadedTools??=new Set;for(let ke of x.toolCalls)m.add(ke.name);let v={},R=0,P=0,T,q=!1;for(;P<4&&!T;P++){let ke=je?hr(A,E,je,m):[];Xe.length&&!(E.tools??=[]).some(Qe=>(Qe?.function?.name??Qe?.name)===Ve)&&E.tools.push(Ar(A));let Ce=await ta(d,b,S,Buffer.from(JSON.stringify(E)),Ye,B);if(v=Mi(v,Ce.usage),Ce.status>=300){T=Ce;break}let ii=Rr(A,Ce.json),lt=Xe.length?ii.filter(Qe=>Qe.name===Ve):[],ti=ke.length?fr(A,Ce.json,ke):[];if(!lt.length&&!ti.length){T=Ce;break}R+=Ce.usage.inputTokens??0;for(let Qe of ti)m.add(Qe);if(lt.length){if(ii.some(Sa=>Sa.name!==Ve&&Sa.name!==ba))break;let Qe=new Map(ii.map(Sa=>[Sa.id,Sa.name===Ve?Cr(H.parked,Sa.args):`Loaded: ${ti.join(", ")||"none"}. Their full definitions are now in the tool list.`]));Pr(A,E,Ce.json,Qe)}}T||(q=!0,Object.keys(E).forEach(ke=>delete E[ke]),Object.assign(E,ss,je?{tools:je.original}:{}),T=await ta(d,b,S,Buffer.from(JSON.stringify(E)),Ye,B),v=Mi(v,T.usage));let ge=jn(Ci(A,E).refs,G,o),la=te.total-ge.total-R,Oe=je&&!q?Math.max(0,te.parts.tools-ge.parts.tools):0,ca=Xe.length&&!q?Math.max(0,la+R-Oe):0,ot=(ke,Ce)=>Oe+ca?Math.round(la*ke/(Oe+ca)):Ce?Math.round(la/((je?1:0)+(Xe.length?1:0))):0;je&&(Oe||P>1||q)&&ee.actions.push({id:"lazy-tools",saved:ot(Oe,!0),count:P-1}),Xe.length&&ee.actions.push({id:"park-tool-outputs",saved:ot(ca,!0),count:Xe.length});for(let ke of Le(A,T.json).toolCalls)m.add(ke.name);te={...ge,total:ge.total},be["x-token-lens-input-tokens"]=String(te.total),be["x-token-lens-saved-tokens"]=String(ee.actions.reduce((ke,Ce)=>ke+Ce.saved,0)),be["x-token-lens-actions"]=ee.actions.map(ke=>ke.id).join(","),ie={...T,usage:v}}else ei&&(ie=await ta(d,b,S,Buffer.from(JSON.stringify(E)),Ye,B));let mn,ai=!1;if(ie&&ei){let m=await ei,v=et.filter(R=>ee.actions.some(P=>P.id===R));if(m&&m.status<300&&v.length){let R=Le(A,m.json),P=Nn(Le(A,ie.json),R,ee.actions.reduce((T,q)=>T+q.saved,0),a.verify.minSimilarity);ie.status<300&&P.match?(f.confirm(k,ra,v),mn={check:P,ids:v,originalOutput:R,costUsd:ua(G,m.usage),tokens:(m.usage.inputTokens??0)+(m.usage.outputTokens??0)}):(mn={check:{...P,match:!1},ids:v,originalOutput:R,costUsd:ua(G,ie.usage),tokens:(ie.usage.inputTokens??0)+(ie.usage.outputTokens??0)},ie=m,ai=!0,te=Te,be["x-token-lens-input-tokens"]=String(Te.total),be["x-token-lens-saved-tokens"]="0",be["x-token-lens-answer"]="original",delete be["x-token-lens-actions"])}}let pe;if(ie)p.writeHead(ie.status,{...ie.headers,...be}),p.write(ie.buf),pe={status:ie.status,usage:ie.usage,replay:{at:Date.now(),status:ie.status,headers:ie.headers,body:ie.buf,usage:ie.usage}};else{let m=ee.actions.length?Buffer.from(JSON.stringify(E)):D;pe=await Q(d,p,b,S,m,be,Ye,B)}pe.usage.inputTokens&&!je&&!ai&&o.observe(G,te.raw,pe.usage.inputTokens),Ki&&pe.replay&&pe.status<300&&(l.set(Wi,pe.replay),l.size>500&&l.delete(l.keys().next().value));let pn=ai?[]:ee.actions.filter(m=>m.id!=="flex"||!Ye?.fellBack);va(pe.status,pe.usage,te,pn,{},Ye&&!Ye.fellBack&&pe.status<300?a.flexDiscount:1),dn?.(),p.end();let at=`${k}\0${H.agent}`,Ne=g.get(at)??{checks:0,checkTokens:0,saved:0};g.set(at,Ne);let ni=pn.reduce((m,v)=>m+v.saved,0);Ne.saved+=ni;let nt=()=>{if(!x.stream)return D;let{stream:m,stream_options:v,...R}=JSON.parse(D.toString("utf8"));return Buffer.from(JSON.stringify(R))},it=(m,v,R)=>{let P={...m,actions:v,costUsd:R};return H.checks.push(P),h?.markRun(H),H.checks.length>200&&H.checks.shift(),t.recordCheck(k,H.agent,G,m.match,R),a.logRequests&&n(JSON.stringify({ts:m.at,app:k,run:Ae,outputCheck:m.match?"same":"different",similarity:m.similarity,sameToolCalls:m.sameToolCalls})),P},tt=async(m,v,R)=>{let P=await ta(d,b,S,nt(),void 0,B);if(P.status<300&&(Ne.checkTokens+=(P.usage.inputTokens??Te.total)+(P.usage.outputTokens??0),t.recordCheck(k,H.agent,G,!0,ua(G,P.usage),!1),!Nn(Le(A,P.json),v,0,a.verify.minSimilarity).match)){m.modelVaries=!0;return}let T=f.suspend(k,ra,R,m);h&&T.length&&await h.suspend(f.list().filter(q=>q.app===k&&q.agent===ra&&T.includes(q.fix))),T.length&&n(JSON.stringify({ts:new Date().toISOString(),level:"warn",app:k,agent:ra,switchedOff:T,reason:"an output check found a different answer"}))},rt=m=>{let v=m().catch(R=>n(JSON.stringify({ts:new Date().toISOString(),level:"error",outputCheck:String(R?.message??R)})));c.add(v),v.finally(()=>c.delete(v))};if(mn){let m=mn;Ne.checks++,Ne.checkTokens+=m.tokens;let v=it(m.check,m.ids,m.costUsd);m.check.match||rt(()=>tt(v,m.originalOutput,m.ids));return}let os=pn.some(m=>Ma.has(m.id))&&a.verify.sampleRate>0?Math.max(a.verify.sampleRate,.25):a.verify.sampleRate,ls=Z(d,"x-token-lens-verify")==="1",cs=Ne.checks<a.verify.freeChecks||Ne.checkTokens<Ne.saved*a.verify.maxShareOfSavings,st=x.stream?pe.streamText!==void 0?mr(A,pe.streamText):void 0:pe.replay?Le(A,Wn(pe.replay.body)):void 0;ni>0&&pe.status<300&&st&&(ls||cs&&Math.random()<os)&&(Ne.checks++,rt(async()=>{let m=await ta(d,b,S,nt(),void 0,B);if(m.status>=300)return;let v=m.usage;Ne.checkTokens+=(v.inputTokens??te.total)+(v.outputTokens??0);let R=Le(A,m.json),P=Nn(st,R,ni,a.verify.minSimilarity),T=pn.filter(ge=>ge.saved>0).map(ge=>ge.id),q=it(P,T,ua(G,v));P.match||await tt(q,R,T)}))}let Gi=ll(async(d,p)=>{let b=new URL(d.url??"/","http://gateway"),k=Ri(b.pathname);try{if(d.method==="GET"&&(b.pathname==="/healthz"||b.pathname==="/"))return me(p,200,{ok:!0,service:"token-lens-gateway",upstreams:Object.keys(a.upstreams),mode:a.mode});let M=ia(d);if(b.pathname==="/metrics"||b.pathname.startsWith("/token-lens/")){let I=/^\/token-lens\/models/.test(b.pathname)&&d.method!=="GET";if(!M&&!se(d)&&!I)throw new J(401,"Missing or unknown Token Lens key.");return await ze(d,p,b,M)}if(!b.pathname.startsWith("/v1/"))throw new J(404,"Not found. Point your SDK's base URL at this gateway (OpenAI: <gateway>/v1, Anthropic: <gateway>).");if(!M)throw new J(401,"Missing or unknown Token Lens key.");await Xr(d,p,b,M)}catch(M){if(M instanceof J)return $e(p,k,M.status,M.message);if(M.name==="AbortError")return;n(JSON.stringify({ts:new Date().toISOString(),level:"error",path:b.pathname,error:String(M.message??M)})),$e(p,k,502,`Upstream request failed: ${M.message}`)}});return Gi.on("close",()=>{u.stop(),h?.stop()}),{server:Gi,cfg:a,runs:r,metrics:t,counter:o,batches:u,guard:f,storage:h,settle:()=>Promise.all([...c]).then(()=>{})}}var na=kt(),{server:Jr}=$r(na),Fi=Object.entries(na.upstreams);Fi.length||console.warn("token-lens: no upstreams configured. Set OPENAI_API_KEY / ANTHROPIC_API_KEY or TOKEN_LENS_CONFIG.");!na.clientKeys.length&&Fi.some(([,a])=>a.apiKey)&&console.warn("token-lens: no client keys set, so anyone who can reach this port can use the upstream keys. Set TOKEN_LENS_CLIENT_KEYS unless the network is private.");Jr.listen(na.port,na.host,()=>{console.log(`token-lens gateway on http://${na.host}:${na.port} (mode: ${na.mode}; upstreams: ${Fi.map(([a,e])=>`${a}=${e.type}`).join(", ")||"none"})`)});for(let a of["SIGINT","SIGTERM"])process.on(a,()=>{Jr.close(()=>process.exit(0)),setTimeout(()=>process.exit(0),5e3).unref()});
3375
+ `));H.prefixPrint=qi;let Ui=Fr(A,E),es=Gr(A,E),sn=qr(x.toolCalls);if(sn.length){let m=w.get(Ee);m||w.set(Ee,m=new Map);for(let P of sn){let T=m.get(P);T||(m.size>=2e4&&m.delete(m.keys().next().value),m.set(P,T=new Set)),T.size<50&&T.add(Ae)}let v=new Set,R=0;for(let P of sn){let T=m.get(P);if(T.size>1){R++;for(let q of T)q!==Ae&&v.add(q)}}H.sharedReads=R?{files:R,of:sn.length,runs:v.size,agents:[...new Set([...v].map(P=>r.get(P)?.agent??"-"))].slice(0,5)}:void 0}let Hi=m=>a.reportToolArgs?m:`input #${en(m).slice(0,8)}`,Yn=new Map;for(let m of x.toolCalls){let v=`${m.name}\0${m.args}`,R=Yn.get(v)??{name:m.name,args:Hi(m.args),count:0};R.count++,Yn.set(v,R)}let as=[...Yn.values()].filter(m=>m.count>1),Vi,Da=x.toolCalls.at(-1);if(Da){let m=0;for(let v=x.toolCalls.length-1;v>=0&&x.toolCalls[v].name===Da.name&&x.toolCalls[v].args===Da.args;v--)m++;m>1&&(Vi={name:Da.name,args:Hi(Da.args),times:m})}let Te=jn(x.refs,G,o),Xn=Te.parts.system+Te.parts.tools,sa={"x-token-lens-run":Ae,"x-token-lens-profile":G.id},ns=()=>{let m=S.type==="anthropic"?"anthropic":"openai",v=Rn(A,E);if(!v.length)return;let R=Math.max(...v.map(T=>T.turn)),P=T=>Si(m,T)??0;return{count:v.length,tokens:v.reduce((T,q)=>T+P(q),0),oldTokens:v.filter(T=>T.turn<R-1).reduce((T,q)=>T+P(q),0),provider:m}},wa=(m,v,R,P,T={},q=1)=>{let ge={images:ns(),at:new Date().toISOString(),model:x.model,profile:G.id,upstream:y,status:m,parts:R.parts,input:R.total,toolOutputs:R.toolOutputs,usage:v,actions:P,duplicate:Qr,window:G.contextWindow,ms:Date.now()-M,costUsd:T.replayed||m>=400?0:ua(G,{...v,inputTokens:v.inputTokens??R.total})*q,costBeforeUsd:gn(G,{...v,inputTokens:v.inputTokens??R.total},P.reduce((Oe,ca)=>Oe+ca.saved,0)),toolCalls:x.toolCalls.length,repeatedToolCalls:as,sameCallStreak:Vi,api:A,...rn&&Xn>=1024?{cacheBreak:{kind:rn.kind,where:rn.where,tokens:Math.round(rn.lostShare*Xn)}}:{},...Ui?{jsonRetries:Ui,structuredOutput:es}:{},...G.priceTier?{priceTier:G.priceTier}:{},...q!==1?{priceFactor:q}:{},...T};r.record(H,ge),h?.markRun(H);let la=P.reduce((Oe,ca)=>Oe+ca.saved,0);t.record({app:k,agent:H.agent,run:Ae,profile:G,status:m,usage:v,estimatedInput:R.total,savedInput:la,replayed:!!T.replayed,priceFactor:q}),s.record(k,j,{profile:G,status:m,usage:v,estimatedInput:R.total,savedInput:la,replayed:!!T.replayed,priceFactor:q}),a.logRequests&&n(JSON.stringify({ts:ge.at,app:k,run:Ae,model:x.model,profile:G.id,upstream:y,status:m,input:v.inputTokens??R.total,output:v.outputTokens,cached:v.cachedInputTokens,saved:la,actions:P.map(Oe=>Oe.id),replayed:T.replayed||void 0,ms:ge.ms}))},Wi=`${Ee}\0${Kn}`,Ki=a.safe.dedupeRetries&&U==="optimize"&&!x.stream&&x.temperature===0,oa=Ki?l.get(Wi):void 0;if(oa&&Date.now()-oa.at<=a.retryWindowSeconds*1e3){p.writeHead(oa.status,{...oa.headers,...sa,"x-token-lens-replayed":"true"}),wa(oa.status,oa.usage,Te,[],{replayed:!0}),p.end(oa.body);return}let on=Number(Z(d,"x-token-lens-max-calls"))||a.runBudget.maxCalls,ln=Number(Z(d,"x-token-lens-max-cost-usd"))||a.runBudget.maxCostUsd,Qn=H.calls.filter(m=>!m.replayed&&!m.overBudget&&m.status<400),$i=Qn.reduce((m,v)=>m+v.costUsd,0);if(on&&Qn.length>=on||ln&&$i>=ln){let m=`Token Lens stopped this run: it reached its budget (${Qn.length} calls, $${$i.toFixed(4)} spent; limit ${[on?`${on} calls`:"",ln?`$${ln}`:""].filter(Boolean).join(" / ")}). Start a new run or raise the limit.`;p.writeHead(429,{"content-type":"application/json",...sa,"x-token-lens-budget":"exceeded"}),p.end(JSON.stringify(xn(A,429,m))),wa(429,{},Te,[],{overBudget:!0});return}let Sa;try{Sa={customer:si(a.customers,Z(d,"x-token-lens-plan")?.slice(0,64),m=>Z(d,m)),team:oi(a.customers,k)}}catch(m){throw m instanceof Aa?new J(400,m.message):m}if(j||(Sa.customer={}),li(Sa.customer)||li(Sa.team)){let m=s.over(k,j,Sa);if(m){let R=m.limit.endsWith("Usd"),P=ge=>R?`$${ge.toFixed(4)}`:`${Math.round(ge).toLocaleString("en-US")} tokens`,T=m.limit.startsWith("daily")?"daily":"monthly",q=`Token Lens stopped this call: ${m.who==="customer"?`customer "${j}"`:"this team"} reached its ${T} limit (${P(m.spent)} of ${P(m.cap)}). It resets at the start of the next UTC ${T==="daily"?"day":"month"}.`;p.writeHead(429,{"content-type":"application/json",...sa,"x-token-lens-budget":`${m.who}-${T}-${R?"usd":"tokens"}`}),p.end(JSON.stringify(xn(A,429,q))),wa(429,{},Te,[],{overBudget:!0});return}let v=s.remaining(k,j,Sa);v.usd!==void 0&&(sa["x-token-lens-budget-left-usd"]=v.usd.toFixed(6)),v.tokens!==void 0&&(sa["x-token-lens-budget-left-tokens"]=String(Math.floor(v.tokens)))}let Ji=Z(d,"x-token-lens-batch"),cn=Ji==="true"||Ji==="1",Me=f.configFor(a,k,ra,W,!!x.stream||cn),Yi=Me.cfg.optIn.warmCache||(Me.allow.has("warm-cache")?{maxWaitMs:15e3}:!1),un,dn;if(Yi&&U==="optimize"&&!cn&&Xn>=1024&&Me.cfg.safe.promptCaching){let m=[k,y,x.model,$n].join("\0"),v=N.get(m);if(v){let R=Date.now(),P;await Promise.race([v,new Promise(T=>P=setTimeout(T,Yi.maxWaitMs))]),clearTimeout(P),un=Date.now()-R}else if(!F.has(m)){let R;N.set(m,new Promise(P=>R=P)),dn=()=>{N.has(m)&&(N.delete(m),F.add(m),F.size>5e3&&F.delete(F.values().next().value),R())},p.on("close",dn)}}let ee={actions:[],tips:[]},te=Te;if(U==="optimize"){let m={cfg:Me.cfg,body:E,parsed:x,profile:G,upstream:S,counter:o,prefixRepeats:dn?Math.max(Jn,1):Jn,runId:Ae,level:ne,allow:Me.allow,conversationTokens:Te.total-Te.parts.prompt-Te.parts.context};ee=cr(m),ee.actions.length&&(te=jn(x.refs,G,o));let v=ur(m,te);v&&ee.actions.push(v),un!==void 0&&ee.actions.push({id:"warm-cache",saved:0})}let is=Me.cfg.optIn.flex||Me.allow.has("flex"),ts=S.flex??(S.type==="openai"&&/(^|\.)api\.openai\.com$/.test(new URL(S.baseUrl).hostname)),Ye=is&&ts&&A!=="anthropic"&&!cn&&E.service_tier===void 0?{fellBack:!1}:void 0;if(Ye&&(E.service_tier="flex",ee.actions.push({id:"flex",saved:0})),a.blockOverContext&&te.total+(x.maxOutputTokens??0)>G.contextWindow){let m=`This request needs about ${te.total+(x.maxOutputTokens??0)} tokens but ${G.label} has a ${G.contextWindow}-token context window. Rejected by Token Lens before it was sent.`;$e(p,A,400,m),wa(400,{},te,ee.actions);return}if(cn){if(x.stream)return $e(p,A,400,"Requests sent with x-token-lens-batch can't stream.");let m=u.canBatch(A,I,S);if(m)return $e(p,A,400,m);let v=K(d,S);for(let T of["content-length","host","accept-encoding"])delete v[T];let R=[...ee.actions,{id:"batch",saved:0}],P=u.add({app:k,protocol:A,path:I,upstream:y,body:E,onDone:(T,q)=>wa(q?200:502,T,te,R,{},q?a.batch.discount:1)},S,v);p.writeHead(202,{"content-type":"application/json",...sa,"x-token-lens-batch-id":P.id}),p.end(JSON.stringify({id:P.id,object:"token_lens.batch_job",status:P.status,result_url:`/token-lens/batches/${P.id}`}));return}let Xi=Di({...H,calls:[...H.calls,{parts:te.parts,status:200}]}),rs=ee.actions.reduce((m,v)=>m+v.saved,0),be={...sa,"x-token-lens-input-tokens":String(te.total),"x-token-lens-saved-tokens":String(rs)};ee.actions.length&&(be["x-token-lens-actions"]=ee.actions.map(m=>m.id).join(","));let Qi=ee.tips.filter(m=>m.id!=="lighter-model");Qi.length&&(be["x-token-lens-tips"]=Qi.map(m=>m.id).join(",")),Xi&&(be["x-token-lens-growing"]=Xi),un!==void 0&&(be["x-token-lens-warm-wait-ms"]=String(un)),G.priceTier&&te.total>.9*G.priceTier.aboveInputTokens&&(be["x-token-lens-price-tier"]=`${te.total>G.priceTier.aboveInputTokens?"above":"near"} ${G.priceTier.aboveInputTokens}`);let Zi=Me.cfg.optIn.lazyTools||(Me.allow.has("lazy-tools")?{minTools:8}:!1),je=U==="optimize"&&Zi?gr(A,E,Zi.minTools):null,Zn=Me.cfg.optIn.parkToolOutputs||(Me.allow.has("park-tool-outputs")?{minTokens:1500,keepLast:1}:!1),ss=je||Zn?structuredClone(E):void 0,Xe=U==="optimize"&&Zn&&!x.stream&&(A==="openai-chat"||A==="anthropic")&&E.tool_choice!=="none"?Tr(x,Zn,o,G,H.parked??=new Map):[];if(H.parked&&H.parked.size>50)for(let m of[...H.parked.keys()].slice(0,H.parked.size-50))H.parked.delete(m);let et=f.onProbation(k,ra,[...ee.actions.map(m=>m.id),...je?["lazy-tools"]:[],...Xe.length?["park-tool-outputs"]:[]]),ei=et.length?ta(d,b,S,D,void 0,B).catch(()=>{}):void 0,ie;if(je||Xe.length){let m=H.loadedTools??=new Set;for(let ke of x.toolCalls)m.add(ke.name);let v={},R=0,P=0,T,q=!1;for(;P<4&&!T;P++){let ke=je?hr(A,E,je,m):[];Xe.length&&!(E.tools??=[]).some(Qe=>(Qe?.function?.name??Qe?.name)===Ve)&&E.tools.push(Ar(A));let Ce=await ta(d,b,S,Buffer.from(JSON.stringify(E)),Ye,B);if(v=Mi(v,Ce.usage),Ce.status>=300){T=Ce;break}let ii=Rr(A,Ce.json),lt=Xe.length?ii.filter(Qe=>Qe.name===Ve):[],ti=ke.length?fr(A,Ce.json,ke):[];if(!lt.length&&!ti.length){T=Ce;break}R+=Ce.usage.inputTokens??0;for(let Qe of ti)m.add(Qe);if(lt.length){if(ii.some(_a=>_a.name!==Ve&&_a.name!==ka))break;let Qe=new Map(ii.map(_a=>[_a.id,_a.name===Ve?Cr(H.parked,_a.args):`Loaded: ${ti.join(", ")||"none"}. Their full definitions are now in the tool list.`]));Pr(A,E,Ce.json,Qe)}}T||(q=!0,Object.keys(E).forEach(ke=>delete E[ke]),Object.assign(E,ss,je?{tools:je.original}:{}),T=await ta(d,b,S,Buffer.from(JSON.stringify(E)),Ye,B),v=Mi(v,T.usage));let ge=jn(Ci(A,E).refs,G,o),la=te.total-ge.total-R,Oe=je&&!q?Math.max(0,te.parts.tools-ge.parts.tools):0,ca=Xe.length&&!q?Math.max(0,la+R-Oe):0,ot=(ke,Ce)=>Oe+ca?Math.round(la*ke/(Oe+ca)):Ce?Math.round(la/((je?1:0)+(Xe.length?1:0))):0;je&&(Oe||P>1||q)&&ee.actions.push({id:"lazy-tools",saved:ot(Oe,!0),count:P-1}),Xe.length&&ee.actions.push({id:"park-tool-outputs",saved:ot(ca,!0),count:Xe.length});for(let ke of Le(A,T.json).toolCalls)m.add(ke.name);te={...ge,total:ge.total},be["x-token-lens-input-tokens"]=String(te.total),be["x-token-lens-saved-tokens"]=String(ee.actions.reduce((ke,Ce)=>ke+Ce.saved,0)),be["x-token-lens-actions"]=ee.actions.map(ke=>ke.id).join(","),ie={...T,usage:v}}else ei&&(ie=await ta(d,b,S,Buffer.from(JSON.stringify(E)),Ye,B));let mn,ai=!1;if(ie&&ei){let m=await ei,v=et.filter(R=>ee.actions.some(P=>P.id===R));if(m&&m.status<300&&v.length){let R=Le(A,m.json),P=Nn(Le(A,ie.json),R,ee.actions.reduce((T,q)=>T+q.saved,0),a.verify.minSimilarity);ie.status<300&&P.match?(f.confirm(k,ra,v),mn={check:P,ids:v,originalOutput:R,costUsd:ua(G,m.usage),tokens:(m.usage.inputTokens??0)+(m.usage.outputTokens??0)}):(mn={check:{...P,match:!1},ids:v,originalOutput:R,costUsd:ua(G,ie.usage),tokens:(ie.usage.inputTokens??0)+(ie.usage.outputTokens??0)},ie=m,ai=!0,te=Te,be["x-token-lens-input-tokens"]=String(Te.total),be["x-token-lens-saved-tokens"]="0",be["x-token-lens-answer"]="original",delete be["x-token-lens-actions"])}}let pe;if(ie)p.writeHead(ie.status,{...ie.headers,...be}),p.write(ie.buf),pe={status:ie.status,usage:ie.usage,replay:{at:Date.now(),status:ie.status,headers:ie.headers,body:ie.buf,usage:ie.usage}};else{let m=ee.actions.length?Buffer.from(JSON.stringify(E)):D;pe=await Q(d,p,b,S,m,be,Ye,B)}pe.usage.inputTokens&&!je&&!ai&&o.observe(G,te.raw,pe.usage.inputTokens),Ki&&pe.replay&&pe.status<300&&(l.set(Wi,pe.replay),l.size>500&&l.delete(l.keys().next().value));let pn=ai?[]:ee.actions.filter(m=>m.id!=="flex"||!Ye?.fellBack);wa(pe.status,pe.usage,te,pn,{},Ye&&!Ye.fellBack&&pe.status<300?a.flexDiscount:1),dn?.(),p.end();let at=`${k}\0${H.agent}`,Ne=g.get(at)??{checks:0,checkTokens:0,saved:0};g.set(at,Ne);let ni=pn.reduce((m,v)=>m+v.saved,0);Ne.saved+=ni;let nt=()=>{if(!x.stream)return D;let{stream:m,stream_options:v,...R}=JSON.parse(D.toString("utf8"));return Buffer.from(JSON.stringify(R))},it=(m,v,R)=>{let P={...m,actions:v,costUsd:R};return H.checks.push(P),h?.markRun(H),H.checks.length>200&&H.checks.shift(),t.recordCheck(k,H.agent,G,m.match,R),a.logRequests&&n(JSON.stringify({ts:m.at,app:k,run:Ae,outputCheck:m.match?"same":"different",similarity:m.similarity,sameToolCalls:m.sameToolCalls})),P},tt=async(m,v,R)=>{let P=await ta(d,b,S,nt(),void 0,B);if(P.status<300&&(Ne.checkTokens+=(P.usage.inputTokens??Te.total)+(P.usage.outputTokens??0),t.recordCheck(k,H.agent,G,!0,ua(G,P.usage),!1),!Nn(Le(A,P.json),v,0,a.verify.minSimilarity).match)){m.modelVaries=!0;return}let T=f.suspend(k,ra,R,m);h&&T.length&&await h.suspend(f.list().filter(q=>q.app===k&&q.agent===ra&&T.includes(q.fix))),T.length&&n(JSON.stringify({ts:new Date().toISOString(),level:"warn",app:k,agent:ra,switchedOff:T,reason:"an output check found a different answer"}))},rt=m=>{let v=m().catch(R=>n(JSON.stringify({ts:new Date().toISOString(),level:"error",outputCheck:String(R?.message??R)})));c.add(v),v.finally(()=>c.delete(v))};if(mn){let m=mn;Ne.checks++,Ne.checkTokens+=m.tokens;let v=it(m.check,m.ids,m.costUsd);m.check.match||rt(()=>tt(v,m.originalOutput,m.ids));return}let os=pn.some(m=>Ma.has(m.id))&&a.verify.sampleRate>0?Math.max(a.verify.sampleRate,.25):a.verify.sampleRate,ls=Z(d,"x-token-lens-verify")==="1",cs=Ne.checks<a.verify.freeChecks||Ne.checkTokens<Ne.saved*a.verify.maxShareOfSavings,st=x.stream?pe.streamText!==void 0?mr(A,pe.streamText):void 0:pe.replay?Le(A,Wn(pe.replay.body)):void 0;ni>0&&pe.status<300&&st&&(ls||cs&&Math.random()<os)&&(Ne.checks++,rt(async()=>{let m=await ta(d,b,S,nt(),void 0,B);if(m.status>=300)return;let v=m.usage;Ne.checkTokens+=(v.inputTokens??te.total)+(v.outputTokens??0);let R=Le(A,m.json),P=Nn(st,R,ni,a.verify.minSimilarity),T=pn.filter(ge=>ge.saved>0).map(ge=>ge.id),q=it(P,T,ua(G,v));P.match||await tt(q,R,T)}))}let Gi=ll(async(d,p)=>{let b=new URL(d.url??"/","http://gateway"),k=Ri(b.pathname);try{if(d.method==="GET"&&(b.pathname==="/healthz"||b.pathname==="/"))return me(p,200,{ok:!0,service:"token-lens-gateway",upstreams:Object.keys(a.upstreams),mode:a.mode});let M=ia(d);if(b.pathname==="/metrics"||b.pathname.startsWith("/token-lens/")){let I=/^\/token-lens\/models/.test(b.pathname)&&d.method!=="GET";if(!M&&!se(d)&&!I)throw new J(401,"Missing or unknown Token Lens key.");return await ze(d,p,b,M)}if(!b.pathname.startsWith("/v1/"))throw new J(404,"Not found. Point your SDK's base URL at this gateway (OpenAI: <gateway>/v1, Anthropic: <gateway>).");if(!M)throw new J(401,"Missing or unknown Token Lens key.");await Xr(d,p,b,M)}catch(M){if(M instanceof J)return $e(p,k,M.status,M.message);if(M.name==="AbortError")return;n(JSON.stringify({ts:new Date().toISOString(),level:"error",path:b.pathname,error:String(M.message??M)})),$e(p,k,502,`Upstream request failed: ${M.message}`)}});return Gi.on("close",()=>{u.stop(),h?.stop()}),{server:Gi,cfg:a,runs:r,metrics:t,counter:o,batches:u,guard:f,storage:h,settle:()=>Promise.all([...c]).then(()=>{})}}var na=kt(),{server:Jr}=$r(na),Fi=Object.entries(na.upstreams);Fi.length||console.warn("token-lens: no upstreams configured. Set OPENAI_API_KEY / ANTHROPIC_API_KEY or TOKEN_LENS_CONFIG.");!na.clientKeys.length&&Fi.some(([,a])=>a.apiKey)&&console.warn("token-lens: no client keys set, so anyone who can reach this port can use the upstream keys. Set TOKEN_LENS_CLIENT_KEYS unless the network is private.");Jr.listen(na.port,na.host,()=>{console.log(`token-lens gateway on http://${na.host}:${na.port} (mode: ${na.mode}; upstreams: ${Fi.map(([a,e])=>`${a}=${e.type}`).join(", ")||"none"})`)});for(let a of["SIGINT","SIGTERM"])process.on(a,()=>{Jr.close(()=>process.exit(0)),setTimeout(()=>process.exit(0),5e3).unref()});