@mastra/inngest 0.0.0-break-rename-vnext-legacy-20251002212351 → 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 +212 -4
- package/dist/index.cjs +596 -326
- 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 +597 -327
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
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,48 +608,40 @@ function createStep(params) {
|
|
|
461
608
|
name: params.name,
|
|
462
609
|
args: inputData
|
|
463
610
|
};
|
|
464
|
-
|
|
465
|
-
|
|
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,
|
|
466
617
|
runtimeContext,
|
|
467
618
|
tracingContext,
|
|
468
619
|
onFinish: (result) => {
|
|
469
620
|
streamPromise.resolve(result.text);
|
|
621
|
+
void agentOptions?.onFinish?.(result);
|
|
470
622
|
},
|
|
471
623
|
abortSignal
|
|
472
624
|
});
|
|
473
|
-
|
|
474
|
-
return abort();
|
|
475
|
-
}
|
|
476
|
-
await emitter.emit("watch-v2", {
|
|
477
|
-
type: "tool-call-streaming-start",
|
|
478
|
-
...toolData ?? {}
|
|
479
|
-
});
|
|
480
|
-
for await (const chunk of fullStream) {
|
|
481
|
-
if (chunk.type === "text-delta") {
|
|
482
|
-
await emitter.emit("watch-v2", {
|
|
483
|
-
type: "tool-call-delta",
|
|
484
|
-
...toolData ?? {},
|
|
485
|
-
argsTextDelta: chunk.payload.text
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
}
|
|
625
|
+
stream = fullStream;
|
|
489
626
|
} else {
|
|
490
|
-
const
|
|
627
|
+
const modelOutput = await params.stream(inputData.prompt, {
|
|
628
|
+
...agentOptions ?? {},
|
|
491
629
|
runtimeContext,
|
|
492
630
|
tracingContext,
|
|
493
631
|
onFinish: (result) => {
|
|
494
632
|
streamPromise.resolve(result.text);
|
|
633
|
+
void agentOptions?.onFinish?.(result);
|
|
495
634
|
},
|
|
496
635
|
abortSignal
|
|
497
636
|
});
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
637
|
+
stream = modelOutput.fullStream;
|
|
638
|
+
}
|
|
639
|
+
if (streamFormat === "legacy") {
|
|
501
640
|
await emitter.emit("watch-v2", {
|
|
502
641
|
type: "tool-call-streaming-start",
|
|
503
642
|
...toolData ?? {}
|
|
504
643
|
});
|
|
505
|
-
for await (const chunk of
|
|
644
|
+
for await (const chunk of stream) {
|
|
506
645
|
if (chunk.type === "text-delta") {
|
|
507
646
|
await emitter.emit("watch-v2", {
|
|
508
647
|
type: "tool-call-delta",
|
|
@@ -511,11 +650,18 @@ function createStep(params) {
|
|
|
511
650
|
});
|
|
512
651
|
}
|
|
513
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);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (abortSignal.aborted) {
|
|
663
|
+
return abort();
|
|
514
664
|
}
|
|
515
|
-
await emitter.emit("watch-v2", {
|
|
516
|
-
type: "tool-call-streaming-finish",
|
|
517
|
-
...toolData ?? {}
|
|
518
|
-
});
|
|
519
665
|
return {
|
|
520
666
|
text: await streamPromise.promise
|
|
521
667
|
};
|
|
@@ -560,7 +706,10 @@ function createStep(params) {
|
|
|
560
706
|
function init(inngest) {
|
|
561
707
|
return {
|
|
562
708
|
createWorkflow(params) {
|
|
563
|
-
return new InngestWorkflow(
|
|
709
|
+
return new InngestWorkflow(
|
|
710
|
+
params,
|
|
711
|
+
inngest
|
|
712
|
+
);
|
|
564
713
|
},
|
|
565
714
|
createStep,
|
|
566
715
|
cloneStep(step, opts) {
|
|
@@ -569,6 +718,9 @@ function init(inngest) {
|
|
|
569
718
|
description: step.description,
|
|
570
719
|
inputSchema: step.inputSchema,
|
|
571
720
|
outputSchema: step.outputSchema,
|
|
721
|
+
resumeSchema: step.resumeSchema,
|
|
722
|
+
suspendSchema: step.suspendSchema,
|
|
723
|
+
stateSchema: step.stateSchema,
|
|
572
724
|
execute: step.execute,
|
|
573
725
|
component: step.component
|
|
574
726
|
};
|
|
@@ -595,19 +747,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
595
747
|
this.inngestStep = inngestStep;
|
|
596
748
|
this.inngestAttempts = inngestAttempts;
|
|
597
749
|
}
|
|
598
|
-
async
|
|
599
|
-
await params.emitter.emit("watch-v2", {
|
|
600
|
-
type: "workflow-start",
|
|
601
|
-
payload: { runId: params.runId }
|
|
602
|
-
});
|
|
603
|
-
const result = await super.execute(params);
|
|
604
|
-
await params.emitter.emit("watch-v2", {
|
|
605
|
-
type: "workflow-finish",
|
|
606
|
-
payload: { runId: params.runId }
|
|
607
|
-
});
|
|
608
|
-
return result;
|
|
609
|
-
}
|
|
610
|
-
async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
|
|
750
|
+
async fmtReturnValue(emitter, stepResults, lastOutput, error) {
|
|
611
751
|
const base = {
|
|
612
752
|
status: lastOutput.status,
|
|
613
753
|
steps: stepResults
|
|
@@ -654,14 +794,13 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
654
794
|
});
|
|
655
795
|
const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
|
|
656
796
|
if (stepResult?.status === "suspended") {
|
|
657
|
-
const nestedPath = stepResult?.
|
|
797
|
+
const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
|
|
658
798
|
return nestedPath ? [[stepId, ...nestedPath]] : [[stepId]];
|
|
659
799
|
}
|
|
660
800
|
return [];
|
|
661
801
|
});
|
|
662
802
|
base.suspended = suspendedStepIds;
|
|
663
803
|
}
|
|
664
|
-
executionSpan?.end();
|
|
665
804
|
return base;
|
|
666
805
|
}
|
|
667
806
|
// async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
|
|
@@ -693,41 +832,54 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
693
832
|
if (fn) {
|
|
694
833
|
const stepCallId = crypto.randomUUID();
|
|
695
834
|
duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
|
|
696
|
-
return await fn(
|
|
697
|
-
|
|
698
|
-
workflowId,
|
|
699
|
-
mastra: this.mastra,
|
|
700
|
-
runtimeContext,
|
|
701
|
-
inputData: prevOutput,
|
|
702
|
-
runCount: -1,
|
|
703
|
-
tracingContext: {
|
|
704
|
-
currentSpan: sleepSpan
|
|
705
|
-
},
|
|
706
|
-
getInitData: () => stepResults?.input,
|
|
707
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
708
|
-
// TODO: this function shouldn't have suspend probably?
|
|
709
|
-
suspend: async (_suspendPayload) => {
|
|
710
|
-
},
|
|
711
|
-
bail: () => {
|
|
712
|
-
},
|
|
713
|
-
abort: () => {
|
|
714
|
-
abortController?.abort();
|
|
715
|
-
},
|
|
716
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
717
|
-
// TODO: add streamVNext support
|
|
718
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
719
|
-
engine: { step: this.inngestStep },
|
|
720
|
-
abortSignal: abortController?.signal,
|
|
721
|
-
writer: new tools.ToolStream(
|
|
835
|
+
return await fn(
|
|
836
|
+
workflows.createDeprecationProxy(
|
|
722
837
|
{
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
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
|
+
)
|
|
727
875
|
},
|
|
728
|
-
|
|
876
|
+
{
|
|
877
|
+
paramName: "runCount",
|
|
878
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
879
|
+
logger: this.logger
|
|
880
|
+
}
|
|
729
881
|
)
|
|
730
|
-
|
|
882
|
+
);
|
|
731
883
|
});
|
|
732
884
|
sleepSpan?.update({
|
|
733
885
|
attributes: {
|
|
@@ -770,41 +922,54 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
770
922
|
if (fn) {
|
|
771
923
|
date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
|
|
772
924
|
const stepCallId = crypto.randomUUID();
|
|
773
|
-
return await fn(
|
|
774
|
-
|
|
775
|
-
workflowId,
|
|
776
|
-
mastra: this.mastra,
|
|
777
|
-
runtimeContext,
|
|
778
|
-
inputData: prevOutput,
|
|
779
|
-
runCount: -1,
|
|
780
|
-
tracingContext: {
|
|
781
|
-
currentSpan: sleepUntilSpan
|
|
782
|
-
},
|
|
783
|
-
getInitData: () => stepResults?.input,
|
|
784
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
785
|
-
// TODO: this function shouldn't have suspend probably?
|
|
786
|
-
suspend: async (_suspendPayload) => {
|
|
787
|
-
},
|
|
788
|
-
bail: () => {
|
|
789
|
-
},
|
|
790
|
-
abort: () => {
|
|
791
|
-
abortController?.abort();
|
|
792
|
-
},
|
|
793
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
794
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
795
|
-
// TODO: add streamVNext support
|
|
796
|
-
engine: { step: this.inngestStep },
|
|
797
|
-
abortSignal: abortController?.signal,
|
|
798
|
-
writer: new tools.ToolStream(
|
|
925
|
+
return await fn(
|
|
926
|
+
workflows.createDeprecationProxy(
|
|
799
927
|
{
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
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
|
+
)
|
|
804
965
|
},
|
|
805
|
-
|
|
966
|
+
{
|
|
967
|
+
paramName: "runCount",
|
|
968
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
969
|
+
logger: this.logger
|
|
970
|
+
}
|
|
806
971
|
)
|
|
807
|
-
|
|
972
|
+
);
|
|
808
973
|
});
|
|
809
974
|
if (date && !(date instanceof Date)) {
|
|
810
975
|
date = new Date(date);
|
|
@@ -906,38 +1071,60 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
906
1071
|
const isResume = !!resume?.steps?.length;
|
|
907
1072
|
let result;
|
|
908
1073
|
let runId;
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
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 ?? {},
|
|
921
1086
|
runId,
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
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 }
|
|
927
1096
|
}
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
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
|
+
}
|
|
941
1128
|
}
|
|
942
1129
|
const res = await this.inngestStep.run(
|
|
943
1130
|
`workflow.${executionContext.workflowId}.step.${step.id}.nestedwf-results`,
|
|
@@ -976,7 +1163,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
976
1163
|
return stepRes2?.status === "suspended";
|
|
977
1164
|
});
|
|
978
1165
|
for (const [stepName, stepResult] of suspendedSteps) {
|
|
979
|
-
const suspendPath = [stepName, ...stepResult?.
|
|
1166
|
+
const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
|
|
980
1167
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
981
1168
|
await emitter.emit("watch", {
|
|
982
1169
|
type: "watch",
|
|
@@ -984,7 +1171,11 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
984
1171
|
currentStep: {
|
|
985
1172
|
id: step.id,
|
|
986
1173
|
status: "suspended",
|
|
987
|
-
payload:
|
|
1174
|
+
payload: stepResult.payload,
|
|
1175
|
+
suspendPayload: {
|
|
1176
|
+
...stepResult?.suspendPayload,
|
|
1177
|
+
__workflow_meta: { runId, path: suspendPath }
|
|
1178
|
+
}
|
|
988
1179
|
},
|
|
989
1180
|
workflowState: {
|
|
990
1181
|
status: "running",
|
|
@@ -1006,7 +1197,11 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1006
1197
|
executionContext,
|
|
1007
1198
|
result: {
|
|
1008
1199
|
status: "suspended",
|
|
1009
|
-
payload:
|
|
1200
|
+
payload: stepResult.payload,
|
|
1201
|
+
suspendPayload: {
|
|
1202
|
+
...stepResult?.suspendPayload,
|
|
1203
|
+
__workflow_meta: { runId, path: suspendPath }
|
|
1204
|
+
}
|
|
1010
1205
|
}
|
|
1011
1206
|
};
|
|
1012
1207
|
}
|
|
@@ -1071,132 +1266,186 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1071
1266
|
}
|
|
1072
1267
|
);
|
|
1073
1268
|
Object.assign(executionContext, res.executionContext);
|
|
1074
|
-
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
|
+
};
|
|
1075
1277
|
}
|
|
1076
|
-
const
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
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
|
+
});
|
|
1083
1368
|
}
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
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
|
+
}
|
|
1112
1401
|
},
|
|
1113
|
-
|
|
1402
|
+
eventTimestamp: Date.now()
|
|
1114
1403
|
});
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
status: "suspended",
|
|
1139
|
-
suspendedPayload: suspended.payload,
|
|
1140
|
-
payload: inputData,
|
|
1141
|
-
suspendedAt: Date.now(),
|
|
1142
|
-
startedAt,
|
|
1143
|
-
resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
|
|
1144
|
-
resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
|
|
1145
|
-
};
|
|
1146
|
-
} else if (bailed) {
|
|
1147
|
-
execResults = { status: "bailed", output: bailed.payload, payload: inputData, endedAt: Date.now(), startedAt };
|
|
1148
|
-
}
|
|
1149
|
-
if (execResults.status === "failed") {
|
|
1150
|
-
if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
|
|
1151
|
-
const error = new Error(execResults.error);
|
|
1152
|
-
stepAISpan?.error({ error });
|
|
1153
|
-
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
|
+
});
|
|
1154
1427
|
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
type: "watch",
|
|
1158
|
-
payload: {
|
|
1159
|
-
currentStep: {
|
|
1160
|
-
id: step.id,
|
|
1161
|
-
...execResults
|
|
1162
|
-
},
|
|
1163
|
-
workflowState: {
|
|
1164
|
-
status: "running",
|
|
1165
|
-
steps: { ...stepResults, [step.id]: execResults },
|
|
1166
|
-
result: null,
|
|
1167
|
-
error: null
|
|
1168
|
-
}
|
|
1169
|
-
},
|
|
1170
|
-
eventTimestamp: Date.now()
|
|
1428
|
+
stepAISpan?.end({ output: execResults });
|
|
1429
|
+
return { result: execResults, executionContext, stepResults };
|
|
1171
1430
|
});
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
payload: {
|
|
1191
|
-
id: step.id,
|
|
1192
|
-
metadata: {}
|
|
1193
|
-
}
|
|
1194
|
-
});
|
|
1195
|
-
}
|
|
1196
|
-
stepAISpan?.end({ output: execResults });
|
|
1197
|
-
return { result: execResults, executionContext, stepResults };
|
|
1198
|
-
});
|
|
1199
|
-
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") {
|
|
1200
1449
|
await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}.score`, async () => {
|
|
1201
1450
|
if (step.scorers) {
|
|
1202
1451
|
await this.runScorers({
|
|
@@ -1215,6 +1464,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1215
1464
|
}
|
|
1216
1465
|
Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
|
|
1217
1466
|
Object.assign(stepResults, stepRes.stepResults);
|
|
1467
|
+
executionContext.state = stepRes.executionContext.state;
|
|
1218
1468
|
return stepRes.result;
|
|
1219
1469
|
}
|
|
1220
1470
|
async persistStepUpdate({
|
|
@@ -1231,16 +1481,21 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1231
1481
|
await this.inngestStep.run(
|
|
1232
1482
|
`workflow.${workflowId}.run.${runId}.path.${JSON.stringify(executionContext.executionPath)}.stepUpdate`,
|
|
1233
1483
|
async () => {
|
|
1484
|
+
const shouldPersistSnapshot = this.options.shouldPersistSnapshot({ stepResults, workflowStatus });
|
|
1485
|
+
if (!shouldPersistSnapshot) {
|
|
1486
|
+
return;
|
|
1487
|
+
}
|
|
1234
1488
|
await this.mastra?.getStorage()?.persistWorkflowSnapshot({
|
|
1235
1489
|
workflowName: workflowId,
|
|
1236
1490
|
runId,
|
|
1237
1491
|
resourceId,
|
|
1238
1492
|
snapshot: {
|
|
1239
1493
|
runId,
|
|
1240
|
-
value:
|
|
1494
|
+
value: executionContext.state,
|
|
1241
1495
|
context: stepResults,
|
|
1242
1496
|
activePaths: [],
|
|
1243
1497
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1498
|
+
resumeLabels: executionContext.resumeLabels,
|
|
1244
1499
|
waitingPaths: {},
|
|
1245
1500
|
serializedStepGraph,
|
|
1246
1501
|
status: workflowStatus,
|
|
@@ -1258,9 +1513,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1258
1513
|
runId,
|
|
1259
1514
|
entry,
|
|
1260
1515
|
prevOutput,
|
|
1261
|
-
prevStep,
|
|
1262
1516
|
stepResults,
|
|
1263
|
-
serializedStepGraph,
|
|
1264
1517
|
resume,
|
|
1265
1518
|
executionContext,
|
|
1266
1519
|
emitter,
|
|
@@ -1293,43 +1546,56 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1293
1546
|
tracingPolicy: this.options?.tracingPolicy
|
|
1294
1547
|
});
|
|
1295
1548
|
try {
|
|
1296
|
-
const result = await cond(
|
|
1297
|
-
|
|
1298
|
-
workflowId,
|
|
1299
|
-
mastra: this.mastra,
|
|
1300
|
-
runtimeContext,
|
|
1301
|
-
runCount: -1,
|
|
1302
|
-
inputData: prevOutput,
|
|
1303
|
-
tracingContext: {
|
|
1304
|
-
currentSpan: evalSpan
|
|
1305
|
-
},
|
|
1306
|
-
getInitData: () => stepResults?.input,
|
|
1307
|
-
getStepResult: workflows.getStepResult.bind(this, stepResults),
|
|
1308
|
-
// TODO: this function shouldn't have suspend probably?
|
|
1309
|
-
suspend: async (_suspendPayload) => {
|
|
1310
|
-
},
|
|
1311
|
-
bail: () => {
|
|
1312
|
-
},
|
|
1313
|
-
abort: () => {
|
|
1314
|
-
abortController.abort();
|
|
1315
|
-
},
|
|
1316
|
-
[_constants.EMITTER_SYMBOL]: emitter,
|
|
1317
|
-
[_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
1318
|
-
// TODO: add streamVNext support
|
|
1319
|
-
engine: {
|
|
1320
|
-
step: this.inngestStep
|
|
1321
|
-
},
|
|
1322
|
-
abortSignal: abortController.signal,
|
|
1323
|
-
writer: new tools.ToolStream(
|
|
1549
|
+
const result = await cond(
|
|
1550
|
+
workflows.createDeprecationProxy(
|
|
1324
1551
|
{
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
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
|
+
)
|
|
1329
1591
|
},
|
|
1330
|
-
|
|
1592
|
+
{
|
|
1593
|
+
paramName: "runCount",
|
|
1594
|
+
deprecationMessage: workflows.runCountDeprecationMessage,
|
|
1595
|
+
logger: this.logger
|
|
1596
|
+
}
|
|
1331
1597
|
)
|
|
1332
|
-
|
|
1598
|
+
);
|
|
1333
1599
|
evalSpan?.end({
|
|
1334
1600
|
output: result,
|
|
1335
1601
|
attributes: {
|
|
@@ -1357,13 +1623,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1357
1623
|
}
|
|
1358
1624
|
});
|
|
1359
1625
|
const results = await Promise.all(
|
|
1360
|
-
stepsToRun.map(
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
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,
|
|
1367
1634
|
stepResults,
|
|
1368
1635
|
resume,
|
|
1369
1636
|
executionContext: {
|
|
@@ -1371,8 +1638,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1371
1638
|
runId,
|
|
1372
1639
|
executionPath: [...executionContext.executionPath, index],
|
|
1373
1640
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1641
|
+
resumeLabels: executionContext.resumeLabels,
|
|
1374
1642
|
retryConfig: executionContext.retryConfig,
|
|
1375
|
-
|
|
1643
|
+
state: executionContext.state
|
|
1376
1644
|
},
|
|
1377
1645
|
emitter,
|
|
1378
1646
|
abortController,
|
|
@@ -1382,20 +1650,22 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
|
|
|
1382
1650
|
tracingContext: {
|
|
1383
1651
|
currentSpan: conditionalSpan
|
|
1384
1652
|
}
|
|
1385
|
-
})
|
|
1386
|
-
|
|
1653
|
+
});
|
|
1654
|
+
stepResults[step.step.id] = result;
|
|
1655
|
+
return result;
|
|
1656
|
+
})
|
|
1387
1657
|
);
|
|
1388
|
-
const hasFailed = results.find((result) => result.
|
|
1389
|
-
const hasSuspended = results.find((result) => result.
|
|
1658
|
+
const hasFailed = results.find((result) => result.status === "failed");
|
|
1659
|
+
const hasSuspended = results.find((result) => result.status === "suspended");
|
|
1390
1660
|
if (hasFailed) {
|
|
1391
|
-
execResults = { status: "failed", error: hasFailed.
|
|
1661
|
+
execResults = { status: "failed", error: hasFailed.error };
|
|
1392
1662
|
} else if (hasSuspended) {
|
|
1393
|
-
execResults = { status: "suspended",
|
|
1663
|
+
execResults = { status: "suspended", suspendPayload: hasSuspended.suspendPayload };
|
|
1394
1664
|
} else {
|
|
1395
1665
|
execResults = {
|
|
1396
1666
|
status: "success",
|
|
1397
1667
|
output: results.reduce((acc, result, index) => {
|
|
1398
|
-
if (result.
|
|
1668
|
+
if (result.status === "success") {
|
|
1399
1669
|
acc[stepsToRun[index].step.id] = result.output;
|
|
1400
1670
|
}
|
|
1401
1671
|
return acc;
|