Skip to content

Commit a77b107

Browse files
committed
Don't make assumptions about the number of cached messages
1 parent 2359aa4 commit a77b107

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/api/providers/gemini.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { BaseProvider } from "./base-provider"
1717
export 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

Comments
 (0)