|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createAnthropicAdapter } from "@intx/inference/providers"; |
| 3 | +import type { |
| 4 | + ConversationTurn, |
| 5 | + InferenceOptions, |
| 6 | +} from "@intx/types/runtime"; |
| 7 | + |
| 8 | +const source = { |
| 9 | + sourceId: "test-anthropic", |
| 10 | + provider: "anthropic", |
| 11 | + model: "test-anthropic-model", |
| 12 | +}; |
| 13 | + |
| 14 | +function userTurn(text: string): ConversationTurn { |
| 15 | + return { |
| 16 | + role: "user", |
| 17 | + timestamp: 0, |
| 18 | + content: [{ type: "text", text }], |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +function assistantTurn(text: string): ConversationTurn { |
| 23 | + return { |
| 24 | + role: "assistant", |
| 25 | + timestamp: 0, |
| 26 | + content: [{ type: "text", text }], |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +type WireMessage = { |
| 31 | + role: string; |
| 32 | + content: { cache_control?: unknown; text?: string }[]; |
| 33 | +}; |
| 34 | + |
| 35 | +function wireMessages(body: string): WireMessage[] { |
| 36 | + return (JSON.parse(body) as { messages: WireMessage[] }).messages; |
| 37 | +} |
| 38 | + |
| 39 | +describe("anthropic cache breakpoint with ephemeral turns", () => { |
| 40 | + test("breakpoint lands on the last persisted user turn, not the ephemeral tail", () => { |
| 41 | + const persisted = [userTurn("q1"), assistantTurn("a1"), userTurn("q2")]; |
| 42 | + const nudge = userTurn("wrap up soon"); |
| 43 | + const request = createAnthropicAdapter(source).buildRequest( |
| 44 | + [...persisted, nudge], |
| 45 | + "test-anthropic-model", |
| 46 | + { ephemeralTurns: [nudge] } as InferenceOptions, |
| 47 | + ); |
| 48 | + const messages = wireMessages(request.body); |
| 49 | + |
| 50 | + expect(messages).toHaveLength(4); |
| 51 | + expect(messages[3]?.content[0]?.text).toBe("wrap up soon"); |
| 52 | + expect( |
| 53 | + messages[2]?.content[messages[2].content.length - 1]?.cache_control, |
| 54 | + ).toEqual({ type: "ephemeral" }); |
| 55 | + expect( |
| 56 | + messages[3]?.content.filter((block) => block.cache_control !== undefined), |
| 57 | + ).toEqual([]); |
| 58 | + }); |
| 59 | +}); |
0 commit comments