Repository navigation
fix(openai-shim): always emit message_delta with usage to match Claude Code stream protocol - #2599
Conversation
…e Code stream protocol Some providers (Aliyun MaaS, OpenRouter) deliver finish_reason and usage on separate chunks; the shim emitted a usage-less message_delta first, crashing SDK consumers (e.g. Claudian) that read event.usage unguarded. Defer the terminal delta until usage arrives or the stream ends, with a zeroed usage fallback.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Summary
openaiStreamToAnthropicemitted amessage_deltawithout ausageobject whenever the finish_reason chunk carried no usage, then a second delta once usage arrived on a later chunk.usageonmessage_delta, so SDK consumers that readevent.usageunguarded (e.g. Claudian, anyincludePartialMessageslistener) crash withCannot read properties of undefined (reading 'input_tokens').message_deltauntil usage is available (or the stream ends), so exactly one delta is emitted and it always carries ausageobject; the truncated-stream path gets a zeroed usage. Also keeps the most recently seen chunk usage so a usage-before-finish ordering is no longer dropped.Impact
message_delta; usage accounting is preserved (compaction and cost tracking read it).Testing
src/services/api/openaiShim/→ core checks + focused provider-stream regression tests.e04864d3(drop deadhasEmittedFinalUsageassignment flagged by CodeQL — one-line deletion, no behavior change) reran the focused stream suite and the intent scan; all other results from the initial revision remain applicable (inputs unchanged).bun run test ./src/services/api/openaiShim/— 489 pass / 0 fail (5 assertions updated to the new single-delta shape; rerun green aftere04864d3).bun run test ./src/services/api/openaiShim/requestExecutor.integration.test.ts ./src/services/api/openaiShim/requestExecutor.test.ts— 240 pass / 0 fail.bun run check:static— pass (0 ESLint errors; 7 pre-existinganywarnings insrc/entrypoints/sdk*, unchanged by this PR).bun run typecheck:type-tests— pass (11 files).node bin/openclaude --versionandNODE_DISABLE_COMPILE_CACHE=1 node bin/openclaude --version— both print0.37.0 (OpenClaude).bun run build— succeeds; runtime re-check of the failing shape:message_delta count: 1, all have usage: True.git fetch https://github.com/Twigpine/openclaude.git main && bun run security:pr-scan -- --base FETCH_HEAD --head HEAD— no suspicious additions (rerun after follow-up commit).bun run typecheck:fastis blocked locally: its script shells out tobun checkfrom the filesystem root, and the local Bun 1.3.11 has nocheckbuiltin (error: Script not found "check").bun run typecheck(tsc) was run instead — no errors in the changed files; the reported errors are pre-existing in unrelated files (e.g.src/components/HighlightedCode/Fallback.test.tsx,src/entrypoints/sdk/sdkUtilityTypes.ts) and exist on the base as well. CI runs the authoritativetypecheck:fast.web/inputs changed).e04864d3; monitoring the re-run.Notes
glm-5.3) via a live--print --output-format stream-json --include-partial-messagesrun before/after the fix (two deltas → one delta with usage); unit tests cover the split finish/usage chunk shape directly.Summary by CodeRabbit