* Hydrates the console store from IndexedDB on startup. * Applies the same normalization and trimming as the old persist merge.
()
| 718 | * Applies the same normalization and trimming as the old persist merge. |
| 719 | */ |
| 720 | async function hydrateConsoleStore(): Promise<void> { |
| 721 | try { |
| 722 | const data = await loadConsoleData() |
| 723 | |
| 724 | if (!data) { |
| 725 | useTerminalConsoleStore.setState({ _hasHydrated: true }) |
| 726 | return |
| 727 | } |
| 728 | |
| 729 | const oneHourAgo = Date.now() - 60 * 60 * 1000 |
| 730 | |
| 731 | const workflowEntries = Object.fromEntries( |
| 732 | Object.entries(data.workflowEntries).map(([workflowId, entries]) => [ |
| 733 | workflowId, |
| 734 | trimWorkflowConsoleEntries( |
| 735 | entries.map((entry, index) => { |
| 736 | let updated = entry |
| 737 | if (entry.executionOrder === undefined) { |
| 738 | updated = { ...updated, executionOrder: index + 1 } |
| 739 | } |
| 740 | if ( |
| 741 | entry.isRunning && |
| 742 | entry.startedAt && |
| 743 | new Date(entry.startedAt).getTime() < oneHourAgo |
| 744 | ) { |
| 745 | updated = { ...updated, isRunning: false } |
| 746 | } |
| 747 | updated = { |
| 748 | ...updated, |
| 749 | input: normalizeConsoleInput(updated.input), |
| 750 | output: normalizeConsoleOutput(updated.output), |
| 751 | error: normalizeConsoleError(updated.error), |
| 752 | warning: |
| 753 | typeof updated.warning === 'string' |
| 754 | ? (normalizeConsoleError(updated.warning) ?? undefined) |
| 755 | : updated.warning, |
| 756 | } |
| 757 | return updated |
| 758 | }) |
| 759 | ), |
| 760 | ]) |
| 761 | ) |
| 762 | |
| 763 | const currentState = useTerminalConsoleStore.getState() |
| 764 | const mergedWorkflowEntries = { ...workflowEntries } |
| 765 | |
| 766 | for (const [wfId, currentEntries] of Object.entries(currentState.workflowEntries)) { |
| 767 | if (currentEntries.length > 0) { |
| 768 | const persistedEntries = mergedWorkflowEntries[wfId] ?? [] |
| 769 | const persistedIds = new Set(persistedEntries.map((e) => e.id)) |
| 770 | const newEntries = currentEntries.filter((e) => !persistedIds.has(e.id)) |
| 771 | if (newEntries.length > 0) { |
| 772 | mergedWorkflowEntries[wfId] = trimWorkflowConsoleEntries([ |
| 773 | ...newEntries, |
| 774 | ...persistedEntries, |
| 775 | ]) |
| 776 | } |
| 777 | } |
no test coverage detected