@mastra/inngest 0.0.0-tool-call-parts-20250630193309 → 0.0.0-transpile-packages-20250730132657

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
  }) {
@@ -122,6 +155,7 @@ var InngestRun = class extends workflows.Run {
122
155
  data: {
123
156
  inputData: params.resumeData,
124
157
  runId: this.runId,
158
+ workflowId: this.workflowId,
125
159
  stepResults: snapshot?.context,
126
160
  resume: {
127
161
  steps,
@@ -324,8 +358,12 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
324
358
  return this.function;
325
359
  }
326
360
  this.function = this.inngest.createFunction(
327
- // @ts-ignore
328
- { 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
+ },
329
367
  { event: `workflow.${this.id}` },
330
368
  async ({ event, step, attempt, publish }) => {
331
369
  let { inputData, runId, resume } = event.data;
@@ -367,7 +405,8 @@ var InngestWorkflow = class _InngestWorkflow extends workflows.Workflow {
367
405
  retryConfig: this.retryConfig,
368
406
  runtimeContext: new di.RuntimeContext(),
369
407
  // TODO
370
- resume
408
+ resume,
409
+ abortController: new AbortController()
371
410
  });
372
411
  return { result, runId };
373
412
  }
@@ -411,7 +450,7 @@ function createStep(params) {
411
450
  outputSchema: zod.z.object({
412
451
  text: zod.z.string()
413
452
  }),
414
- execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext }) => {
453
+ execute: async ({ inputData, [_constants.EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
415
454
  let streamPromise = {};
416
455
  streamPromise.promise = new Promise((resolve, reject) => {
417
456
  streamPromise.resolve = resolve;
@@ -431,8 +470,12 @@ function createStep(params) {
431
470
  runtimeContext,
432
471
  onFinish: (result) => {
433
472
  streamPromise.resolve(result.text);
434
- }
473
+ },
474
+ abortSignal
435
475
  });
476
+ if (abortSignal.aborted) {
477
+ return abort();
478
+ }
436
479
  for await (const chunk of fullStream) {
437
480
  switch (chunk.type) {
438
481
  case "text-delta":
@@ -607,7 +650,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
607
650
  resume,
608
651
  prevOutput,
609
652
  emitter,
610
- runtimeContext
653
+ abortController,
654
+ runtimeContext,
655
+ writableStream
611
656
  }) {
612
657
  return super.executeStep({
613
658
  workflowId,
@@ -618,11 +663,132 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
618
663
  resume,
619
664
  prevOutput,
620
665
  emitter,
621
- runtimeContext
666
+ abortController,
667
+ runtimeContext,
668
+ writableStream
622
669
  });
623
670
  }
624
- async executeSleep({ id, duration }) {
625
- 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);
626
792
  }
627
793
  async executeWaitForEvent({ event, timeout }) {
628
794
  const eventData = await this.inngestStep.waitForEvent(`user-event-${event}`, {
@@ -641,7 +807,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
641
807
  resume,
642
808
  prevOutput,
643
809
  emitter,
644
- runtimeContext
810
+ abortController,
811
+ runtimeContext,
812
+ writableStream
645
813
  }) {
646
814
  const startedAt = await this.inngestStep.run(
647
815
  `workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
@@ -672,7 +840,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
672
840
  type: "step-start",
673
841
  payload: {
674
842
  id: step.id,
675
- status: "running"
843
+ status: "running",
844
+ payload: prevOutput,
845
+ startedAt: startedAt2
676
846
  }
677
847
  });
678
848
  return startedAt2;
@@ -858,6 +1028,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
858
1028
  runId: executionContext.runId,
859
1029
  mastra: this.mastra,
860
1030
  runtimeContext,
1031
+ writableStream,
861
1032
  inputData: prevOutput,
862
1033
  resumeData: resume?.steps[0] === step.id ? resume?.resumePayload : void 0,
863
1034
  getInitData: () => stepResults?.input,
@@ -884,7 +1055,8 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
884
1055
  [_constants.EMITTER_SYMBOL]: emitter,
885
1056
  engine: {
886
1057
  step: this.inngestStep
887
- }
1058
+ },
1059
+ abortSignal: abortController.signal
888
1060
  });
889
1061
  const endedAt = Date.now();
890
1062
  execResults = {
@@ -1015,7 +1187,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1015
1187
  resume,
1016
1188
  executionContext,
1017
1189
  emitter,
1018
- runtimeContext
1190
+ abortController,
1191
+ runtimeContext,
1192
+ writableStream
1019
1193
  }) {
1020
1194
  let execResults;
1021
1195
  const truthyIndexes = (await Promise.all(
@@ -1024,6 +1198,7 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1024
1198
  try {
1025
1199
  const result = await cond({
1026
1200
  runId,
1201
+ workflowId,
1027
1202
  mastra: this.mastra,
1028
1203
  runtimeContext,
1029
1204
  runCount: -1,
@@ -1044,10 +1219,23 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1044
1219
  },
1045
1220
  bail: () => {
1046
1221
  },
1222
+ abort: () => {
1223
+ abortController.abort();
1224
+ },
1047
1225
  [_constants.EMITTER_SYMBOL]: emitter,
1048
1226
  engine: {
1049
1227
  step: this.inngestStep
1050
- }
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
+ )
1051
1239
  });
1052
1240
  return result ? index : null;
1053
1241
  } catch (e) {
@@ -1076,7 +1264,9 @@ var InngestExecutionEngine = class extends workflows.DefaultExecutionEngine {
1076
1264
  executionSpan: executionContext.executionSpan
1077
1265
  },
1078
1266
  emitter,
1079
- runtimeContext
1267
+ abortController,
1268
+ runtimeContext,
1269
+ writableStream
1080
1270
  })
1081
1271
  )
1082
1272
  );