@workflow/web-shared 5.0.8 → 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.
- package/dist/components/event-list-view.d.ts +2 -2
- package/dist/components/event-list-view.d.ts.map +1 -1
- package/dist/components/sidebar/attribute-panel.d.ts +2 -2
- package/dist/components/sidebar/attribute-panel.d.ts.map +1 -1
- package/dist/components/sidebar/attributes-block.d.ts +5 -5
- package/dist/components/sidebar/attributes-block.d.ts.map +1 -1
- package/dist/components/sidebar/conversation-view.d.ts +1 -1
- package/dist/components/sidebar/conversation-view.d.ts.map +1 -1
- package/dist/components/sidebar/copyable-data-block.d.ts +2 -2
- package/dist/components/sidebar/copyable-data-block.d.ts.map +1 -1
- package/dist/components/sidebar/events-list.d.ts +1 -1
- package/dist/components/sidebar/events-list.d.ts.map +1 -1
- package/dist/components/sidebar/sidebar-data-context.d.ts +1 -1
- package/dist/components/sidebar/sidebar-data-context.d.ts.map +1 -1
- package/dist/components/stream-viewer-skeleton.d.ts +1 -1
- package/dist/components/stream-viewer-skeleton.d.ts.map +1 -1
- package/dist/components/stream-viewer.d.ts +2 -1
- package/dist/components/stream-viewer.d.ts.map +1 -1
- package/dist/components/trace-viewer/components/draggable-border.d.ts +1 -1
- package/dist/components/trace-viewer/components/draggable-border.d.ts.map +1 -1
- package/dist/components/trace-viewer/components/event-list.d.ts +1 -1
- package/dist/components/trace-viewer/components/event-list.d.ts.map +1 -1
- package/dist/components/trace-viewer/components/split-pane.d.ts +1 -1
- package/dist/components/trace-viewer/components/split-pane.d.ts.map +1 -1
- package/dist/components/trace-viewer/components/trace-shortcut-helper.d.ts +1 -1
- package/dist/components/trace-viewer/components/trace-shortcut-helper.d.ts.map +1 -1
- package/dist/components/trace-viewer/components/trace-viewer-skeleton.d.ts +1 -1
- package/dist/components/trace-viewer/components/trace-viewer-skeleton.d.ts.map +1 -1
- package/dist/components/trace-viewer/context.d.ts +1 -1
- package/dist/components/trace-viewer/context.d.ts.map +1 -1
- package/dist/components/trace-viewer/icons.d.ts +4 -4
- package/dist/components/trace-viewer/icons.d.ts.map +1 -1
- package/dist/components/trace-viewer/utils.d.ts.map +1 -1
- package/dist/components/trace-viewer/utils.js +12 -3
- package/dist/components/trace-viewer.d.ts +1 -1
- package/dist/components/trace-viewer.d.ts.map +1 -1
- package/dist/components/ui/collapsible.d.ts +4 -4
- package/dist/components/ui/collapsible.d.ts.map +1 -1
- package/dist/components/ui/decrypt-button.d.ts +1 -1
- package/dist/components/ui/decrypt-button.d.ts.map +1 -1
- package/dist/components/ui/error-card.d.ts +1 -1
- package/dist/components/ui/error-card.d.ts.map +1 -1
- package/dist/components/ui/error-stack-block.d.ts +1 -1
- package/dist/components/ui/error-stack-block.d.ts.map +1 -1
- package/dist/components/ui/load-more-button.d.ts +1 -1
- package/dist/components/ui/load-more-button.d.ts.map +1 -1
- package/dist/components/ui/menu-dropdown.d.ts +1 -1
- package/dist/components/ui/menu-dropdown.d.ts.map +1 -1
- package/dist/components/ui/skeleton.d.ts +1 -1
- package/dist/components/ui/skeleton.d.ts.map +1 -1
- package/dist/components/ui/spinner.d.ts +1 -1
- package/dist/components/ui/spinner.d.ts.map +1 -1
- package/dist/components/workflow-traces/trace-span-construction.d.ts +8 -2
- package/dist/components/workflow-traces/trace-span-construction.d.ts.map +1 -1
- package/dist/components/workflow-traces/trace-span-construction.js +29 -11
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/lib/event-analysis.d.ts.map +1 -1
- package/dist/lib/event-analysis.js +10 -2
- package/dist/lib/event-materialization.d.ts +4 -0
- package/dist/lib/event-materialization.d.ts.map +1 -1
- package/dist/lib/event-materialization.js +25 -7
- package/dist/lib/hydration.d.ts +5 -3
- package/dist/lib/hydration.d.ts.map +1 -1
- package/dist/lib/hydration.js +10 -6
- package/dist/lib/start-hook.d.ts +30 -0
- package/dist/lib/start-hook.d.ts.map +1 -0
- package/dist/lib/start-hook.js +50 -0
- package/dist/lib/state-hydration.d.ts +37 -0
- package/dist/lib/state-hydration.d.ts.map +1 -0
- package/dist/lib/state-hydration.js +148 -0
- package/dist/lib/trace-builder.d.ts.map +1 -1
- package/dist/lib/trace-builder.js +12 -5
- package/package.json +19 -18
- package/src/components/trace-viewer/utils.test.ts +24 -0
- package/src/components/trace-viewer/utils.ts +14 -2
- package/src/components/workflow-traces/trace-span-construction.test.ts +30 -0
- package/src/components/workflow-traces/trace-span-construction.ts +34 -11
- package/src/index.ts +6 -0
- package/src/lib/event-analysis.ts +9 -0
- package/src/lib/event-materialization.ts +26 -7
- package/src/lib/hydration.ts +11 -5
- package/src/lib/start-hook.test.ts +174 -0
- package/src/lib/start-hook.ts +82 -0
- package/src/lib/state-hydration.test.ts +332 -0
- package/src/lib/state-hydration.ts +208 -0
- package/src/lib/trace-builder.ts +17 -2
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
7
|
type Event,
|
|
8
|
+
isRunCreationEvent,
|
|
8
9
|
isTerminalWorkflowRunStatus,
|
|
9
10
|
type WorkflowRunStatus,
|
|
10
11
|
} from '@workflow/world';
|
|
12
|
+
import { findStartHookCreation } from './start-hook';
|
|
11
13
|
|
|
12
14
|
// Time thresholds for Re-enqueue button visibility
|
|
13
15
|
const STEP_ACTIVITY_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
|
|
@@ -115,6 +117,13 @@ export function analyzeEvents(events: Event[] | undefined): EventAnalysis {
|
|
|
115
117
|
}
|
|
116
118
|
}
|
|
117
119
|
|
|
120
|
+
// A start Hook has no `hook_created`: the run's creation stands for it.
|
|
121
|
+
const startHook = findStartHookCreation(events);
|
|
122
|
+
if (startHook && !hookCreated.has(startHook.hookId)) {
|
|
123
|
+
const creationEvent = events.find(isRunCreationEvent);
|
|
124
|
+
if (creationEvent) hookCreated.set(startHook.hookId, creationEvent);
|
|
125
|
+
}
|
|
126
|
+
|
|
118
127
|
// Find pending items (created but not completed)
|
|
119
128
|
const pendingSleepIds = Array.from(waitCreated.keys()).filter(
|
|
120
129
|
(id) => !waitCompleted.has(id)
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
type StepStatus,
|
|
19
19
|
} from '@workflow/world';
|
|
20
20
|
import { findDuplicateEventIds } from './duplicate-events';
|
|
21
|
+
import { findStartHookCreation } from './start-hook';
|
|
21
22
|
|
|
22
23
|
// ---------------------------------------------------------------------------
|
|
23
24
|
// Materialized entity types
|
|
@@ -198,14 +199,35 @@ export function materializeSteps(
|
|
|
198
199
|
|
|
199
200
|
/**
|
|
200
201
|
* Group hook_* events by correlationId and build Hook-like entities.
|
|
202
|
+
*
|
|
203
|
+
* A run's start Hook has no `hook_created`: the run's creation event stands
|
|
204
|
+
* for it (see {@link findStartHookCreation}), so it is materialized from that
|
|
205
|
+
* even before anything is delivered to it.
|
|
201
206
|
*/
|
|
202
207
|
export function materializeHooks(events: Event[]): MaterializedHook[] {
|
|
203
208
|
const groups = groupByCorrelationId(events, isHookLifecycleEventType);
|
|
204
209
|
const hooks: MaterializedHook[] = [];
|
|
205
210
|
|
|
211
|
+
const startHook = findStartHookCreation(events);
|
|
212
|
+
if (startHook && !groups.has(startHook.hookId)) {
|
|
213
|
+
groups.set(startHook.hookId, []);
|
|
214
|
+
}
|
|
215
|
+
|
|
206
216
|
for (const [correlationId, hookEvents] of groups) {
|
|
207
217
|
const created = hookEvents.find((e) => e.eventType === 'hook_created');
|
|
208
|
-
|
|
218
|
+
const creation = created
|
|
219
|
+
? {
|
|
220
|
+
runId: created.runId,
|
|
221
|
+
token:
|
|
222
|
+
created.eventType === 'hook_created'
|
|
223
|
+
? created.eventData?.token
|
|
224
|
+
: undefined,
|
|
225
|
+
createdAt: getEventTimestamp(created) ?? created.createdAt,
|
|
226
|
+
}
|
|
227
|
+
: correlationId === startHook?.hookId
|
|
228
|
+
? startHook
|
|
229
|
+
: undefined;
|
|
230
|
+
if (!creation) continue;
|
|
209
231
|
|
|
210
232
|
const receivedEvents = hookEvents.filter(
|
|
211
233
|
(e) => e.eventType === 'hook_received'
|
|
@@ -215,12 +237,9 @@ export function materializeHooks(events: Event[]): MaterializedHook[] {
|
|
|
215
237
|
|
|
216
238
|
hooks.push({
|
|
217
239
|
hookId: correlationId,
|
|
218
|
-
runId:
|
|
219
|
-
token:
|
|
220
|
-
|
|
221
|
-
? created.eventData?.token
|
|
222
|
-
: undefined,
|
|
223
|
-
createdAt: getEventTimestamp(created) ?? created.createdAt,
|
|
240
|
+
runId: creation.runId,
|
|
241
|
+
token: creation.token,
|
|
242
|
+
createdAt: creation.createdAt,
|
|
224
243
|
receivedCount: receivedEvents.length,
|
|
225
244
|
lastReceivedAt: getEventTimestamp(lastReceived),
|
|
226
245
|
disposedAt: getEventTimestamp(disposed),
|
package/src/lib/hydration.ts
CHANGED
|
@@ -530,9 +530,10 @@ function replaceEncryptedAndExpiredWithMarkers<T>(resource: T): T {
|
|
|
530
530
|
*/
|
|
531
531
|
export async function hydrateResourceIOWithKey<T>(
|
|
532
532
|
resource: T,
|
|
533
|
-
key: Uint8Array
|
|
533
|
+
key: Uint8Array,
|
|
534
|
+
extraRevivers?: Revivers
|
|
534
535
|
): Promise<T> {
|
|
535
|
-
return hydrateResourceIOAsync(resource, key);
|
|
536
|
+
return hydrateResourceIOAsync(resource, key, extraRevivers);
|
|
536
537
|
}
|
|
537
538
|
|
|
538
539
|
/**
|
|
@@ -541,11 +542,14 @@ export async function hydrateResourceIOWithKey<T>(
|
|
|
541
542
|
* This follows the same resource-field mapping as {@link hydrateResourceIO},
|
|
542
543
|
* but can also inflate compressed browser payloads through the registered
|
|
543
544
|
* zstd WASM decoder. When a key is provided, encrypted fields are decrypted
|
|
544
|
-
* first and then inflated/hydrated.
|
|
545
|
+
* first and then inflated/hydrated. `extraRevivers` are merged over the web
|
|
546
|
+
* revivers, for example a run's frozen state revivers (see
|
|
547
|
+
* `createRunStateHydrator`).
|
|
545
548
|
*/
|
|
546
549
|
export async function hydrateResourceIOAsync<T>(
|
|
547
550
|
resource: T,
|
|
548
|
-
key?: Uint8Array
|
|
551
|
+
key?: Uint8Array,
|
|
552
|
+
extraRevivers?: Revivers
|
|
549
553
|
): Promise<T> {
|
|
550
554
|
const { hydrateDataWithKey, deriveRunPayloadKeys } = await import(
|
|
551
555
|
'@workflow/core/serialization-format'
|
|
@@ -563,7 +567,9 @@ export async function hydrateResourceIOAsync<T>(
|
|
|
563
567
|
// run's X25519 scalar in addition to its AES key. Both derive from the same
|
|
564
568
|
// 32 bytes the key-retrieval endpoint returns.
|
|
565
569
|
const cryptoKey = key ? await deriveRunPayloadKeys(key) : undefined;
|
|
566
|
-
const revivers =
|
|
570
|
+
const revivers = extraRevivers
|
|
571
|
+
? { ...getRevivers(), ...extraRevivers }
|
|
572
|
+
: getRevivers();
|
|
567
573
|
|
|
568
574
|
async function hydrateField(value: unknown): Promise<unknown> {
|
|
569
575
|
// Already-hydrated: encrypted marker with stored bytes
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { Event, EventType, WorkflowRun } from '@workflow/world';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { otelTimeToMs } from '../components/workflow-traces/trace-time-utils';
|
|
4
|
+
import { analyzeEvents } from './event-analysis';
|
|
5
|
+
import { materializeHooks } from './event-materialization';
|
|
6
|
+
import { findStartHookCreation } from './start-hook';
|
|
7
|
+
import { buildTrace } from './trace-builder';
|
|
8
|
+
|
|
9
|
+
const RUN_ID = 'wrun_01ARZ3NDEKTSV4RRFFQ69G5FAV';
|
|
10
|
+
const START_HOOK_ID = 'hook_01ARZ3NDEKTSV4RRFFQ69G5FAV';
|
|
11
|
+
const TOKEN = 'start-token';
|
|
12
|
+
const BASE_TIME = Date.parse('2026-01-01T00:00:00.000Z');
|
|
13
|
+
|
|
14
|
+
let slot = 0;
|
|
15
|
+
|
|
16
|
+
function event(
|
|
17
|
+
eventType: EventType,
|
|
18
|
+
at: number,
|
|
19
|
+
{
|
|
20
|
+
correlationId,
|
|
21
|
+
eventData = {},
|
|
22
|
+
}: { correlationId?: string; eventData?: Record<string, unknown> } = {}
|
|
23
|
+
): Event {
|
|
24
|
+
slot += 1;
|
|
25
|
+
return {
|
|
26
|
+
eventId: `evnt_${String(slot).padStart(26, '0')}`,
|
|
27
|
+
runId: RUN_ID,
|
|
28
|
+
eventType,
|
|
29
|
+
correlationId,
|
|
30
|
+
createdAt: new Date(BASE_TIME + at * 1000),
|
|
31
|
+
occurredAt: new Date(BASE_TIME + at * 1000),
|
|
32
|
+
eventData,
|
|
33
|
+
specVersion: 8,
|
|
34
|
+
} as unknown as Event;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const run = {
|
|
38
|
+
runId: RUN_ID,
|
|
39
|
+
workflowName: 'workflow//./workflows/demo//demo',
|
|
40
|
+
status: 'completed',
|
|
41
|
+
createdAt: new Date(BASE_TIME),
|
|
42
|
+
completedAt: new Date(BASE_TIME + 60_000),
|
|
43
|
+
} as unknown as WorkflowRun;
|
|
44
|
+
|
|
45
|
+
/** The shape of a start Hook run's log: deliveries but no `hook_created`. */
|
|
46
|
+
function startHookRunLog(): Event[] {
|
|
47
|
+
slot = 0;
|
|
48
|
+
return [
|
|
49
|
+
event('run_created', 0, {
|
|
50
|
+
correlationId: RUN_ID,
|
|
51
|
+
eventData: { startHook: { token: TOKEN, hookId: START_HOOK_ID } },
|
|
52
|
+
}),
|
|
53
|
+
event('hook_received', 1, { correlationId: START_HOOK_ID }),
|
|
54
|
+
event('run_started', 1),
|
|
55
|
+
event('hook_received', 2, { correlationId: START_HOOK_ID }),
|
|
56
|
+
event('run_completed', 60),
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('start Hook', () => {
|
|
61
|
+
it('builds a span for a start Hook from its deliveries', () => {
|
|
62
|
+
const trace = buildTrace(
|
|
63
|
+
run,
|
|
64
|
+
startHookRunLog(),
|
|
65
|
+
new Date(BASE_TIME + 61_000),
|
|
66
|
+
{
|
|
67
|
+
isCompleteHistory: true,
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const hookSpans = trace.spans.filter((span) => span.resource === 'hook');
|
|
72
|
+
expect(hookSpans).toHaveLength(1);
|
|
73
|
+
const [hookSpan] = hookSpans;
|
|
74
|
+
expect(hookSpan.spanId).toBe(START_HOOK_ID);
|
|
75
|
+
expect(hookSpan.name).toBe(TOKEN);
|
|
76
|
+
expect(otelTimeToMs(hookSpan.startTime)).toBe(BASE_TIME);
|
|
77
|
+
expect(hookSpan.events.map((spanEvent) => spanEvent.name)).toEqual([
|
|
78
|
+
'hook_received',
|
|
79
|
+
'hook_received',
|
|
80
|
+
]);
|
|
81
|
+
expect(hookSpan.attributes.data).toMatchObject({
|
|
82
|
+
hookId: START_HOOK_ID,
|
|
83
|
+
token: TOKEN,
|
|
84
|
+
receivedCount: 2,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('builds a span for a start Hook nothing was delivered to yet', () => {
|
|
89
|
+
slot = 0;
|
|
90
|
+
const events = [
|
|
91
|
+
event('run_created', 0, {
|
|
92
|
+
correlationId: RUN_ID,
|
|
93
|
+
eventData: { startHook: { token: TOKEN, hookId: START_HOOK_ID } },
|
|
94
|
+
}),
|
|
95
|
+
event('run_started', 1),
|
|
96
|
+
];
|
|
97
|
+
const trace = buildTrace(
|
|
98
|
+
{ ...run, status: 'running', completedAt: undefined } as WorkflowRun,
|
|
99
|
+
events,
|
|
100
|
+
new Date(BASE_TIME + 5000)
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
expect(
|
|
104
|
+
trace.spans
|
|
105
|
+
.filter((span) => span.resource === 'hook')
|
|
106
|
+
.map((s) => s.spanId)
|
|
107
|
+
).toEqual([START_HOOK_ID]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('ends a start Hook span at its disposal', () => {
|
|
111
|
+
const events = startHookRunLog();
|
|
112
|
+
events.splice(
|
|
113
|
+
4,
|
|
114
|
+
0,
|
|
115
|
+
event('hook_disposed', 10, { correlationId: START_HOOK_ID })
|
|
116
|
+
);
|
|
117
|
+
const trace = buildTrace(run, events, new Date(BASE_TIME + 61_000));
|
|
118
|
+
const [hookSpan] = trace.spans.filter((span) => span.resource === 'hook');
|
|
119
|
+
|
|
120
|
+
expect(otelTimeToMs(hookSpan.endTime)).toBe(BASE_TIME + 10_000);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('recognizes a start Hook listed without event data by its id', () => {
|
|
124
|
+
const events = startHookRunLog().map(
|
|
125
|
+
(e) => ({ ...e, eventData: undefined }) as unknown as Event
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
expect(findStartHookCreation(events)).toEqual({
|
|
129
|
+
hookId: START_HOOK_ID,
|
|
130
|
+
runId: RUN_ID,
|
|
131
|
+
token: undefined,
|
|
132
|
+
createdAt: new Date(BASE_TIME),
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('finds no start Hook on a run started without one', () => {
|
|
137
|
+
slot = 0;
|
|
138
|
+
const events = [
|
|
139
|
+
event('run_created', 0, { correlationId: RUN_ID }),
|
|
140
|
+
event('run_started', 1),
|
|
141
|
+
event('hook_created', 2, {
|
|
142
|
+
correlationId: 'hook_other',
|
|
143
|
+
eventData: { token: 'other' },
|
|
144
|
+
}),
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
expect(findStartHookCreation(events, run)).toBeUndefined();
|
|
148
|
+
const trace = buildTrace(run, events, new Date(BASE_TIME + 5000));
|
|
149
|
+
expect(
|
|
150
|
+
trace.spans
|
|
151
|
+
.filter((span) => span.resource === 'hook')
|
|
152
|
+
.map((s) => s.spanId)
|
|
153
|
+
).toEqual(['hook_other']);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('materializes a start Hook', () => {
|
|
157
|
+
expect(materializeHooks(startHookRunLog())).toMatchObject([
|
|
158
|
+
{
|
|
159
|
+
hookId: START_HOOK_ID,
|
|
160
|
+
runId: RUN_ID,
|
|
161
|
+
token: TOKEN,
|
|
162
|
+
createdAt: new Date(BASE_TIME),
|
|
163
|
+
receivedCount: 2,
|
|
164
|
+
},
|
|
165
|
+
]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('counts an undisposed start Hook as pending', () => {
|
|
169
|
+
expect(analyzeEvents(startHookRunLog().slice(0, 4))).toMatchObject({
|
|
170
|
+
hasPendingHooks: true,
|
|
171
|
+
pendingHookIds: [START_HOOK_ID],
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Event,
|
|
3
|
+
isHookLifecycleEventType,
|
|
4
|
+
isRunCreationEvent,
|
|
5
|
+
startHookId,
|
|
6
|
+
type WorkflowRun,
|
|
7
|
+
} from '@workflow/world';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The creation of a run's start Hook (`start(workflow, args, { hook })`).
|
|
11
|
+
*
|
|
12
|
+
* A start Hook has no `hook_created` of its own: the World creates it while
|
|
13
|
+
* it admits the run, and the run's creation event (`run_created`, or the
|
|
14
|
+
* `run_started` holding the first slot) stands for it, carrying
|
|
15
|
+
* `eventData.startHook`. Its deliveries are ordinary `hook_received` events
|
|
16
|
+
* under {@link startHookId}`(runId)`, and it ends with an ordinary
|
|
17
|
+
* `hook_disposed`. Anything that rebuilds Hooks from the log has to seed the
|
|
18
|
+
* start Hook from here, or it drops the Hook and every delivery to it.
|
|
19
|
+
*/
|
|
20
|
+
export interface StartHookCreation {
|
|
21
|
+
hookId: string;
|
|
22
|
+
runId: string;
|
|
23
|
+
token?: string;
|
|
24
|
+
createdAt: Date;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function eventTimestamp(event: Event | undefined): Date | undefined {
|
|
28
|
+
const value = event?.occurredAt ?? event?.createdAt;
|
|
29
|
+
if (!value) return undefined;
|
|
30
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
31
|
+
return Number.isNaN(date.getTime()) ? undefined : date;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function tokenOf(startHook: unknown): string | undefined {
|
|
35
|
+
if (!startHook || typeof startHook !== 'object') return undefined;
|
|
36
|
+
const { token } = startHook as { token?: unknown };
|
|
37
|
+
return typeof token === 'string' && token.length > 0 ? token : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The run's start Hook, or `undefined` for a run started without one.
|
|
42
|
+
*
|
|
43
|
+
* Read from the creation event's `eventData.startHook`, falling back to
|
|
44
|
+
* `run.startHook`. Events listed without their data still identify the start
|
|
45
|
+
* Hook by its id, so a run whose log holds an event for
|
|
46
|
+
* {@link startHookId}`(runId)` has one even when no token is known.
|
|
47
|
+
*/
|
|
48
|
+
export function findStartHookCreation(
|
|
49
|
+
events: readonly Event[],
|
|
50
|
+
run?: Pick<WorkflowRun, 'runId' | 'createdAt'> & { startHook?: unknown }
|
|
51
|
+
): StartHookCreation | undefined {
|
|
52
|
+
const creationEvent = events.find(isRunCreationEvent);
|
|
53
|
+
const runId = run?.runId ?? creationEvent?.runId ?? events[0]?.runId;
|
|
54
|
+
if (!runId?.startsWith('wrun_') || runId.length <= 'wrun_'.length) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const hookId = startHookId(runId);
|
|
58
|
+
const token =
|
|
59
|
+
tokenOf(
|
|
60
|
+
creationEvent && 'eventData' in creationEvent
|
|
61
|
+
? (creationEvent.eventData as { startHook?: unknown } | undefined)
|
|
62
|
+
?.startHook
|
|
63
|
+
: undefined
|
|
64
|
+
) ?? tokenOf(run?.startHook);
|
|
65
|
+
const hookEvents = events.filter(
|
|
66
|
+
(event) =>
|
|
67
|
+
event.correlationId === hookId &&
|
|
68
|
+
isHookLifecycleEventType(event.eventType)
|
|
69
|
+
);
|
|
70
|
+
if (token === undefined && hookEvents.length === 0) return undefined;
|
|
71
|
+
// `hook_created` for a start Hook's id is refused by every World, so one in
|
|
72
|
+
// the log means this is not a start Hook after all.
|
|
73
|
+
if (hookEvents.some((event) => event.eventType === 'hook_created')) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const createdAt =
|
|
77
|
+
eventTimestamp(creationEvent) ??
|
|
78
|
+
(run?.createdAt ? new Date(run.createdAt) : undefined) ??
|
|
79
|
+
eventTimestamp(hookEvents[0]);
|
|
80
|
+
if (!createdAt) return undefined;
|
|
81
|
+
return { hookId, runId, token, createdAt };
|
|
82
|
+
}
|