ghc-proxy 0.3.1 → 0.4.0

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.
@@ -0,0 +1,1134 @@
1
+ import { n as __exportAll } from "./main.mjs";
2
+
3
+ //#region node_modules/gpt-tokenizer/esm/constants.js
4
+ const ALL_SPECIAL_TOKENS = "all";
5
+ const DEFAULT_MERGE_CACHE_SIZE = 1e5;
6
+
7
+ //#endregion
8
+ //#region node_modules/gpt-tokenizer/esm/utfUtil.js
9
+ const isAscii = (codePoint) => codePoint <= 127;
10
+ const HIGH_SURROGATE_START = 55296;
11
+ const HIGH_SURROGATE_END = 56319;
12
+ function endsWithIncompleteUtfPairSurrogate(string) {
13
+ if (string.length === 0) return false;
14
+ const lastCharCode = string.charCodeAt(string.length - 1);
15
+ return lastCharCode >= HIGH_SURROGATE_START && lastCharCode <= HIGH_SURROGATE_END;
16
+ }
17
+ function isValidUTF8(bytes) {
18
+ let i = 0;
19
+ while (i < bytes.length) {
20
+ const byte1 = bytes[i];
21
+ let numBytes = 0;
22
+ let codePoint = 0;
23
+ if (byte1 <= 127) {
24
+ numBytes = 1;
25
+ codePoint = byte1;
26
+ } else if ((byte1 & 224) === 192) {
27
+ numBytes = 2;
28
+ codePoint = byte1 & 31;
29
+ if (byte1 <= 193) return false;
30
+ } else if ((byte1 & 240) === 224) {
31
+ numBytes = 3;
32
+ codePoint = byte1 & 15;
33
+ } else if ((byte1 & 248) === 240) {
34
+ numBytes = 4;
35
+ codePoint = byte1 & 7;
36
+ if (byte1 > 244) return false;
37
+ } else return false;
38
+ if (i + numBytes > bytes.length) return false;
39
+ for (let j = 1; j < numBytes; j++) {
40
+ const byte = bytes[i + j];
41
+ if (byte === void 0 || (byte & 192) !== 128) return false;
42
+ codePoint = codePoint << 6 | byte & 63;
43
+ }
44
+ if (numBytes === 2 && codePoint < 128) return false;
45
+ if (numBytes === 3 && codePoint < 2048) return false;
46
+ if (numBytes === 4 && codePoint < 65536) return false;
47
+ if (codePoint >= 55296 && codePoint <= 57343) return false;
48
+ if (codePoint > 1114111) return false;
49
+ i += numBytes;
50
+ }
51
+ return true;
52
+ }
53
+ const textDecoder = new TextDecoder("utf8", { fatal: false });
54
+ function tryConvertToString(arr) {
55
+ if (!isValidUTF8(arr)) return;
56
+ return textDecoder.decode(arr);
57
+ }
58
+ function compareUint8Arrays(a, b) {
59
+ const len = Math.min(a.length, b.length);
60
+ for (let i = 0; i < len; i++) if (a[i] !== b[i]) return a[i] - b[i];
61
+ return a.length - b.length;
62
+ }
63
+
64
+ //#endregion
65
+ //#region node_modules/gpt-tokenizer/esm/util.js
66
+ function getMaxValueFromMap(map) {
67
+ let max = 0;
68
+ map.forEach((val) => {
69
+ max = Math.max(max, val);
70
+ });
71
+ return max;
72
+ }
73
+ function escapeRegExp(string) {
74
+ return string.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&");
75
+ }
76
+ function getSpecialTokenRegex(tokens) {
77
+ const inner = [...tokens].map(escapeRegExp).join("|");
78
+ return new RegExp(`(${inner})`);
79
+ }
80
+
81
+ //#endregion
82
+ //#region node_modules/gpt-tokenizer/esm/BytePairEncodingCore.js
83
+ const emptyBuffer = new Uint8Array(0);
84
+ const decoder = new TextDecoder("utf8");
85
+ var BytePairEncodingCore = class {
86
+ mergeableBytePairRankCount;
87
+ /**
88
+ * an array where the index is the BPE rank,
89
+ * and the value is the string or the array of bytes that it decodes to
90
+ * it may contain holes if token is unused
91
+ */
92
+ bytePairRankDecoder;
93
+ bytePairNonUtfRankDecoder = /* @__PURE__ */ new Map();
94
+ bytePairNonUtfSortedEncoder;
95
+ /**
96
+ * a reverse map of the bytePairRankDecoder,
97
+ * where the key is the string and the value is the rank
98
+ * values that cannot be represented as a string are present in `bytePairNonUtfSortedEncoder`
99
+ */
100
+ bytePairStringRankEncoder;
101
+ tokenSplitRegex;
102
+ specialTokensEncoder;
103
+ specialTokensDecoder;
104
+ specialTokenPatternRegex;
105
+ textEncoder = new TextEncoder();
106
+ mergeCache;
107
+ mergeCacheSize;
108
+ constructor({ bytePairRankDecoder, specialTokensEncoder, tokenSplitRegex, mergeCacheSize = DEFAULT_MERGE_CACHE_SIZE }) {
109
+ this.bytePairRankDecoder = bytePairRankDecoder;
110
+ this.bytePairStringRankEncoder = /* @__PURE__ */ new Map();
111
+ this.mergeCacheSize = mergeCacheSize;
112
+ if (mergeCacheSize > 0) this.mergeCache = /* @__PURE__ */ new Map();
113
+ this.mergeableBytePairRankCount = Object.keys(bytePairRankDecoder).length;
114
+ const binaryLookup = [];
115
+ bytePairRankDecoder.forEach((value, rank) => {
116
+ if (typeof value === "string") {
117
+ this.bytePairStringRankEncoder.set(value, rank);
118
+ return;
119
+ }
120
+ const byteArray = new Uint8Array(value);
121
+ binaryLookup.push([byteArray, rank]);
122
+ this.bytePairNonUtfRankDecoder.set(rank, byteArray);
123
+ });
124
+ this.bytePairNonUtfSortedEncoder = binaryLookup.sort((a, b) => compareUint8Arrays(a[0], b[0]));
125
+ this.specialTokensEncoder = specialTokensEncoder ?? /* @__PURE__ */ new Map();
126
+ this.specialTokensDecoder = specialTokensEncoder ? new Map([...specialTokensEncoder].map(([key, value]) => [value, key])) : /* @__PURE__ */ new Map();
127
+ this.tokenSplitRegex = tokenSplitRegex;
128
+ const allSpecialTokensRegex = [...this.specialTokensEncoder.keys()].map(escapeRegExp).join("|");
129
+ try {
130
+ this.specialTokenPatternRegex = new RegExp(allSpecialTokensRegex, "y");
131
+ } catch {
132
+ throw new Error("Invalid regular expression pattern.");
133
+ }
134
+ }
135
+ setMergeCacheSize(newSize) {
136
+ if (this.mergeCacheSize === 0 && newSize > 0) this.mergeCache = /* @__PURE__ */ new Map();
137
+ this.mergeCacheSize = newSize;
138
+ if (newSize === 0) this.mergeCache = void 0;
139
+ }
140
+ clearMergeCache() {
141
+ this.mergeCache?.clear();
142
+ }
143
+ *encodeNativeGenerator(text, allowedSpecial) {
144
+ let startIndex = 0;
145
+ let lastTokenLength = 0;
146
+ while (true) {
147
+ const nextSpecialMatch = this.findNextSpecialToken(text, allowedSpecial, startIndex);
148
+ const nextSpecialStartIndex = nextSpecialMatch?.[0];
149
+ const endIndex = nextSpecialStartIndex ?? text.length;
150
+ const textBeforeSpecial = startIndex === 0 && endIndex === text.length ? text : text.slice(startIndex, endIndex);
151
+ for (const [match] of textBeforeSpecial.matchAll(this.tokenSplitRegex)) {
152
+ const token = this.getBpeRankFromString(match);
153
+ if (token !== void 0) {
154
+ lastTokenLength = 1;
155
+ yield [token];
156
+ continue;
157
+ }
158
+ const tokens = this.bytePairEncode(match);
159
+ lastTokenLength = tokens.length;
160
+ yield tokens;
161
+ }
162
+ if (nextSpecialStartIndex !== void 0) {
163
+ const specialToken = nextSpecialMatch[1];
164
+ const specialTokenValue = this.specialTokensEncoder.get(specialToken);
165
+ if (specialTokenValue === void 0) throw new Error(`Special token "${specialToken}" is not in the special token encoder.`);
166
+ yield [specialTokenValue];
167
+ startIndex = nextSpecialStartIndex + specialToken.length;
168
+ lastTokenLength = 1;
169
+ } else break;
170
+ }
171
+ return lastTokenLength;
172
+ }
173
+ encodeNative(text, allowedSpecial) {
174
+ let startIndex = 0;
175
+ const tokensArray = [];
176
+ while (true) {
177
+ const nextSpecialMatch = this.findNextSpecialToken(text, allowedSpecial, startIndex);
178
+ const nextSpecialStartIndex = nextSpecialMatch?.[0];
179
+ const endIndex = nextSpecialStartIndex ?? text.length;
180
+ const textBeforeSpecial = startIndex === 0 && endIndex === text.length ? text : text.slice(startIndex, endIndex);
181
+ for (const [match] of textBeforeSpecial.matchAll(this.tokenSplitRegex)) {
182
+ const token = this.getBpeRankFromString(match);
183
+ if (token !== void 0) {
184
+ tokensArray.push(token);
185
+ continue;
186
+ }
187
+ const tokens = this.bytePairEncode(match);
188
+ tokensArray.push(...tokens);
189
+ }
190
+ if (nextSpecialStartIndex !== void 0) {
191
+ const specialToken = nextSpecialMatch[1];
192
+ const specialTokenValue = this.specialTokensEncoder.get(specialToken);
193
+ if (specialTokenValue === void 0) throw new Error(`Special token "${specialToken}" is not in the special token encoder.`);
194
+ tokensArray.push(specialTokenValue);
195
+ startIndex = nextSpecialStartIndex + specialToken.length;
196
+ } else break;
197
+ }
198
+ return tokensArray;
199
+ }
200
+ countNative(text, allowedSpecial) {
201
+ let startIndex = 0;
202
+ let tokensCount = 0;
203
+ while (true) {
204
+ const nextSpecialMatch = this.findNextSpecialToken(text, allowedSpecial, startIndex);
205
+ const nextSpecialStartIndex = nextSpecialMatch?.[0];
206
+ const endIndex = nextSpecialStartIndex ?? text.length;
207
+ const textBeforeSpecial = startIndex === 0 && endIndex === text.length ? text : text.slice(startIndex, endIndex);
208
+ for (const [match] of textBeforeSpecial.matchAll(this.tokenSplitRegex)) {
209
+ if (this.getBpeRankFromString(match) !== void 0) {
210
+ tokensCount++;
211
+ continue;
212
+ }
213
+ const tokens = this.bytePairEncode(match);
214
+ tokensCount += tokens.length;
215
+ }
216
+ if (nextSpecialStartIndex !== void 0) {
217
+ const specialToken = nextSpecialMatch[1];
218
+ if (this.specialTokensEncoder.get(specialToken) === void 0) throw new Error(`Special token "${specialToken}" is not in the special token encoder.`);
219
+ tokensCount++;
220
+ startIndex = nextSpecialStartIndex + specialToken.length;
221
+ } else break;
222
+ }
223
+ return tokensCount;
224
+ }
225
+ *decodeNativeGenerator(tokens) {
226
+ for (const token of tokens) {
227
+ const tokenBytes = this.tryDecodeToken(token);
228
+ if (tokenBytes) yield tokenBytes;
229
+ }
230
+ }
231
+ decodeNative(tokens) {
232
+ let decoded = "";
233
+ let intBuffer = emptyBuffer;
234
+ for (const token of tokens) {
235
+ const tokenBytes = this.tryDecodeToken(token);
236
+ if (tokenBytes === void 0) throw new Error(`Token ${token} is not in the byte pair encoder.`);
237
+ if (typeof tokenBytes === "string") {
238
+ if (intBuffer !== emptyBuffer) {
239
+ decoded += decoder.decode(intBuffer, { stream: true });
240
+ intBuffer = emptyBuffer;
241
+ }
242
+ decoded += tokenBytes;
243
+ } else {
244
+ const newBuffer = new Uint8Array(intBuffer.length + tokenBytes.length);
245
+ newBuffer.set(intBuffer);
246
+ newBuffer.set(tokenBytes, intBuffer.length);
247
+ intBuffer = newBuffer;
248
+ }
249
+ }
250
+ if (intBuffer !== emptyBuffer) decoded += decoder.decode(intBuffer, { stream: true });
251
+ return decoded;
252
+ }
253
+ async *decodeNativeAsyncIterable(tokens) {
254
+ for await (const token of tokens) {
255
+ const tokenBytesOrString = this.tryDecodeToken(token);
256
+ if (tokenBytesOrString) yield tokenBytesOrString;
257
+ }
258
+ }
259
+ getBpeRankFromString(key) {
260
+ return this.bytePairStringRankEncoder.get(key);
261
+ }
262
+ getBpeRankFromStringOrThrow(key) {
263
+ const value = this.getBpeRankFromString(key);
264
+ if (value === void 0) throw new Error(`The byte-pair encoding does not contain a value for: ${key}`);
265
+ return value;
266
+ }
267
+ getBpeRankFromBytes(key) {
268
+ const keyAsString = tryConvertToString(key);
269
+ if (keyAsString !== void 0) return this.getBpeRankFromString(keyAsString);
270
+ const index = this.binarySearch(key);
271
+ if (index !== -1) return this.bytePairNonUtfSortedEncoder[index][1];
272
+ }
273
+ getBpeRankFromBytesOrThrow(key) {
274
+ const value = this.getBpeRankFromBytes(key);
275
+ if (value === void 0) throw new Error(`The byte-pair encoding does not contain a value for: ${key.toString()}`);
276
+ return value;
277
+ }
278
+ binarySearch(key) {
279
+ let low = 0;
280
+ let high = this.bytePairNonUtfSortedEncoder.length - 1;
281
+ while (low <= high) {
282
+ const mid = low + high >>> 1;
283
+ const midKey = this.bytePairNonUtfSortedEncoder[mid][0];
284
+ let cmp = 0;
285
+ const maxLength = Math.min(midKey.length, key.length);
286
+ for (let i = 0; i < maxLength; i++) {
287
+ cmp = midKey[i] - key[i];
288
+ if (cmp !== 0) break;
289
+ }
290
+ if (cmp === 0) cmp = midKey.length - key.length;
291
+ if (cmp === 0) return mid;
292
+ if (cmp < 0) low = mid + 1;
293
+ else high = mid - 1;
294
+ }
295
+ return -1;
296
+ }
297
+ findNextSpecialToken(text, allowedSpecial, startIndex) {
298
+ let searchIndex = startIndex;
299
+ while (true) {
300
+ this.specialTokenPatternRegex.lastIndex = searchIndex;
301
+ const nextSpecialMatch = this.specialTokenPatternRegex.exec(text);
302
+ if (!nextSpecialMatch) return;
303
+ const specialToken = nextSpecialMatch[0];
304
+ if (allowedSpecial?.has(specialToken)) return [nextSpecialMatch.index + searchIndex, specialToken];
305
+ searchIndex = nextSpecialMatch.index + searchIndex + 1;
306
+ }
307
+ }
308
+ tryDecodeToken(tokenRank) {
309
+ const value = this.bytePairRankDecoder[tokenRank];
310
+ if (typeof value === "string") return value;
311
+ if (typeof value === "object") {
312
+ const fromBinary = this.bytePairNonUtfRankDecoder.get(tokenRank);
313
+ if (fromBinary) return fromBinary;
314
+ }
315
+ return this.specialTokensDecoder.get(tokenRank);
316
+ }
317
+ addToMergeCache(key, value) {
318
+ if (!this.mergeCache) return;
319
+ if (this.mergeCache.size >= this.mergeCacheSize) {
320
+ const firstKey = this.mergeCache.keys().next().value;
321
+ this.mergeCache.delete(firstKey);
322
+ }
323
+ this.mergeCache.set(key, value);
324
+ }
325
+ bytePairEncode(input) {
326
+ if (input.length === 1 && isAscii(input.codePointAt(0))) return [this.getBpeRankFromStringOrThrow(input)];
327
+ if (this.mergeCache?.has(input)) {
328
+ const result = this.mergeCache.get(input);
329
+ this.mergeCache.delete(input);
330
+ this.mergeCache.set(input, result);
331
+ return result;
332
+ }
333
+ const inputBytes = this.textEncoder.encode(input);
334
+ const result = this.bytePairMerge(inputBytes);
335
+ this.addToMergeCache(input, result);
336
+ return result;
337
+ }
338
+ bytePairMerge(piece) {
339
+ const starts = [];
340
+ const ranks = [];
341
+ const getRank = (startIndex, pairStart = starts[startIndex], pairEnd = starts[startIndex + 2]) => {
342
+ if (pairEnd === void 0) return Number.POSITIVE_INFINITY;
343
+ const key = piece.subarray(pairStart, pairEnd);
344
+ return this.getBpeRankFromBytes(key) ?? Number.POSITIVE_INFINITY;
345
+ };
346
+ for (let i = 0; i <= piece.length; i++) {
347
+ starts.push(i);
348
+ if (i < piece.length - 1) ranks.push(getRank(i, i, i + 2));
349
+ else ranks.push(Number.POSITIVE_INFINITY);
350
+ }
351
+ while (starts.length > 1) {
352
+ let lowestRank = Number.POSITIVE_INFINITY;
353
+ let lowestPartitionIndex = -1;
354
+ for (let i = 0; i < ranks.length - 1; i++) {
355
+ const rank = ranks[i];
356
+ if (rank < lowestRank) {
357
+ lowestRank = rank;
358
+ lowestPartitionIndex = i;
359
+ }
360
+ }
361
+ if (lowestRank === Number.POSITIVE_INFINITY || lowestPartitionIndex === -1) break;
362
+ starts.splice(lowestPartitionIndex + 1, 1);
363
+ ranks.splice(lowestPartitionIndex, 1);
364
+ ranks[lowestPartitionIndex] = getRank(lowestPartitionIndex);
365
+ if (lowestPartitionIndex > 0) ranks[lowestPartitionIndex - 1] = getRank(lowestPartitionIndex - 1);
366
+ }
367
+ const output = [];
368
+ for (let i = 0; i < starts.length - 1; i++) {
369
+ const pairStart = starts[i];
370
+ const pairEnd = starts[i + 1];
371
+ const bpeValue = this.getBpeRankFromBytesOrThrow(piece.subarray(pairStart, pairEnd));
372
+ output.push(bpeValue);
373
+ }
374
+ return output;
375
+ }
376
+ };
377
+
378
+ //#endregion
379
+ //#region node_modules/gpt-tokenizer/esm/functionCalling.js
380
+ const MESSAGE_TOKEN_OVERHEAD = 3;
381
+ const MESSAGE_NAME_TOKEN_OVERHEAD = 1;
382
+ const FUNCTION_ROLE_TOKEN_DISCOUNT = 2;
383
+ const FUNCTION_CALL_METADATA_TOKEN_OVERHEAD = 3;
384
+ const FUNCTION_DEFINITION_TOKEN_OVERHEAD = 9;
385
+ const COMPLETION_REQUEST_TOKEN_OVERHEAD = 3;
386
+ const FUNCTION_CALL_NAME_TOKEN_OVERHEAD = 4;
387
+ const FUNCTION_CALL_NONE_TOKEN_OVERHEAD = 1;
388
+ const SYSTEM_FUNCTION_TOKEN_DEDUCTION = 4;
389
+ const NEWLINE = "\n";
390
+ function countMessageTokens(message, countStringTokens) {
391
+ let tokens = 0;
392
+ if (message.role) tokens += countStringTokens(message.role);
393
+ if (message.content) tokens += countStringTokens(message.content);
394
+ if (message.name) tokens += countStringTokens(message.name) + MESSAGE_NAME_TOKEN_OVERHEAD;
395
+ if (message.function_call) {
396
+ const { name, arguments: args } = message.function_call;
397
+ if (name) tokens += countStringTokens(name);
398
+ if (args) tokens += countStringTokens(args);
399
+ tokens += FUNCTION_CALL_METADATA_TOKEN_OVERHEAD;
400
+ }
401
+ tokens += MESSAGE_TOKEN_OVERHEAD;
402
+ if (message.role === "function") tokens -= FUNCTION_ROLE_TOKEN_DISCOUNT;
403
+ return tokens;
404
+ }
405
+ function formatObjectProperties(obj, indent, formatType) {
406
+ if (!obj.properties) return "";
407
+ const lines = [];
408
+ const requiredParams = new Set(obj.required ?? []);
409
+ const indentString = " ".repeat(indent);
410
+ for (const [name, param] of Object.entries(obj.properties)) {
411
+ if (param.description && indent < 2) lines.push(`${indentString}// ${param.description}`);
412
+ const isRequired = requiredParams.has(name);
413
+ const formattedType = formatType(param, indent);
414
+ lines.push(`${indentString}${name}${isRequired ? "" : "?"}: ${formattedType},`);
415
+ }
416
+ return lines.join("\n");
417
+ }
418
+ function formatFunctionType(param, indent) {
419
+ switch (param.type) {
420
+ case "string": return param.enum?.map((value) => JSON.stringify(value)).join(" | ") ?? "string";
421
+ case "integer":
422
+ case "number": return param.enum?.map((value) => `${value}`).join(" | ") ?? "number";
423
+ case "boolean": return "boolean";
424
+ case "null": return "null";
425
+ case "array": return param.items ? `${formatFunctionType(param.items, indent)}[]` : "any[]";
426
+ case "object": return `{
427
+ ${formatObjectProperties(param, indent + 2, formatFunctionType)}
428
+ ${" ".repeat(indent)}}`;
429
+ default: return "any";
430
+ }
431
+ }
432
+ function formatFunctionDefinitions(functions) {
433
+ const lines = ["namespace functions {", ""];
434
+ for (const fn of functions) {
435
+ if (fn.description) lines.push(`// ${fn.description}`);
436
+ const { parameters } = fn;
437
+ const properties = parameters?.properties;
438
+ if (!parameters || !properties || Object.keys(properties).length === 0) lines.push(`type ${fn.name} = () => any;`);
439
+ else {
440
+ lines.push(`type ${fn.name} = (_: {`);
441
+ const formattedProperties = formatObjectProperties(parameters, 0, formatFunctionType);
442
+ if (formattedProperties.length > 0) lines.push(formattedProperties);
443
+ lines.push("}) => any;");
444
+ }
445
+ lines.push("");
446
+ }
447
+ lines.push("} // namespace functions");
448
+ return lines.join("\n");
449
+ }
450
+ function estimateTokensInFunctions(functions, countStringTokens) {
451
+ let tokens = countStringTokens(formatFunctionDefinitions(functions));
452
+ tokens += FUNCTION_DEFINITION_TOKEN_OVERHEAD;
453
+ return tokens;
454
+ }
455
+ function padSystemMessage(message, hasFunctions, isSystemPadded) {
456
+ if (!hasFunctions || isSystemPadded || message.role !== "system") return message;
457
+ if (!message.content || message.content.endsWith(NEWLINE)) return message;
458
+ return {
459
+ ...message,
460
+ content: `${message.content}${NEWLINE}`
461
+ };
462
+ }
463
+ function computeChatCompletionTokenCount(request, countStringTokens) {
464
+ const { messages, functions, function_call: functionCall } = request;
465
+ const hasFunctions = Boolean(functions && functions.length > 0);
466
+ let paddedSystem = false;
467
+ let total = 0;
468
+ for (const message of messages) {
469
+ const messageToCount = padSystemMessage(message, hasFunctions, paddedSystem);
470
+ if (messageToCount !== message && message.role === "system") paddedSystem = true;
471
+ else if (message.role === "system" && hasFunctions && !paddedSystem) paddedSystem = true;
472
+ total += countMessageTokens(messageToCount, countStringTokens);
473
+ }
474
+ total += COMPLETION_REQUEST_TOKEN_OVERHEAD;
475
+ if (hasFunctions && functions) {
476
+ total += estimateTokensInFunctions(functions, countStringTokens);
477
+ if (messages.some((message) => message.role === "system")) total -= SYSTEM_FUNCTION_TOKEN_DEDUCTION;
478
+ }
479
+ if (functionCall && functionCall !== "auto") {
480
+ if (functionCall === "none") total += FUNCTION_CALL_NONE_TOKEN_OVERHEAD;
481
+ else if (typeof functionCall === "object" && functionCall.name) total += countStringTokens(functionCall.name) + FUNCTION_CALL_NAME_TOKEN_OVERHEAD;
482
+ }
483
+ return total;
484
+ }
485
+
486
+ //#endregion
487
+ //#region node_modules/gpt-tokenizer/esm/modelsChatEnabled.gen.js
488
+ const chatEnabledModels = [
489
+ "chatgpt-4o-latest",
490
+ "codex-mini-latest",
491
+ "computer-use-preview",
492
+ "computer-use-preview-2025-03-11",
493
+ "gpt-3.5",
494
+ "gpt-3.5-0301",
495
+ "gpt-3.5-turbo",
496
+ "gpt-3.5-turbo-0125",
497
+ "gpt-3.5-turbo-0613",
498
+ "gpt-3.5-turbo-1106",
499
+ "gpt-3.5-turbo-16k-0613",
500
+ "gpt-3.5-turbo-instruct",
501
+ "gpt-4",
502
+ "gpt-4-0125-preview",
503
+ "gpt-4-0314",
504
+ "gpt-4-0613",
505
+ "gpt-4-1106-preview",
506
+ "gpt-4-1106-vision-preview",
507
+ "gpt-4-32k",
508
+ "gpt-4-turbo",
509
+ "gpt-4-turbo-2024-04-09",
510
+ "gpt-4-turbo-preview",
511
+ "gpt-4.1",
512
+ "gpt-4.1-2025-04-14",
513
+ "gpt-4.1-mini",
514
+ "gpt-4.1-mini-2025-04-14",
515
+ "gpt-4.1-nano",
516
+ "gpt-4.1-nano-2025-04-14",
517
+ "gpt-4.5-preview",
518
+ "gpt-4.5-preview-2025-02-27",
519
+ "gpt-4o",
520
+ "gpt-4o-2024-05-13",
521
+ "gpt-4o-2024-08-06",
522
+ "gpt-4o-2024-11-20",
523
+ "gpt-4o-audio-preview",
524
+ "gpt-4o-audio-preview-2024-10-01",
525
+ "gpt-4o-audio-preview-2024-12-17",
526
+ "gpt-4o-audio-preview-2025-06-03",
527
+ "gpt-4o-mini",
528
+ "gpt-4o-mini-2024-07-18",
529
+ "gpt-4o-mini-audio-preview",
530
+ "gpt-4o-mini-audio-preview-2024-12-17",
531
+ "gpt-4o-mini-search-preview",
532
+ "gpt-4o-mini-search-preview-2025-03-11",
533
+ "gpt-4o-search-preview",
534
+ "gpt-4o-search-preview-2025-03-11",
535
+ "gpt-5",
536
+ "gpt-5-2025-08-07",
537
+ "gpt-5-chat-latest",
538
+ "gpt-5-codex",
539
+ "gpt-5-mini",
540
+ "gpt-5-mini-2025-08-07",
541
+ "gpt-5-nano",
542
+ "gpt-5-nano-2025-08-07",
543
+ "gpt-5-pro",
544
+ "gpt-5-pro-2025-10-06",
545
+ "gpt-audio",
546
+ "gpt-audio-2025-08-28",
547
+ "gpt-audio-mini",
548
+ "gpt-audio-mini-2025-10-06",
549
+ "gpt-oss-120b",
550
+ "gpt-oss-20b",
551
+ "o1",
552
+ "o1-2024-12-17",
553
+ "o1-mini",
554
+ "o1-mini-2024-09-12",
555
+ "o1-preview",
556
+ "o1-preview-2024-09-12",
557
+ "o1-pro",
558
+ "o1-pro-2025-03-19",
559
+ "o3",
560
+ "o3-2025-04-16",
561
+ "o3-deep-research",
562
+ "o3-deep-research-2025-06-26",
563
+ "o3-mini",
564
+ "o3-mini-2025-01-31",
565
+ "o3-pro",
566
+ "o3-pro-2025-06-10",
567
+ "o4-mini",
568
+ "o4-mini-2025-04-16",
569
+ "o4-mini-deep-research",
570
+ "o4-mini-deep-research-2025-06-26"
571
+ ];
572
+
573
+ //#endregion
574
+ //#region node_modules/gpt-tokenizer/esm/modelsMap.js
575
+ var modelsMap_exports = /* @__PURE__ */ __exportAll({
576
+ cl100k_base: () => cl100k_base,
577
+ o200k_base: () => o200k_base$1,
578
+ o200k_harmony: () => o200k_harmony,
579
+ p50k_base: () => p50k_base,
580
+ p50k_edit: () => p50k_edit,
581
+ r50k_base: () => r50k_base
582
+ });
583
+ const p50k_base = [
584
+ "text-davinci-002",
585
+ "text-davinci-003",
586
+ "code-davinci-001",
587
+ "code-davinci-002",
588
+ "davinci-codex",
589
+ "code-cushman-001",
590
+ "code-cushman-002",
591
+ "cushman-codex"
592
+ ];
593
+ const r50k_base = [
594
+ "text-ada-001",
595
+ "text-babbage-001",
596
+ "text-curie-001",
597
+ "text-davinci-001",
598
+ "ada",
599
+ "babbage",
600
+ "curie",
601
+ "davinci",
602
+ "code-search-ada-code-001",
603
+ "code-search-ada-text-001",
604
+ "text-similarity-ada-001",
605
+ "text-search-ada-doc-001",
606
+ "text-search-ada-query-001",
607
+ "text-similarity-babbage-001",
608
+ "text-search-babbage-doc-001",
609
+ "text-search-babbage-query-001",
610
+ "code-search-babbage-code-001",
611
+ "code-search-babbage-text-001",
612
+ "text-similarity-curie-001",
613
+ "text-search-curie-doc-001",
614
+ "text-search-curie-query-001",
615
+ "text-similarity-davinci-001",
616
+ "text-search-davinci-doc-001",
617
+ "text-search-davinci-query-001"
618
+ ];
619
+ const p50k_edit = ["code-davinci-edit-001", "text-davinci-edit-001"];
620
+ const cl100k_base = [
621
+ "gpt-3.5",
622
+ "gpt-3.5-0301",
623
+ "gpt-3.5-turbo",
624
+ "gpt-3.5-turbo-0125",
625
+ "gpt-3.5-turbo-0613",
626
+ "gpt-3.5-turbo-1106",
627
+ "gpt-3.5-turbo-16k-0613",
628
+ "gpt-3.5-turbo-instruct",
629
+ "gpt-4",
630
+ "gpt-4-0125-preview",
631
+ "gpt-4-0314",
632
+ "gpt-4-0613",
633
+ "gpt-4-1106-preview",
634
+ "gpt-4-1106-vision-preview",
635
+ "gpt-4-32k",
636
+ "gpt-4-turbo",
637
+ "gpt-4-turbo-2024-04-09",
638
+ "gpt-4-turbo-preview",
639
+ "text-embedding-3-large",
640
+ "text-embedding-3-small",
641
+ "text-embedding-ada-002",
642
+ "babbage-002",
643
+ "davinci-002"
644
+ ];
645
+ const o200k_base$1 = [];
646
+ const o200k_harmony = ["gpt-oss-20b", "gpt-oss-120b"];
647
+
648
+ //#endregion
649
+ //#region node_modules/gpt-tokenizer/esm/specialTokens.js
650
+ const EndOfText = "<|endoftext|>";
651
+ const FimPrefix = "<|fim_prefix|>";
652
+ const FimMiddle = "<|fim_middle|>";
653
+ const FimSuffix = "<|fim_suffix|>";
654
+ const ImStart = "<|im_start|>";
655
+ const ImEnd = "<|im_end|>";
656
+ const ImSep = "<|im_sep|>";
657
+ const EndOfPrompt = "<|endofprompt|>";
658
+ const HarmonyStartOfText = "<|startoftext|>";
659
+ const HarmonyStart = "<|start|>";
660
+ const HarmonyEnd = "<|end|>";
661
+ const HarmonyMessage = "<|message|>";
662
+ const HarmonyChannel = "<|channel|>";
663
+ const HarmonyReturn = "<|return|>";
664
+ const HarmonyConstrain = "<|constrain|>";
665
+ const HarmonyCall = "<|call|>";
666
+
667
+ //#endregion
668
+ //#region node_modules/gpt-tokenizer/esm/mapping.js
669
+ const o200k_base = "o200k_base";
670
+ const DEFAULT_ENCODING = o200k_base;
671
+ /**
672
+ * maps model names to encoding names
673
+ * if a model is not listed, it uses the default encoding for new models
674
+ * which is `o200k_base`
675
+ */
676
+ const modelToEncodingMap = Object.fromEntries(Object.entries(modelsMap_exports).flatMap(([encodingName, models]) => models.map((modelName) => [modelName, encodingName])));
677
+ const gpt3params = {
678
+ messageSeparator: "\n",
679
+ roleSeparator: "\n"
680
+ };
681
+ const gpt4params = {
682
+ messageSeparator: "",
683
+ roleSeparator: ImSep
684
+ };
685
+ const chatModelParams = Object.fromEntries(chatEnabledModels.flatMap((modelName) => modelName.startsWith("gpt-3.5") ? [[modelName, gpt3params]] : [[modelName, gpt4params]]));
686
+
687
+ //#endregion
688
+ //#region node_modules/gpt-tokenizer/esm/encodingParams/constants.js
689
+ const R50K_TOKEN_SPLIT_REGEX = /'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu;
690
+ const CONTRACTION_SUFFIX_PATTERN = String.raw`'(?:[sS]|[dD]|[mM]|[tT]|[lL][lL]|[vV][eE]|[rR][eE])`;
691
+ const OPTIONAL_CONTRACTION_SUFFIX = String.raw`(?:${CONTRACTION_SUFFIX_PATTERN})?`;
692
+ const CL100K_TOKEN_SPLIT_PATTERN = String.raw`${CONTRACTION_SUFFIX_PATTERN}|[^\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`;
693
+ const CL100K_TOKEN_SPLIT_REGEX = new RegExp(CL100K_TOKEN_SPLIT_PATTERN, "gu");
694
+ const O200K_TOKEN_SPLIT_PATTERN = 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}]+${OPTIONAL_CONTRACTION_SUFFIX}|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*${OPTIONAL_CONTRACTION_SUFFIX}|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`;
695
+ const O200K_TOKEN_SPLIT_REGEX = new RegExp(O200K_TOKEN_SPLIT_PATTERN, "gu");
696
+
697
+ //#endregion
698
+ //#region node_modules/gpt-tokenizer/esm/encodingParams/cl100k_base.js
699
+ function Cl100KBase(bytePairRankDecoder) {
700
+ return {
701
+ tokenSplitRegex: CL100K_TOKEN_SPLIT_REGEX,
702
+ bytePairRankDecoder,
703
+ specialTokensEncoder: new Map([
704
+ [EndOfText, 100257],
705
+ [FimPrefix, 100258],
706
+ [FimMiddle, 100259],
707
+ [FimSuffix, 100260],
708
+ [ImStart, 100264],
709
+ [ImEnd, 100265],
710
+ [ImSep, 100266],
711
+ [EndOfPrompt, 100276]
712
+ ])
713
+ };
714
+ }
715
+
716
+ //#endregion
717
+ //#region node_modules/gpt-tokenizer/esm/encodingParams/o200k_base.js
718
+ const O200K_BASE_SPECIAL_TOKEN_ENTRIES = [
719
+ [EndOfText, 199999],
720
+ [FimPrefix, 2e5],
721
+ [FimMiddle, 200001],
722
+ [FimSuffix, 200002],
723
+ [ImStart, 200003],
724
+ [ImEnd, 200004],
725
+ [ImSep, 200005],
726
+ [EndOfPrompt, 200006]
727
+ ];
728
+ const createO200KSpecialTokenMap = () => new Map(O200K_BASE_SPECIAL_TOKEN_ENTRIES);
729
+ function O200KBase(bytePairRankDecoder) {
730
+ return {
731
+ tokenSplitRegex: O200K_TOKEN_SPLIT_REGEX,
732
+ bytePairRankDecoder,
733
+ specialTokensEncoder: createO200KSpecialTokenMap()
734
+ };
735
+ }
736
+
737
+ //#endregion
738
+ //#region node_modules/gpt-tokenizer/esm/encodingParams/o200k_harmony.js
739
+ const RESERVED_TOKEN_RANGE_START = 200013;
740
+ const RESERVED_TOKEN_RANGE_END = 201088;
741
+ const STATIC_SPECIAL_TOKEN_ENTRIES = [
742
+ [HarmonyStartOfText, 199998],
743
+ [EndOfText, 199999],
744
+ ["<|reserved_200000|>", 2e5],
745
+ ["<|reserved_200001|>", 200001],
746
+ [HarmonyReturn, 200002],
747
+ [HarmonyConstrain, 200003],
748
+ ["<|reserved_200004|>", 200004],
749
+ [HarmonyChannel, 200005],
750
+ [HarmonyStart, 200006],
751
+ [HarmonyEnd, 200007],
752
+ [HarmonyMessage, 200008],
753
+ ["<|reserved_200009|>", 200009],
754
+ ["<|reserved_200010|>", 200010],
755
+ ["<|reserved_200011|>", 200011],
756
+ [HarmonyCall, 200012]
757
+ ];
758
+ function O200KHarmony(bytePairRankDecoder) {
759
+ const specialTokensEncoder = new Map(STATIC_SPECIAL_TOKEN_ENTRIES);
760
+ for (let tokenId = RESERVED_TOKEN_RANGE_START; tokenId < RESERVED_TOKEN_RANGE_END; tokenId += 1) specialTokensEncoder.set(`<|reserved_${tokenId}|>`, tokenId);
761
+ specialTokensEncoder.set(EndOfPrompt, 200018);
762
+ return {
763
+ tokenSplitRegex: O200K_TOKEN_SPLIT_REGEX,
764
+ bytePairRankDecoder,
765
+ specialTokensEncoder,
766
+ chatFormatter: "harmony"
767
+ };
768
+ }
769
+
770
+ //#endregion
771
+ //#region node_modules/gpt-tokenizer/esm/encodingParams/p50k_base.js
772
+ function P50KBase(bytePairRankDecoder) {
773
+ return {
774
+ expectedVocabularySize: 50281,
775
+ tokenSplitRegex: R50K_TOKEN_SPLIT_REGEX,
776
+ bytePairRankDecoder,
777
+ specialTokensEncoder: new Map([[EndOfText, 50256]])
778
+ };
779
+ }
780
+
781
+ //#endregion
782
+ //#region node_modules/gpt-tokenizer/esm/encodingParams/p50k_edit.js
783
+ function P50KEdit(bytePairRankDecoder) {
784
+ return {
785
+ tokenSplitRegex: R50K_TOKEN_SPLIT_REGEX,
786
+ bytePairRankDecoder,
787
+ specialTokensEncoder: new Map([
788
+ [EndOfText, 50256],
789
+ [FimPrefix, 50281],
790
+ [FimMiddle, 50282],
791
+ [FimSuffix, 50283]
792
+ ])
793
+ };
794
+ }
795
+
796
+ //#endregion
797
+ //#region node_modules/gpt-tokenizer/esm/encodingParams/r50k_base.js
798
+ function R50KBase(bytePairRankDecoder) {
799
+ return {
800
+ expectedVocabularySize: 50257,
801
+ tokenSplitRegex: R50K_TOKEN_SPLIT_REGEX,
802
+ bytePairRankDecoder,
803
+ specialTokensEncoder: new Map([[EndOfText, 50256]])
804
+ };
805
+ }
806
+
807
+ //#endregion
808
+ //#region node_modules/gpt-tokenizer/esm/modelParams.js
809
+ function getEncodingParams(encodingName, getMergeableRanks) {
810
+ const mergeableBytePairRanks = getMergeableRanks(encodingName);
811
+ switch (encodingName.toLowerCase()) {
812
+ case "r50k_base": return R50KBase(mergeableBytePairRanks);
813
+ case "p50k_base": return P50KBase(mergeableBytePairRanks);
814
+ case "p50k_edit": return P50KEdit(mergeableBytePairRanks);
815
+ case "cl100k_base": return Cl100KBase(mergeableBytePairRanks);
816
+ case "o200k_base": return O200KBase(mergeableBytePairRanks);
817
+ case "o200k_harmony": return O200KHarmony(mergeableBytePairRanks);
818
+ default: throw new Error(`Unknown encoding name: ${encodingName}`);
819
+ }
820
+ }
821
+
822
+ //#endregion
823
+ //#region node_modules/gpt-tokenizer/esm/GptEncoding.js
824
+ var GptEncoding = class GptEncoding {
825
+ static EndOfPrompt = EndOfPrompt;
826
+ static EndOfText = EndOfText;
827
+ static FimMiddle = FimMiddle;
828
+ static FimPrefix = FimPrefix;
829
+ static FimSuffix = FimSuffix;
830
+ modelName;
831
+ modelSpec;
832
+ bytePairEncodingCoreProcessor;
833
+ specialTokensEncoder;
834
+ specialTokensSet;
835
+ allSpecialTokenRegex;
836
+ defaultSpecialTokenConfig;
837
+ chatFormatter;
838
+ countChatCompletionTokens;
839
+ vocabularySize;
840
+ constructor({ bytePairRankDecoder: mergeableBytePairRanks, specialTokensEncoder, expectedVocabularySize, modelName, modelSpec, chatFormatter, ...rest }) {
841
+ this.specialTokensEncoder = specialTokensEncoder;
842
+ this.specialTokensSet = new Set(this.specialTokensEncoder.keys());
843
+ this.allSpecialTokenRegex = getSpecialTokenRegex(this.specialTokensSet);
844
+ this.bytePairEncodingCoreProcessor = new BytePairEncodingCore({
845
+ bytePairRankDecoder: mergeableBytePairRanks,
846
+ specialTokensEncoder,
847
+ ...rest
848
+ });
849
+ this.defaultSpecialTokenConfig = this.processSpecialTokens();
850
+ const maxTokenValue = Math.max(mergeableBytePairRanks.length - 1, getMaxValueFromMap(specialTokensEncoder));
851
+ this.vocabularySize = this.bytePairEncodingCoreProcessor.mergeableBytePairRankCount + specialTokensEncoder.size;
852
+ if (expectedVocabularySize !== void 0) {
853
+ if (this.vocabularySize !== expectedVocabularySize) throw new Error("The number of mergeable tokens and special tokens must be equal to expectedVocabularySize.");
854
+ if (maxTokenValue !== expectedVocabularySize - 1) throw new Error(`The model encodings are invalid. The maximum token value must be equal to expectedVocabularySize - 1. Currently ${maxTokenValue}, expected ${expectedVocabularySize - 1}`);
855
+ }
856
+ this.encode = this.encode.bind(this);
857
+ this.decode = this.decode.bind(this);
858
+ this.encodeGenerator = this.encodeGenerator.bind(this);
859
+ this.decodeGenerator = this.decodeGenerator.bind(this);
860
+ this.decodeAsyncGenerator = this.decodeAsyncGenerator.bind(this);
861
+ this.decodeAsync = this.decodeAsync.bind(this);
862
+ this.isWithinTokenLimit = this.isWithinTokenLimit.bind(this);
863
+ this.encodeChat = this.encodeChat.bind(this);
864
+ this.encodeChatGenerator = this.encodeChatGenerator.bind(this);
865
+ this.countTokens = this.countTokens.bind(this);
866
+ this.setMergeCacheSize = this.setMergeCacheSize.bind(this);
867
+ this.clearMergeCache = this.clearMergeCache.bind(this);
868
+ this.estimateCost = this.estimateCost.bind(this);
869
+ if (modelSpec?.supported_features?.includes("function_calling")) this.countChatCompletionTokens = this.countChatCompletionTokensInternal.bind(this);
870
+ this.modelName = modelName;
871
+ this.modelSpec = modelSpec;
872
+ this.chatFormatter = chatFormatter ?? "chatml";
873
+ }
874
+ *encodeHarmonyChatGenerator(chat, encodeOptions) {
875
+ const harmonyStart = this.specialTokensEncoder.get(HarmonyStart);
876
+ const harmonyMessage = this.specialTokensEncoder.get(HarmonyMessage);
877
+ const harmonyEnd = this.specialTokensEncoder.get(HarmonyEnd);
878
+ const harmonyReturn = this.specialTokensEncoder.get(HarmonyReturn);
879
+ const harmonyCall = this.specialTokensEncoder.get(HarmonyCall);
880
+ const harmonyChannel = this.specialTokensEncoder.get(HarmonyChannel);
881
+ const harmonyConstrain = this.specialTokensEncoder.get(HarmonyConstrain);
882
+ if (harmonyStart === void 0 || harmonyMessage === void 0 || harmonyEnd === void 0 || harmonyReturn === void 0 || harmonyCall === void 0 || harmonyChannel === void 0 || harmonyConstrain === void 0) throw new Error("Harmony chat format requires dedicated special tokens.");
883
+ const encodeHeaderText = (text) => text.length > 0 ? this.encode(text) : [];
884
+ const resolveTerminatorToken = (terminator) => {
885
+ switch (terminator) {
886
+ case "<|return|>": return harmonyReturn;
887
+ case "<|call|>": return harmonyCall;
888
+ default: return harmonyEnd;
889
+ }
890
+ };
891
+ for (const message of chat) {
892
+ if (message.content === void 0) throw new Error("Content must be defined for all messages.");
893
+ const roleOrName = message.name ?? message.role ?? "user";
894
+ yield [harmonyStart];
895
+ yield encodeHeaderText(roleOrName);
896
+ const recipientInRole = message.recipient && (message.recipientPlacement === "role" || !message.channel);
897
+ const recipientInChannel = message.recipient && !recipientInRole;
898
+ if (recipientInRole) yield encodeHeaderText(` to=${message.recipient}`);
899
+ if (message.channel) {
900
+ yield [harmonyChannel];
901
+ yield encodeHeaderText(message.channel);
902
+ if (recipientInChannel) yield encodeHeaderText(` to=${message.recipient}`);
903
+ }
904
+ if (message.constraint) {
905
+ yield [harmonyConstrain];
906
+ yield encodeHeaderText(message.constraint);
907
+ }
908
+ yield [harmonyMessage];
909
+ yield* this.encodeGenerator(message.content, encodeOptions);
910
+ yield [resolveTerminatorToken(message.terminator)];
911
+ }
912
+ const assistantPrime = encodeOptions?.primeWithAssistantResponse ?? "assistant";
913
+ if (assistantPrime.length > 0) {
914
+ yield [harmonyStart];
915
+ yield encodeHeaderText(assistantPrime);
916
+ }
917
+ }
918
+ static getEncodingApi(encodingName, getMergeableRanks) {
919
+ return new GptEncoding(getEncodingParams(encodingName, getMergeableRanks));
920
+ }
921
+ static getEncodingApiForModel(modelName, getMergeableRanks, modelSpec) {
922
+ return new GptEncoding({
923
+ ...getEncodingParams(modelToEncodingMap[modelName] ?? DEFAULT_ENCODING, getMergeableRanks),
924
+ modelName,
925
+ modelSpec
926
+ });
927
+ }
928
+ processSpecialTokens({ allowedSpecial, disallowedSpecial } = {}) {
929
+ let regexPattern;
930
+ if (allowedSpecial === ALL_SPECIAL_TOKENS || allowedSpecial?.has(ALL_SPECIAL_TOKENS)) {
931
+ allowedSpecial = new Set(this.specialTokensSet);
932
+ const allowedSpecialSet = allowedSpecial;
933
+ if (disallowedSpecial === ALL_SPECIAL_TOKENS) throw new Error("allowedSpecial and disallowedSpecial cannot both be set to \"all\".");
934
+ if (typeof disallowedSpecial === "object") disallowedSpecial.forEach((val) => allowedSpecialSet.delete(val));
935
+ else disallowedSpecial = /* @__PURE__ */ new Set();
936
+ }
937
+ if (!disallowedSpecial || disallowedSpecial === ALL_SPECIAL_TOKENS || disallowedSpecial.has(ALL_SPECIAL_TOKENS)) {
938
+ disallowedSpecial = new Set(this.specialTokensSet);
939
+ const disallowedSpecialSet = disallowedSpecial;
940
+ if (allowedSpecial?.size) {
941
+ allowedSpecial.forEach((val) => disallowedSpecialSet.delete(val));
942
+ disallowedSpecial.forEach((val) => allowedSpecial.delete(val));
943
+ if (disallowedSpecial.size > 0) regexPattern = getSpecialTokenRegex(disallowedSpecial);
944
+ } else regexPattern = this.allSpecialTokenRegex;
945
+ }
946
+ return {
947
+ allowedSpecial,
948
+ regexPattern
949
+ };
950
+ }
951
+ encodeGenerator(lineToEncode, encodeOptions) {
952
+ const specialTokenConfig = encodeOptions ? this.processSpecialTokens(encodeOptions) : this.defaultSpecialTokenConfig;
953
+ if (specialTokenConfig.regexPattern) {
954
+ const match = lineToEncode.match(specialTokenConfig.regexPattern);
955
+ if (match !== null) throw new Error(`Disallowed special token found: ${match[0]}`);
956
+ }
957
+ return this.bytePairEncodingCoreProcessor.encodeNativeGenerator(lineToEncode, specialTokenConfig.allowedSpecial);
958
+ }
959
+ encode(lineToEncode, encodeOptions) {
960
+ const specialTokenConfig = encodeOptions ? this.processSpecialTokens(encodeOptions) : this.defaultSpecialTokenConfig;
961
+ if (specialTokenConfig.regexPattern) {
962
+ const match = lineToEncode.match(specialTokenConfig.regexPattern);
963
+ if (match !== null) throw new Error(`Disallowed special token found: ${match[0]}`);
964
+ }
965
+ return this.bytePairEncodingCoreProcessor.encodeNative(lineToEncode, specialTokenConfig.allowedSpecial);
966
+ }
967
+ /**
968
+ * Progressively tokenizes an OpenAI chat.
969
+ * Warning: gpt-3.5-turbo and gpt-4 chat format may change over time.
970
+ * Returns tokens assuming the 'gpt-3.5-turbo-0301' / 'gpt-4-0314' format.
971
+ * Based on OpenAI's guidelines: https://github.com/openai/openai-python/blob/main/chatml.md
972
+ * Also mentioned in section 6 of this document: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
973
+ * @param encodeOptions Options controlling how special tokens are handled.
974
+ */
975
+ *encodeChatGenerator(chat, model = this.modelName, encodeOptions) {
976
+ if (!model) throw new Error("Model name must be provided either during initialization or passed in to the method.");
977
+ const params = chatModelParams[model];
978
+ if (!params) throw new Error(`Model '${model}' does not support chat.`);
979
+ if (this.chatFormatter === "harmony") {
980
+ yield* this.encodeHarmonyChatGenerator(chat, encodeOptions);
981
+ return;
982
+ }
983
+ const chatStartToken = this.specialTokensEncoder.get(ImStart);
984
+ const chatEndToken = this.specialTokensEncoder.get(ImEnd);
985
+ if (chatStartToken === void 0 || chatEndToken === void 0) throw new Error(`Model '${model}' does not support chat.`);
986
+ const allowedSpecial = new Set([ImSep]);
987
+ const { messageSeparator, roleSeparator } = params;
988
+ const encodedMessageSeparator = messageSeparator.length > 0 ? this.encode(messageSeparator) : [];
989
+ const encodedRoleSeparator = roleSeparator.length > 0 ? this.encode(roleSeparator, { allowedSpecial }) : [];
990
+ const nameCache = /* @__PURE__ */ new Map();
991
+ for (const { role = "system", name = role, content } of chat) {
992
+ if (content === void 0) throw new Error("Content must be defined for all messages.");
993
+ yield [chatStartToken];
994
+ const encodedName = nameCache.get(name) ?? this.encode(name);
995
+ nameCache.set(name, encodedName);
996
+ yield encodedName;
997
+ if (encodedRoleSeparator.length > 0) yield encodedRoleSeparator;
998
+ yield* this.encodeGenerator(content, encodeOptions);
999
+ yield [chatEndToken];
1000
+ yield encodedMessageSeparator;
1001
+ }
1002
+ const assistantPrime = encodeOptions?.primeWithAssistantResponse ?? "assistant";
1003
+ if (assistantPrime.length > 0) {
1004
+ yield [chatStartToken];
1005
+ yield* this.encodeGenerator(assistantPrime, encodeOptions);
1006
+ }
1007
+ if (encodedRoleSeparator.length > 0) yield encodedRoleSeparator;
1008
+ }
1009
+ /**
1010
+ * Encodes a chat into a single array of tokens.
1011
+ * Warning: gpt-3.5-turbo and gpt-4 chat format may change over time.
1012
+ * Returns tokens assuming the 'gpt-3.5-turbo-0301' / 'gpt-4-0314' format.
1013
+ * Based on OpenAI's guidelines: https://github.com/openai/openai-python/blob/main/chatml.md
1014
+ * Also mentioned in section 6 of this document: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
1015
+ * @param encodeOptions Options controlling how special tokens are handled.
1016
+ */
1017
+ encodeChat(chat, model = this.modelName, encodeOptions) {
1018
+ return [...this.encodeChatGenerator(chat, model, encodeOptions)].flat();
1019
+ }
1020
+ /**
1021
+ * Checks whether the provided input stays within the provided token limit.
1022
+ * @param input The string or chat messages to evaluate.
1023
+ * @param tokenLimit The maximum allowed number of tokens.
1024
+ * @param encodeOptions Options controlling how special tokens are handled.
1025
+ * @returns {false | number} false if token limit is exceeded, otherwise the number of tokens
1026
+ */
1027
+ isWithinTokenLimit(input, tokenLimit, encodeOptions) {
1028
+ const tokenGenerator = typeof input === "string" ? this.encodeGenerator(input, encodeOptions) : this.encodeChatGenerator(input, void 0, encodeOptions);
1029
+ let count = 0;
1030
+ for (const tokens of tokenGenerator) {
1031
+ count += tokens.length;
1032
+ if (count > tokenLimit) return false;
1033
+ }
1034
+ return count;
1035
+ }
1036
+ /**
1037
+ * Counts the number of tokens in the input.
1038
+ * @param input The string or chat messages to evaluate.
1039
+ * @param encodeOptions Options controlling how special tokens are handled.
1040
+ * @returns {number} The number of tokens.
1041
+ */
1042
+ countTokens(input, encodeOptions) {
1043
+ if (typeof input === "string") {
1044
+ const specialTokenConfig = encodeOptions ? this.processSpecialTokens(encodeOptions) : this.defaultSpecialTokenConfig;
1045
+ if (specialTokenConfig.regexPattern) {
1046
+ const match = input.match(specialTokenConfig.regexPattern);
1047
+ if (match !== null) throw new Error(`Disallowed special token found: ${match[0]}`);
1048
+ }
1049
+ return this.bytePairEncodingCoreProcessor.countNative(input, specialTokenConfig.allowedSpecial);
1050
+ }
1051
+ const tokenGenerator = this.encodeChatGenerator(input, void 0, encodeOptions);
1052
+ let count = 0;
1053
+ for (const tokens of tokenGenerator) count += tokens.length;
1054
+ return count;
1055
+ }
1056
+ countStringTokens(text) {
1057
+ if (!text) return 0;
1058
+ return this.bytePairEncodingCoreProcessor.countNative(text);
1059
+ }
1060
+ countChatCompletionTokensInternal(request) {
1061
+ return computeChatCompletionTokenCount(request, (text) => this.countStringTokens(text));
1062
+ }
1063
+ setMergeCacheSize(size) {
1064
+ this.bytePairEncodingCoreProcessor.setMergeCacheSize(size);
1065
+ }
1066
+ clearMergeCache() {
1067
+ this.bytePairEncodingCoreProcessor.clearMergeCache();
1068
+ }
1069
+ decode(inputTokensToDecode) {
1070
+ return this.bytePairEncodingCoreProcessor.decodeNative(inputTokensToDecode);
1071
+ }
1072
+ *decodeGenerator(inputTokensToDecode) {
1073
+ const decodedByteGenerator = this.bytePairEncodingCoreProcessor.decodeNativeGenerator(inputTokensToDecode);
1074
+ let buffer = "";
1075
+ for (const decodedPart of decodedByteGenerator) {
1076
+ buffer += typeof decodedPart === "string" ? decodedPart : decoder.decode(decodedPart, { stream: true });
1077
+ if (buffer.length === 0 || endsWithIncompleteUtfPairSurrogate(buffer)) continue;
1078
+ else {
1079
+ yield buffer;
1080
+ buffer = "";
1081
+ }
1082
+ }
1083
+ if (buffer.length > 0) yield buffer;
1084
+ }
1085
+ async *decodeAsyncGenerator(inputTokensToDecode) {
1086
+ const decodedByteGenerator = this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(inputTokensToDecode);
1087
+ let buffer = "";
1088
+ for await (const decodedPart of decodedByteGenerator) {
1089
+ buffer += typeof decodedPart === "string" ? decodedPart : decoder.decode(decodedPart, { stream: true });
1090
+ if (buffer.length === 0 || endsWithIncompleteUtfPairSurrogate(buffer)) continue;
1091
+ else {
1092
+ yield buffer;
1093
+ buffer = "";
1094
+ }
1095
+ }
1096
+ if (buffer.length > 0) yield buffer;
1097
+ }
1098
+ async decodeAsync(inputTokensToDecode) {
1099
+ const decodedByteGenerator = this.bytePairEncodingCoreProcessor.decodeNativeAsyncIterable(inputTokensToDecode);
1100
+ let buffer = "";
1101
+ for await (const decodedPart of decodedByteGenerator) buffer += typeof decodedPart === "string" ? decodedPart : decoder.decode(decodedPart, { stream: true });
1102
+ return buffer;
1103
+ }
1104
+ /**
1105
+ * Estimates the cost of processing a given token count using the model's pricing.
1106
+ *
1107
+ * @param tokenCount - The number of tokens to estimate cost for
1108
+ * @returns Cost estimate object with applicable price components (input, output, batchInput, batchOutput)
1109
+ */
1110
+ estimateCost(tokenCount, modelSpec = this.modelSpec) {
1111
+ if (!modelSpec) throw new Error("Model spec must be provided either during initialization or passed in to the method.");
1112
+ if (!modelSpec.price_data) throw new Error(`No cost information available for model: ${modelSpec.name}`);
1113
+ const priceDataPerMillion = modelSpec.price_data;
1114
+ const result = {};
1115
+ const millionTokens = tokenCount / 1e6;
1116
+ if (priceDataPerMillion.main) result.main = {
1117
+ input: priceDataPerMillion.main.input && priceDataPerMillion.main.input * millionTokens,
1118
+ output: priceDataPerMillion.main.output && priceDataPerMillion.main.output * millionTokens,
1119
+ cached_input: priceDataPerMillion.main.cached_input && priceDataPerMillion.main.cached_input * millionTokens,
1120
+ cached_output: priceDataPerMillion.main.cached_output && priceDataPerMillion.main.cached_output * millionTokens
1121
+ };
1122
+ if (priceDataPerMillion.batch) result.batch = {
1123
+ input: priceDataPerMillion.batch.input && priceDataPerMillion.batch.input * millionTokens,
1124
+ output: priceDataPerMillion.batch.output && priceDataPerMillion.batch.output * millionTokens,
1125
+ cached_input: priceDataPerMillion.batch.cached_input && priceDataPerMillion.batch.cached_input * millionTokens,
1126
+ cached_output: priceDataPerMillion.batch.cached_output && priceDataPerMillion.batch.cached_output * millionTokens
1127
+ };
1128
+ return result;
1129
+ }
1130
+ };
1131
+
1132
+ //#endregion
1133
+ export { ImStart as _, FimPrefix as a, HarmonyChannel as c, HarmonyMessage as d, HarmonyReturn as f, ImSep as g, ImEnd as h, FimMiddle as i, HarmonyConstrain as l, HarmonyStartOfText as m, EndOfPrompt as n, FimSuffix as o, HarmonyStart as p, EndOfText as r, HarmonyCall as s, GptEncoding as t, HarmonyEnd as u, ALL_SPECIAL_TOKENS as v, DEFAULT_MERGE_CACHE_SIZE as y };
1134
+ //# sourceMappingURL=GptEncoding-DuDWxow_.mjs.map