@mastra/inngest 0.0.0-redis-cloud-transporter-20250508194049 → 0.0.0-taofeeqInngest-20250603090617
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 +223 -2
- package/dist/_tsup-dts-rollup.d.cts +39 -34
- package/dist/_tsup-dts-rollup.d.ts +39 -34
- package/dist/index.cjs +22 -10
- package/dist/index.js +18 -6
- package/package.json +10 -7
- package/src/index.test.ts +46 -53
- package/src/index.ts +44 -29
package/src/index.ts
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
2
|
import { subscribe } from '@inngest/realtime';
|
|
3
|
-
import type { Mastra, WorkflowRun } from '@mastra/core';
|
|
3
|
+
import type { Mastra, WorkflowRun, WorkflowRuns } from '@mastra/core';
|
|
4
4
|
import { RuntimeContext } from '@mastra/core/di';
|
|
5
|
-
import {
|
|
5
|
+
import { Workflow, createStep, Run, DefaultExecutionEngine, cloneStep } from '@mastra/core/workflows';
|
|
6
6
|
import type {
|
|
7
7
|
ExecuteFunction,
|
|
8
8
|
ExecutionContext,
|
|
9
9
|
ExecutionEngine,
|
|
10
10
|
ExecutionGraph,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
NewWorkflowConfig,
|
|
11
|
+
Step,
|
|
12
|
+
WorkflowConfig,
|
|
14
13
|
StepFlowEntry,
|
|
15
14
|
StepResult,
|
|
16
15
|
WorkflowResult,
|
|
17
|
-
|
|
16
|
+
SerializedStepFlowEntry,
|
|
17
|
+
} from '@mastra/core/workflows';
|
|
18
|
+
import { EMITTER_SYMBOL } from '@mastra/core/workflows/_constants';
|
|
18
19
|
import type { Span } from '@opentelemetry/api';
|
|
19
20
|
import type { Inngest, BaseContext } from 'inngest';
|
|
20
21
|
import { serve as inngestServe } from 'inngest/hono';
|
|
21
22
|
import type { z } from 'zod';
|
|
22
23
|
|
|
23
24
|
export function serve({ mastra, inngest }: { mastra: Mastra; inngest: Inngest }): ReturnType<typeof inngestServe> {
|
|
24
|
-
const wfs = mastra.
|
|
25
|
+
const wfs = mastra.getWorkflows();
|
|
25
26
|
const functions = Object.values(wfs).flatMap(wf => {
|
|
26
27
|
if (wf instanceof InngestWorkflow) {
|
|
27
28
|
wf.__registerMastra(mastra);
|
|
@@ -36,11 +37,12 @@ export function serve({ mastra, inngest }: { mastra: Mastra; inngest: Inngest })
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
export class InngestRun<
|
|
39
|
-
TSteps extends
|
|
40
|
+
TSteps extends Step<string, any, any>[] = Step<string, any, any>[],
|
|
40
41
|
TInput extends z.ZodType<any> = z.ZodType<any>,
|
|
41
42
|
TOutput extends z.ZodType<any> = z.ZodType<any>,
|
|
42
43
|
> extends Run<TSteps, TInput, TOutput> {
|
|
43
44
|
private inngest: Inngest;
|
|
45
|
+
serializedStepGraph: SerializedStepFlowEntry[];
|
|
44
46
|
#mastra: Mastra;
|
|
45
47
|
|
|
46
48
|
constructor(
|
|
@@ -49,6 +51,7 @@ export class InngestRun<
|
|
|
49
51
|
runId: string;
|
|
50
52
|
executionEngine: ExecutionEngine;
|
|
51
53
|
executionGraph: ExecutionGraph;
|
|
54
|
+
serializedStepGraph: SerializedStepFlowEntry[];
|
|
52
55
|
mastra?: Mastra;
|
|
53
56
|
retryConfig?: {
|
|
54
57
|
attempts?: number;
|
|
@@ -60,11 +63,12 @@ export class InngestRun<
|
|
|
60
63
|
) {
|
|
61
64
|
super(params);
|
|
62
65
|
this.inngest = inngest;
|
|
66
|
+
this.serializedStepGraph = params.serializedStepGraph;
|
|
63
67
|
this.#mastra = params.mastra!;
|
|
64
68
|
}
|
|
65
69
|
|
|
66
70
|
async getRuns(eventId: string) {
|
|
67
|
-
const response = await fetch(`${this.inngest.apiBaseUrl}/v1/events/${eventId}/runs`, {
|
|
71
|
+
const response = await fetch(`${this.inngest.apiBaseUrl ?? 'https://api.inngest.com'}/v1/events/${eventId}/runs`, {
|
|
68
72
|
headers: {
|
|
69
73
|
Authorization: `Bearer ${process.env.INNGEST_SIGNING_KEY}`,
|
|
70
74
|
},
|
|
@@ -96,6 +100,7 @@ export class InngestRun<
|
|
|
96
100
|
runId: this.runId,
|
|
97
101
|
snapshot: {
|
|
98
102
|
runId: this.runId,
|
|
103
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
99
104
|
value: {},
|
|
100
105
|
context: {} as any,
|
|
101
106
|
activePaths: [],
|
|
@@ -196,18 +201,18 @@ export class InngestRun<
|
|
|
196
201
|
}
|
|
197
202
|
|
|
198
203
|
export class InngestWorkflow<
|
|
199
|
-
TSteps extends
|
|
204
|
+
TSteps extends Step<string, any, any>[] = Step<string, any, any>[],
|
|
200
205
|
TWorkflowId extends string = string,
|
|
201
206
|
TInput extends z.ZodType<any> = z.ZodType<any>,
|
|
202
207
|
TOutput extends z.ZodType<any> = z.ZodType<any>,
|
|
203
208
|
TPrevSchema extends z.ZodType<any> = TInput,
|
|
204
|
-
> extends
|
|
209
|
+
> extends Workflow<TSteps, TWorkflowId, TInput, TOutput, TPrevSchema> {
|
|
205
210
|
#mastra: Mastra;
|
|
206
211
|
public inngest: Inngest;
|
|
207
212
|
|
|
208
213
|
private function: ReturnType<Inngest['createFunction']> | undefined;
|
|
209
214
|
|
|
210
|
-
constructor(params:
|
|
215
|
+
constructor(params: WorkflowConfig<TWorkflowId, TInput, TOutput, TSteps>, inngest: Inngest) {
|
|
211
216
|
super(params);
|
|
212
217
|
this.#mastra = params.mastra!;
|
|
213
218
|
this.inngest = inngest;
|
|
@@ -226,7 +231,7 @@ export class InngestWorkflow<
|
|
|
226
231
|
return { runs: [], total: 0 };
|
|
227
232
|
}
|
|
228
233
|
|
|
229
|
-
return storage.getWorkflowRuns({ workflowName: this.id, ...(args ?? {}) });
|
|
234
|
+
return storage.getWorkflowRuns({ workflowName: this.id, ...(args ?? {}) }) as unknown as WorkflowRuns;
|
|
230
235
|
}
|
|
231
236
|
|
|
232
237
|
async getWorkflowRunById(runId: string): Promise<WorkflowRun | null> {
|
|
@@ -235,7 +240,7 @@ export class InngestWorkflow<
|
|
|
235
240
|
this.logger.debug('Cannot get workflow runs. Mastra engine is not initialized');
|
|
236
241
|
return null;
|
|
237
242
|
}
|
|
238
|
-
const run = await storage.getWorkflowRunById({ runId, workflowName: this.id });
|
|
243
|
+
const run = (await storage.getWorkflowRunById({ runId, workflowName: this.id })) as unknown as WorkflowRun;
|
|
239
244
|
|
|
240
245
|
return (
|
|
241
246
|
run ??
|
|
@@ -278,6 +283,7 @@ export class InngestWorkflow<
|
|
|
278
283
|
runId: runIdToUse,
|
|
279
284
|
executionEngine: this.executionEngine,
|
|
280
285
|
executionGraph: this.executionGraph,
|
|
286
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
281
287
|
mastra: this.#mastra,
|
|
282
288
|
retryConfig: this.retryConfig,
|
|
283
289
|
cleanup: () => this.runs.delete(runIdToUse),
|
|
@@ -329,6 +335,7 @@ export class InngestWorkflow<
|
|
|
329
335
|
workflowId: this.id,
|
|
330
336
|
runId,
|
|
331
337
|
graph: this.executionGraph,
|
|
338
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
332
339
|
input: inputData,
|
|
333
340
|
emitter,
|
|
334
341
|
retryConfig: this.retryConfig,
|
|
@@ -394,7 +401,7 @@ export function init(inngest: Inngest) {
|
|
|
394
401
|
TInput extends z.ZodType<any> = z.ZodType<any>,
|
|
395
402
|
TOutput extends z.ZodType<any> = z.ZodType<any>,
|
|
396
403
|
TSteps extends Step<string, any, any>[] = Step<string, any, any>[],
|
|
397
|
-
>(params:
|
|
404
|
+
>(params: WorkflowConfig<TWorkflowId, TInput, TOutput, TSteps>) {
|
|
398
405
|
return new InngestWorkflow(params, inngest);
|
|
399
406
|
},
|
|
400
407
|
createStep,
|
|
@@ -416,8 +423,8 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
416
423
|
protected async fmtReturnValue<TOutput>(
|
|
417
424
|
executionSpan: Span | undefined,
|
|
418
425
|
emitter: { emit: (event: string, data: any) => Promise<void> },
|
|
419
|
-
stepResults: Record<string, StepResult<any>>,
|
|
420
|
-
lastOutput: StepResult<any>,
|
|
426
|
+
stepResults: Record<string, StepResult<any, any, any, any>>,
|
|
427
|
+
lastOutput: StepResult<any, any, any, any>,
|
|
421
428
|
error?: Error | string,
|
|
422
429
|
): Promise<TOutput> {
|
|
423
430
|
const base: any = {
|
|
@@ -501,7 +508,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
501
508
|
workflowId: string;
|
|
502
509
|
runId: string;
|
|
503
510
|
step: Step<string, any, any>;
|
|
504
|
-
stepResults: Record<string, StepResult<any>>;
|
|
511
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
505
512
|
executionContext: ExecutionContext;
|
|
506
513
|
resume?: {
|
|
507
514
|
steps: string[];
|
|
@@ -510,7 +517,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
510
517
|
prevOutput: any;
|
|
511
518
|
emitter: { emit: (event: string, data: any) => Promise<void> };
|
|
512
519
|
runtimeContext: RuntimeContext;
|
|
513
|
-
}): Promise<StepResult<any>> {
|
|
520
|
+
}): Promise<StepResult<any, any, any, any>> {
|
|
514
521
|
return super.executeStep({
|
|
515
522
|
workflowId,
|
|
516
523
|
runId,
|
|
@@ -534,7 +541,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
534
541
|
runtimeContext,
|
|
535
542
|
}: {
|
|
536
543
|
step: Step<string, any, any>;
|
|
537
|
-
stepResults: Record<string, StepResult<any>>;
|
|
544
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
538
545
|
executionContext: {
|
|
539
546
|
workflowId: string;
|
|
540
547
|
runId: string;
|
|
@@ -550,7 +557,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
550
557
|
prevOutput: any;
|
|
551
558
|
emitter: { emit: (event: string, data: any) => Promise<void> };
|
|
552
559
|
runtimeContext: RuntimeContext;
|
|
553
|
-
}): Promise<StepResult<any>> {
|
|
560
|
+
}): Promise<StepResult<any, any, any, any>> {
|
|
554
561
|
await this.inngestStep.run(
|
|
555
562
|
`workflow.${executionContext.workflowId}.run.${executionContext.runId}.step.${step.id}.running_ev`,
|
|
556
563
|
async () => {
|
|
@@ -644,7 +651,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
644
651
|
return { executionContext, result: { status: 'failed', error: result?.error } };
|
|
645
652
|
} else if (result.status === 'suspended') {
|
|
646
653
|
const suspendedSteps = Object.entries(result.steps).filter(([_stepName, stepResult]) => {
|
|
647
|
-
const stepRes: StepResult<any> = stepResult as StepResult<any>;
|
|
654
|
+
const stepRes: StepResult<any, any, any, any> = stepResult as StepResult<any, any, any, any>;
|
|
648
655
|
return stepRes?.status === 'suspended';
|
|
649
656
|
});
|
|
650
657
|
|
|
@@ -732,7 +739,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
732
739
|
);
|
|
733
740
|
|
|
734
741
|
Object.assign(executionContext, res.executionContext);
|
|
735
|
-
return res.result as StepResult<any>;
|
|
742
|
+
return res.result as StepResult<any, any, any, any>;
|
|
736
743
|
}
|
|
737
744
|
|
|
738
745
|
const stepRes = await this.inngestStep.run(`workflow.${executionContext.workflowId}.step.${step.id}`, async () => {
|
|
@@ -740,6 +747,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
740
747
|
let suspended: { payload: any } | undefined;
|
|
741
748
|
try {
|
|
742
749
|
const result = await step.execute({
|
|
750
|
+
runId: executionContext.runId,
|
|
743
751
|
mastra: this.mastra!,
|
|
744
752
|
runtimeContext,
|
|
745
753
|
inputData: prevOutput,
|
|
@@ -816,10 +824,12 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
816
824
|
runId,
|
|
817
825
|
stepResults,
|
|
818
826
|
executionContext,
|
|
827
|
+
serializedStepGraph,
|
|
819
828
|
}: {
|
|
820
829
|
workflowId: string;
|
|
821
830
|
runId: string;
|
|
822
|
-
stepResults: Record<string, StepResult<any>>;
|
|
831
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
832
|
+
serializedStepGraph: SerializedStepFlowEntry[];
|
|
823
833
|
executionContext: ExecutionContext;
|
|
824
834
|
}) {
|
|
825
835
|
await this.inngestStep.run(
|
|
@@ -834,6 +844,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
834
844
|
context: stepResults as any,
|
|
835
845
|
activePaths: [],
|
|
836
846
|
suspendedPaths: executionContext.suspendedPaths,
|
|
847
|
+
serializedStepGraph,
|
|
837
848
|
// @ts-ignore
|
|
838
849
|
timestamp: Date.now(),
|
|
839
850
|
},
|
|
@@ -849,6 +860,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
849
860
|
prevOutput,
|
|
850
861
|
prevStep,
|
|
851
862
|
stepResults,
|
|
863
|
+
serializedStepGraph,
|
|
852
864
|
resume,
|
|
853
865
|
executionContext,
|
|
854
866
|
emitter,
|
|
@@ -858,18 +870,19 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
858
870
|
runId: string;
|
|
859
871
|
entry: { type: 'conditional'; steps: StepFlowEntry[]; conditions: ExecuteFunction<any, any, any, any>[] };
|
|
860
872
|
prevStep: StepFlowEntry;
|
|
873
|
+
serializedStepGraph: SerializedStepFlowEntry[];
|
|
861
874
|
prevOutput: any;
|
|
862
|
-
stepResults: Record<string, StepResult<any>>;
|
|
875
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
863
876
|
resume?: {
|
|
864
877
|
steps: string[];
|
|
865
|
-
stepResults: Record<string, StepResult<any>>;
|
|
878
|
+
stepResults: Record<string, StepResult<any, any, any, any>>;
|
|
866
879
|
resumePayload: any;
|
|
867
880
|
resumePath: number[];
|
|
868
881
|
};
|
|
869
882
|
executionContext: ExecutionContext;
|
|
870
883
|
emitter: { emit: (event: string, data: any) => Promise<void> };
|
|
871
884
|
runtimeContext: RuntimeContext;
|
|
872
|
-
}): Promise<StepResult<any>> {
|
|
885
|
+
}): Promise<StepResult<any, any, any, any>> {
|
|
873
886
|
let execResults: any;
|
|
874
887
|
const truthyIndexes = (
|
|
875
888
|
await Promise.all(
|
|
@@ -877,6 +890,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
877
890
|
this.inngestStep.run(`workflow.${workflowId}.conditional.${index}`, async () => {
|
|
878
891
|
try {
|
|
879
892
|
const result = await cond({
|
|
893
|
+
runId,
|
|
880
894
|
mastra: this.mastra!,
|
|
881
895
|
runtimeContext,
|
|
882
896
|
inputData: prevOutput,
|
|
@@ -896,7 +910,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
896
910
|
|
|
897
911
|
// TODO: this function shouldn't have suspend probably?
|
|
898
912
|
suspend: async (_suspendPayload: any) => {},
|
|
899
|
-
emitter,
|
|
913
|
+
[EMITTER_SYMBOL]: emitter,
|
|
900
914
|
});
|
|
901
915
|
return result ? index : null;
|
|
902
916
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -909,7 +923,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
909
923
|
).filter((index: any): index is number => index !== null);
|
|
910
924
|
|
|
911
925
|
const stepsToRun = entry.steps.filter((_, index) => truthyIndexes.includes(index));
|
|
912
|
-
const results: StepResult<any>[] = await Promise.all(
|
|
926
|
+
const results: StepResult<any, any, any, any>[] = await Promise.all(
|
|
913
927
|
stepsToRun.map((step, index) =>
|
|
914
928
|
this.executeEntry({
|
|
915
929
|
workflowId,
|
|
@@ -918,6 +932,7 @@ export class InngestExecutionEngine extends DefaultExecutionEngine {
|
|
|
918
932
|
prevStep,
|
|
919
933
|
stepResults,
|
|
920
934
|
resume,
|
|
935
|
+
serializedStepGraph,
|
|
921
936
|
executionContext: {
|
|
922
937
|
workflowId,
|
|
923
938
|
runId,
|