Open History to see the last commit
1import type { CompactionInfo } from '@tanstack/ai-compaction'
2
3/**
4 * Process-local record of compaction events for the `/compaction` demo. The
5 * chat route writes here from `withCompaction`'s `onCompact` callback; the
6 * inspect route reads it. Same singleton or the reader sees nothing.
7 */
8export interface CompactionEvent extends CompactionInfo {
9 /** Wall-clock time the compaction fired. */
10 at: number
11}
12
13const eventsByThread = new Map<string, Array<CompactionEvent>>()
14
15export function recordCompaction(threadId: string, info: CompactionInfo): void {
16 const list = eventsByThread.get(threadId) ?? []
17 list.push({ ...info, at: Date.now() })
18 eventsByThread.set(threadId, list)
19}
20
21export function getCompactions(threadId: string): Array<CompactionEvent> {
22 return eventsByThread.get(threadId) ?? []
23}
24
25export function clearCompactions(threadId: string): void {
26 eventsByThread.delete(threadId)
27}