@mastra/inngest 0.0.0-fix-fetching-workflow-snapshots-20250625000954 → 0.0.0-fix-traces-pagination-plus-share-for-cloud-20250717083008

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) {
@@ -295,23 +330,26 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
295
330
  this.inngest
296
331
  );
297
332
  this.runs.set(runIdToUse, run);
298
- await this.mastra?.getStorage()?.persistWorkflowSnapshot({
299
- workflowName: this.id,
300
- runId: runIdToUse,
301
- snapshot: {
333
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
334
+ if (!workflowSnapshotInStorage) {
335
+ await this.mastra?.getStorage()?.persistWorkflowSnapshot({
336
+ workflowName: this.id,
302
337
  runId: runIdToUse,
303
- status: "pending",
304
- value: {},
305
- context: {},
306
- activePaths: [],
307
- serializedStepGraph: this.serializedStepGraph,
308
- suspendedPaths: {},
309
- result: void 0,
310
- error: void 0,
311
- // @ts-ignore
312
- timestamp: Date.now()
313
- }
314
- });
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
+ }
315
353
  return run;
316
354
  }
317
355
  getFunction() {
@@ -319,8 +357,12 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
319
357
  return this.function;
320
358
  }
321
359
  this.function = this.inngest.createFunction(
322
- // @ts-ignore
323
- { 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
+ },
324
366
  { event: `workflow.${this.id}` },
325
367
  async ({ event, step, attempt, publish }) => {
326
368
  let { inputData, runId, resume } = event.data;
@@ -362,7 +404,8 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
362
404
  retryConfig: this.retryConfig,
363
405
  runtimeContext: new di.RuntimeContext(),
364
406
  // TODO
365
- resume
407
+ resume,
408
+ abortController: new AbortController()
366
409
  });
367
410
  return { result, runId };
368
411
  }
@@ -406,7 +449,7 @@ function createStep(params) {
406
449
  outputSchema: zod.z.object({
407
450
  text: zod.z.string()
408
451
  }),
409
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext }) => {
452
+ execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
410
453
  let streamPromise = {};
411
454
  streamPromise.promise = new Promise((resolve, reject) => {
412
455
  streamPromise.resolve = resolve;
@@ -426,8 +469,12 @@ function createStep(params) {
426
469
  runtimeContext,
427
470
  onFinish: (result) => {
428
471
  streamPromise.resolve(result.text);
429
- }
472
+ },
473
+ abortSignal
430
474
  });
475
+ if (abortSignal.aborted) {
476
+ return abort();
477
+ }
431
478
  for await (const chunk of fullStream) {
432
479
  switch (chunk.type) {
433
480
  case "text-delta":
@@ -602,6 +649,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
602
649
  resume,
603
650
  prevOutput,
604
651
  emitter,
652
+ abortController,
605
653
  runtimeContext
606
654
  }) {
607
655
  return super.executeStep({
@@ -613,11 +661,107 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
613
661
  resume,
614
662
  prevOutput,
615
663
  emitter,
664
+ abortController,
616
665
  runtimeContext
617
666
  });
618
667
  }
619
- async executeSleep({ id, duration }) {
620
- 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);
621
765
  }
622
766
  async executeWaitForEvent({ event, timeout }) {
623
767
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -636,6 +780,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
636
780
  resume,
637
781
  prevOutput,
638
782
  emitter,
783
+ abortController,
639
784
  runtimeContext
640
785
  }) {
641
786
  const startedAt = await this.inngestStep.run(
@@ -666,7 +811,10 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
666
811
  await emitter.emit("watch-v2", {
667
812
  type: "step-start",
668
813
  payload: {
669
- id: step.id
814
+ id: step.id,
815
+ status: "running",
816
+ payload: prevOutput,
817
+ startedAt: startedAt2
670
818
  }
671
819
  });
672
820
  return startedAt2;
@@ -734,7 +882,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
734
882
  type: "step-result",
735
883
  payload: {
736
884
  id: step.id,
737
- status: "failed"
885
+ status: "failed",
886
+ error: result?.error,
887
+ payload: prevOutput
738
888
  }
739
889
  });
740
890
  return { executionContext, result: { status: "failed", error: result?.error } };
@@ -766,7 +916,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
766
916
  await emitter.emit("watch-v2", {
767
917
  type: "step-suspended",
768
918
  payload: {
769
- id: step.id
919
+ id: step.id,
920
+ status: "suspended"
770
921
  }
771
922
  });
772
923
  return {
@@ -819,6 +970,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
819
970
  },
820
971
  eventTimestamp: Date.now()
821
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
+ });
822
981
  await emitter.emit("watch-v2", {
823
982
  type: "step-finish",
824
983
  payload: {
@@ -867,7 +1026,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
867
1026
  [_constants.EMITTER_SYMBOL]: emitter,
868
1027
  engine: {
869
1028
  step: this.inngestStep
870
- }
1029
+ },
1030
+ abortSignal: abortController.signal
871
1031
  });
872
1032
  const endedAt = Date.now();
873
1033
  execResults = {
@@ -929,8 +1089,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
929
1089
  type: "step-suspended",
930
1090
  payload: {
931
1091
  id: step.id,
932
- status: execResults.status,
933
- output: execResults.status === "success" ? execResults?.output : void 0
1092
+ ...execResults
934
1093
  }
935
1094
  });
936
1095
  } else {
@@ -938,8 +1097,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
938
1097
  type: "step-result",
939
1098
  payload: {
940
1099
  id: step.id,
941
- status: execResults.status,
942
- output: execResults.status === "success" ? execResults?.output : void 0
1100
+ ...execResults
943
1101
  }
944
1102
  });
945
1103
  await emitter.emit("watch-v2", {
@@ -1000,6 +1158,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1000
1158
  resume,
1001
1159
  executionContext,
1002
1160
  emitter,
1161
+ abortController,
1003
1162
  runtimeContext
1004
1163
  }) {
1005
1164
  let execResults;
@@ -1011,6 +1170,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1011
1170
  runId,
1012
1171
  mastra: this.mastra,
1013
1172
  runtimeContext,
1173
+ runCount: -1,
1014
1174
  inputData: prevOutput,
1015
1175
  getInitData: () => stepResults?.input,
1016
1176
  getStepResult: (step) => {
@@ -1028,10 +1188,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1028
1188
  },
1029
1189
  bail: () => {
1030
1190
  },
1191
+ abort: () => {
1192
+ abortController.abort();
1193
+ },
1031
1194
  [_constants.EMITTER_SYMBOL]: emitter,
1032
1195
  engine: {
1033
1196
  step: this.inngestStep
1034
- }
1197
+ },
1198
+ abortSignal: abortController.signal
1035
1199
  });
1036
1200
  return result ? index : null;
1037
1201
  } catch (e) {
@@ -1060,6 +1224,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1060
1224
  executionSpan: executionContext.executionSpan
1061
1225
  },
1062
1226
  emitter,
1227
+ abortController,
1063
1228
  runtimeContext
1064
1229
  })
1065
1230
  )