Adds packages/empty-reply (0.1.0): a reply made of reasoning alone, with end_turn (Anthropic) or response.completed (Responses), loses its end events so Claude Code and Codex retry; a whole reply becomes an error body or status "failed". max_tokens, refusal and incomplete are kept. A stopgap for yetone/magpie#3358 until the gateway checks this itself. Reviewed at 900 head: bun test packages/empty-reply 17 pass, bun scripts/check.mjs all packages ok, bun test scripts 22 pass (mote, Bun 1.4.2). Read every case in cases.json against the Anthropic and Responses stream shapes. The author ran Claude Code 2.1.319 and codex-cli 0.167.0 against a reasoning-only Chat upstream (ordered and rotate). Not run by the reviewer in a live gateway.
28 lines
1.2 KiB
JavaScript
28 lines
1.2 KiB
JavaScript
import * as mod from "./empty-reply.middleware.js"
|
|
import { check } from "../../scripts/middleware.mjs"
|
|
import { expect, test } from "bun:test"
|
|
|
|
check(import.meta.dir, mod)
|
|
|
|
// Claude Code 2.1.319 treated the early-ended stream as a broken connection:
|
|
// it retried three times and gave up with "Connection lost before a response
|
|
// was produced." A retry is a new request, so a rotating group can move on.
|
|
test("drops the end of a stream that said nothing", () => {
|
|
const ctx = { protocol: "anthropic", stream: true, options: {}, state: {} }
|
|
const events = [
|
|
{ type: "message_start", message: { id: "msg_1", type: "message", role: "assistant", content: [] } },
|
|
{ type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } },
|
|
{ type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: "hmm" } },
|
|
{ type: "content_block_stop", index: 0 },
|
|
{ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null } },
|
|
{ type: "message_stop" },
|
|
]
|
|
const out = events.filter((ev) => mod.onEvent(structuredClone(ev), ctx) !== null)
|
|
expect(out.map((ev) => ev.type)).toEqual([
|
|
"message_start",
|
|
"content_block_start",
|
|
"content_block_delta",
|
|
"content_block_stop",
|
|
])
|
|
})
|