@mastra/inngest 0.0.0-cloudflare-deployer-dont-install-deps-20250714111754 → 0.0.0-configure-project-root-for-private-packages-20250919100548

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,13 @@ 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
+ }) {
14
19
  const wfs = mastra.getWorkflows();
15
- const functions = Array.from(
20
+ const workflowFunctions = Array.from(
16
21
  new Set(
17
22
  Object.values(wfs).flatMap((wf) => {
18
23
  if (wf instanceof InngestWorkflow) {
@@ -25,7 +30,7 @@ function serve({ mastra, inngest }) {
25
30
  );
26
31
  return hono.serve({
27
32
  client: inngest,
28
- functions
33
+ functions: [...workflowFunctions, ...userFunctions]
29
34
  });
30
35
  }
31
36
  var InngestRun = class extends workflows.Run {
@@ -53,7 +58,6 @@ var InngestRun = class extends workflows.Run {
53
58
  await new Promise((resolve) => setTimeout(resolve, 1e3));
54
59
  runs = await this.getRuns(eventId);
55
60
  if (runs?.[0]?.status === "Failed") {
56
- console.log("run", runs?.[0]);
57
61
  throw new Error(`Function run ${runs?.[0]?.status}`);
58
62
  } else if (runs?.[0]?.status === "Cancelled") {
59
63
  const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
@@ -86,6 +90,7 @@ var InngestRun = class extends workflows.Run {
86
90
  await this.#mastra?.storage?.persistWorkflowSnapshot({
87
91
  workflowName: this.workflowId,
88
92
  runId: this.runId,
93
+ resourceId: this.resourceId,
89
94
  snapshot: {
90
95
  ...snapshot,
91
96
  status: "canceled"
@@ -99,6 +104,7 @@ var InngestRun = class extends workflows.Run {
99
104
  await this.#mastra.getStorage()?.persistWorkflowSnapshot({
100
105
  workflowName: this.workflowId,
101
106
  runId: this.runId,
107
+ resourceId: this.resourceId,
102
108
  snapshot: {
103
109
  runId: this.runId,
104
110
  serializedStepGraph: this.serializedStepGraph,
@@ -106,6 +112,7 @@ var InngestRun = class extends workflows.Run {
106
112
  context: {},
107
113
  activePaths: [],
108
114
  suspendedPaths: {},
115
+ waitingPaths: {},
109
116
  timestamp: Date.now(),
110
117
  status: "running"
111
118
  }
@@ -114,7 +121,8 @@ var InngestRun = class extends workflows.Run {
114
121
  name: `workflow.${this.workflowId}`,
115
122
  data: {
116
123
  inputData,
117
- runId: this.runId
124
+ runId: this.runId,
125
+ resourceId: this.resourceId
118
126
  }
119
127
  });
120
128
  const eventId = eventOutput.ids[0];
@@ -155,6 +163,7 @@ var InngestRun = class extends workflows.Run {
155
163
  data: {
156
164
  inputData: params.resumeData,
157
165
  runId: this.runId,
166
+ workflowId: this.workflowId,
158
167
  stepResults: snapshot?.context,
159
168
  resume: {
160
169
  steps,
@@ -204,7 +213,11 @@ var InngestRun = class extends workflows.Run {
204
213
  const writer = writable.getWriter();
205
214
  const unwatch = this.watch(async (event) => {
206
215
  try {
207
- await writer.write(event);
216
+ const e = {
217
+ ...event,
218
+ type: event.type.replace("workflow-", "")
219
+ };
220
+ await writer.write(e);
208
221
  } catch {
209
222
  }
210
223
  }, "watch-v2");
@@ -235,8 +248,14 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
235
248
  #mastra;
236
249
  inngest;
237
250
  function;
251
+ flowControlConfig;
238
252
  constructor(params, inngest) {
239
- super(params);
253
+ const { concurrency, rateLimit, throttle, debounce, priority, ...workflowParams } = params;
254
+ super(workflowParams);
255
+ const flowControlEntries = Object.entries({ concurrency, rateLimit, throttle, debounce, priority }).filter(
256
+ ([_, value]) => value !== void 0
257
+ );
258
+ this.flowControlConfig = flowControlEntries.length > 0 ? Object.fromEntries(flowControlEntries) : void 0;
240
259
  this.#mastra = params.mastra;
241
260
  this.inngest = inngest;
242
261
  }
@@ -257,27 +276,6 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
257
276
  const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
258
277
  return run ?? (this.runs.get(runId) ? { ...this.runs.get(runId), workflowName: this.id } : null);
259
278
  }
260
- async getWorkflowRunExecutionResult(runId) {
261
- const storage = this.#mastra?.getStorage();
262
- if (!storage) {
263
- this.logger.debug("Cannot get workflow run execution result. Mastra storage is not initialized");
264
- return null;
265
- }
266
- const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
267
- if (!run?.snapshot) {
268
- return null;
269
- }
270
- if (typeof run.snapshot === "string") {
271
- return null;
272
- }
273
- return {
274
- status: run.snapshot.status,
275
- result: run.snapshot.result,
276
- error: run.snapshot.error,
277
- payload: run.snapshot.context?.input,
278
- steps: run.snapshot.context
279
- };
280
- }
281
279
  __registerMastra(mastra) {
282
280
  this.#mastra = mastra;
283
281
  this.executionEngine.__registerMastra(mastra);
@@ -296,23 +294,14 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
296
294
  }
297
295
  }
298
296
  }
299
- createRun(options) {
300
- const runIdToUse = options?.runId || crypto.randomUUID();
301
- const run = this.runs.get(runIdToUse) ?? new InngestRun(
302
- {
303
- workflowId: this.id,
304
- runId: runIdToUse,
305
- executionEngine: this.executionEngine,
306
- executionGraph: this.executionGraph,
307
- serializedStepGraph: this.serializedStepGraph,
308
- mastra: this.#mastra,
309
- retryConfig: this.retryConfig,
310
- cleanup: () => this.runs.delete(runIdToUse)
311
- },
312
- this.inngest
297
+ /**
298
+ * @deprecated Use createRunAsync() instead.
299
+ * @throws {Error} Always throws an error directing users to use createRunAsync()
300
+ */
301
+ createRun(_options) {
302
+ throw new Error(
303
+ "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."
313
304
  );
314
- this.runs.set(runIdToUse, run);
315
- return run;
316
305
  }
317
306
  async createRunAsync(options) {
318
307
  const runIdToUse = options?.runId || crypto.randomUUID();
@@ -320,6 +309,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
320
309
  {
321
310
  workflowId: this.id,
322
311
  runId: runIdToUse,
312
+ resourceId: options?.resourceId,
323
313
  executionEngine: this.executionEngine,
324
314
  executionGraph: this.executionGraph,
325
315
  serializedStepGraph: this.serializedStepGraph,
@@ -330,17 +320,19 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
330
320
  this.inngest
331
321
  );
332
322
  this.runs.set(runIdToUse, run);
333
- const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
323
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse, false);
334
324
  if (!workflowSnapshotInStorage) {
335
325
  await this.mastra?.getStorage()?.persistWorkflowSnapshot({
336
326
  workflowName: this.id,
337
327
  runId: runIdToUse,
328
+ resourceId: options?.resourceId,
338
329
  snapshot: {
339
330
  runId: runIdToUse,
340
331
  status: "pending",
341
332
  value: {},
342
333
  context: {},
343
334
  activePaths: [],
335
+ waitingPaths: {},
344
336
  serializedStepGraph: this.serializedStepGraph,
345
337
  suspendedPaths: {},
346
338
  result: void 0,
@@ -361,11 +353,13 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
361
353
  id: `workflow.${this.id}`,
362
354
  // @ts-ignore
363
355
  retries: this.retryConfig?.attempts ?? 0,
364
- cancelOn: [{ event: `cancel.workflow.${this.id}` }]
356
+ cancelOn: [{ event: `cancel.workflow.${this.id}` }],
357
+ // Spread flow control configuration
358
+ ...this.flowControlConfig
365
359
  },
366
360
  { event: `workflow.${this.id}` },
367
361
  async ({ event, step, attempt, publish }) => {
368
- let { inputData, runId, resume } = event.data;
362
+ let { inputData, runId, resourceId, resume } = event.data;
369
363
  if (!runId) {
370
364
  runId = await step.run(`workflow.${this.id}.runIdGen`, async () => {
371
365
  return crypto.randomUUID();
@@ -397,6 +391,7 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
397
391
  const result = await engine.execute({
398
392
  workflowId: this.id,
399
393
  runId,
394
+ resourceId,
400
395
  graph: this.executionGraph,
401
396
  serializedStepGraph: this.serializedStepGraph,
402
397
  input: inputData,
@@ -405,7 +400,9 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
405
400
  runtimeContext: new di.RuntimeContext(),
406
401
  // TODO
407
402
  resume,
408
- abortController: new AbortController()
403
+ abortController: new AbortController(),
404
+ currentSpan: void 0
405
+ // TODO: Pass actual parent AI span from workflow execution context
409
406
  });
410
407
  return { result, runId };
411
408
  }
@@ -442,14 +439,12 @@ function createStep(params) {
442
439
  // @ts-ignore
443
440
  inputSchema: zod.z.object({
444
441
  prompt: zod.z.string()
445
- // resourceId: z.string().optional(),
446
- // threadId: z.string().optional(),
447
442
  }),
448
443
  // @ts-ignore
449
444
  outputSchema: zod.z.object({
450
445
  text: zod.z.string()
451
446
  }),
452
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
447
+ execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort, tracingContext }) => {
453
448
  let streamPromise = {};
454
449
  streamPromise.promise = new Promise((resolve, reject) => {
455
450
  streamPromise.resolve = resolve;
@@ -459,14 +454,9 @@ function createStep(params) {
459
454
  name: params.name,
460
455
  args: inputData
461
456
  };
462
- await emitter.emit("watch-v2", {
463
- type: "tool-call-streaming-start",
464
- ...toolData
465
- });
466
457
  const { fullStream } = await params.stream(inputData.prompt, {
467
- // resourceId: inputData.resourceId,
468
- // threadId: inputData.threadId,
469
458
  runtimeContext,
459
+ tracingContext,
470
460
  onFinish: (result) => {
471
461
  streamPromise.resolve(result.text);
472
462
  },
@@ -475,30 +465,23 @@ function createStep(params) {
475
465
  if (abortSignal.aborted) {
476
466
  return abort();
477
467
  }
468
+ await emitter.emit("watch-v2", {
469
+ type: "tool-call-streaming-start",
470
+ ...toolData ?? {}
471
+ });
478
472
  for await (const chunk of fullStream) {
479
- switch (chunk.type) {
480
- case "text-delta":
481
- await emitter.emit("watch-v2", {
482
- type: "tool-call-delta",
483
- ...toolData,
484
- argsTextDelta: chunk.textDelta
485
- });
486
- break;
487
- case "step-start":
488
- case "step-finish":
489
- case "finish":
490
- break;
491
- case "tool-call":
492
- case "tool-result":
493
- case "tool-call-streaming-start":
494
- case "tool-call-delta":
495
- case "source":
496
- case "file":
497
- default:
498
- await emitter.emit("watch-v2", chunk);
499
- break;
473
+ if (chunk.type === "text-delta") {
474
+ await emitter.emit("watch-v2", {
475
+ type: "tool-call-delta",
476
+ ...toolData ?? {},
477
+ argsTextDelta: chunk.textDelta
478
+ });
500
479
  }
501
480
  }
481
+ await emitter.emit("watch-v2", {
482
+ type: "tool-call-streaming-finish",
483
+ ...toolData ?? {}
484
+ });
502
485
  return {
503
486
  text: await streamPromise.promise
504
487
  };
@@ -515,11 +498,12 @@ function createStep(params) {
515
498
  id: params.id,
516
499
  inputSchema: params.inputSchema,
517
500
  outputSchema: params.outputSchema,
518
- execute: async ({ inputData, mastra, runtimeContext }) => {
501
+ execute: async ({ inputData, mastra, runtimeContext, tracingContext }) => {
519
502
  return params.execute({
520
503
  context: inputData,
521
- mastra,
522
- runtimeContext
504
+ mastra: aiTracing.wrapMastra(mastra, tracingContext),
505
+ runtimeContext,
506
+ tracingContext
523
507
  });
524
508
  }
525
509
  };
@@ -566,19 +550,19 @@ function init(inngest) {
566
550
  var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
567
551
  inngestStep;
568
552
  inngestAttempts;
569
- constructor(mastra, inngestStep, inngestAttempts = 0) {
570
- super({ mastra });
553
+ constructor(mastra, inngestStep, inngestAttempts = 0, options) {
554
+ super({ mastra, options });
571
555
  this.inngestStep = inngestStep;
572
556
  this.inngestAttempts = inngestAttempts;
573
557
  }
574
558
  async execute(params) {
575
559
  await params.emitter.emit("watch-v2", {
576
- type: "start",
560
+ type: "workflow-start",
577
561
  payload: { runId: params.runId }
578
562
  });
579
563
  const result = await super.execute(params);
580
564
  await params.emitter.emit("watch-v2", {
581
- type: "finish",
565
+ type: "workflow-finish",
582
566
  payload: { runId: params.runId }
583
567
  });
584
568
  return result;
@@ -640,31 +624,6 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
640
624
  executionSpan?.end();
641
625
  return base;
642
626
  }
643
- async superExecuteStep({
644
- workflowId,
645
- runId,
646
- step,
647
- stepResults,
648
- executionContext,
649
- resume,
650
- prevOutput,
651
- emitter,
652
- abortController,
653
- runtimeContext
654
- }) {
655
- return super.executeStep({
656
- workflowId,
657
- runId,
658
- step,
659
- stepResults,
660
- executionContext,
661
- resume,
662
- prevOutput,
663
- emitter,
664
- abortController,
665
- runtimeContext
666
- });
667
- }
668
627
  // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
669
628
  // await this.inngestStep.sleep(id, duration);
670
629
  // }
@@ -676,17 +635,34 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
676
635
  stepResults,
677
636
  emitter,
678
637
  abortController,
679
- runtimeContext
638
+ runtimeContext,
639
+ executionContext,
640
+ writableStream,
641
+ tracingContext
680
642
  }) {
681
643
  let { duration, fn } = entry;
644
+ const sleepSpan = tracingContext?.currentSpan?.createChildSpan({
645
+ type: aiTracing.AISpanType.WORKFLOW_SLEEP,
646
+ name: `sleep: ${duration ? `${duration}ms` : "dynamic"}`,
647
+ attributes: {
648
+ durationMs: duration,
649
+ sleepType: fn ? "dynamic" : "fixed"
650
+ },
651
+ tracingPolicy: this.options?.tracingPolicy
652
+ });
682
653
  if (fn) {
654
+ const stepCallId = crypto.randomUUID();
683
655
  duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
684
656
  return await fn({
685
657
  runId,
658
+ workflowId,
686
659
  mastra: this.mastra,
687
660
  runtimeContext,
688
661
  inputData: prevOutput,
689
662
  runCount: -1,
663
+ tracingContext: {
664
+ currentSpan: sleepSpan
665
+ },
690
666
  getInitData: () => stepResults?.input,
691
667
  getStepResult: (step) => {
692
668
  if (!step?.id) {
@@ -707,12 +683,34 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
707
683
  abortController?.abort();
708
684
  },
709
685
  [_constants.EMITTER_SYMBOL]: emitter,
686
+ // TODO: add streamVNext support
687
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
710
688
  engine: { step: this.inngestStep },
711
- abortSignal: abortController?.signal
689
+ abortSignal: abortController?.signal,
690
+ writer: new tools.ToolStream(
691
+ {
692
+ prefix: "workflow-step",
693
+ callId: stepCallId,
694
+ name: "sleep",
695
+ runId
696
+ },
697
+ writableStream
698
+ )
712
699
  });
713
700
  });
701
+ sleepSpan?.update({
702
+ attributes: {
703
+ durationMs: duration
704
+ }
705
+ });
706
+ }
707
+ try {
708
+ await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
709
+ sleepSpan?.end();
710
+ } catch (e) {
711
+ sleepSpan?.error({ error: e });
712
+ throw e;
714
713
  }
715
- await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
716
714
  }
717
715
  async executeSleepUntil({
718
716
  workflowId,
@@ -722,17 +720,35 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
722
720
  stepResults,
723
721
  emitter,
724
722
  abortController,
725
- runtimeContext
723
+ runtimeContext,
724
+ executionContext,
725
+ writableStream,
726
+ tracingContext
726
727
  }) {
727
728
  let { date, fn } = entry;
729
+ const sleepUntilSpan = tracingContext?.currentSpan?.createChildSpan({
730
+ type: aiTracing.AISpanType.WORKFLOW_SLEEP,
731
+ name: `sleepUntil: ${date ? date.toISOString() : "dynamic"}`,
732
+ attributes: {
733
+ untilDate: date,
734
+ durationMs: date ? Math.max(0, date.getTime() - Date.now()) : void 0,
735
+ sleepType: fn ? "dynamic" : "fixed"
736
+ },
737
+ tracingPolicy: this.options?.tracingPolicy
738
+ });
728
739
  if (fn) {
729
740
  date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
741
+ const stepCallId = crypto.randomUUID();
730
742
  return await fn({
731
743
  runId,
744
+ workflowId,
732
745
  mastra: this.mastra,
733
746
  runtimeContext,
734
747
  inputData: prevOutput,
735
748
  runCount: -1,
749
+ tracingContext: {
750
+ currentSpan: sleepUntilSpan
751
+ },
736
752
  getInitData: () => stepResults?.input,
737
753
  getStepResult: (step) => {
738
754
  if (!step?.id) {
@@ -753,15 +769,42 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
753
769
  abortController?.abort();
754
770
  },
755
771
  [_constants.EMITTER_SYMBOL]: emitter,
772
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
773
+ // TODO: add streamVNext support
756
774
  engine: { step: this.inngestStep },
757
- abortSignal: abortController?.signal
775
+ abortSignal: abortController?.signal,
776
+ writer: new tools.ToolStream(
777
+ {
778
+ prefix: "workflow-step",
779
+ callId: stepCallId,
780
+ name: "sleep",
781
+ runId
782
+ },
783
+ writableStream
784
+ )
758
785
  });
759
786
  });
787
+ if (date && !(date instanceof Date)) {
788
+ date = new Date(date);
789
+ }
790
+ const time = !date ? 0 : date.getTime() - Date.now();
791
+ sleepUntilSpan?.update({
792
+ attributes: {
793
+ durationMs: Math.max(0, time)
794
+ }
795
+ });
760
796
  }
761
797
  if (!(date instanceof Date)) {
798
+ sleepUntilSpan?.end();
762
799
  return;
763
800
  }
764
- await this.inngestStep.sleepUntil(entry.id, date);
801
+ try {
802
+ await this.inngestStep.sleepUntil(entry.id, date);
803
+ sleepUntilSpan?.end();
804
+ } catch (e) {
805
+ sleepUntilSpan?.error({ error: e });
806
+ throw e;
807
+ }
765
808
  }
766
809
  async executeWaitForEvent({ event, timeout }) {
767
810
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -781,8 +824,20 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
781
824
  prevOutput,
782
825
  emitter,
783
826
  abortController,
784
- runtimeContext
827
+ runtimeContext,
828
+ tracingContext,
829
+ writableStream,
830
+ disableScorers
785
831
  }) {
832
+ const stepAISpan = tracingContext?.currentSpan?.createChildSpan({
833
+ name: `workflow step: '${step.id}'`,
834
+ type: aiTracing.AISpanType.WORKFLOW_STEP,
835
+ input: prevOutput,
836
+ attributes: {
837
+ stepId: step.id
838
+ },
839
+ tracingPolicy: this.options?.tracingPolicy
840
+ });
786
841
  const startedAt = await this.inngestStep.run(
787
842
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
788
843
  async () => {
@@ -809,7 +864,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
809
864
  eventTimestamp: Date.now()
810
865
  });
811
866
  await emitter.emit("watch-v2", {
812
- type: "step-start",
867
+ type: "workflow-step-start",
813
868
  payload: {
814
869
  id: step.id,
815
870
  status: "running",
@@ -879,7 +934,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
879
934
  eventTimestamp: Date.now()
880
935
  });
881
936
  await emitter.emit("watch-v2", {
882
- type: "step-result",
937
+ type: "workflow-step-result",
883
938
  payload: {
884
939
  id: step.id,
885
940
  status: "failed",
@@ -914,7 +969,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
914
969
  eventTimestamp: Date.now()
915
970
  });
916
971
  await emitter.emit("watch-v2", {
917
- type: "step-suspended",
972
+ type: "workflow-step-suspended",
918
973
  payload: {
919
974
  id: step.id,
920
975
  status: "suspended"
@@ -971,7 +1026,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
971
1026
  eventTimestamp: Date.now()
972
1027
  });
973
1028
  await emitter.emit("watch-v2", {
974
- type: "step-result",
1029
+ type: "workflow-step-result",
975
1030
  payload: {
976
1031
  id: step.id,
977
1032
  status: "success",
@@ -979,7 +1034,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
979
1034
  }
980
1035
  });
981
1036
  await emitter.emit("watch-v2", {
982
- type: "step-finish",
1037
+ type: "workflow-step-finish",
983
1038
  payload: {
984
1039
  id: step.id,
985
1040
  metadata: {}
@@ -1000,8 +1055,12 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1000
1055
  runId: executionContext.runId,
1001
1056
  mastra: this.mastra,
1002
1057
  runtimeContext,
1058
+ writableStream,
1003
1059
  inputData: prevOutput,
1004
1060
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
1061
+ tracingContext: {
1062
+ currentSpan: stepAISpan
1063
+ },
1005
1064
  getInitData: () => stepResults?.input,
1006
1065
  getStepResult: (step2) => {
1007
1066
  const result2 = stepResults[step2.id];
@@ -1065,7 +1124,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1065
1124
  }
1066
1125
  if (execResults.status === "failed") {
1067
1126
  if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
1068
- throw execResults.error;
1127
+ const error = new Error(execResults.error);
1128
+ stepAISpan?.error({ error });
1129
+ throw error;
1069
1130
  }
1070
1131
  }
1071
1132
  await emitter.emit("watch", {
@@ -1086,7 +1147,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1086
1147
  });
1087
1148
  if (execResults.status === "suspended") {
1088
1149
  await emitter.emit("watch-v2", {
1089
- type: "step-suspended",
1150
+ type: "workflow-step-suspended",
1090
1151
  payload: {
1091
1152
  id: step.id,
1092
1153
  ...execResults
@@ -1094,22 +1155,40 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1094
1155
  });
1095
1156
  } else {
1096
1157
  await emitter.emit("watch-v2", {
1097
- type: "step-result",
1158
+ type: "workflow-step-result",
1098
1159
  payload: {
1099
1160
  id: step.id,
1100
1161
  ...execResults
1101
1162
  }
1102
1163
  });
1103
1164
  await emitter.emit("watch-v2", {
1104
- type: "step-finish",
1165
+ type: "workflow-step-finish",
1105
1166
  payload: {
1106
1167
  id: step.id,
1107
1168
  metadata: {}
1108
1169
  }
1109
1170
  });
1110
1171
  }
1172
+ stepAISpan?.end({ output: execResults });
1111
1173
  return { result: execResults, executionContext, stepResults };
1112
1174
  });
1175
+ if (disableScorers !== false) {
1176
+ await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}.score`, async () => {
1177
+ if (step.scorers) {
1178
+ await this.runScorers({
1179
+ scorers: step.scorers,
1180
+ runId: executionContext.runId,
1181
+ input: prevOutput,
1182
+ output: stepRes.result,
1183
+ workflowId: executionContext.workflowId,
1184
+ stepId: step.id,
1185
+ runtimeContext,
1186
+ disableScorers,
1187
+ tracingContext: { currentSpan: stepAISpan }
1188
+ });
1189
+ }
1190
+ });
1191
+ }
1113
1192
  Object.assign(executionContext.suspendedPaths, stepRes.executionContext.suspendedPaths);
1114
1193
  Object.assign(stepResults, stepRes.stepResults);
1115
1194
  return stepRes.result;
@@ -1118,6 +1197,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1118
1197
  workflowId,
1119
1198
  runId,
1120
1199
  stepResults,
1200
+ resourceId,
1121
1201
  executionContext,
1122
1202
  serializedStepGraph,
1123
1203
  workflowStatus,
@@ -1130,12 +1210,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1130
1210
  await this.mastra?.getStorage()?.persistWorkflowSnapshot({
1131
1211
  workflowName: workflowId,
1132
1212
  runId,
1213
+ resourceId,
1133
1214
  snapshot: {
1134
1215
  runId,
1135
1216
  value: {},
1136
1217
  context: stepResults,
1137
1218
  activePaths: [],
1138
1219
  suspendedPaths: executionContext.suspendedPaths,
1220
+ waitingPaths: {},
1139
1221
  serializedStepGraph,
1140
1222
  status: workflowStatus,
1141
1223
  result,
@@ -1159,19 +1241,44 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1159
1241
  executionContext,
1160
1242
  emitter,
1161
1243
  abortController,
1162
- runtimeContext
1244
+ runtimeContext,
1245
+ writableStream,
1246
+ disableScorers,
1247
+ tracingContext
1163
1248
  }) {
1249
+ const conditionalSpan = tracingContext?.currentSpan?.createChildSpan({
1250
+ type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL,
1251
+ name: `conditional: '${entry.conditions.length} conditions'`,
1252
+ input: prevOutput,
1253
+ attributes: {
1254
+ conditionCount: entry.conditions.length
1255
+ },
1256
+ tracingPolicy: this.options?.tracingPolicy
1257
+ });
1164
1258
  let execResults;
1165
1259
  const truthyIndexes = (await Promise.all(
1166
1260
  entry.conditions.map(
1167
1261
  (cond, index) => this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
1262
+ const evalSpan = conditionalSpan?.createChildSpan({
1263
+ type: aiTracing.AISpanType.WORKFLOW_CONDITIONAL_EVAL,
1264
+ name: `condition: '${index}'`,
1265
+ input: prevOutput,
1266
+ attributes: {
1267
+ conditionIndex: index
1268
+ },
1269
+ tracingPolicy: this.options?.tracingPolicy
1270
+ });
1168
1271
  try {
1169
1272
  const result = await cond({
1170
1273
  runId,
1274
+ workflowId,
1171
1275
  mastra: this.mastra,
1172
1276
  runtimeContext,
1173
1277
  runCount: -1,
1174
1278
  inputData: prevOutput,
1279
+ tracingContext: {
1280
+ currentSpan: evalSpan
1281
+ },
1175
1282
  getInitData: () => stepResults?.input,
1176
1283
  getStepResult: (step) => {
1177
1284
  if (!step?.id) {
@@ -1192,29 +1299,58 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1192
1299
  abortController.abort();
1193
1300
  },
1194
1301
  [_constants.EMITTER_SYMBOL]: emitter,
1302
+ [_constants.STREAM_FORMAT_SYMBOL]: executionContext.format,
1303
+ // TODO: add streamVNext support
1195
1304
  engine: {
1196
1305
  step: this.inngestStep
1197
1306
  },
1198
- abortSignal: abortController.signal
1307
+ abortSignal: abortController.signal,
1308
+ writer: new tools.ToolStream(
1309
+ {
1310
+ prefix: "workflow-step",
1311
+ callId: crypto.randomUUID(),
1312
+ name: "conditional",
1313
+ runId
1314
+ },
1315
+ writableStream
1316
+ )
1317
+ });
1318
+ evalSpan?.end({
1319
+ output: result,
1320
+ attributes: {
1321
+ result: !!result
1322
+ }
1199
1323
  });
1200
1324
  return result ? index : null;
1201
1325
  } catch (e) {
1326
+ evalSpan?.error({
1327
+ error: e instanceof Error ? e : new Error(String(e)),
1328
+ attributes: {
1329
+ result: false
1330
+ }
1331
+ });
1202
1332
  return null;
1203
1333
  }
1204
1334
  })
1205
1335
  )
1206
1336
  )).filter((index) => index !== null);
1207
1337
  const stepsToRun = entry.steps.filter((_, index) => truthyIndexes.includes(index));
1338
+ conditionalSpan?.update({
1339
+ attributes: {
1340
+ truthyIndexes,
1341
+ selectedSteps: stepsToRun.map((s) => s.type === "step" ? s.step.id : `control-${s.type}`)
1342
+ }
1343
+ });
1208
1344
  const results = await Promise.all(
1209
1345
  stepsToRun.map(
1210
1346
  (step, index) => this.executeEntry({
1211
1347
  workflowId,
1212
1348
  runId,
1213
1349
  entry: step,
1350
+ serializedStepGraph,
1214
1351
  prevStep,
1215
1352
  stepResults,
1216
1353
  resume,
1217
- serializedStepGraph,
1218
1354
  executionContext: {
1219
1355
  workflowId,
1220
1356
  runId,
@@ -1225,7 +1361,12 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1225
1361
  },
1226
1362
  emitter,
1227
1363
  abortController,
1228
- runtimeContext
1364
+ runtimeContext,
1365
+ writableStream,
1366
+ disableScorers,
1367
+ tracingContext: {
1368
+ currentSpan: conditionalSpan
1369
+ }
1229
1370
  })
1230
1371
  )
1231
1372
  );
@@ -1246,6 +1387,15 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1246
1387
  }, {})
1247
1388
  };
1248
1389
  }
1390
+ if (execResults.status === "failed") {
1391
+ conditionalSpan?.error({
1392
+ error: new Error(execResults.error)
1393
+ });
1394
+ } else {
1395
+ conditionalSpan?.end({
1396
+ output: execResults.output || execResults
1397
+ });
1398
+ }
1249
1399
  return execResults;
1250
1400
  }
1251
1401
  };
@@ -1256,3 +1406,5 @@ exports.InngestWorkflow = InngestWorkflow;
1256
1406
  exports.createStep = createStep;
1257
1407
  exports.init = init;
1258
1408
  exports.serve = serve;
1409
+ //# sourceMappingURL=index.cjs.map
1410
+ //# sourceMappingURL=index.cjs.map