@mastra/inngest 0.0.0-just-snapshot-20251014192224 → 0.0.0-main-test-05-11-2025-2-20251105214713
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/CHANGELOG.md +209 -3
- package/dist/index.cjs +378 -392
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +72 -69
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +379 -393
- package/dist/index.js.map +1 -1
- package/package.json +13 -10
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var crypto = require('crypto');
|
|
4
|
+
var web = require('stream/web');
|
|
4
5
|
var realtime = require('@inngest/realtime');
|
|
5
|
-
var aiTracing = require('@mastra/core/ai-tracing');
|
|
6
6
|
var di = require('@mastra/core/di');
|
|
7
|
+
var observability = require('@mastra/core/observability');
|
|
8
|
+
var stream = require('@mastra/core/stream');
|
|
7
9
|
var tools = require('@mastra/core/tools');
|
|
8
10
|
var workflows = require('@mastra/core/workflows');
|
|
9
11
|
var _constants = require('@mastra/core/workflows/_constants');
|
|
@@ -18,7 +20,7 @@ function serve({
|
|
|
18
20
|
functions: userFunctions = [],
|
|
19
21
|
registerOptions
|
|
20
22
|
}) {
|
|
21
|
-
const wfs = mastra.
|
|
23
|
+
const wfs = mastra.listWorkflows();
|
|
22
24
|
const workflowFunctions = Array.from(
|
|
23
25
|
new Set(
|
|
24
26
|
Object.values(wfs).flatMap((wf) => {
|
|
@@ -57,11 +59,12 @@ var InngestRun = class extends workflows.Run {
|
|
|
57
59
|
}
|
|
58
60
|
async getRunOutput(eventId) {
|
|
59
61
|
let runs = await this.getRuns(eventId);
|
|
62
|
+
const storage = this.#mastra?.getStorage();
|
|
60
63
|
while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
|
|
61
64
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
62
65
|
runs = await this.getRuns(eventId);
|
|
63
66
|
if (runs?.[0]?.status === "Failed") {
|
|
64
|
-
const snapshot = await
|
|
67
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
65
68
|
workflowName: this.workflowId,
|
|
66
69
|
runId: this.runId
|
|
67
70
|
});
|
|
@@ -70,7 +73,7 @@ var InngestRun = class extends workflows.Run {
|
|
|
70
73
|
};
|
|
71
74
|
}
|
|
72
75
|
if (runs?.[0]?.status === "Cancelled") {
|
|
73
|
-
const snapshot = await
|
|
76
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
74
77
|
workflowName: this.workflowId,
|
|
75
78
|
runId: this.runId
|
|
76
79
|
});
|
|
@@ -79,25 +82,20 @@ var InngestRun = class extends workflows.Run {
|
|
|
79
82
|
}
|
|
80
83
|
return runs?.[0];
|
|
81
84
|
}
|
|
82
|
-
async sendEvent(event, data) {
|
|
83
|
-
await this.inngest.send({
|
|
84
|
-
name: `user-event-${event}`,
|
|
85
|
-
data
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
85
|
async cancel() {
|
|
86
|
+
const storage = this.#mastra?.getStorage();
|
|
89
87
|
await this.inngest.send({
|
|
90
88
|
name: `cancel.workflow.${this.workflowId}`,
|
|
91
89
|
data: {
|
|
92
90
|
runId: this.runId
|
|
93
91
|
}
|
|
94
92
|
});
|
|
95
|
-
const snapshot = await
|
|
93
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
96
94
|
workflowName: this.workflowId,
|
|
97
95
|
runId: this.runId
|
|
98
96
|
});
|
|
99
97
|
if (snapshot) {
|
|
100
|
-
await
|
|
98
|
+
await storage?.persistWorkflowSnapshot({
|
|
101
99
|
workflowName: this.workflowId,
|
|
102
100
|
runId: this.runId,
|
|
103
101
|
resourceId: this.resourceId,
|
|
@@ -108,9 +106,15 @@ var InngestRun = class extends workflows.Run {
|
|
|
108
106
|
});
|
|
109
107
|
}
|
|
110
108
|
}
|
|
111
|
-
async start({
|
|
109
|
+
async start(params) {
|
|
110
|
+
return this._start(params);
|
|
111
|
+
}
|
|
112
|
+
async _start({
|
|
112
113
|
inputData,
|
|
113
|
-
initialState
|
|
114
|
+
initialState,
|
|
115
|
+
outputOptions,
|
|
116
|
+
tracingOptions,
|
|
117
|
+
format
|
|
114
118
|
}) {
|
|
115
119
|
await this.#mastra.getStorage()?.persistWorkflowSnapshot({
|
|
116
120
|
workflowName: this.workflowId,
|
|
@@ -123,6 +127,7 @@ var InngestRun = class extends workflows.Run {
|
|
|
123
127
|
context: {},
|
|
124
128
|
activePaths: [],
|
|
125
129
|
suspendedPaths: {},
|
|
130
|
+
resumeLabels: {},
|
|
126
131
|
waitingPaths: {},
|
|
127
132
|
timestamp: Date.now(),
|
|
128
133
|
status: "running"
|
|
@@ -136,7 +141,10 @@ var InngestRun = class extends workflows.Run {
|
|
|
136
141
|
inputData: inputDataToUse,
|
|
137
142
|
initialState: initialStateToUse,
|
|
138
143
|
runId: this.runId,
|
|
139
|
-
resourceId: this.resourceId
|
|
144
|
+
resourceId: this.resourceId,
|
|
145
|
+
outputOptions,
|
|
146
|
+
tracingOptions,
|
|
147
|
+
format
|
|
140
148
|
}
|
|
141
149
|
});
|
|
142
150
|
const eventId = eventOutput.ids[0];
|
|
@@ -165,10 +173,11 @@ var InngestRun = class extends workflows.Run {
|
|
|
165
173
|
return p;
|
|
166
174
|
}
|
|
167
175
|
async _resume(params) {
|
|
176
|
+
const storage = this.#mastra?.getStorage();
|
|
168
177
|
const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
169
178
|
(step) => typeof step === "string" ? step : step?.id
|
|
170
179
|
);
|
|
171
|
-
const snapshot = await
|
|
180
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
172
181
|
workflowName: this.workflowId,
|
|
173
182
|
runId: this.runId
|
|
174
183
|
});
|
|
@@ -202,12 +211,12 @@ var InngestRun = class extends workflows.Run {
|
|
|
202
211
|
}
|
|
203
212
|
return result;
|
|
204
213
|
}
|
|
205
|
-
watch(cb
|
|
214
|
+
watch(cb) {
|
|
206
215
|
let active = true;
|
|
207
216
|
const streamPromise = realtime.subscribe(
|
|
208
217
|
{
|
|
209
218
|
channel: `workflow:${this.workflowId}:${this.runId}`,
|
|
210
|
-
topics: [
|
|
219
|
+
topics: ["watch"],
|
|
211
220
|
app: this.inngest
|
|
212
221
|
},
|
|
213
222
|
(message) => {
|
|
@@ -225,20 +234,35 @@ var InngestRun = class extends workflows.Run {
|
|
|
225
234
|
});
|
|
226
235
|
};
|
|
227
236
|
}
|
|
228
|
-
|
|
237
|
+
streamLegacy({ inputData, requestContext } = {}) {
|
|
229
238
|
const { readable, writable } = new TransformStream();
|
|
230
239
|
const writer = writable.getWriter();
|
|
231
240
|
const unwatch = this.watch(async (event) => {
|
|
232
241
|
try {
|
|
242
|
+
await writer.write({
|
|
243
|
+
// @ts-ignore
|
|
244
|
+
type: "start",
|
|
245
|
+
// @ts-ignore
|
|
246
|
+
payload: { runId: this.runId }
|
|
247
|
+
});
|
|
233
248
|
const e = {
|
|
234
249
|
...event,
|
|
235
250
|
type: event.type.replace("workflow-", "")
|
|
236
251
|
};
|
|
252
|
+
if (e.type === "step-output") {
|
|
253
|
+
e.type = e.payload.output.type;
|
|
254
|
+
e.payload = e.payload.output.payload;
|
|
255
|
+
}
|
|
237
256
|
await writer.write(e);
|
|
238
257
|
} catch {
|
|
239
258
|
}
|
|
240
|
-
}
|
|
259
|
+
});
|
|
241
260
|
this.closeStreamAction = async () => {
|
|
261
|
+
await writer.write({
|
|
262
|
+
type: "finish",
|
|
263
|
+
// @ts-ignore
|
|
264
|
+
payload: { runId: this.runId }
|
|
265
|
+
});
|
|
242
266
|
unwatch();
|
|
243
267
|
try {
|
|
244
268
|
await writer.close();
|
|
@@ -248,7 +272,7 @@ var InngestRun = class extends workflows.Run {
|
|
|
248
272
|
writer.releaseLock();
|
|
249
273
|
}
|
|
250
274
|
};
|
|
251
|
-
this.executionResults = this.
|
|
275
|
+
this.executionResults = this._start({ inputData, requestContext, format: "legacy" }).then((result) => {
|
|
252
276
|
if (result.status !== "suspended") {
|
|
253
277
|
this.closeStreamAction?.().catch(() => {
|
|
254
278
|
});
|
|
@@ -260,6 +284,82 @@ var InngestRun = class extends workflows.Run {
|
|
|
260
284
|
getWorkflowState: () => this.executionResults
|
|
261
285
|
};
|
|
262
286
|
}
|
|
287
|
+
stream({
|
|
288
|
+
inputData,
|
|
289
|
+
requestContext,
|
|
290
|
+
tracingOptions,
|
|
291
|
+
closeOnSuspend = true,
|
|
292
|
+
initialState,
|
|
293
|
+
outputOptions
|
|
294
|
+
} = {}) {
|
|
295
|
+
if (this.closeStreamAction && this.streamOutput) {
|
|
296
|
+
return this.streamOutput;
|
|
297
|
+
}
|
|
298
|
+
this.closeStreamAction = async () => {
|
|
299
|
+
};
|
|
300
|
+
const self = this;
|
|
301
|
+
const stream$1 = new web.ReadableStream({
|
|
302
|
+
async start(controller) {
|
|
303
|
+
const unwatch = self.watch(async ({ type, from = stream.ChunkFrom.WORKFLOW, payload }) => {
|
|
304
|
+
controller.enqueue({
|
|
305
|
+
type,
|
|
306
|
+
runId: self.runId,
|
|
307
|
+
from,
|
|
308
|
+
payload: {
|
|
309
|
+
stepName: payload?.id,
|
|
310
|
+
...payload
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
self.closeStreamAction = async () => {
|
|
315
|
+
unwatch();
|
|
316
|
+
try {
|
|
317
|
+
await controller.close();
|
|
318
|
+
} catch (err) {
|
|
319
|
+
console.error("Error closing stream:", err);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
const executionResultsPromise = self._start({
|
|
323
|
+
inputData,
|
|
324
|
+
requestContext,
|
|
325
|
+
// tracingContext, // We are not able to pass a reference to a span here, what to do?
|
|
326
|
+
initialState,
|
|
327
|
+
tracingOptions,
|
|
328
|
+
outputOptions,
|
|
329
|
+
format: "vnext"
|
|
330
|
+
});
|
|
331
|
+
let executionResults;
|
|
332
|
+
try {
|
|
333
|
+
executionResults = await executionResultsPromise;
|
|
334
|
+
if (closeOnSuspend) {
|
|
335
|
+
self.closeStreamAction?.().catch(() => {
|
|
336
|
+
});
|
|
337
|
+
} else if (executionResults.status !== "suspended") {
|
|
338
|
+
self.closeStreamAction?.().catch(() => {
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
if (self.streamOutput) {
|
|
342
|
+
self.streamOutput.updateResults(
|
|
343
|
+
executionResults
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
} catch (err) {
|
|
347
|
+
self.streamOutput?.rejectResults(err);
|
|
348
|
+
self.closeStreamAction?.().catch(() => {
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
this.streamOutput = new stream.WorkflowRunOutput({
|
|
354
|
+
runId: this.runId,
|
|
355
|
+
workflowId: this.workflowId,
|
|
356
|
+
stream: stream$1
|
|
357
|
+
});
|
|
358
|
+
return this.streamOutput;
|
|
359
|
+
}
|
|
360
|
+
streamVNext(args = {}) {
|
|
361
|
+
return this.stream(args);
|
|
362
|
+
}
|
|
263
363
|
};
|
|
264
364
|
var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
265
365
|
#mastra;
|
|
@@ -276,13 +376,13 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
276
376
|
this.#mastra = params.mastra;
|
|
277
377
|
this.inngest = inngest;
|
|
278
378
|
}
|
|
279
|
-
async
|
|
379
|
+
async listWorkflowRuns(args) {
|
|
280
380
|
const storage = this.#mastra?.getStorage();
|
|
281
381
|
if (!storage) {
|
|
282
382
|
this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
|
|
283
383
|
return { runs: [], total: 0 };
|
|
284
384
|
}
|
|
285
|
-
return storage.
|
|
385
|
+
return storage.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
|
|
286
386
|
}
|
|
287
387
|
async getWorkflowRunById(runId) {
|
|
288
388
|
const storage = this.#mastra?.getStorage();
|
|
@@ -311,16 +411,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
311
411
|
}
|
|
312
412
|
}
|
|
313
413
|
}
|
|
314
|
-
|
|
315
|
-
* @deprecated Use createRunAsync() instead.
|
|
316
|
-
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
317
|
-
*/
|
|
318
|
-
createRun(_options) {
|
|
319
|
-
throw new Error(
|
|
320
|
-
"createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = workflow.createRun();\n After: const run = await workflow.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
|
|
321
|
-
);
|
|
322
|
-
}
|
|
323
|
-
async createRunAsync(options) {
|
|
414
|
+
async createRun(options) {
|
|
324
415
|
const runIdToUse = options?.runId || crypto.randomUUID();
|
|
325
416
|
const run = this.runs.get(runIdToUse) ?? new InngestRun(
|
|
326
417
|
{
|
|
@@ -357,6 +448,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
357
448
|
waitingPaths: {},
|
|
358
449
|
serializedStepGraph: this.serializedStepGraph,
|
|
359
450
|
suspendedPaths: {},
|
|
451
|
+
resumeLabels: {},
|
|
360
452
|
result: void 0,
|
|
361
453
|
error: void 0,
|
|
362
454
|
// @ts-ignore
|
|
@@ -381,7 +473,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
381
473
|
},
|
|
382
474
|
{ event: `workflow.${this.id}` },
|
|
383
475
|
async ({ event, step, attempt, publish }) => {
|
|
384
|
-
let { inputData, initialState, runId, resourceId, resume, outputOptions } = event.data;
|
|
476
|
+
let { inputData, initialState, runId, resourceId, resume, outputOptions, format } = event.data;
|
|
385
477
|
if (!runId) {
|
|
386
478
|
runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
|
|
387
479
|
return crypto.randomUUID();
|
|
@@ -420,13 +512,19 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
420
512
|
initialState,
|
|
421
513
|
emitter,
|
|
422
514
|
retryConfig: this.retryConfig,
|
|
423
|
-
|
|
515
|
+
requestContext: new di.RequestContext(),
|
|
424
516
|
// TODO
|
|
425
517
|
resume,
|
|
518
|
+
format,
|
|
426
519
|
abortController: new AbortController(),
|
|
427
|
-
currentSpan:
|
|
428
|
-
|
|
429
|
-
|
|
520
|
+
// currentSpan: undefined, // TODO: Pass actual parent Span from workflow execution context
|
|
521
|
+
outputOptions,
|
|
522
|
+
writableStream: new WritableStream({
|
|
523
|
+
write(chunk) {
|
|
524
|
+
void emitter.emit("watch", chunk).catch(() => {
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
})
|
|
430
528
|
});
|
|
431
529
|
await step.run(`workflow.${this.id}.finalize`, async () => {
|
|
432
530
|
if (result.status === "failed") {
|
|
@@ -464,7 +562,7 @@ function isAgent(params) {
|
|
|
464
562
|
function isTool(params) {
|
|
465
563
|
return params instanceof tools.Tool;
|
|
466
564
|
}
|
|
467
|
-
function createStep(params) {
|
|
565
|
+
function createStep(params, agentOptions) {
|
|
468
566
|
if (isAgent(params)) {
|
|
469
567
|
return {
|
|
470
568
|
id: params.name,
|
|
@@ -472,12 +570,23 @@ function createStep(params) {
|
|
|
472
570
|
// @ts-ignore
|
|
473
571
|
inputSchema: zod.z.object({
|
|
474
572
|
prompt: zod.z.string()
|
|
573
|
+
// resourceId: z.string().optional(),
|
|
574
|
+
// threadId: z.string().optional(),
|
|
475
575
|
}),
|
|
476
576
|
// @ts-ignore
|
|
477
577
|
outputSchema: zod.z.object({
|
|
478
578
|
text: zod.z.string()
|
|
479
579
|
}),
|
|
480
|
-
execute: async ({
|
|
580
|
+
execute: async ({
|
|
581
|
+
inputData,
|
|
582
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
583
|
+
[_constants.STREAM_FORMAT_SYMBOL]: streamFormat,
|
|
584
|
+
requestContext,
|
|
585
|
+
tracingContext,
|
|
586
|
+
abortSignal,
|
|
587
|
+
abort,
|
|
588
|
+
writer
|
|
589
|
+
}) => {
|
|
481
590
|
let streamPromise = {};
|
|
482
591
|
streamPromise.promise = new Promise((resolve, reject) => {
|
|
483
592
|
streamPromise.resolve = resolve;
|
|
@@ -487,61 +596,60 @@ function createStep(params) {
|
|
|
487
596
|
name: params.name,
|
|
488
597
|
args: inputData
|
|
489
598
|
};
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
599
|
+
let stream;
|
|
600
|
+
if ((await params.getModel()).specificationVersion === "v1") {
|
|
601
|
+
const { fullStream } = await params.streamLegacy(inputData.prompt, {
|
|
602
|
+
...agentOptions ?? {},
|
|
603
|
+
// resourceId: inputData.resourceId,
|
|
604
|
+
// threadId: inputData.threadId,
|
|
605
|
+
requestContext,
|
|
493
606
|
tracingContext,
|
|
494
607
|
onFinish: (result) => {
|
|
495
608
|
streamPromise.resolve(result.text);
|
|
609
|
+
void agentOptions?.onFinish?.(result);
|
|
496
610
|
},
|
|
497
611
|
abortSignal
|
|
498
612
|
});
|
|
499
|
-
|
|
500
|
-
return abort();
|
|
501
|
-
}
|
|
502
|
-
await emitter.emit("watch-v2", {
|
|
503
|
-
type: "tool-call-streaming-start",
|
|
504
|
-
...toolData ?? {}
|
|
505
|
-
});
|
|
506
|
-
for await (const chunk of fullStream) {
|
|
507
|
-
if (chunk.type === "text-delta") {
|
|
508
|
-
await emitter.emit("watch-v2", {
|
|
509
|
-
type: "tool-call-delta",
|
|
510
|
-
...toolData ?? {},
|
|
511
|
-
argsTextDelta: chunk.payload.text
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
}
|
|
613
|
+
stream = fullStream;
|
|
515
614
|
} else {
|
|
516
|
-
const
|
|
517
|
-
|
|
615
|
+
const modelOutput = await params.stream(inputData.prompt, {
|
|
616
|
+
...agentOptions ?? {},
|
|
617
|
+
requestContext,
|
|
518
618
|
tracingContext,
|
|
519
619
|
onFinish: (result) => {
|
|
520
620
|
streamPromise.resolve(result.text);
|
|
621
|
+
void agentOptions?.onFinish?.(result);
|
|
521
622
|
},
|
|
522
623
|
abortSignal
|
|
523
624
|
});
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
await emitter.emit("watch
|
|
625
|
+
stream = modelOutput.fullStream;
|
|
626
|
+
}
|
|
627
|
+
if (streamFormat === "legacy") {
|
|
628
|
+
await emitter.emit("watch", {
|
|
528
629
|
type: "tool-call-streaming-start",
|
|
529
630
|
...toolData ?? {}
|
|
530
631
|
});
|
|
531
|
-
for await (const chunk of
|
|
632
|
+
for await (const chunk of stream) {
|
|
532
633
|
if (chunk.type === "text-delta") {
|
|
533
|
-
await emitter.emit("watch
|
|
634
|
+
await emitter.emit("watch", {
|
|
534
635
|
type: "tool-call-delta",
|
|
535
636
|
...toolData ?? {},
|
|
536
637
|
argsTextDelta: chunk.textDelta
|
|
537
638
|
});
|
|
538
639
|
}
|
|
539
640
|
}
|
|
641
|
+
await emitter.emit("watch", {
|
|
642
|
+
type: "tool-call-streaming-finish",
|
|
643
|
+
...toolData ?? {}
|
|
644
|
+
});
|
|
645
|
+
} else {
|
|
646
|
+
for await (const chunk of stream) {
|
|
647
|
+
await writer.write(chunk);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
if (abortSignal.aborted) {
|
|
651
|
+
return abort();
|
|
540
652
|
}
|
|
541
|
-
await emitter.emit("watch-v2", {
|
|
542
|
-
type: "tool-call-streaming-finish",
|
|
543
|
-
...toolData ?? {}
|
|
544
|
-
});
|
|
545
653
|
return {
|
|
546
654
|
text: await streamPromise.promise
|
|
547
655
|
};
|
|
@@ -560,11 +668,11 @@ function createStep(params) {
|
|
|
560
668
|
description: params.description,
|
|
561
669
|
inputSchema: params.inputSchema,
|
|
562
670
|
outputSchema: params.outputSchema,
|
|
563
|
-
execute: async ({ inputData, mastra,
|
|
671
|
+
execute: async ({ inputData, mastra, requestContext, tracingContext, suspend, resumeData }) => {
|
|
564
672
|
return params.execute({
|
|
565
673
|
context: inputData,
|
|
566
|
-
mastra:
|
|
567
|
-
|
|
674
|
+
mastra: observability.wrapMastra(mastra, tracingContext),
|
|
675
|
+
requestContext,
|
|
568
676
|
tracingContext,
|
|
569
677
|
suspend,
|
|
570
678
|
resumeData
|
|
@@ -627,63 +735,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
627
735
|
this.inngestStep = inngestStep;
|
|
628
736
|
this.inngestAttempts = inngestAttempts;
|
|
629
737
|
}
|
|
630
|
-
async
|
|
631
|
-
await params.emitter.emit("watch-v2", {
|
|
632
|
-
type: "workflow-start",
|
|
633
|
-
payload: { runId: params.runId }
|
|
634
|
-
});
|
|
635
|
-
const result = await super.execute(params);
|
|
636
|
-
await params.emitter.emit("watch-v2", {
|
|
637
|
-
type: "workflow-finish",
|
|
638
|
-
payload: { runId: params.runId }
|
|
639
|
-
});
|
|
640
|
-
return result;
|
|
641
|
-
}
|
|
642
|
-
async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
|
|
738
|
+
async fmtReturnValue(emitter, stepResults, lastOutput, error) {
|
|
643
739
|
const base = {
|
|
644
740
|
status: lastOutput.status,
|
|
645
741
|
steps: stepResults
|
|
646
742
|
};
|
|
647
743
|
if (lastOutput.status === "success") {
|
|
648
|
-
await emitter.emit("watch", {
|
|
649
|
-
type: "watch",
|
|
650
|
-
payload: {
|
|
651
|
-
workflowState: {
|
|
652
|
-
status: lastOutput.status,
|
|
653
|
-
steps: stepResults,
|
|
654
|
-
result: lastOutput.output
|
|
655
|
-
}
|
|
656
|
-
},
|
|
657
|
-
eventTimestamp: Date.now()
|
|
658
|
-
});
|
|
659
744
|
base.result = lastOutput.output;
|
|
660
745
|
} else if (lastOutput.status === "failed") {
|
|
661
746
|
base.error = error instanceof Error ? error?.stack ?? error.message : lastOutput?.error instanceof Error ? lastOutput.error.message : lastOutput.error ?? error ?? "Unknown error";
|
|
662
|
-
await emitter.emit("watch", {
|
|
663
|
-
type: "watch",
|
|
664
|
-
payload: {
|
|
665
|
-
workflowState: {
|
|
666
|
-
status: lastOutput.status,
|
|
667
|
-
steps: stepResults,
|
|
668
|
-
result: null,
|
|
669
|
-
error: base.error
|
|
670
|
-
}
|
|
671
|
-
},
|
|
672
|
-
eventTimestamp: Date.now()
|
|
673
|
-
});
|
|
674
747
|
} else if (lastOutput.status === "suspended") {
|
|
675
|
-
await emitter.emit("watch", {
|
|
676
|
-
type: "watch",
|
|
677
|
-
payload: {
|
|
678
|
-
workflowState: {
|
|
679
|
-
status: lastOutput.status,
|
|
680
|
-
steps: stepResults,
|
|
681
|
-
result: null,
|
|
682
|
-
error: null
|
|
683
|
-
}
|
|
684
|
-
},
|
|
685
|
-
eventTimestamp: Date.now()
|
|
686
|
-
});
|
|
687
748
|
const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
|
|
688
749
|
if (stepResult?.status === "suspended") {
|
|
689
750
|
const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
|
|
@@ -693,7 +754,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
693
754
|
});
|
|
694
755
|
base.suspended = suspendedStepIds;
|
|
695
756
|
}
|
|
696
|
-
executionSpan?.end();
|
|
697
757
|
return base;
|
|
698
758
|
}
|
|
699
759
|
// async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
|
|
@@ -707,14 +767,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
707
767
|
stepResults,
|
|
708
768
|
emitter,
|
|
709
769
|
abortController,
|
|
710
|
-
|
|
770
|
+
requestContext,
|
|
711
771
|
executionContext,
|
|
712
772
|
writableStream,
|
|
713
773
|
tracingContext
|
|
714
774
|
}) {
|
|
715
775
|
let { duration, fn } = entry;
|
|
716
776
|
const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
717
|
-
type:
|
|
777
|
+
type: observability.SpanType.WORKFLOW_SLEEP,
|
|
718
778
|
name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
|
|
719
779
|
attributes: {
|
|
720
780
|
durationMs: duration,
|
|
@@ -725,45 +785,53 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
725
785
|
if (fn) {
|
|
726
786
|
const stepCallId = crypto.randomUUID();
|
|
727
787
|
duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
|
|
728
|
-
return await fn(
|
|
729
|
-
|
|
730
|
-
workflowId,
|
|
731
|
-
mastra: this.mastra,
|
|
732
|
-
runtimeContext,
|
|
733
|
-
inputData: prevOutput,
|
|
734
|
-
state: executionContext.state,
|
|
735
|
-
setState: (state) => {
|
|
736
|
-
executionContext.state = state;
|
|
737
|
-
},
|
|
738
|
-
runCount: -1,
|
|
739
|
-
tracingContext: {
|
|
740
|
-
currentSpan: sleepSpan
|
|
741
|
-
},
|
|
742
|
-
getInitData: () => stepResults?.input,
|
|
743
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
744
|
-
// TODO: this function shouldn't have suspend probably?
|
|
745
|
-
suspend: async (_suspendPayload) => {
|
|
746
|
-
},
|
|
747
|
-
bail: () => {
|
|
748
|
-
},
|
|
749
|
-
abort: () => {
|
|
750
|
-
abortController?.abort();
|
|
751
|
-
},
|
|
752
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
753
|
-
// TODO: add streamVNext support
|
|
754
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
755
|
-
engine: { step: this.inngestStep },
|
|
756
|
-
abortSignal: abortController?.signal,
|
|
757
|
-
writer: new tools.ToolStream(
|
|
788
|
+
return await fn(
|
|
789
|
+
workflows.createDeprecationProxy(
|
|
758
790
|
{
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
791
|
+
runId,
|
|
792
|
+
workflowId,
|
|
793
|
+
mastra: this.mastra,
|
|
794
|
+
requestContext,
|
|
795
|
+
inputData: prevOutput,
|
|
796
|
+
state: executionContext.state,
|
|
797
|
+
setState: (state) => {
|
|
798
|
+
executionContext.state = state;
|
|
799
|
+
},
|
|
800
|
+
retryCount: -1,
|
|
801
|
+
tracingContext: {
|
|
802
|
+
currentSpan: sleepSpan
|
|
803
|
+
},
|
|
804
|
+
getInitData: () => stepResults?.input,
|
|
805
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
806
|
+
// TODO: this function shouldn't have suspend probably?
|
|
807
|
+
suspend: async (_suspendPayload) => {
|
|
808
|
+
},
|
|
809
|
+
bail: () => {
|
|
810
|
+
},
|
|
811
|
+
abort: () => {
|
|
812
|
+
abortController?.abort();
|
|
813
|
+
},
|
|
814
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
815
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
816
|
+
engine: { step: this.inngestStep },
|
|
817
|
+
abortSignal: abortController?.signal,
|
|
818
|
+
writer: new tools.ToolStream(
|
|
819
|
+
{
|
|
820
|
+
prefix: "workflow-step",
|
|
821
|
+
callId: stepCallId,
|
|
822
|
+
name: "sleep",
|
|
823
|
+
runId
|
|
824
|
+
},
|
|
825
|
+
writableStream
|
|
826
|
+
)
|
|
763
827
|
},
|
|
764
|
-
|
|
828
|
+
{
|
|
829
|
+
paramName: "runCount",
|
|
830
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
831
|
+
logger: this.logger
|
|
832
|
+
}
|
|
765
833
|
)
|
|
766
|
-
|
|
834
|
+
);
|
|
767
835
|
});
|
|
768
836
|
sleepSpan?.update({
|
|
769
837
|
attributes: {
|
|
@@ -787,14 +855,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
787
855
|
stepResults,
|
|
788
856
|
emitter,
|
|
789
857
|
abortController,
|
|
790
|
-
|
|
858
|
+
requestContext,
|
|
791
859
|
executionContext,
|
|
792
860
|
writableStream,
|
|
793
861
|
tracingContext
|
|
794
862
|
}) {
|
|
795
863
|
let { date, fn } = entry;
|
|
796
864
|
const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
797
|
-
type:
|
|
865
|
+
type: observability.SpanType.WORKFLOW_SLEEP,
|
|
798
866
|
name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
|
|
799
867
|
attributes: {
|
|
800
868
|
untilDate: date,
|
|
@@ -806,45 +874,53 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
806
874
|
if (fn) {
|
|
807
875
|
date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
|
|
808
876
|
const stepCallId = crypto.randomUUID();
|
|
809
|
-
return await fn(
|
|
810
|
-
|
|
811
|
-
workflowId,
|
|
812
|
-
mastra: this.mastra,
|
|
813
|
-
runtimeContext,
|
|
814
|
-
inputData: prevOutput,
|
|
815
|
-
state: executionContext.state,
|
|
816
|
-
setState: (state) => {
|
|
817
|
-
executionContext.state = state;
|
|
818
|
-
},
|
|
819
|
-
runCount: -1,
|
|
820
|
-
tracingContext: {
|
|
821
|
-
currentSpan: sleepUntilSpan
|
|
822
|
-
},
|
|
823
|
-
getInitData: () => stepResults?.input,
|
|
824
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
825
|
-
// TODO: this function shouldn't have suspend probably?
|
|
826
|
-
suspend: async (_suspendPayload) => {
|
|
827
|
-
},
|
|
828
|
-
bail: () => {
|
|
829
|
-
},
|
|
830
|
-
abort: () => {
|
|
831
|
-
abortController?.abort();
|
|
832
|
-
},
|
|
833
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
834
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
835
|
-
// TODO: add streamVNext support
|
|
836
|
-
engine: { step: this.inngestStep },
|
|
837
|
-
abortSignal: abortController?.signal,
|
|
838
|
-
writer: new tools.ToolStream(
|
|
877
|
+
return await fn(
|
|
878
|
+
workflows.createDeprecationProxy(
|
|
839
879
|
{
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
880
|
+
runId,
|
|
881
|
+
workflowId,
|
|
882
|
+
mastra: this.mastra,
|
|
883
|
+
requestContext,
|
|
884
|
+
inputData: prevOutput,
|
|
885
|
+
state: executionContext.state,
|
|
886
|
+
setState: (state) => {
|
|
887
|
+
executionContext.state = state;
|
|
888
|
+
},
|
|
889
|
+
retryCount: -1,
|
|
890
|
+
tracingContext: {
|
|
891
|
+
currentSpan: sleepUntilSpan
|
|
892
|
+
},
|
|
893
|
+
getInitData: () => stepResults?.input,
|
|
894
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
895
|
+
// TODO: this function shouldn't have suspend probably?
|
|
896
|
+
suspend: async (_suspendPayload) => {
|
|
897
|
+
},
|
|
898
|
+
bail: () => {
|
|
899
|
+
},
|
|
900
|
+
abort: () => {
|
|
901
|
+
abortController?.abort();
|
|
902
|
+
},
|
|
903
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
904
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
905
|
+
engine: { step: this.inngestStep },
|
|
906
|
+
abortSignal: abortController?.signal,
|
|
907
|
+
writer: new tools.ToolStream(
|
|
908
|
+
{
|
|
909
|
+
prefix: "workflow-step",
|
|
910
|
+
callId: stepCallId,
|
|
911
|
+
name: "sleep",
|
|
912
|
+
runId
|
|
913
|
+
},
|
|
914
|
+
writableStream
|
|
915
|
+
)
|
|
844
916
|
},
|
|
845
|
-
|
|
917
|
+
{
|
|
918
|
+
paramName: "runCount",
|
|
919
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
920
|
+
logger: this.logger
|
|
921
|
+
}
|
|
846
922
|
)
|
|
847
|
-
|
|
923
|
+
);
|
|
848
924
|
});
|
|
849
925
|
if (date && !(date instanceof Date)) {
|
|
850
926
|
date = new Date(date);
|
|
@@ -868,16 +944,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
868
944
|
throw e;
|
|
869
945
|
}
|
|
870
946
|
}
|
|
871
|
-
async executeWaitForEvent({ event, timeout }) {
|
|
872
|
-
const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
|
|
873
|
-
event: `user-event-${event}`,
|
|
874
|
-
timeout: timeout ?? 5e3
|
|
875
|
-
});
|
|
876
|
-
if (eventData === null) {
|
|
877
|
-
throw "Timeout waiting for event";
|
|
878
|
-
}
|
|
879
|
-
return eventData?.data;
|
|
880
|
-
}
|
|
881
947
|
async executeStep({
|
|
882
948
|
step,
|
|
883
949
|
stepResults,
|
|
@@ -886,14 +952,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
886
952
|
prevOutput,
|
|
887
953
|
emitter,
|
|
888
954
|
abortController,
|
|
889
|
-
|
|
955
|
+
requestContext,
|
|
890
956
|
tracingContext,
|
|
891
957
|
writableStream,
|
|
892
958
|
disableScorers
|
|
893
959
|
}) {
|
|
894
|
-
const
|
|
960
|
+
const stepSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
895
961
|
name: `workflow step: '${step.id}'`,
|
|
896
|
-
type:
|
|
962
|
+
type: observability.SpanType.WORKFLOW_STEP,
|
|
897
963
|
input: prevOutput,
|
|
898
964
|
attributes: {
|
|
899
965
|
stepId: step.id
|
|
@@ -910,27 +976,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
910
976
|
async () => {
|
|
911
977
|
const startedAt2 = Date.now();
|
|
912
978
|
await emitter.emit("watch", {
|
|
913
|
-
type: "watch",
|
|
914
|
-
payload: {
|
|
915
|
-
currentStep: {
|
|
916
|
-
id: step.id,
|
|
917
|
-
status: "running"
|
|
918
|
-
},
|
|
919
|
-
workflowState: {
|
|
920
|
-
status: "running",
|
|
921
|
-
steps: {
|
|
922
|
-
...stepResults,
|
|
923
|
-
[step.id]: {
|
|
924
|
-
status: "running"
|
|
925
|
-
}
|
|
926
|
-
},
|
|
927
|
-
result: null,
|
|
928
|
-
error: null
|
|
929
|
-
}
|
|
930
|
-
},
|
|
931
|
-
eventTimestamp: Date.now()
|
|
932
|
-
});
|
|
933
|
-
await emitter.emit("watch-v2", {
|
|
934
979
|
type: "workflow-step-start",
|
|
935
980
|
payload: {
|
|
936
981
|
id: step.id,
|
|
@@ -1006,23 +1051,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1006
1051
|
async () => {
|
|
1007
1052
|
if (result.status === "failed") {
|
|
1008
1053
|
await emitter.emit("watch", {
|
|
1009
|
-
type: "watch",
|
|
1010
|
-
payload: {
|
|
1011
|
-
currentStep: {
|
|
1012
|
-
id: step.id,
|
|
1013
|
-
status: "failed",
|
|
1014
|
-
error: result?.error
|
|
1015
|
-
},
|
|
1016
|
-
workflowState: {
|
|
1017
|
-
status: "running",
|
|
1018
|
-
steps: stepResults,
|
|
1019
|
-
result: null,
|
|
1020
|
-
error: null
|
|
1021
|
-
}
|
|
1022
|
-
},
|
|
1023
|
-
eventTimestamp: Date.now()
|
|
1024
|
-
});
|
|
1025
|
-
await emitter.emit("watch-v2", {
|
|
1026
1054
|
type: "workflow-step-result",
|
|
1027
1055
|
payload: {
|
|
1028
1056
|
id: step.id,
|
|
@@ -1041,27 +1069,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1041
1069
|
const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
|
|
1042
1070
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
1043
1071
|
await emitter.emit("watch", {
|
|
1044
|
-
type: "watch",
|
|
1045
|
-
payload: {
|
|
1046
|
-
currentStep: {
|
|
1047
|
-
id: step.id,
|
|
1048
|
-
status: "suspended",
|
|
1049
|
-
payload: stepResult.payload,
|
|
1050
|
-
suspendPayload: {
|
|
1051
|
-
...stepResult?.suspendPayload,
|
|
1052
|
-
__workflow_meta: { runId, path: suspendPath }
|
|
1053
|
-
}
|
|
1054
|
-
},
|
|
1055
|
-
workflowState: {
|
|
1056
|
-
status: "running",
|
|
1057
|
-
steps: stepResults,
|
|
1058
|
-
result: null,
|
|
1059
|
-
error: null
|
|
1060
|
-
}
|
|
1061
|
-
},
|
|
1062
|
-
eventTimestamp: Date.now()
|
|
1063
|
-
});
|
|
1064
|
-
await emitter.emit("watch-v2", {
|
|
1065
1072
|
type: "workflow-step-suspended",
|
|
1066
1073
|
payload: {
|
|
1067
1074
|
id: step.id,
|
|
@@ -1080,23 +1087,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1080
1087
|
}
|
|
1081
1088
|
};
|
|
1082
1089
|
}
|
|
1083
|
-
await emitter.emit("watch", {
|
|
1084
|
-
type: "watch",
|
|
1085
|
-
payload: {
|
|
1086
|
-
currentStep: {
|
|
1087
|
-
id: step.id,
|
|
1088
|
-
status: "suspended",
|
|
1089
|
-
payload: {}
|
|
1090
|
-
},
|
|
1091
|
-
workflowState: {
|
|
1092
|
-
status: "running",
|
|
1093
|
-
steps: stepResults,
|
|
1094
|
-
result: null,
|
|
1095
|
-
error: null
|
|
1096
|
-
}
|
|
1097
|
-
},
|
|
1098
|
-
eventTimestamp: Date.now()
|
|
1099
|
-
});
|
|
1100
1090
|
return {
|
|
1101
1091
|
executionContext,
|
|
1102
1092
|
result: {
|
|
@@ -1106,23 +1096,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1106
1096
|
};
|
|
1107
1097
|
}
|
|
1108
1098
|
await emitter.emit("watch", {
|
|
1109
|
-
type: "watch",
|
|
1110
|
-
payload: {
|
|
1111
|
-
currentStep: {
|
|
1112
|
-
id: step.id,
|
|
1113
|
-
status: "success",
|
|
1114
|
-
output: result?.result
|
|
1115
|
-
},
|
|
1116
|
-
workflowState: {
|
|
1117
|
-
status: "running",
|
|
1118
|
-
steps: stepResults,
|
|
1119
|
-
result: null,
|
|
1120
|
-
error: null
|
|
1121
|
-
}
|
|
1122
|
-
},
|
|
1123
|
-
eventTimestamp: Date.now()
|
|
1124
|
-
});
|
|
1125
|
-
await emitter.emit("watch-v2", {
|
|
1126
1099
|
type: "workflow-step-result",
|
|
1127
1100
|
payload: {
|
|
1128
1101
|
id: step.id,
|
|
@@ -1130,7 +1103,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1130
1103
|
output: result?.result
|
|
1131
1104
|
}
|
|
1132
1105
|
});
|
|
1133
|
-
await emitter.emit("watch
|
|
1106
|
+
await emitter.emit("watch", {
|
|
1134
1107
|
type: "workflow-step-finish",
|
|
1135
1108
|
payload: {
|
|
1136
1109
|
id: step.id,
|
|
@@ -1150,6 +1123,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1150
1123
|
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1151
1124
|
};
|
|
1152
1125
|
}
|
|
1126
|
+
const stepCallId = crypto.randomUUID();
|
|
1153
1127
|
let stepRes;
|
|
1154
1128
|
try {
|
|
1155
1129
|
stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
|
|
@@ -1163,8 +1137,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1163
1137
|
const result = await step.execute({
|
|
1164
1138
|
runId: executionContext.runId,
|
|
1165
1139
|
mastra: this.mastra,
|
|
1166
|
-
|
|
1167
|
-
|
|
1140
|
+
requestContext,
|
|
1141
|
+
writer: new tools.ToolStream(
|
|
1142
|
+
{
|
|
1143
|
+
prefix: "workflow-step",
|
|
1144
|
+
callId: stepCallId,
|
|
1145
|
+
name: step.id,
|
|
1146
|
+
runId: executionContext.runId
|
|
1147
|
+
},
|
|
1148
|
+
writableStream
|
|
1149
|
+
),
|
|
1168
1150
|
state: executionContext?.state ?? {},
|
|
1169
1151
|
setState: (state) => {
|
|
1170
1152
|
executionContext.state = state;
|
|
@@ -1172,12 +1154,21 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1172
1154
|
inputData,
|
|
1173
1155
|
resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
|
|
1174
1156
|
tracingContext: {
|
|
1175
|
-
currentSpan:
|
|
1157
|
+
currentSpan: stepSpan
|
|
1176
1158
|
},
|
|
1177
1159
|
getInitData: () => stepResults?.input,
|
|
1178
1160
|
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1179
|
-
suspend: async (suspendPayload) => {
|
|
1161
|
+
suspend: async (suspendPayload, suspendOptions) => {
|
|
1180
1162
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
1163
|
+
if (suspendOptions?.resumeLabel) {
|
|
1164
|
+
const resumeLabel = Array.isArray(suspendOptions.resumeLabel) ? suspendOptions.resumeLabel : [suspendOptions.resumeLabel];
|
|
1165
|
+
for (const label of resumeLabel) {
|
|
1166
|
+
executionContext.resumeLabels[label] = {
|
|
1167
|
+
stepId: step.id,
|
|
1168
|
+
foreachIndex: executionContext.foreachIndex
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1181
1172
|
suspended = { payload: suspendPayload };
|
|
1182
1173
|
},
|
|
1183
1174
|
bail: (result2) => {
|
|
@@ -1190,6 +1181,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1190
1181
|
runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
|
|
1191
1182
|
},
|
|
1192
1183
|
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1184
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1193
1185
|
engine: {
|
|
1194
1186
|
step: this.inngestStep
|
|
1195
1187
|
},
|
|
@@ -1217,7 +1209,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1217
1209
|
};
|
|
1218
1210
|
execResults = stepFailure;
|
|
1219
1211
|
const fallbackErrorMessage = `Step ${step.id} failed`;
|
|
1220
|
-
|
|
1212
|
+
stepSpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
|
|
1221
1213
|
throw new inngest.RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
|
|
1222
1214
|
cause: execResults
|
|
1223
1215
|
});
|
|
@@ -1241,24 +1233,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1241
1233
|
startedAt
|
|
1242
1234
|
};
|
|
1243
1235
|
}
|
|
1244
|
-
await emitter.emit("watch", {
|
|
1245
|
-
type: "watch",
|
|
1246
|
-
payload: {
|
|
1247
|
-
currentStep: {
|
|
1248
|
-
id: step.id,
|
|
1249
|
-
...execResults
|
|
1250
|
-
},
|
|
1251
|
-
workflowState: {
|
|
1252
|
-
status: "running",
|
|
1253
|
-
steps: { ...stepResults, [step.id]: execResults },
|
|
1254
|
-
result: null,
|
|
1255
|
-
error: null
|
|
1256
|
-
}
|
|
1257
|
-
},
|
|
1258
|
-
eventTimestamp: Date.now()
|
|
1259
|
-
});
|
|
1260
1236
|
if (execResults.status === "suspended") {
|
|
1261
|
-
await emitter.emit("watch
|
|
1237
|
+
await emitter.emit("watch", {
|
|
1262
1238
|
type: "workflow-step-suspended",
|
|
1263
1239
|
payload: {
|
|
1264
1240
|
id: step.id,
|
|
@@ -1266,14 +1242,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1266
1242
|
}
|
|
1267
1243
|
});
|
|
1268
1244
|
} else {
|
|
1269
|
-
await emitter.emit("watch
|
|
1245
|
+
await emitter.emit("watch", {
|
|
1270
1246
|
type: "workflow-step-result",
|
|
1271
1247
|
payload: {
|
|
1272
1248
|
id: step.id,
|
|
1273
1249
|
...execResults
|
|
1274
1250
|
}
|
|
1275
1251
|
});
|
|
1276
|
-
await emitter.emit("watch
|
|
1252
|
+
await emitter.emit("watch", {
|
|
1277
1253
|
type: "workflow-step-finish",
|
|
1278
1254
|
payload: {
|
|
1279
1255
|
id: step.id,
|
|
@@ -1281,7 +1257,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1281
1257
|
}
|
|
1282
1258
|
});
|
|
1283
1259
|
}
|
|
1284
|
-
|
|
1260
|
+
stepSpan?.end({ output: execResults });
|
|
1285
1261
|
return { result: execResults, executionContext, stepResults };
|
|
1286
1262
|
});
|
|
1287
1263
|
} catch (e) {
|
|
@@ -1311,9 +1287,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1311
1287
|
output: stepRes.result,
|
|
1312
1288
|
workflowId: executionContext.workflowId,
|
|
1313
1289
|
stepId: step.id,
|
|
1314
|
-
|
|
1290
|
+
requestContext,
|
|
1315
1291
|
disableScorers,
|
|
1316
|
-
tracingContext: { currentSpan:
|
|
1292
|
+
tracingContext: { currentSpan: stepSpan }
|
|
1317
1293
|
});
|
|
1318
1294
|
}
|
|
1319
1295
|
});
|
|
@@ -1351,6 +1327,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1351
1327
|
context: stepResults,
|
|
1352
1328
|
activePaths: [],
|
|
1353
1329
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1330
|
+
resumeLabels: executionContext.resumeLabels,
|
|
1354
1331
|
waitingPaths: {},
|
|
1355
1332
|
serializedStepGraph,
|
|
1356
1333
|
status: workflowStatus,
|
|
@@ -1368,20 +1345,18 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1368
1345
|
runId,
|
|
1369
1346
|
entry,
|
|
1370
1347
|
prevOutput,
|
|
1371
|
-
prevStep,
|
|
1372
1348
|
stepResults,
|
|
1373
|
-
serializedStepGraph,
|
|
1374
1349
|
resume,
|
|
1375
1350
|
executionContext,
|
|
1376
1351
|
emitter,
|
|
1377
1352
|
abortController,
|
|
1378
|
-
|
|
1353
|
+
requestContext,
|
|
1379
1354
|
writableStream,
|
|
1380
1355
|
disableScorers,
|
|
1381
1356
|
tracingContext
|
|
1382
1357
|
}) {
|
|
1383
1358
|
const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
1384
|
-
type:
|
|
1359
|
+
type: observability.SpanType.WORKFLOW_CONDITIONAL,
|
|
1385
1360
|
name: `conditional: '${entry.conditions.length} conditions'`,
|
|
1386
1361
|
input: prevOutput,
|
|
1387
1362
|
attributes: {
|
|
@@ -1394,7 +1369,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1394
1369
|
entry.conditions.map(
|
|
1395
1370
|
(cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
|
|
1396
1371
|
const evalSpan = conditionalSpan?.createChildSpan({
|
|
1397
|
-
type:
|
|
1372
|
+
type: observability.SpanType.WORKFLOW_CONDITIONAL_EVAL,
|
|
1398
1373
|
name: `condition: '${index}'`,
|
|
1399
1374
|
input: prevOutput,
|
|
1400
1375
|
attributes: {
|
|
@@ -1403,47 +1378,55 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1403
1378
|
tracingPolicy: this.options?.tracingPolicy
|
|
1404
1379
|
});
|
|
1405
1380
|
try {
|
|
1406
|
-
const result = await cond(
|
|
1407
|
-
|
|
1408
|
-
workflowId,
|
|
1409
|
-
mastra: this.mastra,
|
|
1410
|
-
runtimeContext,
|
|
1411
|
-
runCount: -1,
|
|
1412
|
-
inputData: prevOutput,
|
|
1413
|
-
state: executionContext.state,
|
|
1414
|
-
setState: (state) => {
|
|
1415
|
-
executionContext.state = state;
|
|
1416
|
-
},
|
|
1417
|
-
tracingContext: {
|
|
1418
|
-
currentSpan: evalSpan
|
|
1419
|
-
},
|
|
1420
|
-
getInitData: () => stepResults?.input,
|
|
1421
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1422
|
-
// TODO: this function shouldn't have suspend probably?
|
|
1423
|
-
suspend: async (_suspendPayload) => {
|
|
1424
|
-
},
|
|
1425
|
-
bail: () => {
|
|
1426
|
-
},
|
|
1427
|
-
abort: () => {
|
|
1428
|
-
abortController.abort();
|
|
1429
|
-
},
|
|
1430
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1431
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1432
|
-
// TODO: add streamVNext support
|
|
1433
|
-
engine: {
|
|
1434
|
-
step: this.inngestStep
|
|
1435
|
-
},
|
|
1436
|
-
abortSignal: abortController.signal,
|
|
1437
|
-
writer: new tools.ToolStream(
|
|
1381
|
+
const result = await cond(
|
|
1382
|
+
workflows.createDeprecationProxy(
|
|
1438
1383
|
{
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1384
|
+
runId,
|
|
1385
|
+
workflowId,
|
|
1386
|
+
mastra: this.mastra,
|
|
1387
|
+
requestContext,
|
|
1388
|
+
retryCount: -1,
|
|
1389
|
+
inputData: prevOutput,
|
|
1390
|
+
state: executionContext.state,
|
|
1391
|
+
setState: (state) => {
|
|
1392
|
+
executionContext.state = state;
|
|
1393
|
+
},
|
|
1394
|
+
tracingContext: {
|
|
1395
|
+
currentSpan: evalSpan
|
|
1396
|
+
},
|
|
1397
|
+
getInitData: () => stepResults?.input,
|
|
1398
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1399
|
+
// TODO: this function shouldn't have suspend probably?
|
|
1400
|
+
suspend: async (_suspendPayload) => {
|
|
1401
|
+
},
|
|
1402
|
+
bail: () => {
|
|
1403
|
+
},
|
|
1404
|
+
abort: () => {
|
|
1405
|
+
abortController.abort();
|
|
1406
|
+
},
|
|
1407
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1408
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1409
|
+
engine: {
|
|
1410
|
+
step: this.inngestStep
|
|
1411
|
+
},
|
|
1412
|
+
abortSignal: abortController.signal,
|
|
1413
|
+
writer: new tools.ToolStream(
|
|
1414
|
+
{
|
|
1415
|
+
prefix: "workflow-step",
|
|
1416
|
+
callId: crypto.randomUUID(),
|
|
1417
|
+
name: "conditional",
|
|
1418
|
+
runId
|
|
1419
|
+
},
|
|
1420
|
+
writableStream
|
|
1421
|
+
)
|
|
1443
1422
|
},
|
|
1444
|
-
|
|
1423
|
+
{
|
|
1424
|
+
paramName: "runCount",
|
|
1425
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
1426
|
+
logger: this.logger
|
|
1427
|
+
}
|
|
1445
1428
|
)
|
|
1446
|
-
|
|
1429
|
+
);
|
|
1447
1430
|
evalSpan?.end({
|
|
1448
1431
|
output: result,
|
|
1449
1432
|
attributes: {
|
|
@@ -1471,13 +1454,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1471
1454
|
}
|
|
1472
1455
|
});
|
|
1473
1456
|
const results = await Promise.all(
|
|
1474
|
-
stepsToRun.map(
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1457
|
+
stepsToRun.map(async (step, index) => {
|
|
1458
|
+
const currStepResult = stepResults[step.step.id];
|
|
1459
|
+
if (currStepResult && currStepResult.status === "success") {
|
|
1460
|
+
return currStepResult;
|
|
1461
|
+
}
|
|
1462
|
+
const result = await this.executeStep({
|
|
1463
|
+
step: step.step,
|
|
1464
|
+
prevOutput,
|
|
1481
1465
|
stepResults,
|
|
1482
1466
|
resume,
|
|
1483
1467
|
executionContext: {
|
|
@@ -1485,32 +1469,34 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1485
1469
|
runId,
|
|
1486
1470
|
executionPath: [...executionContext.executionPath, index],
|
|
1487
1471
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1472
|
+
resumeLabels: executionContext.resumeLabels,
|
|
1488
1473
|
retryConfig: executionContext.retryConfig,
|
|
1489
|
-
executionSpan: executionContext.executionSpan,
|
|
1490
1474
|
state: executionContext.state
|
|
1491
1475
|
},
|
|
1492
1476
|
emitter,
|
|
1493
1477
|
abortController,
|
|
1494
|
-
|
|
1478
|
+
requestContext,
|
|
1495
1479
|
writableStream,
|
|
1496
1480
|
disableScorers,
|
|
1497
1481
|
tracingContext: {
|
|
1498
1482
|
currentSpan: conditionalSpan
|
|
1499
1483
|
}
|
|
1500
|
-
})
|
|
1501
|
-
|
|
1484
|
+
});
|
|
1485
|
+
stepResults[step.step.id] = result;
|
|
1486
|
+
return result;
|
|
1487
|
+
})
|
|
1502
1488
|
);
|
|
1503
|
-
const hasFailed = results.find((result) => result.
|
|
1504
|
-
const hasSuspended = results.find((result) => result.
|
|
1489
|
+
const hasFailed = results.find((result) => result.status === "failed");
|
|
1490
|
+
const hasSuspended = results.find((result) => result.status === "suspended");
|
|
1505
1491
|
if (hasFailed) {
|
|
1506
|
-
execResults = { status: "failed", error: hasFailed.
|
|
1492
|
+
execResults = { status: "failed", error: hasFailed.error };
|
|
1507
1493
|
} else if (hasSuspended) {
|
|
1508
|
-
execResults = { status: "suspended", suspendPayload: hasSuspended.
|
|
1494
|
+
execResults = { status: "suspended", suspendPayload: hasSuspended.suspendPayload };
|
|
1509
1495
|
} else {
|
|
1510
1496
|
execResults = {
|
|
1511
1497
|
status: "success",
|
|
1512
1498
|
output: results.reduce((acc, result, index) => {
|
|
1513
|
-
if (result.
|
|
1499
|
+
if (result.status === "success") {
|
|
1514
1500
|
acc[stepsToRun[index].step.id] = result.output;
|
|
1515
1501
|
}
|
|
1516
1502
|
return acc;
|