@@ -17,7 +17,7 @@ import { BaseProvider } from "./base-provider"
1717export class GeminiHandler extends BaseProvider implements SingleCompletionHandler {
1818 protected options : ApiHandlerOptions
1919 private client : GoogleGenAI
20- private contentCaches : Map < string , string >
20+ private contentCaches : Map < string , { key : string ; count : number } >
2121
2222 constructor ( options : ApiHandlerOptions ) {
2323 super ( )
@@ -34,26 +34,27 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
3434 const { id : model , thinkingConfig, maxOutputTokens, supportsPromptCache } = this . getModel ( )
3535
3636 const contents = messages . map ( convertAnthropicMessageToGemini )
37- let uncachedContent : Content | undefined = undefined
37+ let uncachedContent : Content [ ] | undefined = undefined
3838 let cachedContent : string | undefined = undefined
3939 let cacheWriteTokens : number = 0
4040
4141 // https://ai.google.dev/gemini-api/docs/caching?lang=node
4242 if ( supportsPromptCache && taskId ) {
43- cachedContent = this . contentCaches . get ( taskId )
43+ const cacheEntry = this . contentCaches . get ( taskId )
4444
45- if ( cachedContent ) {
46- uncachedContent = convertAnthropicMessageToGemini ( messages [ messages . length - 1 ] )
45+ if ( cacheEntry ) {
46+ uncachedContent = contents . slice ( cacheEntry . count , contents . length )
47+ cachedContent = cacheEntry . key
4748 }
4849
49- const updatedCachedContent = await this . client . caches . create ( {
50+ const newCacheEntry = await this . client . caches . create ( {
5051 model,
5152 config : { contents, systemInstruction, ttl : "300s" } ,
5253 } )
5354
54- if ( updatedCachedContent . name ) {
55- this . contentCaches . set ( taskId , updatedCachedContent . name )
56- cacheWriteTokens = updatedCachedContent . usageMetadata ?. totalTokenCount ?? 0
55+ if ( newCacheEntry . name ) {
56+ this . contentCaches . set ( taskId , { key : newCacheEntry . name , count : contents . length } )
57+ cacheWriteTokens = newCacheEntry . usageMetadata ?. totalTokenCount ?? 0
5758 }
5859 }
5960
0 commit comments