|
| 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 | +}); |
0 commit comments