|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import type { TextBlockParam, Tool } from '@anthropic-ai/sdk/resources' |
| 5 | +import { describe, expect, it } from 'vitest' |
| 6 | +import { applyAnthropicPromptCache } from '@/providers/anthropic/utils' |
| 7 | + |
| 8 | +const LARGE = 'x'.repeat(8_000) // ~2,000 est. tokens, above the 1,024 gate |
| 9 | +const SMALL = 'x'.repeat(400) // ~100 est. tokens, below the gate |
| 10 | + |
| 11 | +const tool = (name: string): Tool => ({ |
| 12 | + name, |
| 13 | + description: 'does a thing', |
| 14 | + input_schema: { type: 'object', properties: {} }, |
| 15 | +}) |
| 16 | + |
| 17 | +describe('applyAnthropicPromptCache', () => { |
| 18 | + it('converts a large system prompt to a cached text block and tags the last tool', () => { |
| 19 | + const payload: { system?: string | TextBlockParam[] } = { system: LARGE } |
| 20 | + const tools = [tool('a'), tool('b')] |
| 21 | + |
| 22 | + applyAnthropicPromptCache(payload, tools, LARGE) |
| 23 | + |
| 24 | + expect(Array.isArray(payload.system)).toBe(true) |
| 25 | + const blocks = payload.system as TextBlockParam[] |
| 26 | + expect(blocks).toHaveLength(1) |
| 27 | + expect(blocks[0]).toMatchObject({ |
| 28 | + type: 'text', |
| 29 | + text: LARGE, |
| 30 | + cache_control: { type: 'ephemeral' }, |
| 31 | + }) |
| 32 | + // Only the LAST tool carries the breakpoint; earlier tools are untouched. |
| 33 | + expect(tools[0].cache_control).toBeUndefined() |
| 34 | + expect(tools[1].cache_control).toEqual({ type: 'ephemeral' }) |
| 35 | + }) |
| 36 | + |
| 37 | + it('tags the system block when the system alone is large and there are no tools', () => { |
| 38 | + const payload: { system?: string | TextBlockParam[] } = { system: LARGE } |
| 39 | + |
| 40 | + applyAnthropicPromptCache(payload, undefined, LARGE) |
| 41 | + |
| 42 | + const blocks = payload.system as TextBlockParam[] |
| 43 | + expect(blocks[0].cache_control).toEqual({ type: 'ephemeral' }) |
| 44 | + }) |
| 45 | + |
| 46 | + it('tags the tools even when payload.system was relocated/blanked (gate uses the request prompt)', () => { |
| 47 | + // No-messages path: the provider moves the system text into a user message |
| 48 | + // and blanks payload.system, but the original prompt is large, so the tools |
| 49 | + // prefix is still worth caching. |
| 50 | + const payload: { system?: string | TextBlockParam[] } = { system: '' } |
| 51 | + const tools = [tool('a')] |
| 52 | + |
| 53 | + applyAnthropicPromptCache(payload, tools, LARGE) |
| 54 | + |
| 55 | + expect(payload.system).toBe('') // empty system is never converted |
| 56 | + expect(tools[0].cache_control).toEqual({ type: 'ephemeral' }) |
| 57 | + }) |
| 58 | + |
| 59 | + it('leaves a small, tool-less prefix untouched (no write surcharge on one-shot calls)', () => { |
| 60 | + const payload: { system?: string | TextBlockParam[] } = { system: SMALL } |
| 61 | + |
| 62 | + applyAnthropicPromptCache(payload, undefined, SMALL) |
| 63 | + |
| 64 | + expect(payload.system).toBe(SMALL) |
| 65 | + }) |
| 66 | + |
| 67 | + it('does nothing when the combined prefix is below the threshold', () => { |
| 68 | + const payload: { system?: string | TextBlockParam[] } = { system: SMALL } |
| 69 | + const tools = [tool('a')] |
| 70 | + |
| 71 | + applyAnthropicPromptCache(payload, tools, SMALL) |
| 72 | + |
| 73 | + expect(payload.system).toBe(SMALL) |
| 74 | + expect(tools[0].cache_control).toBeUndefined() |
| 75 | + }) |
| 76 | +}) |
0 commit comments