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

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/src/index.ts CHANGED
@@ -33,13 +33,17 @@ export type InngestEngineType = {
33
33
 
34
34
  export function serve({ mastra, inngest }: { mastra: Mastra; inngest: Inngest }): ReturnType<typeof inngestServe> {
35
35
  const wfs = mastra.getWorkflows();
36
- const functions = Object.values(wfs).flatMap(wf => {
37
- if (wf instanceof InngestWorkflow) {
38
- wf.__registerMastra(mastra);
39
- return wf.getFunctions();
40
- }
41
- return [];
42
- });
36
+ const functions = Array.from(
37
+ new Set(
38
+ Object.values(wfs).flatMap(wf => {
39
+ if (wf instanceof InngestWorkflow) {
40
+ wf.__registerMastra(mastra);
41
+ return wf.getFunctions();
42
+ }
43
+ return [];
44
+ }),
45
+ ),
46
+ );
43
47
  return inngestServe({
44
48
  client: inngest,
45
49
  functions,
@@ -94,8 +98,15 @@ export class InngestRun<
94
98
  while (runs?.[0]?.status !== 'Completed' || runs?.[0]?.event_id !== eventId) {
95
99
  await new Promise(resolve => setTimeout(resolve, 1000));
96
100
  runs = await this.getRuns(eventId);
97
- if (runs?.[0]?.status === 'Failed' || runs?.[0]?.status === 'Cancelled') {
101
+ if (runs?.[0]?.status === 'Failed') {
102
+ console.log('run', runs?.[0]);
98
103
  throw new Error(`Function run ${runs?.[0]?.status}`);
104
+ } else if (runs?.[0]?.status === 'Cancelled') {
105
+ const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
106
+ workflowName: this.workflowId,
107
+ runId: this.runId,
108
+ });
109
+ return { output: { result: { steps: snapshot?.context, status: 'canceled' } } };
99
110
  }
100
111
  }
101
112
  return runs?.[0];
@@ -108,6 +119,30 @@ export class InngestRun<
108
119
  });
109
120
  }
110
121
 
122
+ async cancel() {
123
+ await this.inngest.send({
124
+ name: `cancel.workflow.${this.workflowId}`,
125
+ data: {
126
+ runId: this.runId,
127
+ },
128
+ });
129
+
130
+ const snapshot = await this.#mastra?.storage?.loadWorkflowSnapshot({
131
+ workflowName: this.workflowId,
132
+ runId: this.runId,
133
+ });
134
+ if (snapshot) {
135
+ await this.#mastra?.storage?.persistWorkflowSnapshot({
136
+ workflowName: this.workflowId,
137
+ runId: this.runId,
138
+ snapshot: {
139
+ ...snapshot,
140
+ status: 'canceled' as any,
141
+ },
142
+ });
143
+ }
144
+ }
145
+
111
146
  async start({
112
147
  inputData,
113
148
  }: {
@@ -196,6 +231,7 @@ export class InngestRun<
196
231
  data: {
197
232
  inputData: params.resumeData,
198
233
  runId: this.runId,
234
+ workflowId: this.workflowId,
199
235
  stepResults: snapshot?.context as any,
200
236
  resume: {
201
237
  steps,
@@ -464,8 +500,12 @@ export class InngestWorkflow<
464
500
  return this.function;
465
501
  }
466
502
  this.function = this.inngest.createFunction(
467
- // @ts-ignore
468
- { id: `workflow.${this.id}`, retries: this.retryConfig?.attempts ?? 0 },
503
+ {
504
+ id: `workflow.${this.id}`,
505
+ // @ts-ignore
506
+ retries: this.retryConfig?.attempts ?? 0,
507
+ cancelOn: [{ event: `cancel.workflow.${this.id}` }],
508
+ },
469
509
  { event: `workflow.${this.id}` },
470
510
  async ({ event, step, attempt, publish }) => {
471
511
  let { inputData, runId, resume } = event.data;
@@ -514,6 +554,7 @@ export class InngestWorkflow<
514
554
  retryConfig: this.retryConfig,
515
555
  runtimeContext: new RuntimeContext(), // TODO
516
556
  resume,
557
+ abortController: new AbortController(),
517
558
  });
518
559
 
519
560
  return { result, runId };
@@ -636,7 +677,7 @@ export function createStep<
636
677
  outputSchema: z.object({
637
678
  text: z.string(),
638
679
  }),
639
- execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext }) => {
680
+ execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
640
681
  let streamPromise = {} as {
641
682
  promise: Promise<string>;
642
683
  resolve: (value: string) => void;
@@ -662,8 +703,13 @@ export function createStep<
662
703
  onFinish: result => {
663
704
  streamPromise.resolve(result.text);
664
705
  },
706
+ abortSignal,
665
707
  });
666
708
 
709
+ if (abortSignal.aborted) {
710
+ return abort();
711
+ }
712
+
667
713
  for await (const chunk of fullStream) {
668
714
  switch (chunk.type) {
669
715
  case 'text-delta':
@@ -821,6 +867,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
821
867
  delay?: number;
822
868
  };
823
869
  runtimeContext: RuntimeContext;
870
+ abortController: AbortController;
824
871
  }): Promise<TOutput> {
825
872
  await params.emitter.emit('watch-v2', {
826
873
  type: 'start',
@@ -920,6 +967,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
920
967
  resume,
921
968
  prevOutput,
922
969
  emitter,
970
+ abortController,
923
971
  runtimeContext,
924
972
  }: {
925
973
  workflowId: string;
@@ -933,6 +981,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
933
981
  };
934
982
  prevOutput: any;
935
983
  emitter: Emitter;
984
+ abortController: AbortController;
936
985
  runtimeContext: RuntimeContext;
937
986
  }): Promise<StepResult<any, any, any, any>> {
938
987
  return super.executeStep({
@@ -944,12 +993,165 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
944
993
  resume,
945
994
  prevOutput,
946
995
  emitter,
996
+ abortController,
947
997
  runtimeContext,
948
998
  });
949
999
  }
950
1000
 
951
- async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
952
- await this.inngestStep.sleep(id, duration);
1001
+ // async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
1002
+ // await this.inngestStep.sleep(id, duration);
1003
+ // }
1004
+
1005
+ async executeSleep({
1006
+ workflowId,
1007
+ runId,
1008
+ entry,
1009
+ prevOutput,
1010
+ stepResults,
1011
+ emitter,
1012
+ abortController,
1013
+ runtimeContext,
1014
+ }: {
1015
+ workflowId: string;
1016
+ runId: string;
1017
+ serializedStepGraph: SerializedStepFlowEntry[];
1018
+ entry: {
1019
+ type: 'sleep';
1020
+ id: string;
1021
+ duration?: number;
1022
+ fn?: ExecuteFunction<any, any, any, any, InngestEngineType>;
1023
+ };
1024
+ prevStep: StepFlowEntry;
1025
+ prevOutput: any;
1026
+ stepResults: Record<string, StepResult<any, any, any, any>>;
1027
+ resume?: {
1028
+ steps: string[];
1029
+ stepResults: Record<string, StepResult<any, any, any, any>>;
1030
+ resumePayload: any;
1031
+ resumePath: number[];
1032
+ };
1033
+ executionContext: ExecutionContext;
1034
+ emitter: Emitter;
1035
+ abortController: AbortController;
1036
+ runtimeContext: RuntimeContext;
1037
+ }): Promise<void> {
1038
+ let { duration, fn } = entry;
1039
+
1040
+ if (fn) {
1041
+ duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
1042
+ return await fn({
1043
+ runId,
1044
+ workflowId,
1045
+ mastra: this.mastra!,
1046
+ runtimeContext,
1047
+ inputData: prevOutput,
1048
+ runCount: -1,
1049
+ getInitData: () => stepResults?.input as any,
1050
+ getStepResult: (step: any) => {
1051
+ if (!step?.id) {
1052
+ return null;
1053
+ }
1054
+
1055
+ const result = stepResults[step.id];
1056
+ if (result?.status === 'success') {
1057
+ return result.output;
1058
+ }
1059
+
1060
+ return null;
1061
+ },
1062
+
1063
+ // TODO: this function shouldn't have suspend probably?
1064
+ suspend: async (_suspendPayload: any): Promise<any> => {},
1065
+ bail: () => {},
1066
+ abort: () => {
1067
+ abortController?.abort();
1068
+ },
1069
+ [EMITTER_SYMBOL]: emitter,
1070
+ engine: { step: this.inngestStep },
1071
+ abortSignal: abortController?.signal,
1072
+ });
1073
+ });
1074
+ }
1075
+
1076
+ await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
1077
+ }
1078
+
1079
+ async executeSleepUntil({
1080
+ workflowId,
1081
+ runId,
1082
+ entry,
1083
+ prevOutput,
1084
+ stepResults,
1085
+ emitter,
1086
+ abortController,
1087
+ runtimeContext,
1088
+ }: {
1089
+ workflowId: string;
1090
+ runId: string;
1091
+ serializedStepGraph: SerializedStepFlowEntry[];
1092
+ entry: {
1093
+ type: 'sleepUntil';
1094
+ id: string;
1095
+ date?: Date;
1096
+ fn?: ExecuteFunction<any, any, any, any, InngestEngineType>;
1097
+ };
1098
+ prevStep: StepFlowEntry;
1099
+ prevOutput: any;
1100
+ stepResults: Record<string, StepResult<any, any, any, any>>;
1101
+ resume?: {
1102
+ steps: string[];
1103
+ stepResults: Record<string, StepResult<any, any, any, any>>;
1104
+ resumePayload: any;
1105
+ resumePath: number[];
1106
+ };
1107
+ executionContext: ExecutionContext;
1108
+ emitter: Emitter;
1109
+ abortController: AbortController;
1110
+ runtimeContext: RuntimeContext;
1111
+ }): Promise<void> {
1112
+ let { date, fn } = entry;
1113
+
1114
+ if (fn) {
1115
+ date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
1116
+ return await fn({
1117
+ runId,
1118
+ workflowId,
1119
+ mastra: this.mastra!,
1120
+ runtimeContext,
1121
+ inputData: prevOutput,
1122
+ runCount: -1,
1123
+ getInitData: () => stepResults?.input as any,
1124
+ getStepResult: (step: any) => {
1125
+ if (!step?.id) {
1126
+ return null;
1127
+ }
1128
+
1129
+ const result = stepResults[step.id];
1130
+ if (result?.status === 'success') {
1131
+ return result.output;
1132
+ }
1133
+
1134
+ return null;
1135
+ },
1136
+
1137
+ // TODO: this function shouldn't have suspend probably?
1138
+ suspend: async (_suspendPayload: any): Promise<any> => {},
1139
+ bail: () => {},
1140
+ abort: () => {
1141
+ abortController?.abort();
1142
+ },
1143
+ [EMITTER_SYMBOL]: emitter,
1144
+ engine: { step: this.inngestStep },
1145
+ abortSignal: abortController?.signal,
1146
+ });
1147
+ });
1148
+ }
1149
+
1150
+ if (!(date instanceof Date)) {
1151
+ return;
1152
+ }
1153
+
1154
+ await this.inngestStep.sleepUntil(entry.id, date);
953
1155
  }
954
1156
 
955
1157
  async executeWaitForEvent({ event, timeout }: { event: string; timeout?: number }): Promise<any> {
@@ -972,17 +1174,12 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
972
1174
  resume,
973
1175
  prevOutput,
974
1176
  emitter,
1177
+ abortController,
975
1178
  runtimeContext,
976
1179
  }: {
977
1180
  step: Step<string, any, any>;
978
1181
  stepResults: Record<string, StepResult<any, any, any, any>>;
979
- executionContext: {
980
- workflowId: string;
981
- runId: string;
982
- executionPath: number[];
983
- suspendedPaths: Record<string, number[]>;
984
- retryConfig: { attempts: number; delay: number };
985
- };
1182
+ executionContext: ExecutionContext;
986
1183
  resume?: {
987
1184
  steps: string[];
988
1185
  resumePayload: any;
@@ -990,6 +1187,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
990
1187
  };
991
1188
  prevOutput: any;
992
1189
  emitter: Emitter;
1190
+ abortController: AbortController;
993
1191
  runtimeContext: RuntimeContext;
994
1192
  }): Promise<StepResult<any, any, any, any>> {
995
1193
  const startedAt = await this.inngestStep.run(
@@ -1023,6 +1221,8 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1023
1221
  payload: {
1024
1222
  id: step.id,
1025
1223
  status: 'running',
1224
+ payload: prevOutput,
1225
+ startedAt,
1026
1226
  },
1027
1227
  });
1028
1228
 
@@ -1260,6 +1460,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1260
1460
  engine: {
1261
1461
  step: this.inngestStep,
1262
1462
  },
1463
+ abortSignal: abortController.signal,
1263
1464
  });
1264
1465
  const endedAt = Date.now();
1265
1466
 
@@ -1377,6 +1578,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1377
1578
  workflowStatus: 'success' | 'failed' | 'suspended' | 'running';
1378
1579
  result?: Record<string, any>;
1379
1580
  error?: string | Error;
1581
+ runtimeContext: RuntimeContext;
1380
1582
  }) {
1381
1583
  await this.inngestStep.run(
1382
1584
  `workflow.${workflowId}.run.${runId}.path.${JSON.stringify(executionContext.executionPath)}.stepUpdate`,
@@ -1413,6 +1615,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1413
1615
  resume,
1414
1616
  executionContext,
1415
1617
  emitter,
1618
+ abortController,
1416
1619
  runtimeContext,
1417
1620
  }: {
1418
1621
  workflowId: string;
@@ -1434,6 +1637,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1434
1637
  };
1435
1638
  executionContext: ExecutionContext;
1436
1639
  emitter: Emitter;
1640
+ abortController: AbortController;
1437
1641
  runtimeContext: RuntimeContext;
1438
1642
  }): Promise<StepResult<any, any, any, any>> {
1439
1643
  let execResults: any;
@@ -1444,6 +1648,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1444
1648
  try {
1445
1649
  const result = await cond({
1446
1650
  runId,
1651
+ workflowId,
1447
1652
  mastra: this.mastra!,
1448
1653
  runtimeContext,
1449
1654
  runCount: -1,
@@ -1465,10 +1670,14 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1465
1670
  // TODO: this function shouldn't have suspend probably?
1466
1671
  suspend: async (_suspendPayload: any) => {},
1467
1672
  bail: () => {},
1673
+ abort: () => {
1674
+ abortController.abort();
1675
+ },
1468
1676
  [EMITTER_SYMBOL]: emitter,
1469
1677
  engine: {
1470
1678
  step: this.inngestStep,
1471
1679
  },
1680
+ abortSignal: abortController.signal,
1472
1681
  });
1473
1682
  return result ? index : null;
1474
1683
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -1500,6 +1709,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
1500
1709
  executionSpan: executionContext.executionSpan,
1501
1710
  },
1502
1711
  emitter,
1712
+ abortController,
1503
1713
  runtimeContext,
1504
1714
  }),
1505
1715
  ),
package/vitest.config.ts CHANGED
@@ -4,5 +4,11 @@ export default defineConfig({
4
4
  test: {
5
5
  globals: true,
6
6
  include: ['src/**/*.test.ts'],
7
+ pool: 'threads',
8
+ poolOptions: {
9
+ threads: {
10
+ singleThread: true,
11
+ },
12
+ },
7
13
  },
8
14
  });