@mastra/inngest 0.0.0-working-memory-per-user-20250620163010 → 0.0.0-zod-v4-compat-part-2-20250820135355

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) {
@@ -120,6 +155,7 @@ var InngestRun = class extends workflows.Run {
120
155
  data: {
121
156
  inputData: params.resumeData,
122
157
  runId: this.runId,
158
+ workflowId: this.workflowId,
123
159
  stepResults: snapshot?.context,
124
160
  resume: {
125
161
  steps,
@@ -279,13 +315,55 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
279
315
  this.runs.set(runIdToUse, run);
280
316
  return run;
281
317
  }
318
+ async createRunAsync(options) {
319
+ const runIdToUse = options?.runId || crypto.randomUUID();
320
+ const run = this.runs.get(runIdToUse) ?? new InngestRun(
321
+ {
322
+ workflowId: this.id,
323
+ runId: runIdToUse,
324
+ executionEngine: this.executionEngine,
325
+ executionGraph: this.executionGraph,
326
+ serializedStepGraph: this.serializedStepGraph,
327
+ mastra: this.#mastra,
328
+ retryConfig: this.retryConfig,
329
+ cleanup: () => this.runs.delete(runIdToUse)
330
+ },
331
+ this.inngest
332
+ );
333
+ this.runs.set(runIdToUse, run);
334
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
335
+ if (!workflowSnapshotInStorage) {
336
+ await this.mastra?.getStorage()?.persistWorkflowSnapshot({
337
+ workflowName: this.id,
338
+ runId: runIdToUse,
339
+ snapshot: {
340
+ runId: runIdToUse,
341
+ status: "pending",
342
+ value: {},
343
+ context: {},
344
+ activePaths: [],
345
+ serializedStepGraph: this.serializedStepGraph,
346
+ suspendedPaths: {},
347
+ result: void 0,
348
+ error: void 0,
349
+ // @ts-ignore
350
+ timestamp: Date.now()
351
+ }
352
+ });
353
+ }
354
+ return run;
355
+ }
282
356
  getFunction() {
283
357
  if (this.function) {
284
358
  return this.function;
285
359
  }
286
360
  this.function = this.inngest.createFunction(
287
- // @ts-ignore
288
- { id: `workflow.${this.id}`, retries: this.retryConfig?.attempts ?? 0 },
361
+ {
362
+ id: `workflow.${this.id}`,
363
+ // @ts-ignore
364
+ retries: this.retryConfig?.attempts ?? 0,
365
+ cancelOn: [{ event: `cancel.workflow.${this.id}` }]
366
+ },
289
367
  { event: `workflow.${this.id}` },
290
368
  async ({ event, step, attempt, publish }) => {
291
369
  let { inputData, runId, resume } = event.data;
@@ -327,7 +405,8 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
327
405
  retryConfig: this.retryConfig,
328
406
  runtimeContext: new di.RuntimeContext(),
329
407
  // TODO
330
- resume
408
+ resume,
409
+ abortController: new AbortController()
331
410
  });
332
411
  return { result, runId };
333
412
  }
@@ -371,7 +450,7 @@ function createStep(params) {
371
450
  outputSchema: zod.z.object({
372
451
  text: zod.z.string()
373
452
  }),
374
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext }) => {
453
+ execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
375
454
  let streamPromise = {};
376
455
  streamPromise.promise = new Promise((resolve, reject) => {
377
456
  streamPromise.resolve = resolve;
@@ -391,8 +470,12 @@ function createStep(params) {
391
470
  runtimeContext,
392
471
  onFinish: (result) => {
393
472
  streamPromise.resolve(result.text);
394
- }
473
+ },
474
+ abortSignal
395
475
  });
476
+ if (abortSignal.aborted) {
477
+ return abort();
478
+ }
396
479
  for await (const chunk of fullStream) {
397
480
  switch (chunk.type) {
398
481
  case "text-delta":
@@ -567,7 +650,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
567
650
  resume,
568
651
  prevOutput,
569
652
  emitter,
570
- runtimeContext
653
+ abortController,
654
+ runtimeContext,
655
+ writableStream
571
656
  }) {
572
657
  return super.executeStep({
573
658
  workflowId,
@@ -578,11 +663,132 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
578
663
  resume,
579
664
  prevOutput,
580
665
  emitter,
581
- runtimeContext
666
+ abortController,
667
+ runtimeContext,
668
+ writableStream
582
669
  });
583
670
  }
584
- async executeSleep({ id, duration }) {
585
- await this.inngestStep.sleep(id, duration);
671
+ // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
672
+ // await this.inngestStep.sleep(id, duration);
673
+ // }
674
+ async executeSleep({
675
+ workflowId,
676
+ runId,
677
+ entry,
678
+ prevOutput,
679
+ stepResults,
680
+ emitter,
681
+ abortController,
682
+ runtimeContext,
683
+ writableStream
684
+ }) {
685
+ let { duration, fn } = entry;
686
+ if (fn) {
687
+ const stepCallId = crypto.randomUUID();
688
+ duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
689
+ return await fn({
690
+ runId,
691
+ workflowId,
692
+ mastra: this.mastra,
693
+ runtimeContext,
694
+ inputData: prevOutput,
695
+ runCount: -1,
696
+ getInitData: () => stepResults?.input,
697
+ getStepResult: (step) => {
698
+ if (!step?.id) {
699
+ return null;
700
+ }
701
+ const result = stepResults[step.id];
702
+ if (result?.status === "success") {
703
+ return result.output;
704
+ }
705
+ return null;
706
+ },
707
+ // TODO: this function shouldn't have suspend probably?
708
+ suspend: async (_suspendPayload) => {
709
+ },
710
+ bail: () => {
711
+ },
712
+ abort: () => {
713
+ abortController?.abort();
714
+ },
715
+ [_constants.EMITTER_SYMBOL]: emitter,
716
+ engine: { step: this.inngestStep },
717
+ abortSignal: abortController?.signal,
718
+ writer: new tools.ToolStream(
719
+ {
720
+ prefix: "step",
721
+ callId: stepCallId,
722
+ name: "sleep",
723
+ runId
724
+ },
725
+ writableStream
726
+ )
727
+ });
728
+ });
729
+ }
730
+ await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
731
+ }
732
+ async executeSleepUntil({
733
+ workflowId,
734
+ runId,
735
+ entry,
736
+ prevOutput,
737
+ stepResults,
738
+ emitter,
739
+ abortController,
740
+ runtimeContext,
741
+ writableStream
742
+ }) {
743
+ let { date, fn } = entry;
744
+ if (fn) {
745
+ date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
746
+ const stepCallId = crypto.randomUUID();
747
+ return await fn({
748
+ runId,
749
+ workflowId,
750
+ mastra: this.mastra,
751
+ runtimeContext,
752
+ inputData: prevOutput,
753
+ runCount: -1,
754
+ getInitData: () => stepResults?.input,
755
+ getStepResult: (step) => {
756
+ if (!step?.id) {
757
+ return null;
758
+ }
759
+ const result = stepResults[step.id];
760
+ if (result?.status === "success") {
761
+ return result.output;
762
+ }
763
+ return null;
764
+ },
765
+ // TODO: this function shouldn't have suspend probably?
766
+ suspend: async (_suspendPayload) => {
767
+ },
768
+ bail: () => {
769
+ },
770
+ abort: () => {
771
+ abortController?.abort();
772
+ },
773
+ [_constants.EMITTER_SYMBOL]: emitter,
774
+ engine: { step: this.inngestStep },
775
+ abortSignal: abortController?.signal,
776
+ writer: new tools.ToolStream(
777
+ {
778
+ prefix: "step",
779
+ callId: stepCallId,
780
+ name: "sleep",
781
+ runId
782
+ },
783
+ writableStream
784
+ )
785
+ });
786
+ });
787
+ }
788
+ if (!(date instanceof Date)) {
789
+ return;
790
+ }
791
+ await this.inngestStep.sleepUntil(entry.id, date);
586
792
  }
587
793
  async executeWaitForEvent({ event, timeout }) {
588
794
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -601,7 +807,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
601
807
  resume,
602
808
  prevOutput,
603
809
  emitter,
604
- runtimeContext
810
+ abortController,
811
+ runtimeContext,
812
+ writableStream
605
813
  }) {
606
814
  const startedAt = await this.inngestStep.run(
607
815
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
@@ -631,7 +839,10 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
631
839
  await emitter.emit("watch-v2", {
632
840
  type: "step-start",
633
841
  payload: {
634
- id: step.id
842
+ id: step.id,
843
+ status: "running",
844
+ payload: prevOutput,
845
+ startedAt: startedAt2
635
846
  }
636
847
  });
637
848
  return startedAt2;
@@ -699,7 +910,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
699
910
  type: "step-result",
700
911
  payload: {
701
912
  id: step.id,
702
- status: "failed"
913
+ status: "failed",
914
+ error: result?.error,
915
+ payload: prevOutput
703
916
  }
704
917
  });
705
918
  return { executionContext, result: { status: "failed", error: result?.error } };
@@ -731,7 +944,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
731
944
  await emitter.emit("watch-v2", {
732
945
  type: "step-suspended",
733
946
  payload: {
734
- id: step.id
947
+ id: step.id,
948
+ status: "suspended"
735
949
  }
736
950
  });
737
951
  return {
@@ -784,6 +998,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
784
998
  },
785
999
  eventTimestamp: Date.now()
786
1000
  });
1001
+ await emitter.emit("watch-v2", {
1002
+ type: "step-result",
1003
+ payload: {
1004
+ id: step.id,
1005
+ status: "success",
1006
+ output: result?.result
1007
+ }
1008
+ });
787
1009
  await emitter.emit("watch-v2", {
788
1010
  type: "step-finish",
789
1011
  payload: {
@@ -800,11 +1022,13 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
800
1022
  const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
801
1023
  let execResults;
802
1024
  let suspended;
1025
+ let bailed;
803
1026
  try {
804
1027
  const result = await step.execute({
805
1028
  runId: executionContext.runId,
806
1029
  mastra: this.mastra,
807
1030
  runtimeContext,
1031
+ writableStream,
808
1032
  inputData: prevOutput,
809
1033
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
810
1034
  getInitData: () => stepResults?.input,
@@ -819,6 +1043,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
819
1043
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
820
1044
  suspended = { payload: suspendPayload };
821
1045
  },
1046
+ bail: (result2) => {
1047
+ bailed = { payload: result2 };
1048
+ },
822
1049
  resume: {
823
1050
  steps: resume?.steps?.slice(1) || [],
824
1051
  resumePayload: resume?.resumePayload,
@@ -828,7 +1055,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
828
1055
  [_constants.EMITTER_SYMBOL]: emitter,
829
1056
  engine: {
830
1057
  step: this.inngestStep
831
- }
1058
+ },
1059
+ abortSignal: abortController.signal
832
1060
  });
833
1061
  const endedAt = Date.now();
834
1062
  execResults = {
@@ -861,6 +1089,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
861
1089
  resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
862
1090
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
863
1091
  };
1092
+ } else if (bailed) {
1093
+ execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
864
1094
  }
865
1095
  if (execResults.status === "failed") {
866
1096
  if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
@@ -888,8 +1118,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
888
1118
  type: "step-suspended",
889
1119
  payload: {
890
1120
  id: step.id,
891
- status: execResults.status,
892
- output: execResults.status === "success" ? execResults?.output : void 0
1121
+ ...execResults
893
1122
  }
894
1123
  });
895
1124
  } else {
@@ -897,8 +1126,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
897
1126
  type: "step-result",
898
1127
  payload: {
899
1128
  id: step.id,
900
- status: execResults.status,
901
- output: execResults.status === "success" ? execResults?.output : void 0
1129
+ ...execResults
902
1130
  }
903
1131
  });
904
1132
  await emitter.emit("watch-v2", {
@@ -959,7 +1187,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
959
1187
  resume,
960
1188
  executionContext,
961
1189
  emitter,
962
- runtimeContext
1190
+ abortController,
1191
+ runtimeContext,
1192
+ writableStream
963
1193
  }) {
964
1194
  let execResults;
965
1195
  const truthyIndexes = (await Promise.all(
@@ -968,8 +1198,10 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
968
1198
  try {
969
1199
  const result = await cond({
970
1200
  runId,
1201
+ workflowId,
971
1202
  mastra: this.mastra,
972
1203
  runtimeContext,
1204
+ runCount: -1,
973
1205
  inputData: prevOutput,
974
1206
  getInitData: () => stepResults?.input,
975
1207
  getStepResult: (step) => {
@@ -985,10 +1217,25 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
985
1217
  // TODO: this function shouldn't have suspend probably?
986
1218
  suspend: async (_suspendPayload) => {
987
1219
  },
1220
+ bail: () => {
1221
+ },
1222
+ abort: () => {
1223
+ abortController.abort();
1224
+ },
988
1225
  [_constants.EMITTER_SYMBOL]: emitter,
989
1226
  engine: {
990
1227
  step: this.inngestStep
991
- }
1228
+ },
1229
+ abortSignal: abortController.signal,
1230
+ writer: new tools.ToolStream(
1231
+ {
1232
+ prefix: "step",
1233
+ callId: crypto.randomUUID(),
1234
+ name: "conditional",
1235
+ runId
1236
+ },
1237
+ writableStream
1238
+ )
992
1239
  });
993
1240
  return result ? index : null;
994
1241
  } catch (e) {
@@ -1017,7 +1264,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1017
1264
  executionSpan: executionContext.executionSpan
1018
1265
  },
1019
1266
  emitter,
1020
- runtimeContext
1267
+ abortController,
1268
+ runtimeContext,
1269
+ writableStream
1021
1270
  })
1022
1271
  )
1023
1272
  );
@@ -1048,3 +1297,5 @@ exports.InngestWorkflow = InngestWorkflow;
1048
1297
  exports.createStep = createStep;
1049
1298
  exports.init = init;
1050
1299
  exports.serve = serve;
1300
+ //# sourceMappingURL=index.cjs.map
1301
+ //# sourceMappingURL=index.cjs.map