| |
| import { patchSessionEntryCore } from "../../config/sessions/session-accessor.js"; |
| import { mergeSessionSnapshotChanges } from "../../config/sessions/session-snapshot-merge.js"; |
| import type { SessionEntry } from "../../config/sessions/types.js"; |
| |
| type PersistSessionEntryParams = { |
| sessionStore: Record<string, SessionEntry>; |
| sessionKey: string; |
| storePath: string; |
| initialEntry: SessionEntry; |
| entry: SessionEntry; |
| shouldPersist?: (entry: SessionEntry | undefined) => boolean; |
| }; |
|
|
| |
| export async function persistAgentSession( |
| params: PersistSessionEntryParams, |
| ): Promise<SessionEntry | undefined> { |
| let rejectedMissingEntry = false; |
| const persisted = await patchSessionEntryCore( |
| { sessionKey: params.sessionKey, storePath: params.storePath }, |
| (_entry, context) => { |
| const shouldPersistCurrent = params.shouldPersist?.(context.existingEntry); |
| if (!context.existingEntry && shouldPersistCurrent !== true) { |
| rejectedMissingEntry = true; |
| return null; |
| } |
| if (shouldPersistCurrent === false) { |
| rejectedMissingEntry = !context.existingEntry; |
| return null; |
| } |
| if (!context.existingEntry) { |
| return params.entry; |
| } |
| if (context.existingEntry.sessionId !== params.initialEntry.sessionId) { |
| return null; |
| } |
| |
| |
| return mergeSessionSnapshotChanges({ |
| initial: params.initialEntry, |
| next: params.entry, |
| current: context.existingEntry, |
| }); |
| }, |
| { |
| fallbackEntry: params.sessionStore[params.sessionKey] ?? params.entry, |
| replaceEntry: true, |
| }, |
| ); |
| if (rejectedMissingEntry) { |
| delete params.sessionStore[params.sessionKey]; |
| return undefined; |
| } |
| if (persisted) { |
| params.sessionStore[params.sessionKey] = persisted; |
| } else { |
| delete params.sessionStore[params.sessionKey]; |
| } |
| return persisted ?? undefined; |
| } |
|
|