@mastra/inngest 0.0.0-tool-call-parts-20250630193309 → 0.0.0-tsconfig-compile-20250703214351
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/CHANGELOG.md +63 -3
- package/dist/_tsup-dts-rollup.d.cts +56 -16
- package/dist/_tsup-dts-rollup.d.ts +56 -16
- package/dist/index.cjs +167 -18
- package/dist/index.js +167 -18
- package/docker-compose.yaml +3 -3
- package/package.json +9 -8
- package/src/index.test.ts +839 -187
- package/src/index.ts +225 -20
- package/vitest.config.ts +6 -0
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 =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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'
|
|
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
|
}: {
|
|
@@ -464,8 +499,12 @@ export class InngestWorkflow<
|
|
|
464
499
|
return this.function;
|
|
465
500
|
}
|
|
466
501
|
this.function = this.inngest.createFunction(
|
|
467
|
-
|
|
468
|
-
|
|
502
|
+
{
|
|
503
|
+
id: `workflow.${this.id}`,
|
|
504
|
+
// @ts-ignore
|
|
505
|
+
retries: this.retryConfig?.attempts ?? 0,
|
|
506
|
+
cancelOn: [{ event: `cancel.workflow.${this.id}` }],
|
|
507
|
+
},
|
|
469
508
|
{ event: `workflow.${this.id}` },
|
|
470
509
|
async ({ event, step, attempt, publish }) => {
|
|
471
510
|
let { inputData, runId, resume } = event.data;
|
|
@@ -514,6 +553,7 @@ export class InngestWorkflow<
|
|
|
514
553
|
retryConfig: this.retryConfig,
|
|
515
554
|
runtimeContext: new RuntimeContext(), // TODO
|
|
516
555
|
resume,
|
|
556
|
+
abortController: new AbortController(),
|
|
517
557
|
});
|
|
518
558
|
|
|
519
559
|
return { result, runId };
|
|
@@ -636,7 +676,7 @@ export function createStep<
|
|
|
636
676
|
outputSchema: z.object({
|
|
637
677
|
text: z.string(),
|
|
638
678
|
}),
|
|
639
|
-
execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext }) => {
|
|
679
|
+
execute: async ({ inputData, [EMITTER_SYMBOL]: emitter, runtimeContext, abortSignal, abort }) => {
|
|
640
680
|
let streamPromise = {} as {
|
|
641
681
|
promise: Promise<string>;
|
|
642
682
|
resolve: (value: string) => void;
|
|
@@ -662,8 +702,13 @@ export function createStep<
|
|
|
662
702
|
onFinish: result => {
|
|
663
703
|
streamPromise.resolve(result.text);
|
|
664
704
|
},
|
|
705
|
+
abortSignal,
|
|
665
706
|
});
|
|
666
707
|
|
|
708
|
+
if (abortSignal.aborted) {
|
|
709
|
+
return abort();
|
|
710
|
+
}
|
|
711
|
+
|
|
667
712
|
for await (const chunk of fullStream) {
|
|
668
713
|
switch (chunk.type) {
|
|
669
714
|
case 'text-delta':
|
|
@@ -821,6 +866,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
821
866
|
delay?: number;
|
|
822
867
|
};
|
|
823
868
|
runtimeContext: RuntimeContext;
|
|
869
|
+
abortController: AbortController;
|
|
824
870
|
}): Promise<TOutput> {
|
|
825
871
|
await params.emitter.emit('watch-v2', {
|
|
826
872
|
type: 'start',
|
|
@@ -920,6 +966,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
920
966
|
resume,
|
|
921
967
|
prevOutput,
|
|
922
968
|
emitter,
|
|
969
|
+
abortController,
|
|
923
970
|
runtimeContext,
|
|
924
971
|
}: {
|
|
925
972
|
workflowId: string;
|
|
@@ -933,6 +980,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
933
980
|
};
|
|
934
981
|
prevOutput: any;
|
|
935
982
|
emitter: Emitter;
|
|
983
|
+
abortController: AbortController;
|
|
936
984
|
runtimeContext: RuntimeContext;
|
|
937
985
|
}): Promise<StepResult<any, any, any, any>> {
|
|
938
986
|
return super.executeStep({
|
|
@@ -944,12 +992,163 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
944
992
|
resume,
|
|
945
993
|
prevOutput,
|
|
946
994
|
emitter,
|
|
995
|
+
abortController,
|
|
947
996
|
runtimeContext,
|
|
948
997
|
});
|
|
949
998
|
}
|
|
950
999
|
|
|
951
|
-
async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
|
|
952
|
-
|
|
1000
|
+
// async executeSleep({ id, duration }: { id: string; duration: number }): Promise<void> {
|
|
1001
|
+
// await this.inngestStep.sleep(id, duration);
|
|
1002
|
+
// }
|
|
1003
|
+
|
|
1004
|
+
async executeSleep({
|
|
1005
|
+
workflowId,
|
|
1006
|
+
runId,
|
|
1007
|
+
entry,
|
|
1008
|
+
prevOutput,
|
|
1009
|
+
stepResults,
|
|
1010
|
+
emitter,
|
|
1011
|
+
abortController,
|
|
1012
|
+
runtimeContext,
|
|
1013
|
+
}: {
|
|
1014
|
+
workflowId: string;
|
|
1015
|
+
runId: string;
|
|
1016
|
+
serializedStepGraph: SerializedStepFlowEntry[];
|
|
1017
|
+
entry: {
|
|
1018
|
+
type: 'sleep';
|
|
1019
|
+
id: string;
|
|
1020
|
+
duration?: number;
|
|
1021
|
+
fn?: ExecuteFunction<any, any, any, any, InngestEngineType>;
|
|
1022
|
+
};
|
|
1023
|
+
prevStep: StepFlowEntry;
|
|
1024
|
+
prevOutput: any;
|
|
1025
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
1026
|
+
resume?: {
|
|
1027
|
+
steps: string[];
|
|
1028
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
1029
|
+
resumePayload: any;
|
|
1030
|
+
resumePath: number[];
|
|
1031
|
+
};
|
|
1032
|
+
executionContext: ExecutionContext;
|
|
1033
|
+
emitter: Emitter;
|
|
1034
|
+
abortController: AbortController;
|
|
1035
|
+
runtimeContext: RuntimeContext;
|
|
1036
|
+
}): Promise<void> {
|
|
1037
|
+
let { duration, fn } = entry;
|
|
1038
|
+
|
|
1039
|
+
if (fn) {
|
|
1040
|
+
duration = await this.inngestStep.run(`workflow.${workflowId}.sleep.${entry.id}`, async () => {
|
|
1041
|
+
return await fn({
|
|
1042
|
+
runId,
|
|
1043
|
+
mastra: this.mastra!,
|
|
1044
|
+
runtimeContext,
|
|
1045
|
+
inputData: prevOutput,
|
|
1046
|
+
runCount: -1,
|
|
1047
|
+
getInitData: () => stepResults?.input as any,
|
|
1048
|
+
getStepResult: (step: any) => {
|
|
1049
|
+
if (!step?.id) {
|
|
1050
|
+
return null;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
const result = stepResults[step.id];
|
|
1054
|
+
if (result?.status === 'success') {
|
|
1055
|
+
return result.output;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
return null;
|
|
1059
|
+
},
|
|
1060
|
+
|
|
1061
|
+
// TODO: this function shouldn't have suspend probably?
|
|
1062
|
+
suspend: async (_suspendPayload: any): Promise<any> => {},
|
|
1063
|
+
bail: () => {},
|
|
1064
|
+
abort: () => {
|
|
1065
|
+
abortController?.abort();
|
|
1066
|
+
},
|
|
1067
|
+
[EMITTER_SYMBOL]: emitter,
|
|
1068
|
+
engine: { step: this.inngestStep },
|
|
1069
|
+
abortSignal: abortController?.signal,
|
|
1070
|
+
});
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
await this.inngestStep.sleep(entry.id, !duration || duration < 0 ? 0 : duration);
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
async executeSleepUntil({
|
|
1078
|
+
workflowId,
|
|
1079
|
+
runId,
|
|
1080
|
+
entry,
|
|
1081
|
+
prevOutput,
|
|
1082
|
+
stepResults,
|
|
1083
|
+
emitter,
|
|
1084
|
+
abortController,
|
|
1085
|
+
runtimeContext,
|
|
1086
|
+
}: {
|
|
1087
|
+
workflowId: string;
|
|
1088
|
+
runId: string;
|
|
1089
|
+
serializedStepGraph: SerializedStepFlowEntry[];
|
|
1090
|
+
entry: {
|
|
1091
|
+
type: 'sleepUntil';
|
|
1092
|
+
id: string;
|
|
1093
|
+
date?: Date;
|
|
1094
|
+
fn?: ExecuteFunction<any, any, any, any, InngestEngineType>;
|
|
1095
|
+
};
|
|
1096
|
+
prevStep: StepFlowEntry;
|
|
1097
|
+
prevOutput: any;
|
|
1098
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
1099
|
+
resume?: {
|
|
1100
|
+
steps: string[];
|
|
1101
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
1102
|
+
resumePayload: any;
|
|
1103
|
+
resumePath: number[];
|
|
1104
|
+
};
|
|
1105
|
+
executionContext: ExecutionContext;
|
|
1106
|
+
emitter: Emitter;
|
|
1107
|
+
abortController: AbortController;
|
|
1108
|
+
runtimeContext: RuntimeContext;
|
|
1109
|
+
}): Promise<void> {
|
|
1110
|
+
let { date, fn } = entry;
|
|
1111
|
+
|
|
1112
|
+
if (fn) {
|
|
1113
|
+
date = await this.inngestStep.run(`workflow.${workflowId}.sleepUntil.${entry.id}`, async () => {
|
|
1114
|
+
return await fn({
|
|
1115
|
+
runId,
|
|
1116
|
+
mastra: this.mastra!,
|
|
1117
|
+
runtimeContext,
|
|
1118
|
+
inputData: prevOutput,
|
|
1119
|
+
runCount: -1,
|
|
1120
|
+
getInitData: () => stepResults?.input as any,
|
|
1121
|
+
getStepResult: (step: any) => {
|
|
1122
|
+
if (!step?.id) {
|
|
1123
|
+
return null;
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
const result = stepResults[step.id];
|
|
1127
|
+
if (result?.status === 'success') {
|
|
1128
|
+
return result.output;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
return null;
|
|
1132
|
+
},
|
|
1133
|
+
|
|
1134
|
+
// TODO: this function shouldn't have suspend probably?
|
|
1135
|
+
suspend: async (_suspendPayload: any): Promise<any> => {},
|
|
1136
|
+
bail: () => {},
|
|
1137
|
+
abort: () => {
|
|
1138
|
+
abortController?.abort();
|
|
1139
|
+
},
|
|
1140
|
+
[EMITTER_SYMBOL]: emitter,
|
|
1141
|
+
engine: { step: this.inngestStep },
|
|
1142
|
+
abortSignal: abortController?.signal,
|
|
1143
|
+
});
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
if (!(date instanceof Date)) {
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
await this.inngestStep.sleepUntil(entry.id, date);
|
|
953
1152
|
}
|
|
954
1153
|
|
|
955
1154
|
async executeWaitForEvent({ event, timeout }: { event: string; timeout?: number }): Promise<any> {
|
|
@@ -972,17 +1171,12 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
972
1171
|
resume,
|
|
973
1172
|
prevOutput,
|
|
974
1173
|
emitter,
|
|
1174
|
+
abortController,
|
|
975
1175
|
runtimeContext,
|
|
976
1176
|
}: {
|
|
977
1177
|
step: Step<string, any, any>;
|
|
978
1178
|
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
|
-
};
|
|
1179
|
+
executionContext: ExecutionContext;
|
|
986
1180
|
resume?: {
|
|
987
1181
|
steps: string[];
|
|
988
1182
|
resumePayload: any;
|
|
@@ -990,6 +1184,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
990
1184
|
};
|
|
991
1185
|
prevOutput: any;
|
|
992
1186
|
emitter: Emitter;
|
|
1187
|
+
abortController: AbortController;
|
|
993
1188
|
runtimeContext: RuntimeContext;
|
|
994
1189
|
}): Promise<StepResult<any, any, any, any>> {
|
|
995
1190
|
const startedAt = await this.inngestStep.run(
|
|
@@ -1023,6 +1218,8 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
1023
1218
|
payload: {
|
|
1024
1219
|
id: step.id,
|
|
1025
1220
|
status: 'running',
|
|
1221
|
+
payload: prevOutput,
|
|
1222
|
+
startedAt,
|
|
1026
1223
|
},
|
|
1027
1224
|
});
|
|
1028
1225
|
|
|
@@ -1260,6 +1457,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
1260
1457
|
engine: {
|
|
1261
1458
|
step: this.inngestStep,
|
|
1262
1459
|
},
|
|
1460
|
+
abortSignal: abortController.signal,
|
|
1263
1461
|
});
|
|
1264
1462
|
const endedAt = Date.now();
|
|
1265
1463
|
|
|
@@ -1413,6 +1611,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
1413
1611
|
resume,
|
|
1414
1612
|
executionContext,
|
|
1415
1613
|
emitter,
|
|
1614
|
+
abortController,
|
|
1416
1615
|
runtimeContext,
|
|
1417
1616
|
}: {
|
|
1418
1617
|
workflowId: string;
|
|
@@ -1434,6 +1633,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
1434
1633
|
};
|
|
1435
1634
|
executionContext: ExecutionContext;
|
|
1436
1635
|
emitter: Emitter;
|
|
1636
|
+
abortController: AbortController;
|
|
1437
1637
|
runtimeContext: RuntimeContext;
|
|
1438
1638
|
}): Promise<StepResult<any, any, any, any>> {
|
|
1439
1639
|
let execResults: any;
|
|
@@ -1465,10 +1665,14 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
1465
1665
|
// TODO: this function shouldn't have suspend probably?
|
|
1466
1666
|
suspend: async (_suspendPayload: any) => {},
|
|
1467
1667
|
bail: () => {},
|
|
1668
|
+
abort: () => {
|
|
1669
|
+
abortController.abort();
|
|
1670
|
+
},
|
|
1468
1671
|
[EMITTER_SYMBOL]: emitter,
|
|
1469
1672
|
engine: {
|
|
1470
1673
|
step: this.inngestStep,
|
|
1471
1674
|
},
|
|
1675
|
+
abortSignal: abortController.signal,
|
|
1472
1676
|
});
|
|
1473
1677
|
return result ? index : null;
|
|
1474
1678
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -1500,6 +1704,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
1500
1704
|
executionSpan: executionContext.executionSpan,
|
|
1501
1705
|
},
|
|
1502
1706
|
emitter,
|
|
1707
|
+
abortController,
|
|
1503
1708
|
runtimeContext,
|
|
1504
1709
|
}),
|
|
1505
1710
|
),
|