|
| 1 | +/** In-memory replayable ACP event ledger for ephemeral bridge sessions. */ |
| 2 | +import type { SessionUpdate } from "@agentclientprotocol/sdk"; |
| 3 | +import { |
| 4 | + cloneAcpLedgerValue, |
| 5 | + createAcpPromptUpdates, |
| 6 | + normalizeAcpLedgerOptions, |
| 7 | + type AcpEventLedger, |
| 8 | + type AcpEventLedgerEntry, |
| 9 | + type AcpEventLedgerReplay, |
| 10 | + type AcpLedgerOptions, |
| 11 | + type AcpLedgerSession, |
| 12 | + type AcpMutableLedgerState, |
| 13 | +} from "./event-ledger.types.js"; |
| 14 | + |
| 15 | +const LEDGER_VERSION = 1; |
| 16 | + |
| 17 | +type LedgerStore = { |
| 18 | + version: 1; |
| 19 | + sessions: Record<string, AcpLedgerSession>; |
| 20 | +}; |
| 21 | + |
| 22 | +type MemoryLedgerState = AcpMutableLedgerState & { |
| 23 | + store: LedgerStore; |
| 24 | +}; |
| 25 | + |
| 26 | +function createEmptyStore(): LedgerStore { |
| 27 | + return { |
| 28 | + version: LEDGER_VERSION, |
| 29 | + sessions: {}, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function serializeLedgerStore(store: LedgerStore): string { |
| 34 | + return JSON.stringify(store); |
| 35 | +} |
| 36 | + |
| 37 | +function getSerializedLedgerByteLength(store: LedgerStore): number { |
| 38 | + return Buffer.byteLength(serializeLedgerStore(store), "utf8"); |
| 39 | +} |
| 40 | + |
| 41 | +function getOrCreateSession( |
| 42 | + state: MemoryLedgerState, |
| 43 | + params: { |
| 44 | + sessionId: string; |
| 45 | + sessionKey: string; |
| 46 | + cwd: string; |
| 47 | + complete: boolean; |
| 48 | + reset?: boolean; |
| 49 | + }, |
| 50 | +): AcpLedgerSession { |
| 51 | + const now = state.now(); |
| 52 | + const existing = state.store.sessions[params.sessionId]; |
| 53 | + if (!params.reset && existing) { |
| 54 | + existing.sessionKey = params.sessionKey; |
| 55 | + if (params.cwd) { |
| 56 | + existing.cwd = params.cwd; |
| 57 | + } |
| 58 | + existing.complete = existing.complete || params.complete; |
| 59 | + existing.updatedAt = now; |
| 60 | + return existing; |
| 61 | + } |
| 62 | + const session: AcpLedgerSession = { |
| 63 | + sessionId: params.sessionId, |
| 64 | + sessionKey: params.sessionKey, |
| 65 | + cwd: params.cwd, |
| 66 | + complete: params.complete, |
| 67 | + createdAt: now, |
| 68 | + updatedAt: now, |
| 69 | + nextSeq: 1, |
| 70 | + events: [], |
| 71 | + }; |
| 72 | + state.store.sessions[params.sessionId] = session; |
| 73 | + return session; |
| 74 | +} |
| 75 | + |
| 76 | +function trimLedger(state: MemoryLedgerState): void { |
| 77 | + for (const session of Object.values(state.store.sessions)) { |
| 78 | + if (session.events.length <= state.maxEventsPerSession) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + session.events = session.events.slice(-state.maxEventsPerSession); |
| 82 | + session.complete = false; |
| 83 | + } |
| 84 | + |
| 85 | + const sessions = Object.values(state.store.sessions); |
| 86 | + if (sessions.length > state.maxSessions) { |
| 87 | + for (const session of sessions |
| 88 | + .toSorted((a, b) => b.updatedAt - a.updatedAt) |
| 89 | + .slice(state.maxSessions)) { |
| 90 | + delete state.store.sessions[session.sessionId]; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + let serializedBytes = getSerializedLedgerByteLength(state.store); |
| 95 | + while (serializedBytes > state.maxSerializedBytes) { |
| 96 | + const session = Object.values(state.store.sessions) |
| 97 | + .filter((candidate) => candidate.events.length > 0) |
| 98 | + .toSorted((a, b) => a.updatedAt - b.updatedAt)[0]; |
| 99 | + if (!session) { |
| 100 | + break; |
| 101 | + } |
| 102 | + session.events.shift(); |
| 103 | + session.complete = false; |
| 104 | + serializedBytes = getSerializedLedgerByteLength(state.store); |
| 105 | + } |
| 106 | + |
| 107 | + while (serializedBytes > state.maxSerializedBytes) { |
| 108 | + const session = Object.values(state.store.sessions).toSorted( |
| 109 | + (a, b) => a.updatedAt - b.updatedAt, |
| 110 | + )[0]; |
| 111 | + if (!session) { |
| 112 | + break; |
| 113 | + } |
| 114 | + delete state.store.sessions[session.sessionId]; |
| 115 | + serializedBytes = getSerializedLedgerByteLength(state.store); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function appendUpdate( |
| 120 | + state: MemoryLedgerState, |
| 121 | + params: { |
| 122 | + sessionId: string; |
| 123 | + sessionKey: string; |
| 124 | + runId?: string; |
| 125 | + update: SessionUpdate; |
| 126 | + }, |
| 127 | +): void { |
| 128 | + const session = getOrCreateSession(state, { |
| 129 | + sessionId: params.sessionId, |
| 130 | + sessionKey: params.sessionKey, |
| 131 | + cwd: "", |
| 132 | + complete: false, |
| 133 | + }); |
| 134 | + const now = state.now(); |
| 135 | + session.updatedAt = now; |
| 136 | + session.events.push({ |
| 137 | + seq: session.nextSeq, |
| 138 | + at: now, |
| 139 | + sessionId: params.sessionId, |
| 140 | + sessionKey: params.sessionKey, |
| 141 | + ...(params.runId ? { runId: params.runId } : {}), |
| 142 | + update: cloneAcpLedgerValue(params.update), |
| 143 | + }); |
| 144 | + session.nextSeq += 1; |
| 145 | + trimLedger(state); |
| 146 | +} |
| 147 | + |
| 148 | +function createLedgerApi(params: { |
| 149 | + state: MemoryLedgerState; |
| 150 | + mutate: (fn: () => void) => Promise<void>; |
| 151 | + read: <T>(fn: () => T) => Promise<T>; |
| 152 | +}): AcpEventLedger { |
| 153 | + const buildReplay = (session: AcpLedgerSession): AcpEventLedgerReplay => ({ |
| 154 | + complete: true, |
| 155 | + sessionId: session.sessionId, |
| 156 | + sessionKey: session.sessionKey, |
| 157 | + events: session.events.map((event: AcpEventLedgerEntry) => cloneAcpLedgerValue(event)), |
| 158 | + }); |
| 159 | + |
| 160 | + return { |
| 161 | + async startSession(sessionParams) { |
| 162 | + await params.mutate(() => { |
| 163 | + getOrCreateSession(params.state, sessionParams); |
| 164 | + trimLedger(params.state); |
| 165 | + }); |
| 166 | + }, |
| 167 | + |
| 168 | + async recordUserPrompt(promptParams) { |
| 169 | + await params.mutate(() => { |
| 170 | + for (const update of createAcpPromptUpdates(promptParams.prompt)) { |
| 171 | + appendUpdate(params.state, { |
| 172 | + sessionId: promptParams.sessionId, |
| 173 | + sessionKey: promptParams.sessionKey, |
| 174 | + runId: promptParams.runId, |
| 175 | + update, |
| 176 | + }); |
| 177 | + } |
| 178 | + }); |
| 179 | + }, |
| 180 | + |
| 181 | + async recordUpdate(updateParams) { |
| 182 | + await params.mutate(() => { |
| 183 | + appendUpdate(params.state, updateParams); |
| 184 | + }); |
| 185 | + }, |
| 186 | + |
| 187 | + async markIncomplete(markParams) { |
| 188 | + await params.mutate(() => { |
| 189 | + const session = params.state.store.sessions[markParams.sessionId]; |
| 190 | + if (!session || session.sessionKey !== markParams.sessionKey) { |
| 191 | + return; |
| 192 | + } |
| 193 | + session.complete = false; |
| 194 | + session.updatedAt = params.state.now(); |
| 195 | + }); |
| 196 | + }, |
| 197 | + |
| 198 | + async readReplay(replayParams) { |
| 199 | + return params.read(() => { |
| 200 | + const session = params.state.store.sessions[replayParams.sessionId]; |
| 201 | + if (!session || session.sessionKey !== replayParams.sessionKey || !session.complete) { |
| 202 | + return { complete: false, events: [] }; |
| 203 | + } |
| 204 | + return buildReplay(session); |
| 205 | + }); |
| 206 | + }, |
| 207 | + |
| 208 | + async readReplayBySessionId(replayParams) { |
| 209 | + return params.read(() => { |
| 210 | + const session = params.state.store.sessions[replayParams.sessionId]; |
| 211 | + if (!session || !session.complete) { |
| 212 | + return { complete: false, events: [] }; |
| 213 | + } |
| 214 | + return buildReplay(session); |
| 215 | + }); |
| 216 | + }, |
| 217 | + |
| 218 | + async readReplayBySessionKey(replayParams) { |
| 219 | + return params.read(() => { |
| 220 | + const session = Object.values(params.state.store.sessions) |
| 221 | + .filter( |
| 222 | + (candidate) => candidate.sessionKey === replayParams.sessionKey && candidate.complete, |
| 223 | + ) |
| 224 | + .toSorted((a, b) => b.updatedAt - a.updatedAt)[0]; |
| 225 | + if (!session) { |
| 226 | + return { complete: false, events: [] }; |
| 227 | + } |
| 228 | + return buildReplay(session); |
| 229 | + }); |
| 230 | + }, |
| 231 | + }; |
| 232 | +} |
| 233 | + |
| 234 | +/** Creates an in-memory ACP event ledger for tests and ephemeral runtimes. */ |
| 235 | +export function createInMemoryAcpEventLedger(options: AcpLedgerOptions = {}): AcpEventLedger { |
| 236 | + const normalized = normalizeAcpLedgerOptions(options); |
| 237 | + const state: MemoryLedgerState = { |
| 238 | + store: createEmptyStore(), |
| 239 | + ...normalized, |
| 240 | + }; |
| 241 | + return createLedgerApi({ |
| 242 | + state, |
| 243 | + mutate: async (fn) => { |
| 244 | + fn(); |
| 245 | + }, |
| 246 | + read: async (fn) => fn(), |
| 247 | + }); |
| 248 | +} |
0 commit comments