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