@mastra/inngest 0.0.0-sidebar-window-undefined-fix-20251029233656 → 0.0.0-span-scorring-test-20251124132129

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/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 { wrapMastra, AISpanType } from '@mastra/core/ai-tracing';
5
- import { RuntimeContext } from '@mastra/core/di';
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.getWorkflows();
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 this.#mastra?.storage?.loadWorkflowSnapshot({
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 this.#mastra?.storage?.loadWorkflowSnapshot({
74
+ const snapshot = await storage?.loadWorkflowSnapshot({
74
75
  workflowName: this.workflowId,
75
76
  runId: this.runId
76
77
  });
@@ -79,38 +80,40 @@ 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 this.#mastra?.storage?.loadWorkflowSnapshot({
91
+ const snapshot = await storage?.loadWorkflowSnapshot({
96
92
  workflowName: this.workflowId,
97
93
  runId: this.runId
98
94
  });
99
95
  if (snapshot) {
100
- await this.#mastra?.storage?.persistWorkflowSnapshot({
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
  }
110
107
  }
111
- async start({
108
+ async start(params) {
109
+ return this._start(params);
110
+ }
111
+ async _start({
112
112
  inputData,
113
- initialState
113
+ initialState,
114
+ outputOptions,
115
+ tracingOptions,
116
+ format
114
117
  }) {
115
118
  await this.#mastra.getStorage()?.persistWorkflowSnapshot({
116
119
  workflowName: this.workflowId,
@@ -119,14 +122,15 @@ var InngestRun = class extends Run {
119
122
  snapshot: {
120
123
  runId: this.runId,
121
124
  serializedStepGraph: this.serializedStepGraph,
125
+ status: "running",
122
126
  value: {},
123
127
  context: {},
124
128
  activePaths: [],
125
129
  suspendedPaths: {},
130
+ activeStepsPath: {},
126
131
  resumeLabels: {},
127
132
  waitingPaths: {},
128
- timestamp: Date.now(),
129
- status: "running"
133
+ timestamp: Date.now()
130
134
  }
131
135
  });
132
136
  const inputDataToUse = await this._validateInput(inputData);
@@ -137,7 +141,10 @@ var InngestRun = class extends Run {
137
141
  inputData: inputDataToUse,
138
142
  initialState: initialStateToUse,
139
143
  runId: this.runId,
140
- resourceId: this.resourceId
144
+ resourceId: this.resourceId,
145
+ outputOptions,
146
+ tracingOptions,
147
+ format
141
148
  }
142
149
  });
143
150
  const eventId = eventOutput.ids[0];
@@ -166,10 +173,16 @@ var InngestRun = class extends Run {
166
173
  return p;
167
174
  }
168
175
  async _resume(params) {
169
- const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
170
- (step) => typeof step === "string" ? step : step?.id
171
- );
172
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
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({
173
186
  workflowName: this.workflowId,
174
187
  runId: this.runId
175
188
  });
@@ -187,9 +200,99 @@ var InngestRun = class extends Run {
187
200
  steps,
188
201
  stepResults: snapshot?.context,
189
202
  resumePayload: resumeDataToUse,
190
- // @ts-ignore
191
- resumePath: snapshot?.suspendedPaths?.[steps?.[0]]
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()
192
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
193
296
  }
194
297
  });
195
298
  const eventId = eventOutput.ids[0];
@@ -203,12 +306,12 @@ var InngestRun = class extends Run {
203
306
  }
204
307
  return result;
205
308
  }
206
- watch(cb, type = "watch") {
309
+ watch(cb) {
207
310
  let active = true;
208
311
  const streamPromise = subscribe(
209
312
  {
210
313
  channel: `workflow:${this.workflowId}:${this.runId}`,
211
- topics: [type],
314
+ topics: ["watch"],
212
315
  app: this.inngest
213
316
  },
214
317
  (message) => {
@@ -226,20 +329,35 @@ var InngestRun = class extends Run {
226
329
  });
227
330
  };
228
331
  }
229
- streamLegacy({ inputData, runtimeContext } = {}) {
332
+ streamLegacy({ inputData, requestContext } = {}) {
230
333
  const { readable, writable } = new TransformStream();
231
334
  const writer = writable.getWriter();
232
335
  const unwatch = this.watch(async (event) => {
233
336
  try {
337
+ await writer.write({
338
+ // @ts-ignore
339
+ type: "start",
340
+ // @ts-ignore
341
+ payload: { runId: this.runId }
342
+ });
234
343
  const e = {
235
344
  ...event,
236
345
  type: event.type.replace("workflow-", "")
237
346
  };
347
+ if (e.type === "step-output") {
348
+ e.type = e.payload.output.type;
349
+ e.payload = e.payload.output.payload;
350
+ }
238
351
  await writer.write(e);
239
352
  } catch {
240
353
  }
241
- }, "watch-v2");
354
+ });
242
355
  this.closeStreamAction = async () => {
356
+ await writer.write({
357
+ type: "finish",
358
+ // @ts-ignore
359
+ payload: { runId: this.runId }
360
+ });
243
361
  unwatch();
244
362
  try {
245
363
  await writer.close();
@@ -249,7 +367,7 @@ var InngestRun = class extends Run {
249
367
  writer.releaseLock();
250
368
  }
251
369
  };
252
- this.executionResults = this.start({ inputData, runtimeContext }).then((result) => {
370
+ this.executionResults = this._start({ inputData, requestContext, format: "legacy" }).then((result) => {
253
371
  if (result.status !== "suspended") {
254
372
  this.closeStreamAction?.().catch(() => {
255
373
  });
@@ -263,11 +381,18 @@ var InngestRun = class extends Run {
263
381
  }
264
382
  stream({
265
383
  inputData,
266
- runtimeContext,
267
- closeOnSuspend = true
384
+ requestContext,
385
+ tracingOptions,
386
+ closeOnSuspend = true,
387
+ initialState,
388
+ outputOptions
268
389
  } = {}) {
390
+ if (this.closeStreamAction && this.streamOutput) {
391
+ return this.streamOutput;
392
+ }
393
+ this.closeStreamAction = async () => {
394
+ };
269
395
  const self = this;
270
- let streamOutput;
271
396
  const stream = new ReadableStream({
272
397
  async start(controller) {
273
398
  const unwatch = self.watch(async ({ type, from = ChunkFrom.WORKFLOW, payload }) => {
@@ -276,11 +401,11 @@ var InngestRun = class extends Run {
276
401
  runId: self.runId,
277
402
  from,
278
403
  payload: {
279
- stepName: payload.id,
404
+ stepName: payload?.id,
280
405
  ...payload
281
406
  }
282
407
  });
283
- }, "watch-v2");
408
+ });
284
409
  self.closeStreamAction = async () => {
285
410
  unwatch();
286
411
  try {
@@ -289,29 +414,115 @@ var InngestRun = class extends Run {
289
414
  console.error("Error closing stream:", err);
290
415
  }
291
416
  };
292
- const executionResultsPromise = self.start({
417
+ const executionResultsPromise = self._start({
293
418
  inputData,
294
- runtimeContext
419
+ requestContext,
420
+ // tracingContext, // We are not able to pass a reference to a span here, what to do?
421
+ initialState,
422
+ tracingOptions,
423
+ outputOptions,
424
+ format: "vnext"
295
425
  });
296
- const executionResults = await executionResultsPromise;
297
- if (closeOnSuspend) {
426
+ let executionResults;
427
+ try {
428
+ executionResults = await executionResultsPromise;
429
+ if (closeOnSuspend) {
430
+ self.closeStreamAction?.().catch(() => {
431
+ });
432
+ } else if (executionResults.status !== "suspended") {
433
+ self.closeStreamAction?.().catch(() => {
434
+ });
435
+ }
436
+ if (self.streamOutput) {
437
+ self.streamOutput.updateResults(
438
+ executionResults
439
+ );
440
+ }
441
+ } catch (err) {
442
+ self.streamOutput?.rejectResults(err);
298
443
  self.closeStreamAction?.().catch(() => {
299
444
  });
300
- } else if (executionResults.status !== "suspended") {
445
+ }
446
+ }
447
+ });
448
+ this.streamOutput = new WorkflowRunOutput({
449
+ runId: this.runId,
450
+ workflowId: this.workflowId,
451
+ stream
452
+ });
453
+ return this.streamOutput;
454
+ }
455
+ streamVNext(args = {}) {
456
+ return this.stream(args);
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);
301
515
  self.closeStreamAction?.().catch(() => {
302
516
  });
303
- }
304
- if (streamOutput) {
305
- streamOutput.updateResults(executionResults);
306
517
  }
307
518
  }
308
519
  });
309
- streamOutput = new WorkflowRunOutput({
520
+ this.streamOutput = new WorkflowRunOutput({
310
521
  runId: this.runId,
311
522
  workflowId: this.workflowId,
312
523
  stream
313
524
  });
314
- return streamOutput;
525
+ return this.streamOutput;
315
526
  }
316
527
  };
317
528
  var InngestWorkflow = class _InngestWorkflow extends Workflow {
@@ -322,6 +533,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
322
533
  constructor(params, inngest) {
323
534
  const { concurrency, rateLimit, throttle, debounce, priority, ...workflowParams } = params;
324
535
  super(workflowParams);
536
+ this.engineType = "inngest";
325
537
  const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
326
538
  ([_, value]) => value !== void 0
327
539
  );
@@ -329,13 +541,13 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
329
541
  this.#mastra = params.mastra;
330
542
  this.inngest = inngest;
331
543
  }
332
- async getWorkflowRuns(args) {
544
+ async listWorkflowRuns(args) {
333
545
  const storage = this.#mastra?.getStorage();
334
546
  if (!storage) {
335
547
  this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
336
548
  return { runs: [], total: 0 };
337
549
  }
338
- return storage.getWorkflowRuns({ workflowName: this.id, ...args ?? {} });
550
+ return storage.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
339
551
  }
340
552
  async getWorkflowRunById(runId) {
341
553
  const storage = this.#mastra?.getStorage();
@@ -364,16 +576,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
364
576
  }
365
577
  }
366
578
  }
367
- /**
368
- * @deprecated Use createRunAsync() instead.
369
- * @throws {Error} Always throws an error directing users to use createRunAsync()
370
- */
371
- createRun(_options) {
372
- throw new Error(
373
- "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."
374
- );
375
- }
376
- async createRunAsync(options) {
579
+ async createRun(options) {
377
580
  const runIdToUse = options?.runId || randomUUID();
378
581
  const run = this.runs.get(runIdToUse) ?? new InngestRun(
379
582
  {
@@ -386,7 +589,9 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
386
589
  mastra: this.#mastra,
387
590
  retryConfig: this.retryConfig,
388
591
  cleanup: () => this.runs.delete(runIdToUse),
389
- workflowSteps: this.steps
592
+ workflowSteps: this.steps,
593
+ workflowEngineType: this.engineType,
594
+ validateInputs: this.options.validateInputs
390
595
  },
391
596
  this.inngest
392
597
  );
@@ -407,13 +612,13 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
407
612
  value: {},
408
613
  context: {},
409
614
  activePaths: [],
615
+ activeStepsPath: {},
410
616
  waitingPaths: {},
411
617
  serializedStepGraph: this.serializedStepGraph,
412
618
  suspendedPaths: {},
413
619
  resumeLabels: {},
414
620
  result: void 0,
415
621
  error: void 0,
416
- // @ts-ignore
417
622
  timestamp: Date.now()
418
623
  }
419
624
  });
@@ -427,15 +632,14 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
427
632
  this.function = this.inngest.createFunction(
428
633
  {
429
634
  id: `workflow.${this.id}`,
430
- // @ts-ignore
431
- retries: this.retryConfig?.attempts ?? 0,
635
+ retries: Math.min(this.retryConfig?.attempts ?? 0, 20),
432
636
  cancelOn: [{ event: `cancel.workflow.${this.id}` }],
433
637
  // Spread flow control configuration
434
638
  ...this.flowControlConfig
435
639
  },
436
640
  { event: `workflow.${this.id}` },
437
641
  async ({ event, step, attempt, publish }) => {
438
- let { inputData, initialState, runId, resourceId, resume, outputOptions } = event.data;
642
+ let { inputData, initialState, runId, resourceId, resume, outputOptions, format, timeTravel } = event.data;
439
643
  if (!runId) {
440
644
  runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
441
645
  return randomUUID();
@@ -474,13 +678,20 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
474
678
  initialState,
475
679
  emitter,
476
680
  retryConfig: this.retryConfig,
477
- runtimeContext: new RuntimeContext(),
681
+ requestContext: new RequestContext(),
478
682
  // TODO
479
683
  resume,
684
+ timeTravel,
685
+ format,
480
686
  abortController: new AbortController(),
481
- currentSpan: void 0,
482
- // TODO: Pass actual parent AI span from workflow execution context
483
- outputOptions
687
+ // currentSpan: undefined, // TODO: Pass actual parent Span from workflow execution context
688
+ outputOptions,
689
+ writableStream: new WritableStream({
690
+ write(chunk) {
691
+ void emitter.emit("watch", chunk).catch(() => {
692
+ });
693
+ }
694
+ })
484
695
  });
485
696
  await step.run(`workflow.${this.id}.finalize`, async () => {
486
697
  if (result.status === "failed") {
@@ -518,20 +729,29 @@ function isAgent(params) {
518
729
  function isTool(params) {
519
730
  return params instanceof Tool;
520
731
  }
521
- function createStep(params) {
732
+ function createStep(params, agentOptions) {
522
733
  if (isAgent(params)) {
523
734
  return {
524
735
  id: params.name,
525
736
  description: params.getDescription(),
526
- // @ts-ignore
527
737
  inputSchema: z.object({
528
738
  prompt: z.string()
739
+ // resourceId: z.string().optional(),
740
+ // threadId: z.string().optional(),
529
741
  }),
530
- // @ts-ignore
531
742
  outputSchema: z.object({
532
743
  text: z.string()
533
744
  }),
534
- execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort, tracingContext }) => {
745
+ execute: async ({
746
+ inputData,
747
+ [EMITTER_SYMBOL]: emitter,
748
+ [STREAM_FORMAT_SYMBOL]: streamFormat,
749
+ requestContext,
750
+ tracingContext,
751
+ abortSignal,
752
+ abort,
753
+ writer
754
+ }) => {
535
755
  let streamPromise = {};
536
756
  streamPromise.promise = new Promise((resolve, reject) => {
537
757
  streamPromise.resolve = resolve;
@@ -541,61 +761,60 @@ function createStep(params) {
541
761
  name: params.name,
542
762
  args: inputData
543
763
  };
544
- if ((await params.getLLM()).getModel().specificationVersion === `v2`) {
545
- const { fullStream } = await params.stream(inputData.prompt, {
546
- runtimeContext,
764
+ let stream;
765
+ if ((await params.getModel()).specificationVersion === "v1") {
766
+ const { fullStream } = await params.streamLegacy(inputData.prompt, {
767
+ ...agentOptions ?? {},
768
+ // resourceId: inputData.resourceId,
769
+ // threadId: inputData.threadId,
770
+ requestContext,
547
771
  tracingContext,
548
772
  onFinish: (result) => {
549
773
  streamPromise.resolve(result.text);
774
+ void agentOptions?.onFinish?.(result);
550
775
  },
551
776
  abortSignal
552
777
  });
553
- if (abortSignal.aborted) {
554
- return abort();
555
- }
556
- await emitter.emit("watch-v2", {
557
- type: "tool-call-streaming-start",
558
- ...toolData ?? {}
559
- });
560
- for await (const chunk of fullStream) {
561
- if (chunk.type === "text-delta") {
562
- await emitter.emit("watch-v2", {
563
- type: "tool-call-delta",
564
- ...toolData ?? {},
565
- argsTextDelta: chunk.payload.text
566
- });
567
- }
568
- }
778
+ stream = fullStream;
569
779
  } else {
570
- const { fullStream } = await params.streamLegacy(inputData.prompt, {
571
- runtimeContext,
780
+ const modelOutput = await params.stream(inputData.prompt, {
781
+ ...agentOptions ?? {},
782
+ requestContext,
572
783
  tracingContext,
573
784
  onFinish: (result) => {
574
785
  streamPromise.resolve(result.text);
786
+ void agentOptions?.onFinish?.(result);
575
787
  },
576
788
  abortSignal
577
789
  });
578
- if (abortSignal.aborted) {
579
- return abort();
580
- }
581
- await emitter.emit("watch-v2", {
790
+ stream = modelOutput.fullStream;
791
+ }
792
+ if (streamFormat === "legacy") {
793
+ await emitter.emit("watch", {
582
794
  type: "tool-call-streaming-start",
583
795
  ...toolData ?? {}
584
796
  });
585
- for await (const chunk of fullStream) {
797
+ for await (const chunk of stream) {
586
798
  if (chunk.type === "text-delta") {
587
- await emitter.emit("watch-v2", {
799
+ await emitter.emit("watch", {
588
800
  type: "tool-call-delta",
589
801
  ...toolData ?? {},
590
802
  argsTextDelta: chunk.textDelta
591
803
  });
592
804
  }
593
805
  }
806
+ await emitter.emit("watch", {
807
+ type: "tool-call-streaming-finish",
808
+ ...toolData ?? {}
809
+ });
810
+ } else {
811
+ for await (const chunk of stream) {
812
+ await writer.write(chunk);
813
+ }
814
+ }
815
+ if (abortSignal.aborted) {
816
+ return abort();
594
817
  }
595
- await emitter.emit("watch-v2", {
596
- type: "tool-call-streaming-finish",
597
- ...toolData ?? {}
598
- });
599
818
  return {
600
819
  text: await streamPromise.promise
601
820
  };
@@ -609,20 +828,36 @@ function createStep(params) {
609
828
  }
610
829
  return {
611
830
  // TODO: tool probably should have strong id type
612
- // @ts-ignore
613
831
  id: params.id,
614
832
  description: params.description,
615
833
  inputSchema: params.inputSchema,
616
834
  outputSchema: params.outputSchema,
617
- execute: async ({ inputData, mastra, runtimeContext, tracingContext, suspend, resumeData }) => {
618
- return params.execute({
619
- context: inputData,
620
- mastra: wrapMastra(mastra, tracingContext),
621
- runtimeContext,
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,
622
850
  tracingContext,
623
- suspend,
624
- resumeData
625
- });
851
+ resumeData,
852
+ workflow: {
853
+ runId,
854
+ suspend,
855
+ workflowId,
856
+ state,
857
+ setState
858
+ }
859
+ };
860
+ return params.execute(inputData, toolContext);
626
861
  },
627
862
  component: "TOOL"
628
863
  };
@@ -656,6 +891,8 @@ function init(inngest) {
656
891
  suspendSchema: step.suspendSchema,
657
892
  stateSchema: step.stateSchema,
658
893
  execute: step.execute,
894
+ retries: step.retries,
895
+ scorers: step.scorers,
659
896
  component: step.component
660
897
  };
661
898
  },
@@ -681,63 +918,16 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
681
918
  this.inngestStep = inngestStep;
682
919
  this.inngestAttempts = inngestAttempts;
683
920
  }
684
- async execute(params) {
685
- await params.emitter.emit("watch-v2", {
686
- type: "workflow-start",
687
- payload: { runId: params.runId }
688
- });
689
- const result = await super.execute(params);
690
- await params.emitter.emit("watch-v2", {
691
- type: "workflow-finish",
692
- payload: { runId: params.runId }
693
- });
694
- return result;
695
- }
696
921
  async fmtReturnValue(emitter, stepResults, lastOutput, error) {
697
922
  const base = {
698
923
  status: lastOutput.status,
699
924
  steps: stepResults
700
925
  };
701
926
  if (lastOutput.status === "success") {
702
- await emitter.emit("watch", {
703
- type: "watch",
704
- payload: {
705
- workflowState: {
706
- status: lastOutput.status,
707
- steps: stepResults,
708
- result: lastOutput.output
709
- }
710
- },
711
- eventTimestamp: Date.now()
712
- });
713
927
  base.result = lastOutput.output;
714
928
  } else if (lastOutput.status === "failed") {
715
929
  base.error = error instanceof Error ? error?.stack ?? error.message : lastOutput?.error instanceof Error ? lastOutput.error.message : lastOutput.error ?? error ?? "Unknown error";
716
- await emitter.emit("watch", {
717
- type: "watch",
718
- payload: {
719
- workflowState: {
720
- status: lastOutput.status,
721
- steps: stepResults,
722
- result: null,
723
- error: base.error
724
- }
725
- },
726
- eventTimestamp: Date.now()
727
- });
728
930
  } else if (lastOutput.status === "suspended") {
729
- await emitter.emit("watch", {
730
- type: "watch",
731
- payload: {
732
- workflowState: {
733
- status: lastOutput.status,
734
- steps: stepResults,
735
- result: null,
736
- error: null
737
- }
738
- },
739
- eventTimestamp: Date.now()
740
- });
741
931
  const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
742
932
  if (stepResult?.status === "suspended") {
743
933
  const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
@@ -760,14 +950,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
760
950
  stepResults,
761
951
  emitter,
762
952
  abortController,
763
- runtimeContext,
953
+ requestContext,
764
954
  executionContext,
765
955
  writableStream,
766
956
  tracingContext
767
957
  }) {
768
958
  let { duration, fn } = entry;
769
959
  const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
770
- type: AISpanType.WORKFLOW_SLEEP,
960
+ type: SpanType.WORKFLOW_SLEEP,
771
961
  name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
772
962
  attributes: {
773
963
  durationMs: duration,
@@ -784,13 +974,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
784
974
  runId,
785
975
  workflowId,
786
976
  mastra: this.mastra,
787
- runtimeContext,
977
+ requestContext,
788
978
  inputData: prevOutput,
789
979
  state: executionContext.state,
790
980
  setState: (state) => {
791
981
  executionContext.state = state;
792
982
  },
793
- runCount: -1,
794
983
  retryCount: -1,
795
984
  tracingContext: {
796
985
  currentSpan: sleepSpan
@@ -806,7 +995,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
806
995
  abortController?.abort();
807
996
  },
808
997
  [EMITTER_SYMBOL]: emitter,
809
- // TODO: add streamVNext support
810
998
  [STREAM_FORMAT_SYMBOL]: executionContext.format,
811
999
  engine: { step: this.inngestStep },
812
1000
  abortSignal: abortController?.signal,
@@ -850,14 +1038,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
850
1038
  stepResults,
851
1039
  emitter,
852
1040
  abortController,
853
- runtimeContext,
1041
+ requestContext,
854
1042
  executionContext,
855
1043
  writableStream,
856
1044
  tracingContext
857
1045
  }) {
858
1046
  let { date, fn } = entry;
859
1047
  const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
860
- type: AISpanType.WORKFLOW_SLEEP,
1048
+ type: SpanType.WORKFLOW_SLEEP,
861
1049
  name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
862
1050
  attributes: {
863
1051
  untilDate: date,
@@ -875,13 +1063,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
875
1063
  runId,
876
1064
  workflowId,
877
1065
  mastra: this.mastra,
878
- runtimeContext,
1066
+ requestContext,
879
1067
  inputData: prevOutput,
880
1068
  state: executionContext.state,
881
1069
  setState: (state) => {
882
1070
  executionContext.state = state;
883
1071
  },
884
- runCount: -1,
885
1072
  retryCount: -1,
886
1073
  tracingContext: {
887
1074
  currentSpan: sleepUntilSpan
@@ -898,7 +1085,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
898
1085
  },
899
1086
  [EMITTER_SYMBOL]: emitter,
900
1087
  [STREAM_FORMAT_SYMBOL]: executionContext.format,
901
- // TODO: add streamVNext support
902
1088
  engine: { step: this.inngestStep },
903
1089
  abortSignal: abortController?.signal,
904
1090
  writer: new ToolStream(
@@ -941,32 +1127,23 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
941
1127
  throw e;
942
1128
  }
943
1129
  }
944
- async executeWaitForEvent({ event, timeout }) {
945
- const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
946
- event: `user-event-${event}`,
947
- timeout: timeout ?? 5e3
948
- });
949
- if (eventData === null) {
950
- throw "Timeout waiting for event";
951
- }
952
- return eventData?.data;
953
- }
954
1130
  async executeStep({
955
1131
  step,
956
1132
  stepResults,
957
1133
  executionContext,
958
1134
  resume,
1135
+ timeTravel,
959
1136
  prevOutput,
960
1137
  emitter,
961
1138
  abortController,
962
- runtimeContext,
1139
+ requestContext,
963
1140
  tracingContext,
964
1141
  writableStream,
965
1142
  disableScorers
966
1143
  }) {
967
- const stepAISpan = tracingContext?.currentSpan?.createChildSpan({
1144
+ const stepSpan = tracingContext?.currentSpan?.createChildSpan({
968
1145
  name: `workflow step: '${step.id}'`,
969
- type: AISpanType.WORKFLOW_STEP,
1146
+ type: SpanType.WORKFLOW_STEP,
970
1147
  input: prevOutput,
971
1148
  attributes: {
972
1149
  stepId: step.id
@@ -976,34 +1153,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
976
1153
  const { inputData, validationError } = await validateStepInput({
977
1154
  prevOutput,
978
1155
  step,
979
- validateInputs: this.options?.validateInputs ?? false
1156
+ validateInputs: this.options?.validateInputs ?? true
980
1157
  });
981
1158
  const startedAt = await this.inngestStep.run(
982
1159
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
983
1160
  async () => {
984
1161
  const startedAt2 = Date.now();
985
1162
  await emitter.emit("watch", {
986
- type: "watch",
987
- payload: {
988
- currentStep: {
989
- id: step.id,
990
- status: "running"
991
- },
992
- workflowState: {
993
- status: "running",
994
- steps: {
995
- ...stepResults,
996
- [step.id]: {
997
- status: "running"
998
- }
999
- },
1000
- result: null,
1001
- error: null
1002
- }
1003
- },
1004
- eventTimestamp: Date.now()
1005
- });
1006
- await emitter.emit("watch-v2", {
1007
1163
  type: "workflow-step-start",
1008
1164
  payload: {
1009
1165
  id: step.id,
@@ -1019,9 +1175,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1019
1175
  const isResume = !!resume?.steps?.length;
1020
1176
  let result;
1021
1177
  let runId;
1178
+ const isTimeTravel = !!(timeTravel && timeTravel.steps?.length > 1 && timeTravel.steps[0] === step.id);
1022
1179
  try {
1023
1180
  if (isResume) {
1024
- runId = stepResults[resume?.steps?.[0]]?.suspendPayload?.__workflow_meta?.runId ?? randomUUID();
1181
+ runId = stepResults[resume?.steps?.[0] ?? ""]?.suspendPayload?.__workflow_meta?.runId ?? randomUUID();
1025
1182
  const snapshot = await this.mastra?.getStorage()?.loadWorkflowSnapshot({
1026
1183
  workflowName: step.id,
1027
1184
  runId
@@ -1037,8 +1194,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1037
1194
  steps: resume.steps.slice(1),
1038
1195
  stepResults: snapshot?.context,
1039
1196
  resumePayload: resume.resumePayload,
1040
- // @ts-ignore
1041
- resumePath: snapshot?.suspendedPaths?.[resume.steps?.[1]]
1197
+ resumePath: resume.steps?.[1] ? snapshot?.suspendedPaths?.[resume.steps?.[1]] : void 0
1042
1198
  },
1043
1199
  outputOptions: { includeState: true }
1044
1200
  }
@@ -1046,6 +1202,32 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1046
1202
  result = invokeResp.result;
1047
1203
  runId = invokeResp.runId;
1048
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;
1049
1231
  } else {
1050
1232
  const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
1051
1233
  function: step.getFunction(),
@@ -1079,23 +1261,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1079
1261
  async () => {
1080
1262
  if (result.status === "failed") {
1081
1263
  await emitter.emit("watch", {
1082
- type: "watch",
1083
- payload: {
1084
- currentStep: {
1085
- id: step.id,
1086
- status: "failed",
1087
- error: result?.error
1088
- },
1089
- workflowState: {
1090
- status: "running",
1091
- steps: stepResults,
1092
- result: null,
1093
- error: null
1094
- }
1095
- },
1096
- eventTimestamp: Date.now()
1097
- });
1098
- await emitter.emit("watch-v2", {
1099
1264
  type: "workflow-step-result",
1100
1265
  payload: {
1101
1266
  id: step.id,
@@ -1114,27 +1279,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1114
1279
  const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
1115
1280
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1116
1281
  await emitter.emit("watch", {
1117
- type: "watch",
1118
- payload: {
1119
- currentStep: {
1120
- id: step.id,
1121
- status: "suspended",
1122
- payload: stepResult.payload,
1123
- suspendPayload: {
1124
- ...stepResult?.suspendPayload,
1125
- __workflow_meta: { runId, path: suspendPath }
1126
- }
1127
- },
1128
- workflowState: {
1129
- status: "running",
1130
- steps: stepResults,
1131
- result: null,
1132
- error: null
1133
- }
1134
- },
1135
- eventTimestamp: Date.now()
1136
- });
1137
- await emitter.emit("watch-v2", {
1138
1282
  type: "workflow-step-suspended",
1139
1283
  payload: {
1140
1284
  id: step.id,
@@ -1153,23 +1297,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1153
1297
  }
1154
1298
  };
1155
1299
  }
1156
- await emitter.emit("watch", {
1157
- type: "watch",
1158
- payload: {
1159
- currentStep: {
1160
- id: step.id,
1161
- status: "suspended",
1162
- payload: {}
1163
- },
1164
- workflowState: {
1165
- status: "running",
1166
- steps: stepResults,
1167
- result: null,
1168
- error: null
1169
- }
1170
- },
1171
- eventTimestamp: Date.now()
1172
- });
1173
1300
  return {
1174
1301
  executionContext,
1175
1302
  result: {
@@ -1179,23 +1306,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1179
1306
  };
1180
1307
  }
1181
1308
  await emitter.emit("watch", {
1182
- type: "watch",
1183
- payload: {
1184
- currentStep: {
1185
- id: step.id,
1186
- status: "success",
1187
- output: result?.result
1188
- },
1189
- workflowState: {
1190
- status: "running",
1191
- steps: stepResults,
1192
- result: null,
1193
- error: null
1194
- }
1195
- },
1196
- eventTimestamp: Date.now()
1197
- });
1198
- await emitter.emit("watch-v2", {
1199
1309
  type: "workflow-step-result",
1200
1310
  payload: {
1201
1311
  id: step.id,
@@ -1203,7 +1313,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1203
1313
  output: result?.result
1204
1314
  }
1205
1315
  });
1206
- await emitter.emit("watch-v2", {
1316
+ await emitter.emit("watch", {
1207
1317
  type: "workflow-step-finish",
1208
1318
  payload: {
1209
1319
  id: step.id,
@@ -1223,29 +1333,56 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1223
1333
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1224
1334
  };
1225
1335
  }
1336
+ const stepCallId = randomUUID();
1226
1337
  let stepRes;
1227
1338
  try {
1228
1339
  stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
1229
1340
  let execResults;
1230
1341
  let suspended;
1231
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
+ }
1232
1358
  try {
1233
1359
  if (validationError) {
1234
1360
  throw validationError;
1235
1361
  }
1362
+ const retryCount = this.getOrGenerateRetryCount(step.id);
1236
1363
  const result = await step.execute({
1237
1364
  runId: executionContext.runId,
1365
+ workflowId: executionContext.workflowId,
1238
1366
  mastra: this.mastra,
1239
- runtimeContext,
1240
- writableStream,
1367
+ requestContext,
1368
+ retryCount,
1369
+ writer: new ToolStream(
1370
+ {
1371
+ prefix: "workflow-step",
1372
+ callId: stepCallId,
1373
+ name: step.id,
1374
+ runId: executionContext.runId
1375
+ },
1376
+ writableStream
1377
+ ),
1241
1378
  state: executionContext?.state ?? {},
1242
1379
  setState: (state) => {
1243
1380
  executionContext.state = state;
1244
1381
  },
1245
1382
  inputData,
1246
- resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
1383
+ resumeData: resumeDataToUse,
1247
1384
  tracingContext: {
1248
- currentSpan: stepAISpan
1385
+ currentSpan: stepSpan
1249
1386
  },
1250
1387
  getInitData: () => stepResults?.input,
1251
1388
  getStepResult: getStepResult.bind(this, stepResults),
@@ -1265,13 +1402,11 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1265
1402
  bail: (result2) => {
1266
1403
  bailed = { payload: result2 };
1267
1404
  },
1268
- resume: {
1269
- steps: resume?.steps?.slice(1) || [],
1270
- resumePayload: resume?.resumePayload,
1271
- // @ts-ignore
1272
- runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
1405
+ abort: () => {
1406
+ abortController?.abort();
1273
1407
  },
1274
1408
  [EMITTER_SYMBOL]: emitter,
1409
+ [STREAM_FORMAT_SYMBOL]: executionContext.format,
1275
1410
  engine: {
1276
1411
  step: this.inngestStep
1277
1412
  },
@@ -1284,8 +1419,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1284
1419
  startedAt,
1285
1420
  endedAt,
1286
1421
  payload: inputData,
1287
- resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1288
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1422
+ resumedAt: resumeDataToUse ? startedAt : void 0,
1423
+ resumePayload: resumeDataToUse
1289
1424
  };
1290
1425
  } catch (e) {
1291
1426
  const stepFailure = {
@@ -1294,12 +1429,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1294
1429
  error: e instanceof Error ? e.message : String(e),
1295
1430
  endedAt: Date.now(),
1296
1431
  startedAt,
1297
- resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1298
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1432
+ resumedAt: resumeDataToUse ? startedAt : void 0,
1433
+ resumePayload: resumeDataToUse
1299
1434
  };
1300
1435
  execResults = stepFailure;
1301
1436
  const fallbackErrorMessage = `Step ${step.id} failed`;
1302
- stepAISpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1437
+ stepSpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1303
1438
  throw new RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
1304
1439
  cause: execResults
1305
1440
  });
@@ -1308,11 +1443,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1308
1443
  execResults = {
1309
1444
  status: "suspended",
1310
1445
  suspendPayload: suspended.payload,
1446
+ ...execResults.output ? { suspendOutput: execResults.output } : {},
1311
1447
  payload: inputData,
1312
1448
  suspendedAt: Date.now(),
1313
1449
  startedAt,
1314
- resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
1315
- resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1450
+ resumedAt: resumeDataToUse ? startedAt : void 0,
1451
+ resumePayload: resumeDataToUse
1316
1452
  };
1317
1453
  } else if (bailed) {
1318
1454
  execResults = {
@@ -1323,24 +1459,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1323
1459
  startedAt
1324
1460
  };
1325
1461
  }
1326
- await emitter.emit("watch", {
1327
- type: "watch",
1328
- payload: {
1329
- currentStep: {
1330
- id: step.id,
1331
- ...execResults
1332
- },
1333
- workflowState: {
1334
- status: "running",
1335
- steps: { ...stepResults, [step.id]: execResults },
1336
- result: null,
1337
- error: null
1338
- }
1339
- },
1340
- eventTimestamp: Date.now()
1341
- });
1342
1462
  if (execResults.status === "suspended") {
1343
- await emitter.emit("watch-v2", {
1463
+ await emitter.emit("watch", {
1344
1464
  type: "workflow-step-suspended",
1345
1465
  payload: {
1346
1466
  id: step.id,
@@ -1348,14 +1468,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1348
1468
  }
1349
1469
  });
1350
1470
  } else {
1351
- await emitter.emit("watch-v2", {
1471
+ await emitter.emit("watch", {
1352
1472
  type: "workflow-step-result",
1353
1473
  payload: {
1354
1474
  id: step.id,
1355
1475
  ...execResults
1356
1476
  }
1357
1477
  });
1358
- await emitter.emit("watch-v2", {
1478
+ await emitter.emit("watch", {
1359
1479
  type: "workflow-step-finish",
1360
1480
  payload: {
1361
1481
  id: step.id,
@@ -1363,7 +1483,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1363
1483
  }
1364
1484
  });
1365
1485
  }
1366
- stepAISpan?.end({ output: execResults });
1486
+ stepSpan?.end({ output: execResults });
1367
1487
  return { result: execResults, executionContext, stepResults };
1368
1488
  });
1369
1489
  } catch (e) {
@@ -1393,15 +1513,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1393
1513
  output: stepRes.result,
1394
1514
  workflowId: executionContext.workflowId,
1395
1515
  stepId: step.id,
1396
- runtimeContext,
1516
+ requestContext,
1397
1517
  disableScorers,
1398
- tracingContext: { currentSpan: stepAISpan }
1518
+ tracingContext: { currentSpan: stepSpan }
1399
1519
  });
1400
1520
  }
1401
1521
  });
1402
1522
  }
1403
1523
  Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
1404
- Object.assign(stepResults, stepRes.stepResults);
1405
1524
  executionContext.state = stepRes.executionContext.state;
1406
1525
  return stepRes.result;
1407
1526
  }
@@ -1429,17 +1548,17 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1429
1548
  resourceId,
1430
1549
  snapshot: {
1431
1550
  runId,
1551
+ status: workflowStatus,
1432
1552
  value: executionContext.state,
1433
1553
  context: stepResults,
1434
- activePaths: [],
1554
+ activePaths: executionContext.executionPath,
1555
+ activeStepsPath: executionContext.activeStepsPath,
1435
1556
  suspendedPaths: executionContext.suspendedPaths,
1436
1557
  resumeLabels: executionContext.resumeLabels,
1437
1558
  waitingPaths: {},
1438
1559
  serializedStepGraph,
1439
- status: workflowStatus,
1440
1560
  result,
1441
1561
  error,
1442
- // @ts-ignore
1443
1562
  timestamp: Date.now()
1444
1563
  }
1445
1564
  });
@@ -1452,17 +1571,18 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1452
1571
  entry,
1453
1572
  prevOutput,
1454
1573
  stepResults,
1574
+ timeTravel,
1455
1575
  resume,
1456
1576
  executionContext,
1457
1577
  emitter,
1458
1578
  abortController,
1459
- runtimeContext,
1579
+ requestContext,
1460
1580
  writableStream,
1461
1581
  disableScorers,
1462
1582
  tracingContext
1463
1583
  }) {
1464
1584
  const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
1465
- type: AISpanType.WORKFLOW_CONDITIONAL,
1585
+ type: SpanType.WORKFLOW_CONDITIONAL,
1466
1586
  name: `conditional: '${entry.conditions.length} conditions'`,
1467
1587
  input: prevOutput,
1468
1588
  attributes: {
@@ -1475,7 +1595,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1475
1595
  entry.conditions.map(
1476
1596
  (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
1477
1597
  const evalSpan = conditionalSpan?.createChildSpan({
1478
- type: AISpanType.WORKFLOW_CONDITIONAL_EVAL,
1598
+ type: SpanType.WORKFLOW_CONDITIONAL_EVAL,
1479
1599
  name: `condition: '${index}'`,
1480
1600
  input: prevOutput,
1481
1601
  attributes: {
@@ -1490,8 +1610,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1490
1610
  runId,
1491
1611
  workflowId,
1492
1612
  mastra: this.mastra,
1493
- runtimeContext,
1494
- runCount: -1,
1613
+ requestContext,
1495
1614
  retryCount: -1,
1496
1615
  inputData: prevOutput,
1497
1616
  state: executionContext.state,
@@ -1513,7 +1632,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1513
1632
  },
1514
1633
  [EMITTER_SYMBOL]: emitter,
1515
1634
  [STREAM_FORMAT_SYMBOL]: executionContext.format,
1516
- // TODO: add streamVNext support
1517
1635
  engine: {
1518
1636
  step: this.inngestStep
1519
1637
  },
@@ -1572,10 +1690,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1572
1690
  prevOutput,
1573
1691
  stepResults,
1574
1692
  resume,
1693
+ timeTravel,
1575
1694
  executionContext: {
1576
1695
  workflowId,
1577
1696
  runId,
1578
1697
  executionPath: [...executionContext.executionPath, index],
1698
+ activeStepsPath: executionContext.activeStepsPath,
1579
1699
  suspendedPaths: executionContext.suspendedPaths,
1580
1700
  resumeLabels: executionContext.resumeLabels,
1581
1701
  retryConfig: executionContext.retryConfig,
@@ -1583,7 +1703,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1583
1703
  },
1584
1704
  emitter,
1585
1705
  abortController,
1586
- runtimeContext,
1706
+ requestContext,
1587
1707
  writableStream,
1588
1708
  disableScorers,
1589
1709
  tracingContext: {
@@ -1599,13 +1719,19 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1599
1719
  if (hasFailed) {
1600
1720
  execResults = { status: "failed", error: hasFailed.error };
1601
1721
  } else if (hasSuspended) {
1602
- execResults = { status: "suspended", suspendPayload: hasSuspended.suspendPayload };
1722
+ execResults = {
1723
+ status: "suspended",
1724
+ suspendPayload: hasSuspended.suspendPayload,
1725
+ ...hasSuspended.suspendOutput ? { suspendOutput: hasSuspended.suspendOutput } : {}
1726
+ };
1603
1727
  } else {
1604
1728
  execResults = {
1605
1729
  status: "success",
1606
1730
  output: results.reduce((acc, result, index) => {
1607
1731
  if (result.status === "success") {
1608
- acc[stepsToRun[index].step.id] = result.output;
1732
+ if ("step" in stepsToRun[index]) {
1733
+ acc[stepsToRun[index].step.id] = result.output;
1734
+ }
1609
1735
  }
1610
1736
  return acc;
1611
1737
  }, {})