Skip to content

Commit af973e5

Browse files
authored
Merge pull request #316 from lidge-jun/agent/fix-anthropic-stream-eof
fix(anthropic): preserve terminal SSE frames
2 parents d56d294 + 2467ced commit af973e5

5 files changed

Lines changed: 259 additions & 38 deletions

File tree

‎docs-site/src/content/docs/reference/adapters.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ streams the response back **untranslated**.
6262
output headroom, and **drops `temperature`/`top_p`** when thinking is enabled (Anthropic forbids
6363
them there).
6464
- Always sends `anthropic-version: 2023-06-01`. Streams `content_block_delta` (`text_delta`,
65-
`thinking_delta`, `input_json_delta`).
65+
`thinking_delta`, compatible `reasoning_delta`, `input_json_delta`). The SSE decoder preserves
66+
event state across fetch chunks and accepts a terminal `message_stop` without a trailing newline.
6667

6768
## `google`
6869

‎src/adapters/anthropic.ts‎

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { normalizeAnthropicImages } from "./anthropic-image-normalize";
2121
import { neutralizeIdentity } from "./identity";
2222
import { CLAUDE_CODE_HEADERS, claudeCodeSessionId } from "./client-fingerprint";
2323
import { buildNonOpenAIToolCatalogNudgeForTools } from "./tool-catalog-nudge";
24+
import { decodeServerSentEvents } from "../lib/sse-decoder";
2425

2526
/** Map a user content part to an Anthropic content block (text or image source). */
2627
function toAnthropicContentPart(p: OcxContentPart): unknown {
@@ -719,9 +720,6 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
719720
return;
720721
}
721722

722-
const reader = response.body.getReader();
723-
const decoder = new TextDecoder();
724-
let buffer = "";
725723
let currentBlockType = "";
726724
let currentToolCallId = "";
727725
let currentToolCallName = "";
@@ -739,34 +737,19 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
739737
};
740738
};
741739

742-
try {
743-
while (true) {
744-
const { done, value } = await reader.read();
745-
if (done) break;
746-
buffer += decoder.decode(value, { stream: true });
740+
for await (const record of decodeServerSentEvents(response.body)) {
741+
const payload = record.data.trim();
742+
if (!payload) continue;
747743

748-
const lines = buffer.split("\n");
749-
buffer = lines.pop() ?? "";
750-
751-
let currentEventType = "";
752-
for (const line of lines) {
753-
if (line.startsWith("event: ")) {
754-
currentEventType = line.slice(7).trim();
755-
continue;
756-
}
757-
if (!line.startsWith("data: ")) continue;
758-
const payload = line.slice(6).trim();
759-
if (!payload) continue;
760-
761-
let data: Record<string, unknown>;
762-
try {
763-
data = JSON.parse(payload) as Record<string, unknown>;
764-
} catch {
765-
debugDroppedFrame("anthropic", payload);
766-
continue;
767-
}
744+
let data: Record<string, unknown>;
745+
try {
746+
data = JSON.parse(payload) as Record<string, unknown>;
747+
} catch {
748+
debugDroppedFrame("anthropic", payload);
749+
continue;
750+
}
768751

769-
switch (currentEventType || data.type) {
752+
switch (record.event || data.type) {
770753
case "message_start": {
771754
const message = data.message as { usage?: Record<string, number> } | undefined;
772755
pendingUsage = mergeAnthropicUsage(pendingUsage, message?.usage);
@@ -794,7 +777,12 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
794777
yield { type: "text_delta", text: delta.text };
795778
} else if (delta.type === "thinking_delta" && typeof delta.thinking === "string") {
796779
yield { type: "thinking_delta", thinking: delta.thinking };
797-
} else if (delta.type === "signature_delta" && typeof delta.signature === "string" && currentBlockType === "thinking") {
780+
} else if (delta.type === "reasoning_delta" && typeof delta.reasoning === "string") {
781+
// Some Anthropic-compatible reasoning models use `reasoning` names for the
782+
// otherwise equivalent thinking block. Preserve it as raw reasoning and keep
783+
// later text blocks independent.
784+
yield { type: "thinking_delta", thinking: delta.reasoning };
785+
} else if (delta.type === "signature_delta" && typeof delta.signature === "string" && (currentBlockType === "thinking" || currentBlockType === "reasoning")) {
798786
// Arrives once, just before the thinking block's content_block_stop; block-scoped
799787
// so a stray signature on a non-thinking block can never be captured.
800788
yield { type: "thinking_signature", signature: delta.signature };
@@ -827,20 +815,15 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
827815
yield { type: "error", message: err?.message ?? "Anthropic error" };
828816
return;
829817
}
830-
}
831-
currentEventType = "";
832-
}
833818
}
834-
if (pendingUsage && !emittedDone) yield* emitDone();
835-
} finally {
836-
reader.releaseLock();
837819
}
820+
if (pendingUsage && !emittedDone) yield* emitDone();
838821
},
839822

840823
async parseResponse(response: Response): Promise<AdapterEvent[]> {
841824
const json = await response.json() as Record<string, unknown>;
842825
const events: AdapterEvent[] = [];
843-
const content = json.content as { type: string; text?: string; id?: string; name?: string; input?: unknown; thinking?: string; signature?: string; data?: string }[] | undefined;
826+
const content = json.content as { type: string; text?: string; id?: string; name?: string; input?: unknown; thinking?: string; reasoning?: string; signature?: string; data?: string }[] | undefined;
844827
if (content) {
845828
for (const block of content) {
846829
if (block.type === "text" && block.text) {
@@ -850,6 +833,8 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
850833
if (typeof block.signature === "string" && block.signature) {
851834
events.push({ type: "thinking_signature", signature: block.signature });
852835
}
836+
} else if (block.type === "reasoning" && typeof block.reasoning === "string") {
837+
events.push({ type: "thinking_delta", thinking: block.reasoning });
853838
} else if (block.type === "redacted_thinking" && typeof block.data === "string") {
854839
events.push({ type: "redacted_thinking", data: block.data });
855840
} else if (block.type === "tool_use") {

‎src/lib/sse-decoder.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export interface ServerSentEvent {
2+
event?: string;
3+
data: string;
4+
}
5+
6+
/**
7+
* Decode text/event-stream records across arbitrary fetch chunk boundaries.
8+
*
9+
* The final record is dispatched at EOF even when the upstream omits the trailing blank line or
10+
* final newline. That matters for compatible APIs that place a terminal event in the last bytes of
11+
* the body: dropping that record turns a successful response into an adapter_eof failure.
12+
*/
13+
export async function* decodeServerSentEvents(
14+
source: ReadableStream<Uint8Array>,
15+
): AsyncGenerator<ServerSentEvent> {
16+
const reader = source.getReader();
17+
const decoder = new TextDecoder();
18+
let buffer = "";
19+
let event: string | undefined;
20+
let dataLines: string[] = [];
21+
22+
const dispatch = (): ServerSentEvent | undefined => {
23+
if (dataLines.length === 0) {
24+
event = undefined;
25+
return undefined;
26+
}
27+
const record = { ...(event ? { event } : {}), data: dataLines.join("\n") };
28+
event = undefined;
29+
dataLines = [];
30+
return record;
31+
};
32+
33+
const acceptLine = (rawLine: string): ServerSentEvent | undefined => {
34+
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
35+
if (line === "") return dispatch();
36+
if (line.startsWith(":")) return undefined;
37+
38+
const colon = line.indexOf(":");
39+
const field = colon < 0 ? line : line.slice(0, colon);
40+
let value = colon < 0 ? "" : line.slice(colon + 1);
41+
if (value.startsWith(" ")) value = value.slice(1);
42+
43+
if (field === "event") event = value;
44+
else if (field === "data") dataLines.push(value);
45+
return undefined;
46+
};
47+
48+
try {
49+
while (true) {
50+
const { done, value } = await reader.read();
51+
if (value) buffer += decoder.decode(value, { stream: !done });
52+
53+
let newline: number;
54+
while ((newline = buffer.indexOf("\n")) >= 0) {
55+
const record = acceptLine(buffer.slice(0, newline));
56+
buffer = buffer.slice(newline + 1);
57+
if (record) yield record;
58+
}
59+
60+
if (!done) continue;
61+
buffer += decoder.decode();
62+
if (buffer.length > 0) {
63+
const record = acceptLine(buffer);
64+
buffer = "";
65+
if (record) yield record;
66+
}
67+
const finalRecord = dispatch();
68+
if (finalRecord) yield finalRecord;
69+
break;
70+
}
71+
} finally {
72+
try { await reader.cancel(); } catch { /* already closed/errored */ }
73+
try { reader.releaseLock(); } catch { /* already released */ }
74+
}
75+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { createAnthropicAdapter } from "../src/adapters/anthropic";
3+
import { bridgeToResponsesSSE } from "../src/bridge";
4+
import { responsesSseToAnthropicSse } from "../src/claude/outbound";
5+
import type { AdapterEvent, OcxProviderConfig } from "../src/types";
6+
7+
const provider = {
8+
adapter: "anthropic",
9+
baseUrl: "https://api.kimi.com/coding",
10+
apiKey: "test-key",
11+
authMode: "key",
12+
} as OcxProviderConfig;
13+
14+
const kimiCompatibleSse = [
15+
'event: message_start\ndata: {"type":"message_start","message":{}}',
16+
'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"reasoning","reasoning":""}}',
17+
'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"reasoning_delta","reasoning":"think"}}',
18+
'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}',
19+
'event: content_block_start\ndata: {"type":"content_block_start","index":1,"content_block":{"type":"text","text":""}}',
20+
'event: content_block_delta\ndata: {"type":"content_block_delta","index":1,"delta":{"type":"text_delta","text":"visible"}}',
21+
'event: content_block_stop\ndata: {"type":"content_block_stop","index":1}',
22+
// Deliberately no usage snapshot and no newline after the terminal data line.
23+
'event: message_stop\ndata: {"type":"message_stop"}',
24+
].join("\n\n");
25+
26+
function arbitrarilyChunkedResponse(text = kimiCompatibleSse): Response {
27+
const encoder = new TextEncoder();
28+
const bytes = encoder.encode(text);
29+
const cuts = [3, 19, 47, 101, 173, 251, 337, 419, bytes.length];
30+
return new Response(new ReadableStream<Uint8Array>({
31+
start(controller) {
32+
let offset = 0;
33+
for (const end of cuts) {
34+
if (end <= offset) continue;
35+
controller.enqueue(bytes.slice(offset, Math.min(end, bytes.length)));
36+
offset = end;
37+
if (offset >= bytes.length) break;
38+
}
39+
controller.close();
40+
},
41+
}));
42+
}
43+
44+
async function collectAdapterEvents(response: Response): Promise<AdapterEvent[]> {
45+
const events: AdapterEvent[] = [];
46+
for await (const event of createAnthropicAdapter(provider).parseStream(response)) events.push(event);
47+
return events;
48+
}
49+
50+
describe("Anthropic-compatible reasoning stream termination (#312)", () => {
51+
test("preserves reasoning and visible text, then emits done from final message_stop", async () => {
52+
const events = await collectAdapterEvents(arbitrarilyChunkedResponse());
53+
54+
expect(events).toContainEqual({ type: "thinking_delta", thinking: "think" });
55+
expect(events).toContainEqual({ type: "text_delta", text: "visible" });
56+
expect(events.at(-1)).toEqual({ type: "done", usage: undefined });
57+
});
58+
59+
test("Responses bridge completes instead of reporting adapter_eof", async () => {
60+
const responses = bridgeToResponsesSSE(
61+
createAnthropicAdapter(provider).parseStream(arbitrarilyChunkedResponse()),
62+
"kimi/k3",
63+
undefined,
64+
undefined,
65+
undefined,
66+
undefined,
67+
0,
68+
);
69+
const text = await new Response(responses).text();
70+
71+
expect(text).toContain("response.reasoning_summary_text.delta");
72+
expect(text).toContain("response.output_text.delta");
73+
expect(text).toContain("response.completed");
74+
expect(text).not.toContain("response.incomplete");
75+
expect(text).toContain("visible");
76+
});
77+
78+
test("Claude Messages translation keeps content blocks and message_stop", async () => {
79+
const responses = bridgeToResponsesSSE(
80+
createAnthropicAdapter(provider).parseStream(arbitrarilyChunkedResponse()),
81+
"kimi/k3",
82+
undefined,
83+
undefined,
84+
undefined,
85+
undefined,
86+
0,
87+
);
88+
const anthropic = responsesSseToAnthropicSse(responses, "k3", { pingIntervalMs: 0 });
89+
const text = await new Response(anthropic).text();
90+
91+
expect(text).toContain('"type":"thinking_delta","thinking":"think"');
92+
expect(text).toContain('"type":"text_delta","text":"visible"');
93+
expect(text).toContain("event: message_stop");
94+
});
95+
96+
test("non-streaming compatible reasoning blocks map without hiding later text", async () => {
97+
const events = await createAnthropicAdapter(provider).parseResponse(new Response(JSON.stringify({
98+
content: [
99+
{ type: "reasoning", reasoning: "think" },
100+
{ type: "text", text: "visible" },
101+
],
102+
usage: { input_tokens: 2, output_tokens: 3 },
103+
stop_reason: "end_turn",
104+
})));
105+
106+
expect(events).toEqual([
107+
{ type: "thinking_delta", thinking: "think" },
108+
{ type: "text_delta", text: "visible" },
109+
{ type: "done", usage: { inputTokens: 2, outputTokens: 3 }, stopReason: "end_turn" },
110+
]);
111+
});
112+
});

‎tests/sse-decoder.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { decodeServerSentEvents } from "../src/lib/sse-decoder";
3+
4+
function chunkedStream(chunks: string[]): ReadableStream<Uint8Array> {
5+
const encoder = new TextEncoder();
6+
return new ReadableStream({
7+
start(controller) {
8+
for (const chunk of chunks) controller.enqueue(encoder.encode(chunk));
9+
controller.close();
10+
},
11+
});
12+
}
13+
14+
async function collect(chunks: string[]) {
15+
const records = [];
16+
for await (const record of decodeServerSentEvents(chunkedStream(chunks))) records.push(record);
17+
return records;
18+
}
19+
20+
describe("text/event-stream decoder", () => {
21+
test("preserves event state across arbitrary reader chunks", async () => {
22+
expect(await collect([
23+
"event: content_",
24+
"block_delta\n",
25+
"data: {\"type\":\"content_block_delta\"}\n\n",
26+
])).toEqual([{
27+
event: "content_block_delta",
28+
data: '{"type":"content_block_delta"}',
29+
}]);
30+
});
31+
32+
test("dispatches the terminal record without a final newline or blank delimiter", async () => {
33+
expect(await collect([
34+
"event: message_stop\n",
35+
'data: {"type":"message_stop"}',
36+
])).toEqual([{
37+
event: "message_stop",
38+
data: '{"type":"message_stop"}',
39+
}]);
40+
});
41+
42+
test("joins multiline data and accepts CRLF framing", async () => {
43+
expect(await collect([
44+
"event: custom\r\ndata: first\r\n",
45+
"data: second\r\n\r\n",
46+
])).toEqual([{ event: "custom", data: "first\nsecond" }]);
47+
});
48+
});

0 commit comments

Comments
 (0)