@workflow/web-shared 5.0.7 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/dist/components/event-list-view.d.ts +2 -2
  2. package/dist/components/event-list-view.d.ts.map +1 -1
  3. package/dist/components/sidebar/attribute-panel.d.ts +2 -2
  4. package/dist/components/sidebar/attribute-panel.d.ts.map +1 -1
  5. package/dist/components/sidebar/attribute-panel.js +2 -2
  6. package/dist/components/sidebar/attributes-block.d.ts +5 -5
  7. package/dist/components/sidebar/attributes-block.d.ts.map +1 -1
  8. package/dist/components/sidebar/conversation-view.d.ts +1 -1
  9. package/dist/components/sidebar/conversation-view.d.ts.map +1 -1
  10. package/dist/components/sidebar/copyable-data-block.d.ts +2 -2
  11. package/dist/components/sidebar/copyable-data-block.d.ts.map +1 -1
  12. package/dist/components/sidebar/entity-detail-panel.d.ts.map +1 -1
  13. package/dist/components/sidebar/entity-detail-panel.js +4 -17
  14. package/dist/components/sidebar/events-list.d.ts +1 -1
  15. package/dist/components/sidebar/events-list.d.ts.map +1 -1
  16. package/dist/components/sidebar/sidebar-data-context.d.ts +1 -1
  17. package/dist/components/sidebar/sidebar-data-context.d.ts.map +1 -1
  18. package/dist/components/stream-viewer-skeleton.d.ts +1 -1
  19. package/dist/components/stream-viewer-skeleton.d.ts.map +1 -1
  20. package/dist/components/stream-viewer.d.ts +2 -1
  21. package/dist/components/stream-viewer.d.ts.map +1 -1
  22. package/dist/components/trace-viewer/components/draggable-border.d.ts +1 -1
  23. package/dist/components/trace-viewer/components/draggable-border.d.ts.map +1 -1
  24. package/dist/components/trace-viewer/components/event-list.d.ts +1 -1
  25. package/dist/components/trace-viewer/components/event-list.d.ts.map +1 -1
  26. package/dist/components/trace-viewer/components/split-pane.d.ts +1 -1
  27. package/dist/components/trace-viewer/components/split-pane.d.ts.map +1 -1
  28. package/dist/components/trace-viewer/components/trace-shortcut-helper.d.ts +1 -1
  29. package/dist/components/trace-viewer/components/trace-shortcut-helper.d.ts.map +1 -1
  30. package/dist/components/trace-viewer/components/trace-viewer-skeleton.d.ts +1 -1
  31. package/dist/components/trace-viewer/components/trace-viewer-skeleton.d.ts.map +1 -1
  32. package/dist/components/trace-viewer/context.d.ts +1 -1
  33. package/dist/components/trace-viewer/context.d.ts.map +1 -1
  34. package/dist/components/trace-viewer/icons.d.ts +4 -4
  35. package/dist/components/trace-viewer/icons.d.ts.map +1 -1
  36. package/dist/components/trace-viewer/utils.d.ts.map +1 -1
  37. package/dist/components/trace-viewer/utils.js +12 -3
  38. package/dist/components/trace-viewer.d.ts +1 -1
  39. package/dist/components/trace-viewer.d.ts.map +1 -1
  40. package/dist/components/ui/collapsible.d.ts +4 -4
  41. package/dist/components/ui/collapsible.d.ts.map +1 -1
  42. package/dist/components/ui/decrypt-button.d.ts +1 -1
  43. package/dist/components/ui/decrypt-button.d.ts.map +1 -1
  44. package/dist/components/ui/error-card.d.ts +1 -1
  45. package/dist/components/ui/error-card.d.ts.map +1 -1
  46. package/dist/components/ui/error-stack-block.d.ts +1 -1
  47. package/dist/components/ui/error-stack-block.d.ts.map +1 -1
  48. package/dist/components/ui/load-more-button.d.ts +1 -1
  49. package/dist/components/ui/load-more-button.d.ts.map +1 -1
  50. package/dist/components/ui/menu-dropdown.d.ts +1 -1
  51. package/dist/components/ui/menu-dropdown.d.ts.map +1 -1
  52. package/dist/components/ui/skeleton.d.ts +1 -1
  53. package/dist/components/ui/skeleton.d.ts.map +1 -1
  54. package/dist/components/ui/spinner.d.ts +1 -1
  55. package/dist/components/ui/spinner.d.ts.map +1 -1
  56. package/dist/components/workflow-traces/trace-span-construction.d.ts +8 -2
  57. package/dist/components/workflow-traces/trace-span-construction.d.ts.map +1 -1
  58. package/dist/components/workflow-traces/trace-span-construction.js +29 -11
  59. package/dist/index.d.ts +1 -0
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +2 -1
  62. package/dist/lib/event-analysis.d.ts.map +1 -1
  63. package/dist/lib/event-analysis.js +10 -2
  64. package/dist/lib/event-materialization.d.ts +4 -0
  65. package/dist/lib/event-materialization.d.ts.map +1 -1
  66. package/dist/lib/event-materialization.js +25 -7
  67. package/dist/lib/hydration.d.ts +5 -3
  68. package/dist/lib/hydration.d.ts.map +1 -1
  69. package/dist/lib/hydration.js +10 -6
  70. package/dist/lib/start-hook.d.ts +30 -0
  71. package/dist/lib/start-hook.d.ts.map +1 -0
  72. package/dist/lib/start-hook.js +50 -0
  73. package/dist/lib/state-hydration.d.ts +37 -0
  74. package/dist/lib/state-hydration.d.ts.map +1 -0
  75. package/dist/lib/state-hydration.js +148 -0
  76. package/dist/lib/trace-builder.d.ts.map +1 -1
  77. package/dist/lib/trace-builder.js +12 -5
  78. package/package.json +19 -18
  79. package/src/components/sidebar/attribute-panel.tsx +1 -1
  80. package/src/components/sidebar/entity-detail-panel.tsx +31 -61
  81. package/src/components/trace-viewer/utils.test.ts +24 -0
  82. package/src/components/trace-viewer/utils.ts +14 -2
  83. package/src/components/workflow-traces/trace-span-construction.test.ts +30 -0
  84. package/src/components/workflow-traces/trace-span-construction.ts +34 -11
  85. package/src/index.ts +6 -0
  86. package/src/lib/event-analysis.ts +9 -0
  87. package/src/lib/event-materialization.ts +26 -7
  88. package/src/lib/hydration.ts +11 -5
  89. package/src/lib/start-hook.test.ts +174 -0
  90. package/src/lib/start-hook.ts +82 -0
  91. package/src/lib/state-hydration.test.ts +332 -0
  92. package/src/lib/state-hydration.ts +208 -0
  93. package/src/lib/trace-builder.ts +17 -2
@@ -0,0 +1,208 @@
1
+ /**
2
+ * Shows the actual values of frozen state stored by reference.
3
+ *
4
+ * A step input can be a reference to frozen state an earlier step
5
+ * stored, and a step output only its change from that state.
6
+ * Rebuilding them needs the run's earlier payloads. A run state hydrator reads
7
+ * them once, only when a hydrated value needs them, and reuses them for every
8
+ * later inspection of the run.
9
+ */
10
+
11
+ import {
12
+ createStateResolver,
13
+ decodePayloadWithKey,
14
+ deriveRunPayloadKeys,
15
+ type StateResolver,
16
+ stateSourceId,
17
+ } from '@workflow/core/serialization-format';
18
+ import { getWebRevivers, hydrateResourceIOAsync } from './hydration';
19
+
20
+ /** One page of a run's events, oldest first, with their data. */
21
+ export interface StateHistoryPage {
22
+ data: readonly unknown[];
23
+ cursor?: string | null;
24
+ hasMore?: boolean;
25
+ }
26
+
27
+ export interface RunStateHydratorOptions {
28
+ /** The run's `input`, as stored (`resolveData: 'all'`). */
29
+ loadRunInput: () => Promise<unknown>;
30
+ /**
31
+ * A page of the run's events in ascending order, with their data, starting
32
+ * after `cursor` (or at the start without one).
33
+ */
34
+ loadEvents: (cursor: string | undefined) => Promise<StateHistoryPage>;
35
+ }
36
+
37
+ export interface RunStateHydrator {
38
+ /**
39
+ * Like `hydrateResourceIOAsync`, but also rebuilds frozen state values,
40
+ * reading the run's history the first time a value needs it.
41
+ */
42
+ hydrate<T>(resource: T, key?: Uint8Array): Promise<T>;
43
+ }
44
+
45
+ type Dict = Record<string, unknown>;
46
+
47
+ /** Like the runtime's cold recovery, never read more than this many pages per read. */
48
+ const MAX_PAGES = 1000;
49
+
50
+ /**
51
+ * The payload of an event that frozen state values can build on, and the
52
+ * id later state refers to its strings by (hook payloads only).
53
+ */
54
+ function stateSourceOf(
55
+ event: unknown
56
+ ): { value: unknown; source?: string } | undefined {
57
+ if (event === null || typeof event !== 'object') return undefined;
58
+ const { eventType, eventData, eventId } = event as Dict;
59
+ if (eventData === null || typeof eventData !== 'object') return undefined;
60
+ if (eventType === 'hook_received')
61
+ return {
62
+ value: (eventData as Dict).payload,
63
+ source: stateSourceId({
64
+ eventType,
65
+ eventId: typeof eventId === 'string' ? eventId : undefined,
66
+ }),
67
+ };
68
+ if (eventType === 'step_completed')
69
+ return { value: (eventData as Dict).result };
70
+ return undefined;
71
+ }
72
+
73
+ /**
74
+ * Creates the state hydrator for one run. Keep it for as long as the run is on
75
+ * screen and hydrate every resource of the run through it.
76
+ */
77
+ export function createRunStateHydrator(
78
+ options: RunStateHydratorOptions
79
+ ): RunStateHydrator {
80
+ // A different key can open different payloads, so each key gets its own.
81
+ let state:
82
+ | {
83
+ key: Uint8Array | undefined;
84
+ resolver: StateResolver;
85
+ cursor: string | undefined;
86
+ startedInput: boolean;
87
+ seen: Set<string>;
88
+ /** Set once the whole history has been read. */
89
+ complete: boolean;
90
+ reading: Promise<void> | undefined;
91
+ }
92
+ | undefined;
93
+
94
+ const stateFor = (key: Uint8Array | undefined) => {
95
+ if (!state || state.key !== key)
96
+ state = {
97
+ key,
98
+ resolver: createStateResolver(getWebRevivers()),
99
+ cursor: undefined,
100
+ startedInput: false,
101
+ seen: new Set(),
102
+ complete: false,
103
+ reading: undefined,
104
+ };
105
+ return state;
106
+ };
107
+
108
+ const add = async (
109
+ resolver: StateResolver,
110
+ value: unknown,
111
+ key: Awaited<ReturnType<typeof deriveRunPayloadKeys>> | undefined,
112
+ source?: string
113
+ ) => {
114
+ if (value === undefined || value === null) return;
115
+ try {
116
+ resolver.add(await decodePayloadWithKey(value, key), source);
117
+ } catch {
118
+ // A payload that cannot be read leaves the values built on it as
119
+ // placeholders.
120
+ }
121
+ };
122
+
123
+ /**
124
+ * Reads the events added since the last read, in log order, a page at a
125
+ * time, until `resolved` says the value being inspected no longer needs
126
+ * more. Inspecting an early step reads only the start of a long run, and
127
+ * the next inspection continues from there.
128
+ */
129
+ const readHistory = async (
130
+ current: NonNullable<typeof state>,
131
+ resolved: () => Promise<boolean>
132
+ ) => {
133
+ const key = current.key
134
+ ? await deriveRunPayloadKeys(current.key)
135
+ : undefined;
136
+ if (!current.startedInput) {
137
+ // The run input only supplies strings later state may refer to; a run
138
+ // whose input can't be read still rebuilds everything else.
139
+ await add(
140
+ current.resolver,
141
+ await options.loadRunInput().catch(() => undefined),
142
+ key,
143
+ stateSourceId('run-input')
144
+ );
145
+ current.startedInput = true;
146
+ }
147
+ // A cursor that repeats would otherwise loop forever in the browser.
148
+ const cursors = new Set<string>();
149
+ for (let pages = 0; pages < MAX_PAGES; pages++) {
150
+ const page = await options.loadEvents(current.cursor);
151
+ for (const event of page.data) {
152
+ // A re-read from the last cursor can repeat events.
153
+ const id = (event as Dict | null)?.eventId;
154
+ if (typeof id === 'string') {
155
+ if (current.seen.has(id)) continue;
156
+ current.seen.add(id);
157
+ }
158
+ const payload = stateSourceOf(event);
159
+ if (payload)
160
+ await add(current.resolver, payload.value, key, payload.source);
161
+ }
162
+ if (page.cursor) current.cursor = page.cursor;
163
+ if (!page.hasMore) current.complete = true;
164
+ if (!page.hasMore || !page.cursor || cursors.has(page.cursor)) return;
165
+ cursors.add(page.cursor);
166
+ if (await resolved()) return;
167
+ // Let the page paint and respond between pages of a long run.
168
+ await new Promise((resolve) => setTimeout(resolve, 0));
169
+ }
170
+ };
171
+
172
+ return {
173
+ async hydrate(resource, key) {
174
+ const current = stateFor(key);
175
+ const missed = current.resolver.unresolved;
176
+ const hydrated = await hydrateResourceIOAsync(
177
+ resource,
178
+ key,
179
+ current.resolver.revivers
180
+ );
181
+ if (current.resolver.unresolved === missed) return hydrated;
182
+ // Whether what has been read so far rebuilds this resource.
183
+ const resolved = async () => {
184
+ const before = current.resolver.unresolved;
185
+ await hydrateResourceIOAsync(resource, key, current.resolver.revivers);
186
+ return current.resolver.unresolved === before;
187
+ };
188
+ // Concurrent inspections share one read. One started by another
189
+ // inspection may stop before this resource's records, so read on until
190
+ // this resource resolves or the history has been read to the end. Each
191
+ // pass reads at least one page, so this ends.
192
+ for (let pass = 0; pass < MAX_PAGES; pass++) {
193
+ const shared = current.reading !== undefined;
194
+ if (!current.reading)
195
+ current.reading = readHistory(current, resolved).finally(() => {
196
+ current.reading = undefined;
197
+ });
198
+ try {
199
+ await current.reading;
200
+ } catch {
201
+ return hydrated;
202
+ }
203
+ if (!shared || current.complete || (await resolved())) break;
204
+ }
205
+ return hydrateResourceIOAsync(resource, key, current.resolver.revivers);
206
+ },
207
+ };
208
+ }
@@ -25,6 +25,7 @@ import {
25
25
  import { otelTimeToMs } from '../components/workflow-traces/trace-time-utils';
26
26
  import { findDuplicateEventIds } from './duplicate-events';
27
27
  import { isSealedNoopEvent } from './sealed-events';
28
+ import { findStartHookCreation, type StartHookCreation } from './start-hook';
28
29
  import type { Span } from './trace-types';
29
30
 
30
31
  /**
@@ -139,6 +140,7 @@ function buildSpans(
139
140
  groupedEvents: GroupedEvents,
140
141
  now: Date,
141
142
  latestKnownTime: Date,
143
+ startHook: StartHookCreation | undefined,
142
144
  getStepAttributes?: GetStepAttributes
143
145
  ) {
144
146
  // Active child spans cap at latestKnownTime so they don't extend into
@@ -151,8 +153,20 @@ function buildSpans(
151
153
  .map((events) => stepToSpan(events, childMaxEnd, getStepAttributes))
152
154
  .filter((span): span is Span => span !== null);
153
155
 
154
- const hookSpans = Array.from(groupedEvents.hookEvents.values())
155
- .map((events) => hookToSpan(events, childMaxEnd))
156
+ // A start Hook exists from the run's creation, before (and whether or not)
157
+ // anything is delivered to it, and has no `hook_created` of its own.
158
+ const hookEventGroups = new Map(groupedEvents.hookEvents);
159
+ if (startHook && !hookEventGroups.has(startHook.hookId)) {
160
+ hookEventGroups.set(startHook.hookId, []);
161
+ }
162
+ const hookSpans = Array.from(hookEventGroups)
163
+ .map(([hookId, events]) =>
164
+ hookToSpan(
165
+ events,
166
+ childMaxEnd,
167
+ hookId === startHook?.hookId ? startHook : undefined
168
+ )
169
+ )
156
170
  .filter((span): span is Span => span !== null);
157
171
 
158
172
  const waitSpans = Array.from(groupedEvents.timerEvents.values())
@@ -241,6 +255,7 @@ export function buildTrace(
241
255
  groupedEvents,
242
256
  now,
243
257
  latestKnownTime,
258
+ findStartHookCreation(actedOnEvents, run),
244
259
  getStepAttributes
245
260
  );
246
261
  const sortedCascadingSpans = cascadeSpans(runSpan, spans);