@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
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { produce } from 'immer';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
// The SDK's own writers, so the fixtures are exactly what a run stores.
|
|
4
|
+
import { StateContext } from '../../../core/src/serialization/state-refs';
|
|
5
|
+
import * as workflow from '../../../core/src/serialization/workflow';
|
|
6
|
+
import { hydrateResourceIOAsync } from './hydration';
|
|
7
|
+
import { createRunStateHydrator } from './state-hydration';
|
|
8
|
+
|
|
9
|
+
type Message = { role: string; content: string };
|
|
10
|
+
|
|
11
|
+
/** A run whose second step's input refers to the first step's output. */
|
|
12
|
+
function storedRun() {
|
|
13
|
+
const body = new StateContext();
|
|
14
|
+
const write = (value: unknown) =>
|
|
15
|
+
workflow.serialize(value, { stateContext: body });
|
|
16
|
+
const read = (bytes: Uint8Array) =>
|
|
17
|
+
workflow.deserialize(bytes, { stateContext: body }) as any;
|
|
18
|
+
|
|
19
|
+
const output1 = write({
|
|
20
|
+
history: produce([] as Message[], (d) => {
|
|
21
|
+
d.push({ role: 'user', content: 'hello' });
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
const v1 = read(output1).history;
|
|
25
|
+
const input2 = write({ history: v1 });
|
|
26
|
+
const output2 = write({
|
|
27
|
+
history: produce(v1 as Message[], (d) => {
|
|
28
|
+
d.push({ role: 'assistant', content: 'hi' });
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
31
|
+
const runInput = workflow.serialize(
|
|
32
|
+
{ prompt: 'hello' },
|
|
33
|
+
{ stateContext: new StateContext() }
|
|
34
|
+
);
|
|
35
|
+
const events = [
|
|
36
|
+
{ eventId: 'e1', eventType: 'run_created', eventData: {} },
|
|
37
|
+
{
|
|
38
|
+
eventId: 'e2',
|
|
39
|
+
eventType: 'step_completed',
|
|
40
|
+
eventData: { result: output1 },
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
eventId: 'e3',
|
|
44
|
+
eventType: 'step_completed',
|
|
45
|
+
eventData: { result: output2 },
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
return { runInput, input2, output2, events };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
describe('createRunStateHydrator', () => {
|
|
52
|
+
beforeEach(() => {});
|
|
53
|
+
afterEach(() => {});
|
|
54
|
+
|
|
55
|
+
it('shows the actual input and output of a step, reading the history once', async () => {
|
|
56
|
+
const run = storedRun();
|
|
57
|
+
const loadRunInput = vi.fn(async () => run.runInput);
|
|
58
|
+
const loadEvents = vi.fn(async () => ({
|
|
59
|
+
data: run.events,
|
|
60
|
+
hasMore: false,
|
|
61
|
+
}));
|
|
62
|
+
const hydrator = createRunStateHydrator({ loadRunInput, loadEvents });
|
|
63
|
+
const step = { stepId: 's2', input: run.input2, output: run.output2 };
|
|
64
|
+
|
|
65
|
+
// Without the history the input is only a placeholder.
|
|
66
|
+
const plain = (await hydrateResourceIOAsync(step)) as any;
|
|
67
|
+
expect(plain.input.history).toMatch(/^<state /);
|
|
68
|
+
|
|
69
|
+
const shown = (await hydrator.hydrate(step)) as any;
|
|
70
|
+
expect(shown.input.history).toEqual([{ role: 'user', content: 'hello' }]);
|
|
71
|
+
expect(shown.output.history).toEqual([
|
|
72
|
+
{ role: 'user', content: 'hello' },
|
|
73
|
+
{ role: 'assistant', content: 'hi' },
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
// Later inspections reuse what was read.
|
|
77
|
+
await hydrator.hydrate(step);
|
|
78
|
+
expect(loadEvents).toHaveBeenCalledTimes(1);
|
|
79
|
+
expect(loadRunInput).toHaveBeenCalledTimes(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('stops reading when the events cursor does not advance', async () => {
|
|
83
|
+
const run = storedRun();
|
|
84
|
+
// A misbehaving backend: always more, always the same cursor.
|
|
85
|
+
const loadEvents = vi.fn(async () => ({
|
|
86
|
+
data: [],
|
|
87
|
+
cursor: 'stuck',
|
|
88
|
+
hasMore: true,
|
|
89
|
+
}));
|
|
90
|
+
const hydrator = createRunStateHydrator({
|
|
91
|
+
loadRunInput: async () => run.runInput,
|
|
92
|
+
loadEvents,
|
|
93
|
+
});
|
|
94
|
+
const shown = (await hydrator.hydrate({
|
|
95
|
+
stepId: 's2',
|
|
96
|
+
input: run.input2,
|
|
97
|
+
})) as any;
|
|
98
|
+
expect(shown.input.history).toMatch(/^<state /);
|
|
99
|
+
expect(loadEvents).toHaveBeenCalledTimes(2);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('does not read the history for values that do not need it', async () => {
|
|
103
|
+
const loadEvents = vi.fn();
|
|
104
|
+
const hydrator = createRunStateHydrator({
|
|
105
|
+
loadRunInput: vi.fn(),
|
|
106
|
+
loadEvents,
|
|
107
|
+
});
|
|
108
|
+
const run = storedRun();
|
|
109
|
+
const shown = (await hydrator.hydrate({ input: run.runInput })) as any;
|
|
110
|
+
expect(shown.input).toEqual({ prompt: 'hello' });
|
|
111
|
+
expect(loadEvents).not.toHaveBeenCalled();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('reads only newer events when a later step needs them', async () => {
|
|
115
|
+
const run = storedRun();
|
|
116
|
+
const pages = [
|
|
117
|
+
{ data: run.events.slice(0, 1), cursor: 'c1', hasMore: false },
|
|
118
|
+
{ data: run.events, cursor: 'c2', hasMore: false },
|
|
119
|
+
];
|
|
120
|
+
const loadEvents = vi.fn(async () => pages.shift()!);
|
|
121
|
+
const hydrator = createRunStateHydrator({
|
|
122
|
+
loadRunInput: async () => run.runInput,
|
|
123
|
+
loadEvents,
|
|
124
|
+
});
|
|
125
|
+
const step = { input: run.input2 };
|
|
126
|
+
|
|
127
|
+
// The first read happened before the step that stored the state.
|
|
128
|
+
expect(((await hydrator.hydrate(step)) as any).input.history).toMatch(
|
|
129
|
+
/^<state /
|
|
130
|
+
);
|
|
131
|
+
expect(((await hydrator.hydrate(step)) as any).input.history).toEqual([
|
|
132
|
+
{ role: 'user', content: 'hello' },
|
|
133
|
+
]);
|
|
134
|
+
expect(loadEvents).toHaveBeenNthCalledWith(2, 'c1');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('resolves strings later state takes from a hook payload', async () => {
|
|
138
|
+
const message = 'Weather in Oslo for the next three days, please. '.repeat(
|
|
139
|
+
4
|
|
140
|
+
);
|
|
141
|
+
const hook = workflow.serialize(
|
|
142
|
+
{ message },
|
|
143
|
+
{ stateContext: new StateContext() }
|
|
144
|
+
);
|
|
145
|
+
const replay = new StateContext();
|
|
146
|
+
replay.addSource(
|
|
147
|
+
'hook:evnt_h',
|
|
148
|
+
workflow.deserialize(hook, { stateContext: replay })
|
|
149
|
+
);
|
|
150
|
+
const output = workflow.serialize(
|
|
151
|
+
{
|
|
152
|
+
history: produce([] as Message[], (d) => {
|
|
153
|
+
d.push({ role: 'user', content: message });
|
|
154
|
+
}),
|
|
155
|
+
},
|
|
156
|
+
{ stateContext: replay }
|
|
157
|
+
);
|
|
158
|
+
const v1 = (workflow.deserialize(output, { stateContext: replay }) as any)
|
|
159
|
+
.history;
|
|
160
|
+
const input = workflow.serialize({ history: v1 }, { stateContext: replay });
|
|
161
|
+
const hydrator = createRunStateHydrator({
|
|
162
|
+
loadRunInput: async () => undefined,
|
|
163
|
+
loadEvents: async () => ({
|
|
164
|
+
data: [
|
|
165
|
+
{
|
|
166
|
+
eventId: 'evnt_h',
|
|
167
|
+
eventType: 'hook_received',
|
|
168
|
+
eventData: { payload: hook },
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
eventId: 'e2',
|
|
172
|
+
eventType: 'step_completed',
|
|
173
|
+
eventData: { result: output },
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
hasMore: false,
|
|
177
|
+
}),
|
|
178
|
+
});
|
|
179
|
+
const shown = (await hydrator.hydrate({ input })) as any;
|
|
180
|
+
expect(shown.input.history).toEqual([{ role: 'user', content: message }]);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('reads only as much of a long run as the inspected step needs', async () => {
|
|
184
|
+
const writer = new StateContext();
|
|
185
|
+
let history: readonly unknown[] = Object.freeze([]);
|
|
186
|
+
const events: { eventId: string; eventType: string; eventData: unknown }[] =
|
|
187
|
+
[];
|
|
188
|
+
const inputs: Uint8Array[] = [];
|
|
189
|
+
for (let i = 0; i < 2_000; i++) {
|
|
190
|
+
inputs.push(
|
|
191
|
+
workflow.serialize(
|
|
192
|
+
{ history },
|
|
193
|
+
{ stateContext: writer, stateInput: true }
|
|
194
|
+
)
|
|
195
|
+
);
|
|
196
|
+
const next = Object.freeze([
|
|
197
|
+
...history,
|
|
198
|
+
Object.freeze({ role: 'user', content: `message ${i}` }),
|
|
199
|
+
]);
|
|
200
|
+
const result = workflow.serialize(
|
|
201
|
+
{ history: next },
|
|
202
|
+
{ stateContext: writer }
|
|
203
|
+
);
|
|
204
|
+
history = (workflow.deserialize(result, { stateContext: writer }) as any)
|
|
205
|
+
.history;
|
|
206
|
+
events.push({
|
|
207
|
+
eventId: `e${i}`,
|
|
208
|
+
eventType: 'step_completed',
|
|
209
|
+
eventData: { result },
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
const PAGE = 100;
|
|
213
|
+
let pages = 0;
|
|
214
|
+
const hydrator = createRunStateHydrator({
|
|
215
|
+
loadRunInput: async () => undefined,
|
|
216
|
+
loadEvents: async (cursor) => {
|
|
217
|
+
pages++;
|
|
218
|
+
const from = cursor ? Number(cursor) : 0;
|
|
219
|
+
const to = from + PAGE;
|
|
220
|
+
return {
|
|
221
|
+
data: events.slice(from, to),
|
|
222
|
+
cursor: String(to),
|
|
223
|
+
hasMore: to < events.length,
|
|
224
|
+
};
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// Step 51's input refers to step 50's output: the first page has it.
|
|
229
|
+
let started = performance.now();
|
|
230
|
+
const early = (await hydrator.hydrate({ input: inputs[51] })) as any;
|
|
231
|
+
const earlyMs = performance.now() - started;
|
|
232
|
+
expect(early.input.history).toHaveLength(51);
|
|
233
|
+
expect(pages).toBe(1);
|
|
234
|
+
|
|
235
|
+
// The last step needs the whole run: read on from where the first stopped.
|
|
236
|
+
started = performance.now();
|
|
237
|
+
const late = (await hydrator.hydrate({ input: inputs[1_999] })) as any;
|
|
238
|
+
const lateMs = performance.now() - started;
|
|
239
|
+
expect(late.input.history).toHaveLength(1_999);
|
|
240
|
+
expect(pages).toBe(20);
|
|
241
|
+
// ~10 ms and ~250 ms on a laptop.
|
|
242
|
+
expect(earlyMs).toBeLessThan(lateMs);
|
|
243
|
+
expect(lateMs).toBeLessThan(10_000);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('reads on for every concurrent inspection that needs a later page', async () => {
|
|
247
|
+
const writer = new StateContext();
|
|
248
|
+
let history: readonly unknown[] = Object.freeze([]);
|
|
249
|
+
const events: { eventId: string; eventType: string; eventData: unknown }[] =
|
|
250
|
+
[];
|
|
251
|
+
const inputs: Uint8Array[] = [];
|
|
252
|
+
for (let i = 0; i < 400; i++) {
|
|
253
|
+
inputs.push(
|
|
254
|
+
workflow.serialize(
|
|
255
|
+
{ history },
|
|
256
|
+
{ stateContext: writer, stateInput: true }
|
|
257
|
+
)
|
|
258
|
+
);
|
|
259
|
+
const next = Object.freeze([
|
|
260
|
+
...history,
|
|
261
|
+
Object.freeze({ role: 'user', content: `message ${i}` }),
|
|
262
|
+
]);
|
|
263
|
+
const result = workflow.serialize(
|
|
264
|
+
{ history: next },
|
|
265
|
+
{ stateContext: writer }
|
|
266
|
+
);
|
|
267
|
+
history = (workflow.deserialize(result, { stateContext: writer }) as any)
|
|
268
|
+
.history;
|
|
269
|
+
events.push({
|
|
270
|
+
eventId: `e${i}`,
|
|
271
|
+
eventType: 'step_completed',
|
|
272
|
+
eventData: { result },
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
const hydrator = createRunStateHydrator({
|
|
276
|
+
loadRunInput: async () => undefined,
|
|
277
|
+
loadEvents: async (cursor) => {
|
|
278
|
+
const from = cursor ? Number(cursor) : 0;
|
|
279
|
+
const to = from + 100;
|
|
280
|
+
return {
|
|
281
|
+
data: events.slice(from, to),
|
|
282
|
+
cursor: String(to),
|
|
283
|
+
hasMore: to < events.length,
|
|
284
|
+
};
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
// Each needs a later page than the one before it; started together, the
|
|
288
|
+
// later ones first wait on a read that stops early.
|
|
289
|
+
const results = (await Promise.all(
|
|
290
|
+
[51, 151, 251, 351].map((i) => hydrator.hydrate({ input: inputs[i] }))
|
|
291
|
+
)) as any[];
|
|
292
|
+
expect(results.map((r) => r.input.history.length)).toEqual([
|
|
293
|
+
51, 151, 251, 351,
|
|
294
|
+
]);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("rebuilds the value when the run input can't be loaded", async () => {
|
|
298
|
+
const writer = new StateContext();
|
|
299
|
+
let history: readonly unknown[] = Object.freeze([]);
|
|
300
|
+
const events: { eventId: string; eventType: string; eventData: unknown }[] =
|
|
301
|
+
[];
|
|
302
|
+
for (let i = 0; i < 3; i++) {
|
|
303
|
+
const next = Object.freeze([
|
|
304
|
+
...history,
|
|
305
|
+
Object.freeze({ role: 'user', content: `message ${i}` }),
|
|
306
|
+
]);
|
|
307
|
+
const result = workflow.serialize(
|
|
308
|
+
{ history: next },
|
|
309
|
+
{ stateContext: writer }
|
|
310
|
+
);
|
|
311
|
+
history = (workflow.deserialize(result, { stateContext: writer }) as any)
|
|
312
|
+
.history;
|
|
313
|
+
events.push({
|
|
314
|
+
eventId: `e${i}`,
|
|
315
|
+
eventType: 'step_completed',
|
|
316
|
+
eventData: { result },
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
const input = workflow.serialize(
|
|
320
|
+
{ history },
|
|
321
|
+
{ stateContext: writer, stateInput: true }
|
|
322
|
+
);
|
|
323
|
+
const hydrator = createRunStateHydrator({
|
|
324
|
+
loadRunInput: async () => {
|
|
325
|
+
throw new Error('run not found');
|
|
326
|
+
},
|
|
327
|
+
loadEvents: async () => ({ data: events, hasMore: false }),
|
|
328
|
+
});
|
|
329
|
+
const shown = (await hydrator.hydrate({ input })) as any;
|
|
330
|
+
expect(shown.input.history).toHaveLength(3);
|
|
331
|
+
});
|
|
332
|
+
});
|
|
@@ -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
|
+
}
|
package/src/lib/trace-builder.ts
CHANGED
|
@@ -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
|
-
|
|
155
|
-
|
|
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);
|