summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/global-sync/session-cache.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/app/src/context/global-sync/session-cache.ts')
-rw-r--r--packages/app/src/context/global-sync/session-cache.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/packages/app/src/context/global-sync/session-cache.ts b/packages/app/src/context/global-sync/session-cache.ts
new file mode 100644
index 000000000..0177ebbe1
--- /dev/null
+++ b/packages/app/src/context/global-sync/session-cache.ts
@@ -0,0 +1,62 @@
+import type {
+ FileDiff,
+ Message,
+ Part,
+ PermissionRequest,
+ QuestionRequest,
+ SessionStatus,
+ Todo,
+} from "@opencode-ai/sdk/v2/client"
+
+export const SESSION_CACHE_LIMIT = 40
+
+type SessionCache = {
+ session_status: Record<string, SessionStatus | undefined>
+ session_diff: Record<string, FileDiff[] | undefined>
+ todo: Record<string, Todo[] | undefined>
+ message: Record<string, Message[] | undefined>
+ part: Record<string, Part[] | undefined>
+ permission: Record<string, PermissionRequest[] | undefined>
+ question: Record<string, QuestionRequest[] | undefined>
+}
+
+export function dropSessionCaches(store: SessionCache, sessionIDs: Iterable<string>) {
+ const stale = new Set(Array.from(sessionIDs).filter(Boolean))
+ if (stale.size === 0) return
+
+ for (const key of Object.keys(store.part)) {
+ const parts = store.part[key]
+ if (!parts?.some((part) => stale.has(part?.sessionID ?? ""))) continue
+ delete store.part[key]
+ }
+
+ for (const sessionID of stale) {
+ delete store.message[sessionID]
+ delete store.todo[sessionID]
+ delete store.session_diff[sessionID]
+ delete store.session_status[sessionID]
+ delete store.permission[sessionID]
+ delete store.question[sessionID]
+ }
+}
+
+export function pickSessionCacheEvictions(input: {
+ seen: Set<string>
+ keep: string
+ limit: number
+ preserve?: Iterable<string>
+}) {
+ const stale: string[] = []
+ const keep = new Set([input.keep, ...Array.from(input.preserve ?? [])])
+ if (input.seen.has(input.keep)) input.seen.delete(input.keep)
+ input.seen.add(input.keep)
+ for (const id of input.seen) {
+ if (input.seen.size - stale.length <= input.limit) break
+ if (keep.has(id)) continue
+ stale.push(id)
+ }
+ for (const id of stale) {
+ input.seen.delete(id)
+ }
+ return stale
+}