|
| 1 | +import { describe, expect, test } from '@jest/globals'; |
| 2 | +import { addCacheBreakpoints } from '@/lib/providers/openrouter/request-helpers'; |
| 3 | +import type { GatewayRequest } from '@/lib/providers/openrouter/types'; |
| 4 | +import type OpenAI from 'openai'; |
| 5 | + |
| 6 | +describe('addCacheBreakpoints', () => { |
| 7 | + test('adds a cache breakpoint to the last eligible chat completions message when none exist', () => { |
| 8 | + const request: GatewayRequest = { |
| 9 | + kind: 'chat_completions', |
| 10 | + body: { |
| 11 | + model: 'test-model', |
| 12 | + messages: [ |
| 13 | + { role: 'system', content: 'You are helpful.' }, |
| 14 | + { role: 'user', content: 'First prompt' }, |
| 15 | + { role: 'assistant', content: 'First response' }, |
| 16 | + { |
| 17 | + role: 'user', |
| 18 | + content: [ |
| 19 | + { type: 'text', text: 'Latest prompt' }, |
| 20 | + { type: 'text', text: 'Latest detail' }, |
| 21 | + ], |
| 22 | + }, |
| 23 | + ], |
| 24 | + }, |
| 25 | + }; |
| 26 | + |
| 27 | + addCacheBreakpoints(request); |
| 28 | + |
| 29 | + const lastContent = request.body.messages.at(-1)?.content; |
| 30 | + expect(Array.isArray(lastContent)).toBe(true); |
| 31 | + if (!Array.isArray(lastContent)) return; |
| 32 | + expect(lastContent.at(-1)).toMatchObject({ |
| 33 | + type: 'text', |
| 34 | + text: 'Latest detail', |
| 35 | + cache_control: { type: 'ephemeral' }, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + test('does nothing for chat completions requests when any cache_control is already present', () => { |
| 40 | + const request: GatewayRequest = { |
| 41 | + kind: 'chat_completions', |
| 42 | + body: { |
| 43 | + model: 'test-model', |
| 44 | + messages: [ |
| 45 | + { role: 'system', content: 'You are helpful.' }, |
| 46 | + { |
| 47 | + role: 'user', |
| 48 | + content: [ |
| 49 | + { |
| 50 | + type: 'text', |
| 51 | + text: 'First prompt', |
| 52 | + cache_control: { type: 'ephemeral' }, |
| 53 | + } as OpenAI.ChatCompletionContentPartText, |
| 54 | + ], |
| 55 | + }, |
| 56 | + { role: 'assistant', content: 'First response' }, |
| 57 | + { |
| 58 | + role: 'user', |
| 59 | + content: [ |
| 60 | + { type: 'text', text: 'Latest prompt' }, |
| 61 | + { type: 'text', text: 'Latest detail' }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + }; |
| 67 | + |
| 68 | + addCacheBreakpoints(request); |
| 69 | + |
| 70 | + const lastContent = |
| 71 | + request.kind === 'chat_completions' && request.body.messages.at(-1)?.content; |
| 72 | + expect(lastContent).toEqual([ |
| 73 | + { type: 'text', text: 'Latest prompt' }, |
| 74 | + { type: 'text', text: 'Latest detail' }, |
| 75 | + ]); |
| 76 | + }); |
| 77 | + |
| 78 | + test('does nothing for responses requests when any cache_control is already present', () => { |
| 79 | + const request: GatewayRequest = { |
| 80 | + kind: 'responses', |
| 81 | + body: { |
| 82 | + model: 'test-model', |
| 83 | + input: [ |
| 84 | + { |
| 85 | + type: 'message', |
| 86 | + role: 'user', |
| 87 | + content: [ |
| 88 | + { |
| 89 | + type: 'input_text', |
| 90 | + text: 'First prompt', |
| 91 | + // @ts-expect-error non-standard cache_control extension |
| 92 | + cache_control: { type: 'ephemeral' }, |
| 93 | + }, |
| 94 | + ], |
| 95 | + }, |
| 96 | + { |
| 97 | + type: 'function_call_output', |
| 98 | + call_id: 'call_123', |
| 99 | + output: [ |
| 100 | + { type: 'input_text', text: 'Tool output' }, |
| 101 | + { type: 'input_text', text: 'Tool detail' }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + ], |
| 105 | + }, |
| 106 | + }; |
| 107 | + |
| 108 | + addCacheBreakpoints(request); |
| 109 | + |
| 110 | + const lastItem = request.kind === 'responses' && request.body.input?.at(-1); |
| 111 | + expect(lastItem).toMatchObject({ |
| 112 | + type: 'function_call_output', |
| 113 | + output: [ |
| 114 | + { type: 'input_text', text: 'Tool output' }, |
| 115 | + { type: 'input_text', text: 'Tool detail' }, |
| 116 | + ], |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + test('adds top-level cache_control on messages request when none is present', () => { |
| 121 | + const request: GatewayRequest = { |
| 122 | + kind: 'messages', |
| 123 | + body: { |
| 124 | + model: 'anthropic/claude-sonnet-4-5', |
| 125 | + max_tokens: 1024, |
| 126 | + messages: [ |
| 127 | + { role: 'user', content: 'First prompt' }, |
| 128 | + { role: 'assistant', content: 'First response' }, |
| 129 | + { role: 'user', content: 'Latest prompt' }, |
| 130 | + ], |
| 131 | + }, |
| 132 | + }; |
| 133 | + |
| 134 | + addCacheBreakpoints(request); |
| 135 | + |
| 136 | + expect(request.body.cache_control).toEqual({ type: 'ephemeral' }); |
| 137 | + }); |
| 138 | + |
| 139 | + test('does nothing for messages request when any cache_control is already present', () => { |
| 140 | + const request: GatewayRequest = { |
| 141 | + kind: 'messages', |
| 142 | + body: { |
| 143 | + model: 'anthropic/claude-sonnet-4-5', |
| 144 | + max_tokens: 1024, |
| 145 | + messages: [ |
| 146 | + { |
| 147 | + role: 'user', |
| 148 | + content: [ |
| 149 | + { |
| 150 | + type: 'text', |
| 151 | + text: 'First prompt', |
| 152 | + cache_control: { type: 'ephemeral' }, |
| 153 | + }, |
| 154 | + ], |
| 155 | + }, |
| 156 | + { role: 'assistant', content: 'First response' }, |
| 157 | + { role: 'user', content: 'Latest prompt' }, |
| 158 | + ], |
| 159 | + }, |
| 160 | + }; |
| 161 | + |
| 162 | + addCacheBreakpoints(request); |
| 163 | + |
| 164 | + expect(request.body.cache_control).toBeUndefined(); |
| 165 | + }); |
| 166 | +}); |
0 commit comments