@mastra/inngest 0.0.0-bundle-recursion-20251030002519 → 0.0.0-client-js-listmessages-agentid-fix-20251119175531
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 +350 -3
- package/dist/index.cjs +337 -270
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +80 -57
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +339 -272
- package/dist/index.js.map +1 -1
- package/package.json +18 -13
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
|
-
import { ReadableStream } from 'stream/web';
|
|
2
|
+
import { ReadableStream, WritableStream } from 'stream/web';
|
|
3
3
|
import { subscribe } from '@inngest/realtime';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { RequestContext } from '@mastra/core/di';
|
|
5
|
+
import { SpanType } from '@mastra/core/observability';
|
|
6
6
|
import { ChunkFrom, WorkflowRunOutput } from '@mastra/core/stream';
|
|
7
7
|
import { ToolStream, Tool } from '@mastra/core/tools';
|
|
8
|
-
import { Run, Workflow, DefaultExecutionEngine, createDeprecationProxy, getStepResult, runCountDeprecationMessage, validateStepInput } from '@mastra/core/workflows';
|
|
8
|
+
import { Run, createTimeTravelExecutionParams, Workflow, DefaultExecutionEngine, createDeprecationProxy, getStepResult, runCountDeprecationMessage, validateStepInput, validateStepResumeData } from '@mastra/core/workflows';
|
|
9
9
|
import { EMITTER_SYMBOL, STREAM_FORMAT_SYMBOL } from '@mastra/core/workflows/_constants';
|
|
10
10
|
import { NonRetriableError, RetryAfterError } from 'inngest';
|
|
11
11
|
import { serve as serve$1 } from 'inngest/hono';
|
|
@@ -18,7 +18,7 @@ function serve({
|
|
|
18
18
|
functions: userFunctions = [],
|
|
19
19
|
registerOptions
|
|
20
20
|
}) {
|
|
21
|
-
const wfs = mastra.
|
|
21
|
+
const wfs = mastra.listWorkflows();
|
|
22
22
|
const workflowFunctions = Array.from(
|
|
23
23
|
new Set(
|
|
24
24
|
Object.values(wfs).flatMap((wf) => {
|
|
@@ -57,11 +57,12 @@ var InngestRun = class extends Run {
|
|
|
57
57
|
}
|
|
58
58
|
async getRunOutput(eventId) {
|
|
59
59
|
let runs = await this.getRuns(eventId);
|
|
60
|
+
const storage = this.#mastra?.getStorage();
|
|
60
61
|
while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
|
|
61
62
|
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
62
63
|
runs = await this.getRuns(eventId);
|
|
63
64
|
if (runs?.[0]?.status === "Failed") {
|
|
64
|
-
const snapshot = await
|
|
65
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
65
66
|
workflowName: this.workflowId,
|
|
66
67
|
runId: this.runId
|
|
67
68
|
});
|
|
@@ -70,7 +71,7 @@ var InngestRun = class extends Run {
|
|
|
70
71
|
};
|
|
71
72
|
}
|
|
72
73
|
if (runs?.[0]?.status === "Cancelled") {
|
|
73
|
-
const snapshot = await
|
|
74
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
74
75
|
workflowName: this.workflowId,
|
|
75
76
|
runId: this.runId
|
|
76
77
|
});
|
|
@@ -79,31 +80,27 @@ var InngestRun = class extends Run {
|
|
|
79
80
|
}
|
|
80
81
|
return runs?.[0];
|
|
81
82
|
}
|
|
82
|
-
async sendEvent(event, data) {
|
|
83
|
-
await this.inngest.send({
|
|
84
|
-
name: `user-event-${event}`,
|
|
85
|
-
data
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
83
|
async cancel() {
|
|
84
|
+
const storage = this.#mastra?.getStorage();
|
|
89
85
|
await this.inngest.send({
|
|
90
86
|
name: `cancel.workflow.${this.workflowId}`,
|
|
91
87
|
data: {
|
|
92
88
|
runId: this.runId
|
|
93
89
|
}
|
|
94
90
|
});
|
|
95
|
-
const snapshot = await
|
|
91
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
96
92
|
workflowName: this.workflowId,
|
|
97
93
|
runId: this.runId
|
|
98
94
|
});
|
|
99
95
|
if (snapshot) {
|
|
100
|
-
await
|
|
96
|
+
await storage?.persistWorkflowSnapshot({
|
|
101
97
|
workflowName: this.workflowId,
|
|
102
98
|
runId: this.runId,
|
|
103
99
|
resourceId: this.resourceId,
|
|
104
100
|
snapshot: {
|
|
105
101
|
...snapshot,
|
|
106
|
-
status: "canceled"
|
|
102
|
+
status: "canceled",
|
|
103
|
+
value: snapshot.value
|
|
107
104
|
}
|
|
108
105
|
});
|
|
109
106
|
}
|
|
@@ -125,14 +122,15 @@ var InngestRun = class extends Run {
|
|
|
125
122
|
snapshot: {
|
|
126
123
|
runId: this.runId,
|
|
127
124
|
serializedStepGraph: this.serializedStepGraph,
|
|
125
|
+
status: "running",
|
|
128
126
|
value: {},
|
|
129
127
|
context: {},
|
|
130
128
|
activePaths: [],
|
|
131
129
|
suspendedPaths: {},
|
|
130
|
+
activeStepsPath: {},
|
|
132
131
|
resumeLabels: {},
|
|
133
132
|
waitingPaths: {},
|
|
134
|
-
timestamp: Date.now()
|
|
135
|
-
status: "running"
|
|
133
|
+
timestamp: Date.now()
|
|
136
134
|
}
|
|
137
135
|
});
|
|
138
136
|
const inputDataToUse = await this._validateInput(inputData);
|
|
@@ -175,10 +173,16 @@ var InngestRun = class extends Run {
|
|
|
175
173
|
return p;
|
|
176
174
|
}
|
|
177
175
|
async _resume(params) {
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
)
|
|
181
|
-
|
|
176
|
+
const storage = this.#mastra?.getStorage();
|
|
177
|
+
let steps = [];
|
|
178
|
+
if (typeof params.step === "string") {
|
|
179
|
+
steps = params.step.split(".");
|
|
180
|
+
} else {
|
|
181
|
+
steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
182
|
+
(step) => typeof step === "string" ? step : step?.id
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
182
186
|
workflowName: this.workflowId,
|
|
183
187
|
runId: this.runId
|
|
184
188
|
});
|
|
@@ -196,9 +200,99 @@ var InngestRun = class extends Run {
|
|
|
196
200
|
steps,
|
|
197
201
|
stepResults: snapshot?.context,
|
|
198
202
|
resumePayload: resumeDataToUse,
|
|
199
|
-
|
|
200
|
-
|
|
203
|
+
resumePath: steps?.[0] ? snapshot?.suspendedPaths?.[steps?.[0]] : void 0
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
const eventId = eventOutput.ids[0];
|
|
208
|
+
if (!eventId) {
|
|
209
|
+
throw new Error("Event ID is not set");
|
|
210
|
+
}
|
|
211
|
+
const runOutput = await this.getRunOutput(eventId);
|
|
212
|
+
const result = runOutput?.output?.result;
|
|
213
|
+
if (result.status === "failed") {
|
|
214
|
+
result.error = new Error(result.error);
|
|
215
|
+
}
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
218
|
+
async timeTravel(params) {
|
|
219
|
+
const p = this._timeTravel(params).then((result) => {
|
|
220
|
+
if (result.status !== "suspended") {
|
|
221
|
+
this.closeStreamAction?.().catch(() => {
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
});
|
|
226
|
+
this.executionResults = p;
|
|
227
|
+
return p;
|
|
228
|
+
}
|
|
229
|
+
async _timeTravel(params) {
|
|
230
|
+
if (!params.step || Array.isArray(params.step) && params.step?.length === 0) {
|
|
231
|
+
throw new Error("Step is required and must be a valid step or array of steps");
|
|
232
|
+
}
|
|
233
|
+
let steps = [];
|
|
234
|
+
if (typeof params.step === "string") {
|
|
235
|
+
steps = params.step.split(".");
|
|
236
|
+
} else {
|
|
237
|
+
steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
238
|
+
(step) => typeof step === "string" ? step : step?.id
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
if (steps.length === 0) {
|
|
242
|
+
throw new Error("No steps provided to timeTravel");
|
|
243
|
+
}
|
|
244
|
+
const storage = this.#mastra?.getStorage();
|
|
245
|
+
const snapshot = await storage?.loadWorkflowSnapshot({
|
|
246
|
+
workflowName: this.workflowId,
|
|
247
|
+
runId: this.runId
|
|
248
|
+
});
|
|
249
|
+
if (!snapshot) {
|
|
250
|
+
await storage?.persistWorkflowSnapshot({
|
|
251
|
+
workflowName: this.workflowId,
|
|
252
|
+
runId: this.runId,
|
|
253
|
+
resourceId: this.resourceId,
|
|
254
|
+
snapshot: {
|
|
255
|
+
runId: this.runId,
|
|
256
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
257
|
+
status: "pending",
|
|
258
|
+
value: {},
|
|
259
|
+
context: {},
|
|
260
|
+
activePaths: [],
|
|
261
|
+
suspendedPaths: {},
|
|
262
|
+
activeStepsPath: {},
|
|
263
|
+
resumeLabels: {},
|
|
264
|
+
waitingPaths: {},
|
|
265
|
+
timestamp: Date.now()
|
|
201
266
|
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
if (snapshot?.status === "running") {
|
|
270
|
+
throw new Error("This workflow run is still running, cannot time travel");
|
|
271
|
+
}
|
|
272
|
+
let inputDataToUse = params.inputData;
|
|
273
|
+
if (inputDataToUse && steps.length === 1) {
|
|
274
|
+
inputDataToUse = await this._validateTimetravelInputData(params.inputData, this.workflowSteps[steps[0]]);
|
|
275
|
+
}
|
|
276
|
+
const timeTravelData = createTimeTravelExecutionParams({
|
|
277
|
+
steps,
|
|
278
|
+
inputData: inputDataToUse,
|
|
279
|
+
resumeData: params.resumeData,
|
|
280
|
+
context: params.context,
|
|
281
|
+
nestedStepsContext: params.nestedStepsContext,
|
|
282
|
+
snapshot: snapshot ?? { context: {} },
|
|
283
|
+
graph: this.executionGraph,
|
|
284
|
+
initialState: params.initialState
|
|
285
|
+
});
|
|
286
|
+
const eventOutput = await this.inngest.send({
|
|
287
|
+
name: `workflow.${this.workflowId}`,
|
|
288
|
+
data: {
|
|
289
|
+
initialState: timeTravelData.state,
|
|
290
|
+
runId: this.runId,
|
|
291
|
+
workflowId: this.workflowId,
|
|
292
|
+
stepResults: timeTravelData.stepResults,
|
|
293
|
+
timeTravel: timeTravelData,
|
|
294
|
+
tracingOptions: params.tracingOptions,
|
|
295
|
+
outputOptions: params.outputOptions
|
|
202
296
|
}
|
|
203
297
|
});
|
|
204
298
|
const eventId = eventOutput.ids[0];
|
|
@@ -212,12 +306,12 @@ var InngestRun = class extends Run {
|
|
|
212
306
|
}
|
|
213
307
|
return result;
|
|
214
308
|
}
|
|
215
|
-
watch(cb
|
|
309
|
+
watch(cb) {
|
|
216
310
|
let active = true;
|
|
217
311
|
const streamPromise = subscribe(
|
|
218
312
|
{
|
|
219
313
|
channel: `workflow:${this.workflowId}:${this.runId}`,
|
|
220
|
-
topics: [
|
|
314
|
+
topics: ["watch"],
|
|
221
315
|
app: this.inngest
|
|
222
316
|
},
|
|
223
317
|
(message) => {
|
|
@@ -235,7 +329,7 @@ var InngestRun = class extends Run {
|
|
|
235
329
|
});
|
|
236
330
|
};
|
|
237
331
|
}
|
|
238
|
-
streamLegacy({ inputData,
|
|
332
|
+
streamLegacy({ inputData, requestContext } = {}) {
|
|
239
333
|
const { readable, writable } = new TransformStream();
|
|
240
334
|
const writer = writable.getWriter();
|
|
241
335
|
const unwatch = this.watch(async (event) => {
|
|
@@ -257,7 +351,7 @@ var InngestRun = class extends Run {
|
|
|
257
351
|
await writer.write(e);
|
|
258
352
|
} catch {
|
|
259
353
|
}
|
|
260
|
-
}
|
|
354
|
+
});
|
|
261
355
|
this.closeStreamAction = async () => {
|
|
262
356
|
await writer.write({
|
|
263
357
|
type: "finish",
|
|
@@ -273,7 +367,7 @@ var InngestRun = class extends Run {
|
|
|
273
367
|
writer.releaseLock();
|
|
274
368
|
}
|
|
275
369
|
};
|
|
276
|
-
this.executionResults = this._start({ inputData,
|
|
370
|
+
this.executionResults = this._start({ inputData, requestContext, format: "legacy" }).then((result) => {
|
|
277
371
|
if (result.status !== "suspended") {
|
|
278
372
|
this.closeStreamAction?.().catch(() => {
|
|
279
373
|
});
|
|
@@ -287,7 +381,7 @@ var InngestRun = class extends Run {
|
|
|
287
381
|
}
|
|
288
382
|
stream({
|
|
289
383
|
inputData,
|
|
290
|
-
|
|
384
|
+
requestContext,
|
|
291
385
|
tracingOptions,
|
|
292
386
|
closeOnSuspend = true,
|
|
293
387
|
initialState,
|
|
@@ -311,7 +405,7 @@ var InngestRun = class extends Run {
|
|
|
311
405
|
...payload
|
|
312
406
|
}
|
|
313
407
|
});
|
|
314
|
-
}
|
|
408
|
+
});
|
|
315
409
|
self.closeStreamAction = async () => {
|
|
316
410
|
unwatch();
|
|
317
411
|
try {
|
|
@@ -322,7 +416,7 @@ var InngestRun = class extends Run {
|
|
|
322
416
|
};
|
|
323
417
|
const executionResultsPromise = self._start({
|
|
324
418
|
inputData,
|
|
325
|
-
|
|
419
|
+
requestContext,
|
|
326
420
|
// tracingContext, // We are not able to pass a reference to a span here, what to do?
|
|
327
421
|
initialState,
|
|
328
422
|
tracingOptions,
|
|
@@ -361,6 +455,75 @@ var InngestRun = class extends Run {
|
|
|
361
455
|
streamVNext(args = {}) {
|
|
362
456
|
return this.stream(args);
|
|
363
457
|
}
|
|
458
|
+
timeTravelStream({
|
|
459
|
+
inputData,
|
|
460
|
+
resumeData,
|
|
461
|
+
initialState,
|
|
462
|
+
step,
|
|
463
|
+
context,
|
|
464
|
+
nestedStepsContext,
|
|
465
|
+
requestContext,
|
|
466
|
+
tracingOptions,
|
|
467
|
+
outputOptions
|
|
468
|
+
}) {
|
|
469
|
+
this.closeStreamAction = async () => {
|
|
470
|
+
};
|
|
471
|
+
const self = this;
|
|
472
|
+
const stream = new ReadableStream({
|
|
473
|
+
async start(controller) {
|
|
474
|
+
const unwatch = self.watch(async ({ type, from = ChunkFrom.WORKFLOW, payload }) => {
|
|
475
|
+
controller.enqueue({
|
|
476
|
+
type,
|
|
477
|
+
runId: self.runId,
|
|
478
|
+
from,
|
|
479
|
+
payload: {
|
|
480
|
+
stepName: payload?.id,
|
|
481
|
+
...payload
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
self.closeStreamAction = async () => {
|
|
486
|
+
unwatch();
|
|
487
|
+
try {
|
|
488
|
+
await controller.close();
|
|
489
|
+
} catch (err) {
|
|
490
|
+
console.error("Error closing stream:", err);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
const executionResultsPromise = self._timeTravel({
|
|
494
|
+
inputData,
|
|
495
|
+
step,
|
|
496
|
+
context,
|
|
497
|
+
nestedStepsContext,
|
|
498
|
+
resumeData,
|
|
499
|
+
initialState,
|
|
500
|
+
requestContext,
|
|
501
|
+
tracingOptions,
|
|
502
|
+
outputOptions
|
|
503
|
+
});
|
|
504
|
+
self.executionResults = executionResultsPromise;
|
|
505
|
+
let executionResults;
|
|
506
|
+
try {
|
|
507
|
+
executionResults = await executionResultsPromise;
|
|
508
|
+
self.closeStreamAction?.().catch(() => {
|
|
509
|
+
});
|
|
510
|
+
if (self.streamOutput) {
|
|
511
|
+
self.streamOutput.updateResults(executionResults);
|
|
512
|
+
}
|
|
513
|
+
} catch (err) {
|
|
514
|
+
self.streamOutput?.rejectResults(err);
|
|
515
|
+
self.closeStreamAction?.().catch(() => {
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
this.streamOutput = new WorkflowRunOutput({
|
|
521
|
+
runId: this.runId,
|
|
522
|
+
workflowId: this.workflowId,
|
|
523
|
+
stream
|
|
524
|
+
});
|
|
525
|
+
return this.streamOutput;
|
|
526
|
+
}
|
|
364
527
|
};
|
|
365
528
|
var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
366
529
|
#mastra;
|
|
@@ -370,6 +533,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
370
533
|
constructor(params, inngest) {
|
|
371
534
|
const { concurrency, rateLimit, throttle, debounce, priority, ...workflowParams } = params;
|
|
372
535
|
super(workflowParams);
|
|
536
|
+
this.engineType = "inngest";
|
|
373
537
|
const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
|
|
374
538
|
([_, value]) => value !== void 0
|
|
375
539
|
);
|
|
@@ -377,13 +541,13 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
377
541
|
this.#mastra = params.mastra;
|
|
378
542
|
this.inngest = inngest;
|
|
379
543
|
}
|
|
380
|
-
async
|
|
544
|
+
async listWorkflowRuns(args) {
|
|
381
545
|
const storage = this.#mastra?.getStorage();
|
|
382
546
|
if (!storage) {
|
|
383
547
|
this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
|
|
384
548
|
return { runs: [], total: 0 };
|
|
385
549
|
}
|
|
386
|
-
return storage.
|
|
550
|
+
return storage.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
|
|
387
551
|
}
|
|
388
552
|
async getWorkflowRunById(runId) {
|
|
389
553
|
const storage = this.#mastra?.getStorage();
|
|
@@ -412,16 +576,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
412
576
|
}
|
|
413
577
|
}
|
|
414
578
|
}
|
|
415
|
-
|
|
416
|
-
* @deprecated Use createRunAsync() instead.
|
|
417
|
-
* @throws {Error} Always throws an error directing users to use createRunAsync()
|
|
418
|
-
*/
|
|
419
|
-
createRun(_options) {
|
|
420
|
-
throw new Error(
|
|
421
|
-
"createRun() has been deprecated. Please use createRunAsync() instead.\n\nMigration guide:\n Before: const run = workflow.createRun();\n After: const run = await workflow.createRunAsync();\n\nNote: createRunAsync() is an async method, so make sure your calling function is async."
|
|
422
|
-
);
|
|
423
|
-
}
|
|
424
|
-
async createRunAsync(options) {
|
|
579
|
+
async createRun(options) {
|
|
425
580
|
const runIdToUse = options?.runId || randomUUID();
|
|
426
581
|
const run = this.runs.get(runIdToUse) ?? new InngestRun(
|
|
427
582
|
{
|
|
@@ -434,7 +589,9 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
434
589
|
mastra: this.#mastra,
|
|
435
590
|
retryConfig: this.retryConfig,
|
|
436
591
|
cleanup: () => this.runs.delete(runIdToUse),
|
|
437
|
-
workflowSteps: this.steps
|
|
592
|
+
workflowSteps: this.steps,
|
|
593
|
+
workflowEngineType: this.engineType,
|
|
594
|
+
validateInputs: this.options.validateInputs
|
|
438
595
|
},
|
|
439
596
|
this.inngest
|
|
440
597
|
);
|
|
@@ -455,13 +612,13 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
455
612
|
value: {},
|
|
456
613
|
context: {},
|
|
457
614
|
activePaths: [],
|
|
615
|
+
activeStepsPath: {},
|
|
458
616
|
waitingPaths: {},
|
|
459
617
|
serializedStepGraph: this.serializedStepGraph,
|
|
460
618
|
suspendedPaths: {},
|
|
461
619
|
resumeLabels: {},
|
|
462
620
|
result: void 0,
|
|
463
621
|
error: void 0,
|
|
464
|
-
// @ts-ignore
|
|
465
622
|
timestamp: Date.now()
|
|
466
623
|
}
|
|
467
624
|
});
|
|
@@ -475,15 +632,14 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
475
632
|
this.function = this.inngest.createFunction(
|
|
476
633
|
{
|
|
477
634
|
id: `workflow.${this.id}`,
|
|
478
|
-
|
|
479
|
-
retries: this.retryConfig?.attempts ?? 0,
|
|
635
|
+
retries: Math.min(this.retryConfig?.attempts ?? 0, 20),
|
|
480
636
|
cancelOn: [{ event: `cancel.workflow.${this.id}` }],
|
|
481
637
|
// Spread flow control configuration
|
|
482
638
|
...this.flowControlConfig
|
|
483
639
|
},
|
|
484
640
|
{ event: `workflow.${this.id}` },
|
|
485
641
|
async ({ event, step, attempt, publish }) => {
|
|
486
|
-
let { inputData, initialState, runId, resourceId, resume, outputOptions, format } = event.data;
|
|
642
|
+
let { inputData, initialState, runId, resourceId, resume, outputOptions, format, timeTravel } = event.data;
|
|
487
643
|
if (!runId) {
|
|
488
644
|
runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
|
|
489
645
|
return randomUUID();
|
|
@@ -522,16 +678,17 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
522
678
|
initialState,
|
|
523
679
|
emitter,
|
|
524
680
|
retryConfig: this.retryConfig,
|
|
525
|
-
|
|
681
|
+
requestContext: new RequestContext(),
|
|
526
682
|
// TODO
|
|
527
683
|
resume,
|
|
684
|
+
timeTravel,
|
|
528
685
|
format,
|
|
529
686
|
abortController: new AbortController(),
|
|
530
|
-
// currentSpan: undefined, // TODO: Pass actual parent
|
|
687
|
+
// currentSpan: undefined, // TODO: Pass actual parent Span from workflow execution context
|
|
531
688
|
outputOptions,
|
|
532
689
|
writableStream: new WritableStream({
|
|
533
690
|
write(chunk) {
|
|
534
|
-
void emitter.emit("watch
|
|
691
|
+
void emitter.emit("watch", chunk).catch(() => {
|
|
535
692
|
});
|
|
536
693
|
}
|
|
537
694
|
})
|
|
@@ -577,13 +734,11 @@ function createStep(params, agentOptions) {
|
|
|
577
734
|
return {
|
|
578
735
|
id: params.name,
|
|
579
736
|
description: params.getDescription(),
|
|
580
|
-
// @ts-ignore
|
|
581
737
|
inputSchema: z.object({
|
|
582
738
|
prompt: z.string()
|
|
583
739
|
// resourceId: z.string().optional(),
|
|
584
740
|
// threadId: z.string().optional(),
|
|
585
741
|
}),
|
|
586
|
-
// @ts-ignore
|
|
587
742
|
outputSchema: z.object({
|
|
588
743
|
text: z.string()
|
|
589
744
|
}),
|
|
@@ -591,7 +746,7 @@ function createStep(params, agentOptions) {
|
|
|
591
746
|
inputData,
|
|
592
747
|
[EMITTER_SYMBOL]: emitter,
|
|
593
748
|
[STREAM_FORMAT_SYMBOL]: streamFormat,
|
|
594
|
-
|
|
749
|
+
requestContext,
|
|
595
750
|
tracingContext,
|
|
596
751
|
abortSignal,
|
|
597
752
|
abort,
|
|
@@ -612,7 +767,7 @@ function createStep(params, agentOptions) {
|
|
|
612
767
|
...agentOptions ?? {},
|
|
613
768
|
// resourceId: inputData.resourceId,
|
|
614
769
|
// threadId: inputData.threadId,
|
|
615
|
-
|
|
770
|
+
requestContext,
|
|
616
771
|
tracingContext,
|
|
617
772
|
onFinish: (result) => {
|
|
618
773
|
streamPromise.resolve(result.text);
|
|
@@ -624,7 +779,7 @@ function createStep(params, agentOptions) {
|
|
|
624
779
|
} else {
|
|
625
780
|
const modelOutput = await params.stream(inputData.prompt, {
|
|
626
781
|
...agentOptions ?? {},
|
|
627
|
-
|
|
782
|
+
requestContext,
|
|
628
783
|
tracingContext,
|
|
629
784
|
onFinish: (result) => {
|
|
630
785
|
streamPromise.resolve(result.text);
|
|
@@ -635,20 +790,20 @@ function createStep(params, agentOptions) {
|
|
|
635
790
|
stream = modelOutput.fullStream;
|
|
636
791
|
}
|
|
637
792
|
if (streamFormat === "legacy") {
|
|
638
|
-
await emitter.emit("watch
|
|
793
|
+
await emitter.emit("watch", {
|
|
639
794
|
type: "tool-call-streaming-start",
|
|
640
795
|
...toolData ?? {}
|
|
641
796
|
});
|
|
642
797
|
for await (const chunk of stream) {
|
|
643
798
|
if (chunk.type === "text-delta") {
|
|
644
|
-
await emitter.emit("watch
|
|
799
|
+
await emitter.emit("watch", {
|
|
645
800
|
type: "tool-call-delta",
|
|
646
801
|
...toolData ?? {},
|
|
647
802
|
argsTextDelta: chunk.textDelta
|
|
648
803
|
});
|
|
649
804
|
}
|
|
650
805
|
}
|
|
651
|
-
await emitter.emit("watch
|
|
806
|
+
await emitter.emit("watch", {
|
|
652
807
|
type: "tool-call-streaming-finish",
|
|
653
808
|
...toolData ?? {}
|
|
654
809
|
});
|
|
@@ -673,20 +828,36 @@ function createStep(params, agentOptions) {
|
|
|
673
828
|
}
|
|
674
829
|
return {
|
|
675
830
|
// TODO: tool probably should have strong id type
|
|
676
|
-
// @ts-ignore
|
|
677
831
|
id: params.id,
|
|
678
832
|
description: params.description,
|
|
679
833
|
inputSchema: params.inputSchema,
|
|
680
834
|
outputSchema: params.outputSchema,
|
|
681
|
-
execute: async ({
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
835
|
+
execute: async ({
|
|
836
|
+
inputData,
|
|
837
|
+
mastra,
|
|
838
|
+
requestContext,
|
|
839
|
+
tracingContext,
|
|
840
|
+
suspend,
|
|
841
|
+
resumeData,
|
|
842
|
+
runId,
|
|
843
|
+
workflowId,
|
|
844
|
+
state,
|
|
845
|
+
setState
|
|
846
|
+
}) => {
|
|
847
|
+
const toolContext = {
|
|
848
|
+
mastra,
|
|
849
|
+
requestContext,
|
|
686
850
|
tracingContext,
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
851
|
+
resumeData,
|
|
852
|
+
workflow: {
|
|
853
|
+
runId,
|
|
854
|
+
suspend,
|
|
855
|
+
workflowId,
|
|
856
|
+
state,
|
|
857
|
+
setState
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
return params.execute(inputData, toolContext);
|
|
690
861
|
},
|
|
691
862
|
component: "TOOL"
|
|
692
863
|
};
|
|
@@ -720,6 +891,8 @@ function init(inngest) {
|
|
|
720
891
|
suspendSchema: step.suspendSchema,
|
|
721
892
|
stateSchema: step.stateSchema,
|
|
722
893
|
execute: step.execute,
|
|
894
|
+
retries: step.retries,
|
|
895
|
+
scorers: step.scorers,
|
|
723
896
|
component: step.component
|
|
724
897
|
};
|
|
725
898
|
},
|
|
@@ -751,45 +924,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
751
924
|
steps: stepResults
|
|
752
925
|
};
|
|
753
926
|
if (lastOutput.status === "success") {
|
|
754
|
-
await emitter.emit("watch", {
|
|
755
|
-
type: "watch",
|
|
756
|
-
payload: {
|
|
757
|
-
workflowState: {
|
|
758
|
-
status: lastOutput.status,
|
|
759
|
-
steps: stepResults,
|
|
760
|
-
result: lastOutput.output
|
|
761
|
-
}
|
|
762
|
-
},
|
|
763
|
-
eventTimestamp: Date.now()
|
|
764
|
-
});
|
|
765
927
|
base.result = lastOutput.output;
|
|
766
928
|
} else if (lastOutput.status === "failed") {
|
|
767
929
|
base.error = error instanceof Error ? error?.stack ?? error.message : lastOutput?.error instanceof Error ? lastOutput.error.message : lastOutput.error ?? error ?? "Unknown error";
|
|
768
|
-
await emitter.emit("watch", {
|
|
769
|
-
type: "watch",
|
|
770
|
-
payload: {
|
|
771
|
-
workflowState: {
|
|
772
|
-
status: lastOutput.status,
|
|
773
|
-
steps: stepResults,
|
|
774
|
-
result: null,
|
|
775
|
-
error: base.error
|
|
776
|
-
}
|
|
777
|
-
},
|
|
778
|
-
eventTimestamp: Date.now()
|
|
779
|
-
});
|
|
780
930
|
} else if (lastOutput.status === "suspended") {
|
|
781
|
-
await emitter.emit("watch", {
|
|
782
|
-
type: "watch",
|
|
783
|
-
payload: {
|
|
784
|
-
workflowState: {
|
|
785
|
-
status: lastOutput.status,
|
|
786
|
-
steps: stepResults,
|
|
787
|
-
result: null,
|
|
788
|
-
error: null
|
|
789
|
-
}
|
|
790
|
-
},
|
|
791
|
-
eventTimestamp: Date.now()
|
|
792
|
-
});
|
|
793
931
|
const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
|
|
794
932
|
if (stepResult?.status === "suspended") {
|
|
795
933
|
const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
|
|
@@ -812,14 +950,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
812
950
|
stepResults,
|
|
813
951
|
emitter,
|
|
814
952
|
abortController,
|
|
815
|
-
|
|
953
|
+
requestContext,
|
|
816
954
|
executionContext,
|
|
817
955
|
writableStream,
|
|
818
956
|
tracingContext
|
|
819
957
|
}) {
|
|
820
958
|
let { duration, fn } = entry;
|
|
821
959
|
const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
822
|
-
type:
|
|
960
|
+
type: SpanType.WORKFLOW_SLEEP,
|
|
823
961
|
name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
|
|
824
962
|
attributes: {
|
|
825
963
|
durationMs: duration,
|
|
@@ -836,13 +974,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
836
974
|
runId,
|
|
837
975
|
workflowId,
|
|
838
976
|
mastra: this.mastra,
|
|
839
|
-
|
|
977
|
+
requestContext,
|
|
840
978
|
inputData: prevOutput,
|
|
841
979
|
state: executionContext.state,
|
|
842
980
|
setState: (state) => {
|
|
843
981
|
executionContext.state = state;
|
|
844
982
|
},
|
|
845
|
-
runCount: -1,
|
|
846
983
|
retryCount: -1,
|
|
847
984
|
tracingContext: {
|
|
848
985
|
currentSpan: sleepSpan
|
|
@@ -901,14 +1038,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
901
1038
|
stepResults,
|
|
902
1039
|
emitter,
|
|
903
1040
|
abortController,
|
|
904
|
-
|
|
1041
|
+
requestContext,
|
|
905
1042
|
executionContext,
|
|
906
1043
|
writableStream,
|
|
907
1044
|
tracingContext
|
|
908
1045
|
}) {
|
|
909
1046
|
let { date, fn } = entry;
|
|
910
1047
|
const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
911
|
-
type:
|
|
1048
|
+
type: SpanType.WORKFLOW_SLEEP,
|
|
912
1049
|
name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
|
|
913
1050
|
attributes: {
|
|
914
1051
|
untilDate: date,
|
|
@@ -926,13 +1063,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
926
1063
|
runId,
|
|
927
1064
|
workflowId,
|
|
928
1065
|
mastra: this.mastra,
|
|
929
|
-
|
|
1066
|
+
requestContext,
|
|
930
1067
|
inputData: prevOutput,
|
|
931
1068
|
state: executionContext.state,
|
|
932
1069
|
setState: (state) => {
|
|
933
1070
|
executionContext.state = state;
|
|
934
1071
|
},
|
|
935
|
-
runCount: -1,
|
|
936
1072
|
retryCount: -1,
|
|
937
1073
|
tracingContext: {
|
|
938
1074
|
currentSpan: sleepUntilSpan
|
|
@@ -991,32 +1127,23 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
991
1127
|
throw e;
|
|
992
1128
|
}
|
|
993
1129
|
}
|
|
994
|
-
async executeWaitForEvent({ event, timeout }) {
|
|
995
|
-
const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
|
|
996
|
-
event: `user-event-${event}`,
|
|
997
|
-
timeout: timeout ?? 5e3
|
|
998
|
-
});
|
|
999
|
-
if (eventData === null) {
|
|
1000
|
-
throw "Timeout waiting for event";
|
|
1001
|
-
}
|
|
1002
|
-
return eventData?.data;
|
|
1003
|
-
}
|
|
1004
1130
|
async executeStep({
|
|
1005
1131
|
step,
|
|
1006
1132
|
stepResults,
|
|
1007
1133
|
executionContext,
|
|
1008
1134
|
resume,
|
|
1135
|
+
timeTravel,
|
|
1009
1136
|
prevOutput,
|
|
1010
1137
|
emitter,
|
|
1011
1138
|
abortController,
|
|
1012
|
-
|
|
1139
|
+
requestContext,
|
|
1013
1140
|
tracingContext,
|
|
1014
1141
|
writableStream,
|
|
1015
1142
|
disableScorers
|
|
1016
1143
|
}) {
|
|
1017
|
-
const
|
|
1144
|
+
const stepSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
1018
1145
|
name: `workflow step: '${step.id}'`,
|
|
1019
|
-
type:
|
|
1146
|
+
type: SpanType.WORKFLOW_STEP,
|
|
1020
1147
|
input: prevOutput,
|
|
1021
1148
|
attributes: {
|
|
1022
1149
|
stepId: step.id
|
|
@@ -1026,34 +1153,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1026
1153
|
const { inputData, validationError } = await validateStepInput({
|
|
1027
1154
|
prevOutput,
|
|
1028
1155
|
step,
|
|
1029
|
-
validateInputs: this.options?.validateInputs ??
|
|
1156
|
+
validateInputs: this.options?.validateInputs ?? true
|
|
1030
1157
|
});
|
|
1031
1158
|
const startedAt = await this.inngestStep.run(
|
|
1032
1159
|
`workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
|
|
1033
1160
|
async () => {
|
|
1034
1161
|
const startedAt2 = Date.now();
|
|
1035
1162
|
await emitter.emit("watch", {
|
|
1036
|
-
type: "watch",
|
|
1037
|
-
payload: {
|
|
1038
|
-
currentStep: {
|
|
1039
|
-
id: step.id,
|
|
1040
|
-
status: "running"
|
|
1041
|
-
},
|
|
1042
|
-
workflowState: {
|
|
1043
|
-
status: "running",
|
|
1044
|
-
steps: {
|
|
1045
|
-
...stepResults,
|
|
1046
|
-
[step.id]: {
|
|
1047
|
-
status: "running"
|
|
1048
|
-
}
|
|
1049
|
-
},
|
|
1050
|
-
result: null,
|
|
1051
|
-
error: null
|
|
1052
|
-
}
|
|
1053
|
-
},
|
|
1054
|
-
eventTimestamp: Date.now()
|
|
1055
|
-
});
|
|
1056
|
-
await emitter.emit("watch-v2", {
|
|
1057
1163
|
type: "workflow-step-start",
|
|
1058
1164
|
payload: {
|
|
1059
1165
|
id: step.id,
|
|
@@ -1069,9 +1175,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1069
1175
|
const isResume = !!resume?.steps?.length;
|
|
1070
1176
|
let result;
|
|
1071
1177
|
let runId;
|
|
1178
|
+
const isTimeTravel = !!(timeTravel && timeTravel.steps?.length > 1 && timeTravel.steps[0] === step.id);
|
|
1072
1179
|
try {
|
|
1073
1180
|
if (isResume) {
|
|
1074
|
-
runId = stepResults[resume?.steps?.[0]]?.suspendPayload?.__workflow_meta?.runId ?? randomUUID();
|
|
1181
|
+
runId = stepResults[resume?.steps?.[0] ?? ""]?.suspendPayload?.__workflow_meta?.runId ?? randomUUID();
|
|
1075
1182
|
const snapshot = await this.mastra?.getStorage()?.loadWorkflowSnapshot({
|
|
1076
1183
|
workflowName: step.id,
|
|
1077
1184
|
runId
|
|
@@ -1087,8 +1194,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1087
1194
|
steps: resume.steps.slice(1),
|
|
1088
1195
|
stepResults: snapshot?.context,
|
|
1089
1196
|
resumePayload: resume.resumePayload,
|
|
1090
|
-
|
|
1091
|
-
resumePath: snapshot?.suspendedPaths?.[resume.steps?.[1]]
|
|
1197
|
+
resumePath: resume.steps?.[1] ? snapshot?.suspendedPaths?.[resume.steps?.[1]] : void 0
|
|
1092
1198
|
},
|
|
1093
1199
|
outputOptions: { includeState: true }
|
|
1094
1200
|
}
|
|
@@ -1096,6 +1202,32 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1096
1202
|
result = invokeResp.result;
|
|
1097
1203
|
runId = invokeResp.runId;
|
|
1098
1204
|
executionContext.state = invokeResp.result.state;
|
|
1205
|
+
} else if (isTimeTravel) {
|
|
1206
|
+
const snapshot = await this.mastra?.getStorage()?.loadWorkflowSnapshot({
|
|
1207
|
+
workflowName: step.id,
|
|
1208
|
+
runId: executionContext.runId
|
|
1209
|
+
}) ?? { context: {} };
|
|
1210
|
+
const timeTravelParams = createTimeTravelExecutionParams({
|
|
1211
|
+
steps: timeTravel.steps.slice(1),
|
|
1212
|
+
inputData: timeTravel.inputData,
|
|
1213
|
+
resumeData: timeTravel.resumeData,
|
|
1214
|
+
context: timeTravel.nestedStepResults?.[step.id] ?? {},
|
|
1215
|
+
nestedStepsContext: timeTravel.nestedStepResults ?? {},
|
|
1216
|
+
snapshot,
|
|
1217
|
+
graph: step.buildExecutionGraph()
|
|
1218
|
+
});
|
|
1219
|
+
const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
|
|
1220
|
+
function: step.getFunction(),
|
|
1221
|
+
data: {
|
|
1222
|
+
timeTravel: timeTravelParams,
|
|
1223
|
+
initialState: executionContext.state ?? {},
|
|
1224
|
+
runId: executionContext.runId,
|
|
1225
|
+
outputOptions: { includeState: true }
|
|
1226
|
+
}
|
|
1227
|
+
});
|
|
1228
|
+
result = invokeResp.result;
|
|
1229
|
+
runId = invokeResp.runId;
|
|
1230
|
+
executionContext.state = invokeResp.result.state;
|
|
1099
1231
|
} else {
|
|
1100
1232
|
const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
|
|
1101
1233
|
function: step.getFunction(),
|
|
@@ -1129,23 +1261,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1129
1261
|
async () => {
|
|
1130
1262
|
if (result.status === "failed") {
|
|
1131
1263
|
await emitter.emit("watch", {
|
|
1132
|
-
type: "watch",
|
|
1133
|
-
payload: {
|
|
1134
|
-
currentStep: {
|
|
1135
|
-
id: step.id,
|
|
1136
|
-
status: "failed",
|
|
1137
|
-
error: result?.error
|
|
1138
|
-
},
|
|
1139
|
-
workflowState: {
|
|
1140
|
-
status: "running",
|
|
1141
|
-
steps: stepResults,
|
|
1142
|
-
result: null,
|
|
1143
|
-
error: null
|
|
1144
|
-
}
|
|
1145
|
-
},
|
|
1146
|
-
eventTimestamp: Date.now()
|
|
1147
|
-
});
|
|
1148
|
-
await emitter.emit("watch-v2", {
|
|
1149
1264
|
type: "workflow-step-result",
|
|
1150
1265
|
payload: {
|
|
1151
1266
|
id: step.id,
|
|
@@ -1164,27 +1279,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1164
1279
|
const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
|
|
1165
1280
|
executionContext.suspendedPaths[step.id] = executionContext.executionPath;
|
|
1166
1281
|
await emitter.emit("watch", {
|
|
1167
|
-
type: "watch",
|
|
1168
|
-
payload: {
|
|
1169
|
-
currentStep: {
|
|
1170
|
-
id: step.id,
|
|
1171
|
-
status: "suspended",
|
|
1172
|
-
payload: stepResult.payload,
|
|
1173
|
-
suspendPayload: {
|
|
1174
|
-
...stepResult?.suspendPayload,
|
|
1175
|
-
__workflow_meta: { runId, path: suspendPath }
|
|
1176
|
-
}
|
|
1177
|
-
},
|
|
1178
|
-
workflowState: {
|
|
1179
|
-
status: "running",
|
|
1180
|
-
steps: stepResults,
|
|
1181
|
-
result: null,
|
|
1182
|
-
error: null
|
|
1183
|
-
}
|
|
1184
|
-
},
|
|
1185
|
-
eventTimestamp: Date.now()
|
|
1186
|
-
});
|
|
1187
|
-
await emitter.emit("watch-v2", {
|
|
1188
1282
|
type: "workflow-step-suspended",
|
|
1189
1283
|
payload: {
|
|
1190
1284
|
id: step.id,
|
|
@@ -1203,23 +1297,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1203
1297
|
}
|
|
1204
1298
|
};
|
|
1205
1299
|
}
|
|
1206
|
-
await emitter.emit("watch", {
|
|
1207
|
-
type: "watch",
|
|
1208
|
-
payload: {
|
|
1209
|
-
currentStep: {
|
|
1210
|
-
id: step.id,
|
|
1211
|
-
status: "suspended",
|
|
1212
|
-
payload: {}
|
|
1213
|
-
},
|
|
1214
|
-
workflowState: {
|
|
1215
|
-
status: "running",
|
|
1216
|
-
steps: stepResults,
|
|
1217
|
-
result: null,
|
|
1218
|
-
error: null
|
|
1219
|
-
}
|
|
1220
|
-
},
|
|
1221
|
-
eventTimestamp: Date.now()
|
|
1222
|
-
});
|
|
1223
1300
|
return {
|
|
1224
1301
|
executionContext,
|
|
1225
1302
|
result: {
|
|
@@ -1229,23 +1306,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1229
1306
|
};
|
|
1230
1307
|
}
|
|
1231
1308
|
await emitter.emit("watch", {
|
|
1232
|
-
type: "watch",
|
|
1233
|
-
payload: {
|
|
1234
|
-
currentStep: {
|
|
1235
|
-
id: step.id,
|
|
1236
|
-
status: "success",
|
|
1237
|
-
output: result?.result
|
|
1238
|
-
},
|
|
1239
|
-
workflowState: {
|
|
1240
|
-
status: "running",
|
|
1241
|
-
steps: stepResults,
|
|
1242
|
-
result: null,
|
|
1243
|
-
error: null
|
|
1244
|
-
}
|
|
1245
|
-
},
|
|
1246
|
-
eventTimestamp: Date.now()
|
|
1247
|
-
});
|
|
1248
|
-
await emitter.emit("watch-v2", {
|
|
1249
1309
|
type: "workflow-step-result",
|
|
1250
1310
|
payload: {
|
|
1251
1311
|
id: step.id,
|
|
@@ -1253,7 +1313,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1253
1313
|
output: result?.result
|
|
1254
1314
|
}
|
|
1255
1315
|
});
|
|
1256
|
-
await emitter.emit("watch
|
|
1316
|
+
await emitter.emit("watch", {
|
|
1257
1317
|
type: "workflow-step-finish",
|
|
1258
1318
|
payload: {
|
|
1259
1319
|
id: step.id,
|
|
@@ -1280,14 +1340,32 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1280
1340
|
let execResults;
|
|
1281
1341
|
let suspended;
|
|
1282
1342
|
let bailed;
|
|
1343
|
+
const { resumeData: timeTravelResumeData, validationError: timeTravelResumeValidationError } = await validateStepResumeData({
|
|
1344
|
+
resumeData: timeTravel?.stepResults[step.id]?.status === "suspended" ? timeTravel?.resumeData : void 0,
|
|
1345
|
+
step
|
|
1346
|
+
});
|
|
1347
|
+
let resumeDataToUse;
|
|
1348
|
+
if (timeTravelResumeData && !timeTravelResumeValidationError) {
|
|
1349
|
+
resumeDataToUse = timeTravelResumeData;
|
|
1350
|
+
} else if (timeTravelResumeData && timeTravelResumeValidationError) {
|
|
1351
|
+
this.logger.warn("Time travel resume data validation failed", {
|
|
1352
|
+
stepId: step.id,
|
|
1353
|
+
error: timeTravelResumeValidationError.message
|
|
1354
|
+
});
|
|
1355
|
+
} else if (resume?.steps[0] === step.id) {
|
|
1356
|
+
resumeDataToUse = resume?.resumePayload;
|
|
1357
|
+
}
|
|
1283
1358
|
try {
|
|
1284
1359
|
if (validationError) {
|
|
1285
1360
|
throw validationError;
|
|
1286
1361
|
}
|
|
1362
|
+
const retryCount = this.getOrGenerateRetryCount(step.id);
|
|
1287
1363
|
const result = await step.execute({
|
|
1288
1364
|
runId: executionContext.runId,
|
|
1365
|
+
workflowId: executionContext.workflowId,
|
|
1289
1366
|
mastra: this.mastra,
|
|
1290
|
-
|
|
1367
|
+
requestContext,
|
|
1368
|
+
retryCount,
|
|
1291
1369
|
writer: new ToolStream(
|
|
1292
1370
|
{
|
|
1293
1371
|
prefix: "workflow-step",
|
|
@@ -1302,9 +1380,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1302
1380
|
executionContext.state = state;
|
|
1303
1381
|
},
|
|
1304
1382
|
inputData,
|
|
1305
|
-
resumeData:
|
|
1383
|
+
resumeData: resumeDataToUse,
|
|
1306
1384
|
tracingContext: {
|
|
1307
|
-
currentSpan:
|
|
1385
|
+
currentSpan: stepSpan
|
|
1308
1386
|
},
|
|
1309
1387
|
getInitData: () => stepResults?.input,
|
|
1310
1388
|
getStepResult: getStepResult.bind(this, stepResults),
|
|
@@ -1324,11 +1402,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1324
1402
|
bail: (result2) => {
|
|
1325
1403
|
bailed = { payload: result2 };
|
|
1326
1404
|
},
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
resumePayload: resume?.resumePayload,
|
|
1330
|
-
// @ts-ignore
|
|
1331
|
-
runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
|
|
1405
|
+
abort: () => {
|
|
1406
|
+
abortController?.abort();
|
|
1332
1407
|
},
|
|
1333
1408
|
[EMITTER_SYMBOL]: emitter,
|
|
1334
1409
|
[STREAM_FORMAT_SYMBOL]: executionContext.format,
|
|
@@ -1344,8 +1419,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1344
1419
|
startedAt,
|
|
1345
1420
|
endedAt,
|
|
1346
1421
|
payload: inputData,
|
|
1347
|
-
resumedAt:
|
|
1348
|
-
resumePayload:
|
|
1422
|
+
resumedAt: resumeDataToUse ? startedAt : void 0,
|
|
1423
|
+
resumePayload: resumeDataToUse
|
|
1349
1424
|
};
|
|
1350
1425
|
} catch (e) {
|
|
1351
1426
|
const stepFailure = {
|
|
@@ -1354,12 +1429,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1354
1429
|
error: e instanceof Error ? e.message : String(e),
|
|
1355
1430
|
endedAt: Date.now(),
|
|
1356
1431
|
startedAt,
|
|
1357
|
-
resumedAt:
|
|
1358
|
-
resumePayload:
|
|
1432
|
+
resumedAt: resumeDataToUse ? startedAt : void 0,
|
|
1433
|
+
resumePayload: resumeDataToUse
|
|
1359
1434
|
};
|
|
1360
1435
|
execResults = stepFailure;
|
|
1361
1436
|
const fallbackErrorMessage = `Step ${step.id} failed`;
|
|
1362
|
-
|
|
1437
|
+
stepSpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
|
|
1363
1438
|
throw new RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
|
|
1364
1439
|
cause: execResults
|
|
1365
1440
|
});
|
|
@@ -1368,11 +1443,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1368
1443
|
execResults = {
|
|
1369
1444
|
status: "suspended",
|
|
1370
1445
|
suspendPayload: suspended.payload,
|
|
1446
|
+
...execResults.output ? { suspendOutput: execResults.output } : {},
|
|
1371
1447
|
payload: inputData,
|
|
1372
1448
|
suspendedAt: Date.now(),
|
|
1373
1449
|
startedAt,
|
|
1374
|
-
resumedAt:
|
|
1375
|
-
resumePayload:
|
|
1450
|
+
resumedAt: resumeDataToUse ? startedAt : void 0,
|
|
1451
|
+
resumePayload: resumeDataToUse
|
|
1376
1452
|
};
|
|
1377
1453
|
} else if (bailed) {
|
|
1378
1454
|
execResults = {
|
|
@@ -1383,24 +1459,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1383
1459
|
startedAt
|
|
1384
1460
|
};
|
|
1385
1461
|
}
|
|
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
|
-
}
|
|
1399
|
-
},
|
|
1400
|
-
eventTimestamp: Date.now()
|
|
1401
|
-
});
|
|
1402
1462
|
if (execResults.status === "suspended") {
|
|
1403
|
-
await emitter.emit("watch
|
|
1463
|
+
await emitter.emit("watch", {
|
|
1404
1464
|
type: "workflow-step-suspended",
|
|
1405
1465
|
payload: {
|
|
1406
1466
|
id: step.id,
|
|
@@ -1408,14 +1468,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1408
1468
|
}
|
|
1409
1469
|
});
|
|
1410
1470
|
} else {
|
|
1411
|
-
await emitter.emit("watch
|
|
1471
|
+
await emitter.emit("watch", {
|
|
1412
1472
|
type: "workflow-step-result",
|
|
1413
1473
|
payload: {
|
|
1414
1474
|
id: step.id,
|
|
1415
1475
|
...execResults
|
|
1416
1476
|
}
|
|
1417
1477
|
});
|
|
1418
|
-
await emitter.emit("watch
|
|
1478
|
+
await emitter.emit("watch", {
|
|
1419
1479
|
type: "workflow-step-finish",
|
|
1420
1480
|
payload: {
|
|
1421
1481
|
id: step.id,
|
|
@@ -1423,7 +1483,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1423
1483
|
}
|
|
1424
1484
|
});
|
|
1425
1485
|
}
|
|
1426
|
-
|
|
1486
|
+
stepSpan?.end({ output: execResults });
|
|
1427
1487
|
return { result: execResults, executionContext, stepResults };
|
|
1428
1488
|
});
|
|
1429
1489
|
} catch (e) {
|
|
@@ -1453,15 +1513,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1453
1513
|
output: stepRes.result,
|
|
1454
1514
|
workflowId: executionContext.workflowId,
|
|
1455
1515
|
stepId: step.id,
|
|
1456
|
-
|
|
1516
|
+
requestContext,
|
|
1457
1517
|
disableScorers,
|
|
1458
|
-
tracingContext: { currentSpan:
|
|
1518
|
+
tracingContext: { currentSpan: stepSpan }
|
|
1459
1519
|
});
|
|
1460
1520
|
}
|
|
1461
1521
|
});
|
|
1462
1522
|
}
|
|
1463
1523
|
Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
|
|
1464
|
-
Object.assign(stepResults, stepRes.stepResults);
|
|
1465
1524
|
executionContext.state = stepRes.executionContext.state;
|
|
1466
1525
|
return stepRes.result;
|
|
1467
1526
|
}
|
|
@@ -1489,17 +1548,17 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1489
1548
|
resourceId,
|
|
1490
1549
|
snapshot: {
|
|
1491
1550
|
runId,
|
|
1551
|
+
status: workflowStatus,
|
|
1492
1552
|
value: executionContext.state,
|
|
1493
1553
|
context: stepResults,
|
|
1494
|
-
activePaths:
|
|
1554
|
+
activePaths: executionContext.executionPath,
|
|
1555
|
+
activeStepsPath: executionContext.activeStepsPath,
|
|
1495
1556
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1496
1557
|
resumeLabels: executionContext.resumeLabels,
|
|
1497
1558
|
waitingPaths: {},
|
|
1498
1559
|
serializedStepGraph,
|
|
1499
|
-
status: workflowStatus,
|
|
1500
1560
|
result,
|
|
1501
1561
|
error,
|
|
1502
|
-
// @ts-ignore
|
|
1503
1562
|
timestamp: Date.now()
|
|
1504
1563
|
}
|
|
1505
1564
|
});
|
|
@@ -1512,17 +1571,18 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1512
1571
|
entry,
|
|
1513
1572
|
prevOutput,
|
|
1514
1573
|
stepResults,
|
|
1574
|
+
timeTravel,
|
|
1515
1575
|
resume,
|
|
1516
1576
|
executionContext,
|
|
1517
1577
|
emitter,
|
|
1518
1578
|
abortController,
|
|
1519
|
-
|
|
1579
|
+
requestContext,
|
|
1520
1580
|
writableStream,
|
|
1521
1581
|
disableScorers,
|
|
1522
1582
|
tracingContext
|
|
1523
1583
|
}) {
|
|
1524
1584
|
const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
|
|
1525
|
-
type:
|
|
1585
|
+
type: SpanType.WORKFLOW_CONDITIONAL,
|
|
1526
1586
|
name: `conditional: '${entry.conditions.length} conditions'`,
|
|
1527
1587
|
input: prevOutput,
|
|
1528
1588
|
attributes: {
|
|
@@ -1535,7 +1595,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1535
1595
|
entry.conditions.map(
|
|
1536
1596
|
(cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
|
|
1537
1597
|
const evalSpan = conditionalSpan?.createChildSpan({
|
|
1538
|
-
type:
|
|
1598
|
+
type: SpanType.WORKFLOW_CONDITIONAL_EVAL,
|
|
1539
1599
|
name: `condition: '${index}'`,
|
|
1540
1600
|
input: prevOutput,
|
|
1541
1601
|
attributes: {
|
|
@@ -1550,8 +1610,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1550
1610
|
runId,
|
|
1551
1611
|
workflowId,
|
|
1552
1612
|
mastra: this.mastra,
|
|
1553
|
-
|
|
1554
|
-
runCount: -1,
|
|
1613
|
+
requestContext,
|
|
1555
1614
|
retryCount: -1,
|
|
1556
1615
|
inputData: prevOutput,
|
|
1557
1616
|
state: executionContext.state,
|
|
@@ -1631,10 +1690,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1631
1690
|
prevOutput,
|
|
1632
1691
|
stepResults,
|
|
1633
1692
|
resume,
|
|
1693
|
+
timeTravel,
|
|
1634
1694
|
executionContext: {
|
|
1635
1695
|
workflowId,
|
|
1636
1696
|
runId,
|
|
1637
1697
|
executionPath: [...executionContext.executionPath, index],
|
|
1698
|
+
activeStepsPath: executionContext.activeStepsPath,
|
|
1638
1699
|
suspendedPaths: executionContext.suspendedPaths,
|
|
1639
1700
|
resumeLabels: executionContext.resumeLabels,
|
|
1640
1701
|
retryConfig: executionContext.retryConfig,
|
|
@@ -1642,7 +1703,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1642
1703
|
},
|
|
1643
1704
|
emitter,
|
|
1644
1705
|
abortController,
|
|
1645
|
-
|
|
1706
|
+
requestContext,
|
|
1646
1707
|
writableStream,
|
|
1647
1708
|
disableScorers,
|
|
1648
1709
|
tracingContext: {
|
|
@@ -1658,13 +1719,19 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
1658
1719
|
if (hasFailed) {
|
|
1659
1720
|
execResults = { status: "failed", error: hasFailed.error };
|
|
1660
1721
|
} else if (hasSuspended) {
|
|
1661
|
-
execResults = {
|
|
1722
|
+
execResults = {
|
|
1723
|
+
status: "suspended",
|
|
1724
|
+
suspendPayload: hasSuspended.suspendPayload,
|
|
1725
|
+
...hasSuspended.suspendOutput ? { suspendOutput: hasSuspended.suspendOutput } : {}
|
|
1726
|
+
};
|
|
1662
1727
|
} else {
|
|
1663
1728
|
execResults = {
|
|
1664
1729
|
status: "success",
|
|
1665
1730
|
output: results.reduce((acc, result, index) => {
|
|
1666
1731
|
if (result.status === "success") {
|
|
1667
|
-
|
|
1732
|
+
if ("step" in stepsToRun[index]) {
|
|
1733
|
+
acc[stepsToRun[index].step.id] = result.output;
|
|
1734
|
+
}
|
|
1668
1735
|
}
|
|
1669
1736
|
return acc;
|
|
1670
1737
|
}, {})
|