@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.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { randomUUID } from 'crypto';
2
2
  import { subscribe } from '@inngest/realtime';
3
3
  import { RuntimeContext } from '@mastra/core/di';
4
- import { Tool } from '@mastra/core/tools';
4
+ import { ToolStream, Tool } from '@mastra/core/tools';
5
5
  import { Run, Workflow, DefaultExecutionEngine } from '@mastra/core/workflows';
6
6
  import { EMITTER_SYMBOL } from '@mastra/core/workflows/_constants';
7
7
  import { serve as serve$1 } from 'inngest/hono';
@@ -10,13 +10,17 @@ import { z } from 'zod';
10
10
  // src/index.ts
11
11
  function serve({ mastra, inngest }) {
12
12
  const wfs = mastra.getWorkflows();
13
- const functions = Object.values(wfs).flatMap((wf) => {
14
- if (wf instanceof InngestWorkflow) {
15
- wf.__registerMastra(mastra);
16
- return wf.getFunctions();
17
- }
18
- return [];
19
- });
13
+ const functions = Array.from(
14
+ new Set(
15
+ Object.values(wfs).flatMap((wf) => {
16
+ if (wf instanceof InngestWorkflow) {
17
+ wf.__registerMastra(mastra);
18
+ return wf.getFunctions();
19
+ }
20
+ return [];
21
+ })
22
+ )
23
+ );
20
24
  return serve$1({
21
25
  client: inngest,
22
26
  functions
@@ -46,8 +50,15 @@ var InngestRun = class extends Run {
46
50
  while (runs?.[0]?.status !== "Completed" || runs?.[0]?.event_id !== eventId) {
47
51
  await new Promise((resolve) => setTimeout(resolve, 1e3));
48
52
  runs = await this.getRuns(eventId);
49
- if (runs?.[0]?.status === "Failed" || runs?.[0]?.status === "Cancelled") {
53
+ if (runs?.[0]?.status === "Failed") {
54
+ console.log("run", runs?.[0]);
50
55
  throw new Error(`Function run ${runs?.[0]?.status}`);
56
+ } else if (runs?.[0]?.status === "Cancelled") {
57
+ const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
58
+ workflowName: this.workflowId,
59
+ runId: this.runId
60
+ });
61
+ return { output: { result: { steps: snapshot?.context, status: "canceled" } } };
51
62
  }
52
63
  }
53
64
  return runs?.[0];
@@ -58,6 +69,28 @@ var InngestRun = class extends Run {
58
69
  data
59
70
  });
60
71
  }
72
+ async cancel() {
73
+ await this.inngest.send({
74
+ name: `cancel.workflow.${this.workflowId}`,
75
+ data: {
76
+ runId: this.runId
77
+ }
78
+ });
79
+ const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
80
+ workflowName: this.workflowId,
81
+ runId: this.runId
82
+ });
83
+ if (snapshot) {
84
+ await this.#mastra?.storage?.persistWorkflowSnapshot({
85
+ workflowName: this.workflowId,
86
+ runId: this.runId,
87
+ snapshot: {
88
+ ...snapshot,
89
+ status: "canceled"
90
+ }
91
+ });
92
+ }
93
+ }
61
94
  async start({
62
95
  inputData
63
96
  }) {
@@ -91,7 +124,9 @@ var InngestRun = class extends Run {
91
124
  if (result.status === "failed") {
92
125
  result.error = new Error(result.error);
93
126
  }
94
- this.cleanup?.();
127
+ if (result.status !== "suspended") {
128
+ this.cleanup?.();
129
+ }
95
130
  return result;
96
131
  }
97
132
  async resume(params) {
@@ -118,6 +153,7 @@ var InngestRun = class extends Run {
118
153
  data: {
119
154
  inputData: params.resumeData,
120
155
  runId: this.runId,
156
+ workflowId: this.workflowId,
121
157
  stepResults: snapshot?.context,
122
158
  resume: {
123
159
  steps,
@@ -277,13 +313,55 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
277
313
  this.runs.set(runIdToUse, run);
278
314
  return run;
279
315
  }
316
+ async createRunAsync(options) {
317
+ const runIdToUse = options?.runId || randomUUID();
318
+ const run = this.runs.get(runIdToUse) ?? new InngestRun(
319
+ {
320
+ workflowId: this.id,
321
+ runId: runIdToUse,
322
+ executionEngine: this.executionEngine,
323
+ executionGraph: this.executionGraph,
324
+ serializedStepGraph: this.serializedStepGraph,
325
+ mastra: this.#mastra,
326
+ retryConfig: this.retryConfig,
327
+ cleanup: () => this.runs.delete(runIdToUse)
328
+ },
329
+ this.inngest
330
+ );
331
+ this.runs.set(runIdToUse, run);
332
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
333
+ if (!workflowSnapshotInStorage) {
334
+ await this.mastra?.getStorage()?.persistWorkflowSnapshot({
335
+ workflowName: this.id,
336
+ runId: runIdToUse,
337
+ snapshot: {
338
+ runId: runIdToUse,
339
+ status: "pending",
340
+ value: {},
341
+ context: {},
342
+ activePaths: [],
343
+ serializedStepGraph: this.serializedStepGraph,
344
+ suspendedPaths: {},
345
+ result: void 0,
346
+ error: void 0,
347
+ // @ts-ignore
348
+ timestamp: Date.now()
349
+ }
350
+ });
351
+ }
352
+ return run;
353
+ }
280
354
  getFunction() {
281
355
  if (this.function) {
282
356
  return this.function;
283
357
  }
284
358
  this.function = this.inngest.createFunction(
285
- // @ts-ignore
286
- { id: `workflow.${this.id}`, retries: this.retryConfig?.attempts ?? 0 },
359
+ {
360
+ id: `workflow.${this.id}`,
361
+ // @ts-ignore
362
+ retries: this.retryConfig?.attempts ?? 0,
363
+ cancelOn: [{ event: `cancel.workflow.${this.id}` }]
364
+ },
287
365
  { event: `workflow.${this.id}` },
288
366
  async ({ event, step, attempt, publish }) => {
289
367
  let { inputData, runId, resume } = event.data;
@@ -325,7 +403,8 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
325
403
  retryConfig: this.retryConfig,
326
404
  runtimeContext: new RuntimeContext(),
327
405
  // TODO
328
- resume
406
+ resume,
407
+ abortController: new AbortController()
329
408
  });
330
409
  return { result, runId };
331
410
  }
@@ -369,7 +448,7 @@ function createStep(params) {
369
448
  outputSchema: z.object({
370
449
  text: z.string()
371
450
  }),
372
- execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext }) => {
451
+ execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
373
452
  let streamPromise = {};
374
453
  streamPromise.promise = new Promise((resolve, reject) => {
375
454
  streamPromise.resolve = resolve;
@@ -389,8 +468,12 @@ function createStep(params) {
389
468
  runtimeContext,
390
469
  onFinish: (result) => {
391
470
  streamPromise.resolve(result.text);
392
- }
471
+ },
472
+ abortSignal
393
473
  });
474
+ if (abortSignal.aborted) {
475
+ return abort();
476
+ }
394
477
  for await (const chunk of fullStream) {
395
478
  switch (chunk.type) {
396
479
  case "text-delta":
@@ -565,7 +648,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
565
648
  resume,
566
649
  prevOutput,
567
650
  emitter,
568
- runtimeContext
651
+ abortController,
652
+ runtimeContext,
653
+ writableStream
569
654
  }) {
570
655
  return super.executeStep({
571
656
  workflowId,
@@ -576,11 +661,132 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
576
661
  resume,
577
662
  prevOutput,
578
663
  emitter,
579
- runtimeContext
664
+ abortController,
665
+ runtimeContext,
666
+ writableStream
580
667
  });
581
668
  }
582
- async executeSleep({ id, duration }) {
583
- await this.inngestStep.sleep(id, duration);
669
+ // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
670
+ // await this.inngestStep.sleep(id, duration);
671
+ // }
672
+ async executeSleep({
673
+ workflowId,
674
+ runId,
675
+ entry,
676
+ prevOutput,
677
+ stepResults,
678
+ emitter,
679
+ abortController,
680
+ runtimeContext,
681
+ writableStream
682
+ }) {
683
+ let { duration, fn } = entry;
684
+ if (fn) {
685
+ const stepCallId = randomUUID();
686
+ duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
687
+ return await fn({
688
+ runId,
689
+ workflowId,
690
+ mastra: this.mastra,
691
+ runtimeContext,
692
+ inputData: prevOutput,
693
+ runCount: -1,
694
+ getInitData: () => stepResults?.input,
695
+ getStepResult: (step) => {
696
+ if (!step?.id) {
697
+ return null;
698
+ }
699
+ const result = stepResults[step.id];
700
+ if (result?.status === "success") {
701
+ return result.output;
702
+ }
703
+ return null;
704
+ },
705
+ // TODO: this function shouldn't have suspend probably?
706
+ suspend: async (_suspendPayload) => {
707
+ },
708
+ bail: () => {
709
+ },
710
+ abort: () => {
711
+ abortController?.abort();
712
+ },
713
+ [EMITTER_SYMBOL]: emitter,
714
+ engine: { step: this.inngestStep },
715
+ abortSignal: abortController?.signal,
716
+ writer: new ToolStream(
717
+ {
718
+ prefix: "step",
719
+ callId: stepCallId,
720
+ name: "sleep",
721
+ runId
722
+ },
723
+ writableStream
724
+ )
725
+ });
726
+ });
727
+ }
728
+ await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
729
+ }
730
+ async executeSleepUntil({
731
+ workflowId,
732
+ runId,
733
+ entry,
734
+ prevOutput,
735
+ stepResults,
736
+ emitter,
737
+ abortController,
738
+ runtimeContext,
739
+ writableStream
740
+ }) {
741
+ let { date, fn } = entry;
742
+ if (fn) {
743
+ date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
744
+ const stepCallId = randomUUID();
745
+ return await fn({
746
+ runId,
747
+ workflowId,
748
+ mastra: this.mastra,
749
+ runtimeContext,
750
+ inputData: prevOutput,
751
+ runCount: -1,
752
+ getInitData: () => stepResults?.input,
753
+ getStepResult: (step) => {
754
+ if (!step?.id) {
755
+ return null;
756
+ }
757
+ const result = stepResults[step.id];
758
+ if (result?.status === "success") {
759
+ return result.output;
760
+ }
761
+ return null;
762
+ },
763
+ // TODO: this function shouldn't have suspend probably?
764
+ suspend: async (_suspendPayload) => {
765
+ },
766
+ bail: () => {
767
+ },
768
+ abort: () => {
769
+ abortController?.abort();
770
+ },
771
+ [EMITTER_SYMBOL]: emitter,
772
+ engine: { step: this.inngestStep },
773
+ abortSignal: abortController?.signal,
774
+ writer: new ToolStream(
775
+ {
776
+ prefix: "step",
777
+ callId: stepCallId,
778
+ name: "sleep",
779
+ runId
780
+ },
781
+ writableStream
782
+ )
783
+ });
784
+ });
785
+ }
786
+ if (!(date instanceof Date)) {
787
+ return;
788
+ }
789
+ await this.inngestStep.sleepUntil(entry.id, date);
584
790
  }
585
791
  async executeWaitForEvent({ event, timeout }) {
586
792
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -599,7 +805,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
599
805
  resume,
600
806
  prevOutput,
601
807
  emitter,
602
- runtimeContext
808
+ abortController,
809
+ runtimeContext,
810
+ writableStream
603
811
  }) {
604
812
  const startedAt = await this.inngestStep.run(
605
813
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
@@ -629,7 +837,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
629
837
  await emitter.emit("watch-v2", {
630
838
  type: "step-start",
631
839
  payload: {
632
- id: step.id
840
+ id: step.id,
841
+ status: "running",
842
+ payload: prevOutput,
843
+ startedAt: startedAt2
633
844
  }
634
845
  });
635
846
  return startedAt2;
@@ -697,7 +908,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
697
908
  type: "step-result",
698
909
  payload: {
699
910
  id: step.id,
700
- status: "failed"
911
+ status: "failed",
912
+ error: result?.error,
913
+ payload: prevOutput
701
914
  }
702
915
  });
703
916
  return { executionContext, result: { status: "failed", error: result?.error } };
@@ -729,7 +942,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
729
942
  await emitter.emit("watch-v2", {
730
943
  type: "step-suspended",
731
944
  payload: {
732
- id: step.id
945
+ id: step.id,
946
+ status: "suspended"
733
947
  }
734
948
  });
735
949
  return {
@@ -782,6 +996,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
782
996
  },
783
997
  eventTimestamp: Date.now()
784
998
  });
999
+ await emitter.emit("watch-v2", {
1000
+ type: "step-result",
1001
+ payload: {
1002
+ id: step.id,
1003
+ status: "success",
1004
+ output: result?.result
1005
+ }
1006
+ });
785
1007
  await emitter.emit("watch-v2", {
786
1008
  type: "step-finish",
787
1009
  payload: {
@@ -798,11 +1020,13 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
798
1020
  const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
799
1021
  let execResults;
800
1022
  let suspended;
1023
+ let bailed;
801
1024
  try {
802
1025
  const result = await step.execute({
803
1026
  runId: executionContext.runId,
804
1027
  mastra: this.mastra,
805
1028
  runtimeContext,
1029
+ writableStream,
806
1030
  inputData: prevOutput,
807
1031
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
808
1032
  getInitData: () => stepResults?.input,
@@ -817,6 +1041,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
817
1041
  executionContext.suspendedPaths[step.id] = executionContext.executionPath;
818
1042
  suspended = { payload: suspendPayload };
819
1043
  },
1044
+ bail: (result2) => {
1045
+ bailed = { payload: result2 };
1046
+ },
820
1047
  resume: {
821
1048
  steps: resume?.steps?.slice(1) || [],
822
1049
  resumePayload: resume?.resumePayload,
@@ -826,7 +1053,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
826
1053
  [EMITTER_SYMBOL]: emitter,
827
1054
  engine: {
828
1055
  step: this.inngestStep
829
- }
1056
+ },
1057
+ abortSignal: abortController.signal
830
1058
  });
831
1059
  const endedAt = Date.now();
832
1060
  execResults = {
@@ -859,6 +1087,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
859
1087
  resumedAt: resume?.steps[0] === step.id ? startedAt : void 0,
860
1088
  resumePayload: resume?.steps[0] === step.id ? resume?.resumePayload : void 0
861
1089
  };
1090
+ } else if (bailed) {
1091
+ execResults = { status: "bailed", output: bailed.payload, payload: prevOutput, endedAt: Date.now(), startedAt };
862
1092
  }
863
1093
  if (execResults.status === "failed") {
864
1094
  if (executionContext.retryConfig.attempts > 0 && this.inngestAttempts < executionContext.retryConfig.attempts) {
@@ -886,8 +1116,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
886
1116
  type: "step-suspended",
887
1117
  payload: {
888
1118
  id: step.id,
889
- status: execResults.status,
890
- output: execResults.status === "success" ? execResults?.output : void 0
1119
+ ...execResults
891
1120
  }
892
1121
  });
893
1122
  } else {
@@ -895,8 +1124,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
895
1124
  type: "step-result",
896
1125
  payload: {
897
1126
  id: step.id,
898
- status: execResults.status,
899
- output: execResults.status === "success" ? execResults?.output : void 0
1127
+ ...execResults
900
1128
  }
901
1129
  });
902
1130
  await emitter.emit("watch-v2", {
@@ -957,7 +1185,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
957
1185
  resume,
958
1186
  executionContext,
959
1187
  emitter,
960
- runtimeContext
1188
+ abortController,
1189
+ runtimeContext,
1190
+ writableStream
961
1191
  }) {
962
1192
  let execResults;
963
1193
  const truthyIndexes = (await Promise.all(
@@ -966,8 +1196,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
966
1196
  try {
967
1197
  const result = await cond({
968
1198
  runId,
1199
+ workflowId,
969
1200
  mastra: this.mastra,
970
1201
  runtimeContext,
1202
+ runCount: -1,
971
1203
  inputData: prevOutput,
972
1204
  getInitData: () => stepResults?.input,
973
1205
  getStepResult: (step) => {
@@ -983,10 +1215,25 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
983
1215
  // TODO: this function shouldn't have suspend probably?
984
1216
  suspend: async (_suspendPayload) => {
985
1217
  },
1218
+ bail: () => {
1219
+ },
1220
+ abort: () => {
1221
+ abortController.abort();
1222
+ },
986
1223
  [EMITTER_SYMBOL]: emitter,
987
1224
  engine: {
988
1225
  step: this.inngestStep
989
- }
1226
+ },
1227
+ abortSignal: abortController.signal,
1228
+ writer: new ToolStream(
1229
+ {
1230
+ prefix: "step",
1231
+ callId: randomUUID(),
1232
+ name: "conditional",
1233
+ runId
1234
+ },
1235
+ writableStream
1236
+ )
990
1237
  });
991
1238
  return result ? index : null;
992
1239
  } catch (e) {
@@ -1015,7 +1262,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1015
1262
  executionSpan: executionContext.executionSpan
1016
1263
  },
1017
1264
  emitter,
1018
- runtimeContext
1265
+ abortController,
1266
+ runtimeContext,
1267
+ writableStream
1019
1268
  })
1020
1269
  )
1021
1270
  );
@@ -1041,3 +1290,5 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1041
1290
  };
1042
1291
 
1043
1292
  export { InngestExecutionEngine, InngestRun, InngestWorkflow, createStep, init, serve };
1293
+ //# sourceMappingURL=index.js.map
1294
+ //# sourceMappingURL=index.js.map