| 73 | const TIMESTAMP_PATTERN = /^\[\w{3}\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}\s+\w+\]\s*/; |
| 74 | |
| 75 | function normalizeForCache(obj: Record<string, unknown>): Record<string, unknown> { |
| 76 | const result: Record<string, unknown> = {}; |
| 77 | |
| 78 | for (const [key, value] of Object.entries(obj)) { |
| 79 | // Skip fields that don't affect response content |
| 80 | if (["user", "request_id", "x-request-id"].includes(key)) { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | if (key === "messages" && Array.isArray(value)) { |
| 85 | // Strip timestamps from message content |
| 86 | result[key] = value.map((msg: unknown) => { |
| 87 | if (typeof msg === "object" && msg !== null) { |
| 88 | const m = msg as Record<string, unknown>; |
| 89 | if (typeof m.content === "string") { |
| 90 | return { ...m, content: m.content.replace(TIMESTAMP_PATTERN, "") }; |
| 91 | } |
| 92 | } |
| 93 | return msg; |
| 94 | }); |
| 95 | } else { |
| 96 | result[key] = value; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | export class ResponseCache { |
| 104 | private cache = new Map<string, CachedLLMResponse>(); |