Skip to content

Commit 4d2c925

Browse files
committed
test(ai): retain direct Anthropic stream regressions
1 parent 06d953f commit 4d2c925

1 file changed

Lines changed: 208 additions & 0 deletions

File tree

‎packages/ai/src/providers/anthropic.test.ts‎

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,117 @@ describe("Anthropic provider", () => {
13581358

13591359
expect((capturedPayload as { fallbacks?: unknown }).fallbacks).toBeUndefined();
13601360
});
1361+
1362+
it("rebuilds Fable output at a mid-stream server-side fallback boundary", async () => {
1363+
const client = createAnthropicSseClient([
1364+
{
1365+
type: "message_start",
1366+
message: {
1367+
id: "msg_fallback",
1368+
model: "claude-fable-5",
1369+
usage: { input_tokens: 5, output_tokens: 0 },
1370+
},
1371+
},
1372+
{
1373+
type: "content_block_start",
1374+
index: 0,
1375+
content_block: { type: "thinking", thinking: "" },
1376+
},
1377+
{
1378+
type: "content_block_delta",
1379+
index: 0,
1380+
delta: { type: "thinking_delta", thinking: "pre-boundary reasoning" },
1381+
},
1382+
{ type: "content_block_stop", index: 0 },
1383+
{
1384+
type: "content_block_start",
1385+
index: 1,
1386+
content_block: { type: "text", text: "" },
1387+
},
1388+
{
1389+
type: "content_block_delta",
1390+
index: 1,
1391+
delta: { type: "text_delta", text: "partial " },
1392+
},
1393+
{ type: "content_block_stop", index: 1 },
1394+
{
1395+
type: "content_block_start",
1396+
index: 2,
1397+
content_block: { type: "tool_use", id: "call_1", name: "lookup", input: {} },
1398+
},
1399+
{ type: "content_block_stop", index: 2 },
1400+
{
1401+
type: "content_block_start",
1402+
index: 3,
1403+
content_block: {
1404+
type: "fallback",
1405+
from: { model: "claude-fable-5" },
1406+
to: { model: "claude-opus-4-8" },
1407+
},
1408+
},
1409+
{ type: "content_block_stop", index: 3 },
1410+
{
1411+
type: "content_block_start",
1412+
index: 4,
1413+
content_block: { type: "text", text: "" },
1414+
},
1415+
{
1416+
type: "content_block_delta",
1417+
index: 4,
1418+
delta: { type: "text_delta", text: "continued" },
1419+
},
1420+
{ type: "content_block_stop", index: 4 },
1421+
{
1422+
type: "message_delta",
1423+
delta: { stop_reason: "end_turn" },
1424+
usage: { input_tokens: 5, output_tokens: 9 },
1425+
},
1426+
{ type: "message_stop" },
1427+
]);
1428+
1429+
const stream = streamAnthropic(
1430+
makeAnthropicModel({
1431+
id: "claude-fable-5",
1432+
name: "Claude Fable 5",
1433+
cost: { input: 10, output: 50, cacheRead: 1, cacheWrite: 12.5 },
1434+
}),
1435+
{ messages: [{ role: "user", content: "hello", timestamp: 0 }] },
1436+
{ apiKey: "sk-ant-provider", client: client as never },
1437+
);
1438+
const eventTypes: string[] = [];
1439+
for await (const event of stream) {
1440+
eventTypes.push(event.type);
1441+
}
1442+
const result = await stream.result();
1443+
1444+
// Pre-boundary thinking/tool blocks must not replay or execute; text is
1445+
// the continuation prefix the fallback model built on.
1446+
expect(result.stopReason).toBe("stop");
1447+
expect(result.content).toEqual([
1448+
{ type: "text", text: "partial " },
1449+
{ type: "text", text: "continued" },
1450+
]);
1451+
expect(result.responseModel).toBe("claude-opus-4-8");
1452+
expect(result.diagnostics).toEqual([
1453+
expect.objectContaining({
1454+
type: "provider_fallback",
1455+
details: {
1456+
provider: "anthropic",
1457+
fromModel: "claude-fable-5",
1458+
toModel: "claude-opus-4-8",
1459+
},
1460+
}),
1461+
]);
1462+
expect(eventTypes).not.toContain("thinking_start");
1463+
expect(eventTypes).not.toContain("toolcall_start");
1464+
expect(eventTypes.filter((type) => type === "start")).toHaveLength(1);
1465+
// Fallback-served turns bill at the serving model's rates, not Fable's:
1466+
// 5 input tokens at $5/MTok plus 9 output tokens at $25/MTok.
1467+
expect(result.usage.cost.input).toBeCloseTo(0.000025, 10);
1468+
expect(result.usage.cost.output).toBeCloseTo(0.000225, 10);
1469+
expect(result.usage.cost.total).toBeCloseTo(0.00025, 10);
1470+
});
1471+
13611472
it("records a pre-output server-side fallback and keeps the continuation", async () => {
13621473
const client = createAnthropicSseClient([
13631474
{
@@ -1419,6 +1530,55 @@ describe("Anthropic provider", () => {
14191530
]);
14201531
expect(result.usage.cost.total).toBeCloseTo(0.000075, 10);
14211532
});
1533+
1534+
it("routes interleaved active content blocks by their event indexes", async () => {
1535+
const client = createAnthropicSseClient([
1536+
{
1537+
type: "message_start",
1538+
message: { id: "msg_interleaved", usage: { input_tokens: 1, output_tokens: 0 } },
1539+
},
1540+
{
1541+
type: "content_block_start",
1542+
index: 0,
1543+
content_block: { type: "text" },
1544+
},
1545+
{
1546+
type: "content_block_start",
1547+
index: 1,
1548+
content_block: { type: "text" },
1549+
},
1550+
{
1551+
type: "content_block_delta",
1552+
index: 1,
1553+
delta: { type: "text_delta", text: "second" },
1554+
},
1555+
{
1556+
type: "content_block_delta",
1557+
index: 0,
1558+
delta: { type: "text_delta", text: "first" },
1559+
},
1560+
{ type: "content_block_stop", index: 1 },
1561+
{ type: "content_block_stop", index: 0 },
1562+
{
1563+
type: "message_delta",
1564+
delta: { stop_reason: "end_turn" },
1565+
usage: { input_tokens: 1, output_tokens: 2 },
1566+
},
1567+
{ type: "message_stop" },
1568+
]);
1569+
1570+
const result = await streamAnthropic(
1571+
makeAnthropicModel(),
1572+
{ messages: [{ role: "user", content: "hello", timestamp: 0 }] },
1573+
{ apiKey: "sk-ant-provider", client: client as never },
1574+
).result();
1575+
1576+
expect(result.content).toEqual([
1577+
{ type: "text", text: "first" },
1578+
{ type: "text", text: "second" },
1579+
]);
1580+
});
1581+
14221582
it("rejects a malformed later tool before any sibling becomes executable", async () => {
14231583
const client = createAnthropicSseClient([
14241584
{
@@ -2267,6 +2427,54 @@ describe("Anthropic provider", () => {
22672427
// ...and NOT on the trailing volatile carrier, which is left uncached.
22682428
expect(messages[1]?.content).toBe("volatile current-turn metadata");
22692429
});
2430+
2431+
it("emits start event only after message_start so pre-stream SSE errors arrive before any non-error event", async () => {
2432+
function createSseEventResponse(lines: string): Response {
2433+
return new Response(lines, {
2434+
status: 200,
2435+
headers: { "content-type": "text/event-stream" },
2436+
});
2437+
}
2438+
2439+
const client = {
2440+
messages: {
2441+
create: vi.fn(() => ({
2442+
asResponse: () =>
2443+
Promise.resolve(
2444+
createSseEventResponse(
2445+
"event: message_start\ndata: " +
2446+
JSON.stringify({
2447+
type: "message_start",
2448+
message: { id: "msg_1", usage: { input_tokens: 1, output_tokens: 0 } },
2449+
}) +
2450+
"\n\nevent: message_stop\ndata: " +
2451+
JSON.stringify({ type: "message_stop" }) +
2452+
"\n\n",
2453+
),
2454+
),
2455+
})),
2456+
},
2457+
};
2458+
2459+
const stream = streamAnthropic(
2460+
makeAnthropicModel(),
2461+
{ messages: [{ role: "user", content: "hi", timestamp: 0 }] },
2462+
{ apiKey: "sk-ant-key", client: client as never },
2463+
);
2464+
2465+
const eventTypes: string[] = [];
2466+
for await (const event of stream as AsyncIterable<{ type: string }>) {
2467+
eventTypes.push(event.type);
2468+
}
2469+
2470+
// start must come after message_start processing, not before the loop
2471+
const startIndex = eventTypes.indexOf("start");
2472+
expect(startIndex).toBeGreaterThanOrEqual(0);
2473+
// No error before start — the start event should be first non-error event
2474+
const errorBeforeStart = eventTypes.slice(0, startIndex).some((t) => t === "error");
2475+
expect(errorBeforeStart).toBe(false);
2476+
});
2477+
22702478
it("emits error without a preceding start event when SSE error arrives before message_start", async () => {
22712479
function createSseEventResponse(lines: string): Response {
22722480
return new Response(lines, {

0 commit comments

Comments
 (0)