|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createAnthropicAdapter as createAnthropicAdapterProduction } from "../src/adapters/anthropic"; |
| 3 | +import { FREE_PROVIDER_DIRECTORY } from "../src/providers/free-directory"; |
| 4 | +import type { AdapterEvent, OcxProviderConfig } from "../src/types"; |
| 5 | +import { withTestTranslatorBudget } from "./helpers/translator-budget"; |
| 6 | + |
| 7 | +/** |
| 8 | + * #658: AgentRouter's Anthropic-compatible endpoint can close the stream before |
| 9 | + * `content_block_stop`, `message_delta`, and `message_stop`. The default adapter treats |
| 10 | + * that EOF as a fatal truncation; with `anthropicEofTolerance` enabled it may complete |
| 11 | + * only when visible text was received or an open tool call has complete JSON-object |
| 12 | + * arguments. These tests pin the wire behavior; no request reaches agentrouter.org. |
| 13 | + */ |
| 14 | + |
| 15 | +const createAnthropicAdapter = (...args: Parameters<typeof createAnthropicAdapterProduction>) => |
| 16 | + withTestTranslatorBudget(createAnthropicAdapterProduction(...args)); |
| 17 | + |
| 18 | +function providerFor(extra: Partial<OcxProviderConfig> = {}): OcxProviderConfig { |
| 19 | + return { |
| 20 | + adapter: "anthropic", |
| 21 | + baseUrl: "https://agentrouter.org", |
| 22 | + apiKey: "test-key", |
| 23 | + authMode: "key", |
| 24 | + ...extra, |
| 25 | + } as OcxProviderConfig; |
| 26 | +} |
| 27 | + |
| 28 | +const strict = providerFor(); |
| 29 | +const tolerant = providerFor({ anthropicEofTolerance: true }); |
| 30 | + |
| 31 | +const TRUNCATION = "upstream stream ended before message_stop — possible truncation"; |
| 32 | + |
| 33 | +function sseResponse(events: string[]): Response { |
| 34 | + return new Response(events.join("\n\n"), { headers: { "content-type": "text/event-stream" } }); |
| 35 | +} |
| 36 | + |
| 37 | +async function collect(provider: OcxProviderConfig, events: string[]): Promise<AdapterEvent[]> { |
| 38 | + const out: AdapterEvent[] = []; |
| 39 | + for await (const event of createAnthropicAdapter(provider).parseStream(sseResponse(events))) out.push(event); |
| 40 | + return out; |
| 41 | +} |
| 42 | + |
| 43 | +const textEof = [ |
| 44 | + 'event: message_start\ndata: {"type":"message_start","message":{"usage":{"input_tokens":2}}}', |
| 45 | + 'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}', |
| 46 | + 'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"visible"}}', |
| 47 | +]; |
| 48 | + |
| 49 | +function toolEof(partialJson: string, id = "toolu_1"): string[] { |
| 50 | + return [ |
| 51 | + 'event: message_start\ndata: {"type":"message_start","message":{}}', |
| 52 | + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "tool_use", id, name: "get_weather" } })}`, |
| 53 | + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: partialJson } })}`, |
| 54 | + ]; |
| 55 | +} |
| 56 | + |
| 57 | +describe("AgentRouter Anthropic EOF tolerance (#658)", () => { |
| 58 | + test("text EOF completes when anthropicEofTolerance is enabled", async () => { |
| 59 | + const events = await collect(tolerant, textEof); |
| 60 | + |
| 61 | + expect(events).toContainEqual({ type: "text_delta", text: "visible" }); |
| 62 | + expect(events.at(-1)).toEqual({ type: "done", usage: { inputTokens: 2, outputTokens: 0 } }); |
| 63 | + expect(events.some(event => event.type === "error")).toBe(false); |
| 64 | + }); |
| 65 | + |
| 66 | + test("the same EOF without the capability stays a truncation error", async () => { |
| 67 | + const events = await collect(strict, textEof); |
| 68 | + |
| 69 | + expect(events.at(-1)).toEqual({ type: "error", message: TRUNCATION }); |
| 70 | + expect(events.some(event => event.type === "done")).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + test("a complete tool call at EOF closes and completes", async () => { |
| 74 | + const events = await collect(tolerant, toolEof('{"value":42}')); |
| 75 | + |
| 76 | + expect(events).toContainEqual({ type: "tool_call_start", id: "toolu_1", name: "get_weather" }); |
| 77 | + expect(events).toContainEqual({ type: "tool_call_delta", arguments: '{"value":42}' }); |
| 78 | + expect(events.at(-1)).toEqual({ type: "done", usage: undefined }); |
| 79 | + expect(events.some(event => event.type === "error")).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + test("an incomplete tool call at EOF remains a truncation error", async () => { |
| 83 | + const events = await collect(tolerant, toolEof('{"value":')); |
| 84 | + |
| 85 | + expect(events.at(-1)).toEqual({ type: "error", message: TRUNCATION }); |
| 86 | + expect(events.some(event => event.type === "done" || event.type === "tool_call_end")).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + test("EOF before any usable content remains a truncation error", async () => { |
| 90 | + const events = await collect(tolerant, [ |
| 91 | + 'event: message_start\ndata: {"type":"message_start","message":{}}', |
| 92 | + ]); |
| 93 | + |
| 94 | + expect(events.at(-1)).toEqual({ type: "error", message: TRUNCATION }); |
| 95 | + }); |
| 96 | + |
| 97 | + test("a missing tool_use id gets a stable synthesized id on the tolerant path", async () => { |
| 98 | + const events = await collect(tolerant, toolEof('{"value":42}', "")); |
| 99 | + const start = events.find(event => event.type === "tool_call_start"); |
| 100 | + |
| 101 | + expect(start?.type).toBe("tool_call_start"); |
| 102 | + expect((start as { id: string }).id).toMatch(/^toolu_[0-9a-f]{24}$/); |
| 103 | + expect(events.at(-1)).toEqual({ type: "done", usage: undefined }); |
| 104 | + }); |
| 105 | + |
| 106 | + test("non-stream concatenated tool input keeps the last valid object when enabled", async () => { |
| 107 | + const payload = JSON.stringify({ |
| 108 | + content: [{ type: "tool_use", id: "toolu_1", name: "get_weather", input: '{}{"value":42}' }], |
| 109 | + }); |
| 110 | + |
| 111 | + const tolerantEvents = await createAnthropicAdapter(tolerant).parseResponse(new Response(payload)); |
| 112 | + expect(tolerantEvents).toContainEqual({ type: "tool_call_delta", arguments: '{"value":42}' }); |
| 113 | + |
| 114 | + const strictEvents = await createAnthropicAdapter(strict).parseResponse(new Response(payload)); |
| 115 | + expect(strictEvents).toContainEqual({ type: "tool_call_delta", arguments: "{}" }); |
| 116 | + }); |
| 117 | + |
| 118 | + test("the AgentRouter directory row declares the EOF tolerance capability", () => { |
| 119 | + const row = FREE_PROVIDER_DIRECTORY.find(provider => provider.id === "agentrouter"); |
| 120 | + expect(row?.anthropicEofTolerance).toBe(true); |
| 121 | + }); |
| 122 | +}); |
0 commit comments