run-repository.ts
1 import type { RunEventRecord, SessionRunRecord } from '@/types' 2 3 import { 4 deleteStoredItem, 5 loadRuntimeRun as loadStoredRuntimeRun, 6 loadRuntimeRunEvents as loadStoredRuntimeRunEvents, 7 loadRuntimeRunEventsByRunId as loadStoredRuntimeRunEventsByRunId, 8 loadRuntimeRuns as loadStoredRuntimeRuns, 9 patchRuntimeRun as patchStoredRuntimeRun, 10 saveRuntimeRunEvents as saveStoredRuntimeRunEvents, 11 saveRuntimeRuns as saveStoredRuntimeRuns, 12 upsertRuntimeRun as upsertStoredRuntimeRun, 13 upsertRuntimeRunEvent as upsertStoredRuntimeRunEvent, 14 } from '@/lib/server/storage' 15 import { createRecordRepository } from '@/lib/server/persistence/repository-utils' 16 17 export const runRepository = createRecordRepository<SessionRunRecord>( 18 'runtime-runs', 19 { 20 get(id) { 21 return loadStoredRuntimeRun(id) as SessionRunRecord | null 22 }, 23 list() { 24 return loadStoredRuntimeRuns() as Record<string, SessionRunRecord> 25 }, 26 upsert(id, value) { 27 upsertStoredRuntimeRun(id, value as SessionRunRecord) 28 }, 29 replace(data) { 30 saveStoredRuntimeRuns(data as Record<string, SessionRunRecord>) 31 }, 32 patch(id, updater) { 33 return patchStoredRuntimeRun(id, updater as (current: SessionRunRecord | null) => SessionRunRecord | null) as SessionRunRecord | null 34 }, 35 delete(id) { 36 deleteStoredItem('runtime_runs', id) 37 }, 38 }, 39 ) 40 41 export const runEventRepository = createRecordRepository<RunEventRecord>( 42 'runtime-run-events', 43 { 44 get(id) { 45 return (loadStoredRuntimeRunEvents() as Record<string, RunEventRecord>)[id] || null 46 }, 47 list() { 48 return loadStoredRuntimeRunEvents() as Record<string, RunEventRecord> 49 }, 50 upsert(id, value) { 51 upsertStoredRuntimeRunEvent(id, value as RunEventRecord) 52 }, 53 replace(data) { 54 saveStoredRuntimeRunEvents(data as Record<string, RunEventRecord>) 55 }, 56 delete(id) { 57 deleteStoredItem('runtime_run_events', id) 58 }, 59 }, 60 ) 61 62 export const loadRuntimeRuns = () => runRepository.list() 63 export const saveRuntimeRuns = (items: Record<string, SessionRunRecord | Record<string, unknown>>) => runRepository.replace(items as Record<string, SessionRunRecord>) 64 export const loadRuntimeRun = (id: string) => runRepository.get(id) 65 export const upsertRuntimeRun = (id: string, value: SessionRunRecord | Record<string, unknown>) => runRepository.upsert(id, value as SessionRunRecord) 66 export const patchRuntimeRun = (id: string, updater: (current: SessionRunRecord | null) => SessionRunRecord | null) => runRepository.patch(id, updater) 67 68 export const loadRuntimeRunEvents = () => runEventRepository.list() 69 export const saveRuntimeRunEvents = (items: Record<string, RunEventRecord | Record<string, unknown>>) => runEventRepository.replace(items as Record<string, RunEventRecord>) 70 export const upsertRuntimeRunEvent = (id: string, value: RunEventRecord | Record<string, unknown>) => runEventRepository.upsert(id, value as RunEventRecord) 71 export const loadRuntimeRunEventsByRunId = (runId: string) => loadStoredRuntimeRunEventsByRunId(runId) 72 export const deleteRuntimeRun = (id: string) => runRepository.delete(id) 73 export const deleteRuntimeRunEvent = (id: string) => runEventRepository.delete(id)