fix: protect live session sync caches
This commit is contained in:
@@ -18,6 +18,40 @@ type SessionCache = {
|
||||
question: Record<string, QuestionRequest[] | undefined>
|
||||
}
|
||||
|
||||
export function getProtectedSessionCacheIds(store: SessionCache): Set<string> {
|
||||
const protectedIds = new Set<string>()
|
||||
|
||||
for (const [sessionID, status] of Object.entries(store.session_status ?? {})) {
|
||||
if (status && status.type !== "idle") {
|
||||
protectedIds.add(sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
for (const [sessionID, permissions] of Object.entries(store.permission ?? {})) {
|
||||
if ((permissions?.length ?? 0) > 0) {
|
||||
protectedIds.add(sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
for (const [sessionID, questions] of Object.entries(store.question ?? {})) {
|
||||
if ((questions?.length ?? 0) > 0) {
|
||||
protectedIds.add(sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
for (const [sessionID, messages] of Object.entries(store.message ?? {})) {
|
||||
const lastMessage = messages?.[messages.length - 1]
|
||||
if (
|
||||
lastMessage?.role === "assistant"
|
||||
&& typeof (lastMessage as { time?: { completed?: number } }).time?.completed !== "number"
|
||||
) {
|
||||
protectedIds.add(sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
return protectedIds
|
||||
}
|
||||
|
||||
export function dropSessionCaches(store: SessionCache, sessionIDs: Iterable<string>) {
|
||||
const stale = new Set(Array.from(sessionIDs).filter(Boolean))
|
||||
if (stale.size === 0) return
|
||||
|
||||
Reference in New Issue
Block a user