@mastra/inngest 0.0.0-playground-studio-cloud-20251031080052 → 0.0.0-playground-studio-again-20251114100107

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,8 +1,8 @@
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 { wrapMastra, SpanType } from '@mastra/core/observability';
6
6
  import { ChunkFrom, WorkflowRunOutput } from '@mastra/core/stream';
7
7
  import { ToolStream, Tool } from '@mastra/core/tools';
8
8
  import { Run, Workflow, DefaultExecutionEngine, createDeprecationProxy, getStepResult, runCountDeprecationMessage, validateStepInput } from '@mastra/core/workflows';
@@ -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,11 @@ var InngestRun = class extends Run {
166
173
  return p;
167
174
  }
168
175
  async _resume(params) {
176
+ const storage = this.#mastra?.getStorage();
169
177
  const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
170
178
  (step) => typeof step === "string" ? step : step?.id
171
179
  );
172
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
180
+ const snapshot = await storage?.loadWorkflowSnapshot({
173
181
  workflowName: this.workflowId,
174
182
  runId: this.runId
175
183
  });
@@ -203,12 +211,12 @@ var InngestRun = class extends Run {
203
211
  }
204
212
  return result;
205
213
  }
206
- watch(cb, type = "watch") {
214
+ watch(cb) {
207
215
  let active = true;
208
216
  const streamPromise = subscribe(
209
217
  {
210
218
  channel: `workflow:${this.workflowId}:${this.runId}`,
211
- topics: [type],
219
+ topics: ["watch"],
212
220
  app: this.inngest
213
221
  },
214
222
  (message) => {
@@ -226,20 +234,35 @@ var InngestRun = class extends Run {
226
234
  });
227
235
  };
228
236
  }
229
- streamLegacy({ inputData, runtimeContext } = {}) {
237
+ streamLegacy({ inputData, requestContext } = {}) {
230
238
  const { readable, writable } = new TransformStream();
231
239
  const writer = writable.getWriter();
232
240
  const unwatch = this.watch(async (event) => {
233
241
  try {
242
+ await writer.write({
243
+ // @ts-ignore
244
+ type: "start",
245
+ // @ts-ignore
246
+ payload: { runId: this.runId }
247
+ });
234
248
  const e = {
235
249
  ...event,
236
250
  type: event.type.replace("workflow-", "")
237
251
  };
252
+ if (e.type === "step-output") {
253
+ e.type = e.payload.output.type;
254
+ e.payload = e.payload.output.payload;
255
+ }
238
256
  await writer.write(e);
239
257
  } catch {
240
258
  }
241
- }, "watch-v2");
259
+ });
242
260
  this.closeStreamAction = async () => {
261
+ await writer.write({
262
+ type: "finish",
263
+ // @ts-ignore
264
+ payload: { runId: this.runId }
265
+ });
243
266
  unwatch();
244
267
  try {
245
268
  await writer.close();
@@ -249,7 +272,7 @@ var InngestRun = class extends Run {
249
272
  writer.releaseLock();
250
273
  }
251
274
  };
252
- this.executionResults = this.start({ inputData, runtimeContext }).then((result) => {
275
+ this.executionResults = this._start({ inputData, requestContext, format: "legacy" }).then((result) => {
253
276
  if (result.status !== "suspended") {
254
277
  this.closeStreamAction?.().catch(() => {
255
278
  });
@@ -263,11 +286,18 @@ var InngestRun = class extends Run {
263
286
  }
264
287
  stream({
265
288
  inputData,
266
- runtimeContext,
267
- closeOnSuspend = true
289
+ requestContext,
290
+ tracingOptions,
291
+ closeOnSuspend = true,
292
+ initialState,
293
+ outputOptions
268
294
  } = {}) {
295
+ if (this.closeStreamAction && this.streamOutput) {
296
+ return this.streamOutput;
297
+ }
298
+ this.closeStreamAction = async () => {
299
+ };
269
300
  const self = this;
270
- let streamOutput;
271
301
  const stream = new ReadableStream({
272
302
  async start(controller) {
273
303
  const unwatch = self.watch(async ({ type, from = ChunkFrom.WORKFLOW, payload }) => {
@@ -276,11 +306,11 @@ var InngestRun = class extends Run {
276
306
  runId: self.runId,
277
307
  from,
278
308
  payload: {
279
- stepName: payload.id,
309
+ stepName: payload?.id,
280
310
  ...payload
281
311
  }
282
312
  });
283
- }, "watch-v2");
313
+ });
284
314
  self.closeStreamAction = async () => {
285
315
  unwatch();
286
316
  try {
@@ -289,29 +319,46 @@ var InngestRun = class extends Run {
289
319
  console.error("Error closing stream:", err);
290
320
  }
291
321
  };
292
- const executionResultsPromise = self.start({
322
+ const executionResultsPromise = self._start({
293
323
  inputData,
294
- runtimeContext
324
+ requestContext,
325
+ // tracingContext, // We are not able to pass a reference to a span here, what to do?
326
+ initialState,
327
+ tracingOptions,
328
+ outputOptions,
329
+ format: "vnext"
295
330
  });
296
- const executionResults = await executionResultsPromise;
297
- if (closeOnSuspend) {
298
- self.closeStreamAction?.().catch(() => {
299
- });
300
- } else if (executionResults.status !== "suspended") {
331
+ let executionResults;
332
+ try {
333
+ executionResults = await executionResultsPromise;
334
+ if (closeOnSuspend) {
335
+ self.closeStreamAction?.().catch(() => {
336
+ });
337
+ } else if (executionResults.status !== "suspended") {
338
+ self.closeStreamAction?.().catch(() => {
339
+ });
340
+ }
341
+ if (self.streamOutput) {
342
+ self.streamOutput.updateResults(
343
+ executionResults
344
+ );
345
+ }
346
+ } catch (err) {
347
+ self.streamOutput?.rejectResults(err);
301
348
  self.closeStreamAction?.().catch(() => {
302
349
  });
303
350
  }
304
- if (streamOutput) {
305
- streamOutput.updateResults(executionResults);
306
- }
307
351
  }
308
352
  });
309
- streamOutput = new WorkflowRunOutput({
353
+ this.streamOutput = new WorkflowRunOutput({
310
354
  runId: this.runId,
311
355
  workflowId: this.workflowId,
312
356
  stream
313
357
  });
314
- return streamOutput;
358
+ return this.streamOutput;
359
+ }
360
+ streamVNext(args = {}) {
361
+ return this.stream(args);
315
362
  }
316
363
  };
317
364
  var InngestWorkflow = class _InngestWorkflow extends Workflow {
@@ -322,6 +369,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
322
369
  constructor(params, inngest) {
323
370
  const { concurrency, rateLimit, throttle, debounce, priority, ...workflowParams } = params;
324
371
  super(workflowParams);
372
+ this.engineType = "inngest";
325
373
  const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
326
374
  ([_, value]) => value !== void 0
327
375
  );
@@ -329,13 +377,13 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
329
377
  this.#mastra = params.mastra;
330
378
  this.inngest = inngest;
331
379
  }
332
- async getWorkflowRuns(args) {
380
+ async listWorkflowRuns(args) {
333
381
  const storage = this.#mastra?.getStorage();
334
382
  if (!storage) {
335
383
  this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
336
384
  return { runs: [], total: 0 };
337
385
  }
338
- return storage.getWorkflowRuns({ workflowName: this.id, ...args ?? {} });
386
+ return storage.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
339
387
  }
340
388
  async getWorkflowRunById(runId) {
341
389
  const storage = this.#mastra?.getStorage();
@@ -364,16 +412,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
364
412
  }
365
413
  }
366
414
  }
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) {
415
+ async createRun(options) {
377
416
  const runIdToUse = options?.runId || randomUUID();
378
417
  const run = this.runs.get(runIdToUse) ?? new InngestRun(
379
418
  {
@@ -386,7 +425,8 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
386
425
  mastra: this.#mastra,
387
426
  retryConfig: this.retryConfig,
388
427
  cleanup: () => this.runs.delete(runIdToUse),
389
- workflowSteps: this.steps
428
+ workflowSteps: this.steps,
429
+ workflowEngineType: this.engineType
390
430
  },
391
431
  this.inngest
392
432
  );
@@ -407,6 +447,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
407
447
  value: {},
408
448
  context: {},
409
449
  activePaths: [],
450
+ activeStepsPath: {},
410
451
  waitingPaths: {},
411
452
  serializedStepGraph: this.serializedStepGraph,
412
453
  suspendedPaths: {},
@@ -435,7 +476,7 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
435
476
  },
436
477
  { event: `workflow.${this.id}` },
437
478
  async ({ event, step, attempt, publish }) => {
438
- let { inputData, initialState, runId, resourceId, resume, outputOptions } = event.data;
479
+ let { inputData, initialState, runId, resourceId, resume, outputOptions, format } = event.data;
439
480
  if (!runId) {
440
481
  runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
441
482
  return randomUUID();
@@ -474,13 +515,19 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
474
515
  initialState,
475
516
  emitter,
476
517
  retryConfig: this.retryConfig,
477
- runtimeContext: new RuntimeContext(),
518
+ requestContext: new RequestContext(),
478
519
  // TODO
479
520
  resume,
521
+ format,
480
522
  abortController: new AbortController(),
481
- currentSpan: void 0,
482
- // TODO: Pass actual parent AI span from workflow execution context
483
- outputOptions
523
+ // currentSpan: undefined, // TODO: Pass actual parent Span from workflow execution context
524
+ outputOptions,
525
+ writableStream: new WritableStream({
526
+ write(chunk) {
527
+ void emitter.emit("watch", chunk).catch(() => {
528
+ });
529
+ }
530
+ })
484
531
  });
485
532
  await step.run(`workflow.${this.id}.finalize`, async () => {
486
533
  if (result.status === "failed") {
@@ -518,7 +565,7 @@ function isAgent(params) {
518
565
  function isTool(params) {
519
566
  return params instanceof Tool;
520
567
  }
521
- function createStep(params) {
568
+ function createStep(params, agentOptions) {
522
569
  if (isAgent(params)) {
523
570
  return {
524
571
  id: params.name,
@@ -526,12 +573,23 @@ function createStep(params) {
526
573
  // @ts-ignore
527
574
  inputSchema: z.object({
528
575
  prompt: z.string()
576
+ // resourceId: z.string().optional(),
577
+ // threadId: z.string().optional(),
529
578
  }),
530
579
  // @ts-ignore
531
580
  outputSchema: z.object({
532
581
  text: z.string()
533
582
  }),
534
- execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort, tracingContext }) => {
583
+ execute: async ({
584
+ inputData,
585
+ [EMITTER_SYMBOL]: emitter,
586
+ [STREAM_FORMAT_SYMBOL]: streamFormat,
587
+ requestContext,
588
+ tracingContext,
589
+ abortSignal,
590
+ abort,
591
+ writer
592
+ }) => {
535
593
  let streamPromise = {};
536
594
  streamPromise.promise = new Promise((resolve, reject) => {
537
595
  streamPromise.resolve = resolve;
@@ -541,61 +599,60 @@ function createStep(params) {
541
599
  name: params.name,
542
600
  args: inputData
543
601
  };
544
- if ((await params.getLLM()).getModel().specificationVersion === `v2`) {
545
- const { fullStream } = await params.stream(inputData.prompt, {
546
- runtimeContext,
602
+ let stream;
603
+ if ((await params.getModel()).specificationVersion === "v1") {
604
+ const { fullStream } = await params.streamLegacy(inputData.prompt, {
605
+ ...agentOptions ?? {},
606
+ // resourceId: inputData.resourceId,
607
+ // threadId: inputData.threadId,
608
+ requestContext,
547
609
  tracingContext,
548
610
  onFinish: (result) => {
549
611
  streamPromise.resolve(result.text);
612
+ void agentOptions?.onFinish?.(result);
550
613
  },
551
614
  abortSignal
552
615
  });
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
- }
616
+ stream = fullStream;
569
617
  } else {
570
- const { fullStream } = await params.streamLegacy(inputData.prompt, {
571
- runtimeContext,
618
+ const modelOutput = await params.stream(inputData.prompt, {
619
+ ...agentOptions ?? {},
620
+ requestContext,
572
621
  tracingContext,
573
622
  onFinish: (result) => {
574
623
  streamPromise.resolve(result.text);
624
+ void agentOptions?.onFinish?.(result);
575
625
  },
576
626
  abortSignal
577
627
  });
578
- if (abortSignal.aborted) {
579
- return abort();
580
- }
581
- await emitter.emit("watch-v2", {
628
+ stream = modelOutput.fullStream;
629
+ }
630
+ if (streamFormat === "legacy") {
631
+ await emitter.emit("watch", {
582
632
  type: "tool-call-streaming-start",
583
633
  ...toolData ?? {}
584
634
  });
585
- for await (const chunk of fullStream) {
635
+ for await (const chunk of stream) {
586
636
  if (chunk.type === "text-delta") {
587
- await emitter.emit("watch-v2", {
637
+ await emitter.emit("watch", {
588
638
  type: "tool-call-delta",
589
639
  ...toolData ?? {},
590
640
  argsTextDelta: chunk.textDelta
591
641
  });
592
642
  }
593
643
  }
644
+ await emitter.emit("watch", {
645
+ type: "tool-call-streaming-finish",
646
+ ...toolData ?? {}
647
+ });
648
+ } else {
649
+ for await (const chunk of stream) {
650
+ await writer.write(chunk);
651
+ }
652
+ }
653
+ if (abortSignal.aborted) {
654
+ return abort();
594
655
  }
595
- await emitter.emit("watch-v2", {
596
- type: "tool-call-streaming-finish",
597
- ...toolData ?? {}
598
- });
599
656
  return {
600
657
  text: await streamPromise.promise
601
658
  };
@@ -614,11 +671,11 @@ function createStep(params) {
614
671
  description: params.description,
615
672
  inputSchema: params.inputSchema,
616
673
  outputSchema: params.outputSchema,
617
- execute: async ({ inputData, mastra, runtimeContext, tracingContext, suspend, resumeData }) => {
674
+ execute: async ({ inputData, mastra, requestContext, tracingContext, suspend, resumeData }) => {
618
675
  return params.execute({
619
676
  context: inputData,
620
677
  mastra: wrapMastra(mastra, tracingContext),
621
- runtimeContext,
678
+ requestContext,
622
679
  tracingContext,
623
680
  suspend,
624
681
  resumeData
@@ -681,63 +738,16 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
681
738
  this.inngestStep = inngestStep;
682
739
  this.inngestAttempts = inngestAttempts;
683
740
  }
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
741
  async fmtReturnValue(emitter, stepResults, lastOutput, error) {
697
742
  const base = {
698
743
  status: lastOutput.status,
699
744
  steps: stepResults
700
745
  };
701
746
  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
747
  base.result = lastOutput.output;
714
748
  } else if (lastOutput.status === "failed") {
715
749
  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
750
  } 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
751
  const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
742
752
  if (stepResult?.status === "suspended") {
743
753
  const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
@@ -760,14 +770,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
760
770
  stepResults,
761
771
  emitter,
762
772
  abortController,
763
- runtimeContext,
773
+ requestContext,
764
774
  executionContext,
765
775
  writableStream,
766
776
  tracingContext
767
777
  }) {
768
778
  let { duration, fn } = entry;
769
779
  const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
770
- type: AISpanType.WORKFLOW_SLEEP,
780
+ type: SpanType.WORKFLOW_SLEEP,
771
781
  name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
772
782
  attributes: {
773
783
  durationMs: duration,
@@ -784,13 +794,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
784
794
  runId,
785
795
  workflowId,
786
796
  mastra: this.mastra,
787
- runtimeContext,
797
+ requestContext,
788
798
  inputData: prevOutput,
789
799
  state: executionContext.state,
790
800
  setState: (state) => {
791
801
  executionContext.state = state;
792
802
  },
793
- runCount: -1,
794
803
  retryCount: -1,
795
804
  tracingContext: {
796
805
  currentSpan: sleepSpan
@@ -806,7 +815,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
806
815
  abortController?.abort();
807
816
  },
808
817
  [EMITTER_SYMBOL]: emitter,
809
- // TODO: add streamVNext support
810
818
  [STREAM_FORMAT_SYMBOL]: executionContext.format,
811
819
  engine: { step: this.inngestStep },
812
820
  abortSignal: abortController?.signal,
@@ -850,14 +858,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
850
858
  stepResults,
851
859
  emitter,
852
860
  abortController,
853
- runtimeContext,
861
+ requestContext,
854
862
  executionContext,
855
863
  writableStream,
856
864
  tracingContext
857
865
  }) {
858
866
  let { date, fn } = entry;
859
867
  const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
860
- type: AISpanType.WORKFLOW_SLEEP,
868
+ type: SpanType.WORKFLOW_SLEEP,
861
869
  name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
862
870
  attributes: {
863
871
  untilDate: date,
@@ -875,13 +883,12 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
875
883
  runId,
876
884
  workflowId,
877
885
  mastra: this.mastra,
878
- runtimeContext,
886
+ requestContext,
879
887
  inputData: prevOutput,
880
888
  state: executionContext.state,
881
889
  setState: (state) => {
882
890
  executionContext.state = state;
883
891
  },
884
- runCount: -1,
885
892
  retryCount: -1,
886
893
  tracingContext: {
887
894
  currentSpan: sleepUntilSpan
@@ -898,7 +905,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
898
905
  },
899
906
  [EMITTER_SYMBOL]: emitter,
900
907
  [STREAM_FORMAT_SYMBOL]: executionContext.format,
901
- // TODO: add streamVNext support
902
908
  engine: { step: this.inngestStep },
903
909
  abortSignal: abortController?.signal,
904
910
  writer: new ToolStream(
@@ -941,16 +947,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
941
947
  throw e;
942
948
  }
943
949
  }
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
950
  async executeStep({
955
951
  step,
956
952
  stepResults,
@@ -959,14 +955,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
959
955
  prevOutput,
960
956
  emitter,
961
957
  abortController,
962
- runtimeContext,
958
+ requestContext,
963
959
  tracingContext,
964
960
  writableStream,
965
961
  disableScorers
966
962
  }) {
967
- const stepAISpan = tracingContext?.currentSpan?.createChildSpan({
963
+ const stepSpan = tracingContext?.currentSpan?.createChildSpan({
968
964
  name: `workflow step: '${step.id}'`,
969
- type: AISpanType.WORKFLOW_STEP,
965
+ type: SpanType.WORKFLOW_STEP,
970
966
  input: prevOutput,
971
967
  attributes: {
972
968
  stepId: step.id
@@ -983,27 +979,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
983
979
  async () => {
984
980
  const startedAt2 = Date.now();
985
981
  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
982
  type: "workflow-step-start",
1008
983
  payload: {
1009
984
  id: step.id,
@@ -1079,23 +1054,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1079
1054
  async () => {
1080
1055
  if (result.status === "failed") {
1081
1056
  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
1057
  type: "workflow-step-result",
1100
1058
  payload: {
1101
1059
  id: step.id,
@@ -1114,27 +1072,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1114
1072
  const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
1115
1073
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1116
1074
  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
1075
  type: "workflow-step-suspended",
1139
1076
  payload: {
1140
1077
  id: step.id,
@@ -1153,23 +1090,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1153
1090
  }
1154
1091
  };
1155
1092
  }
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
1093
  return {
1174
1094
  executionContext,
1175
1095
  result: {
@@ -1179,23 +1099,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1179
1099
  };
1180
1100
  }
1181
1101
  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
1102
  type: "workflow-step-result",
1200
1103
  payload: {
1201
1104
  id: step.id,
@@ -1203,7 +1106,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1203
1106
  output: result?.result
1204
1107
  }
1205
1108
  });
1206
- await emitter.emit("watch-v2", {
1109
+ await emitter.emit("watch", {
1207
1110
  type: "workflow-step-finish",
1208
1111
  payload: {
1209
1112
  id: step.id,
@@ -1223,6 +1126,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1223
1126
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1224
1127
  };
1225
1128
  }
1129
+ const stepCallId = randomUUID();
1226
1130
  let stepRes;
1227
1131
  try {
1228
1132
  stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
@@ -1236,8 +1140,16 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1236
1140
  const result = await step.execute({
1237
1141
  runId: executionContext.runId,
1238
1142
  mastra: this.mastra,
1239
- runtimeContext,
1240
- writableStream,
1143
+ requestContext,
1144
+ writer: new ToolStream(
1145
+ {
1146
+ prefix: "workflow-step",
1147
+ callId: stepCallId,
1148
+ name: step.id,
1149
+ runId: executionContext.runId
1150
+ },
1151
+ writableStream
1152
+ ),
1241
1153
  state: executionContext?.state ?? {},
1242
1154
  setState: (state) => {
1243
1155
  executionContext.state = state;
@@ -1245,7 +1157,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1245
1157
  inputData,
1246
1158
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
1247
1159
  tracingContext: {
1248
- currentSpan: stepAISpan
1160
+ currentSpan: stepSpan
1249
1161
  },
1250
1162
  getInitData: () => stepResults?.input,
1251
1163
  getStepResult: getStepResult.bind(this, stepResults),
@@ -1272,6 +1184,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1272
1184
  runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
1273
1185
  },
1274
1186
  [EMITTER_SYMBOL]: emitter,
1187
+ [STREAM_FORMAT_SYMBOL]: executionContext.format,
1275
1188
  engine: {
1276
1189
  step: this.inngestStep
1277
1190
  },
@@ -1299,7 +1212,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1299
1212
  };
1300
1213
  execResults = stepFailure;
1301
1214
  const fallbackErrorMessage = `Step ${step.id} failed`;
1302
- stepAISpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1215
+ stepSpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1303
1216
  throw new RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
1304
1217
  cause: execResults
1305
1218
  });
@@ -1308,6 +1221,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1308
1221
  execResults = {
1309
1222
  status: "suspended",
1310
1223
  suspendPayload: suspended.payload,
1224
+ ...execResults.output ? { suspendOutput: execResults.output } : {},
1311
1225
  payload: inputData,
1312
1226
  suspendedAt: Date.now(),
1313
1227
  startedAt,
@@ -1323,24 +1237,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1323
1237
  startedAt
1324
1238
  };
1325
1239
  }
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
1240
  if (execResults.status === "suspended") {
1343
- await emitter.emit("watch-v2", {
1241
+ await emitter.emit("watch", {
1344
1242
  type: "workflow-step-suspended",
1345
1243
  payload: {
1346
1244
  id: step.id,
@@ -1348,14 +1246,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1348
1246
  }
1349
1247
  });
1350
1248
  } else {
1351
- await emitter.emit("watch-v2", {
1249
+ await emitter.emit("watch", {
1352
1250
  type: "workflow-step-result",
1353
1251
  payload: {
1354
1252
  id: step.id,
1355
1253
  ...execResults
1356
1254
  }
1357
1255
  });
1358
- await emitter.emit("watch-v2", {
1256
+ await emitter.emit("watch", {
1359
1257
  type: "workflow-step-finish",
1360
1258
  payload: {
1361
1259
  id: step.id,
@@ -1363,7 +1261,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1363
1261
  }
1364
1262
  });
1365
1263
  }
1366
- stepAISpan?.end({ output: execResults });
1264
+ stepSpan?.end({ output: execResults });
1367
1265
  return { result: execResults, executionContext, stepResults };
1368
1266
  });
1369
1267
  } catch (e) {
@@ -1393,9 +1291,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1393
1291
  output: stepRes.result,
1394
1292
  workflowId: executionContext.workflowId,
1395
1293
  stepId: step.id,
1396
- runtimeContext,
1294
+ requestContext,
1397
1295
  disableScorers,
1398
- tracingContext: { currentSpan: stepAISpan }
1296
+ tracingContext: { currentSpan: stepSpan }
1399
1297
  });
1400
1298
  }
1401
1299
  });
@@ -1429,14 +1327,15 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1429
1327
  resourceId,
1430
1328
  snapshot: {
1431
1329
  runId,
1330
+ status: workflowStatus,
1432
1331
  value: executionContext.state,
1433
1332
  context: stepResults,
1434
- activePaths: [],
1333
+ activePaths: executionContext.executionPath,
1334
+ activeStepsPath: executionContext.activeStepsPath,
1435
1335
  suspendedPaths: executionContext.suspendedPaths,
1436
1336
  resumeLabels: executionContext.resumeLabels,
1437
1337
  waitingPaths: {},
1438
1338
  serializedStepGraph,
1439
- status: workflowStatus,
1440
1339
  result,
1441
1340
  error,
1442
1341
  // @ts-ignore
@@ -1451,20 +1350,18 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1451
1350
  runId,
1452
1351
  entry,
1453
1352
  prevOutput,
1454
- prevStep,
1455
1353
  stepResults,
1456
- serializedStepGraph,
1457
1354
  resume,
1458
1355
  executionContext,
1459
1356
  emitter,
1460
1357
  abortController,
1461
- runtimeContext,
1358
+ requestContext,
1462
1359
  writableStream,
1463
1360
  disableScorers,
1464
1361
  tracingContext
1465
1362
  }) {
1466
1363
  const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
1467
- type: AISpanType.WORKFLOW_CONDITIONAL,
1364
+ type: SpanType.WORKFLOW_CONDITIONAL,
1468
1365
  name: `conditional: '${entry.conditions.length} conditions'`,
1469
1366
  input: prevOutput,
1470
1367
  attributes: {
@@ -1477,7 +1374,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1477
1374
  entry.conditions.map(
1478
1375
  (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
1479
1376
  const evalSpan = conditionalSpan?.createChildSpan({
1480
- type: AISpanType.WORKFLOW_CONDITIONAL_EVAL,
1377
+ type: SpanType.WORKFLOW_CONDITIONAL_EVAL,
1481
1378
  name: `condition: '${index}'`,
1482
1379
  input: prevOutput,
1483
1380
  attributes: {
@@ -1492,8 +1389,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1492
1389
  runId,
1493
1390
  workflowId,
1494
1391
  mastra: this.mastra,
1495
- runtimeContext,
1496
- runCount: -1,
1392
+ requestContext,
1497
1393
  retryCount: -1,
1498
1394
  inputData: prevOutput,
1499
1395
  state: executionContext.state,
@@ -1515,7 +1411,6 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1515
1411
  },
1516
1412
  [EMITTER_SYMBOL]: emitter,
1517
1413
  [STREAM_FORMAT_SYMBOL]: executionContext.format,
1518
- // TODO: add streamVNext support
1519
1414
  engine: {
1520
1415
  step: this.inngestStep
1521
1416
  },
@@ -1564,19 +1459,21 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1564
1459
  }
1565
1460
  });
1566
1461
  const results = await Promise.all(
1567
- stepsToRun.map(
1568
- (step, index) => this.executeEntry({
1569
- workflowId,
1570
- runId,
1571
- entry: step,
1572
- serializedStepGraph,
1573
- prevStep,
1462
+ stepsToRun.map(async (step, index) => {
1463
+ const currStepResult = stepResults[step.step.id];
1464
+ if (currStepResult && currStepResult.status === "success") {
1465
+ return currStepResult;
1466
+ }
1467
+ const result = await this.executeStep({
1468
+ step: step.step,
1469
+ prevOutput,
1574
1470
  stepResults,
1575
1471
  resume,
1576
1472
  executionContext: {
1577
1473
  workflowId,
1578
1474
  runId,
1579
1475
  executionPath: [...executionContext.executionPath, index],
1476
+ activeStepsPath: executionContext.activeStepsPath,
1580
1477
  suspendedPaths: executionContext.suspendedPaths,
1581
1478
  resumeLabels: executionContext.resumeLabels,
1582
1479
  retryConfig: executionContext.retryConfig,
@@ -1584,26 +1481,32 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1584
1481
  },
1585
1482
  emitter,
1586
1483
  abortController,
1587
- runtimeContext,
1484
+ requestContext,
1588
1485
  writableStream,
1589
1486
  disableScorers,
1590
1487
  tracingContext: {
1591
1488
  currentSpan: conditionalSpan
1592
1489
  }
1593
- })
1594
- )
1490
+ });
1491
+ stepResults[step.step.id] = result;
1492
+ return result;
1493
+ })
1595
1494
  );
1596
- const hasFailed = results.find((result) => result.result.status === "failed");
1597
- const hasSuspended = results.find((result) => result.result.status === "suspended");
1495
+ const hasFailed = results.find((result) => result.status === "failed");
1496
+ const hasSuspended = results.find((result) => result.status === "suspended");
1598
1497
  if (hasFailed) {
1599
- execResults = { status: "failed", error: hasFailed.result.error };
1498
+ execResults = { status: "failed", error: hasFailed.error };
1600
1499
  } else if (hasSuspended) {
1601
- execResults = { status: "suspended", suspendPayload: hasSuspended.result.suspendPayload };
1500
+ execResults = {
1501
+ status: "suspended",
1502
+ suspendPayload: hasSuspended.suspendPayload,
1503
+ ...hasSuspended.suspendOutput ? { suspendOutput: hasSuspended.suspendOutput } : {}
1504
+ };
1602
1505
  } else {
1603
1506
  execResults = {
1604
1507
  status: "success",
1605
1508
  output: results.reduce((acc, result, index) => {
1606
- if (result.result.status === "success") {
1509
+ if (result.status === "success") {
1607
1510
  acc[stepsToRun[index].step.id] = result.output;
1608
1511
  }
1609
1512
  return acc;