Files
Netcatty/infrastructure/ai/streamStateErrors.test.ts
T
陈大猫andClaude Opus 4.7 5be9bb58df fix(ai): suppress SDK reasoning/text state-machine error chunks (#1110)
Issue #1101 follow-up. When a third-party Anthropic-compat backend
streams thinking deltas without first emitting a `reasoning-start`
content-block signal (DeepSeek's \`-v4-flash\` is the canonical
offender), the Vercel AI SDK has nothing registered for the incoming
\`part.id\` and enqueues an \`error\` chunk on \`fullStream\` with the
text \`reasoning part <id> not found\` — once per orphan delta. The
analogous error exists for text parts.

These are internal SDK bookkeeping noise, not user-facing errors. Our
\`case 'error'\` handler was treating each one as a real error: it
appended an empty assistant message carrying the SDK string. The
visible damage was a wall of red banners in the chat panel — but the
worse damage was protocol-level. On the next turn we rebuild
\`sdkMessages\` from local history; the placeholder assistants slot
in between the assistant that holds the parallel \`tool_use\` blocks
and the subsequent \`role: 'tool'\` messages with their results. The
Anthropic SDK's \`groupIntoBlocks\` only merges *consecutive* tool
messages into a single user block, so the tool_results no longer
immediately follow their tool_use parent — and the backend rejects
the request as
\`400 messages.N: tool_use ids were found without tool_result blocks
immediately after\`. \`messages.N\` was literally counting our 12-ish
phantom assistants.

Adds \`isSdkStreamStateError(error)\` to
\`infrastructure/ai/shared/streamStateErrors.ts\` (matches
\`/^(reasoning|text)\\s+part\\s+\\S+\\s+not\\s+found$/i\` against
string / Error / \`{ message }\` shapes), and skips placeholder
emission for matching errors in the streaming hook. Drops a
\`console.warn\` so the noise is still visible in devtools when
debugging.

5 unit tests cover positive matches, the Error/object wrappings,
non-matches that should stay surfaced, and non-string inputs.

Refs #1101.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 01:02:26 +08:00

40 lines
1.8 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { isSdkStreamStateError } from "./shared/streamStateErrors";
test("isSdkStreamStateError matches the reasoning-part orphan-delta error", () => {
assert.equal(isSdkStreamStateError("reasoning part 0 not found"), true);
assert.equal(isSdkStreamStateError("reasoning part abc-123 not found"), true);
assert.equal(isSdkStreamStateError("Reasoning Part 0 Not Found"), true);
assert.equal(isSdkStreamStateError(" reasoning part 7 not found "), true);
});
test("isSdkStreamStateError matches the text-part orphan-delta error", () => {
assert.equal(isSdkStreamStateError("text part 0 not found"), true);
assert.equal(isSdkStreamStateError("text part X not found"), true);
});
test("isSdkStreamStateError accepts the error wrapped in an Error or `{ message }`", () => {
assert.equal(isSdkStreamStateError(new Error("reasoning part 0 not found")), true);
assert.equal(isSdkStreamStateError({ message: "reasoning part 0 not found" }), true);
});
test("isSdkStreamStateError ignores unrelated error strings", () => {
assert.equal(isSdkStreamStateError("Network error"), false);
assert.equal(
isSdkStreamStateError("400 messages.13: tool_use ids were found without tool_result blocks"),
false,
);
assert.equal(isSdkStreamStateError("reasoning part not found"), false, "missing the id segment shouldn't match");
assert.equal(isSdkStreamStateError("a reasoning part 0 not found"), false, "leading text shouldn't match");
});
test("isSdkStreamStateError handles non-error inputs without throwing", () => {
assert.equal(isSdkStreamStateError(undefined), false);
assert.equal(isSdkStreamStateError(null), false);
assert.equal(isSdkStreamStateError(42), false);
assert.equal(isSdkStreamStateError({}), false);
assert.equal(isSdkStreamStateError([]), false);
});