(sessionId: string, text: string, signature: string)
| 100 | * Used for Claude models that require signed thinking blocks in multi-turn conversations. |
| 101 | */ |
| 102 | export function cacheSignature(sessionId: string, text: string, signature: string): void { |
| 103 | if (!sessionId || !text || !signature) return; |
| 104 | |
| 105 | let sessionCache = signatureCache.get(sessionId); |
| 106 | if (!sessionCache) { |
| 107 | sessionCache = new Map(); |
| 108 | signatureCache.set(sessionId, sessionCache); |
| 109 | } |
| 110 | |
| 111 | // Evict old entries if we're at capacity |
| 112 | if (sessionCache.size >= MAX_ENTRIES_PER_SESSION) { |
| 113 | const now = Date.now(); |
| 114 | for (const [key, entry] of sessionCache.entries()) { |
| 115 | if (now - entry.timestamp > SIGNATURE_CACHE_TTL_MS) { |
| 116 | sessionCache.delete(key); |
| 117 | } |
| 118 | } |
| 119 | // If still at capacity, remove oldest entries |
| 120 | if (sessionCache.size >= MAX_ENTRIES_PER_SESSION) { |
| 121 | const entries = Array.from(sessionCache.entries()).sort((a, b) => a[1].timestamp - b[1].timestamp); |
| 122 | const toRemove = entries.slice(0, Math.floor(MAX_ENTRIES_PER_SESSION / 4)); |
| 123 | for (const [key] of toRemove) { |
| 124 | sessionCache.delete(key); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | const textHash = hashText(text); |
| 130 | sessionCache.set(textHash, { signature, timestamp: Date.now() }); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Retrieves a cached signature for a given session and text. |
no test coverage detected