2026-03-31 18:47:00 +03:00
|
|
|
/**
|
|
|
|
|
* Session prefetch TTL cache — prevents redundant session fetches
|
|
|
|
|
* within a short window. Port of OpenCode's session-prefetch.ts.
|
|
|
|
|
*
|
|
|
|
|
* Tracks: last fetch time, pagination cursor, completeness.
|
|
|
|
|
* Version counter invalidates stale inflight requests after eviction.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const SESSION_PREFETCH_TTL = 15_000
|
|
|
|
|
|
|
|
|
|
type Meta = {
|
|
|
|
|
limit: number
|
|
|
|
|
cursor?: string
|
|
|
|
|
complete: boolean
|
|
|
|
|
at: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const compositeKey = (directory: string, sessionID: string) =>
|
|
|
|
|
`${directory}\n${sessionID}`
|
|
|
|
|
|
|
|
|
|
const cache = new Map<string, Meta>()
|
|
|
|
|
const inflight = new Map<string, Promise<Meta | undefined>>()
|
|
|
|
|
const rev = new Map<string, number>()
|
2026-05-21 15:45:15 +03:00
|
|
|
const listeners = new Map<string, Set<() => void>>()
|
2026-03-31 18:47:00 +03:00
|
|
|
|
|
|
|
|
const version = (id: string) => rev.get(id) ?? 0
|
|
|
|
|
|
2026-05-21 15:45:15 +03:00
|
|
|
const notify = (id: string) => {
|
|
|
|
|
const callbacks = listeners.get(id)
|
|
|
|
|
if (!callbacks) return
|
|
|
|
|
callbacks.forEach((callback) => callback())
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 18:47:00 +03:00
|
|
|
/** Check if a prefetch/sync can be skipped (recently fetched). */
|
|
|
|
|
export function shouldSkipSessionPrefetch(input: {
|
|
|
|
|
hasMessages: boolean
|
|
|
|
|
info?: Meta
|
|
|
|
|
pageSize: number
|
|
|
|
|
now?: number
|
|
|
|
|
}): boolean {
|
2026-06-02 00:43:05 +03:00
|
|
|
if (!input.hasMessages) {
|
|
|
|
|
return false
|
2026-03-31 18:47:00 +03:00
|
|
|
}
|
2026-06-02 00:43:05 +03:00
|
|
|
|
|
|
|
|
const info = input.info
|
|
|
|
|
if (!info) return true
|
|
|
|
|
if (info.complete) return true
|
|
|
|
|
if (info.limit > input.pageSize) return true
|
|
|
|
|
if (info.limit < input.pageSize) return false
|
|
|
|
|
return (input.now ?? Date.now()) - info.at < SESSION_PREFETCH_TTL
|
2026-03-31 18:47:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getSessionPrefetch(directory: string, sessionID: string): Meta | undefined {
|
|
|
|
|
return cache.get(compositeKey(directory, sessionID))
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 15:45:15 +03:00
|
|
|
export function subscribeSessionPrefetch(directory: string, sessionID: string, callback: () => void) {
|
|
|
|
|
if (!sessionID) return () => undefined
|
|
|
|
|
const id = compositeKey(directory, sessionID)
|
|
|
|
|
let callbacks = listeners.get(id)
|
|
|
|
|
if (!callbacks) {
|
|
|
|
|
callbacks = new Set()
|
|
|
|
|
listeners.set(id, callbacks)
|
|
|
|
|
}
|
|
|
|
|
callbacks.add(callback)
|
|
|
|
|
return () => {
|
|
|
|
|
callbacks?.delete(callback)
|
|
|
|
|
if (callbacks?.size === 0) listeners.delete(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-05-21 15:45:15 +03:00
|
|
|
const id = compositeKey(input.directory, input.sessionID)
|
|
|
|
|
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-05-21 15:45:15 +03:00
|
|
|
notify(id)
|
2026-03-31 18:47:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Invalidate cache for specific sessions (e.g. after eviction). */
|
|
|
|
|
export function clearSessionPrefetch(directory: string, sessionIDs: Iterable<string>) {
|
|
|
|
|
for (const sessionID of sessionIDs) {
|
|
|
|
|
if (!sessionID) continue
|
|
|
|
|
const id = compositeKey(directory, sessionID)
|
|
|
|
|
rev.set(id, version(id) + 1)
|
|
|
|
|
cache.delete(id)
|
|
|
|
|
inflight.delete(id)
|
2026-05-21 15:45:15 +03:00
|
|
|
notify(id)
|
2026-03-31 18:47:00 +03:00
|
|
|
}
|
|
|
|
|
}
|