@mastra/inngest 0.0.0-custom-instrumentation-20250626084921 → 0.0.0-declaration-maps-20250729202623

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,
@@ -295,23 +331,26 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
295
331
  this.inngest
296
332
  );
297
333
  this.runs.set(runIdToUse, run);
298
- await this.mastra?.getStorage()?.persistWorkflowSnapshot({
299
- workflowName: this.id,
300
- runId: runIdToUse,
301
- snapshot: {
334
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
335
+ if (!workflowSnapshotInStorage) {
336
+ await this.mastra?.getStorage()?.persistWorkflowSnapshot({
337
+ workflowName: this.id,
302
338
  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
- });
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
+ }
315
354
  return run;
316
355
  }
317
356
  getFunction() {
@@ -319,8 +358,12 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
319
358
  return this.function;
320
359
  }
321
360
  this.function = this.inngest.createFunction(
322
- // @ts-ignore
323
- { 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
+ },
324
367
  { event: `workflow.${this.id}` },
325
368
  async ({ event, step, attempt, publish }) => {
326
369
  let { inputData, runId, resume } = event.data;
@@ -362,7 +405,8 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
362
405
  retryConfig: this.retryConfig,
363
406
  runtimeContext: new di.RuntimeContext(),
364
407
  // TODO
365
- resume
408
+ resume,
409
+ abortController: new AbortController()
366
410
  });
367
411
  return { result, runId };
368
412
  }
@@ -406,7 +450,7 @@ function createStep(params) {
406
450
  outputSchema: zod.z.object({
407
451
  text: zod.z.string()
408
452
  }),
409
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext }) => {
453
+ execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
410
454
  let streamPromise = {};
411
455
  streamPromise.promise = new Promise((resolve, reject) => {
412
456
  streamPromise.resolve = resolve;
@@ -426,8 +470,12 @@ function createStep(params) {
426
470
  runtimeContext,
427
471
  onFinish: (result) => {
428
472
  streamPromise.resolve(result.text);
429
- }
473
+ },
474
+ abortSignal
430
475
  });
476
+ if (abortSignal.aborted) {
477
+ return abort();
478
+ }
431
479
  for await (const chunk of fullStream) {
432
480
  switch (chunk.type) {
433
481
  case "text-delta":
@@ -602,7 +650,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
602
650
  resume,
603
651
  prevOutput,
604
652
  emitter,
605
- runtimeContext
653
+ abortController,
654
+ runtimeContext,
655
+ writableStream
606
656
  }) {
607
657
  return super.executeStep({
608
658
  workflowId,
@@ -613,11 +663,132 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
613
663
  resume,
614
664
  prevOutput,
615
665
  emitter,
616
- runtimeContext
666
+ abortController,
667
+ runtimeContext,
668
+ writableStream
617
669
  });
618
670
  }
619
- async executeSleep({ id, duration }) {
620
- 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);
621
792
  }
622
793
  async executeWaitForEvent({ event, timeout }) {
623
794
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -636,7 +807,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
636
807
  resume,
637
808
  prevOutput,
638
809
  emitter,
639
- runtimeContext
810
+ abortController,
811
+ runtimeContext,
812
+ writableStream
640
813
  }) {
641
814
  const startedAt = await this.inngestStep.run(
642
815
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
@@ -666,7 +839,10 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
666
839
  await emitter.emit("watch-v2", {
667
840
  type: "step-start",
668
841
  payload: {
669
- id: step.id
842
+ id: step.id,
843
+ status: "running",
844
+ payload: prevOutput,
845
+ startedAt: startedAt2
670
846
  }
671
847
  });
672
848
  return startedAt2;
@@ -734,7 +910,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
734
910
  type: "step-result",
735
911
  payload: {
736
912
  id: step.id,
737
- status: "failed"
913
+ status: "failed",
914
+ error: result?.error,
915
+ payload: prevOutput
738
916
  }
739
917
  });
740
918
  return { executionContext, result: { status: "failed", error: result?.error } };
@@ -766,7 +944,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
766
944
  await emitter.emit("watch-v2", {
767
945
  type: "step-suspended",
768
946
  payload: {
769
- id: step.id
947
+ id: step.id,
948
+ status: "suspended"
770
949
  }
771
950
  });
772
951
  return {
@@ -819,6 +998,14 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
819
998
  },
820
999
  eventTimestamp: Date.now()
821
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
+ });
822
1009
  await emitter.emit("watch-v2", {
823
1010
  type: "step-finish",
824
1011
  payload: {
@@ -841,6 +1028,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
841
1028
  runId: executionContext.runId,
842
1029
  mastra: this.mastra,
843
1030
  runtimeContext,
1031
+ writableStream,
844
1032
  inputData: prevOutput,
845
1033
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
846
1034
  getInitData: () => stepResults?.input,
@@ -867,7 +1055,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
867
1055
  [_constants.EMITTER_SYMBOL]: emitter,
868
1056
  engine: {
869
1057
  step: this.inngestStep
870
- }
1058
+ },
1059
+ abortSignal: abortController.signal
871
1060
  });
872
1061
  const endedAt = Date.now();
873
1062
  execResults = {
@@ -929,8 +1118,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
929
1118
  type: "step-suspended",
930
1119
  payload: {
931
1120
  id: step.id,
932
- status: execResults.status,
933
- output: execResults.status === "success" ? execResults?.output : void 0
1121
+ ...execResults
934
1122
  }
935
1123
  });
936
1124
  } else {
@@ -938,8 +1126,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
938
1126
  type: "step-result",
939
1127
  payload: {
940
1128
  id: step.id,
941
- status: execResults.status,
942
- output: execResults.status === "success" ? execResults?.output : void 0
1129
+ ...execResults
943
1130
  }
944
1131
  });
945
1132
  await emitter.emit("watch-v2", {
@@ -1000,7 +1187,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1000
1187
  resume,
1001
1188
  executionContext,
1002
1189
  emitter,
1003
- runtimeContext
1190
+ abortController,
1191
+ runtimeContext,
1192
+ writableStream
1004
1193
  }) {
1005
1194
  let execResults;
1006
1195
  const truthyIndexes = (await Promise.all(
@@ -1009,8 +1198,10 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1009
1198
  try {
1010
1199
  const result = await cond({
1011
1200
  runId,
1201
+ workflowId,
1012
1202
  mastra: this.mastra,
1013
1203
  runtimeContext,
1204
+ runCount: -1,
1014
1205
  inputData: prevOutput,
1015
1206
  getInitData: () => stepResults?.input,
1016
1207
  getStepResult: (step) => {
@@ -1028,10 +1219,23 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1028
1219
  },
1029
1220
  bail: () => {
1030
1221
  },
1222
+ abort: () => {
1223
+ abortController.abort();
1224
+ },
1031
1225
  [_constants.EMITTER_SYMBOL]: emitter,
1032
1226
  engine: {
1033
1227
  step: this.inngestStep
1034
- }
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
+ )
1035
1239
  });
1036
1240
  return result ? index : null;
1037
1241
  } catch (e) {
@@ -1060,7 +1264,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1060
1264
  executionSpan: executionContext.executionSpan
1061
1265
  },
1062
1266
  emitter,
1063
- runtimeContext
1267
+ abortController,
1268
+ runtimeContext,
1269
+ writableStream
1064
1270
  })
1065
1271
  )
1066
1272
  );
@@ -1091,3 +1297,5 @@ exports.InngestWorkflow = InngestWorkflow;
1091
1297
  exports.createStep = createStep;
1092
1298
  exports.init = init;
1093
1299
  exports.serve = serve;
1300
+ //# sourceMappingURL=index.cjs.map
1301
+ //# sourceMappingURL=index.cjs.map