@@ -30,8 +30,10 @@ type CacheEntry = {
3030
3131export class GeminiHandler extends BaseProvider implements SingleCompletionHandler {
3232 protected options : ApiHandlerOptions
33+
3334 private client : GoogleGenAI
3435 private contentCaches : NodeCache
36+ private isCacheBusy = false
3537
3638 constructor ( options : ApiHandlerOptions ) {
3739 super ( )
@@ -65,8 +67,6 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
6567 cacheKey &&
6668 contentsLength > 4 * CONTEXT_CACHE_TOKEN_MINIMUM
6769
68- console . log ( `[GeminiHandler] isCacheAvailable=${ isCacheAvailable } , contentsLength=${ contentsLength } ` )
69-
7070 if ( isCacheAvailable ) {
7171 const cacheEntry = this . contentCaches . get < CacheEntry > ( cacheKey )
7272
@@ -78,32 +78,38 @@ export class GeminiHandler extends BaseProvider implements SingleCompletionHandl
7878 )
7979 }
8080
81- const timestamp = Date . now ( )
82-
83- const config : CreateCachedContentConfig = {
84- contents,
85- systemInstruction,
86- ttl : `${ CACHE_TTL * 60 } s` ,
87- httpOptions : { timeout : 10_000 } ,
81+ if ( ! this . isCacheBusy ) {
82+ this . isCacheBusy = true
83+ const timestamp = Date . now ( )
84+
85+ this . client . caches
86+ . create ( {
87+ model,
88+ config : {
89+ contents,
90+ systemInstruction,
91+ ttl : `${ CACHE_TTL * 60 } s` ,
92+ httpOptions : { timeout : 120_000 } ,
93+ } ,
94+ } )
95+ . then ( ( result ) => {
96+ const { name, usageMetadata } = result
97+
98+ if ( name ) {
99+ this . contentCaches . set < CacheEntry > ( cacheKey , { key : name , count : contents . length } )
100+ cacheWriteTokens = usageMetadata ?. totalTokenCount ?? 0
101+ console . log (
102+ `[GeminiHandler] cached ${ contents . length } messages (${ cacheWriteTokens } tokens) in ${ Date . now ( ) - timestamp } ms` ,
103+ )
104+ }
105+ } )
106+ . catch ( ( error ) => {
107+ console . error ( `[GeminiHandler] caches.create error` , error )
108+ } )
109+ . finally ( ( ) => {
110+ this . isCacheBusy = false
111+ } )
88112 }
89-
90- this . client . caches
91- . create ( { model, config } )
92- . then ( ( result ) => {
93- console . log ( `[GeminiHandler] caches.create result -> ${ JSON . stringify ( result ) } ` )
94- const { name, usageMetadata } = result
95-
96- if ( name ) {
97- this . contentCaches . set < CacheEntry > ( cacheKey , { key : name , count : contents . length } )
98- cacheWriteTokens = usageMetadata ?. totalTokenCount ?? 0
99- console . log (
100- `[GeminiHandler] cached ${ contents . length } messages (${ cacheWriteTokens } tokens) in ${ Date . now ( ) - timestamp } ms` ,
101- )
102- }
103- } )
104- . catch ( ( error ) => {
105- console . error ( `[GeminiHandler] caches.create error` , error )
106- } )
107113 }
108114
109115 const isCacheUsed = ! ! cachedContent
0 commit comments