MCPcopy Create free account
hub / github.com/TanStack/ai / syncRunsFromSnapshot

Function syncRunsFromSnapshot

packages/ai-devtools/src/store/hook-registry.ts:693–754  ·  view source on GitHub ↗
(
  state: HookRegistryState,
  snapshot: HookStateSnapshotEvent,
  eventId: string,
)

Source from the content-addressed store, hash-verified

691}
692
693function syncRunsFromSnapshot(
694 state: HookRegistryState,
695 snapshot: HookStateSnapshotEvent,
696 eventId: string,
697): void {
698 const rawRuns = (snapshot.state as { runs?: unknown }).runs
699 if (!Array.isArray(rawRuns)) return
700 for (const candidate of rawRuns) {
701 if (!candidate || typeof candidate !== 'object') continue
702 const run = candidate as {
703 id?: unknown
704 status?: unknown
705 startedAt?: unknown
706 updatedAt?: unknown
707 completedAt?: unknown
708 error?: unknown
709 }
710 if (typeof run.id !== 'string') continue
711 const status = normalizeRunStatusFromSnapshot(run.status)
712 if (status === null) {
713 console.warn(
714 `[ai-devtools] unknown run.status in snapshot for hook "${snapshot.hookId}": ${String(run.status)}; skipping run "${run.id}"`,
715 )
716 continue
717 }
718 const startedAt =
719 typeof run.startedAt === 'number' ? run.startedAt : snapshot.timestamp
720 const updatedAt =
721 typeof run.updatedAt === 'number' ? run.updatedAt : startedAt
722 const existing = state.runs[run.id]
723 if (!existing) {
724 state.runs[run.id] = {
725 id: run.id,
726 hookId: snapshot.hookId,
727 status,
728 startedAt,
729 updatedAt,
730 ...(typeof run.completedAt === 'number'
731 ? { completedAt: run.completedAt }
732 : isTerminalRunStatus(status)
733 ? { completedAt: updatedAt }
734 : {}),
735 ...(typeof run.error === 'string' ? { error: run.error } : {}),
736 eventIds: [eventId],
737 }
738 } else {
739 existing.hookId = snapshot.hookId
740 existing.status = status
741 existing.updatedAt = updatedAt
742 if (typeof run.completedAt === 'number') {
743 existing.completedAt = run.completedAt
744 } else if (isTerminalRunStatus(status) && !existing.completedAt) {
745 existing.completedAt = updatedAt
746 }
747 if (typeof run.error === 'string') existing.error = run.error
748 if (!existing.eventIds.includes(eventId)) {
749 existing.eventIds.push(eventId)
750 }

Callers 1

applyHookEventFunction · 0.85

Calls 5

attachRunToHookFunction · 0.85
isTerminalRunStatusFunction · 0.70
warnMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected