mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
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>
51 lines
2.1 KiB
TypeScript
51 lines
2.1 KiB
TypeScript
/**
|
|
* Helpers for detecting Vercel AI SDK internal stream-state errors.
|
|
*
|
|
* Background — issue #1101 follow-up:
|
|
*
|
|
* When a third-party Anthropic-compat backend (DeepSeek's
|
|
* `deepseek-v4-flash` is the canonical offender) streams thinking
|
|
* deltas without first emitting a `reasoning-start` content-block
|
|
* signal, the Vercel AI SDK's reasoning state machine 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 chunks are *internal SDK bookkeeping noise*, not user-facing
|
|
* errors. Worse, treating them as real errors (adding a placeholder
|
|
* assistant message for each) breaks tool_use/tool_result contiguity
|
|
* on the next turn: the Anthropic message grouper splits the
|
|
* tool-result `role: 'tool'` messages from their parent tool_use
|
|
* `role: 'assistant'`, and the backend responds with
|
|
* `400 messages.N: tool_use ids were found without tool_result blocks
|
|
* immediately after`.
|
|
*
|
|
* Filtering these specific errors at the chunk-handler boundary
|
|
* stops the cascade: the orphan deltas are dropped silently (the SDK
|
|
* continues processing other chunks), no fake assistant messages
|
|
* land in history, and the next turn's request stays well-formed.
|
|
*/
|
|
|
|
const STATE_ERROR_PATTERN = /^(?:reasoning|text)\s+part\s+\S+\s+not\s+found$/i;
|
|
|
|
/**
|
|
* Return true if `error` is one of the SDK's internal stream-state
|
|
* tracking errors (e.g. an out-of-order reasoning delta). Accepts
|
|
* the loose `unknown` shape that comes off the chunk so callers
|
|
* don't need to narrow upstream.
|
|
*/
|
|
export function isSdkStreamStateError(error: unknown): boolean {
|
|
if (typeof error === 'string') {
|
|
return STATE_ERROR_PATTERN.test(error.trim());
|
|
}
|
|
if (error instanceof Error) {
|
|
return STATE_ERROR_PATTERN.test(error.message.trim());
|
|
}
|
|
if (error && typeof error === 'object' && 'message' in error) {
|
|
const msg = (error as { message?: unknown }).message;
|
|
if (typeof msg === 'string') return STATE_ERROR_PATTERN.test(msg.trim());
|
|
}
|
|
return false;
|
|
}
|