@mastra/inngest 0.0.0-support-d1-client-20250701191943 → 0.0.0-suspendRuntimeContextTypeFix-20250930142630

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
@@ -2,6 +2,7 @@
2
2
 
3
3
  var crypto = require('crypto');
4
4
  var realtime = require('@inngest/realtime');
5
+ var aiTracing = require('@mastra/core/ai-tracing');
5
6
  var di = require('@mastra/core/di');
6
7
  var tools = require('@mastra/core/tools');
7
8
  var workflows = require('@mastra/core/workflows');
@@ -10,9 +11,14 @@ var hono = require('inngest/hono');
10
11
  var zod = require('zod');
11
12
 
12
13
  // src/index.ts
13
- function serve({ mastra, inngest }) {
14
+ function serve({
15
+ mastra,
16
+ inngest,
17
+ functions: userFunctions = [],
18
+ registerOptions
19
+ }) {
14
20
  const wfs = mastra.getWorkflows();
15
- const functions = Array.from(
21
+ const workflowFunctions = Array.from(
16
22
  new Set(
17
23
  Object.values(wfs).flatMap((wf) => {
18
24
  if (wf instanceof InngestWorkflow) {
@@ -24,8 +30,9 @@ function serve({ mastra, inngest }) {
24
30
  )
25
31
  );
26
32
  return hono.serve({
33
+ ...registerOptions,
27
34
  client: inngest,
28
- functions
35
+ functions: [...workflowFunctions, ...userFunctions]
29
36
  });
30
37
  }
31
38
  var InngestRun = class extends workflows.Run {
@@ -85,6 +92,7 @@ var InngestRun = class extends workflows.Run {
85
92
  await this.#mastra?.storage?.persistWorkflowSnapshot({
86
93
  workflowName: this.workflowId,
87
94
  runId: this.runId,
95
+ resourceId: this.resourceId,
88
96
  snapshot: {
89
97
  ...snapshot,
90
98
  status: "canceled"
@@ -98,6 +106,7 @@ var InngestRun = class extends workflows.Run {
98
106
  await this.#mastra.getStorage()?.persistWorkflowSnapshot({
99
107
  workflowName: this.workflowId,
100
108
  runId: this.runId,
109
+ resourceId: this.resourceId,
101
110
  snapshot: {
102
111
  runId: this.runId,
103
112
  serializedStepGraph: this.serializedStepGraph,
@@ -105,15 +114,18 @@ var InngestRun = class extends workflows.Run {
105
114
  context: {},
106
115
  activePaths: [],
107
116
  suspendedPaths: {},
117
+ waitingPaths: {},
108
118
  timestamp: Date.now(),
109
119
  status: "running"
110
120
  }
111
121
  });
122
+ const inputDataToUse = await this._validateInput(inputData);
112
123
  const eventOutput = await this.inngest.send({
113
124
  name: `workflow.${this.workflowId}`,
114
125
  data: {
115
- inputData,
116
- runId: this.runId
126
+ inputData: inputDataToUse,
127
+ runId: this.runId,
128
+ resourceId: this.resourceId
117
129
  }
118
130
  });
119
131
  const eventId = eventOutput.ids[0];
@@ -149,16 +161,19 @@ var InngestRun = class extends workflows.Run {
149
161
  workflowName: this.workflowId,
150
162
  runId: this.runId
151
163
  });
164
+ const suspendedStep = this.workflowSteps[steps?.[0] ?? ""];
165
+ const resumeDataToUse = await this._validateResumeData(params.resumeData, suspendedStep);
152
166
  const eventOutput = await this.inngest.send({
153
167
  name: `workflow.${this.workflowId}`,
154
168
  data: {
155
- inputData: params.resumeData,
169
+ inputData: resumeDataToUse,
156
170
  runId: this.runId,
171
+ workflowId: this.workflowId,
157
172
  stepResults: snapshot?.context,
158
173
  resume: {
159
174
  steps,
160
175
  stepResults: snapshot?.context,
161
- resumePayload: params.resumeData,
176
+ resumePayload: resumeDataToUse,
162
177
  // @ts-ignore
163
178
  resumePath: snapshot?.suspendedPaths?.[steps?.[0]]
164
179
  }
@@ -203,7 +218,11 @@ var InngestRun = class extends workflows.Run {
203
218
  const writer = writable.getWriter();
204
219
  const unwatch = this.watch(async (event) => {
205
220
  try {
206
- await writer.write(event);
221
+ const e = {
222
+ ...event,
223
+ type: event.type.replace("workflow-", "")
224
+ };
225
+ await writer.write(e);
207
226
  } catch {
208
227
  }
209
228
  }, "watch-v2");
@@ -234,8 +253,14 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
234
253
  #mastra;
235
254
  inngest;
236
255
  function;
256
+ flowControlConfig;
237
257
  constructor(params, inngest) {
238
- super(params);
258
+ const { concurrency, rateLimit, throttle, debounce, priority, ...workflowParams } = params;
259
+ super(workflowParams);
260
+ const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
261
+ ([_, value]) => value !== void 0
262
+ );
263
+ this.flowControlConfig = flowControlEntries.length > 0 ? Object.fromEntries(flowControlEntries) : void 0;
239
264
  this.#mastra = params.mastra;
240
265
  this.inngest = inngest;
241
266
  }
@@ -256,27 +281,6 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
256
281
  const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
257
282
  return run ?? (this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null);
258
283
  }
259
- async getWorkflowRunExecutionResult(runId) {
260
- const storage = this.#mastra?.getStorage();
261
- if (!storage) {
262
- this.logger.debug("Cannot get workflow run execution result. Mastra storage is not initialized");
263
- return null;
264
- }
265
- const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
266
- if (!run?.snapshot) {
267
- return null;
268
- }
269
- if (typeof run.snapshot === "string") {
270
- return null;
271
- }
272
- return {
273
- status: run.snapshot.status,
274
- result: run.snapshot.result,
275
- error: run.snapshot.error,
276
- payload: run.snapshot.context?.input,
277
- steps: run.snapshot.context
278
- };
279
- }
280
284
  __registerMastra(mastra) {
281
285
  this.#mastra = mastra;
282
286
  this.executionEngine.__registerMastra(mastra);
@@ -295,23 +299,14 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
295
299
  }
296
300
  }
297
301
  }
298
- createRun(options) {
299
- const runIdToUse = options?.runId || crypto.randomUUID();
300
- const run = this.runs.get(runIdToUse) ?? new InngestRun(
301
- {
302
- workflowId: this.id,
303
- runId: runIdToUse,
304
- executionEngine: this.executionEngine,
305
- executionGraph: this.executionGraph,
306
- serializedStepGraph: this.serializedStepGraph,
307
- mastra: this.#mastra,
308
- retryConfig: this.retryConfig,
309
- cleanup: () => this.runs.delete(runIdToUse)
310
- },
311
- this.inngest
302
+ /**
303
+ * @deprecated Use createRunAsync() instead.
304
+ * @throws {Error} Always throws an error directing users to use createRunAsync()
305
+ */
306
+ createRun(_options) {
307
+ throw new Error(
308
+ "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."
312
309
  );
313
- this.runs.set(runIdToUse, run);
314
- return run;
315
310
  }
316
311
  async createRunAsync(options) {
317
312
  const runIdToUse = options?.runId || crypto.randomUUID();
@@ -319,27 +314,31 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
319
314
  {
320
315
  workflowId: this.id,
321
316
  runId: runIdToUse,
317
+ resourceId: options?.resourceId,
322
318
  executionEngine: this.executionEngine,
323
319
  executionGraph: this.executionGraph,
324
320
  serializedStepGraph: this.serializedStepGraph,
325
321
  mastra: this.#mastra,
326
322
  retryConfig: this.retryConfig,
327
- cleanup: () => this.runs.delete(runIdToUse)
323
+ cleanup: () => this.runs.delete(runIdToUse),
324
+ workflowSteps: this.steps
328
325
  },
329
326
  this.inngest
330
327
  );
331
328
  this.runs.set(runIdToUse, run);
332
- const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
329
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse, false);
333
330
  if (!workflowSnapshotInStorage) {
334
331
  await this.mastra?.getStorage()?.persistWorkflowSnapshot({
335
332
  workflowName: this.id,
336
333
  runId: runIdToUse,
334
+ resourceId: options?.resourceId,
337
335
  snapshot: {
338
336
  runId: runIdToUse,
339
337
  status: "pending",
340
338
  value: {},
341
339
  context: {},
342
340
  activePaths: [],
341
+ waitingPaths: {},
343
342
  serializedStepGraph: this.serializedStepGraph,
344
343
  suspendedPaths: {},
345
344
  result: void 0,
@@ -360,11 +359,13 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
360
359
  id: `workflow.${this.id}`,
361
360
  // @ts-ignore
362
361
  retries: this.retryConfig?.attempts ?? 0,
363
- cancelOn: [{ event: `cancel.workflow.${this.id}` }]
362
+ cancelOn: [{ event: `cancel.workflow.${this.id}` }],
363
+ // Spread flow control configuration
364
+ ...this.flowControlConfig
364
365
  },
365
366
  { event: `workflow.${this.id}` },
366
367
  async ({ event, step, attempt, publish }) => {
367
- let { inputData, runId, resume } = event.data;
368
+ let { inputData, runId, resourceId, resume } = event.data;
368
369
  if (!runId) {
369
370
  runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
370
371
  return crypto.randomUUID();
@@ -396,6 +397,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
396
397
  const result = await engine.execute({
397
398
  workflowId: this.id,
398
399
  runId,
400
+ resourceId,
399
401
  graph: this.executionGraph,
400
402
  serializedStepGraph: this.serializedStepGraph,
401
403
  input: inputData,
@@ -404,7 +406,9 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
404
406
  runtimeContext: new di.RuntimeContext(),
405
407
  // TODO
406
408
  resume,
407
- abortController: new AbortController()
409
+ abortController: new AbortController(),
410
+ currentSpan: void 0
411
+ // TODO: Pass actual parent AI span from workflow execution context
408
412
  });
409
413
  return { result, runId };
410
414
  }
@@ -438,17 +442,16 @@ function createStep(params) {
438
442
  if (isAgent(params)) {
439
443
  return {
440
444
  id: params.name,
445
+ description: params.getDescription(),
441
446
  // @ts-ignore
442
447
  inputSchema: zod.z.object({
443
448
  prompt: zod.z.string()
444
- // resourceId: z.string().optional(),
445
- // threadId: z.string().optional(),
446
449
  }),
447
450
  // @ts-ignore
448
451
  outputSchema: zod.z.object({
449
452
  text: zod.z.string()
450
453
  }),
451
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
454
+ execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort, tracingContext }) => {
452
455
  let streamPromise = {};
453
456
  streamPromise.promise = new Promise((resolve, reject) => {
454
457
  streamPromise.resolve = resolve;
@@ -458,14 +461,9 @@ function createStep(params) {
458
461
  name: params.name,
459
462
  args: inputData
460
463
  };
461
- await emitter.emit("watch-v2", {
462
- type: "tool-call-streaming-start",
463
- ...toolData
464
- });
465
464
  const { fullStream } = await params.stream(inputData.prompt, {
466
- // resourceId: inputData.resourceId,
467
- // threadId: inputData.threadId,
468
465
  runtimeContext,
466
+ tracingContext,
469
467
  onFinish: (result) => {
470
468
  streamPromise.resolve(result.text);
471
469
  },
@@ -474,34 +472,28 @@ function createStep(params) {
474
472
  if (abortSignal.aborted) {
475
473
  return abort();
476
474
  }
475
+ await emitter.emit("watch-v2", {
476
+ type: "tool-call-streaming-start",
477
+ ...toolData ?? {}
478
+ });
477
479
  for await (const chunk of fullStream) {
478
- switch (chunk.type) {
479
- case "text-delta":
480
- await emitter.emit("watch-v2", {
481
- type: "tool-call-delta",
482
- ...toolData,
483
- argsTextDelta: chunk.textDelta
484
- });
485
- break;
486
- case "step-start":
487
- case "step-finish":
488
- case "finish":
489
- break;
490
- case "tool-call":
491
- case "tool-result":
492
- case "tool-call-streaming-start":
493
- case "tool-call-delta":
494
- case "source":
495
- case "file":
496
- default:
497
- await emitter.emit("watch-v2", chunk);
498
- break;
480
+ if (chunk.type === "text-delta") {
481
+ await emitter.emit("watch-v2", {
482
+ type: "tool-call-delta",
483
+ ...toolData ?? {},
484
+ argsTextDelta: chunk.textDelta
485
+ });
499
486
  }
500
487
  }
488
+ await emitter.emit("watch-v2", {
489
+ type: "tool-call-streaming-finish",
490
+ ...toolData ?? {}
491
+ });
501
492
  return {
502
493
  text: await streamPromise.promise
503
494
  };
504
- }
495
+ },
496
+ component: params.component
505
497
  };
506
498
  }
507
499
  if (isTool(params)) {
@@ -512,15 +504,20 @@ function createStep(params) {
512
504
  // TODO: tool probably should have strong id type
513
505
  // @ts-ignore
514
506
  id: params.id,
507
+ description: params.description,
515
508
  inputSchema: params.inputSchema,
516
509
  outputSchema: params.outputSchema,
517
- execute: async ({ inputData, mastra, runtimeContext }) => {
510
+ execute: async ({ inputData, mastra, runtimeContext, tracingContext, suspend, resumeData }) => {
518
511
  return params.execute({
519
512
  context: inputData,
520
- mastra,
521
- runtimeContext
513
+ mastra: aiTracing.wrapMastra(mastra, tracingContext),
514
+ runtimeContext,
515
+ tracingContext,
516
+ suspend,
517
+ resumeData
522
518
  });
523
- }
519
+ },
520
+ component: "TOOL"
524
521
  };
525
522
  }
526
523
  return {
@@ -545,7 +542,8 @@ function init(inngest) {
545
542
  description: step.description,
546
543
  inputSchema: step.inputSchema,
547
544
  outputSchema: step.outputSchema,
548
- execute: step.execute
545
+ execute: step.execute,
546
+ component: step.component
549
547
  };
550
548
  },
551
549
  cloneWorkflow(workflow, opts) {
@@ -565,19 +563,19 @@ function init(inngest) {
565
563
  var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
566
564
  inngestStep;
567
565
  inngestAttempts;
568
- constructor(mastra, inngestStep, inngestAttempts = 0) {
569
- super({ mastra });
566
+ constructor(mastra, inngestStep, inngestAttempts = 0, options) {
567
+ super({ mastra, options });
570
568
  this.inngestStep = inngestStep;
571
569
  this.inngestAttempts = inngestAttempts;
572
570
  }
573
571
  async execute(params) {
574
572
  await params.emitter.emit("watch-v2", {
575
- type: "start",
573
+ type: "workflow-start",
576
574
  payload: { runId: params.runId }
577
575
  });
578
576
  const result = await super.execute(params);
579
577
  await params.emitter.emit("watch-v2", {
580
- type: "finish",
578
+ type: "workflow-finish",
581
579
  payload: { runId: params.runId }
582
580
  });
583
581
  return result;
@@ -639,33 +637,169 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
639
637
  executionSpan?.end();
640
638
  return base;
641
639
  }
642
- async superExecuteStep({
640
+ // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
641
+ // await this.inngestStep.sleep(id, duration);
642
+ // }
643
+ async executeSleep({
643
644
  workflowId,
644
645
  runId,
645
- step,
646
+ entry,
647
+ prevOutput,
646
648
  stepResults,
649
+ emitter,
650
+ abortController,
651
+ runtimeContext,
647
652
  executionContext,
648
- resume,
653
+ writableStream,
654
+ tracingContext
655
+ }) {
656
+ let { duration, fn } = entry;
657
+ const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
658
+ type: aiTracing.AISpanType.WORKFLOW_SLEEP,
659
+ name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
660
+ attributes: {
661
+ durationMs: duration,
662
+ sleepType: fn ? "dynamic" : "fixed"
663
+ },
664
+ tracingPolicy: this.options?.tracingPolicy
665
+ });
666
+ if (fn) {
667
+ const stepCallId = crypto.randomUUID();
668
+ duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
669
+ return await fn({
670
+ runId,
671
+ workflowId,
672
+ mastra: this.mastra,
673
+ runtimeContext,
674
+ inputData: prevOutput,
675
+ runCount: -1,
676
+ tracingContext: {
677
+ currentSpan: sleepSpan
678
+ },
679
+ getInitData: () => stepResults?.input,
680
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
681
+ // TODO: this function shouldn't have suspend probably?
682
+ suspend: async (_suspendPayload) => {
683
+ },
684
+ bail: () => {
685
+ },
686
+ abort: () => {
687
+ abortController?.abort();
688
+ },
689
+ [_constants.EMITTER_SYMBOL]: emitter,
690
+ // TODO: add streamVNext support
691
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
692
+ engine: { step: this.inngestStep },
693
+ abortSignal: abortController?.signal,
694
+ writer: new tools.ToolStream(
695
+ {
696
+ prefix: "workflow-step",
697
+ callId: stepCallId,
698
+ name: "sleep",
699
+ runId
700
+ },
701
+ writableStream
702
+ )
703
+ });
704
+ });
705
+ sleepSpan?.update({
706
+ attributes: {
707
+ durationMs: duration
708
+ }
709
+ });
710
+ }
711
+ try {
712
+ await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
713
+ sleepSpan?.end();
714
+ } catch (e) {
715
+ sleepSpan?.error({ error: e });
716
+ throw e;
717
+ }
718
+ }
719
+ async executeSleepUntil({
720
+ workflowId,
721
+ runId,
722
+ entry,
649
723
  prevOutput,
724
+ stepResults,
650
725
  emitter,
651
726
  abortController,
652
- runtimeContext
727
+ runtimeContext,
728
+ executionContext,
729
+ writableStream,
730
+ tracingContext
653
731
  }) {
654
- return super.executeStep({
655
- workflowId,
656
- runId,
657
- step,
658
- stepResults,
659
- executionContext,
660
- resume,
661
- prevOutput,
662
- emitter,
663
- abortController,
664
- runtimeContext
732
+ let { date, fn } = entry;
733
+ const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
734
+ type: aiTracing.AISpanType.WORKFLOW_SLEEP,
735
+ name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
736
+ attributes: {
737
+ untilDate: date,
738
+ durationMs: date ? Math.max(0, date.getTime() - Date.now()) : void 0,
739
+ sleepType: fn ? "dynamic" : "fixed"
740
+ },
741
+ tracingPolicy: this.options?.tracingPolicy
665
742
  });
666
- }
667
- async executeSleep({ id, duration }) {
668
- await this.inngestStep.sleep(id, duration);
743
+ if (fn) {
744
+ date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
745
+ const stepCallId = crypto.randomUUID();
746
+ return await fn({
747
+ runId,
748
+ workflowId,
749
+ mastra: this.mastra,
750
+ runtimeContext,
751
+ inputData: prevOutput,
752
+ runCount: -1,
753
+ tracingContext: {
754
+ currentSpan: sleepUntilSpan
755
+ },
756
+ getInitData: () => stepResults?.input,
757
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
758
+ // TODO: this function shouldn't have suspend probably?
759
+ suspend: async (_suspendPayload) => {
760
+ },
761
+ bail: () => {
762
+ },
763
+ abort: () => {
764
+ abortController?.abort();
765
+ },
766
+ [_constants.EMITTER_SYMBOL]: emitter,
767
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
768
+ // TODO: add streamVNext support
769
+ engine: { step: this.inngestStep },
770
+ abortSignal: abortController?.signal,
771
+ writer: new tools.ToolStream(
772
+ {
773
+ prefix: "workflow-step",
774
+ callId: stepCallId,
775
+ name: "sleep",
776
+ runId
777
+ },
778
+ writableStream
779
+ )
780
+ });
781
+ });
782
+ if (date && !(date instanceof Date)) {
783
+ date = new Date(date);
784
+ }
785
+ const time = !date ? 0 : date.getTime() - Date.now();
786
+ sleepUntilSpan?.update({
787
+ attributes: {
788
+ durationMs: Math.max(0, time)
789
+ }
790
+ });
791
+ }
792
+ if (!(date instanceof Date)) {
793
+ sleepUntilSpan?.end();
794
+ return;
795
+ }
796
+ try {
797
+ await this.inngestStep.sleepUntil(entry.id, date);
798
+ sleepUntilSpan?.end();
799
+ } catch (e) {
800
+ sleepUntilSpan?.error({ error: e });
801
+ throw e;
802
+ }
669
803
  }
670
804
  async executeWaitForEvent({ event, timeout }) {
671
805
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -685,8 +819,25 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
685
819
  prevOutput,
686
820
  emitter,
687
821
  abortController,
688
- runtimeContext
822
+ runtimeContext,
823
+ tracingContext,
824
+ writableStream,
825
+ disableScorers
689
826
  }) {
827
+ const stepAISpan = tracingContext?.currentSpan?.createChildSpan({
828
+ name: `workflow step: '${step.id}'`,
829
+ type: aiTracing.AISpanType.WORKFLOW_STEP,
830
+ input: prevOutput,
831
+ attributes: {
832
+ stepId: step.id
833
+ },
834
+ tracingPolicy: this.options?.tracingPolicy
835
+ });
836
+ const { inputData, validationError } = await workflows.validateStepInput({
837
+ prevOutput,
838
+ step,
839
+ validateInputs: this.options?.validateInputs ?? false
840
+ });
690
841
  const startedAt = await this.inngestStep.run(
691
842
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
692
843
  async () => {
@@ -713,10 +864,12 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
713
864
  eventTimestamp: Date.now()
714
865
  });
715
866
  await emitter.emit("watch-v2", {
716
- type: "step-start",
867
+ type: "workflow-step-start",
717
868
  payload: {
718
869
  id: step.id,
719
- status: "running"
870
+ status: "running",
871
+ payload: inputData,
872
+ startedAt: startedAt2
720
873
  }
721
874
  });
722
875
  return startedAt2;
@@ -735,7 +888,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
735
888
  const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
736
889
  function: step.getFunction(),
737
890
  data: {
738
- inputData: prevOutput,
891
+ inputData,
739
892
  runId,
740
893
  resume: {
741
894
  runId,
@@ -753,7 +906,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
753
906
  const invokeResp = await this.inngestStep.invoke(`workflow.${executionContext.workflowId}.step.${step.id}`, {
754
907
  function: step.getFunction(),
755
908
  data: {
756
- inputData: prevOutput
909
+ inputData
757
910
  }
758
911
  });
759
912
  result = invokeResp.result;
@@ -781,7 +934,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
781
934
  eventTimestamp: Date.now()
782
935
  });
783
936
  await emitter.emit("watch-v2", {
784
- type: "step-result",
937
+ type: "workflow-step-result",
785
938
  payload: {
786
939
  id: step.id,
787
940
  status: "failed",
@@ -816,7 +969,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
816
969
  eventTimestamp: Date.now()
817
970
  });
818
971
  await emitter.emit("watch-v2", {
819
- type: "step-suspended",
972
+ type: "workflow-step-suspended",
820
973
  payload: {
821
974
  id: step.id,
822
975
  status: "suspended"
@@ -873,7 +1026,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
873
1026
  eventTimestamp: Date.now()
874
1027
  });
875
1028
  await emitter.emit("watch-v2", {
876
- type: "step-result",
1029
+ type: "workflow-step-result",
877
1030
  payload: {
878
1031
  id: step.id,
879
1032
  status: "success",
@@ -881,7 +1034,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
881
1034
  }
882
1035
  });
883
1036
  await emitter.emit("watch-v2", {
884
- type: "step-finish",
1037
+ type: "workflow-step-finish",
885
1038
  payload: {
886
1039
  id: step.id,
887
1040
  metadata: {}
@@ -898,20 +1051,21 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
898
1051
  let suspended;
899
1052
  let bailed;
900
1053
  try {
1054
+ if (validationError) {
1055
+ throw validationError;
1056
+ }
901
1057
  const result = await step.execute({
902
1058
  runId: executionContext.runId,
903
1059
  mastra: this.mastra,
904
1060
  runtimeContext,
905
- inputData: prevOutput,
1061
+ writableStream,
1062
+ inputData,
906
1063
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
907
- getInitData: () => stepResults?.input,
908
- getStepResult: (step2) => {
909
- const result2 = stepResults[step2.id];
910
- if (result2?.status === "success") {
911
- return result2.output;
912
- }
913
- return null;
1064
+ tracingContext: {
1065
+ currentSpan: stepAISpan
914
1066
  },
1067
+ getInitData: () => stepResults?.input,
1068
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
915
1069
  suspend: async (suspendPayload) => {
916
1070
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
917
1071
  suspended = { payload: suspendPayload };
@@ -937,14 +1091,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
937
1091
  output: result,
938
1092
  startedAt,
939
1093
  endedAt,
940
- payload: prevOutput,
1094
+ payload: inputData,
941
1095
  resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
942
1096
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
943
1097
  };
944
1098
  } catch (e) {
945
1099
  execResults = {
946
1100
  status: "failed",
947
- payload: prevOutput,
1101
+ payload: inputData,
948
1102
  error: e instanceof Error ? e.message : String(e),
949
1103
  endedAt: Date.now(),
950
1104
  startedAt,
@@ -956,18 +1110,20 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
956
1110
  execResults = {
957
1111
  status: "suspended",
958
1112
  suspendedPayload: suspended.payload,
959
- payload: prevOutput,
1113
+ payload: inputData,
960
1114
  suspendedAt: Date.now(),
961
1115
  startedAt,
962
1116
  resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
963
1117
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
964
1118
  };
965
1119
  } else if (bailed) {
966
- execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
1120
+ execResults = { status: "bailed", output: bailed.payload, payload: inputData, endedAt: Date.now(), startedAt };
967
1121
  }
968
1122
  if (execResults.status === "failed") {
969
1123
  if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
970
- throw execResults.error;
1124
+ const error = new Error(execResults.error);
1125
+ stepAISpan?.error({ error });
1126
+ throw error;
971
1127
  }
972
1128
  }
973
1129
  await emitter.emit("watch", {
@@ -988,7 +1144,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
988
1144
  });
989
1145
  if (execResults.status === "suspended") {
990
1146
  await emitter.emit("watch-v2", {
991
- type: "step-suspended",
1147
+ type: "workflow-step-suspended",
992
1148
  payload: {
993
1149
  id: step.id,
994
1150
  ...execResults
@@ -996,22 +1152,40 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
996
1152
  });
997
1153
  } else {
998
1154
  await emitter.emit("watch-v2", {
999
- type: "step-result",
1155
+ type: "workflow-step-result",
1000
1156
  payload: {
1001
1157
  id: step.id,
1002
1158
  ...execResults
1003
1159
  }
1004
1160
  });
1005
1161
  await emitter.emit("watch-v2", {
1006
- type: "step-finish",
1162
+ type: "workflow-step-finish",
1007
1163
  payload: {
1008
1164
  id: step.id,
1009
1165
  metadata: {}
1010
1166
  }
1011
1167
  });
1012
1168
  }
1169
+ stepAISpan?.end({ output: execResults });
1013
1170
  return { result: execResults, executionContext, stepResults };
1014
1171
  });
1172
+ if (disableScorers !== false) {
1173
+ await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}.score`, async () => {
1174
+ if (step.scorers) {
1175
+ await this.runScorers({
1176
+ scorers: step.scorers,
1177
+ runId: executionContext.runId,
1178
+ input: inputData,
1179
+ output: stepRes.result,
1180
+ workflowId: executionContext.workflowId,
1181
+ stepId: step.id,
1182
+ runtimeContext,
1183
+ disableScorers,
1184
+ tracingContext: { currentSpan: stepAISpan }
1185
+ });
1186
+ }
1187
+ });
1188
+ }
1015
1189
  Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
1016
1190
  Object.assign(stepResults, stepRes.stepResults);
1017
1191
  return stepRes.result;
@@ -1020,6 +1194,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1020
1194
  workflowId,
1021
1195
  runId,
1022
1196
  stepResults,
1197
+ resourceId,
1023
1198
  executionContext,
1024
1199
  serializedStepGraph,
1025
1200
  workflowStatus,
@@ -1032,12 +1207,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1032
1207
  await this.mastra?.getStorage()?.persistWorkflowSnapshot({
1033
1208
  workflowName: workflowId,
1034
1209
  runId,
1210
+ resourceId,
1035
1211
  snapshot: {
1036
1212
  runId,
1037
1213
  value: {},
1038
1214
  context: stepResults,
1039
1215
  activePaths: [],
1040
1216
  suspendedPaths: executionContext.suspendedPaths,
1217
+ waitingPaths: {},
1041
1218
  serializedStepGraph,
1042
1219
  status: workflowStatus,
1043
1220
  result,
@@ -1061,30 +1238,46 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1061
1238
  executionContext,
1062
1239
  emitter,
1063
1240
  abortController,
1064
- runtimeContext
1241
+ runtimeContext,
1242
+ writableStream,
1243
+ disableScorers,
1244
+ tracingContext
1065
1245
  }) {
1246
+ const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
1247
+ type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL,
1248
+ name: `conditional: '${entry.conditions.length} conditions'`,
1249
+ input: prevOutput,
1250
+ attributes: {
1251
+ conditionCount: entry.conditions.length
1252
+ },
1253
+ tracingPolicy: this.options?.tracingPolicy
1254
+ });
1066
1255
  let execResults;
1067
1256
  const truthyIndexes = (await Promise.all(
1068
1257
  entry.conditions.map(
1069
1258
  (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
1259
+ const evalSpan = conditionalSpan?.createChildSpan({
1260
+ type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL_EVAL,
1261
+ name: `condition: '${index}'`,
1262
+ input: prevOutput,
1263
+ attributes: {
1264
+ conditionIndex: index
1265
+ },
1266
+ tracingPolicy: this.options?.tracingPolicy
1267
+ });
1070
1268
  try {
1071
1269
  const result = await cond({
1072
1270
  runId,
1271
+ workflowId,
1073
1272
  mastra: this.mastra,
1074
1273
  runtimeContext,
1075
1274
  runCount: -1,
1076
1275
  inputData: prevOutput,
1077
- getInitData: () => stepResults?.input,
1078
- getStepResult: (step) => {
1079
- if (!step?.id) {
1080
- return null;
1081
- }
1082
- const result2 = stepResults[step.id];
1083
- if (result2?.status === "success") {
1084
- return result2.output;
1085
- }
1086
- return null;
1276
+ tracingContext: {
1277
+ currentSpan: evalSpan
1087
1278
  },
1279
+ getInitData: () => stepResults?.input,
1280
+ getStepResult: workflows.getStepResult.bind(this, stepResults),
1088
1281
  // TODO: this function shouldn't have suspend probably?
1089
1282
  suspend: async (_suspendPayload) => {
1090
1283
  },
@@ -1094,29 +1287,58 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1094
1287
  abortController.abort();
1095
1288
  },
1096
1289
  [_constants.EMITTER_SYMBOL]: emitter,
1290
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
1291
+ // TODO: add streamVNext support
1097
1292
  engine: {
1098
1293
  step: this.inngestStep
1099
1294
  },
1100
- abortSignal: abortController.signal
1295
+ abortSignal: abortController.signal,
1296
+ writer: new tools.ToolStream(
1297
+ {
1298
+ prefix: "workflow-step",
1299
+ callId: crypto.randomUUID(),
1300
+ name: "conditional",
1301
+ runId
1302
+ },
1303
+ writableStream
1304
+ )
1305
+ });
1306
+ evalSpan?.end({
1307
+ output: result,
1308
+ attributes: {
1309
+ result: !!result
1310
+ }
1101
1311
  });
1102
1312
  return result ? index : null;
1103
1313
  } catch (e) {
1314
+ evalSpan?.error({
1315
+ error: e instanceof Error ? e : new Error(String(e)),
1316
+ attributes: {
1317
+ result: false
1318
+ }
1319
+ });
1104
1320
  return null;
1105
1321
  }
1106
1322
  })
1107
1323
  )
1108
1324
  )).filter((index) => index !== null);
1109
1325
  const stepsToRun = entry.steps.filter((_, index) => truthyIndexes.includes(index));
1326
+ conditionalSpan?.update({
1327
+ attributes: {
1328
+ truthyIndexes,
1329
+ selectedSteps: stepsToRun.map((s) => s.type === "step" ? s.step.id : `control-${s.type}`)
1330
+ }
1331
+ });
1110
1332
  const results = await Promise.all(
1111
1333
  stepsToRun.map(
1112
1334
  (step, index) => this.executeEntry({
1113
1335
  workflowId,
1114
1336
  runId,
1115
1337
  entry: step,
1338
+ serializedStepGraph,
1116
1339
  prevStep,
1117
1340
  stepResults,
1118
1341
  resume,
1119
- serializedStepGraph,
1120
1342
  executionContext: {
1121
1343
  workflowId,
1122
1344
  runId,
@@ -1127,7 +1349,12 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1127
1349
  },
1128
1350
  emitter,
1129
1351
  abortController,
1130
- runtimeContext
1352
+ runtimeContext,
1353
+ writableStream,
1354
+ disableScorers,
1355
+ tracingContext: {
1356
+ currentSpan: conditionalSpan
1357
+ }
1131
1358
  })
1132
1359
  )
1133
1360
  );
@@ -1148,6 +1375,15 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1148
1375
  }, {})
1149
1376
  };
1150
1377
  }
1378
+ if (execResults.status === "failed") {
1379
+ conditionalSpan?.error({
1380
+ error: new Error(execResults.error)
1381
+ });
1382
+ } else {
1383
+ conditionalSpan?.end({
1384
+ output: execResults.output || execResults
1385
+ });
1386
+ }
1151
1387
  return execResults;
1152
1388
  }
1153
1389
  };
@@ -1158,3 +1394,5 @@ exports.InngestWorkflow = InngestWorkflow;
1158
1394
  exports.createStep = createStep;
1159
1395
  exports.init = init;
1160
1396
  exports.serve = serve;
1397
+ //# sourceMappingURL=index.cjs.map
1398
+ //# sourceMappingURL=index.cjs.map