|
| 1 | +const DEFAULT_DUPLICATE_USER_MESSAGE_WINDOW_MS = 60_000; |
| 2 | +const MIN_DUPLICATE_USER_MESSAGE_CHARS = 24; |
| 3 | + |
| 4 | +type MessageLike = { |
| 5 | + role?: unknown; |
| 6 | + content?: unknown; |
| 7 | + timestamp?: unknown; |
| 8 | +}; |
| 9 | + |
| 10 | +type EntryLike = { |
| 11 | + id?: unknown; |
| 12 | + type?: unknown; |
| 13 | + message?: unknown; |
| 14 | +}; |
| 15 | + |
| 16 | +type DuplicateUserMessageOptions = { |
| 17 | + windowMs?: number; |
| 18 | +}; |
| 19 | + |
| 20 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 21 | + return Boolean(value) && typeof value === "object" && !Array.isArray(value); |
| 22 | +} |
| 23 | + |
| 24 | +function normalizeUserMessageContent(content: unknown): string | undefined { |
| 25 | + if (typeof content === "string") { |
| 26 | + return content.replace(/\s+/g, " ").trim(); |
| 27 | + } |
| 28 | + if (!Array.isArray(content)) { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + const textParts: string[] = []; |
| 32 | + for (const block of content) { |
| 33 | + if (!isRecord(block)) { |
| 34 | + return undefined; |
| 35 | + } |
| 36 | + if (block.type === "image") { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + if (block.type === "text" && typeof block.text === "string") { |
| 40 | + textParts.push(block.text); |
| 41 | + } |
| 42 | + } |
| 43 | + return textParts.join("\n").replace(/\s+/g, " ").trim(); |
| 44 | +} |
| 45 | + |
| 46 | +function duplicateSignature(message: unknown): { key: string; timestamp: number } | undefined { |
| 47 | + if (!isRecord(message) || message.role !== "user" || typeof message.timestamp !== "number") { |
| 48 | + return undefined; |
| 49 | + } |
| 50 | + const text = normalizeUserMessageContent(message.content); |
| 51 | + if (!text || text.length < MIN_DUPLICATE_USER_MESSAGE_CHARS) { |
| 52 | + return undefined; |
| 53 | + } |
| 54 | + return { |
| 55 | + key: text.normalize("NFC").toLowerCase(), |
| 56 | + timestamp: message.timestamp, |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +export function dedupeDuplicateUserMessagesForCompaction<T extends MessageLike>( |
| 61 | + messages: readonly T[], |
| 62 | + options: DuplicateUserMessageOptions = {}, |
| 63 | +): T[] { |
| 64 | + const windowMs = options.windowMs ?? DEFAULT_DUPLICATE_USER_MESSAGE_WINDOW_MS; |
| 65 | + const lastSeenAtByKey = new Map<string, number>(); |
| 66 | + let removed = 0; |
| 67 | + const result: T[] = []; |
| 68 | + for (const message of messages) { |
| 69 | + const signature = duplicateSignature(message); |
| 70 | + if (!signature) { |
| 71 | + result.push(message); |
| 72 | + continue; |
| 73 | + } |
| 74 | + const lastSeenAt = lastSeenAtByKey.get(signature.key); |
| 75 | + lastSeenAtByKey.set(signature.key, signature.timestamp); |
| 76 | + if (typeof lastSeenAt === "number" && signature.timestamp - lastSeenAt <= windowMs) { |
| 77 | + removed += 1; |
| 78 | + continue; |
| 79 | + } |
| 80 | + result.push(message); |
| 81 | + } |
| 82 | + return removed > 0 ? result : [...messages]; |
| 83 | +} |
| 84 | + |
| 85 | +export function collectDuplicateUserMessageEntryIdsForCompaction( |
| 86 | + entries: readonly EntryLike[], |
| 87 | + options: DuplicateUserMessageOptions = {}, |
| 88 | +): Set<string> { |
| 89 | + const windowMs = options.windowMs ?? DEFAULT_DUPLICATE_USER_MESSAGE_WINDOW_MS; |
| 90 | + const lastSeenAtByKey = new Map<string, number>(); |
| 91 | + const duplicateIds = new Set<string>(); |
| 92 | + for (const entry of entries) { |
| 93 | + if (entry.type !== "message" || typeof entry.id !== "string") { |
| 94 | + continue; |
| 95 | + } |
| 96 | + const signature = duplicateSignature( |
| 97 | + isRecord(entry.message) ? (entry.message as MessageLike) : undefined, |
| 98 | + ); |
| 99 | + if (!signature) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + const lastSeenAt = lastSeenAtByKey.get(signature.key); |
| 103 | + lastSeenAtByKey.set(signature.key, signature.timestamp); |
| 104 | + if (typeof lastSeenAt === "number" && signature.timestamp - lastSeenAt <= windowMs) { |
| 105 | + duplicateIds.add(entry.id); |
| 106 | + } |
| 107 | + } |
| 108 | + return duplicateIds; |
| 109 | +} |
0 commit comments