@mastra/inngest 1.1.1 → 1.1.2
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 +22 -0
- package/LICENSE.md +15 -0
- package/dist/execution-engine.d.ts.map +1 -1
- package/dist/index.cjs +274 -89
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +275 -90
- package/dist/index.js.map +1 -1
- package/dist/run.d.ts +8 -2
- package/dist/run.d.ts.map +1 -1
- package/dist/workflow.d.ts.map +1 -1
- package/package.json +14 -13
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { Processor } from '@mastra/core/processors';
|
|
|
4
4
|
import { ProcessorStepOutputSchema, ProcessorStepSchema } from '@mastra/core/processors';
|
|
5
5
|
import type { OutputSchema } from '@mastra/core/stream';
|
|
6
6
|
import type { ToolExecutionContext } from '@mastra/core/tools';
|
|
7
|
-
import { Tool } from '@mastra/core/tools';
|
|
7
|
+
import { Tool, createTool } from '@mastra/core/tools';
|
|
8
8
|
import type { DynamicArgument } from '@mastra/core/types';
|
|
9
9
|
import type { Step, AgentStepOptions, StepParams, StepMetadata } from '@mastra/core/workflows';
|
|
10
10
|
import { Workflow } from '@mastra/core/workflows';
|
|
@@ -82,6 +82,7 @@ export declare function createStep<TProcessorId extends string>(processor: (Proc
|
|
|
82
82
|
*/
|
|
83
83
|
export declare function createStep<TStepId extends string, TStateSchema extends z.ZodTypeAny | undefined, TInputSchema extends z.ZodTypeAny, TOutputSchema extends z.ZodTypeAny, TResumeSchema extends z.ZodTypeAny | undefined = undefined, TSuspendSchema extends z.ZodTypeAny | undefined = undefined>(params: StepParams<TStepId, TStateSchema, TInputSchema, TOutputSchema, TResumeSchema, TSuspendSchema>): Step<TStepId, TStateSchema extends z.ZodTypeAny ? z.infer<TStateSchema> : unknown, z.infer<TInputSchema>, z.infer<TOutputSchema>, TResumeSchema extends z.ZodTypeAny ? z.infer<TResumeSchema> : unknown, TSuspendSchema extends z.ZodTypeAny ? z.infer<TSuspendSchema> : unknown, InngestEngineType>;
|
|
84
84
|
export declare function init(inngest: Inngest): {
|
|
85
|
+
createTool: typeof createTool;
|
|
85
86
|
createWorkflow<TWorkflowId extends string = string, TState = any, TInput = any, TOutput = any, TSteps extends Step<string, any, any, any, any, any, InngestEngineType>[] = Step<string, any, any, any, any, any, InngestEngineType, unknown>[]>(params: InngestWorkflowConfig<TWorkflowId, TState, TInput, TOutput, TSteps>): InngestWorkflow<InngestEngineType, TSteps, TWorkflowId, TState, TInput, TOutput, TInput>;
|
|
86
87
|
createStep: typeof createStep;
|
|
87
88
|
cloneStep<TStepId extends string>(step: Step<TStepId, any, any, any, any, any, InngestEngineType>, opts: {
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAyB,MAAM,oBAAoB,CAAC;AAGlE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAIxD,OAAO,KAAK,EAAE,SAAS,EAAuB,MAAM,yBAAyB,CAAC;AAC9E,OAAO,EAAmB,yBAAyB,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC1G,OAAO,KAAK,EAAa,YAAY,EAAwB,MAAM,qBAAqB,CAAC;AACzF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAyB,MAAM,oBAAoB,CAAC;AAGlE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAIxD,OAAO,KAAK,EAAE,SAAS,EA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CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,iBAAiB,CAAC,EAAE,gFASjE,qBAAqB,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC;;cAOnE,OAAO,SAAS,MAAM,QACxB,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,iBAAiB,CAAC,QACzD;QAAE,EAAE,EAAE,OAAO,CAAA;KAAE,GACpB,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,iBAAiB,CAAC;kBAiB1D,WAAW,SAAS,MAAM,WAC1B,MAAM,YACN,MAAM,YACN,OAAO,YACP,MAAM,SAAS,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,iBAAiB,CAAC,EAAE,wEASzE,KAAK,qBAEK,QAAQ,CAAC,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,QAC/E;QAAE,EAAE,EAAE,WAAW,CAAA;KAAE,GACxB,QAAQ,CAAC,iBAAiB,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC;EAetF"}
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { MessageList, Agent, TripWire } from '@mastra/core/agent';
|
|
|
2
2
|
import { getErrorFromUnknown, MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';
|
|
3
3
|
import { EntityType, SpanType } from '@mastra/core/observability';
|
|
4
4
|
import { ProcessorStepOutputSchema, ProcessorStepSchema, ProcessorRunner } from '@mastra/core/processors';
|
|
5
|
-
import { Tool } from '@mastra/core/tools';
|
|
5
|
+
import { createTool, Tool } from '@mastra/core/tools';
|
|
6
6
|
import { DefaultExecutionEngine, createTimeTravelExecutionParams, Run, hydrateSerializedStepErrors, Workflow } from '@mastra/core/workflows';
|
|
7
7
|
import { PUBSUB_SYMBOL, STREAM_FORMAT_SYMBOL } from '@mastra/core/workflows/_constants';
|
|
8
8
|
import { z } from 'zod';
|
|
@@ -127,9 +127,10 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
127
127
|
* object is finally JSON.stringify'd, our error's toJSON() is called.
|
|
128
128
|
*/
|
|
129
129
|
async wrapDurableOperation(operationId, operationFn) {
|
|
130
|
-
|
|
130
|
+
const result = await this.inngestStep.run(operationId, async () => {
|
|
131
131
|
try {
|
|
132
|
-
|
|
132
|
+
const fnResult = await operationFn();
|
|
133
|
+
return fnResult;
|
|
133
134
|
} catch (e) {
|
|
134
135
|
const errorInstance = getErrorFromUnknown(e, {
|
|
135
136
|
serializeStack: false,
|
|
@@ -144,6 +145,7 @@ var InngestExecutionEngine = class extends DefaultExecutionEngine {
|
|
|
144
145
|
});
|
|
145
146
|
}
|
|
146
147
|
});
|
|
148
|
+
return result;
|
|
147
149
|
}
|
|
148
150
|
/**
|
|
149
151
|
* Provide Inngest step primitive in engine context
|
|
@@ -670,58 +672,158 @@ var InngestRun = class extends Run {
|
|
|
670
672
|
}
|
|
671
673
|
throw new NonRetriableError(`Failed to get runs after ${maxRetries} attempts: ${lastError?.message}`);
|
|
672
674
|
}
|
|
675
|
+
/**
|
|
676
|
+
* Get run output using hybrid approach: realtime subscription + polling fallback.
|
|
677
|
+
* Resolves as soon as either method detects completion.
|
|
678
|
+
*/
|
|
673
679
|
async getRunOutput(eventId, maxWaitMs = 3e5) {
|
|
674
|
-
const startTime = Date.now();
|
|
675
680
|
const storage = this.#mastra?.getStorage();
|
|
676
681
|
const workflowsStore = await storage?.getStore("workflows");
|
|
677
|
-
|
|
678
|
-
let
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
if (
|
|
683
|
-
|
|
682
|
+
return new Promise((resolve, reject) => {
|
|
683
|
+
let resolved = false;
|
|
684
|
+
let unsubscribe = null;
|
|
685
|
+
let pollTimeoutId = null;
|
|
686
|
+
const cleanup = () => {
|
|
687
|
+
if (unsubscribe) {
|
|
688
|
+
try {
|
|
689
|
+
unsubscribe();
|
|
690
|
+
} catch {
|
|
691
|
+
}
|
|
684
692
|
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
);
|
|
688
|
-
}
|
|
689
|
-
if (runs?.[0]?.status === "Completed" && runs?.[0]?.event_id === eventId) {
|
|
690
|
-
if (runs?.[0]?.output?.runId !== void 0) {
|
|
691
|
-
return runs[0];
|
|
693
|
+
if (pollTimeoutId) {
|
|
694
|
+
clearTimeout(pollTimeoutId);
|
|
692
695
|
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
runId: this.runId
|
|
700
|
-
});
|
|
701
|
-
if (snapshot?.context) {
|
|
702
|
-
snapshot.context = hydrateSerializedStepErrors(snapshot.context);
|
|
696
|
+
};
|
|
697
|
+
const handleResult = (result, _source) => {
|
|
698
|
+
if (!resolved) {
|
|
699
|
+
resolved = true;
|
|
700
|
+
cleanup();
|
|
701
|
+
resolve(result);
|
|
703
702
|
}
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
703
|
+
};
|
|
704
|
+
const handleError = (error, _source) => {
|
|
705
|
+
if (!resolved) {
|
|
706
|
+
resolved = true;
|
|
707
|
+
cleanup();
|
|
708
|
+
reject(error);
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
let realtimeStreamPromise = null;
|
|
712
|
+
const startRealtimeSubscription = async () => {
|
|
713
|
+
try {
|
|
714
|
+
realtimeStreamPromise = subscribe(
|
|
715
|
+
{
|
|
716
|
+
channel: `workflow:${this.workflowId}:${this.runId}`,
|
|
717
|
+
topics: ["watch"],
|
|
718
|
+
app: this.inngest
|
|
719
|
+
},
|
|
720
|
+
async (message) => {
|
|
721
|
+
if (resolved) return;
|
|
722
|
+
const event = message.data;
|
|
723
|
+
if (event?.type === "workflow-finish") {
|
|
724
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
725
|
+
workflowName: this.workflowId,
|
|
726
|
+
runId: this.runId
|
|
727
|
+
});
|
|
728
|
+
if (snapshot?.context) {
|
|
729
|
+
snapshot.context = hydrateSerializedStepErrors(snapshot.context);
|
|
730
|
+
}
|
|
731
|
+
const result = {
|
|
732
|
+
output: {
|
|
733
|
+
result: {
|
|
734
|
+
steps: snapshot?.context,
|
|
735
|
+
status: event.payload.status,
|
|
736
|
+
result: event.payload.result,
|
|
737
|
+
error: event.payload.error ? getErrorFromUnknown(event.payload.error, { serializeStack: false }) : void 0
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
handleResult(result);
|
|
742
|
+
}
|
|
711
743
|
}
|
|
744
|
+
);
|
|
745
|
+
unsubscribe = () => {
|
|
746
|
+
realtimeStreamPromise?.then((stream) => stream.cancel().catch(() => {
|
|
747
|
+
})).catch(() => {
|
|
748
|
+
});
|
|
749
|
+
};
|
|
750
|
+
await realtimeStreamPromise;
|
|
751
|
+
} catch {
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
const startPolling = async () => {
|
|
755
|
+
const startTime = Date.now();
|
|
756
|
+
const poll = async () => {
|
|
757
|
+
if (resolved) {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
if (Date.now() - startTime >= maxWaitMs) {
|
|
761
|
+
handleError(new NonRetriableError(`Workflow did not complete within ${maxWaitMs}ms`));
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
try {
|
|
765
|
+
const runs = await this.getRuns(eventId);
|
|
766
|
+
const run = runs?.find((r) => r.event_id === eventId);
|
|
767
|
+
if (run?.status === "Completed") {
|
|
768
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
769
|
+
workflowName: this.workflowId,
|
|
770
|
+
runId: this.runId
|
|
771
|
+
});
|
|
772
|
+
if (snapshot?.context) {
|
|
773
|
+
snapshot.context = hydrateSerializedStepErrors(snapshot.context);
|
|
774
|
+
}
|
|
775
|
+
handleResult({ output: { result: { steps: snapshot?.context, status: "success" } } });
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
if (run?.status === "Failed") {
|
|
779
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
780
|
+
workflowName: this.workflowId,
|
|
781
|
+
runId: this.runId
|
|
782
|
+
});
|
|
783
|
+
if (snapshot?.context) {
|
|
784
|
+
snapshot.context = hydrateSerializedStepErrors(snapshot.context);
|
|
785
|
+
}
|
|
786
|
+
handleResult(
|
|
787
|
+
{
|
|
788
|
+
output: {
|
|
789
|
+
result: {
|
|
790
|
+
steps: snapshot?.context,
|
|
791
|
+
status: "failed",
|
|
792
|
+
error: getErrorFromUnknown(run?.output?.cause?.error, { serializeStack: false })
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
if (run?.status === "Cancelled") {
|
|
799
|
+
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
800
|
+
workflowName: this.workflowId,
|
|
801
|
+
runId: this.runId
|
|
802
|
+
});
|
|
803
|
+
if (snapshot?.context) {
|
|
804
|
+
snapshot.context = hydrateSerializedStepErrors(snapshot.context);
|
|
805
|
+
}
|
|
806
|
+
handleResult(
|
|
807
|
+
{ output: { result: { steps: snapshot?.context, status: "canceled" } } });
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
pollTimeoutId = setTimeout(poll, 200 + Math.random() * 200);
|
|
811
|
+
} catch (error) {
|
|
812
|
+
if (error instanceof NonRetriableError) {
|
|
813
|
+
handleError(error);
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
handleError(
|
|
817
|
+
new NonRetriableError(
|
|
818
|
+
`Failed to poll workflow status: ${error instanceof Error ? error.message : String(error)}`
|
|
819
|
+
));
|
|
712
820
|
}
|
|
713
821
|
};
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
});
|
|
720
|
-
return { output: { result: { steps: snapshot?.context, status: "canceled" } } };
|
|
721
|
-
}
|
|
722
|
-
await new Promise((resolve) => setTimeout(resolve, 1e3 + Math.random() * 1e3));
|
|
723
|
-
}
|
|
724
|
-
throw new NonRetriableError(`Workflow did not complete within ${maxWaitMs}ms`);
|
|
822
|
+
void poll();
|
|
823
|
+
};
|
|
824
|
+
void startRealtimeSubscription();
|
|
825
|
+
void startPolling();
|
|
826
|
+
});
|
|
725
827
|
}
|
|
726
828
|
async cancel() {
|
|
727
829
|
const storage = this.#mastra?.getStorage();
|
|
@@ -829,8 +931,9 @@ var InngestRun = class extends Run {
|
|
|
829
931
|
});
|
|
830
932
|
const inputDataToUse = await this._validateInput(inputData);
|
|
831
933
|
const initialStateToUse = await this._validateInitialState(initialState ?? {});
|
|
934
|
+
const eventName = `workflow.${this.workflowId}`;
|
|
832
935
|
const eventOutput = await this.inngest.send({
|
|
833
|
-
name:
|
|
936
|
+
name: eventName,
|
|
834
937
|
data: {
|
|
835
938
|
inputData: inputDataToUse,
|
|
836
939
|
initialState: initialStateToUse,
|
|
@@ -868,19 +971,23 @@ var InngestRun = class extends Run {
|
|
|
868
971
|
}
|
|
869
972
|
async _resume(params) {
|
|
870
973
|
const storage = this.#mastra?.getStorage();
|
|
871
|
-
let steps = [];
|
|
872
|
-
if (typeof params.step === "string") {
|
|
873
|
-
steps = params.step.split(".");
|
|
874
|
-
} else {
|
|
875
|
-
steps = (Array.isArray(params.step) ? params.step : [params.step]).map(
|
|
876
|
-
(step) => typeof step === "string" ? step : step?.id
|
|
877
|
-
);
|
|
878
|
-
}
|
|
879
974
|
const workflowsStore = await storage?.getStore("workflows");
|
|
880
975
|
const snapshot = await workflowsStore?.loadWorkflowSnapshot({
|
|
881
976
|
workflowName: this.workflowId,
|
|
882
977
|
runId: this.runId
|
|
883
978
|
});
|
|
979
|
+
const snapshotResumeLabel = params.label ? snapshot?.resumeLabels?.[params.label] : void 0;
|
|
980
|
+
const stepParam = snapshotResumeLabel?.stepId ?? params.step;
|
|
981
|
+
let steps = [];
|
|
982
|
+
if (stepParam) {
|
|
983
|
+
if (typeof stepParam === "string") {
|
|
984
|
+
steps = stepParam.split(".");
|
|
985
|
+
} else {
|
|
986
|
+
steps = (Array.isArray(stepParam) ? stepParam : [stepParam]).map(
|
|
987
|
+
(step) => typeof step === "string" ? step : step?.id
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
884
991
|
const suspendedStep = this.workflowSteps[steps?.[0] ?? ""];
|
|
885
992
|
const resumeDataToUse = await this._validateResumeData(params.resumeData, suspendedStep);
|
|
886
993
|
const persistedRequestContext = snapshot?.requestContext ?? {};
|
|
@@ -1467,46 +1574,117 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1467
1574
|
} catch (error) {
|
|
1468
1575
|
throw error;
|
|
1469
1576
|
}
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1577
|
+
let finalizeError;
|
|
1578
|
+
let finalizeErrored = false;
|
|
1579
|
+
try {
|
|
1580
|
+
await step.run(`workflow.${this.id}.finalize`, async () => {
|
|
1581
|
+
if (result.status !== "paused") {
|
|
1582
|
+
await engine.invokeLifecycleCallbacksInternal({
|
|
1583
|
+
status: result.status,
|
|
1584
|
+
result: "result" in result ? result.result : void 0,
|
|
1585
|
+
error: "error" in result ? result.error : void 0,
|
|
1586
|
+
steps: result.steps,
|
|
1587
|
+
tripwire: "tripwire" in result ? result.tripwire : void 0,
|
|
1588
|
+
runId,
|
|
1589
|
+
workflowId: this.id,
|
|
1590
|
+
resourceId,
|
|
1591
|
+
input: inputData,
|
|
1592
|
+
requestContext,
|
|
1593
|
+
state: result.state ?? initialState ?? {}
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
if (workflowSpanData) {
|
|
1597
|
+
const observability = mastra?.observability?.getSelectedInstance({ requestContext });
|
|
1598
|
+
if (observability) {
|
|
1599
|
+
const workflowSpan = observability.rebuildSpan(workflowSpanData);
|
|
1600
|
+
if (result.status === "failed") {
|
|
1601
|
+
workflowSpan.error({
|
|
1602
|
+
error: result.error instanceof Error ? result.error : new Error(String(result.error)),
|
|
1603
|
+
attributes: { status: "failed" }
|
|
1604
|
+
});
|
|
1605
|
+
} else {
|
|
1606
|
+
workflowSpan.end({
|
|
1607
|
+
output: result.status === "success" ? result.result : void 0,
|
|
1608
|
+
attributes: { status: result.status }
|
|
1609
|
+
});
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
const shouldPersistFinalSnapshot = this.options.shouldPersistSnapshot({
|
|
1614
|
+
workflowStatus: result.status,
|
|
1615
|
+
stepResults: result.steps
|
|
1484
1616
|
});
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1617
|
+
if (shouldPersistFinalSnapshot) {
|
|
1618
|
+
const workflowsStore = await mastra?.getStorage()?.getStore("workflows");
|
|
1619
|
+
if (workflowsStore) {
|
|
1620
|
+
let existingSnapshot;
|
|
1621
|
+
if (result.status === "suspended") {
|
|
1622
|
+
existingSnapshot = await workflowsStore.loadWorkflowSnapshot({
|
|
1623
|
+
workflowName: this.id,
|
|
1624
|
+
runId
|
|
1625
|
+
}) ?? void 0;
|
|
1626
|
+
}
|
|
1627
|
+
await workflowsStore.persistWorkflowSnapshot({
|
|
1628
|
+
workflowName: this.id,
|
|
1629
|
+
runId,
|
|
1630
|
+
resourceId,
|
|
1631
|
+
snapshot: {
|
|
1632
|
+
runId,
|
|
1633
|
+
status: result.status,
|
|
1634
|
+
value: result.state ?? initialState ?? {},
|
|
1635
|
+
context: toSnapshotContext(result.steps),
|
|
1636
|
+
activePaths: [],
|
|
1637
|
+
activeStepsPath: {},
|
|
1638
|
+
serializedStepGraph: this.serializedStepGraph,
|
|
1639
|
+
suspendedPaths: existingSnapshot?.suspendedPaths ?? {},
|
|
1640
|
+
waitingPaths: {},
|
|
1641
|
+
resumeLabels: existingSnapshot?.resumeLabels ?? result.resumeLabels ?? {},
|
|
1642
|
+
result: result.status === "success" ? toSnapshotResult(result.result) : void 0,
|
|
1643
|
+
error: result.status === "failed" ? result.error : void 0,
|
|
1644
|
+
timestamp: Date.now()
|
|
1645
|
+
}
|
|
1499
1646
|
});
|
|
1500
1647
|
}
|
|
1501
1648
|
}
|
|
1649
|
+
try {
|
|
1650
|
+
await pubsub.publish(`workflow.events.v2.${runId}`, {
|
|
1651
|
+
type: "watch",
|
|
1652
|
+
runId,
|
|
1653
|
+
data: {
|
|
1654
|
+
type: "workflow-finish",
|
|
1655
|
+
payload: {
|
|
1656
|
+
status: result.status,
|
|
1657
|
+
result: result.status === "success" ? result.result : void 0,
|
|
1658
|
+
error: result.status === "failed" ? result.error : void 0
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
});
|
|
1662
|
+
} catch (publishError) {
|
|
1663
|
+
this.logger.debug?.("Failed to publish workflow-finish event:", publishError);
|
|
1664
|
+
}
|
|
1665
|
+
if (result.status === "failed") {
|
|
1666
|
+
throw new NonRetriableError(`Workflow failed`, {
|
|
1667
|
+
cause: result
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
return result;
|
|
1671
|
+
});
|
|
1672
|
+
} catch (error) {
|
|
1673
|
+
finalizeErrored = true;
|
|
1674
|
+
finalizeError = error;
|
|
1675
|
+
} finally {
|
|
1676
|
+
const observability = mastra?.observability?.getSelectedInstance({ requestContext });
|
|
1677
|
+
if (observability) {
|
|
1678
|
+
try {
|
|
1679
|
+
await observability.flush();
|
|
1680
|
+
} catch (flushError) {
|
|
1681
|
+
this.logger.debug?.("Failed to flush observability:", flushError);
|
|
1682
|
+
}
|
|
1502
1683
|
}
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
}
|
|
1508
|
-
return result;
|
|
1509
|
-
});
|
|
1684
|
+
}
|
|
1685
|
+
if (finalizeErrored) {
|
|
1686
|
+
throw finalizeError;
|
|
1687
|
+
}
|
|
1510
1688
|
return { result, runId };
|
|
1511
1689
|
}
|
|
1512
1690
|
);
|
|
@@ -1533,6 +1711,12 @@ var InngestWorkflow = class _InngestWorkflow extends Workflow {
|
|
|
1533
1711
|
];
|
|
1534
1712
|
}
|
|
1535
1713
|
};
|
|
1714
|
+
function toSnapshotContext(steps) {
|
|
1715
|
+
return steps;
|
|
1716
|
+
}
|
|
1717
|
+
function toSnapshotResult(output) {
|
|
1718
|
+
return output;
|
|
1719
|
+
}
|
|
1536
1720
|
function prepareServeOptions({ mastra, inngest, functions: userFunctions = [], registerOptions }) {
|
|
1537
1721
|
const wfs = mastra.listWorkflows();
|
|
1538
1722
|
const workflowFunctions = Array.from(
|
|
@@ -2214,6 +2398,7 @@ function createStepFromProcessor(processor) {
|
|
|
2214
2398
|
}
|
|
2215
2399
|
function init(inngest) {
|
|
2216
2400
|
return {
|
|
2401
|
+
createTool,
|
|
2217
2402
|
createWorkflow(params) {
|
|
2218
2403
|
return new InngestWorkflow(
|
|
2219
2404
|
params,
|