Signature handling for fallback

This commit is contained in:
Badri Narayanan S
2026-01-03 22:01:57 +05:30
parent df6625b531
commit ac9ec6b358
8 changed files with 618 additions and 24 deletions

View File

@@ -5,11 +5,15 @@
* Gemini models require thoughtSignature on tool calls, but Claude Code
* strips non-standard fields. This cache stores signatures by tool_use_id
* so they can be restored in subsequent requests.
*
* Also caches thinking block signatures with model family for cross-model
* compatibility checking.
*/
import { GEMINI_SIGNATURE_CACHE_TTL_MS } from '../constants.js';
import { GEMINI_SIGNATURE_CACHE_TTL_MS, MIN_SIGNATURE_LENGTH } from '../constants.js';
const signatureCache = new Map();
const thinkingSignatureCache = new Map();
/**
* Store a signature for a tool_use_id
@@ -54,6 +58,11 @@ export function cleanupCache() {
signatureCache.delete(key);
}
}
for (const [key, entry] of thinkingSignatureCache) {
if (now - entry.timestamp > GEMINI_SIGNATURE_CACHE_TTL_MS) {
thinkingSignatureCache.delete(key);
}
}
}
/**
@@ -63,3 +72,43 @@ export function cleanupCache() {
export function getCacheSize() {
return signatureCache.size;
}
/**
* Cache a thinking block signature with its model family
* @param {string} signature - The thinking signature to cache
* @param {string} modelFamily - The model family ('claude' or 'gemini')
*/
export function cacheThinkingSignature(signature, modelFamily) {
if (!signature || signature.length < MIN_SIGNATURE_LENGTH) return;
thinkingSignatureCache.set(signature, {
modelFamily,
timestamp: Date.now()
});
}
/**
* Get the cached model family for a thinking signature
* @param {string} signature - The signature to look up
* @returns {string|null} 'claude', 'gemini', or null if not found/expired
*/
export function getCachedSignatureFamily(signature) {
if (!signature) return null;
const entry = thinkingSignatureCache.get(signature);
if (!entry) return null;
// Check TTL
if (Date.now() - entry.timestamp > GEMINI_SIGNATURE_CACHE_TTL_MS) {
thinkingSignatureCache.delete(signature);
return null;
}
return entry.modelFamily;
}
/**
* Get the current thinking signature cache size (for debugging)
* @returns {number} Number of entries in the thinking signature cache
*/
export function getThinkingCacheSize() {
return thinkingSignatureCache.size;
}