@mastra/inngest 0.0.0-just-snapshot-20251014192224 → 0.0.0-main-test-20251105183450

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.cjs CHANGED
@@ -1,9 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  var crypto = require('crypto');
4
+ var web = require('stream/web');
4
5
  var realtime = require('@inngest/realtime');
5
- var aiTracing = require('@mastra/core/ai-tracing');
6
6
  var di = require('@mastra/core/di');
7
+ var observability = require('@mastra/core/observability');
8
+ var stream = require('@mastra/core/stream');
7
9
  var tools = require('@mastra/core/tools');
8
10
  var workflows = require('@mastra/core/workflows');
9
11
  var _constants = require('@mastra/core/workflows/_constants');
@@ -18,7 +20,7 @@ function serve({
18
20
  functions: userFunctions = [],
19
21
  registerOptions
20
22
  }) {
21
- const wfs = mastra.getWorkflows();
23
+ const wfs = mastra.listWorkflows();
22
24
  const workflowFunctions = Array.from(
23
25
  new Set(
24
26
  Object.values(wfs).flatMap((wf) => {
@@ -57,11 +59,12 @@ var InngestRun = class extends workflows.Run {
57
59
  }
58
60
  async getRunOutput(eventId) {
59
61
  let runs = await this.getRuns(eventId);
62
+ const storage = this.#mastra?.getStorage();
60
63
  while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
61
64
  await new Promise((resolve) => setTimeout(resolve, 1e3));
62
65
  runs = await this.getRuns(eventId);
63
66
  if (runs?.[0]?.status === "Failed") {
64
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
67
+ const snapshot = await storage?.loadWorkflowSnapshot({
65
68
  workflowName: this.workflowId,
66
69
  runId: this.runId
67
70
  });
@@ -70,7 +73,7 @@ var InngestRun = class extends workflows.Run {
70
73
  };
71
74
  }
72
75
  if (runs?.[0]?.status === "Cancelled") {
73
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
76
+ const snapshot = await storage?.loadWorkflowSnapshot({
74
77
  workflowName: this.workflowId,
75
78
  runId: this.runId
76
79
  });
@@ -86,18 +89,19 @@ var InngestRun = class extends workflows.Run {
86
89
  });
87
90
  }
88
91
  async cancel() {
92
+ const storage = this.#mastra?.getStorage();
89
93
  await this.inngest.send({
90
94
  name: `cancel.workflow.${this.workflowId}`,
91
95
  data: {
92
96
  runId: this.runId
93
97
  }
94
98
  });
95
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
99
+ const snapshot = await storage?.loadWorkflowSnapshot({
96
100
  workflowName: this.workflowId,
97
101
  runId: this.runId
98
102
  });
99
103
  if (snapshot) {
100
- await this.#mastra?.storage?.persistWorkflowSnapshot({
104
+ await storage?.persistWorkflowSnapshot({
101
105
  workflowName: this.workflowId,
102
106
  runId: this.runId,
103
107
  resourceId: this.resourceId,
@@ -108,9 +112,15 @@ var InngestRun = class extends workflows.Run {
108
112
  });
109
113
  }
110
114
  }
111
- async start({
115
+ async start(params) {
116
+ return this._start(params);
117
+ }
118
+ async _start({
112
119
  inputData,
113
- initialState
120
+ initialState,
121
+ outputOptions,
122
+ tracingOptions,
123
+ format
114
124
  }) {
115
125
  await this.#mastra.getStorage()?.persistWorkflowSnapshot({
116
126
  workflowName: this.workflowId,
@@ -123,6 +133,7 @@ var InngestRun = class extends workflows.Run {
123
133
  context: {},
124
134
  activePaths: [],
125
135
  suspendedPaths: {},
136
+ resumeLabels: {},
126
137
  waitingPaths: {},
127
138
  timestamp: Date.now(),
128
139
  status: "running"
@@ -136,7 +147,10 @@ var InngestRun = class extends workflows.Run {
136
147
  inputData: inputDataToUse,
137
148
  initialState: initialStateToUse,
138
149
  runId: this.runId,
139
- resourceId: this.resourceId
150
+ resourceId: this.resourceId,
151
+ outputOptions,
152
+ tracingOptions,
153
+ format
140
154
  }
141
155
  });
142
156
  const eventId = eventOutput.ids[0];
@@ -165,10 +179,11 @@ var InngestRun = class extends workflows.Run {
165
179
  return p;
166
180
  }
167
181
  async _resume(params) {
182
+ const storage = this.#mastra?.getStorage();
168
183
  const steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
169
184
  (step) => typeof step === "string" ? step : step?.id
170
185
  );
171
- const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
186
+ const snapshot = await storage?.loadWorkflowSnapshot({
172
187
  workflowName: this.workflowId,
173
188
  runId: this.runId
174
189
  });
@@ -202,12 +217,12 @@ var InngestRun = class extends workflows.Run {
202
217
  }
203
218
  return result;
204
219
  }
205
- watch(cb, type = "watch") {
220
+ watch(cb) {
206
221
  let active = true;
207
222
  const streamPromise = realtime.subscribe(
208
223
  {
209
224
  channel: `workflow:${this.workflowId}:${this.runId}`,
210
- topics: [type],
225
+ topics: ["watch"],
211
226
  app: this.inngest
212
227
  },
213
228
  (message) => {
@@ -225,20 +240,35 @@ var InngestRun = class extends workflows.Run {
225
240
  });
226
241
  };
227
242
  }
228
- stream({ inputData, runtimeContext } = {}) {
243
+ streamLegacy({ inputData, requestContext } = {}) {
229
244
  const { readable, writable } = new TransformStream();
230
245
  const writer = writable.getWriter();
231
246
  const unwatch = this.watch(async (event) => {
232
247
  try {
248
+ await writer.write({
249
+ // @ts-ignore
250
+ type: "start",
251
+ // @ts-ignore
252
+ payload: { runId: this.runId }
253
+ });
233
254
  const e = {
234
255
  ...event,
235
256
  type: event.type.replace("workflow-", "")
236
257
  };
258
+ if (e.type === "step-output") {
259
+ e.type = e.payload.output.type;
260
+ e.payload = e.payload.output.payload;
261
+ }
237
262
  await writer.write(e);
238
263
  } catch {
239
264
  }
240
- }, "watch-v2");
265
+ });
241
266
  this.closeStreamAction = async () => {
267
+ await writer.write({
268
+ type: "finish",
269
+ // @ts-ignore
270
+ payload: { runId: this.runId }
271
+ });
242
272
  unwatch();
243
273
  try {
244
274
  await writer.close();
@@ -248,7 +278,7 @@ var InngestRun = class extends workflows.Run {
248
278
  writer.releaseLock();
249
279
  }
250
280
  };
251
- this.executionResults = this.start({ inputData, runtimeContext }).then((result) => {
281
+ this.executionResults = this._start({ inputData, requestContext, format: "legacy" }).then((result) => {
252
282
  if (result.status !== "suspended") {
253
283
  this.closeStreamAction?.().catch(() => {
254
284
  });
@@ -260,6 +290,82 @@ var InngestRun = class extends workflows.Run {
260
290
  getWorkflowState: () => this.executionResults
261
291
  };
262
292
  }
293
+ stream({
294
+ inputData,
295
+ requestContext,
296
+ tracingOptions,
297
+ closeOnSuspend = true,
298
+ initialState,
299
+ outputOptions
300
+ } = {}) {
301
+ if (this.closeStreamAction && this.streamOutput) {
302
+ return this.streamOutput;
303
+ }
304
+ this.closeStreamAction = async () => {
305
+ };
306
+ const self = this;
307
+ const stream$1 = new web.ReadableStream({
308
+ async start(controller) {
309
+ const unwatch = self.watch(async ({ type, from = stream.ChunkFrom.WORKFLOW, payload }) => {
310
+ controller.enqueue({
311
+ type,
312
+ runId: self.runId,
313
+ from,
314
+ payload: {
315
+ stepName: payload?.id,
316
+ ...payload
317
+ }
318
+ });
319
+ });
320
+ self.closeStreamAction = async () => {
321
+ unwatch();
322
+ try {
323
+ await controller.close();
324
+ } catch (err) {
325
+ console.error("Error closing stream:", err);
326
+ }
327
+ };
328
+ const executionResultsPromise = self._start({
329
+ inputData,
330
+ requestContext,
331
+ // tracingContext, // We are not able to pass a reference to a span here, what to do?
332
+ initialState,
333
+ tracingOptions,
334
+ outputOptions,
335
+ format: "vnext"
336
+ });
337
+ let executionResults;
338
+ try {
339
+ executionResults = await executionResultsPromise;
340
+ if (closeOnSuspend) {
341
+ self.closeStreamAction?.().catch(() => {
342
+ });
343
+ } else if (executionResults.status !== "suspended") {
344
+ self.closeStreamAction?.().catch(() => {
345
+ });
346
+ }
347
+ if (self.streamOutput) {
348
+ self.streamOutput.updateResults(
349
+ executionResults
350
+ );
351
+ }
352
+ } catch (err) {
353
+ self.streamOutput?.rejectResults(err);
354
+ self.closeStreamAction?.().catch(() => {
355
+ });
356
+ }
357
+ }
358
+ });
359
+ this.streamOutput = new stream.WorkflowRunOutput({
360
+ runId: this.runId,
361
+ workflowId: this.workflowId,
362
+ stream: stream$1
363
+ });
364
+ return this.streamOutput;
365
+ }
366
+ streamVNext(args = {}) {
367
+ return this.stream(args);
368
+ }
263
369
  };
264
370
  var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
265
371
  #mastra;
@@ -276,13 +382,13 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
276
382
  this.#mastra = params.mastra;
277
383
  this.inngest = inngest;
278
384
  }
279
- async getWorkflowRuns(args) {
385
+ async listWorkflowRuns(args) {
280
386
  const storage = this.#mastra?.getStorage();
281
387
  if (!storage) {
282
388
  this.logger.debug("Cannot get workflow runs. Mastra engine is not initialized");
283
389
  return { runs: [], total: 0 };
284
390
  }
285
- return storage.getWorkflowRuns({ workflowName: this.id, ...args ?? {} });
391
+ return storage.listWorkflowRuns({ workflowName: this.id, ...args ?? {} });
286
392
  }
287
393
  async getWorkflowRunById(runId) {
288
394
  const storage = this.#mastra?.getStorage();
@@ -311,16 +417,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
311
417
  }
312
418
  }
313
419
  }
314
- /**
315
- * @deprecated Use createRunAsync() instead.
316
- * @throws {Error} Always throws an error directing users to use createRunAsync()
317
- */
318
- createRun(_options) {
319
- throw new Error(
320
- "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."
321
- );
322
- }
323
- async createRunAsync(options) {
420
+ async createRun(options) {
324
421
  const runIdToUse = options?.runId || crypto.randomUUID();
325
422
  const run = this.runs.get(runIdToUse) ?? new InngestRun(
326
423
  {
@@ -357,6 +454,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
357
454
  waitingPaths: {},
358
455
  serializedStepGraph: this.serializedStepGraph,
359
456
  suspendedPaths: {},
457
+ resumeLabels: {},
360
458
  result: void 0,
361
459
  error: void 0,
362
460
  // @ts-ignore
@@ -381,7 +479,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
381
479
  },
382
480
  { event: `workflow.${this.id}` },
383
481
  async ({ event, step, attempt, publish }) => {
384
- let { inputData, initialState, runId, resourceId, resume, outputOptions } = event.data;
482
+ let { inputData, initialState, runId, resourceId, resume, outputOptions, format } = event.data;
385
483
  if (!runId) {
386
484
  runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
387
485
  return crypto.randomUUID();
@@ -420,13 +518,19 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
420
518
  initialState,
421
519
  emitter,
422
520
  retryConfig: this.retryConfig,
423
- runtimeContext: new di.RuntimeContext(),
521
+ requestContext: new di.RequestContext(),
424
522
  // TODO
425
523
  resume,
524
+ format,
426
525
  abortController: new AbortController(),
427
- currentSpan: void 0,
428
- // TODO: Pass actual parent AI span from workflow execution context
429
- outputOptions
526
+ // currentSpan: undefined, // TODO: Pass actual parent Span from workflow execution context
527
+ outputOptions,
528
+ writableStream: new WritableStream({
529
+ write(chunk) {
530
+ void emitter.emit("watch", chunk).catch(() => {
531
+ });
532
+ }
533
+ })
430
534
  });
431
535
  await step.run(`workflow.${this.id}.finalize`, async () => {
432
536
  if (result.status === "failed") {
@@ -464,7 +568,7 @@ function isAgent(params) {
464
568
  function isTool(params) {
465
569
  return params instanceof tools.Tool;
466
570
  }
467
- function createStep(params) {
571
+ function createStep(params, agentOptions) {
468
572
  if (isAgent(params)) {
469
573
  return {
470
574
  id: params.name,
@@ -472,12 +576,23 @@ function createStep(params) {
472
576
  // @ts-ignore
473
577
  inputSchema: zod.z.object({
474
578
  prompt: zod.z.string()
579
+ // resourceId: z.string().optional(),
580
+ // threadId: z.string().optional(),
475
581
  }),
476
582
  // @ts-ignore
477
583
  outputSchema: zod.z.object({
478
584
  text: zod.z.string()
479
585
  }),
480
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort, tracingContext }) => {
586
+ execute: async ({
587
+ inputData,
588
+ [_constants.EMITTER_SYMBOL]: emitter,
589
+ [_constants.STREAM_FORMAT_SYMBOL]: streamFormat,
590
+ requestContext,
591
+ tracingContext,
592
+ abortSignal,
593
+ abort,
594
+ writer
595
+ }) => {
481
596
  let streamPromise = {};
482
597
  streamPromise.promise = new Promise((resolve, reject) => {
483
598
  streamPromise.resolve = resolve;
@@ -487,61 +602,60 @@ function createStep(params) {
487
602
  name: params.name,
488
603
  args: inputData
489
604
  };
490
- if ((await params.getLLM()).getModel().specificationVersion === `v2`) {
491
- const { fullStream } = await params.stream(inputData.prompt, {
492
- runtimeContext,
605
+ let stream;
606
+ if ((await params.getModel()).specificationVersion === "v1") {
607
+ const { fullStream } = await params.streamLegacy(inputData.prompt, {
608
+ ...agentOptions ?? {},
609
+ // resourceId: inputData.resourceId,
610
+ // threadId: inputData.threadId,
611
+ requestContext,
493
612
  tracingContext,
494
613
  onFinish: (result) => {
495
614
  streamPromise.resolve(result.text);
615
+ void agentOptions?.onFinish?.(result);
496
616
  },
497
617
  abortSignal
498
618
  });
499
- if (abortSignal.aborted) {
500
- return abort();
501
- }
502
- await emitter.emit("watch-v2", {
503
- type: "tool-call-streaming-start",
504
- ...toolData ?? {}
505
- });
506
- for await (const chunk of fullStream) {
507
- if (chunk.type === "text-delta") {
508
- await emitter.emit("watch-v2", {
509
- type: "tool-call-delta",
510
- ...toolData ?? {},
511
- argsTextDelta: chunk.payload.text
512
- });
513
- }
514
- }
619
+ stream = fullStream;
515
620
  } else {
516
- const { fullStream } = await params.streamLegacy(inputData.prompt, {
517
- runtimeContext,
621
+ const modelOutput = await params.stream(inputData.prompt, {
622
+ ...agentOptions ?? {},
623
+ requestContext,
518
624
  tracingContext,
519
625
  onFinish: (result) => {
520
626
  streamPromise.resolve(result.text);
627
+ void agentOptions?.onFinish?.(result);
521
628
  },
522
629
  abortSignal
523
630
  });
524
- if (abortSignal.aborted) {
525
- return abort();
526
- }
527
- await emitter.emit("watch-v2", {
631
+ stream = modelOutput.fullStream;
632
+ }
633
+ if (streamFormat === "legacy") {
634
+ await emitter.emit("watch", {
528
635
  type: "tool-call-streaming-start",
529
636
  ...toolData ?? {}
530
637
  });
531
- for await (const chunk of fullStream) {
638
+ for await (const chunk of stream) {
532
639
  if (chunk.type === "text-delta") {
533
- await emitter.emit("watch-v2", {
640
+ await emitter.emit("watch", {
534
641
  type: "tool-call-delta",
535
642
  ...toolData ?? {},
536
643
  argsTextDelta: chunk.textDelta
537
644
  });
538
645
  }
539
646
  }
647
+ await emitter.emit("watch", {
648
+ type: "tool-call-streaming-finish",
649
+ ...toolData ?? {}
650
+ });
651
+ } else {
652
+ for await (const chunk of stream) {
653
+ await writer.write(chunk);
654
+ }
655
+ }
656
+ if (abortSignal.aborted) {
657
+ return abort();
540
658
  }
541
- await emitter.emit("watch-v2", {
542
- type: "tool-call-streaming-finish",
543
- ...toolData ?? {}
544
- });
545
659
  return {
546
660
  text: await streamPromise.promise
547
661
  };
@@ -560,11 +674,11 @@ function createStep(params) {
560
674
  description: params.description,
561
675
  inputSchema: params.inputSchema,
562
676
  outputSchema: params.outputSchema,
563
- execute: async ({ inputData, mastra, runtimeContext, tracingContext, suspend, resumeData }) => {
677
+ execute: async ({ inputData, mastra, requestContext, tracingContext, suspend, resumeData }) => {
564
678
  return params.execute({
565
679
  context: inputData,
566
- mastra: aiTracing.wrapMastra(mastra, tracingContext),
567
- runtimeContext,
680
+ mastra: observability.wrapMastra(mastra, tracingContext),
681
+ requestContext,
568
682
  tracingContext,
569
683
  suspend,
570
684
  resumeData
@@ -627,63 +741,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
627
741
  this.inngestStep = inngestStep;
628
742
  this.inngestAttempts = inngestAttempts;
629
743
  }
630
- async execute(params) {
631
- await params.emitter.emit("watch-v2", {
632
- type: "workflow-start",
633
- payload: { runId: params.runId }
634
- });
635
- const result = await super.execute(params);
636
- await params.emitter.emit("watch-v2", {
637
- type: "workflow-finish",
638
- payload: { runId: params.runId }
639
- });
640
- return result;
641
- }
642
- async fmtReturnValue(executionSpan, emitter, stepResults, lastOutput, error) {
744
+ async fmtReturnValue(emitter, stepResults, lastOutput, error) {
643
745
  const base = {
644
746
  status: lastOutput.status,
645
747
  steps: stepResults
646
748
  };
647
749
  if (lastOutput.status === "success") {
648
- await emitter.emit("watch", {
649
- type: "watch",
650
- payload: {
651
- workflowState: {
652
- status: lastOutput.status,
653
- steps: stepResults,
654
- result: lastOutput.output
655
- }
656
- },
657
- eventTimestamp: Date.now()
658
- });
659
750
  base.result = lastOutput.output;
660
751
  } else if (lastOutput.status === "failed") {
661
752
  base.error = error instanceof Error ? error?.stack ?? error.message : lastOutput?.error instanceof Error ? lastOutput.error.message : lastOutput.error ?? error ?? "Unknown error";
662
- await emitter.emit("watch", {
663
- type: "watch",
664
- payload: {
665
- workflowState: {
666
- status: lastOutput.status,
667
- steps: stepResults,
668
- result: null,
669
- error: base.error
670
- }
671
- },
672
- eventTimestamp: Date.now()
673
- });
674
753
  } else if (lastOutput.status === "suspended") {
675
- await emitter.emit("watch", {
676
- type: "watch",
677
- payload: {
678
- workflowState: {
679
- status: lastOutput.status,
680
- steps: stepResults,
681
- result: null,
682
- error: null
683
- }
684
- },
685
- eventTimestamp: Date.now()
686
- });
687
754
  const suspendedStepIds = Object.entries(stepResults).flatMap(([stepId, stepResult]) => {
688
755
  if (stepResult?.status === "suspended") {
689
756
  const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
@@ -693,7 +760,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
693
760
  });
694
761
  base.suspended = suspendedStepIds;
695
762
  }
696
- executionSpan?.end();
697
763
  return base;
698
764
  }
699
765
  // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
@@ -707,14 +773,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
707
773
  stepResults,
708
774
  emitter,
709
775
  abortController,
710
- runtimeContext,
776
+ requestContext,
711
777
  executionContext,
712
778
  writableStream,
713
779
  tracingContext
714
780
  }) {
715
781
  let { duration, fn } = entry;
716
782
  const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
717
- type: aiTracing.AISpanType.WORKFLOW_SLEEP,
783
+ type: observability.SpanType.WORKFLOW_SLEEP,
718
784
  name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
719
785
  attributes: {
720
786
  durationMs: duration,
@@ -725,45 +791,53 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
725
791
  if (fn) {
726
792
  const stepCallId = crypto.randomUUID();
727
793
  duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
728
- return await fn({
729
- runId,
730
- workflowId,
731
- mastra: this.mastra,
732
- runtimeContext,
733
- inputData: prevOutput,
734
- state: executionContext.state,
735
- setState: (state) => {
736
- executionContext.state = state;
737
- },
738
- runCount: -1,
739
- tracingContext: {
740
- currentSpan: sleepSpan
741
- },
742
- getInitData: () => stepResults?.input,
743
- getStepResult: workflows.getStepResult.bind(this, stepResults),
744
- // TODO: this function shouldn't have suspend probably?
745
- suspend: async (_suspendPayload) => {
746
- },
747
- bail: () => {
748
- },
749
- abort: () => {
750
- abortController?.abort();
751
- },
752
- [_constants.EMITTER_SYMBOL]: emitter,
753
- // TODO: add streamVNext support
754
- [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
755
- engine: { step: this.inngestStep },
756
- abortSignal: abortController?.signal,
757
- writer: new tools.ToolStream(
794
+ return await fn(
795
+ workflows.createDeprecationProxy(
758
796
  {
759
- prefix: "workflow-step",
760
- callId: stepCallId,
761
- name: "sleep",
762
- runId
797
+ runId,
798
+ workflowId,
799
+ mastra: this.mastra,
800
+ requestContext,
801
+ inputData: prevOutput,
802
+ state: executionContext.state,
803
+ setState: (state) => {
804
+ executionContext.state = state;
805
+ },
806
+ retryCount: -1,
807
+ tracingContext: {
808
+ currentSpan: sleepSpan
809
+ },
810
+ getInitData: () => stepResults?.input,
811
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
812
+ // TODO: this function shouldn't have suspend probably?
813
+ suspend: async (_suspendPayload) => {
814
+ },
815
+ bail: () => {
816
+ },
817
+ abort: () => {
818
+ abortController?.abort();
819
+ },
820
+ [_constants.EMITTER_SYMBOL]: emitter,
821
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
822
+ engine: { step: this.inngestStep },
823
+ abortSignal: abortController?.signal,
824
+ writer: new tools.ToolStream(
825
+ {
826
+ prefix: "workflow-step",
827
+ callId: stepCallId,
828
+ name: "sleep",
829
+ runId
830
+ },
831
+ writableStream
832
+ )
763
833
  },
764
- writableStream
834
+ {
835
+ paramName: "runCount",
836
+ deprecationMessage: workflows.runCountDeprecationMessage,
837
+ logger: this.logger
838
+ }
765
839
  )
766
- });
840
+ );
767
841
  });
768
842
  sleepSpan?.update({
769
843
  attributes: {
@@ -787,14 +861,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
787
861
  stepResults,
788
862
  emitter,
789
863
  abortController,
790
- runtimeContext,
864
+ requestContext,
791
865
  executionContext,
792
866
  writableStream,
793
867
  tracingContext
794
868
  }) {
795
869
  let { date, fn } = entry;
796
870
  const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
797
- type: aiTracing.AISpanType.WORKFLOW_SLEEP,
871
+ type: observability.SpanType.WORKFLOW_SLEEP,
798
872
  name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
799
873
  attributes: {
800
874
  untilDate: date,
@@ -806,45 +880,53 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
806
880
  if (fn) {
807
881
  date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
808
882
  const stepCallId = crypto.randomUUID();
809
- return await fn({
810
- runId,
811
- workflowId,
812
- mastra: this.mastra,
813
- runtimeContext,
814
- inputData: prevOutput,
815
- state: executionContext.state,
816
- setState: (state) => {
817
- executionContext.state = state;
818
- },
819
- runCount: -1,
820
- tracingContext: {
821
- currentSpan: sleepUntilSpan
822
- },
823
- getInitData: () => stepResults?.input,
824
- getStepResult: workflows.getStepResult.bind(this, stepResults),
825
- // TODO: this function shouldn't have suspend probably?
826
- suspend: async (_suspendPayload) => {
827
- },
828
- bail: () => {
829
- },
830
- abort: () => {
831
- abortController?.abort();
832
- },
833
- [_constants.EMITTER_SYMBOL]: emitter,
834
- [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
835
- // TODO: add streamVNext support
836
- engine: { step: this.inngestStep },
837
- abortSignal: abortController?.signal,
838
- writer: new tools.ToolStream(
883
+ return await fn(
884
+ workflows.createDeprecationProxy(
839
885
  {
840
- prefix: "workflow-step",
841
- callId: stepCallId,
842
- name: "sleep",
843
- runId
886
+ runId,
887
+ workflowId,
888
+ mastra: this.mastra,
889
+ requestContext,
890
+ inputData: prevOutput,
891
+ state: executionContext.state,
892
+ setState: (state) => {
893
+ executionContext.state = state;
894
+ },
895
+ retryCount: -1,
896
+ tracingContext: {
897
+ currentSpan: sleepUntilSpan
898
+ },
899
+ getInitData: () => stepResults?.input,
900
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
901
+ // TODO: this function shouldn't have suspend probably?
902
+ suspend: async (_suspendPayload) => {
903
+ },
904
+ bail: () => {
905
+ },
906
+ abort: () => {
907
+ abortController?.abort();
908
+ },
909
+ [_constants.EMITTER_SYMBOL]: emitter,
910
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
911
+ engine: { step: this.inngestStep },
912
+ abortSignal: abortController?.signal,
913
+ writer: new tools.ToolStream(
914
+ {
915
+ prefix: "workflow-step",
916
+ callId: stepCallId,
917
+ name: "sleep",
918
+ runId
919
+ },
920
+ writableStream
921
+ )
844
922
  },
845
- writableStream
923
+ {
924
+ paramName: "runCount",
925
+ deprecationMessage: workflows.runCountDeprecationMessage,
926
+ logger: this.logger
927
+ }
846
928
  )
847
- });
929
+ );
848
930
  });
849
931
  if (date && !(date instanceof Date)) {
850
932
  date = new Date(date);
@@ -886,14 +968,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
886
968
  prevOutput,
887
969
  emitter,
888
970
  abortController,
889
- runtimeContext,
971
+ requestContext,
890
972
  tracingContext,
891
973
  writableStream,
892
974
  disableScorers
893
975
  }) {
894
- const stepAISpan = tracingContext?.currentSpan?.createChildSpan({
976
+ const stepSpan = tracingContext?.currentSpan?.createChildSpan({
895
977
  name: `workflow step: '${step.id}'`,
896
- type: aiTracing.AISpanType.WORKFLOW_STEP,
978
+ type: observability.SpanType.WORKFLOW_STEP,
897
979
  input: prevOutput,
898
980
  attributes: {
899
981
  stepId: step.id
@@ -910,27 +992,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
910
992
  async () => {
911
993
  const startedAt2 = Date.now();
912
994
  await emitter.emit("watch", {
913
- type: "watch",
914
- payload: {
915
- currentStep: {
916
- id: step.id,
917
- status: "running"
918
- },
919
- workflowState: {
920
- status: "running",
921
- steps: {
922
- ...stepResults,
923
- [step.id]: {
924
- status: "running"
925
- }
926
- },
927
- result: null,
928
- error: null
929
- }
930
- },
931
- eventTimestamp: Date.now()
932
- });
933
- await emitter.emit("watch-v2", {
934
995
  type: "workflow-step-start",
935
996
  payload: {
936
997
  id: step.id,
@@ -1006,23 +1067,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1006
1067
  async () => {
1007
1068
  if (result.status === "failed") {
1008
1069
  await emitter.emit("watch", {
1009
- type: "watch",
1010
- payload: {
1011
- currentStep: {
1012
- id: step.id,
1013
- status: "failed",
1014
- error: result?.error
1015
- },
1016
- workflowState: {
1017
- status: "running",
1018
- steps: stepResults,
1019
- result: null,
1020
- error: null
1021
- }
1022
- },
1023
- eventTimestamp: Date.now()
1024
- });
1025
- await emitter.emit("watch-v2", {
1026
1070
  type: "workflow-step-result",
1027
1071
  payload: {
1028
1072
  id: step.id,
@@ -1041,27 +1085,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1041
1085
  const suspendPath = [stepName, ...stepResult?.suspendPayload?.__workflow_meta?.path ?? []];
1042
1086
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1043
1087
  await emitter.emit("watch", {
1044
- type: "watch",
1045
- payload: {
1046
- currentStep: {
1047
- id: step.id,
1048
- status: "suspended",
1049
- payload: stepResult.payload,
1050
- suspendPayload: {
1051
- ...stepResult?.suspendPayload,
1052
- __workflow_meta: { runId, path: suspendPath }
1053
- }
1054
- },
1055
- workflowState: {
1056
- status: "running",
1057
- steps: stepResults,
1058
- result: null,
1059
- error: null
1060
- }
1061
- },
1062
- eventTimestamp: Date.now()
1063
- });
1064
- await emitter.emit("watch-v2", {
1065
1088
  type: "workflow-step-suspended",
1066
1089
  payload: {
1067
1090
  id: step.id,
@@ -1080,23 +1103,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1080
1103
  }
1081
1104
  };
1082
1105
  }
1083
- await emitter.emit("watch", {
1084
- type: "watch",
1085
- payload: {
1086
- currentStep: {
1087
- id: step.id,
1088
- status: "suspended",
1089
- payload: {}
1090
- },
1091
- workflowState: {
1092
- status: "running",
1093
- steps: stepResults,
1094
- result: null,
1095
- error: null
1096
- }
1097
- },
1098
- eventTimestamp: Date.now()
1099
- });
1100
1106
  return {
1101
1107
  executionContext,
1102
1108
  result: {
@@ -1106,23 +1112,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1106
1112
  };
1107
1113
  }
1108
1114
  await emitter.emit("watch", {
1109
- type: "watch",
1110
- payload: {
1111
- currentStep: {
1112
- id: step.id,
1113
- status: "success",
1114
- output: result?.result
1115
- },
1116
- workflowState: {
1117
- status: "running",
1118
- steps: stepResults,
1119
- result: null,
1120
- error: null
1121
- }
1122
- },
1123
- eventTimestamp: Date.now()
1124
- });
1125
- await emitter.emit("watch-v2", {
1126
1115
  type: "workflow-step-result",
1127
1116
  payload: {
1128
1117
  id: step.id,
@@ -1130,7 +1119,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1130
1119
  output: result?.result
1131
1120
  }
1132
1121
  });
1133
- await emitter.emit("watch-v2", {
1122
+ await emitter.emit("watch", {
1134
1123
  type: "workflow-step-finish",
1135
1124
  payload: {
1136
1125
  id: step.id,
@@ -1150,6 +1139,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1150
1139
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
1151
1140
  };
1152
1141
  }
1142
+ const stepCallId = crypto.randomUUID();
1153
1143
  let stepRes;
1154
1144
  try {
1155
1145
  stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
@@ -1163,8 +1153,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1163
1153
  const result = await step.execute({
1164
1154
  runId: executionContext.runId,
1165
1155
  mastra: this.mastra,
1166
- runtimeContext,
1167
- writableStream,
1156
+ requestContext,
1157
+ writer: new tools.ToolStream(
1158
+ {
1159
+ prefix: "workflow-step",
1160
+ callId: stepCallId,
1161
+ name: step.id,
1162
+ runId: executionContext.runId
1163
+ },
1164
+ writableStream
1165
+ ),
1168
1166
  state: executionContext?.state ?? {},
1169
1167
  setState: (state) => {
1170
1168
  executionContext.state = state;
@@ -1172,12 +1170,21 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1172
1170
  inputData,
1173
1171
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
1174
1172
  tracingContext: {
1175
- currentSpan: stepAISpan
1173
+ currentSpan: stepSpan
1176
1174
  },
1177
1175
  getInitData: () => stepResults?.input,
1178
1176
  getStepResult: workflows.getStepResult.bind(this, stepResults),
1179
- suspend: async (suspendPayload) => {
1177
+ suspend: async (suspendPayload, suspendOptions) => {
1180
1178
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
1179
+ if (suspendOptions?.resumeLabel) {
1180
+ const resumeLabel = Array.isArray(suspendOptions.resumeLabel) ? suspendOptions.resumeLabel : [suspendOptions.resumeLabel];
1181
+ for (const label of resumeLabel) {
1182
+ executionContext.resumeLabels[label] = {
1183
+ stepId: step.id,
1184
+ foreachIndex: executionContext.foreachIndex
1185
+ };
1186
+ }
1187
+ }
1181
1188
  suspended = { payload: suspendPayload };
1182
1189
  },
1183
1190
  bail: (result2) => {
@@ -1190,6 +1197,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1190
1197
  runId: stepResults[step.id]?.suspendPayload?.__workflow_meta?.runId
1191
1198
  },
1192
1199
  [_constants.EMITTER_SYMBOL]: emitter,
1200
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
1193
1201
  engine: {
1194
1202
  step: this.inngestStep
1195
1203
  },
@@ -1217,7 +1225,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1217
1225
  };
1218
1226
  execResults = stepFailure;
1219
1227
  const fallbackErrorMessage = `Step ${step.id} failed`;
1220
- stepAISpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1228
+ stepSpan?.error({ error: new Error(execResults.error ?? fallbackErrorMessage) });
1221
1229
  throw new inngest.RetryAfterError(execResults.error ?? fallbackErrorMessage, executionContext.retryConfig.delay, {
1222
1230
  cause: execResults
1223
1231
  });
@@ -1241,24 +1249,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1241
1249
  startedAt
1242
1250
  };
1243
1251
  }
1244
- await emitter.emit("watch", {
1245
- type: "watch",
1246
- payload: {
1247
- currentStep: {
1248
- id: step.id,
1249
- ...execResults
1250
- },
1251
- workflowState: {
1252
- status: "running",
1253
- steps: { ...stepResults, [step.id]: execResults },
1254
- result: null,
1255
- error: null
1256
- }
1257
- },
1258
- eventTimestamp: Date.now()
1259
- });
1260
1252
  if (execResults.status === "suspended") {
1261
- await emitter.emit("watch-v2", {
1253
+ await emitter.emit("watch", {
1262
1254
  type: "workflow-step-suspended",
1263
1255
  payload: {
1264
1256
  id: step.id,
@@ -1266,14 +1258,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1266
1258
  }
1267
1259
  });
1268
1260
  } else {
1269
- await emitter.emit("watch-v2", {
1261
+ await emitter.emit("watch", {
1270
1262
  type: "workflow-step-result",
1271
1263
  payload: {
1272
1264
  id: step.id,
1273
1265
  ...execResults
1274
1266
  }
1275
1267
  });
1276
- await emitter.emit("watch-v2", {
1268
+ await emitter.emit("watch", {
1277
1269
  type: "workflow-step-finish",
1278
1270
  payload: {
1279
1271
  id: step.id,
@@ -1281,7 +1273,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1281
1273
  }
1282
1274
  });
1283
1275
  }
1284
- stepAISpan?.end({ output: execResults });
1276
+ stepSpan?.end({ output: execResults });
1285
1277
  return { result: execResults, executionContext, stepResults };
1286
1278
  });
1287
1279
  } catch (e) {
@@ -1311,9 +1303,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1311
1303
  output: stepRes.result,
1312
1304
  workflowId: executionContext.workflowId,
1313
1305
  stepId: step.id,
1314
- runtimeContext,
1306
+ requestContext,
1315
1307
  disableScorers,
1316
- tracingContext: { currentSpan: stepAISpan }
1308
+ tracingContext: { currentSpan: stepSpan }
1317
1309
  });
1318
1310
  }
1319
1311
  });
@@ -1351,6 +1343,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1351
1343
  context: stepResults,
1352
1344
  activePaths: [],
1353
1345
  suspendedPaths: executionContext.suspendedPaths,
1346
+ resumeLabels: executionContext.resumeLabels,
1354
1347
  waitingPaths: {},
1355
1348
  serializedStepGraph,
1356
1349
  status: workflowStatus,
@@ -1368,20 +1361,18 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1368
1361
  runId,
1369
1362
  entry,
1370
1363
  prevOutput,
1371
- prevStep,
1372
1364
  stepResults,
1373
- serializedStepGraph,
1374
1365
  resume,
1375
1366
  executionContext,
1376
1367
  emitter,
1377
1368
  abortController,
1378
- runtimeContext,
1369
+ requestContext,
1379
1370
  writableStream,
1380
1371
  disableScorers,
1381
1372
  tracingContext
1382
1373
  }) {
1383
1374
  const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
1384
- type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL,
1375
+ type: observability.SpanType.WORKFLOW_CONDITIONAL,
1385
1376
  name: `conditional: '${entry.conditions.length} conditions'`,
1386
1377
  input: prevOutput,
1387
1378
  attributes: {
@@ -1394,7 +1385,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1394
1385
  entry.conditions.map(
1395
1386
  (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
1396
1387
  const evalSpan = conditionalSpan?.createChildSpan({
1397
- type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL_EVAL,
1388
+ type: observability.SpanType.WORKFLOW_CONDITIONAL_EVAL,
1398
1389
  name: `condition: '${index}'`,
1399
1390
  input: prevOutput,
1400
1391
  attributes: {
@@ -1403,47 +1394,55 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1403
1394
  tracingPolicy: this.options?.tracingPolicy
1404
1395
  });
1405
1396
  try {
1406
- const result = await cond({
1407
- runId,
1408
- workflowId,
1409
- mastra: this.mastra,
1410
- runtimeContext,
1411
- runCount: -1,
1412
- inputData: prevOutput,
1413
- state: executionContext.state,
1414
- setState: (state) => {
1415
- executionContext.state = state;
1416
- },
1417
- tracingContext: {
1418
- currentSpan: evalSpan
1419
- },
1420
- getInitData: () => stepResults?.input,
1421
- getStepResult: workflows.getStepResult.bind(this, stepResults),
1422
- // TODO: this function shouldn't have suspend probably?
1423
- suspend: async (_suspendPayload) => {
1424
- },
1425
- bail: () => {
1426
- },
1427
- abort: () => {
1428
- abortController.abort();
1429
- },
1430
- [_constants.EMITTER_SYMBOL]: emitter,
1431
- [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
1432
- // TODO: add streamVNext support
1433
- engine: {
1434
- step: this.inngestStep
1435
- },
1436
- abortSignal: abortController.signal,
1437
- writer: new tools.ToolStream(
1397
+ const result = await cond(
1398
+ workflows.createDeprecationProxy(
1438
1399
  {
1439
- prefix: "workflow-step",
1440
- callId: crypto.randomUUID(),
1441
- name: "conditional",
1442
- runId
1400
+ runId,
1401
+ workflowId,
1402
+ mastra: this.mastra,
1403
+ requestContext,
1404
+ retryCount: -1,
1405
+ inputData: prevOutput,
1406
+ state: executionContext.state,
1407
+ setState: (state) => {
1408
+ executionContext.state = state;
1409
+ },
1410
+ tracingContext: {
1411
+ currentSpan: evalSpan
1412
+ },
1413
+ getInitData: () => stepResults?.input,
1414
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
1415
+ // TODO: this function shouldn't have suspend probably?
1416
+ suspend: async (_suspendPayload) => {
1417
+ },
1418
+ bail: () => {
1419
+ },
1420
+ abort: () => {
1421
+ abortController.abort();
1422
+ },
1423
+ [_constants.EMITTER_SYMBOL]: emitter,
1424
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
1425
+ engine: {
1426
+ step: this.inngestStep
1427
+ },
1428
+ abortSignal: abortController.signal,
1429
+ writer: new tools.ToolStream(
1430
+ {
1431
+ prefix: "workflow-step",
1432
+ callId: crypto.randomUUID(),
1433
+ name: "conditional",
1434
+ runId
1435
+ },
1436
+ writableStream
1437
+ )
1443
1438
  },
1444
- writableStream
1439
+ {
1440
+ paramName: "runCount",
1441
+ deprecationMessage: workflows.runCountDeprecationMessage,
1442
+ logger: this.logger
1443
+ }
1445
1444
  )
1446
- });
1445
+ );
1447
1446
  evalSpan?.end({
1448
1447
  output: result,
1449
1448
  attributes: {
@@ -1471,13 +1470,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1471
1470
  }
1472
1471
  });
1473
1472
  const results = await Promise.all(
1474
- stepsToRun.map(
1475
- (step, index) => this.executeEntry({
1476
- workflowId,
1477
- runId,
1478
- entry: step,
1479
- serializedStepGraph,
1480
- prevStep,
1473
+ stepsToRun.map(async (step, index) => {
1474
+ const currStepResult = stepResults[step.step.id];
1475
+ if (currStepResult && currStepResult.status === "success") {
1476
+ return currStepResult;
1477
+ }
1478
+ const result = await this.executeStep({
1479
+ step: step.step,
1480
+ prevOutput,
1481
1481
  stepResults,
1482
1482
  resume,
1483
1483
  executionContext: {
@@ -1485,32 +1485,34 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1485
1485
  runId,
1486
1486
  executionPath: [...executionContext.executionPath, index],
1487
1487
  suspendedPaths: executionContext.suspendedPaths,
1488
+ resumeLabels: executionContext.resumeLabels,
1488
1489
  retryConfig: executionContext.retryConfig,
1489
- executionSpan: executionContext.executionSpan,
1490
1490
  state: executionContext.state
1491
1491
  },
1492
1492
  emitter,
1493
1493
  abortController,
1494
- runtimeContext,
1494
+ requestContext,
1495
1495
  writableStream,
1496
1496
  disableScorers,
1497
1497
  tracingContext: {
1498
1498
  currentSpan: conditionalSpan
1499
1499
  }
1500
- })
1501
- )
1500
+ });
1501
+ stepResults[step.step.id] = result;
1502
+ return result;
1503
+ })
1502
1504
  );
1503
- const hasFailed = results.find((result) => result.result.status === "failed");
1504
- const hasSuspended = results.find((result) => result.result.status === "suspended");
1505
+ const hasFailed = results.find((result) => result.status === "failed");
1506
+ const hasSuspended = results.find((result) => result.status === "suspended");
1505
1507
  if (hasFailed) {
1506
- execResults = { status: "failed", error: hasFailed.result.error };
1508
+ execResults = { status: "failed", error: hasFailed.error };
1507
1509
  } else if (hasSuspended) {
1508
- execResults = { status: "suspended", suspendPayload: hasSuspended.result.suspendPayload };
1510
+ execResults = { status: "suspended", suspendPayload: hasSuspended.suspendPayload };
1509
1511
  } else {
1510
1512
  execResults = {
1511
1513
  status: "success",
1512
1514
  output: results.reduce((acc, result, index) => {
1513
- if (result.result.status === "success") {
1515
+ if (result.status === "success") {
1514
1516
  acc[stepsToRun[index].step.id] = result.output;
1515
1517
  }
1516
1518
  return acc;