maxpool 1.24.94 → 1.24.96

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,50 @@
1
+ // The cache figures of one response, as the tail of an activity line:
2
+ // `cache r180k w3k in1k` = tokens read from the prompt cache, written to it, and
3
+ // sent uncached. Whether the cache held is the one thing a request line did not
4
+ // say, and these three numbers decide it (#642, design by @kyjin).
5
+
6
+ // A figure that is not a count reads as zero rather than as a number nobody sent.
7
+ const count = (/** @type {any} */ n) => (Number.isFinite(n) && n > 0 ? Math.round(n) : 0);
8
+
9
+ // 1000 and up in whole k, smaller counts as they are.
10
+ const k = (/** @type {any} */ n) => {
11
+ const c = count(n);
12
+ return c >= 1000 ? `${Math.round(c / 1000)}k` : String(c);
13
+ };
14
+
15
+ // Share of the prompt read from the cache at or above which it held, and
16
+ // below which it missed; in between is partial.
17
+ const HIT_SHARE = 0.9;
18
+ const MISS_SHARE = 0.5;
19
+
20
+ // A prompt this big that neither read nor wrote the cache MISSED it — a cold GLM
21
+ // turn (z.ai reports no cache writes) must read MISS, not 'none' (2026-11-15).
22
+ const COLD_MISS_INPUT = 2000;
23
+
24
+ /**
25
+ * Whether the cache held: the share of the prompt that was read from it,
26
+ * r / (r + w + in). A small response that neither read nor wrote the cache is not
27
+ * a cached request and has no state to judge; a big one is a miss.
28
+ * @param {Record<string, any>|null|undefined} usage the response's usage object
29
+ * @returns {'hit'|'partial'|'miss'|'none'}
30
+ */
31
+ export function cacheUsageState(usage) {
32
+ if (!usage || typeof usage !== 'object') return 'none';
33
+ const r = count(usage.cache_read_input_tokens);
34
+ const w = count(usage.cache_creation_input_tokens);
35
+ if (r === 0 && w === 0) return count(usage.input_tokens) >= COLD_MISS_INPUT ? 'miss' : 'none';
36
+ const ratio = r / (r + w + count(usage.input_tokens));
37
+ return ratio >= HIT_SHARE ? 'hit' : ratio >= MISS_SHARE ? 'partial' : 'miss';
38
+ }
39
+
40
+ /**
41
+ * A miss ends with MISS: the TUI also paints it red, but its log file and the
42
+ * headless log carry no colour, so the word is what is left there to grep.
43
+ * @param {Record<string, any>|null|undefined} usage the response's usage object
44
+ * @returns {string} '' when the response carried no usage
45
+ */
46
+ export function cacheUsageLabel(usage) {
47
+ if (!usage || typeof usage !== 'object' || !Object.keys(usage).length) return '';
48
+ const label = `cache r${k(usage.cache_read_input_tokens)} w${k(usage.cache_creation_input_tokens)} in${k(usage.input_tokens)}`;
49
+ return cacheUsageState(usage) === 'miss' ? `${label} MISS` : label;
50
+ }