@workflow/web-shared 4.1.0-beta.62 → 4.1.0-beta.64

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 (70) hide show
  1. package/README.md +4 -0
  2. package/dist/components/event-list-view.d.ts +9 -3
  3. package/dist/components/event-list-view.d.ts.map +1 -1
  4. package/dist/components/event-list-view.js +222 -98
  5. package/dist/components/event-list-view.js.map +1 -1
  6. package/dist/components/index.d.ts +1 -0
  7. package/dist/components/index.d.ts.map +1 -1
  8. package/dist/components/index.js +1 -0
  9. package/dist/components/index.js.map +1 -1
  10. package/dist/components/run-trace-view.d.ts +1 -3
  11. package/dist/components/run-trace-view.d.ts.map +1 -1
  12. package/dist/components/run-trace-view.js +2 -2
  13. package/dist/components/run-trace-view.js.map +1 -1
  14. package/dist/components/sidebar/attribute-panel.d.ts.map +1 -1
  15. package/dist/components/sidebar/attribute-panel.js +11 -1
  16. package/dist/components/sidebar/attribute-panel.js.map +1 -1
  17. package/dist/components/sidebar/detail-card.d.ts.map +1 -1
  18. package/dist/components/sidebar/detail-card.js +4 -2
  19. package/dist/components/sidebar/detail-card.js.map +1 -1
  20. package/dist/components/sidebar/entity-detail-panel.d.ts +3 -3
  21. package/dist/components/sidebar/entity-detail-panel.d.ts.map +1 -1
  22. package/dist/components/sidebar/entity-detail-panel.js +43 -26
  23. package/dist/components/sidebar/entity-detail-panel.js.map +1 -1
  24. package/dist/components/trace-viewer/trace-viewer.d.ts +7 -1
  25. package/dist/components/trace-viewer/trace-viewer.d.ts.map +1 -1
  26. package/dist/components/trace-viewer/trace-viewer.js +36 -11
  27. package/dist/components/trace-viewer/trace-viewer.js.map +1 -1
  28. package/dist/components/ui/error-stack-block.d.ts +3 -4
  29. package/dist/components/ui/error-stack-block.d.ts.map +1 -1
  30. package/dist/components/ui/error-stack-block.js +18 -9
  31. package/dist/components/ui/error-stack-block.js.map +1 -1
  32. package/dist/components/ui/menu-dropdown.d.ts +16 -0
  33. package/dist/components/ui/menu-dropdown.d.ts.map +1 -0
  34. package/dist/components/ui/menu-dropdown.js +50 -0
  35. package/dist/components/ui/menu-dropdown.js.map +1 -0
  36. package/dist/components/workflow-trace-view.d.ts +3 -3
  37. package/dist/components/workflow-trace-view.d.ts.map +1 -1
  38. package/dist/components/workflow-trace-view.js +31 -129
  39. package/dist/components/workflow-trace-view.js.map +1 -1
  40. package/dist/components/workflow-traces/trace-span-construction.d.ts +18 -5
  41. package/dist/components/workflow-traces/trace-span-construction.d.ts.map +1 -1
  42. package/dist/components/workflow-traces/trace-span-construction.js +65 -18
  43. package/dist/components/workflow-traces/trace-span-construction.js.map +1 -1
  44. package/dist/index.d.ts +3 -1
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +2 -1
  47. package/dist/index.js.map +1 -1
  48. package/dist/lib/event-materialization.d.ts +72 -0
  49. package/dist/lib/event-materialization.d.ts.map +1 -0
  50. package/dist/lib/event-materialization.js +171 -0
  51. package/dist/lib/event-materialization.js.map +1 -0
  52. package/dist/lib/trace-builder.d.ts +32 -0
  53. package/dist/lib/trace-builder.d.ts.map +1 -0
  54. package/dist/lib/trace-builder.js +129 -0
  55. package/dist/lib/trace-builder.js.map +1 -0
  56. package/package.json +3 -3
  57. package/src/components/event-list-view.tsx +324 -103
  58. package/src/components/index.ts +1 -0
  59. package/src/components/run-trace-view.tsx +0 -6
  60. package/src/components/sidebar/attribute-panel.tsx +17 -2
  61. package/src/components/sidebar/detail-card.tsx +10 -2
  62. package/src/components/sidebar/entity-detail-panel.tsx +59 -21
  63. package/src/components/trace-viewer/trace-viewer.tsx +47 -2
  64. package/src/components/ui/error-stack-block.tsx +26 -16
  65. package/src/components/ui/menu-dropdown.tsx +114 -0
  66. package/src/components/workflow-trace-view.tsx +95 -195
  67. package/src/components/workflow-traces/trace-span-construction.ts +85 -32
  68. package/src/index.ts +13 -0
  69. package/src/lib/event-materialization.ts +243 -0
  70. package/src/lib/trace-builder.ts +201 -0
@@ -0,0 +1,243 @@
1
+ /**
2
+ * Event materialization helpers.
3
+ *
4
+ * These functions convert a flat list of workflow events into entity-like
5
+ * objects (steps, hooks, waits) by grouping events by correlationId and
6
+ * stitching together lifecycle events.
7
+ *
8
+ * This enables a "top-down" data fetching pattern where the client fetches
9
+ * all events for a run once, then materializes entities client-side instead
10
+ * of making separate API calls for each entity type.
11
+ */
12
+
13
+ import type { Event, StepStatus } from '@workflow/world';
14
+
15
+ // ---------------------------------------------------------------------------
16
+ // Materialized entity types
17
+ // ---------------------------------------------------------------------------
18
+
19
+ export interface MaterializedStep {
20
+ stepId: string;
21
+ runId: string;
22
+ stepName: string;
23
+ status: StepStatus;
24
+ attempt: number;
25
+ createdAt: Date;
26
+ startedAt?: Date;
27
+ completedAt?: Date;
28
+ updatedAt: Date;
29
+ /** All events for this step, in insertion order */
30
+ events: Event[];
31
+ }
32
+
33
+ export interface MaterializedHook {
34
+ hookId: string;
35
+ runId: string;
36
+ token?: string;
37
+ createdAt: Date;
38
+ receivedCount: number;
39
+ lastReceivedAt?: Date;
40
+ disposedAt?: Date;
41
+ /** All events for this hook, in insertion order */
42
+ events: Event[];
43
+ }
44
+
45
+ export interface MaterializedWait {
46
+ waitId: string;
47
+ runId: string;
48
+ status: 'waiting' | 'completed';
49
+ createdAt: Date;
50
+ resumeAt?: Date;
51
+ completedAt?: Date;
52
+ /** All events for this wait, in insertion order */
53
+ events: Event[];
54
+ }
55
+
56
+ export interface MaterializedEntities {
57
+ steps: MaterializedStep[];
58
+ hooks: MaterializedHook[];
59
+ waits: MaterializedWait[];
60
+ }
61
+
62
+ // ---------------------------------------------------------------------------
63
+ // Helper: group events by correlationId prefix
64
+ // ---------------------------------------------------------------------------
65
+
66
+ function groupByCorrelationId(
67
+ events: Event[],
68
+ prefixes: string[]
69
+ ): Map<string, Event[]> {
70
+ const groups = new Map<string, Event[]>();
71
+ for (const event of events) {
72
+ const cid = event.correlationId;
73
+ if (!cid) continue;
74
+ if (!prefixes.some((p) => cid.startsWith(p))) continue;
75
+ const existing = groups.get(cid);
76
+ if (existing) {
77
+ existing.push(event);
78
+ } else {
79
+ groups.set(cid, [event]);
80
+ }
81
+ }
82
+ return groups;
83
+ }
84
+
85
+ // ---------------------------------------------------------------------------
86
+ // materializeSteps
87
+ // ---------------------------------------------------------------------------
88
+
89
+ /**
90
+ * Group step_* events by correlationId and build Step-like entities.
91
+ *
92
+ * Handles partial event lists gracefully: a step may only have a
93
+ * step_created event with no completion yet.
94
+ */
95
+ export function materializeSteps(events: Event[]): MaterializedStep[] {
96
+ const groups = groupByCorrelationId(events, ['step_']);
97
+ const steps: MaterializedStep[] = [];
98
+
99
+ for (const [correlationId, stepEvents] of groups) {
100
+ const created = stepEvents.find((e) => e.eventType === 'step_created');
101
+ if (!created) continue;
102
+
103
+ let status: StepStatus = 'pending';
104
+ let attempt = 0;
105
+ let startedAt: Date | undefined;
106
+ let completedAt: Date | undefined;
107
+ let updatedAt = created.createdAt;
108
+
109
+ for (const e of stepEvents) {
110
+ switch (e.eventType) {
111
+ case 'step_started':
112
+ status = 'running';
113
+ attempt += 1;
114
+ if (!startedAt) startedAt = e.createdAt;
115
+ completedAt = undefined;
116
+ updatedAt = e.createdAt;
117
+ break;
118
+ case 'step_completed':
119
+ status = 'completed';
120
+ completedAt = e.createdAt;
121
+ updatedAt = e.createdAt;
122
+ break;
123
+ case 'step_failed':
124
+ status = 'failed';
125
+ completedAt = e.createdAt;
126
+ updatedAt = e.createdAt;
127
+ break;
128
+ case 'step_retrying':
129
+ status = 'pending';
130
+ completedAt = undefined;
131
+ updatedAt = e.createdAt;
132
+ break;
133
+ }
134
+ }
135
+
136
+ steps.push({
137
+ stepId: correlationId,
138
+ runId: created.runId,
139
+ stepName:
140
+ created.eventType === 'step_created'
141
+ ? (created.eventData?.stepName ?? correlationId)
142
+ : correlationId,
143
+ status,
144
+ attempt,
145
+ createdAt: created.createdAt,
146
+ startedAt,
147
+ completedAt,
148
+ updatedAt,
149
+ events: stepEvents,
150
+ });
151
+ }
152
+
153
+ return steps;
154
+ }
155
+
156
+ // ---------------------------------------------------------------------------
157
+ // materializeHooks
158
+ // ---------------------------------------------------------------------------
159
+
160
+ /**
161
+ * Group hook_* events by correlationId and build Hook-like entities.
162
+ */
163
+ export function materializeHooks(events: Event[]): MaterializedHook[] {
164
+ const groups = groupByCorrelationId(events, ['hook_']);
165
+ const hooks: MaterializedHook[] = [];
166
+
167
+ for (const [correlationId, hookEvents] of groups) {
168
+ const created = hookEvents.find((e) => e.eventType === 'hook_created');
169
+ if (!created) continue;
170
+
171
+ const receivedEvents = hookEvents.filter(
172
+ (e) => e.eventType === 'hook_received'
173
+ );
174
+ const disposed = hookEvents.find((e) => e.eventType === 'hook_disposed');
175
+ const lastReceived = receivedEvents.at(-1);
176
+
177
+ hooks.push({
178
+ hookId: correlationId,
179
+ runId: created.runId,
180
+ token:
181
+ created.eventType === 'hook_created'
182
+ ? created.eventData?.token
183
+ : undefined,
184
+ createdAt: created.createdAt,
185
+ receivedCount: receivedEvents.length,
186
+ lastReceivedAt: lastReceived?.createdAt,
187
+ disposedAt: disposed?.createdAt,
188
+ events: hookEvents,
189
+ });
190
+ }
191
+
192
+ return hooks;
193
+ }
194
+
195
+ // ---------------------------------------------------------------------------
196
+ // materializeWaits
197
+ // ---------------------------------------------------------------------------
198
+
199
+ /**
200
+ * Group wait_* events by correlationId and build Wait-like entities.
201
+ */
202
+ export function materializeWaits(events: Event[]): MaterializedWait[] {
203
+ const groups = groupByCorrelationId(events, ['wait_']);
204
+ const waits: MaterializedWait[] = [];
205
+
206
+ for (const [correlationId, waitEvents] of groups) {
207
+ const created = waitEvents.find((e) => e.eventType === 'wait_created');
208
+ if (!created) continue;
209
+
210
+ const completed = waitEvents.find((e) => e.eventType === 'wait_completed');
211
+
212
+ waits.push({
213
+ waitId: correlationId,
214
+ runId: created.runId,
215
+ status: completed ? 'completed' : 'waiting',
216
+ createdAt: created.createdAt,
217
+ resumeAt:
218
+ created.eventType === 'wait_created'
219
+ ? created.eventData?.resumeAt
220
+ : undefined,
221
+ completedAt: completed?.createdAt,
222
+ events: waitEvents,
223
+ });
224
+ }
225
+
226
+ return waits;
227
+ }
228
+
229
+ // ---------------------------------------------------------------------------
230
+ // materializeAll
231
+ // ---------------------------------------------------------------------------
232
+
233
+ /**
234
+ * Convenience function that materializes all entity types from a flat
235
+ * event list.
236
+ */
237
+ export function materializeAll(events: Event[]): MaterializedEntities {
238
+ return {
239
+ steps: materializeSteps(events),
240
+ hooks: materializeHooks(events),
241
+ waits: materializeWaits(events),
242
+ };
243
+ }
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Builds a complete trace from a WorkflowRun and its Events.
3
+ *
4
+ * This module groups raw events by correlation ID and entity type,
5
+ * converts each group into an OpenTelemetry-style Span, and returns
6
+ * a fully-formed Trace ready for the trace viewer.
7
+ */
8
+
9
+ import type { Event, WorkflowRun } from '@workflow/world';
10
+ import type { Span } from '../components/trace-viewer/types';
11
+ import {
12
+ hookToSpan,
13
+ runToSpan,
14
+ stepToSpan,
15
+ waitToSpan,
16
+ WORKFLOW_LIBRARY,
17
+ } from '../components/workflow-traces/trace-span-construction';
18
+ import { otelTimeToMs } from '../components/workflow-traces/trace-time-utils';
19
+
20
+ // ---------------------------------------------------------------------------
21
+ // Event type classifiers
22
+ // ---------------------------------------------------------------------------
23
+
24
+ export const isStepEvent = (eventType: string) => eventType.startsWith('step_');
25
+
26
+ export const isTimerEvent = (eventType: string) =>
27
+ eventType === 'wait_created' || eventType === 'wait_completed';
28
+
29
+ export const isHookLifecycleEvent = (eventType: string) =>
30
+ eventType === 'hook_received' ||
31
+ eventType === 'hook_created' ||
32
+ eventType === 'hook_disposed';
33
+
34
+ // ---------------------------------------------------------------------------
35
+ // Event grouping
36
+ // ---------------------------------------------------------------------------
37
+
38
+ export type GroupedEvents = {
39
+ eventsByStepId: Map<string, Event[]>;
40
+ runLevelEvents: Event[];
41
+ timerEvents: Map<string, Event[]>;
42
+ hookEvents: Map<string, Event[]>;
43
+ };
44
+
45
+ function pushEvent(
46
+ map: Map<string, Event[]>,
47
+ correlationId: string,
48
+ event: Event
49
+ ) {
50
+ const existing = map.get(correlationId);
51
+ if (existing) {
52
+ existing.push(event);
53
+ return;
54
+ }
55
+ map.set(correlationId, [event]);
56
+ }
57
+
58
+ export function groupEventsByCorrelation(events: Event[]): GroupedEvents {
59
+ const eventsByStepId = new Map<string, Event[]>();
60
+ const runLevelEvents: Event[] = [];
61
+ const timerEvents = new Map<string, Event[]>();
62
+ const hookEvents = new Map<string, Event[]>();
63
+
64
+ for (const event of events) {
65
+ const correlationId = event.correlationId;
66
+ if (!correlationId) {
67
+ runLevelEvents.push(event);
68
+ continue;
69
+ }
70
+
71
+ if (isTimerEvent(event.eventType)) {
72
+ pushEvent(timerEvents, correlationId, event);
73
+ continue;
74
+ }
75
+
76
+ if (isHookLifecycleEvent(event.eventType)) {
77
+ pushEvent(hookEvents, correlationId, event);
78
+ continue;
79
+ }
80
+
81
+ if (isStepEvent(event.eventType)) {
82
+ pushEvent(eventsByStepId, correlationId, event);
83
+ continue;
84
+ }
85
+
86
+ runLevelEvents.push(event);
87
+ }
88
+
89
+ return { eventsByStepId, runLevelEvents, timerEvents, hookEvents };
90
+ }
91
+
92
+ // ---------------------------------------------------------------------------
93
+ // Trace construction
94
+ // ---------------------------------------------------------------------------
95
+
96
+ /**
97
+ * Computes the latest known event time from the events list.
98
+ * Active (in-progress) child spans are capped at this time instead of `now`,
99
+ * so they only extend as far as our data goes.
100
+ */
101
+ function computeLatestKnownTime(events: Event[], run: WorkflowRun): Date {
102
+ let latest = new Date(run.createdAt).getTime();
103
+ for (const event of events) {
104
+ const t = new Date(event.createdAt).getTime();
105
+ if (t > latest) latest = t;
106
+ }
107
+ return new Date(latest);
108
+ }
109
+
110
+ function buildSpans(
111
+ run: WorkflowRun,
112
+ groupedEvents: GroupedEvents,
113
+ now: Date,
114
+ latestKnownTime: Date
115
+ ) {
116
+ // Active child spans cap at latestKnownTime so they don't extend into
117
+ // unknown territory. Even when the run is completed, we may not have loaded
118
+ // all events yet, so always use the latest event we've actually seen.
119
+ const childMaxEnd = latestKnownTime;
120
+
121
+ const stepSpans = Array.from(groupedEvents.eventsByStepId.values())
122
+ .map((events) => stepToSpan(events, childMaxEnd))
123
+ .filter((span): span is Span => span !== null);
124
+
125
+ const hookSpans = Array.from(groupedEvents.hookEvents.values())
126
+ .map((events) => hookToSpan(events, childMaxEnd))
127
+ .filter((span): span is Span => span !== null);
128
+
129
+ const waitSpans = Array.from(groupedEvents.timerEvents.values())
130
+ .map((events) => waitToSpan(events, childMaxEnd))
131
+ .filter((span): span is Span => span !== null);
132
+
133
+ return {
134
+ runSpan: runToSpan(run, groupedEvents.runLevelEvents, now),
135
+ spans: [...stepSpans, ...hookSpans, ...waitSpans],
136
+ };
137
+ }
138
+
139
+ function cascadeSpans(runSpan: Span, spans: Span[]) {
140
+ const sortedSpans = [
141
+ runSpan,
142
+ ...spans.slice().sort((a, b) => {
143
+ const aStart = otelTimeToMs(a.startTime);
144
+ const bStart = otelTimeToMs(b.startTime);
145
+ return aStart - bStart;
146
+ }),
147
+ ];
148
+
149
+ return sortedSpans.map((span, index) => {
150
+ const parentSpanId =
151
+ index === 0 ? undefined : String(sortedSpans[index - 1].spanId);
152
+ return {
153
+ ...span,
154
+ parentSpanId,
155
+ };
156
+ });
157
+ }
158
+
159
+ export interface TraceWithMeta {
160
+ traceId: string;
161
+ rootSpanId: string;
162
+ spans: Span[];
163
+ resources: { name: string; attributes: Record<string, string> }[];
164
+ /** Duration in ms from trace start to the latest known event. */
165
+ knownDurationMs: number;
166
+ }
167
+
168
+ export function buildTrace(
169
+ run: WorkflowRun,
170
+ events: Event[],
171
+ now: Date
172
+ ): TraceWithMeta {
173
+ const groupedEvents = groupEventsByCorrelation(events);
174
+ const latestKnownTime = computeLatestKnownTime(events, run);
175
+ const { runSpan, spans } = buildSpans(
176
+ run,
177
+ groupedEvents,
178
+ now,
179
+ latestKnownTime
180
+ );
181
+ const sortedCascadingSpans = cascadeSpans(runSpan, spans);
182
+
183
+ // Compute known duration relative to trace start
184
+ const traceStartMs = otelTimeToMs(runSpan.startTime);
185
+ const knownDurationMs = latestKnownTime.getTime() - traceStartMs;
186
+
187
+ return {
188
+ traceId: run.runId,
189
+ rootSpanId: run.runId,
190
+ spans: sortedCascadingSpans,
191
+ resources: [
192
+ {
193
+ name: 'workflow',
194
+ attributes: {
195
+ 'service.name': WORKFLOW_LIBRARY.name,
196
+ },
197
+ },
198
+ ],
199
+ knownDurationMs: Math.max(0, knownDurationMs),
200
+ };
201
+ }