@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.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,
@@ -293,23 +329,26 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
293
329
  this.inngest
294
330
  );
295
331
  this.runs.set(runIdToUse, run);
296
- await this.mastra?.getStorage()?.persistWorkflowSnapshot({
297
- workflowName: this.id,
298
- runId: runIdToUse,
299
- snapshot: {
332
+ const workflowSnapshotInStorage = await this.getWorkflowRunExecutionResult(runIdToUse);
333
+ if (!workflowSnapshotInStorage) {
334
+ await this.mastra?.getStorage()?.persistWorkflowSnapshot({
335
+ workflowName: this.id,
300
336
  runId: runIdToUse,
301
- status: "pending",
302
- value: {},
303
- context: {},
304
- activePaths: [],
305
- serializedStepGraph: this.serializedStepGraph,
306
- suspendedPaths: {},
307
- result: void 0,
308
- error: void 0,
309
- // @ts-ignore
310
- timestamp: Date.now()
311
- }
312
- });
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
+ }
313
352
  return run;
314
353
  }
315
354
  getFunction() {
@@ -317,8 +356,12 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
317
356
  return this.function;
318
357
  }
319
358
  this.function = this.inngest.createFunction(
320
- // @ts-ignore
321
- { 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
+ },
322
365
  { event: `workflow.${this.id}` },
323
366
  async ({ event, step, attempt, publish }) => {
324
367
  let { inputData, runId, resume } = event.data;
@@ -360,7 +403,8 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
360
403
  retryConfig: this.retryConfig,
361
404
  runtimeContext: new RuntimeContext(),
362
405
  // TODO
363
- resume
406
+ resume,
407
+ abortController: new AbortController()
364
408
  });
365
409
  return { result, runId };
366
410
  }
@@ -404,7 +448,7 @@ function createStep(params) {
404
448
  outputSchema: z.object({
405
449
  text: z.string()
406
450
  }),
407
- execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext }) => {
451
+ execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
408
452
  let streamPromise = {};
409
453
  streamPromise.promise = new Promise((resolve, reject) => {
410
454
  streamPromise.resolve = resolve;
@@ -424,8 +468,12 @@ function createStep(params) {
424
468
  runtimeContext,
425
469
  onFinish: (result) => {
426
470
  streamPromise.resolve(result.text);
427
- }
471
+ },
472
+ abortSignal
428
473
  });
474
+ if (abortSignal.aborted) {
475
+ return abort();
476
+ }
429
477
  for await (const chunk of fullStream) {
430
478
  switch (chunk.type) {
431
479
  case "text-delta":
@@ -600,7 +648,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
600
648
  resume,
601
649
  prevOutput,
602
650
  emitter,
603
- runtimeContext
651
+ abortController,
652
+ runtimeContext,
653
+ writableStream
604
654
  }) {
605
655
  return super.executeStep({
606
656
  workflowId,
@@ -611,11 +661,132 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
611
661
  resume,
612
662
  prevOutput,
613
663
  emitter,
614
- runtimeContext
664
+ abortController,
665
+ runtimeContext,
666
+ writableStream
615
667
  });
616
668
  }
617
- async executeSleep({ id, duration }) {
618
- 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);
619
790
  }
620
791
  async executeWaitForEvent({ event, timeout }) {
621
792
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -634,7 +805,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
634
805
  resume,
635
806
  prevOutput,
636
807
  emitter,
637
- runtimeContext
808
+ abortController,
809
+ runtimeContext,
810
+ writableStream
638
811
  }) {
639
812
  const startedAt = await this.inngestStep.run(
640
813
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
@@ -664,7 +837,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
664
837
  await emitter.emit("watch-v2", {
665
838
  type: "step-start",
666
839
  payload: {
667
- id: step.id
840
+ id: step.id,
841
+ status: "running",
842
+ payload: prevOutput,
843
+ startedAt: startedAt2
668
844
  }
669
845
  });
670
846
  return startedAt2;
@@ -732,7 +908,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
732
908
  type: "step-result",
733
909
  payload: {
734
910
  id: step.id,
735
- status: "failed"
911
+ status: "failed",
912
+ error: result?.error,
913
+ payload: prevOutput
736
914
  }
737
915
  });
738
916
  return { executionContext, result: { status: "failed", error: result?.error } };
@@ -764,7 +942,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
764
942
  await emitter.emit("watch-v2", {
765
943
  type: "step-suspended",
766
944
  payload: {
767
- id: step.id
945
+ id: step.id,
946
+ status: "suspended"
768
947
  }
769
948
  });
770
949
  return {
@@ -817,6 +996,14 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
817
996
  },
818
997
  eventTimestamp: Date.now()
819
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
+ });
820
1007
  await emitter.emit("watch-v2", {
821
1008
  type: "step-finish",
822
1009
  payload: {
@@ -839,6 +1026,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
839
1026
  runId: executionContext.runId,
840
1027
  mastra: this.mastra,
841
1028
  runtimeContext,
1029
+ writableStream,
842
1030
  inputData: prevOutput,
843
1031
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
844
1032
  getInitData: () => stepResults?.input,
@@ -865,7 +1053,8 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
865
1053
  [EMITTER_SYMBOL]: emitter,
866
1054
  engine: {
867
1055
  step: this.inngestStep
868
- }
1056
+ },
1057
+ abortSignal: abortController.signal
869
1058
  });
870
1059
  const endedAt = Date.now();
871
1060
  execResults = {
@@ -927,8 +1116,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
927
1116
  type: "step-suspended",
928
1117
  payload: {
929
1118
  id: step.id,
930
- status: execResults.status,
931
- output: execResults.status === "success" ? execResults?.output : void 0
1119
+ ...execResults
932
1120
  }
933
1121
  });
934
1122
  } else {
@@ -936,8 +1124,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
936
1124
  type: "step-result",
937
1125
  payload: {
938
1126
  id: step.id,
939
- status: execResults.status,
940
- output: execResults.status === "success" ? execResults?.output : void 0
1127
+ ...execResults
941
1128
  }
942
1129
  });
943
1130
  await emitter.emit("watch-v2", {
@@ -998,7 +1185,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
998
1185
  resume,
999
1186
  executionContext,
1000
1187
  emitter,
1001
- runtimeContext
1188
+ abortController,
1189
+ runtimeContext,
1190
+ writableStream
1002
1191
  }) {
1003
1192
  let execResults;
1004
1193
  const truthyIndexes = (await Promise.all(
@@ -1007,8 +1196,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1007
1196
  try {
1008
1197
  const result = await cond({
1009
1198
  runId,
1199
+ workflowId,
1010
1200
  mastra: this.mastra,
1011
1201
  runtimeContext,
1202
+ runCount: -1,
1012
1203
  inputData: prevOutput,
1013
1204
  getInitData: () => stepResults?.input,
1014
1205
  getStepResult: (step) => {
@@ -1026,10 +1217,23 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1026
1217
  },
1027
1218
  bail: () => {
1028
1219
  },
1220
+ abort: () => {
1221
+ abortController.abort();
1222
+ },
1029
1223
  [EMITTER_SYMBOL]: emitter,
1030
1224
  engine: {
1031
1225
  step: this.inngestStep
1032
- }
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
+ )
1033
1237
  });
1034
1238
  return result ? index : null;
1035
1239
  } catch (e) {
@@ -1058,7 +1262,9 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1058
1262
  executionSpan: executionContext.executionSpan
1059
1263
  },
1060
1264
  emitter,
1061
- runtimeContext
1265
+ abortController,
1266
+ runtimeContext,
1267
+ writableStream
1062
1268
  })
1063
1269
  )
1064
1270
  );
@@ -1084,3 +1290,5 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
1084
1290
  };
1085
1291
 
1086
1292
  export { InngestExecutionEngine, InngestRun, InngestWorkflow, createStep, init, serve };
1293
+ //# sourceMappingURL=index.js.map
1294
+ //# sourceMappingURL=index.js.map