@kunal_dutt/token-lens 0.2.18 → 0.2.21

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.md CHANGED
@@ -1,9 +1,12 @@
1
1
  # Token Lens gateway
2
2
 
3
3
  A self-hosted gateway that cuts the tokens your AI features, workflows and
4
- agents spend, on any model: OpenAI, Anthropic, Azure OpenAI, or a model you
5
- run yourself behind an OpenAI-compatible server (vLLM, TGI, SGLang, Ollama,
6
- LiteLLM, an in-house serving stack).
4
+ agents spend, on any model: OpenAI, Anthropic, Google Gemini, Mistral, DeepSeek,
5
+ xAI, Qwen, Kimi, Cohere, Perplexity, Azure OpenAI, Groq, Together, Fireworks,
6
+ Cerebras, OpenRouter, or a model you run yourself behind an OpenAI-compatible
7
+ server (vLLM, TGI, SGLang, Ollama, LM Studio, LiteLLM, an in-house serving stack).
8
+ It also keeps savings, spend and limits per end customer for businesses that
9
+ serve their own customers through it ([Your customers](#your-customers-b2b2c)).
7
10
 
8
11
  It speaks the OpenAI and Anthropic APIs, so nothing in your app changes except
9
12
  its **base URL**. It runs inside your own network; prompts never leave it except
@@ -77,6 +80,39 @@ and keep calling the endpoint your SDK uses.
77
80
  - **Keys:** if callers bring their own key, it is sent the way the other API
78
81
  expects (`Authorization: Bearer` becomes `x-api-key`, and the reverse).
79
82
 
83
+ ### Any provider, from its usual environment variable
84
+
85
+ Set the key a provider's own SDK reads and the gateway sets that provider up
86
+ through its OpenAI-compatible API, and sends its model names to it. No config
87
+ file needed; a provider defined in the config file wins over its variable.
88
+
89
+ | Variable | Provider | Model names routed to it |
90
+ | --- | --- | --- |
91
+ | `OPENAI_API_KEY` (`OPENAI_BASE_URL`) | OpenAI | `gpt-*`, `chatgpt-*`, `o1*`, `o3*`, `o4*`, and anything no route matches |
92
+ | `ANTHROPIC_API_KEY` (`ANTHROPIC_BASE_URL`) | Anthropic | `claude-*` (also from the OpenAI SDK, translated) |
93
+ | `GEMINI_API_KEY` or `GOOGLE_API_KEY` | Google Gemini API | `gemini-*`, `gemma-*` |
94
+ | `MISTRAL_API_KEY` | Mistral | `mistral-*`, `magistral-*`, `codestral-*`, `devstral-*`, `ministral-*`, `pixtral-*`, `open-mistral-*`, `open-mixtral-*` |
95
+ | `DEEPSEEK_API_KEY` | DeepSeek | `deepseek-*` |
96
+ | `XAI_API_KEY` | xAI | `grok-*` |
97
+ | `MOONSHOT_API_KEY` | Moonshot | `kimi-*`, `moonshot-*` |
98
+ | `DASHSCOPE_API_KEY` | Alibaba Cloud (Qwen) | `qwen*`, `qwq-*` |
99
+ | `COHERE_API_KEY` | Cohere | `command-*` |
100
+ | `PERPLEXITY_API_KEY` | Perplexity | `sonar*` |
101
+ | `OPENROUTER_API_KEY` | OpenRouter | `vendor/model` names (`*/*`) |
102
+ | `GROQ_API_KEY`, `TOGETHER_API_KEY`, `FIREWORKS_API_KEY`, `CEREBRAS_API_KEY` | Groq, Together, Fireworks, Cerebras | none: they host many vendors' models under overlapping names |
103
+ | `AZURE_OPENAI_ENDPOINT` + `AZURE_OPENAI_API_KEY` | Azure OpenAI (v1 API) | none |
104
+ | `OLLAMA_BASE_URL`, `LMSTUDIO_BASE_URL` | Local models (`http://localhost:11434`, `http://localhost:1234`) | none |
105
+
106
+ For a provider without an automatic route, either make it the default with
107
+ `TOKEN_LENS_DEFAULT_UPSTREAM=groq` (the upstream's name is the first word of its
108
+ row, lowercase) or add routes in the config file. Bedrock and Vertex AI, which
109
+ need signed requests, can sit behind LiteLLM as an OpenAI-compatible upstream.
110
+
111
+ Each model is counted and priced with its own profile: the families above have
112
+ built-in ones (open-weight families such as Llama, Qwen, Kimi, GLM, Gemma and Phi
113
+ at a typical host's price, since every host charges differently); give exact
114
+ numbers for your host or your own model under `models` (below).
115
+
80
116
  ## Your own model
81
117
 
82
118
  A company model isn't in anyone's built-in list, so register it: its context
@@ -437,6 +473,75 @@ reasoning that outweighs the answer, and runs nearing the context limit.
437
473
  - `POST /token-lens/analyze` with `{"prompt", "model"}` returns the Token Lens
438
474
  analysis of one prompt without calling a model.
439
475
 
476
+ ## Your customers (B2B2C)
477
+
478
+ For a business that runs AI features for its own customers: each call can name
479
+ the customer it is for, and the gateway keeps savings and spend per customer,
480
+ stops a customer at its limits, and keeps customers apart in its caches. It's the
481
+ business's own gateway, so this data stays on its servers like everything else.
482
+
483
+ **Name the customer** with a header, from your backend (customers never hold a
484
+ Token Lens key):
485
+
486
+ ```python
487
+ client.chat.completions.create(model="gpt-5.4", messages=msgs,
488
+ extra_headers={"x-token-lens-customer": "cus_123", "x-token-lens-plan": "free"})
489
+ ```
490
+
491
+ To use the API's own end-user field instead when the header is absent (OpenAI's
492
+ `safety_identifier` or `user`, Anthropic's `metadata.user_id`), set
493
+ `"customers": { "fromBody": true }`; it's off by default, so nothing from a
494
+ request body appears in reports unless you ask for it. Ids are kept as given, up to 128
495
+ characters; with `"hashIds": true` only a short hash is kept, and lookups hash the
496
+ id you ask about, so you can still query your own ids.
497
+
498
+ **Limits** (off unless set). Days and months are UTC. The call that crosses a
499
+ limit completes; the next one gets a 429 in your SDK's error shape, with
500
+ `x-token-lens-budget: customer-daily-usd` (or `-monthly-`, `-tokens`, or `team-…`).
501
+ Responses carry `x-token-lens-budget-left-usd` / `-tokens` while a limit applies.
502
+
503
+ ```json
504
+ "customers": {
505
+ "budget": { "monthlyUsd": 20 },
506
+ "plans": {
507
+ "free": { "dailyUsd": 0.05, "monthlyTokens": 2000000 },
508
+ "pro": { "monthlyUsd": 50 }
509
+ },
510
+ "teamBudgets": { "acme-app": { "monthlyUsd": 5000 }, "*": { "monthlyUsd": 500 } }
511
+ }
512
+ ```
513
+
514
+ - `budget` applies to every customer; `x-token-lens-plan: free` adds that plan's
515
+ limits (an unknown plan is refused with a 400, so a typo never drops a limit).
516
+ - A call can set its own customer limit: `x-token-lens-customer-daily-usd`,
517
+ `-monthly-usd`, `-daily-tokens`, `-monthly-tokens`.
518
+ - `teamBudgets` caps a whole team (client key), all its customers together; `*`
519
+ is every team without its own entry. Useful on its own for B2B: one key per
520
+ team or client business, each with its monthly budget.
521
+ - Only answered calls count: errors, stopped calls and replayed retries don't.
522
+ - With [shared storage](#several-gateway-copies-and-restarts-shared-storage) on,
523
+ every copy sees the others' spend within `storage.flushSeconds`.
524
+
525
+ **Reports**, for a team key (its own customers) or the admin key (`?team=`):
526
+
527
+ | Endpoint | What |
528
+ | --- | --- |
529
+ | `GET /token-lens/customers` | Every customer: tokens and money before and after, saved, spent today and this month, last seen. `.csv` for a spreadsheet. |
530
+ | `GET /token-lens/customers/<id>` | One customer, with its limits and what is left (`?plan=free` for a plan's limits), and a `summary` line to show that customer, e.g. "Token Lens saved 48,210 tokens (31%) and $0.42 (31%) on 120 calls." |
531
+
532
+ The HTML report gets a "By customer" table.
533
+
534
+ **Kept apart:** a retry is answered from an earlier identical call only for the
535
+ same customer, embeddings are cached per customer, and runs and the files their
536
+ tools read are grouped per customer. The model and the request are never changed
537
+ per customer.
538
+
539
+ **Several businesses on one gateway:** set `"isolateTeams": true` (or
540
+ `TOKEN_LENS_ISOLATE_TEAMS=true`) and each client key sees only its own team's
541
+ report, CSV, stats, runs, customers and `/metrics`; the admin key sees all.
542
+
543
+ In the [library](SDK.md#your-customers), the same headers work in-process.
544
+
440
545
  ## Security
441
546
 
442
547
  - Set client keys (`TOKEN_LENS_CLIENT_KEYS="name:key,…"` or `clientKeys` in the
@@ -451,7 +556,8 @@ reasoning that outweighs the answer, and runs nearing the context limit.
451
556
  inputs, which can hold customer data, show only as a short hash (for example
452
557
  `get_order(input #3f2a9c1e)`) unless you set `"reportToolArgs": true`.
453
558
  - `/token-lens/*` and `/metrics` need a client key (when keys are set); model
454
- registration needs the admin key.
559
+ registration needs the admin key. Customer reports only ever show the caller's
560
+ own team's customers; with `isolateTeams`, every report does.
455
561
 
456
562
  ## Privacy: nothing leaves your systems
457
563
 
@@ -468,8 +574,10 @@ server or Token Lens service.
468
574
  and run names never reach it.
469
575
  - **Output checks:** these re-send a sample of requests to the same upstream your
470
576
  app already uses. Set `"verify": { "sampleRate": 0 }` to turn them off.
577
+ - **Customers:** customer ids (or their hash, with `hashIds`) and their totals
578
+ stay on your gateway or your own storage, like every other report.
471
579
  - **Embeddings and file reads:** cached embeddings are kept as a hash of the text
472
- and its vector, in memory, per team. Files several agents read are matched by a
580
+ and its vector, in memory, per team and customer. Files several agents read are matched by a
473
581
  hash of the path; reports show only how many and which agents.
474
582
  - **Test coverage:** a test checks all of this on every change. It records every
475
583
  outbound request and asserts that customer data never appears in reports or
package/README.md CHANGED
@@ -4,6 +4,11 @@ Count and cut the tokens your prompts, chats and agents use, adapted to the
4
4
  model and the task, without switching your model or changing the answer. Every
5
5
  saving is shown in tokens and dollars, priced for the model you use.
6
6
 
7
+ Works with any LLM: GPT, Claude, Gemini and Gemma, DeepSeek, Mistral, Grok,
8
+ Llama, Qwen, Kimi, GLM, Cohere Command, Perplexity Sonar, Phi, and your own or
9
+ local models. For people (in their AI app), companies (gateway or library, per
10
+ team) and companies serving their own customers (savings and limits per customer).
11
+
7
12
  **Collects no data.** No accounts, analytics or telemetry. It makes no network
8
13
  requests of its own, except an exact token count from Anthropic or Google when
9
14
  you set your own `ANTHROPIC_API_KEY` or `GEMINI_API_KEY` for it. See [PRIVACY.md](PRIVACY.md).
@@ -17,7 +22,9 @@ each start picks up the newest version, so there's nothing to update.
17
22
  | --- | --- |
18
23
  | **Claude Code** | `claude mcp add token-lens -- npx -y @kunal_dutt/token-lens@latest` |
19
24
  | **Claude Desktop** | Add the JSON below to `claude_desktop_config.json` (keeps itself up to date), or for a one-click install without Node, download [token-lens.mcpb](https://unpkg.com/@kunal_dutt/token-lens@latest/token-lens.mcpb) and open it. |
20
- | **Cursor, VS Code, Windsurf, Zed, any MCP app** | Add a stdio server with the JSON below. |
25
+ | **Codex CLI** | `codex mcp add token-lens -- npx -y @kunal_dutt/token-lens@latest` |
26
+ | **Gemini CLI** | Add the JSON below to `~/.gemini/settings.json`. |
27
+ | **Cursor, VS Code, Windsurf, Zed, Cline, LM Studio, any MCP app** | Add a stdio server with the JSON below. |
21
28
  | **ChatGPT, claude.ai and mobile apps** | Need a connector URL: use the public one from the Token Lens listing, or run your own (below). |
22
29
 
23
30
  ```json
@@ -31,8 +38,9 @@ each start picks up the newest version, so there's nothing to update.
31
38
  Then ask, for example: "optimize this prompt with Token Lens", "how full is this
32
39
  chat?" or "optimize this agent's system prompt and tools".
33
40
 
34
- Tools: `optimize_prompt`, `optimize_agent`, `count_tokens`,
35
- `check_conversation`, `list_model_profiles`.
41
+ Tools: `optimize_prompt`, `optimize_agent`, `audit_agent_rules`, `count_tokens`,
42
+ `check_conversation`, `list_model_profiles`. Each takes the model's name, so it
43
+ adapts to whichever model you use.
36
44
 
37
45
  ## Run your own connector
38
46
 
@@ -64,7 +72,10 @@ read as a system prompt. It runs inside your CI and sends nothing anywhere.
64
72
 
65
73
  A self-hosted proxy that speaks the OpenAI and Anthropic APIs and optimizes
66
74
  every call your apps, workflows and agents make, with any model, including your
67
- own. It talks only to the model endpoints you configure. See
75
+ own. Most providers need only their usual key variable (`GEMINI_API_KEY`,
76
+ `MISTRAL_API_KEY`, `DEEPSEEK_API_KEY`, `OLLAMA_BASE_URL`…). Businesses serving
77
+ their own customers get savings, spend and daily or monthly limits per customer
78
+ and plan. It talks only to the model endpoints you configure. See
68
79
  [GATEWAY.md](GATEWAY.md).
69
80
 
70
81
  ```bash
package/check-agent.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
2
- import{execFileSync as zi}from"node:child_process";import{existsSync as Pi,readFileSync as Ii,writeFileSync as Mi}from"node:fs";import{join as _a}from"node:path";var E="<|endoftext|>",j="<|fim_prefix|>",x="<|fim_middle|>",D="<|fim_suffix|>",F="<|im_start|>",G="<|im_end|>",N="<|im_sep|>",L="<|endofprompt|>",he="<|startoftext|>",$="<|start|>",Y="<|end|>",X="<|message|>",Q="<|channel|>",Z="<|return|>",ee="<|constrain|>",ae="<|call|>";var je=e=>e<=127;function be(e){if(e.length===0)return!1;let a=e.charCodeAt(e.length-1);return a>=55296&&a<=56319}function Ta(e){let a=0;for(;a<e.length;){let i=e[a],n=0,r=0;if(i<=127)n=1,r=i;else if((i&224)===192){if(n=2,r=i&31,i<=193)return!1}else if((i&240)===224)n=3,r=i&15;else if((i&248)===240){if(n=4,r=i&7,i>244)return!1}else return!1;if(a+n>e.length)return!1;for(let t=1;t<n;t++){let o=e[a+t];if(o===void 0||(o&192)!==128)return!1;r=r<<6|o&63}if(n===2&&r<128||n===3&&r<2048||n===4&&r<65536||r>=55296&&r<=57343||r>1114111)return!1;a+=n}return!0}var Ra=new TextDecoder("utf8",{fatal:!1});function xe(e){if(Ta(e))return Ra.decode(e)}function De(e,a){let i=Math.min(e.length,a.length);for(let n=0;n<i;n++)if(e[n]!==a[n])return e[n]-a[n];return e.length-a.length}function Ne(e){let a=0;return e.forEach(i=>{a=Math.max(a,i)}),a}function ke(e){return e.replace(/[$()*+.?[\\\]^{|}]/g,"\\$&")}function fe(e){let a=[...e].map(ke).join("|");return new RegExp(`(${a})`)}var se=new Uint8Array(0),U=new TextDecoder("utf8"),Oe=class{mergeableBytePairRankCount;bytePairRankDecoder;bytePairNonUtfRankDecoder=new Map;bytePairNonUtfSortedEncoder;bytePairStringRankEncoder;tokenSplitRegex;specialTokensEncoder;specialTokensDecoder;specialTokenPatternRegex;textEncoder=new TextEncoder;mergeCache;mergeCacheSize;constructor({bytePairRankDecoder:e,specialTokensEncoder:a,tokenSplitRegex:i,mergeCacheSize:n=1e5}){this.bytePairRankDecoder=e,this.bytePairStringRankEncoder=new Map,this.mergeCacheSize=n,n>0&&(this.mergeCache=new Map),this.mergeableBytePairRankCount=Object.keys(e).length;let r=[];e.forEach((o,s)=>{if(typeof o=="string"){this.bytePairStringRankEncoder.set(o,s);return}let l=new Uint8Array(o);r.push([l,s]),this.bytePairNonUtfRankDecoder.set(s,l)}),this.bytePairNonUtfSortedEncoder=r.sort((o,s)=>De(o[0],s[0])),this.specialTokensEncoder=a??new Map,this.specialTokensDecoder=a?new Map([...a].map(([o,s])=>[s,o])):new Map,this.tokenSplitRegex=i;let t=[...this.specialTokensEncoder.keys()].map(ke).join("|");try{this.specialTokenPatternRegex=new RegExp(t,"y")}catch{throw new Error("Invalid regular expression pattern.")}}setMergeCacheSize(e){this.mergeCacheSize===0&&e>0&&(this.mergeCache=new Map),this.mergeCacheSize=e,e===0&&(this.mergeCache=void 0)}clearMergeCache(){this.mergeCache?.clear()}*encodeNativeGenerator(e,a){let i=0,n=0;for(;;){let r=this.findNextSpecialToken(e,a,i),t=r?.[0],o=t??e.length,s=i===0&&o===e.length?e:e.slice(i,o);for(let[l]of s.matchAll(this.tokenSplitRegex)){let c=this.getBpeRankFromString(l);if(c!==void 0){n=1,yield[c];continue}let d=this.bytePairEncode(l);n=d.length,yield d}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],i=t+l.length,n=1}else break}return n}encodeNative(e,a){let i=0,n=[];for(;;){let r=this.findNextSpecialToken(e,a,i),t=r?.[0],o=t??e.length,s=i===0&&o===e.length?e:e.slice(i,o);for(let[l]of s.matchAll(this.tokenSplitRegex)){let c=this.getBpeRankFromString(l);if(c!==void 0){n.push(c);continue}let d=this.bytePairEncode(l);n.push(...d)}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.`);n.push(c),i=t+l.length}else break}return n}countNative(e,a){let i=0,n=0;for(;;){let r=this.findNextSpecialToken(e,a,i),t=r?.[0],o=t??e.length,s=i===0&&o===e.length?e:e.slice(i,o);for(let[l]of s.matchAll(this.tokenSplitRegex)){if(this.getBpeRankFromString(l)!==void 0){n++;continue}let c=this.bytePairEncode(l);n+=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.`);n++,i=t+l.length}else break}return n}*decodeNativeGenerator(e){for(let a of e){let i=this.tryDecodeToken(a);i&&(yield i)}}decodeNative(e){let a="",i=se;for(let n of e){let r=this.tryDecodeToken(n);if(r===void 0)throw new Error(`Token ${n} is not in the byte pair encoder.`);if(typeof r=="string")i!==se&&(a+=U.decode(i,{stream:!0}),i=se),a+=r;else{let t=new Uint8Array(i.length+r.length);t.set(i),t.set(r,i.length),i=t}}return i!==se&&(a+=U.decode(i,{stream:!0})),a}async*decodeNativeAsyncIterable(e){for await(let a of e){let i=this.tryDecodeToken(a);i&&(yield i)}}getBpeRankFromString(e){return this.bytePairStringRankEncoder.get(e)}getBpeRankFromStringOrThrow(e){let a=this.getBpeRankFromString(e);if(a===void 0)throw new Error(`The byte-pair encoding does not contain a value for: ${e}`);return a}getBpeRankFromBytes(e){let a=xe(e);if(a!==void 0)return this.getBpeRankFromString(a);let i=this.binarySearch(e);if(i!==-1)return this.bytePairNonUtfSortedEncoder[i][1]}getBpeRankFromBytesOrThrow(e){let a=this.getBpeRankFromBytes(e);if(a===void 0)throw new Error(`The byte-pair encoding does not contain a value for: ${e.toString()}`);return a}binarySearch(e){let a=0,i=this.bytePairNonUtfSortedEncoder.length-1;for(;a<=i;){let n=a+i>>>1,r=this.bytePairNonUtfSortedEncoder[n][0],t=0,o=Math.min(r.length,e.length);for(let s=0;s<o&&(t=r[s]-e[s],t===0);s++);if(t===0&&(t=r.length-e.length),t===0)return n;t<0?a=n+1:i=n-1}return-1}findNextSpecialToken(e,a,i){let n=i;for(;;){this.specialTokenPatternRegex.lastIndex=n;let r=this.specialTokenPatternRegex.exec(e);if(!r)return;let t=r[0];if(a?.has(t))return[r.index+n,t];n=r.index+n+1}}tryDecodeToken(e){let a=this.bytePairRankDecoder[e];if(typeof a=="string")return a;if(typeof a=="object"){let i=this.bytePairNonUtfRankDecoder.get(e);if(i)return i}return this.specialTokensDecoder.get(e)}addToMergeCache(e,a){if(this.mergeCache){if(this.mergeCache.size>=this.mergeCacheSize){let i=this.mergeCache.keys().next().value;this.mergeCache.delete(i)}this.mergeCache.set(e,a)}}bytePairEncode(e){if(e.length===1&&je(e.codePointAt(0)))return[this.getBpeRankFromStringOrThrow(e)];if(this.mergeCache?.has(e)){let n=this.mergeCache.get(e);return this.mergeCache.delete(e),this.mergeCache.set(e,n),n}let a=this.textEncoder.encode(e),i=this.bytePairMerge(a);return this.addToMergeCache(e,i),i}bytePairMerge(e){let a=[],i=[],n=(t,o=a[t],s=a[t+2])=>{if(s===void 0)return Number.POSITIVE_INFINITY;let l=e.subarray(o,s);return this.getBpeRankFromBytes(l)??Number.POSITIVE_INFINITY};for(let t=0;t<=e.length;t++)a.push(t),t<e.length-1?i.push(n(t,t,t+2)):i.push(Number.POSITIVE_INFINITY);for(;a.length>1;){let t=Number.POSITIVE_INFINITY,o=-1;for(let s=0;s<i.length-1;s++){let l=i[s];l<t&&(t=l,o=s)}if(t===Number.POSITIVE_INFINITY||o===-1)break;a.splice(o+1,1),i.splice(o,1),i[o]=n(o),o>0&&(i[o-1]=n(o-1))}let r=[];for(let t=0;t<a.length-1;t++){let o=a[t],s=a[t+1],l=this.getBpeRankFromBytesOrThrow(e.subarray(o,s));r.push(l)}return r}};function za(e,a){let i=0;if(e.role&&(i+=a(e.role)),e.content&&(i+=a(e.content)),e.name&&(i+=a(e.name)+1),e.function_call){let{name:n,arguments:r}=e.function_call;n&&(i+=a(n)),r&&(i+=a(r)),i+=3}return i+=3,e.role==="function"&&(i-=2),i}function Be(e,a,i){if(!e.properties)return"";let n=[],r=new Set(e.required??[]),t=" ".repeat(a);for(let[o,s]of Object.entries(e.properties)){s.description&&a<2&&n.push(`${t}// ${s.description}`);let l=r.has(o),c=i(s,a);n.push(`${t}${o}${l?"":"?"}: ${c},`)}return n.join(`
2
+ import{execFileSync as zi}from"node:child_process";import{existsSync as Pi,readFileSync as Ii,writeFileSync as Mi}from"node:fs";import{join as _a}from"node:path";var A="<|endoftext|>",j="<|fim_prefix|>",x="<|fim_middle|>",D="<|fim_suffix|>",F="<|im_start|>",G="<|im_end|>",N="<|im_sep|>",L="<|endofprompt|>",he="<|startoftext|>",$="<|start|>",Y="<|end|>",X="<|message|>",Q="<|channel|>",Z="<|return|>",ee="<|constrain|>",ae="<|call|>";var je=e=>e<=127;function be(e){if(e.length===0)return!1;let a=e.charCodeAt(e.length-1);return a>=55296&&a<=56319}function Ta(e){let a=0;for(;a<e.length;){let i=e[a],n=0,r=0;if(i<=127)n=1,r=i;else if((i&224)===192){if(n=2,r=i&31,i<=193)return!1}else if((i&240)===224)n=3,r=i&15;else if((i&248)===240){if(n=4,r=i&7,i>244)return!1}else return!1;if(a+n>e.length)return!1;for(let t=1;t<n;t++){let o=e[a+t];if(o===void 0||(o&192)!==128)return!1;r=r<<6|o&63}if(n===2&&r<128||n===3&&r<2048||n===4&&r<65536||r>=55296&&r<=57343||r>1114111)return!1;a+=n}return!0}var Ra=new TextDecoder("utf8",{fatal:!1});function xe(e){if(Ta(e))return Ra.decode(e)}function De(e,a){let i=Math.min(e.length,a.length);for(let n=0;n<i;n++)if(e[n]!==a[n])return e[n]-a[n];return e.length-a.length}function Ne(e){let a=0;return e.forEach(i=>{a=Math.max(a,i)}),a}function ke(e){return e.replace(/[$()*+.?[\\\]^{|}]/g,"\\$&")}function fe(e){let a=[...e].map(ke).join("|");return new RegExp(`(${a})`)}var se=new Uint8Array(0),U=new TextDecoder("utf8"),Oe=class{mergeableBytePairRankCount;bytePairRankDecoder;bytePairNonUtfRankDecoder=new Map;bytePairNonUtfSortedEncoder;bytePairStringRankEncoder;tokenSplitRegex;specialTokensEncoder;specialTokensDecoder;specialTokenPatternRegex;textEncoder=new TextEncoder;mergeCache;mergeCacheSize;constructor({bytePairRankDecoder:e,specialTokensEncoder:a,tokenSplitRegex:i,mergeCacheSize:n=1e5}){this.bytePairRankDecoder=e,this.bytePairStringRankEncoder=new Map,this.mergeCacheSize=n,n>0&&(this.mergeCache=new Map),this.mergeableBytePairRankCount=Object.keys(e).length;let r=[];e.forEach((o,s)=>{if(typeof o=="string"){this.bytePairStringRankEncoder.set(o,s);return}let l=new Uint8Array(o);r.push([l,s]),this.bytePairNonUtfRankDecoder.set(s,l)}),this.bytePairNonUtfSortedEncoder=r.sort((o,s)=>De(o[0],s[0])),this.specialTokensEncoder=a??new Map,this.specialTokensDecoder=a?new Map([...a].map(([o,s])=>[s,o])):new Map,this.tokenSplitRegex=i;let t=[...this.specialTokensEncoder.keys()].map(ke).join("|");try{this.specialTokenPatternRegex=new RegExp(t,"y")}catch{throw new Error("Invalid regular expression pattern.")}}setMergeCacheSize(e){this.mergeCacheSize===0&&e>0&&(this.mergeCache=new Map),this.mergeCacheSize=e,e===0&&(this.mergeCache=void 0)}clearMergeCache(){this.mergeCache?.clear()}*encodeNativeGenerator(e,a){let i=0,n=0;for(;;){let r=this.findNextSpecialToken(e,a,i),t=r?.[0],o=t??e.length,s=i===0&&o===e.length?e:e.slice(i,o);for(let[l]of s.matchAll(this.tokenSplitRegex)){let c=this.getBpeRankFromString(l);if(c!==void 0){n=1,yield[c];continue}let d=this.bytePairEncode(l);n=d.length,yield d}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],i=t+l.length,n=1}else break}return n}encodeNative(e,a){let i=0,n=[];for(;;){let r=this.findNextSpecialToken(e,a,i),t=r?.[0],o=t??e.length,s=i===0&&o===e.length?e:e.slice(i,o);for(let[l]of s.matchAll(this.tokenSplitRegex)){let c=this.getBpeRankFromString(l);if(c!==void 0){n.push(c);continue}let d=this.bytePairEncode(l);n.push(...d)}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.`);n.push(c),i=t+l.length}else break}return n}countNative(e,a){let i=0,n=0;for(;;){let r=this.findNextSpecialToken(e,a,i),t=r?.[0],o=t??e.length,s=i===0&&o===e.length?e:e.slice(i,o);for(let[l]of s.matchAll(this.tokenSplitRegex)){if(this.getBpeRankFromString(l)!==void 0){n++;continue}let c=this.bytePairEncode(l);n+=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.`);n++,i=t+l.length}else break}return n}*decodeNativeGenerator(e){for(let a of e){let i=this.tryDecodeToken(a);i&&(yield i)}}decodeNative(e){let a="",i=se;for(let n of e){let r=this.tryDecodeToken(n);if(r===void 0)throw new Error(`Token ${n} is not in the byte pair encoder.`);if(typeof r=="string")i!==se&&(a+=U.decode(i,{stream:!0}),i=se),a+=r;else{let t=new Uint8Array(i.length+r.length);t.set(i),t.set(r,i.length),i=t}}return i!==se&&(a+=U.decode(i,{stream:!0})),a}async*decodeNativeAsyncIterable(e){for await(let a of e){let i=this.tryDecodeToken(a);i&&(yield i)}}getBpeRankFromString(e){return this.bytePairStringRankEncoder.get(e)}getBpeRankFromStringOrThrow(e){let a=this.getBpeRankFromString(e);if(a===void 0)throw new Error(`The byte-pair encoding does not contain a value for: ${e}`);return a}getBpeRankFromBytes(e){let a=xe(e);if(a!==void 0)return this.getBpeRankFromString(a);let i=this.binarySearch(e);if(i!==-1)return this.bytePairNonUtfSortedEncoder[i][1]}getBpeRankFromBytesOrThrow(e){let a=this.getBpeRankFromBytes(e);if(a===void 0)throw new Error(`The byte-pair encoding does not contain a value for: ${e.toString()}`);return a}binarySearch(e){let a=0,i=this.bytePairNonUtfSortedEncoder.length-1;for(;a<=i;){let n=a+i>>>1,r=this.bytePairNonUtfSortedEncoder[n][0],t=0,o=Math.min(r.length,e.length);for(let s=0;s<o&&(t=r[s]-e[s],t===0);s++);if(t===0&&(t=r.length-e.length),t===0)return n;t<0?a=n+1:i=n-1}return-1}findNextSpecialToken(e,a,i){let n=i;for(;;){this.specialTokenPatternRegex.lastIndex=n;let r=this.specialTokenPatternRegex.exec(e);if(!r)return;let t=r[0];if(a?.has(t))return[r.index+n,t];n=r.index+n+1}}tryDecodeToken(e){let a=this.bytePairRankDecoder[e];if(typeof a=="string")return a;if(typeof a=="object"){let i=this.bytePairNonUtfRankDecoder.get(e);if(i)return i}return this.specialTokensDecoder.get(e)}addToMergeCache(e,a){if(this.mergeCache){if(this.mergeCache.size>=this.mergeCacheSize){let i=this.mergeCache.keys().next().value;this.mergeCache.delete(i)}this.mergeCache.set(e,a)}}bytePairEncode(e){if(e.length===1&&je(e.codePointAt(0)))return[this.getBpeRankFromStringOrThrow(e)];if(this.mergeCache?.has(e)){let n=this.mergeCache.get(e);return this.mergeCache.delete(e),this.mergeCache.set(e,n),n}let a=this.textEncoder.encode(e),i=this.bytePairMerge(a);return this.addToMergeCache(e,i),i}bytePairMerge(e){let a=[],i=[],n=(t,o=a[t],s=a[t+2])=>{if(s===void 0)return Number.POSITIVE_INFINITY;let l=e.subarray(o,s);return this.getBpeRankFromBytes(l)??Number.POSITIVE_INFINITY};for(let t=0;t<=e.length;t++)a.push(t),t<e.length-1?i.push(n(t,t,t+2)):i.push(Number.POSITIVE_INFINITY);for(;a.length>1;){let t=Number.POSITIVE_INFINITY,o=-1;for(let s=0;s<i.length-1;s++){let l=i[s];l<t&&(t=l,o=s)}if(t===Number.POSITIVE_INFINITY||o===-1)break;a.splice(o+1,1),i.splice(o,1),i[o]=n(o),o>0&&(i[o-1]=n(o-1))}let r=[];for(let t=0;t<a.length-1;t++){let o=a[t],s=a[t+1],l=this.getBpeRankFromBytesOrThrow(e.subarray(o,s));r.push(l)}return r}};function za(e,a){let i=0;if(e.role&&(i+=a(e.role)),e.content&&(i+=a(e.content)),e.name&&(i+=a(e.name)+1),e.function_call){let{name:n,arguments:r}=e.function_call;n&&(i+=a(n)),r&&(i+=a(r)),i+=3}return i+=3,e.role==="function"&&(i-=2),i}function Be(e,a,i){if(!e.properties)return"";let n=[],r=new Set(e.required??[]),t=" ".repeat(a);for(let[o,s]of Object.entries(e.properties)){s.description&&a<2&&n.push(`${t}// ${s.description}`);let l=r.has(o),c=i(s,a);n.push(`${t}${o}${l?"":"?"}: ${c},`)}return n.join(`
3
3
  `)}function ve(e,a){switch(e.type){case"string":return e.enum?.map(i=>JSON.stringify(i)).join(" | ")??"string";case"integer":case"number":return e.enum?.map(i=>`${i}`).join(" | ")??"number";case"boolean":return"boolean";case"null":return"null";case"array":return e.items?`${ve(e.items,a)}[]`:"any[]";case"object":return`{
4
4
  ${Be(e,a+2,ve)}
5
5
  ${" ".repeat(a)}}`;default:return"any"}}function Pa(e){let a=["namespace functions {",""];for(let i of e){i.description&&a.push(`// ${i.description}`);let{parameters:n}=i,r=n?.properties;if(!n||!r||Object.keys(r).length===0)a.push(`type ${i.name} = () => any;`);else{a.push(`type ${i.name} = (_: {`);let t=Be(n,0,ve);t.length>0&&a.push(t),a.push("}) => any;")}a.push("")}return a.push("} // namespace functions"),a.join(`
@@ -7,7 +7,7 @@ ${" ".repeat(a)}}`;default:return"any"}}function Pa(e){let a=["namespace functio
7
7
  `)?e:{...e,content:`${e.content}
8
8
  `}}function Fe(e,a){let{messages:i,functions:n,function_call:r}=e,t=!!(n&&n.length>0),o=!1,s=0;for(let l of i){let c=Ma(l,t,o);(c!==l&&l.role==="system"||l.role==="system"&&t&&!o)&&(o=!0),s+=za(c,a)}return s+=3,t&&n&&(s+=Ia(n,a),i.some(l=>l.role==="system")&&(s-=4)),r&&r!=="auto"&&(r==="none"?s+=1:typeof r=="object"&&r.name&&(s+=a(r.name)+4)),s}var Ge=["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 qe=Object.defineProperty,He=(e,a)=>{let i={};for(var n in e)qe(i,n,{get:e[n],enumerable:!0});return a||qe(i,Symbol.toStringTag,{value:"Module"}),i};var Ue=He({cl100k_base:()=>La,gpt2:()=>ja,o200k_base:()=>Oa,o200k_harmony:()=>Ba,p50k_base:()=>xa,p50k_edit:()=>Na,r50k_base:()=>Da}),ja=["gpt2","gpt-2"],xa=["text-davinci-002","text-davinci-003","code-davinci-001","code-davinci-002","davinci-codex","code-cushman-001","code-cushman-002","cushman-codex"],Da=["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"],Na=["code-davinci-edit-001","text-davinci-edit-001"],La=["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"],Oa=[],Ba=["gpt-oss-20b","gpt-oss-120b"];var Ve=Object.fromEntries(Object.entries(Ue).flatMap(([e,a])=>a.map(i=>[i,e]))),Fa={messageSeparator:`
9
9
  `,roleSeparator:`
10
- `},Ga={messageSeparator:"",roleSeparator:N},We=Object.fromEntries(Ge.flatMap(e=>e.startsWith("gpt-3.5")?[[e,Fa]]:[[e,Ga]]));var V=new RegExp("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+","gu"),Je=String.raw`'(?:[sS]|[dD]|[mM]|[tT]|[lL][lL]|[vV][eE]|[rR][eE])`,Ke=String.raw`(?:${Je})?`,qa=String.raw`${Je}|[^\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`,$e=new RegExp(qa,"gu"),Ha=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}]+${Ke}|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*${Ke}|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`,oe=new RegExp(Ha,"gu");function Ye(e){return{tokenSplitRegex:$e,bytePairRankDecoder:e,specialTokensEncoder:new Map([[E,100257],[j,100258],[x,100259],[D,100260],[F,100264],[G,100265],[N,100266],[L,100276]])}}var Ua=[[E,199999],[j,2e5],[x,200001],[D,200002],[F,200003],[G,200004],[N,200005],[L,200006]],Va=()=>new Map(Ua);function Xe(e){return{tokenSplitRegex:oe,bytePairRankDecoder:e,specialTokensEncoder:Va()}}var Wa=200013,Ka=201088,Ja=[[he,199998],[E,199999],["<|reserved_200000|>",2e5],["<|reserved_200001|>",200001],[Z,200002],[ee,200003],["<|reserved_200004|>",200004],[Q,200005],[$,200006],[Y,200007],[X,200008],["<|reserved_200009|>",200009],["<|reserved_200010|>",200010],["<|reserved_200011|>",200011],[ae,200012]];function Qe(e){let a=new Map(Ja);for(let i=Wa;i<Ka;i+=1)a.set(`<|reserved_${i}|>`,i);return a.set(L,200018),{tokenSplitRegex:oe,bytePairRankDecoder:e,specialTokensEncoder:a,chatFormatter:"harmony"}}function Ze(e){return{expectedVocabularySize:50281,tokenSplitRegex:V,bytePairRankDecoder:e,specialTokensEncoder:new Map([[E,50256]])}}function ea(e){return{tokenSplitRegex:V,bytePairRankDecoder:e,specialTokensEncoder:new Map([[E,50256],[j,50281],[x,50282],[D,50283]])}}function aa(e){return{expectedVocabularySize:50257,tokenSplitRegex:V,bytePairRankDecoder:e,specialTokensEncoder:new Map([[E,50256]])}}function ye(e,a){let i=a(e);switch(e.toLowerCase()){case"gpt2":case"r50k_base":return aa(i);case"p50k_base":return Ze(i);case"p50k_edit":return ea(i);case"cl100k_base":return Ye(i);case"o200k_base":return Xe(i);case"o200k_harmony":return Qe(i);default:throw new Error(`Unknown encoding name: ${e}`)}}var ia=class we{static EndOfPrompt=L;static EndOfText=E;static FimMiddle=x;static FimPrefix=j;static FimSuffix=D;modelName;modelSpec;bytePairEncodingCoreProcessor;specialTokensEncoder;specialTokensSet;allSpecialTokenRegex;defaultSpecialTokenConfig;chatFormatter;countChatCompletionTokens;vocabularySize;constructor({bytePairRankDecoder:a,specialTokensEncoder:i,expectedVocabularySize:n,modelName:r,modelSpec:t,chatFormatter:o,...s}){this.specialTokensEncoder=i,this.specialTokensSet=new Set(this.specialTokensEncoder.keys()),this.allSpecialTokenRegex=fe(this.specialTokensSet),this.bytePairEncodingCoreProcessor=new Oe({bytePairRankDecoder:a,specialTokensEncoder:i,...s}),this.defaultSpecialTokenConfig=this.processSpecialTokens();let l=Math.max(a.length-1,Ne(i));if(this.vocabularySize=this.bytePairEncodingCoreProcessor.mergeableBytePairRankCount+i.size,n!==void 0){if(this.vocabularySize!==n)throw new Error("The number of mergeable tokens and special tokens must be equal to expectedVocabularySize.");if(l!==n-1)throw new Error(`The model encodings are invalid. The maximum token value must be equal to expectedVocabularySize - 1. Currently ${l}, expected ${n-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=o??"chatml"}*encodeHarmonyChatGenerator(a,i){let n=this.specialTokensEncoder.get($),r=this.specialTokensEncoder.get(X),t=this.specialTokensEncoder.get(Y),o=this.specialTokensEncoder.get(Z),s=this.specialTokensEncoder.get(ae),l=this.specialTokensEncoder.get(Q),c=this.specialTokensEncoder.get(ee);if(n===void 0||r===void 0||t===void 0||o===void 0||s===void 0||l===void 0||c===void 0)throw new Error("Harmony chat format requires dedicated special tokens.");let d=p=>p.length>0?this.encode(p):[],u=p=>{switch(p){case"<|return|>":return o;case"<|call|>":return s;default:return t}};for(let p of a){if(p.content===void 0)throw new Error("Content must be defined for all messages.");let v=p.name??p.role??"user";yield[n],yield d(v);let h=p.recipient&&(p.recipientPlacement==="role"||!p.channel),f=p.recipient&&!h;h&&(yield d(` to=${p.recipient}`)),p.channel&&(yield[l],yield d(p.channel),f&&(yield d(` to=${p.recipient}`))),p.constraint&&(yield[c],yield d(p.constraint)),yield[r],yield*this.encodeGenerator(p.content,i),yield[u(p.terminator)]}let g=i?.primeWithAssistantResponse??"assistant";g.length>0&&(yield[n],yield d(g))}static getEncodingApi(a,i){let n=ye(a,i);return new we(n)}static getEncodingApiForModel(a,i,n){let r=Ve[a]??"o200k_base",t=ye(r,i);return new we({...t,modelName:a,modelSpec:n})}processSpecialTokens({allowedSpecial:a,disallowedSpecial:i}={}){let n;if(a==="all"||a?.has("all")){a=new Set(this.specialTokensSet);let r=a;if(i==="all")throw new Error('allowedSpecial and disallowedSpecial cannot both be set to "all".');typeof i=="object"?i.forEach(t=>r.delete(t)):i=new Set}if(!i||i==="all"||i.has("all")){i=new Set(this.specialTokensSet);let r=i;a?.size?(a.forEach(t=>r.delete(t)),i.forEach(t=>a.delete(t)),i.size>0&&(n=fe(i))):n=this.allSpecialTokenRegex}return{allowedSpecial:a,regexPattern:n}}encodeGenerator(a,i){let n=i?this.processSpecialTokens(i):this.defaultSpecialTokenConfig;if(n.regexPattern){let r=a.match(n.regexPattern);if(r!==null)throw new Error(`Disallowed special token found: ${r[0]}`)}return this.bytePairEncodingCoreProcessor.encodeNativeGenerator(a,n.allowedSpecial)}encode(a,i){let n=i?this.processSpecialTokens(i):this.defaultSpecialTokenConfig;if(n.regexPattern){let r=a.match(n.regexPattern);if(r!==null)throw new Error(`Disallowed special token found: ${r[0]}`)}return this.bytePairEncodingCoreProcessor.encodeNative(a,n.allowedSpecial)}*encodeChatGenerator(a,i=this.modelName,n){if(!i)throw new Error("Model name must be provided either during initialization or passed in to the method.");let r=We[i];if(!r)throw new Error(`Model '${i}' does not support chat.`);if(this.chatFormatter==="harmony"){yield*this.encodeHarmonyChatGenerator(a,n);return}let t=this.specialTokensEncoder.get(F),o=this.specialTokensEncoder.get(G);if(t===void 0||o===void 0)throw new Error(`Model '${i}' does not support chat.`);let s=new Set([N]),{messageSeparator:l,roleSeparator:c}=r,d=l.length>0?this.encode(l):[],u=c.length>0?this.encode(c,{allowedSpecial:s}):[],g=new Map;for(let{role:v="system",name:h=v,content:f}of a){if(f===void 0)throw new Error("Content must be defined for all messages.");yield[t];let S=g.get(h)??this.encode(h);g.set(h,S),yield S,u.length>0&&(yield u),yield*this.encodeGenerator(f,n),yield[o],yield d}let p=n?.primeWithAssistantResponse??"assistant";p.length>0&&(yield[t],yield*this.encodeGenerator(p,n)),u.length>0&&(yield u)}encodeChat(a,i=this.modelName,n){return[...this.encodeChatGenerator(a,i,n)].flat()}isWithinTokenLimit(a,i,n){let r=typeof a=="string"?this.encodeGenerator(a,n):this.encodeChatGenerator(a,void 0,n),t=0;for(let o of r)if(t+=o.length,t>i)return!1;return t}countTokens(a,i){if(typeof a=="string"){let t=i?this.processSpecialTokens(i):this.defaultSpecialTokenConfig;if(t.regexPattern){let o=a.match(t.regexPattern);if(o!==null)throw new Error(`Disallowed special token found: ${o[0]}`)}return this.bytePairEncodingCoreProcessor.countNative(a,t.allowedSpecial)}let n=this.encodeChatGenerator(a,void 0,i),r=0;for(let t of n)r+=t.length;return r}countStringTokens(a){return a?this.bytePairEncodingCoreProcessor.countNative(a):0}countChatCompletionTokensInternal(a){return Fe(a,i=>this.countStringTokens(i))}setMergeCacheSize(a){this.bytePairEncodingCoreProcessor.setMergeCacheSize(a)}clearMergeCache(){this.bytePairEncodingCoreProcessor.clearMergeCache()}decode(a){return this.bytePairEncodingCoreProcessor.decodeNative(a)}*decodeGenerator(a){let i=this.bytePairEncodingCoreProcessor.decodeNativeGenerator(a),n="";for(let r of i)n+=typeof r=="string"?r:U.decode(r,{stream:!0}),!(n.length===0||be(n))&&(yield n,n="");n.length>0&&(yield n)}async*decodeAsyncGenerator(a){let i=this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(a),n="";for await(let r of i)n+=typeof r=="string"?r:U.decode(r,{stream:!0}),!(n.length===0||be(n))&&(yield n,n="");n.length>0&&(yield n)}async decodeAsync(a){let i=this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(a),n="";for await(let r of i)n+=typeof r=="string"?r:U.decode(r,{stream:!0});return n}estimateCost(a,i=this.modelSpec){if(!i)throw new Error("Model spec must be provided either during initialization or passed in to the method.");if(!i.price_data)throw new Error(`No cost information available for model: ${i.name}`);let n=i.price_data,r={},t=a/1e6;return n.main&&(r.main={input:n.main.input&&n.main.input*t,output:n.main.output&&n.main.output*t,cached_input:n.main.cached_input&&n.main.cached_input*t,cached_output:n.main.cached_output&&n.main.cached_output*t}),n.batch&&(r.batch={input:n.batch.input&&n.batch.input*t,output:n.batch.output&&n.batch.output*t,cached_input:n.batch.cached_input&&n.batch.cached_input*t,cached_output:n.batch.cached_output&&n.batch.cached_output*t}),r}};var na=["!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","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"," ",`
10
+ `},Ga={messageSeparator:"",roleSeparator:N},We=Object.fromEntries(Ge.flatMap(e=>e.startsWith("gpt-3.5")?[[e,Fa]]:[[e,Ga]]));var V=new RegExp("'s|'t|'re|'ve|'m|'ll|'d| ?\\p{L}+| ?\\p{N}+| ?[^\\s\\p{L}\\p{N}]+|\\s+(?!\\S)|\\s+","gu"),Je=String.raw`'(?:[sS]|[dD]|[mM]|[tT]|[lL][lL]|[vV][eE]|[rR][eE])`,Ke=String.raw`(?:${Je})?`,qa=String.raw`${Je}|[^\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`,$e=new RegExp(qa,"gu"),Ha=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}]+${Ke}|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*${Ke}|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`,oe=new RegExp(Ha,"gu");function Ye(e){return{tokenSplitRegex:$e,bytePairRankDecoder:e,specialTokensEncoder:new Map([[A,100257],[j,100258],[x,100259],[D,100260],[F,100264],[G,100265],[N,100266],[L,100276]])}}var Ua=[[A,199999],[j,2e5],[x,200001],[D,200002],[F,200003],[G,200004],[N,200005],[L,200006]],Va=()=>new Map(Ua);function Xe(e){return{tokenSplitRegex:oe,bytePairRankDecoder:e,specialTokensEncoder:Va()}}var Wa=200013,Ka=201088,Ja=[[he,199998],[A,199999],["<|reserved_200000|>",2e5],["<|reserved_200001|>",200001],[Z,200002],[ee,200003],["<|reserved_200004|>",200004],[Q,200005],[$,200006],[Y,200007],[X,200008],["<|reserved_200009|>",200009],["<|reserved_200010|>",200010],["<|reserved_200011|>",200011],[ae,200012]];function Qe(e){let a=new Map(Ja);for(let i=Wa;i<Ka;i+=1)a.set(`<|reserved_${i}|>`,i);return a.set(L,200018),{tokenSplitRegex:oe,bytePairRankDecoder:e,specialTokensEncoder:a,chatFormatter:"harmony"}}function Ze(e){return{expectedVocabularySize:50281,tokenSplitRegex:V,bytePairRankDecoder:e,specialTokensEncoder:new Map([[A,50256]])}}function ea(e){return{tokenSplitRegex:V,bytePairRankDecoder:e,specialTokensEncoder:new Map([[A,50256],[j,50281],[x,50282],[D,50283]])}}function aa(e){return{expectedVocabularySize:50257,tokenSplitRegex:V,bytePairRankDecoder:e,specialTokensEncoder:new Map([[A,50256]])}}function ye(e,a){let i=a(e);switch(e.toLowerCase()){case"gpt2":case"r50k_base":return aa(i);case"p50k_base":return Ze(i);case"p50k_edit":return ea(i);case"cl100k_base":return Ye(i);case"o200k_base":return Xe(i);case"o200k_harmony":return Qe(i);default:throw new Error(`Unknown encoding name: ${e}`)}}var ia=class we{static EndOfPrompt=L;static EndOfText=A;static FimMiddle=x;static FimPrefix=j;static FimSuffix=D;modelName;modelSpec;bytePairEncodingCoreProcessor;specialTokensEncoder;specialTokensSet;allSpecialTokenRegex;defaultSpecialTokenConfig;chatFormatter;countChatCompletionTokens;vocabularySize;constructor({bytePairRankDecoder:a,specialTokensEncoder:i,expectedVocabularySize:n,modelName:r,modelSpec:t,chatFormatter:o,...s}){this.specialTokensEncoder=i,this.specialTokensSet=new Set(this.specialTokensEncoder.keys()),this.allSpecialTokenRegex=fe(this.specialTokensSet),this.bytePairEncodingCoreProcessor=new Oe({bytePairRankDecoder:a,specialTokensEncoder:i,...s}),this.defaultSpecialTokenConfig=this.processSpecialTokens();let l=Math.max(a.length-1,Ne(i));if(this.vocabularySize=this.bytePairEncodingCoreProcessor.mergeableBytePairRankCount+i.size,n!==void 0){if(this.vocabularySize!==n)throw new Error("The number of mergeable tokens and special tokens must be equal to expectedVocabularySize.");if(l!==n-1)throw new Error(`The model encodings are invalid. The maximum token value must be equal to expectedVocabularySize - 1. Currently ${l}, expected ${n-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=o??"chatml"}*encodeHarmonyChatGenerator(a,i){let n=this.specialTokensEncoder.get($),r=this.specialTokensEncoder.get(X),t=this.specialTokensEncoder.get(Y),o=this.specialTokensEncoder.get(Z),s=this.specialTokensEncoder.get(ae),l=this.specialTokensEncoder.get(Q),c=this.specialTokensEncoder.get(ee);if(n===void 0||r===void 0||t===void 0||o===void 0||s===void 0||l===void 0||c===void 0)throw new Error("Harmony chat format requires dedicated special tokens.");let d=p=>p.length>0?this.encode(p):[],u=p=>{switch(p){case"<|return|>":return o;case"<|call|>":return s;default:return t}};for(let p of a){if(p.content===void 0)throw new Error("Content must be defined for all messages.");let y=p.name??p.role??"user";yield[n],yield d(y);let h=p.recipient&&(p.recipientPlacement==="role"||!p.channel),f=p.recipient&&!h;h&&(yield d(` to=${p.recipient}`)),p.channel&&(yield[l],yield d(p.channel),f&&(yield d(` to=${p.recipient}`))),p.constraint&&(yield[c],yield d(p.constraint)),yield[r],yield*this.encodeGenerator(p.content,i),yield[u(p.terminator)]}let g=i?.primeWithAssistantResponse??"assistant";g.length>0&&(yield[n],yield d(g))}static getEncodingApi(a,i){let n=ye(a,i);return new we(n)}static getEncodingApiForModel(a,i,n){let r=Ve[a]??"o200k_base",t=ye(r,i);return new we({...t,modelName:a,modelSpec:n})}processSpecialTokens({allowedSpecial:a,disallowedSpecial:i}={}){let n;if(a==="all"||a?.has("all")){a=new Set(this.specialTokensSet);let r=a;if(i==="all")throw new Error('allowedSpecial and disallowedSpecial cannot both be set to "all".');typeof i=="object"?i.forEach(t=>r.delete(t)):i=new Set}if(!i||i==="all"||i.has("all")){i=new Set(this.specialTokensSet);let r=i;a?.size?(a.forEach(t=>r.delete(t)),i.forEach(t=>a.delete(t)),i.size>0&&(n=fe(i))):n=this.allSpecialTokenRegex}return{allowedSpecial:a,regexPattern:n}}encodeGenerator(a,i){let n=i?this.processSpecialTokens(i):this.defaultSpecialTokenConfig;if(n.regexPattern){let r=a.match(n.regexPattern);if(r!==null)throw new Error(`Disallowed special token found: ${r[0]}`)}return this.bytePairEncodingCoreProcessor.encodeNativeGenerator(a,n.allowedSpecial)}encode(a,i){let n=i?this.processSpecialTokens(i):this.defaultSpecialTokenConfig;if(n.regexPattern){let r=a.match(n.regexPattern);if(r!==null)throw new Error(`Disallowed special token found: ${r[0]}`)}return this.bytePairEncodingCoreProcessor.encodeNative(a,n.allowedSpecial)}*encodeChatGenerator(a,i=this.modelName,n){if(!i)throw new Error("Model name must be provided either during initialization or passed in to the method.");let r=We[i];if(!r)throw new Error(`Model '${i}' does not support chat.`);if(this.chatFormatter==="harmony"){yield*this.encodeHarmonyChatGenerator(a,n);return}let t=this.specialTokensEncoder.get(F),o=this.specialTokensEncoder.get(G);if(t===void 0||o===void 0)throw new Error(`Model '${i}' does not support chat.`);let s=new Set([N]),{messageSeparator:l,roleSeparator:c}=r,d=l.length>0?this.encode(l):[],u=c.length>0?this.encode(c,{allowedSpecial:s}):[],g=new Map;for(let{role:y="system",name:h=y,content:f}of a){if(f===void 0)throw new Error("Content must be defined for all messages.");yield[t];let _=g.get(h)??this.encode(h);g.set(h,_),yield _,u.length>0&&(yield u),yield*this.encodeGenerator(f,n),yield[o],yield d}let p=n?.primeWithAssistantResponse??"assistant";p.length>0&&(yield[t],yield*this.encodeGenerator(p,n)),u.length>0&&(yield u)}encodeChat(a,i=this.modelName,n){return[...this.encodeChatGenerator(a,i,n)].flat()}isWithinTokenLimit(a,i,n){let r=typeof a=="string"?this.encodeGenerator(a,n):this.encodeChatGenerator(a,void 0,n),t=0;for(let o of r)if(t+=o.length,t>i)return!1;return t}countTokens(a,i){if(typeof a=="string"){let t=i?this.processSpecialTokens(i):this.defaultSpecialTokenConfig;if(t.regexPattern){let o=a.match(t.regexPattern);if(o!==null)throw new Error(`Disallowed special token found: ${o[0]}`)}return this.bytePairEncodingCoreProcessor.countNative(a,t.allowedSpecial)}let n=this.encodeChatGenerator(a,void 0,i),r=0;for(let t of n)r+=t.length;return r}countStringTokens(a){return a?this.bytePairEncodingCoreProcessor.countNative(a):0}countChatCompletionTokensInternal(a){return Fe(a,i=>this.countStringTokens(i))}setMergeCacheSize(a){this.bytePairEncodingCoreProcessor.setMergeCacheSize(a)}clearMergeCache(){this.bytePairEncodingCoreProcessor.clearMergeCache()}decode(a){return this.bytePairEncodingCoreProcessor.decodeNative(a)}*decodeGenerator(a){let i=this.bytePairEncodingCoreProcessor.decodeNativeGenerator(a),n="";for(let r of i)n+=typeof r=="string"?r:U.decode(r,{stream:!0}),!(n.length===0||be(n))&&(yield n,n="");n.length>0&&(yield n)}async*decodeAsyncGenerator(a){let i=this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(a),n="";for await(let r of i)n+=typeof r=="string"?r:U.decode(r,{stream:!0}),!(n.length===0||be(n))&&(yield n,n="");n.length>0&&(yield n)}async decodeAsync(a){let i=this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(a),n="";for await(let r of i)n+=typeof r=="string"?r:U.decode(r,{stream:!0});return n}estimateCost(a,i=this.modelSpec){if(!i)throw new Error("Model spec must be provided either during initialization or passed in to the method.");if(!i.price_data)throw new Error(`No cost information available for model: ${i.name}`);let n=i.price_data,r={},t=a/1e6;return n.main&&(r.main={input:n.main.input&&n.main.input*t,output:n.main.output&&n.main.output*t,cached_input:n.main.cached_input&&n.main.cached_input*t,cached_output:n.main.cached_output&&n.main.cached_output*t}),n.batch&&(r.batch={input:n.batch.input&&n.batch.input*t,output:n.batch.output&&n.batch.output*t,cached_input:n.batch.cached_input&&n.batch.cached_input*t,cached_output:n.batch.cached_output&&n.batch.cached_output*t}),r}};var na=["!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","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"," ",`
11
11
  `,"\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",`
12
12
 
13
13
  `,"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",`;
@@ -3267,7 +3267,7 @@ ${" ".repeat(a)}}`;default:return"any"}}function Pa(e){let a=["namespace functio
3267
3267
  `," surfers"," \u05DE\u05EA\u05D0\u05D9\u05DD",`')):
3268
3268
  `,".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
3269
3269
  `,"\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"],ta=na.concat(ra);na.length=0;ra.length=0;var $a=ia.getEncodingApi("o200k_base",()=>ta),{decode:Yn,decodeAsyncGenerator:Xn,decodeGenerator:Qn,encode:Zn,encodeGenerator:er,isWithinTokenLimit:ar,countTokens:sa,encodeChat:ir,encodeChatGenerator:nr,vocabularySize:rr,setMergeCacheSize:tr,clearMergeCache:sr,estimateCost:or}=$a;var Ya=/[A-Za-z]+|\d+|[^\sA-Za-z\d]/g;function Se(e,a=4){if(!e)return 0;let i=e.length/a,n=0;for(let r of e.match(Ya)??[]){let t=/^[A-Za-z]/.test(r)?6:/^\d/.test(r)?3:1;n+=Math.max(1,Math.ceil(r.length/t))}return Math.max(1,Math.round(Math.max(i,n)))}function I(e,a){if(!e)return{tokens:0,exact:a.tokenizer!=="heuristic"};if(a.tokenizer==="o200k")try{return{tokens:sa(e),exact:!0}}catch{}return{tokens:Se(e,a.charsPerToken),exact:!1}}var Xa={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/]},Qa=/\b(bug|broken|fails?|failing|doesn'?t work|not working|wrong output|fix (this|the|my|it))\b/,oa=["debugging","translation","summarizing","editing","analysis","coding","brainstorming","writing","research"],Za=/```[\s\S]*?```|~~~[\s\S]*?~~~/g,_e=/^\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*$/,le=/^\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:)/,ei=/^\s*([^,\t\n]+[,\t]){2,}[^,\t\n]+\s*$|^\s*[[{]\s*"\w+"\s*:/,ai=/\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/,ii=/\b(full|entire|whole|complete)\s+(file|code|script|program|document|text|essay)\b|\brewrite (the )?(whole|entire)\b/;function ni(e,a){let i=e.filter(n=>n.trim());return i.length?i.filter(n=>a.test(n)).length/i.length:0}function Ee(e){let a=e.replace(/^\s+/,""),i=e.length-a.length,n=/\n\s*\n|:\s*\n/.exec(a);if(!n)return{instruction:e,material:""};let r=i+n.index+n[0].length;return{instruction:e.slice(0,r),material:e.slice(r)}}function ie(e){let a=e.split(`
3270
- `),i=e.match(Za)??[],{instruction:n,material:r}=Ee(e),t=n.toLowerCase().trim(),o={hasCode:i.length>0||ni(a,_e)>.3,hasLogs:a.filter(d=>le.test(d)).length>=2,hasData:a.filter(d=>ei.test(d)).length>=3,pastedTokens:Se(r),hasLengthLimit:ai.test(e.toLowerCase()),asksForFullOutput:ii.test(t)},s=new Map;for(let d of oa){let u=Xa[d],g=u.reduce((p,v)=>p+(v.test(t)?2:0),0);!g&&r&&(g=u.reduce((p,v)=>p+(v.test(r.slice(0,2e3).toLowerCase())?.5:0),0)),s.set(d,g)}(o.hasCode||o.hasLogs)&&Qa.test(t)&&s.set("debugging",(s.get("debugging")??0)+2),o.hasLogs&&s.set("debugging",(s.get("debugging")??0)+2),o.hasCode&&s.set("coding",(s.get("coding")??0)+1.5),o.hasData&&s.set("analysis",(s.get("analysis")??0)+1);let l="chat",c=0;for(let d of oa){let u=s.get(d)??0;u>c&&(l=d,c=u)}return{task:l,confidence:Math.min(1,c/4),signals:o}}var la=new Set(["chat","writing","brainstorming","research"]);var ri=[["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 ce(e){let a=new Map;for(let[i,n]of ri)for(let r of e.match(n)??[]){let t=`${i}:${r.toLowerCase()}`;a.set(t,(a.get(t)??0)+1)}return a}function ne(e,a,i="count"){let n=ce(e),r=ce(a),t=[];for(let[o,s]of n)(r.get(o)??0)<(i==="count"?s:1)&&t.push(o);return{ok:t.length===0,lost:t}}function Ae(e,a){let i=ce(e),n=ce(a),r={};for(let t of i.keys()){let o=t.slice(0,t.indexOf(":")),[s,l]=r[o]??[0,0];r[o]=[s+1,l+(n.has(t)?1:0)]}return r}var ti=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(" ")),ca=/[a-z][a-z'-]{2,}/g;function Ce(e,a){let i=new Set(a.toLowerCase().match(ca)??[]),n=new Set;for(let r of e.toLowerCase().match(ca)??[])!i.has(r)&&!ti.has(r)&&n.add(r);return[...n]}var b=(e,a,i,n,r=!1)=>({re:new RegExp(e,"gim"),repl:a,name:i,level:n,reasoningOnly:r}),si=[b(String.raw`\bdue to the fact that\b`,"because","wordy phrasing","light"),b(String.raw`\bin spite of the fact that\b`,"although","wordy phrasing","light"),b(String.raw`\bin the event that\b`,"if","wordy phrasing","light"),b(String.raw`\bfor the purpose of\b`,"for","wordy phrasing","light"),b(String.raw`\bin order to\b`,"to","wordy phrasing","light"),b(String.raw`\bwith (?:regard|respect) to\b`,"about","wordy phrasing","light"),b(String.raw`\bat this point in time\b`,"now","wordy phrasing","light"),b(String.raw`\bin the near future\b`,"soon","wordy phrasing","light"),b(String.raw`\ba large number of\b`,"many","wordy phrasing","light"),b(String.raw`\bthe majority of\b`,"most","wordy phrasing","light"),b(String.raw`\b(is|are) able to\b`,"can","wordy phrasing","light"),b(String.raw`\bmake sure that\b`,"ensure","wordy phrasing","light"),b(String.raw`\b(?:let'?s|please)?\s*think (?:about (?:this|it) )?step[- ]by[- ]step\b[.,]?\s*`,"","step-by-step (model already reasons)","light",!0),b(String.raw`\btake a deep breath(?: and)?[.,]?\s*`,"","step-by-step (model already reasons)","light",!0),b(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),b(String.raw`^\s*(?:hi|hello|hey)(?: there)?(?: chatgpt| claude| gemini)?[,!.]?\s*`,"","politeness","balanced"),b(String.raw`(^|[.!?]\s+)(?:could|can|would|will) you (?:please )?(?:kindly )?([^.!?\n]*)\?`,"$1$2.","politeness","balanced"),b(String.raw`(^|[.!?]\s+)(?:could|can|would|will) you (?:please )?(?:kindly )?`,"$1","politeness","balanced"),b(String.raw`\bi would (?:really )?(?:like|love) (?:for )?you to\b\s*`,"","politeness","balanced"),b(String.raw`\bi (?:want|need) you to\b\s*`,"","politeness","balanced"),b(String.raw`\bplease\b,?\s*`,"","politeness","balanced"),b(String.raw`\bkindly\b\s*`,"","politeness","balanced"),b(String.raw`\bthank you(?: so much| very much| a lot)?(?: in advance)?[.!]?`,"","politeness","balanced"),b(String.raw`\bthanks(?: so much| very much| a lot)?(?: in advance)?[.!]?`,"","politeness","balanced"),b(String.raw`\b(?:it is|it's) (?:important|worth noting) (?:to note )?that\b\s*`,"","filler","balanced"),b(String.raw`\b(?:please )?note that\b\s*`,"","filler","balanced"),b(String.raw`\b(?:you are|act as|as) (?:an?|my) (?:ai|large language model|helpful assistant|ai assistant)[,.]?\s*`,"","filler","balanced"),b(String.raw`\bi was wondering if\b\s*`,"","filler","balanced"),b(String.raw`(?<!\bnot )\b(?:basically|actually|really|very|just|simply|quite|literally|totally|definitely)\b\s*`,"","hedges","aggressive"),b(String.raw`\b(?:if possible|if you can|if you don't mind)\b[,.]?\s*`,"","hedges","aggressive")],ue={light:0,balanced:1,aggressive:2},oi=/(```[\s\S]*?```|~~~[\s\S]*?~~~)/,li=new RegExp(["`[^`\\n]+`","https?:\\/\\/\\S+","<([A-Za-z][\\w-]*)[^>]*>[\\s\\S]*?<\\/\\1>",'"""[\\s\\S]*?"""'].join("|"),"g");function ci(e){return/^\s*>/.test(e)||/^\s{4,}\S/.test(e)||_e.test(e)||le.test(e)}function ui(e,a){return a&&/^[A-Z]/.test(e)?a[0].toUpperCase()+a.slice(1):a}function di(e,a,i){for(let n of si){if(ue[n.level]>ue[a.level]||n.reasoningOnly&&!a.profile.reasoning)continue;n.re.lastIndex=0;let r=e.replace(n.re,(...t)=>{let o=t[0],s=t.slice(1,-2),l=n.repl.replace(/\$(\d)/g,(d,u)=>s[Number(u)-1]??""),c=n.repl.startsWith("$1")?s[0]??"":"";return c+ui(o.slice(c.length),l.slice(c.length))});r!==e&&(i.push(n.name),e=r)}return e}function mi(e,a){let i=new Set;return e.split(`
3270
+ `),i=e.match(Za)??[],{instruction:n,material:r}=Ee(e),t=n.toLowerCase().trim(),o={hasCode:i.length>0||ni(a,_e)>.3,hasLogs:a.filter(d=>le.test(d)).length>=2,hasData:a.filter(d=>ei.test(d)).length>=3,pastedTokens:Se(r),hasLengthLimit:ai.test(e.toLowerCase()),asksForFullOutput:ii.test(t)},s=new Map;for(let d of oa){let u=Xa[d],g=u.reduce((p,y)=>p+(y.test(t)?2:0),0);!g&&r&&(g=u.reduce((p,y)=>p+(y.test(r.slice(0,2e3).toLowerCase())?.5:0),0)),s.set(d,g)}(o.hasCode||o.hasLogs)&&Qa.test(t)&&s.set("debugging",(s.get("debugging")??0)+2),o.hasLogs&&s.set("debugging",(s.get("debugging")??0)+2),o.hasCode&&s.set("coding",(s.get("coding")??0)+1.5),o.hasData&&s.set("analysis",(s.get("analysis")??0)+1);let l="chat",c=0;for(let d of oa){let u=s.get(d)??0;u>c&&(l=d,c=u)}return{task:l,confidence:Math.min(1,c/4),signals:o}}var la=new Set(["chat","writing","brainstorming","research"]);var ri=[["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 ce(e){let a=new Map;for(let[i,n]of ri)for(let r of e.match(n)??[]){let t=`${i}:${r.toLowerCase()}`;a.set(t,(a.get(t)??0)+1)}return a}function ne(e,a,i="count"){let n=ce(e),r=ce(a),t=[];for(let[o,s]of n)(r.get(o)??0)<(i==="count"?s:1)&&t.push(o);return{ok:t.length===0,lost:t}}function Ae(e,a){let i=ce(e),n=ce(a),r={};for(let t of i.keys()){let o=t.slice(0,t.indexOf(":")),[s,l]=r[o]??[0,0];r[o]=[s+1,l+(n.has(t)?1:0)]}return r}var ti=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(" ")),ca=/[a-z][a-z'-]{2,}/g;function Ce(e,a){let i=new Set(a.toLowerCase().match(ca)??[]),n=new Set;for(let r of e.toLowerCase().match(ca)??[])!i.has(r)&&!ti.has(r)&&n.add(r);return[...n]}var b=(e,a,i,n,r=!1)=>({re:new RegExp(e,"gim"),repl:a,name:i,level:n,reasoningOnly:r}),si=[b(String.raw`\bdue to the fact that\b`,"because","wordy phrasing","light"),b(String.raw`\bin spite of the fact that\b`,"although","wordy phrasing","light"),b(String.raw`\bin the event that\b`,"if","wordy phrasing","light"),b(String.raw`\bfor the purpose of\b`,"for","wordy phrasing","light"),b(String.raw`\bin order to\b`,"to","wordy phrasing","light"),b(String.raw`\bwith (?:regard|respect) to\b`,"about","wordy phrasing","light"),b(String.raw`\bat this point in time\b`,"now","wordy phrasing","light"),b(String.raw`\bin the near future\b`,"soon","wordy phrasing","light"),b(String.raw`\ba large number of\b`,"many","wordy phrasing","light"),b(String.raw`\bthe majority of\b`,"most","wordy phrasing","light"),b(String.raw`\b(is|are) able to\b`,"can","wordy phrasing","light"),b(String.raw`\bmake sure that\b`,"ensure","wordy phrasing","light"),b(String.raw`\b(?:let'?s|please)?\s*think (?:about (?:this|it) )?step[- ]by[- ]step\b[.,]?\s*`,"","step-by-step (model already reasons)","light",!0),b(String.raw`\btake a deep breath(?: and)?[.,]?\s*`,"","step-by-step (model already reasons)","light",!0),b(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),b(String.raw`^\s*(?:hi|hello|hey)(?: there)?(?: chatgpt| claude| gemini)?[,!.]?\s*`,"","politeness","balanced"),b(String.raw`(^|[.!?]\s+)(?:could|can|would|will) you (?:please )?(?:kindly )?([^.!?\n]*)\?`,"$1$2.","politeness","balanced"),b(String.raw`(^|[.!?]\s+)(?:could|can|would|will) you (?:please )?(?:kindly )?`,"$1","politeness","balanced"),b(String.raw`\bi would (?:really )?(?:like|love) (?:for )?you to\b\s*`,"","politeness","balanced"),b(String.raw`\bi (?:want|need) you to\b\s*`,"","politeness","balanced"),b(String.raw`\bplease\b,?\s*`,"","politeness","balanced"),b(String.raw`\bkindly\b\s*`,"","politeness","balanced"),b(String.raw`\bthank you(?: so much| very much| a lot)?(?: in advance)?[.!]?`,"","politeness","balanced"),b(String.raw`\bthanks(?: so much| very much| a lot)?(?: in advance)?[.!]?`,"","politeness","balanced"),b(String.raw`\b(?:it is|it's) (?:important|worth noting) (?:to note )?that\b\s*`,"","filler","balanced"),b(String.raw`\b(?:please )?note that\b\s*`,"","filler","balanced"),b(String.raw`\b(?:you are|act as|as) (?:an?|my) (?:ai|large language model|helpful assistant|ai assistant)[,.]?\s*`,"","filler","balanced"),b(String.raw`\bi was wondering if\b\s*`,"","filler","balanced"),b(String.raw`(?<!\bnot )\b(?:basically|actually|really|very|just|simply|quite|literally|totally|definitely)\b\s*`,"","hedges","aggressive"),b(String.raw`\b(?:if possible|if you can|if you don't mind)\b[,.]?\s*`,"","hedges","aggressive")],ue={light:0,balanced:1,aggressive:2},oi=/(```[\s\S]*?```|~~~[\s\S]*?~~~)/,li=new RegExp(["`[^`\\n]+`","https?:\\/\\/\\S+","<([A-Za-z][\\w-]*)[^>]*>[\\s\\S]*?<\\/\\1>",'"""[\\s\\S]*?"""'].join("|"),"g");function ci(e){return/^\s*>/.test(e)||/^\s{4,}\S/.test(e)||_e.test(e)||le.test(e)}function ui(e,a){return a&&/^[A-Z]/.test(e)?a[0].toUpperCase()+a.slice(1):a}function di(e,a,i){for(let n of si){if(ue[n.level]>ue[a.level]||n.reasoningOnly&&!a.profile.reasoning)continue;n.re.lastIndex=0;let r=e.replace(n.re,(...t)=>{let o=t[0],s=t.slice(1,-2),l=n.repl.replace(/\$(\d)/g,(d,u)=>s[Number(u)-1]??""),c=n.repl.startsWith("$1")?s[0]??"":"";return c+ui(o.slice(c.length),l.slice(c.length))});r!==e&&(i.push(n.name),e=r)}return e}function mi(e,a){let i=new Set;return e.split(`
3271
3271
  `).map(n=>n.split(/(?<=[.!?])\s+/).filter(r=>{let t=r.replace(/\W+/g," ").trim().toLowerCase();return t.split(" ").length<4?!0:i.has(t)?(a.push("repeated sentence"),!1):(i.add(t),!0)}).join(" ")).join(`
3272
3272
  `)}function pi(e){return e.replace(/[ \t]+/g," ").replace(/ +([,.;:!?])/g,"$1").replace(/([,;:])(?=[.!?])/g,"").replace(/^[ ,;:]+/gm,"").replace(/ +$/gm,"").replace(/\n{3,}/g,`
3273
3273
 
@@ -3278,7 +3278,7 @@ ${" ".repeat(a)}}`;default:return"any"}}function Pa(e){let a=["namespace functio
3278
3278
  `)}function ma(e){let a=e.split(`
3279
3279
  `).filter(i=>i.trim());return a.length>=3&&a.filter(i=>le.test(i)||pa.test(i)).length/a.length>=.4}function hi(e,a){let i=/^(?:```|~~~)\s*([\w+-]*)/.exec(e)?.[1]?.toLowerCase()??"",n=e.replace(/^(?:```|~~~).*\n?/,"");if(/^(md|markdown|diff|patch|text|txt)$/.test(i)||/"""|'''|`[^`\n]*\n|<<-?\s*['"]?\w+/.test(n))return e;let r=e.replace(/[ \t]+$/gm,"").replace(/\n{4,}/g,`
3280
3280
 
3281
- `);return r!==e&&a.push("whitespace in code"),r}function O(e,a){let i=[],{instruction:n}=Ee(e),r=n.length,t=a.task==="debugging"&&a.level!=="light",o=0,s=[];for(let[u,g]of e.split(oi).entries()){let p=o;if(o+=g.length,u%2===1){s.push(t&&ma(g)?da(g,a.level,i):a.level==="light"?g:hi(g,i));continue}let v=g.slice(0,Math.max(0,r-p)),h=g.slice(v.length),f=h;t&&ma(h)?f=da(h,a.level,i):la.has(a.task)&&(f=ua(h,a,i)),s.push(ua(v,a,i)+f)}let l=s.join(""),c=I(e,a.profile).tokens,d=I(l,a.profile).tokens;return d>=c||!l.trim()?{text:e,rules:[],originalTokens:c,tokens:c,saved:0}:{text:l,rules:[...new Set(i)],originalTokens:c,tokens:d,saved:c-d}}function Te(e,a="input"){let i=e.pricing;return i?(a==="output"?i.outputPerMTok:a==="cached"?i.cachedInputPerMTok??i.inputPerMTok:i.inputPerMTok)/1e6:null}function W(e,a,i="input"){let n=Te(a,i);return n===null?null:e*n}function q(e,a,i="input"){let n=W(e,a,i);return{tokens:e,usd:n,usdPer1k:n===null?null:n*1e3}}function ga(e,a){let i=Te(a,"input"),n=Te(a,"cached"),r=i===null||n===null?null:e*(i-n);return{tokens:e,usd:r,usdPer1k:r===null?null:r*1e3}}function K(e){if(e==null||!Number.isFinite(e))return"n/a";let a=Math.abs(e),i=e<0?"-":"";if(a===0)return"$0";if(a>=100)return`${i}$${a.toLocaleString("en-US",{maximumFractionDigits:0})}`;if(a>=.1)return`${i}$${a.toFixed(2)}`;let n=Math.min(8,Math.max(2,1-Math.floor(Math.log10(a))));return`${i}$${a.toFixed(n).replace(/(\.\d*?[1-9])0+$/,"$1")}`}var A=e=>e,H={"gpt-astra":A({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":A({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:A({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":A({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":A({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":A({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":A({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":A({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":A({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:A({id:"deepseek",label:"DeepSeek",family:"deepseek",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.28,outputPerMTok:.42,cachedInputPerMTok:.028}}),"deepseek-reasoning":A({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:A({id:"mistral",label:"Mistral",family:"mistral",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:6}}),grok:A({id:"grok",label:"Grok",family:"xai",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:6}}),generic:A({id:"generic",label:"Unknown model",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:10}})},bi=[[/\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"],[/\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"],[/\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 ki={chatgpt:"gpt",claude:"claude-sonnet",gemini:"gemini-flash",deepseek:"deepseek",mistral:"mistral",grok:"grok",generic:"generic"},fi=[];var vr={...H};function re(e,a="generic"){let i=(e??"").toLowerCase().replace(/\s+/g," ").trim();if(i){for(let[n,r]of fi)if(n.test(i))return H[r];if(a==="gemini"&&/\b(pro|deep think)\b/.test(i))return H["gemini-pro"];if(a==="gemini"&&/\bflash\b/.test(i))return H["gemini-flash"];if(a==="deepseek"&&/\b(deepthink|r1|think)/.test(i))return H["deepseek-reasoning"];for(let[n,r]of bi)if(n.test(i))return H[r]}return H[ki[a]??"generic"]}function ze(e,a){return e===a||ne(e,a,"presence").ok&&Ce(e,a).length===0}function Re(e){return Array.isArray(e)?e.map(Re):!e||typeof e!="object"?e:Object.fromEntries(Object.entries(e).filter(([a])=>a!=="description").map(([a,i])=>[a,Re(i)]))}function ha(e){let a=[];e.system&&a.push(["system prompt",e.system]);for(let i of e.tools??[]){i.description&&a.push([`tool ${i.name}`,i.description]);let n=(r,t)=>{if(Array.isArray(r))r.forEach((o,s)=>n(o,`${t}[${s}]`));else if(r&&typeof r=="object")for(let[o,s]of Object.entries(r))o==="description"&&typeof s=="string"?a.push([`tool ${i.name} ${t}`,s]):n(s,`${t}.${o}`)};n(i.parameters,"params")}for(let[i,n]of(e.examples??[]).entries())a.push([`example ${i+1}`,n]);return e.prompt&&a.push(["next prompt",e.prompt]),a}var de=(e,a)=>`${e} ${a}${e===1?"":"s"}`;function ba(e,a){let i=[],n=ha(e),r=new Map(ha(a)),t={},o=[];for(let[h,f]of n){let S=r.get(h)??"";for(let[T,[P,ge]]of Object.entries(Ae(f,S))){let[C,te]=t[T]??[0,0];t[T]=[C+P,te+ge]}ne(f,S,"presence").ok||o.push(h)}let s=Object.entries(t).map(([h,[f,S]])=>`${S}/${f} ${h}s`);i.push({name:"Facts and constraints kept",passed:o.length===0,detail:o.length?`Lost in: ${o.join(", ")}`:s.length?`Kept ${s.join(", ")}.`:"No numbers, links, code or constraint words to keep."});let l=[];for(let[h,f]of n){let S=Ce(f,r.get(h)??"");S.length&&l.push(`${h}: ${S.join(", ")}`)}i.push({name:"Instructions kept",passed:l.length===0,detail:l.length?`Missing words: ${l.join("; ")}`:`Every content word of ${de(n.length,"text")} is still there; only filler was removed.`});let c=(h=[])=>JSON.stringify(h.map(f=>[f.name,Re(f.parameters??null)])),d=c(e.tools)===c(a.tools);i.push({name:"Tool interface unchanged",passed:d,detail:d?`${de(e.tools?.length??0,"tool")}: same names, parameters, types, required fields and enums.`:"Tool names or parameter schemas differ."});let u=e.messages??[],g=a.messages??[],p=0,v=u.length===g.length&&u.every((h,f)=>{let S=g[f];if(S.role!==h.role)return!1;if(S.content===h.content)return!0;let T=/^\[Same content as message (\d+) above\.\]$/.exec(S.content),P=!!T&&u[Number(T[1])-1]?.content.trim()===h.content.trim();return P&&p++,P});return u.length&&i.push({name:"Conversation unchanged",passed:v,detail:v?`${de(u.length,"message")} in order${p?`; ${de(p,"exact repeat")} replaced by a pointer to the first copy`:", all verbatim"}.`:"Messages were dropped, reordered or edited."}),i.push({name:"Model and examples unchanged",passed:(e.examples?.length??0)===(a.examples?.length??0),detail:"Token Lens never switches the model or drops examples on its own."}),{passed:i.every(h=>h.passed),checks:i}}var w=e=>e.toLocaleString("en-US"),ka=(e,a)=>a?Math.round(e/a*100):0;function fa(e){return`${e.name}
3281
+ `);return r!==e&&a.push("whitespace in code"),r}function O(e,a){let i=[],{instruction:n}=Ee(e),r=n.length,t=a.task==="debugging"&&a.level!=="light",o=0,s=[];for(let[u,g]of e.split(oi).entries()){let p=o;if(o+=g.length,u%2===1){s.push(t&&ma(g)?da(g,a.level,i):a.level==="light"?g:hi(g,i));continue}let y=g.slice(0,Math.max(0,r-p)),h=g.slice(y.length),f=h;t&&ma(h)?f=da(h,a.level,i):la.has(a.task)&&(f=ua(h,a,i)),s.push(ua(y,a,i)+f)}let l=s.join(""),c=I(e,a.profile).tokens,d=I(l,a.profile).tokens;return d>=c||!l.trim()?{text:e,rules:[],originalTokens:c,tokens:c,saved:0}:{text:l,rules:[...new Set(i)],originalTokens:c,tokens:d,saved:c-d}}function Te(e,a="input"){let i=e.pricing;return i?(a==="output"?i.outputPerMTok:a==="cached"?i.cachedInputPerMTok??i.inputPerMTok:i.inputPerMTok)/1e6:null}function W(e,a,i="input"){let n=Te(a,i);return n===null?null:e*n}function q(e,a,i="input"){let n=W(e,a,i);return{tokens:e,usd:n,usdPer1k:n===null?null:n*1e3}}function ga(e,a){let i=Te(a,"input"),n=Te(a,"cached"),r=i===null||n===null?null:e*(i-n);return{tokens:e,usd:r,usdPer1k:r===null?null:r*1e3}}function K(e){if(e==null||!Number.isFinite(e))return"n/a";let a=Math.abs(e),i=e<0?"-":"";if(a===0)return"$0";if(a>=100)return`${i}$${a.toLocaleString("en-US",{maximumFractionDigits:0})}`;if(a>=.1)return`${i}$${a.toFixed(2)}`;let n=Math.min(8,Math.max(2,1-Math.floor(Math.log10(a))));return`${i}$${a.toFixed(n).replace(/(\.\d*?[1-9])0+$/,"$1")}`}var v=e=>e,H={"gpt-astra":v({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":v({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:v({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":v({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":v({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":v({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":v({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":v({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":v({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:v({id:"deepseek",label:"DeepSeek",family:"deepseek",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.28,outputPerMTok:.42,cachedInputPerMTok:.028}}),"deepseek-reasoning":v({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:v({id:"mistral",label:"Mistral",family:"mistral",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:6}}),grok:v({id:"grok",label:"Grok",family:"xai",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:6}}),llama:v({id:"llama",label:"Llama",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.27,outputPerMTok:.85}}),qwen:v({id:"qwen",label:"Qwen",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.4,outputPerMTok:1.2}}),kimi:v({id:"kimi",label:"Kimi",family:"other",contextWindow:256e3,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.6,outputPerMTok:2.5,cachedInputPerMTok:.15}}),glm:v({id:"glm",label:"GLM",family:"other",contextWindow:2e5,tokenizer:"heuristic",charsPerToken:3.8,reasoning:!1,pricing:{inputPerMTok:.6,outputPerMTok:2.2,cachedInputPerMTok:.11}}),command:v({id:"command",label:"Cohere Command",family:"other",contextWindow:256e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2.5,outputPerMTok:10}}),sonar:v({id:"sonar",label:"Perplexity Sonar",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:1,outputPerMTok:1}}),gemma:v({id:"gemma",label:"Gemma",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.1,outputPerMTok:.2}}),phi:v({id:"phi",label:"Phi",family:"other",contextWindow:16e3,tokenizer:"heuristic",charsPerToken:3.9,reasoning:!1,pricing:{inputPerMTok:.07,outputPerMTok:.14}}),generic:v({id:"generic",label:"Unknown model",family:"other",contextWindow:128e3,tokenizer:"heuristic",charsPerToken:4,reasoning:!1,pricing:{inputPerMTok:2,outputPerMTok:10}})},bi=[[/\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 ki={chatgpt:"gpt",claude:"claude-sonnet",gemini:"gemini-flash",deepseek:"deepseek",mistral:"mistral",grok:"grok",generic:"generic"},fi=[];var vr={...H};function re(e,a="generic"){let i=(e??"").toLowerCase().replace(/\s+/g," ").trim();if(i){for(let[n,r]of fi)if(n.test(i))return H[r];if(a==="gemini"&&/\b(pro|deep think)\b/.test(i))return H["gemini-pro"];if(a==="gemini"&&/\bflash\b/.test(i))return H["gemini-flash"];if(a==="deepseek"&&/\b(deepthink|r1|think)/.test(i))return H["deepseek-reasoning"];for(let[n,r]of bi)if(n.test(i))return H[r]}return H[ki[a]??"generic"]}function ze(e,a){return e===a||ne(e,a,"presence").ok&&Ce(e,a).length===0}function Re(e){return Array.isArray(e)?e.map(Re):!e||typeof e!="object"?e:Object.fromEntries(Object.entries(e).filter(([a])=>a!=="description").map(([a,i])=>[a,Re(i)]))}function ha(e){let a=[];e.system&&a.push(["system prompt",e.system]);for(let i of e.tools??[]){i.description&&a.push([`tool ${i.name}`,i.description]);let n=(r,t)=>{if(Array.isArray(r))r.forEach((o,s)=>n(o,`${t}[${s}]`));else if(r&&typeof r=="object")for(let[o,s]of Object.entries(r))o==="description"&&typeof s=="string"?a.push([`tool ${i.name} ${t}`,s]):n(s,`${t}.${o}`)};n(i.parameters,"params")}for(let[i,n]of(e.examples??[]).entries())a.push([`example ${i+1}`,n]);return e.prompt&&a.push(["next prompt",e.prompt]),a}var de=(e,a)=>`${e} ${a}${e===1?"":"s"}`;function ba(e,a){let i=[],n=ha(e),r=new Map(ha(a)),t={},o=[];for(let[h,f]of n){let _=r.get(h)??"";for(let[T,[P,ge]]of Object.entries(Ae(f,_))){let[C,te]=t[T]??[0,0];t[T]=[C+P,te+ge]}ne(f,_,"presence").ok||o.push(h)}let s=Object.entries(t).map(([h,[f,_]])=>`${_}/${f} ${h}s`);i.push({name:"Facts and constraints kept",passed:o.length===0,detail:o.length?`Lost in: ${o.join(", ")}`:s.length?`Kept ${s.join(", ")}.`:"No numbers, links, code or constraint words to keep."});let l=[];for(let[h,f]of n){let _=Ce(f,r.get(h)??"");_.length&&l.push(`${h}: ${_.join(", ")}`)}i.push({name:"Instructions kept",passed:l.length===0,detail:l.length?`Missing words: ${l.join("; ")}`:`Every content word of ${de(n.length,"text")} is still there; only filler was removed.`});let c=(h=[])=>JSON.stringify(h.map(f=>[f.name,Re(f.parameters??null)])),d=c(e.tools)===c(a.tools);i.push({name:"Tool interface unchanged",passed:d,detail:d?`${de(e.tools?.length??0,"tool")}: same names, parameters, types, required fields and enums.`:"Tool names or parameter schemas differ."});let u=e.messages??[],g=a.messages??[],p=0,y=u.length===g.length&&u.every((h,f)=>{let _=g[f];if(_.role!==h.role)return!1;if(_.content===h.content)return!0;let T=/^\[Same content as message (\d+) above\.\]$/.exec(_.content),P=!!T&&u[Number(T[1])-1]?.content.trim()===h.content.trim();return P&&p++,P});return u.length&&i.push({name:"Conversation unchanged",passed:y,detail:y?`${de(u.length,"message")} in order${p?`; ${de(p,"exact repeat")} replaced by a pointer to the first copy`:", all verbatim"}.`:"Messages were dropped, reordered or edited."}),i.push({name:"Model and examples unchanged",passed:(e.examples?.length??0)===(a.examples?.length??0),detail:"Token Lens never switches the model or drops examples on its own."}),{passed:i.every(h=>h.passed),checks:i}}var S=e=>e.toLocaleString("en-US"),ka=(e,a)=>a?Math.round(e/a*100):0;function fa(e){return`${e.name}
3282
3282
  ${e.description??""}
3283
3283
  ${e.parameters?JSON.stringify(e.parameters):""}`}function wa(e,a=[]){if(e&&typeof e=="object")for(let[i,n]of Object.entries(e))i==="description"&&typeof n=="string"?a.push(n):wa(n,a);return a}function va(e){let a=(e.description??"").replace(/\s+/g," ").trim().split(/(?<=[.!?])\s/)[0]??"";return a.length>100?`${a.slice(0,97).trimEnd()}...`:a}function vi(e){let a=e.map(n=>`- ${n.name}${va(n)?`: ${va(n)}`:""}`).join(`
3284
3284
  `),i=e.map(n=>n.name);return{tools:[{name:"get_tool_schema",description:`Get the full definition (description and parameters) of a tool before calling it. Available tools:
@@ -3291,7 +3291,7 @@ ${a}`,parameters:{type:"object",properties:{tool_name:{type:"string",enum:i}},re
3291
3291
 
3292
3292
  ${i.join(`
3293
3293
  `)}`,moved:i}:{text:e,moved:i}}var Ei=/\b(?:respond|reply|return|output|answer|format|give)\w*\b[^.\n]{0,60}\bjson\b|\bvalid json\b|\bjson (?:object|format|schema|only)\b/i;function Ai(e){for(let a=0;a<e.length;a++){let i=e[a];if(i!=="{"&&i!=="[")continue;let n=i==="{"?"}":"]",r=0;for(let t=a;t<e.length;t++)if(e[t]===i)r++;else if(e[t]===n&&--r===0){try{let o=JSON.parse(e.slice(a,t+1));if(o&&typeof o=="object")return o}catch{}break}}}function Pe(e){if(Array.isArray(e))return{type:"array",items:e.length?Pe(e[0]):{}};if(e===null)return{type:"null"};if(typeof e=="object"){let a=Object.fromEntries(Object.entries(e).map(([i,n])=>[i,Pe(n)]));return{type:"object",properties:a,required:Object.keys(a),additionalProperties:!1}}return typeof e=="number"?{type:Number.isInteger(e)?"integer":"number"}:typeof e=="boolean"?{type:"boolean"}:{type:"string"}}function Ci(e,a){let i=Ai([e.system,...e.examples??[],e.prompt].filter(Boolean).join(`
3294
- `)),n=i===void 0?{type:"object",properties:{},additionalProperties:!0}:Pe(i),r=n.type==="object"?n:{type:"object",properties:{items:n},required:["items"],additionalProperties:!1},t=a.family==="anthropic"?{tools:[{name:"return_result",description:"Return the result.",input_schema:r,strict:!0}],tool_choice:{type:"tool",name:"return_result"}}:a.family==="google"?{generationConfig:{responseMimeType:"application/json",responseSchema:r}}:{response_format:{type:"json_schema",json_schema:{name:"result",strict:!0,schema:r}}};return{fromExample:i!==void 0,setup:t}}var ya=1024;function me(e,a){let i=typeof a=="string"?re(a):a,n=m=>m?I(m,i).tokens:0,r=n(e.system),t=(e.tools??[]).map(m=>({tool:m,tokens:n(fa(m))})),o=t.reduce((m,k)=>m+k.tokens,0),s=(e.examples??[]).map(n),l=s.reduce((m,k)=>m+k,0),c=(e.messages??[]).map(m=>({m,tokens:m.tokens??n(m.content)})),d=c.reduce((m,k)=>m+k.tokens,0),u=n(e.prompt),g=r+o+l,p=g+d+u,v=[{area:"system",label:"System prompt",tokens:r},{area:"tools",label:`Tool definitions (${t.length})`,tokens:o},{area:"examples",label:`Examples (${s.length})`,tokens:l},{area:"history",label:`Conversation (${c.length} messages)`,tokens:d},{area:"prompt",label:"Next prompt",tokens:u}].filter(m=>m.tokens>0),h=c.filter(m=>m.m.role==="user").length,f=h?Math.round(d/h):0,S=i.contextWindow-p,T=f>0?Math.max(0,Math.floor(S/f)):null,P=[],ge=new Set(["prompt-caching","volatile-prefix"]),C=m=>m.saves>0&&P.push({...m,usd:(ge.has(m.id)?ga(m.saves,i):q(m.saves,i)).usd});if(e.system){let m=O(e.system,{profile:i,task:"chat",level:"balanced"});C({id:"system-wording",area:"system",issue:`System prompt is ${w(r)} tokens and is re-sent on every call.`,fix:`Tighten its wording (${m.rules.join(", ")||"filler"}); meaning and constraints are kept.`,risk:"safe",saves:m.saved});let k=_i(e.system);k.moved.length&&g>=ya&&C({id:"volatile-prefix",area:"system",issue:`The system prompt has ${k.moved.length} line${k.moved.length>1?"s":""} that change between calls (${k.moved.map(y=>`"${y.trim().slice(0,40)}"`).join(", ")}) ahead of the rest, so the provider's discount for a repeated opening can't apply.`,fix:"Move those lines to the end of the system prompt (ready to paste below): same content, so the model reads the same facts, and everything before them is billed at the cached price.",risk:"safe",saves:Math.round(g*.9)})}g>=ya&&(i.family==="anthropic"||i.family==="openai"||i.family==="google")&&C({id:"prompt-caching",area:"system",issue:`${w(g)} tokens of system prompt, tools and examples are identical on every call.`,fix:i.family==="anthropic"?"Mark the end of the tools and system prompt with cache_control so repeat calls read it from cache at a fraction of the price. The saving shown is net of the cached-read price; each cache write (when the cache is cold) costs about 25% extra on those tokens.":i.family==="openai"?"Keep this prefix byte-identical and first in every request; OpenAI caches repeated prefixes automatically and bills them at a discount.":"Use context caching for this prefix so repeat calls don't pay full price for it. The saving shown is net of the cached price, before any cache storage charge.",risk:"safe",saves:Math.round(g*.9)});let te=new Set((e.messages??[]).flatMap(m=>m.toolCalls??[]));for(let{tool:m,tokens:k}of t){let z=[m.description??"",...wa(m.parameters)].filter(Boolean).reduce((_,B)=>_+O(B,{profile:i,task:"chat",level:"balanced"}).saved,0);C({id:`tool-wording:${m.name}`,area:"tools",issue:`Tool "${m.name}" definition is ${w(k)} tokens.`,fix:"Tighten the tool and parameter descriptions; names, types and constraints are kept.",risk:"safe",saves:z})}if(t.length>=yi||t.length>=3&&o>=wi){let k=vi(e.tools??[]).tools.reduce((y,z)=>y+n(fa(z)),0);C({id:"lazy-tools",area:"tools",issue:`${t.length} tool definitions (${w(o)} tokens) are sent in full on every call, whichever tools the call uses.`,fix:`Load tool definitions on demand: send a one-line catalog plus get_tool_schema and invoke_tool (${w(k)} tokens) and return each full definition only when the agent asks for it. Same tools and model; the first use of each tool in a run adds one short lookup.`,risk:"safe",saves:o-k})}if(te.size&&h>=3){let m=t.filter(y=>!te.has(y.tool.name)),k=m.reduce((y,z)=>y+z.tokens,0);m.length&&C({id:"unused-tools",area:"tools",issue:`${m.length} of ${t.length} tools were never called in ${h} turns (${m.map(y=>y.tool.name).join(", ")}).`,fix:"Load them only for the tasks that need them, or merge them into fewer tools.",risk:"review",saves:k})}if(s.length>3||l>Math.max(1500,r)){let m=[...s].sort((k,y)=>k-y).slice(0,Math.min(3,s.length));C({id:"examples",area:"examples",issue:`${s.length} examples take ${w(l)} tokens (${ka(l,p)}% of each call).`,fix:"Keep the 2\u20133 most different examples, or shorten each to the part that shows the format.",risk:"review",saves:l-m.reduce((k,y)=>k+y,0)})}if(c.length){let m=new Map,k=0;c.forEach(({m:_,tokens:B},J)=>{let Me=_.content.trim();B>=200&&m.has(Me)?k+=B-10:m.set(Me,J)}),C({id:"duplicate-history",area:"history",issue:"The same large content appears more than once in the conversation.",fix:"Replace repeats with a pointer to the first copy.",risk:"safe",saves:k});let z=c.filter(_=>_.m.role==="tool").filter(_=>_.tokens>2e3);if(z.length){let _=z.reduce((B,J)=>B+J.tokens,0);C({id:"large-tool-results",area:"history",issue:`${z.length} tool results are over 2,000 tokens (${w(_)} in total, ${ka(_,p)}% of the context) and stay in every later call.`,fix:"Have tools return only the fields the agent uses, paginate, or replace old results with a short summary once used.",risk:"review",saves:Math.round(_*.7)})}if(d>i.contextWindow*.5||T!==null&&T<5||d>4e4){let _=c.slice(-6).reduce((B,J)=>B+J.tokens,0);C({id:"summarize-history",area:"history",issue:`History is ${w(d)} tokens and grows ~${w(f)} per turn${T!==null?`; about ${T} turns until the context is full`:""}.`,fix:"Summarize older turns into a short brief and keep the last few verbatim.",risk:"review",saves:Math.max(0,d-_-400)})}}if(e.prompt){let m=O(e.prompt,{profile:i,task:ie(e.prompt).task,level:"balanced"});C({id:"prompt-wording",area:"prompt",issue:`The next prompt is ${w(u)} tokens.`,fix:`Tighten its wording (${m.rules.join(", ")}); code and pasted material are untouched.`,risk:"safe",saves:m.saved})}let R=i.pricing?.longContext;if(R&&i.pricing&&p>=R.aboveTokens*.85){let m=p>R.aboveTokens,k=p*(R.inputPerMTok-i.pricing.inputPerMTok)/1e6,y=[...v].sort((z,_)=>_.tokens-z.tokens)[0];P.push({id:"price-threshold",area:y?.area??"history",issue:m?`This call is ${w(p)} tokens, over ${w(R.aboveTokens)}, so ${i.label} bills the whole request at $${R.inputPerMTok}/M input instead of $${i.pricing.inputPerMTok}/M (\u2248$${k.toFixed(4)} extra per call).`:`This call is ${w(p)} tokens, ${w(R.aboveTokens-p)} below ${w(R.aboveTokens)}; past that, ${i.label} bills the whole request at $${R.inputPerMTok}/M input instead of $${i.pricing.inputPerMTok}/M.`,fix:`The biggest part is ${y?.label??"the conversation"} (${w(y?.tokens??0)} tokens). Keeping the call under ${w(R.aboveTokens)} (cache or trim it, or start from a handoff brief) avoids the higher rate. Dropping content can change the answer, so check the output before you rely on it.`,risk:"review",saves:m?p-R.aboveTokens:0,usd:m?k:null})}return[e.system,e.prompt,...e.examples??[]].some(m=>m&&Ei.test(m))&&P.push({id:"structured-output",area:"output",issue:"The agent asks for JSON in words, so a reply that doesn't parse costs a retry: the whole call again.",fix:`Turn on the provider's strict JSON mode (setup below, schema ${Ci(e,i).fromExample?"taken from your example":"to fill in"}); every reply then matches the schema and no call is spent on a retry. The instructions stay as they are, so the content asked for is the same; check a few outputs once after switching.`,risk:"review",saves:0,usd:null}),P.sort((m,k)=>k.saves-m.saves),{profile:i,total:p,costPerCall:W(p,i),fixedOverhead:g,breakdown:v,growthPerTurn:f,turnsLeft:T,findings:P}}function Sa(e,a,i="balanced"){let n=me(e,a),r=n.profile,t=(u,g=ie(u).task)=>{let p=O(u,{profile:r,task:g,level:i}).text;return ze(u,p)?p:u},o=u=>Array.isArray(u)?u.map(o):!u||typeof u!="object"?u:Object.fromEntries(Object.entries(u).map(([g,p])=>[g,g==="description"&&typeof p=="string"?t(p,"chat"):o(p)])),s={...e};if(e.system&&(s.system=t(e.system,"chat")),e.tools&&(s.tools=e.tools.map(u=>({...u,description:u.description===void 0?void 0:t(u.description,"chat"),parameters:o(u.parameters)}))),e.messages){let u=new Map;s.messages=e.messages.map((g,p)=>{let v=g.content.trim(),h=u.get(v);return h!==void 0&&I(v,r).tokens>=200?{...g,content:`[Same content as message ${h+1} above.]`}:(u.set(v,p),g)})}e.prompt&&(s.prompt=t(e.prompt));let l=new Set(["system-wording","duplicate-history","prompt-wording"]);for(let u of n.findings)(l.has(u.id)||u.id.startsWith("tool-wording:"))&&(u.applied=!0);let c=ba(e,s);if(!c.passed){for(let u of n.findings)u.applied=!1;return{spec:e,report:n,before:n.total,after:n.total,savings:q(0,r),verification:c}}let d=me(s,r).total;return{spec:s,report:n,before:n.total,after:d,savings:q(n.total-d,r),verification:c}}function ji(e){let a={paths:[]};for(let i=0;i<e.length;i++){let n=e[i],r=()=>e[++i];n==="--model"?a.model=r():n==="--base"?a.base=r():n==="--out"?a.out=r():n==="--calls-per-month"?a.callsPerMonth=Number(r()):n==="--fail-above"?a.failAbove=Number(r()):a.paths.push(n)}return a}var Ie=(...e)=>zi("git",e,{encoding:"utf8",maxBuffer:64<<20,stdio:["ignore","pipe","pipe"]});function xi(){let e=process.env.GITHUB_BASE_REF??process.env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME;return e?`origin/${e}`:"HEAD~1"}function Ea(e){let a=e?.type==="function"&&e.function?e.function:e;return!a||typeof a.name!="string"?null:{name:a.name,description:a.description,parameters:a.parameters??a.input_schema??a.inputSchema}}function Aa(e,a){if(!e.endsWith(".json"))return{system:a};let i=JSON.parse(a);if(Array.isArray(i))return{tools:i.map(Ea).filter(t=>!!t)};let n=[i.system,i.instructions,i.system_prompt].map(t=>Array.isArray(t)?t.map(o=>o?.text??"").join(`
3294
+ `)),n=i===void 0?{type:"object",properties:{},additionalProperties:!0}:Pe(i),r=n.type==="object"?n:{type:"object",properties:{items:n},required:["items"],additionalProperties:!1},t=a.family==="anthropic"?{tools:[{name:"return_result",description:"Return the result.",input_schema:r,strict:!0}],tool_choice:{type:"tool",name:"return_result"}}:a.family==="google"?{generationConfig:{responseMimeType:"application/json",responseSchema:r}}:{response_format:{type:"json_schema",json_schema:{name:"result",strict:!0,schema:r}}};return{fromExample:i!==void 0,setup:t}}var ya=1024;function me(e,a){let i=typeof a=="string"?re(a):a,n=m=>m?I(m,i).tokens:0,r=n(e.system),t=(e.tools??[]).map(m=>({tool:m,tokens:n(fa(m))})),o=t.reduce((m,k)=>m+k.tokens,0),s=(e.examples??[]).map(n),l=s.reduce((m,k)=>m+k,0),c=(e.messages??[]).map(m=>({m,tokens:m.tokens??n(m.content)})),d=c.reduce((m,k)=>m+k.tokens,0),u=n(e.prompt),g=r+o+l,p=g+d+u,y=[{area:"system",label:"System prompt",tokens:r},{area:"tools",label:`Tool definitions (${t.length})`,tokens:o},{area:"examples",label:`Examples (${s.length})`,tokens:l},{area:"history",label:`Conversation (${c.length} messages)`,tokens:d},{area:"prompt",label:"Next prompt",tokens:u}].filter(m=>m.tokens>0),h=c.filter(m=>m.m.role==="user").length,f=h?Math.round(d/h):0,_=i.contextWindow-p,T=f>0?Math.max(0,Math.floor(_/f)):null,P=[],ge=new Set(["prompt-caching","volatile-prefix"]),C=m=>m.saves>0&&P.push({...m,usd:(ge.has(m.id)?ga(m.saves,i):q(m.saves,i)).usd});if(e.system){let m=O(e.system,{profile:i,task:"chat",level:"balanced"});C({id:"system-wording",area:"system",issue:`System prompt is ${S(r)} tokens and is re-sent on every call.`,fix:`Tighten its wording (${m.rules.join(", ")||"filler"}); meaning and constraints are kept.`,risk:"safe",saves:m.saved});let k=_i(e.system);k.moved.length&&g>=ya&&C({id:"volatile-prefix",area:"system",issue:`The system prompt has ${k.moved.length} line${k.moved.length>1?"s":""} that change between calls (${k.moved.map(w=>`"${w.trim().slice(0,40)}"`).join(", ")}) ahead of the rest, so the provider's discount for a repeated opening can't apply.`,fix:"Move those lines to the end of the system prompt (ready to paste below): same content, so the model reads the same facts, and everything before them is billed at the cached price.",risk:"safe",saves:Math.round(g*.9)})}g>=ya&&(i.family==="anthropic"||i.family==="openai"||i.family==="google")&&C({id:"prompt-caching",area:"system",issue:`${S(g)} tokens of system prompt, tools and examples are identical on every call.`,fix:i.family==="anthropic"?"Mark the end of the tools and system prompt with cache_control so repeat calls read it from cache at a fraction of the price. The saving shown is net of the cached-read price; each cache write (when the cache is cold) costs about 25% extra on those tokens.":i.family==="openai"?"Keep this prefix byte-identical and first in every request; OpenAI caches repeated prefixes automatically and bills them at a discount.":"Use context caching for this prefix so repeat calls don't pay full price for it. The saving shown is net of the cached price, before any cache storage charge.",risk:"safe",saves:Math.round(g*.9)});let te=new Set((e.messages??[]).flatMap(m=>m.toolCalls??[]));for(let{tool:m,tokens:k}of t){let z=[m.description??"",...wa(m.parameters)].filter(Boolean).reduce((E,B)=>E+O(B,{profile:i,task:"chat",level:"balanced"}).saved,0);C({id:`tool-wording:${m.name}`,area:"tools",issue:`Tool "${m.name}" definition is ${S(k)} tokens.`,fix:"Tighten the tool and parameter descriptions; names, types and constraints are kept.",risk:"safe",saves:z})}if(t.length>=yi||t.length>=3&&o>=wi){let k=vi(e.tools??[]).tools.reduce((w,z)=>w+n(fa(z)),0);C({id:"lazy-tools",area:"tools",issue:`${t.length} tool definitions (${S(o)} tokens) are sent in full on every call, whichever tools the call uses.`,fix:`Load tool definitions on demand: send a one-line catalog plus get_tool_schema and invoke_tool (${S(k)} tokens) and return each full definition only when the agent asks for it. Same tools and model; the first use of each tool in a run adds one short lookup.`,risk:"safe",saves:o-k})}if(te.size&&h>=3){let m=t.filter(w=>!te.has(w.tool.name)),k=m.reduce((w,z)=>w+z.tokens,0);m.length&&C({id:"unused-tools",area:"tools",issue:`${m.length} of ${t.length} tools were never called in ${h} turns (${m.map(w=>w.tool.name).join(", ")}).`,fix:"Load them only for the tasks that need them, or merge them into fewer tools.",risk:"review",saves:k})}if(s.length>3||l>Math.max(1500,r)){let m=[...s].sort((k,w)=>k-w).slice(0,Math.min(3,s.length));C({id:"examples",area:"examples",issue:`${s.length} examples take ${S(l)} tokens (${ka(l,p)}% of each call).`,fix:"Keep the 2\u20133 most different examples, or shorten each to the part that shows the format.",risk:"review",saves:l-m.reduce((k,w)=>k+w,0)})}if(c.length){let m=new Map,k=0;c.forEach(({m:E,tokens:B},J)=>{let Me=E.content.trim();B>=200&&m.has(Me)?k+=B-10:m.set(Me,J)}),C({id:"duplicate-history",area:"history",issue:"The same large content appears more than once in the conversation.",fix:"Replace repeats with a pointer to the first copy.",risk:"safe",saves:k});let z=c.filter(E=>E.m.role==="tool").filter(E=>E.tokens>2e3);if(z.length){let E=z.reduce((B,J)=>B+J.tokens,0);C({id:"large-tool-results",area:"history",issue:`${z.length} tool results are over 2,000 tokens (${S(E)} in total, ${ka(E,p)}% of the context) and stay in every later call.`,fix:"Have tools return only the fields the agent uses, paginate, or replace old results with a short summary once used.",risk:"review",saves:Math.round(E*.7)})}if(d>i.contextWindow*.5||T!==null&&T<5||d>4e4){let E=c.slice(-6).reduce((B,J)=>B+J.tokens,0);C({id:"summarize-history",area:"history",issue:`History is ${S(d)} tokens and grows ~${S(f)} per turn${T!==null?`; about ${T} turns until the context is full`:""}.`,fix:"Summarize older turns into a short brief and keep the last few verbatim.",risk:"review",saves:Math.max(0,d-E-400)})}}if(e.prompt){let m=O(e.prompt,{profile:i,task:ie(e.prompt).task,level:"balanced"});C({id:"prompt-wording",area:"prompt",issue:`The next prompt is ${S(u)} tokens.`,fix:`Tighten its wording (${m.rules.join(", ")}); code and pasted material are untouched.`,risk:"safe",saves:m.saved})}let R=i.pricing?.longContext;if(R&&i.pricing&&p>=R.aboveTokens*.85){let m=p>R.aboveTokens,k=p*(R.inputPerMTok-i.pricing.inputPerMTok)/1e6,w=[...y].sort((z,E)=>E.tokens-z.tokens)[0];P.push({id:"price-threshold",area:w?.area??"history",issue:m?`This call is ${S(p)} tokens, over ${S(R.aboveTokens)}, so ${i.label} bills the whole request at $${R.inputPerMTok}/M input instead of $${i.pricing.inputPerMTok}/M (\u2248$${k.toFixed(4)} extra per call).`:`This call is ${S(p)} tokens, ${S(R.aboveTokens-p)} below ${S(R.aboveTokens)}; past that, ${i.label} bills the whole request at $${R.inputPerMTok}/M input instead of $${i.pricing.inputPerMTok}/M.`,fix:`The biggest part is ${w?.label??"the conversation"} (${S(w?.tokens??0)} tokens). Keeping the call under ${S(R.aboveTokens)} (cache or trim it, or start from a handoff brief) avoids the higher rate. Dropping content can change the answer, so check the output before you rely on it.`,risk:"review",saves:m?p-R.aboveTokens:0,usd:m?k:null})}return[e.system,e.prompt,...e.examples??[]].some(m=>m&&Ei.test(m))&&P.push({id:"structured-output",area:"output",issue:"The agent asks for JSON in words, so a reply that doesn't parse costs a retry: the whole call again.",fix:`Turn on the provider's strict JSON mode (setup below, schema ${Ci(e,i).fromExample?"taken from your example":"to fill in"}); every reply then matches the schema and no call is spent on a retry. The instructions stay as they are, so the content asked for is the same; check a few outputs once after switching.`,risk:"review",saves:0,usd:null}),P.sort((m,k)=>k.saves-m.saves),{profile:i,total:p,costPerCall:W(p,i),fixedOverhead:g,breakdown:y,growthPerTurn:f,turnsLeft:T,findings:P}}function Sa(e,a,i="balanced"){let n=me(e,a),r=n.profile,t=(u,g=ie(u).task)=>{let p=O(u,{profile:r,task:g,level:i}).text;return ze(u,p)?p:u},o=u=>Array.isArray(u)?u.map(o):!u||typeof u!="object"?u:Object.fromEntries(Object.entries(u).map(([g,p])=>[g,g==="description"&&typeof p=="string"?t(p,"chat"):o(p)])),s={...e};if(e.system&&(s.system=t(e.system,"chat")),e.tools&&(s.tools=e.tools.map(u=>({...u,description:u.description===void 0?void 0:t(u.description,"chat"),parameters:o(u.parameters)}))),e.messages){let u=new Map;s.messages=e.messages.map((g,p)=>{let y=g.content.trim(),h=u.get(y);return h!==void 0&&I(y,r).tokens>=200?{...g,content:`[Same content as message ${h+1} above.]`}:(u.set(y,p),g)})}e.prompt&&(s.prompt=t(e.prompt));let l=new Set(["system-wording","duplicate-history","prompt-wording"]);for(let u of n.findings)(l.has(u.id)||u.id.startsWith("tool-wording:"))&&(u.applied=!0);let c=ba(e,s);if(!c.passed){for(let u of n.findings)u.applied=!1;return{spec:e,report:n,before:n.total,after:n.total,savings:q(0,r),verification:c}}let d=me(s,r).total;return{spec:s,report:n,before:n.total,after:d,savings:q(n.total-d,r),verification:c}}function ji(e){let a={paths:[]};for(let i=0;i<e.length;i++){let n=e[i],r=()=>e[++i];n==="--model"?a.model=r():n==="--base"?a.base=r():n==="--out"?a.out=r():n==="--calls-per-month"?a.callsPerMonth=Number(r()):n==="--fail-above"?a.failAbove=Number(r()):a.paths.push(n)}return a}var Ie=(...e)=>zi("git",e,{encoding:"utf8",maxBuffer:64<<20,stdio:["ignore","pipe","pipe"]});function xi(){let e=process.env.GITHUB_BASE_REF??process.env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME;return e?`origin/${e}`:"HEAD~1"}function Ea(e){let a=e?.type==="function"&&e.function?e.function:e;return!a||typeof a.name!="string"?null:{name:a.name,description:a.description,parameters:a.parameters??a.input_schema??a.inputSchema}}function Aa(e,a){if(!e.endsWith(".json"))return{system:a};let i=JSON.parse(a);if(Array.isArray(i))return{tools:i.map(Ea).filter(t=>!!t)};let n=[i.system,i.instructions,i.system_prompt].map(t=>Array.isArray(t)?t.map(o=>o?.text??"").join(`
3295
3295
  `):t).find(t=>typeof t=="string"),r=Array.isArray(i.tools)?i.tools.map(Ea).filter(t=>!!t):void 0;return{system:n,tools:r,examples:Array.isArray(i.examples)?i.examples.map(String):void 0,messages:Array.isArray(i.messages)?i.messages:void 0,prompt:typeof i.prompt=="string"?i.prompt:void 0}}var M=e=>e.toLocaleString("en-US"),Di=e=>e>0?`+${M(e)}`:M(e),pe=e=>e===null?"n/a":e>0?`+${K(e)}`:K(e),Ca=(e,a)=>e===null?null:e*a;function Ni(e,a,i,n){let r=d=>{if(d===null)return{tokens:0,usd:0};let u=me(Aa(e,d),n);return{tokens:u.total,usd:u.costPerCall}},t=r(a),o=r(i),s=0,l=0,c=[];if(i!==null){let d=Sa(Aa(e,i),n);s=d.savings.tokens,l=d.savings.usd;for(let u of d.report.findings.slice(0,3))u.saves>0&&c.push(`${u.issue} ${u.fix} (saves ${M(u.saves)} tokens${u.usd?`, ${K(u.usd)}`:""} per call${u.risk==="review"?", review first":""})`)}return{path:e,before:t.tokens,after:o.tokens,usdBefore:t.usd,usdAfter:o.usd,canSave:s,canSaveUsd:l,fixes:c}}function Li(e,a,i){let n=e.reduce((s,l)=>s+l.after-l.before,0),r=e.some(s=>s.usdAfter===null||s.usdBefore===null)?null:e.reduce((s,l)=>s+l.usdAfter-l.usdBefore,0),t=["### Token Lens: agent cost check","",`This change ${n>0?"adds":n<0?"removes":"keeps"} **${M(Math.abs(n))} input tokens per call** (${pe(r)} per call, ${pe(Ca(r,1e3))} per 1,000 calls`+(i?`, ${pe(Ca(r,i))} per month at ${M(i)} calls`:"")+`) on ${a.label}.`,"","| File | Before | After | Change | Per call | Same output, fewer tokens |","| --- | ---: | ---: | ---: | ---: | ---: |"];for(let s of e){let l=s.usdAfter===null||s.usdBefore===null?null:s.usdAfter-s.usdBefore;t.push(`| \`${s.path}\` | ${M(s.before)} | ${M(s.after)} | ${Di(s.after-s.before)} | ${pe(l)} | `+(s.canSave?`\u2212${M(s.canSave)} (${K(s.canSaveUsd)})`:"\u2013")+" |")}let o=e.filter(s=>s.fixes.length);if(o.length){t.push("","**Where to trim**","");for(let s of o)for(let l of s.fixes)t.push(`- \`${s.path}\`: ${l}`)}return t.push("",`<sub>Input tokens re-sent on every call (system prompt, tools, examples), counted for ${a.label} at ${K(a.pricing?a.pricing.inputPerMTok:null)} per 1M input tokens. "Same output" fixes keep every fact, number, constraint and tool interface. Computed in this CI job; nothing was sent anywhere. [Token Lens](https://www.npmjs.com/package/@kunal_dutt/token-lens)</sub>`),t.join(`
3296
3296
  `)+`
3297
3297
  `}function At(e){let a=ji(e);if(!a.model||!a.paths.length)return console.error("usage: token-lens check-agent --model <the model your agent uses> [--base <ref>] [--out file.md] <pathspec>..."),2;let i=re(a.model),n=a.base??xi(),r=Ie("diff","--name-only","--diff-filter=ACMRD",n,"--",...a.paths.map(c=>`:(glob)${c}`)).split(`