2026-03-31 18:47:00 +03:00
|
|
|
/**
|
2026-07-21 20:52:20 +03:00
|
|
|
* Runtime-scoped pagination metadata shared with the session message loader.
|
2026-03-31 18:47:00 +03:00
|
|
|
*/
|
|
|
|
|
|
2026-07-21 20:52:20 +03:00
|
|
|
import { getRuntimeKey } from "@/lib/runtime-switch"
|
2026-03-31 18:47:00 +03:00
|
|
|
|
|
|
|
|
type Meta = {
|
|
|
|
|
limit: number
|
|
|
|
|
cursor?: string
|
|
|
|
|
complete: boolean
|
|
|
|
|
at: number
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 20:52:20 +03:00
|
|
|
const MAX_PREFETCH_ENTRIES = 200
|
|
|
|
|
const compositeKey = (runtimeKey: string, directory: string, sessionID: string) =>
|
|
|
|
|
`${runtimeKey}\n${directory}\n${sessionID}`
|
2026-03-31 18:47:00 +03:00
|
|
|
|
|
|
|
|
const cache = new Map<string, Meta>()
|
|
|
|
|
|
2026-07-21 20:52:20 +03:00
|
|
|
export function getSessionPrefetch(directory: string, sessionID: string, runtimeKey = getRuntimeKey()): Meta | undefined {
|
|
|
|
|
const id = compositeKey(runtimeKey, directory, sessionID)
|
|
|
|
|
const value = cache.get(id)
|
|
|
|
|
if (value) {
|
|
|
|
|
cache.delete(id)
|
|
|
|
|
cache.set(id, value)
|
2026-05-21 15:45:15 +03:00
|
|
|
}
|
2026-07-21 20:52:20 +03:00
|
|
|
return value
|
2026-05-21 15:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 18:47:00 +03:00
|
|
|
export function setSessionPrefetch(input: {
|
|
|
|
|
directory: string
|
|
|
|
|
sessionID: string
|
|
|
|
|
limit: number
|
|
|
|
|
cursor?: string
|
|
|
|
|
complete: boolean
|
|
|
|
|
at?: number
|
2026-07-21 20:52:20 +03:00
|
|
|
runtimeKey?: string
|
2026-03-31 18:47:00 +03:00
|
|
|
}) {
|
2026-07-21 20:52:20 +03:00
|
|
|
const id = compositeKey(input.runtimeKey ?? getRuntimeKey(), input.directory, input.sessionID)
|
|
|
|
|
cache.delete(id)
|
2026-05-21 15:45:15 +03:00
|
|
|
cache.set(id, {
|
2026-03-31 18:47:00 +03:00
|
|
|
limit: input.limit,
|
|
|
|
|
cursor: input.cursor,
|
|
|
|
|
complete: input.complete,
|
|
|
|
|
at: input.at ?? Date.now(),
|
|
|
|
|
})
|
2026-07-21 20:52:20 +03:00
|
|
|
while (cache.size > MAX_PREFETCH_ENTRIES) {
|
|
|
|
|
const oldest = cache.keys().next().value
|
|
|
|
|
if (!oldest) break
|
|
|
|
|
cache.delete(oldest)
|
|
|
|
|
}
|
2026-03-31 18:47:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Invalidate cache for specific sessions (e.g. after eviction). */
|
2026-07-21 20:52:20 +03:00
|
|
|
export function clearSessionPrefetch(directory: string, sessionIDs: Iterable<string>, runtimeKey = getRuntimeKey()) {
|
2026-03-31 18:47:00 +03:00
|
|
|
for (const sessionID of sessionIDs) {
|
|
|
|
|
if (!sessionID) continue
|
2026-07-21 20:52:20 +03:00
|
|
|
const id = compositeKey(runtimeKey, directory, sessionID)
|
2026-03-31 18:47:00 +03:00
|
|
|
cache.delete(id)
|
2026-07-21 20:52:20 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearDirectorySessionPrefetch(directory: string, runtimeKey = getRuntimeKey()) {
|
|
|
|
|
const prefix = `${runtimeKey}\n${directory}\n`
|
|
|
|
|
for (const id of cache.keys()) {
|
|
|
|
|
if (id.startsWith(prefix)) cache.delete(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearRuntimeSessionPrefetch(runtimeKey: string) {
|
|
|
|
|
const prefix = `${runtimeKey}\n`
|
|
|
|
|
for (const id of cache.keys()) {
|
|
|
|
|
if (id.startsWith(prefix)) cache.delete(id)
|
2026-03-31 18:47:00 +03:00
|
|
|
}
|
|
|
|
|
}
|