implemented Claude caching breakpoints (OpenRouter version).
This commit is contained in:
@@ -21,20 +21,32 @@ export class ClaudeOpenRouterProvider extends LLMProvider {
|
|||||||
/**
|
/**
|
||||||
* Build OpenAI-compatible messages array where each message content is an array of parts.
|
* Build OpenAI-compatible messages array where each message content is an array of parts.
|
||||||
*/
|
*/
|
||||||
private buildRequestMessages(messages: Message[], systemPrompt?: string): Array<{ role: string; content: Array<{ type: 'text'; text: string }> }> {
|
private buildRequestMessages(messages: Message[], systemPrompt?: string): Array<{ role: string; content: Array<{ type: 'text'; text: string; cache_control?: { type: 'ephemeral' } }> }> {
|
||||||
const openAIMessages: Array<{ role: string; content: Array<{ type: 'text'; text: string }> }> = [];
|
type ORPart = { type: 'text'; text: string; cache_control?: { type: 'ephemeral' } };
|
||||||
|
const openAIMessages: Array<{ role: string; content: Array<ORPart> }> = [];
|
||||||
|
|
||||||
// Add system prompt if provided
|
// Add system prompt if provided
|
||||||
if (systemPrompt) {
|
if (systemPrompt) {
|
||||||
openAIMessages.push({ role: 'system', content: [{ type: 'text', text: systemPrompt }] });
|
openAIMessages.push({ role: 'system', content: [{ type: 'text', text: systemPrompt, cache_control: { type: 'ephemeral' } }] });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add conversation history with preserved roles
|
// Add conversation history with preserved roles
|
||||||
|
let lastUserIndex = -1;
|
||||||
|
for (let i = 0; i < messages.length; i++) {
|
||||||
|
if (messages[i].role === 'user') lastUserIndex = i;
|
||||||
|
}
|
||||||
|
|
||||||
openAIMessages.push(
|
openAIMessages.push(
|
||||||
...messages.map(msg => ({
|
...messages.map((msg, i) => {
|
||||||
role: msg.role,
|
const part: ORPart = { type: 'text', text: msg.content };
|
||||||
content: [{ type: 'text' as const, text: msg.content }]
|
if (msg.role === 'user' && i === lastUserIndex) {
|
||||||
}))
|
part.cache_control = { type: 'ephemeral' };
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
role: msg.role,
|
||||||
|
content: [part]
|
||||||
|
};
|
||||||
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
return openAIMessages;
|
return openAIMessages;
|
||||||
@@ -43,7 +55,7 @@ export class ClaudeOpenRouterProvider extends LLMProvider {
|
|||||||
/**
|
/**
|
||||||
* Create API request with proper headers and body
|
* Create API request with proper headers and body
|
||||||
*/
|
*/
|
||||||
private async createApiRequest(messages: Array<{ role: string; content: Array<{ type: 'text'; text: string }> }>, config: ReturnType<typeof this.getCurrentConfig>, streaming: boolean): Promise<Response> {
|
private async createApiRequest(messages: Array<{ role: string; content: Array<{ type: 'text'; text: string; cache_control?: { type: 'ephemeral' } }> }>, config: ReturnType<typeof this.getCurrentConfig>, streaming: boolean): Promise<Response> {
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
'Authorization': `Bearer ${config.apiKey}`,
|
'Authorization': `Bearer ${config.apiKey}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|||||||
Reference in New Issue
Block a user