@dedenlabs/claude-code-router-cli 2.0.7 → 2.0.8-beta.1

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.
@@ -1,2027 +0,0 @@
1
- "use strict";
2
-
3
- // node_modules/lru-cache/dist/esm/index.js
4
- var defaultPerf = typeof performance === "object" && performance && typeof performance.now === "function" ? performance : Date;
5
- var warned = /* @__PURE__ */ new Set();
6
- var PROCESS = typeof process === "object" && !!process ? process : {};
7
- var emitWarning = (msg, type, code, fn) => {
8
- typeof PROCESS.emitWarning === "function" ? PROCESS.emitWarning(msg, type, code, fn) : console.error(`[${code}] ${type}: ${msg}`);
9
- };
10
- var AC = globalThis.AbortController;
11
- var AS = globalThis.AbortSignal;
12
- if (typeof AC === "undefined") {
13
- AS = class AbortSignal {
14
- onabort;
15
- _onabort = [];
16
- reason;
17
- aborted = false;
18
- addEventListener(_, fn) {
19
- this._onabort.push(fn);
20
- }
21
- };
22
- AC = class AbortController {
23
- constructor() {
24
- warnACPolyfill();
25
- }
26
- signal = new AS();
27
- abort(reason) {
28
- if (this.signal.aborted)
29
- return;
30
- this.signal.reason = reason;
31
- this.signal.aborted = true;
32
- for (const fn of this.signal._onabort) {
33
- fn(reason);
34
- }
35
- this.signal.onabort?.(reason);
36
- }
37
- };
38
- let printACPolyfillWarning = PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== "1";
39
- const warnACPolyfill = () => {
40
- if (!printACPolyfillWarning)
41
- return;
42
- printACPolyfillWarning = false;
43
- emitWarning("AbortController is not defined. If using lru-cache in node 14, load an AbortController polyfill from the `node-abort-controller` package. A minimal polyfill is provided for use by LRUCache.fetch(), but it should not be relied upon in other contexts (eg, passing it to other APIs that use AbortController/AbortSignal might have undesirable effects). You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.", "NO_ABORT_CONTROLLER", "ENOTSUP", warnACPolyfill);
44
- };
45
- }
46
- var shouldWarn = (code) => !warned.has(code);
47
- var TYPE = Symbol("type");
48
- var isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
49
- var getUintArray = (max) => !isPosInt(max) ? null : max <= Math.pow(2, 8) ? Uint8Array : max <= Math.pow(2, 16) ? Uint16Array : max <= Math.pow(2, 32) ? Uint32Array : max <= Number.MAX_SAFE_INTEGER ? ZeroArray : null;
50
- var ZeroArray = class extends Array {
51
- constructor(size) {
52
- super(size);
53
- this.fill(0);
54
- }
55
- };
56
- var Stack = class _Stack {
57
- heap;
58
- length;
59
- // private constructor
60
- static #constructing = false;
61
- static create(max) {
62
- const HeapCls = getUintArray(max);
63
- if (!HeapCls)
64
- return [];
65
- _Stack.#constructing = true;
66
- const s = new _Stack(max, HeapCls);
67
- _Stack.#constructing = false;
68
- return s;
69
- }
70
- constructor(max, HeapCls) {
71
- if (!_Stack.#constructing) {
72
- throw new TypeError("instantiate Stack using Stack.create(n)");
73
- }
74
- this.heap = new HeapCls(max);
75
- this.length = 0;
76
- }
77
- push(n) {
78
- this.heap[this.length++] = n;
79
- }
80
- pop() {
81
- return this.heap[--this.length];
82
- }
83
- };
84
- var LRUCache = class _LRUCache {
85
- // options that cannot be changed without disaster
86
- #max;
87
- #maxSize;
88
- #dispose;
89
- #onInsert;
90
- #disposeAfter;
91
- #fetchMethod;
92
- #memoMethod;
93
- #perf;
94
- /**
95
- * {@link LRUCache.OptionsBase.perf}
96
- */
97
- get perf() {
98
- return this.#perf;
99
- }
100
- /**
101
- * {@link LRUCache.OptionsBase.ttl}
102
- */
103
- ttl;
104
- /**
105
- * {@link LRUCache.OptionsBase.ttlResolution}
106
- */
107
- ttlResolution;
108
- /**
109
- * {@link LRUCache.OptionsBase.ttlAutopurge}
110
- */
111
- ttlAutopurge;
112
- /**
113
- * {@link LRUCache.OptionsBase.updateAgeOnGet}
114
- */
115
- updateAgeOnGet;
116
- /**
117
- * {@link LRUCache.OptionsBase.updateAgeOnHas}
118
- */
119
- updateAgeOnHas;
120
- /**
121
- * {@link LRUCache.OptionsBase.allowStale}
122
- */
123
- allowStale;
124
- /**
125
- * {@link LRUCache.OptionsBase.noDisposeOnSet}
126
- */
127
- noDisposeOnSet;
128
- /**
129
- * {@link LRUCache.OptionsBase.noUpdateTTL}
130
- */
131
- noUpdateTTL;
132
- /**
133
- * {@link LRUCache.OptionsBase.maxEntrySize}
134
- */
135
- maxEntrySize;
136
- /**
137
- * {@link LRUCache.OptionsBase.sizeCalculation}
138
- */
139
- sizeCalculation;
140
- /**
141
- * {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
142
- */
143
- noDeleteOnFetchRejection;
144
- /**
145
- * {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
146
- */
147
- noDeleteOnStaleGet;
148
- /**
149
- * {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
150
- */
151
- allowStaleOnFetchAbort;
152
- /**
153
- * {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
154
- */
155
- allowStaleOnFetchRejection;
156
- /**
157
- * {@link LRUCache.OptionsBase.ignoreFetchAbort}
158
- */
159
- ignoreFetchAbort;
160
- // computed properties
161
- #size;
162
- #calculatedSize;
163
- #keyMap;
164
- #keyList;
165
- #valList;
166
- #next;
167
- #prev;
168
- #head;
169
- #tail;
170
- #free;
171
- #disposed;
172
- #sizes;
173
- #starts;
174
- #ttls;
175
- #hasDispose;
176
- #hasFetchMethod;
177
- #hasDisposeAfter;
178
- #hasOnInsert;
179
- /**
180
- * Do not call this method unless you need to inspect the
181
- * inner workings of the cache. If anything returned by this
182
- * object is modified in any way, strange breakage may occur.
183
- *
184
- * These fields are private for a reason!
185
- *
186
- * @internal
187
- */
188
- static unsafeExposeInternals(c) {
189
- return {
190
- // properties
191
- starts: c.#starts,
192
- ttls: c.#ttls,
193
- sizes: c.#sizes,
194
- keyMap: c.#keyMap,
195
- keyList: c.#keyList,
196
- valList: c.#valList,
197
- next: c.#next,
198
- prev: c.#prev,
199
- get head() {
200
- return c.#head;
201
- },
202
- get tail() {
203
- return c.#tail;
204
- },
205
- free: c.#free,
206
- // methods
207
- isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
208
- backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
209
- moveToTail: (index) => c.#moveToTail(index),
210
- indexes: (options) => c.#indexes(options),
211
- rindexes: (options) => c.#rindexes(options),
212
- isStale: (index) => c.#isStale(index)
213
- };
214
- }
215
- // Protected read-only members
216
- /**
217
- * {@link LRUCache.OptionsBase.max} (read-only)
218
- */
219
- get max() {
220
- return this.#max;
221
- }
222
- /**
223
- * {@link LRUCache.OptionsBase.maxSize} (read-only)
224
- */
225
- get maxSize() {
226
- return this.#maxSize;
227
- }
228
- /**
229
- * The total computed size of items in the cache (read-only)
230
- */
231
- get calculatedSize() {
232
- return this.#calculatedSize;
233
- }
234
- /**
235
- * The number of items stored in the cache (read-only)
236
- */
237
- get size() {
238
- return this.#size;
239
- }
240
- /**
241
- * {@link LRUCache.OptionsBase.fetchMethod} (read-only)
242
- */
243
- get fetchMethod() {
244
- return this.#fetchMethod;
245
- }
246
- get memoMethod() {
247
- return this.#memoMethod;
248
- }
249
- /**
250
- * {@link LRUCache.OptionsBase.dispose} (read-only)
251
- */
252
- get dispose() {
253
- return this.#dispose;
254
- }
255
- /**
256
- * {@link LRUCache.OptionsBase.onInsert} (read-only)
257
- */
258
- get onInsert() {
259
- return this.#onInsert;
260
- }
261
- /**
262
- * {@link LRUCache.OptionsBase.disposeAfter} (read-only)
263
- */
264
- get disposeAfter() {
265
- return this.#disposeAfter;
266
- }
267
- constructor(options) {
268
- const { max = 0, ttl, ttlResolution = 1, ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, onInsert, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0, maxEntrySize = 0, sizeCalculation, fetchMethod, memoMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, perf } = options;
269
- if (perf !== void 0) {
270
- if (typeof perf?.now !== "function") {
271
- throw new TypeError("perf option must have a now() method if specified");
272
- }
273
- }
274
- this.#perf = perf ?? defaultPerf;
275
- if (max !== 0 && !isPosInt(max)) {
276
- throw new TypeError("max option must be a nonnegative integer");
277
- }
278
- const UintArray = max ? getUintArray(max) : Array;
279
- if (!UintArray) {
280
- throw new Error("invalid max value: " + max);
281
- }
282
- this.#max = max;
283
- this.#maxSize = maxSize;
284
- this.maxEntrySize = maxEntrySize || this.#maxSize;
285
- this.sizeCalculation = sizeCalculation;
286
- if (this.sizeCalculation) {
287
- if (!this.#maxSize && !this.maxEntrySize) {
288
- throw new TypeError("cannot set sizeCalculation without setting maxSize or maxEntrySize");
289
- }
290
- if (typeof this.sizeCalculation !== "function") {
291
- throw new TypeError("sizeCalculation set to non-function");
292
- }
293
- }
294
- if (memoMethod !== void 0 && typeof memoMethod !== "function") {
295
- throw new TypeError("memoMethod must be a function if defined");
296
- }
297
- this.#memoMethod = memoMethod;
298
- if (fetchMethod !== void 0 && typeof fetchMethod !== "function") {
299
- throw new TypeError("fetchMethod must be a function if specified");
300
- }
301
- this.#fetchMethod = fetchMethod;
302
- this.#hasFetchMethod = !!fetchMethod;
303
- this.#keyMap = /* @__PURE__ */ new Map();
304
- this.#keyList = new Array(max).fill(void 0);
305
- this.#valList = new Array(max).fill(void 0);
306
- this.#next = new UintArray(max);
307
- this.#prev = new UintArray(max);
308
- this.#head = 0;
309
- this.#tail = 0;
310
- this.#free = Stack.create(max);
311
- this.#size = 0;
312
- this.#calculatedSize = 0;
313
- if (typeof dispose === "function") {
314
- this.#dispose = dispose;
315
- }
316
- if (typeof onInsert === "function") {
317
- this.#onInsert = onInsert;
318
- }
319
- if (typeof disposeAfter === "function") {
320
- this.#disposeAfter = disposeAfter;
321
- this.#disposed = [];
322
- } else {
323
- this.#disposeAfter = void 0;
324
- this.#disposed = void 0;
325
- }
326
- this.#hasDispose = !!this.#dispose;
327
- this.#hasOnInsert = !!this.#onInsert;
328
- this.#hasDisposeAfter = !!this.#disposeAfter;
329
- this.noDisposeOnSet = !!noDisposeOnSet;
330
- this.noUpdateTTL = !!noUpdateTTL;
331
- this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
332
- this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
333
- this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
334
- this.ignoreFetchAbort = !!ignoreFetchAbort;
335
- if (this.maxEntrySize !== 0) {
336
- if (this.#maxSize !== 0) {
337
- if (!isPosInt(this.#maxSize)) {
338
- throw new TypeError("maxSize must be a positive integer if specified");
339
- }
340
- }
341
- if (!isPosInt(this.maxEntrySize)) {
342
- throw new TypeError("maxEntrySize must be a positive integer if specified");
343
- }
344
- this.#initializeSizeTracking();
345
- }
346
- this.allowStale = !!allowStale;
347
- this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
348
- this.updateAgeOnGet = !!updateAgeOnGet;
349
- this.updateAgeOnHas = !!updateAgeOnHas;
350
- this.ttlResolution = isPosInt(ttlResolution) || ttlResolution === 0 ? ttlResolution : 1;
351
- this.ttlAutopurge = !!ttlAutopurge;
352
- this.ttl = ttl || 0;
353
- if (this.ttl) {
354
- if (!isPosInt(this.ttl)) {
355
- throw new TypeError("ttl must be a positive integer if specified");
356
- }
357
- this.#initializeTTLTracking();
358
- }
359
- if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
360
- throw new TypeError("At least one of max, maxSize, or ttl is required");
361
- }
362
- if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
363
- const code = "LRU_CACHE_UNBOUNDED";
364
- if (shouldWarn(code)) {
365
- warned.add(code);
366
- const msg = "TTL caching without ttlAutopurge, max, or maxSize can result in unbounded memory consumption.";
367
- emitWarning(msg, "UnboundedCacheWarning", code, _LRUCache);
368
- }
369
- }
370
- }
371
- /**
372
- * Return the number of ms left in the item's TTL. If item is not in cache,
373
- * returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
374
- */
375
- getRemainingTTL(key) {
376
- return this.#keyMap.has(key) ? Infinity : 0;
377
- }
378
- #initializeTTLTracking() {
379
- const ttls = new ZeroArray(this.#max);
380
- const starts = new ZeroArray(this.#max);
381
- this.#ttls = ttls;
382
- this.#starts = starts;
383
- this.#setItemTTL = (index, ttl, start = this.#perf.now()) => {
384
- starts[index] = ttl !== 0 ? start : 0;
385
- ttls[index] = ttl;
386
- if (ttl !== 0 && this.ttlAutopurge) {
387
- const t = setTimeout(() => {
388
- if (this.#isStale(index)) {
389
- this.#delete(this.#keyList[index], "expire");
390
- }
391
- }, ttl + 1);
392
- if (t.unref) {
393
- t.unref();
394
- }
395
- }
396
- };
397
- this.#updateItemAge = (index) => {
398
- starts[index] = ttls[index] !== 0 ? this.#perf.now() : 0;
399
- };
400
- this.#statusTTL = (status, index) => {
401
- if (ttls[index]) {
402
- const ttl = ttls[index];
403
- const start = starts[index];
404
- if (!ttl || !start)
405
- return;
406
- status.ttl = ttl;
407
- status.start = start;
408
- status.now = cachedNow || getNow();
409
- const age = status.now - start;
410
- status.remainingTTL = ttl - age;
411
- }
412
- };
413
- let cachedNow = 0;
414
- const getNow = () => {
415
- const n = this.#perf.now();
416
- if (this.ttlResolution > 0) {
417
- cachedNow = n;
418
- const t = setTimeout(() => cachedNow = 0, this.ttlResolution);
419
- if (t.unref) {
420
- t.unref();
421
- }
422
- }
423
- return n;
424
- };
425
- this.getRemainingTTL = (key) => {
426
- const index = this.#keyMap.get(key);
427
- if (index === void 0) {
428
- return 0;
429
- }
430
- const ttl = ttls[index];
431
- const start = starts[index];
432
- if (!ttl || !start) {
433
- return Infinity;
434
- }
435
- const age = (cachedNow || getNow()) - start;
436
- return ttl - age;
437
- };
438
- this.#isStale = (index) => {
439
- const s = starts[index];
440
- const t = ttls[index];
441
- return !!t && !!s && (cachedNow || getNow()) - s > t;
442
- };
443
- }
444
- // conditionally set private methods related to TTL
445
- #updateItemAge = () => {
446
- };
447
- #statusTTL = () => {
448
- };
449
- #setItemTTL = () => {
450
- };
451
- /* c8 ignore stop */
452
- #isStale = () => false;
453
- #initializeSizeTracking() {
454
- const sizes = new ZeroArray(this.#max);
455
- this.#calculatedSize = 0;
456
- this.#sizes = sizes;
457
- this.#removeItemSize = (index) => {
458
- this.#calculatedSize -= sizes[index];
459
- sizes[index] = 0;
460
- };
461
- this.#requireSize = (k, v, size, sizeCalculation) => {
462
- if (this.#isBackgroundFetch(v)) {
463
- return 0;
464
- }
465
- if (!isPosInt(size)) {
466
- if (sizeCalculation) {
467
- if (typeof sizeCalculation !== "function") {
468
- throw new TypeError("sizeCalculation must be a function");
469
- }
470
- size = sizeCalculation(v, k);
471
- if (!isPosInt(size)) {
472
- throw new TypeError("sizeCalculation return invalid (expect positive integer)");
473
- }
474
- } else {
475
- throw new TypeError("invalid size value (must be positive integer). When maxSize or maxEntrySize is used, sizeCalculation or size must be set.");
476
- }
477
- }
478
- return size;
479
- };
480
- this.#addItemSize = (index, size, status) => {
481
- sizes[index] = size;
482
- if (this.#maxSize) {
483
- const maxSize = this.#maxSize - sizes[index];
484
- while (this.#calculatedSize > maxSize) {
485
- this.#evict(true);
486
- }
487
- }
488
- this.#calculatedSize += sizes[index];
489
- if (status) {
490
- status.entrySize = size;
491
- status.totalCalculatedSize = this.#calculatedSize;
492
- }
493
- };
494
- }
495
- #removeItemSize = (_i) => {
496
- };
497
- #addItemSize = (_i, _s, _st) => {
498
- };
499
- #requireSize = (_k, _v, size, sizeCalculation) => {
500
- if (size || sizeCalculation) {
501
- throw new TypeError("cannot set size without setting maxSize or maxEntrySize on cache");
502
- }
503
- return 0;
504
- };
505
- *#indexes({ allowStale = this.allowStale } = {}) {
506
- if (this.#size) {
507
- for (let i = this.#tail; true; ) {
508
- if (!this.#isValidIndex(i)) {
509
- break;
510
- }
511
- if (allowStale || !this.#isStale(i)) {
512
- yield i;
513
- }
514
- if (i === this.#head) {
515
- break;
516
- } else {
517
- i = this.#prev[i];
518
- }
519
- }
520
- }
521
- }
522
- *#rindexes({ allowStale = this.allowStale } = {}) {
523
- if (this.#size) {
524
- for (let i = this.#head; true; ) {
525
- if (!this.#isValidIndex(i)) {
526
- break;
527
- }
528
- if (allowStale || !this.#isStale(i)) {
529
- yield i;
530
- }
531
- if (i === this.#tail) {
532
- break;
533
- } else {
534
- i = this.#next[i];
535
- }
536
- }
537
- }
538
- }
539
- #isValidIndex(index) {
540
- return index !== void 0 && this.#keyMap.get(this.#keyList[index]) === index;
541
- }
542
- /**
543
- * Return a generator yielding `[key, value]` pairs,
544
- * in order from most recently used to least recently used.
545
- */
546
- *entries() {
547
- for (const i of this.#indexes()) {
548
- if (this.#valList[i] !== void 0 && this.#keyList[i] !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
549
- yield [this.#keyList[i], this.#valList[i]];
550
- }
551
- }
552
- }
553
- /**
554
- * Inverse order version of {@link LRUCache.entries}
555
- *
556
- * Return a generator yielding `[key, value]` pairs,
557
- * in order from least recently used to most recently used.
558
- */
559
- *rentries() {
560
- for (const i of this.#rindexes()) {
561
- if (this.#valList[i] !== void 0 && this.#keyList[i] !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
562
- yield [this.#keyList[i], this.#valList[i]];
563
- }
564
- }
565
- }
566
- /**
567
- * Return a generator yielding the keys in the cache,
568
- * in order from most recently used to least recently used.
569
- */
570
- *keys() {
571
- for (const i of this.#indexes()) {
572
- const k = this.#keyList[i];
573
- if (k !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
574
- yield k;
575
- }
576
- }
577
- }
578
- /**
579
- * Inverse order version of {@link LRUCache.keys}
580
- *
581
- * Return a generator yielding the keys in the cache,
582
- * in order from least recently used to most recently used.
583
- */
584
- *rkeys() {
585
- for (const i of this.#rindexes()) {
586
- const k = this.#keyList[i];
587
- if (k !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
588
- yield k;
589
- }
590
- }
591
- }
592
- /**
593
- * Return a generator yielding the values in the cache,
594
- * in order from most recently used to least recently used.
595
- */
596
- *values() {
597
- for (const i of this.#indexes()) {
598
- const v = this.#valList[i];
599
- if (v !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
600
- yield this.#valList[i];
601
- }
602
- }
603
- }
604
- /**
605
- * Inverse order version of {@link LRUCache.values}
606
- *
607
- * Return a generator yielding the values in the cache,
608
- * in order from least recently used to most recently used.
609
- */
610
- *rvalues() {
611
- for (const i of this.#rindexes()) {
612
- const v = this.#valList[i];
613
- if (v !== void 0 && !this.#isBackgroundFetch(this.#valList[i])) {
614
- yield this.#valList[i];
615
- }
616
- }
617
- }
618
- /**
619
- * Iterating over the cache itself yields the same results as
620
- * {@link LRUCache.entries}
621
- */
622
- [Symbol.iterator]() {
623
- return this.entries();
624
- }
625
- /**
626
- * A String value that is used in the creation of the default string
627
- * description of an object. Called by the built-in method
628
- * `Object.prototype.toString`.
629
- */
630
- [Symbol.toStringTag] = "LRUCache";
631
- /**
632
- * Find a value for which the supplied fn method returns a truthy value,
633
- * similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
634
- */
635
- find(fn, getOptions = {}) {
636
- for (const i of this.#indexes()) {
637
- const v = this.#valList[i];
638
- const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
639
- if (value === void 0)
640
- continue;
641
- if (fn(value, this.#keyList[i], this)) {
642
- return this.get(this.#keyList[i], getOptions);
643
- }
644
- }
645
- }
646
- /**
647
- * Call the supplied function on each item in the cache, in order from most
648
- * recently used to least recently used.
649
- *
650
- * `fn` is called as `fn(value, key, cache)`.
651
- *
652
- * If `thisp` is provided, function will be called in the `this`-context of
653
- * the provided object, or the cache if no `thisp` object is provided.
654
- *
655
- * Does not update age or recenty of use, or iterate over stale values.
656
- */
657
- forEach(fn, thisp = this) {
658
- for (const i of this.#indexes()) {
659
- const v = this.#valList[i];
660
- const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
661
- if (value === void 0)
662
- continue;
663
- fn.call(thisp, value, this.#keyList[i], this);
664
- }
665
- }
666
- /**
667
- * The same as {@link LRUCache.forEach} but items are iterated over in
668
- * reverse order. (ie, less recently used items are iterated over first.)
669
- */
670
- rforEach(fn, thisp = this) {
671
- for (const i of this.#rindexes()) {
672
- const v = this.#valList[i];
673
- const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
674
- if (value === void 0)
675
- continue;
676
- fn.call(thisp, value, this.#keyList[i], this);
677
- }
678
- }
679
- /**
680
- * Delete any stale entries. Returns true if anything was removed,
681
- * false otherwise.
682
- */
683
- purgeStale() {
684
- let deleted = false;
685
- for (const i of this.#rindexes({ allowStale: true })) {
686
- if (this.#isStale(i)) {
687
- this.#delete(this.#keyList[i], "expire");
688
- deleted = true;
689
- }
690
- }
691
- return deleted;
692
- }
693
- /**
694
- * Get the extended info about a given entry, to get its value, size, and
695
- * TTL info simultaneously. Returns `undefined` if the key is not present.
696
- *
697
- * Unlike {@link LRUCache#dump}, which is designed to be portable and survive
698
- * serialization, the `start` value is always the current timestamp, and the
699
- * `ttl` is a calculated remaining time to live (negative if expired).
700
- *
701
- * Always returns stale values, if their info is found in the cache, so be
702
- * sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
703
- * if relevant.
704
- */
705
- info(key) {
706
- const i = this.#keyMap.get(key);
707
- if (i === void 0)
708
- return void 0;
709
- const v = this.#valList[i];
710
- const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
711
- if (value === void 0)
712
- return void 0;
713
- const entry = { value };
714
- if (this.#ttls && this.#starts) {
715
- const ttl = this.#ttls[i];
716
- const start = this.#starts[i];
717
- if (ttl && start) {
718
- const remain = ttl - (this.#perf.now() - start);
719
- entry.ttl = remain;
720
- entry.start = Date.now();
721
- }
722
- }
723
- if (this.#sizes) {
724
- entry.size = this.#sizes[i];
725
- }
726
- return entry;
727
- }
728
- /**
729
- * Return an array of [key, {@link LRUCache.Entry}] tuples which can be
730
- * passed to {@link LRUCache#load}.
731
- *
732
- * The `start` fields are calculated relative to a portable `Date.now()`
733
- * timestamp, even if `performance.now()` is available.
734
- *
735
- * Stale entries are always included in the `dump`, even if
736
- * {@link LRUCache.OptionsBase.allowStale} is false.
737
- *
738
- * Note: this returns an actual array, not a generator, so it can be more
739
- * easily passed around.
740
- */
741
- dump() {
742
- const arr = [];
743
- for (const i of this.#indexes({ allowStale: true })) {
744
- const key = this.#keyList[i];
745
- const v = this.#valList[i];
746
- const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
747
- if (value === void 0 || key === void 0)
748
- continue;
749
- const entry = { value };
750
- if (this.#ttls && this.#starts) {
751
- entry.ttl = this.#ttls[i];
752
- const age = this.#perf.now() - this.#starts[i];
753
- entry.start = Math.floor(Date.now() - age);
754
- }
755
- if (this.#sizes) {
756
- entry.size = this.#sizes[i];
757
- }
758
- arr.unshift([key, entry]);
759
- }
760
- return arr;
761
- }
762
- /**
763
- * Reset the cache and load in the items in entries in the order listed.
764
- *
765
- * The shape of the resulting cache may be different if the same options are
766
- * not used in both caches.
767
- *
768
- * The `start` fields are assumed to be calculated relative to a portable
769
- * `Date.now()` timestamp, even if `performance.now()` is available.
770
- */
771
- load(arr) {
772
- this.clear();
773
- for (const [key, entry] of arr) {
774
- if (entry.start) {
775
- const age = Date.now() - entry.start;
776
- entry.start = this.#perf.now() - age;
777
- }
778
- this.set(key, entry.value, entry);
779
- }
780
- }
781
- /**
782
- * Add a value to the cache.
783
- *
784
- * Note: if `undefined` is specified as a value, this is an alias for
785
- * {@link LRUCache#delete}
786
- *
787
- * Fields on the {@link LRUCache.SetOptions} options param will override
788
- * their corresponding values in the constructor options for the scope
789
- * of this single `set()` operation.
790
- *
791
- * If `start` is provided, then that will set the effective start
792
- * time for the TTL calculation. Note that this must be a previous
793
- * value of `performance.now()` if supported, or a previous value of
794
- * `Date.now()` if not.
795
- *
796
- * Options object may also include `size`, which will prevent
797
- * calling the `sizeCalculation` function and just use the specified
798
- * number if it is a positive integer, and `noDisposeOnSet` which
799
- * will prevent calling a `dispose` function in the case of
800
- * overwrites.
801
- *
802
- * If the `size` (or return value of `sizeCalculation`) for a given
803
- * entry is greater than `maxEntrySize`, then the item will not be
804
- * added to the cache.
805
- *
806
- * Will update the recency of the entry.
807
- *
808
- * If the value is `undefined`, then this is an alias for
809
- * `cache.delete(key)`. `undefined` is never stored in the cache.
810
- */
811
- set(k, v, setOptions = {}) {
812
- if (v === void 0) {
813
- this.delete(k);
814
- return this;
815
- }
816
- const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status } = setOptions;
817
- let { noUpdateTTL = this.noUpdateTTL } = setOptions;
818
- const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation);
819
- if (this.maxEntrySize && size > this.maxEntrySize) {
820
- if (status) {
821
- status.set = "miss";
822
- status.maxEntrySizeExceeded = true;
823
- }
824
- this.#delete(k, "set");
825
- return this;
826
- }
827
- let index = this.#size === 0 ? void 0 : this.#keyMap.get(k);
828
- if (index === void 0) {
829
- index = this.#size === 0 ? this.#tail : this.#free.length !== 0 ? this.#free.pop() : this.#size === this.#max ? this.#evict(false) : this.#size;
830
- this.#keyList[index] = k;
831
- this.#valList[index] = v;
832
- this.#keyMap.set(k, index);
833
- this.#next[this.#tail] = index;
834
- this.#prev[index] = this.#tail;
835
- this.#tail = index;
836
- this.#size++;
837
- this.#addItemSize(index, size, status);
838
- if (status)
839
- status.set = "add";
840
- noUpdateTTL = false;
841
- if (this.#hasOnInsert) {
842
- this.#onInsert?.(v, k, "add");
843
- }
844
- } else {
845
- this.#moveToTail(index);
846
- const oldVal = this.#valList[index];
847
- if (v !== oldVal) {
848
- if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
849
- oldVal.__abortController.abort(new Error("replaced"));
850
- const { __staleWhileFetching: s } = oldVal;
851
- if (s !== void 0 && !noDisposeOnSet) {
852
- if (this.#hasDispose) {
853
- this.#dispose?.(s, k, "set");
854
- }
855
- if (this.#hasDisposeAfter) {
856
- this.#disposed?.push([s, k, "set"]);
857
- }
858
- }
859
- } else if (!noDisposeOnSet) {
860
- if (this.#hasDispose) {
861
- this.#dispose?.(oldVal, k, "set");
862
- }
863
- if (this.#hasDisposeAfter) {
864
- this.#disposed?.push([oldVal, k, "set"]);
865
- }
866
- }
867
- this.#removeItemSize(index);
868
- this.#addItemSize(index, size, status);
869
- this.#valList[index] = v;
870
- if (status) {
871
- status.set = "replace";
872
- const oldValue = oldVal && this.#isBackgroundFetch(oldVal) ? oldVal.__staleWhileFetching : oldVal;
873
- if (oldValue !== void 0)
874
- status.oldValue = oldValue;
875
- }
876
- } else if (status) {
877
- status.set = "update";
878
- }
879
- if (this.#hasOnInsert) {
880
- this.onInsert?.(v, k, v === oldVal ? "update" : "replace");
881
- }
882
- }
883
- if (ttl !== 0 && !this.#ttls) {
884
- this.#initializeTTLTracking();
885
- }
886
- if (this.#ttls) {
887
- if (!noUpdateTTL) {
888
- this.#setItemTTL(index, ttl, start);
889
- }
890
- if (status)
891
- this.#statusTTL(status, index);
892
- }
893
- if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
894
- const dt = this.#disposed;
895
- let task;
896
- while (task = dt?.shift()) {
897
- this.#disposeAfter?.(...task);
898
- }
899
- }
900
- return this;
901
- }
902
- /**
903
- * Evict the least recently used item, returning its value or
904
- * `undefined` if cache is empty.
905
- */
906
- pop() {
907
- try {
908
- while (this.#size) {
909
- const val = this.#valList[this.#head];
910
- this.#evict(true);
911
- if (this.#isBackgroundFetch(val)) {
912
- if (val.__staleWhileFetching) {
913
- return val.__staleWhileFetching;
914
- }
915
- } else if (val !== void 0) {
916
- return val;
917
- }
918
- }
919
- } finally {
920
- if (this.#hasDisposeAfter && this.#disposed) {
921
- const dt = this.#disposed;
922
- let task;
923
- while (task = dt?.shift()) {
924
- this.#disposeAfter?.(...task);
925
- }
926
- }
927
- }
928
- }
929
- #evict(free) {
930
- const head = this.#head;
931
- const k = this.#keyList[head];
932
- const v = this.#valList[head];
933
- if (this.#hasFetchMethod && this.#isBackgroundFetch(v)) {
934
- v.__abortController.abort(new Error("evicted"));
935
- } else if (this.#hasDispose || this.#hasDisposeAfter) {
936
- if (this.#hasDispose) {
937
- this.#dispose?.(v, k, "evict");
938
- }
939
- if (this.#hasDisposeAfter) {
940
- this.#disposed?.push([v, k, "evict"]);
941
- }
942
- }
943
- this.#removeItemSize(head);
944
- if (free) {
945
- this.#keyList[head] = void 0;
946
- this.#valList[head] = void 0;
947
- this.#free.push(head);
948
- }
949
- if (this.#size === 1) {
950
- this.#head = this.#tail = 0;
951
- this.#free.length = 0;
952
- } else {
953
- this.#head = this.#next[head];
954
- }
955
- this.#keyMap.delete(k);
956
- this.#size--;
957
- return head;
958
- }
959
- /**
960
- * Check if a key is in the cache, without updating the recency of use.
961
- * Will return false if the item is stale, even though it is technically
962
- * in the cache.
963
- *
964
- * Check if a key is in the cache, without updating the recency of
965
- * use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
966
- * to `true` in either the options or the constructor.
967
- *
968
- * Will return `false` if the item is stale, even though it is technically in
969
- * the cache. The difference can be determined (if it matters) by using a
970
- * `status` argument, and inspecting the `has` field.
971
- *
972
- * Will not update item age unless
973
- * {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
974
- */
975
- has(k, hasOptions = {}) {
976
- const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
977
- const index = this.#keyMap.get(k);
978
- if (index !== void 0) {
979
- const v = this.#valList[index];
980
- if (this.#isBackgroundFetch(v) && v.__staleWhileFetching === void 0) {
981
- return false;
982
- }
983
- if (!this.#isStale(index)) {
984
- if (updateAgeOnHas) {
985
- this.#updateItemAge(index);
986
- }
987
- if (status) {
988
- status.has = "hit";
989
- this.#statusTTL(status, index);
990
- }
991
- return true;
992
- } else if (status) {
993
- status.has = "stale";
994
- this.#statusTTL(status, index);
995
- }
996
- } else if (status) {
997
- status.has = "miss";
998
- }
999
- return false;
1000
- }
1001
- /**
1002
- * Like {@link LRUCache#get} but doesn't update recency or delete stale
1003
- * items.
1004
- *
1005
- * Returns `undefined` if the item is stale, unless
1006
- * {@link LRUCache.OptionsBase.allowStale} is set.
1007
- */
1008
- peek(k, peekOptions = {}) {
1009
- const { allowStale = this.allowStale } = peekOptions;
1010
- const index = this.#keyMap.get(k);
1011
- if (index === void 0 || !allowStale && this.#isStale(index)) {
1012
- return;
1013
- }
1014
- const v = this.#valList[index];
1015
- return this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
1016
- }
1017
- #backgroundFetch(k, index, options, context) {
1018
- const v = index === void 0 ? void 0 : this.#valList[index];
1019
- if (this.#isBackgroundFetch(v)) {
1020
- return v;
1021
- }
1022
- const ac = new AC();
1023
- const { signal } = options;
1024
- signal?.addEventListener("abort", () => ac.abort(signal.reason), {
1025
- signal: ac.signal
1026
- });
1027
- const fetchOpts = {
1028
- signal: ac.signal,
1029
- options,
1030
- context
1031
- };
1032
- const cb = (v2, updateCache = false) => {
1033
- const { aborted } = ac.signal;
1034
- const ignoreAbort = options.ignoreFetchAbort && v2 !== void 0;
1035
- if (options.status) {
1036
- if (aborted && !updateCache) {
1037
- options.status.fetchAborted = true;
1038
- options.status.fetchError = ac.signal.reason;
1039
- if (ignoreAbort)
1040
- options.status.fetchAbortIgnored = true;
1041
- } else {
1042
- options.status.fetchResolved = true;
1043
- }
1044
- }
1045
- if (aborted && !ignoreAbort && !updateCache) {
1046
- return fetchFail(ac.signal.reason);
1047
- }
1048
- const bf2 = p;
1049
- const vl = this.#valList[index];
1050
- if (vl === p || ignoreAbort && updateCache && vl === void 0) {
1051
- if (v2 === void 0) {
1052
- if (bf2.__staleWhileFetching !== void 0) {
1053
- this.#valList[index] = bf2.__staleWhileFetching;
1054
- } else {
1055
- this.#delete(k, "fetch");
1056
- }
1057
- } else {
1058
- if (options.status)
1059
- options.status.fetchUpdated = true;
1060
- this.set(k, v2, fetchOpts.options);
1061
- }
1062
- }
1063
- return v2;
1064
- };
1065
- const eb = (er) => {
1066
- if (options.status) {
1067
- options.status.fetchRejected = true;
1068
- options.status.fetchError = er;
1069
- }
1070
- return fetchFail(er);
1071
- };
1072
- const fetchFail = (er) => {
1073
- const { aborted } = ac.signal;
1074
- const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
1075
- const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
1076
- const noDelete = allowStale || options.noDeleteOnFetchRejection;
1077
- const bf2 = p;
1078
- if (this.#valList[index] === p) {
1079
- const del = !noDelete || bf2.__staleWhileFetching === void 0;
1080
- if (del) {
1081
- this.#delete(k, "fetch");
1082
- } else if (!allowStaleAborted) {
1083
- this.#valList[index] = bf2.__staleWhileFetching;
1084
- }
1085
- }
1086
- if (allowStale) {
1087
- if (options.status && bf2.__staleWhileFetching !== void 0) {
1088
- options.status.returnedStale = true;
1089
- }
1090
- return bf2.__staleWhileFetching;
1091
- } else if (bf2.__returned === bf2) {
1092
- throw er;
1093
- }
1094
- };
1095
- const pcall = (res, rej) => {
1096
- const fmp = this.#fetchMethod?.(k, v, fetchOpts);
1097
- if (fmp && fmp instanceof Promise) {
1098
- fmp.then((v2) => res(v2 === void 0 ? void 0 : v2), rej);
1099
- }
1100
- ac.signal.addEventListener("abort", () => {
1101
- if (!options.ignoreFetchAbort || options.allowStaleOnFetchAbort) {
1102
- res(void 0);
1103
- if (options.allowStaleOnFetchAbort) {
1104
- res = (v2) => cb(v2, true);
1105
- }
1106
- }
1107
- });
1108
- };
1109
- if (options.status)
1110
- options.status.fetchDispatched = true;
1111
- const p = new Promise(pcall).then(cb, eb);
1112
- const bf = Object.assign(p, {
1113
- __abortController: ac,
1114
- __staleWhileFetching: v,
1115
- __returned: void 0
1116
- });
1117
- if (index === void 0) {
1118
- this.set(k, bf, { ...fetchOpts.options, status: void 0 });
1119
- index = this.#keyMap.get(k);
1120
- } else {
1121
- this.#valList[index] = bf;
1122
- }
1123
- return bf;
1124
- }
1125
- #isBackgroundFetch(p) {
1126
- if (!this.#hasFetchMethod)
1127
- return false;
1128
- const b = p;
1129
- return !!b && b instanceof Promise && b.hasOwnProperty("__staleWhileFetching") && b.__abortController instanceof AC;
1130
- }
1131
- async fetch(k, fetchOptions = {}) {
1132
- const {
1133
- // get options
1134
- allowStale = this.allowStale,
1135
- updateAgeOnGet = this.updateAgeOnGet,
1136
- noDeleteOnStaleGet = this.noDeleteOnStaleGet,
1137
- // set options
1138
- ttl = this.ttl,
1139
- noDisposeOnSet = this.noDisposeOnSet,
1140
- size = 0,
1141
- sizeCalculation = this.sizeCalculation,
1142
- noUpdateTTL = this.noUpdateTTL,
1143
- // fetch exclusive options
1144
- noDeleteOnFetchRejection = this.noDeleteOnFetchRejection,
1145
- allowStaleOnFetchRejection = this.allowStaleOnFetchRejection,
1146
- ignoreFetchAbort = this.ignoreFetchAbort,
1147
- allowStaleOnFetchAbort = this.allowStaleOnFetchAbort,
1148
- context,
1149
- forceRefresh = false,
1150
- status,
1151
- signal
1152
- } = fetchOptions;
1153
- if (!this.#hasFetchMethod) {
1154
- if (status)
1155
- status.fetch = "get";
1156
- return this.get(k, {
1157
- allowStale,
1158
- updateAgeOnGet,
1159
- noDeleteOnStaleGet,
1160
- status
1161
- });
1162
- }
1163
- const options = {
1164
- allowStale,
1165
- updateAgeOnGet,
1166
- noDeleteOnStaleGet,
1167
- ttl,
1168
- noDisposeOnSet,
1169
- size,
1170
- sizeCalculation,
1171
- noUpdateTTL,
1172
- noDeleteOnFetchRejection,
1173
- allowStaleOnFetchRejection,
1174
- allowStaleOnFetchAbort,
1175
- ignoreFetchAbort,
1176
- status,
1177
- signal
1178
- };
1179
- let index = this.#keyMap.get(k);
1180
- if (index === void 0) {
1181
- if (status)
1182
- status.fetch = "miss";
1183
- const p = this.#backgroundFetch(k, index, options, context);
1184
- return p.__returned = p;
1185
- } else {
1186
- const v = this.#valList[index];
1187
- if (this.#isBackgroundFetch(v)) {
1188
- const stale = allowStale && v.__staleWhileFetching !== void 0;
1189
- if (status) {
1190
- status.fetch = "inflight";
1191
- if (stale)
1192
- status.returnedStale = true;
1193
- }
1194
- return stale ? v.__staleWhileFetching : v.__returned = v;
1195
- }
1196
- const isStale = this.#isStale(index);
1197
- if (!forceRefresh && !isStale) {
1198
- if (status)
1199
- status.fetch = "hit";
1200
- this.#moveToTail(index);
1201
- if (updateAgeOnGet) {
1202
- this.#updateItemAge(index);
1203
- }
1204
- if (status)
1205
- this.#statusTTL(status, index);
1206
- return v;
1207
- }
1208
- const p = this.#backgroundFetch(k, index, options, context);
1209
- const hasStale = p.__staleWhileFetching !== void 0;
1210
- const staleVal = hasStale && allowStale;
1211
- if (status) {
1212
- status.fetch = isStale ? "stale" : "refresh";
1213
- if (staleVal && isStale)
1214
- status.returnedStale = true;
1215
- }
1216
- return staleVal ? p.__staleWhileFetching : p.__returned = p;
1217
- }
1218
- }
1219
- async forceFetch(k, fetchOptions = {}) {
1220
- const v = await this.fetch(k, fetchOptions);
1221
- if (v === void 0)
1222
- throw new Error("fetch() returned undefined");
1223
- return v;
1224
- }
1225
- memo(k, memoOptions = {}) {
1226
- const memoMethod = this.#memoMethod;
1227
- if (!memoMethod) {
1228
- throw new Error("no memoMethod provided to constructor");
1229
- }
1230
- const { context, forceRefresh, ...options } = memoOptions;
1231
- const v = this.get(k, options);
1232
- if (!forceRefresh && v !== void 0)
1233
- return v;
1234
- const vv = memoMethod(k, v, {
1235
- options,
1236
- context
1237
- });
1238
- this.set(k, vv, options);
1239
- return vv;
1240
- }
1241
- /**
1242
- * Return a value from the cache. Will update the recency of the cache
1243
- * entry found.
1244
- *
1245
- * If the key is not found, get() will return `undefined`.
1246
- */
1247
- get(k, getOptions = {}) {
1248
- const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status } = getOptions;
1249
- const index = this.#keyMap.get(k);
1250
- if (index !== void 0) {
1251
- const value = this.#valList[index];
1252
- const fetching = this.#isBackgroundFetch(value);
1253
- if (status)
1254
- this.#statusTTL(status, index);
1255
- if (this.#isStale(index)) {
1256
- if (status)
1257
- status.get = "stale";
1258
- if (!fetching) {
1259
- if (!noDeleteOnStaleGet) {
1260
- this.#delete(k, "expire");
1261
- }
1262
- if (status && allowStale)
1263
- status.returnedStale = true;
1264
- return allowStale ? value : void 0;
1265
- } else {
1266
- if (status && allowStale && value.__staleWhileFetching !== void 0) {
1267
- status.returnedStale = true;
1268
- }
1269
- return allowStale ? value.__staleWhileFetching : void 0;
1270
- }
1271
- } else {
1272
- if (status)
1273
- status.get = "hit";
1274
- if (fetching) {
1275
- return value.__staleWhileFetching;
1276
- }
1277
- this.#moveToTail(index);
1278
- if (updateAgeOnGet) {
1279
- this.#updateItemAge(index);
1280
- }
1281
- return value;
1282
- }
1283
- } else if (status) {
1284
- status.get = "miss";
1285
- }
1286
- }
1287
- #connect(p, n) {
1288
- this.#prev[n] = p;
1289
- this.#next[p] = n;
1290
- }
1291
- #moveToTail(index) {
1292
- if (index !== this.#tail) {
1293
- if (index === this.#head) {
1294
- this.#head = this.#next[index];
1295
- } else {
1296
- this.#connect(this.#prev[index], this.#next[index]);
1297
- }
1298
- this.#connect(this.#tail, index);
1299
- this.#tail = index;
1300
- }
1301
- }
1302
- /**
1303
- * Deletes a key out of the cache.
1304
- *
1305
- * Returns true if the key was deleted, false otherwise.
1306
- */
1307
- delete(k) {
1308
- return this.#delete(k, "delete");
1309
- }
1310
- #delete(k, reason) {
1311
- let deleted = false;
1312
- if (this.#size !== 0) {
1313
- const index = this.#keyMap.get(k);
1314
- if (index !== void 0) {
1315
- deleted = true;
1316
- if (this.#size === 1) {
1317
- this.#clear(reason);
1318
- } else {
1319
- this.#removeItemSize(index);
1320
- const v = this.#valList[index];
1321
- if (this.#isBackgroundFetch(v)) {
1322
- v.__abortController.abort(new Error("deleted"));
1323
- } else if (this.#hasDispose || this.#hasDisposeAfter) {
1324
- if (this.#hasDispose) {
1325
- this.#dispose?.(v, k, reason);
1326
- }
1327
- if (this.#hasDisposeAfter) {
1328
- this.#disposed?.push([v, k, reason]);
1329
- }
1330
- }
1331
- this.#keyMap.delete(k);
1332
- this.#keyList[index] = void 0;
1333
- this.#valList[index] = void 0;
1334
- if (index === this.#tail) {
1335
- this.#tail = this.#prev[index];
1336
- } else if (index === this.#head) {
1337
- this.#head = this.#next[index];
1338
- } else {
1339
- const pi = this.#prev[index];
1340
- this.#next[pi] = this.#next[index];
1341
- const ni = this.#next[index];
1342
- this.#prev[ni] = this.#prev[index];
1343
- }
1344
- this.#size--;
1345
- this.#free.push(index);
1346
- }
1347
- }
1348
- }
1349
- if (this.#hasDisposeAfter && this.#disposed?.length) {
1350
- const dt = this.#disposed;
1351
- let task;
1352
- while (task = dt?.shift()) {
1353
- this.#disposeAfter?.(...task);
1354
- }
1355
- }
1356
- return deleted;
1357
- }
1358
- /**
1359
- * Clear the cache entirely, throwing away all values.
1360
- */
1361
- clear() {
1362
- return this.#clear("delete");
1363
- }
1364
- #clear(reason) {
1365
- for (const index of this.#rindexes({ allowStale: true })) {
1366
- const v = this.#valList[index];
1367
- if (this.#isBackgroundFetch(v)) {
1368
- v.__abortController.abort(new Error("deleted"));
1369
- } else {
1370
- const k = this.#keyList[index];
1371
- if (this.#hasDispose) {
1372
- this.#dispose?.(v, k, reason);
1373
- }
1374
- if (this.#hasDisposeAfter) {
1375
- this.#disposed?.push([v, k, reason]);
1376
- }
1377
- }
1378
- }
1379
- this.#keyMap.clear();
1380
- this.#valList.fill(void 0);
1381
- this.#keyList.fill(void 0);
1382
- if (this.#ttls && this.#starts) {
1383
- this.#ttls.fill(0);
1384
- this.#starts.fill(0);
1385
- }
1386
- if (this.#sizes) {
1387
- this.#sizes.fill(0);
1388
- }
1389
- this.#head = 0;
1390
- this.#tail = 0;
1391
- this.#free.length = 0;
1392
- this.#calculatedSize = 0;
1393
- this.#size = 0;
1394
- if (this.#hasDisposeAfter && this.#disposed) {
1395
- const dt = this.#disposed;
1396
- let task;
1397
- while (task = dt?.shift()) {
1398
- this.#disposeAfter?.(...task);
1399
- }
1400
- }
1401
- }
1402
- };
1403
-
1404
- // src/utils/unified-router.ts
1405
- var UnifiedRouter = class {
1406
- config;
1407
- rules = /* @__PURE__ */ new Map();
1408
- cache;
1409
- stats;
1410
- initialized = false;
1411
- constructor(config2) {
1412
- this.config = config2;
1413
- this.rules = /* @__PURE__ */ new Map();
1414
- this.cache = new LRUCache({
1415
- max: config2.cache?.maxSize || 1e3,
1416
- ttl: config2.cache?.ttl || 3e5
1417
- // 5分钟
1418
- });
1419
- this.stats = this.initializeStats();
1420
- this.loadRules();
1421
- this.initialized = true;
1422
- if (this.isDebugEnabled()) {
1423
- console.log(`[DEBUG] [UnifiedRouter] \u521D\u59CB\u5316\u5B8C\u6210`);
1424
- console.log(`[DEBUG] [UnifiedRouter] \u5F15\u64CE\u7C7B\u578B: ${config2.engine}`);
1425
- console.log(`[DEBUG] [UnifiedRouter] \u9ED8\u8BA4\u8DEF\u7531: ${config2.defaultRoute}`);
1426
- console.log(`[DEBUG] [UnifiedRouter] \u8DEF\u7531\u89C4\u5219\u6570: ${config2.rules?.length || 0}`);
1427
- console.log(`[DEBUG] [UnifiedRouter] \u7F13\u5B58\u542F\u7528: ${config2.cache?.enabled !== false}`);
1428
- if (config2.contextThreshold) {
1429
- console.log(`[DEBUG] [UnifiedRouter] \u4E0A\u4E0B\u6587\u9608\u503C: default=${config2.contextThreshold.default}, longContext=${config2.contextThreshold.longContext}`);
1430
- }
1431
- }
1432
- }
1433
- /**
1434
- * 主要路由方法 - 实现IRouter接口
1435
- */
1436
- async route(req, tokenCount2, config2, lastUsage) {
1437
- const routeResult = await this.evaluate(req, tokenCount2, config2, lastUsage);
1438
- return routeResult.route;
1439
- }
1440
- /**
1441
- * 详细路由评估方法
1442
- */
1443
- async evaluate(req, tokenCount2, config2, lastUsage) {
1444
- const startTime = Date.now();
1445
- console.log(`\u{1F680} [UNIFIED ROUTER] \u5F00\u59CB\u8BC4\u4F30\u8DEF\u7531\uFF0C\u8BF7\u6C42\u6A21\u578B: ${req.body?.model}`);
1446
- try {
1447
- const context = {
1448
- tokenCount: tokenCount2,
1449
- messages: req.body?.messages || [],
1450
- system: req.body?.system || [],
1451
- tools: req.body?.tools || [],
1452
- sessionId: req.sessionId,
1453
- lastUsage,
1454
- log: req.log || { info: console.log, error: console.error },
1455
- event: req.event,
1456
- req
1457
- };
1458
- const matchResult = await this.evaluateRules(context);
1459
- let finalRoute = matchResult.matched && matchResult.action ? matchResult.action.route : this.config.defaultRoute;
1460
- if (finalRoute.includes("${")) {
1461
- finalRoute = this.processVariableSubstitution(finalRoute, req, context);
1462
- }
1463
- const cacheKey = this.generateCacheKey(req, tokenCount2, finalRoute);
1464
- if (this.config.cache?.enabled !== false) {
1465
- const cachedResult = this.cache.get(cacheKey);
1466
- if (cachedResult) {
1467
- this.updateStats(cachedResult, true);
1468
- return { ...cachedResult, fromCache: true };
1469
- }
1470
- }
1471
- const result = {
1472
- route: finalRoute,
1473
- matchedRule: matchResult.ruleName,
1474
- transformers: matchResult.action?.transformers || [],
1475
- decisionTime: Date.now() - startTime,
1476
- fromCache: false,
1477
- metadata: {
1478
- context: {
1479
- tokenCount: tokenCount2,
1480
- hasTools: context.tools.length > 0,
1481
- hasThinking: req.body?.thinking || false,
1482
- sessionId: context.sessionId
1483
- }
1484
- }
1485
- };
1486
- if (this.config.cache?.enabled !== false) {
1487
- this.cache.set(cacheKey, result);
1488
- }
1489
- this.updateStats(result, false);
1490
- return result;
1491
- } catch (error) {
1492
- const logObj = req.log || { error: console.error };
1493
- logObj.error?.(`\u8DEF\u7531\u8BC4\u4F30\u5931\u8D25: ${error.message}`);
1494
- return {
1495
- route: this.config.defaultRoute,
1496
- decisionTime: Date.now() - startTime,
1497
- fromCache: false,
1498
- transformers: [],
1499
- metadata: {
1500
- error: error.message,
1501
- fallback: true
1502
- }
1503
- };
1504
- }
1505
- }
1506
- /**
1507
- * 评估所有路由规则
1508
- */
1509
- async evaluateRules(context) {
1510
- console.log(`\u{1F3AF} [EVALUATE RULES] \u5F00\u59CB\u8BC4\u4F30\u8DEF\u7531\u89C4\u5219\u65B9\u6CD5\u88AB\u8C03\u7528\uFF01`);
1511
- console.log(`\u{1F3AF} [EVALUATE RULES] \u8BF7\u6C42\u6A21\u578B: ${context.req?.body?.model}`);
1512
- console.log(`\u{1F3AF} [EVALUATE RULES] context.req\u5B58\u5728: ${!!context.req}`);
1513
- console.log(`\u{1F3AF} [EVALUATE RULES] context.req.body\u5B58\u5728: ${!!context.req?.body}`);
1514
- const evaluations = [];
1515
- const allRules = Array.from(this.rules.values());
1516
- console.log(`\u{1F50D} [RULE ENGINE] \u603B\u89C4\u5219\u6570: ${allRules.length}`);
1517
- allRules.forEach((rule, index) => {
1518
- console.log(`\u{1F50D} [RULE ${index}] ${rule.name}: enabled=${rule.enabled}, priority=${rule.priority}`);
1519
- });
1520
- const sortedRules = allRules.filter((rule) => rule.enabled !== false).sort((a, b) => (b.priority || 0) - (a.priority || 0));
1521
- console.log(`\u{1F50D} [RULE ENGINE] \u5F00\u59CB\u8BC4\u4F30 ${sortedRules.length} \u4E2A\u8DEF\u7531\u89C4\u5219 (tokenCount: ${tokenCount})`);
1522
- console.log(`\u{1F50D} [RULE ENGINE] sortedRules\u6570\u7EC4:`, sortedRules.map((r) => ({ name: r.name, enabled: r.enabled, priority: r.priority })));
1523
- this.debugLog("info", `\u5F00\u59CB\u8BC4\u4F30 ${sortedRules.length} \u4E2A\u8DEF\u7531\u89C4\u5219`, { tokenCount });
1524
- if (sortedRules.length === 0) {
1525
- console.error(`\u274C [RULE ENGINE] \u9519\u8BEF\uFF1AsortedRules\u6570\u7EC4\u4E3A\u7A7A\uFF01`);
1526
- return {
1527
- matched: false,
1528
- evaluations: []
1529
- };
1530
- }
1531
- for (const rule of sortedRules) {
1532
- console.log(`\u{1F50E} [EVALUATING RULE] \u8BC4\u4F30\u89C4\u5219: ${rule.name} (\u4F18\u5148\u7EA7: ${rule.priority})`);
1533
- context.log?.info?.(`\u{1F50E} [EVALUATING RULE] \u8BC4\u4F30\u89C4\u5219: ${rule.name} (\u4F18\u5148\u7EA7: ${rule.priority})`);
1534
- const evaluation = await this.evaluateCondition(rule.condition, context);
1535
- evaluations.push(evaluation);
1536
- const matchStatus = evaluation.matches ? "\u2705 \u5339\u914D" : "\u274C \u4E0D\u5339\u914D";
1537
- console.log(`\u{1F4CA} [RULE RESULT] ${rule.name}: ${matchStatus} (\u8BC4\u4F30\u8017\u65F6: ${evaluation.evaluationTime}ms)`);
1538
- context.log?.info?.(`\u{1F4CA} [RULE RESULT] ${rule.name}: ${matchStatus} (\u8BC4\u4F30\u8017\u65F6: ${evaluation.evaluationTime}ms)`);
1539
- if (evaluation.matches) {
1540
- console.log(`\u{1F3AF} [RULE MATCHED] \u89C4\u5219 ${rule.name} \u5339\u914D\u6210\u529F\uFF0C\u505C\u6B62\u540E\u7EED\u89C4\u5219\u8BC4\u4F30`);
1541
- context.log?.info?.(`\u{1F3AF} [RULE MATCHED] \u89C4\u5219 ${rule.name} \u5339\u914D\u6210\u529F\uFF0C\u505C\u6B62\u540E\u7EED\u89C4\u5219\u8BC4\u4F30`);
1542
- return {
1543
- matched: true,
1544
- ruleName: rule.name,
1545
- action: rule.action,
1546
- priority: rule.priority,
1547
- evaluations
1548
- };
1549
- }
1550
- }
1551
- console.log(`\u{1F50D} [RULE ENGINE] \u6CA1\u6709\u89C4\u5219\u5339\u914D\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u8DEF\u7531`);
1552
- context.log?.info?.(`\u{1F504} [RULE ENGINE] \u6240\u6709\u89C4\u5219\u8BC4\u4F30\u5B8C\u6210\uFF0C\u65E0\u5339\u914D\u89C4\u5219\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u8DEF\u7531`);
1553
- return {
1554
- matched: false,
1555
- evaluations
1556
- };
1557
- }
1558
- /**
1559
- * 评估单个条件
1560
- */
1561
- async evaluateCondition(condition, context) {
1562
- const startTime = Date.now();
1563
- try {
1564
- let matches = false;
1565
- let value;
1566
- switch (condition.type) {
1567
- case "tokenThreshold":
1568
- value = context.tokenCount;
1569
- const thresholdValue = condition.value;
1570
- const isDefaultThreshold = thresholdValue === this.getTokenThreshold("default");
1571
- const isLongContextThreshold = thresholdValue === this.getTokenThreshold("longContext");
1572
- this.debugLog("debug", "\u8BC4\u4F30 token \u9608\u503C\u6761\u4EF6", {
1573
- tokenCount: value,
1574
- threshold: thresholdValue,
1575
- isDefaultThreshold,
1576
- isLongContextThreshold,
1577
- operator: condition.operator || "gt"
1578
- });
1579
- matches = this.compareNumbers(value, thresholdValue, condition.operator || "gt");
1580
- break;
1581
- case "modelContains":
1582
- value = context.req?.body?.model || "";
1583
- matches = this.compareStrings(value, condition.value, condition.operator || "contains");
1584
- break;
1585
- case "toolExists":
1586
- value = context.tools.some(
1587
- (tool) => tool.type?.includes(condition.value) || tool.function?.name?.includes(condition.value)
1588
- );
1589
- matches = condition.operator === "exists" ? value : value === condition.value;
1590
- break;
1591
- case "fieldExists":
1592
- const fieldValue = this.getFieldValue(context.req?.body, condition.field);
1593
- value = fieldValue;
1594
- matches = condition.operator === "exists" ? fieldValue !== void 0 && fieldValue !== null : this.compareValues(fieldValue, condition.value, condition.operator || "eq");
1595
- break;
1596
- case "custom":
1597
- matches = await this.evaluateCustomCondition(condition, context);
1598
- value = matches;
1599
- break;
1600
- default:
1601
- throw new Error(`\u4E0D\u652F\u6301\u7684\u6761\u4EF6\u7C7B\u578B: ${condition.type}`);
1602
- }
1603
- return {
1604
- matches,
1605
- value,
1606
- evaluationTime: Date.now() - startTime
1607
- };
1608
- } catch (error) {
1609
- return {
1610
- matches: false,
1611
- evaluationTime: Date.now() - startTime,
1612
- error: error.message
1613
- };
1614
- }
1615
- }
1616
- /**
1617
- * 数字比较
1618
- */
1619
- compareNumbers(actual, expected, operator) {
1620
- switch (operator) {
1621
- case "gt":
1622
- return actual > expected;
1623
- case "lt":
1624
- return actual < expected;
1625
- case "eq":
1626
- return actual === expected;
1627
- default:
1628
- return false;
1629
- }
1630
- }
1631
- /**
1632
- * 字符串比较
1633
- */
1634
- compareStrings(actual, expected, operator) {
1635
- switch (operator) {
1636
- case "contains":
1637
- return actual.includes(expected);
1638
- case "startsWith":
1639
- return actual.startsWith(expected);
1640
- case "eq":
1641
- return actual === expected;
1642
- default:
1643
- return false;
1644
- }
1645
- }
1646
- /**
1647
- * 通用值比较
1648
- */
1649
- compareValues(actual, expected, operator) {
1650
- switch (operator) {
1651
- case "eq":
1652
- return actual === expected;
1653
- case "contains":
1654
- return Array.isArray(actual) ? actual.includes(expected) : String(actual).includes(expected);
1655
- default:
1656
- return false;
1657
- }
1658
- }
1659
- /**
1660
- * 获取对象字段值(支持嵌套路径)
1661
- */
1662
- getFieldValue(obj, fieldPath) {
1663
- const parts = fieldPath.split(".");
1664
- let current = obj;
1665
- for (const part of parts) {
1666
- if (current && typeof current === "object" && part in current) {
1667
- current = current[part];
1668
- } else {
1669
- return void 0;
1670
- }
1671
- }
1672
- return current;
1673
- }
1674
- /**
1675
- * 评估自定义条件
1676
- */
1677
- async evaluateCustomCondition(condition, context) {
1678
- const { customFunction } = condition;
1679
- console.log(`\u{1F50D} [CUSTOM CONDITION] \u8BC4\u4F30\u81EA\u5B9A\u4E49\u6761\u4EF6: ${customFunction}`);
1680
- console.log(`\u{1F50D} [CUSTOM CONDITION] \u8BF7\u6C42\u6A21\u578B: "${context.req?.body?.model}"`);
1681
- switch (customFunction) {
1682
- case "modelContainsComma":
1683
- const hasComma = context.req?.body?.model?.includes(",") || false;
1684
- console.log(`\u{1F50D} [CUSTOM CONDITION] modelContainsComma \u7ED3\u679C: ${hasComma}`);
1685
- return hasComma;
1686
- case "directModelMapping":
1687
- const model = context.req?.body?.model;
1688
- const isDirectModel = model && !model.includes(",") && model.trim() !== "";
1689
- console.log(`\u{1F50D} [CUSTOM CONDITION] directModelMapping \u7ED3\u679C: ${isDirectModel} (model="${model}")`);
1690
- return isDirectModel;
1691
- default:
1692
- console.log(`\u26A0\uFE0F [CUSTOM CONDITION] \u672A\u77E5\u7684\u81EA\u5B9A\u4E49\u6761\u4EF6\u51FD\u6570: ${customFunction}`);
1693
- context.log?.warn?.(`\u672A\u77E5\u7684\u81EA\u5B9A\u4E49\u6761\u4EF6\u51FD\u6570: ${customFunction}`);
1694
- return false;
1695
- }
1696
- }
1697
- /**
1698
- * 生成缓存键
1699
- */
1700
- /**
1701
- * 处理变量替换
1702
- */
1703
- processVariableSubstitution(route, req, context) {
1704
- let processedRoute = route;
1705
- if (processedRoute.includes("${userModel}")) {
1706
- const userModel = req.body?.model;
1707
- if (userModel) {
1708
- processedRoute = processedRoute.replace(/\$\{userModel\}/g, userModel);
1709
- } else {
1710
- this.debugLog("\u8B66\u544A: ${userModel} \u53D8\u91CF\u66FF\u6362\u5931\u8D25\uFF0C\u672A\u627E\u5230\u539F\u59CB\u7528\u6237\u6A21\u578B");
1711
- processedRoute = this.config.defaultRoute;
1712
- }
1713
- }
1714
- if (processedRoute.includes("${subagent}")) {
1715
- const systemText = req.body?.system?.[1]?.text || "";
1716
- const match = systemText.match(/<CCR-SUBAGENT-MODEL>(.*?)<\/CCR-SUBAGENT-MODEL>/s);
1717
- if (match && match[1]) {
1718
- processedRoute = processedRoute.replace(/\$\{subagent\}/g, match[1]);
1719
- } else {
1720
- this.debugLog("\u8B66\u544A: ${subagent} \u53D8\u91CF\u66FF\u6362\u5931\u8D25\uFF0C\u672A\u627E\u5230\u5B50\u4EE3\u7406\u6A21\u578B\u6807\u8BB0");
1721
- processedRoute = this.config.defaultRoute;
1722
- }
1723
- }
1724
- if (processedRoute.includes("${mappedModel}")) {
1725
- const userModel = req.body?.model;
1726
- if (userModel && !userModel.includes(",")) {
1727
- const mappedRoute = this.mapDirectModelToProvider(userModel, req);
1728
- if (mappedRoute) {
1729
- processedRoute = processedRoute.replace(/\$\{mappedModel\}/g, mappedRoute);
1730
- } else {
1731
- this.debugLog(`\u8B66\u544A: ${mappedModel} \u53D8\u91CF\u66FF\u6362\u5931\u8D25\uFF0C\u672A\u627E\u5230\u6A21\u578B ${userModel} \u7684\u6620\u5C04`);
1732
- processedRoute = this.config.defaultRoute;
1733
- }
1734
- } else {
1735
- this.debugLog("\u8B66\u544A: ${mappedModel} \u53D8\u91CF\u66FF\u6362\u5931\u8D25\uFF0C\u7528\u6237\u6A21\u578B\u683C\u5F0F\u4E0D\u6B63\u786E");
1736
- processedRoute = this.config.defaultRoute;
1737
- }
1738
- }
1739
- this.debugLog(`\u53D8\u91CF\u66FF\u6362: ${route} -> ${processedRoute}`);
1740
- return processedRoute;
1741
- }
1742
- /**
1743
- * 将直接模型名映射到provider,model格式
1744
- */
1745
- mapDirectModelToProvider(modelName, req) {
1746
- const providers = req.config?.Providers || [];
1747
- const matchedProvider = providers.find(
1748
- (provider) => provider.name.toLowerCase() === modelName.toLowerCase()
1749
- );
1750
- if (matchedProvider) {
1751
- if (matchedProvider.models && matchedProvider.models.length > 0) {
1752
- this.debugLog(`\u76F4\u63A5\u6A21\u578B\u6620\u5C04: ${modelName} -> ${matchedProvider.name},${matchedProvider.models[0]}`);
1753
- return `${matchedProvider.name},${matchedProvider.models[0]}`;
1754
- } else if (matchedProvider.model) {
1755
- this.debugLog(`\u76F4\u63A5\u6A21\u578B\u6620\u5C04: ${modelName} -> ${matchedProvider.name},${matchedProvider.model}`);
1756
- return `${matchedProvider.name},${matchedProvider.model}`;
1757
- }
1758
- }
1759
- const commonMappings = {
1760
- "haiku": "haiku,glm-4.5-air",
1761
- "sonnet": "sonnet,glm-4.6",
1762
- "opus": "opus,glm-4.6",
1763
- "deepseek": "deepseek,deepseek-chat",
1764
- "gemini": "gemini,gemini-2.5-flash",
1765
- "ollama": "ollama,qwen2.5-coder:latest"
1766
- };
1767
- const mappedRoute = commonMappings[modelName.toLowerCase()];
1768
- if (mappedRoute) {
1769
- this.debugLog(`\u4F7F\u7528\u5E38\u89C1\u6A21\u578B\u6620\u5C04: ${modelName} -> ${mappedRoute}`);
1770
- return mappedRoute;
1771
- }
1772
- this.debugLog(`\u672A\u627E\u5230\u6A21\u578B ${modelName} \u7684\u6620\u5C04`);
1773
- return null;
1774
- }
1775
- generateCacheKey(req, tokenCount2, finalRoute) {
1776
- const keyData = {
1777
- model: req.body?.model,
1778
- finalRoute: finalRoute || req.body?.model,
1779
- tokenCount: tokenCount2,
1780
- hasTools: Array.isArray(req.body?.tools) && req.body.tools.length > 0,
1781
- thinking: req.body?.thinking || false,
1782
- sessionId: req.sessionId
1783
- };
1784
- return Buffer.from(JSON.stringify(keyData)).toString("base64");
1785
- }
1786
- /**
1787
- * 加载路由规则
1788
- */
1789
- loadRules() {
1790
- for (const rule of this.config.rules) {
1791
- this.rules.set(rule.name, rule);
1792
- }
1793
- }
1794
- /**
1795
- * 初始化统计信息
1796
- */
1797
- initializeStats() {
1798
- return {
1799
- totalRoutes: 0,
1800
- ruleMatches: {},
1801
- cacheHits: 0,
1802
- cacheMisses: 0,
1803
- avgRouteTime: 0,
1804
- groupStats: {}
1805
- };
1806
- }
1807
- /**
1808
- * 更新统计信息
1809
- */
1810
- updateStats(result, fromCache) {
1811
- this.stats.totalRoutes++;
1812
- if (fromCache) {
1813
- this.stats.cacheHits++;
1814
- } else {
1815
- this.stats.cacheMisses++;
1816
- if (result.matchedRule) {
1817
- this.stats.ruleMatches[result.matchedRule] = (this.stats.ruleMatches[result.matchedRule] || 0) + 1;
1818
- }
1819
- }
1820
- this.stats.avgRouteTime = (this.stats.avgRouteTime * (this.stats.totalRoutes - 1) + result.decisionTime) / this.stats.totalRoutes;
1821
- }
1822
- // IUnifiedRouter 接口方法实现
1823
- addRule(rule) {
1824
- this.rules.set(rule.name, rule);
1825
- const existingIndex = this.config.rules.findIndex((r) => r.name === rule.name);
1826
- if (existingIndex >= 0) {
1827
- this.config.rules[existingIndex] = rule;
1828
- } else {
1829
- this.config.rules.push(rule);
1830
- }
1831
- }
1832
- removeRule(ruleName) {
1833
- this.rules.delete(ruleName);
1834
- this.config.rules = this.config.rules.filter((r) => r.name !== ruleName);
1835
- }
1836
- toggleRule(ruleName, enabled) {
1837
- const rule = this.rules.get(ruleName);
1838
- if (rule) {
1839
- rule.enabled = enabled;
1840
- const configRule = this.config.rules.find((r) => r.name === ruleName);
1841
- if (configRule) {
1842
- configRule.enabled = enabled;
1843
- }
1844
- }
1845
- }
1846
- getRules() {
1847
- return Array.from(this.rules.values());
1848
- }
1849
- clearCache() {
1850
- this.cache.clear();
1851
- }
1852
- getStats() {
1853
- return { ...this.stats };
1854
- }
1855
- /**
1856
- * 更新配置
1857
- */
1858
- updateConfig(config2) {
1859
- this.config = { ...this.config, ...config2 };
1860
- if (config2.rules) {
1861
- this.loadRules();
1862
- }
1863
- if (config2.cache) {
1864
- this.cache = new LRUCache({
1865
- max: config2.cache.maxSize || 1e3,
1866
- ttl: config2.cache.ttl || 3e5
1867
- });
1868
- }
1869
- }
1870
- /**
1871
- * 获取当前配置
1872
- */
1873
- getConfig() {
1874
- return { ...this.config };
1875
- }
1876
- /**
1877
- * 验证规则配置
1878
- */
1879
- validateRule(rule) {
1880
- const errors = [];
1881
- if (!rule.name) {
1882
- errors.push("\u89C4\u5219\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A");
1883
- }
1884
- if (!rule.condition) {
1885
- errors.push("\u6761\u4EF6\u914D\u7F6E\u4E0D\u80FD\u4E3A\u7A7A");
1886
- } else {
1887
- if (!rule.condition.type) {
1888
- errors.push("\u6761\u4EF6\u7C7B\u578B\u4E0D\u80FD\u4E3A\u7A7A");
1889
- }
1890
- if (rule.condition.value === void 0 && rule.condition.type !== "custom") {
1891
- errors.push("\u6761\u4EF6\u503C\u4E0D\u80FD\u4E3A\u7A7A");
1892
- }
1893
- }
1894
- if (!rule.action) {
1895
- errors.push("\u52A8\u4F5C\u914D\u7F6E\u4E0D\u80FD\u4E3A\u7A7A");
1896
- } else if (!rule.action.route) {
1897
- errors.push("\u76EE\u6807\u8DEF\u7531\u4E0D\u80FD\u4E3A\u7A7A");
1898
- }
1899
- return {
1900
- valid: errors.length === 0,
1901
- errors
1902
- };
1903
- }
1904
- /**
1905
- * 检查是否启用 debug 模式
1906
- */
1907
- isDebugEnabled() {
1908
- return this.config.debug?.enabled === true;
1909
- }
1910
- /**
1911
- * 根据 debug 配置输出日志
1912
- */
1913
- debugLog(level, message, data) {
1914
- if (!this.isDebugEnabled()) return;
1915
- const debugLevel = this.config.debug?.logLevel || "info";
1916
- const levels = { debug: 0, info: 1, warn: 2, error: 3 };
1917
- const currentLevel = levels[debugLevel] || 1;
1918
- const messageLevel = levels[level] || 1;
1919
- if (messageLevel >= currentLevel) {
1920
- const prefix = `[${level.toUpperCase()}] [UnifiedRouter]`;
1921
- if (data) {
1922
- console.log(`${prefix} ${message}`, data);
1923
- } else {
1924
- console.log(`${prefix} ${message}`);
1925
- }
1926
- }
1927
- }
1928
- /**
1929
- * 获取有效的 token 阈值(支持配置化)
1930
- */
1931
- getTokenThreshold(thresholdType) {
1932
- if (this.config.contextThreshold && this.config.contextThreshold[thresholdType]) {
1933
- return this.config.contextThreshold[thresholdType];
1934
- }
1935
- this.debugLog("warn", `contextThreshold.${thresholdType} \u672A\u914D\u7F6E\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u503C`, {
1936
- defaultValue: thresholdType === "longContext" ? 18e4 : 6e4,
1937
- type: thresholdType
1938
- });
1939
- return thresholdType === "longContext" ? 18e4 : 6e4;
1940
- }
1941
- };
1942
-
1943
- // src/reproduce_issue.ts
1944
- var config = {
1945
- engine: "unified",
1946
- defaultRoute: "sonnet,glm-4.6",
1947
- rules: [
1948
- {
1949
- name: "userSpecified",
1950
- condition: {
1951
- type: "custom",
1952
- customFunction: "modelContainsComma"
1953
- },
1954
- action: {
1955
- route: "${userModel}",
1956
- description: "\u7528\u6237\u6307\u5B9A\u8DEF\u7531"
1957
- },
1958
- priority: 200,
1959
- enabled: true
1960
- },
1961
- {
1962
- name: "directModelMapping",
1963
- condition: {
1964
- type: "custom",
1965
- customFunction: "directModelMapping"
1966
- },
1967
- action: {
1968
- route: "${mappedModel}",
1969
- description: "\u76F4\u63A5\u6A21\u578B\u6620\u5C04"
1970
- },
1971
- priority: 190,
1972
- enabled: true
1973
- },
1974
- {
1975
- name: "longContext",
1976
- condition: {
1977
- type: "tokenThreshold",
1978
- value: 18e4,
1979
- operator: "gt"
1980
- },
1981
- action: {
1982
- route: "sonnet,glm-4.6",
1983
- description: "\u957F\u4E0A\u4E0B\u6587\u8DEF\u7531"
1984
- },
1985
- priority: 100,
1986
- enabled: true
1987
- }
1988
- ],
1989
- groups: {
1990
- default: {
1991
- routes: ["default"],
1992
- instances: 1
1993
- }
1994
- },
1995
- contextThreshold: {
1996
- default: 6e4,
1997
- longContext: 18e4
1998
- },
1999
- cache: {
2000
- enabled: false,
2001
- // Disable cache for debugging
2002
- ttl: 3e5,
2003
- maxSize: 1e3
2004
- },
2005
- debug: {
2006
- enabled: true,
2007
- logLevel: "debug"
2008
- }
2009
- };
2010
- async function runTest() {
2011
- const router = new UnifiedRouter(config);
2012
- const req = {
2013
- body: {
2014
- model: "opus",
2015
- messages: []
2016
- },
2017
- log: {
2018
- info: console.log,
2019
- error: console.error,
2020
- warn: console.warn
2021
- }
2022
- };
2023
- console.log("Testing with model: opus");
2024
- const result = await router.route(req, 1e3, config);
2025
- console.log("Result route:", result);
2026
- }
2027
- runTest().catch(console.error);