maxpool 1.24.67 → 1.24.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/account-manager.js +158 -24
- package/src/capacity-ledger.js +294 -81
- package/src/cloud-login.js +126 -0
- package/src/config.js +36 -0
- package/src/index.js +64 -4
- package/src/pool-link.js +8 -0
- package/src/provider-thinking-scrub.js +330 -0
- package/src/server.js +151 -33
- package/src/tui.js +141 -73
package/src/server.js
CHANGED
|
@@ -3,6 +3,7 @@ import { appendEventLog } from './event-log.js';
|
|
|
3
3
|
import { atomicConfigUpdate } from './config.js';
|
|
4
4
|
import { HOP_HEADER, nextHopCount, safeText } from './pool-link.js';
|
|
5
5
|
import { ThreadOwners, readThreadIntent, threadRefusalBody, isThreadlessAccount } from './thread-gate.js';
|
|
6
|
+
import { ProviderThinkingScrubber, scrubProviderResponseBody, scrubEnabledFor, foreignToolBlockText, describeScrub } from './provider-thinking-scrub.js';
|
|
6
7
|
import { writeFile, mkdir } from 'node:fs/promises';
|
|
7
8
|
import { readFileSync } from 'node:fs';
|
|
8
9
|
import { join } from 'node:path';
|
|
@@ -495,6 +496,9 @@ export function createProxyServer(accountManager, config, hooks = {}) {
|
|
|
495
496
|
}
|
|
496
497
|
requestInfo.sessionKey = headerValue(req.headers, 'x-maxpool-session');
|
|
497
498
|
requestInfo.caller = req._caller || null;
|
|
499
|
+
// Read per request (not captured at boot) so a config flip + seamless reload
|
|
500
|
+
// takes effect on the next request with no restart.
|
|
501
|
+
requestInfo.providerThinkingScrub = Boolean(config.providerThinkingScrub);
|
|
498
502
|
// The CLIENT tells us how long it will wait — the only source that is actually
|
|
499
503
|
// true. A `cc` session exports CLAUDE_STREAM_IDLE_TIMEOUT_MS=3h and forwards it
|
|
500
504
|
// here; a bare `claude` sends nothing and keeps the conservative 240s default,
|
|
@@ -677,6 +681,12 @@ async function forwardRequest(
|
|
|
677
681
|
if (pre.body) {
|
|
678
682
|
body = pre.body;
|
|
679
683
|
requestInfo = { ...requestInfo, thinkingStripped: true };
|
|
684
|
+
// Never hide a repair (Anthropic gateway guidance): this pre-strip rewrites the
|
|
685
|
+
// request body on EVERY turn of a latched session and was silent — the only
|
|
686
|
+
// visible trace was the one-time latch line, so per-turn rewrites (each a cache
|
|
687
|
+
// prefix change) were uncountable. One line per rewrite, same prefix as the
|
|
688
|
+
// reactive repair so a single grep counts both.
|
|
689
|
+
console.log(`[Maxpool] Recovering session on Claude: pre-strip removed ${pre.removed} provider thinking block(s), converted ${pre.converted} provider search block(s) [sess ${String(requestInfo.sessionKey || '?').slice(0, 8)}]`);
|
|
680
690
|
if (pre.converted) {
|
|
681
691
|
// The body now replays cleanly on Claude, so drop the predictive verdict —
|
|
682
692
|
// otherwise routing still exiles it and _noteRequestPolicy latches it sticky.
|
|
@@ -1689,7 +1699,38 @@ async function forwardRequest(
|
|
|
1689
1699
|
// is buffered, so latch the session Anthropic-incompatible (sticky → once per
|
|
1690
1700
|
// session) and retry PROVIDER-only, rather than surfacing the 400. Only worth
|
|
1691
1701
|
// it when a provider can actually serve it (profile=all with a GLM/Kimi token).
|
|
1702
|
+
//
|
|
1703
|
+
// REPAIR FIRST (2026-11-06, "customer fups"): the broad strip + coordinate repair
|
|
1704
|
+
// above may both have found nothing while the transcript is still curable — the
|
|
1705
|
+
// measured case is a GLM-authored `mcp_tool_use`/`mcp_tool_result` pair, which
|
|
1706
|
+
// both repairs predate. Try the conversion once here; if it changes the body,
|
|
1707
|
+
// retry THAT on Claude instead of pinning. Pin only when the conversion finds
|
|
1708
|
+
// nothing (a genuinely unrepairable transcript). This kills the pin loop: pin →
|
|
1709
|
+
// provider exhausts → "can only run on GLM" with ~59h Retry — while a text-
|
|
1710
|
+
// converted body replays 200 (verified live, count_tokens, 2026-11-06).
|
|
1692
1711
|
const providerAvailable = accountManager.accounts?.some(a => a.type === 'provider' && a.enabled !== false);
|
|
1712
|
+
// Exact-shape gate (red-team M2): the loose isAnthropicIncompatBody matcher also
|
|
1713
|
+
// fires on unrelated unknown-tag 400s (its expected-tags list mentions
|
|
1714
|
+
// server_tool_use); a rewrite must key on the two MCP wordings or the
|
|
1715
|
+
// deterministic server-tool-id wording, never the fuzzy predicate.
|
|
1716
|
+
const incompatRepairable = /server_tool_use\.id: String should match pattern/i.test(errorBody)
|
|
1717
|
+
|| /mcp_tool_(use|result)/.test(errorBody)
|
|
1718
|
+
&& /(does not match any of the expected tags|without a corresponding)/i.test(errorBody);
|
|
1719
|
+
if (anthropicIncompat && incompatRepairable && !requestInfo.incompatRepairTried && canRepairBody) {
|
|
1720
|
+
const repaired = stripForeignThinkingBlocks(body);
|
|
1721
|
+
if (repaired.body) {
|
|
1722
|
+
// Latch the pre-strip sticky so the NEXT turn repairs up front instead of
|
|
1723
|
+
// re-paying this rejected round-trip every turn (the client resends the whole
|
|
1724
|
+
// poisoned history each turn — same cache-churn the audit is about).
|
|
1725
|
+
accountManager.markSessionThinkingContaminated?.(requestInfo.sessionKey);
|
|
1726
|
+
console.log(`[Maxpool] Recovering session on Claude: incompatibility repair removed ${repaired.removed} thinking block(s), converted ${repaired.converted} provider tool block(s) — retrying on Claude before pinning`);
|
|
1727
|
+
return forwardRequest(
|
|
1728
|
+
req, res, repaired.body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir,
|
|
1729
|
+
retryConfig, queueConfig, { ...requestInfo, incompatRepairTried: true, anthropicIncompatible: false, repairCount: repairCount + 1 },
|
|
1730
|
+
repaired.body.length <= retryConfig.maxRetryBufferBytes, canQueueBufferedBody, excludedIndexes,
|
|
1731
|
+
);
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1693
1734
|
if (anthropicIncompat && requestInfo.sessionKey && providerAvailable
|
|
1694
1735
|
&& canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
|
|
1695
1736
|
accountManager.markSessionIncompatible?.(requestInfo.sessionKey, requestInfo.homeProvider);
|
|
@@ -1836,17 +1877,26 @@ async function forwardRequest(
|
|
|
1836
1877
|
|
|
1837
1878
|
if (isStreaming) {
|
|
1838
1879
|
const streamLog = logDir ? [] : null;
|
|
1880
|
+
// PROVIDER-THINKING SCRUB (owner 2026-11-05): applied inside streamResponse at the
|
|
1881
|
+
// res.write boundary only — usage accounting, capacity and streamLog below still
|
|
1882
|
+
// see the ORIGINAL upstream stream (red-team finding 6).
|
|
1883
|
+
const scrubber = scrubEnabledFor(account, { providerThinkingScrub: requestInfo.providerThinkingScrub })
|
|
1884
|
+
? new ProviderThinkingScrubber({ onScrub: () => requestInfo._scrubbed = (requestInfo._scrubbed || 0) + 1 })
|
|
1885
|
+
: null;
|
|
1839
1886
|
// CAPACITY LEDGER: accrue in a FINALLY — a stream that dies mid-flight throws
|
|
1840
1887
|
// from streamResponse, and the tokens genuinely delivered before the failure
|
|
1841
1888
|
// are real capacity; skipping them under-counts exactly the longest
|
|
1842
1889
|
// generations (red-team F5). One accrual per request, per-stream running max
|
|
1843
1890
|
// (M3 — cumulative interim deltas must not be summed).
|
|
1844
1891
|
try {
|
|
1845
|
-
await streamResponse(upstreamRes.body, res, upstreamRes.status, responseHeaders, account.index, accountManager, streamLog, requestInfo);
|
|
1892
|
+
await streamResponse(upstreamRes.body, res, upstreamRes.status, responseHeaders, account.index, accountManager, streamLog, requestInfo, undefined, scrubber);
|
|
1846
1893
|
} finally {
|
|
1847
1894
|
accountManager.accrueCapacity?.(account.index, {
|
|
1848
1895
|
input: requestInfo._capacityInput || 0,
|
|
1849
1896
|
output: requestInfo._capacityOutput || 0,
|
|
1897
|
+
cacheRead: requestInfo._capacityCacheRead || 0,
|
|
1898
|
+
cacheCreation: requestInfo._capacityCacheCreation || 0,
|
|
1899
|
+
model: requestInfo.model,
|
|
1850
1900
|
});
|
|
1851
1901
|
}
|
|
1852
1902
|
accountManager.releaseAccount(lease, { success: true, status: upstreamRes.status });
|
|
@@ -1854,6 +1904,12 @@ async function forwardRequest(
|
|
|
1854
1904
|
logSections.push(`=== RESPONSE BODY (streamed) ===\n${streamLog.join('')}`);
|
|
1855
1905
|
writeRequestLog(logDir, reqId, logSections);
|
|
1856
1906
|
}
|
|
1907
|
+
// SCRUB VISIBILITY (E2E verification V1): the counter existed but was never
|
|
1908
|
+
// surfaced — client-side "no thinking" is indistinguishable from "z.ai emitted
|
|
1909
|
+
// none". One line whenever the scrub actually removed blocks.
|
|
1910
|
+
if (scrubber && (requestInfo._scrubbed || scrubber.convertedCount)) {
|
|
1911
|
+
console.log(`[Maxpool] scrub: ${describeScrub(scrubber.stats())} from "${account.name}" response [sess ${String(requestInfo.sessionKey || '?').slice(0, 8)}]`);
|
|
1912
|
+
}
|
|
1857
1913
|
} else { // Bound the non-streaming body read so a mid-body upstream stall can't hang
|
|
1858
1914
|
// the request forever and leak the lease (same class as the streaming idle
|
|
1859
1915
|
// guard). On timeout → UPSTREAM_BODY → the caller frees the lease.
|
|
@@ -1909,7 +1965,21 @@ async function forwardRequest(
|
|
|
1909
1965
|
// body reaches Claude Code. Computed BEFORE writeHead so a length change lands
|
|
1910
1966
|
// in the SAME header block — setting content-length after writeHead is a no-op
|
|
1911
1967
|
// and would ship a body/length mismatch (the client then hangs or truncates).
|
|
1912
|
-
|
|
1968
|
+
// PROVIDER-THINKING SCRUB runs in the SAME lane (red team finding 2: CC falls
|
|
1969
|
+
// back to non-streaming exactly when providers misbehave; an SSE-only scrub
|
|
1970
|
+
// would re-poison histories on the degraded path). Same header rule applies.
|
|
1971
|
+
let outBuf = normalizeModelEcho(buf, requestInfo.model);
|
|
1972
|
+
if (scrubEnabledFor(account, { providerThinkingScrub: requestInfo.providerThinkingScrub })) {
|
|
1973
|
+
const scrubStats = {};
|
|
1974
|
+
const scrubbed = scrubProviderResponseBody(safeJsonParse(outBuf), scrubStats);
|
|
1975
|
+
if (scrubbed) {
|
|
1976
|
+
outBuf = Buffer.from(JSON.stringify(scrubbed));
|
|
1977
|
+
requestInfo._scrubbed = (requestInfo._scrubbed || 0) + scrubStats.thinking + scrubStats.unknown.length;
|
|
1978
|
+
// SCRUB VISIBILITY (E2E V1): ONE summary line per response, matching the
|
|
1979
|
+
// streaming lane's line.
|
|
1980
|
+
console.log(`[Maxpool] scrub: ${describeScrub(scrubStats)} from "${account.name}" response (buffered)`);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1913
1983
|
const outHeaders = outBuf === buf
|
|
1914
1984
|
? responseHeaders
|
|
1915
1985
|
: { ...responseHeaders, 'content-length': String(outBuf.length) };
|
|
@@ -2756,7 +2826,12 @@ function repairEffort(body, mode, errorBody = '') {
|
|
|
2756
2826
|
|
|
2757
2827
|
/** Collect foreign server-tool ids across the WHOLE transcript first — a call sits on the
|
|
2758
2828
|
* assistant turn but its result is often carried on the FOLLOWING user turn, so a
|
|
2759
|
-
* per-message scan would leave that result behind (and it alone still 400s).
|
|
2829
|
+
* per-message scan would leave that result behind (and it alone still 400s).
|
|
2830
|
+
* `mcp_tool_use` is collected the same way: GLM emits provider-authored MCP calls
|
|
2831
|
+
* (webReader etc. with `call_…` ids), and Anthropic rejects them BOTH ways — the plain
|
|
2832
|
+
* Messages API lists no `mcp_tool_use` tag at all, and the mcp beta rejects the pair
|
|
2833
|
+
* because the provider puts the `mcp_tool_result` on the assistant turn (measured live
|
|
2834
|
+
* 2026-11-06, "customer fups" session). Conversion to text is accepted either way. */
|
|
2760
2835
|
function collectForeignServerToolIds(messages) {
|
|
2761
2836
|
const ids = new Set();
|
|
2762
2837
|
const clientIds = new Set();
|
|
@@ -2764,7 +2839,8 @@ function collectForeignServerToolIds(messages) {
|
|
|
2764
2839
|
if (!Array.isArray(blocks)) return;
|
|
2765
2840
|
for (const b of blocks) {
|
|
2766
2841
|
if (b?.type === 'tool_use' && b.id) clientIds.add(b.id);
|
|
2767
|
-
if (b?.type === 'server_tool_use'
|
|
2842
|
+
if ((b?.type === 'server_tool_use' || b?.type === 'mcp_tool_use')
|
|
2843
|
+
&& !ANTHROPIC_TOOL_ID.test(String(b.id || ''))) ids.add(b.id);
|
|
2768
2844
|
if (Array.isArray(b?.content)) walk(b.content); // nested, mirrors detectTranscriptOrigin
|
|
2769
2845
|
}
|
|
2770
2846
|
};
|
|
@@ -2783,24 +2859,13 @@ function convertForeignServerTools(content, foreignIds) {
|
|
|
2783
2859
|
let converted = 0;
|
|
2784
2860
|
const out = [];
|
|
2785
2861
|
for (const b of content) {
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
if (b?.tool_use_id && foreignIds.has(b.tool_use_id)) {
|
|
2794
|
-
const rows = Array.isArray(b.content) ? b.content : [];
|
|
2795
|
-
// Fall back through the shapes a PROVIDER may use — the point of converting rather
|
|
2796
|
-
// than dropping is to keep what the search found; assuming Anthropic's {title,url}
|
|
2797
|
-
// would silently discard a GLM row that carries text instead.
|
|
2798
|
-
const found = rows
|
|
2799
|
-
.map(r => [r?.title, r?.url].filter(Boolean).join(' — ')
|
|
2800
|
-
|| (typeof r === 'string' ? r : (r?.text ?? r?.content ?? r?.snippet ?? '')))
|
|
2801
|
-
.map(t => (typeof t === 'string' ? t.slice(0, 300) : ''))
|
|
2802
|
-
.filter(Boolean).slice(0, 10);
|
|
2803
|
-
out.push({ type: 'text', text: found.length ? `[search results: ${found.join(' | ')}]` : '[search returned no usable results]' });
|
|
2862
|
+
// Text conventions live in provider-thinking-scrub.js (foreignToolBlockText) so the
|
|
2863
|
+
// request-side repair and the response-side scrub can never drift apart.
|
|
2864
|
+
const isCall = (b?.type === 'server_tool_use' || b?.type === 'mcp_tool_use') && foreignIds.has(b.id);
|
|
2865
|
+
// Any result block referring to a foreign call — web_search_tool_result, mcp_tool_result and friends.
|
|
2866
|
+
const isResult = !isCall && b?.tool_use_id && foreignIds.has(b.tool_use_id);
|
|
2867
|
+
if (isCall || isResult) {
|
|
2868
|
+
out.push({ type: 'text', text: foreignToolBlockText(b) });
|
|
2804
2869
|
converted++;
|
|
2805
2870
|
continue;
|
|
2806
2871
|
}
|
|
@@ -3332,8 +3397,12 @@ const ANTHROPIC_TOOL_ID = /^(toolu|srvtoolu)_/;
|
|
|
3332
3397
|
// from the tool-use id shapes in its `messages`. Returns { anthropicIncompatible,
|
|
3333
3398
|
// homeProvider }.
|
|
3334
3399
|
// anthropicIncompatible = the transcript has a `server_tool_use` id NOT matching
|
|
3335
|
-
// ^srvtoolu_
|
|
3336
|
-
//
|
|
3400
|
+
// ^srvtoolu_, OR a foreign `mcp_tool_use` id (2026-11-06: GLM emits provider-authored
|
|
3401
|
+
// MCP calls with `call_…` ids; Anthropic rejects the block type on the plain API and
|
|
3402
|
+
// the mis-roled result pair under the mcp beta — both measured live). Either means
|
|
3403
|
+
// Anthropic 400s on replay, but BOTH are repairable by the pre-strip conversion, so
|
|
3404
|
+
// a `true` here routes into the up-front repair rather than a permanent exile.
|
|
3405
|
+
// Anthropic validates a client `tool_use.id` LOOSELY
|
|
3337
3406
|
// (^[a-zA-Z0-9_-]+$), so GLM `call_…` / Kimi `tool_…` client ids PASS and a
|
|
3338
3407
|
// Kimi/GLM session without server-tools is FINE on Claude — we do NOT predict
|
|
3339
3408
|
// those (a rejected thinking signature, if it ever happens, self-heals via the
|
|
@@ -3353,7 +3422,12 @@ function detectTranscriptOrigin(json) {
|
|
|
3353
3422
|
if (Array.isArray(value)) { for (const v of value) { visit(v); if (incompatible) return; } return; }
|
|
3354
3423
|
if (!value || typeof value !== 'object') return;
|
|
3355
3424
|
const t = value.type;
|
|
3356
|
-
|
|
3425
|
+
// Incompatible only when the CONVERTER can also cure it (red-team M4): flagging a
|
|
3426
|
+
// nested or id-colliding block the converter skips would exile the session to GLM
|
|
3427
|
+
// before Claude is ever tried — the incident's dead-session outcome. The id set is
|
|
3428
|
+
// computed by the converter's own collector (nested-walking, collision-guarded).
|
|
3429
|
+
const convertibleIds = collectForeignServerToolIds(json?.messages || []);
|
|
3430
|
+
if ((t === 'server_tool_use' || t === 'mcp_tool_use') && typeof value.id === 'string' && convertibleIds.has(value.id)) {
|
|
3357
3431
|
incompatible = true; noteForeign(value.id); return;
|
|
3358
3432
|
}
|
|
3359
3433
|
if (t === 'tool_use' && typeof value.id === 'string' && !ANTHROPIC_TOOL_ID.test(value.id)) noteForeign(value.id);
|
|
@@ -3372,13 +3446,19 @@ function detectTranscriptOrigin(json) {
|
|
|
3372
3446
|
// self-heal onto a provider instead of surfacing the 400.
|
|
3373
3447
|
function isAnthropicIncompatBody(body) {
|
|
3374
3448
|
if (!body) return false;
|
|
3375
|
-
// The deterministic server-tool-id 400, the known invalid-signature error,
|
|
3449
|
+
// The deterministic server-tool-id 400, the known invalid-signature error, a
|
|
3376
3450
|
// thinking-block signature VALIDATION error (missing/invalid) — the last gated on
|
|
3377
3451
|
// a validation verb so a user message merely echoing "thinking"/"signature" can't
|
|
3378
|
-
// false-latch the session provider-pinned for life
|
|
3452
|
+
// false-latch the session provider-pinned for life — and the two MCP shapes
|
|
3453
|
+
// (2026-11-06, verbatim, measured live): the plain API's unknown-tag rejection and
|
|
3454
|
+
// the mcp beta's mis-roled-pair rejection. NB the plain-API text lists
|
|
3455
|
+
// 'server_tool_use' among its expected tags, which USED to be the only reason an
|
|
3456
|
+
// mcp-only transcript tripped this predicate — an accident of wording, now pinned.
|
|
3379
3457
|
return /srvtoolu_|server_tool_use/.test(body)
|
|
3380
3458
|
|| /invalid `signature` in `thinking`/i.test(body)
|
|
3381
|
-
|| (/thinking/.test(body) && /signature/.test(body) && /(should match|required|invalid|expected|must )/i.test(body))
|
|
3459
|
+
|| (/thinking/.test(body) && /signature/.test(body) && /(should match|required|invalid|expected|must )/i.test(body))
|
|
3460
|
+
|| (/mcp_tool_(use|result)/.test(body)
|
|
3461
|
+
&& /(does not match any of the expected tags|without a corresponding)/i.test(body));
|
|
3382
3462
|
}
|
|
3383
3463
|
|
|
3384
3464
|
function containsThinkingBlock(value) {
|
|
@@ -3712,7 +3792,7 @@ function totalLen(chunks) {
|
|
|
3712
3792
|
return n;
|
|
3713
3793
|
}
|
|
3714
3794
|
|
|
3715
|
-
async function streamResponse(webStream, res, status, responseHeaders, accountIndex, accountManager, streamLog, requestInfo = {}, idleMs = STREAM_IDLE_MS) {
|
|
3795
|
+
async function streamResponse(webStream, res, status, responseHeaders, accountIndex, accountManager, streamLog, requestInfo = {}, idleMs = STREAM_IDLE_MS, scrubber = null) {
|
|
3716
3796
|
const reader = webStream.getReader();
|
|
3717
3797
|
const decoder = new TextDecoder();
|
|
3718
3798
|
let sseBuffer = '';
|
|
@@ -3858,10 +3938,15 @@ async function streamResponse(webStream, res, status, responseHeaders, accountIn
|
|
|
3858
3938
|
}
|
|
3859
3939
|
}
|
|
3860
3940
|
|
|
3861
|
-
// Forward chunk immediately
|
|
3862
|
-
|
|
3941
|
+
// Forward chunk immediately. Decode ONCE (B1, build review 2026-11-06): a second
|
|
3942
|
+
// decode({stream:true}) on this shared TextDecoder flushes a pending partial UTF-8
|
|
3943
|
+
// sequence as U+FFFD — the usage/streamLog lane garbled in BOTH flag states. The
|
|
3944
|
+
// single decoded text feeds the scrubber (client lane) and the accounting lane.
|
|
3945
|
+
const chunkText = decoder.decode(out, { stream: true });
|
|
3946
|
+
const writeText = scrubber ? scrubber.push(chunkText) : null;
|
|
3947
|
+
const ok = scrubber ? (writeText ? res.write(writeText) : true) : res.write(out);
|
|
3863
3948
|
|
|
3864
|
-
const text =
|
|
3949
|
+
const text = chunkText;
|
|
3865
3950
|
|
|
3866
3951
|
// Capture for logging
|
|
3867
3952
|
if (streamLog) streamLog.push(text);
|
|
@@ -3979,7 +4064,17 @@ async function streamResponse(webStream, res, status, responseHeaders, accountIn
|
|
|
3979
4064
|
// means the upstream died mid-event (an event without its '\n\n' terminator).
|
|
3980
4065
|
// Complete-event streams without a terminal message_stop (z.ai compat shape,
|
|
3981
4066
|
// test fixtures) end with an EMPTY residual and pass through unchanged.
|
|
3982
|
-
|
|
4067
|
+
const truncatedMidEvent = Boolean(sseBuffer.trim());
|
|
4068
|
+
// Scrubber tail flush (an SSE event without its trailing '\n\n' is still buffered
|
|
4069
|
+
// inside the scrubber). Runs BEFORE the sentinel so the error frame is always the
|
|
4070
|
+
// last thing the client sees; skipped when the sentinel fires — the scrubber tail is
|
|
4071
|
+
// then the same half-event the sentinel reports, and emitting it would hand the
|
|
4072
|
+
// client a corrupt frame ahead of the error.
|
|
4073
|
+
if (scrubber && !truncatedMidEvent && !res.destroyed && !res.writableEnded) {
|
|
4074
|
+
const tail = scrubber.flush();
|
|
4075
|
+
if (tail) { try { res.write(tail); } catch { /* client gone */ } }
|
|
4076
|
+
}
|
|
4077
|
+
if ((committed || res.headersSent) && truncatedMidEvent && !res.destroyed && !res.writableEnded) {
|
|
3983
4078
|
try {
|
|
3984
4079
|
res.write(`event: error\ndata: ${JSON.stringify({
|
|
3985
4080
|
type: 'error',
|
|
@@ -3992,6 +4087,14 @@ async function streamResponse(webStream, res, status, responseHeaders, accountIn
|
|
|
3992
4087
|
}
|
|
3993
4088
|
}
|
|
3994
4089
|
|
|
4090
|
+
/** Running-max the cache counters of one usage object into the request's capacity book.
|
|
4091
|
+
* Capacity tokens = input + cache_read + cache_creation + output (every token the
|
|
4092
|
+
* provider processed), the same definition for Anthropic and z.ai. */
|
|
4093
|
+
function noteCapacityCache(requestInfo, usage) {
|
|
4094
|
+
requestInfo._capacityCacheRead = Math.max(requestInfo._capacityCacheRead || 0, usage?.cache_read_input_tokens || 0);
|
|
4095
|
+
requestInfo._capacityCacheCreation = Math.max(requestInfo._capacityCacheCreation || 0, usage?.cache_creation_input_tokens || 0);
|
|
4096
|
+
}
|
|
4097
|
+
|
|
3995
4098
|
function parseSSEEvent(event, accountIndex, accountManager, requestInfo = {}) {
|
|
3996
4099
|
const dataLine = event.split('\n').find(l => l.startsWith('data: '));
|
|
3997
4100
|
if (!dataLine) return;
|
|
@@ -4007,10 +4110,17 @@ function parseSSEEvent(event, accountIndex, accountManager, requestInfo = {}) {
|
|
|
4007
4110
|
accountManager.updateUsage(accountIndex, data.message.usage.input_tokens, 0);
|
|
4008
4111
|
accountManager.countCallerTokens?.(requestInfo.caller, data.message.usage.input_tokens);
|
|
4009
4112
|
requestInfo._capacityInput = Math.max(requestInfo._capacityInput || 0, data.message.usage.input_tokens || 0);
|
|
4113
|
+
noteCapacityCache(requestInfo, data.message.usage);
|
|
4010
4114
|
} else if (data.type === 'message_delta' && data.usage) {
|
|
4011
4115
|
accountManager.updateUsage(accountIndex, 0, data.usage.output_tokens);
|
|
4012
4116
|
accountManager.countCallerTokens?.(requestInfo.caller, data.usage.output_tokens);
|
|
4013
4117
|
requestInfo._capacityOutput = Math.max(requestInfo._capacityOutput || 0, data.usage.output_tokens || 0);
|
|
4118
|
+
// Providers differ in WHICH event carries the cache/input counters (z.ai: all of
|
|
4119
|
+
// them on message_delta, message_start is zeros — captured 2026-11-06, pinned by
|
|
4120
|
+
// test/capacity-seam.test.js + test/fixtures/zai-sse-usage.txt), so every field is
|
|
4121
|
+
// a running max over BOTH events.
|
|
4122
|
+
requestInfo._capacityInput = Math.max(requestInfo._capacityInput || 0, data.usage.input_tokens || 0);
|
|
4123
|
+
noteCapacityCache(requestInfo, data.usage);
|
|
4014
4124
|
}
|
|
4015
4125
|
}
|
|
4016
4126
|
if (sseEventContainsThinking(data)) {
|
|
@@ -4071,6 +4181,9 @@ function extractUsageFromBody(buffer, accountIndex, accountManager, requestInfo
|
|
|
4071
4181
|
accountManager.accrueCapacity?.(accountIndex, {
|
|
4072
4182
|
input: json.usage.input_tokens || 0,
|
|
4073
4183
|
output: json.usage.output_tokens || 0,
|
|
4184
|
+
cacheRead: json.usage.cache_read_input_tokens || 0,
|
|
4185
|
+
cacheCreation: json.usage.cache_creation_input_tokens || 0,
|
|
4186
|
+
model: requestInfo.model,
|
|
4074
4187
|
});
|
|
4075
4188
|
}
|
|
4076
4189
|
}
|
|
@@ -4112,3 +4225,8 @@ function computeRetryAfter(accountManager, requestInfo = {}) {
|
|
|
4112
4225
|
|
|
4113
4226
|
// Test seam: prepareRuntimeProviders with a plain headers object.
|
|
4114
4227
|
export const __test = { prepareRuntimeProviders };
|
|
4228
|
+
|
|
4229
|
+
/** Parse-or-null helper for the buffered scrub lane (never throws on a non-JSON body). */
|
|
4230
|
+
function safeJsonParse(buf) {
|
|
4231
|
+
try { return JSON.parse(buf.toString('utf8')); } catch { return null; }
|
|
4232
|
+
}
|