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/config.js
CHANGED
|
@@ -4,6 +4,8 @@ import { existsSync } from 'node:fs';
|
|
|
4
4
|
import { join, dirname } from 'node:path';
|
|
5
5
|
import { homedir } from 'node:os';
|
|
6
6
|
import { randomBytes } from 'node:crypto';
|
|
7
|
+
import { copyFile } from 'node:fs/promises';
|
|
8
|
+
import { SCHEMA_VERSION as CAPACITY_SCHEMA_VERSION } from './capacity-ledger.js';
|
|
7
9
|
|
|
8
10
|
export function getConfigPath() {
|
|
9
11
|
if (process.env.MAXPOOL_CONFIG) return process.env.MAXPOOL_CONFIG;
|
|
@@ -68,6 +70,13 @@ export function createDefaultConfig() {
|
|
|
68
70
|
// is blind to idle / out-of-band-used accounts and the scorer will pile traffic
|
|
69
71
|
// onto exactly the account it cannot see. 0 = off.
|
|
70
72
|
quotaProbeSeconds: 60,
|
|
73
|
+
// Provider-thinking scrub (owner 2026-11-05): drop `thinking`/`redacted_thinking`
|
|
74
|
+
// blocks (and any unknown block type) from PROVIDER responses before they reach the
|
|
75
|
+
// client, so a mixed GLM+Claude session never stores foreign thinking — the stored
|
|
76
|
+
// blocks are what Anthropic signature-400s on and the repair strips, breaking the
|
|
77
|
+
// cache prefix. Claude-account responses are NEVER scrubbed. Default OFF until the
|
|
78
|
+
// E2E package is owner-approved.
|
|
79
|
+
providerThinkingScrub: false,
|
|
71
80
|
routing: {
|
|
72
81
|
mode: 'automatic',
|
|
73
82
|
preferredAccount: null,
|
|
@@ -287,6 +296,31 @@ export async function readGeneration(path) {
|
|
|
287
296
|
* generation you read; omit it to read-then-bump unconditionally (single-owner
|
|
288
297
|
* fast path). Returns the generation written, or null when refused.
|
|
289
298
|
*/
|
|
299
|
+
/**
|
|
300
|
+
* Capacity-ledger rollback protection (red-team J2). An older build cannot read a newer
|
|
301
|
+
* capacity schema, boots with an EMPTY ledger and its next flush would overwrite the
|
|
302
|
+
* file, destroying the history. So, before every state write:
|
|
303
|
+
* - an on-disk `capacity.schemaVersion` NEWER than this build's refuses the write
|
|
304
|
+
* (throws loudly; the caller surfaces it) — never clobber a newer build's ledger;
|
|
305
|
+
* - the FIRST write of this build's schema over an OLDER one first copies the file to
|
|
306
|
+
* `<state>.v<old>.bak-<utc ts>` so a rollback keeps its history.
|
|
307
|
+
*/
|
|
308
|
+
async function guardCapacitySchema(path, nextState) {
|
|
309
|
+
let disk;
|
|
310
|
+
try { disk = JSON.parse(await readFile(path, 'utf-8')); } catch { return; }
|
|
311
|
+
const onDiskV = Number(disk?.capacity?.schemaVersion);
|
|
312
|
+
if (!Number.isFinite(onDiskV)) return;
|
|
313
|
+
if (onDiskV > CAPACITY_SCHEMA_VERSION) {
|
|
314
|
+
const msg = `refusing to write ${path}: its capacity ledger is schema v${onDiskV}, newer than this build's v${CAPACITY_SCHEMA_VERSION} (a newer maxpool owns it; overwriting would destroy its history)`;
|
|
315
|
+
console.error(`[maxpool] ${msg}`);
|
|
316
|
+
throw Object.assign(new Error(msg), { code: 'STATE_SCHEMA_NEWER' });
|
|
317
|
+
}
|
|
318
|
+
if (onDiskV < CAPACITY_SCHEMA_VERSION && Number(nextState?.capacity?.schemaVersion) === CAPACITY_SCHEMA_VERSION) {
|
|
319
|
+
const ts = new Date().toISOString().replace(/[-:]/g, '').replace(/\.\d+Z$/, 'Z');
|
|
320
|
+
await copyFile(path, `${path}.v${onDiskV}.bak-${ts}`);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
290
324
|
let _stateWriteChain = Promise.resolve();
|
|
291
325
|
|
|
292
326
|
/**
|
|
@@ -298,6 +332,7 @@ let _stateWriteChain = Promise.resolve();
|
|
|
298
332
|
*/
|
|
299
333
|
export function saveStateUnbumped(state, generation) {
|
|
300
334
|
const run = async () => {
|
|
335
|
+
await guardCapacitySchema(getStatePath(), state);
|
|
301
336
|
await atomicWrite(getStatePath(), JSON.stringify({ ...state, _generation: generation }, null, 2) + '\n');
|
|
302
337
|
};
|
|
303
338
|
const result = _stateWriteChain.then(run, run);
|
|
@@ -316,6 +351,7 @@ export function saveState(state, { expectedGeneration = null } = {}) {
|
|
|
316
351
|
return null;
|
|
317
352
|
}
|
|
318
353
|
const next = onDisk + 1;
|
|
354
|
+
await guardCapacitySchema(path, state);
|
|
319
355
|
await atomicWrite(path, JSON.stringify({ ...state, _generation: next }, null, 2) + '\n');
|
|
320
356
|
return next;
|
|
321
357
|
};
|
package/src/index.js
CHANGED
|
@@ -16,6 +16,7 @@ import { spawn, spawnSync } from 'node:child_process';
|
|
|
16
16
|
import { createInterface } from 'node:readline';
|
|
17
17
|
import { loadOrCreateConfig, loadConfig, saveConfig, atomicConfigUpdate, getConfigPath, loadState, saveState, getStatePath, getLogPath, readGeneration, flushConfigWrites, flushStateWrites, saveStateUnbumped } from './config.js';
|
|
18
18
|
import { setEventLogPath, installConsoleMirror, setConsoleStdoutSuppressed, appendEventLog } from './event-log.js';
|
|
19
|
+
import { sendLoginToPool, planCliLogin, resolveUsablePools } from './cloud-login.js';
|
|
19
20
|
import { SleepGuard } from './sleep-guard.js';
|
|
20
21
|
import { AccountManager } from './account-manager.js';
|
|
21
22
|
import { createProxyServer, REQUEST_IDLE_MAX_MS } from './server.js';
|
|
@@ -1745,7 +1746,65 @@ async function loginOAuthCommand() {
|
|
|
1745
1746
|
process.exit(1);
|
|
1746
1747
|
}
|
|
1747
1748
|
|
|
1748
|
-
|
|
1749
|
+
// Cloud-aware: a linked team pool with a usable key may receive the login instead.
|
|
1750
|
+
// Profile FIRST (once): a local match must win over the cloud default.
|
|
1751
|
+
const profile = await fetchProfile(creds.accessToken);
|
|
1752
|
+
const localMatch = (profile?.accountUuid && config.accounts.find(a => a.type === 'oauth' && a.accountUuid === profile.accountUuid))
|
|
1753
|
+
|| (profile?.email && config.accounts.find(a => a.type === 'oauth' && a.name === profile.email))
|
|
1754
|
+
|| null;
|
|
1755
|
+
const { usable, unresolved, error } = await resolveUsablePools(config.pools, async (names) => {
|
|
1756
|
+
const { resolveSecrets } = await import('./secret-resolver.js');
|
|
1757
|
+
return resolveSecrets(names);
|
|
1758
|
+
});
|
|
1759
|
+
const flags = { local: args.includes('--local'), cloud: args.includes('--cloud'), pool: argValue('--pool') };
|
|
1760
|
+
const isTTY = !!(process.stdin.isTTY && process.stdout.isTTY);
|
|
1761
|
+
const ask = async (q) => {
|
|
1762
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
1763
|
+
const a = await new Promise(resolve => rl.question(q, resolve));
|
|
1764
|
+
rl.close();
|
|
1765
|
+
return a;
|
|
1766
|
+
};
|
|
1767
|
+
const input = { usable, unresolved, error, localMatch, flags, isTTY };
|
|
1768
|
+
let plan = planCliLogin(input);
|
|
1769
|
+
if (plan.action === 'ask-fallback') {
|
|
1770
|
+
console.error(plan.message);
|
|
1771
|
+
input.fallbackAnswer = await ask('Save on this laptop instead? [y/N]: ');
|
|
1772
|
+
plan = planCliLogin(input);
|
|
1773
|
+
}
|
|
1774
|
+
if (plan.action === 'ask-pool') {
|
|
1775
|
+
usable.forEach((p, i) => console.error(` ${i + 1}. ${p.name}`));
|
|
1776
|
+
input.poolAnswer = await ask(`Which team pool? [1-${usable.length}, default 1]: `);
|
|
1777
|
+
plan = planCliLogin(input);
|
|
1778
|
+
}
|
|
1779
|
+
if (plan.action === 'ask-target') {
|
|
1780
|
+
input.answer = await ask(`Add/update this account in the team pool "${plan.pool.name}" (recommended) or this laptop only? [T/l]: `);
|
|
1781
|
+
plan = planCliLogin(input);
|
|
1782
|
+
}
|
|
1783
|
+
if (plan.action === 'fail') {
|
|
1784
|
+
console.error(plan.message);
|
|
1785
|
+
process.exit(1);
|
|
1786
|
+
}
|
|
1787
|
+
if (plan.action === 'local') {
|
|
1788
|
+
await upsertOAuthAccount(config, name, creds, 'login', profile);
|
|
1789
|
+
return;
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
const pool = plan.pool;
|
|
1793
|
+
if (!profile?.accountUuid) {
|
|
1794
|
+
// Without the uuid the server cannot match a re-authorization and would ADD a
|
|
1795
|
+
// duplicate row — the exact bug class v1.24.67 fixed on the server side. Abort.
|
|
1796
|
+
console.error(`Could not fetch the account profile (${profile?.error || 'no token'}) — the team pool needs the account id to add or re-authorize safely. NOTHING was saved on this laptop. Try again, or use --local.`);
|
|
1797
|
+
process.exit(1);
|
|
1798
|
+
}
|
|
1799
|
+
if (!name) name = profile?.email || `account-${Date.now().toString(36)}`;
|
|
1800
|
+
console.log(`Sending "${name}" to the team pool "${pool.name}"...`);
|
|
1801
|
+
const r = await sendLoginToPool({ url: pool.url, key: pool.key, name, profile, creds });
|
|
1802
|
+
if (r.ok) {
|
|
1803
|
+
console.log(`Done: "${r.name || name}" ${r.result === 'added' ? 'added to' : 're-authenticated in'} the team pool "${pool.name}". Nothing was saved on this laptop.`);
|
|
1804
|
+
return;
|
|
1805
|
+
}
|
|
1806
|
+
console.error(`${r.error}. NOTHING was saved on this laptop — not retried locally. Try again, or use --local to save here only.`);
|
|
1807
|
+
process.exit(1);
|
|
1749
1808
|
}
|
|
1750
1809
|
|
|
1751
1810
|
// ── env ─────────────────────────────────────────────────────
|
|
@@ -2189,6 +2248,7 @@ Commands:
|
|
|
2189
2248
|
server Start the proxy server (default)
|
|
2190
2249
|
login OAuth login via browser (adds the account you're signed into at claude.ai)
|
|
2191
2250
|
login --api Add an API key account
|
|
2251
|
+
login --cloud|--local [--pool <name>] Send the login to the linked team pool (default when one is configured) or keep it on this laptop
|
|
2192
2252
|
env [--with-key] Print env vars to use with Claude
|
|
2193
2253
|
pool shared <host> <key> Point this machine at a shared team pool
|
|
2194
2254
|
pool local Point this machine back at its own local pool
|
|
@@ -2217,9 +2277,9 @@ Config: ${getConfigPath()}
|
|
|
2217
2277
|
|
|
2218
2278
|
// ── shared account upsert ────────────────────────────────────
|
|
2219
2279
|
|
|
2220
|
-
async function upsertOAuthAccount(config, name, creds, source = 'unknown') {
|
|
2221
|
-
// Fetch profile to auto-name and deduplicate by account UUID
|
|
2222
|
-
const profile = await fetchProfile(creds.accessToken);
|
|
2280
|
+
async function upsertOAuthAccount(config, name, creds, source = 'unknown', prefetchedProfile = null) {
|
|
2281
|
+
// Fetch profile to auto-name and deduplicate by account UUID (reuse one already fetched)
|
|
2282
|
+
const profile = prefetchedProfile || await fetchProfile(creds.accessToken);
|
|
2223
2283
|
const profileOk = profile && !profile.error;
|
|
2224
2284
|
|
|
2225
2285
|
if (!profileOk) {
|
package/src/pool-link.js
CHANGED
|
@@ -44,6 +44,14 @@ export function accountIdentity(account) {
|
|
|
44
44
|
return null;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/** Identity of a REMOTE status row (same key shape as accountIdentity). */
|
|
48
|
+
export function remoteAccountIdentity(a) {
|
|
49
|
+
if (!a) return null;
|
|
50
|
+
if (a.type === 'oauth') return a.accountUuid ? `uuid:${a.accountUuid}` : null;
|
|
51
|
+
if (a.type === 'provider' && a.provider && a.keyFingerprint) return `key:${a.provider}:${a.keyFingerprint}`;
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
47
55
|
/** Identity map from a REMOTE pool's status JSON (key-gated /maxpool/status):
|
|
48
56
|
* { accounts: [{name,type,provider,accountUuid,enabled,...}], callers, ... }.
|
|
49
57
|
* Provider rows don't carry credentials — the remote pool exports a
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-thinking scrub — keep foreign reasoning OUT of the shared transcript.
|
|
3
|
+
*
|
|
4
|
+
* WHY (owner 2026-11-05, task research/maxpool-round2): a mixed GLM+Claude session
|
|
5
|
+
* stores GLM-authored `thinking` blocks; Claude rejects their signatures (400), and the
|
|
6
|
+
* repair that recovers the session strips ALL thinking — rewriting the cached prefix from
|
|
7
|
+
* the first thinking block onward. Scrubbing provider thinking at the RESPONSE boundary
|
|
8
|
+
* means the transcript never contains it: no 400s, no repair, and the Claude-side cache
|
|
9
|
+
* prefix is never mutated by contamination. Claude accounts are NEVER scrubbed (their
|
|
10
|
+
* thinking is validly signed, cache-relevant, and must round-trip).
|
|
11
|
+
*
|
|
12
|
+
* Two lanes (red team 2026-11-05, finding 2): the streaming lane (streamResponse) and
|
|
13
|
+
* the buffered/non-streaming lane (next to normalizeModelEcho). Both must scrub or the
|
|
14
|
+
* fallback lane re-poisons histories CC itself hits exactly when providers misbehave.
|
|
15
|
+
*
|
|
16
|
+
* Red-team finding 1: NEVER skip SSE block indexes. A second CC reducer family (SDK
|
|
17
|
+
* paths, query-tool subagents, RC hosts) pushes on content_block_start and indexes by
|
|
18
|
+
* ARRAY POSITION — a suppressed block shifts every later one and silently corrupts text.
|
|
19
|
+
* So the streaming scrub RENUMBERS: an oldIndex→newIndex map rewrites every subsequent
|
|
20
|
+
* start/delta/stop for the same message. Internal consumers (usage ledger, capacity,
|
|
21
|
+
* streamLog) still see the PRE-scrub stream (finding 6: scrub applies at res.write only).
|
|
22
|
+
*
|
|
23
|
+
* Red-team finding 3: a thinking-only provider turn must not scrub to `content: []` —
|
|
24
|
+
* Anthropic rejects empty assistant content on the next request and no repair covers it.
|
|
25
|
+
* Synthesize a "(reasoning omitted)" text block instead.
|
|
26
|
+
*
|
|
27
|
+
* Red-team finding 4: unknown/renamed provider block types would pass through, get
|
|
28
|
+
* stored, and 400 later in a class no repair matches. The provider pass allows only
|
|
29
|
+
* known Anthropic types; anything else is dropped.
|
|
30
|
+
* `redacted_thinking` is also scrubbed — a provider can never produce a valid one.
|
|
31
|
+
*
|
|
32
|
+
* Red-team findings 1+6 (2026-11-06, round 3): the KNOWN provider tool blocks —
|
|
33
|
+
* mcp_tool_use / mcp_tool_result / server_tool_use / web_search_tool_result — must not be
|
|
34
|
+
* dropped either (that deleted GLM's web-tool calls and fetched results from the client
|
|
35
|
+
* transcript). They are CONVERTED to text in both lanes via foreignToolBlockText(), the
|
|
36
|
+
* same convention the REQUEST-side repair (convertForeignServerTools in server.js) uses —
|
|
37
|
+
* one exported helper, no drifting copies. Truly unknown types stay dropped.
|
|
38
|
+
*
|
|
39
|
+
* Provenance-ready for the deferred replay-memory upgrade (B): nothing here parses block
|
|
40
|
+
* content. If B is ever built, its memory keys off (session, provider, turn) and stores
|
|
41
|
+
* these blocks as opaque bytes — a z.ai format change cannot corrupt it.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/** Anthropic content-block types a PROVIDER response may carry through to the client. */
|
|
45
|
+
const PROVIDER_ALLOWED_BLOCK_TYPES = new Set([
|
|
46
|
+
'text', 'tool_use', 'thinking', 'redacted_thinking', // thinking* are listed so the
|
|
47
|
+
// scrub can MATCH them explicitly; both are removed below — never forwarded.
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Provider-authored TOOL blocks the scrub CONVERTS to text (red-team 2026-11-06 round-3
|
|
52
|
+
* finding 1) instead of dropping. GLM emits these on response turns (web search, MCP
|
|
53
|
+
* tools like webReader); the client transcript must keep what was called and found, in
|
|
54
|
+
* the same text form the request-side repair produces. Everything else unknown = dropped.
|
|
55
|
+
*/
|
|
56
|
+
const PROVIDER_CONVERTIBLE_TOOL_TYPES = new Set([
|
|
57
|
+
'server_tool_use', 'web_search_tool_result', 'mcp_tool_use', 'mcp_tool_result',
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
const SCRUB_PLACEHOLDER = '(reasoning omitted)';
|
|
61
|
+
|
|
62
|
+
// ── ONE converter shared by the request-side repair (server.js convertForeignServerTools)
|
|
63
|
+
// ── and both scrub lanes. Pure: block in → string out, no globals, no state.
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Text rendering of one provider tool block, request-side convention:
|
|
67
|
+
* server_tool_use → "[searched the web for: <query>]" / "[used <name>]"
|
|
68
|
+
* mcp_tool_use → "[called <name> (<server>) on <input, 300 code points>]"
|
|
69
|
+
* web_search_tool_result → "[search results: …]"
|
|
70
|
+
* mcp_tool_result → "[<name> result: …]" / "[mcp tool returned no usable results]"
|
|
71
|
+
* Truncation is by CODE POINTS: a raw .slice can split a surrogate pair (an emoji at
|
|
72
|
+
* char 299), and a lone surrogate makes the body invalid JSON-UTF8 Anthropic 400s with
|
|
73
|
+
* no repair path — killing the session the conversion is saving (red-team M1).
|
|
74
|
+
*/
|
|
75
|
+
export function foreignToolBlockText(b) {
|
|
76
|
+
if (b?.type === 'server_tool_use') {
|
|
77
|
+
const q = b.input?.query;
|
|
78
|
+
return q ? `[searched the web for: ${q}]` : `[used ${b.name || 'a tool'}]`;
|
|
79
|
+
}
|
|
80
|
+
if (b?.type === 'mcp_tool_use') {
|
|
81
|
+
const inp = JSON.stringify(b.input ?? {});
|
|
82
|
+
return `[called ${b.name || 'a tool'}${b.server_name ? ` (${b.server_name})` : ''} on ${codePointClamp(inp, 300)}]`;
|
|
83
|
+
}
|
|
84
|
+
// Any result block (web_search_tool_result / mcp_tool_result) referring to a foreign call.
|
|
85
|
+
const rows = Array.isArray(b?.content) ? b.content.slice() : [];
|
|
86
|
+
// Fall back through the shapes a PROVIDER may use — the point of converting rather
|
|
87
|
+
// than dropping is to keep what the search found; assuming Anthropic's {title,url}
|
|
88
|
+
// would silently discard a GLM row that carries text instead.
|
|
89
|
+
if (typeof b?.content === 'string' && b.content) rows.push(b.content); // a plain-string result (e.g. an error row) — keep it (red-team M6)
|
|
90
|
+
const found = rows
|
|
91
|
+
.map(r => [r?.title, r?.url].filter(Boolean).join(' — ')
|
|
92
|
+
|| (typeof r === 'string' ? r : (r?.text ?? r?.content ?? r?.snippet ?? '')))
|
|
93
|
+
.map(t => (typeof t === 'string' ? codePointClamp(t, 300) : ''))
|
|
94
|
+
.filter(Boolean).slice(0, 10);
|
|
95
|
+
if (b?.type === 'mcp_tool_result') {
|
|
96
|
+
// An MCP result carries {type:'text'} rows, not {title,url} — label it as the tool's
|
|
97
|
+
// answer rather than "[search results]" so the transcript stays honest.
|
|
98
|
+
return found.length ? `[${b.name || 'mcp tool'} result: ${found.join(' | ')}]` : '[mcp tool returned no usable results]';
|
|
99
|
+
}
|
|
100
|
+
return found.length ? `[search results: ${found.join(' | ')}]` : '[search returned no usable results]';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function codePointClamp(s, n) { return Array.from(s).slice(0, n).join(''); }
|
|
104
|
+
|
|
105
|
+
/** One summary line for the logs — per RESPONSE, not per block (red-team 2026-11-06 finding 6). */
|
|
106
|
+
export function describeScrub(stats) {
|
|
107
|
+
const parts = [];
|
|
108
|
+
if (stats.thinking) parts.push(`removed ${stats.thinking} provider thinking block(s)`);
|
|
109
|
+
if (stats.converted) parts.push(`converted ${stats.converted} provider tool block(s)`);
|
|
110
|
+
if (stats.unknown.length) parts.push(`dropped ${stats.unknown.length} unknown block(s): ${[...new Set(stats.unknown)].map(t => `"${t}"`).join(', ')}`);
|
|
111
|
+
return parts.join(', ');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Streaming-lane block classifier: null = forward verbatim, 'drop' | block = convert. */
|
|
115
|
+
function scrubCell(bt) {
|
|
116
|
+
if (bt === 'thinking' || bt === 'redacted_thinking') return 'drop';
|
|
117
|
+
if (PROVIDER_CONVERTIBLE_TOOL_TYPES.has(String(bt))) return 'convert';
|
|
118
|
+
if (PROVIDER_ALLOWED_BLOCK_TYPES.has(String(bt))) return 'forward';
|
|
119
|
+
return 'drop';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Scrub a COMPLETE parsed response body (buffered lane). Mutates nothing; returns a new
|
|
124
|
+
* object when something was scrubbed, else null (caller forwards the original).
|
|
125
|
+
* statsOut (optional object) receives { thinking, converted, unknown:[] }.
|
|
126
|
+
*/
|
|
127
|
+
export function scrubProviderResponseBody(json, statsOut) {
|
|
128
|
+
if (!json || typeof json !== 'object' || !Array.isArray(json.content)) return null;
|
|
129
|
+
const stats = { thinking: 0, converted: 0, unknown: [] };
|
|
130
|
+
let changed = false;
|
|
131
|
+
const out = [];
|
|
132
|
+
for (const block of json.content) {
|
|
133
|
+
const cell = scrubCell(block?.type);
|
|
134
|
+
if (cell === 'drop') {
|
|
135
|
+
if (block?.type === 'thinking' || block?.type === 'redacted_thinking') stats.thinking++;
|
|
136
|
+
else stats.unknown.push(String(block?.type));
|
|
137
|
+
changed = true;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (cell === 'convert') {
|
|
141
|
+
stats.converted++;
|
|
142
|
+
out.push({ type: 'text', text: foreignToolBlockText(block) }); // round-3 finding 1
|
|
143
|
+
changed = true;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
out.push(block);
|
|
147
|
+
}
|
|
148
|
+
if (!changed) return null;
|
|
149
|
+
if (statsOut) Object.assign(statsOut, stats);
|
|
150
|
+
if (out.length === 0) out.push({ type: 'text', text: SCRUB_PLACEHOLDER }); // finding 3
|
|
151
|
+
return { ...json, content: out };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Streaming-lane scrub state. Feed it each raw SSE chunk BEFORE res.write; it returns
|
|
156
|
+
* the bytes to write (possibly rewritten, possibly split — renumbering can change an
|
|
157
|
+
* event's length). Parse-level, not regex-level: SSE events are small and framed, and a
|
|
158
|
+
* regex cannot renumber `index` fields safely.
|
|
159
|
+
*/
|
|
160
|
+
export class ProviderThinkingScrubber {
|
|
161
|
+
constructor({ onScrub } = {}) {
|
|
162
|
+
this.onScrub = onScrub || null; // fires once per scrubbed block (instrumentation)
|
|
163
|
+
this.rewrite = null; // Map oldIndex -> newIndex (-1 = suppressed)
|
|
164
|
+
this.keptCount = 0;
|
|
165
|
+
this.sawAnyThinking = false;
|
|
166
|
+
this.buffer = ''; // incomplete SSE tail
|
|
167
|
+
this.scrubbedCount = 0;
|
|
168
|
+
this.convertedCount = 0; // tool blocks converted to text (NOT counted in scrubbedCount)
|
|
169
|
+
this._unknown = [];
|
|
170
|
+
this._pending = null; // { oldIndex, cell:'convert', block, json } while a convertible block streams
|
|
171
|
+
this._stopped = false; // placeholder already injected (message_delta or message_stop)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Per-response stats for describeScrub() — one summary line per response. */
|
|
175
|
+
stats() { return { thinking: this.scrubbedCount, converted: this.convertedCount, unknown: this._unknown }; }
|
|
176
|
+
|
|
177
|
+
/** Process one decoded chunk; returns the string to write (may be ''). */
|
|
178
|
+
push(text) {
|
|
179
|
+
this.buffer += text;
|
|
180
|
+
const events = this.buffer.split('\n\n');
|
|
181
|
+
this.buffer = events.pop() ?? '';
|
|
182
|
+
let out = '';
|
|
183
|
+
for (const ev of events) out += this.event(ev);
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Flush at stream end: process any trailing event without a blank line. */
|
|
188
|
+
flush() {
|
|
189
|
+
const rest = this.buffer;
|
|
190
|
+
this.buffer = '';
|
|
191
|
+
return rest ? this.event(rest) : '';
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
event(ev) {
|
|
195
|
+
const lines = ev.split('\n');
|
|
196
|
+
let data = null;
|
|
197
|
+
let dataLine = -1;
|
|
198
|
+
for (let i = 0; i < lines.length; i++) {
|
|
199
|
+
if (lines[i].startsWith('data:')) { data = lines[i].slice(5).trimStart(); dataLine = i; break; }
|
|
200
|
+
}
|
|
201
|
+
if (data == null) return ev + '\n\n'; // non-data frame (event:, ping) — verbatim
|
|
202
|
+
let j;
|
|
203
|
+
try { j = JSON.parse(data); } catch { return ev + '\n\n'; } // malformed JSON — verbatim
|
|
204
|
+
const t = j?.type;
|
|
205
|
+
if (t === 'content_block_start') {
|
|
206
|
+
const bt = j?.content_block?.type;
|
|
207
|
+
const cell = scrubCell(bt);
|
|
208
|
+
if (cell === 'drop') {
|
|
209
|
+
this.scrubbedCount++;
|
|
210
|
+
this.sawAnyThinking = this.sawAnyThinking || bt === 'thinking' || bt === 'redacted_thinking';
|
|
211
|
+
if (bt !== 'thinking' && bt !== 'redacted_thinking') this._unknown.push(String(bt));
|
|
212
|
+
this.onScrub?.(bt);
|
|
213
|
+
if (!this.rewrite) this.rewrite = new Map();
|
|
214
|
+
this.rewrite.set(j.index, -1); // suppressed: start/delta/stop all dropped
|
|
215
|
+
return '';
|
|
216
|
+
}
|
|
217
|
+
if (cell === 'convert') {
|
|
218
|
+
// Round-3 finding 1: buffer the whole block, then emit it as ONE text block at
|
|
219
|
+
// the renumbered index — the client transcript keeps the call and its result.
|
|
220
|
+
const newIdx = this.nextIndex(j.index);
|
|
221
|
+
this.convertedCount++;
|
|
222
|
+
this._pending = { oldIndex: j.index, block: { ...j.content_block }, json: '' };
|
|
223
|
+
const startEv = { type: 'content_block_start', index: newIdx, content_block: { type: 'text', text: '' } };
|
|
224
|
+
return `event: content_block_start\ndata: ${JSON.stringify(startEv)}\n\n`;
|
|
225
|
+
}
|
|
226
|
+
// Keep: renumber past every suppressed block before it.
|
|
227
|
+
if (!this.rewrite) this.rewrite = new Map();
|
|
228
|
+
this.keptCount = (this.keptCount || 0) + 1;
|
|
229
|
+
const newIdx = this.nextIndex(j.index);
|
|
230
|
+
if (newIdx !== j.index) { j = { ...j, index: newIdx }; return this.render(lines, dataLine, j) + '\n\n'; }
|
|
231
|
+
return ev + '\n\n';
|
|
232
|
+
}
|
|
233
|
+
if (t === 'content_block_delta') {
|
|
234
|
+
if (this._pending && j.index === this._pending.oldIndex) {
|
|
235
|
+
const d = j.delta?.type;
|
|
236
|
+
if (d === 'input_json_delta') this._pending.json += String(j.delta.partial_json ?? '');
|
|
237
|
+
else if (d === 'text_delta') this._pending.block.text = (this._pending.block.text || '') + (j.delta.text ?? '');
|
|
238
|
+
else if (d === 'thinking_delta' || d === 'signature_delta') { /* content only — ignored */ }
|
|
239
|
+
else {
|
|
240
|
+
// A delta type we do not model inside a convertible block: rather than guess
|
|
241
|
+
// (a wrong guess DELETES provider content), fail open and convert from what we
|
|
242
|
+
// have — an accurate bracket summary without the unknown payload.
|
|
243
|
+
this._pending.unknownDelta = true;
|
|
244
|
+
}
|
|
245
|
+
return '';
|
|
246
|
+
}
|
|
247
|
+
if (this.rewrite) {
|
|
248
|
+
const mapped = this.rewrite.get(j.index);
|
|
249
|
+
if (mapped === -1) return ''; // belongs to a suppressed block
|
|
250
|
+
if (mapped !== undefined && mapped !== j.index) {
|
|
251
|
+
j = { ...j, index: mapped };
|
|
252
|
+
return this.render(lines, dataLine, j) + '\n\n';
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return ev + '\n\n';
|
|
256
|
+
}
|
|
257
|
+
if (t === 'content_block_stop') {
|
|
258
|
+
if (this._pending && j.index === this._pending.oldIndex) {
|
|
259
|
+
const p = this._pending;
|
|
260
|
+
this._pending = null;
|
|
261
|
+
const blockIdx = this.rewrite.get(p.oldIndex);
|
|
262
|
+
const block = { ...p.block };
|
|
263
|
+
if (p.json !== '') {
|
|
264
|
+
try { block.input = JSON.parse(p.json); }
|
|
265
|
+
catch { /* provider sent malformed partial JSON — convert from the metadata we hold */ }
|
|
266
|
+
}
|
|
267
|
+
const text = foreignToolBlockText(block);
|
|
268
|
+
const deltaEv = { type: 'content_block_delta', index: blockIdx, delta: { type: 'text_delta', text } };
|
|
269
|
+
return `event: content_block_delta\ndata: ${JSON.stringify(deltaEv)}\n\nevent: content_block_stop\ndata: ${JSON.stringify({ type: 'content_block_stop', index: blockIdx })}\n\n`;
|
|
270
|
+
}
|
|
271
|
+
if (this.rewrite) {
|
|
272
|
+
const mapped = this.rewrite.get(j.index);
|
|
273
|
+
if (mapped === -1) return '';
|
|
274
|
+
if (mapped !== undefined && mapped !== j.index) {
|
|
275
|
+
j = { ...j, index: mapped };
|
|
276
|
+
return this.render(lines, dataLine, j) + '\n\n';
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return ev + '\n\n';
|
|
280
|
+
}
|
|
281
|
+
if (t === 'message_delta' && this.scrubbedCount > 0 && !this.keptCount && !this.convertedCount) {
|
|
282
|
+
// Finding 3 (streaming): every block was suppressed — emit ONE complete placeholder
|
|
283
|
+
// text block before the message closes, so the stored turn is never content:[].
|
|
284
|
+
// A converted tool block counts as content (round-3 finding 1) — no placeholder then.
|
|
285
|
+
this.keptCount = 1;
|
|
286
|
+
this._stopped = true;
|
|
287
|
+
const startEv = { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } };
|
|
288
|
+
const deltaEv = { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: SCRUB_PLACEHOLDER } };
|
|
289
|
+
const stopEv = { type: 'content_block_stop', index: 0 };
|
|
290
|
+
return `event: content_block_start\ndata: ${JSON.stringify(startEv)}\n\n`
|
|
291
|
+
+ `event: content_block_delta\ndata: ${JSON.stringify(deltaEv)}\n\n`
|
|
292
|
+
+ `event: content_block_stop\ndata: ${JSON.stringify(stopEv)}\n\n`
|
|
293
|
+
+ ev + '\n\n';
|
|
294
|
+
}
|
|
295
|
+
if (t === 'message_stop' && !this._stopped && this.scrubbedCount > 0 && !this.keptCount && !this.convertedCount) {
|
|
296
|
+
// Finding 4: a thinking-only stream can close with message_stop and NO message_delta
|
|
297
|
+
// (z.ai compat shape) — inject the placeholder block events BEFORE message_stop so
|
|
298
|
+
// the stored turn is never content:[] on that path either.
|
|
299
|
+
this.keptCount = 1;
|
|
300
|
+
this._stopped = true;
|
|
301
|
+
const startEv = { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } };
|
|
302
|
+
const deltaEv = { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: SCRUB_PLACEHOLDER } };
|
|
303
|
+
const stopEv = { type: 'content_block_stop', index: 0 };
|
|
304
|
+
return `event: content_block_start\ndata: ${JSON.stringify(startEv)}\n\nevent: content_block_delta\ndata: ${JSON.stringify(deltaEv)}\n\nevent: content_block_stop\ndata: ${JSON.stringify(stopEv)}\n\n` + ev + '\n\n';
|
|
305
|
+
}
|
|
306
|
+
// message_start / message_stop / ping / usage: verbatim
|
|
307
|
+
return ev + '\n\n';
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
nextIndex(oldIdx) {
|
|
311
|
+
// Compute the renumbered index for a KEPT or CONVERTED block: count how many
|
|
312
|
+
// suppressed blocks (map value -1) sit before it, subtract. Deterministic; no array reliance.
|
|
313
|
+
if (!this.rewrite) this.rewrite = new Map();
|
|
314
|
+
let shift = 0;
|
|
315
|
+
for (const [o, n] of this.rewrite) if (o < oldIdx && n === -1) shift++;
|
|
316
|
+
const mapped = oldIdx - shift;
|
|
317
|
+
this.rewrite.set(oldIdx, mapped);
|
|
318
|
+
return mapped;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
render(lines, dataLine, j) {
|
|
322
|
+
lines[dataLine] = 'data: ' + JSON.stringify(j);
|
|
323
|
+
return lines.join('\n');
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Is the scrub active for this request? (config flag + provider account) */
|
|
328
|
+
export function scrubEnabledFor(account, config) {
|
|
329
|
+
return Boolean(config?.providerThinkingScrub) && account?.type === 'provider';
|
|
330
|
+
}
|