@mastra/inngest 0.0.0-break-rename-vnext-legacy-20250926163953 → 0.0.0-bundle-recursion-20251030002519
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 +230 -4
- package/dist/index.cjs +621 -324
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +87 -57
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +622 -325
- package/dist/index.js.map +1 -1
- package/package.json +13 -13
package/dist/index.cjs
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
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
6
|
var aiTracing = require('@mastra/core/ai-tracing');
|
|
6
7
|
var di = require('@mastra/core/di');
|
|
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');
|
|
12
|
+
var inngest = require('inngest');
|
|
10
13
|
var hono = require('inngest/hono');
|
|
11
14
|
var zod = require('zod');
|
|
12
15
|
|
|
@@ -60,8 +63,15 @@ var InngestRun = class extends workflows.Run {
|
|
|
60
63
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
61
64
|
runs = await this.getRuns(eventId);
|
|
62
65
|
if (runs?.[0]?.status === "Failed") {
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
|
|
67
|
+
workflowName: this.workflowId,
|
|
68
|
+
runId: this.runId
|
|
69
|
+
});
|
|
70
|
+
return {
|
|
71
|
+
output: { result: { steps: snapshot?.context, status: "failed", error: runs?.[0]?.output?.message } }
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (runs?.[0]?.status === "Cancelled") {
|
|
65
75
|
const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
|
|
66
76
|
workflowName: this.workflowId,
|
|
67
77
|
runId: this.runId
|
|
@@ -100,8 +110,15 @@ var InngestRun = class extends workflows.Run {
|
|
|
100
110
|
});
|
|
101
111
|
}
|
|
102
112
|
}
|
|
103
|
-
async start({
|
|
104
|
-
|
|
113
|
+
async start(params) {
|
|
114
|
+
return this._start(params);
|
|
115
|
+
}
|
|
116
|
+
async _start({
|
|
117
|
+
inputData,
|
|
118
|
+
initialState,
|
|
119
|
+
outputOptions,
|
|
120
|
+
tracingOptions,
|
|
121
|
+
format
|
|
105
122
|
}) {
|
|
106
123
|
await this.#mastra.getStorage()?.persistWorkflowSnapshot({
|
|
107
124
|
workflowName: this.workflowId,
|
|
@@ -114,18 +131,24 @@ var InngestRun = class extends workflows.Run {
|
|
|
114
131
|
context: {},
|
|
115
132
|
activePaths: [],
|
|
116
133
|
suspendedPaths: {},
|
|
134
|
+
resumeLabels: {},
|
|
117
135
|
waitingPaths: {},
|
|
118
136
|
timestamp: Date.now(),
|
|
119
137
|
status: "running"
|
|
120
138
|
}
|
|
121
139
|
});
|
|
122
140
|
const inputDataToUse = await this._validateInput(inputData);
|
|
141
|
+
const initialStateToUse = await this._validateInitialState(initialState ?? {});
|
|
123
142
|
const eventOutput = await this.inngest.send({
|
|
124
143
|
name: `workflow.${this.workflowId}`,
|
|
125
144
|
data: {
|
|
126
145
|
inputData: inputDataToUse,
|
|
146
|
+
initialState: initialStateToUse,
|
|
127
147
|
runId: this.runId,
|
|
128
|
-
resourceId: this.resourceId
|
|
148
|
+
resourceId: this.resourceId,
|
|
149
|
+
outputOptions,
|
|
150
|
+
tracingOptions,
|
|
151
|
+
format
|
|
129
152
|
}
|
|
130
153
|
});
|
|
131
154
|
const eventId = eventOutput.ids[0];
|
|
@@ -167,6 +190,7 @@ var InngestRun = class extends workflows.Run {
|
|
|
167
190
|
name: `workflow.${this.workflowId}`,
|
|
168
191
|
data: {
|
|
169
192
|
inputData: resumeDataToUse,
|
|
193
|
+
initialState: snapshot?.value ?? {},
|
|
170
194
|
runId: this.runId,
|
|
171
195
|
workflowId: this.workflowId,
|
|
172
196
|
stepResults: snapshot?.context,
|
|
@@ -213,20 +237,35 @@ var InngestRun = class extends workflows.Run {
|
|
|
213
237
|
});
|
|
214
238
|
};
|
|
215
239
|
}
|
|
216
|
-
|
|
240
|
+
streamLegacy({ inputData, runtimeContext } = {}) {
|
|
217
241
|
const { readable, writable } = new TransformStream();
|
|
218
242
|
const writer = writable.getWriter();
|
|
219
243
|
const unwatch = this.watch(async (event) => {
|
|
220
244
|
try {
|
|
245
|
+
await writer.write({
|
|
246
|
+
// @ts-ignore
|
|
247
|
+
type: "start",
|
|
248
|
+
// @ts-ignore
|
|
249
|
+
payload: { runId: this.runId }
|
|
250
|
+
});
|
|
221
251
|
const e = {
|
|
222
252
|
...event,
|
|
223
253
|
type: event.type.replace("workflow-", "")
|
|
224
254
|
};
|
|
255
|
+
if (e.type === "step-output") {
|
|
256
|
+
e.type = e.payload.output.type;
|
|
257
|
+
e.payload = e.payload.output.payload;
|
|
258
|
+
}
|
|
225
259
|
await writer.write(e);
|
|
226
260
|
} catch {
|
|
227
261
|
}
|
|
228
262
|
}, "watch-v2");
|
|
229
263
|
this.closeStreamAction = async () => {
|
|
264
|
+
await writer.write({
|
|
265
|
+
type: "finish",
|
|
266
|
+
// @ts-ignore
|
|
267
|
+
payload: { runId: this.runId }
|
|
268
|
+
});
|
|
230
269
|
unwatch();
|
|
231
270
|
try {
|
|
232
271
|
await writer.close();
|
|
@@ -236,7 +275,7 @@ var InngestRun = class extends workflows.Run {
|
|
|
236
275
|
writer.releaseLock();
|
|
237
276
|
}
|
|
238
277
|
};
|
|
239
|
-
this.executionResults = this.
|
|
278
|
+
this.executionResults = this._start({ inputData, runtimeContext, format: "legacy" }).then((result) => {
|
|
240
279
|
if (result.status !== "suspended") {
|
|
241
280
|
this.closeStreamAction?.().catch(() => {
|
|
242
281
|
});
|
|
@@ -248,6 +287,82 @@ var InngestRun = class extends workflows.Run {
|
|
|
248
287
|
getWorkflowState: () => this.executionResults
|
|
249
288
|
};
|
|
250
289
|
}
|
|
290
|
+
stream({
|
|
291
|
+
inputData,
|
|
292
|
+
runtimeContext,
|
|
293
|
+
tracingOptions,
|
|
294
|
+
closeOnSuspend = true,
|
|
295
|
+
initialState,
|
|
296
|
+
outputOptions
|
|
297
|
+
} = {}) {
|
|
298
|
+
if (this.closeStreamAction && this.streamOutput) {
|
|
299
|
+
return this.streamOutput;
|
|
300
|
+
}
|
|
301
|
+
this.closeStreamAction = async () => {
|
|
302
|
+
};
|
|
303
|
+
const self = this;
|
|
304
|
+
const stream$1 = new web.ReadableStream({
|
|
305
|
+
async start(controller) {
|
|
306
|
+
const unwatch = self.watch(async ({ type, from = stream.ChunkFrom.WORKFLOW, payload }) => {
|
|
307
|
+
controller.enqueue({
|
|
308
|
+
type,
|
|
309
|
+
runId: self.runId,
|
|
310
|
+
from,
|
|
311
|
+
payload: {
|
|
312
|
+
stepName: payload?.id,
|
|
313
|
+
...payload
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}, "watch-v2");
|
|
317
|
+
self.closeStreamAction = async () => {
|
|
318
|
+
unwatch();
|
|
319
|
+
try {
|
|
320
|
+
await controller.close();
|
|
321
|
+
} catch (err) {
|
|
322
|
+
console.error("Error closing stream:", err);
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const executionResultsPromise = self._start({
|
|
326
|
+
inputData,
|
|
327
|
+
runtimeContext,
|
|
328
|
+
// tracingContext, // We are not able to pass a reference to a span here, what to do?
|
|
329
|
+
initialState,
|
|
330
|
+
tracingOptions,
|
|
331
|
+
outputOptions,
|
|
332
|
+
format: "vnext"
|
|
333
|
+
});
|
|
334
|
+
let executionResults;
|
|
335
|
+
try {
|
|
336
|
+
executionResults = await executionResultsPromise;
|
|
337
|
+
if (closeOnSuspend) {
|
|
338
|
+
self.closeStreamAction?.().catch(() => {
|
|
339
|
+
});
|
|
340
|
+
} else if (executionResults.status !== "suspended") {
|
|
341
|
+
self.closeStreamAction?.().catch(() => {
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
if (self.streamOutput) {
|
|
345
|
+
self.streamOutput.updateResults(
|
|
346
|
+
executionResults
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
} catch (err) {
|
|
350
|
+
self.streamOutput?.rejectResults(err);
|
|
351
|
+
self.closeStreamAction?.().catch(() => {
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
this.streamOutput = new stream.WorkflowRunOutput({
|
|
357
|
+
runId: this.runId,
|
|
358
|
+
workflowId: this.workflowId,
|
|
359
|
+
stream: stream$1
|
|
360
|
+
});
|
|
361
|
+
return this.streamOutput;
|
|
362
|
+
}
|
|
363
|
+
streamVNext(args = {}) {
|
|
364
|
+
return this.stream(args);
|
|
365
|
+
}
|
|
251
366
|
};
|
|
252
367
|
var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
253
368
|
#mastra;
|
|
@@ -326,8 +441,12 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
326
441
|
this.inngest
|
|
327
442
|
);
|
|
328
443
|
this.runs.set(runIdToUse, run);
|
|
444
|
+
const shouldPersistSnapshot = this.options.shouldPersistSnapshot({
|
|
445
|
+
workflowStatus: run.workflowRunStatus,
|
|
446
|
+
stepResults: {}
|
|
447
|
+
});
|
|
329
448
|
const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse, false);
|
|
330
|
-
if (!workflowSnapshotInStorage) {
|
|
449
|
+
if (!workflowSnapshotInStorage && shouldPersistSnapshot) {
|
|
331
450
|
await this.mastra?.getStorage()?.persistWorkflowSnapshot({
|
|
332
451
|
workflowName: this.id,
|
|
333
452
|
runId: runIdToUse,
|
|
@@ -341,6 +460,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
341
460
|
waitingPaths: {},
|
|
342
461
|
serializedStepGraph: this.serializedStepGraph,
|
|
343
462
|
suspendedPaths: {},
|
|
463
|
+
resumeLabels: {},
|
|
344
464
|
result: void 0,
|
|
345
465
|
error: void 0,
|
|
346
466
|
// @ts-ignore
|
|
@@ -365,7 +485,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
365
485
|
},
|
|
366
486
|
{ event: `workflow.${this.id}` },
|
|
367
487
|
async ({ event, step, attempt, publish }) => {
|
|
368
|
-
let { inputData, runId, resourceId, resume } = event.data;
|
|
488
|
+
let { inputData, initialState, runId, resourceId, resume, outputOptions, format } = event.data;
|
|
369
489
|
if (!runId) {
|
|
370
490
|
runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
|
|
371
491
|
return crypto.randomUUID();
|
|
@@ -393,7 +513,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
393
513
|
once: (_event, _callback) => {
|
|
394
514
|
}
|
|
395
515
|
};
|
|
396
|
-
const engine = new InngestExecutionEngine(this.#mastra, step, attempt);
|
|
516
|
+
const engine = new InngestExecutionEngine(this.#mastra, step, attempt, this.options);
|
|
397
517
|
const result = await engine.execute({
|
|
398
518
|
workflowId: this.id,
|
|
399
519
|
runId,
|
|
@@ -401,14 +521,30 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
|
|
|
401
521
|
graph: this.executionGraph,
|
|
402
522
|
serializedStepGraph: this.serializedStepGraph,
|
|
403
523
|
input: inputData,
|
|
524
|
+
initialState,
|
|
404
525
|
emitter,
|
|
405
526
|
retryConfig: this.retryConfig,
|
|
406
527
|
runtimeContext: new di.RuntimeContext(),
|
|
407
528
|
// TODO
|
|
408
529
|
resume,
|
|
530
|
+
format,
|
|
409
531
|
abortController: new AbortController(),
|
|
410
|
-
currentSpan:
|
|
411
|
-
|
|
532
|
+
// currentSpan: undefined, // TODO: Pass actual parent AI span from workflow execution context
|
|
533
|
+
outputOptions,
|
|
534
|
+
writableStream: new WritableStream({
|
|
535
|
+
write(chunk) {
|
|
536
|
+
void emitter.emit("watch-v2", chunk).catch(() => {
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
})
|
|
540
|
+
});
|
|
541
|
+
await step.run(`workflow.${this.id}.finalize`, async () => {
|
|
542
|
+
if (result.status === "failed") {
|
|
543
|
+
throw new inngest.NonRetriableError(`Workflow failed`, {
|
|
544
|
+
cause: result
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
return result;
|
|
412
548
|
});
|
|
413
549
|
return { result, runId };
|
|
414
550
|
}
|
|
@@ -438,7 +574,7 @@ function isAgent(params) {
|
|
|
438
574
|
function isTool(params) {
|
|
439
575
|
return params instanceof tools.Tool;
|
|
440
576
|
}
|
|
441
|
-
function createStep(params) {
|
|
577
|
+
function createStep(params, agentOptions) {
|
|
442
578
|
if (isAgent(params)) {
|
|
443
579
|
return {
|
|
444
580
|
id: params.name,
|
|
@@ -446,12 +582,23 @@ function createStep(params) {
|
|
|
446
582
|
// @ts-ignore
|
|
447
583
|
inputSchema: zod.z.object({
|
|
448
584
|
prompt: zod.z.string()
|
|
585
|
+
// resourceId: z.string().optional(),
|
|
586
|
+
// threadId: z.string().optional(),
|
|
449
587
|
}),
|
|
450
588
|
// @ts-ignore
|
|
451
589
|
outputSchema: zod.z.object({
|
|
452
590
|
text: zod.z.string()
|
|
453
591
|
}),
|
|
454
|
-
execute: async ({
|
|
592
|
+
execute: async ({
|
|
593
|
+
inputData,
|
|
594
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
595
|
+
[_constants.STREAM_FORMAT_SYMBOL]: streamFormat,
|
|
596
|
+
runtimeContext,
|
|
597
|
+
tracingContext,
|
|
598
|
+
abortSignal,
|
|
599
|
+
abort,
|
|
600
|
+
writer
|
|
601
|
+
}) => {
|
|
455
602
|
let streamPromise = {};
|
|
456
603
|
streamPromise.promise = new Promise((resolve, reject) => {
|
|
457
604
|
streamPromise.resolve = resolve;
|
|
@@ -461,34 +608,60 @@ function createStep(params) {
|
|
|
461
608
|
name: params.name,
|
|
462
609
|
args: inputData
|
|
463
610
|
};
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
611
|
+
let stream;
|
|
612
|
+
if ((await params.getModel()).specificationVersion === "v1") {
|
|
613
|
+
const { fullStream } = await params.streamLegacy(inputData.prompt, {
|
|
614
|
+
...agentOptions ?? {},
|
|
615
|
+
// resourceId: inputData.resourceId,
|
|
616
|
+
// threadId: inputData.threadId,
|
|
617
|
+
runtimeContext,
|
|
618
|
+
tracingContext,
|
|
619
|
+
onFinish: (result) => {
|
|
620
|
+
streamPromise.resolve(result.text);
|
|
621
|
+
void agentOptions?.onFinish?.(result);
|
|
622
|
+
},
|
|
623
|
+
abortSignal
|
|
624
|
+
});
|
|
625
|
+
stream = fullStream;
|
|
626
|
+
} else {
|
|
627
|
+
const modelOutput = await params.stream(inputData.prompt, {
|
|
628
|
+
...agentOptions ?? {},
|
|
629
|
+
runtimeContext,
|
|
630
|
+
tracingContext,
|
|
631
|
+
onFinish: (result) => {
|
|
632
|
+
streamPromise.resolve(result.text);
|
|
633
|
+
void agentOptions?.onFinish?.(result);
|
|
634
|
+
},
|
|
635
|
+
abortSignal
|
|
636
|
+
});
|
|
637
|
+
stream = modelOutput.fullStream;
|
|
474
638
|
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
639
|
+
if (streamFormat === "legacy") {
|
|
640
|
+
await emitter.emit("watch-v2", {
|
|
641
|
+
type: "tool-call-streaming-start",
|
|
642
|
+
...toolData ?? {}
|
|
643
|
+
});
|
|
644
|
+
for await (const chunk of stream) {
|
|
645
|
+
if (chunk.type === "text-delta") {
|
|
646
|
+
await emitter.emit("watch-v2", {
|
|
647
|
+
type: "tool-call-delta",
|
|
648
|
+
...toolData ?? {},
|
|
649
|
+
argsTextDelta: chunk.textDelta
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
await emitter.emit("watch-v2", {
|
|
654
|
+
type: "tool-call-streaming-finish",
|
|
655
|
+
...toolData ?? {}
|
|
656
|
+
});
|
|
657
|
+
} else {
|
|
658
|
+
for await (const chunk of stream) {
|
|
659
|
+
await writer.write(chunk);
|
|
486
660
|
}
|
|
487
661
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
});
|
|
662
|
+
if (abortSignal.aborted) {
|
|
663
|
+
return abort();
|
|
664
|
+
}
|
|
492
665
|
return {
|
|
493
666
|
text: await streamPromise.promise
|
|
494
667
|
};
|
|
@@ -533,7 +706,10 @@ function createStep(params) {
|
|
|
533
706
|
function init(inngest) {
|
|
534
707
|
return {
|
|
535
708
|
createWorkflow(params) {
|
|
536
|
-
return new InngestWorkflow(
|
|
709
|
+
return new InngestWorkflow(
|
|
710
|
+
params,
|
|
711
|
+
inngest
|
|
712
|
+
);
|
|
537
713
|
},
|
|
538
714
|
createStep,
|
|
539
715
|
cloneStep(step, opts) {
|
|
@@ -542,6 +718,9 @@ function init(inngest) {
|
|
|
542
718
|
description: step.description,
|
|
543
719
|
inputSchema: step.inputSchema,
|
|
544
720
|
outputSchema: step.outputSchema,
|
|
721
|
+
resumeSchema: step.resumeSchema,
|
|
722
|
+
suspendSchema: step.suspendSchema,
|
|
723
|
+
stateSchema: step.stateSchema,
|
|
545
724
|
execute: step.execute,
|
|
546
725
|
component: step.component
|
|
547
726
|
};
|
|
@@ -568,19 +747,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
568
747
|
this.inngestStep = inngestStep;
|
|
569
748
|
this.inngestAttempts = inngestAttempts;
|
|
570
749
|
}
|
|
571
|
-
async
|
|
572
|
-
await params.emitter.emit("watch-v2", {
|
|
573
|
-
type: "workflow-start",
|
|
574
|
-
payload: { runId: params.runId }
|
|
575
|
-
});
|
|
576
|
-
const result = await super.execute(params);
|
|
577
|
-
await params.emitter.emit("watch-v2", {
|
|
578
|
-
type: "workflow-finish",
|
|
579
|
-
payload: { runId: params.runId }
|
|
580
|
-
});
|
|
581
|
-
return result;
|
|
582
|
-
}
|
|
583
|
-
async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
|
|
750
|
+
async fmtReturnValue(emitter, stepResults, lastOutput, error) {
|
|
584
751
|
const base = {
|
|
585
752
|
status: lastOutput.status,
|
|
586
753
|
steps: stepResults
|
|
@@ -627,14 +794,13 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
627
794
|
});
|
|
628
795
|
const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
|
|
629
796
|
if (stepResult?.status === "suspended") {
|
|
630
|
-
const nestedPath = stepResult?.
|
|
797
|
+
const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
|
|
631
798
|
return nestedPath ? [[stepId, ...nestedPath]] : [[stepId]];
|
|
632
799
|
}
|
|
633
800
|
return [];
|
|
634
801
|
});
|
|
635
802
|
base.suspended = suspendedStepIds;
|
|
636
803
|
}
|
|
637
|
-
executionSpan?.end();
|
|
638
804
|
return base;
|
|
639
805
|
}
|
|
640
806
|
// async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
|
|
@@ -666,41 +832,54 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
666
832
|
if (fn) {
|
|
667
833
|
const stepCallId = crypto.randomUUID();
|
|
668
834
|
duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
|
|
669
|
-
return await fn(
|
|
670
|
-
|
|
671
|
-
workflowId,
|
|
672
|
-
mastra: this.mastra,
|
|
673
|
-
runtimeContext,
|
|
674
|
-
inputData: prevOutput,
|
|
675
|
-
runCount: -1,
|
|
676
|
-
tracingContext: {
|
|
677
|
-
currentSpan: sleepSpan
|
|
678
|
-
},
|
|
679
|
-
getInitData: () => stepResults?.input,
|
|
680
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
681
|
-
// TODO: this function shouldn't have suspend probably?
|
|
682
|
-
suspend: async (_suspendPayload) => {
|
|
683
|
-
},
|
|
684
|
-
bail: () => {
|
|
685
|
-
},
|
|
686
|
-
abort: () => {
|
|
687
|
-
abortController?.abort();
|
|
688
|
-
},
|
|
689
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
690
|
-
// TODO: add streamVNext support
|
|
691
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
692
|
-
engine: { step: this.inngestStep },
|
|
693
|
-
abortSignal: abortController?.signal,
|
|
694
|
-
writer: new tools.ToolStream(
|
|
835
|
+
return await fn(
|
|
836
|
+
workflows.createDeprecationProxy(
|
|
695
837
|
{
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
838
|
+
runId,
|
|
839
|
+
workflowId,
|
|
840
|
+
mastra: this.mastra,
|
|
841
|
+
runtimeContext,
|
|
842
|
+
inputData: prevOutput,
|
|
843
|
+
state: executionContext.state,
|
|
844
|
+
setState: (state) => {
|
|
845
|
+
executionContext.state = state;
|
|
846
|
+
},
|
|
847
|
+
runCount: -1,
|
|
848
|
+
retryCount: -1,
|
|
849
|
+
tracingContext: {
|
|
850
|
+
currentSpan: sleepSpan
|
|
851
|
+
},
|
|
852
|
+
getInitData: () => stepResults?.input,
|
|
853
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
854
|
+
// TODO: this function shouldn't have suspend probably?
|
|
855
|
+
suspend: async (_suspendPayload) => {
|
|
856
|
+
},
|
|
857
|
+
bail: () => {
|
|
858
|
+
},
|
|
859
|
+
abort: () => {
|
|
860
|
+
abortController?.abort();
|
|
861
|
+
},
|
|
862
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
863
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
864
|
+
engine: { step: this.inngestStep },
|
|
865
|
+
abortSignal: abortController?.signal,
|
|
866
|
+
writer: new tools.ToolStream(
|
|
867
|
+
{
|
|
868
|
+
prefix: "workflow-step",
|
|
869
|
+
callId: stepCallId,
|
|
870
|
+
name: "sleep",
|
|
871
|
+
runId
|
|
872
|
+
},
|
|
873
|
+
writableStream
|
|
874
|
+
)
|
|
700
875
|
},
|
|
701
|
-
|
|
876
|
+
{
|
|
877
|
+
paramName: "runCount",
|
|
878
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
879
|
+
logger: this.logger
|
|
880
|
+
}
|
|
702
881
|
)
|
|
703
|
-
|
|
882
|
+
);
|
|
704
883
|
});
|
|
705
884
|
sleepSpan?.update({
|
|
706
885
|
attributes: {
|
|
@@ -743,41 +922,54 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
743
922
|
if (fn) {
|
|
744
923
|
date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
|
|
745
924
|
const stepCallId = crypto.randomUUID();
|
|
746
|
-
return await fn(
|
|
747
|
-
|
|
748
|
-
workflowId,
|
|
749
|
-
mastra: this.mastra,
|
|
750
|
-
runtimeContext,
|
|
751
|
-
inputData: prevOutput,
|
|
752
|
-
runCount: -1,
|
|
753
|
-
tracingContext: {
|
|
754
|
-
currentSpan: sleepUntilSpan
|
|
755
|
-
},
|
|
756
|
-
getInitData: () => stepResults?.input,
|
|
757
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
758
|
-
// TODO: this function shouldn't have suspend probably?
|
|
759
|
-
suspend: async (_suspendPayload) => {
|
|
760
|
-
},
|
|
761
|
-
bail: () => {
|
|
762
|
-
},
|
|
763
|
-
abort: () => {
|
|
764
|
-
abortController?.abort();
|
|
765
|
-
},
|
|
766
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
767
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
768
|
-
// TODO: add streamVNext support
|
|
769
|
-
engine: { step: this.inngestStep },
|
|
770
|
-
abortSignal: abortController?.signal,
|
|
771
|
-
writer: new tools.ToolStream(
|
|
925
|
+
return await fn(
|
|
926
|
+
workflows.createDeprecationProxy(
|
|
772
927
|
{
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
928
|
+
runId,
|
|
929
|
+
workflowId,
|
|
930
|
+
mastra: this.mastra,
|
|
931
|
+
runtimeContext,
|
|
932
|
+
inputData: prevOutput,
|
|
933
|
+
state: executionContext.state,
|
|
934
|
+
setState: (state) => {
|
|
935
|
+
executionContext.state = state;
|
|
936
|
+
},
|
|
937
|
+
runCount: -1,
|
|
938
|
+
retryCount: -1,
|
|
939
|
+
tracingContext: {
|
|
940
|
+
currentSpan: sleepUntilSpan
|
|
941
|
+
},
|
|
942
|
+
getInitData: () => stepResults?.input,
|
|
943
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
944
|
+
// TODO: this function shouldn't have suspend probably?
|
|
945
|
+
suspend: async (_suspendPayload) => {
|
|
946
|
+
},
|
|
947
|
+
bail: () => {
|
|
948
|
+
},
|
|
949
|
+
abort: () => {
|
|
950
|
+
abortController?.abort();
|
|
951
|
+
},
|
|
952
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
953
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
954
|
+
engine: { step: this.inngestStep },
|
|
955
|
+
abortSignal: abortController?.signal,
|
|
956
|
+
writer: new tools.ToolStream(
|
|
957
|
+
{
|
|
958
|
+
prefix: "workflow-step",
|
|
959
|
+
callId: stepCallId,
|
|
960
|
+
name: "sleep",
|
|
961
|
+
runId
|
|
962
|
+
},
|
|
963
|
+
writableStream
|
|
964
|
+
)
|
|
777
965
|
},
|
|
778
|
-
|
|
966
|
+
{
|
|
967
|
+
paramName: "runCount",
|
|
968
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
969
|
+
logger: this.logger
|
|
970
|
+
}
|
|
779
971
|
)
|
|
780
|
-
|
|
972
|
+
);
|
|
781
973
|
});
|
|
782
974
|
if (date && !(date instanceof Date)) {
|
|
783
975
|
date = new Date(date);
|
|
@@ -879,38 +1071,60 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
879
1071
|
const isResume = !!resume?.steps?.length;
|
|
880
1072
|
let result;
|
|
881
1073
|
let runId;
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
1074
|
+
try {
|
|
1075
|
+
if (isResume) {
|
|
1076
|
+
runId = stepResults[resume?.steps?.[0]]?.suspendPayload?.__workflow_meta?.runId ?? crypto.randomUUID();
|
|
1077
|
+
const snapshot = await this.mastra?.getStorage()?.loadWorkflowSnapshot({
|
|
1078
|
+
workflowName: step.id,
|
|
1079
|
+
runId
|
|
1080
|
+
});
|
|
1081
|
+
const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
|
|
1082
|
+
function: step.getFunction(),
|
|
1083
|
+
data: {
|
|
1084
|
+
inputData,
|
|
1085
|
+
initialState: executionContext.state ?? snapshot?.value ?? {},
|
|
894
1086
|
runId,
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
1087
|
+
resume: {
|
|
1088
|
+
runId,
|
|
1089
|
+
steps: resume.steps.slice(1),
|
|
1090
|
+
stepResults: snapshot?.context,
|
|
1091
|
+
resumePayload: resume.resumePayload,
|
|
1092
|
+
// @ts-ignore
|
|
1093
|
+
resumePath: snapshot?.suspendedPaths?.[resume.steps?.[1]]
|
|
1094
|
+
},
|
|
1095
|
+
outputOptions: { includeState: true }
|
|
900
1096
|
}
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1097
|
+
});
|
|
1098
|
+
result = invokeResp.result;
|
|
1099
|
+
runId = invokeResp.runId;
|
|
1100
|
+
executionContext.state = invokeResp.result.state;
|
|
1101
|
+
} else {
|
|
1102
|
+
const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
|
|
1103
|
+
function: step.getFunction(),
|
|
1104
|
+
data: {
|
|
1105
|
+
inputData,
|
|
1106
|
+
initialState: executionContext.state ?? {},
|
|
1107
|
+
outputOptions: { includeState: true }
|
|
1108
|
+
}
|
|
1109
|
+
});
|
|
1110
|
+
result = invokeResp.result;
|
|
1111
|
+
runId = invokeResp.runId;
|
|
1112
|
+
executionContext.state = invokeResp.result.state;
|
|
1113
|
+
}
|
|
1114
|
+
} catch (e) {
|
|
1115
|
+
const errorCause = e?.cause;
|
|
1116
|
+
if (errorCause && typeof errorCause === "object") {
|
|
1117
|
+
result = errorCause;
|
|
1118
|
+
runId = errorCause.runId || crypto.randomUUID();
|
|
1119
|
+
} else {
|
|
1120
|
+
runId = crypto.randomUUID();
|
|
1121
|
+
result = {
|
|
1122
|
+
status: "failed",
|
|
1123
|
+
error: e instanceof Error ? e : new Error(String(e)),
|
|
1124
|
+
steps: {},
|
|
1125
|
+
input: inputData
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
914
1128
|
}
|
|
915
1129
|
const res = await this.inngestStep.run(
|
|
916
1130
|
`workflow.${executionContext.workflowId}.step.${step.id}.nestedwf-results`,
|
|
@@ -949,7 +1163,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
949
1163
|
return stepRes2?.status === "suspended";
|
|
950
1164
|
});
|
|
951
1165
|
for (const [stepName, stepResult] of suspendedSteps) {
|
|
952
|
-
const suspendPath = [stepName, ...stepResult?.
|
|
1166
|
+
const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
|
|
953
1167
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
954
1168
|
await emitter.emit("watch", {
|
|
955
1169
|
type: "watch",
|
|
@@ -957,7 +1171,11 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
957
1171
|
currentStep: {
|
|
958
1172
|
id: step.id,
|
|
959
1173
|
status: "suspended",
|
|
960
|
-
payload:
|
|
1174
|
+
payload: stepResult.payload,
|
|
1175
|
+
suspendPayload: {
|
|
1176
|
+
...stepResult?.suspendPayload,
|
|
1177
|
+
__workflow_meta: { runId, path: suspendPath }
|
|
1178
|
+
}
|
|
961
1179
|
},
|
|
962
1180
|
workflowState: {
|
|
963
1181
|
status: "running",
|
|
@@ -979,7 +1197,11 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
979
1197
|
executionContext,
|
|
980
1198
|
result: {
|
|
981
1199
|
status: "suspended",
|
|
982
|
-
payload:
|
|
1200
|
+
payload: stepResult.payload,
|
|
1201
|
+
suspendPayload: {
|
|
1202
|
+
...stepResult?.suspendPayload,
|
|
1203
|
+
__workflow_meta: { runId, path: suspendPath }
|
|
1204
|
+
}
|
|
983
1205
|
}
|
|
984
1206
|
};
|
|
985
1207
|
}
|
|
@@ -1044,132 +1266,186 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1044
1266
|
}
|
|
1045
1267
|
);
|
|
1046
1268
|
Object.assign(executionContext, res.executionContext);
|
|
1047
|
-
return
|
|
1269
|
+
return {
|
|
1270
|
+
...res.result,
|
|
1271
|
+
startedAt,
|
|
1272
|
+
endedAt: Date.now(),
|
|
1273
|
+
payload: inputData,
|
|
1274
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1275
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1276
|
+
};
|
|
1048
1277
|
}
|
|
1049
|
-
const
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1278
|
+
const stepCallId = crypto.randomUUID();
|
|
1279
|
+
let stepRes;
|
|
1280
|
+
try {
|
|
1281
|
+
stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
|
|
1282
|
+
let execResults;
|
|
1283
|
+
let suspended;
|
|
1284
|
+
let bailed;
|
|
1285
|
+
try {
|
|
1286
|
+
if (validationError) {
|
|
1287
|
+
throw validationError;
|
|
1288
|
+
}
|
|
1289
|
+
const result = await step.execute({
|
|
1290
|
+
runId: executionContext.runId,
|
|
1291
|
+
mastra: this.mastra,
|
|
1292
|
+
runtimeContext,
|
|
1293
|
+
writer: new tools.ToolStream(
|
|
1294
|
+
{
|
|
1295
|
+
prefix: "workflow-step",
|
|
1296
|
+
callId: stepCallId,
|
|
1297
|
+
name: step.id,
|
|
1298
|
+
runId: executionContext.runId
|
|
1299
|
+
},
|
|
1300
|
+
writableStream
|
|
1301
|
+
),
|
|
1302
|
+
state: executionContext?.state ?? {},
|
|
1303
|
+
setState: (state) => {
|
|
1304
|
+
executionContext.state = state;
|
|
1305
|
+
},
|
|
1306
|
+
inputData,
|
|
1307
|
+
resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
|
|
1308
|
+
tracingContext: {
|
|
1309
|
+
currentSpan: stepAISpan
|
|
1310
|
+
},
|
|
1311
|
+
getInitData: () => stepResults?.input,
|
|
1312
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1313
|
+
suspend: async (suspendPayload, suspendOptions) => {
|
|
1314
|
+
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
1315
|
+
if (suspendOptions?.resumeLabel) {
|
|
1316
|
+
const resumeLabel = Array.isArray(suspendOptions.resumeLabel) ? suspendOptions.resumeLabel : [suspendOptions.resumeLabel];
|
|
1317
|
+
for (const label of resumeLabel) {
|
|
1318
|
+
executionContext.resumeLabels[label] = {
|
|
1319
|
+
stepId: step.id,
|
|
1320
|
+
foreachIndex: executionContext.foreachIndex
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
suspended = { payload: suspendPayload };
|
|
1325
|
+
},
|
|
1326
|
+
bail: (result2) => {
|
|
1327
|
+
bailed = { payload: result2 };
|
|
1328
|
+
},
|
|
1329
|
+
resume: {
|
|
1330
|
+
steps: resume?.steps?.slice(1) || [],
|
|
1331
|
+
resumePayload: resume?.resumePayload,
|
|
1332
|
+
// @ts-ignore
|
|
1333
|
+
runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
|
|
1334
|
+
},
|
|
1335
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1336
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1337
|
+
engine: {
|
|
1338
|
+
step: this.inngestStep
|
|
1339
|
+
},
|
|
1340
|
+
abortSignal: abortController.signal
|
|
1341
|
+
});
|
|
1342
|
+
const endedAt = Date.now();
|
|
1343
|
+
execResults = {
|
|
1344
|
+
status: "success",
|
|
1345
|
+
output: result,
|
|
1346
|
+
startedAt,
|
|
1347
|
+
endedAt,
|
|
1348
|
+
payload: inputData,
|
|
1349
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1350
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1351
|
+
};
|
|
1352
|
+
} catch (e) {
|
|
1353
|
+
const stepFailure = {
|
|
1354
|
+
status: "failed",
|
|
1355
|
+
payload: inputData,
|
|
1356
|
+
error: e instanceof Error ? e.message : String(e),
|
|
1357
|
+
endedAt: Date.now(),
|
|
1358
|
+
startedAt,
|
|
1359
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1360
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1361
|
+
};
|
|
1362
|
+
execResults = stepFailure;
|
|
1363
|
+
const fallbackErrorMessage = `Step ${step.id} failed`;
|
|
1364
|
+
stepAISpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
|
|
1365
|
+
throw new inngest.RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
|
|
1366
|
+
cause: execResults
|
|
1367
|
+
});
|
|
1056
1368
|
}
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1369
|
+
if (suspended) {
|
|
1370
|
+
execResults = {
|
|
1371
|
+
status: "suspended",
|
|
1372
|
+
suspendPayload: suspended.payload,
|
|
1373
|
+
payload: inputData,
|
|
1374
|
+
suspendedAt: Date.now(),
|
|
1375
|
+
startedAt,
|
|
1376
|
+
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1377
|
+
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1378
|
+
};
|
|
1379
|
+
} else if (bailed) {
|
|
1380
|
+
execResults = {
|
|
1381
|
+
status: "bailed",
|
|
1382
|
+
output: bailed.payload,
|
|
1383
|
+
payload: inputData,
|
|
1384
|
+
endedAt: Date.now(),
|
|
1385
|
+
startedAt
|
|
1386
|
+
};
|
|
1387
|
+
}
|
|
1388
|
+
await emitter.emit("watch", {
|
|
1389
|
+
type: "watch",
|
|
1390
|
+
payload: {
|
|
1391
|
+
currentStep: {
|
|
1392
|
+
id: step.id,
|
|
1393
|
+
...execResults
|
|
1394
|
+
},
|
|
1395
|
+
workflowState: {
|
|
1396
|
+
status: "running",
|
|
1397
|
+
steps: { ...stepResults, [step.id]: execResults },
|
|
1398
|
+
result: null,
|
|
1399
|
+
error: null
|
|
1400
|
+
}
|
|
1085
1401
|
},
|
|
1086
|
-
|
|
1402
|
+
eventTimestamp: Date.now()
|
|
1087
1403
|
});
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
status: "suspended",
|
|
1112
|
-
suspendedPayload: suspended.payload,
|
|
1113
|
-
payload: inputData,
|
|
1114
|
-
suspendedAt: Date.now(),
|
|
1115
|
-
startedAt,
|
|
1116
|
-
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1117
|
-
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1118
|
-
};
|
|
1119
|
-
} else if (bailed) {
|
|
1120
|
-
execResults = { status: "bailed", output: bailed.payload, payload: inputData, endedAt: Date.now(), startedAt };
|
|
1121
|
-
}
|
|
1122
|
-
if (execResults.status === "failed") {
|
|
1123
|
-
if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
|
|
1124
|
-
const error = new Error(execResults.error);
|
|
1125
|
-
stepAISpan?.error({ error });
|
|
1126
|
-
throw error;
|
|
1404
|
+
if (execResults.status === "suspended") {
|
|
1405
|
+
await emitter.emit("watch-v2", {
|
|
1406
|
+
type: "workflow-step-suspended",
|
|
1407
|
+
payload: {
|
|
1408
|
+
id: step.id,
|
|
1409
|
+
...execResults
|
|
1410
|
+
}
|
|
1411
|
+
});
|
|
1412
|
+
} else {
|
|
1413
|
+
await emitter.emit("watch-v2", {
|
|
1414
|
+
type: "workflow-step-result",
|
|
1415
|
+
payload: {
|
|
1416
|
+
id: step.id,
|
|
1417
|
+
...execResults
|
|
1418
|
+
}
|
|
1419
|
+
});
|
|
1420
|
+
await emitter.emit("watch-v2", {
|
|
1421
|
+
type: "workflow-step-finish",
|
|
1422
|
+
payload: {
|
|
1423
|
+
id: step.id,
|
|
1424
|
+
metadata: {}
|
|
1425
|
+
}
|
|
1426
|
+
});
|
|
1127
1427
|
}
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
type: "watch",
|
|
1131
|
-
payload: {
|
|
1132
|
-
currentStep: {
|
|
1133
|
-
id: step.id,
|
|
1134
|
-
...execResults
|
|
1135
|
-
},
|
|
1136
|
-
workflowState: {
|
|
1137
|
-
status: "running",
|
|
1138
|
-
steps: { ...stepResults, [step.id]: execResults },
|
|
1139
|
-
result: null,
|
|
1140
|
-
error: null
|
|
1141
|
-
}
|
|
1142
|
-
},
|
|
1143
|
-
eventTimestamp: Date.now()
|
|
1428
|
+
stepAISpan?.end({ output: execResults });
|
|
1429
|
+
return { result: execResults, executionContext, stepResults };
|
|
1144
1430
|
});
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
payload: {
|
|
1164
|
-
id: step.id,
|
|
1165
|
-
metadata: {}
|
|
1166
|
-
}
|
|
1167
|
-
});
|
|
1168
|
-
}
|
|
1169
|
-
stepAISpan?.end({ output: execResults });
|
|
1170
|
-
return { result: execResults, executionContext, stepResults };
|
|
1171
|
-
});
|
|
1172
|
-
if (disableScorers !== false) {
|
|
1431
|
+
} catch (e) {
|
|
1432
|
+
const stepFailure = e instanceof Error ? e?.cause : {
|
|
1433
|
+
status: "failed",
|
|
1434
|
+
error: e instanceof Error ? e.message : String(e),
|
|
1435
|
+
payload: inputData,
|
|
1436
|
+
startedAt,
|
|
1437
|
+
endedAt: Date.now()
|
|
1438
|
+
};
|
|
1439
|
+
stepRes = {
|
|
1440
|
+
result: stepFailure,
|
|
1441
|
+
executionContext,
|
|
1442
|
+
stepResults: {
|
|
1443
|
+
...stepResults,
|
|
1444
|
+
[step.id]: stepFailure
|
|
1445
|
+
}
|
|
1446
|
+
};
|
|
1447
|
+
}
|
|
1448
|
+
if (disableScorers !== false && stepRes.result.status === "success") {
|
|
1173
1449
|
await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}.score`, async () => {
|
|
1174
1450
|
if (step.scorers) {
|
|
1175
1451
|
await this.runScorers({
|
|
@@ -1188,6 +1464,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1188
1464
|
}
|
|
1189
1465
|
Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
|
|
1190
1466
|
Object.assign(stepResults, stepRes.stepResults);
|
|
1467
|
+
executionContext.state = stepRes.executionContext.state;
|
|
1191
1468
|
return stepRes.result;
|
|
1192
1469
|
}
|
|
1193
1470
|
async persistStepUpdate({
|
|
@@ -1204,16 +1481,21 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1204
1481
|
await this.inngestStep.run(
|
|
1205
1482
|
`workflow.${workflowId}.run.${runId}.path.${JSON.stringify(executionContext.executionPath)}.stepUpdate`,
|
|
1206
1483
|
async () => {
|
|
1484
|
+
const shouldPersistSnapshot = this.options.shouldPersistSnapshot({ stepResults, workflowStatus });
|
|
1485
|
+
if (!shouldPersistSnapshot) {
|
|
1486
|
+
return;
|
|
1487
|
+
}
|
|
1207
1488
|
await this.mastra?.getStorage()?.persistWorkflowSnapshot({
|
|
1208
1489
|
workflowName: workflowId,
|
|
1209
1490
|
runId,
|
|
1210
1491
|
resourceId,
|
|
1211
1492
|
snapshot: {
|
|
1212
1493
|
runId,
|
|
1213
|
-
value:
|
|
1494
|
+
value: executionContext.state,
|
|
1214
1495
|
context: stepResults,
|
|
1215
1496
|
activePaths: [],
|
|
1216
1497
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1498
|
+
resumeLabels: executionContext.resumeLabels,
|
|
1217
1499
|
waitingPaths: {},
|
|
1218
1500
|
serializedStepGraph,
|
|
1219
1501
|
status: workflowStatus,
|
|
@@ -1231,9 +1513,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1231
1513
|
runId,
|
|
1232
1514
|
entry,
|
|
1233
1515
|
prevOutput,
|
|
1234
|
-
prevStep,
|
|
1235
1516
|
stepResults,
|
|
1236
|
-
serializedStepGraph,
|
|
1237
1517
|
resume,
|
|
1238
1518
|
executionContext,
|
|
1239
1519
|
emitter,
|
|
@@ -1266,43 +1546,56 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1266
1546
|
tracingPolicy: this.options?.tracingPolicy
|
|
1267
1547
|
});
|
|
1268
1548
|
try {
|
|
1269
|
-
const result = await cond(
|
|
1270
|
-
|
|
1271
|
-
workflowId,
|
|
1272
|
-
mastra: this.mastra,
|
|
1273
|
-
runtimeContext,
|
|
1274
|
-
runCount: -1,
|
|
1275
|
-
inputData: prevOutput,
|
|
1276
|
-
tracingContext: {
|
|
1277
|
-
currentSpan: evalSpan
|
|
1278
|
-
},
|
|
1279
|
-
getInitData: () => stepResults?.input,
|
|
1280
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1281
|
-
// TODO: this function shouldn't have suspend probably?
|
|
1282
|
-
suspend: async (_suspendPayload) => {
|
|
1283
|
-
},
|
|
1284
|
-
bail: () => {
|
|
1285
|
-
},
|
|
1286
|
-
abort: () => {
|
|
1287
|
-
abortController.abort();
|
|
1288
|
-
},
|
|
1289
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1290
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1291
|
-
// TODO: add streamVNext support
|
|
1292
|
-
engine: {
|
|
1293
|
-
step: this.inngestStep
|
|
1294
|
-
},
|
|
1295
|
-
abortSignal: abortController.signal,
|
|
1296
|
-
writer: new tools.ToolStream(
|
|
1549
|
+
const result = await cond(
|
|
1550
|
+
workflows.createDeprecationProxy(
|
|
1297
1551
|
{
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1552
|
+
runId,
|
|
1553
|
+
workflowId,
|
|
1554
|
+
mastra: this.mastra,
|
|
1555
|
+
runtimeContext,
|
|
1556
|
+
runCount: -1,
|
|
1557
|
+
retryCount: -1,
|
|
1558
|
+
inputData: prevOutput,
|
|
1559
|
+
state: executionContext.state,
|
|
1560
|
+
setState: (state) => {
|
|
1561
|
+
executionContext.state = state;
|
|
1562
|
+
},
|
|
1563
|
+
tracingContext: {
|
|
1564
|
+
currentSpan: evalSpan
|
|
1565
|
+
},
|
|
1566
|
+
getInitData: () => stepResults?.input,
|
|
1567
|
+
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1568
|
+
// TODO: this function shouldn't have suspend probably?
|
|
1569
|
+
suspend: async (_suspendPayload) => {
|
|
1570
|
+
},
|
|
1571
|
+
bail: () => {
|
|
1572
|
+
},
|
|
1573
|
+
abort: () => {
|
|
1574
|
+
abortController.abort();
|
|
1575
|
+
},
|
|
1576
|
+
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1577
|
+
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1578
|
+
engine: {
|
|
1579
|
+
step: this.inngestStep
|
|
1580
|
+
},
|
|
1581
|
+
abortSignal: abortController.signal,
|
|
1582
|
+
writer: new tools.ToolStream(
|
|
1583
|
+
{
|
|
1584
|
+
prefix: "workflow-step",
|
|
1585
|
+
callId: crypto.randomUUID(),
|
|
1586
|
+
name: "conditional",
|
|
1587
|
+
runId
|
|
1588
|
+
},
|
|
1589
|
+
writableStream
|
|
1590
|
+
)
|
|
1302
1591
|
},
|
|
1303
|
-
|
|
1592
|
+
{
|
|
1593
|
+
paramName: "runCount",
|
|
1594
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
1595
|
+
logger: this.logger
|
|
1596
|
+
}
|
|
1304
1597
|
)
|
|
1305
|
-
|
|
1598
|
+
);
|
|
1306
1599
|
evalSpan?.end({
|
|
1307
1600
|
output: result,
|
|
1308
1601
|
attributes: {
|
|
@@ -1330,13 +1623,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1330
1623
|
}
|
|
1331
1624
|
});
|
|
1332
1625
|
const results = await Promise.all(
|
|
1333
|
-
stepsToRun.map(
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1626
|
+
stepsToRun.map(async (step, index) => {
|
|
1627
|
+
const currStepResult = stepResults[step.step.id];
|
|
1628
|
+
if (currStepResult && currStepResult.status === "success") {
|
|
1629
|
+
return currStepResult;
|
|
1630
|
+
}
|
|
1631
|
+
const result = await this.executeStep({
|
|
1632
|
+
step: step.step,
|
|
1633
|
+
prevOutput,
|
|
1340
1634
|
stepResults,
|
|
1341
1635
|
resume,
|
|
1342
1636
|
executionContext: {
|
|
@@ -1344,8 +1638,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1344
1638
|
runId,
|
|
1345
1639
|
executionPath: [...executionContext.executionPath, index],
|
|
1346
1640
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1641
|
+
resumeLabels: executionContext.resumeLabels,
|
|
1347
1642
|
retryConfig: executionContext.retryConfig,
|
|
1348
|
-
|
|
1643
|
+
state: executionContext.state
|
|
1349
1644
|
},
|
|
1350
1645
|
emitter,
|
|
1351
1646
|
abortController,
|
|
@@ -1355,20 +1650,22 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1355
1650
|
tracingContext: {
|
|
1356
1651
|
currentSpan: conditionalSpan
|
|
1357
1652
|
}
|
|
1358
|
-
})
|
|
1359
|
-
|
|
1653
|
+
});
|
|
1654
|
+
stepResults[step.step.id] = result;
|
|
1655
|
+
return result;
|
|
1656
|
+
})
|
|
1360
1657
|
);
|
|
1361
|
-
const hasFailed = results.find((result) => result.
|
|
1362
|
-
const hasSuspended = results.find((result) => result.
|
|
1658
|
+
const hasFailed = results.find((result) => result.status === "failed");
|
|
1659
|
+
const hasSuspended = results.find((result) => result.status === "suspended");
|
|
1363
1660
|
if (hasFailed) {
|
|
1364
|
-
execResults = { status: "failed", error: hasFailed.
|
|
1661
|
+
execResults = { status: "failed", error: hasFailed.error };
|
|
1365
1662
|
} else if (hasSuspended) {
|
|
1366
|
-
execResults = { status: "suspended",
|
|
1663
|
+
execResults = { status: "suspended", suspendPayload: hasSuspended.suspendPayload };
|
|
1367
1664
|
} else {
|
|
1368
1665
|
execResults = {
|
|
1369
1666
|
status: "success",
|
|
1370
1667
|
output: results.reduce((acc, result, index) => {
|
|
1371
|
-
if (result.
|
|
1668
|
+
if (result.status === "success") {
|
|
1372
1669
|
acc[stepsToRun[index].step.id] = result.output;
|
|
1373
1670
|
}
|
|
1374
1671
|
return acc;
|