@mastra/inngest 0.0.0-ai-v5-20250625173645 → 0.0.0-ai-v5-20250710191716

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
@@ -12,13 +12,17 @@ var zod = require('zod');
12
12
  // src/index.ts
13
13
  function serve({ mastra, inngest }) {
14
14
  const wfs = mastra.getWorkflows();
15
- const functions = Object.values(wfs).flatMap((wf) => {
16
- if (wf instanceof InngestWorkflow) {
17
- wf.__registerMastra(mastra);
18
- return wf.getFunctions();
19
- }
20
- return [];
21
- });
15
+ const functions = Array.from(
16
+ new Set(
17
+ Object.values(wfs).flatMap((wf) => {
18
+ if (wf instanceof InngestWorkflow) {
19
+ wf.__registerMastra(mastra);
20
+ return wf.getFunctions();
21
+ }
22
+ return [];
23
+ })
24
+ )
25
+ );
22
26
  return hono.serve({
23
27
  client: inngest,
24
28
  functions
@@ -48,8 +52,15 @@ var InngestRun = class extends workflows.Run {
48
52
  while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
49
53
  await new Promise((resolve) => setTimeout(resolve, 1e3));
50
54
  runs = await this.getRuns(eventId);
51
- if (runs?.[0]?.status === "Failed" || runs?.[0]?.status === "Cancelled") {
55
+ if (runs?.[0]?.status === "Failed") {
56
+ console.log("run", runs?.[0]);
52
57
  throw new Error(`Function run ${runs?.[0]?.status}`);
58
+ } else if (runs?.[0]?.status === "Cancelled") {
59
+ const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
60
+ workflowName: this.workflowId,
61
+ runId: this.runId
62
+ });
63
+ return { output: { result: { steps: snapshot?.context, status: "canceled" } } };
53
64
  }
54
65
  }
55
66
  return runs?.[0];
@@ -60,6 +71,28 @@ var InngestRun = class extends workflows.Run {
60
71
  data
61
72
  });
62
73
  }
74
+ async cancel() {
75
+ await this.inngest.send({
76
+ name: `cancel.workflow.${this.workflowId}`,
77
+ data: {
78
+ runId: this.runId
79
+ }
80
+ });
81
+ const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
82
+ workflowName: this.workflowId,
83
+ runId: this.runId
84
+ });
85
+ if (snapshot) {
86
+ await this.#mastra?.storage?.persistWorkflowSnapshot({
87
+ workflowName: this.workflowId,
88
+ runId: this.runId,
89
+ snapshot: {
90
+ ...snapshot,
91
+ status: "canceled"
92
+ }
93
+ });
94
+ }
95
+ }
63
96
  async start({
64
97
  inputData
65
98
  }) {
@@ -93,7 +126,9 @@ var InngestRun = class extends workflows.Run {
93
126
  if (result.status === "failed") {
94
127
  result.error = new Error(result.error);
95
128
  }
96
- this.cleanup?.();
129
+ if (result.status !== "suspended") {
130
+ this.cleanup?.();
131
+ }
97
132
  return result;
98
133
  }
99
134
  async resume(params) {
@@ -279,13 +314,55 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
279
314
  this.runs.set(runIdToUse, run);
280
315
  return run;
281
316
  }
317
+ async createRunAsync(options) {
318
+ const runIdToUse = options?.runId || crypto.randomUUID();
319
+ const run = this.runs.get(runIdToUse) ?? new InngestRun(
320
+ {
321
+ workflowId: this.id,
322
+ runId: runIdToUse,
323
+ executionEngine: this.executionEngine,
324
+ executionGraph: this.executionGraph,
325
+ serializedStepGraph: this.serializedStepGraph,
326
+ mastra: this.#mastra,
327
+ retryConfig: this.retryConfig,
328
+ cleanup: () => this.runs.delete(runIdToUse)
329
+ },
330
+ this.inngest
331
+ );
332
+ this.runs.set(runIdToUse, run);
333
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
334
+ if (!workflowSnapshotInStorage) {
335
+ await this.mastra?.getStorage()?.persistWorkflowSnapshot({
336
+ workflowName: this.id,
337
+ runId: runIdToUse,
338
+ snapshot: {
339
+ runId: runIdToUse,
340
+ status: "pending",
341
+ value: {},
342
+ context: {},
343
+ activePaths: [],
344
+ serializedStepGraph: this.serializedStepGraph,
345
+ suspendedPaths: {},
346
+ result: void 0,
347
+ error: void 0,
348
+ // @ts-ignore
349
+ timestamp: Date.now()
350
+ }
351
+ });
352
+ }
353
+ return run;
354
+ }
282
355
  getFunction() {
283
356
  if (this.function) {
284
357
  return this.function;
285
358
  }
286
359
  this.function = this.inngest.createFunction(
287
- // @ts-ignore
288
- { id: `workflow.${this.id}`, retries: this.retryConfig?.attempts ?? 0 },
360
+ {
361
+ id: `workflow.${this.id}`,
362
+ // @ts-ignore
363
+ retries: this.retryConfig?.attempts ?? 0,
364
+ cancelOn: [{ event: `cancel.workflow.${this.id}` }]
365
+ },
289
366
  { event: `workflow.${this.id}` },
290
367
  async ({ event, step, attempt, publish }) => {
291
368
  let { inputData, runId, resume } = event.data;
@@ -327,7 +404,8 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
327
404
  retryConfig: this.retryConfig,
328
405
  runtimeContext: new di.RuntimeContext(),
329
406
  // TODO
330
- resume
407
+ resume,
408
+ abortController: new AbortController()
331
409
  });
332
410
  return { result, runId };
333
411
  }
@@ -371,7 +449,7 @@ function createStep(params) {
371
449
  outputSchema: zod.z.object({
372
450
  text: zod.z.string()
373
451
  }),
374
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext }) => {
452
+ execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
375
453
  let streamPromise = {};
376
454
  streamPromise.promise = new Promise((resolve, reject) => {
377
455
  streamPromise.resolve = resolve;
@@ -391,8 +469,12 @@ function createStep(params) {
391
469
  runtimeContext,
392
470
  onFinish: (result) => {
393
471
  streamPromise.resolve(result.text);
394
- }
472
+ },
473
+ abortSignal
395
474
  });
475
+ if (abortSignal.aborted) {
476
+ return abort();
477
+ }
396
478
  for await (const chunk of fullStream) {
397
479
  switch (chunk.type) {
398
480
  case "text":
@@ -567,6 +649,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
567
649
  resume,
568
650
  prevOutput,
569
651
  emitter,
652
+ abortController,
570
653
  runtimeContext
571
654
  }) {
572
655
  return super.executeStep({
@@ -578,11 +661,107 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
578
661
  resume,
579
662
  prevOutput,
580
663
  emitter,
664
+ abortController,
581
665
  runtimeContext
582
666
  });
583
667
  }
584
- async executeSleep({ id, duration }) {
585
- await this.inngestStep.sleep(id, duration);
668
+ // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
669
+ // await this.inngestStep.sleep(id, duration);
670
+ // }
671
+ async executeSleep({
672
+ workflowId,
673
+ runId,
674
+ entry,
675
+ prevOutput,
676
+ stepResults,
677
+ emitter,
678
+ abortController,
679
+ runtimeContext
680
+ }) {
681
+ let { duration, fn } = entry;
682
+ if (fn) {
683
+ duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
684
+ return await fn({
685
+ runId,
686
+ mastra: this.mastra,
687
+ runtimeContext,
688
+ inputData: prevOutput,
689
+ runCount: -1,
690
+ getInitData: () => stepResults?.input,
691
+ getStepResult: (step) => {
692
+ if (!step?.id) {
693
+ return null;
694
+ }
695
+ const result = stepResults[step.id];
696
+ if (result?.status === "success") {
697
+ return result.output;
698
+ }
699
+ return null;
700
+ },
701
+ // TODO: this function shouldn't have suspend probably?
702
+ suspend: async (_suspendPayload) => {
703
+ },
704
+ bail: () => {
705
+ },
706
+ abort: () => {
707
+ abortController?.abort();
708
+ },
709
+ [_constants.EMITTER_SYMBOL]: emitter,
710
+ engine: { step: this.inngestStep },
711
+ abortSignal: abortController?.signal
712
+ });
713
+ });
714
+ }
715
+ await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
716
+ }
717
+ async executeSleepUntil({
718
+ workflowId,
719
+ runId,
720
+ entry,
721
+ prevOutput,
722
+ stepResults,
723
+ emitter,
724
+ abortController,
725
+ runtimeContext
726
+ }) {
727
+ let { date, fn } = entry;
728
+ if (fn) {
729
+ date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
730
+ return await fn({
731
+ runId,
732
+ mastra: this.mastra,
733
+ runtimeContext,
734
+ inputData: prevOutput,
735
+ runCount: -1,
736
+ getInitData: () => stepResults?.input,
737
+ getStepResult: (step) => {
738
+ if (!step?.id) {
739
+ return null;
740
+ }
741
+ const result = stepResults[step.id];
742
+ if (result?.status === "success") {
743
+ return result.output;
744
+ }
745
+ return null;
746
+ },
747
+ // TODO: this function shouldn't have suspend probably?
748
+ suspend: async (_suspendPayload) => {
749
+ },
750
+ bail: () => {
751
+ },
752
+ abort: () => {
753
+ abortController?.abort();
754
+ },
755
+ [_constants.EMITTER_SYMBOL]: emitter,
756
+ engine: { step: this.inngestStep },
757
+ abortSignal: abortController?.signal
758
+ });
759
+ });
760
+ }
761
+ if (!(date instanceof Date)) {
762
+ return;
763
+ }
764
+ await this.inngestStep.sleepUntil(entry.id, date);
586
765
  }
587
766
  async executeWaitForEvent({ event, timeout }) {
588
767
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -601,6 +780,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
601
780
  resume,
602
781
  prevOutput,
603
782
  emitter,
783
+ abortController,
604
784
  runtimeContext
605
785
  }) {
606
786
  const startedAt = await this.inngestStep.run(
@@ -631,7 +811,10 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
631
811
  await emitter.emit("watch-v2", {
632
812
  type: "step-start",
633
813
  payload: {
634
- id: step.id
814
+ id: step.id,
815
+ status: "running",
816
+ payload: prevOutput,
817
+ startedAt: startedAt2
635
818
  }
636
819
  });
637
820
  return startedAt2;
@@ -699,7 +882,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
699
882
  type: "step-result",
700
883
  payload: {
701
884
  id: step.id,
702
- status: "failed"
885
+ status: "failed",
886
+ error: result?.error,
887
+ payload: prevOutput
703
888
  }
704
889
  });
705
890
  return { executionContext, result: { status: "failed", error: result?.error } };
@@ -731,7 +916,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
731
916
  await emitter.emit("watch-v2", {
732
917
  type: "step-suspended",
733
918
  payload: {
734
- id: step.id
919
+ id: step.id,
920
+ status: "suspended"
735
921
  }
736
922
  });
737
923
  return {
@@ -784,6 +970,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
784
970
  },
785
971
  eventTimestamp: Date.now()
786
972
  });
973
+ await emitter.emit("watch-v2", {
974
+ type: "step-result",
975
+ payload: {
976
+ id: step.id,
977
+ status: "success",
978
+ output: result?.result
979
+ }
980
+ });
787
981
  await emitter.emit("watch-v2", {
788
982
  type: "step-finish",
789
983
  payload: {
@@ -800,6 +994,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
800
994
  const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
801
995
  let execResults;
802
996
  let suspended;
997
+ let bailed;
803
998
  try {
804
999
  const result = await step.execute({
805
1000
  runId: executionContext.runId,
@@ -819,6 +1014,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
819
1014
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
820
1015
  suspended = { payload: suspendPayload };
821
1016
  },
1017
+ bail: (result2) => {
1018
+ bailed = { payload: result2 };
1019
+ },
822
1020
  resume: {
823
1021
  steps: resume?.steps?.slice(1) || [],
824
1022
  resumePayload: resume?.resumePayload,
@@ -828,7 +1026,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
828
1026
  [_constants.EMITTER_SYMBOL]: emitter,
829
1027
  engine: {
830
1028
  step: this.inngestStep
831
- }
1029
+ },
1030
+ abortSignal: abortController.signal
832
1031
  });
833
1032
  const endedAt = Date.now();
834
1033
  execResults = {
@@ -861,6 +1060,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
861
1060
  resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
862
1061
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
863
1062
  };
1063
+ } else if (bailed) {
1064
+ execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
864
1065
  }
865
1066
  if (execResults.status === "failed") {
866
1067
  if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
@@ -888,8 +1089,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
888
1089
  type: "step-suspended",
889
1090
  payload: {
890
1091
  id: step.id,
891
- status: execResults.status,
892
- output: execResults.status === "success" ? execResults?.output : void 0
1092
+ ...execResults
893
1093
  }
894
1094
  });
895
1095
  } else {
@@ -897,8 +1097,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
897
1097
  type: "step-result",
898
1098
  payload: {
899
1099
  id: step.id,
900
- status: execResults.status,
901
- output: execResults.status === "success" ? execResults?.output : void 0
1100
+ ...execResults
902
1101
  }
903
1102
  });
904
1103
  await emitter.emit("watch-v2", {
@@ -959,6 +1158,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
959
1158
  resume,
960
1159
  executionContext,
961
1160
  emitter,
1161
+ abortController,
962
1162
  runtimeContext
963
1163
  }) {
964
1164
  let execResults;
@@ -970,6 +1170,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
970
1170
  runId,
971
1171
  mastra: this.mastra,
972
1172
  runtimeContext,
1173
+ runCount: -1,
973
1174
  inputData: prevOutput,
974
1175
  getInitData: () => stepResults?.input,
975
1176
  getStepResult: (step) => {
@@ -985,10 +1186,16 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
985
1186
  // TODO: this function shouldn't have suspend probably?
986
1187
  suspend: async (_suspendPayload) => {
987
1188
  },
1189
+ bail: () => {
1190
+ },
1191
+ abort: () => {
1192
+ abortController.abort();
1193
+ },
988
1194
  [_constants.EMITTER_SYMBOL]: emitter,
989
1195
  engine: {
990
1196
  step: this.inngestStep
991
- }
1197
+ },
1198
+ abortSignal: abortController.signal
992
1199
  });
993
1200
  return result ? index : null;
994
1201
  } catch (e) {
@@ -1017,6 +1224,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1017
1224
  executionSpan: executionContext.executionSpan
1018
1225
  },
1019
1226
  emitter,
1227
+ abortController,
1020
1228
  runtimeContext
1021
1229
  })
1022
1230
  )