-
Notifications
You must be signed in to change notification settings - Fork 37
don't add cache_control if already present #1833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0059cf2
fix(openrouter): preserve existing cache_control
kilo-code-bot[bot] bc7ee34
Only check messages
chrarnoldus 784941f
Formatting
chrarnoldus d355f13
fix(openrouter): add messages API support to addCacheBreakpoints
kilo-code-bot[bot] e4a2ced
Retain request level cache_control
chrarnoldus 5a266f1
Fix type errors
chrarnoldus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import { describe, expect, test } from '@jest/globals'; | ||
| import { addCacheBreakpoints } from '@/lib/providers/openrouter/request-helpers'; | ||
| import type { GatewayRequest } from '@/lib/providers/openrouter/types'; | ||
| import type OpenAI from 'openai'; | ||
|
|
||
| describe('addCacheBreakpoints', () => { | ||
| test('adds a cache breakpoint to the last eligible chat completions message when none exist', () => { | ||
| const request: GatewayRequest = { | ||
| kind: 'chat_completions', | ||
| body: { | ||
| model: 'test-model', | ||
| messages: [ | ||
| { role: 'system', content: 'You are helpful.' }, | ||
| { role: 'user', content: 'First prompt' }, | ||
| { role: 'assistant', content: 'First response' }, | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { type: 'text', text: 'Latest prompt' }, | ||
| { type: 'text', text: 'Latest detail' }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| addCacheBreakpoints(request); | ||
|
|
||
| const lastContent = request.body.messages.at(-1)?.content; | ||
| expect(Array.isArray(lastContent)).toBe(true); | ||
| if (!Array.isArray(lastContent)) return; | ||
| expect(lastContent.at(-1)).toMatchObject({ | ||
| type: 'text', | ||
| text: 'Latest detail', | ||
| cache_control: { type: 'ephemeral' }, | ||
| }); | ||
| }); | ||
|
|
||
| test('does nothing for chat completions requests when any cache_control is already present', () => { | ||
| const request: GatewayRequest = { | ||
| kind: 'chat_completions', | ||
| body: { | ||
| model: 'test-model', | ||
| messages: [ | ||
| { role: 'system', content: 'You are helpful.' }, | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'First prompt', | ||
| cache_control: { type: 'ephemeral' }, | ||
| } as OpenAI.ChatCompletionContentPartText, | ||
| ], | ||
| }, | ||
| { role: 'assistant', content: 'First response' }, | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { type: 'text', text: 'Latest prompt' }, | ||
| { type: 'text', text: 'Latest detail' }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| addCacheBreakpoints(request); | ||
|
|
||
| const lastContent = | ||
| request.kind === 'chat_completions' && request.body.messages.at(-1)?.content; | ||
| expect(lastContent).toEqual([ | ||
| { type: 'text', text: 'Latest prompt' }, | ||
| { type: 'text', text: 'Latest detail' }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('does nothing for responses requests when any cache_control is already present', () => { | ||
| const request: GatewayRequest = { | ||
| kind: 'responses', | ||
| body: { | ||
| model: 'test-model', | ||
| input: [ | ||
| { | ||
| type: 'message', | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'input_text', | ||
| text: 'First prompt', | ||
| // @ts-expect-error non-standard cache_control extension | ||
| cache_control: { type: 'ephemeral' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| type: 'function_call_output', | ||
| call_id: 'call_123', | ||
| output: [ | ||
| { type: 'input_text', text: 'Tool output' }, | ||
| { type: 'input_text', text: 'Tool detail' }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| addCacheBreakpoints(request); | ||
|
|
||
| const lastItem = request.kind === 'responses' && request.body.input?.at(-1); | ||
| expect(lastItem).toMatchObject({ | ||
| type: 'function_call_output', | ||
| output: [ | ||
| { type: 'input_text', text: 'Tool output' }, | ||
| { type: 'input_text', text: 'Tool detail' }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| test('adds top-level cache_control on messages request when none is present', () => { | ||
| const request: GatewayRequest = { | ||
| kind: 'messages', | ||
| body: { | ||
| model: 'anthropic/claude-sonnet-4-5', | ||
| max_tokens: 1024, | ||
| messages: [ | ||
| { role: 'user', content: 'First prompt' }, | ||
| { role: 'assistant', content: 'First response' }, | ||
| { role: 'user', content: 'Latest prompt' }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| addCacheBreakpoints(request); | ||
|
|
||
| expect(request.body.cache_control).toEqual({ type: 'ephemeral' }); | ||
| }); | ||
|
|
||
| test('does nothing for messages request when any cache_control is already present', () => { | ||
| const request: GatewayRequest = { | ||
| kind: 'messages', | ||
| body: { | ||
| model: 'anthropic/claude-sonnet-4-5', | ||
| max_tokens: 1024, | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'First prompt', | ||
| cache_control: { type: 'ephemeral' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { role: 'assistant', content: 'First response' }, | ||
| { role: 'user', content: 'Latest prompt' }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| addCacheBreakpoints(request); | ||
|
|
||
| expect(request.body.cache_control).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.